]> icculus.org git repositories - mikachu/openbox.git/blob - util/epist/main.cc
open an X display
[mikachu/openbox.git] / util / epist / main.cc
1 // -*- mode: C++; indent-tabs-mode: nil; -*-
2 // main.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 #include <X11/Xlib.h>
29
30 #ifdef    HAVE_UNISTD_H
31 #  include <sys/types.h>
32 #  include <unistd.h>
33 #endif // HAVE_UNISTD_H
34
35 #ifdef    HAVE_SIGNAL_H
36 #  include <signal.h>
37 #endif // HAVE_SIGNAL_H
38
39 #ifdef    HAVE_SYS_SIGNAL_H
40 #  include <sys/signal.h>
41 #endif // HAVE_SYS_SIGNAL_H
42
43 #ifdef    HAVE_LIBGEN_H
44 #  include <libgen.h>
45 #endif // HAVE_LIBGEN_H
46 }
47
48 #include <iostream>
49
50 using std::cout;
51 using std::endl;
52
53 bool _shutdown = false;
54 char **_argv;
55 char *_display_name = 0;
56 Display *_display = 0;
57
58 #ifdef   HAVE_SIGACTION
59 static void signalhandler(int sig)
60 #else //  HAVE_SIGACTION
61 static RETSIGTYPE signalhandler(int sig)
62 #endif // HAVE_SIGACTION
63 {
64   switch (sig) {
65   case SIGSEGV:
66     cout << "Segmentation fault. Aborting and dumping core.\n";
67     abort();
68   case SIGHUP:
69     cout << "Restarting on request.\n";
70     execvp(_argv[0], _argv);
71     execvp(basename(_argv[0]), _argv);
72   }
73   _shutdown = true;
74
75 #ifndef   HAVE_SIGACTION
76   // assume broken, braindead sysv signal semantics
77   signal(sig, (RETSIGTYPE (*)(int)) signalhandler);
78 #endif // HAVE_SIGACTION
79 }
80
81
82 int main(int, char **argv) {
83   _argv = argv;
84
85 #ifdef    HAVE_SIGACTION
86   struct sigaction action;
87
88   action.sa_handler = signalhandler;
89   action.sa_mask = sigset_t();
90   action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
91
92   sigaction(SIGPIPE, &action, NULL);
93   sigaction(SIGSEGV, &action, NULL);
94   sigaction(SIGFPE, &action, NULL);
95   sigaction(SIGTERM, &action, NULL);
96   sigaction(SIGINT, &action, NULL);
97   sigaction(SIGHUP, &action, NULL);
98 #else // !HAVE_SIGACTION
99   signal(SIGPIPE, (RETSIGTYPE (*)(int)) signalhandler);
100   signal(SIGSEGV, (RETSIGTYPE (*)(int)) signalhandler);
101   signal(SIGFPE, (RETSIGTYPE (*)(int)) signalhandler);
102   signal(SIGTERM, (RETSIGTYPE (*)(int)) signalhandler);
103   signal(SIGINT, (RETSIGTYPE (*)(int)) signalhandler);
104   signal(SIGHUP, (RETSIGTYPE (*)(int)) signalhandler);
105 #endif // HAVE_SIGACTION
106
107   _display = XOpenDisplay(_display_name);
108   if (! _display) {
109     cout << "Connection to X server '" << _display_name << "' failed.\n";
110     return 1;
111   }
112
113   while (! _shutdown) {
114     usleep(500);
115   }
116
117   XCloseDisplay(_display);
118   return 0;
119 }