4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "bcdisplay.h"
23 #include "bcdisplayinfo.h"
24 #include "bcsignals.h"
25 #include "bcwindowbase.h"
31 #include <X11/Xutil.h>
36 #define TEST_SIZE2 164
38 int BC_DisplayInfo::top_border = -1;
39 int BC_DisplayInfo::left_border = -1;
40 int BC_DisplayInfo::bottom_border = -1;
41 int BC_DisplayInfo::right_border = -1;
42 int BC_DisplayInfo::auto_reposition_x = -1;
43 int BC_DisplayInfo::auto_reposition_y = -1;
46 BC_DisplayInfo::BC_DisplayInfo(const char *display_name, int show_error)
49 xinerama_screens = -1;
51 init_window(display_name, show_error);
54 BC_DisplayInfo::~BC_DisplayInfo()
56 if( xinerama_info ) XFree(xinerama_info);
58 XCloseDisplay(display);
63 void BC_DisplayInfo::parse_geometry(char *geom, int *x, int *y, int *width, int *height)
65 XParseGeometry(geom, x, y, (unsigned int*)width, (unsigned int*)height);
69 int BC_DisplayInfo::get_xinerama_screens()
71 if( xinerama_screens < 0 ) {
73 if( XineramaIsActive(display) )
74 xinerama_info = XineramaQueryScreens(display, &xinerama_screens);
76 return xinerama_screens;
79 int BC_DisplayInfo::xinerama_geometry(int screen, int &x, int &y, int &w, int &h)
81 int screens = get_xinerama_screens();
82 if( !screens ) return 1;
85 while( --k >= 0 && xinerama_info[k].screen_number != screen );
87 x = xinerama_info[k].x_org; w = xinerama_info[k].width;
88 y = xinerama_info[k].y_org; h = xinerama_info[k].height;
91 int sx0 = INT_MAX, sx1 = INT_MIN;
92 int sy0 = INT_MAX, sy1 = INT_MIN;
93 for( int i=0; i<screens; ++i ) {
94 int x0 = xinerama_info[i].x_org;
95 int x1 = x0 + xinerama_info[i].width;
96 if( sx0 > x0 ) sx0 = x0;
97 if( sx1 < x1 ) sx1 = x1;
98 int y0 = xinerama_info[i].y_org;
99 int y1 = y0 + xinerama_info[i].height;
100 if( sy0 > y0 ) sy0 = y0;
101 if( sy1 < y1 ) sy1 = y1;
103 x = sx0; w = sx1 - sx0;
104 y = sy0; h = sy1 - sy0;
109 static void get_top_coords(Display *display, Window win, int &px,int &py, int &tx,int &ty)
111 Window *pcwin = 0; unsigned int ncwin = 0;
112 Window cwin = 0, pwin = 0, root = 0;
113 XQueryTree(display, win, &root, &pwin, &pcwin, &ncwin);
114 if( pcwin ) XFree(pcwin);
115 XTranslateCoordinates(display, pwin, root, 0,0, &px,&py, &cwin);
116 //printf(" win=%lx, px/py=%d/%d\n", win, px,py);
118 int nx = px, ny = py; pwin = win;
119 for( int i=5; --i>=0; ) {
120 win = pwin; pwin = 0; pcwin = 0; ncwin = 0;
122 // XQuerytTree has been known to fail here
123 XQueryTree(display, win, &rwin, &pwin, &pcwin, &ncwin);
124 if( pcwin ) XFree(pcwin);
125 if( !rwin || rwin != root || pwin == root ) break;
126 XTranslateCoordinates(display, pwin, root, 0,0, &nx,&ny, &cwin);
127 //printf(" win=%lx, nx/ny=%d/%d\n", win, nx,ny);
133 void BC_DisplayInfo::test_window(int &x_out,
141 BC_Display::lock_display("BC_DisplayInfo::test_window");
151 unsigned long mask = CWEventMask | CWWinGravity | CWBackPixel;
152 XSetWindowAttributes attr;
153 attr.event_mask = StructureNotifyMask;
154 attr.win_gravity = SouthEastGravity;
155 attr.background_pixel = BlackPixel(display,screen);
156 Window win = XCreateWindow(display, rootwin,
157 x_in, y_in, TEST_SIZE, TEST_SIZE,
158 0, default_depth, InputOutput,
160 XSizeHints size_hints;
161 XGetNormalHints(display, win, &size_hints);
162 size_hints.flags = PPosition | PSize;
165 size_hints.width = TEST_SIZE;
166 size_hints.height = TEST_SIZE;
167 XSetStandardProperties(display, win,
168 "x", "x", None, 0, 0, &size_hints);
169 XClearWindow(display, win);
170 XMapWindow(display, win);
171 XFlush(display); XSync(display, 0); usleep(100000);
177 XNextEvent(display, &event);
178 //printf("BC_DisplayInfo::test_window 1 event=%d %d\n", event.type, XPending(display));
179 if( event.xany.window != win ) continue;
180 if( event.type != ConfigureNotify ) continue;
182 int rx = 0, ry = 0, px = 0, py = 0, tx = 0, ty = 0;
183 //printf("BC_DisplayInfo::test_window 1 state=%d x=%d y=%d w=%d h=%d bw=%d sev=%d\n",
184 // state, event.xconfigure.x, event.xconfigure.y,
185 // event.xconfigure.width, event.xconfigure.height,
186 // event.xconfigure.border_width, event.xconfigure.send_event);
187 get_top_coords(display,win, px,py, tx,ty);
188 //printf("x_in,y_in=%d,%d dx,dy=%d,%d\n", x_in,y_in, x_in-tx,y_in-ty);
190 case 0: // Get creation config
191 XTranslateCoordinates(display, win, rootwin, 0,0, &rx,&ry, &cwin);
194 XMoveResizeWindow(display, win, x_in,y_in, TEST_SIZE2,TEST_SIZE2);
195 XFlush(display); XSync(display, 0); usleep(100000);
198 case 1: // Get moveresize resizing
199 XTranslateCoordinates(display, win, rootwin, 0,0, &rx,&ry, &cwin);
202 x_in += TEST_DSIZE; y_in += TEST_DSIZE;
203 XMoveResizeWindow(display, win, x_in,y_in, TEST_SIZE2,TEST_SIZE2);
204 XFlush(display); XSync(display, 0); usleep(100000);
207 case 2: // Get moveresize move
208 XTranslateCoordinates(display, win, rootwin, 0,0, &rx,&ry, &cwin);
209 x_out2 = px - x_out1 - TEST_DSIZE;
210 y_out2 = py - y_out1 - TEST_DSIZE;
215 //printf("\nBC_DisplayInfo::test_window 3 x0,y0=%d,%d, x1,y1=%d,%d, x2,y2=%d,%d\n",
216 // x_out,y_out, x_out1,y_out1, x_out2,y_out2);
217 //printf("\nx_in,y_in=%d,%d\n", x_in,y_in);
219 XDestroyWindow(display, win);
223 x_out = MAX(0, MIN(x_out, 48));
224 y_out = MAX(0, MIN(y_out, 48));
227 BC_Display::unlock_display();
231 void BC_DisplayInfo::init_borders()
235 BC_DisplayInfo display_info;
236 display_info.test_window(left_border, top_border,
237 auto_reposition_x, auto_reposition_y, 100, 100);
238 right_border = left_border;
239 bottom_border = left_border;
240 //printf("BC_DisplayInfo::init_borders border=%d %d auto=%d %d\n",
241 // left_border, top_border, auto_reposition_x, auto_reposition_y);
246 int BC_DisplayInfo::get_top_border()
252 int BC_DisplayInfo::get_left_border()
258 int BC_DisplayInfo::get_right_border()
264 int BC_DisplayInfo::get_bottom_border()
267 return bottom_border;
270 void BC_DisplayInfo::init_window(const char *display_name, int show_error)
272 if(display_name && display_name[0] == 0) display_name = NULL;
275 display = BC_Display::get_display(display_name);
278 // This function must be the first Xlib
279 // function a multi-threaded program calls
282 if((display = XOpenDisplay(display_name)) == NULL)
284 if(!show_error) return;
285 fprintf(stderr,_("BC_DisplayInfo::init_window: cannot open display \"%s\".\n"),
286 display_name ? display_name : "");
287 if(getenv("DISPLAY") == NULL)
288 fprintf(stderr, _("'DISPLAY' environment variable not set.\n"));
289 if((display = XOpenDisplay(0)) == NULL) {
290 fprintf(stderr,_("BC_DisplayInfo::init_window: cannot connect to X server.\n"));
294 #endif // SINGLE_THREAD
297 BC_Display::lock_display("BC_DisplayInfo::init_window");
299 screen = DefaultScreen(display);
300 rootwin = RootWindow(display, screen);
301 vis = DefaultVisual(display, screen);
302 default_depth = DefaultDepth(display, screen);
304 BC_Display::unlock_display();
305 #endif // SINGLE_THREAD
309 int BC_DisplayInfo::get_root_w()
312 BC_Display::lock_display("BC_DisplayInfo::get_root_w");
314 Screen *screen_ptr = XDefaultScreenOfDisplay(display);
315 int result = WidthOfScreen(screen_ptr);
317 BC_Display::unlock_display();
322 int BC_DisplayInfo::get_root_h()
325 BC_Display::lock_display("BC_DisplayInfo::get_root_h");
327 Screen *screen_ptr = XDefaultScreenOfDisplay(display);
328 int result = HeightOfScreen(screen_ptr);
330 BC_Display::unlock_display();
335 int BC_DisplayInfo::get_abs_cursor_x()
337 int abs_x, abs_y, win_x, win_y;
338 unsigned int temp_mask;
342 BC_Display::lock_display("BC_DisplayInfo::get_abs_cursor_x");
344 XQueryPointer(display,
354 BC_Display::unlock_display();
359 int BC_DisplayInfo::get_abs_cursor_y()
361 int abs_x, abs_y, win_x, win_y;
362 unsigned int temp_mask;
366 BC_Display::lock_display("BC_DisplayInfo::get_abs_cursor_y");
368 XQueryPointer(display,
378 BC_Display::unlock_display();
384 int BC_DisplayInfo::get_screen_count()
386 return XScreenCount(display);
390 const char *BC_DisplayInfo::host_display_name(const char *display_name)
392 return XDisplayName(display_name);