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 also unshade the window..
223 _xatom
->sendClientMessage(_screen
->rootWindow(), XAtom::net_active_window
,
228 void XWindow::sendTo(unsigned int dest
) const {
229 _xatom
->sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_desktop
,
234 void XWindow::move(int x
, int y
) const {
235 // get the window's decoration sizes (margins)
237 Window win
= _window
, parent
, root
, last
= None
;
238 Window
*children
= 0;
239 unsigned int nchildren
;
240 XWindowAttributes wattr
;
242 while (XQueryTree(_epist
->getXDisplay(), win
, &root
, &parent
, &children
,
244 if (children
&& nchildren
> 0)
245 XFree(children
); // don't care about the children
247 if (! parent
) // no parent!?
250 // if the parent window is the root window, stop here
258 if (! (XTranslateCoordinates(_epist
->getXDisplay(), last
, win
, 0, 0,
259 (int *) &margins
.left
,
260 (int *) &margins
.top
,
262 XGetWindowAttributes(_epist
->getXDisplay(), win
, &wattr
)))
265 margins
.right
= wattr
.width
- _rect
.width() - margins
.left
;
266 margins
.bottom
= wattr
.height
- _rect
.height() - margins
.top
;
268 margins
.left
+= wattr
.border_width
;
269 margins
.right
+= wattr
.border_width
;
270 margins
.top
+= wattr
.border_width
;
271 margins
.bottom
+= wattr
.border_width
;
273 // this makes things work. why? i don't know. but you need them.
277 // find the frame's reference position based on the window's gravity
279 case NorthWestGravity
:
284 x
+= (margins
.left
+ margins
.right
) / 2;
287 case NorthEastGravity
:
292 y
+= (margins
.top
+ margins
.bottom
) / 2;
295 x
+= (margins
.left
+ margins
.right
) / 2;
296 y
+= (margins
.top
+ margins
.bottom
) / 2;
300 y
+= (margins
.top
+ margins
.bottom
) / 2;
301 case SouthWestGravity
:
306 x
+= (margins
.left
+ margins
.right
) / 2;
309 case SouthEastGravity
:
317 XMoveWindow(_epist
->getXDisplay(), _window
, x
, y
);
321 void XWindow::resize(unsigned int width
, unsigned int height
) const {
322 // resize in increments if requested by the window
324 unsigned int wdest
= width
/ _inc_x
* _inc_x
+ _base_x
;
325 unsigned int hdest
= height
/ _inc_y
* _inc_y
+ _base_y
;
328 while (width
> wdest
)
331 while (width
< wdest
)
334 if (height
> hdest
) {
335 while (height
> hdest
)
338 while (height
< hdest
)
342 XResizeWindow(_epist
->getXDisplay(), _window
, wdest
, hdest
);
346 void XWindow::toggleMaximize(Max max
) const {
350 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
351 _window
, (_max_vert
== _max_horz
? 2 : 1),
352 _xatom
->getAtom(XAtom::net_wm_state_maximized_horz
),
353 _xatom
->getAtom(XAtom::net_wm_state_maximized_vert
));
358 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
360 _xatom
->getAtom(XAtom::net_wm_state_maximized_horz
));
365 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
367 _xatom
->getAtom(XAtom::net_wm_state_maximized_vert
));
371 assert(false); // you should not do this. it is pointless and probly a bug
377 void XWindow::maximize(Max max
) const {
381 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
383 _xatom
->getAtom(XAtom::net_wm_state_maximized_horz
),
384 _xatom
->getAtom(XAtom::net_wm_state_maximized_vert
));
389 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
391 _xatom
->getAtom(XAtom::net_wm_state_maximized_horz
),
392 _xatom
->getAtom(XAtom::net_wm_state_maximized_vert
));
397 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
399 _xatom
->getAtom(XAtom::net_wm_state_maximized_horz
));
404 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
406 _xatom
->getAtom(XAtom::net_wm_state_maximized_vert
));