From 62aa0c4415b65c66ba525ca942013762fa05ce20 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Fri, 24 Mar 2017 00:12:21 +0100 Subject: [PATCH] Split up dbus functions into separate file --- Makefile.common | 1 + gfx/common/dbus_common.c | 163 +++++++++++++++++++++++++++++++++++++++ gfx/common/dbus_common.h | 31 ++++++++ gfx/common/x11_common.c | 137 +------------------------------- gfx/common/x11_common.h | 4 - griffin/griffin.c | 1 + 6 files changed, 197 insertions(+), 140 deletions(-) create mode 100644 gfx/common/dbus_common.c create mode 100644 gfx/common/dbus_common.h diff --git a/Makefile.common b/Makefile.common index 71a3fd46cd..898003a5b1 100644 --- a/Makefile.common +++ b/Makefile.common @@ -669,6 +669,7 @@ endif ifeq ($(HAVE_X11), 1) OBJ += input/common/input_x11_common.o \ input/drivers/x11_input.o \ + gfx/common/dbus_common.o \ gfx/common/x11_common.o \ gfx/common/xinerama_common.o \ input/drivers_keyboard/keyboard_event_x11.o diff --git a/gfx/common/dbus_common.c b/gfx/common/dbus_common.c new file mode 100644 index 0000000000..4fd335a8f6 --- /dev/null +++ b/gfx/common/dbus_common.c @@ -0,0 +1,163 @@ +/* RetroArch - A frontend for libretro. + * Copyright (C) 2011-2017 - Daniel De Matteis + * + * RetroArch 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 Found- + * ation, either version 3 of the License, or (at your option) any later version. + * + * RetroArch 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 RetroArch. + * If not, see . + */ + +#ifdef HAVE_CONFIG_H +#include "../../config.h" +#endif + +#ifdef HAVE_DBUS +#include +static DBusConnection* dbus_connection = NULL; +static unsigned int dbus_screensaver_cookie = 0; +#endif + +#include "../../verbosity.h" + +void dbus_ensure_connection(void) +{ +#ifdef HAVE_DBUS + DBusError err; + int ret; + + dbus_error_init(&err); + + dbus_connection = dbus_bus_get_private(DBUS_BUS_SESSION, &err); + + if (dbus_error_is_set(&err)) + { + RARCH_LOG("[DBus]: Failed to get DBus connection. Screensaver will not be suspended via DBus.\n"); + dbus_error_free(&err); + } + + if (dbus_connection) + dbus_connection_set_exit_on_disconnect(dbus_connection, true); +#endif +} + +void dbus_close_connection(void) +{ +#ifdef HAVE_DBUS + if (!dbus_connection) + return; + + dbus_connection_close(dbus_connection); + dbus_connection_unref(dbus_connection); + dbus_connection = NULL; +#endif +} + +bool dbus_screensaver_inhibit(void) +{ + bool ret = false; +#ifdef HAVE_DBUS + const char *app = "RetroArch"; + const char *reason = "Playing a game"; + DBusMessage *msg = NULL; + DBusMessage *reply = NULL; + + if (!dbus_connection) + return false; /* DBus connection was not obtained */ + + if (dbus_screensaver_cookie > 0) + return true; /* Already inhibited */ + + msg = dbus_message_new_method_call("org.freedesktop.ScreenSaver", + "/org/freedesktop/ScreenSaver", + "org.freedesktop.ScreenSaver", + "Inhibit"); + + if (!msg) + return false; + + if (!dbus_message_append_args(msg, + DBUS_TYPE_STRING, &app, + DBUS_TYPE_STRING, &reason, + DBUS_TYPE_INVALID)) + { + dbus_message_unref(msg); + return false; + } + + reply = dbus_connection_send_with_reply_and_block(dbus_connection, + msg, 300, NULL); + + if (reply != NULL) + { + if (!dbus_message_get_args(reply, NULL, + DBUS_TYPE_UINT32, &dbus_screensaver_cookie, + DBUS_TYPE_INVALID)) + dbus_screensaver_cookie = 0; + else + ret = true; + + dbus_message_unref(reply); + } + + dbus_message_unref(msg); + + if (dbus_screensaver_cookie == 0) + { + RARCH_ERR("[DBus]: Failed to suspend screensaver via DBus.\n"); + } + else + { + RARCH_LOG("[DBus]: Suspended screensaver via DBus.\n"); + } + +#endif + + return ret; +} + +void dbus_screensaver_uninhibit(void) +{ +#ifdef HAVE_DBUS + DBusMessage *msg = NULL; + + if (!dbus_connection) + return; + + if (dbus_screensaver_cookie == 0) + return; + + msg = dbus_message_new_method_call("org.freedesktop.ScreenSaver", + "/org/freedesktop/ScreenSaver", + "org.freedesktop.ScreenSaver", + "UnInhibit"); + if (!msg) + return; + + dbus_message_append_args(msg, + DBUS_TYPE_UINT32, &dbus_screensaver_cookie, + DBUS_TYPE_INVALID); + + if (dbus_connection_send(dbus_connection, msg, NULL)) + dbus_connection_flush(dbus_connection); + dbus_message_unref(msg); + + dbus_screensaver_cookie = 0; +#endif +} + +/* Returns false when fallback should be attempted */ +bool dbus_suspend_screensaver(bool enable) +{ +#ifdef HAVE_DBUS + if (enable) + return dbus_screensaver_inhibit(); + dbus_screensaver_uninhibit(); +#endif + return false; +} diff --git a/gfx/common/dbus_common.h b/gfx/common/dbus_common.h new file mode 100644 index 0000000000..c8a4e5f3e4 --- /dev/null +++ b/gfx/common/dbus_common.h @@ -0,0 +1,31 @@ +/* RetroArch - A frontend for libretro. + * Copyright (C) 2011-2017 - Daniel De Matteis + * + * RetroArch 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 Found- + * ation, either version 3 of the License, or (at your option) any later version. + * + * RetroArch 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 RetroArch. + * If not, see . + */ + +#ifndef DBUS_COMMON_H__ +#define DBUS_COMMON_H__ + +#include + +void dbus_ensure_connection(void); + +void dbus_close_connection(void); + +bool dbus_screensaver_inhibit(void); + +void dbus_screensaver_uninhibit(void); + +bool dbus_suspend_screensaver(bool enable); + +#endif diff --git a/gfx/common/x11_common.c b/gfx/common/x11_common.c index 584ce3eec9..edcb33f4cc 100644 --- a/gfx/common/x11_common.c +++ b/gfx/common/x11_common.c @@ -40,11 +40,6 @@ #define MOVERESIZE_X_SHIFT 8 #define MOVERESIZE_Y_SHIFT 9 -#ifdef HAVE_DBUS -#include -static DBusConnection* dbus_connection = NULL; -static unsigned int dbus_screensaver_cookie = 0; -#endif static XF86VidModeModeInfo desktop_mode; static bool xdg_screensaver_available = true; @@ -66,133 +61,6 @@ static Atom g_x11_quit_atom; static XIM g_x11_xim; static XIC g_x11_xic; -#ifdef HAVE_DBUS -static void dbus_ensure_connection(void) -{ - DBusError err; - int ret; - - dbus_error_init(&err); - - dbus_connection = dbus_bus_get_private(DBUS_BUS_SESSION, &err); - - if (dbus_error_is_set(&err)) - { - RARCH_LOG("[DBus]: Failed to get DBus connection. Screensaver will not be suspended via DBus.\n"); - dbus_error_free(&err); - } - - if (dbus_connection) - dbus_connection_set_exit_on_disconnect(dbus_connection, true); -} - -static void dbus_close_connection(void) -{ - if (!dbus_connection) - return; - - dbus_connection_close(dbus_connection); - dbus_connection_unref(dbus_connection); - dbus_connection = NULL; -} - -static bool dbus_screensaver_inhibit(void) -{ - const char *app = "RetroArch"; - const char *reason = "Playing a game"; - DBusMessage *msg = NULL; - DBusMessage *reply = NULL; - bool ret = false; - - if (!dbus_connection) - return false; /* DBus connection was not obtained */ - - if (dbus_screensaver_cookie > 0) - return true; /* Already inhibited */ - - msg = dbus_message_new_method_call("org.freedesktop.ScreenSaver", - "/org/freedesktop/ScreenSaver", - "org.freedesktop.ScreenSaver", - "Inhibit"); - - if (!msg) - return false; - - if (!dbus_message_append_args(msg, - DBUS_TYPE_STRING, &app, - DBUS_TYPE_STRING, &reason, - DBUS_TYPE_INVALID)) - { - dbus_message_unref(msg); - return false; - } - - reply = dbus_connection_send_with_reply_and_block(dbus_connection, - msg, 300, NULL); - - if (reply != NULL) - { - if (!dbus_message_get_args(reply, NULL, - DBUS_TYPE_UINT32, &dbus_screensaver_cookie, - DBUS_TYPE_INVALID)) - dbus_screensaver_cookie = 0; - else - ret = true; - - dbus_message_unref(reply); - } - - dbus_message_unref(msg); - - if (dbus_screensaver_cookie == 0) - { - RARCH_ERR("[DBus]: Failed to suspend screensaver via DBus.\n"); - } - else - { - RARCH_LOG("[DBus]: Suspended screensaver via DBus.\n"); - } - - return ret; -} - -static void dbus_screensaver_uninhibit(void) -{ - DBusMessage *msg = NULL; - - if (!dbus_connection) - return; - - if (dbus_screensaver_cookie == 0) - return; - - msg = dbus_message_new_method_call("org.freedesktop.ScreenSaver", - "/org/freedesktop/ScreenSaver", - "org.freedesktop.ScreenSaver", - "UnInhibit"); - if (!msg) - return; - - dbus_message_append_args(msg, - DBUS_TYPE_UINT32, &dbus_screensaver_cookie, - DBUS_TYPE_INVALID); - - if (dbus_connection_send(dbus_connection, msg, NULL)) - dbus_connection_flush(dbus_connection); - dbus_message_unref(msg); - - dbus_screensaver_cookie = 0; -} - -/* Returns false when fallback should be attempted */ -bool x11_suspend_screensaver_dbus(bool enable) -{ - if (enable) return dbus_screensaver_inhibit(); - dbus_screensaver_uninhibit(); - return false; -} -#endif - static void x11_hide_mouse(Display *dpy, Window win) { static char bm_no_data[] = {0, 0, 0, 0, 0, 0, 0, 0}; @@ -319,7 +187,7 @@ void x11_suspend_screensaver_xdg_screensaver(Window wnd, bool enable) void x11_suspend_screensaver(Window wnd, bool enable) { #ifdef HAVE_DBUS - if (x11_suspend_screensaver_dbus(enable)) + if (dbus_suspend_screensaver(enable)) return; #endif x11_suspend_screensaver_xdg_screensaver(wnd, enable); @@ -628,10 +496,7 @@ bool x11_connect(void) return false; } -#ifdef HAVE_DBUS dbus_ensure_connection(); -#endif - return true; } diff --git a/gfx/common/x11_common.h b/gfx/common/x11_common.h index 50f509fe4d..ad745a70a7 100644 --- a/gfx/common/x11_common.h +++ b/gfx/common/x11_common.h @@ -17,10 +17,6 @@ #ifndef X11_COMMON_H__ #define X11_COMMON_H__ -#ifdef HAVE_CONFIG_H -#include "../../config.h" -#endif - #include #include diff --git a/griffin/griffin.c b/griffin/griffin.c index 6cdaa868fb..a6e5897ca5 100644 --- a/griffin/griffin.c +++ b/griffin/griffin.c @@ -209,6 +209,7 @@ VIDEO CONTEXT #if defined(HAVE_X11) #include "../gfx/common/x11_common.c" +#include "../gfx/common/dbus_common.c" #include "../gfx/common/xinerama_common.c" #ifndef HAVE_OPENGLES