Commit 11fdcca2 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

power: probe D-Bus services and return error if none are found

parent 5520e829
...@@ -39,6 +39,8 @@ enum vlc_inhibit_api ...@@ -39,6 +39,8 @@ enum vlc_inhibit_api
GNOME, /**< GNOME 2.26..3.4 */ GNOME, /**< GNOME 2.26..3.4 */
}; };
#define MAX_API (GNOME+1)
static const char dbus_service[][32] = static const char dbus_service[][32] =
{ {
[FREEDESKTOP] = "org.freedesktop.PowerManagement", [FREEDESKTOP] = "org.freedesktop.PowerManagement",
...@@ -66,14 +68,15 @@ static const char dbus_method_uninhibit[][10] = ...@@ -66,14 +68,15 @@ static const char dbus_method_uninhibit[][10] =
struct vlc_inhibit_sys struct vlc_inhibit_sys
{ {
DBusConnection *conn; DBusConnection *conn;
dbus_uint32_t cookie[2]; dbus_uint32_t cookie;
enum vlc_inhibit_api api;
}; };
static void InhibitAPI(vlc_inhibit_t *ih, unsigned flags, static void Inhibit(vlc_inhibit_t *ih, unsigned flags)
enum vlc_inhibit_api type)
{ {
vlc_inhibit_sys_t *sys = ih->p_sys; vlc_inhibit_sys_t *sys = ih->p_sys;
DBusConnection *conn = sys->conn; DBusConnection *conn = sys->conn;
enum vlc_inhibit_api type = sys->api;
const char *method = flags ? "Inhibit" : dbus_method_uninhibit[type]; const char *method = flags ? "Inhibit" : dbus_method_uninhibit[type];
dbus_bool_t ret; dbus_bool_t ret;
...@@ -110,9 +113,8 @@ static void InhibitAPI(vlc_inhibit_t *ih, unsigned flags, ...@@ -110,9 +113,8 @@ static void InhibitAPI(vlc_inhibit_t *ih, unsigned flags,
} }
} }
} else { } else {
if (sys->cookie[type]) if (sys->cookie)
ret = dbus_message_append_args(msg, DBUS_TYPE_UINT32, ret = dbus_message_append_args(msg, DBUS_TYPE_UINT32, &sys->cookie,
&sys->cookie[type],
DBUS_TYPE_INVALID); DBUS_TYPE_INVALID);
else else
ret = false; ret = false;
...@@ -128,14 +130,14 @@ static void InhibitAPI(vlc_inhibit_t *ih, unsigned flags, ...@@ -128,14 +130,14 @@ static void InhibitAPI(vlc_inhibit_t *ih, unsigned flags,
goto giveup; /* no reponse?! */ goto giveup; /* no reponse?! */
if (!dbus_message_get_args(reply, NULL, if (!dbus_message_get_args(reply, NULL,
DBUS_TYPE_UINT32, &sys->cookie[type], DBUS_TYPE_UINT32, &sys->cookie,
DBUS_TYPE_INVALID)) DBUS_TYPE_INVALID))
sys->cookie[type] = 0; sys->cookie = 0;
dbus_message_unref(reply); dbus_message_unref(reply);
} else { /* just send and flush */ } else { /* just send and flush */
if (dbus_connection_send (conn, msg, NULL)) { if (dbus_connection_send (conn, msg, NULL)) {
sys->cookie[type] = 0; sys->cookie = 0;
dbus_connection_flush(conn); dbus_connection_flush(conn);
} }
} }
...@@ -143,11 +145,7 @@ giveup: ...@@ -143,11 +145,7 @@ giveup:
dbus_message_unref(msg); dbus_message_unref(msg);
} }
static void Inhibit (vlc_inhibit_t *ih, unsigned flags) static void Close(vlc_object_t *obj);
{
for (int type = 0; type < 2; type++)
InhibitAPI (ih, flags, type);
}
static int Open (vlc_object_t *obj) static int Open (vlc_object_t *obj)
{ {
...@@ -157,22 +155,37 @@ static int Open (vlc_object_t *obj) ...@@ -157,22 +155,37 @@ static int Open (vlc_object_t *obj)
return VLC_ENOMEM; return VLC_ENOMEM;
DBusError err; DBusError err;
dbus_error_init(&err);
dbus_error_init (&err);
sys->conn = dbus_bus_get_private (DBUS_BUS_SESSION, &err); sys->conn = dbus_bus_get_private (DBUS_BUS_SESSION, &err);
if (sys->conn == NULL) if (sys->conn == NULL)
{ {
msg_Err (obj, "cannot connect to session bus: %s", err.message); msg_Err(ih, "cannot connect to session bus: %s", err.message);
dbus_error_free (&err); dbus_error_free(&err);
free (sys); free(sys);
return VLC_EGENERIC; return VLC_EGENERIC;
} }
sys->cookie[0] = 0;
sys->cookie[1] = 0;
ih->p_sys = sys; sys->cookie = 0;
ih->inhibit = Inhibit;
return VLC_SUCCESS; for (unsigned i = 0; i < MAX_API; i++)
{
if (dbus_bus_name_has_owner(sys->conn, dbus_service[i], &err))
{
msg_Dbg(ih, "found service %s", dbus_service[i]);
sys->api = i;
ih->p_sys = sys;
ih->inhibit = Inhibit;
return VLC_SUCCESS;
}
msg_Dbg(ih, "cannot find service %s: %s", dbus_service[i],
err.message);
dbus_error_free(&err);
}
Close(obj);
return VLC_EGENERIC;
} }
static void Close (vlc_object_t *obj) static void Close (vlc_object_t *obj)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment