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