all: Add GetCriticalAction daemon method

This allows desktop front-ends to get which action will
actually be taken when we hit critical battery.

This is not a property as availability of actions might
change over the course of the run of the system, and
we didn't want to make unnecessary D-Bus calls on startup.
This commit is contained in:
Bastien Nocera 2013-10-17 09:04:03 +02:00
parent a7870229ee
commit db31456921
11 changed files with 145 additions and 20 deletions

View file

@ -144,6 +144,31 @@ up_client_get_display_device (UpClient *client)
return device;
}
/**
* up_client_get_critical_action:
* @client: a #UpClient instance.
*
* Gets a string representing the configured critical action,
* depending on availability.
*
* Return value: the action name, or %NULL on error.
*
* Since: 1.0
**/
char *
up_client_get_critical_action (UpClient *client)
{
char *action;
g_return_val_if_fail (UP_IS_CLIENT (client), NULL);
if (!up_client_glue_call_get_critical_action_sync (client->priv->proxy,
&action,
NULL, NULL)) {
return NULL;
}
return action;
}
/**
* up_client_get_daemon_version:
* @client: a #UpClient instance.

View file

@ -81,6 +81,7 @@ gboolean up_client_enumerate_devices_sync (UpClient *client,
GCancellable *cancellable,
GError **error);
UpDevice * up_client_get_display_device (UpClient *client);
char * up_client_get_critical_action (UpClient *client);
/* accessors */
GPtrArray *up_client_get_devices (UpClient *client);

View file

