/* * gnome_screensaver.c v0.0.7 * * Enable/Disable the GNOME screensaver * Supports GNOME screensaver API 2.14 and 2.15 * * Call gnome_screensaver_control(1) to enable and * gnome_screensaver_control(0) to disable * */ #include #include #include #include "gnome_screensaver.h" #include "mp_msg.h" #include "help_mp.h" #define GS_SERVICE "org.gnome.ScreenSaver" #define GS_PATH "/org/gnome/ScreenSaver" #define GS_INTERFACE "org.gnome.ScreenSaver" #define GS_APPLICATION_NAME "MPlayer" #define GS_REASON_FOR_INHIBIT "Playing a movie" #if defined(ARCH_X86_64) static guint64 cookie; #else static guint32 cookie; #endif void gnome_screensaver_control(int enable) { DBusGConnection *connection; GError *error; DBusGProxy *proxy; gboolean ret; char *funcname = "gnome_screensaver_control()"; g_type_init(); /* Get a connection to the session bus */ error = NULL; connection = dbus_g_bus_get(DBUS_BUS_SESSION, &error); if (connection == NULL) { mp_msg(MSGT_VO, MSGL_V, funcname, MSGTR_OpenBusConnectionError, error->message); g_error_free(error); return; } /* Create a proxy object */ proxy = dbus_g_proxy_new_for_name(connection, GS_SERVICE, GS_PATH, GS_INTERFACE); /* Enable the screensaver */ if (enable) { /* First call the GNOME screensaver 2.15 API method */ error = NULL; ret = dbus_g_proxy_call(proxy, "UnInhibit", &error, G_TYPE_UINT, cookie, G_TYPE_INVALID, G_TYPE_INVALID); /* If this fails, try the GNOME screensaver 2.14 API */ if (!ret && error->domain == DBUS_GERROR && error->code == DBUS_GERROR_UNKNOWN_METHOD) { mp_msg(MSGT_VO, MSGL_V, "%s: GNOME screensaver 2.15 API failed, trying 2.14 API\n", funcname); g_error_free(error); error = NULL; ret = dbus_g_proxy_call(proxy, "AllowActivation", &error, G_TYPE_INVALID, G_TYPE_INVALID); } } /* Disable the screensaver */ else { /* First call the GNOME screensaver 2.15 API method */ error = NULL; ret = dbus_g_proxy_call(proxy, "Inhibit", &error, G_TYPE_STRING, GS_APPLICATION_NAME, G_TYPE_STRING, GS_REASON_FOR_INHIBIT, G_TYPE_INVALID, G_TYPE_UINT, cookie, G_TYPE_INVALID); /* If this fails, try the GNOME screensaver 2.14 API */ if (!ret && error->domain == DBUS_GERROR && error->code == DBUS_GERROR_UNKNOWN_METHOD) { mp_msg(MSGT_VO, MSGL_V, "%s: GNOME screensaver 2.15 API failed, trying 2.14 API\n", funcname); g_error_free(error); error = NULL; ret = dbus_g_proxy_call(proxy, "InhibitActivation", &error, G_TYPE_STRING, GS_REASON_FOR_INHIBIT, G_TYPE_INVALID, G_TYPE_INVALID); } } if (!ret) { /* Check if it's a remote exception or a regular GError */ if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_REMOTE_EXCEPTION) { mp_msg(MSGT_VO, MSGL_V, funcname, MSGTR_RemoteMethodException, dbus_g_error_get_name(error), error->message); } else { mp_msg(MSGT_VO, MSGL_V, funcname, MSGTR_GError, error->message); } g_error_free(error); } else { mp_msg(MSGT_VO, MSGL_INFO, enable ? MSGTR_GNOMEScreensaverEnabled : MSGTR_GNOMEScreensaverDisabled); } g_object_unref(proxy); }