main: Make PpdProfile into a bitmask

So that drivers can support multiple profiles.
This commit is contained in:
Bastien Nocera 2020-08-05 14:47:03 +02:00
parent 6be395152f
commit 17b655f9f5
5 changed files with 47 additions and 32 deletions

View file

@ -35,9 +35,21 @@ typedef struct {
GPtrArray *actions;
} PpdApp;
#define GET_DRIVER(p) (data->profile_data[p].driver)
#define ACTIVE_DRIVER (data->profile_data[data->active_profile].driver)
#define SELECTED_DRIVER (data->profile_data[data->selected_profile].driver)
static guint
flags_to_index (PpdProfile profile)
{
guint i;
for (i = 0; i < NUM_PROFILES; i++)
if (profile & 1 << i)
return i;
g_assert_not_reached ();
}
#define GET_DRIVER(p) (data->profile_data[flags_to_index(p)].driver)
#define ACTIVE_DRIVER (data->profile_data[flags_to_index(data->active_profile)].driver)
#define SELECTED_DRIVER (data->profile_data[flags_to_index(data->selected_profile)].driver)
/* profile drivers and actions */
#include "ppd-action-trickle-charge.h"
@ -71,8 +83,8 @@ typedef enum {
static const char *
profile_to_str (PpdProfile profile)
{
GEnumClass *klass = g_type_class_ref (PPD_TYPE_PROFILE);
GEnumValue *value = g_enum_get_value (klass, profile);
GFlagsClass *klass = g_type_class_ref (PPD_TYPE_PROFILE);
GFlagsValue *value = g_flags_get_first_value (klass, profile);
const gchar *name = value ? value->value_nick : "";
g_type_class_unref (klass);
return name;
@ -81,8 +93,8 @@ profile_to_str (PpdProfile profile)
static PpdProfile
profile_from_str (const char *str)
{
GEnumClass *klass = g_type_class_ref (PPD_TYPE_PROFILE);
GEnumValue *value = g_enum_get_value_by_nick (klass, str);
GFlagsClass *klass = g_type_class_ref (PPD_TYPE_PROFILE);
GFlagsValue *value = g_flags_get_value_by_nick (klass, str);
PpdProfile profile = value ? value->value : PPD_PROFILE_UNSET;
g_type_class_unref (klass);
return profile;
@ -106,7 +118,7 @@ get_inhibited (PpdApp *data)
PpdProfileDriver *driver;
const char *ret;
driver = data->profile_data[data->selected_profile].driver;
driver = SELECTED_DRIVER;
ret = ppd_profile_driver_get_inhibited (driver);
g_assert (ret != NULL);
return ret;
@ -393,10 +405,10 @@ bus_acquired_handler (GDBusConnection *connection,
static gboolean
has_required_drivers (PpdApp *data)
{
if (!data->profile_data[PPD_PROFILE_BALANCED].driver ||
!G_IS_OBJECT (data->profile_data[PPD_PROFILE_BALANCED].driver) ||
!data->profile_data[PPD_PROFILE_POWER_SAVER].driver ||
!G_IS_OBJECT (data->profile_data[PPD_PROFILE_POWER_SAVER].driver)) {
if (!data->profile_data[flags_to_index(PPD_PROFILE_BALANCED)].driver ||
!G_IS_OBJECT (data->profile_data[flags_to_index(PPD_PROFILE_BALANCED)].driver) ||
!data->profile_data[flags_to_index(PPD_PROFILE_POWER_SAVER)].driver ||
!G_IS_OBJECT (data->profile_data[flags_to_index(PPD_PROFILE_POWER_SAVER)].driver)) {
return FALSE;
}
return TRUE;
@ -436,7 +448,7 @@ name_acquired_handler (GDBusConnection *connection,
continue;
}
data->profile_data[profile].driver = driver;
data->profile_data[flags_to_index(profile)].driver = driver;
g_signal_connect (G_OBJECT (driver), "notify::inhibited",
G_CALLBACK (driver_inhibited_changed_cb), data);
@ -526,6 +538,8 @@ int main (int argc, char **argv)
data = g_new0 (PpdApp, 1);
data->actions = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
data->active_profile = PPD_PROFILE_BALANCED;
data->selected_profile = PPD_PROFILE_BALANCED;
/* Set up D-Bus */
setup_dbus (data);

View file

@ -11,4 +11,4 @@
#include "ppd-profile.h"
#define NUM_PROFILES (PPD_PROFILE_PERFORMANCE + 1)
#define NUM_PROFILES 3

View file

@ -43,7 +43,7 @@ ppd_profile_driver_set_property (GObject *object,
priv->driver_name = g_value_dup_string (value);
break;
case PROP_PROFILE:
priv->profile = g_value_get_enum (value);
priv->profile = g_value_get_flags (value);
break;
case PROP_INHIBITED:
g_clear_pointer (&priv->inhibited, g_free);
@ -68,7 +68,7 @@ ppd_profile_driver_get_property (GObject *object,
g_value_set_string (value, priv->driver_name);
break;
case PROP_PROFILE:
g_value_set_enum (value, priv->profile);
g_value_set_flags (value, priv->profile);
break;
case PROP_INHIBITED:
g_value_set_string (value, priv->inhibited);
@ -107,12 +107,12 @@ ppd_profile_driver_class_init (PpdProfileDriverClass *klass)
NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (object_class, PROP_PROFILE,
g_param_spec_enum("profile",
"Profile",
"Profile implemented by this driver",
PPD_TYPE_PROFILE,
PPD_PROFILE_UNSET,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_param_spec_flags("profile",
"Profile",
"Profile implemented by this driver",
PPD_TYPE_PROFILE,
0,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (object_class, PROP_INHIBITED,
g_param_spec_string("inhibited",
"Inhibited",

View file

@ -11,9 +11,8 @@
/**
* PpdProfile:
* @PPD_PROFILE_UNSET: unset profile, when bugs occur
* @PPD_PROFILE_BALANCED: balanced, the default profile
* @PPD_PROFILE_POWER_SAVER: "power-saver", the battery saving profile
* @PPD_PROFILE_BALANCED: balanced, the default profile
* @PPD_PROFILE_PERFORMANCE: as fast as possible, a profile that does
* not care about noise or battery consumption, only available
* on some systems.
@ -21,8 +20,10 @@
* The different profiles available for users to select.
*/
typedef enum {
PPD_PROFILE_UNSET = -1,
PPD_PROFILE_BALANCED = 0,
PPD_PROFILE_POWER_SAVER,
PPD_PROFILE_PERFORMANCE
PPD_PROFILE_POWER_SAVER = 1 << 0,
PPD_PROFILE_BALANCED = 1 << 1,
PPD_PROFILE_PERFORMANCE = 1 << 2
} PpdProfile;
#define PPD_PROFILE_ALL (PPD_PROFILE_BALANCED | PPD_PROFILE_POWER_SAVER | PPD_PROFILE_PERFORMANCE)
#define PPD_PROFILE_UNSET (0)

View file

@ -239,10 +239,10 @@ class Tests(dbusmock.DBusTestCase):
profiles = self.get_dbus_property('Profiles')
self.assertEqual(len(profiles), 2)
self.assertEqual(profiles[0]['Driver'], 'balanced')
self.assertEqual(profiles[1]['Driver'], 'power-saver')
self.assertEqual(profiles[0]['Profile'], 'balanced')
self.assertEqual(profiles[1]['Profile'], 'power-saver')
self.assertEqual(profiles[1]['Driver'], 'balanced')
self.assertEqual(profiles[0]['Driver'], 'power-saver')
self.assertEqual(profiles[1]['Profile'], 'balanced')
self.assertEqual(profiles[0]['Profile'], 'power-saver')
self.set_dbus_property('SelectedProfile', GLib.Variant.new_string('power-saver'))
self.assertEqual(self.get_dbus_property('ActiveProfile'), 'power-saver')