drag win rework, new search, igor theme fixes, sams preset icon
[goodguy/history.git] / cinelerra-5.1 / guicast / bcdisplay.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 "bcclipboard.h"
23 #include "bcdisplay.h"
24 #include "bcrepeater.h"
25 #include "bcsignals.h"
26 #include "bcsubwindow.h"
27 #include "bcwindowbase.h"
28 #include "bcwindowevents.h"
29 #include "condition.h"
30 #include "language.h"
31 #include "mutex.h"
32
33 #include <pthread.h>
34
35 #ifdef SINGLE_THREAD
36
37 pthread_mutex_t BC_Display::display_lock = PTHREAD_MUTEX_INITIALIZER;
38 BC_Display* BC_Display::display_global = 0;
39
40 BC_Display::BC_Display(const char *display_name)
41 {
42         done = 0;
43         motion_events = 0;
44         resize_events = 0;
45         translation_events = 0;
46         window_locked = 0;
47         clipboard = 0;
48
49 // This function must be the first Xlib
50 // function a multi-threaded program calls
51         XInitThreads();
52
53         if(display_name && display_name[0] == 0) display_name = NULL;
54         if((display = XOpenDisplay(display_name)) == NULL)
55         {
56                 printf("BC_Display::BC_Display: cannot connect to X server %s\n",
57                         display_name);
58                 if(getenv("DISPLAY") == NULL)
59         {
60                         printf(_("'DISPLAY' environment variable not set.\n"));
61                         exit(1);
62                 }
63                 else
64 // Try again with default display.
65                 {
66                         if((display = XOpenDisplay(0)) == NULL)
67                         {
68                                 printf("BC_Display::BC_Display: cannot connect to default X server.\n");
69                                 exit(1);
70                         }
71                 }
72         }
73
74 // Create atoms for events
75         SetDoneXAtom = XInternAtom(display, "BC_REPEAT_EVENT", False);
76         RepeaterXAtom = XInternAtom(display, "BC_CLOSE_EVENT", False);
77         DelWinXAtom = XInternAtom(display, "WM_DELETE_WINDOW", False);
78         ProtoXAtom = XInternAtom(display, "WM_PROTOCOLS", False);
79
80
81 // Start event handling
82         event_thread = new BC_WindowEvents(this);
83         event_thread->start();
84
85
86         event_lock = new Mutex("BC_Display::event_lock", 1);
87         event_condition = new Condition(0, "BC_Display::event_condition");
88 }
89
90 BC_Display::~BC_Display()
91 {
92 }
93
94 Display* BC_Display::get_display(const char *name)
95 {
96         pthread_mutex_lock(&display_lock);
97         if(!display_global)
98         {
99                 display_global = new BC_Display(name);
100         }
101         pthread_mutex_unlock(&display_lock);
102
103         return display_global->display;
104 }
105
106 int BC_Display::is_first(BC_WindowBase *window)
107 {
108         int result = 0;
109
110         if(windows.size() && windows.values[0] == window) result = 1;
111
112         return result;
113 }
114
115 void BC_Display::dump_windows()
116 {
117         for(int i = 0; i < windows.size(); i++) {
118                 printf("BC_Display::dump_windows %d window=%p window->win=0x%08jx\n",
119                         i, windows.get(i), windows.get(i)->win);
120         }
121 }
122
123 void BC_Display::new_window(BC_WindowBase *window)
124 {
125 //printf("BC_Display::new_window %d\n", __LINE__);
126         if(!clipboard)
127         {
128                 clipboard = new BC_Clipboard(window);
129                 clipboard->start_clipboard();
130         }
131
132 //printf("BC_Display::new_window %d\n", __LINE__);
133         windows.append(window);
134 //      dump_windows();
135 //printf("BC_Display::new_window %d\n", __LINE__);
136 }
137
138 void BC_Display::delete_window(BC_WindowBase *window)
139 {
140 //printf("BC_Display::delete_window %d\n", __LINE__);
141         windows.remove(window);
142 //printf("BC_Display::delete_window %d\n", __LINE__);
143 }
144
145 // If the event happened in any subwindow
146 int BC_Display::is_event_win(XEvent *event, BC_WindowBase *window)
147 {
148         Window event_win = event->xany.window;
149 //printf("BC_Display::is_event_win %d\n", __LINE__);
150         if(event_win == 0 || window->win == event_win) return 1;
151 //printf("BC_Display::is_event_win %d\n", __LINE__);
152         for(int i = 0; i < window->subwindows->size(); i++)
153         {
154                 if(is_event_win(event, window->subwindows->get(i)))
155                 {
156 //printf("BC_Display::is_event_win %d\n", __LINE__);
157                         return 1;
158                 }
159         }
160
161 // Test popups in the main window only
162         if(window->window_type == MAIN_WINDOW)
163         {
164 // Not all events are handled by popups.
165                 for(int i = 0; i < window->popups.size(); i++)
166                 {
167                         if(window->popups.get(i)->win == event_win &&
168                                 event->type != ConfigureNotify) return 1;
169                 }
170         }
171 //printf("BC_Display::is_event_win %d\n", __LINE__);
172         return 0;
173 }
174
175 void BC_Display::loop()
176 {
177         const int debug = 0;
178 if(debug) printf("BC_Display::loop %d\n", __LINE__);
179
180         while(!done)
181         {
182 // If an event is waiting, process it now.
183 if(debug) printf("BC_Display::loop %d\n", __LINE__);
184                 if(get_event_count())
185                 {
186 if(debug) printf("BC_Display::loop %d\n", __LINE__);
187                         handle_event();
188 if(debug) printf("BC_Display::loop %d\n", __LINE__);
189                 }
190                 else
191 // Otherwise, process all compressed events & get the next event.
192                 {
193 if(debug) printf("BC_Display::loop %d\n", __LINE__);
194                         lock_display("BC_Display::loop");
195                         for(int i = 0; i < windows.size(); i++)
196                         {
197                                 BC_WindowBase *window = windows.get(i);
198 if(debug) printf("BC_Display::loop %d %d %d %d\n",
199 __LINE__,
200 window->resize_events,
201 window->motion_events,
202 window->translation_events);
203                                 if(window->resize_events)
204                                         window->dispatch_resize_event(window->last_resize_w,
205                                                 window->last_resize_h);
206                                 if(window->motion_events)
207                                         window->dispatch_motion_event();
208 if(debug) printf("BC_Display::loop %d\n", __LINE__);
209                                 if(window->translation_events)
210                                         window->dispatch_translation_event();
211                         }
212 if(debug) printf("BC_Display::loop %d\n", __LINE__);
213                         unlock_display();
214 if(debug) printf("BC_Display::loop %d\n", __LINE__);
215
216                         handle_event();
217 if(debug) printf("BC_Display::loop %d\n", __LINE__);
218                 }
219         }
220 if(debug) printf("BC_Display::loop %d\n", __LINE__);
221 }
222
223 void BC_Display::handle_event()
224 {
225 const int debug = 0;
226         XEvent *event = get_event();
227 if(debug)  printf("BC_Display::handle_event %d type=%d\n",
228 __LINE__,
229 event->type);
230
231         lock_display("BC_Display::handle_event");
232 if(debug) printf("BC_Display::handle_event %d\n", __LINE__);
233         for(int i = 0; i < windows.size(); i++)
234         {
235 // Test if event was inside window
236 if(debug) printf("BC_Display::handle_event %d\n", __LINE__);
237                 BC_WindowBase *window = windows.get(i);
238 if(debug) printf("BC_Display::handle_event %d\n", __LINE__);
239                 if(is_event_win(event, window))
240 // Dispatch event
241                         window->dispatch_event(event);
242 if(debug) printf("BC_Display::handle_event %d\n", __LINE__);
243         }
244 if(debug) printf("BC_Display::handle_event %d\n", __LINE__);
245         unlock_display();
246 if(debug) printf("BC_Display::handle_event %d\n", __LINE__);
247
248         delete event;
249 if(debug) printf("BC_Display::handle_event %d\n", __LINE__);
250 }
251
252 // Get pending events for the given window
253 int BC_Display::get_event_count(BC_WindowBase *window)
254 {
255         int result = 0;
256         event_lock->lock("BC_WindowBase::get_event_count 2");
257         for(int i = 0; i < common_events.size(); i++)
258         {
259                 XEvent *event = common_events.get(i);
260                 if(is_event_win(event, window)) result++;
261         }
262         event_lock->unlock();
263         return result;
264 }
265
266 int BC_Display::get_event_count()
267 {
268         event_lock->lock("BC_WindowBase::get_event_count 1");
269         int result = common_events.size();
270         event_lock->unlock();
271         return result;
272 }
273
274 XEvent* BC_Display::get_event()
275 {
276         XEvent *result = 0;
277         while(!done && !result)
278         {
279 //printf("BC_Display::get_event %d\n", __LINE__);
280                 event_condition->lock("BC_WindowBase::get_event");
281 //printf("BC_Display::get_event %d\n", __LINE__);
282                 event_lock->lock("BC_WindowBase::get_event");
283 //printf("BC_Display::get_event %d\n", __LINE__);
284
285                 if(common_events.total && !done)
286                 {
287                         result = common_events.values[0];
288                         common_events.remove_number(0);
289                 }
290
291                 event_lock->unlock();
292         }
293         return result;
294 }
295
296 void BC_Display::put_event(XEvent *event)
297 {
298         event_lock->lock("BC_WindowBase::put_event");
299         common_events.append(event);
300         event_lock->unlock();
301         event_condition->unlock();
302 }
303
304 void BC_Display::set_repeat(BC_WindowBase *window, int64_t duration)
305 {
306 return;
307 // test repeater database for duplicates
308         for(int i = 0; i < repeaters.total; i++)
309         {
310 // Matching delay already exists
311                 if(repeaters.values[i]->delay == duration)
312                 {
313                         repeaters.values[i]->start_repeating(window);
314                         return;
315                 }
316         }
317
318         BC_Repeater *repeater = new BC_Repeater(window, duration);
319         repeater->initialize();
320         repeaters.append(repeater);
321     repeater->start_repeating();
322 }
323
324 void BC_Display::unset_repeat(BC_WindowBase *window, int64_t duration)
325 {
326         for(int i = 0; i < repeaters.size(); i++)
327         {
328                 if(repeaters.get(i)->delay == duration)
329                 {
330                         repeaters.get(i)->stop_repeating(window);
331                 }
332         }
333 }
334
335 void BC_Display::unset_all_repeaters(BC_WindowBase *window)
336 {
337         for(int i = 0; i < repeaters.total; i++)
338         {
339                 repeaters.values[i]->stop_all_repeating(window);
340         }
341 }
342
343 void BC_Display::arm_repeat(int64_t duration)
344 {
345         XEvent *event = BC_WindowBase::new_xevent();
346         XClientMessageEvent *ptr = (XClientMessageEvent*)event;
347         event->xany.window = 0;
348         ptr->type = ClientMessage;
349         ptr->message_type = RepeaterXAtom;
350         ptr->format = 32;
351         ptr->data.l[0] = duration;
352
353 // Couldn't use XSendEvent since it locked up randomly.
354         put_event(event);
355 }
356
357 void BC_Display::arm_completion(BC_WindowBase *window)
358 {
359         XEvent *event = BC_WindowBase::new_xevent();
360         XClientMessageEvent *ptr = (XClientMessageEvent*)event;
361         event->xany.window = window->win;
362         event->type = ClientMessage;
363         ptr->message_type = SetDoneXAtom;
364         ptr->format = 32;
365
366         put_event(event);
367 }
368
369 void BC_Display::unlock_repeaters(int64_t duration)
370 {
371         for(int i = 0; i < repeaters.size(); i++)
372         {
373                 if(repeaters.get(i)->delay == duration)
374                 {
375                         repeaters.get(i)->repeat_lock->unlock();
376                 }
377         }
378 }
379
380
381 void BC_Display::lock_display(const char *location)
382 {
383         pthread_mutex_lock(&BC_Display::display_lock);
384         ++BC_Display::display_global->window_locked;
385         pthread_mutex_unlock(&BC_Display::display_lock);
386
387 //printf("BC_Display::lock_display %d %s result=%d\n", __LINE__, location, result);
388         XLockDisplay(BC_Display::display_global->display);
389 }
390
391 void BC_Display::unlock_display()
392 {
393         pthread_mutex_lock(&BC_Display::display_lock);
394         --BC_Display::display_global->window_locked;
395 //      if(BC_Display::display_global->window_locked < 0)
396 //              BC_Display::display_global->window_locked = 0;
397         pthread_mutex_unlock(&BC_Display::display_lock);
398
399 //printf("BC_Display::unlock_display %d result=%d\n", __LINE__, result);
400         XUnlockDisplay(BC_Display::display_global->display);
401 }
402
403
404 int BC_Display::get_display_locked()
405 {
406         return BC_Display::display_global->window_locked;
407 }
408
409
410 #endif