1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
8 #include "screeninfo.hh"
12 #include <X11/keysym.h>
15 #include <X11/XKBlib.h>
19 #include <X11/extensions/shape.h>
23 #include <X11/extensions/Xinerama.h>
28 #endif // HAVE_STDIO_H
32 #endif // HAVE_STDLIB_H
36 #endif // HAVE_SIGNAL_H
40 #endif // HAVE_FCNTL_H
43 # include <sys/types.h>
45 #endif // HAVE_UNISTD_H
48 #define _(str) gettext(str)
54 Display *OBDisplay::display = (Display*) 0;
55 bool OBDisplay::_xkb = false;
56 int OBDisplay::_xkb_event_basep = 0;
57 bool OBDisplay::_shape = false;
58 int OBDisplay::_shape_event_basep = 0;
59 bool OBDisplay::_xinerama = false;
60 int OBDisplay::_xinerama_event_basep = 0;
61 unsigned int OBDisplay::_mask_list[8];
62 unsigned int OBDisplay::_scrollLockMask = 0;
63 unsigned int OBDisplay::_numLockMask = 0;
64 OBDisplay::ScreenInfoList OBDisplay::_screenInfoList;
65 BGCCache *OBDisplay::_gccache = (BGCCache*) 0;
66 int OBDisplay::_grab_count = 0;
69 int OBDisplay::xerrorHandler(Display *d, XErrorEvent *e)
74 //if (e->error_code != BadWindow)
76 XGetErrorText(d, e->error_code, errtxt, 128);
77 printf("X Error: %s\n", errtxt);
78 if (e->error_code != BadWindow)
90 void OBDisplay::initialize(char *name)
96 printf("getenv(DISPLAY)=%s\n", getenv("DISPLAY"));
97 if (!(display = XOpenDisplay(name))) {
98 printf(_("Unable to open connection to the X server. Please set the \n\
99 DISPLAY environment variable approriately, or use the '-display' command \n\
100 line argument.\n\n"));
103 if (fcntl(ConnectionNumber(display), F_SETFD, 1) == -1) {
104 printf(_("Couldn't mark display connection as close-on-exec.\n\n"));
108 // set our error handler for X errors
109 XSetErrorHandler(xerrorHandler);
111 // set the DISPLAY environment variable for any lauched children, to the
112 // display we're using, so they open in the right place.
113 // XXX rm -> std::string dtmp = "DISPLAY=" + DisplayString(display);
114 if (putenv(const_cast<char*>((std::string("DISPLAY=") +
115 DisplayString(display)).c_str()))) {
116 printf(_("warning: couldn't set environment variable 'DISPLAY'\n"));
120 // find the availability of X extensions we like to use
122 _xkb = XkbQueryExtension(display, &junk, &_xkb_event_basep, &junk, NULL,
127 _shape = XShapeQueryExtension(display, &_shape_event_basep, &junk);
131 _xinerama = XineramaQueryExtension(display, &_xinerama_event_basep, &junk);
134 // get lock masks that are defined by the display (not constant)
135 XModifierKeymap *modmap;
137 modmap = XGetModifierMapping(display);
138 if (modmap && modmap->max_keypermod > 0) {
139 const int mask_table[] = {
140 ShiftMask, LockMask, ControlMask, Mod1Mask,
141 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
143 const size_t size = (sizeof(mask_table) / sizeof(mask_table[0])) *
144 modmap->max_keypermod;
145 // get the values of the keyboard lock modifiers
146 // Note: Caps lock is not retrieved the same way as Scroll and Num lock
147 // since it doesn't need to be.
148 const KeyCode num_lock = XKeysymToKeycode(display, XK_Num_Lock);
149 const KeyCode scroll_lock = XKeysymToKeycode(display, XK_Scroll_Lock);
151 for (size_t cnt = 0; cnt < size; ++cnt) {
152 if (! modmap->modifiermap[cnt]) continue;
154 if (num_lock == modmap->modifiermap[cnt])
155 _numLockMask = mask_table[cnt / modmap->max_keypermod];
156 if (scroll_lock == modmap->modifiermap[cnt])
157 _scrollLockMask = mask_table[cnt / modmap->max_keypermod];
161 if (modmap) XFreeModifiermap(modmap);
164 _mask_list[1] = LockMask;
165 _mask_list[2] = _numLockMask;
166 _mask_list[3] = LockMask | _numLockMask;
167 _mask_list[4] = _scrollLockMask;
168 _mask_list[5] = _scrollLockMask | LockMask;
169 _mask_list[6] = _scrollLockMask | _numLockMask;
170 _mask_list[7] = _scrollLockMask | LockMask | _numLockMask;
172 // Get information on all the screens which are available.
173 _screenInfoList.reserve(ScreenCount(display));
174 for (int i = 0; i < ScreenCount(display); ++i)
175 _screenInfoList.push_back(ScreenInfo(i));
177 _gccache = new BGCCache(_screenInfoList.size());
181 void OBDisplay::destroy()
184 while (_grab_count > 0)
186 XCloseDisplay(display);
190 const ScreenInfo* OBDisplay::screenInfo(int snum) {
192 assert(snum < static_cast<int>(_screenInfoList.size()));
193 return &_screenInfoList[snum];
197 const ScreenInfo* OBDisplay::findScreen(Window root)
199 ScreenInfoList::iterator it, end = _screenInfoList.end();
200 for (it = _screenInfoList.begin(); it != end; ++it)
201 if (it->rootWindow() == root)
207 void OBDisplay::grab()
209 if (_grab_count == 0)
210 XGrabServer(display);
215 void OBDisplay::ungrab()
217 if (_grab_count == 0) return;
219 if (_grab_count == 0)
220 XUngrabServer(display);
230 * Grabs a button, but also grabs the button in every possible combination
231 * with the keyboard lock keys, so that they do not cancel out the event.
233 * if allow_scroll_lock is true then only the top half of the lock mask
234 * table is used and scroll lock is ignored. This value defaults to false.
236 void OBDisplay::grabButton(unsigned int button, unsigned int modifiers,
237 Window grab_window, bool owner_events,
238 unsigned int event_mask, int pointer_mode,
239 int keyboard_mode, Window confine_to,
240 Cursor cursor, bool allow_scroll_lock) {
241 unsigned int length = (allow_scroll_lock) ? 8 / 2:
243 for (size_t cnt = 0; cnt < length; ++cnt)
244 XGrabButton(otk::OBDisplay::display, button, modifiers | _mask_list[cnt],
245 grab_window, owner_events, event_mask, pointer_mode,
246 keyboard_mode, confine_to, cursor);
251 * Releases the grab on a button, and ungrabs all possible combinations of the
252 * keyboard lock keys.
254 void OBDisplay::ungrabButton(unsigned int button, unsigned int modifiers,
255 Window grab_window) {
256 for (size_t cnt = 0; cnt < 8; ++cnt)
257 XUngrabButton(otk::OBDisplay::display, button, modifiers | _mask_list[cnt],
261 void OBDisplay::grabKey(unsigned int keycode, unsigned int modifiers,
262 Window grab_window, bool owner_events,
263 int pointer_mode, int keyboard_mode,
264 bool allow_scroll_lock)
266 unsigned int length = (allow_scroll_lock) ? 8 / 2:
268 for (size_t cnt = 0; cnt < length; ++cnt)
269 XGrabKey(otk::OBDisplay::display, keycode, modifiers | _mask_list[cnt],
270 grab_window, owner_events, pointer_mode, keyboard_mode);
273 void OBDisplay::ungrabKey(unsigned int keycode, unsigned int modifiers,
276 for (size_t cnt = 0; cnt < 8; ++cnt)
277 XUngrabKey(otk::OBDisplay::display, keycode, modifiers | _mask_list[cnt],