apply FSL license
[apps/core/preloaded/app-selector.git] / app-selector-util.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7
8  *     http://www.tizenopensource.org/license
9
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18
19
20 #include <Ecore.h>
21 #include <Ecore_X.h>
22
23 #include <sys/shm.h>
24
25 #include <X11/Xatom.h>
26 #include <X11/Xlib.h>
27 #include <X11/Xutil.h>
28 #include <X11/extensions/Xcomposite.h>
29 #include <X11/extensions/XShm.h>
30
31 #include "app-selector.h"
32 #include "app-selector-util.h"
33
34
35 static pid_t __get_win_pid(Display *d, Window win);
36 static int __tm_get_win_with_pid(Display *d, Window * win, pid_t pid);
37 static Window __find_top_win(Window win);
38
39
40 static pid_t __get_win_pid(Display *d, Window win)
41 {
42         int ret;
43         pid_t pid;
44
45         Atom a_pid;
46         Atom a_type;
47         int format;
48         unsigned long nitems;
49         unsigned long bytes_after;
50         unsigned char *prop_ret;
51
52         if (!d)
53                 return -1;
54
55         a_pid = XInternAtom(d, "_NET_WM_PID", True);
56         if (a_pid == 0)
57                 return -1;
58
59         ret = XGetWindowProperty(d, win, a_pid, 0, 1, False, XA_CARDINAL,
60                                  &a_type, &format, &nitems, &bytes_after,
61                                  &prop_ret);
62
63         if (ret != 0 || prop_ret == NULL)
64                 return -1;
65
66         pid = *(unsigned long *)prop_ret;
67
68         XFree(prop_ret);
69         return pid;
70 }
71
72 static int __tm_get_win_with_pid(Display *d, Window * win, pid_t pid)
73 {
74         int ret;
75         pid_t p;
76         int n;
77         Window root, parent, *child;
78
79         p = __get_win_pid(d, *win);
80         if (p == pid)
81                 return 1;
82
83         ret = XQueryTree(d, *win, &root, &parent, &child, &n);
84         if (ret) {
85                 int i;
86                 int found = 0;
87
88                 for (i = 0; i < n; i++) {
89                         found = __tm_get_win_with_pid(d, &child[i], pid);
90                         if (found) {
91                                 *win = child[i];
92                                 break;
93                         }
94                 }
95
96                 XFree(child);
97                 if (found)
98                         return 1;
99         }
100         return 0;
101 }
102
103 static Window __find_top_win(Window win)
104 {
105         Window root, parent, *childw, return_win;
106         unsigned int nchild;
107         if (!XQueryTree(ecore_x_display_get(),
108             win, &root, &parent, &childw, &nchild)) {
109 /*              DBG_PRT("XQueryTree failed.\n");*/
110                 return win;
111         }
112         if (parent == root) {
113 /*              DBG_PRT("toplevel window: 0x%x \n", (unsigned int)win);*/
114                 return_win = win;
115                 XFree(childw);
116                 return return_win;
117         } else {
118 /*              DBG_PRT("window: 0x%x \n",(unsigned int)win);*/
119                 XFree(childw);
120                 return __find_top_win(parent);
121         }
122 }
123
124 int set_transient(Ecore_X_Display *display, Ecore_X_Window win,
125                   Ecore_X_Window prev_win)
126 {
127         int ret = 0;
128
129         ret = XSetTransientForHint(display, win, prev_win);
130
131         return ret;
132 }