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