]> icculus.org git repositories - mikachu/openbox.git/blob - util/epist/epist.cc
proper scoping
[mikachu/openbox.git] / util / epist / epist.cc
1 // -*- mode: C++; indent-tabs-mode: nil; -*-
2 // epist.cc for Epistrophy - 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 #ifdef    HAVE_STDLIB_H
34 #  include <stdlib.h>
35 #endif // HAVE_STDLIB_H
36
37 #ifdef    HAVE_SIGNAL_H
38 #  include <signal.h>
39 #endif // HAVE_SIGNAL_H
40
41 #ifdef    HAVE_LIBGEN_H
42 #  include <libgen.h>
43 #endif // HAVE_LIBGEN_H
44 }
45
46 #include <iostream>
47 #include <string>
48
49 using std::cout;
50 using std::endl;
51 using std::string;
52
53 #include "actions.hh"
54 #include "epist.hh"
55 #include "screen.hh"
56 #include "window.hh"
57 #include "parser.hh"
58 #include "../../src/XAtom.hh"
59
60
61 epist::epist(char **argv, char *dpy_name, char *rc_file)
62   : BaseDisplay(argv[0], dpy_name) {
63     
64   _argv = argv;
65
66   if (rc_file)
67     _rc_file = rc_file;
68   else
69     _rc_file = expandTilde("~/.epistrc");
70
71   _xatom = new XAtom(getXDisplay());
72
73   _active = _clients.end();
74   
75   for (unsigned int i = 0; i < getNumberOfScreens(); ++i) {
76     screen *s = new screen(this, i);
77     if (s->managed()) {
78       _screens.push_back(s);
79       s->updateEverything();
80     }
81   }
82   if (_screens.empty()) {
83     cout << "No compatible window manager found on any screens. Aborting.\n";
84     ::exit(1);
85   }
86
87   _ktree = new keytree(getXDisplay());
88
89   // set up the key tree
90   parser p(_ktree);
91   p.parse(_rc_file);
92
93   activateGrabs();
94 }
95
96
97 epist::~epist() {
98   delete _xatom;
99 }
100
101 void epist::activateGrabs() {
102
103   ScreenList::const_iterator scrit, scrend = _screens.end();
104   
105   for (scrit = _screens.begin(); scrit != scrend; ++scrit)
106       _ktree->grabDefaults(*scrit);
107 }
108
109
110 bool epist::handleSignal(int sig) {
111   switch (sig) {
112   case SIGHUP: {
113     cout << "epist: Restarting on request.\n";
114     
115     execvp(_argv[0], _argv);
116
117     string base(basename(_argv[0]));
118     execvp(base.c_str(), _argv);
119     
120     return false;  // this should be unreachable
121   }
122
123   case SIGTERM:
124   case SIGINT:
125   case SIGPIPE:
126     shutdown();
127     return true;
128   }
129
130   return false;
131 }
132
133
134 void epist::process_event(XEvent *e) {
135   ScreenList::const_iterator it, end = _screens.end();
136   for (it = _screens.begin(); it != end; ++it) {
137     if ((*it)->rootWindow() == e->xany.window) {
138       (*it)->processEvent(*e);
139       return;
140     }
141   }
142
143   // wasnt a root window, try for client windows
144   XWindow *w = findWindow(e->xany.window);
145   if (w) w->processEvent(*e);
146 }
147   
148
149 void epist::addWindow(XWindow *window) {
150   _windows.insert(WindowLookupPair(window->window(), window));
151 }
152
153
154 void epist::removeWindow(XWindow *window) {
155   _windows.erase(window->window());
156 }
157
158
159 XWindow *epist::findWindow(Window window) const {
160   WindowLookup::const_iterator it = _windows.find(window);
161   if (it != _windows.end())
162     return it->second;
163
164   return 0;
165 }
166
167
168 void epist::cycleScreen(int current, bool forward) const {
169   unsigned int i;
170   for (i = 0; i < _screens.size(); ++i)
171     if (_screens[i]->number() == current) {
172       current = i;
173       break;
174     }
175   assert(i < _screens.size());  // current is for an unmanaged screen
176   
177   int dest = current + (forward ? 1 : -1);
178
179   if (dest < 0) dest = (signed)_screens.size() - 1;
180   else if (dest >= (signed)_screens.size()) dest = 0;
181
182   const XWindow *target = _screens[dest]->lastActiveWindow();
183   if (target) target->focus();
184 }