1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
#include "stdafx.h"
#include "ScreenSaver.h"
namespace gui {
#if defined(WINDOWS)
ScreenSaver::ScreenSaver() {}
void ScreenSaver::platformInit() {}
void ScreenSaver::platformDestroy() {}
void ScreenSaver::inhibit() {
// ES_CONTINUOUS makes ES_DISPLAY_REQUIRED "permanent" until we say otherwise.
SetThreadExecutionState(ES_CONTINUOUS | ES_DISPLAY_REQUIRED);
}
void ScreenSaver::resume() {
// Just specifying ES_CONTINUOUS resets all other flags.
SetThreadExecutionState(ES_CONTINUOUS);
}
#elif defined(LINUX) && defined(GUI_GTK)
ScreenSaver::ScreenSaver() : sessionBus(NULL) {}
void ScreenSaver::platformInit() {
// Note 'g_bus_get_sync' seems to reuse a global connection if possible. This is good for
// us, since we don't have to look into getting the DBus connection some other way to avoid
// duplicate connections! (This is only heavily implied by the thread-safety discussion in
// the documentation, not stated explicitly)
sessionBus = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL);
}
void ScreenSaver::platformDestroy() {
if (sessionBus)
g_object_unref(sessionBus);
}
// Helper to print a GVariant.
static void print(GVariant *variant) {
GString *str = g_variant_print_string(variant, NULL, true);
PLN(str->str);
g_string_free(str, true);
}
void ScreenSaver::inhibit() {
// Note: 'wantCookie' might already be true. We get called again from init for example.
if (hasCookie)
return;
wantCookie = true;
if (initConnection()) {
// If this is the first call, we might not yet have established who to talk to. We have
// set 'wantCookie', that is enough for the init code to call us again later.
if (state == sInitializing)
return;
// Send a request!
sendInhibit();
} else {
// Start the backup code (works only on X11).
if (inhibitX11()) {
const guint interval = 10;
hasCookie = true;
cookie = g_timeout_add_seconds(interval, &ScreenSaver::backupTimer, this);
} else {
std::cerr << "Failed to inhibit the screensaver. No *.ScreenSaver clients found on the session DBus, "
<< "nor are we able to use the X11 fallback." << std::endl;
}
}
}
void ScreenSaver::resume() {
wantCookie = false;
// No cookie, no worries.
if (!hasCookie)
return;
if (state == sInitialized) {
// Return the cookie. Note: Since we have a cookie, we know that we have a name to connect to.
GVariant *params = g_variant_new("(u)", cookie);
const DBusName &name = names.back();
g_dbus_connection_call(sessionBus,
name.busName.c_str(),
name.path.c_str(),
name.interfaceName.c_str(),
"UnInhibit",
params,
NULL, // No result.
G_DBUS_CALL_FLAGS_NONE,
-1, // Default timeout
NULL, // No cancellation
NULL, // No callback
this);
} else {
// Backup solution: destroy the timer.
GSource *s = g_main_context_find_source_by_id(NULL, cookie);
if (s)
g_source_destroy(s);
}
cookie = 0;
hasCookie = false;
wantCookie = false;
}
bool ScreenSaver::initConnection() {
if (!sessionBus)
return false;
// Tried before and failed?
if (state == sNoRemoteEndpoint)
return false;
// Have we already started initialization?
if (state != sUnknown)
return true;
// We need to start initialization. Start by asking for all names on the bus, so that we can
// find and try out the promising ones.
g_dbus_connection_call(sessionBus,
"org.freedesktop.DBus",
"/org/freedesktop/DBus",
"org.freedesktop.DBus",
"ListNames",
NULL, // No parameters.
G_VARIANT_TYPE("(as)"), // Result, list of strings
G_DBUS_CALL_FLAGS_NONE,
-1, // Default timeout
NULL, // No cancellation
&ScreenSaver::nameListReceived,
this);
state = sInitializing;
return true;
}
void ScreenSaver::initDone() {
if (names.empty()) {
state = sNoRemoteEndpoint;
// If someone attempted to inhibit the screen saver, call 'inhibit' again to trigger
// the fallback or to request a cookie now.
if (wantCookie)
inhibit();
} else {
state = sInitialized;
}
}
void ScreenSaver::nameListReceived(GObject *source, GAsyncResult *result, gpointer data) {
ScreenSaver *me = (ScreenSaver *)data;
GError *error = NULL;
GVariant *value = g_dbus_connection_call_finish(G_DBUS_CONNECTION(source), result, &error);
if (error) {
std::wcerr << L"Failed to look up the screen saver service on the session bus: " << error->message << std::endl;
g_error_free(error);
}
if (value) {
me->extractEndpoints(value);
g_variant_unref(value);
}
// Note: Handles failures properly if the list of names is empty.
me->sendInhibit();
}
void ScreenSaver::sendInhibit() {
// During initialization?
if (names.empty()) {
initDone();
return;
}
// Ask the screen saver name by calling 'Inhibit' - we always want a cookie when we are
// initialized anyway.
GVariant *params = g_variant_new("(ss)", "Storm GUI framework", "Frame:inhibitScreenSaver");
const DBusName &name = names.back();
g_dbus_connection_call(sessionBus,
name.busName.c_str(),
name.path.c_str(),
name.interfaceName.c_str(),
"Inhibit",
params,
G_VARIANT_TYPE("(u)"),
G_DBUS_CALL_FLAGS_NONE,
-1, // Default timeout
NULL, // No cancellation
&ScreenSaver::cookieReceived,
this);
}
void ScreenSaver::cookieReceived(GObject *source, GAsyncResult *result, gpointer data) {
ScreenSaver *me = (ScreenSaver *)data;
GError *error = NULL;
GVariant *value = g_dbus_connection_call_finish(G_DBUS_CONNECTION(source), result, &error);
bool initializing = me->state == sInitializing;
if (value) {
if (g_variant_is_container(value) && g_variant_n_children(value) >= 1) {
GVariant *child = g_variant_get_child_value(value, 0);
me->cookie = g_variant_get_uint32(child);
me->hasCookie = true;
g_variant_unref(child);
}
g_variant_unref(value);
if (initializing) {
// Keep only the current name in the list.
me->names.erase(me->names.begin(), me->names.end() - 1);
// And we are done!
me->initDone();
}
if (me->hasCookie && !me->wantCookie) {
// Someone asked for the cookie to be cancelled already!
me->resume();
}
}
if (error) {
if (initializing) {
if (g_error_matches(error, G_DBUS_ERROR, G_DBUS_ERROR_SERVICE_UNKNOWN)) {
// OK to skip the error.
} else if (initializing) {
std::wcerr << L"Failed to communicate with " << me->names.back().busName.c_str()
<< L": " << error->message << L". Will try other names." << std::endl;
}
g_error_free(error);
// More to try out...
me->names.pop_back();
me->sendInhibit();
} else {
std::wcerr << L"Error requesting screen saver inhibit cookie: " << error->message << std::endl;
g_error_free(error);
}
}
}
static bool isScreenSaverName(const std::string &name) {
if (name.size() > 12 && name.substr(name.size() - 12) == ".ScreenSaver")
return true;
else
return false;
}
static ScreenSaver::DBusName dbusInfo(const std::string &name) {
std::string path = "/" + name;
for (size_t i = 0; i < path.size(); i++)
if (path[i] == '.')
path[i] = '/';
ScreenSaver::DBusName out = {
name,
path,
name,
};
return out;
}
void ScreenSaver::extractEndpoints(GVariant *tuple) {
if (!g_variant_is_container(tuple) || g_variant_n_children(tuple) < 1)
return;
// First candidate. We place it last (that's where we look first).
const std::string bestName = "org.freedesktop.ScreenSaver";
bool hasBestName = false;
GVariant *array = g_variant_get_child_value(tuple, 0);
size_t children = g_variant_n_children(array);
for (size_t i = 0; i < children; i++) {
GVariant *str = g_variant_get_child_value(array, i);
std::string name(g_variant_get_string(str, NULL));
g_variant_unref(str);
if (name == bestName)
hasBestName = true;
else if (isScreenSaverName(name))
names.push_back(dbusInfo(name));
}
g_variant_unref(array);
if (hasBestName)
names.push_back(dbusInfo(bestName));
}
bool ScreenSaver::inhibitX11() {
GdkDisplay *gdkDisplay = gdk_display_get_default();
if (gdkDisplay && GDK_IS_X11_DISPLAY(gdkDisplay)) {
Display *xDisplay = GDK_DISPLAY_XDISPLAY(gdkDisplay);
XResetScreenSaver(xDisplay);
return true;
} else {
return false;
}
}
gboolean ScreenSaver::backupTimer(gpointer data) {
ScreenSaver *me = (ScreenSaver *)data;
me->inhibitX11();
return G_SOURCE_CONTINUE;
}
#else
#error "Screen saver interaction is not defined for your platform."
#endif
}
|