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"
37 pthread_mutex_t BC_Display::display_lock = PTHREAD_MUTEX_INITIALIZER;
38 BC_Display* BC_Display::display_global = 0;
40 BC_Display::BC_Display(const char *display_name)
45 translation_events = 0;
49 // This function must be the first Xlib
50 // function a multi-threaded program calls
53 if(display_name && display_name[0] == 0) display_name = NULL;
54 if((display = XOpenDisplay(display_name)) == NULL)
56 printf("BC_Display::BC_Display: cannot connect to X server %s\n",
58 if(getenv("DISPLAY") == NULL)
60 printf(_("'DISPLAY' environment variable not set.\n"));
64 // Try again with default display.
66 if((display = XOpenDisplay(0)) == NULL)
68 printf("BC_Display::BC_Display: cannot connect to default X server.\n");
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);
81 // Start event handling
82 event_thread = new BC_WindowEvents(this);
83 event_thread->start();
86 event_lock = new Mutex("BC_Display::event_lock", 1);
87 event_condition = new Condition(0, "BC_Display::event_condition");
90 BC_Display::~BC_Display()
94 Display* BC_Display::get_display(const char *name)
96 pthread_mutex_lock(&display_lock);
99 display_global = new BC_Display(name);
101 pthread_mutex_unlock(&display_lock);
103 return display_global->display;
106 int BC_Display::is_first(BC_WindowBase *window)
110 if(windows.size() && windows.values[0] == window) result = 1;
115 void BC_Display::dump_windows()
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);
123 void BC_Display::new_window(BC_WindowBase *window)
126 clipboard = new BC_Clipboard(window);
127 clipboard->start_clipboard();
130 windows.append(window);
134 void BC_Display::delete_window(BC_WindowBase *window)
136 windows.remove(window);
139 // If the event happened in any subwindow
140 int BC_Display::is_event_win(XEvent *event, BC_WindowBase *window)
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))) {
150 // Test popups in the main window only
151 if(window->window_type == MAIN_WINDOW)
153 // Not all events are handled by popups.
154 for(int i = 0; i < window->popups.size(); i++)
156 if(window->popups.get(i)->win == event_win &&
157 event->type != ConfigureNotify) return 1;
160 //printf("BC_Display::is_event_win %d\n", __LINE__);
164 void BC_Display::loop()
169 // If an event is waiting, process it now.
170 if(get_event_count())
175 // Otherwise, process all compressed events & get the next event.
177 lock_display("BC_Display::loop");
178 for(int i = 0; i < windows.size(); i++)
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();
198 void BC_Display::handle_event()
200 XEvent *event = get_event();
201 // printf("BC_Display::handle_event %d type=%d\n", __LINE__, event->type);
203 lock_display("BC_Display::handle_event");
204 for(int i = 0; i < windows.size(); i++)
206 // Test if event was inside window
207 BC_WindowBase *window = windows.get(i);
208 if(is_event_win(event, window))
210 window->dispatch_event(event);
217 // Get pending events for the given window
218 int BC_Display::get_event_count(BC_WindowBase *window)
221 event_lock->lock("BC_WindowBase::get_event_count 2");
222 for(int i = 0; i < common_events.size(); i++)
224 XEvent *event = common_events.get(i);
225 if(is_event_win(event, window)) result++;
227 event_lock->unlock();
231 int BC_Display::get_event_count()
233 event_lock->lock("BC_WindowBase::get_event_count 1");
234 int result = common_events.size();
235 event_lock->unlock();
239 XEvent* BC_Display::get_event()
242 while(!done && !result) {
243 event_condition->lock("BC_WindowBase::get_event");
244 event_lock->lock("BC_WindowBase::get_event");
246 if(common_events.total && !done)
248 result = common_events.values[0];
249 common_events.remove_number(0);
252 event_lock->unlock();
257 void BC_Display::put_event(XEvent *event)
259 event_lock->lock("BC_WindowBase::put_event");
260 common_events.append(event);
261 event_lock->unlock();
262 event_condition->unlock();
265 void BC_Display::set_repeat(BC_WindowBase *window, int64_t duration)
268 // test repeater database for duplicates
269 for(int i = 0; i < repeaters.total; i++)
271 // Matching delay already exists
272 if(repeaters.values[i]->delay == duration)
274 repeaters.values[i]->start_repeating(window);
279 BC_Repeater *repeater = new BC_Repeater(window, duration);
280 repeater->initialize();
281 repeaters.append(repeater);
282 repeater->start_repeating();
285 void BC_Display::unset_repeat(BC_WindowBase *window, int64_t duration)
287 for(int i = 0; i < repeaters.size(); i++)
289 if(repeaters.get(i)->delay == duration)
291 repeaters.get(i)->stop_repeating(window);
296 void BC_Display::unset_all_repeaters(BC_WindowBase *window)
298 for(int i = 0; i < repeaters.total; i++)
300 repeaters.values[i]->stop_all_repeating(window);
304 void BC_Display::arm_repeat(int64_t duration)
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;
312 ptr->data.l[0] = duration;
314 // Couldn't use XSendEvent since it locked up randomly.
318 void BC_Display::arm_completion(BC_WindowBase *window)
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;
330 void BC_Display::unlock_repeaters(int64_t duration)
332 for(int i = 0; i < repeaters.size(); i++)
334 if(repeaters.get(i)->delay == duration)
336 repeaters.get(i)->repeat_lock->unlock();
342 void BC_Display::lock_display(const char *location)
344 pthread_mutex_lock(&BC_Display::display_lock);
345 ++BC_Display::display_global->window_locked;
346 pthread_mutex_unlock(&BC_Display::display_lock);
348 //printf("BC_Display::lock_display %d %s result=%d\n", __LINE__, location, result);
349 XLockDisplay(BC_Display::display_global->display);
352 void BC_Display::unlock_display()
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);
360 //printf("BC_Display::unlock_display %d result=%d\n", __LINE__, result);
361 XUnlockDisplay(BC_Display::display_global->display);
365 int BC_Display::get_display_locked()
367 return BC_Display::display_global->window_locked;