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++)
119 printf("BC_Display::dump_windows %d window=%p window->win=%p\n",
122 windows.get(i)->win);
126 void BC_Display::new_window(BC_WindowBase *window)
128 //printf("BC_Display::new_window %d\n", __LINE__);
131 clipboard = new BC_Clipboard("");
132 clipboard->start_clipboard();
135 //printf("BC_Display::new_window %d\n", __LINE__);
136 windows.append(window);
138 //printf("BC_Display::new_window %d\n", __LINE__);
141 void BC_Display::delete_window(BC_WindowBase *window)
143 //printf("BC_Display::delete_window %d\n", __LINE__);
144 windows.remove(window);
145 //printf("BC_Display::delete_window %d\n", __LINE__);
148 // If the event happened in any subwindow
149 int BC_Display::is_event_win(XEvent *event, BC_WindowBase *window)
151 Window event_win = event->xany.window;
152 //printf("BC_Display::is_event_win %d\n", __LINE__);
153 if(event_win == 0 || window->win == event_win) return 1;
154 //printf("BC_Display::is_event_win %d\n", __LINE__);
155 for(int i = 0; i < window->subwindows->size(); i++)
157 if(is_event_win(event, window->subwindows->get(i)))
159 //printf("BC_Display::is_event_win %d\n", __LINE__);
164 // Test popups in the main window only
165 if(window->window_type == MAIN_WINDOW)
167 // Not all events are handled by popups.
168 for(int i = 0; i < window->popups.size(); i++)
170 if(window->popups.get(i)->win == event_win &&
171 event->type != ConfigureNotify) return 1;
174 //printf("BC_Display::is_event_win %d\n", __LINE__);
178 void BC_Display::loop()
181 if(debug) printf("BC_Display::loop %d\n", __LINE__);
185 // If an event is waiting, process it now.
186 if(debug) printf("BC_Display::loop %d\n", __LINE__);
187 if(get_event_count())
189 if(debug) printf("BC_Display::loop %d\n", __LINE__);
191 if(debug) printf("BC_Display::loop %d\n", __LINE__);
194 // Otherwise, process all compressed events & get the next event.
196 if(debug) printf("BC_Display::loop %d\n", __LINE__);
197 lock_display("BC_Display::loop");
198 for(int i = 0; i < windows.size(); i++)
200 BC_WindowBase *window = windows.get(i);
201 if(debug) printf("BC_Display::loop %d %d %d %d\n",
203 window->resize_events,
204 window->motion_events,
205 window->translation_events);
206 if(window->resize_events)
207 window->dispatch_resize_event(window->last_resize_w,
208 window->last_resize_h);
209 if(window->motion_events)
210 window->dispatch_motion_event();
211 if(debug) printf("BC_Display::loop %d\n", __LINE__);
212 if(window->translation_events)
213 window->dispatch_translation_event();
215 if(debug) printf("BC_Display::loop %d\n", __LINE__);
217 if(debug) printf("BC_Display::loop %d\n", __LINE__);
220 if(debug) printf("BC_Display::loop %d\n", __LINE__);
223 if(debug) printf("BC_Display::loop %d\n", __LINE__);
226 void BC_Display::handle_event()
229 XEvent *event = get_event();
230 if(debug) printf("BC_Display::handle_event %d type=%d\n",
234 lock_display("BC_Display::handle_event");
235 if(debug) printf("BC_Display::handle_event %d\n", __LINE__);
236 for(int i = 0; i < windows.size(); i++)
238 // Test if event was inside window
239 if(debug) printf("BC_Display::handle_event %d\n", __LINE__);
240 BC_WindowBase *window = windows.get(i);
241 if(debug) printf("BC_Display::handle_event %d\n", __LINE__);
242 if(is_event_win(event, window))
244 window->dispatch_event(event);
245 if(debug) printf("BC_Display::handle_event %d\n", __LINE__);
247 if(debug) printf("BC_Display::handle_event %d\n", __LINE__);
249 if(debug) printf("BC_Display::handle_event %d\n", __LINE__);
252 if(debug) printf("BC_Display::handle_event %d\n", __LINE__);
255 // Get pending events for the given window
256 int BC_Display::get_event_count(BC_WindowBase *window)
259 event_lock->lock("BC_WindowBase::get_event_count 2");
260 for(int i = 0; i < common_events.size(); i++)
262 XEvent *event = common_events.get(i);
263 if(is_event_win(event, window)) result++;
265 event_lock->unlock();
269 int BC_Display::get_event_count()
271 event_lock->lock("BC_WindowBase::get_event_count 1");
272 int result = common_events.size();
273 event_lock->unlock();
277 XEvent* BC_Display::get_event()
280 while(!done && !result)
282 //printf("BC_Display::get_event %d\n", __LINE__);
283 event_condition->lock("BC_WindowBase::get_event");
284 //printf("BC_Display::get_event %d\n", __LINE__);
285 event_lock->lock("BC_WindowBase::get_event");
286 //printf("BC_Display::get_event %d\n", __LINE__);
288 if(common_events.total && !done)
290 result = common_events.values[0];
291 common_events.remove_number(0);
294 event_lock->unlock();
299 void BC_Display::put_event(XEvent *event)
301 event_lock->lock("BC_WindowBase::put_event");
302 common_events.append(event);
303 event_lock->unlock();
304 event_condition->unlock();
307 void BC_Display::set_repeat(BC_WindowBase *window, int64_t duration)
310 // test repeater database for duplicates
311 for(int i = 0; i < repeaters.total; i++)
313 // Matching delay already exists
314 if(repeaters.values[i]->delay == duration)
316 repeaters.values[i]->start_repeating(window);
321 BC_Repeater *repeater = new BC_Repeater(window, duration);
322 repeater->initialize();
323 repeaters.append(repeater);
324 repeater->start_repeating();
327 void BC_Display::unset_repeat(BC_WindowBase *window, int64_t duration)
329 for(int i = 0; i < repeaters.size(); i++)
331 if(repeaters.get(i)->delay == duration)
333 repeaters.get(i)->stop_repeating(window);
338 int BC_Display::unset_all_repeaters(BC_WindowBase *window)
340 for(int i = 0; i < repeaters.total; i++)
342 repeaters.values[i]->stop_all_repeating(window);
346 void BC_Display::arm_repeat(int64_t duration)
348 XEvent *event = BC_WindowBase::new_xevent();
349 XClientMessageEvent *ptr = (XClientMessageEvent*)event;
350 event->xany.window = 0;
351 ptr->type = ClientMessage;
352 ptr->message_type = RepeaterXAtom;
354 ptr->data.l[0] = duration;
356 // Couldn't use XSendEvent since it locked up randomly.
360 void BC_Display::arm_completion(BC_WindowBase *window)
362 XEvent *event = BC_WindowBase::new_xevent();
363 XClientMessageEvent *ptr = (XClientMessageEvent*)event;
364 event->xany.window = window->win;
365 event->type = ClientMessage;
366 ptr->message_type = SetDoneXAtom;
372 void BC_Display::unlock_repeaters(int64_t duration)
374 for(int i = 0; i < repeaters.size(); i++)
376 if(repeaters.get(i)->delay == duration)
378 repeaters.get(i)->repeat_lock->unlock();
384 void BC_Display::lock_display(const char *location)
386 pthread_mutex_lock(&BC_Display::display_lock);
387 int result = ++BC_Display::display_global->window_locked;
388 pthread_mutex_unlock(&BC_Display::display_lock);
390 //printf("BC_Display::lock_display %d %s result=%d\n", __LINE__, location, result);
391 XLockDisplay(BC_Display::display_global->display);
394 void BC_Display::unlock_display()
396 pthread_mutex_lock(&BC_Display::display_lock);
397 int result = --BC_Display::display_global->window_locked;
398 // if(BC_Display::display_global->window_locked < 0)
399 // BC_Display::display_global->window_locked = 0;
400 pthread_mutex_unlock(&BC_Display::display_lock);
402 //printf("BC_Display::unlock_display %d result=%d\n", __LINE__, result);
403 /* if(result == 1) */ XUnlockDisplay(BC_Display::display_global->display);
407 int BC_Display::get_display_locked()
409 return BC_Display::display_global->window_locked;