]> icculus.org git repositories - mikachu/openbox.git/blob - util/epist/process.cc
now we know the state of windows
[mikachu/openbox.git] / util / epist / process.cc
1 // -*- mode: C++; indent-tabs-mode: nil; -*-
2 // process.cc for Epistory - 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 #include "process.hh"
24 #include "epist.hh"
25 #include "window.hh"
26
27 #ifdef    HAVE_CONFIG_H
28 #  include "../../config.h"
29 #endif // HAVE_CONFIG_H
30
31 #include <iostream>
32
33 using std::cout;
34 using std::endl;
35
36 #include "../../src/XAtom.hh"
37
38 WindowList _clients;
39 WindowList::iterator _active = _clients.end();
40
41 void processEvent(const XEvent &e) {
42   switch (e.type) {
43   case PropertyNotify:
44     if (e.xany.window == _root) {
45       // root window
46       if (e.xproperty.atom == _xatom->getAtom(XAtom::net_active_window))
47         updateActiveWindow();
48       if (e.xproperty.atom == _xatom->getAtom(XAtom::net_client_list))
49         updateClientList();
50     } else {
51       // a client window
52       WindowList::iterator it, end = _clients.end();
53       for (it = _clients.begin(); it != end; ++it)
54         if (*it == e.xproperty.window)
55           break;
56       assert(it != end);  // this means a client somehow got removed from the
57                           // list!
58       it->updateState();
59     }
60     break;
61   }
62 }
63
64
65 void updateClientList() {
66   WindowList::iterator insert_point = _active;
67   if (insert_point != _clients.end())
68     ++insert_point; // get to the item client the focused client
69   
70   // get the client list from the root window
71   Window *rootclients = 0;
72   unsigned long num = (unsigned) -1;
73   if (! _xatom->getValue(_root, XAtom::net_client_list, XAtom::window, num,
74                          &rootclients)) {
75     _clients.clear(); // no clients left
76     if (rootclients) delete [] rootclients;
77     return;
78   }
79   
80   WindowList::iterator it, end = _clients.end();
81   unsigned long i;
82   
83   // insert new clients after the active window
84   for (i = 0; i < num; ++i) {
85     for (it = _clients.begin(); it != end; ++it)
86       if (*it == rootclients[i])
87         break;
88     if (it == end) {  // didn't already exist
89       _clients.insert(insert_point, rootclients[i]);
90       cout << "Added window: " << rootclients[i] << endl;
91     }
92   }
93
94   // remove clients that no longer exist
95   for (it = _clients.begin(); it != end;) {
96     WindowList::iterator it2 = it++;
97     for (i = 0; i < num; ++i)
98       if (*it2 == rootclients[i])
99         break;
100     if (i == num)  { // no longer exists
101       _clients.erase(it2);
102       cout << "Removed window: " << it2->window() << endl;
103     }
104   }
105
106   if (rootclients) delete [] rootclients;
107 }
108
109
110 void updateActiveWindow() {
111   Window a = None;
112   _xatom->getValue(_root, XAtom::net_active_window, XAtom::window, a);
113   
114   WindowList::iterator it, end = _clients.end();
115   for (it = _clients.begin(); it != end; ++it) {
116     if (*it == a)
117       break;
118   }
119   _active = it;
120
121   cout << "Active window is now: ";
122   if (_active == _clients.end()) cout << "None\n";
123   else cout << _active->window() << endl;
124 }