version update, bld tweaks, resize track booby, 10fps for images
[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
45
46 BC_DisplayInfo::BC_DisplayInfo(const char *display_name, int show_error)
47 {
48         screen = -1;
49         init_window(display_name, show_error);
50 }
51
52 BC_DisplayInfo::~BC_DisplayInfo()
53 {
54 #ifndef SINGLE_THREAD
55         XCloseDisplay(display);
56 #endif
57 }
58
59
60 void BC_DisplayInfo::parse_geometry(char *geom, int *x, int *y, int *width, int *height)
61 {
62         XParseGeometry(geom, x, y, (unsigned int*)width, (unsigned int*)height);
63 }
64
65 static void get_top_coords(Display *display, Window win, int &px,int &py, int &tx,int &ty)
66 {
67         Window *pcwin = 0;  unsigned int ncwin = 0;
68         Window cwin = 0, pwin = 0, root = 0;
69         XQueryTree(display, win, &root, &pwin, &pcwin, &ncwin);
70         if( pcwin ) XFree(pcwin);
71         XTranslateCoordinates(display, pwin, root, 0,0, &px,&py, &cwin);
72 //printf(" win=%lx, px/py=%d/%d\n", win, px,py);
73
74         int nx = px, ny = py;  pwin = win;
75         for( int i=5; --i>=0; ) {
76                 win = pwin;  pwin = 0;  pcwin = 0;  ncwin = 0;
77                 Window rwin = 0;
78 // XQuerytTree has been known to fail here
79                 XQueryTree(display, win, &rwin, &pwin, &pcwin, &ncwin);
80                 if( pcwin ) XFree(pcwin);
81                 if( !rwin || rwin != root || pwin == root ) break;
82                 XTranslateCoordinates(display, pwin, root, 0,0, &nx,&ny, &cwin);
83 //printf(" win=%lx, nx/ny=%d/%d\n", win, nx,ny);
84         }
85         tx = nx;  ty = ny;
86 }
87
88
89 void BC_DisplayInfo::test_window(int &x_out,
90         int &y_out,
91         int &x_out2,
92         int &y_out2,
93         int x_in,
94         int y_in)
95 {
96 #ifdef SINGLE_THREAD
97         BC_Display::lock_display("BC_DisplayInfo::test_window");
98 #endif
99
100         x_out = 0;
101         y_out = 0;
102         int x_out1 = 0;
103         int y_out1 = 0;
104         x_out2 = 0;
105         y_out2 = 0;
106
107         unsigned long mask = CWEventMask | CWWinGravity | CWBackPixel;
108         XSetWindowAttributes attr;
109         attr.event_mask = StructureNotifyMask;
110         attr.win_gravity = SouthEastGravity;
111         attr.background_pixel = BlackPixel(display,screen);
112         Window win = XCreateWindow(display, rootwin,
113                         x_in, y_in, TEST_SIZE, TEST_SIZE,
114                         0, default_depth, InputOutput,
115                         vis, mask, &attr);
116         XSizeHints size_hints;
117         XGetNormalHints(display, win, &size_hints);
118         size_hints.flags = PPosition | PSize;
119         size_hints.x = x_in;
120         size_hints.y = y_in;
121         size_hints.width = TEST_SIZE;
122         size_hints.height = TEST_SIZE;
123         XSetStandardProperties(display, win,
124                 "x", "x", None, 0, 0, &size_hints);
125         XClearWindow(display, win);
126         XMapWindow(display, win);
127         XFlush(display);  XSync(display, 0);  usleep(100000);
128
129         XEvent event;
130         int state = 0;
131
132         while( state < 3 ) {
133                 XNextEvent(display, &event);
134 //printf("BC_DisplayInfo::test_window 1 event=%d %d\n", event.type, XPending(display));
135                 if( event.xany.window != win ) continue;
136                 if( event.type != ConfigureNotify ) continue;
137                 Window cwin = 0;
138                 int rx = 0, ry = 0, px = 0, py = 0, tx = 0, ty = 0;
139 //printf("BC_DisplayInfo::test_window 1 state=%d x=%d y=%d w=%d h=%d bw=%d sev=%d\n",
140 //  state, event.xconfigure.x, event.xconfigure.y,
141 //  event.xconfigure.width, event.xconfigure.height,
142 //  event.xconfigure.border_width, event.xconfigure.send_event);
143                 get_top_coords(display,win, px,py, tx,ty);
144 //printf("x_in,y_in=%d,%d dx,dy=%d,%d\n", x_in,y_in, x_in-tx,y_in-ty);
145                 switch( state ) {
146                 case 0: // Get creation config
147                         XTranslateCoordinates(display, win, rootwin, 0,0, &rx,&ry, &cwin);
148                         x_out = rx - x_in;
149                         y_out = ry - y_in;
150                         XMoveResizeWindow(display, win, x_in,y_in, TEST_SIZE2,TEST_SIZE2);
151                         XFlush(display);  XSync(display, 0);  usleep(100000);
152                         ++state;
153                         break;
154                 case 1: // Get moveresize resizing
155                         XTranslateCoordinates(display, win, rootwin, 0,0, &rx,&ry, &cwin);
156                         x_out1 = px;
157                         y_out1 = py;
158                         x_in += TEST_DSIZE;  y_in += TEST_DSIZE;
159                         XMoveResizeWindow(display, win, x_in,y_in, TEST_SIZE2,TEST_SIZE2);
160                         XFlush(display);  XSync(display, 0);  usleep(100000);
161                         ++state;
162                         break;
163                 case 2: // Get moveresize move
164                         XTranslateCoordinates(display, win, rootwin, 0,0, &rx,&ry, &cwin);
165                         x_out2 = px - x_out1 - TEST_DSIZE;
166                         y_out2 = py - y_out1 - TEST_DSIZE;
167                         ++state;
168                         break;
169                 }
170         }
171 //printf("\nBC_DisplayInfo::test_window 3 x0,y0=%d,%d, x1,y1=%d,%d, x2,y2=%d,%d\n",
172 //  x_out,y_out, x_out1,y_out1, x_out2,y_out2);
173 //printf("\nx_in,y_in=%d,%d\n", x_in,y_in);
174
175         XDestroyWindow(display, win);
176         XFlush(display);
177         XSync(display, 0);
178
179         x_out = MAX(0, MIN(x_out, 48));
180         y_out = MAX(0, MIN(y_out, 48));
181
182 #ifdef SINGLE_THREAD
183         BC_Display::unlock_display();
184 #endif
185 }
186
187 void BC_DisplayInfo::init_borders()
188 {
189         if(top_border < 0)
190         {
191                 BC_DisplayInfo display_info;
192                 display_info.test_window(left_border, top_border,
193                         auto_reposition_x, auto_reposition_y, 100, 100);
194                 right_border = left_border;
195                 bottom_border = left_border;
196 //printf("BC_DisplayInfo::init_borders border=%d %d auto=%d %d\n",
197 //  left_border, top_border, auto_reposition_x, auto_reposition_y);
198         }
199 }
200
201
202 int BC_DisplayInfo::get_top_border()
203 {
204         init_borders();
205         return top_border;
206 }
207
208 int BC_DisplayInfo::get_left_border()
209 {
210         init_borders();
211         return left_border;
212 }
213
214 int BC_DisplayInfo::get_right_border()
215 {
216         init_borders();
217         return right_border;
218 }
219
220 int BC_DisplayInfo::get_bottom_border()
221 {
222         init_borders();
223         return bottom_border;
224 }
225
226 void BC_DisplayInfo::init_window(const char *display_name, int show_error)
227 {
228         if(display_name && display_name[0] == 0) display_name = NULL;
229
230 #ifdef SINGLE_THREAD
231         display = BC_Display::get_display(display_name);
232 #else
233
234 // This function must be the first Xlib
235 // function a multi-threaded program calls
236         XInitThreads();
237
238         if((display = XOpenDisplay(display_name)) == NULL)
239         {
240                 if(!show_error) return;
241                 fprintf(stderr,_("BC_DisplayInfo::init_window: cannot open display \"%s\".\n"),
242                         display_name ? display_name : "");
243                 if(getenv("DISPLAY") == NULL)
244                         fprintf(stderr, _("'DISPLAY' environment variable not set.\n"));
245                 if((display = XOpenDisplay(0)) == NULL) {
246                         fprintf(stderr,_("BC_DisplayInfo::init_window: cannot connect to X server.\n"));
247                         exit(1);
248                 }
249         }
250 #endif // SINGLE_THREAD
251
252 #ifdef SINGLE_THREAD
253         BC_Display::lock_display("BC_DisplayInfo::init_window");
254 #endif
255         screen = DefaultScreen(display);
256         rootwin = RootWindow(display, screen);
257         vis = DefaultVisual(display, screen);
258         default_depth = DefaultDepth(display, screen);
259 #ifdef SINGLE_THREAD
260         BC_Display::unlock_display();
261 #endif // SINGLE_THREAD
262 }
263
264
265 int BC_DisplayInfo::get_root_w()
266 {
267 #ifdef SINGLE_THREAD
268         BC_Display::lock_display("BC_DisplayInfo::get_root_w");
269 #endif
270         Screen *screen_ptr = XDefaultScreenOfDisplay(display);
271         int result = WidthOfScreen(screen_ptr);
272 #ifdef SINGLE_THREAD
273         BC_Display::unlock_display();
274 #endif
275         return result;
276 }
277
278 int BC_DisplayInfo::get_root_h()
279 {
280 #ifdef SINGLE_THREAD
281         BC_Display::lock_display("BC_DisplayInfo::get_root_h");
282 #endif
283         Screen *screen_ptr = XDefaultScreenOfDisplay(display);
284         int result = HeightOfScreen(screen_ptr);
285 #ifdef SINGLE_THREAD
286         BC_Display::unlock_display();
287 #endif
288         return result;
289 }
290
291 int BC_DisplayInfo::get_abs_cursor_x()
292 {
293         int abs_x, abs_y, win_x, win_y;
294         unsigned int temp_mask;
295         Window temp_win;
296
297 #ifdef SINGLE_THREAD
298         BC_Display::lock_display("BC_DisplayInfo::get_abs_cursor_x");
299 #endif
300         XQueryPointer(display,
301            rootwin,
302            &temp_win,
303            &temp_win,
304        &abs_x,
305            &abs_y,
306            &win_x,
307            &win_y,
308            &temp_mask);
309 #ifdef SINGLE_THREAD
310         BC_Display::unlock_display();
311 #endif
312         return abs_x;
313 }
314
315 int BC_DisplayInfo::get_abs_cursor_y()
316 {
317         int abs_x, abs_y, win_x, win_y;
318         unsigned int temp_mask;
319         Window temp_win;
320
321 #ifdef SINGLE_THREAD
322         BC_Display::lock_display("BC_DisplayInfo::get_abs_cursor_y");
323 #endif
324         XQueryPointer(display,
325            rootwin,
326            &temp_win,
327            &temp_win,
328        &abs_x,
329            &abs_y,
330            &win_x,
331            &win_y,
332            &temp_mask);
333 #ifdef SINGLE_THREAD
334         BC_Display::unlock_display();
335 #endif
336         return abs_y;
337 }
338
339
340 int BC_DisplayInfo::get_screen_count()
341 {
342         return XScreenCount(display);
343 }
344
345
346 const char *BC_DisplayInfo::host_display_name(const char *display_name)
347 {
348         return XDisplayName(display_name);
349 }
350