4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
31 #include <X11/Xatom.h>
33 #include "appcore-internal.h"
36 static Atom a_active_win;
38 static pid_t __get_win_pid(Display *d, Window win)
46 unsigned long bytes_after;
47 unsigned char *prop_ret;
48 XWindowAttributes attr;
50 _retv_if(d == NULL || !a_pid, -1);
52 if (!XGetWindowAttributes(d, win, &attr))
55 if (attr.override_redirect || attr.class == InputOnly)
59 r = XGetWindowProperty(d, win, a_pid, 0, 1, False, XA_CARDINAL,
60 &a_type, &format, &nitems, &bytes_after,
62 if (r != Success || prop_ret == NULL)
65 if (a_type == XA_CARDINAL && format == 32)
66 pid = *(unsigned long *)prop_ret;
75 static int __find_win(Display *d, Window *win, pid_t pid)
80 Window root, parent, *child;
82 p = __get_win_pid(d, *win);
86 r = XQueryTree(d, *win, &root, &parent, &child, &n);
91 for (i = 0; i < n; i++) {
92 found = __find_win(d, &child[i], pid);
107 static void __add_win_list(Eina_List **list, Window *win)
115 EINA_LIST_FOREACH(*list, l, w) {
120 *list = eina_list_append(*list, *win);
123 static void __foreach_win(Eina_List **list, Display *d, Window *win, pid_t pid)
129 Window root, parent, *child;
131 p = __get_win_pid(d, *win);
133 __add_win_list(list, win);
135 r = XQueryTree(d, *win, &root, &parent, &child, &n);
137 for (i = 0; i < n; i++) {
138 __foreach_win(list, d, &child[i], pid);
144 static int __iconify_win(Eina_List *list, Display *d)
152 EINA_LIST_FOREACH(list, l, w) {
153 XIconifyWindow(d, w, 0);
159 static int __raise_win(Display *d, Window win)
165 a_active_win = XInternAtom(d, "_NET_ACTIVE_WINDOW", False);
167 root = XDefaultRootWindow(d);
169 xev.xclient.type = ClientMessage;
170 xev.xclient.display = d;
171 xev.xclient.window = win;
172 xev.xclient.message_type = a_active_win;
173 xev.xclient.format = 32;
174 xev.xclient.data.l[0] = 1;
175 xev.xclient.data.l[1] = CurrentTime;
176 xev.xclient.data.l[2] = 0;
177 xev.xclient.data.l[3] = 0;
178 xev.xclient.data.l[4] = 0;
179 XSendEvent(d, root, False,
180 SubstructureRedirectMask | SubstructureNotifyMask, &xev);
185 EXPORT_API int x_raise_win(pid_t pid)
200 errno = ESRCH; /* No such process */
204 d = XOpenDisplay(NULL);
205 _retv_if(d == NULL, -1);
207 win = XDefaultRootWindow(d);
210 a_pid = XInternAtom(d, "_NET_WM_PID", True);
212 found = __find_win(d, &win, pid);
219 r = __raise_win(d, win);
226 EXPORT_API int x_pause_win(pid_t pid)
231 Eina_List *list_win = NULL;
244 d = XOpenDisplay(NULL);
245 _retv_if(d == NULL, -1);
247 win = XDefaultRootWindow(d);
249 a_pid = XInternAtom(d, "_NET_WM_PID", True);
251 __foreach_win(&list_win, d, &win, pid);
252 r = __iconify_win(list_win, d);
257 eina_list_free(list_win);