1 // -*- mode: C++; indent-tabs-mode: nil; -*-
2 // window.cc for Epistophy - a key handler for NETWM/EWMH window managers.
3 // Copyright (c) 2002 - 2002 Ben Jansens <ben at orodu.net>
5 // Permission is hereby granted, free of charge, to any person obtaining a
6 // copy of this software and associated documentation files (the "Software"),
7 // to deal in the Software without restriction, including without limitation
8 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 // and/or sell copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following conditions:
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 // DEALINGS IN THE SOFTWARE.
24 # include "../../config.h"
25 #endif // HAVE_CONFIG_H
37 #include "../../src/XAtom.hh"
39 XWindow::XWindow(epist *epist, screen *screen, Window window)
40 : _epist(epist), _screen(screen), _xatom(epist->xatom()), _window(window) {
44 XSelectInput(_epist->getXDisplay(), _window,
45 PropertyChangeMask | StructureNotifyMask);
51 _epist->addWindow(this);
57 XSelectInput(_epist->getXDisplay(), _window, None);
58 _epist->removeWindow(this);
62 void XWindow::updateState() {
64 _shaded = _iconic = _max_vert = _max_horz = false;
66 unsigned long num = (unsigned) -1;
68 if (! _xatom->getValue(_window, XAtom::net_wm_state, XAtom::atom,
71 for (unsigned long i = 0; i < num; ++i) {
72 if (state[i] == _xatom->getAtom(XAtom::net_wm_state_maximized_vert))
74 if (state[i] == _xatom->getAtom(XAtom::net_wm_state_maximized_horz))
76 if (state[i] == _xatom->getAtom(XAtom::net_wm_state_shaded))
78 if (state[i] == _xatom->getAtom(XAtom::net_wm_state_hidden))
86 void XWindow::updateDesktop() {
87 if (! _xatom->getValue(_window, XAtom::net_wm_desktop, XAtom::cardinal,
88 static_cast<unsigned long>(_desktop)))
93 void XWindow::updateTitle() {
97 if (! _xatom->getValue(_window, XAtom::net_wm_name, XAtom::utf8, _title)) {
99 _xatom->getValue(_window, XAtom::wm_name, XAtom::ansi, _title);
107 void XWindow::updateClass() {
109 _app_name = _app_class = "";
112 unsigned long num = 2;
114 if (! _xatom->getValue(_window, XAtom::wm_class, XAtom::ansi, num, v))
117 if (num > 0) _app_name = v[0];
118 if (num > 1) _app_class = v[1];
122 void XWindow::processEvent(const XEvent &e) {
123 assert(e.xany.window == _window);
128 if (e.xproperty.atom == _xatom->getAtom(XAtom::net_wm_state))
130 else if (e.xproperty.atom == _xatom->getAtom(XAtom::net_wm_desktop))
132 else if (e.xproperty.atom == _xatom->getAtom(XAtom::net_wm_name) ||
133 e.xproperty.atom == _xatom->getAtom(XAtom::wm_name))
135 else if (e.xproperty.atom == _xatom->getAtom(XAtom::wm_class))
146 void XWindow::findFramePosition(int &x, int &y) const {
147 Window win = _window, parent, root, last = None;
148 Window *children = 0;
149 unsigned int nchildren;
150 int gravity, top, bottom, left, right;
151 XWindowAttributes wattr;
154 unsigned int cwidth, cheight;
156 // get the location, size and gravity of the client window
157 if (! XGetWindowAttributes(_epist->getXDisplay(), _window, &wattr)) return;
158 cwidth = wattr.width;
159 cheight = wattr.height;
160 if (! XGetWMNormalHints(_epist->getXDisplay(), _window, &size, &ret)) return;
161 if (size.flags & PWinGravity)
162 gravity = size.win_gravity;
164 gravity = NorthWestGravity;
166 while (XQueryTree(_epist->getXDisplay(), win, &root, &parent, &children,
168 if (children && nchildren > 0)
169 XFree(children); // don't care about the children
171 if (! parent) // no parent!?
174 // if the parent window is the root window, stop here
182 if (! (XTranslateCoordinates(_epist->getXDisplay(), last, win, 0, 0,
183 &left, &top, &parent) &&
184 XGetWindowAttributes(_epist->getXDisplay(), win, &wattr)))
187 right = wattr.width - cwidth - left;
188 bottom = wattr.height - cheight - top;
190 left += wattr.border_width;
191 right += wattr.border_width;
192 top += wattr.border_width;
193 bottom += wattr.border_width;
195 // find the client's location
199 // this makes things work. why? i don't know. but you need them.
203 // find the frame's reference position based on the window's gravity
205 case NorthWestGravity:
210 x += (left + right) / 2;
213 case NorthEastGravity:
218 y += (top + bottom) / 2;
221 x += (left + right) / 2;
222 y += (top + bottom) / 2;
226 y += (top + bottom) / 2;
227 case SouthWestGravity:
232 x += (left + right) / 2;
235 case SouthEastGravity:
245 void XWindow::shade(const bool sh) const {
246 _xatom->sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
247 _window, (sh ? 1 : 0),
248 _xatom->getAtom(XAtom::net_wm_state_shaded));
252 void XWindow::close() const {
253 _xatom->sendClientMessage(_screen->rootWindow(), XAtom::net_close_window,
258 void XWindow::raise() const {
259 XRaiseWindow(_epist->getXDisplay(), _window);
263 void XWindow::lower() const {
264 XLowerWindow(_epist->getXDisplay(), _window);
268 void XWindow::iconify() const {
269 _xatom->sendClientMessage(_screen->rootWindow(), XAtom::wm_change_state,
270 _window, IconicState);
274 void XWindow::focus() const {
275 // this will also unshade the window..
276 _xatom->sendClientMessage(_screen->rootWindow(), XAtom::net_active_window,
281 void XWindow::sendTo(unsigned int dest) const {
282 _xatom->sendClientMessage(_screen->rootWindow(), XAtom::net_wm_desktop,
287 void XWindow::move(int x, int y) const {
289 findFramePosition(fx, fy);
290 XMoveWindow(_epist->getXDisplay(), _window, fx + x, fy + y);
294 void XWindow::toggleMaximize(Max max) const {
298 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
299 _window, (_max_vert == _max_horz ? 2 : 1),
300 _xatom->getAtom(XAtom::net_wm_state_maximized_horz),
301 _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
306 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
308 _xatom->getAtom(XAtom::net_wm_state_maximized_horz));
313 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
315 _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
319 assert(false); // you should not do this. it is pointless and probly a bug
325 void XWindow::maximize(Max max) const {
329 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
331 _xatom->getAtom(XAtom::net_wm_state_maximized_horz),
332 _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
337 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
339 _xatom->getAtom(XAtom::net_wm_state_maximized_horz),
340 _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
345 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
347 _xatom->getAtom(XAtom::net_wm_state_maximized_horz));
352 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
354 _xatom->getAtom(XAtom::net_wm_state_maximized_vert));