]> icculus.org git repositories - mikachu/openbox.git/blob - util/epist/process.cc
epist now handles -display on the command line, and gets the client list and act
[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.xproperty.atom == _xatom->getAtom(XAtom::net_active_window))
45       updateActiveWindow();
46     if (e.xproperty.atom == _xatom->getAtom(XAtom::net_client_list))
47       updateClientList();
48     break;
49   }
50 }
51
52
53 void updateClientList() {
54   WindowList::iterator insert_point = _active;
55   if (insert_point != _clients.end())
56     ++insert_point; // get to the item client the focused client
57   
58   // get the client list from the root window
59   Window *rootclients = 0;
60   unsigned long num = (unsigned) -1;
61   if (! _xatom->getValue(_root, XAtom::net_client_list, XAtom::window, num,
62                          &rootclients)) {
63     _clients.clear(); // no clients left
64     if (rootclients) delete [] rootclients;
65     return;
66   }
67   
68   WindowList::iterator it, end = _clients.end();
69   unsigned long i;
70   
71   // insert new clients after the active window
72   for (i = 0; i < num; ++i) {
73     for (it = _clients.begin(); it != end; ++it)
74       if (*it == rootclients[i])
75         break;
76     if (it == end) {  // didn't already exist
77       _clients.insert(insert_point, rootclients[i]);
78       cout << "Added window: " << rootclients[i] << endl;
79     }
80   }
81
82   // remove clients that no longer exist
83   for (it = _clients.begin(); it != end;) {
84     WindowList::iterator it2 = it++;
85     for (i = 0; i < num; ++i)
86       if (*it2 == rootclients[i])
87         break;
88     if (i == num)  { // no longer exists
89       _clients.erase(it2);
90       cout << "Removed window: " << it2->window() << endl;
91     }
92   }
93
94   if (rootclients) delete [] rootclients;
95 }
96
97
98 void updateActiveWindow() {
99   Window a = None;
100   _xatom->getValue(_root, XAtom::net_active_window, XAtom::window, a);
101   
102   WindowList::iterator it, end = _clients.end();
103   for (it = _clients.begin(); it != end; ++it) {
104     if (*it == a)
105       break;
106   }
107   _active = it;
108
109   cout << "Active window is now: ";
110   if (_active == _clients.end()) cout << "None\n";
111   else cout << _active->window() << endl;
112 }