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