]> icculus.org git repositories - mikachu/openbox.git/blob - util/epist/window.cc
took out some debug messages
[mikachu/openbox.git] / util / epist / window.cc
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>
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(bool raise) const {
265   // this will cause the window to be uniconified also
266
267   if (raise) {
268     _xatom->sendClientMessage(_screen->rootWindow(), XAtom::net_active_window,
269                               _window);
270   } else {
271     XSetInputFocus(_epist->getXDisplay(), _window, None, CurrentTime);
272   }
273 }
274
275
276 void XWindow::sendTo(unsigned int dest) const {
277   _xatom->sendClientMessage(_screen->rootWindow(), XAtom::net_wm_desktop,
278                             _window, dest);
279 }
280
281
282 void XWindow::move(int x, int y) const {
283   // get the window's decoration sizes (margins)
284   Strut margins;
285   Window win = _window, parent, root, last = None;
286   Window *children = 0;
287   unsigned int nchildren;
288   XWindowAttributes wattr;
289   
290   while (XQueryTree(_epist->getXDisplay(), win, &root, &parent, &children,
291                     &nchildren)) {
292     if (children && nchildren > 0)
293       XFree(children); // don't care about the children
294
295     if (! parent) // no parent!?
296       return;
297
298     // if the parent window is the root window, stop here
299     if (parent == root)
300       break;
301
302     last = win;
303     win = parent;
304   }
305
306   if (! (XTranslateCoordinates(_epist->getXDisplay(), last, win, 0, 0,
307                                (int *) &margins.left,
308                                (int *) &margins.top,
309                                &parent) &&
310          XGetWindowAttributes(_epist->getXDisplay(), win, &wattr)))
311     return;
312
313   margins.right = wattr.width - _rect.width() - margins.left;
314   margins.bottom = wattr.height - _rect.height() - margins.top;
315
316   margins.left += wattr.border_width;
317   margins.right += wattr.border_width;
318   margins.top += wattr.border_width;
319   margins.bottom += wattr.border_width;
320
321   // this makes things work. why? i don't know. but you need them.
322   margins.right -= 2;
323   margins.bottom -= 2;
324   
325   // find the frame's reference position based on the window's gravity
326   switch (_gravity) {
327   case NorthWestGravity:
328     x -= margins.left;
329     y -= margins.top;
330     break;
331   case NorthGravity:
332     x += (margins.left + margins.right) / 2;
333     y -= margins.top;
334     break;
335   case NorthEastGravity:
336     x += margins.right;
337     y -= margins.top;
338   case WestGravity:
339     x -= margins.left;
340     y += (margins.top + margins.bottom) / 2;
341     break;
342   case CenterGravity:
343     x += (margins.left + margins.right) / 2;
344     y += (margins.top + margins.bottom) / 2;
345     break;
346   case EastGravity:
347     x += margins.right;
348     y += (margins.top + margins.bottom) / 2;
349   case SouthWestGravity:
350     x -= margins.left;
351     y += margins.bottom;
352     break;
353   case SouthGravity:
354     x += (margins.left + margins.right) / 2;
355     y += margins.bottom;
356     break;
357   case SouthEastGravity:
358     x += margins.right;
359     y += margins.bottom;
360     break;
361   default:
362     break;
363   }
364   
365   XMoveWindow(_epist->getXDisplay(), _window, x, y);
366 }
367
368
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();
372   
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;
375
376   XResizeWindow(_epist->getXDisplay(), _window, wdest, hdest);
377 }
378
379
380 void XWindow::resizeAbs(unsigned int width, unsigned int height) const {
381   // resize in increments if requested by the window
382
383   unsigned int wdest = width / _inc_x * _inc_x + _base_x;
384   unsigned int hdest = height / _inc_y * _inc_y + _base_y;
385
386   if (width > wdest) {
387     while (width > wdest)
388       wdest += _inc_x;
389   } else {
390     while (width < wdest)
391       wdest -= _inc_x;
392   }
393   if (height > hdest) {
394     while (height > hdest)
395       hdest += _inc_y;
396   } else {
397     while (height < hdest)
398       hdest -= _inc_y;
399   }
400   
401   XResizeWindow(_epist->getXDisplay(), _window, wdest, hdest);
402 }
403
404
405 void XWindow::toggleMaximize(Max max) const {
406   switch (max) {
407   case Max_Full:
408     _xatom->
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));
413     break;
414
415   case Max_Horz:
416     _xatom->
417       sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
418                         _window, 2,
419                         _xatom->getAtom(XAtom::net_wm_state_maximized_horz));
420     break;
421
422   case Max_Vert:
423     _xatom->
424       sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
425                         _window, 2,
426                         _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
427     break;
428     
429   case Max_None:
430     assert(false);  // you should not do this. it is pointless and probly a bug
431     break;
432   }
433 }
434
435
436 void XWindow::maximize(Max max) const {
437   switch (max) {
438   case Max_None:
439     _xatom->
440       sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
441                         _window, 0,
442                         _xatom->getAtom(XAtom::net_wm_state_maximized_horz),
443                         _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
444     break;
445
446   case Max_Full:
447     _xatom->
448       sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
449                         _window, 1,
450                         _xatom->getAtom(XAtom::net_wm_state_maximized_horz),
451                         _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
452     break;
453
454   case Max_Horz:
455     _xatom->
456       sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
457                         _window, 1,
458                         _xatom->getAtom(XAtom::net_wm_state_maximized_horz));
459     break;
460
461   case Max_Vert:
462     _xatom->
463       sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
464                         _window, 1,
465                         _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
466     break;
467   }
468 }
469
470
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));
476 }