]> icculus.org git repositories - mikachu/openbox.git/blob - util/epist/epist.cc
implement window resizing
[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   _actions.push_back(Action(Action::nextWorkspace,
82                             XKeysymToKeycode(getXDisplay(),
83                                              XStringToKeysym("Tab")),
84                             ControlMask));
85   _actions.push_back(Action(Action::prevWorkspace,
86                            XKeysymToKeycode(getXDisplay(),
87                                              XStringToKeysym("Tab")),
88                            ControlMask | ShiftMask));
89   _actions.push_back(Action(Action::toggleshade,
90                             XKeysymToKeycode(getXDisplay(),
91                                              XStringToKeysym("F5")),
92                             Mod1Mask));
93   _actions.push_back(Action(Action::close,
94                             XKeysymToKeycode(getXDisplay(),
95                                              XStringToKeysym("F4")),
96                             Mod1Mask));
97   _actions.push_back(Action(Action::nextWindow,
98                             XKeysymToKeycode(getXDisplay(),
99                                              XStringToKeysym("Tab")),
100                             Mod1Mask));
101   _actions.push_back(Action(Action::prevWindow,
102                            XKeysymToKeycode(getXDisplay(),
103                                              XStringToKeysym("Tab")),
104                            Mod1Mask | ShiftMask));
105   _actions.push_back(Action(Action::nextWindowOnAllWorkspaces,
106                             XKeysymToKeycode(getXDisplay(),
107                                              XStringToKeysym("Tab")),
108                             Mod1Mask | ControlMask));
109   _actions.push_back(Action(Action::prevWindowOnAllWorkspaces,
110                            XKeysymToKeycode(getXDisplay(),
111                                              XStringToKeysym("Tab")),
112                            Mod1Mask | ShiftMask | ControlMask));
113   _actions.push_back(Action(Action::raise,
114                            XKeysymToKeycode(getXDisplay(),
115                                              XStringToKeysym("Up")),
116                            Mod1Mask));
117   _actions.push_back(Action(Action::lower,
118                            XKeysymToKeycode(getXDisplay(),
119                                              XStringToKeysym("Down")),
120                            Mod1Mask));
121   _actions.push_back(Action(Action::moveWindowUp,
122                            XKeysymToKeycode(getXDisplay(),
123                                              XStringToKeysym("Up")),
124                            Mod1Mask | ControlMask, 1));
125   _actions.push_back(Action(Action::moveWindowDown,
126                            XKeysymToKeycode(getXDisplay(),
127                                              XStringToKeysym("Down")),
128                            Mod1Mask | ControlMask, 1));
129   _actions.push_back(Action(Action::moveWindowLeft,
130                            XKeysymToKeycode(getXDisplay(),
131                                              XStringToKeysym("Left")),
132                            Mod1Mask | ControlMask, 1));
133   _actions.push_back(Action(Action::moveWindowRight,
134                            XKeysymToKeycode(getXDisplay(),
135                                              XStringToKeysym("Right")),
136                            Mod1Mask | ControlMask, 1));
137   _actions.push_back(Action(Action::resizeWindowHeight,
138                            XKeysymToKeycode(getXDisplay(),
139                                              XStringToKeysym("Up")),
140                            ShiftMask | Mod1Mask | ControlMask, -1));
141   _actions.push_back(Action(Action::resizeWindowHeight,
142                            XKeysymToKeycode(getXDisplay(),
143                                              XStringToKeysym("Down")),
144                            ShiftMask | Mod1Mask | ControlMask, 1));
145   _actions.push_back(Action(Action::resizeWindowWidth,
146                            XKeysymToKeycode(getXDisplay(),
147                                              XStringToKeysym("Left")),
148                            ShiftMask | Mod1Mask | ControlMask, -1));
149   _actions.push_back(Action(Action::resizeWindowWidth,
150                            XKeysymToKeycode(getXDisplay(),
151                                              XStringToKeysym("Right")),
152                            ShiftMask | Mod1Mask | ControlMask, 1));
153   _actions.push_back(Action(Action::iconify,
154                            XKeysymToKeycode(getXDisplay(),
155                                              XStringToKeysym("I")),
156                            Mod1Mask | ControlMask));
157   _actions.push_back(Action(Action::toggleomnipresent,
158                            XKeysymToKeycode(getXDisplay(),
159                                              XStringToKeysym("O")),
160                            Mod1Mask | ControlMask));
161   _actions.push_back(Action(Action::toggleMaximizeHorizontal,
162                            XKeysymToKeycode(getXDisplay(),
163                                              XStringToKeysym("X")),
164                            ShiftMask | Mod1Mask));
165   _actions.push_back(Action(Action::toggleMaximizeVertical,
166                            XKeysymToKeycode(getXDisplay(),
167                                              XStringToKeysym("X")),
168                            ShiftMask | ControlMask));
169   _actions.push_back(Action(Action::toggleMaximizeFull,
170                            XKeysymToKeycode(getXDisplay(),
171                                              XStringToKeysym("X")),
172                            Mod1Mask | ControlMask));
173   _actions.push_back(Action(Action::changeWorkspace,
174                            XKeysymToKeycode(getXDisplay(),
175                                              XStringToKeysym("1")),
176                            Mod1Mask | ControlMask, 0));
177   _actions.push_back(Action(Action::changeWorkspace,
178                            XKeysymToKeycode(getXDisplay(),
179                                              XStringToKeysym("2")),
180                            Mod1Mask | ControlMask, 1));
181   _actions.push_back(Action(Action::changeWorkspace,
182                            XKeysymToKeycode(getXDisplay(),
183                                              XStringToKeysym("3")),
184                            Mod1Mask | ControlMask, 2));
185   _actions.push_back(Action(Action::changeWorkspace,
186                            XKeysymToKeycode(getXDisplay(),
187                                              XStringToKeysym("4")),
188                            Mod1Mask | ControlMask, 3));
189   _actions.push_back(Action(Action::sendToWorkspace,
190                            XKeysymToKeycode(getXDisplay(),
191                                              XStringToKeysym("1")),
192                            Mod1Mask | ControlMask | ShiftMask, 0));
193   _actions.push_back(Action(Action::sendToWorkspace,
194                            XKeysymToKeycode(getXDisplay(),
195                                              XStringToKeysym("2")),
196                            Mod1Mask | ControlMask | ShiftMask, 1));
197   _actions.push_back(Action(Action::sendToWorkspace,
198                            XKeysymToKeycode(getXDisplay(),
199                                              XStringToKeysym("3")),
200                            Mod1Mask | ControlMask | ShiftMask, 2));
201   _actions.push_back(Action(Action::sendToWorkspace,
202                            XKeysymToKeycode(getXDisplay(),
203                                              XStringToKeysym("4")),
204                            Mod1Mask | ControlMask | ShiftMask, 3));
205   _actions.push_back(Action(Action::execute,
206                            XKeysymToKeycode(getXDisplay(),
207                                              XStringToKeysym("space")),
208                            Mod1Mask, "rxvt"));
209   activateGrabs();
210 }
211
212
213 epist::~epist() {
214   delete _xatom;
215 }
216
217 void epist::activateGrabs() {
218
219   ScreenList::const_iterator scrit, scrend = _screens.end();
220   
221   for (scrit = _screens.begin(); scrit != scrend; ++scrit) {
222     ActionList::const_iterator ait, end = _actions.end();
223
224     for(ait = _actions.begin(); ait != end; ++ait) {
225       XGrabKey(getXDisplay(), ait->keycode(), ait->modifierMask(),
226                (*scrit)->rootWindow(), False, GrabModeAsync, GrabModeAsync);
227     }
228   }
229 }
230
231
232 bool epist::handleSignal(int sig) {
233   switch (sig) {
234   case SIGHUP:
235     cout << "epist: Restarting on request.\n";
236     execvp(_argv[0], _argv);
237     execvp(basename(_argv[0]), _argv);
238     return false;  // this should be unreachable
239
240   case SIGTERM:
241   case SIGINT:
242   case SIGPIPE:
243     shutdown();
244     return true;
245   }
246
247   return false;
248 }
249
250
251 void epist::process_event(XEvent *e) {
252   Window root;
253
254   if (e->xany.type == KeyPress)
255     root = e->xkey.root;
256   else
257     root = e->xany.window;
258   
259   ScreenList::const_iterator it, end = _screens.end();
260   for (it = _screens.begin(); it != end; ++it) {
261     if ((*it)->rootWindow() == root) {
262       (*it)->processEvent(*e);
263       return;
264     }
265   }
266
267   // wasnt a root window, try for client windows
268   XWindow *w = findWindow(e->xany.window);
269   if (w) w->processEvent(*e);
270 }
271   
272
273 void epist::addWindow(XWindow *window) {
274   _windows.insert(WindowLookupPair(window->window(), window));
275 }
276
277
278 void epist::removeWindow(XWindow *window) {
279   _windows.erase(window->window());
280 }
281
282
283 XWindow *epist::findWindow(Window window) const {
284   WindowLookup::const_iterator it = _windows.find(window);
285   if (it != _windows.end())
286     return it->second;
287
288   return 0;
289 }