1 // BaseDisplay.cc for Openbox
2 // Copyright (c) 2001 Sean 'Shaleh' Perry <shaleh@debian.org>
3 // Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net)
5 // Permission is hereby granted, free of charge, to any person obtaining a
6 // copy of this software and associated documentation files (the "Software"),
7 // to deal in the Software without restriction, including without limitation
8 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 // and/or sell copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following conditions:
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 // DEALINGS IN THE SOFTWARE.
23 // stupid macros needed to access some functions in version 2 of the GNU C
30 # include "../config.h"
31 #endif // HAVE_CONFIG_H
34 #include <X11/Xatom.h>
35 #include <X11/Xutil.h>
36 #include <X11/cursorfont.h>
37 #include <X11/keysym.h>
40 # include <X11/extensions/shape.h>
45 #endif // HAVE_FCNTL_H
49 #endif // HAVE_STDIO_H
53 #endif // HAVE_STDLIB_H
57 #endif // HAVE_STRING_H
60 # include <sys/types.h>
62 #endif // HAVE_UNISTD_H
64 #ifdef HAVE_SYS_SELECT_H
65 # include <sys/select.h>
66 #endif // HAVE_SYS_SELECT_H
70 #endif // HAVE_SIGNAL_H
74 # define SA_NODEFER SA_INTERRUPT
75 # else // !SA_INTERRUPT
76 # define SA_NODEFER (0)
77 # endif // SA_INTERRUPT
80 #ifdef HAVE_SYS_WAIT_H
81 # include <sys/types.h>
82 # include <sys/wait.h>
83 #endif // HAVE_SYS_WAIT_H
85 #if defined(HAVE_PROCESS_H) && defined(__EMX__)
87 #endif // HAVE_PROCESS_H __EMX__
90 #include "BaseDisplay.h"
95 // X error handler to handle any and all X errors while the application is
97 static Bool internal_error
= False
;
98 static Window last_bad_window
= None
;
100 BaseDisplay
*base_display
;
102 static int handleXErrors(Display
*d
, XErrorEvent
*e
) {
106 XGetErrorText(d
, e
->error_code
, errtxt
, 128);
107 fprintf(stderr
, i18n
->getMessage(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
);
113 if (e
->error_code
== BadWindow
) last_bad_window
= e
->resourceid
;
114 if (internal_error
) abort();
120 // signal handler to allow for proper and gentle shutdown
122 #ifndef HAVE_SIGACTION
123 static RETSIGTYPE
signalhandler(int sig
) {
124 #else // HAVE_SIGACTION
125 static void signalhandler(int sig
) {
126 #endif // HAVE_SIGACTION
128 static int re_enter
= 0;
133 waitpid(-1, &status
, WNOHANG
| WUNTRACED
);
135 #ifndef HAVE_SIGACTION
136 // assume broken, braindead sysv signal semantics
137 signal(SIGCHLD
, (RETSIGTYPE (*)(int)) signalhandler
);
138 #endif // HAVE_SIGACTION
143 if (base_display
->handleSignal(sig
)) {
145 #ifndef HAVE_SIGACTION
146 // assume broken, braindead sysv signal semantics
147 signal(sig
, (RETSIGTYPE (*)(int)) signalhandler
);
148 #endif // HAVE_SIGACTION
153 fprintf(stderr
, i18n
->getMessage(BaseDisplaySet
, BaseDisplaySignalCaught
,
154 "%s: signal %d caught\n"),
155 base_display
->getApplicationName(), sig
);
157 if (! base_display
->isStartup() && ! re_enter
) {
158 internal_error
= True
;
161 fprintf(stderr
, i18n
->getMessage(BaseDisplaySet
, BaseDisplayShuttingDown
,
163 base_display
->shutdown();
166 if (sig
!= SIGTERM
&& sig
!= SIGINT
) {
167 fprintf(stderr
, i18n
->getMessage(BaseDisplaySet
, BaseDisplayAborting
,
168 "aborting... dumping core\n"));
179 // convenience functions
181 void bexec(const char *command
, char* displaystring
) {
184 putenv(displaystring
);
185 execl("/bin/sh", "/bin/sh", "-c", command
, NULL
);
191 char *bstrdup(const char *s
) {
192 const int l
= strlen(s
) + 1;
193 char *n
= new char[l
];
198 BaseDisplay::BaseDisplay(const char *app_name
, char *dpy_name
) {
199 application_name
= bstrdup(app_name
);
204 last_bad_window
= None
;
206 ::base_display
= this;
208 #ifdef HAVE_SIGACTION
209 struct sigaction action
;
211 action
.sa_handler
= signalhandler
;
212 action
.sa_mask
= sigset_t();
213 action
.sa_flags
= SA_NOCLDSTOP
| SA_NODEFER
;
215 sigaction(SIGPIPE
, &action
, NULL
);
216 sigaction(SIGSEGV
, &action
, NULL
);
217 sigaction(SIGFPE
, &action
, NULL
);
218 sigaction(SIGTERM
, &action
, NULL
);
219 sigaction(SIGINT
, &action
, NULL
);
220 sigaction(SIGCHLD
, &action
, NULL
);
221 sigaction(SIGHUP
, &action
, NULL
);
222 sigaction(SIGUSR1
, &action
, NULL
);
223 sigaction(SIGUSR2
, &action
, NULL
);
224 #else // !HAVE_SIGACTION
225 signal(SIGPIPE
, (RETSIGTYPE (*)(int)) signalhandler
);
226 signal(SIGSEGV
, (RETSIGTYPE (*)(int)) signalhandler
);
227 signal(SIGFPE
, (RETSIGTYPE (*)(int)) signalhandler
);
228 signal(SIGTERM
, (RETSIGTYPE (*)(int)) signalhandler
);
229 signal(SIGINT
, (RETSIGTYPE (*)(int)) signalhandler
);
230 signal(SIGUSR1
, (RETSIGTYPE (*)(int)) signalhandler
);
231 signal(SIGUSR2
, (RETSIGTYPE (*)(int)) signalhandler
);
232 signal(SIGHUP
, (RETSIGTYPE (*)(int)) signalhandler
);
233 signal(SIGCHLD
, (RETSIGTYPE (*)(int)) signalhandler
);
234 #endif // HAVE_SIGACTION
236 if (! (display
= XOpenDisplay(dpy_name
))) {
237 fprintf(stderr
, i18n
->getMessage(BaseDisplaySet
, BaseDisplayXConnectFail
,
238 "BaseDisplay::BaseDisplay: connection to X server failed.\n"));
240 } else if (fcntl(ConnectionNumber(display
), F_SETFD
, 1) == -1) {
242 i18n
->getMessage(BaseDisplaySet
, BaseDisplayCloseOnExecFail
,
243 "BaseDisplay::BaseDisplay: couldn't mark display connection "
244 "as close-on-exec\n"));
248 display_name
= XDisplayName(dpy_name
);
251 shape
.extensions
= XShapeQueryExtension(display
, &shape
.event_basep
,
254 shape
.extensions
= False
;
257 xa_wm_colormap_windows
=
258 XInternAtom(display
, "WM_COLORMAP_WINDOWS", False
);
259 xa_wm_protocols
= XInternAtom(display
, "WM_PROTOCOLS", False
);
260 xa_wm_state
= XInternAtom(display
, "WM_STATE", False
);
261 xa_wm_change_state
= XInternAtom(display
, "WM_CHANGE_STATE", False
);
262 xa_wm_delete_window
= XInternAtom(display
, "WM_DELETE_WINDOW", False
);
263 xa_wm_take_focus
= XInternAtom(display
, "WM_TAKE_FOCUS", False
);
264 motif_wm_hints
= XInternAtom(display
, "_MOTIF_WM_HINTS", False
);
266 openbox_hints
= XInternAtom(display
, "_BLACKBOX_HINTS", False
);
267 openbox_attributes
= XInternAtom(display
, "_BLACKBOX_ATTRIBUTES", False
);
268 openbox_change_attributes
=
269 XInternAtom(display
, "_BLACKBOX_CHANGE_ATTRIBUTES", False
);
271 openbox_structure_messages
=
272 XInternAtom(display
, "_BLACKBOX_STRUCTURE_MESSAGES", False
);
273 openbox_notify_startup
=
274 XInternAtom(display
, "_BLACKBOX_NOTIFY_STARTUP", False
);
275 openbox_notify_window_add
=
276 XInternAtom(display
, "_BLACKBOX_NOTIFY_WINDOW_ADD", False
);
277 openbox_notify_window_del
=
278 XInternAtom(display
, "_BLACKBOX_NOTIFY_WINDOW_DEL", False
);
279 openbox_notify_current_workspace
=
280 XInternAtom(display
, "_BLACKBOX_NOTIFY_CURRENT_WORKSPACE", False
);
281 openbox_notify_workspace_count
=
282 XInternAtom(display
, "_BLACKBOX_NOTIFY_WORKSPACE_COUNT", False
);
283 openbox_notify_window_focus
=
284 XInternAtom(display
, "_BLACKBOX_NOTIFY_WINDOW_FOCUS", False
);
285 openbox_notify_window_raise
=
286 XInternAtom(display
, "_BLACKBOX_NOTIFY_WINDOW_RAISE", False
);
287 openbox_notify_window_lower
=
288 XInternAtom(display
, "_BLACKBOX_NOTIFY_WINDOW_LOWER", False
);
290 openbox_change_workspace
=
291 XInternAtom(display
, "_BLACKBOX_CHANGE_WORKSPACE", False
);
292 openbox_change_window_focus
=
293 XInternAtom(display
, "_BLACKBOX_CHANGE_WINDOW_FOCUS", False
);
294 openbox_cycle_window_focus
=
295 XInternAtom(display
, "_BLACKBOX_CYCLE_WINDOW_FOCUS", False
);
299 net_supported
= XInternAtom(display
, "_NET_SUPPORTED", False
);
300 net_client_list
= XInternAtom(display
, "_NET_CLIENT_LIST", False
);
301 net_client_list_stacking
= XInternAtom(display
, "_NET_CLIENT_LIST_STACKING", False
);
302 net_number_of_desktops
= XInternAtom(display
, "_NET_NUMBER_OF_DESKTOPS", False
);
303 net_desktop_geometry
= XInternAtom(display
, "_NET_DESKTOP_GEOMETRY", False
);
304 net_desktop_viewport
= XInternAtom(display
, "_NET_DESKTOP_VIEWPORT", False
);
305 net_current_desktop
= XInternAtom(display
, "_NET_CURRENT_DESKTOP", False
);
306 net_desktop_names
= XInternAtom(display
, "_NET_DESKTOP_NAMES", False
);
307 net_active_window
= XInternAtom(display
, "_NET_ACTIVE_WINDOW", False
);
308 net_workarea
= XInternAtom(display
, "_NET_WORKAREA", False
);
309 net_supporting_wm_check
= XInternAtom(display
, "_NET_SUPPORTING_WM_CHECK", False
);
310 net_virtual_roots
= XInternAtom(display
, "_NET_VIRTUAL_ROOTS", False
);
312 net_close_window
= XInternAtom(display
, "_NET_CLOSE_WINDOW", False
);
313 net_wm_moveresize
= XInternAtom(display
, "_NET_WM_MOVERESIZE", False
);
315 net_properties
= XInternAtom(display
, "_NET_PROPERTIES", False
);
316 net_wm_name
= XInternAtom(display
, "_NET_WM_NAME", False
);
317 net_wm_desktop
= XInternAtom(display
, "_NET_WM_DESKTOP", False
);
318 net_wm_window_type
= XInternAtom(display
, "_NET_WM_WINDOW_TYPE", False
);
319 net_wm_state
= XInternAtom(display
, "_NET_WM_STATE", False
);
320 net_wm_strut
= XInternAtom(display
, "_NET_WM_STRUT", False
);
321 net_wm_icon_geometry
= XInternAtom(display
, "_NET_WM_ICON_GEOMETRY", False
);
322 net_wm_icon
= XInternAtom(display
, "_NET_WM_ICON", False
);
323 net_wm_pid
= XInternAtom(display
, "_NET_WM_PID", False
);
324 net_wm_handled_icons
= XInternAtom(display
, "_NET_WM_HANDLED_ICONS", False
);
326 net_wm_ping
= XInternAtom(display
, "_NET_WM_PING", False
);
330 cursor
.session
= XCreateFontCursor(display
, XC_left_ptr
);
331 cursor
.move
= XCreateFontCursor(display
, XC_fleur
);
332 cursor
.ll_angle
= XCreateFontCursor(display
, XC_ll_angle
);
333 cursor
.lr_angle
= XCreateFontCursor(display
, XC_lr_angle
);
334 cursor
.ul_angle
= XCreateFontCursor(display
, XC_ul_angle
);
335 cursor
.ur_angle
= XCreateFontCursor(display
, XC_ur_angle
);
337 XSetErrorHandler((XErrorHandler
) handleXErrors
);
339 screenInfoList
.reserve(numberOfScreens());
340 for (unsigned int s
= 0; s
< numberOfScreens(); s
++)
341 screenInfoList
.push_back(new ScreenInfo(*this, s
));
344 NumLockMask
= ScrollLockMask
= 0;
346 const XModifierKeymap
* const modmap
= XGetModifierMapping(display
);
347 if (modmap
&& modmap
->max_keypermod
> 0) {
348 const int mask_table
[] = {
349 ShiftMask
, LockMask
, ControlMask
, Mod1Mask
,
350 Mod2Mask
, Mod3Mask
, Mod4Mask
, Mod5Mask
352 const size_t size
= (sizeof(mask_table
) / sizeof(mask_table
[0])) *
353 modmap
->max_keypermod
;
354 // get the values of the keyboard lock modifiers
355 // Note: Caps lock is not retrieved the same way as Scroll and Num lock
356 // since it doesn't need to be.
357 const KeyCode num_lock_code
= XKeysymToKeycode(display
, XK_Num_Lock
);
358 const KeyCode scroll_lock_code
= XKeysymToKeycode(display
, XK_Scroll_Lock
);
360 for (size_t cnt
= 0; cnt
< size
; ++cnt
) {
361 if (! modmap
->modifiermap
[cnt
]) continue;
363 if (num_lock_code
== modmap
->modifiermap
[cnt
])
364 NumLockMask
= mask_table
[cnt
/ modmap
->max_keypermod
];
365 if (scroll_lock_code
== modmap
->modifiermap
[cnt
])
366 ScrollLockMask
= mask_table
[cnt
/ modmap
->max_keypermod
];
371 MaskList
[1] = LockMask
;
372 MaskList
[2] = NumLockMask
;
373 MaskList
[3] = ScrollLockMask
;
374 MaskList
[4] = LockMask
| NumLockMask
;
375 MaskList
[5] = NumLockMask
| ScrollLockMask
;
376 MaskList
[6] = LockMask
| ScrollLockMask
;
377 MaskList
[7] = LockMask
| NumLockMask
| ScrollLockMask
;
378 MaskListLength
= sizeof(MaskList
) / sizeof(MaskList
[0]);
380 if (modmap
) XFreeModifiermap(const_cast<XModifierKeymap
*>(modmap
));
382 NumLockMask
= Mod2Mask
;
383 ScrollLockMask
= Mod5Mask
;
388 BaseDisplay::~BaseDisplay(void) {
389 std::for_each(screenInfoList
.begin(), screenInfoList
.end(),
391 // we don't create the BTimers, we don't delete them
393 if (application_name
!= NULL
)
394 delete [] application_name
;
396 XCloseDisplay(display
);
400 void BaseDisplay::eventLoop(void) {
403 int xfd
= ConnectionNumber(display
);
405 while ((! _shutdown
) && (! internal_error
)) {
406 if (XPending(display
)) {
408 XNextEvent(display
, &e
);
410 if (last_bad_window
!= None
&& e
.xany
.window
== last_bad_window
) {
412 fprintf(stderr
, i18n
->getMessage(BaseDisplaySet
,
413 BaseDisplayBadWindowRemove
,
414 "BaseDisplay::eventLoop(): removing bad window "
415 "from event queue\n"));
418 last_bad_window
= None
;
423 timeval now
, tm
, *timeout
= (timeval
*) 0;
428 if (!timerList
.empty()) {
429 gettimeofday(&now
, 0);
431 tm
.tv_sec
= tm
.tv_usec
= 0l;
433 BTimer
*timer
= timerList
.front();
434 ASSERT(timer
!= NULL
);
436 tm
.tv_sec
= timer
->getStartTime().tv_sec
+
437 timer
->getTimeout().tv_sec
- now
.tv_sec
;
438 tm
.tv_usec
= timer
->getStartTime().tv_usec
+
439 timer
->getTimeout().tv_usec
- now
.tv_usec
;
441 while (tm
.tv_usec
>= 1000000) {
443 tm
.tv_usec
-= 1000000;
446 while (tm
.tv_usec
< 0) {
449 tm
.tv_usec
+= 1000000;
459 select(xfd
+ 1, &rfds
, 0, 0, timeout
);
461 // check for timer timeout
462 gettimeofday(&now
, 0);
464 TimerList::iterator it
;
465 for (it
= timerList
.begin(); it
!= timerList
.end(); ) {
467 ++it
; // the timer may be removed from the list, so move ahead now
468 ASSERT(timer
!= NULL
);
469 tm
.tv_sec
= timer
->getStartTime().tv_sec
+
470 timer
->getTimeout().tv_sec
;
471 tm
.tv_usec
= timer
->getStartTime().tv_usec
+
472 timer
->getTimeout().tv_usec
;
474 if ((now
.tv_sec
< tm
.tv_sec
) ||
475 (now
.tv_sec
== tm
.tv_sec
&& now
.tv_usec
< tm
.tv_usec
))
478 timer
->fireTimeout();
480 // restart the current timer so that the start time is updated
481 if (! timer
->doOnce())
485 // delete timer; // USE THIS?
493 const Bool
BaseDisplay::validateWindow(Window window
) {
495 if (XCheckTypedWindowEvent(display
, window
, DestroyNotify
, &event
)) {
496 XPutBackEvent(display
, &event
);
505 void BaseDisplay::grab(void) {
506 if (! server_grabs
++)
507 XGrabServer(display
);
511 void BaseDisplay::ungrab(void) {
512 if (! --server_grabs
)
513 XUngrabServer(display
);
517 void BaseDisplay::addTimer(BTimer
*timer
) {
518 ASSERT(timer
!= (BTimer
*) 0);
520 TimerList::iterator it
;
521 for (it
= timerList
.begin(); it
!= timerList
.end(); ++it
) {
523 if ((tmp
->getTimeout().tv_sec
> timer
->getTimeout().tv_sec
) ||
524 ((tmp
->getTimeout().tv_sec
== timer
->getTimeout().tv_sec
) &&
525 (tmp
->getTimeout().tv_usec
>= timer
->getTimeout().tv_usec
)))
529 timerList
.insert(it
, timer
);
533 void BaseDisplay::removeTimer(BTimer
*timer
) {
534 timerList
.remove(timer
);
539 * Grabs a button, but also grabs the button in every possible combination with
540 * the keyboard lock keys, so that they do not cancel out the event.
542 void BaseDisplay::grabButton(unsigned int button
, unsigned int modifiers
,
543 Window grab_window
, Bool owner_events
,
544 unsigned int event_mask
, int pointer_mode
,
545 int keybaord_mode
, Window confine_to
,
549 for (size_t cnt
= 0; cnt
< MaskListLength
; ++cnt
)
550 XGrabButton(display
, button
, modifiers
| MaskList
[cnt
], grab_window
,
551 owner_events
, event_mask
, pointer_mode
, keybaord_mode
,
554 XGrabButton(display
, button
, modifiers
, grab_window
,
555 owner_events
, event_mask
, pointer_mode
, keybaord_mode
,
561 * Releases the grab on a button, and ungrabs all possible combinations of the
562 * keyboard lock keys.
564 void BaseDisplay::ungrabButton(unsigned int button
, unsigned int modifiers
,
565 Window grab_window
) const {
567 for (size_t cnt
= 0; cnt
< MaskListLength
; ++cnt
)
568 XUngrabButton(display
, button
, modifiers
| MaskList
[cnt
], grab_window
);
570 XUngrabButton(display
, button
, modifiers
, grab_window
);
575 ScreenInfo::ScreenInfo(BaseDisplay
&d
, int num
) : basedisplay(d
),
579 root_window
= RootWindow(basedisplay
.getXDisplay(), screen_number
);
580 depth
= DefaultDepth(basedisplay
.getXDisplay(), screen_number
);
582 m_size
= Size(WidthOfScreen(ScreenOfDisplay(basedisplay
.getXDisplay(),
584 HeightOfScreen(ScreenOfDisplay(basedisplay
.getXDisplay(),
587 // search for a TrueColor Visual... if we can't find one... we will use the
588 // default visual for the screen
589 XVisualInfo vinfo_template
, *vinfo_return
;
592 vinfo_template
.screen
= screen_number
;
593 vinfo_template
.c_class
= TrueColor
;
595 visual
= (Visual
*) 0;
597 if ((vinfo_return
= XGetVisualInfo(basedisplay
.getXDisplay(),
598 VisualScreenMask
| VisualClassMask
,
599 &vinfo_template
, &vinfo_nitems
)) &&
601 for (int i
= 0; i
< vinfo_nitems
; i
++) {
602 if (depth
< (vinfo_return
+ i
)->depth
) {
603 depth
= (vinfo_return
+ i
)->depth
;
604 visual
= (vinfo_return
+ i
)->visual
;
612 colormap
= XCreateColormap(basedisplay
.getXDisplay(), root_window
,
615 visual
= DefaultVisual(basedisplay
.getXDisplay(), screen_number
);
616 colormap
= DefaultColormap(basedisplay
.getXDisplay(), screen_number
);