]> Dogcows Code - chaz/openbox/blob - otk/display.cc
allow for ignoring x errors.
[chaz/openbox] / otk / display.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #ifdef HAVE_CONFIG_H
4 # include "../config.h"
5 #endif
6
7 #include "display.hh"
8 #include "screeninfo.hh"
9 #include "rendercontrol.hh"
10 #include "util.hh"
11
12 extern "C" {
13 #include <X11/keysym.h>
14
15 #ifdef XKB
16 #include <X11/XKBlib.h>
17 #endif // XKB
18
19 #ifdef SHAPE
20 #include <X11/extensions/shape.h>
21 #endif // SHAPE
22
23 #ifdef XINERAMA
24 #include <X11/extensions/Xinerama.h>
25 #endif // XINERAMA
26
27 #ifdef HAVE_STDIO_H
28 # include <stdio.h>
29 #endif // HAVE_STDIO_H
30
31 #ifdef HAVE_SIGNAL_H
32 # include <signal.h>
33 #endif // HAVE_SIGNAL_H
34
35 #ifdef HAVE_FCNTL_H
36 # include <fcntl.h>
37 #endif // HAVE_FCNTL_H
38
39 #ifdef HAVE_UNISTD_H
40 # include <sys/types.h>
41 # include <unistd.h>
42 #endif // HAVE_UNISTD_H
43
44 #include "../src/gettext.h"
45 #define _(str) gettext(str)
46 }
47
48 namespace otk {
49
50
51 Display *display = (Display*) 0;
52
53 static int xerrorHandler(::Display *d, XErrorEvent *e)
54 {
55 if (!display->ignoreErrors()) {
56 #ifdef DEBUG
57 char errtxt[128];
58
59 //if (e->error_code != BadWindow)
60 {
61 XGetErrorText(d, e->error_code, errtxt, 128);
62 printf("X Error: %s\n", errtxt);
63 if (e->error_code != BadWindow)
64 abort();
65 }
66 #else
67 (void)d;
68 (void)e;
69 #endif
70 }
71 return false;
72 }
73
74
75 Display::Display()
76 : _display(0),
77 _xkb(false),
78 _xkb_event_basep(0),
79 _shape(false),
80 _shape_event_basep(0),
81 _xinerama(false),
82 _xinerama_event_basep(0),
83 _mask_list(),
84 _num_lock_mask(0),
85 _scroll_lock_mask(0),
86 _grab_count(0),
87 _screeninfo_list(0),
88 _rendercontrol_list(0)
89 {
90 int junk;
91 (void)junk;
92
93 display = this;
94
95 // Open the X display
96 if (!(_display = XOpenDisplay(NULL))) {
97 printf(_("Unable to open connection to the X server. Please set the \n\
98 DISPLAY environment variable approriately.\n\n"));
99 ::exit(1);
100 }
101 if (fcntl(ConnectionNumber(_display), F_SETFD, 1) == -1) {
102 printf(_("Couldn't mark display connection as close-on-exec.\n\n"));
103 ::exit(1);
104 }
105 if (!XSupportsLocale())
106 printf(_("X server does not support locale.\n"));
107 if (!XSetLocaleModifiers(""))
108 printf(_("Cannot set locale modifiers for the X server.\n"));
109
110 // set our error handler for X errors
111 XSetErrorHandler(xerrorHandler);
112
113 // set the DISPLAY environment variable for any lauched children, to the
114 // display we're using, so they open in the right place.
115 putenv(std::string("DISPLAY=") + DisplayString(_display));
116
117 // find the availability of X extensions we like to use
118 #ifdef XKB
119 _xkb = XkbQueryExtension(_display, &junk, &_xkb_event_basep, &junk, NULL,
120 NULL);
121 #endif
122
123 #ifdef SHAPE
124 _shape = XShapeQueryExtension(_display, &_shape_event_basep, &junk);
125 #endif
126
127 #ifdef XINERAMA
128 _xinerama = XineramaQueryExtension(_display, &_xinerama_event_basep, &junk);
129 #endif // XINERAMA
130
131 // get lock masks that are defined by the display (not constant)
132 _modmap = XGetModifierMapping(_display);
133 assert(_modmap);
134 if (_modmap && _modmap->max_keypermod > 0) {
135 const int mask_table[] = {
136 ShiftMask, LockMask, ControlMask, Mod1Mask,
137 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
138 };
139 const size_t size = (sizeof(mask_table) / sizeof(mask_table[0])) *
140 _modmap->max_keypermod;
141 // get the values of the keyboard lock modifiers
142 // Note: Caps lock is not retrieved the same way as Scroll and Num lock
143 // since it doesn't need to be.
144 const KeyCode num_lock = XKeysymToKeycode(_display, XK_Num_Lock);
145 const KeyCode scroll_lock = XKeysymToKeycode(_display, XK_Scroll_Lock);
146
147 for (size_t cnt = 0; cnt < size; ++cnt) {
148 if (! _modmap->modifiermap[cnt]) continue;
149
150 if (num_lock == _modmap->modifiermap[cnt])
151 _num_lock_mask = mask_table[cnt / _modmap->max_keypermod];
152 if (scroll_lock == _modmap->modifiermap[cnt])
153 _scroll_lock_mask = mask_table[cnt / _modmap->max_keypermod];
154 }
155 }
156
157 _mask_list[0] = 0;
158 _mask_list[1] = LockMask;
159 _mask_list[2] = _num_lock_mask;
160 _mask_list[3] = LockMask | _num_lock_mask;
161 _mask_list[4] = _scroll_lock_mask;
162 _mask_list[5] = _scroll_lock_mask | LockMask;
163 _mask_list[6] = _scroll_lock_mask | _num_lock_mask;
164 _mask_list[7] = _scroll_lock_mask | LockMask | _num_lock_mask;
165
166 // Get information on all the screens which are available, and create their
167 // RenderControl
168 _screeninfo_list = new ScreenInfo*[ScreenCount(_display)];
169 _rendercontrol_list = new RenderControl*[ScreenCount(_display)];
170 for (int i = 0; i < ScreenCount(_display); ++i) {
171 _screeninfo_list[i] = new ScreenInfo(i);
172 _rendercontrol_list[i] = RenderControl::getRenderControl(i);
173 }
174 }
175
176
177 Display::~Display()
178 {
179 while (_grab_count > 0)
180 ungrab();
181
182 XFreeModifiermap(_modmap);
183
184 for (int i = 0; i < ScreenCount(_display); ++i) {
185 delete _rendercontrol_list[i];
186 delete _screeninfo_list[i];
187 }
188 delete [] _rendercontrol_list;
189 delete [] _screeninfo_list;
190
191 XCloseDisplay(_display);
192 }
193
194
195 const ScreenInfo* Display::screenInfo(int snum) const
196 {
197 assert(snum >= 0);
198 assert(snum < (signed) ScreenCount(_display));
199 return _screeninfo_list[snum];
200 }
201
202
203 const ScreenInfo* Display::findScreen(Window root) const
204 {
205 for (int i = 0; i < ScreenCount(_display); ++i)
206 if (_screeninfo_list[i]->rootWindow() == root)
207 return _screeninfo_list[i];
208 return 0;
209 }
210
211
212 const RenderControl *Display::renderControl(int snum) const
213 {
214 assert(snum >= 0);
215 assert(snum < (signed) ScreenCount(_display));
216 return _rendercontrol_list[snum];
217 }
218
219
220 void Display::setIgnoreErrors(bool t)
221 {
222 _ignore_errors = t;
223 // sync up so that anything already sent is/isn't ignored!
224 XSync(_display, false);
225 }
226
227 void Display::grab()
228 {
229 if (_grab_count == 0) {
230 XGrabServer(_display);
231 XSync(_display, false); // make sure it kicks in
232 }
233 _grab_count++;
234 }
235
236
237 void Display::ungrab()
238 {
239 if (_grab_count == 0) return;
240 _grab_count--;
241 if (_grab_count == 0) {
242 XUngrabServer(_display);
243 XFlush(_display); // ungrab as soon as possible
244 }
245 }
246
247
248
249
250
251
252
253 /*
254 * Grabs a button, but also grabs the button in every possible combination
255 * with the keyboard lock keys, so that they do not cancel out the event.
256
257 * if allow_scroll_lock is true then only the top half of the lock mask
258 * table is used and scroll lock is ignored. This value defaults to false.
259 */
260 void Display::grabButton(unsigned int button, unsigned int modifiers,
261 Window grab_window, bool owner_events,
262 unsigned int event_mask, int pointer_mode,
263 int keyboard_mode, Window confine_to,
264 Cursor cursor, bool allow_scroll_lock) const
265 {
266 unsigned int length = (allow_scroll_lock) ? 8 / 2:
267 8;
268 for (size_t cnt = 0; cnt < length; ++cnt)
269 XGrabButton(_display, button, modifiers | _mask_list[cnt],
270 grab_window, owner_events, event_mask, pointer_mode,
271 keyboard_mode, confine_to, cursor);
272 }
273
274
275 /*
276 * Releases the grab on a button, and ungrabs all possible combinations of the
277 * keyboard lock keys.
278 */
279 void Display::ungrabButton(unsigned int button, unsigned int modifiers,
280 Window grab_window) const
281 {
282 for (size_t cnt = 0; cnt < 8; ++cnt)
283 XUngrabButton(_display, button, modifiers | _mask_list[cnt],
284 grab_window);
285 }
286
287 void Display::grabKey(unsigned int keycode, unsigned int modifiers,
288 Window grab_window, bool owner_events,
289 int pointer_mode, int keyboard_mode,
290 bool allow_scroll_lock) const
291 {
292 unsigned int length = (allow_scroll_lock) ? 8 / 2:
293 8;
294 for (size_t cnt = 0; cnt < length; ++cnt)
295 XGrabKey(_display, keycode, modifiers | _mask_list[cnt],
296 grab_window, owner_events, pointer_mode, keyboard_mode);
297 }
298
299 void Display::ungrabKey(unsigned int keycode, unsigned int modifiers,
300 Window grab_window) const
301 {
302 for (size_t cnt = 0; cnt < 8; ++cnt)
303 XUngrabKey(_display, keycode, modifiers | _mask_list[cnt],
304 grab_window);
305 }
306
307 }
This page took 0.051328 seconds and 5 git commands to generate.