]> Dogcows Code - chaz/openbox/blob - otk/display.cc
provide RenderControls to all otk from the display class. initialize them all there...
[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 "gccache.hh"
10 #include "rendercontrol.hh"
11 #include "util.hh"
12
13 extern "C" {
14 #include <X11/keysym.h>
15
16 #ifdef XKB
17 #include <X11/XKBlib.h>
18 #endif // XKB
19
20 #ifdef SHAPE
21 #include <X11/extensions/shape.h>
22 #endif // SHAPE
23
24 #ifdef XINERAMA
25 #include <X11/extensions/Xinerama.h>
26 #endif // XINERAMA
27
28 #ifdef HAVE_STDIO_H
29 # include <stdio.h>
30 #endif // HAVE_STDIO_H
31
32 #ifdef HAVE_SIGNAL_H
33 # include <signal.h>
34 #endif // HAVE_SIGNAL_H
35
36 #ifdef HAVE_FCNTL_H
37 # include <fcntl.h>
38 #endif // HAVE_FCNTL_H
39
40 #ifdef HAVE_UNISTD_H
41 # include <sys/types.h>
42 # include <unistd.h>
43 #endif // HAVE_UNISTD_H
44
45 #include "gettext.h"
46 #define _(str) gettext(str)
47 }
48
49 namespace otk {
50
51
52 Display *display = (Display*) 0;
53
54 static int xerrorHandler(::Display *d, XErrorEvent *e)
55 {
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 _screenInfoList(),
88 _renderControlList(),
89 _gccache((GCCache*) 0)
90 {
91 int junk;
92 (void)junk;
93
94 display = this;
95
96 // Open the X display
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"));
100 ::exit(1);
101 }
102 if (fcntl(ConnectionNumber(_display), F_SETFD, 1) == -1) {
103 printf(_("Couldn't mark display connection as close-on-exec.\n\n"));
104 ::exit(1);
105 }
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"));
110
111 // set our error handler for X errors
112 XSetErrorHandler(xerrorHandler);
113
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));
117
118 // find the availability of X extensions we like to use
119 #ifdef XKB
120 _xkb = XkbQueryExtension(_display, &junk, &_xkb_event_basep, &junk, NULL,
121 NULL);
122 #endif
123
124 #ifdef SHAPE
125 _shape = XShapeQueryExtension(_display, &_shape_event_basep, &junk);
126 #endif
127
128 #ifdef XINERAMA
129 _xinerama = XineramaQueryExtension(_display, &_xinerama_event_basep, &junk);
130 #endif // XINERAMA
131
132 // get lock masks that are defined by the display (not constant)
133 XModifierKeymap *modmap;
134
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
140 };
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);
148
149 for (size_t cnt = 0; cnt < size; ++cnt) {
150 if (! modmap->modifiermap[cnt]) continue;
151
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];
156 }
157 }
158
159 if (modmap) XFreeModifiermap(modmap);
160
161 _mask_list[0] = 0;
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;
169
170 // Get information on all the screens which are available.
171 _screenInfoList.reserve(ScreenCount(_display));
172 for (int i = 0; i < ScreenCount(_display); ++i)
173 _screenInfoList.push_back(i);
174
175 _renderControlList.reserve(ScreenCount(_display));
176 for (int i = 0; i < ScreenCount(_display); ++i)
177 _renderControlList.push_back(RenderControl::getRenderControl(i));
178
179 _gccache = new GCCache(_screenInfoList.size());
180 }
181
182
183 Display::~Display()
184 {
185 delete _gccache;
186 while (_grab_count > 0)
187 ungrab();
188 XCloseDisplay(_display);
189 }
190
191
192 const ScreenInfo* Display::screenInfo(int snum)
193 {
194 assert(snum >= 0);
195 assert(snum < static_cast<int>(_screenInfoList.size()));
196 return &_screenInfoList[snum];
197 }
198
199
200 const ScreenInfo* Display::findScreen(Window root)
201 {
202 std::vector<ScreenInfo>::iterator it, end = _screenInfoList.end();
203 for (it = _screenInfoList.begin(); it != end; ++it)
204 if (it->rootWindow() == root)
205 return &(*it);
206 return 0;
207 }
208
209
210 const RenderControl *Display::renderControl(int snum)
211 {
212 assert(snum >= 0);
213 assert(snum < (signed) _renderControlList.size());
214 return _renderControlList[snum];
215 }
216
217
218 void Display::grab()
219 {
220 if (_grab_count == 0)
221 XGrabServer(_display);
222 _grab_count++;
223 }
224
225
226 void Display::ungrab()
227 {
228 if (_grab_count == 0) return;
229 _grab_count--;
230 if (_grab_count == 0)
231 XUngrabServer(_display);
232 }
233
234
235
236
237
238
239
240 /*
241 * Grabs a button, but also grabs the button in every possible combination
242 * with the keyboard lock keys, so that they do not cancel out the event.
243
244 * if allow_scroll_lock is true then only the top half of the lock mask
245 * table is used and scroll lock is ignored. This value defaults to false.
246 */
247 void Display::grabButton(unsigned int button, unsigned int modifiers,
248 Window grab_window, bool owner_events,
249 unsigned int event_mask, int pointer_mode,
250 int keyboard_mode, Window confine_to,
251 Cursor cursor, bool allow_scroll_lock) const
252 {
253 unsigned int length = (allow_scroll_lock) ? 8 / 2:
254 8;
255 for (size_t cnt = 0; cnt < length; ++cnt)
256 XGrabButton(_display, button, modifiers | _mask_list[cnt],
257 grab_window, owner_events, event_mask, pointer_mode,
258 keyboard_mode, confine_to, cursor);
259 }
260
261
262 /*
263 * Releases the grab on a button, and ungrabs all possible combinations of the
264 * keyboard lock keys.
265 */
266 void Display::ungrabButton(unsigned int button, unsigned int modifiers,
267 Window grab_window) const
268 {
269 for (size_t cnt = 0; cnt < 8; ++cnt)
270 XUngrabButton(_display, button, modifiers | _mask_list[cnt],
271 grab_window);
272 }
273
274 void Display::grabKey(unsigned int keycode, unsigned int modifiers,
275 Window grab_window, bool owner_events,
276 int pointer_mode, int keyboard_mode,
277 bool allow_scroll_lock) const
278 {
279 unsigned int length = (allow_scroll_lock) ? 8 / 2:
280 8;
281 for (size_t cnt = 0; cnt < length; ++cnt)
282 XGrabKey(_display, keycode, modifiers | _mask_list[cnt],
283 grab_window, owner_events, pointer_mode, keyboard_mode);
284 }
285
286 void Display::ungrabKey(unsigned int keycode, unsigned int modifiers,
287 Window grab_window) const
288 {
289 for (size_t cnt = 0; cnt < 8; ++cnt)
290 XUngrabKey(_display, keycode, modifiers | _mask_list[cnt],
291 grab_window);
292 }
293
294 }
This page took 0.045582 seconds and 5 git commands to generate.