Parent: [b28eaf] (diff)

Download this file

x11mon.cpp    89 lines (79 with data), 1.8 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#ifndef TEST_X11MON
/* Copyright (C) 2006 J.F.Dockes */
// Poll state of X11 connectibility (to detect end of user session).
#include "autoconfig.h"
#ifndef DISABLE_X11MON
#include <stdio.h>
#include <X11/Xlib.h>
#include <signal.h>
#include <setjmp.h>
#define DODEBUG
#ifdef DODEBUG
#define DEBUG(X) fprintf X
#else
#define DEBUG(X) fprintf X
#endif
static Display *m_display;
static bool m_ok;
static jmp_buf env;
static int errorHandler(Display *, XErrorEvent*)
{
DEBUG((stderr, "x11mon: error handler: Got X11 error\n"));
m_ok = false;
return 0;
}
static int ioErrorHandler(Display *)
{
DEBUG((stderr, "x11mon: error handler: Got X11 IO error\n"));
m_ok = false;
m_display = 0;
longjmp(env, 1);
}
bool x11IsAlive()
{
// Xlib always exits on IO errors. Need a setjmp to avoid this (will jump
// from IO error handler instead of returning).
if (setjmp(env)) {
DEBUG((stderr, "x11IsAlive: Long jump\n"));
return false;
}
if (m_display == 0) {
signal(SIGPIPE, SIG_IGN);
XSetErrorHandler(errorHandler);
XSetIOErrorHandler(ioErrorHandler);
if ((m_display = XOpenDisplay(0)) == 0) {
DEBUG((stderr, "x11IsAlive: cant connect\n"));
m_ok = false;
return false;
}
}
m_ok = true;
bool sync= XSynchronize(m_display, true);
XNoOp(m_display);
XSynchronize(m_display, sync);
return m_ok;
}
#else
bool x11IsAlive()
{
return true;
}
#endif /* DISABLE_X11MON */
#else
// Test driver
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "x11mon.h"
int main(int argc, char **argv)
{
for (;;) {
if (!x11IsAlive()) {
fprintf(stderr, "x11IsAlive failed\n");
} else {
fprintf(stderr, "x11IsAlive Ok\n");
}
sleep(1);
}
}
#endif