]> icculus.org git repositories - mikachu/openbox.git/blob - otk/display.cc
Added bevels
[mikachu/openbox.git] / 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     _screeninfo_list(0),
88     _rendercontrol_list(0),
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, and create their
171   // RenderControl
172   _screeninfo_list = new ScreenInfo*[ScreenCount(_display)];
173   _rendercontrol_list = new RenderControl*[ScreenCount(_display)];
174   for (int i = 0; i < ScreenCount(_display); ++i) {
175     _screeninfo_list[i] = new ScreenInfo(i);
176     _rendercontrol_list[i] = RenderControl::getRenderControl(i);
177   }
178
179   _gccache = new GCCache(ScreenCount(_display));
180 }
181
182
183 Display::~Display()
184 {
185   delete _gccache;
186   while (_grab_count > 0)
187     ungrab();
188
189   for (int i = 0; i < ScreenCount(_display); ++i) {
190     delete _rendercontrol_list[i];
191     delete _screeninfo_list[i];
192   }
193   delete [] _rendercontrol_list;
194   delete [] _screeninfo_list;
195   
196   XCloseDisplay(_display);
197 }
198
199
200 const ScreenInfo* Display::screenInfo(int snum)
201 {
202   assert(snum >= 0);
203   assert(snum < (signed) ScreenCount(_display));
204   return _screeninfo_list[snum];
205 }
206
207
208 const ScreenInfo* Display::findScreen(Window root)
209 {
210   for (int i = 0; i < ScreenCount(_display); ++i)
211     if (_screeninfo_list[i]->rootWindow() == root)
212       return _screeninfo_list[i];
213   return 0;
214 }
215
216
217 const RenderControl *Display::renderControl(int snum)
218 {
219   assert(snum >= 0);
220   assert(snum < (signed) ScreenCount(_display));
221   return _rendercontrol_list[snum];
222 }
223
224
225 void Display::grab()
226 {
227   if (_grab_count == 0)
228     XGrabServer(_display);
229   _grab_count++;
230 }
231
232
233 void Display::ungrab()
234 {
235   if (_grab_count == 0) return;
236   _grab_count--;
237   if (_grab_count == 0)
238     XUngrabServer(_display);
239 }
240
241
242
243
244
245
246
247 /*
248  * Grabs a button, but also grabs the button in every possible combination
249  * with the keyboard lock keys, so that they do not cancel out the event.
250
251  * if allow_scroll_lock is true then only the top half of the lock mask
252  * table is used and scroll lock is ignored.  This value defaults to false.
253  */
254 void Display::grabButton(unsigned int button, unsigned int modifiers,
255                          Window grab_window, bool owner_events,
256                          unsigned int event_mask, int pointer_mode,
257                          int keyboard_mode, Window confine_to,
258                          Cursor cursor, bool allow_scroll_lock) const
259 {
260   unsigned int length = (allow_scroll_lock) ? 8 / 2:
261                                               8;
262   for (size_t cnt = 0; cnt < length; ++cnt)
263     XGrabButton(_display, button, modifiers | _mask_list[cnt],
264                 grab_window, owner_events, event_mask, pointer_mode,
265                 keyboard_mode, confine_to, cursor);
266 }
267
268
269 /*
270  * Releases the grab on a button, and ungrabs all possible combinations of the
271  * keyboard lock keys.
272  */
273 void Display::ungrabButton(unsigned int button, unsigned int modifiers,
274                            Window grab_window) const
275 {
276   for (size_t cnt = 0; cnt < 8; ++cnt)
277     XUngrabButton(_display, button, modifiers | _mask_list[cnt],
278                   grab_window);
279 }
280
281 void Display::grabKey(unsigned int keycode, unsigned int modifiers,
282                         Window grab_window, bool owner_events,
283                         int pointer_mode, int keyboard_mode,
284                         bool allow_scroll_lock) const
285 {
286   unsigned int length = (allow_scroll_lock) ? 8 / 2:
287                                               8;
288   for (size_t cnt = 0; cnt < length; ++cnt)
289     XGrabKey(_display, keycode, modifiers | _mask_list[cnt],
290                 grab_window, owner_events, pointer_mode, keyboard_mode);
291 }
292
293 void Display::ungrabKey(unsigned int keycode, unsigned int modifiers,
294                           Window grab_window) const
295 {
296   for (size_t cnt = 0; cnt < 8; ++cnt)
297     XUngrabKey(_display, keycode, modifiers | _mask_list[cnt],
298                grab_window);
299 }
300
301 }