Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions install_files/com.sidevesh.Luminance.gschema.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,25 @@
<summary>Whether to link display brightness adjustments</summary>
<description>When true, changes to the brightness of one display will also affect the brightness of other displays connected to the same computer.</description>
</key>
<key name="is-brightness-proportionally-linked" type="b">
<default>false</default>
<summary>Whether to proportionally link display brightness adjustments</summary>
<description>When true, changes to the brightness of one display will proportionally affect other displays based on each display's min/max range. Mutually exclusive with is-brightness-linked.</description>
</key>
<key name="should-hide-internal-if-lid-closed" type="b">
<default>false</default>
<summary>Whether to hide the internal display when the lid is closed</summary>
<description>When true, the internal display will be hidden when the lid is closed. This is useful for laptops that are connected to external displays.</description>
</key>
<key name="max-brightness-per-display" type="a{si}">
<default>{}</default>
<summary>Maximum brightness percentage per display</summary>
<description>A dictionary mapping display names to their maximum brightness percentage (0-100). When set, the brightness slider for that display will be capped at the specified value instead of 100%.</description>
</key>
<key name="min-brightness-per-display" type="a{si}">
<default>{}</default>
<summary>Minimum brightness percentage per display</summary>
<description>A dictionary mapping display names to their minimum brightness percentage (0-100). When set, the brightness slider for that display will start at the specified value instead of 0%.</description>
</key>
</schema>
</schemalist>
12 changes: 12 additions & 0 deletions src/constants/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@
#define IS_BRIGHTNESS_LINKED_GSETTINGS_KEY "is-brightness-linked"
#endif

#ifndef IS_BRIGHTNESS_PROPORTIONALLY_LINKED_GSETTINGS_KEY
#define IS_BRIGHTNESS_PROPORTIONALLY_LINKED_GSETTINGS_KEY "is-brightness-proportionally-linked"
#endif

#ifndef SHOULD_HIDE_INTERNAL_IF_LID_CLOSED_GSETTINGS_KEY
#define SHOULD_HIDE_INTERNAL_IF_LID_CLOSED_GSETTINGS_KEY "should-hide-internal-if-lid-closed"
#endif

#ifndef MAX_BRIGHTNESS_PER_DISPLAY_GSETTINGS_KEY
#define MAX_BRIGHTNESS_PER_DISPLAY_GSETTINGS_KEY "max-brightness-per-display"
#endif

#ifndef MIN_BRIGHTNESS_PER_DISPLAY_GSETTINGS_KEY
#define MIN_BRIGHTNESS_PER_DISPLAY_GSETTINGS_KEY "min-brightness-per-display"
#endif
93 changes: 93 additions & 0 deletions src/states/brightness_range.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include <gtk/gtk.h>

#include "../constants/main.c"

#ifndef BRIGHTNESS_RANGE_STATE
#define BRIGHTNESS_RANGE_STATE

// --- Generic helper for per-display brightness limits (min or max) ---

static GVariant* _get_brightness_dict(const gchar *gsettings_key) {
GSettings *settings = g_settings_new(APP_INFO_PACKAGE_NAME);
GVariant *dict = g_settings_get_value(settings, gsettings_key);
g_object_unref(settings);
return dict;
}

static void _set_brightness_dict(const gchar *gsettings_key, const gchar *display_name, gint percentage) {
GSettings *settings = g_settings_new(APP_INFO_PACKAGE_NAME);
GVariant *dict = g_settings_get_value(settings, gsettings_key);

GVariantBuilder builder;
g_variant_builder_init(&builder, G_VARIANT_TYPE("a{si}"));

GVariantIter iter;
gchar *key;
gint32 val;
g_variant_iter_init(&iter, dict);
while (g_variant_iter_loop(&iter, "{&si}", &key, &val)) {
if (g_strcmp0(key, display_name) != 0) {
g_variant_builder_add(&builder, "{&si}", key, val);
}
}

g_variant_builder_add(&builder, "{&si}", display_name, percentage);

GVariant *new_dict = g_variant_builder_end(&builder);
g_settings_set_value(settings, gsettings_key, new_dict);

g_variant_unref(dict);
g_object_unref(settings);
}

static gdouble _get_brightness_from_dict(GVariant *dict, const gchar *display_name, gdouble default_value) {
gint32 int_val = 0;
if (g_variant_lookup(dict, display_name, "i", &int_val)) {
if (int_val >= 0 && int_val <= 100) {
return (gdouble)int_val;
}
}
return default_value;
}

// --- Max brightness ---

GVariant *_max_brightness_cache = NULL;

gdouble get_max_brightness_percentage(const gchar *display_name) {
if (_max_brightness_cache == NULL) {
_max_brightness_cache = _get_brightness_dict(MAX_BRIGHTNESS_PER_DISPLAY_GSETTINGS_KEY);
}
return _get_brightness_from_dict(_max_brightness_cache, display_name, 100.0);
}

void set_max_brightness_percentage(const gchar *display_name, gint percentage) {
if (_max_brightness_cache != NULL) {
g_variant_unref(_max_brightness_cache);
_max_brightness_cache = NULL;
}
_set_brightness_dict(MAX_BRIGHTNESS_PER_DISPLAY_GSETTINGS_KEY, display_name, percentage);
_max_brightness_cache = _get_brightness_dict(MAX_BRIGHTNESS_PER_DISPLAY_GSETTINGS_KEY);
}

// --- Min brightness ---

GVariant *_min_brightness_cache = NULL;

