]> Dogcows Code - chaz/openbox/blob - util/epist/epist.cc
63a3192037724e727176b9921d323f976387eb6c
[chaz/openbox] / util / epist / epist.cc
1 // -*- mode: C++; indent-tabs-mode: nil; -*-
2 // epist.cc for Epistory - 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_STDIO_H
34 # include <stdio.h>
35 #endif // HAVE_STDIO_H
36
37 #ifdef HAVE_STDLIB_H
38 # include <stdlib.h>
39 #endif // HAVE_STDLIB_H
40
41 #ifdef HAVE_SIGNAL_H
42 # include <signal.h>
43 #endif // HAVE_SIGNAL_H
44
45 #ifdef HAVE_LIBGEN_H
46 # include <libgen.h>
47 #endif // HAVE_LIBGEN_H
48 }
49
50 #include <iostream>
51 #include <string>
52
53 using std::cout;
54 using std::endl;
55 using std::string;
56
57 #include "epist.hh"
58 #include "process.hh"
59 #include "../../src/XAtom.hh"
60
61 bool _shutdown = false;
62 char **_argv;
63 char *_display_name = 0;
64 Display *_display = 0;
65 Window _root = None;
66 XAtom *_xatom;
67
68
69 #ifdef HAVE_SIGACTION
70 static void signalhandler(int sig)
71 #else // HAVE_SIGACTION
72 static RETSIGTYPE signalhandler(int sig)
73 #endif // HAVE_SIGACTION
74 {
75 switch (sig) {
76 case SIGSEGV:
77 cout << "epist: Segmentation fault. Aborting and dumping core.\n";
78 abort();
79 case SIGHUP:
80 cout << "epist: Restarting on request.\n";
81 execvp(_argv[0], _argv);
82 execvp(basename(_argv[0]), _argv);
83 }
84 _shutdown = true;
85
86 #ifndef HAVE_SIGACTION
87 // assume broken, braindead sysv signal semantics
88 signal(sig, (RETSIGTYPE (*)(int)) signalhandler);
89 #endif // HAVE_SIGACTION
90 }
91
92
93 void parseCommandLine(int argc, char **argv) {
94 _argv = argv;
95
96 for (int i = 1; i < argc; ++i) {
97 if (string(argv[i]) == "-display") {
98 if (++i >= argc) {
99 cout << "error:: '-display' requires an argument\n";
100 exit(1);
101 }
102 _display_name = argv[i];
103
104 string dtmp = (string)"DISPLAY=" + _display_name;
105 if (putenv(const_cast<char*>(dtmp.c_str()))) {
106 cout << "warning: couldn't set environment variable 'DISPLAY'\n";
107 perror("putenv()");
108 }
109 }
110 }
111 }
112
113
114 bool findSupportingWM() {
115 Window support_win;
116 if (! _xatom->getValue(_root, XAtom::net_supporting_wm_check, XAtom::window,
117 support_win) || support_win == None)
118 return false;
119
120 string title;
121 _xatom->getValue(support_win, XAtom::net_wm_name, XAtom::utf8, title);
122 cout << "Found compatible window manager: " << title << endl;
123 return true;
124 }
125
126
127 int main(int argc, char **argv) {
128 #ifdef HAVE_SIGACTION
129 struct sigaction action;
130
131 action.sa_handler = signalhandler;
132 action.sa_mask = sigset_t();
133 action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
134
135 sigaction(SIGPIPE, &action, NULL);
136 sigaction(SIGSEGV, &action, NULL);
137 sigaction(SIGFPE, &action, NULL);
138 sigaction(SIGTERM, &action, NULL);
139 sigaction(SIGINT, &action, NULL);
140 sigaction(SIGHUP, &action, NULL);
141 #else // !HAVE_SIGACTION
142 signal(SIGPIPE, (RETSIGTYPE (*)(int)) signalhandler);
143 signal(SIGSEGV, (RETSIGTYPE (*)(int)) signalhandler);
144 signal(SIGFPE, (RETSIGTYPE (*)(int)) signalhandler);
145 signal(SIGTERM, (RETSIGTYPE (*)(int)) signalhandler);
146 signal(SIGINT, (RETSIGTYPE (*)(int)) signalhandler);
147 signal(SIGHUP, (RETSIGTYPE (*)(int)) signalhandler);
148 #endif // HAVE_SIGACTION
149
150 parseCommandLine(argc, argv);
151
152 _display = XOpenDisplay(_display_name);
153 if (! _display) {
154 cout << "Connection to X server '" << _display_name << "' failed.\n";
155 return 1;
156 }
157 _root = RootWindow(_display, DefaultScreen(_display));
158 _xatom = new XAtom(_display);
159
160 XSelectInput(_display, _root, PropertyChangeMask);
161
162 // find a window manager supporting NETWM, waiting for it to load if we must
163 while (! (_shutdown || findSupportingWM()));
164
165 if (! _shutdown) {
166 updateClientList();
167 updateActiveWindow();
168 }
169
170 while (! _shutdown) {
171 if (XPending(_display)) {
172 XEvent e;
173 XNextEvent(_display, &e);
174 processEvent(e);
175 } else {
176 usleep(300);
177 }
178 }
179
180 XSelectInput(_display, _root, None);
181 delete _xatom;
182 XCloseDisplay(_display);
183 return 0;
184 }
This page took 0.039282 seconds and 4 git commands to generate.