From 5bd398ed1870385de8a2f7e49d41d3480101b509 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guido=20G=C3=BCnther?= Date: Wed, 12 Jun 2024 08:47:41 +0200 Subject: [PATCH] feedback-led: Support colors as strings The FbdDevLedColor enum defines the hardware color of the LED. We used it for the feedbacks color value as long as the kernel mostly had single color LEDs. As multicolor LEDs are common nowadays switch the color to a string which will allow us to use RGB values later on. --- src/fbd-feedback-led.c | 63 ++++++++++++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 14 deletions(-) diff --git a/src/fbd-feedback-led.c b/src/fbd-feedback-led.c index bc606ad..4cae94c 100644 --- a/src/fbd-feedback-led.c +++ b/src/fbd-feedback-led.c @@ -31,16 +31,35 @@ enum { static GParamSpec *props[PROP_LAST_PROP]; typedef struct _FbdFeedbackLed { - FbdFeedbackBase parent; + FbdFeedbackBase parent; - guint frequency; - guint priority; - guint max_brightness; - FbdFeedbackLedColor color; + guint frequency; + guint priority; + guint max_brightness; + char *color; } FbdFeedbackLed; G_DEFINE_TYPE (FbdFeedbackLed, fbd_feedback_led, FBD_TYPE_FEEDBACK_BASE) + +static FbdFeedbackLedColor +color_string_to_color (const char *color) +{ + + if (g_strcmp0 (color, "red") == 0) + return FBD_FEEDBACK_LED_COLOR_RED; + else if (g_strcmp0 (color, "green") == 0) + return FBD_FEEDBACK_LED_COLOR_GREEN; + else if (g_strcmp0 (color, "blue") == 0) + return FBD_FEEDBACK_LED_COLOR_BLUE; + else if (g_strcmp0 (color, "white") == 0) + return FBD_FEEDBACK_LED_COLOR_WHITE; + + g_warning_once ("Can't parse color '%s' using white", color); + return FBD_FEEDBACK_LED_COLOR_WHITE; +} + + static void fbd_feedback_led_set_property (GObject *object, guint property_id, @@ -60,7 +79,7 @@ fbd_feedback_led_set_property (GObject *object, self->max_brightness = g_value_get_uint (value); break; case PROP_COLOR: - self->color = g_value_get_enum (value); + self->color = g_value_dup_string (value); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); @@ -88,7 +107,7 @@ fbd_feedback_led_get_property (GObject *object, g_value_set_uint (value, self->max_brightness); break; case PROP_COLOR: - g_value_set_enum (value, self->color); + g_value_set_string (value, self->color); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); @@ -103,13 +122,15 @@ fbd_feedback_led_run (FbdFeedbackBase *base) FbdFeedbackLed *self = FBD_FEEDBACK_LED (base); FbdFeedbackManager *manager = fbd_feedback_manager_get_default (); FbdDevLeds *dev = fbd_feedback_manager_get_dev_leds (manager); + FbdFeedbackLedColor color; g_return_if_fail (FBD_IS_DEV_LEDS (dev)); g_debug ("Periodic led feedback: self->max_brightness, self->frequency"); + color = color_string_to_color (self->color); /* FIXME: handle priority */ fbd_dev_leds_start_periodic (dev, - self->color, + color, self->max_brightness, self->frequency); } @@ -121,9 +142,11 @@ fbd_feedback_led_end (FbdFeedbackBase *base) FbdFeedbackLed *self = FBD_FEEDBACK_LED (base); FbdFeedbackManager *manager = fbd_feedback_manager_get_default (); FbdDevLeds *dev = fbd_feedback_manager_get_dev_leds (manager); + FbdFeedbackLedColor color; + color = color_string_to_color (self->color); if (dev) - fbd_dev_leds_stop (dev, self->color); + fbd_dev_leds_stop (dev, color); fbd_feedback_base_done (FBD_FEEDBACK_BASE (self)); } @@ -138,6 +161,17 @@ fbd_feedback_led_is_available (FbdFeedbackBase *base) } +static void +fbd_feedback_led_finalize (GObject *object) +{ + FbdFeedbackLed *self = FBD_FEEDBACK_LED (object); + + g_clear_pointer (&self->color, g_free); + + G_OBJECT_CLASS (fbd_feedback_led_parent_class)->finalize (object); +} + + static void fbd_feedback_led_class_init (FbdFeedbackLedClass *klass) { @@ -146,6 +180,7 @@ fbd_feedback_led_class_init (FbdFeedbackLedClass *klass) object_class->set_property = fbd_feedback_led_set_property; object_class->get_property = fbd_feedback_led_get_property; + object_class->finalize = fbd_feedback_led_finalize; base_class->run = fbd_feedback_led_run; base_class->end = fbd_feedback_led_end; @@ -163,13 +198,13 @@ fbd_feedback_led_class_init (FbdFeedbackLedClass *klass) /** * FbdFeedbackLed:color: * - * The color the LED should blink with. + * The color the LED should blink with. The color is given as color + * name or `rgb()` color value. */ props[PROP_COLOR] = - g_param_spec_enum ("color", "", "", - FBD_FEEDBACK_LED_COLOR_WHITE, - FBD_TYPE_FEEDBACK_LED_COLOR, - G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + g_param_spec_string ("color", "", "", + NULL, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); /** * FbdFeedbackLed:priority: *