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