diff options
author | Y.C cheng <yc.cheng@canonical.com> | 2014-03-03 17:18:05 +0800 |
---|---|---|
committer | Y.C cheng <yc.cheng@canonical.com> | 2014-03-03 17:18:05 +0800 |
commit | 18fd0fa741f818ed8a6d09b8a2a4623e85d6bbdf (patch) | |
tree | 9399dc248cceefcdd4f219db5c06fc08b4f211dd /src | |
parent | 8c96b02c3ad3ee3286f75324ac6c262a091f0a43 (diff) | |
download | ayatana-indicator-power-18fd0fa741f818ed8a6d09b8a2a4623e85d6bbdf.tar.gz ayatana-indicator-power-18fd0fa741f818ed8a6d09b8a2a4623e85d6bbdf.tar.bz2 ayatana-indicator-power-18fd0fa741f818ed8a6d09b8a2a4623e85d6bbdf.zip |
set brightness via powerd if it exist (using dbus)
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/ib-brightness-powerd-control.c | 166 | ||||
-rw-r--r-- | src/ib-brightness-powerd-control.h | 34 | ||||
-rw-r--r-- | src/service.c | 53 |
4 files changed, 250 insertions, 5 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index be746db..2461592 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -46,6 +46,8 @@ libindciatorpower_upower_a_LDFLAGS = $(COVERAGE_LDFLAGS) libindicatorpower_service_a_SOURCES = \ ib-brightness-control.c \ ib-brightness-control.h \ + ib-brightness-powerd-control.c \ + ib-brightness-powerd-control.h \ device-provider.c \ device-provider.h \ device.c \ diff --git a/src/ib-brightness-powerd-control.c b/src/ib-brightness-powerd-control.c new file mode 100644 index 0000000..3395bda --- /dev/null +++ b/src/ib-brightness-powerd-control.c @@ -0,0 +1,166 @@ +/* + * 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 <http://www.gnu.org/licenses/>. + * + * Authors: + * Yuan-Chen Cheng <yc.cheng@canonical.com> + */ + +#include <gudev/gudev.h> + +#include <errno.h> +#include <stdlib.h> +#include <fcntl.h> +#include <string.h> + +#include "ib-brightness-powerd-control.h" + +GDBusProxy* +powerd_get_proxy(void) +{ + GError *error = NULL; + 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_error_free (error); + g_debug ("could not connect to powerd: %s", error->message); + return NULL; + } + return powerd_proxy; +} + + +static gboolean +getBrightnessParams(GDBusProxy* powerd_proxy, 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, + -1, NULL, &error); + if (!ret) { + g_warning("getBrightnessParams failed: %s", error->message); + g_error_free(error); + return FALSE; + } + + g_variant_get(ret, "((iiib))", min, max, dflt, ab_supported); + g_variant_unref(ret); + return TRUE; +} + +static gboolean setUserBrightness(GDBusProxy* powerd_proxy, int brightness) +{ + GVariant *ret = NULL; + GError *error = NULL; + + ret = g_dbus_proxy_call_sync(powerd_proxy, + "setUserBrightness", + g_variant_new("(i)", brightness), + G_DBUS_CALL_FLAGS_NONE, + -1, NULL, &error); + if (!ret) { + g_warning("setUserBrightness failed: %s", error->message); + g_error_free(error); + return FALSE; + } else { + g_variant_unref(ret); + return TRUE; + } +} + +struct _IbBrightnessPowerdControl +{ + gboolean inited; + + GDBusProxy *powerd_proxy; + + int min; + int max; + int dflt; + gboolean ab_supported; + + int current; +}; + + +static void ib_brightness_init(IbBrightnessPowerdControl *control) +{ + gboolean ret = getBrightnessParams(control->powerd_proxy, &(control->min), + &(control->max), &(control->dflt), &(control->ab_supported)); + if (! ret) return; + + ib_brightness_powerd_control_set_value(control, control->max); + + control->inited = TRUE; +} + +IbBrightnessPowerdControl* +ib_brightness_powerd_control_new (GDBusProxy* powerd_proxy) +{ + IbBrightnessPowerdControl *control; + + control = g_new0 (IbBrightnessPowerdControl, 1); + control->inited = FALSE; + control->powerd_proxy = powerd_proxy; + + ib_brightness_init(control); + + return control; +} + +void +ib_brightness_powerd_control_set_value (IbBrightnessPowerdControl* self, gint value) +{ + gboolean ret; + if (! self->inited) return; + if (value > self->max || value < self->min) return; + ret = setUserBrightness(self->powerd_proxy, value); + if (ret) + { + self->current = value; + } +} + +gint +ib_brightness_powerd_control_get_value (IbBrightnessPowerdControl* self) +{ + if (! self->inited) return 0; + return self->current; +} + +gint +ib_brightness_powerd_control_get_max_value (IbBrightnessPowerdControl* self) +{ + if (! self->inited) return 0; + return self->max; +} + +void +ib_brightness_powerd_control_free (IbBrightnessPowerdControl *self) +{ + g_object_unref (self->powerd_proxy); + g_free (self); +} + diff --git a/src/ib-brightness-powerd-control.h b/src/ib-brightness-powerd-control.h new file mode 100644 index 0000000..77e44cb --- /dev/null +++ b/src/ib-brightness-powerd-control.h @@ -0,0 +1,34 @@ +/* + * 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 <http://www.gnu.org/licenses/>. + * + * Authors: + * Y.C Cheng <yc.cheng@canonical.com> + */ + +#ifndef __IB_BRIGHTNESS_POWERD_CONTROL_H__ +#define __IB_BRIGHTNESS_POWERD_CONTROL_H__ + +#include <gio/gio.h> +GDBusProxy* powerd_get_proxy(); + +typedef struct _IbBrightnessPowerdControl IbBrightnessPowerdControl; + +IbBrightnessPowerdControl* ib_brightness_powerd_control_new (GDBusProxy* powerd_proxy); +void ib_brightness_powerd_control_set_value (IbBrightnessPowerdControl* self, gint value); +gint ib_brightness_powerd_control_get_value (IbBrightnessPowerdControl* self); +gint ib_brightness_powerd_control_get_max_value (IbBrightnessPowerdControl* self); +void ib_brightness_powerd_control_free (IbBrightnessPowerdControl *self); + +#endif diff --git a/src/service.c b/src/service.c index 982a24e..0f976d1 100644 --- a/src/service.c +++ b/src/service.c @@ -27,6 +27,7 @@ #include "device.h" #include "device-provider.h" #include "ib-brightness-control.h" +#include "ib-brightness-powerd-control.h" #include "service.h" #define BUS_NAME "com.canonical.indicator.power" @@ -104,6 +105,7 @@ struct _IndicatorPowerServicePrivate GSettings * settings; IbBrightnessControl * brightness_control; + IbBrightnessPowerdControl * brightness_powerd_control; guint own_id; guint actions_export_id; @@ -440,7 +442,12 @@ create_phone_devices_section (IndicatorPowerService * self G_GNUC_UNUSED) static void get_brightness_range (IndicatorPowerService * self, gint * low, gint * high) { - const int max = ib_brightness_control_get_max_value (self->priv->brightness_control); + int max = 0; + if (self->priv->brightness_control) { + max = ib_brightness_control_get_max_value (self->priv->brightness_control); + } else { + max = ib_brightness_powerd_control_get_max_value (self->priv->brightness_powerd_control); + } *low = max * 0.05; /* 5% minimum -- don't let the screen go completely dark */ *high = max; } @@ -483,7 +490,15 @@ static GVariant * action_state_for_brightness (IndicatorPowerService * self) { priv_t * p = self->priv; - const gint brightness = ib_brightness_control_get_value (p->brightness_control); + gint brightness = 0; + if (p->brightness_control) + { + brightness = ib_brightness_control_get_value (p->brightness_control); + } + else if (p->brightness_powerd_control) + { + brightness = ib_brightness_powerd_control_get_value (p->brightness_powerd_control); + } return g_variant_new_double (brightness_to_percentage (self, brightness)); } @@ -502,7 +517,16 @@ on_brightness_change_requested (GSimpleAction * action G_GNUC_UNUSED, IndicatorPowerService * self = INDICATOR_POWER_SERVICE (gself); const gdouble percentage = g_variant_get_double (parameter); const int brightness = percentage_to_brightness (self, percentage); - ib_brightness_control_set_value (self->priv->brightness_control, brightness); + + if (self->priv->brightness_control) + { + ib_brightness_control_set_value (self->priv->brightness_control, brightness); + } + else if (self->priv->brightness_powerd_control) + { + ib_brightness_powerd_control_set_value (self->priv->brightness_powerd_control, brightness); + } + update_brightness_action_state (self); } @@ -999,7 +1023,14 @@ my_dispose (GObject * o) g_clear_object (&p->conn); - g_clear_pointer (&p->brightness_control, ib_brightness_control_free); + if (p->brightness_control) + { + g_clear_pointer (&p->brightness_control, ib_brightness_control_free); + } + else if (p->brightness_powerd_control) + { + g_clear_pointer (&p->brightness_powerd_control, ib_brightness_powerd_control_free); + } indicator_power_service_set_device_provider (self, NULL); @@ -1013,6 +1044,7 @@ my_dispose (GObject * o) static void indicator_power_service_init (IndicatorPowerService * self) { + GDBusProxy *powerd_proxy = NULL; priv_t * p = G_TYPE_INSTANCE_GET_PRIVATE (self, INDICATOR_TYPE_POWER_SERVICE, IndicatorPowerServicePrivate); @@ -1022,7 +1054,18 @@ indicator_power_service_init (IndicatorPowerService * self) p->settings = g_settings_new ("com.canonical.indicator.power"); - p->brightness_control = ib_brightness_control_new (); + p->brightness_control = NULL; + p->brightness_powerd_control = NULL; + + powerd_proxy = powerd_get_proxy(); + if (powerd_proxy != NULL) + { + p->brightness_powerd_control = ib_brightness_powerd_control_new(powerd_proxy); + } + else + { + p->brightness_control = ib_brightness_control_new (); + } init_gactions (self); |