Commit 86dede8c authored by Guido Gunther's avatar Guido Gunther

Merge branch 'fix-brightness' into 'master'

settings: Make brightness slider work correctly

See merge request Librem5/phosh!90
parents 569a6e6a aab40bcf
......@@ -27,9 +27,6 @@ typedef struct
GtkWidget *scale_volume;
GtkWidget *btn_rotation;
GtkAdjustment *adj_brightness;
GtkAdjustment *adj_volume;
GtkWidget *btn_settings;
GDesktopAppInfo *settings_info;
GtkWidget *btn_airplane_mode;
......@@ -54,11 +51,11 @@ GtkWidget *phosh_settings (const char* name)
static void
brightness_changed_cb (GtkAdjustment *adj_brightness, gpointer *unused)
brightness_value_changed_cb (GtkScale *scale_brightness, gpointer *unused)
{
int brightness;
brightness = (int)gtk_adjustment_get_value (adj_brightness);
brightness = (int)gtk_range_get_value (GTK_RANGE (scale_brightness));
brightness_set (brightness);
}
......@@ -113,18 +110,16 @@ phosh_settings_constructed (GObject *object)
gtk_window_resize (GTK_WINDOW (self), 100, 100);
gtk_widget_realize(GTK_WIDGET (self));
priv->adj_brightness = gtk_adjustment_new (0, 0, 100, 1, 10, 10);
gtk_range_set_adjustment (GTK_RANGE (priv->scale_brightness), priv->adj_brightness);
gtk_range_set_range (GTK_RANGE (priv->scale_brightness), 0, 100);
gtk_range_set_round_digits (GTK_RANGE (priv->scale_brightness), 0);
brightness_init (priv->adj_brightness);
g_signal_connect (priv->adj_brightness,
gtk_range_set_increments (GTK_RANGE (priv->scale_brightness), 1, 10);
brightness_init (priv->scale_brightness);
g_signal_connect (priv->scale_brightness,
"value-changed",
G_CALLBACK(brightness_changed_cb),
G_CALLBACK(brightness_value_changed_cb),
NULL);
priv->adj_volume = gtk_adjustment_new (0, 0, 100, 1, 10, 10);
gtk_range_set_adjustment (GTK_RANGE (priv->scale_volume), priv->adj_volume);
gtk_range_set_range (GTK_RANGE (priv->scale_volume), 0, 100);
if (phosh_shell_get_rotation (phosh_shell_get_default ()))
gtk_switch_set_active (GTK_SWITCH (priv->btn_rotation), TRUE);
......@@ -161,12 +156,24 @@ phosh_settings_constructed (GObject *object)
}
static void
phosh_settings_dispose (GObject *object)
{
brightness_dispose ();
G_OBJECT_CLASS (phosh_settings_parent_class)->dispose (object);
}
static void
phosh_settings_class_init (PhoshSettingsClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
object_class->dispose = phosh_settings_dispose;
gtk_widget_class_set_template_from_resource (widget_class,
"/sm/puri/phosh/ui/settings-menu.ui");
......
......@@ -11,32 +11,32 @@
#include <gtk/gtk.h>
GDBusProxy *brightness_proxy;
gboolean setting_brightness;
void
brightness_changed_cb (GDBusProxy *proxy,
GVariant *changed_props,
GVariant *invalidated_props,
GtkAdjustment *adj)
gpointer *user_data)
{
GtkScale *scale = GTK_SCALE (user_data);
gint value;
gboolean ret;
gint value, cur;
setting_brightness = TRUE;
ret = g_variant_lookup (changed_props,
"Brightness",
"i", &value);
/* The value returned from DBUs is one lower
* than the value we set */
cur = gtk_adjustment_get_value (adj);
if (ret && cur != value + 1) {
gtk_adjustment_set_value (adj, value + 1);
}
g_return_if_fail (ret);
if (value < 0 || value > 100)
value = 100.0;
gtk_range_set_value (GTK_RANGE (scale), value);
}
void
brightness_init (GtkAdjustment *adj)
brightness_init (GtkScale *scale)
{
GError *err = NULL;
GDBusConnection *session_con;
......@@ -65,18 +65,20 @@ brightness_init (GtkAdjustment *adj)
return;
}
/* Set adjustment to current brightness */
/* Set scale to current brightness */
var = g_dbus_proxy_get_cached_property (proxy, "Brightness");
if (var) {
g_variant_get (var, "i", &value);
gtk_adjustment_set_value (adj, value);
setting_brightness = TRUE;
gtk_range_set_value (GTK_RANGE (scale), value);
setting_brightness = FALSE;
g_variant_unref (var);
}
g_signal_connect (proxy,
"g-properties-changed",
G_CALLBACK(brightness_changed_cb),
adj);
scale);
brightness_proxy = proxy;
}
......@@ -99,18 +101,21 @@ brightness_set_cb (GDBusProxy *proxy, GAsyncResult *res, gpointer unused)
if (var)
g_variant_unref (var);
setting_brightness = FALSE;
}
void
brightness_set (int brightness)
{
g_return_if_fail (brightness_proxy);
if (!brightness_proxy)
return;
/* Don't let the display go completely dark for now */
if (brightness < 10)
brightness = 10;
if (setting_brightness)
return;
setting_brightness = TRUE;
g_dbus_proxy_call (brightness_proxy,
"org.freedesktop.DBus.Properties.Set",
g_variant_new (
......@@ -124,3 +129,10 @@ brightness_set (int brightness)
(GAsyncReadyCallback)brightness_set_cb,
NULL);
}
void
brightness_dispose ()
{
g_clear_pointer (&brightness_proxy, g_object_unref);
}
......@@ -6,4 +6,5 @@
*/
void brightness_init ();
void brightness_dispose ();
void brightness_set (int brightness);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment