1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
8 #include "screeninfo.hh"
13 #include <X11/keysym.h>
16 #include <X11/XKBlib.h>
20 #include <X11/extensions/shape.h>
24 #include <X11/extensions/Xinerama.h>
29 #endif // HAVE_STDIO_H
33 #endif // HAVE_SIGNAL_H
37 #endif // HAVE_FCNTL_H
40 # include <sys/types.h>
42 #endif // HAVE_UNISTD_H
45 #define _(str) gettext(str)
51 ::Display *Display::display = (::Display*) 0;
52 bool Display::_xkb = false;
53 int Display::_xkb_event_basep = 0;
54 bool Display::_shape = false;
55 int Display::_shape_event_basep = 0;
56 bool Display::_xinerama = false;
57 int Display::_xinerama_event_basep = 0;
58 unsigned int Display::_mask_list[8];
59 unsigned int Display::_scrollLockMask = 0;
60 unsigned int Display::_numLockMask = 0;
61 Display::ScreenInfoList Display::_screenInfoList;
62 GCCache *Display::_gccache = (GCCache*) 0;
63 int Display::_grab_count = 0;
66 static int xerrorHandler(::Display *d, XErrorEvent *e)
71 //if (e->error_code != BadWindow)
73 XGetErrorText(d, e->error_code, errtxt, 128);
74 printf("X Error: %s\n", errtxt);
75 if (e->error_code != BadWindow)
87 void Display::initialize(char *name)
93 if (!(display = XOpenDisplay(name))) {
94 printf(_("Unable to open connection to the X server. Please set the \n\
95 DISPLAY environment variable approriately, or use the '-display' command \n\
96 line argument.\n\n"));
99 if (fcntl(ConnectionNumber(display), F_SETFD, 1) == -1) {
100 printf(_("Couldn't mark display connection as close-on-exec.\n\n"));
104 // set our error handler for X errors
105 XSetErrorHandler(xerrorHandler);
107 // set the DISPLAY environment variable for any lauched children, to the
108 // display we're using, so they open in the right place.
109 // XXX rm -> std::string dtmp = "DISPLAY=" + DisplayString(display);
110 putenv(std::string("DISPLAY=") + DisplayString(display));
112 // find the availability of X extensions we like to use
114 _xkb = XkbQueryExtension(display, &junk, &_xkb_event_basep, &junk, NULL,
119 _shape = XShapeQueryExtension(display, &_shape_event_basep, &junk);
123 _xinerama = XineramaQueryExtension(display, &_xinerama_event_basep, &junk);
126 // get lock masks that are defined by the display (not constant)
127 XModifierKeymap *modmap;
129 modmap = XGetModifierMapping(display);
130 if (modmap && modmap->max_keypermod > 0) {
131 const int mask_table[] = {
132 ShiftMask, LockMask, ControlMask, Mod1Mask,
133 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
135 const size_t size = (sizeof(mask_table) / sizeof(mask_table[0])) *
136 modmap->max_keypermod;
137 // get the values of the keyboard lock modifiers
138 // Note: Caps lock is not retrieved the same way as Scroll and Num lock
139 // since it doesn't need to be.
140 const KeyCode num_lock = XKeysymToKeycode(display, XK_Num_Lock);
141 const KeyCode scroll_lock = XKeysymToKeycode(display, XK_Scroll_Lock);
143 for (size_t cnt = 0; cnt < size; ++cnt) {
144 if (! modmap->modifiermap[cnt]) continue;
146 if (num_lock == modmap->modifiermap[cnt])
147 _numLockMask = mask_table[cnt / modmap->max_keypermod];
148 if (scroll_lock == modmap->modifiermap[cnt])
149 _scrollLockMask = mask_table[cnt / modmap->max_keypermod];
153 if (modmap) XFreeModifiermap(modmap);
156 _mask_list[1] = LockMask;
157 _mask_list[2] = _numLockMask;
158 _mask_list[3] = LockMask | _numLockMask;
159 _mask_list[4] = _scrollLockMask;
160 _mask_list[5] = _scrollLockMask | LockMask;
161 _mask_list[6] = _scrollLockMask | _numLockMask;
162 _mask_list[7] = _scrollLockMask | LockMask | _numLockMask;
164 // Get information on all the screens which are available.
165 _screenInfoList.reserve(ScreenCount(display));
166 for (int i = 0; i < ScreenCount(display); ++i)
167 _screenInfoList.push_back(ScreenInfo(i));
169 _gccache = new GCCache(_screenInfoList.size());
173 void Display::destroy()
176 while (_grab_count > 0)
178 XCloseDisplay(display);
182 const ScreenInfo* Display::screenInfo(int snum) {
184 assert(snum < static_cast<int>(_screenInfoList.size()));
185 return &_screenInfoList[snum];
189 const ScreenInfo* Display::findScreen(Window root)
191 ScreenInfoList::iterator it, end = _screenInfoList.end();
192 for (it = _screenInfoList.begin(); it != end; ++it)
193 if (it->rootWindow() == root)
201 if (_grab_count == 0)
202 XGrabServer(display);
207 void Display::ungrab()
209 if (_grab_count == 0) return;
211 if (_grab_count == 0)
212 XUngrabServer(display);
222 * Grabs a button, but also grabs the button in every possible combination
223 * with the keyboard lock keys, so that they do not cancel out the event.
225 * if allow_scroll_lock is true then only the top half of the lock mask
226 * table is used and scroll lock is ignored. This value defaults to false.
228 void Display::grabButton(unsigned int button, unsigned int modifiers,
229 Window grab_window, bool owner_events,
230 unsigned int event_mask, int pointer_mode,
231 int keyboard_mode, Window confine_to,
232 Cursor cursor, bool allow_scroll_lock) {
233 unsigned int length = (allow_scroll_lock) ? 8 / 2:
235 for (size_t cnt = 0; cnt < length; ++cnt)
236 XGrabButton(Display::display, button, modifiers | _mask_list[cnt],
237 grab_window, owner_events, event_mask, pointer_mode,
238 keyboard_mode, confine_to, cursor);
243 * Releases the grab on a button, and ungrabs all possible combinations of the
244 * keyboard lock keys.
246 void Display::ungrabButton(unsigned int button, unsigned int modifiers,
247 Window grab_window) {
248 for (size_t cnt = 0; cnt < 8; ++cnt)
249 XUngrabButton(Display::display, button, modifiers | _mask_list[cnt],
253 void Display::grabKey(unsigned int keycode, unsigned int modifiers,
254 Window grab_window, bool owner_events,
255 int pointer_mode, int keyboard_mode,
256 bool allow_scroll_lock)
258 unsigned int length = (allow_scroll_lock) ? 8 / 2:
260 for (size_t cnt = 0; cnt < length; ++cnt)
261 XGrabKey(Display::display, keycode, modifiers | _mask_list[cnt],
262 grab_window, owner_events, pointer_mode, keyboard_mode);
265 void Display::ungrabKey(unsigned int keycode, unsigned int modifiers,
268 for (size_t cnt = 0; cnt < 8; ++cnt)
269 XUngrabKey(Display::display, keycode, modifiers | _mask_list[cnt],