]> icculus.org git repositories - mikachu/openbox.git/blob - util/epist/window.cc
keep track of window dimentions
[mikachu/openbox.git] / util / epist / window.cc
1 // -*- mode: C++; indent-tabs-mode: nil; -*-
2 // window.cc for Epistophy - 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   updateDimentions();
48   updateGravity();
49   updateState();
50   updateDesktop();
51   updateTitle();
52   updateClass();
53
54   _epist->addWindow(this);
55 }
56
57
58 XWindow::~XWindow() {
59   if (! _unmapped)
60     XSelectInput(_epist->getXDisplay(), _window, None);
61   _epist->removeWindow(this);
62 }
63
64
65 void XWindow::updateDimentions() {
66   Window root, child;
67   int x, y;
68   unsigned int w, h, b, d;
69
70   if (XGetGeometry(_epist->getXDisplay(), _window, &root, &x, &y, &w, &h,
71                      &b, &d) &&
72       XTranslateCoordinates(_epist->getXDisplay(), _window, root, x, y,
73                             &x, &y, &child))
74     _rect.setRect(x, y, w, h);
75   else
76     _rect.setRect(0, 0, 1, 1);
77 }
78
79
80 void XWindow::updateGravity() {
81   XSizeHints size;
82   long ret;
83
84   if (XGetWMNormalHints(_epist->getXDisplay(), _window, &size, &ret) &&
85       (size.flags & PWinGravity))
86     _gravity = size.win_gravity;
87   else
88     _gravity = NorthWestGravity;
89 }
90
91
92 void XWindow::updateState() {
93   // set the defaults
94   _shaded = _iconic = _max_vert = _max_horz = false;
95   
96   unsigned long num = (unsigned) -1;
97   Atom *state;
98   if (! _xatom->getValue(_window, XAtom::net_wm_state, XAtom::atom,
99                          num, &state))
100     return;
101   for (unsigned long i = 0; i < num; ++i) {
102     if (state[i] == _xatom->getAtom(XAtom::net_wm_state_maximized_vert))
103       _max_vert = true;
104     if (state[i] == _xatom->getAtom(XAtom::net_wm_state_maximized_horz))
105       _max_horz = true;
106     if (state[i] == _xatom->getAtom(XAtom::net_wm_state_shaded))
107       _shaded = true;
108     if (state[i] == _xatom->getAtom(XAtom::net_wm_state_hidden))
109       _iconic = true;
110   }
111
112   delete [] state;
113 }
114
115
116 void XWindow::updateDesktop() {
117   if (! _xatom->getValue(_window, XAtom::net_wm_desktop, XAtom::cardinal,
118                          static_cast<unsigned long>(_desktop)))
119     _desktop = 0;
120 }
121
122
123 void XWindow::updateTitle() {
124   _title = "";
125   
126   // try netwm
127   if (! _xatom->getValue(_window, XAtom::net_wm_name, XAtom::utf8, _title)) {
128     // try old x stuff
129     _xatom->getValue(_window, XAtom::wm_name, XAtom::ansi, _title);
130   }
131
132   if (_title.empty())
133     _title = "Unnamed";
134 }
135
136
137 void XWindow::updateClass() {
138   // set the defaults
139   _app_name = _app_class = "";
140
141   XAtom::StringVect v;
142   unsigned long num = 2;
143
144   if (! _xatom->getValue(_window, XAtom::wm_class, XAtom::ansi, num, v))
145     return;
146
147   if (num > 0) _app_name = v[0];
148   if (num > 1) _app_class = v[1];
149 }
150
151
152 void XWindow::processEvent(const XEvent &e) {
153   assert(e.xany.window == _window);
154
155   switch (e.type) {
156   case ConfigureNotify:
157     updateDimentions();
158     break;
159   case PropertyNotify:
160     if (e.xproperty.atom == XA_WM_NORMAL_HINTS)
161       updateGravity();
162     else if (e.xproperty.atom == _xatom->getAtom(XAtom::net_wm_state))
163       updateState();
164     else if (e.xproperty.atom == _xatom->getAtom(XAtom::net_wm_desktop))
165       updateDesktop();
166     else if (e.xproperty.atom == _xatom->getAtom(XAtom::net_wm_name) ||
167              e.xproperty.atom == _xatom->getAtom(XAtom::wm_name))
168       updateTitle();
169     else if (e.xproperty.atom == _xatom->getAtom(XAtom::wm_class))
170       updateClass();
171     break;
172   case DestroyNotify:
173   case UnmapNotify:
174     _unmapped = true;
175     break;
176   }
177 }
178
179
180 void XWindow::shade(const bool sh) const {
181   _xatom->sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
182                             _window, (sh ? 1 : 0),
183                             _xatom->getAtom(XAtom::net_wm_state_shaded));
184 }
185
186
187 void XWindow::close() const {
188   _xatom->sendClientMessage(_screen->rootWindow(), XAtom::net_close_window,
189                             _window);
190 }
191
192
193 void XWindow::raise() const {
194   XRaiseWindow(_epist->getXDisplay(), _window);
195 }
196
197
198 void XWindow::lower() const {
199   XLowerWindow(_epist->getXDisplay(), _window);
200 }
201
202
203 void XWindow::iconify() const {
204   _xatom->sendClientMessage(_screen->rootWindow(), XAtom::wm_change_state,
205                             _window, IconicState);
206 }
207
208
209 void XWindow::focus() const {
210   // this will also unshade the window..
211   _xatom->sendClientMessage(_screen->rootWindow(), XAtom::net_active_window,
212                             _window);
213 }
214
215
216 void XWindow::sendTo(unsigned int dest) const {
217   _xatom->sendClientMessage(_screen->rootWindow(), XAtom::net_wm_desktop,
218                             _window, dest);
219 }
220
221
222 void XWindow::move(int x, int y) const {
223   // get the window's decoration sizes (margins)
224   Strut margins;
225   Window win = _window, parent, root, last = None;
226   Window *children = 0;
227   unsigned int nchildren;
228   XWindowAttributes wattr;
229   
230   while (XQueryTree(_epist->getXDisplay(), win, &root, &parent, &children,
231                     &nchildren)) {
232     if (children && nchildren > 0)
233       XFree(children); // don't care about the children
234
235     if (! parent) // no parent!?
236       return;
237
238     // if the parent window is the root window, stop here
239     if (parent == root)
240       break;
241
242     last = win;
243     win = parent;
244   }
245
246   if (! (XTranslateCoordinates(_epist->getXDisplay(), last, win, 0, 0,
247                                (int *) &margins.left,
248                                (int *) &margins.top,
249                                &parent) &&
250          XGetWindowAttributes(_epist->getXDisplay(), win, &wattr)))
251     return;
252
253   margins.right = wattr.width - _rect.width() - margins.left;
254   margins.bottom = wattr.height - _rect.height() - margins.top;
255
256   margins.left += wattr.border_width;
257   margins.right += wattr.border_width;
258   margins.top += wattr.border_width;
259   margins.bottom += wattr.border_width;
260
261   // this makes things work. why? i don't know. but you need them.
262   margins.right -= 2;
263   margins.bottom -= 2;
264   
265   // find the frame's reference position based on the window's gravity
266   switch (_gravity) {
267   case NorthWestGravity:
268     x -= margins.left;
269     y -= margins.top;
270     break;
271   case NorthGravity:
272     x += (margins.left + margins.right) / 2;
273     y -= margins.top;
274     break;
275   case NorthEastGravity:
276     x += margins.right;
277     y -= margins.top;
278   case WestGravity:
279     x -= margins.left;
280     y += (margins.top + margins.bottom) / 2;
281     break;
282   case CenterGravity:
283     x += (margins.left + margins.right) / 2;
284     y += (margins.top + margins.bottom) / 2;
285     break;
286   case EastGravity:
287     x += margins.right;
288     y += (margins.top + margins.bottom) / 2;
289   case SouthWestGravity:
290     x -= margins.left;
291     y += margins.bottom;
292     break;
293   case SouthGravity:
294     x += (margins.left + margins.right) / 2;
295     y += margins.bottom;
296     break;
297   case SouthEastGravity:
298     x += margins.right;
299     y += margins.bottom;
300     break;
301   default:
302     break;
303   }
304   
305   XMoveWindow(_epist->getXDisplay(), _window, x, y);
306 }
307
308
309 void XWindow::resize(unsigned int width, unsigned int height) const {
310   XResizeWindow(_epist->getXDisplay(), _window, width, height);
311 }
312
313
314 void XWindow::toggleMaximize(Max max) const {
315   switch (max) {
316   case Max_Full:
317     _xatom->
318       sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
319                         _window, (_max_vert == _max_horz ? 2 : 1),
320                         _xatom->getAtom(XAtom::net_wm_state_maximized_horz),
321                         _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
322     break;
323
324   case Max_Horz:
325     _xatom->
326       sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
327                         _window, 2,
328                         _xatom->getAtom(XAtom::net_wm_state_maximized_horz));
329     break;
330
331   case Max_Vert:
332     _xatom->
333       sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
334                         _window, 2,
335                         _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
336     break;
337     
338   case Max_None:
339     assert(false);  // you should not do this. it is pointless and probly a bug
340     break;
341   }
342 }
343
344
345 void XWindow::maximize(Max max) const {
346   switch (max) {
347   case Max_None:
348     _xatom->
349       sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
350                         _window, 0,
351                         _xatom->getAtom(XAtom::net_wm_state_maximized_horz),
352                         _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
353     break;
354
355   case Max_Full:
356     _xatom->
357       sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
358                         _window, 1,
359                         _xatom->getAtom(XAtom::net_wm_state_maximized_horz),
360                         _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
361     break;
362
363   case Max_Horz:
364     _xatom->
365       sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
366                         _window, 1,
367                         _xatom->getAtom(XAtom::net_wm_state_maximized_horz));
368     break;
369
370   case Max_Vert:
371     _xatom->
372       sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
373                         _window, 1,
374                         _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
375     break;
376   }
377 }