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.
24 #include <X11/Xatom.h>
26 #include "simple_util.h"
29 static int (*x_old_error)(Display *, XErrorEvent *);
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);
36 static pid_t __get_win_pid(Display *d, Window win)
44 unsigned long bytes_after;
45 unsigned char *prop_ret;
47 retv_if(d == NULL || !a_pid, -1);
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) {
56 if (a_type == XA_CARDINAL && format == 32) {
57 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);
79 r = XQueryTree(d, *win, &root, &parent, &child, &n);
84 for (i = 0; i < n; i++) {
85 found = __find_win(d, &child[i], pid);
101 static int __raise_win(Display *d, Window win)
103 XWindowAttributes attr;
104 attr.map_state = IsUnmapped;
108 XGetWindowAttributes(d, win, &attr);
110 if (attr.map_state == IsUnmapped) {
112 } else if (attr.map_state == IsUnviewable) {
114 } else if (attr.map_state == IsViewable) {
118 retv_if(attr.map_state != IsViewable, -1);
120 XSetInputFocus(d, win, RevertToPointerRoot, CurrentTime);
125 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);
150 found = __find_win(d, &win, pid);
153 _E("cannot found window with pid - %d", pid);
157 r = __raise_win(d, win);
159 _E("fail to raise win");
167 int x_util_get_default_size(double *w, double *h)
172 d = XOpenDisplay(NULL);
177 screen_num = DefaultScreen(d);
179 *w = DisplayWidth(d, screen_num);
180 *h = DisplayHeight(d, screen_num);
182 _D("Root Width = %lf, Height = %lf\n", *w, *h);
189 static int __cb_x_error(Display *disp, XErrorEvent *ev)
191 _E("X error received - Error Code = %d", ev->error_code);
197 x_old_error = XSetErrorHandler(__cb_x_error);
203 XSetErrorHandler(x_old_error);