From f0f48abc26803c029497080b28079a954fdf1fe6 Mon Sep 17 00:00:00 2001 From: Bastien Nocera Date: Wed, 5 Aug 2020 11:06:16 +0200 Subject: [PATCH] main: Remove profile specificity from actions Make the actions handle transitions themselves, so they can minimise the work they have to do when they know both the origin and the target profiles. --- src/power-profiles-daemon.c | 37 ++++++++++----------------------- src/ppd-action-trickle-charge.c | 27 +++++++++++------------- src/ppd-action.c | 19 ++++------------- src/ppd-action.h | 12 +++++------ 4 files changed, 32 insertions(+), 63 deletions(-) diff --git a/src/power-profiles-daemon.c b/src/power-profiles-daemon.c index ff21c49..b9dd5a6 100644 --- a/src/power-profiles-daemon.c +++ b/src/power-profiles-daemon.c @@ -32,7 +32,7 @@ typedef struct { PpdProfile active_profile; PpdProfile selected_profile; ProfileData profile_data[NUM_PROFILES]; - GPtrArray *actions[NUM_PROFILES]; + GPtrArray *actions; } PpdApp; #define GET_DRIVER(p) (data->profile_data[p].driver) @@ -185,8 +185,8 @@ send_dbus_event (PpdApp *data, } static void -set_all_actions_active (GPtrArray *actions, - gboolean active) +actions_activate_profile (GPtrArray *actions, + PpdProfile profile) { guint i; @@ -199,14 +199,10 @@ set_all_actions_active (GPtrArray *actions, action = g_ptr_array_index (actions, i); - if (active) - ret = ppd_action_activate (action, &error); - else - ret = ppd_action_deactivate (action, &error); - + ret = ppd_action_activate_profile (action, profile, &error); if (!ret) - g_warning ("Failed to %s action '%s': %s", - active ? "activate" : "deactivate", + g_warning ("Failed to activate action '%s' to profile %s: %s", + profile_to_str (profile), ppd_action_get_action_name (action), error->message); } @@ -230,7 +226,6 @@ set_active_profile (PpdApp *data, error->message); g_clear_error (&error); } - set_all_actions_active (data->actions[data->active_profile], FALSE); driver = GET_DRIVER(target_profile); if (!ppd_profile_driver_activate (driver, &error)) { @@ -239,7 +234,7 @@ set_active_profile (PpdApp *data, error->message); g_clear_error (&error); } - set_all_actions_active (data->actions[target_profile], TRUE); + actions_activate_profile (data->actions, target_profile); data->active_profile = target_profile; } @@ -467,7 +462,7 @@ name_acquired_handler (GDBusConnection *connection, continue; } - g_ptr_array_add (data->actions[profile], action); + g_ptr_array_add (data->actions, action); } else { g_assert_not_reached (); } @@ -479,12 +474,7 @@ name_acquired_handler (GDBusConnection *connection, } /* Set initial state for actions */ - for (i = 0; i < NUM_PROFILES; i++) { - if (i == data->active_profile) - continue; - set_all_actions_active (data->actions[i], FALSE); - } - set_all_actions_active (data->actions[data->active_profile], TRUE); + actions_activate_profile (data->actions, data->active_profile); send_dbus_event (data, PROP_ALL); @@ -523,8 +513,6 @@ setup_dbus (PpdApp *data) static void free_app_data (PpdApp *data) { - guint i; - if (data == NULL) return; @@ -533,8 +521,7 @@ free_app_data (PpdApp *data) data->name_id = 0; } - for (i = 0; i < NUM_PROFILES; i++) - g_ptr_array_free (data->actions[i], TRUE); + g_ptr_array_free (data->actions, TRUE); g_clear_pointer (&data->introspection_data, g_dbus_node_info_unref); g_clear_object (&data->connection); @@ -546,11 +533,9 @@ int main (int argc, char **argv) { PpdApp *data; int ret = 0; - guint i; data = g_new0 (PpdApp, 1); - for (i = 0; i < NUM_PROFILES; i++) - data->actions[i] = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); + data->actions = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); /* Set up D-Bus */ setup_dbus (data); diff --git a/src/ppd-action-trickle-charge.c b/src/ppd-action-trickle-charge.c index 3f5b00d..a6f8c0d 100644 --- a/src/ppd-action-trickle-charge.c +++ b/src/ppd-action-trickle-charge.c @@ -68,22 +68,20 @@ set_charge_type (PpdActionTrickleCharge *action, } static gboolean -ppd_action_trickle_charge_activate (PpdAction *action, - GError **error) +ppd_action_trickle_charge_activate_profile (PpdAction *action, + PpdProfile profile, + GError **error) { PpdActionTrickleCharge *self = PPD_ACTION_TRICKLE_CHARGE (action); - set_charge_type (self, "Trickle"); - self->active = TRUE; - return TRUE; -} -static gboolean -ppd_action_trickle_charge_deactivate (PpdAction *action, - GError **error) -{ - PpdActionTrickleCharge *self = PPD_ACTION_TRICKLE_CHARGE (action); - set_charge_type (self, "Fast"); - self->active = FALSE; + if (profile == PPD_PROFILE_POWER_SAVER) { + set_charge_type (self, "Trickle"); + self->active = TRUE; + } else { + set_charge_type (self, "Fast"); + self->active = FALSE; + } + return TRUE; } @@ -130,8 +128,7 @@ ppd_action_trickle_charge_class_init (PpdActionTrickleChargeClass *klass) object_class->finalize = ppd_action_trickle_charge_finalize; driver_class = PPD_ACTION_CLASS(klass); - driver_class->activate = ppd_action_trickle_charge_activate; - driver_class->deactivate = ppd_action_trickle_charge_deactivate; + driver_class->activate_profile = ppd_action_trickle_charge_activate_profile; } static void diff --git a/src/ppd-action.c b/src/ppd-action.c index 4f9fe73..6de1c74 100644 --- a/src/ppd-action.c +++ b/src/ppd-action.c @@ -121,27 +121,16 @@ ppd_action_probe (PpdAction *action) } gboolean -ppd_action_activate (PpdAction *action, +ppd_action_activate_profile (PpdAction *action, + PpdProfile profile, GError **error) { g_return_val_if_fail (PPD_IS_ACTION (action), FALSE); - if (!PPD_ACTION_GET_CLASS (action)->activate) + if (!PPD_ACTION_GET_CLASS (action)->activate_profile) return TRUE; - return PPD_ACTION_GET_CLASS (action)->activate (action, error); -} - -gboolean -ppd_action_deactivate (PpdAction *action, - GError **error) -{ - g_return_val_if_fail (PPD_IS_ACTION (action), FALSE); - - if (!PPD_ACTION_GET_CLASS (action)->deactivate) - return TRUE; - - return PPD_ACTION_GET_CLASS (action)->deactivate (action, error); + return PPD_ACTION_GET_CLASS (action)->activate_profile (action, profile, error); } const char * diff --git a/src/ppd-action.h b/src/ppd-action.h index f3b8b48..48d2ac2 100644 --- a/src/ppd-action.h +++ b/src/ppd-action.h @@ -19,15 +19,13 @@ struct _PpdActionClass { GObjectClass parent_class; - gboolean (* probe) (PpdAction *action); - gboolean (* activate) (PpdAction *action, - GError **error); - gboolean (* deactivate) (PpdAction *action, - GError **error); + gboolean (* probe) (PpdAction *action); + gboolean (* activate_profile) (PpdAction *action, + PpdProfile profile, + GError **error); }; gboolean ppd_action_probe (PpdAction *action); -gboolean ppd_action_activate (PpdAction *action, GError **error); -gboolean ppd_action_deactivate (PpdAction *action, GError **error); +gboolean ppd_action_activate_profile (PpdAction *action, PpdProfile profile, GError **error); const char *ppd_action_get_action_name (PpdAction *action); PpdProfile ppd_action_get_profile (PpdAction *action);