]>
Dogcows Code - chaz/openbox/blob - otk/display.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
8 #include "screeninfo.hh"
12 #include <X11/keysym.h>
15 #include <X11/extensions/shape.h>
20 #endif // HAVE_STDIO_H
24 #endif // HAVE_STDLIB_H
28 #endif // HAVE_SIGNAL_H
32 #endif // HAVE_FCNTL_H
35 # include <sys/types.h>
37 #endif // HAVE_UNISTD_H
40 #define _(str) gettext(str)
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 unsigned int OBDisplay::_scrollLockMask
= 0;
53 unsigned int OBDisplay::_numLockMask
= 0;
54 OBDisplay::ScreenInfoList
OBDisplay::_screenInfoList
;
55 BGCCache
*OBDisplay::_gccache
= (BGCCache
*) 0;
56 int OBDisplay::_grab_count
= 0;
59 int OBDisplay::xerrorHandler(Display
*d
, XErrorEvent
*e
)
64 //if (e->error_code != BadWindow)
66 XGetErrorText(d
, e
->error_code
, errtxt
, 128);
67 printf("X Error: %s\n", errtxt
);
78 void OBDisplay::initialize(char *name
)
84 if (!(display
= XOpenDisplay(name
))) {
85 printf(_("Unable to open connection to the X server. Please set the \n\
86 DISPLAY environment variable approriately, or use the '-display' command \n\
87 line argument.\n\n"));
90 if (fcntl(ConnectionNumber(display
), F_SETFD
, 1) == -1) {
91 printf(_("Couldn't mark display connection as close-on-exec.\n\n"));
95 // set our error handler for X errors
96 XSetErrorHandler(xerrorHandler
);
98 // set the DISPLAY environment variable for any lauched children, to the
99 // display we're using, so they open in the right place.
100 // XXX rm -> std::string dtmp = "DISPLAY=" + DisplayString(display);
101 if (putenv(const_cast<char*>((std::string("DISPLAY=") +
102 DisplayString(display
)).c_str()))) {
103 printf(_("warning: couldn't set environment variable 'DISPLAY'\n"));
107 // find the availability of X extensions we like to use
109 _shape
= XShapeQueryExtension(display
, &_shape_event_basep
, &junk
);
113 _xinerama
= XineramaQueryExtension(display
, &_xinerama_event_basep
, &junk
);
116 // get lock masks that are defined by the display (not constant)
117 XModifierKeymap
*modmap
;
119 modmap
= XGetModifierMapping(display
);
120 if (modmap
&& modmap
->max_keypermod
> 0) {
121 const int mask_table
[] = {
122 ShiftMask
, LockMask
, ControlMask
, Mod1Mask
,
123 Mod2Mask
, Mod3Mask
, Mod4Mask
, Mod5Mask
125 const size_t size
= (sizeof(mask_table
) / sizeof(mask_table
[0])) *
126 modmap
->max_keypermod
;
127 // get the values of the keyboard lock modifiers
128 // Note: Caps lock is not retrieved the same way as Scroll and Num lock
129 // since it doesn't need to be.
130 const KeyCode num_lock
= XKeysymToKeycode(display
, XK_Num_Lock
);
131 const KeyCode scroll_lock
= XKeysymToKeycode(display
, XK_Scroll_Lock
);
133 for (size_t cnt
= 0; cnt
< size
; ++cnt
) {
134 if (! modmap
->modifiermap
[cnt
]) continue;
136 if (num_lock
== modmap
->modifiermap
[cnt
])
137 _numLockMask
= mask_table
[cnt
/ modmap
->max_keypermod
];
138 if (scroll_lock
== modmap
->modifiermap
[cnt
])
139 _scrollLockMask
= mask_table
[cnt
/ modmap
->max_keypermod
];
143 if (modmap
) XFreeModifiermap(modmap
);
146 _mask_list
[1] = LockMask
;
147 _mask_list
[2] = _numLockMask
;
148 _mask_list
[3] = LockMask
| _numLockMask
;
149 _mask_list
[4] = _scrollLockMask
;
150 _mask_list
[5] = _scrollLockMask
| LockMask
;
151 _mask_list
[6] = _scrollLockMask
| _numLockMask
;
152 _mask_list
[7] = _scrollLockMask
| LockMask
| _numLockMask
;
154 // Get information on all the screens which are available.
155 _screenInfoList
.reserve(ScreenCount(display
));
156 for (int i
= 0; i
< ScreenCount(display
); ++i
)
157 _screenInfoList
.push_back(ScreenInfo(i
));
159 _gccache
= new BGCCache(_screenInfoList
.size());
163 void OBDisplay::destroy()
166 while (_grab_count
> 0)
168 XCloseDisplay(display
);
172 const ScreenInfo
* OBDisplay::screenInfo(int snum
) {
174 assert(snum
< static_cast<int>(_screenInfoList
.size()));
175 return &_screenInfoList
[snum
];
179 const ScreenInfo
* OBDisplay::findScreen(Window root
)
181 ScreenInfoList::iterator it
, end
= _screenInfoList
.end();
182 for (it
= _screenInfoList
.begin(); it
!= end
; ++it
)
183 if (it
->rootWindow() == root
)
189 void OBDisplay::grab()
191 if (_grab_count
== 0)
192 XGrabServer(display
);
197 void OBDisplay::ungrab()
199 if (_grab_count
== 0) return;
201 if (_grab_count
== 0)
202 XUngrabServer(display
);
212 * Grabs a button, but also grabs the button in every possible combination
213 * with the keyboard lock keys, so that they do not cancel out the event.
215 * if allow_scroll_lock is true then only the top half of the lock mask
216 * table is used and scroll lock is ignored. This value defaults to false.
218 void OBDisplay::grabButton(unsigned int button
, unsigned int modifiers
,
219 Window grab_window
, bool owner_events
,
220 unsigned int event_mask
, int pointer_mode
,
221 int keyboard_mode
, Window confine_to
,
222 Cursor cursor
, bool allow_scroll_lock
) {
223 unsigned int length
= (allow_scroll_lock
) ? 8 / 2:
225 for (size_t cnt
= 0; cnt
< length
; ++cnt
)
226 XGrabButton(otk::OBDisplay::display
, button
, modifiers
| _mask_list
[cnt
],
227 grab_window
, owner_events
, event_mask
, pointer_mode
,
228 keyboard_mode
, confine_to
, cursor
);
233 * Releases the grab on a button, and ungrabs all possible combinations of the
234 * keyboard lock keys.
236 void OBDisplay::ungrabButton(unsigned int button
, unsigned int modifiers
,
237 Window grab_window
) {
238 for (size_t cnt
= 0; cnt
< 8; ++cnt
)
239 XUngrabButton(otk::OBDisplay::display
, button
, modifiers
| _mask_list
[cnt
],
243 void OBDisplay::grabKey(unsigned int keycode
, unsigned int modifiers
,
244 Window grab_window
, bool owner_events
,
245 int pointer_mode
, int keyboard_mode
,
246 bool allow_scroll_lock
)
248 unsigned int length
= (allow_scroll_lock
) ? 8 / 2:
250 for (size_t cnt
= 0; cnt
< length
; ++cnt
)
251 XGrabKey(otk::OBDisplay::display
, keycode
, modifiers
| _mask_list
[cnt
],
252 grab_window
, owner_events
, pointer_mode
, keyboard_mode
);
255 void OBDisplay::ungrabKey(unsigned int keycode
, unsigned int modifiers
,
258 for (size_t cnt
= 0; cnt
< 8; ++cnt
)
259 XUngrabKey(otk::OBDisplay::display
, keycode
, modifiers
| _mask_list
[cnt
],
This page took 0.050813 seconds and 4 git commands to generate.