1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
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 // defined by black/openbox
40 const unsigned long XWindow::PropBlackboxAttributesElements
;
41 const unsigned long XWindow::AttribDecoration
;
42 const unsigned long XWindow::DecorNone
;
43 const unsigned long XWindow::DecorNormal
;
46 XWindow::XWindow(epist
*epist
, screen
*screen
, Window window
)
47 : _epist(epist
), _screen(screen
), _xatom(epist
->xatom()), _window(window
) {
51 XSelectInput(_epist
->getXDisplay(), _window
,
52 PropertyChangeMask
| StructureNotifyMask
);
54 updateBlackboxAttributes();
63 _epist
->addWindow(this);
69 XSelectInput(_epist
->getXDisplay(), _window
, None
);
70 _epist
->removeWindow(this);
74 void XWindow::updateDimentions() {
77 unsigned int w
, h
, b
, d
;
79 if (XGetGeometry(_epist
->getXDisplay(), _window
, &root
, &x
, &y
, &w
, &h
,
81 XTranslateCoordinates(_epist
->getXDisplay(), _window
, root
, x
, y
,
83 _rect
.setRect(x
, y
, w
, h
);
85 _rect
.setRect(0, 0, 1, 1);
89 void XWindow::updateBlackboxAttributes() {
91 unsigned long num
= PropBlackboxAttributesElements
;
95 if (_xatom
->getValue(_window
,
96 XAtom::blackbox_attributes
, XAtom::blackbox_attributes
,
98 if (num
== PropBlackboxAttributesElements
)
99 if (data
[0] & AttribDecoration
)
100 _decorated
= (data
[4] != DecorNone
);
106 void XWindow::updateNormalHints() {
111 _gravity
= NorthWestGravity
;
113 _base_x
= _base_y
= 0;
115 if (XGetWMNormalHints(_epist
->getXDisplay(), _window
, &size
, &ret
)) {
116 if (size
.flags
& PWinGravity
)
117 _gravity
= size
.win_gravity
;
118 if (size
.flags
& PBaseSize
) {
119 _base_x
= size
.base_width
;
120 _base_y
= size
.base_height
;
122 if (size
.flags
& PResizeInc
) {
123 _inc_x
= size
.width_inc
;
124 _inc_y
= size
.height_inc
;
130 void XWindow::updateWMHints() {
133 if ((hints
= XGetWMHints(_epist
->getXDisplay(), _window
)) != NULL
) {
134 _can_focus
= hints
->input
;
137 // assume a window takes input if it doesnt specify
143 void XWindow::updateState() {
145 _shaded
= _iconic
= _max_vert
= _max_horz
= false;
147 unsigned long num
= (unsigned) -1;
149 if (! _xatom
->getValue(_window
, XAtom::net_wm_state
, XAtom::atom
,
152 for (unsigned long i
= 0; i
< num
; ++i
) {
153 if (state
[i
] == _xatom
->getAtom(XAtom::net_wm_state_maximized_vert
))
155 if (state
[i
] == _xatom
->getAtom(XAtom::net_wm_state_maximized_horz
))
157 if (state
[i
] == _xatom
->getAtom(XAtom::net_wm_state_shaded
))
159 if (state
[i
] == _xatom
->getAtom(XAtom::net_wm_state_hidden
))
167 void XWindow::updateDesktop() {
168 if (! _xatom
->getValue(_window
, XAtom::net_wm_desktop
, XAtom::cardinal
,
169 static_cast<unsigned long>(_desktop
)))
174 void XWindow::updateTitle() {
178 if (! _xatom
->getValue(_window
, XAtom::net_wm_name
, XAtom::utf8
, _title
)) {
180 _xatom
->getValue(_window
, XAtom::wm_name
, XAtom::ansi
, _title
);
188 void XWindow::updateClass() {
190 _app_name
= _app_class
= "";
193 unsigned long num
= 2;
195 if (! _xatom
->getValue(_window
, XAtom::wm_class
, XAtom::ansi
, num
, v
))
198 if (num
> 0) _app_name
= v
[0];
199 if (num
> 1) _app_class
= v
[1];
203 void XWindow::processEvent(const XEvent
&e
) {
204 assert(e
.xany
.window
== _window
);
207 case ConfigureNotify
:
211 if (e
.xproperty
.atom
== XA_WM_NORMAL_HINTS
)
213 else if (e
.xproperty
.atom
== XA_WM_HINTS
)
215 else if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::blackbox_attributes
))
216 updateBlackboxAttributes();
217 else if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::net_wm_state
))
219 else if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::net_wm_desktop
))
221 else if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::net_wm_name
) ||
222 e
.xproperty
.atom
== _xatom
->getAtom(XAtom::wm_name
))
224 else if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::wm_class
))
235 void XWindow::shade(const bool sh
) const {
236 _xatom
->sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
237 _window
, (sh
? 1 : 0),
238 _xatom
->getAtom(XAtom::net_wm_state_shaded
));
242 void XWindow::close() const {
243 _xatom
->sendClientMessage(_screen
->rootWindow(), XAtom::net_close_window
,
248 void XWindow::raise() const {
249 XRaiseWindow(_epist
->getXDisplay(), _window
);
253 void XWindow::lower() const {
254 XLowerWindow(_epist
->getXDisplay(), _window
);
258 void XWindow::iconify() const {
259 _xatom
->sendClientMessage(_screen
->rootWindow(), XAtom::wm_change_state
,
260 _window
, IconicState
);
264 void XWindow::focus(bool raise
) const {
265 // this will cause the window to be uniconified also
268 _xatom
->sendClientMessage(_screen
->rootWindow(), XAtom::net_active_window
,
271 XSetInputFocus(_epist
->getXDisplay(), _window
, None
, CurrentTime
);
276 void XWindow::sendTo(unsigned int dest
) const {
277 _xatom
->sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_desktop
,
282 void XWindow::move(int x
, int y
) const {
283 // get the window's decoration sizes (margins)
285 Window win
= _window
, parent
, root
, last
= None
;
286 Window
*children
= 0;
287 unsigned int nchildren
;
288 XWindowAttributes wattr
;
290 while (XQueryTree(_epist
->getXDisplay(), win
, &root
, &parent
, &children
,
292 if (children
&& nchildren
> 0)
293 XFree(children
); // don't care about the children
295 if (! parent
) // no parent!?
298 // if the parent window is the root window, stop here
306 if (! (XTranslateCoordinates(_epist
->getXDisplay(), last
, win
, 0, 0,
307 (int *) &margins
.left
,
308 (int *) &margins
.top
,
310 XGetWindowAttributes(_epist
->getXDisplay(), win
, &wattr
)))
313 margins
.right
= wattr
.width
- _rect
.width() - margins
.left
;
314 margins
.bottom
= wattr
.height
- _rect
.height() - margins
.top
;
316 margins
.left
+= wattr
.border_width
;
317 margins
.right
+= wattr
.border_width
;
318 margins
.top
+= wattr
.border_width
;
319 margins
.bottom
+= wattr
.border_width
;
321 // this makes things work. why? i don't know. but you need them.
325 // find the frame's reference position based on the window's gravity
327 case NorthWestGravity
:
332 x
+= (margins
.left
+ margins
.right
) / 2;
335 case NorthEastGravity
:
340 y
+= (margins
.top
+ margins
.bottom
) / 2;
343 x
+= (margins
.left
+ margins
.right
) / 2;
344 y
+= (margins
.top
+ margins
.bottom
) / 2;
348 y
+= (margins
.top
+ margins
.bottom
) / 2;
349 case SouthWestGravity
:
354 x
+= (margins
.left
+ margins
.right
) / 2;
357 case SouthEastGravity
:
365 XMoveWindow(_epist
->getXDisplay(), _window
, x
, y
);
369 void XWindow::resizeRel(int dwidth
, int dheight
) const {
370 // resize in increments if requested by the window
371 unsigned int width
= _rect
.width(), height
= _rect
.height();
373 unsigned int wdest
= width
+ (dwidth
* _inc_x
) / _inc_x
* _inc_x
+ _base_x
;
374 unsigned int hdest
= height
+ (dheight
* _inc_y
) / _inc_y
* _inc_y
+ _base_y
;
376 XResizeWindow(_epist
->getXDisplay(), _window
, wdest
, hdest
);
380 void XWindow::resizeAbs(unsigned int width
, unsigned int height
) const {
381 // resize in increments if requested by the window
383 unsigned int wdest
= width
/ _inc_x
* _inc_x
+ _base_x
;
384 unsigned int hdest
= height
/ _inc_y
* _inc_y
+ _base_y
;
387 while (width
> wdest
)
390 while (width
< wdest
)
393 if (height
> hdest
) {
394 while (height
> hdest
)
397 while (height
< hdest
)
401 XResizeWindow(_epist
->getXDisplay(), _window
, wdest
, hdest
);
405 void XWindow::toggleMaximize(Max max
) const {
409 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
410 _window
, (_max_vert
== _max_horz
? 2 : 1),
411 _xatom
->getAtom(XAtom::net_wm_state_maximized_horz
),
412 _xatom
->getAtom(XAtom::net_wm_state_maximized_vert
));
417 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
419 _xatom
->getAtom(XAtom::net_wm_state_maximized_horz
));
424 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
426 _xatom
->getAtom(XAtom::net_wm_state_maximized_vert
));
430 assert(false); // you should not do this. it is pointless and probly a bug
436 void XWindow::maximize(Max max
) const {
440 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
442 _xatom
->getAtom(XAtom::net_wm_state_maximized_horz
),
443 _xatom
->getAtom(XAtom::net_wm_state_maximized_vert
));
448 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
450 _xatom
->getAtom(XAtom::net_wm_state_maximized_horz
),
451 _xatom
->getAtom(XAtom::net_wm_state_maximized_vert
));
456 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
458 _xatom
->getAtom(XAtom::net_wm_state_maximized_horz
));
463 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
465 _xatom
->getAtom(XAtom::net_wm_state_maximized_vert
));
471 void XWindow::decorate(bool d
) const {
472 _xatom
->sendClientMessage(_screen
->rootWindow(),
473 XAtom::blackbox_change_attributes
,
474 _window
, AttribDecoration
,
475 0, 0, 0, (d
? DecorNormal
: DecorNone
));