]> Dogcows Code - chaz/openbox/blob - otk/display.cc
un-static otk::Display. add an operator* to it. make a global ob::display var.
[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 "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 "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 #ifdef DEBUG
56 char errtxt[128];
57
58 //if (e->error_code != BadWindow)
59 {
60 XGetErrorText(d, e->error_code, errtxt, 128);
61 printf("X Error: %s\n", errtxt);
62 if (e->error_code != BadWindow)
63 abort();
64 }
65 #else
66 (void)d;
67 (void)e;
68 #endif
69
70 return false;
71 }
72
73
74 Display::Display()
75 : _display(0),
76 _xkb(false),
77 _xkb_event_basep(0),
78 _shape(false),
79 _shape_event_basep(0),
80 _xinerama(false),
81 _xinerama_event_basep(0),
82 _mask_list(),
83 _num_lock_mask(0),
84 _scroll_lock_mask(0),
85 _grab_count(0),
86 _screenInfoList(),
87 _gccache((GCCache*) 0)
88 {
89 int junk;
90 (void)junk;
91
92 // Open the X display
93 if (!(_display = XOpenDisplay(NULL))) {
94 printf(_("Unable to open connection to the X server. Please set the \n\
95 DISPLAY environment variable approriately.\n\n"));
96 ::exit(1);
97 }
98 if (fcntl(ConnectionNumber(_display), F_SETFD, 1) == -1) {
99 printf(_("Couldn't mark display connection as close-on-exec.\n\n"));
100 ::exit(1);
101 }
102 if (!XSupportsLocale())
103 printf(_("X server does not support locale.\n"));
104 if (!XSetLocaleModifiers(""))
105 printf(_("Cannot set locale modifiers for the X server.\n"));
106
107 // set our error handler for X errors
108 XSetErrorHandler(xerrorHandler);
109
110 // set the DISPLAY environment variable for any lauched children, to the
111 // display we're using, so they open in the right place.
112 putenv(std::string("DISPLAY=") + DisplayString(_display));
113
114 // find the availability of X extensions we like to use
115 #ifdef XKB
116 _xkb = XkbQueryExtension(_display, &junk, &_xkb_event_basep, &junk, NULL,
117 NULL);
118 #endif
119
120 #ifdef SHAPE
121 _shape = XShapeQueryExtension(_display, &_shape_event_basep, &junk);
122 #endif
123
124 #ifdef XINERAMA
125 _xinerama = XineramaQueryExtension(_display, &_xinerama_event_basep, &junk);
126 #endif // XINERAMA
127
128 // get lock masks that are defined by the display (not constant)
129 XModifierKeymap *modmap;
130
131 modmap = XGetModifierMapping(_display);
132 if (modmap && modmap->max_keypermod > 0) {
133 const int mask_table[] = {
134 ShiftMask, LockMask, ControlMask, Mod1Mask,
135 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
136 };
137 const size_t size = (sizeof(mask_table) / sizeof(mask_table[0])) *
138 modmap->max_keypermod;
139 // get the values of the keyboard lock modifiers
140 // Note: Caps lock is not retrieved the same way as Scroll and Num lock
141 // since it doesn't need to be.
142 const KeyCode num_lock = XKeysymToKeycode(_display, XK_Num_Lock);
143 const KeyCode scroll_lock = XKeysymToKeycode(_display, XK_Scroll_Lock);
144
145 for (size_t cnt = 0; cnt < size; ++cnt) {
146 if (! modmap->modifiermap[cnt]) continue;
147
148 if (num_lock == modmap->modifiermap[cnt])
149 _num_lock_mask = mask_table[cnt / modmap->max_keypermod];
150 if (scroll_lock == modmap->modifiermap[cnt])
151 _scroll_lock_mask = mask_table[cnt / modmap->max_keypermod];
152 }
153 }
154
155 if (modmap) XFreeModifiermap(modmap);
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.
167 _screenInfoList.reserve(ScreenCount(_display));
168 for (int i = 0; i < ScreenCount(_display); ++i)
169 _screenInfoList.push_back(ScreenInfo(i));
170
171 _gccache = new GCCache(_screenInfoList.size());
172 }
173
174
175 Display::~Display()
176 {
177 delete _gccache;
178 while (_grab_count > 0)
179 ungrab();
180 XCloseDisplay(_display);
181 }
182
183
184 const ScreenInfo* Display::screenInfo(int snum)
185 {
186 assert(snum >= 0);
187 assert(snum < static_cast<int>(_screenInfoList.size()));
188 return &_screenInfoList[snum];
189 }
190
191
192 const ScreenInfo* Display::findScreen(Window root)
193 {
194 ScreenInfoList::iterator it, end = _screenInfoList.end();
195 for (it = _screenInfoList.begin(); it != end; ++it)
196 if (it->rootWindow() == root)
197 return &(*it);
198 return 0;
199 }
200
201
202 void Display::grab()
203 {
204 if (_grab_count == 0)
205 XGrabServer(_display);
206 _grab_count++;
207 }
208
209
210 void Display::ungrab()
211 {
212 if (_grab_count == 0) return;
213 _grab_count--;
214 if (_grab_count == 0)
215 XUngrabServer(_display);
216 }
217
218
219
220
221
222
223
224 /*
225 * Grabs a button, but also grabs the button in every possible combination
226 * with the keyboard lock keys, so that they do not cancel out the event.
227
228 * if allow_scroll_lock is true then only the top half of the lock mask
229 * table is used and scroll lock is ignored. This value defaults to false.
230 */
231 void Display::grabButton(unsigned int button, unsigned int modifiers,
232 Window grab_window, bool owner_events,
233 unsigned int event_mask, int pointer_mode,
234 int keyboard_mode, Window confine_to,
235 Cursor cursor, bool allow_scroll_lock) const
236 {
237 unsigned int length = (allow_scroll_lock) ? 8 / 2:
238 8;
239 for (size_t cnt = 0; cnt < length; ++cnt)
240 XGrabButton(_display, button, modifiers | _mask_list[cnt],
241 grab_window, owner_events, event_mask, pointer_mode,
242 keyboard_mode, confine_to, cursor);
243 }
244
245
246 /*
247 * Releases the grab on a button, and ungrabs all possible combinations of the
248 * keyboard lock keys.
249 */
250 void Display::ungrabButton(unsigned int button, unsigned int modifiers,
251 Window grab_window) const
252 {
253 for (size_t cnt = 0; cnt < 8; ++cnt)
254 XUngrabButton(_display, button, modifiers | _mask_list[cnt],
255 grab_window);
256 }
257
258 void Display::grabKey(unsigned int keycode, unsigned int modifiers,
259 Window grab_window, bool owner_events,
260 int pointer_mode, int keyboard_mode,
261 bool allow_scroll_lock) const
262 {
263 unsigned int length = (allow_scroll_lock) ? 8 / 2:
264 8;
265 for (size_t cnt = 0; cnt < length; ++cnt)
266 XGrabKey(_display, keycode, modifiers | _mask_list[cnt],
267 grab_window, owner_events, pointer_mode, keyboard_mode);
268 }
269
270 void Display::ungrabKey(unsigned int keycode, unsigned int modifiers,
271 Window grab_window) const
272 {
273 for (size_t cnt = 0; cnt < 8; ++cnt)
274 XUngrabKey(_display, keycode, modifiers | _mask_list[cnt],
275 grab_window);
276 }
277
278 }
This page took 0.044878 seconds and 5 git commands to generate.