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