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;
44 char BC_DisplayInfo::gl_shader_version[64] = { 0, };
46 BC_DisplayInfo::BC_DisplayInfo(const char *display_name, int show_error)
51 xinerama_screens = -1;
53 init_window(display_name, show_error);
62 BC_DisplayInfo::~BC_DisplayInfo()
64 if( xinerama_info ) XFree(xinerama_info);
66 XCloseDisplay(display);
71 void BC_DisplayInfo::parse_geometry(char *geom, int *x, int *y, int *width, int *height)
73 XParseGeometry(geom, x, y, (unsigned int*)width, (unsigned int*)height);
77 int BC_DisplayInfo::get_xinerama_screens()
79 if( xinerama_screens < 0 ) {
81 if( XineramaIsActive(display) )
82 xinerama_info = XineramaQueryScreens(display, &xinerama_screens);
84 return xinerama_screens;
87 int BC_DisplayInfo::xinerama_geometry(int screen, int &x, int &y, int &w, int &h)
89 int screens = get_xinerama_screens();
90 if( !screens ) return 1;
93 while( --k >= 0 && xinerama_info[k].screen_number != screen );
95 x = xinerama_info[k].x_org; w = xinerama_info[k].width;
96 y = xinerama_info[k].y_org; h = xinerama_info[k].height;
99 int sx0 = INT_MAX, sx1 = INT_MIN;
100 int sy0 = INT_MAX, sy1 = INT_MIN;
101 for( int i=0; i<screens; ++i ) {
102 int x0 = xinerama_info[i].x_org;
103 int x1 = x0 + xinerama_info[i].width;
104 if( sx0 > x0 ) sx0 = x0;
105 if( sx1 < x1 ) sx1 = x1;
106 int y0 = xinerama_info[i].y_org;
107 int y1 = y0 + xinerama_info[i].height;
108 if( sy0 > y0 ) sy0 = y0;
109 if( sy1 < y1 ) sy1 = y1;
111 x = sx0; w = sx1 - sx0;
112 y = sy0; h = sy1 - sy0;
117 int BC_DisplayInfo::xinerama_big_screen()
119 int screens = get_xinerama_screens();
120 int best = 0, ret = -1;
121 for( int k=screens; --k>=0; ) {
122 int w = xinerama_info[k].width, h = xinerama_info[k].height;
125 ret = xinerama_info[k].screen_number;
132 static void get_top_coords(Display *display, Window win, int &px,int &py, int &tx,int &ty)
134 Window *pcwin = 0; unsigned int ncwin = 0;
135 Window cwin = 0, pwin = 0, root = 0;
136 XQueryTree(display, win, &root, &pwin, &pcwin, &ncwin);
137 if( pcwin ) XFree(pcwin);
138 XTranslateCoordinates(display, pwin, root, 0,0, &px,&py, &cwin);
139 //printf(" win=%lx, px/py=%d/%d\n", win, px,py);
141 int nx = px, ny = py; pwin = win;
142 for( int i=5; --i>=0; ) {
143 win = pwin; pwin = 0; pcwin = 0; ncwin = 0;
145 // XQuerytTree has been known to fail here
146 XQueryTree(display, win, &rwin, &pwin, &pcwin, &ncwin);
147 if( pcwin ) XFree(pcwin);
148 if( !rwin || rwin != root || pwin == root ) break;
149 XTranslateCoordinates(display, pwin, root, 0,0, &nx,&ny, &cwin);
150 //printf(" win=%lx, nx/ny=%d/%d\n", win, nx,ny);
157 int BC_DisplayInfo::gl_fb_config()
160 // find prefered config via glxinfo: mesa_hack
161 static int attribs[] = {
168 GLX_ACCUM_RED_SIZE, 1,
169 GLX_ACCUM_GREEN_SIZE, 1,
170 GLX_ACCUM_BLUE_SIZE, 1,
171 GLX_ACCUM_ALPHA_SIZE, 1,
175 vis_info = glXChooseVisual(display, scrnum, attribs);
176 VisualID vis_id = vis_info ? vis_info->visualid : 0;
177 fb_cfgs = glXGetFBConfigs(display, scrnum, &ncfgs);
178 int n = !fb_cfgs ? 0 : ncfgs;
179 for( int i=0; --n>=0; ++i ) {
180 if( vis_info ) { XFree(vis_info); vis_info = 0; }
181 vis_info = glXGetVisualFromFBConfig(display, cfg=fb_cfgs[i]);
182 if( vis_info && vis_info->visualid == vis_id ) break;
186 if( fb_cfgs ) { XFree(fb_cfgs); fb_cfgs = 0; }
187 if( vis_info ) { XFree(vis_info); vis_info = 0; }
192 GLX_CONFIG_CAVEAT, GLX_SLOW_CONFIG,
193 GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT | GLX_PBUFFER_BIT | GLX_PIXMAP_BIT,
195 GLX_RENDER_TYPE, GLX_RGBA_BIT,
196 GLX_ACCUM_RED_SIZE, 1,
197 GLX_ACCUM_GREEN_SIZE, 1,
198 GLX_ACCUM_BLUE_SIZE, 1,
199 GLX_ACCUM_ALPHA_SIZE, 1,
206 fb_cfgs = glXChooseFBConfig(display, scrnum, fb_attrs+2, &ncfgs);
207 if( fb_cfgs && ncfgs ) break;
208 fb_cfgs = glXChooseFBConfig(display, scrnum, fb_attrs+0, &ncfgs);
209 if( fb_cfgs && ncfgs ) break;
211 fb_cfgs = glXChooseFBConfig(display, scrnum, fb_attrs+2, &ncfgs);
212 if( fb_cfgs && ncfgs ) break;
213 fb_cfgs = glXChooseFBConfig(display, scrnum, fb_attrs+0, &ncfgs);
215 if( fb_cfgs && ncfgs ) {
216 for( int i=0; !vis_info && i<ncfgs; ++i )
217 vis_info = glXGetVisualFromFBConfig(display, cfg=fb_cfgs[i]);
220 vis = vis_info->visual;
221 depth = vis_info->depth;
224 printf("%s\n", "BC_DisplayInfo::gl_fb_config failed");
230 int BC_DisplayInfo::gl_probe(Window win)
232 GLXContext glx_ctx = !cfg ? 0 :
233 glXCreateNewContext(display, cfg, GLX_RGBA_TYPE, 0, True);
234 GLXWindow glx_win = !cfg ? 0 :
235 glXCreateWindow(display, cfg, win, 0);
236 if( glx_ctx && glx_win &&
237 glXMakeContextCurrent(display, glx_win, glx_win, glx_ctx) ) {
238 const char *shader_version = (const char *)
239 glGetString(GL_SHADING_LANGUAGE_VERSION);
241 strncpy(gl_shader_version, shader_version, sizeof(gl_shader_version));
243 glXMakeContextCurrent(display, None, None, 0);
244 if( glx_ctx ) glXDestroyContext(display, glx_ctx);
245 if( glx_win ) glXDestroyWindow(display, glx_win);
246 if( fb_cfgs ) XFree(fb_cfgs);
247 if( vis_info ) XFree(vis_info);
253 void BC_DisplayInfo::test_window(int &x_out, int &y_out, int &x_out2, int &y_out2,
257 BC_Display::lock_display("BC_DisplayInfo::test_window");
269 unsigned long mask = CWEventMask | CWWinGravity | CWBackPixel | CWColormap;
270 XSetWindowAttributes attr;
271 attr.event_mask = StructureNotifyMask;
272 attr.win_gravity = SouthEastGravity;
273 attr.background_pixel = BlackPixel(display, scrnum);
274 attr.colormap = XCreateColormap(display, rootwin, vis, AllocNone);
275 Window win = XCreateWindow(display, rootwin,
276 x_in, y_in, TEST_SIZE, TEST_SIZE,
277 0, depth, InputOutput,
279 XSizeHints size_hints;
280 XGetNormalHints(display, win, &size_hints);
281 size_hints.flags = PPosition | PSize;
284 size_hints.width = TEST_SIZE;
285 size_hints.height = TEST_SIZE;
286 XSetStandardProperties(display, win,
287 "x", "x", None, 0, 0, &size_hints);
288 XClearWindow(display, win);
289 XMapWindow(display, win);
290 XFlush(display); XSync(display, 0); usleep(100000);
295 XNextEvent(display, &event);
296 //printf("BC_DisplayInfo::test_window 1 event=%d %d\n", event.type, XPending(display));
297 if( event.xany.window != win ) continue;
298 if( event.type != ConfigureNotify ) continue;
300 int rx = 0, ry = 0, px = 0, py = 0, tx = 0, ty = 0;
301 //printf("BC_DisplayInfo::test_window 1 state=%d x=%d y=%d w=%d h=%d bw=%d sev=%d\n",
302 // state, event.xconfigure.x, event.xconfigure.y,
303 // event.xconfigure.width, event.xconfigure.height,
304 // event.xconfigure.border_width, event.xconfigure.send_event);
305 get_top_coords(display,win, px,py, tx,ty);
306 //printf("x_in,y_in=%d,%d dx,dy=%d,%d\n", x_in,y_in, x_in-tx,y_in-ty);
308 case 0: // Get creation config
309 XTranslateCoordinates(display, win, rootwin, 0,0, &rx,&ry, &cwin);
312 XMoveResizeWindow(display, win, x_in,y_in, TEST_SIZE2,TEST_SIZE2);
313 XFlush(display); XSync(display, 0); usleep(100000);
316 case 1: // Get moveresize resizing
317 XTranslateCoordinates(display, win, rootwin, 0,0, &rx,&ry, &cwin);
320 x_in += TEST_DSIZE; y_in += TEST_DSIZE;
321 XMoveResizeWindow(display, win, x_in,y_in, TEST_SIZE2,TEST_SIZE2);
322 XFlush(display); XSync(display, 0); usleep(100000);
325 case 2: // Get moveresize move
326 XTranslateCoordinates(display, win, rootwin, 0,0, &rx,&ry, &cwin);
327 x_out2 = px - x_out1 - TEST_DSIZE;
328 y_out2 = py - y_out1 - TEST_DSIZE;
333 //printf("\nBC_DisplayInfo::test_window 3 x0,y0=%d,%d, x1,y1=%d,%d, x2,y2=%d,%d\n",
334 // x_out,y_out, x_out1,y_out1, x_out2,y_out2);
335 //printf("\nx_in,y_in=%d,%d\n", x_in,y_in);
340 XDestroyWindow(display, win);
344 x_out = MAX(0, MIN(x_out, 48));
345 y_out = MAX(0, MIN(y_out, 48));
348 BC_Display::unlock_display();
352 void BC_DisplayInfo::init_borders()
356 BC_DisplayInfo display_info;
357 display_info.test_window(left_border, top_border,
358 auto_reposition_x, auto_reposition_y, 100, 100);
359 right_border = left_border;
360 bottom_border = left_border;
361 //printf("BC_DisplayInfo::init_borders border=%d %d auto=%d %d\n",
362 // left_border, top_border, auto_reposition_x, auto_reposition_y);
367 int BC_DisplayInfo::get_top_border()
373 int BC_DisplayInfo::get_left_border()
379 int BC_DisplayInfo::get_right_border()
385 int BC_DisplayInfo::get_bottom_border()
388 return bottom_border;
391 const char *BC_DisplayInfo::get_gl_shader_version()
394 return gl_shader_version;
397 void BC_DisplayInfo::init_window(const char *display_name, int show_error)
399 if(display_name && display_name[0] == 0) display_name = NULL;
402 display = BC_Display::get_display(display_name);
405 // This function must be the first Xlib
406 // function a multi-threaded program calls
409 if((display = XOpenDisplay(display_name)) == NULL)
411 if(!show_error) return;
412 fprintf(stderr,_("BC_DisplayInfo::init_window: cannot open display \"%s\".\n"),
413 display_name ? display_name : "");
414 if(getenv("DISPLAY") == NULL)
415 fprintf(stderr, _("'DISPLAY' environment variable not set.\n"));
416 if((display = XOpenDisplay(0)) == NULL) {
417 fprintf(stderr,_("BC_DisplayInfo::init_window: cannot connect to X server.\n"));
421 #endif // SINGLE_THREAD
424 BC_Display::lock_display("BC_DisplayInfo::init_window");
426 scrnum = DefaultScreen(display);
427 rootwin = RootWindow(display, scrnum);
428 vis = DefaultVisual(display, scrnum);
429 depth = DefaultDepth(display, scrnum);
431 BC_Display::unlock_display();
432 #endif // SINGLE_THREAD
436 int BC_DisplayInfo::get_root_w()
439 BC_Display::lock_display("BC_DisplayInfo::get_root_w");
441 Screen *screen_ptr = XDefaultScreenOfDisplay(display);
442 int result = WidthOfScreen(screen_ptr);
444 BC_Display::unlock_display();
449 int BC_DisplayInfo::get_root_h()
452 BC_Display::lock_display("BC_DisplayInfo::get_root_h");
454 Screen *screen_ptr = XDefaultScreenOfDisplay(display);
455 int result = HeightOfScreen(screen_ptr);
457 BC_Display::unlock_display();
462 int BC_DisplayInfo::get_abs_cursor_x()
464 int abs_x, abs_y, win_x, win_y;
465 unsigned int temp_mask;
469 BC_Display::lock_display("BC_DisplayInfo::get_abs_cursor_x");
471 XQueryPointer(display, rootwin, &temp_win, &temp_win,
472 &abs_x, &abs_y, &win_x, &win_y, &temp_mask);
474 BC_Display::unlock_display();
479 int BC_DisplayInfo::get_abs_cursor_y()
481 int abs_x, abs_y, win_x, win_y;
482 unsigned int temp_mask;
486 BC_Display::lock_display("BC_DisplayInfo::get_abs_cursor_y");
488 XQueryPointer(display, rootwin, &temp_win, &temp_win,
489 &abs_x, &abs_y, &win_x, &win_y, &temp_mask);
491 BC_Display::unlock_display();
497 int BC_DisplayInfo::get_screen_count()
499 return XScreenCount(display);
503 const char *BC_DisplayInfo::host_display_name(const char *display_name)
505 return XDisplayName(display_name);