1 // -*- mode: C++; indent-tabs-mode: nil; -*-
2 // window.cc for Epistrophy - 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
);
47 updateBlackboxAttributes();
56 _epist
->addWindow(this);
62 XSelectInput(_epist
->getXDisplay(), _window
, None
);
63 _epist
->removeWindow(this);
67 void XWindow::updateDimentions() {
70 unsigned int w
, h
, b
, d
;
72 if (XGetGeometry(_epist
->getXDisplay(), _window
, &root
, &x
, &y
, &w
, &h
,
74 XTranslateCoordinates(_epist
->getXDisplay(), _window
, root
, x
, y
,
76 _rect
.setRect(x
, y
, w
, h
);
78 _rect
.setRect(0, 0, 1, 1);
82 void XWindow::updateBlackboxAttributes() {
84 unsigned long num
= PropBlackboxAttributesElements
;
88 if (_xatom
->getValue(_window
,
89 XAtom::blackbox_attributes
, XAtom::blackbox_attributes
,
91 if (num
== PropBlackboxAttributesElements
)
92 if (data
[0] & AttribDecoration
)
93 _decorated
= (data
[4] != DecorNone
);
99 void XWindow::updateNormalHints() {
104 _gravity
= NorthWestGravity
;
106 _base_x
= _base_y
= 0;
108 if (XGetWMNormalHints(_epist
->getXDisplay(), _window
, &size
, &ret
)) {
109 if (size
.flags
& PWinGravity
)
110 _gravity
= size
.win_gravity
;
111 if (size
.flags
& PBaseSize
) {
112 _base_x
= size
.base_width
;
113 _base_y
= size
.base_height
;
115 if (size
.flags
& PResizeInc
) {
116 _inc_x
= size
.width_inc
;
117 _inc_y
= size
.height_inc
;
123 void XWindow::updateWMHints() {
126 if ((hints
= XGetWMHints(_epist
->getXDisplay(), _window
)) != NULL
) {
127 _can_focus
= hints
->input
;
130 // assume a window takes input if it doesnt specify
136 void XWindow::updateState() {
138 _shaded
= _iconic
= _max_vert
= _max_horz
= false;
140 unsigned long num
= (unsigned) -1;
142 if (! _xatom
->getValue(_window
, XAtom::net_wm_state
, XAtom::atom
,
145 for (unsigned long i
= 0; i
< num
; ++i
) {
146 if (state
[i
] == _xatom
->getAtom(XAtom::net_wm_state_maximized_vert
))
148 if (state
[i
] == _xatom
->getAtom(XAtom::net_wm_state_maximized_horz
))
150 if (state
[i
] == _xatom
->getAtom(XAtom::net_wm_state_shaded
))
152 if (state
[i
] == _xatom
->getAtom(XAtom::net_wm_state_hidden
))
160 void XWindow::updateDesktop() {
161 if (! _xatom
->getValue(_window
, XAtom::net_wm_desktop
, XAtom::cardinal
,
162 static_cast<unsigned long>(_desktop
)))
167 void XWindow::updateTitle() {
171 if (! _xatom
->getValue(_window
, XAtom::net_wm_name
, XAtom::utf8
, _title
)) {
173 _xatom
->getValue(_window
, XAtom::wm_name
, XAtom::ansi
, _title
);
181 void XWindow::updateClass() {
183 _app_name
= _app_class
= "";
186 unsigned long num
= 2;
188 if (! _xatom
->getValue(_window
, XAtom::wm_class
, XAtom::ansi
, num
, v
))
191 if (num
> 0) _app_name
= v
[0];
192 if (num
> 1) _app_class
= v
[1];
196 void XWindow::processEvent(const XEvent
&e
) {
197 assert(e
.xany
.window
== _window
);
200 case ConfigureNotify
:
204 if (e
.xproperty
.atom
== XA_WM_NORMAL_HINTS
)
206 if (e
.xproperty
.atom
== XA_WM_HINTS
)
208 else if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::net_wm_state
))
210 else if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::net_wm_desktop
))
212 else if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::net_wm_name
) ||
213 e
.xproperty
.atom
== _xatom
->getAtom(XAtom::wm_name
))
215 else if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::wm_class
))
226 void XWindow::shade(const bool sh
) const {
227 _xatom
->sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
228 _window
, (sh
? 1 : 0),
229 _xatom
->getAtom(XAtom::net_wm_state_shaded
));
233 void XWindow::close() const {
234 _xatom
->sendClientMessage(_screen
->rootWindow(), XAtom::net_close_window
,
239 void XWindow::raise() const {
240 XRaiseWindow(_epist
->getXDisplay(), _window
);
244 void XWindow::lower() const {
245 XLowerWindow(_epist
->getXDisplay(), _window
);
249 void XWindow::iconify() const {
250 _xatom
->sendClientMessage(_screen
->rootWindow(), XAtom::wm_change_state
,
251 _window
, IconicState
);
255 void XWindow::focus() const {
256 // this will cause the window to be uniconified also
257 _xatom
->sendClientMessage(_screen
->rootWindow(), XAtom::net_active_window
,
260 //XSetInputFocus(_epist->getXDisplay(), _window, None, CurrentTime);
264 void XWindow::sendTo(unsigned int dest
) const {
265 _xatom
->sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_desktop
,
270 void XWindow::move(int x
, int y
) const {
271 // get the window's decoration sizes (margins)
273 Window win
= _window
, parent
, root
, last
= None
;
274 Window
*children
= 0;
275 unsigned int nchildren
;
276 XWindowAttributes wattr
;
278 while (XQueryTree(_epist
->getXDisplay(), win
, &root
, &parent
, &children
,
280 if (children
&& nchildren
> 0)
281 XFree(children
); // don't care about the children
283 if (! parent
) // no parent!?
286 // if the parent window is the root window, stop here
294 if (! (XTranslateCoordinates(_epist
->getXDisplay(), last
, win
, 0, 0,
295 (int *) &margins
.left
,
296 (int *) &margins
.top
,
298 XGetWindowAttributes(_epist
->getXDisplay(), win
, &wattr
)))
301 margins
.right
= wattr
.width
- _rect
.width() - margins
.left
;
302 margins
.bottom
= wattr
.height
- _rect
.height() - margins
.top
;
304 margins
.left
+= wattr
.border_width
;
305 margins
.right
+= wattr
.border_width
;
306 margins
.top
+= wattr
.border_width
;
307 margins
.bottom
+= wattr
.border_width
;
309 // this makes things work. why? i don't know. but you need them.
313 // find the frame's reference position based on the window's gravity
315 case NorthWestGravity
:
320 x
+= (margins
.left
+ margins
.right
) / 2;
323 case NorthEastGravity
:
328 y
+= (margins
.top
+ margins
.bottom
) / 2;
331 x
+= (margins
.left
+ margins
.right
) / 2;
332 y
+= (margins
.top
+ margins
.bottom
) / 2;
336 y
+= (margins
.top
+ margins
.bottom
) / 2;
337 case SouthWestGravity
:
342 x
+= (margins
.left
+ margins
.right
) / 2;
345 case SouthEastGravity
:
353 XMoveWindow(_epist
->getXDisplay(), _window
, x
, y
);
357 void XWindow::resizeRel(int dwidth
, int dheight
) const {
358 // resize in increments if requested by the window
359 unsigned int width
= _rect
.width(), height
= _rect
.height();
361 unsigned int wdest
= width
+ (dwidth
* _inc_x
) / _inc_x
* _inc_x
+ _base_x
;
362 unsigned int hdest
= height
+ (dheight
* _inc_y
) / _inc_y
* _inc_y
+ _base_y
;
364 XResizeWindow(_epist
->getXDisplay(), _window
, wdest
, hdest
);
368 void XWindow::resizeAbs(unsigned int width
, unsigned int height
) const {
369 // resize in increments if requested by the window
371 unsigned int wdest
= width
/ _inc_x
* _inc_x
+ _base_x
;
372 unsigned int hdest
= height
/ _inc_y
* _inc_y
+ _base_y
;
375 while (width
> wdest
)
378 while (width
< wdest
)
381 if (height
> hdest
) {
382 while (height
> hdest
)
385 while (height
< hdest
)
389 XResizeWindow(_epist
->getXDisplay(), _window
, wdest
, hdest
);
393 void XWindow::toggleMaximize(Max max
) const {
397 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
398 _window
, (_max_vert
== _max_horz
? 2 : 1),
399 _xatom
->getAtom(XAtom::net_wm_state_maximized_horz
),
400 _xatom
->getAtom(XAtom::net_wm_state_maximized_vert
));
405 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
407 _xatom
->getAtom(XAtom::net_wm_state_maximized_horz
));
412 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
414 _xatom
->getAtom(XAtom::net_wm_state_maximized_vert
));
418 assert(false); // you should not do this. it is pointless and probly a bug
424 void XWindow::maximize(Max max
) const {
428 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
430 _xatom
->getAtom(XAtom::net_wm_state_maximized_horz
),
431 _xatom
->getAtom(XAtom::net_wm_state_maximized_vert
));
436 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
438 _xatom
->getAtom(XAtom::net_wm_state_maximized_horz
),
439 _xatom
->getAtom(XAtom::net_wm_state_maximized_vert
));
444 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
446 _xatom
->getAtom(XAtom::net_wm_state_maximized_horz
));
451 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
453 _xatom
->getAtom(XAtom::net_wm_state_maximized_vert
));
459 void XWindow::decorate(bool d
) const {
460 _xatom
->sendClientMessage(_screen
->rootWindow(),
461 XAtom::blackbox_change_attributes
,
462 _window
, AttribDecoration
,
463 0, 0, 0, (d
? DecorNormal
: DecorNone
));