diff options
Diffstat (limited to 'src/service.c')
-rw-r--r-- | src/service.c | 193 |
1 files changed, 103 insertions, 90 deletions
diff --git a/src/service.c b/src/service.c index 0cd448b..665151c 100644 --- a/src/service.c +++ b/src/service.c @@ -22,12 +22,11 @@ #include <gio/gio.h> #include <url-dispatcher.h> +#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,26 @@ 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.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" ); -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; + return g_variant_new_double(indicator_power_brightness_get_percentage(b)); } static void @@ -521,19 +492,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 +518,34 @@ 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; + gboolean ab_supported; + + section = g_menu_new(); + + item = create_brightness_menu_item(); + g_menu_append_item(section, item); + 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); + } - section = g_menu_new (); - update_brightness_action_state (self); - g_menu_append (section, _("Battery settingsā¦"), "indicator.activate-phone-settings"); + g_menu_append(section, _("Battery settingsā¦"), "indicator.activate-phone-settings"); - return G_MENU_MODEL (section); + return G_MENU_MODEL(section); } /*** @@ -612,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)); } } @@ -752,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) { @@ -784,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)); @@ -848,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), @@ -942,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 @@ -1015,16 +1030,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,11 +1049,12 @@ 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); + 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 (); @@ -1050,28 +1063,28 @@ 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::percentage", + G_CALLBACK(update_brightness_action_state), self); init_gactions (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; i<N_PROFILES; ++i) + create_menu(self, i); + + g_signal_connect_swapped(p->brightness, "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 |