]>
Dogcows Code - chaz/openbox/blob - src/XDisplay.cc
1 // XDisplay.cc for Openbox
2 // Copyright (c) 2002 - 2002 Ben Janens (ben at orodu.net)
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
23 # include "../config.h"
35 # include <X11/extensions/shape.h>
46 std::string
XDisplay::_app_name
;
47 Window
XDisplay::_last_bad_window
= None
;
50 * X error handler to handle all X errors while the application is
53 int XDisplay::errorHandler(Display
*d
, XErrorEvent
*e
) {
56 XGetErrorText(d
, e
->error_code
, errtxt
, sizeof(errtxt
)/sizeof(char));
57 cerr
<< _app_name
.c_str() << ": X error: " <<
58 errtxt
<< "(" << e
->error_code
<< ") opcodes " <<
59 e
->request_code
<< "/" << e
->minor_code
<< endl
;
60 cerr
.flags(std::ios_base::hex
);
61 cerr
<< " resource 0x" << e
->resourceid
<< endl
;
62 cerr
.flags(std::ios_base::dec
);
65 if (e
->error_code
== BadWindow
)
66 _last_bad_window
= e
->resourceid
;
72 XDisplay::XDisplay(const std::string
&application_name
, const char *dpyname
) {
73 _app_name
= application_name
;
77 _display
= XOpenDisplay(dpyname
);
78 if (_display
== NULL
) {
79 cerr
<< "Could not open display. Connection to X server failed.\n";
82 if (-1 == fcntl(ConnectionNumber(_display
), F_SETFD
, 1)) {
83 cerr
<< "Could not mark display connection as close-on-exec.\n";
86 _name
= XDisplayName(dpyname
);
88 XSetErrorHandler(errorHandler
);
92 _hasshape
= XShapeQueryExtension(_display
, &_shape_event_base
, &waste
);
101 XDisplay::~XDisplay() {
102 std::for_each(_screens
.begin(), _screens
.end(), PointerAssassin());
103 XCloseDisplay(_display
);
110 void XDisplay::grab() {
112 XGrabServer(_display
);
117 * Release the X server from a grab
119 void XDisplay::ungrab() {
121 XUngrabServer(_display
);
126 * Gets the next event on the queue from the X server.
128 * Returns: true if e contains a new event; false if there is no event to be
131 bool XDisplay::nextEvent(XEvent
&e
) {
132 if(!XPending(_display
))
134 XNextEvent(_display
, &e
);
135 if (_last_bad_window
!= None
) {
136 if (e
.xany
.window
== _last_bad_window
) {
137 cerr
<< "XDisplay::nextEvent(): Removing event for bad window from " <<
141 _last_bad_window
= None
;
147 int XDisplay::connectionNumber() const {
148 return ConnectionNumber(_display
);
153 * Creates a font cursor in the X server and returns it.
155 Cursor
createCursor(unsigned int shape
) const {
156 return XCreateFontCursor(_display
, shape
);
161 void XDisplay::getLockModifers() {
162 NumLockMask
= ScrollLockMask
= 0;
164 const XModifierKeymap
* const modmap
= XGetModifierMapping(display
);
165 if (modmap
&& modmap
->max_keypermod
> 0) {
166 const int mask_table
[] = {
167 ShiftMask
, LockMask
, ControlMask
, Mod1Mask
,
168 Mod2Mask
, Mod3Mask
, Mod4Mask
, Mod5Mask
170 const size_t size
= (sizeof(mask_table
) / sizeof(mask_table
[0])) *
171 modmap
->max_keypermod
;
172 // get the values of the keyboard lock modifiers
173 // Note: Caps lock is not retrieved the same way as Scroll and Num lock
174 // since it doesn't need to be.
175 const KeyCode num_lock_code
= XKeysymToKeycode(display
, XK_Num_Lock
);
176 const KeyCode scroll_lock_code
= XKeysymToKeycode(display
, XK_Scroll_Lock
);
178 for (size_t cnt
= 0; cnt
< size
; ++cnt
) {
179 if (! modmap
->modifiermap
[cnt
]) continue;
181 if (num_lock_code
== modmap
->modifiermap
[cnt
])
182 NumLockMask
= mask_table
[cnt
/ modmap
->max_keypermod
];
183 if (scroll_lock_code
== modmap
->modifiermap
[cnt
])
184 ScrollLockMask
= mask_table
[cnt
/ modmap
->max_keypermod
];
189 MaskList
[1] = LockMask
;
190 MaskList
[2] = NumLockMask
;
191 MaskList
[3] = ScrollLockMask
;
192 MaskList
[4] = LockMask
| NumLockMask
;
193 MaskList
[5] = NumLockMask
| ScrollLockMask
;
194 MaskList
[6] = LockMask
| ScrollLockMask
;
195 MaskList
[7] = LockMask
| NumLockMask
| ScrollLockMask
;
197 if (modmap
) XFreeModifiermap(const_cast<XModifierKeymap
*>(modmap
));
201 unsigned int XDisplay::stripModifiers(const unsigned int state
) const {
203 return state
&= ~(NumLockMask() | ScrollLockMask
| LockMask
);
205 return state
&= ~LockMask
;
211 * Verifies that a window has not requested to be destroyed/unmapped, so
212 * if it is a valid window or not.
213 * Returns: true if the window is valid; false if it is no longer valid.
215 bool XDisplay::validateWindow(Window window
) {
217 if (XCheckTypedWindowEvent(_display
, window
, DestroyNotify
, &event
)) {
218 XPutBackEvent(display
, &event
);
226 * Grabs a button, but also grabs the button in every possible combination with
227 * the keyboard lock keys, so that they do not cancel out the event.
229 void BaseDisplay::grabButton(unsigned int button
, unsigned int modifiers
,
230 Window grab_window
, Bool owner_events
,
231 unsigned int event_mask
, int pointer_mode
,
232 int keybaord_mode
, Window confine_to
,
236 for (size_t cnt
= 0; cnt
< 8; ++cnt
)
237 XGrabButton(_display
, button
, modifiers
| MaskList
[cnt
], grab_window
,
238 owner_events
, event_mask
, pointer_mode
, keybaord_mode
,
241 XGrabButton(_display
, button
, modifiers
, grab_window
,
242 owner_events
, event_mask
, pointer_mode
, keybaord_mode
,
249 * Releases the grab on a button, and ungrabs all possible combinations of the
250 * keyboard lock keys.
252 void BaseDisplay::ungrabButton(unsigned int button
, unsigned int modifiers
,
253 Window grab_window
) const {
255 for (size_t cnt
= 0; cnt
< 8; ++cnt
)
256 XUngrabButton(display
, button
, modifiers
| MaskList
[cnt
], grab_window
);
258 XUngrabButton(display
, button
, modifiers
, grab_window
);
This page took 0.046199 seconds and 4 git commands to generate.