]> icculus.org git repositories - mikachu/openbox.git/blob - util/epist/process.cc
gets a whole lotta window information now, and updtes when it changes!
[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 using std::hex;
36 using std::dec;
37
38 #include "../../src/XAtom.hh"
39
40 WindowList _clients;
41 WindowList::iterator _active = _clients.end();
42
43
44 XWindow *findWindow(const XEvent &e) {
45   WindowList::iterator it, end = _clients.end();
46   for (it = _clients.begin(); it != end; ++it)
47     if (**it == e.xany.window)
48       break;
49   if(it == end)
50     return 0;
51   return *it;
52 }
53
54
55 void processEvent(const XEvent &e) {
56   XWindow *window = 0;
57   if (e.xany.window != _root) {
58     window = findWindow(e);  // find the window
59     assert(window); // we caught an event for a window we don't know about!?
60   }
61
62   switch (e.type) {
63   case PropertyNotify:
64     if (e.xany.window == _root) {
65       // root window
66       if (e.xproperty.atom == _xatom->getAtom(XAtom::net_active_window))
67         updateActiveWindow();
68       if (e.xproperty.atom == _xatom->getAtom(XAtom::net_client_list)) {
69         // catch any window unmaps first
70         XEvent ev;
71         if (XCheckTypedWindowEvent(_display, e.xany.window,
72                                    DestroyNotify, &ev) ||
73             XCheckTypedWindowEvent(_display, e.xany.window,
74                                    UnmapNotify, &ev)) {
75           processEvent(ev);
76         }
77         
78         updateClientList();
79       }
80     } else {
81       // a client window
82       if (e.xproperty.atom == _xatom->getAtom(XAtom::net_wm_state))
83         window->updateState();
84       else if (e.xproperty.atom == _xatom->getAtom(XAtom::net_wm_desktop))
85         window->updateDesktop();
86       else if (e.xproperty.atom == _xatom->getAtom(XAtom::net_wm_name) ||
87                e.xproperty.atom == _xatom->getAtom(XAtom::wm_name))
88         window->updateTitle();
89       else if (e.xproperty.atom == _xatom->getAtom(XAtom::wm_class))
90         window->updateClass();
91     }
92     break;
93   case DestroyNotify:
94   case UnmapNotify:
95     window->setUnmapped(true);
96     break;
97   }
98 }
99
100
101 // do we want to add this window to our list?
102 bool doAddWindow(Window window) {
103   Atom type;
104   if (! _xatom->getValue(window, XAtom::net_wm_window_type, XAtom::atom,
105                          type))
106     return True;
107
108   if (type == _xatom->getAtom(XAtom::net_wm_window_type_dock) ||
109       type == _xatom->getAtom(XAtom::net_wm_window_type_menu))
110     return False;
111
112   return True;
113 }
114
115
116 void updateClientList() {
117   WindowList::iterator insert_point = _active;
118   if (insert_point != _clients.end())
119     ++insert_point; // get to the item client the focused client
120   
121   // get the client list from the root window
122   Window *rootclients = 0;
123   unsigned long num = (unsigned) -1;
124   if (! _xatom->getValue(_root, XAtom::net_client_list, XAtom::window, num,
125                          &rootclients)) {
126     while (! _clients.empty()) {
127       delete _clients.front();
128       _clients.erase(_clients.begin());
129     }
130     if (rootclients) delete [] rootclients;
131     return;
132   }
133   
134   WindowList::iterator it, end = _clients.end();
135   unsigned long i;
136   
137   // insert new clients after the active window
138   for (i = 0; i < num; ++i) {
139     for (it = _clients.begin(); it != end; ++it)
140       if (**it == rootclients[i])
141         break;
142     if (it == end) {  // didn't already exist
143       if (doAddWindow(rootclients[i])) {
144         cout << "Added window: 0x" << hex << rootclients[i] << dec << endl;
145         _clients.insert(insert_point, new XWindow(rootclients[i]));
146       }
147     }
148   }
149
150   // remove clients that no longer exist
151   for (it = _clients.begin(); it != end;) {
152     WindowList::iterator it2 = it++;
153     for (i = 0; i < num; ++i)
154       if (**it2 == rootclients[i])
155         break;
156     if (i == num)  { // no longer exists
157       cout << "Removed window: 0x" << hex << (*it2)->window() << dec << endl;
158       delete *it2;
159       _clients.erase(it2);
160     }
161   }
162
163   if (rootclients) delete [] rootclients;
164 }
165
166
167 void updateActiveWindow() {
168   Window a = None;
169   _xatom->getValue(_root, XAtom::net_active_window, XAtom::window, a);
170   
171   WindowList::iterator it, end = _clients.end();
172   for (it = _clients.begin(); it != end; ++it) {
173     if (**it == a)
174       break;
175   }
176   _active = it;
177
178   cout << "Active window is now: ";
179   if (_active == _clients.end()) cout << "None\n";
180   else cout << "0x" << hex << (*_active)->window() << dec << endl;
181 }