]> Dogcows Code - chaz/openbox/blob - src/BaseDisplay.cc
acquire and provide Xinerama information for the window manager. now we just gotta...
[chaz/openbox] / src / BaseDisplay.cc
1 // -*- mode: C++; indent-tabs-mode: nil; -*-
2 // BaseDisplay.cc for Blackbox - an X11 Window manager
3 // Copyright (c) 2001 - 2002 Sean 'Shaleh' Perry <shaleh@debian.org>
4 // Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net)
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a
7 // copy of this software and associated documentation files (the "Software"),
8 // to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 // and/or sell copies of the Software, and to permit persons to whom the
11 // Software is furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 // DEALINGS IN THE SOFTWARE.
23
24 #ifdef HAVE_CONFIG_H
25 # include "../config.h"
26 #endif // HAVE_CONFIG_H
27
28 extern "C" {
29 #include <X11/Xlib.h>
30 #include <X11/Xatom.h>
31 #include <X11/Xutil.h>
32 #include <X11/keysym.h>
33
34 #ifdef SHAPE
35 # include <X11/extensions/shape.h>
36 #endif // SHAPE
37
38 #ifdef XINERAMA
39 # include <X11/extensions/Xinerama.h>
40 #endif // XINERAMA
41
42 #ifdef HAVE_FCNTL_H
43 # include <fcntl.h>
44 #endif // HAVE_FCNTL_H
45
46 #ifdef HAVE_STDIO_H
47 # include <stdio.h>
48 #endif // HAVE_STDIO_H
49
50 #ifdef HAVE_STDLIB_H
51 # include <stdlib.h>
52 #endif // HAVE_STDLIB_H
53
54 #ifdef HAVE_STRING_H
55 # include <string.h>
56 #endif // HAVE_STRING_H
57
58 #ifdef HAVE_UNISTD_H
59 # include <sys/types.h>
60 # include <unistd.h>
61 #endif // HAVE_UNISTD_H
62
63 #ifdef HAVE_SYS_SELECT_H
64 # include <sys/select.h>
65 #endif // HAVE_SYS_SELECT_H
66
67 #ifdef HAVE_SIGNAL_H
68 # include <signal.h>
69 #endif // HAVE_SIGNAL_H
70
71 #ifndef SA_NODEFER
72 # ifdef SA_INTERRUPT
73 # define SA_NODEFER SA_INTERRUPT
74 # else // !SA_INTERRUPT
75 # define SA_NODEFER (0)
76 # endif // SA_INTERRUPT
77 #endif // SA_NODEFER
78
79 #ifdef HAVE_SYS_WAIT_H
80 # include <sys/types.h>
81 # include <sys/wait.h>
82 #endif // HAVE_SYS_WAIT_H
83 }
84
85 #include <string>
86 using std::string;
87
88 #include "i18n.hh"
89 #include "BaseDisplay.hh"
90 #include "GCCache.hh"
91 #include "Timer.hh"
92 #include "Util.hh"
93
94
95 // X error handler to handle any and all X errors while the application is
96 // running
97 static bool internal_error = False;
98
99 BaseDisplay *base_display;
100
101 static int handleXErrors(Display *d, XErrorEvent *e) {
102 #ifdef DEBUG
103 char errtxt[128];
104
105 XGetErrorText(d, e->error_code, errtxt, 128);
106 fprintf(stderr,
107 i18n(BaseDisplaySet, BaseDisplayXError,
108 "%s: X error: %s(%d) opcodes %d/%d\n resource 0x%lx\n"),
109 base_display->getApplicationName(), errtxt, e->error_code,
110 e->request_code, e->minor_code, e->resourceid);
111 #else
112 // shutup gcc
113 (void) d;
114 (void) e;
115 #endif // DEBUG
116
117 if (internal_error) abort();
118
119 return(False);
120 }
121
122
123 // signal handler to allow for proper and gentle shutdown
124
125 #ifndef HAVE_SIGACTION
126 static RETSIGTYPE signalhandler(int sig) {
127 #else // HAVE_SIGACTION
128 static void signalhandler(int sig) {
129 #endif // HAVE_SIGACTION
130
131 static int re_enter = 0;
132
133 switch (sig) {
134 case SIGCHLD:
135 int status;
136 waitpid(-1, &status, WNOHANG | WUNTRACED);
137
138 #ifndef HAVE_SIGACTION
139 // assume broken, braindead sysv signal semantics
140 signal(SIGCHLD, (RETSIGTYPE (*)(int)) signalhandler);
141 #endif // HAVE_SIGACTION
142
143 break;
144
145 default:
146 if (base_display->handleSignal(sig)) {
147
148 #ifndef HAVE_SIGACTION
149 // assume broken, braindead sysv signal semantics
150 signal(sig, (RETSIGTYPE (*)(int)) signalhandler);
151 #endif // HAVE_SIGACTION
152
153 return;
154 }
155
156 fprintf(stderr, i18n(BaseDisplaySet, BaseDisplaySignalCaught,
157 "%s: signal %d caught\n"),
158 base_display->getApplicationName(), sig);
159
160 if (! base_display->isStartup() && ! re_enter) {
161 internal_error = True;
162
163 re_enter = 1;
164 fprintf(stderr, i18n(BaseDisplaySet, BaseDisplayShuttingDown,
165 "shutting down\n"));
166 base_display->shutdown();
167 }
168
169 if (sig != SIGTERM && sig != SIGINT) {
170 fprintf(stderr, i18n(BaseDisplaySet, BaseDisplayAborting,
171 "aborting... dumping core\n"));
172 abort();
173 }
174
175 exit(0);
176
177 break;
178 }
179 }
180
181
182 BaseDisplay::BaseDisplay(const char *app_name, const char *dpy_name) {
183 application_name = app_name;
184
185 run_state = STARTUP;
186
187 ::base_display = this;
188
189 #ifdef HAVE_SIGACTION
190 struct sigaction action;
191
192 action.sa_handler = signalhandler;
193 action.sa_mask = sigset_t();
194 action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
195
196 sigaction(SIGPIPE, &action, NULL);
197 sigaction(SIGSEGV, &action, NULL);
198 sigaction(SIGFPE, &action, NULL);
199 sigaction(SIGTERM, &action, NULL);
200 sigaction(SIGINT, &action, NULL);
201 sigaction(SIGCHLD, &action, NULL);
202 sigaction(SIGHUP, &action, NULL);
203 sigaction(SIGUSR1, &action, NULL);
204 sigaction(SIGUSR2, &action, NULL);
205 #else // !HAVE_SIGACTION
206 signal(SIGPIPE, (RETSIGTYPE (*)(int)) signalhandler);
207 signal(SIGSEGV, (RETSIGTYPE (*)(int)) signalhandler);
208 signal(SIGFPE, (RETSIGTYPE (*)(int)) signalhandler);
209 signal(SIGTERM, (RETSIGTYPE (*)(int)) signalhandler);
210 signal(SIGINT, (RETSIGTYPE (*)(int)) signalhandler);
211 signal(SIGUSR1, (RETSIGTYPE (*)(int)) signalhandler);
212 signal(SIGUSR2, (RETSIGTYPE (*)(int)) signalhandler);
213 signal(SIGHUP, (RETSIGTYPE (*)(int)) signalhandler);
214 signal(SIGCHLD, (RETSIGTYPE (*)(int)) signalhandler);
215 #endif // HAVE_SIGACTION
216
217 if (! (display = XOpenDisplay(dpy_name))) {
218 fprintf(stderr,
219 i18n(BaseDisplaySet, BaseDisplayXConnectFail,
220 "BaseDisplay::BaseDisplay: connection to X server failed.\n"));
221 ::exit(2);
222 } else if (fcntl(ConnectionNumber(display), F_SETFD, 1) == -1) {
223 fprintf(stderr,
224 i18n(BaseDisplaySet, BaseDisplayCloseOnExecFail,
225 "BaseDisplay::BaseDisplay: couldn't mark display connection "
226 "as close-on-exec\n"));
227 ::exit(2);
228 }
229
230 display_name = XDisplayName(dpy_name);
231
232 #ifdef SHAPE
233 shape.extensions = XShapeQueryExtension(display, &shape.event_basep,
234 &shape.error_basep);
235 #else // !SHAPE
236 shape.extensions = False;
237 #endif // SHAPE
238
239 xinerama.extensions = False;
240 #ifdef XINERAMA
241 if (XineramaQueryExtension(display, &xinerama.event_basep,
242 &xinerama.error_basep)) {
243 if (XineramaQueryVersion(display, &xinerama.major,
244 &xinerama.minor)) {
245 #ifdef DEBUG
246 fprintf(stderr,
247 "BaseDisplay::BaseDisplay: Found Xinerama version %d.%d\n",
248 xinerama.major, xinerama.minor);
249 #endif // DEBUG
250 xinerama.extensions = True;
251 }
252 }
253 #endif // XINERAMA
254
255 XSetErrorHandler((XErrorHandler) handleXErrors);
256
257 screenInfoList.reserve(ScreenCount(display));
258 for (int i = 0; i < ScreenCount(display); ++i)
259 screenInfoList.push_back(ScreenInfo(this, i));
260
261 NumLockMask = ScrollLockMask = 0;
262
263 const XModifierKeymap* const modmap = XGetModifierMapping(display);
264 if (modmap && modmap->max_keypermod > 0) {
265 const int mask_table[] = {
266 ShiftMask, LockMask, ControlMask, Mod1Mask,
267 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
268 };
269 const size_t size = (sizeof(mask_table) / sizeof(mask_table[0])) *
270 modmap->max_keypermod;
271 // get the values of the keyboard lock modifiers
272 // Note: Caps lock is not retrieved the same way as Scroll and Num lock
273 // since it doesn't need to be.
274 const KeyCode num_lock = XKeysymToKeycode(display, XK_Num_Lock);
275 const KeyCode scroll_lock = XKeysymToKeycode(display, XK_Scroll_Lock);
276
277 for (size_t cnt = 0; cnt < size; ++cnt) {
278 if (! modmap->modifiermap[cnt]) continue;
279
280 if (num_lock == modmap->modifiermap[cnt])
281 NumLockMask = mask_table[cnt / modmap->max_keypermod];
282 if (scroll_lock == modmap->modifiermap[cnt])
283 ScrollLockMask = mask_table[cnt / modmap->max_keypermod];
284 }
285 }
286
287 MaskList[0] = 0;
288 MaskList[1] = LockMask;
289 MaskList[2] = NumLockMask;
290 MaskList[3] = LockMask | NumLockMask;
291 MaskList[4] = ScrollLockMask;
292 MaskList[5] = ScrollLockMask | LockMask;
293 MaskList[6] = ScrollLockMask | NumLockMask;
294 MaskList[7] = ScrollLockMask | LockMask | NumLockMask;
295 MaskListLength = sizeof(MaskList) / sizeof(MaskList[0]);
296
297 if (modmap) XFreeModifiermap(const_cast<XModifierKeymap*>(modmap));
298
299 gccache = (BGCCache *) 0;
300 }
301
302
303 BaseDisplay::~BaseDisplay(void) {
304 delete gccache;
305
306 XCloseDisplay(display);
307 }
308
309
310 void BaseDisplay::eventLoop(void) {
311 run();
312
313 const int xfd = ConnectionNumber(display);
314
315 while (run_state == RUNNING && ! internal_error) {
316 if (XPending(display)) {
317 XEvent e;
318 XNextEvent(display, &e);
319 process_event(&e);
320 } else {
321 fd_set rfds;
322 timeval now, tm, *timeout = (timeval *) 0;
323
324 FD_ZERO(&rfds);
325 FD_SET(xfd, &rfds);
326
327 if (! timerList.empty()) {
328 const BTimer* const timer = timerList.top();
329
330 gettimeofday(&now, 0);
331 tm = timer->timeRemaining(now);
332
333 timeout = &tm;
334 }
335
336 select(xfd + 1, &rfds, 0, 0, timeout);
337
338 // check for timer timeout
339 gettimeofday(&now, 0);
340
341 // there is a small chance for deadlock here:
342 // *IF* the timer list keeps getting refreshed *AND* the time between
343 // timer->start() and timer->shouldFire() is within the timer's period
344 // then the timer will keep firing. This should be VERY near impossible.
345 while (! timerList.empty()) {
346 BTimer *timer = timerList.top();
347 if (! timer->shouldFire(now))
348 break;
349
350 timerList.pop();
351
352 timer->fireTimeout();
353 timer->halt();
354 if (timer->isRecurring())
355 timer->start();
356 }
357 }
358 }
359 }
360
361
362 void BaseDisplay::addTimer(BTimer *timer) {
363 if (! timer) return;
364
365 timerList.push(timer);
366 }
367
368
369 void BaseDisplay::removeTimer(BTimer *timer) {
370 timerList.release(timer);
371 }
372
373
374 /*
375 * Grabs a button, but also grabs the button in every possible combination
376 * with the keyboard lock keys, so that they do not cancel out the event.
377
378 * if allow_scroll_lock is true then only the top half of the lock mask
379 * table is used and scroll lock is ignored. This value defaults to false.
380 */
381 void BaseDisplay::grabButton(unsigned int button, unsigned int modifiers,
382 Window grab_window, bool owner_events,
383 unsigned int event_mask, int pointer_mode,
384 int keyboard_mode, Window confine_to,
385 Cursor cursor, bool allow_scroll_lock) const {
386 unsigned int length = (allow_scroll_lock) ? MaskListLength / 2:
387 MaskListLength;
388 for (size_t cnt = 0; cnt < length; ++cnt)
389 XGrabButton(display, button, modifiers | MaskList[cnt], grab_window,
390 owner_events, event_mask, pointer_mode, keyboard_mode,
391 confine_to, cursor);
392 }
393
394
395 /*
396 * Releases the grab on a button, and ungrabs all possible combinations of the
397 * keyboard lock keys.
398 */
399 void BaseDisplay::ungrabButton(unsigned int button, unsigned int modifiers,
400 Window grab_window) const {
401 for (size_t cnt = 0; cnt < MaskListLength; ++cnt)
402 XUngrabButton(display, button, modifiers | MaskList[cnt], grab_window);
403 }
404
405
406 const ScreenInfo* BaseDisplay::getScreenInfo(unsigned int s) const {
407 if (s < screenInfoList.size())
408 return &screenInfoList[s];
409 return (const ScreenInfo*) 0;
410 }
411
412
413 BGCCache* BaseDisplay::gcCache(void) const {
414 if (! gccache)
415 gccache = new BGCCache(this);
416
417 return gccache;
418 }
419
420
421 ScreenInfo::ScreenInfo(BaseDisplay *d, unsigned int num) {
422 basedisplay = d;
423 screen_number = num;
424
425 root_window = RootWindow(basedisplay->getXDisplay(), screen_number);
426
427 rect.setSize(WidthOfScreen(ScreenOfDisplay(basedisplay->getXDisplay(),
428 screen_number)),
429 HeightOfScreen(ScreenOfDisplay(basedisplay->getXDisplay(),
430 screen_number)));
431 /*
432 If the default depth is at least 8 we will use that,
433 otherwise we try to find the largest TrueColor visual.
434 Preference is given to 24 bit over larger depths if 24 bit is an option.
435 */
436
437 depth = DefaultDepth(basedisplay->getXDisplay(), screen_number);
438 visual = DefaultVisual(basedisplay->getXDisplay(), screen_number);
439 colormap = DefaultColormap(basedisplay->getXDisplay(), screen_number);
440
441 if (depth < 8) {
442 // search for a TrueColor Visual... if we can't find one...
443 // we will use the default visual for the screen
444 XVisualInfo vinfo_template, *vinfo_return;
445 int vinfo_nitems;
446 int best = -1;
447
448 vinfo_template.screen = screen_number;
449 vinfo_template.c_class = TrueColor;
450
451 vinfo_return = XGetVisualInfo(basedisplay->getXDisplay(),
452 VisualScreenMask | VisualClassMask,
453 &vinfo_template, &vinfo_nitems);
454 if (vinfo_return) {
455 int max_depth = 1;
456 for (int i = 0; i < vinfo_nitems; ++i) {
457 if (vinfo_return[i].depth > max_depth) {
458 if (max_depth == 24 && vinfo_return[i].depth > 24)
459 break; // prefer 24 bit over 32
460 max_depth = vinfo_return[i].depth;
461 best = i;
462 }
463 }
464 if (max_depth < depth) best = -1;
465 }
466
467 if (best != -1) {
468 depth = vinfo_return[best].depth;
469 visual = vinfo_return[best].visual;
470 colormap = XCreateColormap(basedisplay->getXDisplay(), root_window,
471 visual, AllocNone);
472 }
473
474 XFree(vinfo_return);
475 }
476
477 // get the default display string and strip the screen number
478 string default_string = DisplayString(basedisplay->getXDisplay());
479 const string::size_type pos = default_string.rfind(".");
480 if (pos != string::npos)
481 default_string.resize(pos);
482
483 display_string = string("DISPLAY=") + default_string + '.' +
484 itostring(static_cast<unsigned long>(screen_number));
485
486 #ifdef XINERAMA
487 if (d->hasXineramaExtensions()) {
488 if (d->getXineramaMajorVersion() == 1) {
489 // we know the version 1(.1?) protocol
490
491 /*
492 in this version of Xinerama, we can't query on a per-screen basis, but
493 in future versions we should be able, so the 'activeness' is checked
494 on a pre-screen basis anyways.
495 */
496 xinerama_active = XineramaIsActive(d->getXDisplay());
497 /*
498 If Xinerama is being used, there there is only going to be one screen
499 present. We still, of course, want to use the screen class, but that is
500 why no screen number is used in this function call. There should never
501 be more than one screen present with Xinerama active.
502 */
503 int num;
504 XineramaScreenInfo *info = XineramaQueryScreens(d->getXDisplay(), &num);
505 if (num > 0 && info) {
506 for (int i = 0; i < num; ++i) {
507 xinerama_areas.push_back(Rect(info[i].x_org, info[i].y_org,
508 info[i].width, info[i].height));
509 }
510 XFree(info);
511 }
512 }
513 }
514 #endif // XINERAMA
515 }
This page took 0.061874 seconds and 5 git commands to generate.