small x11mon cleanup

This commit is contained in:
Jean-Francois Dockes 2020-11-04 10:30:42 +01:00
parent c90f418db6
commit 93951a3509
3 changed files with 101 additions and 64 deletions

View File

@ -38,19 +38,10 @@ AM_CPPFLAGS = -Wall -Wno-unused -std=c++11 \
$(DEFS)
noinst_PROGRAMS = textsplit utf8iter fstreewalk rclconfig hldata unac mbox \
circache wipedir mimetype pathut fileudi
circache wipedir mimetype pathut fileudi x11mon
textsplit_SOURCES = trtextsplit.cpp
textsplit_LDADD = ../librecoll.la
pathut_SOURCES = trpathut.cpp
pathut_LDADD = ../librecoll.la
mimetype_SOURCES = trmimetype.cpp
mimetype_LDADD = ../librecoll.la
utf8iter_SOURCES = trutf8iter.cpp
utf8iter_LDADD = ../librecoll.la
circache_SOURCES = trcircache.cpp
circache_LDADD = ../librecoll.la
fileudi_SOURCES = trfileudi.cpp
fileudi_LDADD = ../librecoll.la
@ -58,20 +49,32 @@ fileudi_LDADD = ../librecoll.la
fstreewalk_SOURCES = trfstreewalk.cpp
fstreewalk_LDADD = ../librecoll.la
rclconfig_SOURCES = trrclconfig.cpp
rclconfig_LDADD = ../librecoll.la
hldata_SOURCES = trhldata.cpp
hldata_LDADD = ../librecoll.la
unac_SOURCES = trunac.cpp
unac_LDADD = ../librecoll.la
mbox_SOURCES = trmbox.cpp
mbox_LDADD = ../librecoll.la
circache_SOURCES = trcircache.cpp
circache_LDADD = ../librecoll.la
mimetype_SOURCES = trmimetype.cpp
mimetype_LDADD = ../librecoll.la
pathut_SOURCES = trpathut.cpp
pathut_LDADD = ../librecoll.la
rclconfig_SOURCES = trrclconfig.cpp
rclconfig_LDADD = ../librecoll.la
textsplit_SOURCES = trtextsplit.cpp
textsplit_LDADD = ../librecoll.la
unac_SOURCES = trunac.cpp
unac_LDADD = ../librecoll.la
utf8iter_SOURCES = trutf8iter.cpp
utf8iter_LDADD = ../librecoll.la
wipedir_SOURCES = trwipedir.cpp
wipedir_LDADD = ../librecoll.la
x11mon_SOURCES = trx11mon.cpp
x11mon_LDADD = ../utils/x11mon.o ../librecoll.la -lX11

View File

@ -0,0 +1,38 @@
/* Copyright (C) 2006-2020 J.F.Dockes
*
* License: GPL 2.1
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#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);
}
}

View File

@ -1,19 +1,34 @@
#ifndef TEST_X11MON
/* Copyright (C) 2006 J.F.Dockes */
/* Copyright (C) 2006-2020 J.F.Dockes
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
// Poll state of X11 connectibility (to detect end of user session).
#include "autoconfig.h"
#ifndef DISABLE_X11MON
#include "x11mon.h"
#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
#include "log.h"
static Display *m_display;
static bool m_ok;
@ -21,13 +36,14 @@ static jmp_buf env;
static int errorHandler(Display *, XErrorEvent*)
{
DEBUG((stderr, "x11mon: error handler: Got X11 error\n"));
LOGERR("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"));
LOGERR("x11mon: error handler: Got X11 IO error\n");
m_ok = false;
m_display = 0;
longjmp(env, 1);
@ -38,18 +54,18 @@ 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;
LOGDEB("x11IsAlive: got long jump: X11 error\n");
return false;
}
if (m_display == 0) {
signal(SIGPIPE, SIG_IGN);
XSetErrorHandler(errorHandler);
XSetIOErrorHandler(ioErrorHandler);
if ((m_display = XOpenDisplay(0)) == 0) {
LOGERR("x11IsAlive: cant connect\n");
m_ok = false;
return false;
}
}
m_ok = true;
bool sync= XSynchronize(m_display, true);
@ -57,32 +73,12 @@ bool x11IsAlive()
XSynchronize(m_display, sync);
return m_ok;
}
#else
#else // DISABLE_X11MON->
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