From 3edbc0039787d38a5ca307af4dc6f16ea8a63fe4 Mon Sep 17 00:00:00 2001 From: walker0643 <> Date: Sun, 4 Mar 2018 14:48:15 -0500 Subject: [PATCH] fix X11 crash if DISPLAY is not valid --- CMakeLists.txt | 2 +- src/gui/CMakeLists.txt | 6 ++++-- src/gui/src/DisplayIsValid.cpp | 14 ++++++++++++++ src/gui/src/DisplayIsValid.h | 5 +++++ src/gui/src/main.cpp | 9 +++++++++ 5 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 src/gui/src/DisplayIsValid.cpp create mode 100644 src/gui/src/DisplayIsValid.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 7e182f71..27dccc96 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -212,7 +212,7 @@ if (UNIX) if (HAVE_Xtst) # Xtxt depends on X11. - set (HAVE_X11) + set (HAVE_X11 1) list (APPEND libs Xtst X11) else() diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index 4a7e291a..a5dc9874 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -39,8 +39,10 @@ elseif (APPLE) elseif (${CMAKE_SYSTEM_NAME} MATCHES "Linux" OR ${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD") target_link_libraries (barrier dns_sd) -else() - target_link_libraries (barrier) +endif() + +if (HAVE_X11) + target_link_libraries (barrier X11) endif() if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") diff --git a/src/gui/src/DisplayIsValid.cpp b/src/gui/src/DisplayIsValid.cpp new file mode 100644 index 00000000..5685c42d --- /dev/null +++ b/src/gui/src/DisplayIsValid.cpp @@ -0,0 +1,14 @@ +#ifdef WINAPI_XWINDOWS + +#include "DisplayIsValid.h" +#include + +bool display_is_valid() +{ + auto dsp = XOpenDisplay(NULL); + if (dsp != NULL) + XCloseDisplay(dsp); + return dsp != NULL; +} + +#endif diff --git a/src/gui/src/DisplayIsValid.h b/src/gui/src/DisplayIsValid.h new file mode 100644 index 00000000..d5fcac83 --- /dev/null +++ b/src/gui/src/DisplayIsValid.h @@ -0,0 +1,5 @@ +#pragma once + +#ifdef WINAPI_XWINDOWS +bool display_is_valid(); +#endif diff --git a/src/gui/src/main.cpp b/src/gui/src/main.cpp index 1dbd53ab..76a7d1a2 100644 --- a/src/gui/src/main.cpp +++ b/src/gui/src/main.cpp @@ -23,6 +23,7 @@ #include "MainWindow.h" #include "AppConfig.h" #include "SetupWizard.h" +#include "DisplayIsValid.h" #include #include @@ -54,6 +55,14 @@ bool checkMacAssistiveDevices(); int main(int argc, char* argv[]) { +#ifdef WINAPI_XWINDOWS + // QApplication's constructor will call a fscking abort() if + // DISPLAY is bad. Let's check it first and handle it gracefully + if (!display_is_valid()) { + fprintf(stderr, "The Barrier GUI requires a display. Quitting...\n"); + return 1; + } +#endif #ifdef Q_OS_DARWIN /* Workaround for QTBUG-40332 - "High ping when QNetworkAccessManager is instantiated" */ ::setenv ("QT_BEARER_POLL_TIMEOUT", "-1", 1);