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::updateHints() {
85 _gravity = NorthWestGravity;
87 _base_x = _base_y = 0;
89 if (XGetWMNormalHints(_epist->getXDisplay(), _window, &size, &ret)) {
90 if (size.flags & PWinGravity)
91 _gravity = size.win_gravity;
92 if (size.flags & PBaseSize) {
93 _base_x = size.base_width;
94 _base_y = size.base_height;
96 if (size.flags & PResizeInc) {
97 _inc_x = size.width_inc;
98 _inc_y = size.height_inc;
104 void XWindow::updateState() {
106 _shaded = _iconic = _max_vert = _max_horz = false;
108 unsigned long num = (unsigned) -1;
110 if (! _xatom->getValue(_window, XAtom::net_wm_state, XAtom::atom,
113 for (unsigned long i = 0; i < num; ++i) {
114 if (state[i] == _xatom->getAtom(XAtom::net_wm_state_maximized_vert))
116 if (state[i] == _xatom->getAtom(XAtom::net_wm_state_maximized_horz))
118 if (state[i] == _xatom->getAtom(XAtom::net_wm_state_shaded))
120 if (state[i] == _xatom->getAtom(XAtom::net_wm_state_hidden))
128 void XWindow::updateDesktop() {
129 if (! _xatom->getValue(_window, XAtom::net_wm_desktop, XAtom::cardinal,
130 static_cast<unsigned long>(_desktop)))
135 void XWindow::updateTitle() {
139 if (! _xatom->getValue(_window, XAtom::net_wm_name, XAtom::utf8, _title)) {
141 _xatom->getValue(_window, XAtom::wm_name, XAtom::ansi, _title);
149 void XWindow::updateClass() {
151 _app_name = _app_class = "";
154 unsigned long num = 2;
156 if (! _xatom->getValue(_window, XAtom::wm_class, XAtom::ansi, num, v))
159 if (num > 0) _app_name = v[0];
160 if (num > 1) _app_class = v[1];
164 void XWindow::processEvent(const XEvent &e) {
165 assert(e.xany.window == _window);
168 case ConfigureNotify:
172 if (e.xproperty.atom == XA_WM_NORMAL_HINTS)
174 else if (e.xproperty.atom == _xatom->getAtom(XAtom::net_wm_state))
176 else if (e.xproperty.atom == _xatom->getAtom(XAtom::net_wm_desktop))
178 else if (e.xproperty.atom == _xatom->getAtom(XAtom::net_wm_name) ||
179 e.xproperty.atom == _xatom->getAtom(XAtom::wm_name))
181 else if (e.xproperty.atom == _xatom->getAtom(XAtom::wm_class))
192 void XWindow::shade(const bool sh) const {
193 _xatom->sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
194 _window, (sh ? 1 : 0),
195 _xatom->getAtom(XAtom::net_wm_state_shaded));
199 void XWindow::close() const {
200 _xatom->sendClientMessage(_screen->rootWindow(), XAtom::net_close_window,
205 void XWindow::raise() const {
206 XRaiseWindow(_epist->getXDisplay(), _window);
210 void XWindow::lower() const {
211 XLowerWindow(_epist->getXDisplay(), _window);
215 void XWindow::iconify() const {
216 _xatom->sendClientMessage(_screen->rootWindow(), XAtom::wm_change_state,
217 _window, IconicState);
221 void XWindow::focus() const {
222 // this will cause the window to be uniconified also
223 _xatom->sendClientMessage(_screen->rootWindow(), XAtom::net_active_window,
226 //XSetInputFocus(_epist->getXDisplay(), _window, None, CurrentTime);
230 void XWindow::sendTo(unsigned int dest) const {
231 _xatom->sendClientMessage(_screen->rootWindow(), XAtom::net_wm_desktop,
236 void XWindow::move(int x, int y) const {
237 // get the window's decoration sizes (margins)
239 Window win = _window, parent, root, last = None;
240 Window *children = 0;
241 unsigned int nchildren;
242 XWindowAttributes wattr;
244 while (XQueryTree(_epist->getXDisplay(), win, &root, &parent, &children,
246 if (children && nchildren > 0)
247 XFree(children); // don't care about the children
249 if (! parent) // no parent!?
252 // if the parent window is the root window, stop here
260 if (! (XTranslateCoordinates(_epist->getXDisplay(), last, win, 0, 0,
261 (int *) &margins.left,
262 (int *) &margins.top,
264 XGetWindowAttributes(_epist->getXDisplay(), win, &wattr)))
267 margins.right = wattr.width - _rect.width() - margins.left;
268 margins.bottom = wattr.height - _rect.height() - margins.top;
270 margins.left += wattr.border_width;
271 margins.right += wattr.border_width;
272 margins.top += wattr.border_width;
273 margins.bottom += wattr.border_width;
275 // this makes things work. why? i don't know. but you need them.
279 // find the frame's reference position based on the window's gravity
281 case NorthWestGravity:
286 x += (margins.left + margins.right) / 2;
289 case NorthEastGravity:
294 y += (margins.top + margins.bottom) / 2;
297 x += (margins.left + margins.right) / 2;
298 y += (margins.top + margins.bottom) / 2;
302 y += (margins.top + margins.bottom) / 2;
303 case SouthWestGravity:
308 x += (margins.left + margins.right) / 2;
311 case SouthEastGravity:
319 XMoveWindow(_epist->getXDisplay(), _window, x, y);
323 void XWindow::resize(unsigned int width, unsigned int height) const {
324 // resize in increments if requested by the window
326 unsigned int wdest = width / _inc_x * _inc_x + _base_x;
327 unsigned int hdest = height / _inc_y * _inc_y + _base_y;
330 while (width > wdest)
333 while (width < wdest)
336 if (height > hdest) {
337 while (height > hdest)
340 while (height < hdest)
344 XResizeWindow(_epist->getXDisplay(), _window, wdest, hdest);
348 void XWindow::toggleMaximize(Max max) const {
352 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
353 _window, (_max_vert == _max_horz ? 2 : 1),
354 _xatom->getAtom(XAtom::net_wm_state_maximized_horz),
355 _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
360 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
362 _xatom->getAtom(XAtom::net_wm_state_maximized_horz));
367 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
369 _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
373 assert(false); // you should not do this. it is pointless and probly a bug
379 void XWindow::maximize(Max max) const {
383 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
385 _xatom->getAtom(XAtom::net_wm_state_maximized_horz),
386 _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
391 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
393 _xatom->getAtom(XAtom::net_wm_state_maximized_horz),
394 _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
399 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
401 _xatom->getAtom(XAtom::net_wm_state_maximized_horz));
406 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
408 _xatom->getAtom(XAtom::net_wm_state_maximized_vert));