]> icculus.org git repositories - mikachu/openbox.git/blob - util/epist/epist.cc
add execute command. add string to action class.
[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   screen *s = new screen(this, DefaultScreen(getXDisplay()));
72   if (s->managed())
73     _screens.push_back(s);
74   if (_screens.empty()) {
75     cout << "No compatible window manager found on any screens. Aborting.\n";
76     ::exit(1);
77   }
78
79   _actions.push_back(Action(Action::nextWorkspace,
80                             XKeysymToKeycode(getXDisplay(),
81                                              XStringToKeysym("Tab")),
82                             ControlMask));
83   _actions.push_back(Action(Action::prevWorkspace,
84                            XKeysymToKeycode(getXDisplay(),
85                                              XStringToKeysym("Tab")),
86                            ControlMask | ShiftMask));
87   _actions.push_back(Action(Action::toggleshade,
88                             XKeysymToKeycode(getXDisplay(),
89                                              XStringToKeysym("F5")),
90                             Mod1Mask));
91   _actions.push_back(Action(Action::close,
92                             XKeysymToKeycode(getXDisplay(),
93                                              XStringToKeysym("F4")),
94                             Mod1Mask));
95   _actions.push_back(Action(Action::nextWindow,
96                             XKeysymToKeycode(getXDisplay(),
97                                              XStringToKeysym("Tab")),
98                             Mod1Mask));
99   _actions.push_back(Action(Action::prevWindow,
100                            XKeysymToKeycode(getXDisplay(),
101                                              XStringToKeysym("Tab")),
102                            Mod1Mask | ShiftMask));
103   _actions.push_back(Action(Action::nextWindowOnAllWorkspaces,
104                             XKeysymToKeycode(getXDisplay(),
105                                              XStringToKeysym("Tab")),
106                             Mod1Mask | ControlMask));
107   _actions.push_back(Action(Action::prevWindowOnAllWorkspaces,
108                            XKeysymToKeycode(getXDisplay(),
109                                              XStringToKeysym("Tab")),
110                            Mod1Mask | ShiftMask | ControlMask));
111   _actions.push_back(Action(Action::raise,
112                            XKeysymToKeycode(getXDisplay(),
113                                              XStringToKeysym("Up")),
114                            Mod1Mask));
115   _actions.push_back(Action(Action::lower,
116                            XKeysymToKeycode(getXDisplay(),
117                                              XStringToKeysym("Down")),
118                            Mod1Mask));
119   _actions.push_back(Action(Action::iconify,
120                            XKeysymToKeycode(getXDisplay(),
121                                              XStringToKeysym("I")),
122                            Mod1Mask | ControlMask));
123   _actions.push_back(Action(Action::toggleomnipresent,
124                            XKeysymToKeycode(getXDisplay(),
125                                              XStringToKeysym("O")),
126                            Mod1Mask | ControlMask));
127   _actions.push_back(Action(Action::changeWorkspace,
128                            XKeysymToKeycode(getXDisplay(),
129                                              XStringToKeysym("1")),
130                            Mod1Mask | ControlMask, 0));
131   _actions.push_back(Action(Action::changeWorkspace,
132                            XKeysymToKeycode(getXDisplay(),
133                                              XStringToKeysym("2")),
134                            Mod1Mask | ControlMask, 1));
135   _actions.push_back(Action(Action::changeWorkspace,
136                            XKeysymToKeycode(getXDisplay(),
137                                              XStringToKeysym("3")),
138                            Mod1Mask | ControlMask, 2));
139   _actions.push_back(Action(Action::changeWorkspace,
140                            XKeysymToKeycode(getXDisplay(),
141                                              XStringToKeysym("4")),
142                            Mod1Mask | ControlMask, 3));
143   _actions.push_back(Action(Action::sendToWorkspace,
144                            XKeysymToKeycode(getXDisplay(),
145                                              XStringToKeysym("1")),
146                            Mod1Mask | ControlMask | ShiftMask, 0));
147   _actions.push_back(Action(Action::sendToWorkspace,
148                            XKeysymToKeycode(getXDisplay(),
149                                              XStringToKeysym("2")),
150                            Mod1Mask | ControlMask | ShiftMask, 1));
151   _actions.push_back(Action(Action::sendToWorkspace,
152                            XKeysymToKeycode(getXDisplay(),
153                                              XStringToKeysym("3")),
154                            Mod1Mask | ControlMask | ShiftMask, 2));
155   _actions.push_back(Action(Action::sendToWorkspace,
156                            XKeysymToKeycode(getXDisplay(),
157                                              XStringToKeysym("4")),
158                            Mod1Mask | ControlMask | ShiftMask, 3));
159   _actions.push_back(Action(Action::execute,
160                            XKeysymToKeycode(getXDisplay(),
161                                              XStringToKeysym("F6")),
162                            Mod1Mask, "aterm"));
163   activateGrabs();
164 }
165
166
167 epist::~epist() {
168   delete _xatom;
169 }
170
171 void epist::activateGrabs() {
172
173   ScreenList::const_iterator scrit, scrend = _screens.end();
174   
175   for (scrit = _screens.begin(); scrit != scrend; ++scrit) {
176     ActionList::const_iterator ait, end = _actions.end();
177
178     for(ait = _actions.begin(); ait != end; ++ait) {
179       XGrabKey(getXDisplay(), ait->keycode(), ait->modifierMask(),
180                (*scrit)->rootWindow(), False, GrabModeAsync, GrabModeAsync);
181     }
182   }
183 }
184
185
186 bool epist::handleSignal(int sig) {
187   switch (sig) {
188   case SIGHUP:
189     cout << "epist: Restarting on request.\n";
190     execvp(_argv[0], _argv);
191     execvp(basename(_argv[0]), _argv);
192     return false;  // this should be unreachable
193
194   case SIGTERM:
195   case SIGINT:
196   case SIGPIPE:
197     shutdown();
198     return true;
199   }
200
201   return false;
202 }
203
204
205 void epist::process_event(XEvent *e) {
206   Window root;
207
208   if (e->xany.type == KeyPress)
209     root = e->xkey.root;
210   else
211     root = e->xany.window;
212   
213   ScreenList::const_iterator it, end = _screens.end();
214   for (it = _screens.begin(); it != end; ++it) {
215     if ((*it)->rootWindow() == root) {
216       (*it)->processEvent(*e);
217       return;
218     }
219   }
220
221   // wasnt a root window, try for client windows
222   XWindow *w = findWindow(e->xany.window);
223   if (w) w->processEvent(*e);
224 }
225   
226
227 void epist::addWindow(XWindow *window) {
228   _windows.insert(WindowLookupPair(window->window(), window));
229 }
230
231
232 void epist::removeWindow(XWindow *window) {
233   _windows.erase(window->window());
234 }
235
236
237 XWindow *epist::findWindow(Window window) const {
238   WindowLookup::const_iterator it = _windows.find(window);
239   if (it != _windows.end())
240     return it->second;
241
242   return 0;
243 }