r1053: Add Russian translation.
[cinelerra_cv.git] / guicast / bcdisplayinfo.C
blob56767d710bb6c91881086ccfca9762b6664cd2a5
1 #include "bcdisplayinfo.h"
2 #include "clip.h"
4 #include <X11/X.h>
5 #include <X11/Xlib.h>
6 #include <X11/Xutil.h>
7 #include <unistd.h>
9 #define TEST_SIZE 128
10 #define TEST_SIZE2 164
11 #define TEST_SIZE3 196
12 int BC_DisplayInfo::top_border = -1;
13 int BC_DisplayInfo::left_border = -1;
14 int BC_DisplayInfo::bottom_border = -1;
15 int BC_DisplayInfo::right_border = -1;
16 int BC_DisplayInfo::auto_reposition_x = -1;
17 int BC_DisplayInfo::auto_reposition_y = -1;
20 BC_DisplayInfo::BC_DisplayInfo(char *display_name, int show_error)
22         init_window(display_name, show_error);
25 BC_DisplayInfo::~BC_DisplayInfo()
27         XCloseDisplay(display);
31 void BC_DisplayInfo::parse_geometry(char *geom, int *x, int *y, int *width, int *height)
33         XParseGeometry(geom, x, y, (unsigned int*)width, (unsigned int*)height);
36 void BC_DisplayInfo::test_window(int &x_out, 
37         int &y_out, 
38         int &x_out2, 
39         int &y_out2, 
40         int x_in, 
41         int y_in)
43         unsigned long mask = CWEventMask | CWWinGravity;
44         XSetWindowAttributes attr;
45         XSizeHints size_hints;
47 //printf("BC_DisplayInfo::test_window 1\n");
48         x_out = 0;
49         y_out = 0;
50         x_out2 = 0;
51         y_out2 = 0;
52         attr.event_mask = StructureNotifyMask;
53         attr.win_gravity = SouthEastGravity;
54         Window win = XCreateWindow(display, 
55                         rootwin, 
56                         x_in, 
57                         y_in, 
58                         TEST_SIZE, 
59                         TEST_SIZE, 
60                         0, 
61                         default_depth, 
62                         InputOutput, 
63                         vis, 
64                         mask, 
65                         &attr);
66         XGetNormalHints(display, win, &size_hints);
67         size_hints.flags = PPosition | PSize;
68         size_hints.x = x_in;
69         size_hints.y = y_in;
70         size_hints.width = TEST_SIZE;
71         size_hints.height = TEST_SIZE;
72         XSetStandardProperties(display, 
73                 win, 
74                 "x", 
75                 "x", 
76                 None, 
77                 0, 
78                 0, 
79                 &size_hints);
81         XMapWindow(display, win); 
82         XFlush(display);
83         XSync(display, 0);
84         XMoveResizeWindow(display, 
85                 win, 
86                 x_in, 
87                 y_in,
88                 TEST_SIZE2,
89                 TEST_SIZE2);
90         XFlush(display);
91         XSync(display, 0);
93         XResizeWindow(display, 
94                 win, 
95                 TEST_SIZE3,
96                 TEST_SIZE3);
97         XFlush(display);
98         XSync(display, 0);
100         XEvent event;
101         int last_w = 0;
102         int last_h = 0;
103         int state = 0;
105         do
106         {
107                 XNextEvent(display, &event);
108 //printf("BC_DisplayInfo::test_window 1 event=%d %d\n", event.type, XPending(display));
109                 if(event.type == ConfigureNotify && event.xany.window == win)
110                 {
111 // Get creation repositioning
112                         if(last_w != event.xconfigure.width || last_h != event.xconfigure.height)
113                         {
114                                 state++;
115                                 last_w = event.xconfigure.width;
116                                 last_h = event.xconfigure.height;
117                         }
119                         if(state == 1)
120                         {
121                                 x_out = MAX(event.xconfigure.x + event.xconfigure.border_width - x_in, x_out);
122                                 y_out = MAX(event.xconfigure.y + event.xconfigure.border_width - y_in, y_out);
123                         }
124                         else
125                         if(state == 2)
126 // Get moveresize repositioning
127                         {
128                                 x_out2 = MAX(event.xconfigure.x + event.xconfigure.border_width - x_in, x_out2);
129                                 y_out2 = MAX(event.xconfigure.y + event.xconfigure.border_width - y_in, y_out2);
130                         }
131 // printf("BC_DisplayInfo::test_window 2 state=%d x_out=%d y_out=%d x_in=%d y_in=%d w=%d h=%d\n",
132 // state,
133 // event.xconfigure.x + event.xconfigure.border_width, 
134 // event.xconfigure.y + event.xconfigure.border_width, 
135 // x_in, 
136 // y_in, 
137 // event.xconfigure.width, 
138 // event.xconfigure.height);
139                 }
140         }while(state != 3);
142         XDestroyWindow(display, win);
143         XFlush(display);
144         XSync(display, 0);
146         x_out = MAX(0, x_out);
147         y_out = MAX(0, y_out);
148         x_out = MIN(x_out, 30);
149         y_out = MIN(y_out, 30);
150 //printf("BC_DisplayInfo::test_window 2\n");
153 void BC_DisplayInfo::init_borders()
155         if(top_border < 0)
156         {
158                 test_window(left_border, 
159                         top_border, 
160                         auto_reposition_x, 
161                         auto_reposition_y, 
162                         0, 
163                         0);
164                 right_border = left_border;
165                 bottom_border = left_border;
166 // printf("BC_DisplayInfo::init_borders border=%d %d auto=%d %d\n", 
167 // left_border, 
168 // top_border, 
169 // auto_reposition_x, 
170 // auto_reposition_y);
171         }
175 int BC_DisplayInfo::get_top_border()
177         init_borders();
178         return top_border;
181 int BC_DisplayInfo::get_left_border()
183         init_borders();
184         return left_border;
187 int BC_DisplayInfo::get_right_border()
189         init_borders();
190         return right_border;
193 int BC_DisplayInfo::get_bottom_border()
195         init_borders();
196         return bottom_border;
199 void BC_DisplayInfo::init_window(char *display_name, int show_error)
201         if(display_name && display_name[0] == 0) display_name = NULL;
202         
203 // This function must be the first Xlib
204 // function a multi-threaded program calls
205         XInitThreads();
207         if((display = XOpenDisplay(display_name)) == NULL)
208         {
209                 if(show_error)
210                 {
211                         printf("BC_DisplayInfo::init_window: cannot connect to X server.\n");
212                         if(getenv("DISPLAY") == NULL)
213                         printf("'DISPLAY' environment variable not set.\n");
214                         exit(1);
215                 }
216                 return;
217         }
218         
219         screen = DefaultScreen(display);
220         rootwin = RootWindow(display, screen);
221         vis = DefaultVisual(display, screen);
222         default_depth = DefaultDepth(display, screen);
226 int BC_DisplayInfo::get_root_w()
228         Screen *screen_ptr = XDefaultScreenOfDisplay(display);
229         return WidthOfScreen(screen_ptr);
232 int BC_DisplayInfo::get_root_h()
234         Screen *screen_ptr = XDefaultScreenOfDisplay(display);
235         return HeightOfScreen(screen_ptr);
238 int BC_DisplayInfo::get_abs_cursor_x()
240         int abs_x, abs_y, win_x, win_y;
241         unsigned int temp_mask;
242         Window temp_win;
244         XQueryPointer(display, 
245            rootwin, 
246            &temp_win, 
247            &temp_win,
248        &abs_x, 
249            &abs_y, 
250            &win_x, 
251            &win_y, 
252            &temp_mask);
253         return abs_x;
256 int BC_DisplayInfo::get_abs_cursor_y()
258         int abs_x, abs_y, win_x, win_y;
259         unsigned int temp_mask;
260         Window temp_win;
262         XQueryPointer(display, 
263            rootwin, 
264            &temp_win, 
265            &temp_win,
266        &abs_x, 
267            &abs_y, 
268            &win_x, 
269            &win_y, 
270            &temp_mask);
271         return abs_y;