1 // -*- mode: C++; indent-tabs-mode: nil; -*-
4 # include "../config.h"
8 #include "screeninfo.hh"
12 #include <X11/keysym.h>
16 #endif // HAVE_STDIO_H
20 #endif // HAVE_STDLIB_H
24 #endif // HAVE_SIGNAL_H
28 #endif // HAVE_FCNTL_H
31 # include <sys/types.h>
33 #endif // HAVE_UNISTD_H
36 #define _(str) gettext(str)
42 Display *OBDisplay::display = (Display*) 0;
43 bool OBDisplay::_shape = false;
44 int OBDisplay::_shape_event_basep = 0;
45 bool OBDisplay::_xinerama = false;
46 int OBDisplay::_xinerama_event_basep = 0;
47 unsigned int OBDisplay::_mask_list[8];
48 OBDisplay::ScreenInfoList OBDisplay::_screenInfoList;
49 BGCCache *OBDisplay::_gccache = (BGCCache*) 0;
52 int OBDisplay::xerrorHandler(Display *d, XErrorEvent *e)
57 //if (e->error_code != BadWindow)
59 XGetErrorText(d, e->error_code, errtxt, 128);
60 printf("X Error: %s\n", errtxt);
71 void OBDisplay::initialize(char *name)
77 if (!(display = XOpenDisplay(name))) {
78 printf(_("Unable to open connection to the X server. Please set the \n\
79 DISPLAY environment variable approriately, or use the '-display' command \n\
80 line argument.\n\n"));
83 if (fcntl(ConnectionNumber(display), F_SETFD, 1) == -1) {
84 printf(_("Couldn't mark display connection as close-on-exec.\n\n"));
88 // set our error handler for X errors
89 XSetErrorHandler(xerrorHandler);
91 // set the DISPLAY environment variable for any lauched children, to the
92 // display we're using, so they open in the right place.
93 // XXX rm -> std::string dtmp = "DISPLAY=" + DisplayString(display);
94 if (putenv(const_cast<char*>((std::string("DISPLAY=") +
95 DisplayString(display)).c_str()))) {
96 printf(_("warning: couldn't set environment variable 'DISPLAY'\n"));
100 // find the availability of X extensions we like to use
102 _shape = XShapeQueryExtension(display, &_shape_event_basep, &junk);
106 _xinerama = XineramaQueryExtension(display, &_xinerama_event_basep, &junk);
109 // get lock masks that are defined by the display (not constant)
110 XModifierKeymap *modmap;
111 unsigned int NumLockMask = 0, ScrollLockMask = 0;
113 modmap = XGetModifierMapping(display);
114 if (modmap && modmap->max_keypermod > 0) {
115 const int mask_table[] = {
116 ShiftMask, LockMask, ControlMask, Mod1Mask,
117 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
119 const size_t size = (sizeof(mask_table) / sizeof(mask_table[0])) *
120 modmap->max_keypermod;
121 // get the values of the keyboard lock modifiers
122 // Note: Caps lock is not retrieved the same way as Scroll and Num lock
123 // since it doesn't need to be.
124 const KeyCode num_lock = XKeysymToKeycode(display, XK_Num_Lock);
125 const KeyCode scroll_lock = XKeysymToKeycode(display, XK_Scroll_Lock);
127 for (size_t cnt = 0; cnt < size; ++cnt) {
128 if (! modmap->modifiermap[cnt]) continue;
130 if (num_lock == modmap->modifiermap[cnt])
131 NumLockMask = mask_table[cnt / modmap->max_keypermod];
132 if (scroll_lock == modmap->modifiermap[cnt])
133 ScrollLockMask = mask_table[cnt / modmap->max_keypermod];
137 if (modmap) XFreeModifiermap(modmap);
140 _mask_list[1] = LockMask;
141 _mask_list[2] = NumLockMask;
142 _mask_list[3] = LockMask | NumLockMask;
143 _mask_list[4] = ScrollLockMask;
144 _mask_list[5] = ScrollLockMask | LockMask;
145 _mask_list[6] = ScrollLockMask | NumLockMask;
146 _mask_list[7] = ScrollLockMask | LockMask | NumLockMask;
148 // Get information on all the screens which are available.
149 _screenInfoList.reserve(ScreenCount(display));
150 for (int i = 0; i < ScreenCount(display); ++i)
151 _screenInfoList.push_back(ScreenInfo(i));
153 _gccache = new BGCCache(_screenInfoList.size());
157 void OBDisplay::destroy()
160 XCloseDisplay(display);
164 const ScreenInfo* OBDisplay::screenInfo(int snum) {
166 assert(snum < static_cast<int>(_screenInfoList.size()));
167 return &_screenInfoList[snum];
179 * Grabs a button, but also grabs the button in every possible combination
180 * with the keyboard lock keys, so that they do not cancel out the event.
182 * if allow_scroll_lock is true then only the top half of the lock mask
183 * table is used and scroll lock is ignored. This value defaults to false.
185 void OBDisplay::grabButton(unsigned int button, unsigned int modifiers,
186 Window grab_window, bool owner_events,
187 unsigned int event_mask, int pointer_mode,
188 int keyboard_mode, Window confine_to,
189 Cursor cursor, bool allow_scroll_lock) {
190 unsigned int length = (allow_scroll_lock) ? 8 / 2:
192 for (size_t cnt = 0; cnt < length; ++cnt)
193 XGrabButton(otk::OBDisplay::display, button, modifiers | _mask_list[cnt],
194 grab_window, owner_events, event_mask, pointer_mode,
195 keyboard_mode, confine_to, cursor);
200 * Releases the grab on a button, and ungrabs all possible combinations of the
201 * keyboard lock keys.
203 void OBDisplay::ungrabButton(unsigned int button, unsigned int modifiers,
204 Window grab_window) {
205 for (size_t cnt = 0; cnt < 8; ++cnt)
206 XUngrabButton(otk::OBDisplay::display, button, modifiers | _mask_list[cnt],