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