aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCharles Kerr <charles.kerr@canonical.com>2014-09-11 19:15:14 -0500
committerCharles Kerr <charles.kerr@canonical.com>2014-09-11 19:15:14 -0500
commit54fd6bfd6642b5820536f78e9913f2e1dc46a45a (patch)
tree0a7f1e459797b74c705ce442e274ea5e86dc5831 /src
parentd5d306583034954d99baf1515aea29b036eacbb8 (diff)
downloadayatana-indicator-power-54fd6bfd6642b5820536f78e9913f2e1dc46a45a.tar.gz
ayatana-indicator-power-54fd6bfd6642b5820536f78e9913f2e1dc46a45a.tar.bz2
ayatana-indicator-power-54fd6bfd6642b5820536f78e9913f2e1dc46a45a.zip
add an 'auto brightness' checkbox
Diffstat (limited to 'src')
-rw-r--r--src/brightness.c64
-rw-r--r--src/service.c87
2 files changed, 127 insertions, 24 deletions
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 <gio/gio.h>
#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; 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