]> Dogcows Code - chaz/openbox/blob - otk/display.cc
new python callbacks data, infrastructure. going to rework bindings code. cvs wont...
[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
11 extern "C" {
12 #include <X11/keysym.h>
13
14 #ifdef XKB
15 #include <X11/XKBlib.h>
16 #endif // XKB
17
18 #ifdef SHAPE
19 #include <X11/extensions/shape.h>
20 #endif // SHAPE
21
22 #ifdef HAVE_STDIO_H
23 # include <stdio.h>
24 #endif // HAVE_STDIO_H
25
26 #ifdef HAVE_STDLIB_H
27 # include <stdlib.h>
28 #endif // HAVE_STDLIB_H
29
30 #ifdef HAVE_SIGNAL_H
31 # include <signal.h>
32 #endif // HAVE_SIGNAL_H
33
34 #ifdef HAVE_FCNTL_H
35 # include <fcntl.h>
36 #endif // HAVE_FCNTL_H
37
38 #ifdef HAVE_UNISTD_H
39 # include <sys/types.h>
40 # include <unistd.h>
41 #endif // HAVE_UNISTD_H
42
43 #include "gettext.h"
44 #define _(str) gettext(str)
45 }
46
47 namespace otk {
48
49
50 Display *OBDisplay::display = (Display*) 0;
51 bool OBDisplay::_xkb = false;
52 int OBDisplay::_xkb_event_basep = 0;
53 bool OBDisplay::_shape = false;
54 int OBDisplay::_shape_event_basep = 0;
55 bool OBDisplay::_xinerama = false;
56 int OBDisplay::_xinerama_event_basep = 0;
57 unsigned int OBDisplay::_mask_list[8];
58 unsigned int OBDisplay::_scrollLockMask = 0;
59 unsigned int OBDisplay::_numLockMask = 0;
60 OBDisplay::ScreenInfoList OBDisplay::_screenInfoList;
61 BGCCache *OBDisplay::_gccache = (BGCCache*) 0;
62 int OBDisplay::_grab_count = 0;
63
64
65 int OBDisplay::xerrorHandler(Display *d, XErrorEvent *e)
66 {
67 #ifdef DEBUG
68 char errtxt[128];
69
70 //if (e->error_code != BadWindow)
71 {
72 XGetErrorText(d, e->error_code, errtxt, 128);
73 printf("X Error: %s\n", errtxt);
74 if (e->error_code != BadWindow)
75 abort();
76 }
77 #else
78 (void)d;
79 (void)e;
80 #endif
81
82 return false;
83 }
84
85
86 void OBDisplay::initialize(char *name)
87 {
88 int junk;
89 (void)junk;
90
91 // Open the X display
92 if (!(display = XOpenDisplay(name))) {
93 printf(_("Unable to open connection to the X server. Please set the \n\
94 DISPLAY environment variable approriately, or use the '-display' command \n\
95 line argument.\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
103 // set our error handler for X errors
104 XSetErrorHandler(xerrorHandler);
105
106 // set the DISPLAY environment variable for any lauched children, to the
107 // display we're using, so they open in the right place.
108 // XXX rm -> std::string dtmp = "DISPLAY=" + DisplayString(display);
109 if (putenv(const_cast<char*>((std::string("DISPLAY=") +
110 DisplayString(display)).c_str()))) {
111 printf(_("warning: couldn't set environment variable 'DISPLAY'\n"));
112 perror("putenv()");
113 }
114
115 // find the availability of X extensions we like to use
116 #ifdef XKB
117 _xkb = XkbQueryExtension(display, &junk, &_xkb_event_basep, &junk, NULL,
118 NULL);
119 #endif
120
121 #ifdef SHAPE
122 _shape = XShapeQueryExtension(display, &_shape_event_basep, &junk);
123 #endif
124
125 #ifdef XINERAMA
126 _xinerama = XineramaQueryExtension(display, &_xinerama_event_basep, &junk);
127 #endif // XINERAMA
128
129 // get lock masks that are defined by the display (not constant)
130 XModifierKeymap *modmap;
131
132 modmap = XGetModifierMapping(display);
133 if (modmap && modmap->max_keypermod > 0) {
134 const int mask_table[] = {
135 ShiftMask, LockMask, ControlMask, Mod1Mask,
136 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
137 };
138 const size_t size = (sizeof(mask_table) / sizeof(mask_table[0])) *
139 modmap->max_keypermod;
140 // get the values of the keyboard lock modifiers
141 // Note: Caps lock is not retrieved the same way as Scroll and Num lock
142 // since it doesn't need to be.
143 const KeyCode num_lock = XKeysymToKeycode(display, XK_Num_Lock);
144 const KeyCode scroll_lock = XKeysymToKeycode(display, XK_Scroll_Lock);
145
146 for (size_t cnt = 0; cnt < size; ++cnt) {
147 if (! modmap->modifiermap[cnt]) continue;
148
149 if (num_lock == modmap->modifiermap[cnt])
150 _numLockMask = mask_table[cnt / modmap->max_keypermod];
151 if (scroll_lock == modmap->modifiermap[cnt])
152 _scrollLockMask = mask_table[cnt / modmap->max_keypermod];
153 }
154 }
155
156 if (modmap) XFreeModifiermap(modmap);
157
158 _mask_list[0] = 0;
159 _mask_list[1] = LockMask;
160 _mask_list[2] = _numLockMask;
161 _mask_list[3] = LockMask | _numLockMask;
162 _mask_list[4] = _scrollLockMask;
163 _mask_list[5] = _scrollLockMask | LockMask;
164 _mask_list[6] = _scrollLockMask | _numLockMask;
165 _mask_list[7] = _scrollLockMask | LockMask | _numLockMask;
166
167 // Get information on all the screens which are available.
168 _screenInfoList.reserve(ScreenCount(display));
169 for (int i = 0; i < ScreenCount(display); ++i)
170 _screenInfoList.push_back(ScreenInfo(i));
171
172 _gccache = new BGCCache(_screenInfoList.size());
173 }
174
175
176 void OBDisplay::destroy()
177 {
178 delete _gccache;
179 while (_grab_count > 0)
180 ungrab();
181 XCloseDisplay(display);
182 }
183
184
185 const ScreenInfo* OBDisplay::screenInfo(int snum) {
186 assert(snum >= 0);
187 assert(snum < static_cast<int>(_screenInfoList.size()));
188 return &_screenInfoList[snum];
189 }
190
191
192 const ScreenInfo* OBDisplay::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 OBDisplay::grab()
203 {
204 if (_grab_count == 0)
205 XGrabServer(display);
206 _grab_count++;
207 }
208
209
210 void OBDisplay::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 OBDisplay::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) {
236 unsigned int length = (allow_scroll_lock) ? 8 / 2:
237 8;
238 for (size_t cnt = 0; cnt < length; ++cnt)
239 XGrabButton(otk::OBDisplay::display, button, modifiers | _mask_list[cnt],
240 grab_window, owner_events, event_mask, pointer_mode,
241 keyboard_mode, confine_to, cursor);
242 }
243
244
245 /*
246 * Releases the grab on a button, and ungrabs all possible combinations of the
247 * keyboard lock keys.
248 */
249 void OBDisplay::ungrabButton(unsigned int button, unsigned int modifiers,
250 Window grab_window) {
251 for (size_t cnt = 0; cnt < 8; ++cnt)
252 XUngrabButton(otk::OBDisplay::display, button, modifiers | _mask_list[cnt],
253 grab_window);
254 }
255
256 void OBDisplay::grabKey(unsigned int keycode, unsigned int modifiers,
257 Window grab_window, bool owner_events,
258 int pointer_mode, int keyboard_mode,
259 bool allow_scroll_lock)
260 {
261 unsigned int length = (allow_scroll_lock) ? 8 / 2:
262 8;
263 for (size_t cnt = 0; cnt < length; ++cnt)
264 XGrabKey(otk::OBDisplay::display, keycode, modifiers | _mask_list[cnt],
265 grab_window, owner_events, pointer_mode, keyboard_mode);
266 }
267
268 void OBDisplay::ungrabKey(unsigned int keycode, unsigned int modifiers,
269 Window grab_window)
270 {
271 for (size_t cnt = 0; cnt < 8; ++cnt)
272 XUngrabKey(otk::OBDisplay::display, keycode, modifiers | _mask_list[cnt],
273 grab_window);
274 }
275
276 }
This page took 0.050007 seconds and 4 git commands to generate.