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