Forked from fernandoherreradelasheras/mpv-freedesktop-screensaver-plugin.c
Last active
January 30, 2024 08:23
-
-
Save redhat421/290843550ab30d8472e0788a648fede9 to your computer and use it in GitHub Desktop.
mpv pluging to inhibit screensaver while playing a video using freedesktop dbus api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 mpv` -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; | |
} | |
mpv_observe_property(handle, 0, "pause", MPV_FORMAT_FLAG); | |
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 (event->event_id == MPV_EVENT_PROPERTY_CHANGE) { | |
mpv_event_property *prop = (mpv_event_property *)event->data; | |
if (strcmp(prop->name, "pause") == 0 && (prop->format == MPV_FORMAT_FLAG)) { | |
int pause = *(int *)prop->data; | |
printf("pause: %d\n", pause); | |
if (pause == 0 && !cookie) cookie = inhibit(proxy); | |
if (pause == 1 && cookie) cookie = uninhibit(proxy, cookie); | |
} | |
} | |
if (!cookie && (event->event_id == MPV_EVENT_PLAYBACK_RESTART)) { | |
cookie = inhibit(proxy); | |
} | |
if (cookie && (event->event_id == MPV_EVENT_END_FILE || | |
event->event_id == MPV_EVENT_IDLE )) { | |
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