]> Dogcows Code - chaz/openbox/blob - util/epist/window.cc
fixed getStringValue failure case and compiler warnings
[chaz/openbox] / util / epist / window.cc
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>
4 //
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:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
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.
22
23 #ifdef HAVE_CONFIG_H
24 # include "../../config.h"
25 #endif // HAVE_CONFIG_H
26
27 #include <iostream>
28
29 using std::cout;
30 using std::endl;
31 using std::hex;
32 using std::dec;
33
34 #include "epist.hh"
35 #include "screen.hh"
36 #include "window.hh"
37 #include "../../src/XAtom.hh"
38
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;
44
45
46 XWindow::XWindow(epist *epist, screen *screen, Window window)
47 : _epist(epist), _screen(screen), _xatom(epist->xatom()), _window(window) {
48
49 _unmapped = false;
50
51 XSelectInput(_epist->getXDisplay(), _window,
52 PropertyChangeMask | StructureNotifyMask);
53
54 updateBlackboxAttributes();
55 updateNormalHints();
56 updateWMHints();
57 updateDimentions();
58 updateState();
59 updateDesktop();
60 updateTitle();
61 updateClass();
62
63 _epist->addWindow(this);
64 }
65
66
67 XWindow::~XWindow() {
68 if (! _unmapped)
69 XSelectInput(_epist->getXDisplay(), _window, None);
70 _epist->removeWindow(this);
71 }
72
73
74 void XWindow::updateDimentions() {
75 Window root, child;
76 int x, y;
77 unsigned int w, h, b, d;
78
79 if (XGetGeometry(_epist->getXDisplay(), _window, &root, &x, &y, &w, &h,
80 &b, &d) &&
81 XTranslateCoordinates(_epist->getXDisplay(), _window, root, x, y,
82 &x, &y, &child))
83 _rect.setRect(x, y, w, h);
84 else
85 _rect.setRect(0, 0, 1, 1);
86 }
87
88
89 void XWindow::updateBlackboxAttributes() {
90 unsigned long *data;
91 unsigned long num = PropBlackboxAttributesElements;
92
93 _decorated = true;
94
95 if (_xatom->getValue(_window,
96 XAtom::blackbox_attributes, XAtom::blackbox_attributes,
97 num, &data)) {
98 if (num == PropBlackboxAttributesElements)
99 if (data[0] & AttribDecoration)
100 _decorated = (data[4] != DecorNone);
101 delete data;
102 }
103 }
104
105
106 void XWindow::updateNormalHints() {
107 XSizeHints size;
108 long ret;
109
110 // defaults
111 _gravity = NorthWestGravity;
112 _inc_x = _inc_y = 1;
113 _base_x = _base_y = 0;
114
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;
121 }
122 if (size.flags & PResizeInc) {
123 _inc_x = size.width_inc;
124 _inc_y = size.height_inc;
125 }
126 }
127 }
128
129
130 void XWindow::updateWMHints() {
131 XWMHints *hints;
132
133 if ((hints = XGetWMHints(_epist->getXDisplay(), _window)) != NULL) {
134 _can_focus = hints->input;
135 XFree(hints);
136 } else {
137 // assume a window takes input if it doesnt specify
138 _can_focus = True;
139 }
140 }
141
142
143 void XWindow::updateState() {
144 // set the defaults
145 _shaded = _iconic = _max_vert = _max_horz = false;
146
147 unsigned long num = (unsigned) -1;
148 Atom *state;
149 if (! _xatom->getValue(_window, XAtom::net_wm_state, XAtom::atom,
150 num, &state))
151 return;
152 for (unsigned long i = 0; i < num; ++i) {
153 if (state[i] == _xatom->getAtom(XAtom::net_wm_state_maximized_vert))
154 _max_vert = true;
155 if (state[i] == _xatom->getAtom(XAtom::net_wm_state_maximized_horz))
156 _max_horz = true;
157 if (state[i] == _xatom->getAtom(XAtom::net_wm_state_shaded))
158 _shaded = true;
159 if (state[i] == _xatom->getAtom(XAtom::net_wm_state_hidden))
160 _iconic = true;
161 }
162
163 delete [] state;
164 }
165
166
167 void XWindow::updateDesktop() {
168 if (! _xatom->getValue(_window, XAtom::net_wm_desktop, XAtom::cardinal,
169 static_cast<unsigned long>(_desktop)))
170 _desktop = 0;
171 }
172
173
174 void XWindow::updateTitle() {
175 _title = "";
176
177 // try netwm
178 if (! _xatom->getValue(_window, XAtom::net_wm_name, XAtom::utf8, _title)) {
179 // try old x stuff
180 _xatom->getValue(_window, XAtom::wm_name, XAtom::ansi, _title);
181 }
182
183 if (_title.empty())
184 _title = "Unnamed";
185 }
186
187
188 void XWindow::updateClass() {
189 // set the defaults
190 _app_name = _app_class = "";
191
192 XAtom::StringVect v;
193 unsigned long num = 2;
194
195 if (! _xatom->getValue(_window, XAtom::wm_class, XAtom::ansi, num, v))
196 return;
197
198 if (num > 0) _app_name = v[0];
199 if (num > 1) _app_class = v[1];
200 }
201
202
203 void XWindow::processEvent(const XEvent &e) {
204 assert(e.xany.window == _window);
205
206 switch (e.type) {
207 case ConfigureNotify:
208 updateDimentions();
209 break;
210 case PropertyNotify:
211 if (e.xproperty.atom == XA_WM_NORMAL_HINTS)
212 updateNormalHints();
213 else if (e.xproperty.atom == XA_WM_HINTS)
214 updateWMHints();
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))
218 updateState();
219 else if (e.xproperty.atom == _xatom->getAtom(XAtom::net_wm_desktop))
220 updateDesktop();
221 else if (e.xproperty.atom == _xatom->getAtom(XAtom::net_wm_name) ||
222 e.xproperty.atom == _xatom->getAtom(XAtom::wm_name))
223 updateTitle();
224 else if (e.xproperty.atom == _xatom->getAtom(XAtom::wm_class))
225 updateClass();
226 break;
227 case DestroyNotify:
228 case UnmapNotify:
229 _unmapped = true;
230 break;
231 }
232 }
233
234
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));
239 }
240
241
242 void XWindow::close() const {
243 _xatom->sendClientMessage(_screen->rootWindow(), XAtom::net_close_window,
244 _window);
245 }
246
247
248 void XWindow::raise() const {
249 XRaiseWindow(_epist->getXDisplay(), _window);
250 }
251
252
253 void XWindow::lower() const {
254 XLowerWindow(_epist->getXDisplay(), _window);
255 }
256
257
258 void XWindow::iconify() const {
259 _xatom->sendClientMessage(_screen->rootWindow(), XAtom::wm_change_state,
260 _window, IconicState);
261 }
262
263
264 void XWindow::focus() const {
265 // this will cause the window to be uniconified also
266 _xatom->sendClientMessage(_screen->rootWindow(), XAtom::net_active_window,
267 _window);
268
269 //XSetInputFocus(_epist->getXDisplay(), _window, None, CurrentTime);
270 }
271
272
273 void XWindow::sendTo(unsigned int dest) const {
274 _xatom->sendClientMessage(_screen->rootWindow(), XAtom::net_wm_desktop,
275 _window, dest);
276 }
277
278
279 void XWindow::move(int x, int y) const {
280 // get the window's decoration sizes (margins)
281 Strut margins;
282 Window win = _window, parent, root, last = None;
283 Window *children = 0;
284 unsigned int nchildren;
285 XWindowAttributes wattr;
286
287 while (XQueryTree(_epist->getXDisplay(), win, &root, &parent, &children,
288 &nchildren)) {
289 if (children && nchildren > 0)
290 XFree(children); // don't care about the children
291
292 if (! parent) // no parent!?
293 return;
294
295 // if the parent window is the root window, stop here
296 if (parent == root)
297 break;
298
299 last = win;
300 win = parent;
301 }
302
303 if (! (XTranslateCoordinates(_epist->getXDisplay(), last, win, 0, 0,
304 (int *) &margins.left,
305 (int *) &margins.top,
306 &parent) &&
307 XGetWindowAttributes(_epist->getXDisplay(), win, &wattr)))
308 return;
309
310 margins.right = wattr.width - _rect.width() - margins.left;
311 margins.bottom = wattr.height - _rect.height() - margins.top;
312
313 margins.left += wattr.border_width;
314 margins.right += wattr.border_width;
315 margins.top += wattr.border_width;
316 margins.bottom += wattr.border_width;
317
318 // this makes things work. why? i don't know. but you need them.
319 margins.right -= 2;
320 margins.bottom -= 2;
321
322 // find the frame's reference position based on the window's gravity
323 switch (_gravity) {
324 case NorthWestGravity:
325 x -= margins.left;
326 y -= margins.top;
327 break;
328 case NorthGravity:
329 x += (margins.left + margins.right) / 2;
330 y -= margins.top;
331 break;
332 case NorthEastGravity:
333 x += margins.right;
334 y -= margins.top;
335 case WestGravity:
336 x -= margins.left;
337 y += (margins.top + margins.bottom) / 2;
338 break;
339 case CenterGravity:
340 x += (margins.left + margins.right) / 2;
341 y += (margins.top + margins.bottom) / 2;
342 break;
343 case EastGravity:
344 x += margins.right;
345 y += (margins.top + margins.bottom) / 2;
346 case SouthWestGravity:
347 x -= margins.left;
348 y += margins.bottom;
349 break;
350 case SouthGravity:
351 x += (margins.left + margins.right) / 2;
352 y += margins.bottom;
353 break;
354 case SouthEastGravity:
355 x += margins.right;
356 y += margins.bottom;
357 break;
358 default:
359 break;
360 }
361
362 XMoveWindow(_epist->getXDisplay(), _window, x, y);
363 }
364
365
366 void XWindow::resizeRel(int dwidth, int dheight) const {
367 // resize in increments if requested by the window
368 unsigned int width = _rect.width(), height = _rect.height();
369
370 unsigned int wdest = width + (dwidth * _inc_x) / _inc_x * _inc_x + _base_x;
371 unsigned int hdest = height + (dheight * _inc_y) / _inc_y * _inc_y + _base_y;
372
373 XResizeWindow(_epist->getXDisplay(), _window, wdest, hdest);
374 }
375
376
377 void XWindow::resizeAbs(unsigned int width, unsigned int height) const {
378 // resize in increments if requested by the window
379
380 unsigned int wdest = width / _inc_x * _inc_x + _base_x;
381 unsigned int hdest = height / _inc_y * _inc_y + _base_y;
382
383 if (width > wdest) {
384 while (width > wdest)
385 wdest += _inc_x;
386 } else {
387 while (width < wdest)
388 wdest -= _inc_x;
389 }
390 if (height > hdest) {
391 while (height > hdest)
392 hdest += _inc_y;
393 } else {
394 while (height < hdest)
395 hdest -= _inc_y;
396 }
397
398 XResizeWindow(_epist->getXDisplay(), _window, wdest, hdest);
399 }
400
401
402 void XWindow::toggleMaximize(Max max) const {
403 switch (max) {
404 case Max_Full:
405 _xatom->
406 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
407 _window, (_max_vert == _max_horz ? 2 : 1),
408 _xatom->getAtom(XAtom::net_wm_state_maximized_horz),
409 _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
410 break;
411
412 case Max_Horz:
413 _xatom->
414 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
415 _window, 2,
416 _xatom->getAtom(XAtom::net_wm_state_maximized_horz));
417 break;
418
419 case Max_Vert:
420 _xatom->
421 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
422 _window, 2,
423 _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
424 break;
425
426 case Max_None:
427 assert(false); // you should not do this. it is pointless and probly a bug
428 break;
429 }
430 }
431
432
433 void XWindow::maximize(Max max) const {
434 switch (max) {
435 case Max_None:
436 _xatom->
437 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
438 _window, 0,
439 _xatom->getAtom(XAtom::net_wm_state_maximized_horz),
440 _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
441 break;
442
443 case Max_Full:
444 _xatom->
445 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
446 _window, 1,
447 _xatom->getAtom(XAtom::net_wm_state_maximized_horz),
448 _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
449 break;
450
451 case Max_Horz:
452 _xatom->
453 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
454 _window, 1,
455 _xatom->getAtom(XAtom::net_wm_state_maximized_horz));
456 break;
457
458 case Max_Vert:
459 _xatom->
460 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
461 _window, 1,
462 _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
463 break;
464 }
465 }
466
467
468 void XWindow::decorate(bool d) const {
469 _xatom->sendClientMessage(_screen->rootWindow(),
470 XAtom::blackbox_change_attributes,
471 _window, AttribDecoration,
472 0, 0, 0, (d ? DecorNormal : DecorNone));
473 }
This page took 0.061375 seconds and 4 git commands to generate.