1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
8 #include "screeninfo.hh"
9 #include "rendercontrol.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
44 #include "../src/gettext.h"
45 #define _(str) gettext(str)
51 Display *display = (Display*) 0;
53 static int xerrorHandler(::Display *d, XErrorEvent *e)
58 //if (e->error_code != BadWindow)
60 XGetErrorText(d, e->error_code, errtxt, 128);
61 printf("X Error: %s\n", errtxt);
62 if (e->error_code != BadWindow)
79 _shape_event_basep(0),
81 _xinerama_event_basep(0),
87 _rendercontrol_list(0)
95 if (!(_display = XOpenDisplay(NULL))) {
96 printf(_("Unable to open connection to the X server. Please set the \n\
97 DISPLAY environment variable approriately.\n\n"));
100 if (fcntl(ConnectionNumber(_display), F_SETFD, 1) == -1) {
101 printf(_("Couldn't mark display connection as close-on-exec.\n\n"));
104 if (!XSupportsLocale())
105 printf(_("X server does not support locale.\n"));
106 if (!XSetLocaleModifiers(""))
107 printf(_("Cannot set locale modifiers for the X server.\n"));
109 // set our error handler for X errors
110 XSetErrorHandler(xerrorHandler);
112 // set the DISPLAY environment variable for any lauched children, to the
113 // display we're using, so they open in the right place.
114 putenv(std::string("DISPLAY=") + DisplayString(_display));
116 // find the availability of X extensions we like to use
118 _xkb = XkbQueryExtension(_display, &junk, &_xkb_event_basep, &junk, NULL,
123 _shape = XShapeQueryExtension(_display, &_shape_event_basep, &junk);
127 _xinerama = XineramaQueryExtension(_display, &_xinerama_event_basep, &junk);
130 // get lock masks that are defined by the display (not constant)
131 _modmap = XGetModifierMapping(_display);
133 if (_modmap && _modmap->max_keypermod > 0) {
134 const int mask_table[] = {
135 ShiftMask, LockMask, ControlMask, Mod1Mask,
136 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
138 const size_t size = (sizeof(mask_table) / sizeof(mask_table[0])) *
139 _modmap->max_keypermod;
140 // get the values of the keyboard lock modifiers
141 // Note: Caps lock is not retrieved the same way as Scroll and Num lock
142 // since it doesn't need to be.
143 const KeyCode num_lock = XKeysymToKeycode(_display, XK_Num_Lock);
144 const KeyCode scroll_lock = XKeysymToKeycode(_display, XK_Scroll_Lock);
146 for (size_t cnt = 0; cnt < size; ++cnt) {
147 if (! _modmap->modifiermap[cnt]) continue;
149 if (num_lock == _modmap->modifiermap[cnt])
150 _num_lock_mask = mask_table[cnt / _modmap->max_keypermod];
151 if (scroll_lock == _modmap->modifiermap[cnt])
152 _scroll_lock_mask = mask_table[cnt / _modmap->max_keypermod];
157 _mask_list[1] = LockMask;
158 _mask_list[2] = _num_lock_mask;
159 _mask_list[3] = LockMask | _num_lock_mask;
160 _mask_list[4] = _scroll_lock_mask;
161 _mask_list[5] = _scroll_lock_mask | LockMask;
162 _mask_list[6] = _scroll_lock_mask | _num_lock_mask;
163 _mask_list[7] = _scroll_lock_mask | LockMask | _num_lock_mask;
165 // Get information on all the screens which are available, and create their
167 _screeninfo_list = new ScreenInfo*[ScreenCount(_display)];
168 _rendercontrol_list = new RenderControl*[ScreenCount(_display)];
169 for (int i = 0; i < ScreenCount(_display); ++i) {
170 _screeninfo_list[i] = new ScreenInfo(i);
171 _rendercontrol_list[i] = RenderControl::getRenderControl(i);
178 while (_grab_count > 0)
181 XFreeModifiermap(_modmap);
183 for (int i = 0; i < ScreenCount(_display); ++i) {
184 delete _rendercontrol_list[i];
185 delete _screeninfo_list[i];
187 delete [] _rendercontrol_list;
188 delete [] _screeninfo_list;
190 XCloseDisplay(_display);
194 const ScreenInfo* Display::screenInfo(int snum) const
197 assert(snum < (signed) ScreenCount(_display));
198 return _screeninfo_list[snum];
202 const ScreenInfo* Display::findScreen(Window root) const
204 for (int i = 0; i < ScreenCount(_display); ++i)
205 if (_screeninfo_list[i]->rootWindow() == root)
206 return _screeninfo_list[i];
211 const RenderControl *Display::renderControl(int snum) const
214 assert(snum < (signed) ScreenCount(_display));
215 return _rendercontrol_list[snum];
221 if (_grab_count == 0)
222 XGrabServer(_display);
227 void Display::ungrab()
229 if (_grab_count == 0) return;
231 if (_grab_count == 0)
232 XUngrabServer(_display);
242 * Grabs a button, but also grabs the button in every possible combination
243 * with the keyboard lock keys, so that they do not cancel out the event.
245 * if allow_scroll_lock is true then only the top half of the lock mask
246 * table is used and scroll lock is ignored. This value defaults to false.
248 void Display::grabButton(unsigned int button, unsigned int modifiers,
249 Window grab_window, bool owner_events,
250 unsigned int event_mask, int pointer_mode,
251 int keyboard_mode, Window confine_to,
252 Cursor cursor, bool allow_scroll_lock) const
254 unsigned int length = (allow_scroll_lock) ? 8 / 2:
256 for (size_t cnt = 0; cnt < length; ++cnt)
257 XGrabButton(_display, button, modifiers | _mask_list[cnt],
258 grab_window, owner_events, event_mask, pointer_mode,
259 keyboard_mode, confine_to, cursor);
264 * Releases the grab on a button, and ungrabs all possible combinations of the
265 * keyboard lock keys.
267 void Display::ungrabButton(unsigned int button, unsigned int modifiers,
268 Window grab_window) const
270 for (size_t cnt = 0; cnt < 8; ++cnt)
271 XUngrabButton(_display, button, modifiers | _mask_list[cnt],
275 void Display::grabKey(unsigned int keycode, unsigned int modifiers,
276 Window grab_window, bool owner_events,
277 int pointer_mode, int keyboard_mode,
278 bool allow_scroll_lock) const
280 unsigned int length = (allow_scroll_lock) ? 8 / 2:
282 for (size_t cnt = 0; cnt < length; ++cnt)
283 XGrabKey(_display, keycode, modifiers | _mask_list[cnt],
284 grab_window, owner_events, pointer_mode, keyboard_mode);
287 void Display::ungrabKey(unsigned int keycode, unsigned int modifiers,
288 Window grab_window) const
290 for (size_t cnt = 0; cnt < 8; ++cnt)
291 XUngrabKey(_display, keycode, modifiers | _mask_list[cnt],