osd/modules/input: Fixed a couple of X11 resource leaks.

This commit is contained in:
Vas Crabb 2020-11-16 16:14:18 +11:00
parent 162a7b9157
commit fee9a29a57
3 changed files with 53 additions and 38 deletions

View File

@ -267,15 +267,30 @@ public:
class x11_event_manager : public event_manager_t<x11_event_handler> class x11_event_manager : public event_manager_t<x11_event_handler>
{ {
private: private:
Display * m_display; struct x_cleanup
{
void operator()(Display *ptr) const
{
if (ptr)
XCloseDisplay(ptr);
}
void operator()(XExtensionVersion *ptr) const
{
if (ptr)
XFree(ptr);
}
};
x11_event_manager() template <typename T> using x_ptr = std::unique_ptr<T, x_cleanup>;
: event_manager_t(),
m_display(nullptr) x_ptr<Display> m_display;
x11_event_manager() : event_manager_t()
{ {
} }
public: public:
Display * display() const { return m_display; } Display * display() const { return m_display.get(); }
static x11_event_manager& instance() static x11_event_manager& instance()
{ {
@ -287,18 +302,18 @@ public:
{ {
std::lock_guard<std::mutex> scope_lock(m_lock); std::lock_guard<std::mutex> scope_lock(m_lock);
if (m_display != nullptr) if (m_display)
return 0; return 0;
m_display = XOpenDisplay(nullptr); m_display.reset(XOpenDisplay(nullptr));
if (m_display == nullptr) if (!m_display)
{ {
osd_printf_verbose("Unable to connect to X server\n"); osd_printf_verbose("Unable to connect to X server\n");
return -1; return -1;
} }
XExtensionVersion *version = XGetExtensionVersion(m_display, INAME); x_ptr<XExtensionVersion> version(XGetExtensionVersion(m_display.get(), INAME));
if (!version || (version == reinterpret_cast<XExtensionVersion*>(NoSuchExtension))) if (!version || (version.get() == reinterpret_cast<XExtensionVersion *>(NoSuchExtension)))
{ {
osd_printf_verbose("xinput extension not available!\n"); osd_printf_verbose("xinput extension not available!\n");
return -1; return -1;
@ -313,21 +328,21 @@ public:
XEvent xevent; XEvent xevent;
// If X11 has become invalid for some reason, XPending will crash. Assert instead. // If X11 has become invalid for some reason, XPending will crash. Assert instead.
assert(m_display != nullptr); assert(m_display);
//Get XInput events //Get XInput events
while (XPending(m_display) != 0) while (XPending(m_display.get()) != 0)
{ {
XNextEvent(m_display, &xevent); XNextEvent(m_display.get(), &xevent);
// Find all subscribers for the event type // Find all subscribers for the event type
auto subscribers = m_subscription_index.equal_range(xevent.type); auto const subscribers = m_subscription_index.equal_range(xevent.type);
// Dispatch the events // Dispatch the events
std::for_each(subscribers.first, subscribers.second, [&xevent](auto &pair) std::for_each(
{ subscribers.first,
pair.second->handle_event(xevent); subscribers.second,
}); [&xevent] (auto &pair) { pair.second->handle_event(xevent); });
} }
} }
}; };

View File

@ -20,8 +20,7 @@ class pm_module : public osd_module, public midi_module
{ {
public: public:
pm_module() pm_module() : osd_module(OSD_MIDI_PROVIDER, "pm"), midi_module()
: osd_module(OSD_MIDI_PROVIDER, "pm"), midi_module()
{ {
} }
virtual ~pm_module() { } virtual ~pm_module() { }

View File

@ -440,25 +440,25 @@ void sdl_osd_interface::init(running_machine &machine)
osd_setenv(SDLENV_VIDEODRIVER, stemp, 1); osd_setenv(SDLENV_VIDEODRIVER, stemp, 1);
} }
stemp = options().render_driver(); stemp = options().render_driver();
if (stemp != nullptr) if (stemp != nullptr)
{
if (strcmp(stemp, OSDOPTVAL_AUTO) != 0)
{ {
if (strcmp(stemp, OSDOPTVAL_AUTO) != 0) osd_printf_verbose("Setting SDL renderdriver '%s' ...\n", stemp);
{ //osd_setenv(SDLENV_RENDERDRIVER, stemp, 1);
osd_printf_verbose("Setting SDL renderdriver '%s' ...\n", stemp); SDL_SetHint(SDL_HINT_RENDER_DRIVER, stemp);
//osd_setenv(SDLENV_RENDERDRIVER, stemp, 1);
SDL_SetHint(SDL_HINT_RENDER_DRIVER, stemp);
}
else
{
#if defined(SDLMAME_WIN32)
// OpenGL renderer has less issues with mode switching on windows
osd_printf_verbose("Setting SDL renderdriver '%s' ...\n", "opengl");
//osd_setenv(SDLENV_RENDERDRIVER, stemp, 1);
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");
#endif
}
} }
else
{
#if defined(SDLMAME_WIN32)
// OpenGL renderer has less issues with mode switching on windows
osd_printf_verbose("Setting SDL renderdriver '%s' ...\n", "opengl");
//osd_setenv(SDLENV_RENDERDRIVER, stemp, 1);
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");
#endif
}
}
/* Set the SDL environment variable for drivers wanting to load the /* Set the SDL environment variable for drivers wanting to load the
* lib at startup. * lib at startup.
@ -491,7 +491,8 @@ void sdl_osd_interface::init(running_machine &machine)
/* Initialize SDL */ /* Initialize SDL */
if (SDL_InitSubSystem(SDL_INIT_VIDEO)) { if (SDL_InitSubSystem(SDL_INIT_VIDEO))
{
osd_printf_error("Could not initialize SDL %s\n", SDL_GetError()); osd_printf_error("Could not initialize SDL %s\n", SDL_GetError());
exit(-1); exit(-1);
} }