]> Dogcows Code - chaz/openbox/blob - util/epist/window.cc
implement moveWindowUp/Down/Left/Right
[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 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 }
This page took 0.046087 seconds and 5 git commands to generate.