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