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);
56 BC_DisplayInfo::~BC_DisplayInfo()
58 if( xinerama_info ) XFree(xinerama_info);
60 XCloseDisplay(display);
65 void BC_DisplayInfo::parse_geometry(char *geom, int *x, int *y, int *width, int *height)
67 XParseGeometry(geom, x, y, (unsigned int*)width, (unsigned int*)height);
71 int BC_DisplayInfo::get_xinerama_screens()
73 if( xinerama_screens < 0 ) {
75 if( XineramaIsActive(display) )
76 xinerama_info = XineramaQueryScreens(display, &xinerama_screens);
78 return xinerama_screens;
81 int BC_DisplayInfo::xinerama_geometry(int screen, int &x, int &y, int &w, int &h)
83 int screens = get_xinerama_screens();
84 if( !screens ) return 1;
87 while( --k >= 0 && xinerama_info[k].screen_number != screen );
89 x = xinerama_info[k].x_org; w = xinerama_info[k].width;
90 y = xinerama_info[k].y_org; h = xinerama_info[k].height;
93 int sx0 = INT_MAX, sx1 = INT_MIN;
94 int sy0 = INT_MAX, sy1 = INT_MIN;
95 for( int i=0; i<screens; ++i ) {
96 int x0 = xinerama_info[i].x_org;
97 int x1 = x0 + xinerama_info[i].width;
98 if( sx0 > x0 ) sx0 = x0;
99 if( sx1 < x1 ) sx1 = x1;
100 int y0 = xinerama_info[i].y_org;
101 int y1 = y0 + xinerama_info[i].height;
102 if( sy0 > y0 ) sy0 = y0;
103 if( sy1 < y1 ) sy1 = y1;
105 x = sx0; w = sx1 - sx0;
106 y = sy0; h = sy1 - sy0;
111 static void get_top_coords(Display *display, Window win, int &px,int &py, int &tx,int &ty)
113 Window *pcwin = 0; unsigned int ncwin = 0;
114 Window cwin = 0, pwin = 0, root = 0;
115 XQueryTree(display, win, &root, &pwin, &pcwin, &ncwin);
116 if( pcwin ) XFree(pcwin);
117 XTranslateCoordinates(display, pwin, root, 0,0, &px,&py, &cwin);
118 //printf(" win=%lx, px/py=%d/%d\n", win, px,py);
120 int nx = px, ny = py; pwin = win;
121 for( int i=5; --i>=0; ) {
122 win = pwin; pwin = 0; pcwin = 0; ncwin = 0;
124 // XQuerytTree has been known to fail here
125 XQueryTree(display, win, &rwin, &pwin, &pcwin, &ncwin);
126 if( pcwin ) XFree(pcwin);
127 if( !rwin || rwin != root || pwin == root ) break;
128 XTranslateCoordinates(display, pwin, root, 0,0, &nx,&ny, &cwin);
129 //printf(" win=%lx, nx/ny=%d/%d\n", win, nx,ny);
136 int BC_DisplayInfo::gl_probe()
139 XVisualInfo *vis_info = 0;
140 GLXFBConfig *fb_cfgs = 0, cfg = 0;
142 // find prefered config via glxinfo: mesa_hack
143 static int attribs[] = {
150 GLX_ACCUM_RED_SIZE, 1,
151 GLX_ACCUM_GREEN_SIZE, 1,
152 GLX_ACCUM_BLUE_SIZE, 1,
153 GLX_ACCUM_ALPHA_SIZE, 1,
157 vis_info = glXChooseVisual(display, scrnum, attribs);
158 VisualID vis_id = vis_info ? vis_info->visualid : 0;
159 fb_cfgs = glXGetFBConfigs(display, scrnum, &ncfgs);
160 int n = !fb_cfgs ? 0 : ncfgs;
161 for( int i=0; --n>=0; ++i ) {
162 if( vis_info ) { XFree(vis_info); vis_info = 0; }
163 vis_info = glXGetVisualFromFBConfig(display, cfg=fb_cfgs[i]);
164 if( vis_info && vis_info->visualid == vis_id ) break;
168 if( fb_cfgs ) { XFree(fb_cfgs); fb_cfgs = 0; }
169 if( vis_info ) { XFree(vis_info); vis_info = 0; }
174 GLX_CONFIG_CAVEAT, GLX_SLOW_CONFIG,
175 GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT | GLX_PBUFFER_BIT | GLX_PIXMAP_BIT,
177 GLX_RENDER_TYPE, GLX_RGBA_BIT,
178 GLX_ACCUM_RED_SIZE, 1,
179 GLX_ACCUM_GREEN_SIZE, 1,
180 GLX_ACCUM_BLUE_SIZE, 1,
181 GLX_ACCUM_ALPHA_SIZE, 1,
188 fb_cfgs = glXChooseFBConfig(display, scrnum, fb_attrs+2, &ncfgs);
189 if( fb_cfgs && ncfgs ) break;
190 fb_cfgs = glXChooseFBConfig(display, scrnum, fb_attrs+0, &ncfgs);
191 if( fb_cfgs && ncfgs ) break;
193 fb_cfgs = glXChooseFBConfig(display, scrnum, fb_attrs+2, &ncfgs);
194 if( fb_cfgs && ncfgs ) break;
195 fb_cfgs = glXChooseFBConfig(display, scrnum, fb_attrs+0, &ncfgs);
197 if( fb_cfgs && ncfgs ) {
198 for( int i=0; !vis_info && i<ncfgs; ++i )
199 vis_info = glXGetVisualFromFBConfig(display, cfg=fb_cfgs[i]);
202 vis = vis_info->visual;
203 depth = vis_info->depth;
206 printf("%s\n", "BC_DisplayInfo::gl_fb_config failed");
209 GLXContext glx_ctx = !cfg ? 0 :
210 glXCreateNewContext(display, cfg, GLX_RGBA_TYPE, 0, True);
211 if( glx_ctx && glXMakeContextCurrent(display, None, None, glx_ctx) ) {
212 const char *shader_version = (const char *)
213 glGetString(GL_SHADING_LANGUAGE_VERSION);
215 strncpy(gl_shader_version, shader_version, sizeof(gl_shader_version));
217 glXMakeContextCurrent(display, None, None, 0);
218 if( glx_ctx ) glXDestroyContext(display, glx_ctx);
219 if( fb_cfgs ) XFree(fb_cfgs);
220 if( vis_info ) XFree(vis_info);
226 void BC_DisplayInfo::test_window(int &x_out, int &y_out, int &x_out2, int &y_out2,
230 BC_Display::lock_display("BC_DisplayInfo::test_window");
239 unsigned long mask = CWEventMask | CWWinGravity | CWBackPixel;
240 XSetWindowAttributes attr;
241 attr.event_mask = StructureNotifyMask;
242 attr.win_gravity = SouthEastGravity;
243 attr.background_pixel = BlackPixel(display, scrnum);
244 Window win = XCreateWindow(display, rootwin,
245 x_in, y_in, TEST_SIZE, TEST_SIZE,
246 0, depth, InputOutput,
248 XSizeHints size_hints;
249 XGetNormalHints(display, win, &size_hints);
250 size_hints.flags = PPosition | PSize;
253 size_hints.width = TEST_SIZE;
254 size_hints.height = TEST_SIZE;
255 XSetStandardProperties(display, win,
256 "x", "x", None, 0, 0, &size_hints);
257 XClearWindow(display, win);
258 XMapWindow(display, win);
259 XFlush(display); XSync(display, 0); usleep(100000);
264 XNextEvent(display, &event);
265 //printf("BC_DisplayInfo::test_window 1 event=%d %d\n", event.type, XPending(display));
266 if( event.xany.window != win ) continue;
267 if( event.type != ConfigureNotify ) continue;
269 int rx = 0, ry = 0, px = 0, py = 0, tx = 0, ty = 0;
270 //printf("BC_DisplayInfo::test_window 1 state=%d x=%d y=%d w=%d h=%d bw=%d sev=%d\n",
271 // state, event.xconfigure.x, event.xconfigure.y,
272 // event.xconfigure.width, event.xconfigure.height,
273 // event.xconfigure.border_width, event.xconfigure.send_event);
274 get_top_coords(display,win, px,py, tx,ty);
275 //printf("x_in,y_in=%d,%d dx,dy=%d,%d\n", x_in,y_in, x_in-tx,y_in-ty);
277 case 0: // Get creation config
278 XTranslateCoordinates(display, win, rootwin, 0,0, &rx,&ry, &cwin);
281 XMoveResizeWindow(display, win, x_in,y_in, TEST_SIZE2,TEST_SIZE2);
282 XFlush(display); XSync(display, 0); usleep(100000);
285 case 1: // Get moveresize resizing
286 XTranslateCoordinates(display, win, rootwin, 0,0, &rx,&ry, &cwin);
289 x_in += TEST_DSIZE; y_in += TEST_DSIZE;
290 XMoveResizeWindow(display, win, x_in,y_in, TEST_SIZE2,TEST_SIZE2);
291 XFlush(display); XSync(display, 0); usleep(100000);
294 case 2: // Get moveresize move
295 XTranslateCoordinates(display, win, rootwin, 0,0, &rx,&ry, &cwin);
296 x_out2 = px - x_out1 - TEST_DSIZE;
297 y_out2 = py - y_out1 - TEST_DSIZE;
302 //printf("\nBC_DisplayInfo::test_window 3 x0,y0=%d,%d, x1,y1=%d,%d, x2,y2=%d,%d\n",
303 // x_out,y_out, x_out1,y_out1, x_out2,y_out2);
304 //printf("\nx_in,y_in=%d,%d\n", x_in,y_in);
306 XDestroyWindow(display, win);
310 x_out = MAX(0, MIN(x_out, 48));
311 y_out = MAX(0, MIN(y_out, 48));
317 BC_Display::unlock_display();
321 void BC_DisplayInfo::init_borders()
325 BC_DisplayInfo display_info;
326 display_info.test_window(left_border, top_border,
327 auto_reposition_x, auto_reposition_y, 100, 100);
328 right_border = left_border;
329 bottom_border = left_border;
330 //printf("BC_DisplayInfo::init_borders border=%d %d auto=%d %d\n",
331 // left_border, top_border, auto_reposition_x, auto_reposition_y);
336 int BC_DisplayInfo::get_top_border()
342 int BC_DisplayInfo::get_left_border()
348 int BC_DisplayInfo::get_right_border()
354 int BC_DisplayInfo::get_bottom_border()
357 return bottom_border;
360 const char *BC_DisplayInfo::get_gl_shader_version()
363 return gl_shader_version;
366 void BC_DisplayInfo::init_window(const char *display_name, int show_error)
368 if(display_name && display_name[0] == 0) display_name = NULL;
371 display = BC_Display::get_display(display_name);
374 // This function must be the first Xlib
375 // function a multi-threaded program calls
378 if((display = XOpenDisplay(display_name)) == NULL)
380 if(!show_error) return;
381 fprintf(stderr,_("BC_DisplayInfo::init_window: cannot open display \"%s\".\n"),
382 display_name ? display_name : "");
383 if(getenv("DISPLAY") == NULL)
384 fprintf(stderr, _("'DISPLAY' environment variable not set.\n"));
385 if((display = XOpenDisplay(0)) == NULL) {
386 fprintf(stderr,_("BC_DisplayInfo::init_window: cannot connect to X server.\n"));
390 #endif // SINGLE_THREAD
393 BC_Display::lock_display("BC_DisplayInfo::init_window");
395 scrnum = DefaultScreen(display);
396 rootwin = RootWindow(display, scrnum);
397 vis = DefaultVisual(display, scrnum);
398 depth = DefaultDepth(display, scrnum);
400 BC_Display::unlock_display();
401 #endif // SINGLE_THREAD
405 int BC_DisplayInfo::get_root_w()
408 BC_Display::lock_display("BC_DisplayInfo::get_root_w");
410 Screen *screen_ptr = XDefaultScreenOfDisplay(display);
411 int result = WidthOfScreen(screen_ptr);
413 BC_Display::unlock_display();
418 int BC_DisplayInfo::get_root_h()
421 BC_Display::lock_display("BC_DisplayInfo::get_root_h");
423 Screen *screen_ptr = XDefaultScreenOfDisplay(display);
424 int result = HeightOfScreen(screen_ptr);
426 BC_Display::unlock_display();
431 int BC_DisplayInfo::get_abs_cursor_x()
433 int abs_x, abs_y, win_x, win_y;
434 unsigned int temp_mask;
438 BC_Display::lock_display("BC_DisplayInfo::get_abs_cursor_x");
440 XQueryPointer(display, rootwin, &temp_win, &temp_win,
441 &abs_x, &abs_y, &win_x, &win_y, &temp_mask);
443 BC_Display::unlock_display();
448 int BC_DisplayInfo::get_abs_cursor_y()
450 int abs_x, abs_y, win_x, win_y;
451 unsigned int temp_mask;
455 BC_Display::lock_display("BC_DisplayInfo::get_abs_cursor_y");
457 XQueryPointer(display, rootwin, &temp_win, &temp_win,
458 &abs_x, &abs_y, &win_x, &win_y, &temp_mask);
460 BC_Display::unlock_display();
466 int BC_DisplayInfo::get_screen_count()
468 return XScreenCount(display);
472 const char *BC_DisplayInfo::host_display_name(const char *display_name)
474 return XDisplayName(display_name);