remove Features5, rework gradient plugin, fix paste_edl track title bug, gl_probe...
[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 static void get_top_coords(Display *display, Window win, int &px,int &py, int &tx,int &ty)
118 {
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);
125
126         int nx = px, ny = py;  pwin = win;
127         for( int i=5; --i>=0; ) {
128                 win = pwin;  pwin = 0;  pcwin = 0;  ncwin = 0;
129                 Window rwin = 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);
136         }
137         tx = nx;  ty = ny;
138 }
139
140
141 #ifdef HAVE_GL
142 int BC_DisplayInfo::gl_fb_config()
143 {
144 #if 0
145 // find prefered config via glxinfo: mesa_hack
146         static int attribs[] = {
147                 GLX_RGBA,
148                 GLX_RED_SIZE, 1,
149                 GLX_GREEN_SIZE, 1,
150                 GLX_BLUE_SIZE, 1,
151                 GLX_DEPTH_SIZE, 1,
152                 GLX_STENCIL_SIZE, 1,
153                 GLX_ACCUM_RED_SIZE, 1,
154                 GLX_ACCUM_GREEN_SIZE, 1,
155                 GLX_ACCUM_BLUE_SIZE, 1,
156                 GLX_ACCUM_ALPHA_SIZE, 1,
157                 GLX_DOUBLEBUFFER,
158                 None
159         };
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;
168         }
169         if( n < 0 ) {
170                 cfg = 0;
171                 if( fb_cfgs ) { XFree(fb_cfgs);  fb_cfgs = 0; }
172                 if( vis_info ) { XFree(vis_info);  vis_info = 0; }
173         }
174 #endif
175         if( !fb_cfgs ) do {
176                 int fb_attrs[] = {
177                         GLX_CONFIG_CAVEAT,      GLX_SLOW_CONFIG,
178                         GLX_DRAWABLE_TYPE,      GLX_WINDOW_BIT | GLX_PBUFFER_BIT | GLX_PIXMAP_BIT,
179                         GLX_DOUBLEBUFFER,       1,
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,
185                         GLX_RED_SIZE,           8,
186                         GLX_GREEN_SIZE,         8,
187                         GLX_BLUE_SIZE,          8,
188                         GLX_ALPHA_SIZE,         8,
189                         None
190                 };
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;
195                 fb_attrs[5] = 0;
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);
199         } while(0);
200         if( fb_cfgs && ncfgs ) {
201                 for( int i=0; !vis_info && i<ncfgs; ++i )
202                         vis_info = glXGetVisualFromFBConfig(display, cfg=fb_cfgs[i]);
203         }
204         if( vis_info ) {
205                 vis = vis_info->visual;
206                 depth = vis_info->depth;
207         }
208         else {
209                 printf("%s\n", "BC_DisplayInfo::gl_fb_config failed");
210                 cfg = 0;
211         }
212         return 0;
213 }
214
215 int BC_DisplayInfo::gl_probe(Window win)
216 {
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);
225                 if( shader_version )
226                         strncpy(gl_shader_version, shader_version, sizeof(gl_shader_version));
227         }
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);
233         return 0;
234 }
235 #endif
236
237
238 void BC_DisplayInfo::test_window(int &x_out, int &y_out, int &x_out2, int &y_out2,
239                 int x_in, int y_in)
240 {
241 #ifdef SINGLE_THREAD
242         BC_Display::lock_display("BC_DisplayInfo::test_window");
243 #endif
244 #ifdef HAVE_GL
245         gl_fb_config();
246 #endif
247         x_out = 0;
248         y_out = 0;
249         int x_out1 = 0;
250         int y_out1 = 0;
251         x_out2 = 0;
252         y_out2 = 0;
253
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,
263                         vis, mask, &attr);
264         XSizeHints size_hints;
265         XGetNormalHints(display, win, &size_hints);
266         size_hints.flags = PPosition | PSize;
267         size_hints.x = x_in;
268         size_hints.y = y_in;
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);
276         XEvent event;
277         int state = 0;
278
279         while( state < 3 ) {
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;
284                 Window cwin = 0;
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);
292                 switch( state ) {
293                 case 0: // Get creation config
294                         XTranslateCoordinates(display, win, rootwin, 0,0, &rx,&ry, &cwin);
295                         x_out = rx - x_in;
296                         y_out = ry - y_in;
297                         XMoveResizeWindow(display, win, x_in,y_in, TEST_SIZE2,TEST_SIZE2);
298                         XFlush(display);  XSync(display, 0);  usleep(100000);
299                         ++state;
300                         break;
301                 case 1: // Get moveresize resizing
302                         XTranslateCoordinates(display, win, rootwin, 0,0, &rx,&ry, &cwin);
303                         x_out1 = px;
304                         y_out1 = py;
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);
308                         ++state;
309                         break;
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;
314                         ++state;
315                         break;
316                 }
317         }
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);
321
322 #ifdef HAVE_GL
323         gl_probe(win);
324 #endif
325         XDestroyWindow(display, win);
326         XFlush(display);
327         XSync(display, 0);
328
329         x_out = MAX(0, MIN(x_out, 48));
330         y_out = MAX(0, MIN(y_out, 48));
331
332 #ifdef SINGLE_THREAD
333         BC_Display::unlock_display();
334 #endif
335 }
336
337 void BC_DisplayInfo::init_borders()
338 {
339         if(top_border < 0)
340         {
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);
348         }
349 }
350
351
352 int BC_DisplayInfo::get_top_border()
353 {
354         init_borders();
355         return top_border;
356 }
357
358 int BC_DisplayInfo::get_left_border()
359 {
360         init_borders();
361         return left_border;
362 }
363
364 int BC_DisplayInfo::get_right_border()
365 {
366         init_borders();
367         return right_border;
368 }
369
370 int BC_DisplayInfo::get_bottom_border()
371 {
372         init_borders();
373         return bottom_border;
374 }
375
376 const char *BC_DisplayInfo::get_gl_shader_version()
377 {
378         init_borders();
379         return gl_shader_version;
380 }
381
382 void BC_DisplayInfo::init_window(const char *display_name, int show_error)
383 {
384         if(display_name && display_name[0] == 0) display_name = NULL;
385
386 #ifdef SINGLE_THREAD
387         display = BC_Display::get_display(display_name);
388 #else
389
390 // This function must be the first Xlib
391 // function a multi-threaded program calls
392         XInitThreads();
393
394         if((display = XOpenDisplay(display_name)) == NULL)
395         {
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"));
403                         exit(1);
404                 }
405         }
406 #endif // SINGLE_THREAD
407
408 #ifdef SINGLE_THREAD
409         BC_Display::lock_display("BC_DisplayInfo::init_window");
410 #endif
411         scrnum = DefaultScreen(display);
412         rootwin = RootWindow(display, scrnum);
413         vis = DefaultVisual(display, scrnum);
414         depth = DefaultDepth(display, scrnum);
415 #ifdef SINGLE_THREAD
416         BC_Display::unlock_display();
417 #endif // SINGLE_THREAD
418 }
419
420
421 int BC_DisplayInfo::get_root_w()
422 {
423 #ifdef SINGLE_THREAD
424         BC_Display::lock_display("BC_DisplayInfo::get_root_w");
425 #endif
426         Screen *screen_ptr = XDefaultScreenOfDisplay(display);
427         int result = WidthOfScreen(screen_ptr);
428 #ifdef SINGLE_THREAD
429         BC_Display::unlock_display();
430 #endif
431         return result;
432 }
433
434 int BC_DisplayInfo::get_root_h()
435 {
436 #ifdef SINGLE_THREAD
437         BC_Display::lock_display("BC_DisplayInfo::get_root_h");
438 #endif
439         Screen *screen_ptr = XDefaultScreenOfDisplay(display);
440         int result = HeightOfScreen(screen_ptr);
441 #ifdef SINGLE_THREAD
442         BC_Display::unlock_display();
443 #endif
444         return result;
445 }
446
447 int BC_DisplayInfo::get_abs_cursor_x()
448 {
449         int abs_x, abs_y, win_x, win_y;
450         unsigned int temp_mask;
451         Window temp_win;
452
453 #ifdef SINGLE_THREAD
454         BC_Display::lock_display("BC_DisplayInfo::get_abs_cursor_x");
455 #endif
456         XQueryPointer(display, rootwin, &temp_win, &temp_win,
457                         &abs_x, &abs_y, &win_x, &win_y, &temp_mask);
458 #ifdef SINGLE_THREAD
459         BC_Display::unlock_display();
460 #endif
461         return abs_x;
462 }
463
464 int BC_DisplayInfo::get_abs_cursor_y()
465 {
466         int abs_x, abs_y, win_x, win_y;
467         unsigned int temp_mask;
468         Window temp_win;
469
470 #ifdef SINGLE_THREAD
471         BC_Display::lock_display("BC_DisplayInfo::get_abs_cursor_y");
472 #endif
473         XQueryPointer(display, rootwin, &temp_win, &temp_win,
474                         &abs_x, &abs_y, &win_x, &win_y, &temp_mask);
475 #ifdef SINGLE_THREAD
476         BC_Display::unlock_display();
477 #endif
478         return abs_y;
479 }
480
481
482 int BC_DisplayInfo::get_screen_count()
483 {
484         return XScreenCount(display);
485 }
486
487
488 const char *BC_DisplayInfo::host_display_name(const char *display_name)
489 {
490         return XDisplayName(display_name);
491 }
492