]> Dogcows Code - chaz/openbox/blob - otk/display.cc
remove the block on shutdown
[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 SHAPE
15 #include <X11/extensions/shape.h>
16 #endif // SHAPE
17
18 #ifdef HAVE_STDIO_H
19 # include <stdio.h>
20 #endif // HAVE_STDIO_H
21
22 #ifdef HAVE_STDLIB_H
23 # include <stdlib.h>
24 #endif // HAVE_STDLIB_H
25
26 #ifdef HAVE_SIGNAL_H
27 # include <signal.h>
28 #endif // HAVE_SIGNAL_H
29
30 #ifdef HAVE_FCNTL_H
31 # include <fcntl.h>
32 #endif // HAVE_FCNTL_H
33
34 #ifdef HAVE_UNISTD_H
35 # include <sys/types.h>
36 # include <unistd.h>
37 #endif // HAVE_UNISTD_H
38
39 #include "gettext.h"
40 #define _(str) gettext(str)
41 }
42
43 namespace otk {
44
45
46 Display *OBDisplay::display = (Display*) 0;
47 bool OBDisplay::_shape = false;
48 int OBDisplay::_shape_event_basep = 0;
49 bool OBDisplay::_xinerama = false;
50 int OBDisplay::_xinerama_event_basep = 0;
51 unsigned int OBDisplay::_mask_list[8];
52 OBDisplay::ScreenInfoList OBDisplay::_screenInfoList;
53 BGCCache *OBDisplay::_gccache = (BGCCache*) 0;
54 int OBDisplay::_grab_count = 0;
55
56
57 int OBDisplay::xerrorHandler(Display *d, XErrorEvent *e)
58 {
59 #ifdef DEBUG
60 char errtxt[128];
61
62 //if (e->error_code != BadWindow)
63 {
64 XGetErrorText(d, e->error_code, errtxt, 128);
65 printf("X Error: %s\n", errtxt);
66 }
67 #else
68 (void)d;
69 (void)e;
70 #endif
71
72 return false;
73 }
74
75
76 void OBDisplay::initialize(char *name)
77 {
78 int junk;
79 (void)junk;
80
81 // Open the X display
82 if (!(display = XOpenDisplay(name))) {
83 printf(_("Unable to open connection to the X server. Please set the \n\
84 DISPLAY environment variable approriately, or use the '-display' command \n\
85 line argument.\n\n"));
86 ::exit(1);
87 }
88 if (fcntl(ConnectionNumber(display), F_SETFD, 1) == -1) {
89 printf(_("Couldn't mark display connection as close-on-exec.\n\n"));
90 ::exit(1);
91 }
92
93 // set our error handler for X errors
94 XSetErrorHandler(xerrorHandler);
95
96 // set the DISPLAY environment variable for any lauched children, to the
97 // display we're using, so they open in the right place.
98 // XXX rm -> std::string dtmp = "DISPLAY=" + DisplayString(display);
99 if (putenv(const_cast<char*>((std::string("DISPLAY=") +
100 DisplayString(display)).c_str()))) {
101 printf(_("warning: couldn't set environment variable 'DISPLAY'\n"));
102 perror("putenv()");
103 }
104
105 // find the availability of X extensions we like to use
106 #ifdef SHAPE
107 _shape = XShapeQueryExtension(display, &_shape_event_basep, &junk);
108 #endif
109
110 #ifdef XINERAMA
111 _xinerama = XineramaQueryExtension(display, &_xinerama_event_basep, &junk);
112 #endif // XINERAMA
113
114 // get lock masks that are defined by the display (not constant)
115 XModifierKeymap *modmap;
116 unsigned int NumLockMask = 0, ScrollLockMask = 0;
117
118 modmap = XGetModifierMapping(display);
119 if (modmap && modmap->max_keypermod > 0) {
120 const int mask_table[] = {
121 ShiftMask, LockMask, ControlMask, Mod1Mask,
122 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
123 };
124 const size_t size = (sizeof(mask_table) / sizeof(mask_table[0])) *
125 modmap->max_keypermod;
126 // get the values of the keyboard lock modifiers
127 // Note: Caps lock is not retrieved the same way as Scroll and Num lock
128 // since it doesn't need to be.
129 const KeyCode num_lock = XKeysymToKeycode(display, XK_Num_Lock);
130 const KeyCode scroll_lock = XKeysymToKeycode(display, XK_Scroll_Lock);
131
132 for (size_t cnt = 0; cnt < size; ++cnt) {
133 if (! modmap->modifiermap[cnt]) continue;
134
135 if (num_lock == modmap->modifiermap[cnt])
136 NumLockMask = mask_table[cnt / modmap->max_keypermod];
137 if (scroll_lock == modmap->modifiermap[cnt])
138 ScrollLockMask = mask_table[cnt / modmap->max_keypermod];
139 }
140 }
141
142 if (modmap) XFreeModifiermap(modmap);
143
144 _mask_list[0] = 0;
145 _mask_list[1] = LockMask;
146 _mask_list[2] = NumLockMask;
147 _mask_list[3] = LockMask | NumLockMask;
148 _mask_list[4] = ScrollLockMask;
149 _mask_list[5] = ScrollLockMask | LockMask;
150 _mask_list[6] = ScrollLockMask | NumLockMask;
151 _mask_list[7] = ScrollLockMask | LockMask | NumLockMask;
152
153 // Get information on all the screens which are available.
154 _screenInfoList.reserve(ScreenCount(display));
155 for (int i = 0; i < ScreenCount(display); ++i)
156 _screenInfoList.push_back(ScreenInfo(i));
157
158 _gccache = new BGCCache(_screenInfoList.size());
159 }
160
161
162 void OBDisplay::destroy()
163 {
164 delete _gccache;
165 while (_grab_count > 0)
166 ungrab();
167 XCloseDisplay(display);
168 }
169
170
171 const ScreenInfo* OBDisplay::screenInfo(int snum) {
172 assert(snum >= 0);
173 assert(snum < static_cast<int>(_screenInfoList.size()));
174 return &_screenInfoList[snum];
175 }
176
177
178 void OBDisplay::grab()
179 {
180 if (_grab_count == 0)
181 XGrabServer(display);
182 _grab_count++;
183 }
184
185
186 void OBDisplay::ungrab()
187 {
188 if (_grab_count == 0) return;
189 _grab_count--;
190 if (_grab_count == 0)
191 XUngrabServer(display);
192 }
193
194
195
196
197
198
199
200 /*
201 * Grabs a button, but also grabs the button in every possible combination
202 * with the keyboard lock keys, so that they do not cancel out the event.
203
204 * if allow_scroll_lock is true then only the top half of the lock mask
205 * table is used and scroll lock is ignored. This value defaults to false.
206 */
207 void OBDisplay::grabButton(unsigned int button, unsigned int modifiers,
208 Window grab_window, bool owner_events,
209 unsigned int event_mask, int pointer_mode,
210 int keyboard_mode, Window confine_to,
211 Cursor cursor, bool allow_scroll_lock) {
212 unsigned int length = (allow_scroll_lock) ? 8 / 2:
213 8;
214 for (size_t cnt = 0; cnt < length; ++cnt)
215 XGrabButton(otk::OBDisplay::display, button, modifiers | _mask_list[cnt],
216 grab_window, owner_events, event_mask, pointer_mode,
217 keyboard_mode, confine_to, cursor);
218 }
219
220
221 /*
222 * Releases the grab on a button, and ungrabs all possible combinations of the
223 * keyboard lock keys.
224 */
225 void OBDisplay::ungrabButton(unsigned int button, unsigned int modifiers,
226 Window grab_window) {
227 for (size_t cnt = 0; cnt < 8; ++cnt)
228 XUngrabButton(otk::OBDisplay::display, button, modifiers | _mask_list[cnt],
229 grab_window);
230 }
231
232 void OBDisplay::grabKey(unsigned int keycode, unsigned int modifiers,
233 Window grab_window, bool owner_events,
234 int pointer_mode, int keyboard_mode,
235 bool allow_scroll_lock)
236 {
237 unsigned int length = (allow_scroll_lock) ? 8 / 2:
238 8;
239 for (size_t cnt = 0; cnt < length; ++cnt)
240 XGrabKey(otk::OBDisplay::display, keycode, modifiers | _mask_list[cnt],
241 grab_window, owner_events, pointer_mode, keyboard_mode);
242 }
243
244 void OBDisplay::ungrabKey(unsigned int keycode, unsigned int modifiers,
245 Window grab_window)
246 {
247 for (size_t cnt = 0; cnt < 8; ++cnt)
248 XUngrabKey(otk::OBDisplay::display, keycode, modifiers | _mask_list[cnt],
249 grab_window);
250 }
251
252 }
This page took 0.050373 seconds and 5 git commands to generate.