4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
30 #include <X11/Xatom.h>
32 #include "simple_util.h"
35 static int (*x_old_error) (Display *, XErrorEvent *);
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);
43 static pid_t __get_win_pid(Display *d, Window win)
51 unsigned long bytes_after;
52 unsigned char *prop_ret;
54 retv_if(d == NULL || !a_pid, -1);
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)
62 if (a_type == XA_CARDINAL && format == 32)
63 pid = *(unsigned long *)prop_ret;
72 static int __find_win(Display *d, Window *win, pid_t pid)
77 Window root, parent, *child;
79 p = __get_win_pid(d, *win);
83 r = XQueryTree(d, *win, &root, &parent, &child, &n);
88 for (i = 0; i < n; i++) {
89 found = __find_win(d, &child[i], pid);
104 static int __raise_win(Display *d, Window win)
106 XWindowAttributes attr;
107 attr.map_state = IsUnmapped;
111 XGetWindowAttributes(d, win, &attr);
113 if (attr.map_state == IsUnmapped)
115 else if (attr.map_state == IsUnviewable)
117 else if (attr.map_state == IsViewable)
120 retv_if(attr.map_state != IsViewable, -1);
122 XSetInputFocus(d, win, RevertToPointerRoot, CurrentTime);
127 int x_util_raise_win(pid_t pid)
141 d = XOpenDisplay(NULL);
142 retv_if(d == NULL, -1);
144 win = XDefaultRootWindow(d);
147 a_pid = XInternAtom(d, "X_CLIENT_PID", True);
149 found = __find_win(d, &win, pid);
152 _E("cannot found window with pid - %d", pid);
156 r = __raise_win(d, win);
158 _E("fail to raise win");
165 int x_util_get_default_size(double *w, double *h)
170 d = XOpenDisplay(NULL);
174 screen_num = DefaultScreen(d);
176 *w = DisplayWidth(d, screen_num);
177 *h = DisplayHeight(d, screen_num);
179 _D("Root Width = %lf, Height = %lf\n", *w, *h);
186 static int __cb_x_error(Display *disp, XErrorEvent *ev)
188 _E("X error received - Error Code = %d", ev->error_code);
194 x_old_error = XSetErrorHandler(__cb_x_error);
200 XSetErrorHandler(x_old_error);