From e90e9f728dcac239eb8cab29170378c12d0b4702 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 10 Sep 2014 16:28:42 -0500 Subject: first draft of non-blocking powerd/uscreen brightness impl --- src/CMakeLists.txt | 3 +- src/brightness.c | 393 ++++++++++++++++++++++++++++++++++++ src/brightness.h | 67 ++++++ src/ib-brightness-control.c | 156 -------------- src/ib-brightness-control.h | 33 --- src/ib-brightness-uscreen-control.c | 202 ------------------ src/ib-brightness-uscreen-control.h | 43 ---- src/service.c | 108 +++------- 8 files changed, 494 insertions(+), 511 deletions(-) create mode 100644 src/brightness.c create mode 100644 src/brightness.h delete mode 100644 src/ib-brightness-control.c delete mode 100644 src/ib-brightness-control.h delete mode 100644 src/ib-brightness-uscreen-control.c delete mode 100644 src/ib-brightness-uscreen-control.h (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9b3d815..f7efb80 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,9 +5,8 @@ add_definitions(-DG_LOG_DOMAIN="Indicator-Power") # handwritten sources set(SERVICE_MANUAL_SOURCES + brightness.c device-provider-upower.c - ib-brightness-control.c - ib-brightness-uscreen-control.c device-provider.c device.c notifier.c diff --git a/src/brightness.c b/src/brightness.c new file mode 100644 index 0000000..070305e --- /dev/null +++ b/src/brightness.c @@ -0,0 +1,393 @@ +/* + * Copyright 2014 Canonical Ltd. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + * + * Authors: + * Charles Kerr + */ + +#include "brightness.h" + +#include + +enum +{ + PROP_0, + PROP_PERCENTAGE, + LAST_PROP +}; + +static GParamSpec* properties[LAST_PROP]; + +typedef struct +{ + GDBusConnection * system_bus; + GCancellable * cancellable; + + guint powerd_name_tag; + + double percentage; + + /* powerd brightness params */ + gint powerd_dim; + gint powerd_min; + gint powerd_max; + gint powerd_dflt; + gboolean powerd_ab_supported; + gboolean have_powerd_params; +} +IndicatorPowerBrightnessPrivate; + +typedef IndicatorPowerBrightnessPrivate priv_t; + +G_DEFINE_TYPE_WITH_PRIVATE(IndicatorPowerBrightness, + indicator_power_brightness, + G_TYPE_OBJECT) + +#define get_priv(o) ((priv_t*)indicator_power_brightness_get_instance_private(o)) + +/*** +**** GObject virtual functions +***/ + +static void +my_get_property (GObject * o, + guint property_id, + GValue * value, + GParamSpec * pspec) +{ + IndicatorPowerBrightness * self = INDICATOR_POWER_BRIGHTNESS(o); + + switch (property_id) + { + case PROP_PERCENTAGE: + g_value_set_double(value, indicator_power_brightness_get_percentage(self)); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(o, property_id, pspec); + } +} + +static void +my_set_property (GObject * o, + guint property_id, + const GValue * value, + GParamSpec * pspec) +{ + IndicatorPowerBrightness * self = INDICATOR_POWER_BRIGHTNESS(o); + + switch (property_id) + { + case PROP_PERCENTAGE: + indicator_power_brightness_set_percentage(self, g_value_get_double(value)); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(o, property_id, pspec); + } +} + +static void +my_dispose (GObject * o) +{ + IndicatorPowerBrightness * self = INDICATOR_POWER_BRIGHTNESS(o); + priv_t * p = get_priv(self); + + if (p->cancellable != NULL) + { + g_cancellable_cancel (p->cancellable); + g_clear_object (&p->cancellable); + } + + if (p->powerd_name_tag) + { + g_bus_unwatch_name(p->powerd_name_tag); + p->powerd_name_tag = 0; + } + + g_clear_object(&p->system_bus); + + G_OBJECT_CLASS(indicator_power_brightness_parent_class)->dispose(o); +} + +/*** +**** Percentage <-> Brightness Int conversion helpers +***/ + +static gdouble +brightness_to_percentage(IndicatorPowerBrightness * self, int brightness) +{ + const priv_t * p; + gdouble percentage; + + p = get_priv(self); + if (p->have_powerd_params) + { + const int lo = p->powerd_min; + const int hi = p->powerd_max; + percentage = (brightness-lo) / (double)(hi-lo); + } + else + { + percentage = 0; + } + + return percentage; +} + +static int +percentage_to_brightness(IndicatorPowerBrightness * self, double percentage) +{ + const priv_t * p; + int brightness; + + p = get_priv(self); + if (p->have_powerd_params) + { + const int lo = p->powerd_min; + const int hi = p->powerd_max; + brightness = (int)(lo + (percentage*(hi-lo))); + } + else + { + brightness = 0; + } + + return brightness; +} + +/*** +**** DBus Chatter: com.canonical.powerd +***/ + +static void +on_powerd_brightness_params_ready(GObject * source, + GAsyncResult * res, + gpointer gself) +{ + GError * error; + GVariant * v; + + error = NULL; + v = g_dbus_connection_call_finish(G_DBUS_CONNECTION(source), res, &error); + if (v == NULL) + { + if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) + g_warning("Unable to get system bus: %s", error->message); + + g_error_free(error); + } + else + { + IndicatorPowerBrightness * self = INDICATOR_POWER_BRIGHTNESS(gself); + priv_t * p = get_priv(self); + double percentage; + + p->have_powerd_params = TRUE; + g_variant_get(v, "((iiiib))", &p->powerd_dim, + &p->powerd_min, + &p->powerd_max, + &p->powerd_dflt, + &p->powerd_ab_supported); + g_message("powerd brightness settings: dim=%d, min=%d, max=%d, dflt=%d, ab_supported=%d", + p->powerd_dim, + p->powerd_min, + p->powerd_max, + p->powerd_dflt, + (int)p->powerd_ab_supported); + + /* uscreen doesn't have a get_brightness() function, + so the only way to know the value is to initialize it ourselves + (and hope nobody changes it :P */ + percentage = brightness_to_percentage(self, p->powerd_dflt); + indicator_power_brightness_set_percentage(self, percentage); + + /* cleanup */ + g_variant_unref(v); + } +} + +static void +call_powerd_get_brightness_params(IndicatorPowerBrightness * self) +{ + priv_t * p = get_priv(self); + + g_dbus_connection_call(p->system_bus, + "com.canonical.powerd", + "/com/canonical/powerd", + "com.canonical.powerd", + "getBrightnessParams", + NULL, + G_VARIANT_TYPE("((iiiib))"), + G_DBUS_CALL_FLAGS_NONE, + -1, /* default timeout */ + p->cancellable, + on_powerd_brightness_params_ready, + self); +} + +static void +on_powerd_appeared(GDBusConnection * connection, + const gchar * bus_name G_GNUC_UNUSED, + const gchar * name_owner G_GNUC_UNUSED, + gpointer gself) +{ + IndicatorPowerBrightness * self = INDICATOR_POWER_BRIGHTNESS(gself); + priv_t * p = get_priv(self); + + /* keep a handle to the system bus */ + g_clear_object(&p->system_bus); + p->system_bus = g_object_ref(connection); + + /* get powerd's params */ + call_powerd_get_brightness_params(self); +} + +static void +on_powerd_vanished(GDBusConnection * connection G_GNUC_UNUSED, + const gchar * bus_name G_GNUC_UNUSED, + gpointer gself) +{ + priv_t * p = get_priv(INDICATOR_POWER_BRIGHTNESS(gself)); + p->have_powerd_params = FALSE; +} + +/*** +**** DBus Chatter: com.canonical.Unity.Screen +***/ + +/* setUserBrightness doesn't return anything, + so this function is just to check for bus error messages */ +static void +on_set_uscreen_user_brightness_result(GObject * system_bus, + GAsyncResult * res, + gpointer gself G_GNUC_UNUSED) +{ + GError * error; + GVariant * v; + + error = NULL; + v = g_dbus_connection_call_finish(G_DBUS_CONNECTION(system_bus), res, &error); + if (error != NULL) + { + if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) + g_warning("Unable to call uscreen.setBrightness: %s", error->message); + + g_error_free(error); + } + + g_clear_pointer(&v, g_variant_unref); +} + +void +set_uscreen_user_brightness(IndicatorPowerBrightness * self, + int value) +{ + priv_t * p = get_priv(self); + + g_dbus_connection_call(p->system_bus, + "com.canonical.Unity.Screen", + "/com/canonical/Unity/Screen", + "com.canonical.Unity.Screen", + "setUserBrightness", + g_variant_new_int32(value), + NULL, /* no return args */ + G_DBUS_CALL_FLAGS_NONE, + -1, /* default timeout */ + p->cancellable, + on_set_uscreen_user_brightness_result, + self); +} + + +/*** +**** Instantiation +***/ + +static void +indicator_power_brightness_init (IndicatorPowerBrightness * self) +{ + priv_t * p = get_priv(self); + + p->cancellable = g_cancellable_new(); + + p->powerd_name_tag = g_bus_watch_name(G_BUS_TYPE_SYSTEM, + "com.canonical.powerd", + G_BUS_NAME_WATCHER_FLAGS_NONE, + on_powerd_appeared, + on_powerd_vanished, + self, + NULL); +} + +static void +indicator_power_brightness_class_init (IndicatorPowerBrightnessClass * klass) +{ + GObjectClass * object_class = G_OBJECT_CLASS(klass); + + object_class->dispose = my_dispose; + object_class->get_property = my_get_property; + object_class->set_property = my_set_property; + + properties[PROP_0] = NULL; + + properties[PROP_PERCENTAGE] = g_param_spec_double("percentage", + "Percentage", + "Brightness percentage", + 0.1, /* don't allow completely black */ + 1.0, /* brightest */ + 0.8, + G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS); + + g_object_class_install_properties (object_class, LAST_PROP, properties); +} + +/*** +**** Public API +***/ + +IndicatorPowerBrightness * +indicator_power_brightness_new(void) +{ + gpointer o = g_object_new (INDICATOR_TYPE_POWER_BRIGHTNESS, NULL); + + return INDICATOR_POWER_BRIGHTNESS(o); +} + +void +indicator_power_brightness_set_percentage(IndicatorPowerBrightness * self, + double percentage) +{ + priv_t * p; + + g_return_if_fail(INDICATOR_IS_POWER_BRIGHTNESS(self)); + + p = get_priv(self); + if ((int)(p->percentage*100) != (int)(percentage*100)) + { + set_uscreen_user_brightness(self, percentage_to_brightness(self, percentage)); + + p->percentage = percentage; + g_object_notify_by_pspec(G_OBJECT(self), properties[PROP_PERCENTAGE]); + } +} + +double +indicator_power_brightness_get_percentage(IndicatorPowerBrightness * self) +{ + g_return_val_if_fail(INDICATOR_IS_POWER_BRIGHTNESS(self), 0.0); + + return get_priv(self)->percentage; +} diff --git a/src/brightness.h b/src/brightness.h new file mode 100644 index 0000000..d2fcc61 --- /dev/null +++ b/src/brightness.h @@ -0,0 +1,67 @@ +/* + * Copyright 2014 Canonical Ltd. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + * + * Authors: + * Charles Kerr + */ + +#ifndef INDICATOR_POWER_BRIGHTNESS__H +#define INDICATOR_POWER_BRIGHTNESS__H + +#include +#include + +G_BEGIN_DECLS + +/* standard GObject macros */ +#define INDICATOR_POWER_BRIGHTNESS(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), INDICATOR_TYPE_POWER_BRIGHTNESS, IndicatorPowerBrightness)) +#define INDICATOR_TYPE_POWER_BRIGHTNESS (indicator_power_brightness_get_type()) +#define INDICATOR_IS_POWER_BRIGHTNESS(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), INDICATOR_TYPE_POWER_BRIGHTNESS)) + +typedef struct _IndicatorPowerBrightness IndicatorPowerBrightness; +typedef struct _IndicatorPowerBrightnessClass IndicatorPowerBrightnessClass; + +/* property keys */ +#define INDICATOR_POWER_BRIGHTNESS_PROP_PERCENTAGE "percentage" + +/** + * The Indicator Power Brightness. + */ +struct _IndicatorPowerBrightness +{ + /*< private >*/ + GObject parent; +}; + +struct _IndicatorPowerBrightnessClass +{ + GObjectClass parent_class; +}; + +/*** +**** +***/ + +GType indicator_power_brightness_get_type(void); + +IndicatorPowerBrightness * indicator_power_brightness_new(void); + +void indicator_power_brightness_set_percentage(IndicatorPowerBrightness * self, double percentage); + +double indicator_power_brightness_get_percentage(IndicatorPowerBrightness * self); + +G_END_DECLS + +#endif /* INDICATOR_POWER_BRIGHTNESS__H */ diff --git a/src/ib-brightness-control.c b/src/ib-brightness-control.c deleted file mode 100644 index 67da10c..0000000 --- a/src/ib-brightness-control.c +++ /dev/null @@ -1,156 +0,0 @@ -/* - * Copyright 2012 Canonical Ltd. - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 3, as published - * by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranties of - * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR - * PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - * - * Authors: - * Renato Araujo Oliveira Filho - */ - -#include - -#include -#include -#include -#include - -#include "ib-brightness-control.h" - -struct _IbBrightnessControl -{ - gchar *path; -}; - -IbBrightnessControl* -ib_brightness_control_new (void) -{ - IbBrightnessControl *control; - GUdevClient *client; - gchar *path = NULL; - GList *devices; - - // detect device - client = g_udev_client_new (NULL); - devices = g_udev_client_query_by_subsystem (client, "backlight"); - if (devices != NULL) { - GList *device; - const gchar *device_type; - - for (device = devices; device != NULL; device = device->next) { - device_type = g_udev_device_get_sysfs_attr (device->data, "type"); - if ((g_strcmp0 (device_type, "firmware") == 0) || - (g_strcmp0 (device_type, "platform") == 0) || - (g_strcmp0 (device_type, "raw") == 0)) { - path = g_strdup (g_udev_device_get_sysfs_path (device->data)); - g_debug ("found: %s", path); - break; - } - } - - g_list_free_full (devices, g_object_unref); - } - else { - g_warning ("Fail to query backlight devices."); - } - - control = g_new0 (IbBrightnessControl, 1); - control->path = path; - - g_object_unref (client); - return control; -} - -void -ib_brightness_control_set_value (IbBrightnessControl* self, gint value) -{ - gint fd; - gchar *filename; - gchar *svalue; - size_t length; - gint err; - - if (self->path == NULL) - return; - - filename = g_build_filename (self->path, "brightness", NULL); - fd = open(filename, O_WRONLY); - if (fd < 0) { - g_warning ("Fail to set brightness."); - g_free (filename); - return; - } - - svalue = g_strdup_printf ("%i", value); - length = strlen (svalue); - - err = errno; - errno = 0; - if (write (fd, svalue, length) != (ssize_t)length) { - g_warning ("Fail to write brightness information: %s", g_strerror(errno)); - } - errno = err; - - close (fd); - g_free (svalue); - g_free (filename); -} - -static gint -ib_brightness_control_get_value_from_file (IbBrightnessControl *self, const gchar *file) -{ - GError *error; - gchar *svalue; - gint value; - gchar *filename; - - if (self->path == NULL) - return 0; - - svalue = NULL; - error = NULL; - filename = g_build_filename (self->path, file, NULL); - g_file_get_contents (filename, &svalue, NULL, &error); - if (error) { - g_warning ("Fail to get brightness value: %s", error->message); - value = -1; - g_error_free (error); - } else { - value = atoi (svalue); - g_free (svalue); - } - - g_free (filename); - - return value; - -} - -gint -ib_brightness_control_get_value (IbBrightnessControl* self) -{ - return ib_brightness_control_get_value_from_file (self, "brightness"); -} - -gint -ib_brightness_control_get_max_value (IbBrightnessControl* self) -{ - return ib_brightness_control_get_value_from_file (self, "max_brightness"); -} - -void -ib_brightness_control_free (IbBrightnessControl *self) -{ - g_free (self->path); - g_free (self); -} - diff --git a/src/ib-brightness-control.h b/src/ib-brightness-control.h deleted file mode 100644 index 87711e4..0000000 --- a/src/ib-brightness-control.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2012 Canonical Ltd. - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 3, as published - * by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranties of - * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR - * PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - * - * Authors: - * Renato Araujo Oliveira Filho - */ - -#ifndef __IB_BRIGHTNESS_CONTROL_H__ -#define __IB_BRIGHTNESS_CONTROL_H__ - -#include - -typedef struct _IbBrightnessControl IbBrightnessControl; - -IbBrightnessControl* ib_brightness_control_new (void); -void ib_brightness_control_set_value (IbBrightnessControl* self, gint value); -gint ib_brightness_control_get_value (IbBrightnessControl* self); -gint ib_brightness_control_get_max_value (IbBrightnessControl* self); -void ib_brightness_control_free (IbBrightnessControl *self); - -#endif diff --git a/src/ib-brightness-uscreen-control.c b/src/ib-brightness-uscreen-control.c deleted file mode 100644 index ad2c155..0000000 --- a/src/ib-brightness-uscreen-control.c +++ /dev/null @@ -1,202 +0,0 @@ -/* - * Copyright 2014 Canonical Ltd. - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 3, as published - * by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranties of - * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR - * PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - * - * Authors: - * Yuan-Chen Cheng - */ - -#include "ib-brightness-uscreen-control.h" - -static gboolean getBrightnessParams(GDBusProxy* powerd_proxy, int *dim, int *min, - int *max, int *dflt, gboolean *ab_supported); - -GDBusProxy* -uscreen_get_proxy(brightness_params_t *params) -{ - GError *error = NULL; - gboolean ret; - - g_return_val_if_fail (params != NULL, NULL); - - /* For now we still need to obtain the brigthness params from powerd */ - GDBusProxy* powerd_proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM, - G_DBUS_PROXY_FLAGS_NONE, - NULL, - "com.canonical.powerd", - "/com/canonical/powerd", - "com.canonical.powerd", - NULL, - &error); - - if (error != NULL) - { - g_debug ("could not connect to powerd: %s", error->message); - g_error_free (error); - return NULL; - } - - ret = getBrightnessParams(powerd_proxy, &(params->dim), &(params->min), - &(params->max), &(params->dflt), &(params->ab_supported)); - - if (! ret) - { - g_debug ("can't get brightness parameters from powerd"); - g_object_unref (powerd_proxy); - return NULL; - } - - g_clear_object (&powerd_proxy); - - GDBusProxy* uscreen_proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM, - G_DBUS_PROXY_FLAGS_NONE, - NULL, - "com.canonical.Unity.Screen", - "/com/canonical/Unity/Screen", - "com.canonical.Unity.Screen", - NULL, - &error); - - if (error != NULL) - { - g_debug ("could not connect to unity screen: %s", error->message); - g_error_free (error); - return NULL; - } - - return uscreen_proxy; -} - - -static gboolean -getBrightnessParams(GDBusProxy* powerd_proxy, int *dim, int *min, int *max, int *dflt, gboolean *ab_supported) -{ - GVariant *ret = NULL; - GError *error = NULL; - - ret = g_dbus_proxy_call_sync(powerd_proxy, - "getBrightnessParams", - NULL, - G_DBUS_CALL_FLAGS_NONE, - 400, NULL, &error); // timeout: 400 ms - if (!ret) - { - if (error != NULL) - { - if (!g_error_matches(error, G_DBUS_ERROR, G_DBUS_ERROR_SERVICE_UNKNOWN)) - { - g_warning("getBrightnessParams from powerd failed: %s", error->message); - } - g_error_free(error); - } - return FALSE; - } - - g_variant_get(ret, "((iiiib))", dim, min, max, dflt, ab_supported); - g_variant_unref(ret); - return TRUE; -} - -static gboolean setUserBrightness(GDBusProxy* uscreen_proxy, GCancellable *gcancel, int brightness) -{ - GVariant *ret = NULL; - GError *error = NULL; - - ret = g_dbus_proxy_call_sync(uscreen_proxy, - "setUserBrightness", - g_variant_new("(i)", brightness), - G_DBUS_CALL_FLAGS_NONE, - -1, gcancel, &error); - if (!ret) { - g_warning("setUserBrightness via unity.screen failed: %s", error->message); - g_error_free(error); - return FALSE; - } else { - g_variant_unref(ret); - return TRUE; - } -} - -struct _IbBrightnessUScreenControl -{ - GDBusProxy *uscreen_proxy; - GCancellable *gcancel; - - int dim; - int min; - int max; - int dflt; // defalut value - gboolean ab_supported; - - int current; -}; - -IbBrightnessUscreenControl* -ib_brightness_uscreen_control_new (GDBusProxy* uscreen_proxy, brightness_params_t params) -{ - IbBrightnessUscreenControl *control; - - control = g_new0 (IbBrightnessUscreenControl, 1); - control->uscreen_proxy = uscreen_proxy; - control->gcancel = g_cancellable_new(); - - control->dim = params.dim; - control->min = params.min; - control->max = params.max; - control->dflt = params.dflt; - control->ab_supported = params.ab_supported; - - // XXX: set the brightness value is the only way to sync the brightness value with - // unity.screen, and we should set the user prefered / last set brightness value upon startup. - // Before we have code to store last set brightness value or other mechanism, we set - // it to default brightness that powerd proposed. - ib_brightness_uscreen_control_set_value(control, control->dflt); - - return control; -} - -void -ib_brightness_uscreen_control_set_value (IbBrightnessUscreenControl* self, gint value) -{ - gboolean ret; - - value = CLAMP(value, self->min, self->max); - ret = setUserBrightness(self->uscreen_proxy, self->gcancel, value); - if (ret) - { - self->current = value; - } -} - -gint -ib_brightness_uscreen_control_get_value (IbBrightnessUscreenControl* self) -{ - return self->current; -} - -gint -ib_brightness_uscreen_control_get_max_value (IbBrightnessUscreenControl* self) -{ - return self->max; -} - -void -ib_brightness_uscreen_control_free (IbBrightnessUscreenControl *self) -{ - g_cancellable_cancel (self->gcancel); - g_object_unref (self->gcancel); - g_object_unref (self->uscreen_proxy); - g_free (self); -} - diff --git a/src/ib-brightness-uscreen-control.h b/src/ib-brightness-uscreen-control.h deleted file mode 100644 index 3d026a9..0000000 --- a/src/ib-brightness-uscreen-control.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2014 Canonical Ltd. - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 3, as published - * by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranties of - * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR - * PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - * - * Authors: - * Y.C Cheng - */ - -#ifndef __IB_BRIGHTNESS_USCREEN_CONTROL_H__ -#define __IB_BRIGHTNESS_USCREEN_CONTROL_H__ - -#include - -typedef struct { - int dim; - int min; - int max; - int dflt; - gboolean ab_supported; -} brightness_params_t; - -GDBusProxy* uscreen_get_proxy(brightness_params_t *); - -typedef struct _IbBrightnessUScreenControl IbBrightnessUscreenControl; - -IbBrightnessUscreenControl* ib_brightness_uscreen_control_new (GDBusProxy* uscreen_proxy, brightness_params_t params); -void ib_brightness_uscreen_control_set_value (IbBrightnessUscreenControl* self, gint value); -gint ib_brightness_uscreen_control_get_value (IbBrightnessUscreenControl* self); -gint ib_brightness_uscreen_control_get_max_value (IbBrightnessUscreenControl* self); -void ib_brightness_uscreen_control_free (IbBrightnessUscreenControl *self); - -#endif diff --git a/src/service.c b/src/service.c index 0cd448b..35be231 100644 --- a/src/service.c +++ b/src/service.c @@ -22,12 +22,11 @@ #include #include +#include "brightness.h" #include "dbus-shared.h" #include "device.h" #include "device-provider.h" #include "notifier.h" -#include "ib-brightness-control.h" -#include "ib-brightness-uscreen-control.h" #include "service.h" #define BUS_NAME "com.canonical.indicator.power" @@ -104,8 +103,7 @@ struct _IndicatorPowerServicePrivate GSettings * settings; - IbBrightnessControl * brightness_control; - IbBrightnessUscreenControl * brightness_uscreen_control; + IndicatorPowerBrightness * brightness; guint own_id; guint actions_export_id; @@ -459,53 +457,28 @@ create_phone_devices_section (IndicatorPowerService * self G_GNUC_UNUSED) **** ***/ -static void -get_brightness_range (IndicatorPowerService * self, gint * low, gint * high) +static GMenuItem * +create_brightness_menu_item(void) { - priv_t * p = self->priv; - int max = 0; - if (p->brightness_control) - { - max = ib_brightness_control_get_max_value (self->priv->brightness_control); - } - else if (p->brightness_uscreen_control) - { - max = ib_brightness_uscreen_control_get_max_value (self->priv->brightness_uscreen_control); - } - *low = (gint)(max * 0.05); /* 5% minimum -- don't let the screen go completely dark */ - *high = max; -} + GMenuItem * item; -static gdouble -brightness_to_percentage (IndicatorPowerService * self, int brightness) -{ - int lo, hi; - get_brightness_range (self, &lo, &hi); - return (brightness-lo) / (double)(hi-lo); -} + item = g_menu_item_new(NULL, "indicator.brightness"); + g_menu_item_set_attribute(item, "x-canonical-type", "s", "com.canonical.unity.slider"); + g_menu_item_set_attribute(item, "min-value", "d", 0.1); + g_menu_item_set_attribute(item, "max-value", "d", 1.0); + g_menu_item_set_attribute(item, "min-icon", "s", "torch-off" ); + g_menu_item_set_attribute(item, "max-icon", "s", "torch-on" ); -static int -percentage_to_brightness (IndicatorPowerService * self, double percentage) -{ - int lo, hi; - get_brightness_range (self, &lo, &hi); - return (int)(lo + (percentage*(hi-lo))); + return item; } static GVariant * action_state_for_brightness (IndicatorPowerService * self) { - priv_t * p = self->priv; - gint brightness = 0; - if (p->brightness_control) - { - brightness = ib_brightness_control_get_value (p->brightness_control); - } - else if (p->brightness_uscreen_control) - { - brightness = ib_brightness_uscreen_control_get_value (p->brightness_uscreen_control); - } - return g_variant_new_double (brightness_to_percentage (self, brightness)); + IndicatorPowerBrightness * b = self->priv->brightness; + GVariant * v = g_variant_new_double(indicator_power_brightness_get_percentage(b)); + g_message("new brightness action state: %s", g_variant_print(v, TRUE)); + return v; } static void @@ -521,19 +494,9 @@ on_brightness_change_requested (GSimpleAction * action G_GNUC_UNUSED, gpointer gself) { IndicatorPowerService * self = INDICATOR_POWER_SERVICE (gself); - const gdouble percentage = g_variant_get_double (parameter); - const int brightness = percentage_to_brightness (self, percentage); - if (self->priv->brightness_control) - { - ib_brightness_control_set_value (self->priv->brightness_control, brightness); - } - else if (self->priv->brightness_uscreen_control) - { - ib_brightness_uscreen_control_set_value (self->priv->brightness_uscreen_control, brightness); - } - - update_brightness_action_state (self); + indicator_power_brightness_set_percentage(self->priv->brightness, + g_variant_get_double (parameter)); } static GMenuModel * @@ -557,15 +520,21 @@ create_desktop_settings_section (IndicatorPowerService * self G_GNUC_UNUSED) } static GMenuModel * -create_phone_settings_section (IndicatorPowerService * self) +create_phone_settings_section(IndicatorPowerService * self) { GMenu * section; + GMenuItem * item; + + section = g_menu_new(); - section = g_menu_new (); - update_brightness_action_state (self); - g_menu_append (section, _("Battery settings…"), "indicator.activate-phone-settings"); + item = create_brightness_menu_item(); + g_menu_append_item(section, item); + update_brightness_action_state(self); + g_object_unref(item); - return G_MENU_MODEL (section); + g_menu_append(section, _("Battery settings…"), "indicator.activate-phone-settings"); + + return G_MENU_MODEL(section); } /*** @@ -1015,16 +984,13 @@ my_dispose (GObject * o) g_clear_object (&p->notifier); g_clear_object (&p->brightness_action); + g_clear_object (&p->brightness); g_clear_object (&p->battery_level_action); g_clear_object (&p->header_action); g_clear_object (&p->actions); g_clear_object (&p->conn); - // g_clear_pointer has NULL check inside. - g_clear_pointer (&p->brightness_control, ib_brightness_control_free); - g_clear_pointer (&p->brightness_uscreen_control, ib_brightness_uscreen_control_free); - indicator_power_service_set_device_provider (self, NULL); G_OBJECT_CLASS (indicator_power_service_parent_class)->dispose (o); @@ -1037,8 +1003,6 @@ my_dispose (GObject * o) static void indicator_power_service_init (IndicatorPowerService * self) { - GDBusProxy *uscreen_proxy; - brightness_params_t brightness_params; priv_t * p = G_TYPE_INSTANCE_GET_PRIVATE (self, INDICATOR_TYPE_POWER_SERVICE, IndicatorPowerServicePrivate); @@ -1050,15 +1014,9 @@ indicator_power_service_init (IndicatorPowerService * self) p->notifier = indicator_power_notifier_new (); - uscreen_proxy = uscreen_get_proxy(&brightness_params); - if (uscreen_proxy != NULL) - { - p->brightness_uscreen_control = ib_brightness_uscreen_control_new(uscreen_proxy, brightness_params); - } - else - { - p->brightness_control = ib_brightness_control_new (); - } + p->brightness = indicator_power_brightness_new(); + g_signal_connect_swapped(p->brightness, "notify::perentage", + G_CALLBACK(update_brightness_action_state), self); init_gactions (self); -- cgit v1.2.3 From f5fae618075cd148c5d6653518cd6219f091810b Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 10 Sep 2014 16:52:30 -0500 Subject: fix typo; change g_message() calls to g_debug(); use same percentage range as ubuntu-system-settings --- src/brightness.c | 22 +++++++++++++--------- src/service.c | 8 +++----- 2 files changed, 16 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/brightness.c b/src/brightness.c index 070305e..89613ab 100644 --- a/src/brightness.c +++ b/src/brightness.c @@ -201,12 +201,12 @@ on_powerd_brightness_params_ready(GObject * source, &p->powerd_max, &p->powerd_dflt, &p->powerd_ab_supported); - g_message("powerd brightness settings: dim=%d, min=%d, max=%d, dflt=%d, ab_supported=%d", - p->powerd_dim, - p->powerd_min, - p->powerd_max, - p->powerd_dflt, - (int)p->powerd_ab_supported); + g_debug("powerd brightness settings: dim=%d, min=%d, max=%d, dflt=%d, ab_supported=%d", + p->powerd_dim, + p->powerd_min, + p->powerd_max, + p->powerd_dflt, + (int)p->powerd_ab_supported); /* uscreen doesn't have a get_brightness() function, so the only way to know the value is to initialize it ourselves @@ -302,7 +302,7 @@ set_uscreen_user_brightness(IndicatorPowerBrightness * self, "/com/canonical/Unity/Screen", "com.canonical.Unity.Screen", "setUserBrightness", - g_variant_new_int32(value), + g_variant_new("(i)", value), NULL, /* no return args */ G_DBUS_CALL_FLAGS_NONE, -1, /* default timeout */ @@ -346,8 +346,8 @@ indicator_power_brightness_class_init (IndicatorPowerBrightnessClass * klass) properties[PROP_PERCENTAGE] = g_param_spec_double("percentage", "Percentage", "Brightness percentage", - 0.1, /* don't allow completely black */ - 1.0, /* brightest */ + 0.0, /* minimum */ + 1.0, /* maximum */ 0.8, G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS); @@ -375,6 +375,10 @@ indicator_power_brightness_set_percentage(IndicatorPowerBrightness * self, g_return_if_fail(INDICATOR_IS_POWER_BRIGHTNESS(self)); p = get_priv(self); + + g_debug("%s called; current value is %.2f, desired value is %.2f", + G_STRFUNC, p->percentage, percentage); + if ((int)(p->percentage*100) != (int)(percentage*100)) { set_uscreen_user_brightness(self, percentage_to_brightness(self, percentage)); diff --git a/src/service.c b/src/service.c index 35be231..0415881 100644 --- a/src/service.c +++ b/src/service.c @@ -464,7 +464,7 @@ create_brightness_menu_item(void) item = g_menu_item_new(NULL, "indicator.brightness"); g_menu_item_set_attribute(item, "x-canonical-type", "s", "com.canonical.unity.slider"); - g_menu_item_set_attribute(item, "min-value", "d", 0.1); + g_menu_item_set_attribute(item, "min-value", "d", 0.0); g_menu_item_set_attribute(item, "max-value", "d", 1.0); g_menu_item_set_attribute(item, "min-icon", "s", "torch-off" ); g_menu_item_set_attribute(item, "max-icon", "s", "torch-on" ); @@ -476,9 +476,7 @@ static GVariant * action_state_for_brightness (IndicatorPowerService * self) { IndicatorPowerBrightness * b = self->priv->brightness; - GVariant * v = g_variant_new_double(indicator_power_brightness_get_percentage(b)); - g_message("new brightness action state: %s", g_variant_print(v, TRUE)); - return v; + return g_variant_new_double(indicator_power_brightness_get_percentage(b)); } static void @@ -1015,7 +1013,7 @@ indicator_power_service_init (IndicatorPowerService * self) p->notifier = indicator_power_notifier_new (); p->brightness = indicator_power_brightness_new(); - g_signal_connect_swapped(p->brightness, "notify::perentage", + g_signal_connect_swapped(p->brightness, "notify::percentage", G_CALLBACK(update_brightness_action_state), self); init_gactions (self); -- cgit v1.2.3 From ae12f25f06be25265f09f65874dcb183a6a5020f Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 10 Sep 2014 17:03:40 -0500 Subject: copyediting: whitespace, comment text --- src/brightness.c | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/brightness.c b/src/brightness.c index 89613ab..4a8db52 100644 --- a/src/brightness.c +++ b/src/brightness.c @@ -62,10 +62,10 @@ G_DEFINE_TYPE_WITH_PRIVATE(IndicatorPowerBrightness, ***/ static void -my_get_property (GObject * o, - guint property_id, - GValue * value, - GParamSpec * pspec) +my_get_property(GObject * o, + guint property_id, + GValue * value, + GParamSpec * pspec) { IndicatorPowerBrightness * self = INDICATOR_POWER_BRIGHTNESS(o); @@ -81,10 +81,10 @@ my_get_property (GObject * o, } static void -my_set_property (GObject * o, - guint property_id, - const GValue * value, - GParamSpec * pspec) +my_set_property(GObject * o, + guint property_id, + const GValue * value, + GParamSpec * pspec) { IndicatorPowerBrightness * self = INDICATOR_POWER_BRIGHTNESS(o); @@ -100,15 +100,15 @@ my_set_property (GObject * o, } static void -my_dispose (GObject * o) +my_dispose(GObject * o) { IndicatorPowerBrightness * self = INDICATOR_POWER_BRIGHTNESS(o); priv_t * p = get_priv(self); if (p->cancellable != NULL) { - g_cancellable_cancel (p->cancellable); - g_clear_object (&p->cancellable); + g_cancellable_cancel(p->cancellable); + g_clear_object(&p->cancellable); } if (p->powerd_name_tag) @@ -208,9 +208,9 @@ on_powerd_brightness_params_ready(GObject * source, p->powerd_dflt, (int)p->powerd_ab_supported); - /* uscreen doesn't have a get_brightness() function, - so the only way to know the value is to initialize it ourselves - (and hope nobody changes it :P */ + /* uscreen doesn't have any way of accessing the current brightness, + so the only way to know its value is to initialize it ourselves + (and hope nobody else changes it :P) */ percentage = brightness_to_percentage(self, p->powerd_dflt); indicator_power_brightness_set_percentage(self, percentage); @@ -251,7 +251,7 @@ on_powerd_appeared(GDBusConnection * connection, g_clear_object(&p->system_bus); p->system_bus = g_object_ref(connection); - /* get powerd's params */ + /* update our cache of powerd's brightness params */ call_powerd_get_brightness_params(self); } @@ -261,6 +261,7 @@ on_powerd_vanished(GDBusConnection * connection G_GNUC_UNUSED, gpointer gself) { priv_t * p = get_priv(INDICATOR_POWER_BRIGHTNESS(gself)); + p->have_powerd_params = FALSE; } @@ -282,7 +283,7 @@ on_set_uscreen_user_brightness_result(GObject * system_bus, v = g_dbus_connection_call_finish(G_DBUS_CONNECTION(system_bus), res, &error); if (error != NULL) { - if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) + if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) g_warning("Unable to call uscreen.setBrightness: %s", error->message); g_error_free(error); @@ -317,7 +318,7 @@ set_uscreen_user_brightness(IndicatorPowerBrightness * self, ***/ static void -indicator_power_brightness_init (IndicatorPowerBrightness * self) +indicator_power_brightness_init(IndicatorPowerBrightness * self) { priv_t * p = get_priv(self); @@ -333,7 +334,7 @@ indicator_power_brightness_init (IndicatorPowerBrightness * self) } static void -indicator_power_brightness_class_init (IndicatorPowerBrightnessClass * klass) +indicator_power_brightness_class_init(IndicatorPowerBrightnessClass * klass) { GObjectClass * object_class = G_OBJECT_CLASS(klass); @@ -351,7 +352,7 @@ indicator_power_brightness_class_init (IndicatorPowerBrightnessClass * klass) 0.8, G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS); - g_object_class_install_properties (object_class, LAST_PROP, properties); + g_object_class_install_properties(object_class, LAST_PROP, properties); } /*** @@ -361,7 +362,7 @@ indicator_power_brightness_class_init (IndicatorPowerBrightnessClass * klass) IndicatorPowerBrightness * indicator_power_brightness_new(void) { - gpointer o = g_object_new (INDICATOR_TYPE_POWER_BRIGHTNESS, NULL); + gpointer o = g_object_new(INDICATOR_TYPE_POWER_BRIGHTNESS, NULL); return INDICATOR_POWER_BRIGHTNESS(o); } -- cgit v1.2.3 From 4adcc3265eef70604dcd86587b1042c7033b7edc Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 10 Sep 2014 17:08:58 -0500 Subject: copyediting: rename 'dflt' as 'default_value' for readability --- src/brightness.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/brightness.c b/src/brightness.c index 4a8db52..e407fa4 100644 --- a/src/brightness.c +++ b/src/brightness.c @@ -43,7 +43,7 @@ typedef struct gint powerd_dim; gint powerd_min; gint powerd_max; - gint powerd_dflt; + gint powerd_default_value; gboolean powerd_ab_supported; gboolean have_powerd_params; } @@ -199,19 +199,19 @@ on_powerd_brightness_params_ready(GObject * source, g_variant_get(v, "((iiiib))", &p->powerd_dim, &p->powerd_min, &p->powerd_max, - &p->powerd_dflt, + &p->powerd_default_value, &p->powerd_ab_supported); - g_debug("powerd brightness settings: dim=%d, min=%d, max=%d, dflt=%d, ab_supported=%d", + g_debug("powerd brightness settings: dim=%d, min=%d, max=%d, default=%d, ab_supported=%d", p->powerd_dim, p->powerd_min, p->powerd_max, - p->powerd_dflt, + p->powerd_default_value, (int)p->powerd_ab_supported); /* uscreen doesn't have any way of accessing the current brightness, so the only way to know its value is to initialize it ourselves (and hope nobody else changes it :P) */ - percentage = brightness_to_percentage(self, p->powerd_dflt); + percentage = brightness_to_percentage(self, p->powerd_default_value); indicator_power_brightness_set_percentage(self, percentage); /* cleanup */ -- cgit v1.2.3 From 7ced2387ed536e9bc76ae16bf91dc5890ecf0354 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 11 Sep 2014 13:18:14 -0500 Subject: first draft of adding persistence to the brightness slider --- src/brightness.c | 105 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 86 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/brightness.c b/src/brightness.c index e407fa4..e43189a 100644 --- a/src/brightness.c +++ b/src/brightness.c @@ -21,6 +21,10 @@ #include +#define SCHEMA_NAME "com.ubuntu.touch.system" +#define KEY_BRIGHTNESS "brightness" +#define KEY_NEED_DEFAULT "brightness-needs-a-default" + enum { PROP_0, @@ -35,6 +39,8 @@ typedef struct GDBusConnection * system_bus; GCancellable * cancellable; + GSettings * settings; + guint powerd_name_tag; double percentage; @@ -117,6 +123,7 @@ my_dispose(GObject * o) p->powerd_name_tag = 0; } + g_clear_object(&p->settings); g_clear_object(&p->system_bus); G_OBJECT_CLASS(indicator_power_brightness_parent_class)->dispose(o); @@ -172,6 +179,8 @@ percentage_to_brightness(IndicatorPowerBrightness * self, double percentage) **** DBus Chatter: com.canonical.powerd ***/ +static void set_brightness_global(IndicatorPowerBrightness*, int); + static void on_powerd_brightness_params_ready(GObject * source, GAsyncResult * res, @@ -193,7 +202,6 @@ on_powerd_brightness_params_ready(GObject * source, { IndicatorPowerBrightness * self = INDICATOR_POWER_BRIGHTNESS(gself); priv_t * p = get_priv(self); - double percentage; p->have_powerd_params = TRUE; g_variant_get(v, "((iiiib))", &p->powerd_dim, @@ -208,11 +216,23 @@ on_powerd_brightness_params_ready(GObject * source, p->powerd_default_value, (int)p->powerd_ab_supported); - /* uscreen doesn't have any way of accessing the current brightness, - so the only way to know its value is to initialize it ourselves - (and hope nobody else changes it :P) */ - percentage = brightness_to_percentage(self, p->powerd_default_value); - indicator_power_brightness_set_percentage(self, percentage); + + if (p->settings != NULL) + { + if (g_settings_get_boolean(p->settings, KEY_NEED_DEFAULT)) + { + /* user's first session, so init the schema's default + brightness from powerd's hardware-specific params */ + g_message("%s is true, so setting brightness to powerd default '%d'", KEY_NEED_DEFAULT, p->powerd_default_value); + set_brightness_global(self, p->powerd_default_value); + g_settings_set_boolean(p->settings, KEY_NEED_DEFAULT, FALSE); + } + else + { + /* not the first time, so restore the previous session's brightness */ + set_brightness_global(self, g_settings_get_int(p->settings, KEY_BRIGHTNESS)); + } + } /* cleanup */ g_variant_unref(v); @@ -312,6 +332,43 @@ set_uscreen_user_brightness(IndicatorPowerBrightness * self, self); } +/*** +**** +***/ + +static void +set_brightness_local(IndicatorPowerBrightness * self, int brightness) +{ + priv_t * p = get_priv(self); + p->percentage = brightness_to_percentage(self, brightness); + g_message("%s setting brightness property percentage to %.2f", G_STRFUNC, p->percentage); + g_object_notify_by_pspec(G_OBJECT(self), properties[PROP_PERCENTAGE]); +} + +static void +on_brightness_changed_in_schema(GSettings * settings, + gchar * key, + gpointer gself) +{ + g_message("%s schema changed; updating our local property", G_STRFUNC); + set_brightness_local(INDICATOR_POWER_BRIGHTNESS(gself), + g_settings_get_int(settings, key)); +} + +static void +set_brightness_global(IndicatorPowerBrightness * self, int brightness) +{ + priv_t * p = get_priv(self); + + g_message("%s setting uscreen and local to %d", G_STRFUNC, brightness); + + set_uscreen_user_brightness(self, brightness); + + if (p->settings != NULL) + g_settings_set_int(p->settings, KEY_BRIGHTNESS, brightness); + else + set_brightness_local(self, brightness); +} /*** **** Instantiation @@ -320,10 +377,30 @@ set_uscreen_user_brightness(IndicatorPowerBrightness * self, static void indicator_power_brightness_init(IndicatorPowerBrightness * self) { - priv_t * p = get_priv(self); + priv_t * p; + GSettingsSchema * schema; + p = get_priv(self); p->cancellable = g_cancellable_new(); + schema = g_settings_schema_source_lookup(g_settings_schema_source_get_default(), + SCHEMA_NAME, + TRUE); + + /* "brightness" is only spec'ed for the phone profile, + so fail gracefully & silently if we don't have the + schema for it. */ + if (schema != NULL) + { + if (g_settings_schema_has_key(schema, KEY_BRIGHTNESS)) + { + p->settings = g_settings_new(SCHEMA_NAME); + g_signal_connect(p->settings, "changed::" KEY_BRIGHTNESS, + G_CALLBACK(on_brightness_changed_in_schema), self); + } + g_settings_schema_unref(schema); + } + p->powerd_name_tag = g_bus_watch_name(G_BUS_TYPE_SYSTEM, "com.canonical.powerd", G_BUS_NAME_WATCHER_FLAGS_NONE, @@ -371,22 +448,12 @@ void indicator_power_brightness_set_percentage(IndicatorPowerBrightness * self, double percentage) { - priv_t * p; - g_return_if_fail(INDICATOR_IS_POWER_BRIGHTNESS(self)); - p = get_priv(self); - g_debug("%s called; current value is %.2f, desired value is %.2f", - G_STRFUNC, p->percentage, percentage); - - if ((int)(p->percentage*100) != (int)(percentage*100)) - { - set_uscreen_user_brightness(self, percentage_to_brightness(self, percentage)); + G_STRFUNC, get_priv(self)->percentage, percentage); - p->percentage = percentage; - g_object_notify_by_pspec(G_OBJECT(self), properties[PROP_PERCENTAGE]); - } + set_brightness_global(self, percentage_to_brightness(self, percentage)); } double -- cgit v1.2.3 From 2e3d004df20a3485e37ee4e8916739528f646d9f Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 11 Sep 2014 14:24:31 -0500 Subject: remove the new code's g_message() tracers or replace with g_debug() --- src/brightness.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'src') diff --git a/src/brightness.c b/src/brightness.c index e43189a..9288d50 100644 --- a/src/brightness.c +++ b/src/brightness.c @@ -223,7 +223,7 @@ on_powerd_brightness_params_ready(GObject * source, { /* user's first session, so init the schema's default brightness from powerd's hardware-specific params */ - g_message("%s is true, so setting brightness to powerd default '%d'", KEY_NEED_DEFAULT, p->powerd_default_value); + g_debug("%s is true, so initializing brightness to powerd default '%d'", KEY_NEED_DEFAULT, p->powerd_default_value); set_brightness_global(self, p->powerd_default_value); g_settings_set_boolean(p->settings, KEY_NEED_DEFAULT, FALSE); } @@ -341,7 +341,6 @@ set_brightness_local(IndicatorPowerBrightness * self, int brightness) { priv_t * p = get_priv(self); p->percentage = brightness_to_percentage(self, brightness); - g_message("%s setting brightness property percentage to %.2f", G_STRFUNC, p->percentage); g_object_notify_by_pspec(G_OBJECT(self), properties[PROP_PERCENTAGE]); } @@ -350,7 +349,6 @@ on_brightness_changed_in_schema(GSettings * settings, gchar * key, gpointer gself) { - g_message("%s schema changed; updating our local property", G_STRFUNC); set_brightness_local(INDICATOR_POWER_BRIGHTNESS(gself), g_settings_get_int(settings, key)); } @@ -360,8 +358,6 @@ set_brightness_global(IndicatorPowerBrightness * self, int brightness) { priv_t * p = get_priv(self); - g_message("%s setting uscreen and local to %d", G_STRFUNC, brightness); - set_uscreen_user_brightness(self, brightness); if (p->settings != NULL) -- cgit v1.2.3 From 9d0cc593d2cf79f4763f69aa7d7b58919721fc22 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 11 Sep 2014 14:31:33 -0500 Subject: tweak comments for readability --- src/brightness.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/brightness.c b/src/brightness.c index 9288d50..21526ba 100644 --- a/src/brightness.c +++ b/src/brightness.c @@ -175,9 +175,12 @@ percentage_to_brightness(IndicatorPowerBrightness * self, double percentage) return brightness; } -/*** -**** DBus Chatter: com.canonical.powerd -***/ +/** + * DBus Chatter: com.canonical.powerd + * + * This is used to get default value, and upper and lower bounds, + * of the brightness setting + */ static void set_brightness_global(IndicatorPowerBrightness*, int); @@ -285,9 +288,11 @@ on_powerd_vanished(GDBusConnection * connection G_GNUC_UNUSED, p->have_powerd_params = FALSE; } -/*** -**** DBus Chatter: com.canonical.Unity.Screen -***/ +/** + * DBus Chatter: com.canonical.Unity.Screen + * + * Used to set the backlight brightness via setUserBrightness + */ /* setUserBrightness doesn't return anything, so this function is just to check for bus error messages */ @@ -446,9 +451,6 @@ indicator_power_brightness_set_percentage(IndicatorPowerBrightness * self, { g_return_if_fail(INDICATOR_IS_POWER_BRIGHTNESS(self)); - g_debug("%s called; current value is %.2f, desired value is %.2f", - G_STRFUNC, get_priv(self)->percentage, percentage); - set_brightness_global(self, percentage_to_brightness(self, percentage)); } -- cgit v1.2.3 From 54fd6bfd6642b5820536f78e9913f2e1dc46a45a Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 11 Sep 2014 19:15:14 -0500 Subject: add an 'auto brightness' checkbox --- src/brightness.c | 64 +++++++++++++++++++++++++++++++++++------ src/service.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 127 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/brightness.c b/src/brightness.c index 21526ba..df5511c 100644 --- a/src/brightness.c +++ b/src/brightness.c @@ -22,13 +22,17 @@ #include #define SCHEMA_NAME "com.ubuntu.touch.system" +#define KEY_AUTO "auto-brightness" +#define KEY_AUTO_SUPPORTED "auto-brightness-supported" #define KEY_BRIGHTNESS "brightness" -#define KEY_NEED_DEFAULT "brightness-needs-a-default" +#define KEY_NEED_DEFAULT "brightness-needs-hardware-default" enum { PROP_0, PROP_PERCENTAGE, + PROP_AUTO, + PROP_AUTO_SUPPORTED, LAST_PROP }; @@ -74,6 +78,7 @@ my_get_property(GObject * o, GParamSpec * pspec) { IndicatorPowerBrightness * self = INDICATOR_POWER_BRIGHTNESS(o); + priv_t * p = get_priv(self); switch (property_id) { @@ -81,6 +86,14 @@ my_get_property(GObject * o, g_value_set_double(value, indicator_power_brightness_get_percentage(self)); break; + case PROP_AUTO: + g_value_set_boolean(value, p->settings ? g_settings_get_boolean(p->settings, KEY_AUTO) : FALSE); + break; + + case PROP_AUTO_SUPPORTED: + g_value_set_boolean(value, p->powerd_ab_supported); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID(o, property_id, pspec); } @@ -93,6 +106,7 @@ my_set_property(GObject * o, GParamSpec * pspec) { IndicatorPowerBrightness * self = INDICATOR_POWER_BRIGHTNESS(o); + priv_t * p = get_priv(self); switch (property_id) { @@ -100,6 +114,11 @@ my_set_property(GObject * o, indicator_power_brightness_set_percentage(self, g_value_get_double(value)); break; + case PROP_AUTO: + if (p->settings != NULL) + g_settings_set_boolean (p->settings, KEY_AUTO, g_value_get_boolean(value)); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID(o, property_id, pspec); } @@ -205,6 +224,7 @@ on_powerd_brightness_params_ready(GObject * source, { IndicatorPowerBrightness * self = INDICATOR_POWER_BRIGHTNESS(gself); priv_t * p = get_priv(self); + const gboolean old_ab_supported = p->powerd_ab_supported; p->have_powerd_params = TRUE; g_variant_get(v, "((iiiib))", &p->powerd_dim, @@ -218,7 +238,9 @@ on_powerd_brightness_params_ready(GObject * source, p->powerd_max, p->powerd_default_value, (int)p->powerd_ab_supported); - + + if (old_ab_supported != p->powerd_ab_supported) + g_object_notify_by_pspec(G_OBJECT(self), properties[PROP_AUTO_SUPPORTED]); if (p->settings != NULL) { @@ -371,6 +393,13 @@ set_brightness_global(IndicatorPowerBrightness * self, int brightness) set_brightness_local(self, brightness); } +static void +on_auto_changed_in_schema(IndicatorPowerBrightness * self) +{ + g_object_notify_by_pspec(G_OBJECT(self), properties[PROP_AUTO]); +} + + /*** **** Instantiation ***/ @@ -398,6 +427,8 @@ indicator_power_brightness_init(IndicatorPowerBrightness * self) p->settings = g_settings_new(SCHEMA_NAME); g_signal_connect(p->settings, "changed::" KEY_BRIGHTNESS, G_CALLBACK(on_brightness_changed_in_schema), self); + g_signal_connect_swapped(p->settings, "changed::" KEY_AUTO, + G_CALLBACK(on_auto_changed_in_schema), self); } g_settings_schema_unref(schema); } @@ -422,13 +453,28 @@ indicator_power_brightness_class_init(IndicatorPowerBrightnessClass * klass) properties[PROP_0] = NULL; - properties[PROP_PERCENTAGE] = g_param_spec_double("percentage", - "Percentage", - "Brightness percentage", - 0.0, /* minimum */ - 1.0, /* maximum */ - 0.8, - G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS); + properties[PROP_PERCENTAGE] = g_param_spec_double( + "percentage", + "Percentage", + "Brightness percentage", + 0.0, /* minimum */ + 1.0, /* maximum */ + 0.8, + G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS); + + properties[PROP_AUTO] = g_param_spec_boolean( + "auto-brightness", + "Auto-Brightness", + "Automatically adjust brightness level", + FALSE, + G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS); + + properties[PROP_AUTO_SUPPORTED] = g_param_spec_boolean( + "auto-brightness-supported", + "Auto-Brightness Supported", + "True if the device can automatically adjust brightness", + FALSE, + G_PARAM_READABLE|G_PARAM_STATIC_STRINGS); g_object_class_install_properties(object_class, LAST_PROP, properties); } diff --git a/src/service.c b/src/service.c index 0415881..665151c 100644 --- a/src/service.c +++ b/src/service.c @@ -522,6 +522,7 @@ create_phone_settings_section(IndicatorPowerService * self) { GMenu * section; GMenuItem * item; + gboolean ab_supported; section = g_menu_new(); @@ -530,6 +531,18 @@ create_phone_settings_section(IndicatorPowerService * self) update_brightness_action_state(self); g_object_unref(item); + g_object_get(self->priv->brightness, + "auto-brightness-supported", &ab_supported, + NULL); + + if (ab_supported) + { + item = g_menu_item_new(_("Adjust brightness automatically"), "indicator.auto-brightness"); + g_menu_item_set_attribute(item, "x-canonical-type", "s", "com.canonical.indicator.switch"); + g_menu_append_item(section, item); + g_object_unref(item); + } + g_menu_append(section, _("Battery settings…"), "indicator.activate-phone-settings"); return G_MENU_MODEL(section); @@ -579,7 +592,7 @@ rebuild_now (IndicatorPowerService * self, guint sections) if (sections & SECTION_SETTINGS) { rebuild_section (desktop->submenu, 1, create_desktop_settings_section (self)); - rebuild_section (phone->submenu, 1, create_desktop_settings_section (self)); + rebuild_section (phone->submenu, 1, create_phone_settings_section (self)); } } @@ -719,6 +732,28 @@ on_phone_settings_activated (GSimpleAction * a G_GNUC_UNUSED, **** ***/ +static gboolean +convert_auto_prop_to_state(GBinding * binding G_GNUC_UNUSED, + const GValue * from_value, + GValue * to_value, + gpointer user_data G_GNUC_UNUSED) +{ + const gboolean b = g_value_get_boolean(from_value); + g_value_set_variant(to_value, g_variant_new_boolean(b)); + return TRUE; +} + +static gboolean +convert_auto_state_to_prop(GBinding * binding G_GNUC_UNUSED, + const GValue * from_value, + GValue * to_value, + gpointer user_data G_GNUC_UNUSED) +{ + GVariant * v = g_value_get_variant(from_value); + g_value_set_boolean(to_value, g_variant_get_boolean(v)); + return TRUE; +} + static void init_gactions (IndicatorPowerService * self) { @@ -751,6 +786,16 @@ init_gactions (IndicatorPowerService * self) g_action_map_add_action (G_ACTION_MAP(p->actions), G_ACTION(a)); p->battery_level_action = a; + /* add the auto-brightness action */ + a = g_simple_action_new_stateful("auto-brightness", NULL, g_variant_new_boolean(FALSE)); + g_object_bind_property_full(p->brightness, "auto-brightness", + a, "state", + G_BINDING_SYNC_CREATE|G_BINDING_BIDIRECTIONAL, + convert_auto_prop_to_state, + convert_auto_state_to_prop, + NULL, NULL); + g_action_map_add_action(G_ACTION_MAP(p->actions), G_ACTION(a)); + /* add the brightness action */ a = g_simple_action_new_stateful ("brightness", NULL, action_state_for_brightness (self)); g_action_map_add_action (G_ACTION_MAP(p->actions), G_ACTION(a)); @@ -815,9 +860,6 @@ on_bus_acquired (GDBusConnection * connection, g_string_printf (path, "%s/%s", BUS_PATH, menu_names[i]); - if (menu->menu == NULL) - create_menu (self, i); - if ((id = g_dbus_connection_export_menu_model (connection, path->str, G_MENU_MODEL (menu->menu), @@ -909,6 +951,12 @@ on_devices_changed (IndicatorPowerService * self) rebuild_now (self, SECTION_HEADER | SECTION_DEVICES); } +static void +on_auto_brightness_supported_changed(IndicatorPowerService * self) +{ + rebuild_now(self, SECTION_SETTINGS); +} + /*** **** GObject virtual functions @@ -1001,9 +1049,12 @@ my_dispose (GObject * o) static void indicator_power_service_init (IndicatorPowerService * self) { - priv_t * p = G_TYPE_INSTANCE_GET_PRIVATE (self, - INDICATOR_TYPE_POWER_SERVICE, - IndicatorPowerServicePrivate); + priv_t * p; + int i; + + p = G_TYPE_INSTANCE_GET_PRIVATE (self, + INDICATOR_TYPE_POWER_SERVICE, + IndicatorPowerServicePrivate); self->priv = p; p->cancellable = g_cancellable_new (); @@ -1020,14 +1071,20 @@ indicator_power_service_init (IndicatorPowerService * self) g_signal_connect_swapped (p->settings, "changed", G_CALLBACK(rebuild_header_now), self); - p->own_id = g_bus_own_name (G_BUS_TYPE_SESSION, - BUS_NAME, - G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT, - on_bus_acquired, - NULL, - on_name_lost, - self, - NULL); + for (i=0; ibrightness, "notify::auto-brightness-supported", + G_CALLBACK(on_auto_brightness_supported_changed), self); + + p->own_id = g_bus_own_name(G_BUS_TYPE_SESSION, + BUS_NAME, + G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT, + on_bus_acquired, + NULL, + on_name_lost, + self, + NULL); } static void -- cgit v1.2.3 From e5abeb9845281e0c1f3fec5ba343f9bf4becbad5 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 12 Sep 2014 11:37:02 -0500 Subject: in brightness.c, set_uscreen_user_brightness() should be a private/static function --- src/brightness.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/brightness.c b/src/brightness.c index df5511c..5e7c5e5 100644 --- a/src/brightness.c +++ b/src/brightness.c @@ -339,7 +339,7 @@ on_set_uscreen_user_brightness_result(GObject * system_bus, g_clear_pointer(&v, g_variant_unref); } -void +static void set_uscreen_user_brightness(IndicatorPowerBrightness * self, int value) { -- cgit v1.2.3