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>
47 std::string XDisplay::_app_name;
48 Window XDisplay::_last_bad_window = None;
51 * X error handler to handle all X errors while the application is
54 int XDisplay::XErrorHandler(Display *d, XErrorEvent *e) {
57 XGetErrorText(d, e->error_code, errtxt, sizeof(errtxt)/sizeof(char));
58 cerr << _app_name.c_str() << ": X error: " <<
59 errtxt << "(" << e->error_code << ") opcodes " <<
60 e->request_code << "/" << e->minor_code << endl;
61 cerr.flags(std::ios_base::hex);
62 cerr << " resource 0x" << e->resourceid << endl;
63 cerr.flags(std::ios_base::dec);
66 if (e->error_code == BadWindow)
67 _last_bad_window = e->resourceid;
73 XDisplay::XDisplay(const std::string &application_name, const char *dpyname) {
74 _app_name = application_name;
78 _display = XOpenDisplay(dpyname);
79 if (_display == NULL) {
80 cerr << "Could not open display. Connection to X server failed.\n";
83 if (-1 == fcntl(ConnectionNumber(_display), F_SETFD, 1)) {
84 cerr << "Could not mark display connection as close-on-exec.\n";
87 _name = XDisplayName(dpyname);
89 XSetErrorHandler(XErrorHandler);
93 _hasshape = XShapeQueryExtension(_display, &_shape_event_base, &waste);
96 const unsigned int scount = ScreenCount(_display);
97 _screens.reserve(scount);
98 for (unsigned int s = 0; s < scount; s++)
99 _screens.push_back(new XScreen(this, s));
103 XDisplay::~XDisplay() {
104 std::for_each(_screens.begin(), _screens.end(), PointerAssassin());
105 XCloseDisplay(_display);
110 * Return information about a screen.
112 XScreen *XDisplay::screen(unsigned int s) const {
113 ASSERT(s < _screens.size());
121 void XDisplay::grab() {
123 XGrabServer(_display);
128 * Release the X server from a grab
130 void XDisplay::ungrab() {
132 XUngrabServer(_display);
137 * Gets the next event on the queue from the X server.
139 * Returns: true if e contains a new event; false if there is no event to be
142 bool XDisplay::nextEvent(XEvent &e) {
143 if(!XPending(_display))
145 XNextEvent(_display, &e);
146 if (_last_bad_window != None) {
147 if (e.xany.window == _last_bad_window) {
148 cerr << "XDisplay::nextEvent(): Removing event for bad window from " <<
152 _last_bad_window = None;