]> Dogcows Code - chaz/openbox/blob - util/epist/window.cc
fixed spelling mistake
[chaz/openbox] / 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 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 // this will cause the window to be uniconified also
239 _xatom->sendClientMessage(_screen->rootWindow(), XAtom::net_active_window,
240 _window);
241
242 //XSetInputFocus(_epist->getXDisplay(), _window, None, CurrentTime);
243 }
244
245
246 void XWindow::sendTo(unsigned int dest) const {
247 _xatom->sendClientMessage(_screen->rootWindow(), XAtom::net_wm_desktop,
248 _window, dest);
249 }
250
251
252 void XWindow::move(int x, int y) const {
253 // get the window's decoration sizes (margins)
254 Strut margins;
255 Window win = _window, parent, root, last = None;
256 Window *children = 0;
257 unsigned int nchildren;
258 XWindowAttributes wattr;
259
260 while (XQueryTree(_epist->getXDisplay(), win, &root, &parent, &children,
261 &nchildren)) {
262 if (children && nchildren > 0)
263 XFree(children); // don't care about the children
264
265 if (! parent) // no parent!?
266 return;
267
268 // if the parent window is the root window, stop here
269 if (parent == root)
270 break;
271
272 last = win;
273 win = parent;
274 }
275
276 if (! (XTranslateCoordinates(_epist->getXDisplay(), last, win, 0, 0,
277 (int *) &margins.left,
278 (int *) &margins.top,
279 &parent) &&
280 XGetWindowAttributes(_epist->getXDisplay(), win, &wattr)))
281 return;
282
283 margins.right = wattr.width - _rect.width() - margins.left;
284 margins.bottom = wattr.height - _rect.height() - margins.top;
285
286 margins.left += wattr.border_width;
287 margins.right += wattr.border_width;
288 margins.top += wattr.border_width;
289 margins.bottom += wattr.border_width;
290
291 // this makes things work. why? i don't know. but you need them.
292 margins.right -= 2;
293 margins.bottom -= 2;
294
295 // find the frame's reference position based on the window's gravity
296 switch (_gravity) {
297 case NorthWestGravity:
298 x -= margins.left;
299 y -= margins.top;
300 break;
301 case NorthGravity:
302 x += (margins.left + margins.right) / 2;
303 y -= margins.top;
304 break;
305 case NorthEastGravity:
306 x += margins.right;
307 y -= margins.top;
308 case WestGravity:
309 x -= margins.left;
310 y += (margins.top + margins.bottom) / 2;
311 break;
312 case CenterGravity:
313 x += (margins.left + margins.right) / 2;
314 y += (margins.top + margins.bottom) / 2;
315 break;
316 case EastGravity:
317 x += margins.right;
318 y += (margins.top + margins.bottom) / 2;
319 case SouthWestGravity:
320 x -= margins.left;
321 y += margins.bottom;
322 break;
323 case SouthGravity:
324 x += (margins.left + margins.right) / 2;
325 y += margins.bottom;
326 break;
327 case SouthEastGravity:
328 x += margins.right;
329 y += margins.bottom;
330 break;
331 default:
332 break;
333 }
334
335 XMoveWindow(_epist->getXDisplay(), _window, x, y);
336 }
337
338
339 void XWindow::resizeRel(int dwidth, int dheight) const {
340 // resize in increments if requested by the window
341 unsigned int width = _rect.width(), height = _rect.height();
342
343 unsigned int wdest = width + (dwidth * _inc_x) / _inc_x * _inc_x + _base_x;
344 unsigned int hdest = height + (dheight * _inc_y) / _inc_y * _inc_y + _base_y;
345
346 XResizeWindow(_epist->getXDisplay(), _window, wdest, hdest);
347 }
348
349
350 void XWindow::resizeAbs(unsigned int width, unsigned int height) const {
351 // resize in increments if requested by the window
352
353 unsigned int wdest = width / _inc_x * _inc_x + _base_x;
354 unsigned int hdest = height / _inc_y * _inc_y + _base_y;
355
356 if (width > wdest) {
357 while (width > wdest)
358 wdest += _inc_x;
359 } else {
360 while (width < wdest)
361 wdest -= _inc_x;
362 }
363 if (height > hdest) {
364 while (height > hdest)
365 hdest += _inc_y;
366 } else {
367 while (height < hdest)
368 hdest -= _inc_y;
369 }
370
371 XResizeWindow(_epist->getXDisplay(), _window, wdest, hdest);
372 }
373
374
375 void XWindow::toggleMaximize(Max max) const {
376 switch (max) {
377 case Max_Full:
378 _xatom->
379 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
380 _window, (_max_vert == _max_horz ? 2 : 1),
381 _xatom->getAtom(XAtom::net_wm_state_maximized_horz),
382 _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
383 break;
384
385 case Max_Horz:
386 _xatom->
387 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
388 _window, 2,
389 _xatom->getAtom(XAtom::net_wm_state_maximized_horz));
390 break;
391
392 case Max_Vert:
393 _xatom->
394 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
395 _window, 2,
396 _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
397 break;
398
399 case Max_None:
400 assert(false); // you should not do this. it is pointless and probly a bug
401 break;
402 }
403 }
404
405
406 void XWindow::maximize(Max max) const {
407 switch (max) {
408 case Max_None:
409 _xatom->
410 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
411 _window, 0,
412 _xatom->getAtom(XAtom::net_wm_state_maximized_horz),
413 _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
414 break;
415
416 case Max_Full:
417 _xatom->
418 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
419 _window, 1,
420 _xatom->getAtom(XAtom::net_wm_state_maximized_horz),
421 _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
422 break;
423
424 case Max_Horz:
425 _xatom->
426 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
427 _window, 1,
428 _xatom->getAtom(XAtom::net_wm_state_maximized_horz));
429 break;
430
431 case Max_Vert:
432 _xatom->
433 sendClientMessage(_screen->rootWindow(), XAtom::net_wm_state,
434 _window, 1,
435 _xatom->getAtom(XAtom::net_wm_state_maximized_vert));
436 break;
437 }
438 }
This page took 0.062366 seconds and 5 git commands to generate.