]>
Dogcows Code - chaz/openbox/blob - otk/display.cc
8a7646b832a95fd765e3521f12e3f2ed2a9a9c6c
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
8 #include "screeninfo.hh"
10 #include "rendercontrol.hh"
14 #include <X11/keysym.h>
17 #include <X11/XKBlib.h>
21 #include <X11/extensions/shape.h>
25 #include <X11/extensions/Xinerama.h>
30 #endif // HAVE_STDIO_H
34 #endif // HAVE_SIGNAL_H
38 #endif // HAVE_FCNTL_H
41 # include <sys/types.h>
43 #endif // HAVE_UNISTD_H
46 #define _(str) gettext(str)
52 Display
*display
= (Display
*) 0;
54 static int xerrorHandler(::Display
*d
, XErrorEvent
*e
)
59 //if (e->error_code != BadWindow)
61 XGetErrorText(d
, e
->error_code
, errtxt
, 128);
62 printf("X Error: %s\n", errtxt
);
63 if (e
->error_code
!= BadWindow
)
80 _shape_event_basep(0),
82 _xinerama_event_basep(0),
89 _gccache((GCCache
*) 0)
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"));
102 if (fcntl(ConnectionNumber(_display
), F_SETFD
, 1) == -1) {
103 printf(_("Couldn't mark display connection as close-on-exec.\n\n"));
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"));
111 // set our error handler for X errors
112 XSetErrorHandler(xerrorHandler
);
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
));
118 // find the availability of X extensions we like to use
120 _xkb
= XkbQueryExtension(_display
, &junk
, &_xkb_event_basep
, &junk
, NULL
,
125 _shape
= XShapeQueryExtension(_display
, &_shape_event_basep
, &junk
);
129 _xinerama
= XineramaQueryExtension(_display
, &_xinerama_event_basep
, &junk
);
132 // get lock masks that are defined by the display (not constant)
133 XModifierKeymap
*modmap
;
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
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
);
149 for (size_t cnt
= 0; cnt
< size
; ++cnt
) {
150 if (! modmap
->modifiermap
[cnt
]) continue;
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
];
159 if (modmap
) XFreeModifiermap(modmap
);
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
;
170 // Get information on all the screens which are available.
171 _screenInfoList
.reserve(ScreenCount(_display
));
172 for (int i
= 0; i
< ScreenCount(_display
); ++i
)
173 _screenInfoList
.push_back(i
);
175 _renderControlList
.reserve(ScreenCount(_display
));
176 for (int i
= 0; i
< ScreenCount(_display
); ++i
)
177 _renderControlList
.push_back(RenderControl::getRenderControl(i
));
179 _gccache
= new GCCache(_screenInfoList
.size());
186 while (_grab_count
> 0)
188 XCloseDisplay(_display
);
192 const ScreenInfo
* Display::screenInfo(int snum
)
195 assert(snum
< static_cast<int>(_screenInfoList
.size()));
196 return &_screenInfoList
[snum
];
200 const ScreenInfo
* Display::findScreen(Window root
)
202 std::vector
<ScreenInfo
>::iterator it
, end
= _screenInfoList
.end();
203 for (it
= _screenInfoList
.begin(); it
!= end
; ++it
)
204 if (it
->rootWindow() == root
)
210 const RenderControl
*Display::renderControl(int snum
)
213 assert(snum
< (signed) _renderControlList
.size());
214 return _renderControlList
[snum
];
220 if (_grab_count
== 0)
221 XGrabServer(_display
);
226 void Display::ungrab()
228 if (_grab_count
== 0) return;
230 if (_grab_count
== 0)
231 XUngrabServer(_display
);
241 * Grabs a button, but also grabs the button in every possible combination
242 * with the keyboard lock keys, so that they do not cancel out the event.
244 * if allow_scroll_lock is true then only the top half of the lock mask
245 * table is used and scroll lock is ignored. This value defaults to false.
247 void Display::grabButton(unsigned int button
, unsigned int modifiers
,
248 Window grab_window
, bool owner_events
,
249 unsigned int event_mask
, int pointer_mode
,
250 int keyboard_mode
, Window confine_to
,
251 Cursor cursor
, bool allow_scroll_lock
) const
253 unsigned int length
= (allow_scroll_lock
) ? 8 / 2:
255 for (size_t cnt
= 0; cnt
< length
; ++cnt
)
256 XGrabButton(_display
, button
, modifiers
| _mask_list
[cnt
],
257 grab_window
, owner_events
, event_mask
, pointer_mode
,
258 keyboard_mode
, confine_to
, cursor
);
263 * Releases the grab on a button, and ungrabs all possible combinations of the
264 * keyboard lock keys.
266 void Display::ungrabButton(unsigned int button
, unsigned int modifiers
,
267 Window grab_window
) const
269 for (size_t cnt
= 0; cnt
< 8; ++cnt
)
270 XUngrabButton(_display
, button
, modifiers
| _mask_list
[cnt
],
274 void Display::grabKey(unsigned int keycode
, unsigned int modifiers
,
275 Window grab_window
, bool owner_events
,
276 int pointer_mode
, int keyboard_mode
,
277 bool allow_scroll_lock
) const
279 unsigned int length
= (allow_scroll_lock
) ? 8 / 2:
281 for (size_t cnt
= 0; cnt
< length
; ++cnt
)
282 XGrabKey(_display
, keycode
, modifiers
| _mask_list
[cnt
],
283 grab_window
, owner_events
, pointer_mode
, keyboard_mode
);
286 void Display::ungrabKey(unsigned int keycode
, unsigned int modifiers
,
287 Window grab_window
) const
289 for (size_t cnt
= 0; cnt
< 8; ++cnt
)
290 XUngrabKey(_display
, keycode
, modifiers
| _mask_list
[cnt
],
This page took 0.045501 seconds and 4 git commands to generate.