1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
6 #include "screeninfo.hh"
7 #include "rendercontrol.hh"
11 #include <X11/keysym.h>
14 #include <X11/XKBlib.h>
18 #include <X11/extensions/shape.h>
22 #include <X11/extensions/Xinerama.h>
27 #endif // HAVE_SIGNAL_H
31 #endif // HAVE_FCNTL_H
34 # include <sys/types.h>
36 #endif // HAVE_UNISTD_H
38 #include "../src/gettext.h"
39 #define _(str) gettext(str)
47 Display *display = (Display*) 0;
49 static int xerrorHandler(::Display *d, XErrorEvent *e)
51 if (!display->ignoreErrors()) {
55 //if (e->error_code != BadWindow)
57 XGetErrorText(d, e->error_code, errtxt, 127);
58 printf("X Error: %s\n", errtxt);
59 if (e->error_code != BadWindow)
76 _shape_event_basep(0),
78 _xinerama_event_basep(0),
84 _rendercontrol_list(0)
92 if (!(_display = XOpenDisplay(NULL))) {
93 printf(_("Unable to open connection to the X server. Please set the \n\
94 DISPLAY environment variable approriately.\n\n"));
97 if (fcntl(ConnectionNumber(_display), F_SETFD, 1) == -1) {
98 printf(_("Couldn't mark display connection as close-on-exec.\n\n"));
101 if (!XSupportsLocale())
102 printf(_("X server does not support locale.\n"));
103 if (!XSetLocaleModifiers(""))
104 printf(_("Cannot set locale modifiers for the X server.\n"));
106 // set our error handler for X errors
107 XSetErrorHandler(xerrorHandler);
109 // set the DISPLAY environment variable for any lauched children, to the
110 // display we're using, so they open in the right place.
111 putenv(std::string("DISPLAY=") + DisplayString(_display));
113 // find the availability of X extensions we like to use
115 _xkb = XkbQueryExtension(_display, &junk, &_xkb_event_basep, &junk, NULL,
120 _shape = XShapeQueryExtension(_display, &_shape_event_basep, &junk);
124 _xinerama = XineramaQueryExtension(_display, &_xinerama_event_basep, &junk);
127 // get lock masks that are defined by the display (not constant)
128 _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 _num_lock_mask = mask_table[cnt / _modmap->max_keypermod];
148 if (scroll_lock == _modmap->modifiermap[cnt])
149 _scroll_lock_mask = mask_table[cnt / _modmap->max_keypermod];
154 _mask_list[1] = LockMask;
155 _mask_list[2] = _num_lock_mask;
156 _mask_list[3] = LockMask | _num_lock_mask;
157 _mask_list[4] = _scroll_lock_mask;
158 _mask_list[5] = _scroll_lock_mask | LockMask;
159 _mask_list[6] = _scroll_lock_mask | _num_lock_mask;
160 _mask_list[7] = _scroll_lock_mask | LockMask | _num_lock_mask;
162 // Get information on all the screens which are available, and create their
164 _screeninfo_list = new ScreenInfo*[ScreenCount(_display)];
165 _rendercontrol_list = new RenderControl*[ScreenCount(_display)];
166 for (int i = 0; i < ScreenCount(_display); ++i) {
167 _screeninfo_list[i] = new ScreenInfo(i);
168 _rendercontrol_list[i] = RenderControl::getRenderControl(i);
175 while (_grab_count > 0)
178 XFreeModifiermap(_modmap);
180 for (int i = 0; i < ScreenCount(_display); ++i) {
181 delete _rendercontrol_list[i];
182 delete _screeninfo_list[i];
184 delete [] _rendercontrol_list;
185 delete [] _screeninfo_list;
187 XCloseDisplay(_display);
191 const ScreenInfo* Display::screenInfo(int snum) const
194 assert(snum < (signed) ScreenCount(_display));
195 return _screeninfo_list[snum];
199 const ScreenInfo* Display::findScreen(Window root) const
201 for (int i = 0; i < ScreenCount(_display); ++i)
202 if (_screeninfo_list[i]->rootWindow() == root)
203 return _screeninfo_list[i];
208 const RenderControl *Display::renderControl(int snum) const
211 assert(snum < (signed) ScreenCount(_display));
212 return _rendercontrol_list[snum];
216 void Display::setIgnoreErrors(bool t)
219 // sync up so that anything already sent is/isn't ignored!
220 XSync(_display, false);
225 if (_grab_count == 0) {
226 XGrabServer(_display);
227 XSync(_display, false); // make sure it kicks in
233 void Display::ungrab()
235 if (_grab_count == 0) return;
237 if (_grab_count == 0) {
238 XUngrabServer(_display);
239 XFlush(_display); // ungrab as soon as possible
250 * Grabs a button, but also grabs the button in every possible combination
251 * with the keyboard lock keys, so that they do not cancel out the event.
253 * if allow_scroll_lock is true then only the top half of the lock mask
254 * table is used and scroll lock is ignored. This value defaults to false.
256 void Display::grabButton(unsigned int button, unsigned int modifiers,
257 Window grab_window, bool owner_events,
258 unsigned int event_mask, int pointer_mode,
259 int keyboard_mode, Window confine_to,
260 Cursor cursor, bool allow_scroll_lock) const
262 unsigned int length = (allow_scroll_lock) ? 8 / 2:
264 for (size_t cnt = 0; cnt < length; ++cnt)
265 XGrabButton(_display, button, modifiers | _mask_list[cnt],
266 grab_window, owner_events, event_mask, pointer_mode,
267 keyboard_mode, confine_to, cursor);
272 * Releases the grab on a button, and ungrabs all possible combinations of the
273 * keyboard lock keys.
275 void Display::ungrabButton(unsigned int button, unsigned int modifiers,
276 Window grab_window) const
278 for (size_t cnt = 0; cnt < 8; ++cnt)
279 XUngrabButton(_display, button, modifiers | _mask_list[cnt],
283 void Display::grabKey(unsigned int keycode, unsigned int modifiers,
284 Window grab_window, bool owner_events,
285 int pointer_mode, int keyboard_mode,
286 bool allow_scroll_lock) const
288 unsigned int length = (allow_scroll_lock) ? 8 / 2:
290 for (size_t cnt = 0; cnt < length; ++cnt)
291 XGrabKey(_display, keycode, modifiers | _mask_list[cnt],
292 grab_window, owner_events, pointer_mode, keyboard_mode);
295 void Display::ungrabKey(unsigned int keycode, unsigned int modifiers,
296 Window grab_window) const
298 for (size_t cnt = 0; cnt < 8; ++cnt)
299 XUngrabKey(_display, keycode, modifiers | _mask_list[cnt],