From 4a2d4b3911488fc73d3443ec569093860e37be38 Mon Sep 17 00:00:00 2001 From: George McMullen Date: Tue, 15 Oct 2019 04:13:06 -0700 Subject: [PATCH] Check that m_display is not null to prevent crash (#5725) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Check that m_display is not null to prevent crash https://mametesters.org/view.php?id=7372 * Probe method to check if X11 is actually being used As per: https://github.com/mamedev/mame/pull/5725#issuecomment-540004475 this will help MAME verify X11 has no inputs when X11 is not actually being used (e.g. on RetroPie where SDL display is RPI). * Fix issue where a lightgun with no name would return nullptr As suggested by https://github.com/mamedev/mame/pull/5725#issuecomment-539914514 , a bug in create_lightgun_device() returned nullptr if the lightgun had no name. Now it will create the device with a name using the lightgun's device index * Change older m_display change to assert This module can now be probed and disabled correctly if X11 is not being used. Removed the if statements that would be called every cycle (and fail silently) in favor of asserts, as MAME does not currently handle dynamic hardware configuration changes. * Fixing semicolons in asserts that were ifs --- src/osd/modules/input/input_x11.cpp | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/osd/modules/input/input_x11.cpp b/src/osd/modules/input/input_x11.cpp index 07ade15c764..6b66220e32a 100644 --- a/src/osd/modules/input/input_x11.cpp +++ b/src/osd/modules/input/input_x11.cpp @@ -312,6 +312,9 @@ public: std::lock_guard scope_lock(m_lock); XEvent xevent; + // If X11 has become invalid for some reason, XPending will crash. Assert instead. + assert(m_display != nullptr); + //Get XInput events while (XPending(m_display) != 0) { @@ -443,6 +446,17 @@ public: { } + virtual bool probe() override + { + // If there is no X server, X11 lightguns cannot be supported + if (XOpenDisplay(nullptr) == nullptr) + { + return false; + } + + return true; + } + void input_init(running_machine &machine) override { int index; @@ -454,6 +468,9 @@ public: x11_event_manager::instance().initialize(); m_display = x11_event_manager::instance().display(); + // If the X server has become invalid, a crash can occur + assert(m_display != nullptr); + // Loop through all 8 possible devices for (index = 0; index < 8; index++) { @@ -573,10 +590,9 @@ private: if (m_lightgun_map.initialized) { snprintf(tempname, ARRAY_LENGTH(tempname), "NC%d", index); - devicelist()->create_device(machine, tempname, tempname, *this); - } - - return nullptr; + return devicelist()->create_device(machine, tempname, tempname, *this); + } else + return nullptr; } return devicelist()->create_device(machine, m_lightgun_map.map[index].name.c_str(), m_lightgun_map.map[index].name.c_str(), *this);