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