]> Dogcows Code - chaz/openbox/blob - src/XDisplay.cc
XDisplay's nextEvent completed
[chaz/openbox] / src / XDisplay.cc
1 // XDisplay.cc for Openbox
2 // Copyright (c) 2002 - 2002 Ben Janens (ben at orodu.net)
3 //
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:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
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.
21
22 #ifdef HAVE_CONFIG_H
23 # include "../config.h"
24 #endif
25
26 #ifdef HAVE_UNNISTD_H
27 # include <unistd.h>
28 #endif
29
30 #ifdef HAVE_FCNTL_H
31 # include <fcntl.h>
32 #endif
33
34 #ifdef SHAPE
35 # include <X11/extensions/shape.h>
36 #endif
37
38 #include "XDisplay.h"
39 #include "XScreen.h"
40 #include "Util.h"
41 #include <iostream>
42 #include <algorithm>
43
44 using std::cerr;
45 using std::endl;
46
47 std::string XDisplay::_app_name;
48 Window XDisplay::_last_bad_window = None;
49
50 /*
51 * X error handler to handle all X errors while the application is
52 * running.
53 */
54 int XDisplay::XErrorHandler(Display *d, XErrorEvent *e) {
55 #ifdef DEBUG
56 char errtxt[128];
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);
64 #endif
65
66 if (e->error_code == BadWindow)
67 _last_bad_window = e->resourceid;
68
69 return False;
70 }
71
72
73 XDisplay::XDisplay(const std::string &application_name, const char *dpyname) {
74 _app_name = application_name;
75 _grabs = 0;
76 _hasshape = false;
77
78 _display = XOpenDisplay(dpyname);
79 if (_display == NULL) {
80 cerr << "Could not open display. Connection to X server failed.\n";
81 ::exit(2);
82 }
83 if (-1 == fcntl(ConnectionNumber(_display), F_SETFD, 1)) {
84 cerr << "Could not mark display connection as close-on-exec.\n";
85 ::exit(2);
86 }
87 _name = XDisplayName(dpyname);
88
89 XSetErrorHandler(XErrorHandler);
90
91 #ifdef SHAPE
92 int waste;
93 _hasshape = XShapeQueryExtension(_display, &_shape_event_base, &waste);
94 #endif // SHAPE
95
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));
100 }
101
102
103 XDisplay::~XDisplay() {
104 std::for_each(_screens.begin(), _screens.end(), PointerAssassin());
105 XCloseDisplay(_display);
106 }
107
108
109 /*
110 * Return information about a screen.
111 */
112 XScreen *XDisplay::screen(unsigned int s) const {
113 ASSERT(s < _screens.size());
114 return _screens[s];
115 }
116
117
118 /*
119 * Grab the X server
120 */
121 void XDisplay::grab() {
122 if (_grabs++ == 0)
123 XGrabServer(_display);
124 }
125
126
127 /*
128 * Release the X server from a grab
129 */
130 void XDisplay::ungrab() {
131 if (--_grabs == 0)
132 XUngrabServer(_display);
133 }
134
135
136 /*
137 * Gets the next event on the queue from the X server.
138 *
139 * Returns: true if e contains a new event; false if there is no event to be
140 * returned.
141 */
142 bool XDisplay::nextEvent(XEvent &e) {
143 if(!XPending(_display))
144 return false;
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 " <<
149 "event queue\n";
150 return false;
151 } else
152 _last_bad_window = None;
153 }
154 return true;
155 }
This page took 0.041205 seconds and 4 git commands to generate.