4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
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"
36 pthread_mutex_t BC_Display::display_lock = PTHREAD_MUTEX_INITIALIZER;
37 BC_Display* BC_Display::display_global = 0;
39 BC_Display::BC_Display(const char *display_name)
44 translation_events = 0;
48 // This function must be the first Xlib
49 // function a multi-threaded program calls
52 if(display_name && display_name[0] == 0) display_name = NULL;
53 if((display = XOpenDisplay(display_name)) == NULL)
55 printf("BC_Display::BC_Display: cannot connect to X server %s\n",
57 if(getenv("DISPLAY") == NULL)
59 printf("'DISPLAY' environment variable not set.\n");
63 // Try again with default display.
65 if((display = XOpenDisplay(0)) == NULL)
67 printf("BC_Display::BC_Display: cannot connect to default X server.\n");
73 // Create atoms for events
74 SetDoneXAtom = XInternAtom(display, "BC_REPEAT_EVENT", False);
75 RepeaterXAtom = XInternAtom(display, "BC_CLOSE_EVENT", False);
76 DelWinXAtom = XInternAtom(display, "WM_DELETE_WINDOW", False);
77 ProtoXAtom = XInternAtom(display, "WM_PROTOCOLS", False);
80 // Start event handling
81 event_thread = new BC_WindowEvents(this);
82 event_thread->start();
85 event_lock = new Mutex("BC_Display::event_lock", 1);
86 event_condition = new Condition(0, "BC_Display::event_condition");
89 BC_Display::~BC_Display()
93 Display* BC_Display::get_display(const char *name)
95 pthread_mutex_lock(&display_lock);
98 display_global = new BC_Display(name);
100 pthread_mutex_unlock(&display_lock);
102 return display_global->display;
105 int BC_Display::is_first(BC_WindowBase *window)
109 if(windows.size() && windows.values[0] == window) result = 1;
114 void BC_Display::dump_windows()
116 for(int i = 0; i < windows.size(); i++)
118 printf("BC_Display::dump_windows %d window=%p window->win=%p\n",
121 windows.get(i)->win);
125 void BC_Display::new_window(BC_WindowBase *window)
127 //printf("BC_Display::new_window %d\n", __LINE__);
130 clipboard = new BC_Clipboard("");
131 clipboard->start_clipboard();
134 //printf("BC_Display::new_window %d\n", __LINE__);
135 windows.append(window);
137 //printf("BC_Display::new_window %d\n", __LINE__);
140 void BC_Display::delete_window(BC_WindowBase *window)
142 //printf("BC_Display::delete_window %d\n", __LINE__);
143 windows.remove(window);
144 //printf("BC_Display::delete_window %d\n", __LINE__);
147 // If the event happened in any subwindow
148 int BC_Display::is_event_win(XEvent *event, BC_WindowBase *window)
150 Window event_win = event->xany.window;
151 //printf("BC_Display::is_event_win %d\n", __LINE__);
152 if(event_win == 0 || window->win == event_win) return 1;
153 //printf("BC_Display::is_event_win %d\n", __LINE__);
154 for(int i = 0; i < window->subwindows->size(); i++)
156 if(is_event_win(event, window->subwindows->get(i)))
158 //printf("BC_Display::is_event_win %d\n", __LINE__);
163 // Test popups in the main window only
164 if(window->window_type == MAIN_WINDOW)
166 // Not all events are handled by popups.
167 for(int i = 0; i < window->popups.size(); i++)
169 if(window->popups.get(i)->win == event_win &&
170 event->type != ConfigureNotify) return 1;
173 //printf("BC_Display::is_event_win %d\n", __LINE__);
177 void BC_Display::loop()
180 if(debug) printf("BC_Display::loop %d\n", __LINE__);
184 // If an event is waiting, process it now.
185 if(debug) printf("BC_Display::loop %d\n", __LINE__);
186 if(get_event_count())
188 if(debug) printf("BC_Display::loop %d\n", __LINE__);
190 if(debug) printf("BC_Display::loop %d\n", __LINE__);
193 // Otherwise, process all compressed events & get the next event.
195 if(debug) printf("BC_Display::loop %d\n", __LINE__);
196 lock_display("BC_Display::loop");
197 for(int i = 0; i < windows.size(); i++)
199 BC_WindowBase *window = windows.get(i);
200 if(debug) printf("BC_Display::loop %d %d %d %d\n",
202 window->resize_events,
203 window->motion_events,
204 window->translation_events);
205 if(window->resize_events)
206 window->dispatch_resize_event(window->last_resize_w,
207 window->last_resize_h);
208 if(window->motion_events)
209 window->dispatch_motion_event();
210 if(debug) printf("BC_Display::loop %d\n", __LINE__);
211 if(window->translation_events)
212 window->dispatch_translation_event();
214 if(debug) printf("BC_Display::loop %d\n", __LINE__);
216 if(debug) printf("BC_Display::loop %d\n", __LINE__);
219 if(debug) printf("BC_Display::loop %d\n", __LINE__);
222 if(debug) printf("BC_Display::loop %d\n", __LINE__);
225 void BC_Display::handle_event()
228 XEvent *event = get_event();
229 if(debug) printf("BC_Display::handle_event %d type=%d\n",
233 lock_display("BC_Display::handle_event");
234 if(debug) printf("BC_Display::handle_event %d\n", __LINE__);
235 for(int i = 0; i < windows.size(); i++)
237 // Test if event was inside window
238 if(debug) printf("BC_Display::handle_event %d\n", __LINE__);
239 BC_WindowBase *window = windows.get(i);
240 if(debug) printf("BC_Display::handle_event %d\n", __LINE__);
241 if(is_event_win(event, window))
243 window->dispatch_event(event);
244 if(debug) printf("BC_Display::handle_event %d\n", __LINE__);
246 if(debug) printf("BC_Display::handle_event %d\n", __LINE__);
248 if(debug) printf("BC_Display::handle_event %d\n", __LINE__);
251 if(debug) printf("BC_Display::handle_event %d\n", __LINE__);
254 // Get pending events for the given window
255 int BC_Display::get_event_count(BC_WindowBase *window)
258 event_lock->lock("BC_WindowBase::get_event_count 2");
259 for(int i = 0; i < common_events.size(); i++)
261 XEvent *event = common_events.get(i);
262 if(is_event_win(event, window)) result++;
264 event_lock->unlock();
268 int BC_Display::get_event_count()
270 event_lock->lock("BC_WindowBase::get_event_count 1");
271 int result = common_events.size();
272 event_lock->unlock();
276 XEvent* BC_Display::get_event()
279 while(!done && !result)
281 //printf("BC_Display::get_event %d\n", __LINE__);
282 event_condition->lock("BC_WindowBase::get_event");
283 //printf("BC_Display::get_event %d\n", __LINE__);
284 event_lock->lock("BC_WindowBase::get_event");
285 //printf("BC_Display::get_event %d\n", __LINE__);
287 if(common_events.total && !done)
289 result = common_events.values[0];
290 common_events.remove_number(0);
293 event_lock->unlock();
298 void BC_Display::put_event(XEvent *event)
300 event_lock->lock("BC_WindowBase::put_event");
301 common_events.append(event);
302 event_lock->unlock();
303 event_condition->unlock();
306 void BC_Display::set_repeat(BC_WindowBase *window, int64_t duration)
309 // test repeater database for duplicates
310 for(int i = 0; i < repeaters.total; i++)
312 // Matching delay already exists
313 if(repeaters.values[i]->delay == duration)
315 repeaters.values[i]->start_repeating(window);
320 BC_Repeater *repeater = new BC_Repeater(window, duration);
321 repeater->initialize();
322 repeaters.append(repeater);
323 repeater->start_repeating();
326 void BC_Display::unset_repeat(BC_WindowBase *window, int64_t duration)
328 for(int i = 0; i < repeaters.size(); i++)
330 if(repeaters.get(i)->delay == duration)
332 repeaters.get(i)->stop_repeating(window);
337 int BC_Display::unset_all_repeaters(BC_WindowBase *window)
339 for(int i = 0; i < repeaters.total; i++)
341 repeaters.values[i]->stop_all_repeating(window);
345 void BC_Display::arm_repeat(int64_t duration)
347 XEvent *event = new XEvent;
348 XClientMessageEvent *ptr = (XClientMessageEvent*)event;
349 event->xany.window = 0;
350 ptr->type = ClientMessage;
351 ptr->message_type = RepeaterXAtom;
353 ptr->data.l[0] = duration;
355 // Couldn't use XSendEvent since it locked up randomly.
359 void BC_Display::arm_completion(BC_WindowBase *window)
361 XEvent *event = new XEvent;
362 XClientMessageEvent *ptr = (XClientMessageEvent*)event;
363 event->xany.window = window->win;
364 event->type = ClientMessage;
365 ptr->message_type = SetDoneXAtom;
371 void BC_Display::unlock_repeaters(int64_t duration)
373 for(int i = 0; i < repeaters.size(); i++)
375 if(repeaters.get(i)->delay == duration)
377 repeaters.get(i)->repeat_lock->unlock();
383 void BC_Display::lock_display(const char *location)
385 pthread_mutex_lock(&BC_Display::display_lock);
386 int result = ++BC_Display::display_global->window_locked;
387 pthread_mutex_unlock(&BC_Display::display_lock);
389 //printf("BC_Display::lock_display %d %s result=%d\n", __LINE__, location, result);
390 XLockDisplay(BC_Display::display_global->display);
393 void BC_Display::unlock_display()
395 pthread_mutex_lock(&BC_Display::display_lock);
396 int result = --BC_Display::display_global->window_locked;
397 // if(BC_Display::display_global->window_locked < 0)
398 // BC_Display::display_global->window_locked = 0;
399 pthread_mutex_unlock(&BC_Display::display_lock);
401 //printf("BC_Display::unlock_display %d result=%d\n", __LINE__, result);
402 /* if(result == 1) */ XUnlockDisplay(BC_Display::display_global->display);
406 int BC_Display::get_display_locked()
408 return BC_Display::display_global->window_locked;