From 93951a3509ce3a823c0aefd5925523f66b84d986 Mon Sep 17 00:00:00 2001 From: Jean-Francois Dockes Date: Wed, 4 Nov 2020 10:30:42 +0100 Subject: [PATCH] small x11mon cleanup --- src/testmains/Makefile.am | 43 ++++++++++--------- src/testmains/trx11mon.cpp | 38 +++++++++++++++++ src/utils/x11mon.cpp | 84 ++++++++++++++++++-------------------- 3 files changed, 101 insertions(+), 64 deletions(-) create mode 100644 src/testmains/trx11mon.cpp diff --git a/src/testmains/Makefile.am b/src/testmains/Makefile.am index ee1e0a2d..aeefa5d9 100644 --- a/src/testmains/Makefile.am +++ b/src/testmains/Makefile.am @@ -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 diff --git a/src/testmains/trx11mon.cpp b/src/testmains/trx11mon.cpp new file mode 100644 index 00000000..420dedee --- /dev/null +++ b/src/testmains/trx11mon.cpp @@ -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 +#include +#include + +#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); + } +} + diff --git a/src/utils/x11mon.cpp b/src/utils/x11mon.cpp index eb7d92d9..145acae5 100644 --- a/src/utils/x11mon.cpp +++ b/src/utils/x11mon.cpp @@ -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 #include #include #include -#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 -#include -#include - -#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