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>
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:
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
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.
24 # include "../../config.h"
25 #endif // HAVE_CONFIG_H
29 # include <sys/types.h>
31 #endif // HAVE_UNISTD_H
35 #endif // HAVE_STDIO_H
39 #endif // HAVE_STDLIB_H
43 #endif // HAVE_SIGNAL_H
47 #endif // HAVE_LIBGEN_H
59 #include "../../src/XAtom.hh"
61 bool _shutdown = false;
63 char *_display_name = 0;
64 Display *_display = 0;
70 static void signalhandler(int sig)
71 #else // HAVE_SIGACTION
72 static RETSIGTYPE signalhandler(int sig)
73 #endif // HAVE_SIGACTION
77 cout << "epist: Segmentation fault. Aborting and dumping core.\n";
80 cout << "epist: Restarting on request.\n";
81 execvp(_argv[0], _argv);
82 execvp(basename(_argv[0]), _argv);
86 #ifndef HAVE_SIGACTION
87 // assume broken, braindead sysv signal semantics
88 signal(sig, (RETSIGTYPE (*)(int)) signalhandler);
89 #endif // HAVE_SIGACTION
93 void parseCommandLine(int argc, char **argv) {
96 for (int i = 1; i < argc; ++i) {
97 if (string(argv[i]) == "-display") {
99 cout << "error:: '-display' requires an argument\n";
102 _display_name = argv[i];
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";
114 bool findSupportingWM() {
116 if (! _xatom->getValue(_root, XAtom::net_supporting_wm_check, XAtom::window,
117 support_win) || support_win == None)
121 _xatom->getValue(support_win, XAtom::net_wm_name, XAtom::utf8, title);
122 cout << "Found compatible window manager: " << title << endl;
127 int main(int argc, char **argv) {
128 #ifdef HAVE_SIGACTION
129 struct sigaction action;
131 action.sa_handler = signalhandler;
132 action.sa_mask = sigset_t();
133 action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
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
150 parseCommandLine(argc, argv);
152 _display = XOpenDisplay(_display_name);
154 cout << "Connection to X server '" << _display_name << "' failed.\n";
157 _root = RootWindow(_display, DefaultScreen(_display));
158 _xatom = new XAtom(_display);
160 XSelectInput(_display, _root, PropertyChangeMask);
162 // find a window manager supporting NETWM, waiting for it to load if we must
163 while (! (_shutdown || findSupportingWM()));
167 updateActiveWindow();
170 while (! _shutdown) {
171 if (XPending(_display)) {
173 XNextEvent(_display, &e);
180 XSelectInput(_display, _root, None);
182 XCloseDisplay(_display);