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