]> Dogcows Code - chaz/openbox/blob - otk/display.cc
change the default titlebar layout
[chaz/openbox] / otk / display.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #include "config.h"
4
5 #include "display.hh"
6 #include "screeninfo.hh"
7 #include "rendercontrol.hh"
8 #include "util.hh"
9
10 extern "C" {
11 #include <X11/keysym.h>
12
13 #ifdef XKB
14 #include <X11/XKBlib.h>
15 #endif // XKB
16
17 #ifdef SHAPE
18 #include <X11/extensions/shape.h>
19 #endif // SHAPE
20
21 #ifdef XINERAMA
22 #include <X11/extensions/Xinerama.h>
23 #endif // XINERAMA
24
25 #ifdef HAVE_SIGNAL_H
26 # include <signal.h>
27 #endif // HAVE_SIGNAL_H
28
29 #ifdef HAVE_FCNTL_H
30 # include <fcntl.h>
31 #endif // HAVE_FCNTL_H
32
33 #ifdef HAVE_UNISTD_H
34 # include <sys/types.h>
35 # include <unistd.h>
36 #endif // HAVE_UNISTD_H
37
38 #include "../src/gettext.h"
39 #define _(str) gettext(str)
40 }
41
42 #include <cstdio>
43
44 namespace otk {
45
46
47 Display *display = (Display*) 0;
48
49 static int xerrorHandler(::Display *d, XErrorEvent *e)
50 {
51 if (!display->ignoreErrors()) {
52 #ifdef DEBUG
53 char errtxt[128];
54
55 //if (e->error_code != BadWindow)
56 {
57 XGetErrorText(d, e->error_code, errtxt, 127);
58 printf("X Error: %s\n", errtxt);
59 if (e->error_code != BadWindow)
60 abort();
61 }
62 #else
63 (void)d;
64 (void)e;
65 #endif
66 }
67 return false;
68 }
69
70
71 Display::Display()
72 : _display(0),
73 _xkb(false),
74 _xkb_event_basep(0),
75 _shape(false),
76 _shape_event_basep(0),
77 _xinerama(false),
78 _xinerama_event_basep(0),
79 _mask_list(),
80 _num_lock_mask(0),
81 _scroll_lock_mask(0),
82 _grab_count(0),
83 _screeninfo_list(0),
84 _rendercontrol_list(0)
85 {
86 int junk;
87 (void)junk;
88
89 display = this;
90
91 // Open the X display
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"));
95 ::exit(1);
96 }
97 if (fcntl(ConnectionNumber(_display), F_SETFD, 1) == -1) {
98 printf(_("Couldn't mark display connection as close-on-exec.\n\n"));
99 ::exit(1);
100 }
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"));
105
106 // set our error handler for X errors
107 XSetErrorHandler(xerrorHandler);
108
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));
112
113 // find the availability of X extensions we like to use
114 #ifdef XKB
115 _xkb = XkbQueryExtension(_display, &junk, &_xkb_event_basep, &junk, NULL,
116 NULL);
117 #endif
118
119 #ifdef SHAPE
120 _shape = XShapeQueryExtension(_display, &_shape_event_basep, &junk);
121 #endif
122
123 #ifdef XINERAMA
124 _xinerama = XineramaQueryExtension(_display, &_xinerama_event_basep, &junk);
125 #endif // XINERAMA
126
127 // get lock masks that are defined by the display (not constant)
128 _modmap = XGetModifierMapping(_display);
129 assert(_modmap);
130 if (_modmap && _modmap->max_keypermod > 0) {
131 const int mask_table[] = {
132 ShiftMask, LockMask, ControlMask, Mod1Mask,
133 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
134 };
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);
142
143 for (size_t cnt = 0; cnt < size; ++cnt) {
144 if (! _modmap->modifiermap[cnt]) continue;
145
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];
150 }
151 }
152
153 _mask_list[0] = 0;
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;
161
162 // Get information on all the screens which are available, and create their
163 // RenderControl
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);
169 }
170 }
171
172
173 Display::~Display()
174 {
175 while (_grab_count > 0)
176 ungrab();
177
178 XFreeModifiermap(_modmap);
179
180 for (int i = 0; i < ScreenCount(_display); ++i) {
181 delete _rendercontrol_list[i];
182 delete _screeninfo_list[i];
183 }
184 delete [] _rendercontrol_list;
185 delete [] _screeninfo_list;
186
187 XCloseDisplay(_display);
188 }
189
190
191 const ScreenInfo* Display::screenInfo(int snum) const
192 {
193 assert(snum >= 0);
194 assert(snum < (signed) ScreenCount(_display));
195 return _screeninfo_list[snum];
196 }
197
198
199 const ScreenInfo* Display::findScreen(Window root) const
200 {
201 for (int i = 0; i < ScreenCount(_display); ++i)
202 if (_screeninfo_list[i]->rootWindow() == root)
203 return _screeninfo_list[i];
204 return 0;
205 }
206
207
208 const RenderControl *Display::renderControl(int snum) const
209 {
210 assert(snum >= 0);
211 assert(snum < (signed) ScreenCount(_display));
212 return _rendercontrol_list[snum];
213 }
214
215
216 void Display::setIgnoreErrors(bool t)
217 {
218 // sync up so that anything already sent is/isn't ignored!
219 XSync(_display, false);
220 _ignore_errors = t;
221 }
222
223 void Display::grab()
224 {
225 if (_grab_count == 0) {
226 XGrabServer(_display);
227 XSync(_display, false); // make sure it kicks in
228 }
229 _grab_count++;
230 }
231
232
233 void Display::ungrab()
234 {
235 if (_grab_count == 0) return;
236 _grab_count--;
237 if (_grab_count == 0) {
238 XUngrabServer(_display);
239 XFlush(_display); // ungrab as soon as possible
240 }
241 }
242
243
244
245
246
247
248
249 /*
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.
252
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.
255 */
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
261 {
262 unsigned int length = (allow_scroll_lock) ? 8 / 2:
263 8;
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);
268 }
269
270
271 /*
272 * Releases the grab on a button, and ungrabs all possible combinations of the
273 * keyboard lock keys.
274 */
275 void Display::ungrabButton(unsigned int button, unsigned int modifiers,
276 Window grab_window) const
277 {
278 for (size_t cnt = 0; cnt < 8; ++cnt)
279 XUngrabButton(_display, button, modifiers | _mask_list[cnt],
280 grab_window);
281 }
282
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
287 {
288 unsigned int length = (allow_scroll_lock) ? 8 / 2:
289 8;
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);
293 }
294
295 void Display::ungrabKey(unsigned int keycode, unsigned int modifiers,
296 Window grab_window) const
297 {
298 for (size_t cnt = 0; cnt < 8; ++cnt)
299 XUngrabKey(_display, keycode, modifiers | _mask_list[cnt],
300 grab_window);
301 }
302
303 void Display::ungrabAllKeys(Window grab_window) const
304 {
305 XUngrabKey(_display, AnyKey, AnyModifier, grab_window);
306 }
307
308 }
This page took 0.04874 seconds and 4 git commands to generate.