]> Dogcows Code - chaz/openbox/blob - util/epist/screen.cc
add execute command. add string to action class.
[chaz/openbox] / util / epist / screen.cc
1 // -*- mode: C++; indent-tabs-mode: nil; -*-
2 // screen.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 extern "C" {
28 #ifdef HAVE_UNISTD_H
29 # include <sys/types.h>
30 # include <unistd.h>
31 #endif // HAVE_UNISTD_H
32 }
33
34 #include <iostream>
35 #include <string>
36
37 using std::cout;
38 using std::endl;
39 using std::hex;
40 using std::dec;
41 using std::string;
42
43 #include "../../src/BaseDisplay.hh"
44 #include "../../src/XAtom.hh"
45 #include "screen.hh"
46 #include "epist.hh"
47
48
49 screen::screen(epist *epist, int number) {
50 _epist = epist;
51 _xatom = _epist->xatom();
52 _number = number;
53 _active = _clients.end();
54 _info = _epist->getScreenInfo(_number);
55 _root = _info->getRootWindow();
56
57 // find a window manager supporting NETWM, waiting for it to load if we must
58 int count = 20; // try for 20 seconds
59 _managed = false;
60 while (! (_epist->doShutdown() || _managed || count <= 0)) {
61 if (! (_managed = findSupportingWM()))
62 usleep(1000);
63 --count;
64 }
65 if (_managed)
66 cout << "Found compatible window manager '" << _wm_name << "' for screen "
67 << _number << ".\n";
68 else {
69 cout << "Unable to find a compatible window manager for screen " <<
70 _number << ".\n";
71 return;
72 }
73
74 XSelectInput(_epist->getXDisplay(), _root, PropertyChangeMask);
75
76 updateNumDesktops();
77 updateActiveDesktop();
78 updateClientList();
79 updateActiveWindow();
80 }
81
82
83 screen::~screen() {
84 if (_managed)
85 XSelectInput(_epist->getXDisplay(), _root, None);
86 }
87
88
89 bool screen::findSupportingWM() {
90 Window support_win;
91 if (! _xatom->getValue(_root, XAtom::net_supporting_wm_check, XAtom::window,
92 support_win) || support_win == None)
93 return false;
94
95 string title;
96 _xatom->getValue(support_win, XAtom::net_wm_name, XAtom::utf8, title);
97 _wm_name = title;
98 return true;
99 }
100
101
102 XWindow *screen::findWindow(const XEvent &e) const {
103 assert(_managed);
104
105 WindowList::const_iterator it, end = _clients.end();
106 for (it = _clients.begin(); it != end; ++it)
107 if (**it == e.xany.window)
108 break;
109 if(it == end)
110 return 0;
111 return *it;
112 }
113
114
115 void screen::processEvent(const XEvent &e) {
116 assert(_managed);
117 assert(e.xany.window == _root);
118
119 XWindow *window = 0;
120 if (e.xany.window != _root) {
121 window = findWindow(e); // find the window
122 assert(window); // we caught an event for a window we don't know about!?
123 }
124
125 switch (e.type) {
126 case PropertyNotify:
127 // root window
128 if (e.xproperty.atom == _xatom->getAtom(XAtom::net_number_of_desktops))
129 updateNumDesktops();
130 if (e.xproperty.atom == _xatom->getAtom(XAtom::net_current_desktop))
131 updateActiveDesktop();
132 if (e.xproperty.atom == _xatom->getAtom(XAtom::net_active_window))
133 updateActiveWindow();
134 if (e.xproperty.atom == _xatom->getAtom(XAtom::net_client_list)) {
135 // catch any window unmaps first
136 XEvent ev;
137 if (XCheckTypedWindowEvent(_epist->getXDisplay(), e.xany.window,
138 DestroyNotify, &ev) ||
139 XCheckTypedWindowEvent(_epist->getXDisplay(), e.xany.window,
140 UnmapNotify, &ev)) {
141 processEvent(ev);
142 }
143
144 updateClientList();
145 }
146 break;
147 case KeyPress:
148 handleKeypress(e);
149 break;
150 }
151 }
152
153 void screen::handleKeypress(const XEvent &e) {
154 ActionList::const_iterator it = _epist->actions().begin();
155 ActionList::const_iterator end = _epist->actions().end();
156 for (; it != end; ++it) {
157 if (e.xkey.keycode == it->keycode() &&
158 e.xkey.state == it->modifierMask()) {
159 switch (it->type()) {
160 case Action::nextWorkspace:
161 cycleWorkspace(true);
162 return;
163
164 case Action::prevWorkspace:
165 cycleWorkspace(false);
166 return;
167
168 case Action::nextWindow:
169 cycleWindow(true);
170 return;
171
172 case Action::prevWindow:
173 cycleWindow(false);
174 return;
175
176 case Action::nextWindowOnAllWorkspaces:
177 cycleWindow(true, true);
178 return;
179
180 case Action::prevWindowOnAllWorkspaces:
181 cycleWindow(false, true);
182 return;
183
184 case Action::nextWindowOfClass:
185 cycleWindow(true, false, true);
186 return;
187
188 case Action::prevWindowOfClass:
189 cycleWindow(false, false, true);
190 return;
191
192 case Action::nextWindowOfClassOnAllWorkspaces:
193 cycleWindow(true, true, true);
194 return;
195
196 case Action::prevWindowOfClassOnAllWorkspaces:
197 cycleWindow(false, true, true);
198 return;
199
200 case Action::changeWorkspace:
201 changeWorkspace(it->number());
202 return;
203
204 case Action::execute:
205 execCommand(it->string());
206 return;
207 }
208
209 // these actions require an active window
210 if (_active != _clients.end()) {
211 XWindow *window = *_active;
212
213 switch (it->type()) {
214 case Action::iconify:
215 window->iconify();
216 return;
217
218 case Action::close:
219 window->close();
220 return;
221
222 case Action::raise:
223 window->raise();
224 return;
225
226 case Action::lower:
227 window->lower();
228 return;
229
230 case Action::sendToWorkspace:
231 window->sendTo(it->number());
232 return;
233
234 case Action::toggleomnipresent:
235 if (window->desktop() == 0xffffffff)
236 window->sendTo(_active_desktop);
237 else
238 window->sendTo(0xffffffff);
239 return;
240
241 case Action::toggleshade:
242 window->shade(! window->shaded());
243 return;
244 }
245 }
246 }
247 }
248 }
249
250 // do we want to add this window to our list?
251 bool screen::doAddWindow(Window window) const {
252 assert(_managed);
253
254 Atom type;
255 if (! _xatom->getValue(window, XAtom::net_wm_window_type, XAtom::atom,
256 type))
257 return True;
258
259 if (type == _xatom->getAtom(XAtom::net_wm_window_type_dock) ||
260 type == _xatom->getAtom(XAtom::net_wm_window_type_menu))
261 return False;
262
263 return True;
264 }
265
266
267 void screen::updateNumDesktops() {
268 assert(_managed);
269
270 if (! _xatom->getValue(_root, XAtom::net_number_of_desktops, XAtom::cardinal,
271 (unsigned long)_num_desktops))
272 _num_desktops = 1; // assume that there is at least 1 desktop!
273 }
274
275
276 void screen::updateActiveDesktop() {
277 assert(_managed);
278
279 if (! _xatom->getValue(_root, XAtom::net_current_desktop, XAtom::cardinal,
280 (unsigned long)_active_desktop))
281 _active_desktop = 0; // there must be at least one desktop, and it must
282 // be the current one
283 }
284
285
286 void screen::updateClientList() {
287 assert(_managed);
288
289 WindowList::iterator insert_point = _active;
290 if (insert_point != _clients.end())
291 ++insert_point; // get to the item client the focused client
292
293 // get the client list from the root window
294 Window *rootclients = 0;
295 unsigned long num = (unsigned) -1;
296 if (! _xatom->getValue(_root, XAtom::net_client_list, XAtom::window, num,
297 &rootclients)) {
298 while (! _clients.empty()) {
299 delete _clients.front();
300 _clients.erase(_clients.begin());
301 }
302 if (rootclients) delete [] rootclients;
303 return;
304 }
305
306 WindowList::iterator it, end = _clients.end();
307 unsigned long i;
308
309 // insert new clients after the active window
310 for (i = 0; i < num; ++i) {
311 for (it = _clients.begin(); it != end; ++it)
312 if (**it == rootclients[i])
313 break;
314 if (it == end) { // didn't already exist
315 if (doAddWindow(rootclients[i])) {
316 cout << "Added window: 0x" << hex << rootclients[i] << dec << endl;
317 _clients.insert(insert_point, new XWindow(_epist, this,
318 rootclients[i]));
319 }
320 }
321 }
322
323 // remove clients that no longer exist
324 for (it = _clients.begin(); it != end;) {
325 WindowList::iterator it2 = it++;
326 for (i = 0; i < num; ++i)
327 if (**it2 == rootclients[i])
328 break;
329 if (i == num) { // no longer exists
330 cout << "Removed window: 0x" << hex << (*it2)->window() << dec << endl;
331 delete *it2;
332 _clients.erase(it2);
333 }
334 }
335
336 if (rootclients) delete [] rootclients;
337 }
338
339
340 void screen::updateActiveWindow() {
341 assert(_managed);
342
343 Window a = None;
344 _xatom->getValue(_root, XAtom::net_active_window, XAtom::window, a);
345
346 WindowList::iterator it, end = _clients.end();
347 for (it = _clients.begin(); it != end; ++it) {
348 if (**it == a)
349 break;
350 }
351 _active = it;
352
353 cout << "Active window is now: ";
354 if (_active == _clients.end()) cout << "None\n";
355 else cout << "0x" << hex << (*_active)->window() << dec << endl;
356 }
357
358
359 void screen::execCommand(const std::string &cmd) const {
360 pid_t pid;
361 if ((pid = fork()) == 0) {
362 extern char **environ;
363
364 char *const argv[] = {
365 "sh",
366 "-c",
367 const_cast<char *>(cmd.c_str()),
368 0
369 };
370 // make the command run on the correct screen
371 if (putenv(const_cast<char*>(_info->displayString().c_str()))) {
372 cout << "warning: couldn't set environment variable 'DISPLAY'\n";
373 perror("putenv()");
374 }
375 execve("/bin/sh", argv, environ);
376 exit(127);
377 } else if (pid == -1) {
378 cout << _epist->getApplicationName() <<
379 ": Could not fork a process for executing a command\n";
380 }
381 }
382
383
384 void screen::cycleWindow(const bool forward, const bool alldesktops,
385 const bool sameclass) const {
386 assert(_managed);
387
388 if (_clients.empty()) return;
389
390 WindowList::const_iterator target = _active;
391
392 if (target == _clients.end())
393 target = _clients.begin();
394
395 do {
396 if (forward) {
397 ++target;
398 if (target == _clients.end())
399 target = _clients.begin();
400 } else {
401 if (target == _clients.begin())
402 target = _clients.end();
403 --target;
404 }
405 } while (target == _clients.end() ||
406 (*target)->iconic() ||
407 (! alldesktops && (*target)->desktop() != _active_desktop) ||
408 (sameclass && _active != _clients.end() &&
409 (*target)->appClass() != (*_active)->appClass()));
410
411 if (target != _clients.end())
412 (*target)->focus();
413 }
414
415
416 void screen::cycleWorkspace(const bool forward, const bool loop) const {
417 assert(_managed);
418
419 unsigned int destination = _active_desktop;
420
421 if (forward) {
422 if (destination < _num_desktops - 1)
423 ++destination;
424 else if (loop)
425 destination = 0;
426 } else {
427 if (destination > 0)
428 --destination;
429 else if (loop)
430 destination = _num_desktops - 1;
431 }
432
433 if (destination != _active_desktop)
434 changeWorkspace(destination);
435 }
436
437
438 void screen::changeWorkspace(const int num) const {
439 assert(_managed);
440
441 _xatom->sendClientMessage(_root, XAtom::net_current_desktop, _root, num);
442 }
This page took 0.058627 seconds and 5 git commands to generate.