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