]> icculus.org git repositories - mikachu/openbox.git/blob - util/epist/epist.cc
much nicer hardcoded bindings! :)
[mikachu/openbox.git] / 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 "epist.hh"
54 #include "screen.hh"
55 #include "window.hh"
56 #include "../../src/XAtom.hh"
57
58
59 epist::epist(char **argv, char *dpy_name, char *rc_file)
60   : BaseDisplay(argv[0], dpy_name) {
61     
62   _argv = argv;
63
64   if (rc_file)
65     _rc_file = rc_file;
66   else
67     _rc_file = expandTilde("~/.openbox/epistrc");
68
69   _xatom = new XAtom(getXDisplay());
70
71   for (unsigned int i = 0; i < getNumberOfScreens(); ++i) {
72     screen *s = new screen(this, i);
73     if (s->managed())
74       _screens.push_back(s);
75   }
76   if (_screens.empty()) {
77     cout << "No compatible window manager found on any screens. Aborting.\n";
78     ::exit(1);
79   }
80
81   addAction(Action::nextWorkspace, ControlMask, "Tab");
82   addAction(Action::prevWorkspace, ControlMask | ShiftMask, "Tab");
83   addAction(Action::nextWindow, Mod1Mask, "Tab");
84   addAction(Action::prevWindow, Mod1Mask | ShiftMask, "Tab");
85   addAction(Action::toggleshade, Mod1Mask, "F5");
86   addAction(Action::close, Mod1Mask, "F4");
87   addAction(Action::nextWindowOnAllWorkspaces, Mod1Mask | ControlMask, "Tab");
88   addAction(Action::prevWindowOnAllWorkspaces,
89             Mod1Mask | ShiftMask | ControlMask, "Tab");
90   addAction(Action::raise, Mod1Mask, "Up");
91   addAction(Action::lower, Mod1Mask, "Down");
92   addAction(Action::moveWindowUp, Mod1Mask | ControlMask, "Up", 1);
93   addAction(Action::moveWindowDown, Mod1Mask | ControlMask, "Down", 1);
94   addAction(Action::moveWindowLeft, Mod1Mask | ControlMask, "Left", 1);
95   addAction(Action::moveWindowRight, Mod1Mask | ControlMask, "Right", 1);
96   addAction(Action::resizeWindowHeight, ShiftMask | Mod1Mask | ControlMask,
97             "Up", -1);
98   addAction(Action::resizeWindowHeight, ShiftMask | Mod1Mask | ControlMask,
99             "Down", 1);
100   addAction(Action::resizeWindowWidth, ShiftMask | Mod1Mask | ControlMask,
101             "Left", -1);
102   addAction(Action::resizeWindowWidth, ShiftMask | Mod1Mask | ControlMask,
103             "Right", 1);
104   addAction(Action::iconify, Mod1Mask | ControlMask, "I");
105   addAction(Action::toggleomnipresent, Mod1Mask | ControlMask, "O");
106   addAction(Action::toggleMaximizeHorizontal, ShiftMask | Mod1Mask, "X");
107   addAction(Action::toggleMaximizeVertical, ShiftMask | ControlMask, "X");
108   addAction(Action::toggleMaximizeFull, Mod1Mask | ControlMask, "X");
109   addAction(Action::changeWorkspace, Mod1Mask | ControlMask, "1", 0);
110   addAction(Action::changeWorkspace, Mod1Mask | ControlMask, "2", 1);
111   addAction(Action::changeWorkspace, Mod1Mask | ControlMask, "3", 2);
112   addAction(Action::changeWorkspace, Mod1Mask | ControlMask, "4", 3);
113   addAction(Action::sendToWorkspace, Mod1Mask | ControlMask | ShiftMask,
114             "1", 0);
115   addAction(Action::sendToWorkspace, Mod1Mask | ControlMask | ShiftMask,
116             "2", 1);
117   addAction(Action::sendToWorkspace, Mod1Mask | ControlMask | ShiftMask,
118             "3", 2);
119   addAction(Action::sendToWorkspace, Mod1Mask | ControlMask | ShiftMask,
120             "4", 3);
121   addAction(Action::execute, Mod1Mask | ControlMask, "Escape",
122             "sleep 1 && xset dpms force off");
123   addAction(Action::execute, Mod1Mask, "space",
124             "rxvt");
125
126   activateGrabs();
127 }
128
129
130 epist::~epist() {
131   delete _xatom;
132 }
133
134 void epist::activateGrabs() {
135
136   ScreenList::const_iterator scrit, scrend = _screens.end();
137   
138   for (scrit = _screens.begin(); scrit != scrend; ++scrit) {
139     ActionList::const_iterator ait, end = _actions.end();
140
141     for(ait = _actions.begin(); ait != end; ++ait) {
142       (*scrit)->grabKey(ait->keycode(), ait->modifierMask());
143     }
144   }
145 }
146
147
148 bool epist::handleSignal(int sig) {
149   switch (sig) {
150   case SIGHUP:
151     cout << "epist: Restarting on request.\n";
152     execvp(_argv[0], _argv);
153     execvp(basename(_argv[0]), _argv);
154     return false;  // this should be unreachable
155
156   case SIGTERM:
157   case SIGINT:
158   case SIGPIPE:
159     shutdown();
160     return true;
161   }
162
163   return false;
164 }
165
166
167 void epist::process_event(XEvent *e) {
168   ScreenList::const_iterator it, end = _screens.end();
169   for (it = _screens.begin(); it != end; ++it) {
170     if ((*it)->rootWindow() == e->xany.window) {
171       (*it)->processEvent(*e);
172       return;
173     }
174   }
175
176   // wasnt a root window, try for client windows
177   XWindow *w = findWindow(e->xany.window);
178   if (w) w->processEvent(*e);
179 }
180   
181
182 void epist::addWindow(XWindow *window) {
183   _windows.insert(WindowLookupPair(window->window(), window));
184 }
185
186
187 void epist::removeWindow(XWindow *window) {
188   _windows.erase(window->window());
189 }
190
191
192 XWindow *epist::findWindow(Window window) const {
193   WindowLookup::const_iterator it = _windows.find(window);
194   if (it != _windows.end())
195     return it->second;
196
197   return 0;
198 }
199
200 void epist::addAction(Action::ActionType act, unsigned int modifiers,
201                       string key, int number) {
202   _actions.push_back(Action(act, XKeysymToKeycode(getXDisplay(),
203                                                   XStringToKeysym(key.c_str())),
204                             modifiers, number));
205 }
206
207
208 void epist::addAction(Action::ActionType act, unsigned int modifiers,
209                       string key, std::string str) {
210   _actions.push_back(Action(act, XKeysymToKeycode(getXDisplay(),
211                                                   XStringToKeysym(key.c_str())),
212                             modifiers, str));
213 }