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);
54 _epist->addWindow(this);
60 XSelectInput(_epist->getXDisplay(), _window, None);
61 _epist->removeWindow(this);
65 void XWindow::updateDimentions() {
68 unsigned int w, h, b, d;
70 if (XGetGeometry(_epist->getXDisplay(), _window, &root, &x, &y, &w, &h,
72 XTranslateCoordinates(_epist->getXDisplay(), _window, root, x, y,
74 _rect.setRect(x, y, w, h);
76 _rect.setRect(0, 0, 1, 1);
80 void XWindow::updateGravity() {
84 if (XGetWMNormalHints(_epist->getXDisplay(), _window, &size, &ret) &&
85 (size.flags & PWinGravity))
86 _gravity = size.win_gravity;
88 _gravity = NorthWestGravity;
92 void XWindow::updateState() {
94 _shaded = _iconic = _max_vert = _max_horz = false;
96 unsigned long num = (unsigned) -1;
98 if (! _xatom->getValue(_window, XAtom::net_wm_state, XAtom::atom,
101 for (unsigned long i = 0; i < num; ++i) {
102 if (state[i] == _xatom->getAtom(XAtom::net_wm_state_maximized_vert))
104 if (state[i] == _xatom->getAtom(XAtom::net_wm_state_maximized_horz))
106 if (state[i] == _xatom->getAtom(XAtom::net_wm_state_shaded))
108 if (state[i] == _xatom->getAtom(XAtom::net_wm_state_hidden))
116 void XWindow::updateDesktop() {
117 if (! _xatom->getValue(_window, XAtom::net_wm_desktop, XAtom::cardinal,
118 static_cast<unsigned long>(_desktop)))
123 void XWindow::updateTitle() {
127 if (! _xatom->getValue(_window, XAtom::net_wm_name, XAtom::utf8, _title)) {
129 _xatom->getValue(_window, XAtom::wm_name, XAtom::ansi, _title);
137 void XWindow::updateClass() {
139 _app_name = _app_class = "";
142 unsigned long num = 2;
144 if (! _xatom->getValue(_window, XAtom::wm_class, XAtom::ansi, num, v))
147 if (num > 0) _app_name = v[0];
148 if (num > 1) _app_class = v[1];
152 void XWindow::processEvent(const XEvent &e) {
153 assert(e.xany.window == _window);
156 case ConfigureNotify:
160 if (e.xproperty.atom == XA_WM_NORMAL_HINTS)
162 else if (e.xproperty.atom == _xatom->getAtom(XAtom::net_wm_state))
164 else if (e.xproperty.atom == _xatom->getAtom(XAtom::net_wm_desktop))
166 else if (e.xproperty.atom == _xatom->getAtom(XAtom::net_wm_name) ||
167 e.xproperty.atom == _xatom->getAtom(XAtom::wm_name))
169 else if (e.xproperty.atom == _xatom->getAtom(XAtom::wm_class))
180 void XWindow::shade(const bool sh) const {
181 _xatom->sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
182 _window, (sh ? 1 : 0),
183 _xatom->getAtom(XAtom::net_wm_state_shaded));
187 void XWindow::close() const {
188 _xatom->sendClientMessage(_screen->rootWindow(), XAtom::net_close_window,
193 void XWindow::raise() const {
194 XRaiseWindow(_epist->getXDisplay(), _window);
198 void XWindow::lower() const {
199 XLowerWindow(_epist->getXDisplay(), _window);
203 void XWindow::iconify() const {
204 _xatom->sendClientMessage(_screen->rootWindow(), XAtom::wm_change_state,
205 _window, IconicState);
209 void XWindow::focus() const {
210 // this will also unshade the window..
211 _xatom->sendClientMessage(_screen->rootWindow(), XAtom::net_active_window,
216 void XWindow::sendTo(unsigned int dest) const {
217 _xatom->sendClientMessage(_screen->rootWindow(), XAtom::net_wm_desktop,
222 void XWindow::move(int x, int y) const {
223 // get the window's decoration sizes (margins)
225 Window win = _window, parent, root, last = None;
226 Window *children = 0;
227 unsigned int nchildren;
228 XWindowAttributes wattr;
230 while (XQueryTree(_epist->getXDisplay(), win, &root, &parent, &children,
232 if (children && nchildren > 0)
233 XFree(children); // don't care about the children
235 if (! parent) // no parent!?
238 // if the parent window is the root window, stop here
246 if (! (XTranslateCoordinates(_epist->getXDisplay(), last, win, 0, 0,
247 (int *) &margins.left,
248 (int *) &margins.top,
250 XGetWindowAttributes(_epist->getXDisplay(), win, &wattr)))
253 margins.right = wattr.width - _rect.width() - margins.left;
254 margins.bottom = wattr.height - _rect.height() - margins.top;
256 margins.left += wattr.border_width;
257 margins.right += wattr.border_width;
258 margins.top += wattr.border_width;
259 margins.bottom += wattr.border_width;
261 // this makes things work. why? i don't know. but you need them.
265 // find the frame's reference position based on the window's gravity
267 case NorthWestGravity:
272 x += (margins.left + margins.right) / 2;
275 case NorthEastGravity:
280 y += (margins.top + margins.bottom) / 2;
283 x += (margins.left + margins.right) / 2;
284 y += (margins.top + margins.bottom) / 2;
288 y += (margins.top + margins.bottom) / 2;
289 case SouthWestGravity:
294 x += (margins.left + margins.right) / 2;
297 case SouthEastGravity:
305 XMoveWindow(_epist->getXDisplay(), _window, x, y);
309 void XWindow::resize(unsigned int width, unsigned int height) const {
310 XResizeWindow(_epist->getXDisplay(), _window, width, height);
314 void XWindow::toggleMaximize(Max max) const {
318 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
319 _window, (_max_vert == _max_horz ? 2 : 1),
320 _xatom->getAtom(XAtom::net_wm_state_maximized_horz),
321 _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
326 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
328 _xatom->getAtom(XAtom::net_wm_state_maximized_horz));
333 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
335 _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
339 assert(false); // you should not do this. it is pointless and probly a bug
345 void XWindow::maximize(Max max) const {
349 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
351 _xatom->getAtom(XAtom::net_wm_state_maximized_horz),
352 _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
357 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
359 _xatom->getAtom(XAtom::net_wm_state_maximized_horz),
360 _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
365 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
367 _xatom->getAtom(XAtom::net_wm_state_maximized_horz));
372 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
374 _xatom->getAtom(XAtom::net_wm_state_maximized_vert));