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
);
55 _epist
->addWindow(this);
61 XSelectInput(_epist
->getXDisplay(), _window
, None
);
62 _epist
->removeWindow(this);
66 void XWindow::updateDimentions() {
69 unsigned int w
, h
, b
, d
;
71 if (XGetGeometry(_epist
->getXDisplay(), _window
, &root
, &x
, &y
, &w
, &h
,
73 XTranslateCoordinates(_epist
->getXDisplay(), _window
, root
, x
, y
,
75 _rect
.setRect(x
, y
, w
, h
);
77 _rect
.setRect(0, 0, 1, 1);
81 void XWindow::updateNormalHints() {
86 _gravity
= NorthWestGravity
;
88 _base_x
= _base_y
= 0;
90 if (XGetWMNormalHints(_epist
->getXDisplay(), _window
, &size
, &ret
)) {
91 if (size
.flags
& PWinGravity
)
92 _gravity
= size
.win_gravity
;
93 if (size
.flags
& PBaseSize
) {
94 _base_x
= size
.base_width
;
95 _base_y
= size
.base_height
;
97 if (size
.flags
& PResizeInc
) {
98 _inc_x
= size
.width_inc
;
99 _inc_y
= size
.height_inc
;
105 void XWindow::updateWMHints() {
108 if ((hints
= XGetWMHints(_epist
->getXDisplay(), _window
)) != NULL
) {
109 _can_focus
= hints
->input
;
112 // assume a window takes input if it doesnt specify
118 void XWindow::updateState() {
120 _shaded
= _iconic
= _max_vert
= _max_horz
= false;
122 unsigned long num
= (unsigned) -1;
124 if (! _xatom
->getValue(_window
, XAtom::net_wm_state
, XAtom::atom
,
127 for (unsigned long i
= 0; i
< num
; ++i
) {
128 if (state
[i
] == _xatom
->getAtom(XAtom::net_wm_state_maximized_vert
))
130 if (state
[i
] == _xatom
->getAtom(XAtom::net_wm_state_maximized_horz
))
132 if (state
[i
] == _xatom
->getAtom(XAtom::net_wm_state_shaded
))
134 if (state
[i
] == _xatom
->getAtom(XAtom::net_wm_state_hidden
))
142 void XWindow::updateDesktop() {
143 if (! _xatom
->getValue(_window
, XAtom::net_wm_desktop
, XAtom::cardinal
,
144 static_cast<unsigned long>(_desktop
)))
149 void XWindow::updateTitle() {
153 if (! _xatom
->getValue(_window
, XAtom::net_wm_name
, XAtom::utf8
, _title
)) {
155 _xatom
->getValue(_window
, XAtom::wm_name
, XAtom::ansi
, _title
);
163 void XWindow::updateClass() {
165 _app_name
= _app_class
= "";
168 unsigned long num
= 2;
170 if (! _xatom
->getValue(_window
, XAtom::wm_class
, XAtom::ansi
, num
, v
))
173 if (num
> 0) _app_name
= v
[0];
174 if (num
> 1) _app_class
= v
[1];
178 void XWindow::processEvent(const XEvent
&e
) {
179 assert(e
.xany
.window
== _window
);
182 case ConfigureNotify
:
186 if (e
.xproperty
.atom
== XA_WM_NORMAL_HINTS
)
188 if (e
.xproperty
.atom
== XA_WM_HINTS
)
190 else if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::net_wm_state
))
192 else if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::net_wm_desktop
))
194 else if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::net_wm_name
) ||
195 e
.xproperty
.atom
== _xatom
->getAtom(XAtom::wm_name
))
197 else if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::wm_class
))
208 void XWindow::shade(const bool sh
) const {
209 _xatom
->sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
210 _window
, (sh
? 1 : 0),
211 _xatom
->getAtom(XAtom::net_wm_state_shaded
));
215 void XWindow::close() const {
216 _xatom
->sendClientMessage(_screen
->rootWindow(), XAtom::net_close_window
,
221 void XWindow::raise() const {
222 XRaiseWindow(_epist
->getXDisplay(), _window
);
226 void XWindow::lower() const {
227 XLowerWindow(_epist
->getXDisplay(), _window
);
231 void XWindow::iconify() const {
232 _xatom
->sendClientMessage(_screen
->rootWindow(), XAtom::wm_change_state
,
233 _window
, IconicState
);
237 void XWindow::focus() const {
238 // this will cause the window to be uniconified also
239 _xatom
->sendClientMessage(_screen
->rootWindow(), XAtom::net_active_window
,
242 //XSetInputFocus(_epist->getXDisplay(), _window, None, CurrentTime);
246 void XWindow::sendTo(unsigned int dest
) const {
247 _xatom
->sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_desktop
,
252 void XWindow::move(int x
, int y
) const {
253 // get the window's decoration sizes (margins)
255 Window win
= _window
, parent
, root
, last
= None
;
256 Window
*children
= 0;
257 unsigned int nchildren
;
258 XWindowAttributes wattr
;
260 while (XQueryTree(_epist
->getXDisplay(), win
, &root
, &parent
, &children
,
262 if (children
&& nchildren
> 0)
263 XFree(children
); // don't care about the children
265 if (! parent
) // no parent!?
268 // if the parent window is the root window, stop here
276 if (! (XTranslateCoordinates(_epist
->getXDisplay(), last
, win
, 0, 0,
277 (int *) &margins
.left
,
278 (int *) &margins
.top
,
280 XGetWindowAttributes(_epist
->getXDisplay(), win
, &wattr
)))
283 margins
.right
= wattr
.width
- _rect
.width() - margins
.left
;
284 margins
.bottom
= wattr
.height
- _rect
.height() - margins
.top
;
286 margins
.left
+= wattr
.border_width
;
287 margins
.right
+= wattr
.border_width
;
288 margins
.top
+= wattr
.border_width
;
289 margins
.bottom
+= wattr
.border_width
;
291 // this makes things work. why? i don't know. but you need them.
295 // find the frame's reference position based on the window's gravity
297 case NorthWestGravity
:
302 x
+= (margins
.left
+ margins
.right
) / 2;
305 case NorthEastGravity
:
310 y
+= (margins
.top
+ margins
.bottom
) / 2;
313 x
+= (margins
.left
+ margins
.right
) / 2;
314 y
+= (margins
.top
+ margins
.bottom
) / 2;
318 y
+= (margins
.top
+ margins
.bottom
) / 2;
319 case SouthWestGravity
:
324 x
+= (margins
.left
+ margins
.right
) / 2;
327 case SouthEastGravity
:
335 XMoveWindow(_epist
->getXDisplay(), _window
, x
, y
);
339 void XWindow::resize(unsigned int width
, unsigned int height
) const {
340 // resize in increments if requested by the window
342 unsigned int wdest
= width
/ _inc_x
* _inc_x
+ _base_x
;
343 unsigned int hdest
= height
/ _inc_y
* _inc_y
+ _base_y
;
346 while (width
> wdest
)
349 while (width
< wdest
)
352 if (height
> hdest
) {
353 while (height
> hdest
)
356 while (height
< hdest
)
360 XResizeWindow(_epist
->getXDisplay(), _window
, wdest
, hdest
);
364 void XWindow::toggleMaximize(Max max
) const {
368 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
369 _window
, (_max_vert
== _max_horz
? 2 : 1),
370 _xatom
->getAtom(XAtom::net_wm_state_maximized_horz
),
371 _xatom
->getAtom(XAtom::net_wm_state_maximized_vert
));
376 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
378 _xatom
->getAtom(XAtom::net_wm_state_maximized_horz
));
383 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
385 _xatom
->getAtom(XAtom::net_wm_state_maximized_vert
));
389 assert(false); // you should not do this. it is pointless and probly a bug
395 void XWindow::maximize(Max max
) const {
399 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
401 _xatom
->getAtom(XAtom::net_wm_state_maximized_horz
),
402 _xatom
->getAtom(XAtom::net_wm_state_maximized_vert
));
407 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
409 _xatom
->getAtom(XAtom::net_wm_state_maximized_horz
),
410 _xatom
->getAtom(XAtom::net_wm_state_maximized_vert
));
415 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
417 _xatom
->getAtom(XAtom::net_wm_state_maximized_horz
));
422 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
424 _xatom
->getAtom(XAtom::net_wm_state_maximized_vert
));