1 /* RetroArch - A frontend for libretro.
2 * Copyright (C) 2011-2017 - Daniel De Matteis
3 *
4 * RetroArch is free software: you can redistribute it and/or modify it under the terms
5 * of the GNU General Public License as published by the Free Software Found-
6 * ation, either version 3 of the License, or (at your option) any later version.
7 *
8 * RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10 * PURPOSE. See the GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License along with RetroArch.
13 * If not, see <http://www.gnu.org/licenses/>.
14 */
15
16 #ifdef HAVE_CONFIG_H
17 #include "../../config.h"
18 #endif
19
20 #ifdef HAVE_DBUS
21 #include <dbus/dbus.h>
22 /* TODO/FIXME - static globals */
23 static DBusConnection* dbus_connection = NULL;
24 static unsigned int dbus_screensaver_cookie = 0;
25 #endif
26
27 #include "../../verbosity.h"
28
dbus_ensure_connection(void)29 void dbus_ensure_connection(void)
30 {
31 #ifdef HAVE_DBUS
32 DBusError err;
33 int ret;
34
35 dbus_error_init(&err);
36
37 dbus_connection = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
38
39 if (dbus_error_is_set(&err))
40 {
41 RARCH_LOG("[DBus]: Failed to get DBus connection. Screensaver will not be suspended via DBus.\n");
42 dbus_error_free(&err);
43 }
44
45 if (dbus_connection)
46 dbus_connection_set_exit_on_disconnect(dbus_connection, true);
47 #endif
48 }
49
dbus_close_connection(void)50 void dbus_close_connection(void)
51 {
52 #ifdef HAVE_DBUS
53 if (!dbus_connection)
54 return;
55
56 dbus_connection_close(dbus_connection);
57 dbus_connection_unref(dbus_connection);
58 dbus_connection = NULL;
59 #endif
60 }
61
dbus_screensaver_inhibit(void)62 bool dbus_screensaver_inhibit(void)
63 {
64 bool ret = false;
65 #ifdef HAVE_DBUS
66 const char *app = "RetroArch";
67 const char *reason = "Playing a game";
68 DBusMessage *msg = NULL;
69 DBusMessage *reply = NULL;
70
71 if (!dbus_connection)
72 return false; /* DBus connection was not obtained */
73
74 if (dbus_screensaver_cookie > 0)
75 return true; /* Already inhibited */
76
77 msg = dbus_message_new_method_call("org.freedesktop.ScreenSaver",
78 "/org/freedesktop/ScreenSaver",
79 "org.freedesktop.ScreenSaver",
80 "Inhibit");
81
82 if (!msg)
83 return false;
84
85 if (!dbus_message_append_args(msg,
86 DBUS_TYPE_STRING, &app,
87 DBUS_TYPE_STRING, &reason,
88 DBUS_TYPE_INVALID))
89 {
90 dbus_message_unref(msg);
91 return false;
92 }
93
94 reply = dbus_connection_send_with_reply_and_block(dbus_connection,
95 msg, 300, NULL);
96
97 if (reply)
98 {
99 if (!dbus_message_get_args(reply, NULL,
100 DBUS_TYPE_UINT32, &dbus_screensaver_cookie,
101 DBUS_TYPE_INVALID))
102 dbus_screensaver_cookie = 0;
103 else
104 ret = true;
105
106 dbus_message_unref(reply);
107 }
108
109 dbus_message_unref(msg);
110
111 if (dbus_screensaver_cookie == 0)
112 {
113 RARCH_ERR("[DBus]: Failed to suspend screensaver via DBus.\n");
114 }
115 else
116 {
117 RARCH_LOG("[DBus]: Suspended screensaver via DBus.\n");
118 }
119
120 #endif
121
122 return ret;
123 }
124
dbus_screensaver_uninhibit(void)125 void dbus_screensaver_uninhibit(void)
126 {
127 #ifdef HAVE_DBUS
128 DBusMessage *msg = NULL;
129
130 if (!dbus_connection)
131 return;
132
133 if (dbus_screensaver_cookie == 0)
134 return;
135
136 msg = dbus_message_new_method_call("org.freedesktop.ScreenSaver",
137 "/org/freedesktop/ScreenSaver",
138 "org.freedesktop.ScreenSaver",
139 "UnInhibit");
140 if (!msg)
141 return;
142
143 dbus_message_append_args(msg,
144 DBUS_TYPE_UINT32, &dbus_screensaver_cookie,
145 DBUS_TYPE_INVALID);
146
147 if (dbus_connection_send(dbus_connection, msg, NULL))
148 dbus_connection_flush(dbus_connection);
149 dbus_message_unref(msg);
150
151 dbus_screensaver_cookie = 0;
152 #endif
153 }
154
155 /* Returns false when fallback should be attempted */
dbus_suspend_screensaver(bool enable)156 bool dbus_suspend_screensaver(bool enable)
157 {
158 #ifdef HAVE_DBUS
159 if (enable)
160 return dbus_screensaver_inhibit();
161 dbus_screensaver_uninhibit();
162 #endif
163 return false;
164 }
165