Debian Codegraph @

src/config-front-panel.c

raw symbols SCIP
1 // SPDX-FileCopyrightText: 2026 Geoffrey D. Bennett <[email protected]>
2 // SPDX-License-Identifier: GPL-3.0-or-later
3
4 #include <gtk/gtk.h>
5
6 #include "alsa.h"
7 #include "config-front-panel.h"
8 #include "config-helpers.h"
9 #include "widget-drop-down.h"
10 #include "window-configuration.h"
11
12 struct sleep_preset {
13 const char *label;
14 int seconds;
15 };
16
17 // Preset values for the Front Panel Sleep Time control.
18 // 0 = never sleep; remaining values are common timeouts up to the
19 // 86400-second maximum (24 hours).
20 static const struct sleep_preset sleep_presets[] = {
21 { "Off (never sleep)", 0 },
22 { "30 seconds", 30 },
23 { "1 minute", 60 },
24 { "5 minutes", 300 },
25 { "10 minutes", 600 },
26 { "30 minutes", 1800 },
27 { "1 hour", 3600 },
28 { "2 hours", 7200 },
29 { "4 hours", 14400 },
30 { "8 hours", 28800 },
31 { "24 hours", 86400 },
32 };
33
34 #define SLEEP_PRESET_COUNT G_N_ELEMENTS(sleep_presets)
35
36 static int find_preset_index(int seconds) {
37 for (guint i = 0; i < SLEEP_PRESET_COUNT; i++)
38 if (sleep_presets[i].seconds == seconds)
39 return (int)i;
40 return -1;
41 }
42
43 static int sleep_value_to_row(int value, void *user_data) {
44 int idx = find_preset_index(value);
45 if (idx >= 0)
46 return idx;
47
48 // Custom row, when present, is always the last entry.
49 return SLEEP_PRESET_COUNT;
50 }
51
52 static int sleep_row_to_value(int row, int current_value, void *user_data) {
53 if (row < (int)SLEEP_PRESET_COUNT)
54 return sleep_presets[row].seconds;
55
56 // Click on the "Custom (N s)" row: no-op.
57 return current_value;
58 }
59
60 static char *sleep_button_label(int value, void *user_data) {
61 int idx = find_preset_index(value);
62 if (idx >= 0)
63 return g_strdup(sleep_presets[idx].label);
64 return g_strdup_printf("Custom (%d s)", value);
65 }
66
67 // Keep the model in sync with the live value: a synthetic
68 // "Custom (N s)" row is appended whenever the value isn't a preset,
69 // and removed once it is.
70 static void sleep_refresh_model(
71 GtkStringList *model,
72 int value,
73 void *user_data
74 ) {
75 guint n = g_list_model_get_n_items(G_LIST_MODEL(model));
76 int has_custom = (n > SLEEP_PRESET_COUNT);
77 int is_preset = find_preset_index(value) >= 0;
78
79 if (!is_preset) {
80 char *s = g_strdup_printf("Custom (%d s)", value);
81 const char *additions[] = { s, NULL };
82 if (has_custom)
83 gtk_string_list_splice(
84 model, SLEEP_PRESET_COUNT, 1, additions
85 );
86 else
87 gtk_string_list_append(model, s);
88 g_free(s);
89 } else if (has_custom) {
90 gtk_string_list_remove(model, SLEEP_PRESET_COUNT);
91 }
92 }
93
94 static const struct drop_down_value_ops sleep_ops = {
95 .value_to_row = sleep_value_to_row,
96 .row_to_value = sleep_row_to_value,
97 .button_label = sleep_button_label,
98 .refresh_model = sleep_refresh_model,
99 };
100
101 static GtkWidget *make_sleep_time_dropdown(struct alsa_elem *elem) {
102 GtkStringList *model = gtk_string_list_new(NULL);
103 for (guint i = 0; i < SLEEP_PRESET_COUNT; i++)
104 gtk_string_list_append(model, sleep_presets[i].label);
105
106 return make_value_mapped_drop_down_alsa_elem(
107 elem, model, &sleep_ops, NULL, NULL
108 );
109 }
110
111 // Layout for one control: heading and control sitting next to each
112 // other on top, description on its own line below.
113 static void add_control_row(
114 GtkBox *box,
115 const char *heading,
116 GtkWidget *control,
117 const char *description
118 ) {
119 GtkWidget *header_row = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
120
121 gtk_widget_set_halign(control, GTK_ALIGN_START);
122 gtk_widget_set_valign(control, GTK_ALIGN_CENTER);
123
124 gtk_box_append(GTK_BOX(header_row), config_bold_label(heading));
125 gtk_box_append(GTK_BOX(header_row), control);
126
127 gtk_box_append(box, header_row);
128 gtk_box_append(box, config_wrapped_help_label(description, 60));
129 }
130
131 void add_front_panel_tab(GtkWidget *notebook, struct alsa_card *card) {
132 struct alsa_elem *brightness = get_elem_by_name(
133 card->elems, "Front Panel Brightness"
134 );
135 struct alsa_elem *sleep_time = get_elem_by_name(
136 card->elems, "Front Panel Sleep Time"
137 );
138
139 if (!brightness && !sleep_time)
140 return;
141
142 GtkWidget *content = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
143 gtk_widget_set_margin_top(content, 20);
144 gtk_widget_set_margin_start(content, 20);
145 gtk_widget_set_margin_end(content, 20);
146 gtk_widget_set_margin_bottom(content, 20);
147
148 if (brightness) {
149 add_control_row(
150 GTK_BOX(content),
151 "Brightness",
152 make_drop_down_alsa_elem(brightness, NULL),
153 "Sets the brightness of the front panel LEDs."
154 );
155 }
156
157 if (sleep_time) {
158 if (brightness)
159 config_append_separator(GTK_BOX(content));
160
161 add_control_row(
162 GTK_BOX(content),
163 "Sleep Time",
164 make_sleep_time_dropdown(sleep_time),
165 "After this much inactivity (no front panel adjustments or "
166 "passing audio), the front panel LEDs turn off. Select Off to "
167 "keep them lit indefinitely."
168 );
169 }
170
171 g_object_set_data(G_OBJECT(content), PAGE_ID_KEY, (gpointer)"front-panel");
172 gtk_notebook_append_page(
173 GTK_NOTEBOOK(notebook), content, gtk_label_new("Front Panel")
174 );
175 }