]> icculus.org git repositories - mikachu/openbox.git/blob - util/epist/window.cc
new autoconf shit woowoo
[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   // assume a window takes input if it doesnt specify
134   _can_focus = True;
135   
136   if ((hints = XGetWMHints(_epist->getXDisplay(), _window)) != NULL) {
137     if (hints->flags & InputHint)
138       _can_focus = hints->input;
139     XFree(hints);
140   }
141 }
142
143
144 void XWindow::updateState() {
145   // set the defaults
146   _shaded = _iconic = _max_vert = _max_horz = false;
147   
148   unsigned long num = (unsigned) -1;
149   Atom *state;
150   if (! _xatom->getValue(_window, XAtom::net_wm_state, XAtom::atom,
151                          num, &state))
152     return;
153   for (unsigned long i = 0; i < num; ++i) {
154     if (state[i] == _xatom->getAtom(XAtom::net_wm_state_maximized_vert))
155       _max_vert = true;
156     if (state[i] == _xatom->getAtom(XAtom::net_wm_state_maximized_horz))
157       _max_horz = true;
158     if (state[i] == _xatom->getAtom(XAtom::net_wm_state_shaded))
159       _shaded = true;
160     if (state[i] == _xatom->getAtom(XAtom::net_wm_state_hidden))
161       _iconic = true;
162   }
163
164   delete [] state;
165 }
166
167
168 void XWindow::updateDesktop() {
169   if (! _xatom->getValue(_window, XAtom::net_wm_desktop, XAtom::cardinal,
170                          static_cast<unsigned long>(_desktop)))
171     _desktop = 0;
172 }
173
174
175 void XWindow::updateTitle() {
176   _title = "";
177   
178   // try netwm
179   if (! _xatom->getValue(_window, XAtom::net_wm_name, XAtom::utf8, _title)) {
180     // try old x stuff
181     _xatom->getValue(_window, XAtom::wm_name, XAtom::ansi, _title);
182   }
183
184   if (_title.empty())
185     _title = "Unnamed";
186 }
187
188
189 void XWindow::updateClass() {
190   // set the defaults
191   _app_name = _app_class = "";
192
193   XAtom::StringVect v;
194   unsigned long num = 2;
195
196   if (! _xatom->getValue(_window, XAtom::wm_class, XAtom::ansi, num, v))
197     return;
198
199   if (num > 0) _app_name = v[0];
200   if (num > 1) _app_class = v[1];
201 }
202
203
204 void XWindow::processEvent(const XEvent &e) {
205   assert(e.xany.window == _window);
206
207   switch (e.type) {
208   case ConfigureNotify:
209     updateDimentions();
210     break;
211   case PropertyNotify:
212     if (e.xproperty.atom == XA_WM_NORMAL_HINTS)
213       updateNormalHints();
214     else if (e.xproperty.atom == XA_WM_HINTS)
215       updateWMHints();
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))
219       updateState();
220     else if (e.xproperty.atom == _xatom->getAtom(XAtom::net_wm_desktop))
221       updateDesktop();
222     else if (e.xproperty.atom == _xatom->getAtom(XAtom::net_wm_name) ||
223              e.xproperty.atom == _xatom->getAtom(XAtom::wm_name))
224       updateTitle();
225     else if (e.xproperty.atom == _xatom->getAtom(XAtom::wm_class))
226       updateClass();
227     break;
228   case DestroyNotify:
229   case UnmapNotify:
230     _unmapped = true;
231     break;
232   }
233 }
234
235
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));
240 }
241
242
243 void XWindow::close() const {
244   _xatom->sendClientMessage(_screen->rootWindow(), XAtom::net_close_window,
245                             _window);
246 }
247
248
249 void XWindow::raise() const {
250   XRaiseWindow(_epist->getXDisplay(), _window);
251 }
252
253
254 void XWindow::lower() const {
255   XLowerWindow(_epist->getXDisplay(), _window);
256 }
257
258
259 void XWindow::iconify() const {
260   _xatom->sendClientMessage(_screen->rootWindow(), XAtom::wm_change_state,
261                             _window, IconicState);
262 }
263
264
265 void XWindow::focus(bool raise) const {
266   // this will cause the window to be uniconified also
267
268   if (raise) {
269     _xatom->sendClientMessage(_screen->rootWindow(), XAtom::net_active_window,
270                               _window);
271   } else {
272     XSetInputFocus(_epist->getXDisplay(), _window, None, CurrentTime);
273   }
274 }
275
276
277 void XWindow::sendTo(unsigned int dest) const {
278   _xatom->sendClientMessage(_screen->rootWindow(), XAtom::net_wm_desktop,
279                             _window, dest);
280 }
281
282
283 void XWindow::move(int x, int y) const {
284   // get the window's decoration sizes (margins)
285   Strut margins;
286   Window win = _window, parent, root, last = None;
287   Window *children = 0;
288   unsigned int nchildren;
289   XWindowAttributes wattr;
290   
291   while (XQueryTree(_epist->getXDisplay(), win, &root, &parent, &children,
292                     &nchildren)) {
293     if (children && nchildren > 0)
294       XFree(children); // don't care about the children
295
296     if (! parent) // no parent!?
297       return;
298
299     // if the parent window is the root window, stop here
300     if (parent == root)
301       break;
302
303     last = win;
304     win = parent;
305   }
306
307   if (! (XTranslateCoordinates(_epist->getXDisplay(), last, win, 0, 0,
308                                (int *) &margins.left,
309                                (int *) &margins.top,
310                                &parent) &&
311          XGetWindowAttributes(_epist->getXDisplay(), win, &wattr)))
312     return;
313
314   margins.right = wattr.width - _rect.width() - margins.left;
315   margins.bottom = wattr.height - _rect.height() - margins.top;
316
317   margins.left += wattr.border_width;
318   margins.right += wattr.border_width;
319   margins.top += wattr.border_width;
320   margins.bottom += wattr.border_width;
321
322   // this makes things work. why? i don't know. but you need them.
323   margins.right -= 2;
324   margins.bottom -= 2;
325   
326   // find the frame's reference position based on the window's gravity
327   switch (_gravity) {
328   case NorthWestGravity:
329     x -= margins.left;
330     y -= margins.top;
331     break;
332   case NorthGravity:
333     x += (margins.left + margins.right) / 2;
334     y -= margins.top;
335     break;
336   case NorthEastGravity:
337     x += margins.right;
338     y -= margins.top;
339   case WestGravity:
340     x -= margins.left;
341     y += (margins.top + margins.bottom) / 2;
342     break;
343   case CenterGravity:
344     x += (margins.left + margins.right) / 2;
345     y += (margins.top + margins.bottom) / 2;
346     break;
347   case EastGravity:
348     x += margins.right;
349     y += (margins.top + margins.bottom) / 2;
350   case SouthWestGravity:
351     x -= margins.left;
352     y += margins.bottom;
353     break;
354   case SouthGravity:
355     x += (margins.left + margins.right) / 2;
356     y += margins.bottom;
357     break;
358   case SouthEastGravity:
359     x += margins.right;
360     y += margins.bottom;
361     break;
362   default:
363     break;
364   }
365   
366   XMoveWindow(_epist->getXDisplay(), _window, x, y);
367 }
368
369
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();
373   
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;
376
377   XResizeWindow(_epist->getXDisplay(), _window, wdest, hdest);
378 }
379
380
381 void XWindow::resizeAbs(unsigned int width, unsigned int height) const {
382   // resize in increments if requested by the window
383
384   unsigned int wdest = width / _inc_x * _inc_x + _base_x;
385   unsigned int hdest = height / _inc_y * _inc_y + _base_y;
386
387   if (width > wdest) {
388     while (width > wdest)
389       wdest += _inc_x;
390   } else {
391     while (width < wdest)
392       wdest -= _inc_x;
393   }
394   if (height > hdest) {
395     while (height > hdest)
396       hdest += _inc_y;
397   } else {
398     while (height < hdest)
399       hdest -= _inc_y;
400   }
401   
402   XResizeWindow(_epist->getXDisplay(), _window, wdest, hdest);
403 }
404
405
406 void XWindow::toggleMaximize(Max max) const {
407   switch (max) {
408   case Max_Full:
409     _xatom->
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));
414     break;
415
416   case Max_Horz:
417     _xatom->
418       sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
419                         _window, 2,
420                         _xatom->getAtom(XAtom::net_wm_state_maximized_horz));
421     break;
422
423   case Max_Vert:
424     _xatom->
425       sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
426                         _window, 2,
427                         _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
428     break;
429     
430   case Max_None:
431     assert(false);  // you should not do this. it is pointless and probly a bug
432     break;
433   }
434 }
435
436
437 void XWindow::maximize(Max max) const {
438   switch (max) {
439   case Max_None:
440     _xatom->
441       sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
442                         _window, 0,
443                         _xatom->getAtom(XAtom::net_wm_state_maximized_horz),
444                         _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
445     break;
446
447   case Max_Full:
448     _xatom->
449       sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
450                         _window, 1,
451                         _xatom->getAtom(XAtom::net_wm_state_maximized_horz),
452                         _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
453     break;
454
455   case Max_Horz:
456     _xatom->
457       sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
458                         _window, 1,
459                         _xatom->getAtom(XAtom::net_wm_state_maximized_horz));
460     break;
461
462   case Max_Vert:
463     _xatom->
464       sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
465                         _window, 1,
466                         _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
467     break;
468   }
469 }
470
471
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));
477 }