gdouble get_min_brightness_percentage(const gchar *display_name) {
if (_min_brightness_cache == NULL) {
_min_brightness_cache = _get_brightness_dict(MIN_BRIGHTNESS_PER_DISPLAY_GSETTINGS_KEY);
}
return _get_brightness_from_dict(_min_brightness_cache, display_name, 0.0);
}

void set_min_brightness_percentage(const gchar *display_name, gint percentage) {
if (_min_brightness_cache != NULL) {
g_variant_unref(_min_brightness_cache);
_min_brightness_cache = NULL;
}
_set_brightness_dict(MIN_BRIGHTNESS_PER_DISPLAY_GSETTINGS_KEY, display_name, percentage);
_min_brightness_cache = _get_brightness_dict(MIN_BRIGHTNESS_PER_DISPLAY_GSETTINGS_KEY);
}

#endif
14 changes: 14 additions & 0 deletions src/states/is_brightness_linked.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,18 @@ gboolean get_is_brightness_linked() {
return value;
}

void set_is_brightness_proportionally_linked(gboolean value) {
GSettings *settings = g_settings_new(APP_INFO_PACKAGE_NAME);
g_settings_set_boolean(settings, IS_BRIGHTNESS_PROPORTIONALLY_LINKED_GSETTINGS_KEY, value);
g_object_unref(settings);
}

gboolean get_is_brightness_proportionally_linked() {
GSettings *settings = g_settings_new(APP_INFO_PACKAGE_NAME);
gboolean value = g_settings_get_boolean(settings, IS_BRIGHTNESS_PROPORTIONALLY_LINKED_GSETTINGS_KEY);
g_object_unref(settings);

return value;
}

#endif
74 changes: 74 additions & 0 deletions src/states/max_brightness.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <gtk/gtk.h>

#include "../constants/main.c"

#ifndef MAX_BRIGHTNESS_STATE
#define MAX_BRIGHTNESS_STATE

// Cache the GVariant dict so we don't re-read on every slider movement
GVariant *_max_brightness_cache = NULL;

static GVariant* _get_max_brightness_dict() {
GSettings *settings = g_settings_new(APP_INFO_PACKAGE_NAME);
GVariant *dict = g_settings_get_value(settings, MAX_BRIGHTNESS_PER_DISPLAY_GSETTINGS_KEY);
g_object_unref(settings);
return dict;
}

static void _invalidate_cache() {
if (_max_brightness_cache != NULL) {
g_variant_unref(_max_brightness_cache);
_max_brightness_cache = NULL;
}
}

gdouble get_max_brightness_percentage(const gchar *display_name) {
if (_max_brightness_cache == NULL) {
_max_brightness_cache = _get_max_brightness_dict();
}

gdouble value = 100.0;

gint32 int_val = 0;
if (g_variant_lookup(_max_brightness_cache, display_name, "i", &int_val)) {
if (int_val > 0 && int_val <= 100) {
value = (gdouble)int_val;
}
}

return value;
}

void set_max_brightness_percentage(const gchar *display_name, gint percentage) {
_invalidate_cache();

GSettings *settings = g_settings_new(APP_INFO_PACKAGE_NAME);
GVariant *dict = g_settings_get_value(settings, MAX_BRIGHTNESS_PER_DISPLAY_GSETTINGS_KEY);

GVariantBuilder builder;
g_variant_builder_init(&builder, G_VARIANT_TYPE("a{si}"));

// Copy existing entries, skipping the one we're updating
GVariantIter iter;
gchar *key;
gint32 val;
g_variant_iter_init(&iter, dict);
while (g_variant_iter_loop(&iter, "{&si}", &key, &val)) {
if (g_strcmp0(key, display_name) != 0) {
g_variant_builder_add(&builder, "{&si}", key, val);
}
}

// Add the new/updated entry
g_variant_builder_add(&builder, "{&si}", display_name, percentage);

GVariant *new_dict = g_variant_builder_end(&builder);
g_settings_set_value(settings, MAX_BRIGHTNESS_PER_DISPLAY_GSETTINGS_KEY, new_dict);

g_variant_unref(dict);
g_object_unref(settings);

_max_brightness_cache = _get_max_brightness_dict();
}

#endif
6 changes: 3 additions & 3 deletions src/ui/components/display_brightness_scale.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
static char* format_brightness_value(GtkScale *scale, gdouble value, gpointer data) {
(void)scale;
(void)data;
return g_strdup_printf(" %d%%", (int)value);
return g_strdup_printf("%3d%%", (int)value);
}

GtkWidget* get_display_brightness_scale(gdouble last_value, gdouble max_value) {
GtkWidget *scale = gtk_scale_new_with_range(GTK_ORIENTATION_HORIZONTAL, 0, max_value, 1);
GtkWidget* get_display_brightness_scale(gdouble last_value, gdouble min_value, gdouble max_value) {
GtkWidget *scale = gtk_scale_new_with_range(GTK_ORIENTATION_HORIZONTAL, min_value, max_value, 1);

gtk_scale_set_draw_value(GTK_SCALE(scale), TRUE);
gtk_scale_set_value_pos(GTK_SCALE(scale), GTK_POS_RIGHT);
Expand Down
2 changes: 1 addition & 1 deletion src/ui/components/link_brightness_check_button.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "../constants/main.c"

GtkWidget* get_link_brightness_checkbox(gboolean initial_value) {
GtkWidget *link_brightness_checkbox = gtk_check_button_new_with_label("Sync brightness of all displays");
GtkWidget *link_brightness_checkbox = gtk_check_button_new_with_label("Sync brightness: same value");
gtk_check_button_set_active(GTK_CHECK_BUTTON(link_brightness_checkbox), initial_value);

gtk_widget_set_margin_start(link_brightness_checkbox, MARGIN_UNIT);
Expand Down
Loading