@ -130,6 +130,19 @@ up_backend_coldplug (UpBackend *backend, UpDaemon *daemon)
return TRUE;
}
/**
* up_backend_get_critical_action:
* @backend: The %UpBackend class instance
*
* Which action will be taken when %UP_DEVICE_LEVEL_ACTION
* warning-level occurs.
**/
const char *
up_backend_get_critical_action (UpBackend *backend)
{
return "PowerOff";
}
/**
* up_backend_take_action:
* @backend: The %UpBackend class instance

View file

@ -293,6 +293,19 @@ up_backend_coldplug (UpBackend *backend, UpDaemon *daemon)
return TRUE;
}
/**
* up_backend_get_critical_action:
* @backend: The %UpBackend class instance
*
* Which action will be taken when %UP_DEVICE_LEVEL_ACTION
* warning-level occurs.
**/
const char *
up_backend_get_critical_action (UpBackend *backend)
{
return "PowerOff";
}
/**
* up_backend_take_action:
* @backend: The %UpBackend class instance

View file

@ -367,13 +367,14 @@ check_action_result (GVariant *result)
}
/**
* up_backend_take_action:
* up_backend_get_critical_action:
* @backend: The %UpBackend class instance
*
* Act upon the %UP_DEVICE_LEVEL_ACTION warning-level.
* Which action will be taken when %UP_DEVICE_LEVEL_ACTION
* warning-level occurs.
**/
void
up_backend_take_action (UpBackend *backend)
const char *
up_backend_get_critical_action (UpBackend *backend)
{
struct {
const gchar *method;
@ -388,10 +389,11 @@ up_backend_take_action (UpBackend *backend)
g_return_if_fail (backend->priv->logind_proxy != NULL);
for (i = 0; i < G_N_ELEMENTS (actions); i++) {
gboolean action_available = FALSE;
GVariant *result;
if (actions[i].can_method) {
gboolean action_available;
/* Check whether we can use the method */
result = g_dbus_proxy_call_sync (backend->priv->logind_proxy,
actions[i].can_method,
@ -399,31 +401,44 @@ up_backend_take_action (UpBackend *backend)
G_DBUS_CALL_FLAGS_NONE,
-1, NULL, NULL);
action_available = check_action_result (result);
g_message ("got result %s", g_variant_print (result, TRUE));
g_variant_unref (result);
if (!action_available)
continue;
} else {
action_available = TRUE;
}
/* Take action */
g_debug ("About to call logind method %s", actions[i].method);
g_dbus_proxy_call (backend->priv->logind_proxy,
actions[i].method,
g_variant_new ("(b)", FALSE),
G_DBUS_CALL_FLAGS_NONE,
G_MAXINT,
NULL,
NULL,
NULL);
return;
return actions[i].method;
}
g_assert_not_reached ();
}
/**
* up_backend_take_action:
* @backend: The %UpBackend class instance
*
* Act upon the %UP_DEVICE_LEVEL_ACTION warning-level.
**/
void
up_backend_take_action (UpBackend *backend)
{
const char *method;
method = up_backend_get_critical_action (backend);
g_assert (method != NULL);
/* Take action */
g_debug ("About to call logind method %s", method);
g_dbus_proxy_call (backend->priv->logind_proxy,
method,
g_variant_new ("(b)", FALSE),
G_DBUS_CALL_FLAGS_NONE,
G_MAXINT,
NULL,
NULL,
NULL);
}
/**
* up_backend_class_init:
* @klass: The UpBackendClass

View file

@ -148,6 +148,19 @@ up_backend_coldplug (UpBackend *backend, UpDaemon *daemon)
return TRUE;
}
/**
* up_backend_get_critical_action:
* @backend: The %UpBackend class instance
*
* Which action will be taken when %UP_DEVICE_LEVEL_ACTION
* warning-level occurs.
**/
const char *
up_backend_get_critical_action (UpBackend *backend)
{
return "PowerOff";
}
/**
* up_backend_take_action:
* @backend: The %UpBackend class instance

View file

@ -105,6 +105,33 @@ method return sender=:1.386 -> dest=:1.451 reply_serial=2
</doc:doc>
</method>
<method name="GetCriticalAction">
<annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
<arg name="action" direction="out" type="s">
<doc:doc><doc:summary>A string representing the critical action configured and available.</doc:summary></doc:doc>
</arg>
<doc:doc>
<doc:description>
<doc:para>
When the system's power supply is critical (critically low batteries or UPS),
the system will take this action. Possible values are:
<doc:list>
<doc:item>
<doc:term>HybridSleep</doc:term>
</doc:item>
<doc:item>
<doc:term>Hibernate</doc:term>
</doc:item>
<doc:item>
<doc:term>PowerOff</doc:term>
</doc:item>
</doc:list>
</doc:para>
</doc:description>
</doc:doc>
</method>
<!-- ************************************************************ -->
<signal name="DeviceAdded">

View file

@ -70,6 +70,7 @@ void up_backend_test (gpointer user_data);
gboolean up_backend_coldplug (UpBackend *backend,
UpDaemon *daemon);
void up_backend_take_action (UpBackend *backend);
const char *up_backend_get_critical_action (UpBackend *backend);
G_END_DECLS

View file

@ -448,6 +448,17 @@ up_daemon_get_display_device (UpDaemon *daemon,
return TRUE;
}
/**
* up_daemon_get_critical_action:
**/
gboolean
up_daemon_get_critical_action (UpDaemon *daemon,
DBusGMethodInvocation *context)
{
dbus_g_method_return (context, up_backend_get_critical_action (daemon->priv->backend));
return TRUE;
}
/**
* up_daemon_register_power_daemon:
**/

View file

@ -99,6 +99,8 @@ gboolean up_daemon_enumerate_devices (UpDaemon *daemon,
DBusGMethodInvocation *context);
gboolean up_daemon_get_display_device (UpDaemon *daemon,
DBusGMethodInvocation *context);
gboolean up_daemon_get_critical_action (UpDaemon *daemon,
DBusGMethodInvocation *context);
G_END_DECLS

View file

@ -124,6 +124,7 @@ up_client_print (UpClient *client)
gboolean lid_is_closed;
gboolean lid_is_present;
gboolean is_docked;
char *action;
g_object_get (client,
"daemon-version", &daemon_version,
@ -138,6 +139,9 @@ up_client_print (UpClient *client)
g_print (" lid-is-closed: %s\n", lid_is_closed ? "yes" : "no");
g_print (" lid-is-present: %s\n", lid_is_present ? "yes" : "no");
g_print (" is-docked: %s\n", is_docked ? "yes" : "no");
action = up_client_get_critical_action (client);
g_print (" critical-action: %s\n", action);
g_free (action);
g_free (daemon_version);
}