fcee206668d6e7b1e65893d5a97e9e9a06fb6210
[goodguy/cinelerra.git] / cinelerra-5.1 / guicast / bcdisplayinfo.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include "bcdisplay.h"
23 #include "bcdisplayinfo.h"
24 #include "bcsignals.h"
25 #include "bcwindowbase.h"
26 #include "clip.h"
27 #include "language.h"
28
29 #include <X11/X.h>
30 #include <X11/Xlib.h>
31 #include <X11/Xutil.h>
32 #include <unistd.h>
33
34 #define TEST_SIZE 128
35 #define TEST_DSIZE 28
36 #define TEST_SIZE2 164
37
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, };
45
46 BC_DisplayInfo::BC_DisplayInfo(const char *display_name, int show_error)
47 {
48         vis = 0;
49         depth = 0;
50         scrnum = -1;
51         xinerama_screens = -1;
52         xinerama_info = 0;
53         init_window(display_name, show_error);
54 #ifdef HAVE_GL
55         ncfgs = 0;
56         fb_cfgs = 0;
57         cfg = 0;
58         vis_info = 0;
59 #endif
60 }
61
62 BC_DisplayInfo::~BC_DisplayInfo()
63 {
64         if( xinerama_info ) XFree(xinerama_info);
65 #ifndef SINGLE_THREAD
66         XCloseDisplay(display);
67 #endif
68 }
69
70
71 void BC_DisplayInfo::parse_geometry(char *geom, int *x, int *y, int *width, int *height)
72 {
73         XParseGeometry(geom, x, y, (unsigned int*)width, (unsigned int*)height);
74 }
75
76
77 int BC_DisplayInfo::get_xinerama_screens()
78 {
79         if( xinerama_screens < 0 ) {
80                 xinerama_screens = 0;
81                 if( XineramaIsActive(display) )
82                         xinerama_info = XineramaQueryScreens(display, &xinerama_screens);
83         }
84         return xinerama_screens;
85 }
86
87 int BC_DisplayInfo::xinerama_geometry(int screen, int &x, int &y, int &w, int &h)
88 {
89         int screens = get_xinerama_screens();
90         if( !screens ) return 1;
91         if( screen >= 0 ) {
92                 int k = screens;
93                 while( --k >= 0 && xinerama_info[k].screen_number != screen );
94                 if( k < 0 ) return 1;
95                 x = xinerama_info[k].x_org;  w = xinerama_info[k].width;
96                 y = xinerama_info[k].y_org;  h = xinerama_info[k].height;
97         }
98         else {
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;
110                 }
111                 x = sx0;  w = sx1 - sx0;
112                 y = sy0;  h = sy1 - sy0;
113         }
114         return 0;
115 }
116
117 int BC_DisplayInfo::xinerama_big_screen()
118 {
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;
123                 int sz = w * h;
124                 if( sz > best ) {
125                         ret = xinerama_info[k].screen_number;
126                         best = sz;
127                 }
128         }
129         return ret;
130 }
131
132 static void get_top_coords(Display *display, Window win, int &px,int &py, int &tx,int &ty)
133 {
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);
140
141         int nx = px, ny = py;  pwin = win;
142         for( int i=5; --i>=0; ) {
143                 win = pwin;  pwin = 0;  pcwin = 0;  ncwin = 0;
144                 Window rwin = 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);
151         }
152         tx = nx;  ty = ny;
153 }
154
155
156 #ifdef HAVE_GL
157 int BC_DisplayInfo::gl_fb_config()
158 {
159 #if 0
160 // find prefered config via glxinfo: mesa_hack
161         static int attribs[] = {
162                 GLX_RGBA,
163                 GLX_RED_SIZE, 1,
164                 GLX_GREEN_SIZE, 1,
165                 GLX_BLUE_SIZE, 1,
166                 GLX_DEPTH_SIZE, 1,
167                 GLX_STENCIL_SIZE, 1,
168                 GLX_ACCUM_RED_SIZE, 1,
169                 GLX_ACCUM_GREEN_SIZE, 1,
170                 GLX_ACCUM_BLUE_SIZE, 1,
171                 GLX_ACCUM_ALPHA_SIZE, 1,
172                 GLX_DOUBLEBUFFER,
173                 None
174         };
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;
183         }
184         if( n < 0 ) {
185                 cfg = 0;
186                 if( fb_cfgs ) { XFree(fb_cfgs);  fb_cfgs = 0; }
187                 if( vis_info ) { XFree(vis_info);  vis_info = 0; }
188         }
189 #endif
190         if( !fb_cfgs ) do {
191                 int fb_attrs[] = {
192                         GLX_CONFIG_CAVEAT,      GLX_SLOW_CONFIG,
193                         GLX_DRAWABLE_TYPE,      GLX_WINDOW_BIT | GLX_PBUFFER_BIT | GLX_PIXMAP_BIT,
194                         GLX_DOUBLEBUFFER,       1,
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,
200                         GLX_RED_SIZE,           8,
201                         GLX_GREEN_SIZE,         8,
202                         GLX_BLUE_SIZE,          8,
203                         GLX_ALPHA_SIZE,         8,
204                         None
205                 };
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;
210                 fb_attrs[5] = 0;
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);
214         } while(0);
215         if( fb_cfgs && ncfgs ) {
216                 for( int i=0; !vis_info && i<ncfgs; ++i )
217                         vis_info = glXGetVisualFromFBConfig(display, cfg=fb_cfgs[i]);
218         }
219         if( vis_info ) {
220                 vis = vis_info->visual;
221                 depth = vis_info->depth;
222         }
223         else {
224                 printf("%s\n", "BC_DisplayInfo::gl_fb_config failed");
225                 cfg = 0;
226         }
227         return 0;
228 }
229
230 int BC_DisplayInfo::gl_probe(Window win)
231 {
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);
240                 if( shader_version )
241                         strncpy(gl_shader_version, shader_version, sizeof(gl_shader_version));
242         }
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);
248         return 0;
249 }
250 #endif
251
252
253 void BC_DisplayInfo::test_window(int &x_out, int &y_out, int &x_out2, int &y_out2,
254                 int x_in, int y_in)
255 {
256 #ifdef SINGLE_THREAD
257         BC_Display::lock_display("BC_DisplayInfo::test_window");
258 #endif
259 #ifdef HAVE_GL
260         gl_fb_config();
261 #endif
262         x_out = 0;
263         y_out = 0;
264         int x_out1 = 0;
265         int y_out1 = 0;
266         x_out2 = 0;
267         y_out2 = 0;
268
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,
278                         vis, mask, &attr);
279         XSizeHints size_hints;
280         XGetNormalHints(display, win, &size_hints);
281         size_hints.flags = PPosition | PSize;
282         size_hints.x = x_in;
283         size_hints.y = y_in;
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);
291         XEvent event;
292         int state = 0;
293
294         while( state < 3 ) {
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;
299                 Window cwin = 0;
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);
307                 switch( state ) {
308                 case 0: // Get creation config
309                         XTranslateCoordinates(display, win, rootwin, 0,0, &rx,&ry, &cwin);
310                         x_out = rx - x_in;
311                         y_out = ry - y_in;
312                         XMoveResizeWindow(display, win, x_in,y_in, TEST_SIZE2,TEST_SIZE2);
313                         XFlush(display);  XSync(display, 0);  usleep(100000);
314                         ++state;
315                         break;
316                 case 1: // Get moveresize resizing
317                         XTranslateCoordinates(display, win, rootwin, 0,0, &rx,&ry, &cwin);
318                         x_out1 = px;
319                         y_out1 = py;
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);
323                         ++state;
324                         break;
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;
329                         ++state;
330                         break;
331                 }
332         }
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);
336
337 #ifdef HAVE_GL
338         gl_probe(win);
339 #endif
340         XDestroyWindow(display, win);
341         XFlush(display);
342         XSync(display, 0);
343
344         x_out = MAX(0, MIN(x_out, 48));
345         y_out = MAX(0, MIN(y_out, 48));
346
347 #ifdef SINGLE_THREAD
348         BC_Display::unlock_display();
349 #endif
350 }
351
352 void BC_DisplayInfo::init_borders()
353 {
354         if(top_border < 0)
355         {
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);
363         }
364 }
365
366
367 int BC_DisplayInfo::get_top_border()
368 {
369         init_borders();
370         return top_border;
371 }
372
373 int BC_DisplayInfo::get_left_border()
374 {
375         init_borders();
376         return left_border;
377 }
378
379 int BC_DisplayInfo::get_right_border()
380 {
381         init_borders();
382         return right_border;
383 }
384
385 int BC_DisplayInfo::get_bottom_border()
386 {
387         init_borders();
388         return bottom_border;
389 }
390
391 const char *BC_DisplayInfo::get_gl_shader_version()
392 {
393         init_borders();
394         return gl_shader_version;
395 }
396
397 void BC_DisplayInfo::init_window(const char *display_name, int show_error)
398 {
399         if(display_name && display_name[0] == 0) display_name = NULL;
400
401 #ifdef SINGLE_THREAD
402         display = BC_Display::get_display(display_name);
403 #else
404
405 // This function must be the first Xlib
406 // function a multi-threaded program calls
407         XInitThreads();
408
409         if((display = XOpenDisplay(display_name)) == NULL)
410         {
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"));
418                         exit(1);
419                 }
420         }
421 #endif // SINGLE_THREAD
422
423 #ifdef SINGLE_THREAD
424         BC_Display::lock_display("BC_DisplayInfo::init_window");
425 #endif
426         scrnum = DefaultScreen(display);
427         rootwin = RootWindow(display, scrnum);
428         vis = DefaultVisual(display, scrnum);
429         depth = DefaultDepth(display, scrnum);
430 #ifdef SINGLE_THREAD
431         BC_Display::unlock_display();
432 #endif // SINGLE_THREAD
433 }
434
435
436 int BC_DisplayInfo::get_root_w()
437 {
438 #ifdef SINGLE_THREAD
439         BC_Display::lock_display("BC_DisplayInfo::get_root_w");
440 #endif
441         Screen *screen_ptr = XDefaultScreenOfDisplay(display);
442         int result = WidthOfScreen(screen_ptr);
443 #ifdef SINGLE_THREAD
444         BC_Display::unlock_display();
445 #endif
446         return result;
447 }
448
449 int BC_DisplayInfo::get_root_h()
450 {
451 #ifdef SINGLE_THREAD
452         BC_Display::lock_display("BC_DisplayInfo::get_root_h");
453 #endif
454         Screen *screen_ptr = XDefaultScreenOfDisplay(display);
455         int result = HeightOfScreen(screen_ptr);
456 #ifdef SINGLE_THREAD
457         BC_Display::unlock_display();
458 #endif
459         return result;
460 }
461
462 int BC_DisplayInfo::get_abs_cursor_x()
463 {
464         int abs_x, abs_y, win_x, win_y;
465         unsigned int temp_mask;
466         Window temp_win;
467
468 #ifdef SINGLE_THREAD
469         BC_Display::lock_display("BC_DisplayInfo::get_abs_cursor_x");
470 #endif
471         XQueryPointer(display, rootwin, &temp_win, &temp_win,
472                         &abs_x, &abs_y, &win_x, &win_y, &temp_mask);
473 #ifdef SINGLE_THREAD
474         BC_Display::unlock_display();
475 #endif
476         return abs_x;
477 }
478
479 int BC_DisplayInfo::get_abs_cursor_y()
480 {
481         int abs_x, abs_y, win_x, win_y;
482         unsigned int temp_mask;
483         Window temp_win;
484
485 #ifdef SINGLE_THREAD
486         BC_Display::lock_display("BC_DisplayInfo::get_abs_cursor_y");
487 #endif
488         XQueryPointer(display, rootwin, &temp_win, &temp_win,
489                         &abs_x, &abs_y, &win_x, &win_y, &temp_mask);
490 #ifdef SINGLE_THREAD
491         BC_Display::unlock_display();
492 #endif
493         return abs_y;
494 }
495
496
497 int BC_DisplayInfo::get_screen_count()
498 {
499         return XScreenCount(display);
500 }
501
502
503 const char *BC_DisplayInfo::host_display_name(const char *display_name)
504 {
505         return XDisplayName(display_name);
506 }
507