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 // assume a window takes input if it doesnt specify
136 if ((hints
= XGetWMHints(_epist
->getXDisplay(), _window
)) != NULL
) {
137 if (hints
->flags
& InputHint
)
138 _can_focus
= hints
->input
;
144 void XWindow::updateState() {
146 _shaded
= _iconic
= _max_vert
= _max_horz
= false;
148 unsigned long num
= (unsigned) -1;
150 if (! _xatom
->getValue(_window
, XAtom::net_wm_state
, XAtom::atom
,
153 for (unsigned long i
= 0; i
< num
; ++i
) {
154 if (state
[i
] == _xatom
->getAtom(XAtom::net_wm_state_maximized_vert
))
156 if (state
[i
] == _xatom
->getAtom(XAtom::net_wm_state_maximized_horz
))
158 if (state
[i
] == _xatom
->getAtom(XAtom::net_wm_state_shaded
))
160 if (state
[i
] == _xatom
->getAtom(XAtom::net_wm_state_hidden
))
168 void XWindow::updateDesktop() {
169 if (! _xatom
->getValue(_window
, XAtom::net_wm_desktop
, XAtom::cardinal
,
170 static_cast<unsigned long>(_desktop
)))
175 void XWindow::updateTitle() {
179 if (! _xatom
->getValue(_window
, XAtom::net_wm_name
, XAtom::utf8
, _title
)) {
181 _xatom
->getValue(_window
, XAtom::wm_name
, XAtom::ansi
, _title
);
189 void XWindow::updateClass() {
191 _app_name
= _app_class
= "";
194 unsigned long num
= 2;
196 if (! _xatom
->getValue(_window
, XAtom::wm_class
, XAtom::ansi
, num
, v
))
199 if (num
> 0) _app_name
= v
[0];
200 if (num
> 1) _app_class
= v
[1];
204 void XWindow::processEvent(const XEvent
&e
) {
205 assert(e
.xany
.window
== _window
);
208 case ConfigureNotify
:
212 if (e
.xproperty
.atom
== XA_WM_NORMAL_HINTS
)
214 else if (e
.xproperty
.atom
== XA_WM_HINTS
)
216 else if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::blackbox_attributes
))
217 updateBlackboxAttributes();
218 else if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::net_wm_state
))
220 else if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::net_wm_desktop
))
222 else if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::net_wm_name
) ||
223 e
.xproperty
.atom
== _xatom
->getAtom(XAtom::wm_name
))
225 else if (e
.xproperty
.atom
== _xatom
->getAtom(XAtom::wm_class
))
236 void XWindow::shade(const bool sh
) const {
237 _xatom
->sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
238 _window
, (sh
? 1 : 0),
239 _xatom
->getAtom(XAtom::net_wm_state_shaded
));
243 void XWindow::close() const {
244 _xatom
->sendClientMessage(_screen
->rootWindow(), XAtom::net_close_window
,
249 void XWindow::raise() const {
250 XRaiseWindow(_epist
->getXDisplay(), _window
);
254 void XWindow::lower() const {
255 XLowerWindow(_epist
->getXDisplay(), _window
);
259 void XWindow::iconify() const {
260 _xatom
->sendClientMessage(_screen
->rootWindow(), XAtom::wm_change_state
,
261 _window
, IconicState
);
265 void XWindow::focus(bool raise
) const {
266 // this will cause the window to be uniconified also
269 _xatom
->sendClientMessage(_screen
->rootWindow(), XAtom::net_active_window
,
272 XSetInputFocus(_epist
->getXDisplay(), _window
, None
, CurrentTime
);
277 void XWindow::sendTo(unsigned int dest
) const {
278 _xatom
->sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_desktop
,
283 void XWindow::move(int x
, int y
) const {
284 // get the window's decoration sizes (margins)
286 Window win
= _window
, parent
, root
, last
= None
;
287 Window
*children
= 0;
288 unsigned int nchildren
;
289 XWindowAttributes wattr
;
291 while (XQueryTree(_epist
->getXDisplay(), win
, &root
, &parent
, &children
,
293 if (children
&& nchildren
> 0)
294 XFree(children
); // don't care about the children
296 if (! parent
) // no parent!?
299 // if the parent window is the root window, stop here
307 if (! (XTranslateCoordinates(_epist
->getXDisplay(), last
, win
, 0, 0,
308 (int *) &margins
.left
,
309 (int *) &margins
.top
,
311 XGetWindowAttributes(_epist
->getXDisplay(), win
, &wattr
)))
314 margins
.right
= wattr
.width
- _rect
.width() - margins
.left
;
315 margins
.bottom
= wattr
.height
- _rect
.height() - margins
.top
;
317 margins
.left
+= wattr
.border_width
;
318 margins
.right
+= wattr
.border_width
;
319 margins
.top
+= wattr
.border_width
;
320 margins
.bottom
+= wattr
.border_width
;
322 // this makes things work. why? i don't know. but you need them.
326 // find the frame's reference position based on the window's gravity
328 case NorthWestGravity
:
333 x
+= (margins
.left
+ margins
.right
) / 2;
336 case NorthEastGravity
:
341 y
+= (margins
.top
+ margins
.bottom
) / 2;
344 x
+= (margins
.left
+ margins
.right
) / 2;
345 y
+= (margins
.top
+ margins
.bottom
) / 2;
349 y
+= (margins
.top
+ margins
.bottom
) / 2;
350 case SouthWestGravity
:
355 x
+= (margins
.left
+ margins
.right
) / 2;
358 case SouthEastGravity
:
366 XMoveWindow(_epist
->getXDisplay(), _window
, x
, y
);
370 void XWindow::resizeRel(int dwidth
, int dheight
) const {
371 // resize in increments if requested by the window
372 unsigned int width
= _rect
.width(), height
= _rect
.height();
374 unsigned int wdest
= width
+ (dwidth
* _inc_x
) / _inc_x
* _inc_x
+ _base_x
;
375 unsigned int hdest
= height
+ (dheight
* _inc_y
) / _inc_y
* _inc_y
+ _base_y
;
377 XResizeWindow(_epist
->getXDisplay(), _window
, wdest
, hdest
);
381 void XWindow::resizeAbs(unsigned int width
, unsigned int height
) const {
382 // resize in increments if requested by the window
384 unsigned int wdest
= width
/ _inc_x
* _inc_x
+ _base_x
;
385 unsigned int hdest
= height
/ _inc_y
* _inc_y
+ _base_y
;
388 while (width
> wdest
)
391 while (width
< wdest
)
394 if (height
> hdest
) {
395 while (height
> hdest
)
398 while (height
< hdest
)
402 XResizeWindow(_epist
->getXDisplay(), _window
, wdest
, hdest
);
406 void XWindow::toggleMaximize(Max max
) const {
410 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
411 _window
, (_max_vert
== _max_horz
? 2 : 1),
412 _xatom
->getAtom(XAtom::net_wm_state_maximized_horz
),
413 _xatom
->getAtom(XAtom::net_wm_state_maximized_vert
));
418 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
420 _xatom
->getAtom(XAtom::net_wm_state_maximized_horz
));
425 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
427 _xatom
->getAtom(XAtom::net_wm_state_maximized_vert
));
431 assert(false); // you should not do this. it is pointless and probly a bug
437 void XWindow::maximize(Max max
) const {
441 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
443 _xatom
->getAtom(XAtom::net_wm_state_maximized_horz
),
444 _xatom
->getAtom(XAtom::net_wm_state_maximized_vert
));
449 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
451 _xatom
->getAtom(XAtom::net_wm_state_maximized_horz
),
452 _xatom
->getAtom(XAtom::net_wm_state_maximized_vert
));
457 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
459 _xatom
->getAtom(XAtom::net_wm_state_maximized_horz
));
464 sendClientMessage(_screen
->rootWindow(), XAtom::net_wm_state
,
466 _xatom
->getAtom(XAtom::net_wm_state_maximized_vert
));
472 void XWindow::decorate(bool d
) const {
473 _xatom
->sendClientMessage(_screen
->rootWindow(),
474 XAtom::blackbox_change_attributes
,
475 _window
, AttribDecoration
,
476 0, 0, 0, (d
? DecorNormal
: DecorNone
));