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