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