1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
8 #include "screeninfo.hh"
10 #include "rendercontrol.hh"
14 #include <X11/keysym.h>
17 #include <X11/XKBlib.h>
21 #include <X11/extensions/shape.h>
25 #include <X11/extensions/Xinerama.h>
30 #endif // HAVE_STDIO_H
34 #endif // HAVE_SIGNAL_H
38 #endif // HAVE_FCNTL_H
41 # include <sys/types.h>
43 #endif // HAVE_UNISTD_H
46 #define _(str) gettext(str)
52 Display *display = (Display*) 0;
54 static int xerrorHandler(::Display *d, XErrorEvent *e)
59 //if (e->error_code != BadWindow)
61 XGetErrorText(d, e->error_code, errtxt, 128);
62 printf("X Error: %s\n", errtxt);
63 if (e->error_code != BadWindow)
80 _shape_event_basep(0),
82 _xinerama_event_basep(0),
88 _rendercontrol_list(0),
89 _gccache((GCCache*) 0)
97 if (!(_display = XOpenDisplay(NULL))) {
98 printf(_("Unable to open connection to the X server. Please set the \n\
99 DISPLAY environment variable approriately.\n\n"));
102 if (fcntl(ConnectionNumber(_display), F_SETFD, 1) == -1) {
103 printf(_("Couldn't mark display connection as close-on-exec.\n\n"));
106 if (!XSupportsLocale())
107 printf(_("X server does not support locale.\n"));
108 if (!XSetLocaleModifiers(""))
109 printf(_("Cannot set locale modifiers for the X server.\n"));
111 // set our error handler for X errors
112 XSetErrorHandler(xerrorHandler);
114 // set the DISPLAY environment variable for any lauched children, to the
115 // display we're using, so they open in the right place.
116 putenv(std::string("DISPLAY=") + DisplayString(_display));
118 // find the availability of X extensions we like to use
120 _xkb = XkbQueryExtension(_display, &junk, &_xkb_event_basep, &junk, NULL,
125 _shape = XShapeQueryExtension(_display, &_shape_event_basep, &junk);
129 _xinerama = XineramaQueryExtension(_display, &_xinerama_event_basep, &junk);
132 // get lock masks that are defined by the display (not constant)
133 XModifierKeymap *modmap;
135 modmap = XGetModifierMapping(_display);
136 if (modmap && modmap->max_keypermod > 0) {
137 const int mask_table[] = {
138 ShiftMask, LockMask, ControlMask, Mod1Mask,
139 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
141 const size_t size = (sizeof(mask_table) / sizeof(mask_table[0])) *
142 modmap->max_keypermod;
143 // get the values of the keyboard lock modifiers
144 // Note: Caps lock is not retrieved the same way as Scroll and Num lock
145 // since it doesn't need to be.
146 const KeyCode num_lock = XKeysymToKeycode(_display, XK_Num_Lock);
147 const KeyCode scroll_lock = XKeysymToKeycode(_display, XK_Scroll_Lock);
149 for (size_t cnt = 0; cnt < size; ++cnt) {
150 if (! modmap->modifiermap[cnt]) continue;
152 if (num_lock == modmap->modifiermap[cnt])
153 _num_lock_mask = mask_table[cnt / modmap->max_keypermod];
154 if (scroll_lock == modmap->modifiermap[cnt])
155 _scroll_lock_mask = mask_table[cnt / modmap->max_keypermod];
159 if (modmap) XFreeModifiermap(modmap);
162 _mask_list[1] = LockMask;
163 _mask_list[2] = _num_lock_mask;
164 _mask_list[3] = LockMask | _num_lock_mask;
165 _mask_list[4] = _scroll_lock_mask;
166 _mask_list[5] = _scroll_lock_mask | LockMask;
167 _mask_list[6] = _scroll_lock_mask | _num_lock_mask;
168 _mask_list[7] = _scroll_lock_mask | LockMask | _num_lock_mask;
170 // Get information on all the screens which are available, and create their
172 _screeninfo_list = new ScreenInfo*[ScreenCount(_display)];
173 _rendercontrol_list = new RenderControl*[ScreenCount(_display)];
174 for (int i = 0; i < ScreenCount(_display); ++i) {
175 _screeninfo_list[i] = new ScreenInfo(i);
176 _rendercontrol_list[i] = RenderControl::getRenderControl(i);
179 _gccache = new GCCache(ScreenCount(_display));
186 while (_grab_count > 0)
189 for (int i = 0; i < ScreenCount(_display); ++i) {
190 delete _rendercontrol_list[i];
191 delete _screeninfo_list[i];
193 delete [] _rendercontrol_list;
194 delete [] _screeninfo_list;
196 XCloseDisplay(_display);
200 const ScreenInfo* Display::screenInfo(int snum)
203 assert(snum < (signed) ScreenCount(_display));
204 return _screeninfo_list[snum];
208 const ScreenInfo* Display::findScreen(Window root)
210 for (int i = 0; i < ScreenCount(_display); ++i)
211 if (_screeninfo_list[i]->rootWindow() == root)
212 return _screeninfo_list[i];
217 const RenderControl *Display::renderControl(int snum)
220 assert(snum < (signed) ScreenCount(_display));
221 return _rendercontrol_list[snum];
227 if (_grab_count == 0)
228 XGrabServer(_display);
233 void Display::ungrab()
235 if (_grab_count == 0) return;
237 if (_grab_count == 0)
238 XUngrabServer(_display);
248 * Grabs a button, but also grabs the button in every possible combination
249 * with the keyboard lock keys, so that they do not cancel out the event.
251 * if allow_scroll_lock is true then only the top half of the lock mask
252 * table is used and scroll lock is ignored. This value defaults to false.
254 void Display::grabButton(unsigned int button, unsigned int modifiers,
255 Window grab_window, bool owner_events,
256 unsigned int event_mask, int pointer_mode,
257 int keyboard_mode, Window confine_to,
258 Cursor cursor, bool allow_scroll_lock) const
260 unsigned int length = (allow_scroll_lock) ? 8 / 2:
262 for (size_t cnt = 0; cnt < length; ++cnt)
263 XGrabButton(_display, button, modifiers | _mask_list[cnt],
264 grab_window, owner_events, event_mask, pointer_mode,
265 keyboard_mode, confine_to, cursor);
270 * Releases the grab on a button, and ungrabs all possible combinations of the
271 * keyboard lock keys.
273 void Display::ungrabButton(unsigned int button, unsigned int modifiers,
274 Window grab_window) const
276 for (size_t cnt = 0; cnt < 8; ++cnt)
277 XUngrabButton(_display, button, modifiers | _mask_list[cnt],
281 void Display::grabKey(unsigned int keycode, unsigned int modifiers,
282 Window grab_window, bool owner_events,
283 int pointer_mode, int keyboard_mode,
284 bool allow_scroll_lock) const
286 unsigned int length = (allow_scroll_lock) ? 8 / 2:
288 for (size_t cnt = 0; cnt < length; ++cnt)
289 XGrabKey(_display, keycode, modifiers | _mask_list[cnt],
290 grab_window, owner_events, pointer_mode, keyboard_mode);
293 void Display::ungrabKey(unsigned int keycode, unsigned int modifiers,
294 Window grab_window) const
296 for (size_t cnt = 0; cnt < 8; ++cnt)
297 XUngrabKey(_display, keycode, modifiers | _mask_list[cnt],