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