improve delays created by vicon drawing locks, reset_cache segv fix, gang track toolt...
[goodguy/cinelerra.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         if( !clipboard ) {
126                 clipboard = new BC_Clipboard(window);
127                 clipboard->start_clipboard();
128         }
129
130         windows.append(window);
131 //      dump_windows();
132 }
133
134 void BC_Display::delete_window(BC_WindowBase *window)
135 {
136         windows.remove(window);
137 }
138
139 // If the event happened in any subwindow
140 int BC_Display::is_event_win(XEvent *event, BC_WindowBase *window)
141 {
142         Window event_win = event->xany.window;
143         if(event_win == 0 || window->win == event_win) return 1;
144         for(int i = 0; i < window->subwindows->size(); i++) {
145                 if(is_event_win(event, window->subwindows->get(i))) {
146                         return 1;
147                 }
148         }
149
150 // Test popups in the main window only
151         if(window->window_type == MAIN_WINDOW)
152         {
153 // Not all events are handled by popups.
154                 for(int i = 0; i < window->popups.size(); i++)
155                 {
156                         if(window->popups.get(i)->win == event_win &&
157                                 event->type != ConfigureNotify) return 1;
158                 }
159         }
160 //printf("BC_Display::is_event_win %d\n", __LINE__);
161         return 0;
162 }
163
164 void BC_Display::loop()
165 {
166
167         while(!done)
168         {
169 // If an event is waiting, process it now.
170                 if(get_event_count())
171                 {
172                         handle_event();
173                 }
174                 else
175 // Otherwise, process all compressed events & get the next event.
176                 {
177                         lock_display("BC_Display::loop");
178                         for(int i = 0; i < windows.size(); i++)
179                         {
180                                 BC_WindowBase *window = windows.get(i);
181 // printf("BC_Display::loop %d %d %d %d\n", __LINE__,
182 // window->resize_events, window->motion_events, window->translation_events);
183                                 if(window->resize_events)
184                                         window->dispatch_resize_event(window->last_resize_w,
185                                                 window->last_resize_h);
186                                 if(window->motion_events)
187                                         window->dispatch_motion_event();
188                                 if(window->translation_events)
189                                         window->dispatch_translation_event();
190                         }
191                         unlock_display();
192
193                         handle_event();
194                 }
195         }
196 }
197
198 void BC_Display::handle_event()
199 {
200         XEvent *event = get_event();
201 // printf("BC_Display::handle_event %d type=%d\n", __LINE__, event->type);
202
203         lock_display("BC_Display::handle_event");
204         for(int i = 0; i < windows.size(); i++)
205         {
206 // Test if event was inside window
207                 BC_WindowBase *window = windows.get(i);
208                 if(is_event_win(event, window))
209 // Dispatch event
210                         window->dispatch_event(event);
211         }
212         unlock_display();
213
214         delete event;
215 }
216
217 // Get pending events for the given window
218 int BC_Display::get_event_count(BC_WindowBase *window)
219 {
220         int result = 0;
221         event_lock->lock("BC_WindowBase::get_event_count 2");
222         for(int i = 0; i < common_events.size(); i++)
223         {
224                 XEvent *event = common_events.get(i);
225                 if(is_event_win(event, window)) result++;
226         }
227         event_lock->unlock();
228         return result;
229 }
230
231 int BC_Display::get_event_count()
232 {
233         event_lock->lock("BC_WindowBase::get_event_count 1");
234         int result = common_events.size();
235         event_lock->unlock();
236         return result;
237 }
238
239 XEvent* BC_Display::get_event()
240 {
241         XEvent *result = 0;
242         while(!done && !result) {
243                 event_condition->lock("BC_WindowBase::get_event");
244                 event_lock->lock("BC_WindowBase::get_event");
245
246                 if(common_events.total && !done)
247                 {
248                         result = common_events.values[0];
249                         common_events.remove_number(0);
250                 }
251
252                 event_lock->unlock();
253         }
254         return result;
255 }
256
257 void BC_Display::put_event(XEvent *event)
258 {
259         event_lock->lock("BC_WindowBase::put_event");
260         common_events.append(event);
261         event_lock->unlock();
262         event_condition->unlock();
263 }
264
265 void BC_Display::set_repeat(BC_WindowBase *window, int64_t duration)
266 {
267 return;
268 // test repeater database for duplicates
269         for(int i = 0; i < repeaters.total; i++)
270         {
271 // Matching delay already exists
272                 if(repeaters.values[i]->delay == duration)
273                 {
274                         repeaters.values[i]->start_repeating(window);
275                         return;
276                 }
277         }
278
279         BC_Repeater *repeater = new BC_Repeater(window, duration);
280         repeater->initialize();
281         repeaters.append(repeater);
282     repeater->start_repeating();
283 }
284
285 void BC_Display::unset_repeat(BC_WindowBase *window, int64_t duration)
286 {
287         for(int i = 0; i < repeaters.size(); i++)
288         {
289                 if(repeaters.get(i)->delay == duration)
290                 {
291                         repeaters.get(i)->stop_repeating(window);
292                 }
293         }
294 }
295
296 void BC_Display::unset_all_repeaters(BC_WindowBase *window)
297 {
298         for(int i = 0; i < repeaters.total; i++)
299         {
300                 repeaters.values[i]->stop_all_repeating(window);
301         }
302 }
303
304 void BC_Display::arm_repeat(int64_t duration)
305 {
306         XEvent *event = BC_WindowBase::new_xevent();
307         XClientMessageEvent *ptr = (XClientMessageEvent*)event;
308         event->xany.window = 0;
309         ptr->type = ClientMessage;
310         ptr->message_type = RepeaterXAtom;
311         ptr->format = 32;
312         ptr->data.l[0] = duration;
313
314 // Couldn't use XSendEvent since it locked up randomly.
315         put_event(event);
316 }
317
318 void BC_Display::arm_completion(BC_WindowBase *window)
319 {
320         XEvent *event = BC_WindowBase::new_xevent();
321         XClientMessageEvent *ptr = (XClientMessageEvent*)event;
322         event->xany.window = window->win;
323         event->type = ClientMessage;
324         ptr->message_type = SetDoneXAtom;
325         ptr->format = 32;
326
327         put_event(event);
328 }
329
330 void BC_Display::unlock_repeaters(int64_t duration)
331 {
332         for(int i = 0; i < repeaters.size(); i++)
333         {
334                 if(repeaters.get(i)->delay == duration)
335                 {
336                         repeaters.get(i)->repeat_lock->unlock();
337                 }
338         }
339 }
340
341
342 void BC_Display::lock_display(const char *location)
343 {
344         pthread_mutex_lock(&BC_Display::display_lock);
345         ++BC_Display::display_global->window_locked;
346         pthread_mutex_unlock(&BC_Display::display_lock);
347
348 //printf("BC_Display::lock_display %d %s result=%d\n", __LINE__, location, result);
349         XLockDisplay(BC_Display::display_global->display);
350 }
351
352 void BC_Display::unlock_display()
353 {
354         pthread_mutex_lock(&BC_Display::display_lock);
355         --BC_Display::display_global->window_locked;
356 //      if(BC_Display::display_global->window_locked < 0)
357 //              BC_Display::display_global->window_locked = 0;
358         pthread_mutex_unlock(&BC_Display::display_lock);
359
360 //printf("BC_Display::unlock_display %d result=%d\n", __LINE__, result);
361         XUnlockDisplay(BC_Display::display_global->display);
362 }
363
364
365 int BC_Display::get_display_locked()
366 {
367         return BC_Display::display_global->window_locked;
368 }
369
370
371 #endif