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