b871dae343181ceb7c7458a874d05403fd6bbd2e
[platform/framework/web/wrt.git] / src / wrt-launchpad-daemon / launchpad_src / util_x.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <signal.h>
23
24 #include <X11/Xlib.h>
25 #include <X11/Xatom.h>
26
27 #include "simple_util.h"
28
29 static Atom a_pid;
30 static int (*x_old_error) (Display *, XErrorEvent *);
31
32
33 static pid_t __get_win_pid(Display *d, Window win);
34 static int __find_win(Display *d, Window *win, pid_t pid);
35 static int __raise_win(Display *d, Window win);
36 static int __cb_x_error(Display *disp, XErrorEvent *ev);
37
38 static pid_t __get_win_pid(Display *d, Window win)
39 {
40         int r;
41         pid_t pid;
42
43         Atom a_type;
44         int format;
45         unsigned long nitems;
46         unsigned long bytes_after;
47         unsigned char *prop_ret;
48
49         retv_if(d == NULL || !a_pid, -1);
50
51         prop_ret = NULL;
52         r = XGetWindowProperty(d, win, a_pid, 0, 1, False, XA_CARDINAL,
53                         &a_type, &format, &nitems, &bytes_after, &prop_ret);
54         if (r != Success || prop_ret == NULL)
55                 return -1;
56
57         if (a_type == XA_CARDINAL && format == 32)
58                 pid = *(unsigned long *)prop_ret;
59         else
60                 pid = -1;
61
62         XFree(prop_ret);
63
64         return pid;
65 }
66
67 static int __find_win(Display *d, Window *win, pid_t pid)
68 {
69         int r;
70         pid_t p;
71         unsigned int n;
72         Window root, parent, *child;
73
74         p = __get_win_pid(d, *win);
75         if (p == pid)
76                 return 1;
77
78         r = XQueryTree(d, *win, &root, &parent, &child, &n);
79         if (r) {
80                 int i;
81                 int found = 0;
82
83                 for (i = 0; i < n; i++) {
84                         found = __find_win(d, &child[i], pid);
85                         if (found) {
86                                 *win = child[i];
87                                 break;
88                         }
89                 }
90                 XFree(child);
91
92                 if (found)
93                         return 1;
94         }
95
96         return 0;
97 }
98
99 static int __raise_win(Display *d, Window win)
100 {
101         XWindowAttributes attr;
102         attr.map_state = IsUnmapped;
103
104         XMapRaised(d, win);
105
106         XGetWindowAttributes(d, win, &attr);
107
108         if (attr.map_state == IsUnmapped)
109                 _D("unmapped");
110         else if (attr.map_state == IsUnviewable)
111                 _D("unviewable");
112         else if (attr.map_state == IsViewable)
113                 _D("viewable");
114
115         retv_if(attr.map_state != IsViewable, -1);
116
117         XSetInputFocus(d, win, RevertToPointerRoot, CurrentTime);
118
119         return 0;
120 }
121
122 int x_util_raise_win(pid_t pid)
123 {
124         int r;
125         int found;
126         Display *d;
127         Window win;
128
129         if (pid < 1)
130                 return -1;
131
132         r = kill(pid, 0);
133         if (r == -1)
134                 return -1;
135
136         d = XOpenDisplay(NULL);
137         retv_if(d == NULL, -1);
138
139         win = XDefaultRootWindow(d);
140
141         if (!a_pid)
142                 a_pid = XInternAtom(d, "X_CLIENT_PID", True);
143
144         found = __find_win(d, &win, pid);
145         if (!found) {
146                 XCloseDisplay(d);
147                 _E("cannot found window with pid - %d", pid);
148                 return -1;
149         }
150
151         r = __raise_win(d, win);
152         if (r < 0)
153                 _E("fail to raise win");
154
155         XCloseDisplay(d);
156
157         return r;
158 }
159
160 int x_util_get_default_size(double *w, double *h)
161 {
162         Display *d;
163         int screen_num;
164
165         d = XOpenDisplay(NULL);
166         if (d == NULL)
167                 return -1;
168
169         screen_num = DefaultScreen(d);
170
171         *w = DisplayWidth(d, screen_num);
172         *h = DisplayHeight(d, screen_num);
173
174         _D("Root Width = %lf, Height = %lf\n", *w, *h);
175
176         XCloseDisplay(d);
177
178         return 0;
179 }
180
181 static int __cb_x_error(Display *disp, XErrorEvent *ev)
182 {
183         _E("X error received - Error Code = %d", ev->error_code);
184         return 0;
185 }
186
187 int x_util_init()
188 {
189         x_old_error = XSetErrorHandler(__cb_x_error);
190         return 0;
191 }
192
193 int x_util_fini()
194 {
195         XSetErrorHandler(x_old_error);
196         return 0;
197 }
198