2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include <X11/Xatom.h>
27 #include "simple_util.h"
30 static int (*x_old_error) (Display *, XErrorEvent *);
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);
38 static pid_t __get_win_pid(Display *d, Window win)
46 unsigned long bytes_after;
47 unsigned char *prop_ret;
49 retv_if(d == NULL || !a_pid, -1);
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)
57 if (a_type == XA_CARDINAL && format == 32)
58 pid = *(unsigned long *)prop_ret;
67 static int __find_win(Display *d, Window *win, pid_t pid)
72 Window root, parent, *child;
74 p = __get_win_pid(d, *win);
78 r = XQueryTree(d, *win, &root, &parent, &child, &n);
83 for (i = 0; i < n; i++) {
84 found = __find_win(d, &child[i], pid);
99 static int __raise_win(Display *d, Window win)
101 XWindowAttributes attr;
102 attr.map_state = IsUnmapped;
106 XGetWindowAttributes(d, win, &attr);
108 if (attr.map_state == IsUnmapped)
110 else if (attr.map_state == IsUnviewable)
112 else if (attr.map_state == IsViewable)
115 retv_if(attr.map_state != IsViewable, -1);
117 XSetInputFocus(d, win, RevertToPointerRoot, CurrentTime);
122 int x_util_raise_win(pid_t pid)
136 d = XOpenDisplay(NULL);
137 retv_if(d == NULL, -1);
139 win = XDefaultRootWindow(d);
142 a_pid = XInternAtom(d, "X_CLIENT_PID", True);
144 found = __find_win(d, &win, pid);
147 _E("cannot found window with pid - %d", pid);
151 r = __raise_win(d, win);
153 _E("fail to raise win");
160 int x_util_get_default_size(double *w, double *h)
165 d = XOpenDisplay(NULL);
169 screen_num = DefaultScreen(d);
171 *w = DisplayWidth(d, screen_num);
172 *h = DisplayHeight(d, screen_num);
174 _D("Root Width = %lf, Height = %lf\n", *w, *h);
181 static int __cb_x_error(Display *disp, XErrorEvent *ev)
183 _E("X error received - Error Code = %d", ev->error_code);
189 x_old_error = XSetErrorHandler(__cb_x_error);
195 XSetErrorHandler(x_old_error);