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 static void get_top_coords(Display *display, Window win, int &px,int &py, int &tx,int &ty)
119 Window *pcwin = 0; unsigned int ncwin = 0;
120 Window cwin = 0, pwin = 0, root = 0;
121 XQueryTree(display, win, &root, &pwin, &pcwin, &ncwin);
122 if( pcwin ) XFree(pcwin);
123 XTranslateCoordinates(display, pwin, root, 0,0, &px,&py, &cwin);
124 //printf(" win=%lx, px/py=%d/%d\n", win, px,py);
126 int nx = px, ny = py; pwin = win;
127 for( int i=5; --i>=0; ) {
128 win = pwin; pwin = 0; pcwin = 0; ncwin = 0;
130 // XQuerytTree has been known to fail here
131 XQueryTree(display, win, &rwin, &pwin, &pcwin, &ncwin);
132 if( pcwin ) XFree(pcwin);
133 if( !rwin || rwin != root || pwin == root ) break;
134 XTranslateCoordinates(display, pwin, root, 0,0, &nx,&ny, &cwin);
135 //printf(" win=%lx, nx/ny=%d/%d\n", win, nx,ny);
142 int BC_DisplayInfo::gl_fb_config()
145 // find prefered config via glxinfo: mesa_hack
146 static int attribs[] = {
153 GLX_ACCUM_RED_SIZE, 1,
154 GLX_ACCUM_GREEN_SIZE, 1,
155 GLX_ACCUM_BLUE_SIZE, 1,
156 GLX_ACCUM_ALPHA_SIZE, 1,
160 vis_info = glXChooseVisual(display, scrnum, attribs);
161 VisualID vis_id = vis_info ? vis_info->visualid : 0;
162 fb_cfgs = glXGetFBConfigs(display, scrnum, &ncfgs);
163 int n = !fb_cfgs ? 0 : ncfgs;
164 for( int i=0; --n>=0; ++i ) {
165 if( vis_info ) { XFree(vis_info); vis_info = 0; }
166 vis_info = glXGetVisualFromFBConfig(display, cfg=fb_cfgs[i]);
167 if( vis_info && vis_info->visualid == vis_id ) break;
171 if( fb_cfgs ) { XFree(fb_cfgs); fb_cfgs = 0; }
172 if( vis_info ) { XFree(vis_info); vis_info = 0; }
177 GLX_CONFIG_CAVEAT, GLX_SLOW_CONFIG,
178 GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT | GLX_PBUFFER_BIT | GLX_PIXMAP_BIT,
180 GLX_RENDER_TYPE, GLX_RGBA_BIT,
181 GLX_ACCUM_RED_SIZE, 1,
182 GLX_ACCUM_GREEN_SIZE, 1,
183 GLX_ACCUM_BLUE_SIZE, 1,
184 GLX_ACCUM_ALPHA_SIZE, 1,
191 fb_cfgs = glXChooseFBConfig(display, scrnum, fb_attrs+2, &ncfgs);
192 if( fb_cfgs && ncfgs ) break;
193 fb_cfgs = glXChooseFBConfig(display, scrnum, fb_attrs+0, &ncfgs);
194 if( fb_cfgs && ncfgs ) break;
196 fb_cfgs = glXChooseFBConfig(display, scrnum, fb_attrs+2, &ncfgs);
197 if( fb_cfgs && ncfgs ) break;
198 fb_cfgs = glXChooseFBConfig(display, scrnum, fb_attrs+0, &ncfgs);
200 if( fb_cfgs && ncfgs ) {
201 for( int i=0; !vis_info && i<ncfgs; ++i )
202 vis_info = glXGetVisualFromFBConfig(display, cfg=fb_cfgs[i]);
205 vis = vis_info->visual;
206 depth = vis_info->depth;
209 printf("%s\n", "BC_DisplayInfo::gl_fb_config failed");
215 int BC_DisplayInfo::gl_probe(Window win)
217 GLXContext glx_ctx = !cfg ? 0 :
218 glXCreateNewContext(display, cfg, GLX_RGBA_TYPE, 0, True);
219 GLXWindow glx_win = !cfg ? 0 :
220 glXCreateWindow(display, cfg, win, 0);
221 if( glx_ctx && glx_win &&
222 glXMakeContextCurrent(display, glx_win, glx_win, glx_ctx) ) {
223 const char *shader_version = (const char *)
224 glGetString(GL_SHADING_LANGUAGE_VERSION);
226 strncpy(gl_shader_version, shader_version, sizeof(gl_shader_version));
228 glXMakeContextCurrent(display, None, None, 0);
229 if( glx_ctx ) glXDestroyContext(display, glx_ctx);
230 if( glx_win ) glXDestroyWindow(display, glx_win);
231 if( fb_cfgs ) XFree(fb_cfgs);
232 if( vis_info ) XFree(vis_info);
238 void BC_DisplayInfo::test_window(int &x_out, int &y_out, int &x_out2, int &y_out2,
242 BC_Display::lock_display("BC_DisplayInfo::test_window");
254 unsigned long mask = CWEventMask | CWWinGravity | CWBackPixel | CWColormap;
255 XSetWindowAttributes attr;
256 attr.event_mask = StructureNotifyMask;
257 attr.win_gravity = SouthEastGravity;
258 attr.background_pixel = BlackPixel(display, scrnum);
259 attr.colormap = XCreateColormap(display, rootwin, vis, AllocNone);
260 Window win = XCreateWindow(display, rootwin,
261 x_in, y_in, TEST_SIZE, TEST_SIZE,
262 0, depth, InputOutput,
264 XSizeHints size_hints;
265 XGetNormalHints(display, win, &size_hints);
266 size_hints.flags = PPosition | PSize;
269 size_hints.width = TEST_SIZE;
270 size_hints.height = TEST_SIZE;
271 XSetStandardProperties(display, win,
272 "x", "x", None, 0, 0, &size_hints);
273 XClearWindow(display, win);
274 XMapWindow(display, win);
275 XFlush(display); XSync(display, 0); usleep(100000);
280 XNextEvent(display, &event);
281 //printf("BC_DisplayInfo::test_window 1 event=%d %d\n", event.type, XPending(display));
282 if( event.xany.window != win ) continue;
283 if( event.type != ConfigureNotify ) continue;
285 int rx = 0, ry = 0, px = 0, py = 0, tx = 0, ty = 0;
286 //printf("BC_DisplayInfo::test_window 1 state=%d x=%d y=%d w=%d h=%d bw=%d sev=%d\n",
287 // state, event.xconfigure.x, event.xconfigure.y,
288 // event.xconfigure.width, event.xconfigure.height,
289 // event.xconfigure.border_width, event.xconfigure.send_event);
290 get_top_coords(display,win, px,py, tx,ty);
291 //printf("x_in,y_in=%d,%d dx,dy=%d,%d\n", x_in,y_in, x_in-tx,y_in-ty);
293 case 0: // Get creation config
294 XTranslateCoordinates(display, win, rootwin, 0,0, &rx,&ry, &cwin);
297 XMoveResizeWindow(display, win, x_in,y_in, TEST_SIZE2,TEST_SIZE2);
298 XFlush(display); XSync(display, 0); usleep(100000);
301 case 1: // Get moveresize resizing
302 XTranslateCoordinates(display, win, rootwin, 0,0, &rx,&ry, &cwin);
305 x_in += TEST_DSIZE; y_in += TEST_DSIZE;
306 XMoveResizeWindow(display, win, x_in,y_in, TEST_SIZE2,TEST_SIZE2);
307 XFlush(display); XSync(display, 0); usleep(100000);
310 case 2: // Get moveresize move
311 XTranslateCoordinates(display, win, rootwin, 0,0, &rx,&ry, &cwin);
312 x_out2 = px - x_out1 - TEST_DSIZE;
313 y_out2 = py - y_out1 - TEST_DSIZE;
318 //printf("\nBC_DisplayInfo::test_window 3 x0,y0=%d,%d, x1,y1=%d,%d, x2,y2=%d,%d\n",
319 // x_out,y_out, x_out1,y_out1, x_out2,y_out2);
320 //printf("\nx_in,y_in=%d,%d\n", x_in,y_in);
325 XDestroyWindow(display, win);
329 x_out = MAX(0, MIN(x_out, 48));
330 y_out = MAX(0, MIN(y_out, 48));
333 BC_Display::unlock_display();
337 void BC_DisplayInfo::init_borders()
341 BC_DisplayInfo display_info;
342 display_info.test_window(left_border, top_border,
343 auto_reposition_x, auto_reposition_y, 100, 100);
344 right_border = left_border;
345 bottom_border = left_border;
346 //printf("BC_DisplayInfo::init_borders border=%d %d auto=%d %d\n",
347 // left_border, top_border, auto_reposition_x, auto_reposition_y);
352 int BC_DisplayInfo::get_top_border()
358 int BC_DisplayInfo::get_left_border()
364 int BC_DisplayInfo::get_right_border()
370 int BC_DisplayInfo::get_bottom_border()
373 return bottom_border;
376 const char *BC_DisplayInfo::get_gl_shader_version()
379 return gl_shader_version;
382 void BC_DisplayInfo::init_window(const char *display_name, int show_error)
384 if(display_name && display_name[0] == 0) display_name = NULL;
387 display = BC_Display::get_display(display_name);
390 // This function must be the first Xlib
391 // function a multi-threaded program calls
394 if((display = XOpenDisplay(display_name)) == NULL)
396 if(!show_error) return;
397 fprintf(stderr,_("BC_DisplayInfo::init_window: cannot open display \"%s\".\n"),
398 display_name ? display_name : "");
399 if(getenv("DISPLAY") == NULL)
400 fprintf(stderr, _("'DISPLAY' environment variable not set.\n"));
401 if((display = XOpenDisplay(0)) == NULL) {
402 fprintf(stderr,_("BC_DisplayInfo::init_window: cannot connect to X server.\n"));
406 #endif // SINGLE_THREAD
409 BC_Display::lock_display("BC_DisplayInfo::init_window");
411 scrnum = DefaultScreen(display);
412 rootwin = RootWindow(display, scrnum);
413 vis = DefaultVisual(display, scrnum);
414 depth = DefaultDepth(display, scrnum);
416 BC_Display::unlock_display();
417 #endif // SINGLE_THREAD
421 int BC_DisplayInfo::get_root_w()
424 BC_Display::lock_display("BC_DisplayInfo::get_root_w");
426 Screen *screen_ptr = XDefaultScreenOfDisplay(display);
427 int result = WidthOfScreen(screen_ptr);
429 BC_Display::unlock_display();
434 int BC_DisplayInfo::get_root_h()
437 BC_Display::lock_display("BC_DisplayInfo::get_root_h");
439 Screen *screen_ptr = XDefaultScreenOfDisplay(display);
440 int result = HeightOfScreen(screen_ptr);
442 BC_Display::unlock_display();
447 int BC_DisplayInfo::get_abs_cursor_x()
449 int abs_x, abs_y, win_x, win_y;
450 unsigned int temp_mask;
454 BC_Display::lock_display("BC_DisplayInfo::get_abs_cursor_x");
456 XQueryPointer(display, rootwin, &temp_win, &temp_win,
457 &abs_x, &abs_y, &win_x, &win_y, &temp_mask);
459 BC_Display::unlock_display();
464 int BC_DisplayInfo::get_abs_cursor_y()
466 int abs_x, abs_y, win_x, win_y;
467 unsigned int temp_mask;
471 BC_Display::lock_display("BC_DisplayInfo::get_abs_cursor_y");
473 XQueryPointer(display, rootwin, &temp_win, &temp_win,
474 &abs_x, &abs_y, &win_x, &win_y, &temp_mask);
476 BC_Display::unlock_display();
482 int BC_DisplayInfo::get_screen_count()
484 return XScreenCount(display);
488 const char *BC_DisplayInfo::host_display_name(const char *display_name)
490 return XDisplayName(display_name);