Explorar o código

Merge pull request #96 from mdiluz/disable-screensaver

Add code to disable the screensaver during Game Mode
Alex Smith %!s(int64=6) %!d(string=hai) anos
pai
achega
a725ae21a0
Modificáronse 3 ficheiros con 89 adicións e 0 borrados
  1. 77 0
      daemon/dbus_messaging.c
  2. 5 0
      daemon/dbus_messaging.h
  3. 7 0
      daemon/gamemode.c

+ 77 - 0
daemon/dbus_messaging.c

@@ -187,3 +187,80 @@ void game_mode_context_loop(GameModeContext *context)
 		}
 	}
 }
+
+/**
+ * Attempts to inhibit the screensaver
+ * Uses the "org.freedesktop.ScreenSaver" interface
+ */
+static unsigned int screensaver_inhibit_cookie = 0;
+int game_mode_inhibit_screensaver(bool inhibit)
+{
+	const char *service = "org.freedesktop.ScreenSaver";
+	const char *object_path = "/org/freedesktop/ScreenSaver";
+	const char *interface = "org.freedesktop.ScreenSaver";
+	const char *function = inhibit ? "Inhibit" : "UnInhibit";
+
+	sd_bus_message *msg = NULL;
+	sd_bus *bus = NULL;
+	sd_bus_error err;
+	memset(&err, 0, sizeof(sd_bus_error));
+
+	int result = -1;
+
+	// Open the user bus
+	int ret = sd_bus_open_user(&bus);
+	if (ret < 0) {
+		LOG_ERROR("Could not connect to user bus: %s\n", strerror(-ret));
+		return -1;
+	}
+
+	if (inhibit) {
+		ret = sd_bus_call_method(bus,
+		                         service,
+		                         object_path,
+		                         interface,
+		                         function,
+		                         &err,
+		                         &msg,
+		                         "ss",
+		                         "com.feralinteractive.GameMode",
+		                         "GameMode Activated");
+	} else {
+		ret = sd_bus_call_method(bus,
+		                         service,
+		                         object_path,
+		                         interface,
+		                         function,
+		                         &err,
+		                         &msg,
+		                         "u",
+		                         screensaver_inhibit_cookie);
+	}
+
+	if (ret < 0) {
+		LOG_ERROR(
+		    "Could not call %s on %s: %s\n"
+		    "\t%s\n"
+		    "\t%s\n",
+		    function,
+		    service,
+		    strerror(-ret),
+		    err.name,
+		    err.message);
+	} else if (inhibit) {
+		// Read the reply
+		ret = sd_bus_message_read(msg, "u", &screensaver_inhibit_cookie);
+		if (ret < 0) {
+			LOG_ERROR("Failure to parse response from %s on %s: %s\n",
+			          function,
+			          service,
+			          strerror(-ret));
+		} else {
+			result = 0;
+		}
+	} else {
+		result = 0;
+	}
+
+	return result;
+}

+ 5 - 0
daemon/dbus_messaging.h

@@ -39,3 +39,8 @@ POSSIBILITY OF SUCH DAMAGE.
  * Run the main D-BUS loop "forever"
  */
 void game_mode_context_loop(GameModeContext *context);
+
+/**
+ * Inhibit the screensaver
+ */
+int game_mode_inhibit_screensaver(bool inhibit);

+ 7 - 0
daemon/gamemode.c

@@ -33,6 +33,7 @@ POSSIBILITY OF SUCH DAMAGE.
 
 #include "gamemode.h"
 #include "daemon_config.h"
+#include "dbus_messaging.h"
 #include "governors-query.h"
 #include "governors.h"
 #include "helpers.h"
@@ -202,6 +203,9 @@ static void game_mode_context_enter(GameModeContext *self)
 			memset(self->initial_cpu_mode, 0, sizeof(self->initial_cpu_mode));
 		}
 	}
+
+	/* Inhibit the screensaver */
+	game_mode_inhibit_screensaver(true);
 }
 
 /**
@@ -215,6 +219,9 @@ static void game_mode_context_leave(GameModeContext *self)
 	LOG_MSG("Leaving Game Mode...\n");
 	sd_notifyf(0, "STATUS=%sGameMode is currently deactivated.%s\n", "\x1B[1;36m", "\x1B[0m");
 
+	/* UnInhibit the screensaver */
+	game_mode_inhibit_screensaver(false);
+
 	/* Reset the governer state back to initial */
 	if (self->initial_cpu_mode[0] != '\0') {
 		/* Choose the governor to reset to, using the config to override */