]> Dogcows Code - chaz/openbox/blob - util/epist/epist.cc
f83059435edc458c8cd7edaf4c675cf7c3e71064
[chaz/openbox] / 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 execvp(_argv[0], _argv);
115 string base(basename(_argv[0]));
116 execvp(base.c_str(), _argv);
117 return false; // this should be unreachable
118
119 case SIGTERM:
120 case SIGINT:
121 case SIGPIPE:
122 shutdown();
123 return true;
124 }
125
126 return false;
127 }
128
129
130 void epist::process_event(XEvent *e) {
131 ScreenList::const_iterator it, end = _screens.end();
132 for (it = _screens.begin(); it != end; ++it) {
133 if ((*it)->rootWindow() == e->xany.window) {
134 (*it)->processEvent(*e);
135 return;
136 }
137 }
138
139 // wasnt a root window, try for client windows
140 XWindow *w = findWindow(e->xany.window);
141 if (w) w->processEvent(*e);
142 }
143
144
145 void epist::addWindow(XWindow *window) {
146 _windows.insert(WindowLookupPair(window->window(), window));
147 }
148
149
150 void epist::removeWindow(XWindow *window) {
151 _windows.erase(window->window());
152 }
153
154
155 XWindow *epist::findWindow(Window window) const {
156 WindowLookup::const_iterator it = _windows.find(window);
157 if (it != _windows.end())
158 return it->second;
159
160 return 0;
161 }
162
163
164 void epist::cycleScreen(int current, bool forward) const {
165 unsigned int i;
166 for (i = 0; i < _screens.size(); ++i)
167 if (_screens[i]->number() == current) {
168 current = i;
169 break;
170 }
171 assert(i < _screens.size()); // current is for an unmanaged screen
172
173 int dest = current + (forward ? 1 : -1);
174
175 if (dest < 0) dest = (signed)_screens.size() - 1;
176 else if (dest >= (signed)_screens.size()) dest = 0;
177
178 const XWindow *target = _screens[dest]->lastActiveWindow();
179 if (target) target->focus();
180 }
This page took 0.0481470000000001 seconds and 4 git commands to generate.