Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fernandoherreradelasheras/e66a56ad501f93d1aa5ca94c3753b185 to your computer and use it in GitHub Desktop.
Save fernandoherreradelasheras/e66a56ad501f93d1aa5ca94c3753b185 to your computer and use it in GitHub Desktop.
mpv pluging to inhibit screensaver while playing a video using freedesktop dbus api
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <gio/gio.h>
#include <mpv/client.h>
/**
* mpv pluging to inhibit screensaver while playing a video on GNOME Desktop
* or any other not implementing Wayland "idle-inihibit" protocole
*
* compile and install it with:
* gcc -o ~/.config/mpv/scripts/mpv-freedesktop-screensaver-plugin.so mpv-freedesktop-screensaver-plugin.c `pkg-config --cflags --libs glib-2.0` -pthread -shared -fPIC
*
**/
guint32 inhibit(GDBusProxy *proxy) {
GVariant *result;
GError *error = NULL;
result = g_dbus_proxy_call_sync(proxy,
"Inhibit",
g_variant_new ("(ss)", "mpv", "playing video"),
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error);
if (error != NULL) {
printf("Cannot inhibit screensaver: %s\n", error->message);
g_error_free (error);
return 0;
} else {
guint32 cookie;
g_variant_get(result, "(u)", &cookie);
g_variant_unref(result);
printf("Screensaver inhibited with cookie: %ld\n", cookie);
return cookie;
}
}
guint32 uninhibit(GDBusProxy *proxy, guint32 cookie) {
GVariant *result;
GError *error = NULL;
result = g_dbus_proxy_call_sync(proxy,
"UnInhibit",
g_variant_new("(u)", cookie),
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error);
if (error != NULL) {
printf("Cannot uninhibit screensaver: %s\n", error->message);
g_error_free (error);
return cookie;
} else {
g_variant_unref(result);
printf("Screensaver uninhibited\n");
return 0;
}
}
int mpv_open_cplugin(mpv_handle *handle)
{
printf("Hello world from C plugin '%s'!\n", mpv_client_name(handle));
GDBusProxy *proxy;
GDBusConnection *conn;
GError *error = NULL;
guint32 cookie = 0;
conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
if (error != NULL) {
printf("Cannot get dbus sesstion bus: %s\n", error->message);
g_error_free (error);
return 1;
}
proxy = g_dbus_proxy_new_sync(conn,
G_DBUS_PROXY_FLAGS_NONE,
NULL, /* GDBusInterfaceInfo */
"org.freedesktop.ScreenSaver", /* name */
"/org/freedesktop/ScreenSaver", /* object path */
"org.freedesktop.ScreenSaver", /* interface */
NULL, /* GCancellable */
&error);
if (error != NULL) {
printf("Cannot create proxy for org.freedesktop.ScreenSaver: %s\n", error->message);
g_error_free (error);
g_object_unref(conn);
return 1;
}
while (1) {
mpv_event *event = mpv_wait_event(handle, 0);
if (event->event_id == MPV_EVENT_NONE) {
continue;
}
printf("event: %s\n", mpv_event_name(event->event_id));
if (!cookie && (event->event_id == MPV_EVENT_PLAYBACK_RESTART ||
event->event_id == MPV_EVENT_UNPAUSE)) {
cookie = inhibit(proxy);
}
if (cookie && (event->event_id == MPV_EVENT_END_FILE ||
event->event_id == MPV_EVENT_PAUSE ||
event->event_id == MPV_EVENT_PAUSE)) {
cookie = uninhibit(proxy, cookie);
}
if (event->event_id == MPV_EVENT_SHUTDOWN) {
break;
}
}
g_object_unref(proxy);
g_object_unref(conn);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment