Debian Codegraph @

src/config-monitor-groups.c

raw symbols SCIP
1 // SPDX-FileCopyrightText: 2022-2025 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-helpers.h"
8 #include "custom-names.h"
9 #include "port-enable.h"
10 #include "stereo-link.h"
11 #include "widget-drop-down-two-level.h"
12 #include "widget-gain.h"
13 #include "window-configuration.h"
14
15 // Entry for tracking registered callbacks so they can be removed on rebuild
16 struct monitor_group_cb_entry {
17 struct alsa_elem *elem;
18 void *data;
19 };
20
21 // Register a callback and track it for later cleanup
22 static void register_mg_callback(
23 struct alsa_card *card,
24 struct alsa_elem *elem,
25 AlsaElemCallback *callback,
26 void *data,
27 GDestroyNotify destroy
28 ) {
29 alsa_elem_add_callback(elem, callback, data, destroy);
30
31 struct monitor_group_cb_entry *entry =
32 g_malloc(sizeof(struct monitor_group_cb_entry));
33 entry->elem = elem;
34 entry->data = data;
35 card->monitor_group_cbs = g_list_prepend(
36 card->monitor_group_cbs, entry
37 );
38 }
39
40 // Remove all tracked callbacks and gain widgets (before grid rebuild)
41 static void cleanup_monitor_group_grid(struct alsa_card *card) {
42 for (GList *l = card->monitor_group_cbs; l; l = l->next) {
43 struct monitor_group_cb_entry *entry = l->data;
44 alsa_elem_remove_callbacks_by_data(entry->elem, entry->data);
45 g_free(entry);
46 }
47 g_list_free(card->monitor_group_cbs);
48 card->monitor_group_cbs = NULL;
49
50 for (GList *l = card->monitor_group_gains; l; l = l->next)
51 cleanup_gain_widget(l->data);
52 g_list_free(card->monitor_group_gains);
53 card->monitor_group_gains = NULL;
54 }
55
56 // Structure to track Main/Alt Group controls for one output
57 struct group_output_data {
58 struct alsa_elem *main_elem;
59 struct alsa_elem *alt_elem;
60 struct alsa_elem *main_source_elem;
61 struct alsa_elem *alt_source_elem;
62 struct alsa_elem *main_trim_elem;
63 struct alsa_elem *alt_trim_elem;
64 };
65
66 // Structure to track group output label and its sink
67 struct group_output_label_data {
68 GtkLabel *label;
69 struct routing_snk *snk;
70 };
71
72 // Structure to track widgets that should be enabled/disabled based on group
73 // output state
74 struct group_enable_data {
75 GtkWidget *source_widget;
76 GtkWidget *trim_widget;
77 };
78
79 // Get Main/Alt Group elements for a given output number (1-based)
80 static void get_group_output_elems(
81 struct alsa_card *card,
82 int output_num,
83 struct group_output_data *data
84 ) {
85 char name[64];
86
87 snprintf(name, sizeof(name), "Main Group Output %d Playback Switch", output_num);
88 data->main_elem = get_elem_by_name(card->elems, name);
89
90 snprintf(name, sizeof(name), "Alt Group Output %d Playback Switch", output_num);
91 data->alt_elem = get_elem_by_name(card->elems, name);
92
93 snprintf(name, sizeof(name), "Main Group Output %d Source Playback Enum", output_num);
94 data->main_source_elem = get_elem_by_name(card->elems, name);
95
96 snprintf(name, sizeof(name), "Alt Group Output %d Source Playback Enum", output_num);
97 data->alt_source_elem = get_elem_by_name(card->elems, name);
98
99 snprintf(name, sizeof(name), "Main Group Output %d Trim Playback Volume", output_num);
100 data->main_trim_elem = get_elem_by_name(card->elems, name);
101
102 snprintf(name, sizeof(name), "Alt Group Output %d Trim Playback Volume", output_num);
103 data->alt_trim_elem = get_elem_by_name(card->elems, name);
104 }
105
106 // Get the routing sink for an analogue output by number (1-based)
107 static struct routing_snk *get_analogue_output_snk(
108 struct alsa_card *card,
109 int output_num
110 ) {
111 for (int i = 0; i < card->routing_snks->len; i++) {
112 struct routing_snk *snk = &g_array_index(
113 card->routing_snks, struct routing_snk, i
114 );
115 if (snk->elem->port_category == PC_HW &&
116 snk->elem->hw_type == HW_TYPE_ANALOGUE &&
117 snk->elem->lr_num == output_num) {
118 return snk;
119 }
120 }
121 return NULL;
122 }
123
124 // Callback when enable checkbox is toggled
125 static void enable_checkbox_toggled(GtkCheckButton *button, gpointer user_data) {
126 struct alsa_elem *elem = user_data;
127 gboolean active = gtk_check_button_get_active(button);
128 alsa_set_elem_value(elem, active ? 1 : 0);
129 }
130
131 // Callback when enable element changes
132 static void enable_checkbox_updated(struct alsa_elem *elem, void *private) {
133 GtkCheckButton *button = GTK_CHECK_BUTTON(private);
134 int value = alsa_get_elem_value(elem);
135 gtk_check_button_set_active(button, value != 0);
136 }
137
138 // Callback to update source/trim sensitivity when group output enabled state
139 // changes
140 static void group_enable_updated(struct alsa_elem *elem, void *private) {
141 struct group_enable_data *data = private;
142 int enabled = alsa_get_elem_value(elem);
143
144 if (data->source_widget)
145 gtk_widget_set_sensitive(data->source_widget, enabled);
146 if (data->trim_widget)
147 gtk_widget_set_sensitive(data->trim_widget, enabled);
148 }
149
150 // Free group enable data
151 static void free_group_enable_data(void *data) {
152 g_free(data);
153 }
154
155 // Callback to update group output label when custom name changes
156 static void group_output_label_updated(struct alsa_elem *elem, void *private) {
157 struct group_output_label_data *data = private;
158 char *text = is_snk_linked(data->snk)
159 ? get_snk_pair_display_name(data->snk)
160 : get_snk_display_name_formatted(data->snk);
161 gtk_label_set_text(data->label, text);
162 g_free(text);
163 }
164
165 // Free group output label data
166 static void free_group_output_label_data(void *data) {
167 g_free(data);
168 }
169
170 // Create the Main/Alt Group grid
171 static GtkWidget *create_main_alt_group_grid(struct alsa_card *card) {
172 GtkWidget *grid = gtk_grid_new();
173 gtk_grid_set_row_spacing(GTK_GRID(grid), 6);
174 gtk_grid_set_column_spacing(GTK_GRID(grid), 10);
175 gtk_widget_set_margin_top(grid, 10);
176 gtk_widget_set_valign(grid, GTK_ALIGN_START);
177 gtk_grid_set_row_homogeneous(GTK_GRID(grid), FALSE);
178
179 // Check if source/trim controls exist (check first output)
180 struct group_output_data first_data;
181 get_group_output_elems(card, 1, &first_data);
182 int has_source = first_data.main_source_elem != NULL;
183 int has_trim = first_data.main_trim_elem != NULL;
184
185 // Column layout with separators:
186 // 0: Output label
187 // 1: Separator
188 // 2: Main checkbox
189 // 3: Main Source (if has_source)
190 // 4: Main Trim (if has_trim)
191 // 5: Separator
192 // 6: Alt checkbox
193 // 7: Alt Source (if has_source)
194 // 8: Alt Trim (if has_trim)
195 int sep1_col = 1;
196 int main_col = 2;
197 int main_source_col = has_source ? 3 : -1;
198 int main_trim_col = has_trim ? (has_source ? 4 : 3) : -1;
199 int sep2_col = 2 + 1 + (has_source ? 1 : 0) + (has_trim ? 1 : 0);
200 int alt_col = sep2_col + 1;
201 int alt_source_col = has_source ? alt_col + 1 : -1;
202 int alt_trim_col = has_trim ? alt_col + 1 + (has_source ? 1 : 0) : -1;
203
204 // Calculate column spans for Main and Alt headers
205 int main_span = 1 + (has_source ? 1 : 0) + (has_trim ? 1 : 0);
206 int alt_span = main_span;
207
208 // Row 0: Main group headers
209 GtkWidget *main_header = gtk_label_new(NULL);
210 gtk_label_set_markup(GTK_LABEL(main_header), "<b>Main</b>");
211 gtk_widget_set_halign(main_header, GTK_ALIGN_CENTER);
212 gtk_grid_attach(GTK_GRID(grid), main_header, main_col, 0, main_span, 1);
213
214 GtkWidget *alt_header = gtk_label_new(NULL);
215 gtk_label_set_markup(GTK_LABEL(alt_header), "<b>Alt</b>");
216 gtk_widget_set_halign(alt_header, GTK_ALIGN_CENTER);
217 gtk_grid_attach(GTK_GRID(grid), alt_header, alt_col, 0, alt_span, 1);
218
219 // Row 1: Sub-headings for columns
220 GtkWidget *main_enable_label = gtk_label_new("Enable");
221 gtk_widget_add_css_class(main_enable_label, "dim-label");
222 gtk_grid_attach(GTK_GRID(grid), main_enable_label, main_col, 1, 1, 1);
223
224 if (has_source) {
225 GtkWidget *main_source_label = gtk_label_new("Source");
226 gtk_widget_add_css_class(main_source_label, "dim-label");
227 gtk_grid_attach(GTK_GRID(grid), main_source_label, main_source_col, 1, 1, 1);
228 }
229
230 if (has_trim) {
231 GtkWidget *main_trim_label = gtk_label_new("Trim");
232 gtk_widget_add_css_class(main_trim_label, "dim-label");
233 gtk_grid_attach(GTK_GRID(grid), main_trim_label, main_trim_col, 1, 1, 1);
234 }
235
236 GtkWidget *alt_enable_label = gtk_label_new("Enable");
237 gtk_widget_add_css_class(alt_enable_label, "dim-label");
238 gtk_grid_attach(GTK_GRID(grid), alt_enable_label, alt_col, 1, 1, 1);
239
240 if (has_source) {
241 GtkWidget *alt_source_label = gtk_label_new("Source");
242 gtk_widget_add_css_class(alt_source_label, "dim-label");
243 gtk_grid_attach(GTK_GRID(grid), alt_source_label, alt_source_col, 1, 1, 1);
244 }
245
246 if (has_trim) {
247 GtkWidget *alt_trim_label = gtk_label_new("Trim");
248 gtk_widget_add_css_class(alt_trim_label, "dim-label");
249 gtk_grid_attach(GTK_GRID(grid), alt_trim_label, alt_trim_col, 1, 1, 1);
250 }
251
252 // Add rows for each output (starting after 2 header rows)
253 int row = 2;
254 for (int i = 1; ; i++) {
255 struct group_output_data data;
256 get_group_output_elems(card, i, &data);
257
258 // Stop when no more outputs exist
259 if (!data.main_elem && !data.alt_elem)
260 break;
261
262 // Skip hidden or right-channel-of-linked-pair sinks
263 struct routing_snk *snk = get_analogue_output_snk(card, i);
264 if (snk && (!should_display_snk(snk) ||
265 !is_routing_snk_enabled(snk)))
266 continue;
267
268 int linked = snk && is_snk_linked(snk);
269
270 // Row label — use pair name when linked, otherwise single name
271 char *label_text;
272 if (linked)
273 label_text = get_snk_pair_display_name(snk);
274 else
275 label_text = snk ? get_snk_display_name_formatted(snk) : g_strdup("");
276
277 GtkWidget *label = gtk_label_new(label_text);
278 g_free(label_text);
279 gtk_widget_set_halign(label, GTK_ALIGN_END);
280 gtk_widget_set_valign(label, GTK_ALIGN_CENTER);
281 gtk_grid_attach(GTK_GRID(grid), label, 0, row, 1, 1);
282
283 // Register callbacks to update label when name changes
284 if (snk) {
285 if (snk->custom_name_elem) {
286 struct group_output_label_data *label_data =
287 g_malloc(sizeof(struct group_output_label_data));
288 label_data->label = GTK_LABEL(label);
289 label_data->snk = snk;
290
291 register_mg_callback(
292 card, snk->custom_name_elem,
293 group_output_label_updated,
294 label_data, free_group_output_label_data
295 );
296 }
297
298 // Also update label when pair name changes (for linked pairs)
299 struct alsa_elem *pair_elem = get_snk_pair_name_elem(snk);
300 if (pair_elem) {
301 struct group_output_label_data *pair_label_data =
302 g_malloc(sizeof(struct group_output_label_data));
303 pair_label_data->label = GTK_LABEL(label);
304 pair_label_data->snk = snk;
305
306 register_mg_callback(
307 card, pair_elem,
308 group_output_label_updated,
309 pair_label_data, free_group_output_label_data
310 );
311 }
312 }
313
314 // When linked, get the right channel data for stereo trim
315 struct group_output_data data_r = {0};
316 if (linked) {
317 struct routing_snk *snk_r = get_snk_partner(snk);
318 if (snk_r)
319 get_group_output_elems(card, snk_r->elem->lr_num, &data_r);
320 }
321
322 // Main Source dropdown (create before checkbox so we can track it)
323 GtkWidget *main_source = NULL;
324 if (data.main_source_elem && main_source_col >= 0) {
325 main_source = make_monitor_group_source_dropdown(
326 data.main_source_elem, card, linked
327 );
328 gtk_widget_set_size_request(main_source, 120, -1);
329 gtk_widget_set_hexpand(main_source, FALSE);
330 gtk_widget_set_valign(main_source, GTK_ALIGN_CENTER);
331 gtk_grid_attach(
332 GTK_GRID(grid), main_source, main_source_col, row, 1, 1
333 );
334 }
335
336 // Main Trim (create before checkbox so we can track it)
337 GtkWidget *main_trim = NULL;
338 if (data.main_trim_elem && main_trim_col >= 0) {
339 if (linked && data_r.main_trim_elem) {
340 struct alsa_elem *elems[] = {
341 data.main_trim_elem, data_r.main_trim_elem
342 };
343 main_trim = make_stereo_gain_alsa_elem(
344 elems, 2, 0, WIDGET_GAIN_TAPER_LINEAR, 1, FALSE
345 );
346 } else {
347 main_trim = make_gain_alsa_elem(
348 data.main_trim_elem,
349 0, WIDGET_GAIN_TAPER_LINEAR, 1, FALSE
350 );
351 }
352 gtk_widget_set_size_request(main_trim, 40, 80);
353 gtk_widget_set_hexpand(main_trim, FALSE);
354 gtk_widget_set_valign(main_trim, GTK_ALIGN_CENTER);
355 gtk_grid_attach(
356 GTK_GRID(grid), main_trim, main_trim_col, row, 1, 1
357 );
358 card->monitor_group_gains = g_list_prepend(
359 card->monitor_group_gains, main_trim
360 );
361 }
362
363 // Main checkbox
364 if (data.main_elem) {
365 GtkWidget *main_check = gtk_check_button_new();
366 gtk_widget_set_halign(main_check, GTK_ALIGN_CENTER);
367 gtk_widget_set_valign(main_check, GTK_ALIGN_CENTER);
368
369 g_signal_connect(
370 main_check,
371 "toggled",
372 G_CALLBACK(enable_checkbox_toggled),
373 data.main_elem
374 );
375
376 register_mg_callback(
377 card, data.main_elem,
378 enable_checkbox_updated, main_check, NULL
379 );
380
381 int value = alsa_get_elem_value(data.main_elem);
382 gtk_check_button_set_active(
383 GTK_CHECK_BUTTON(main_check), value != 0
384 );
385
386 gtk_grid_attach(
387 GTK_GRID(grid), main_check, main_col, row, 1, 1
388 );
389
390 // Register callback to enable/disable source and trim
391 if (main_source || main_trim) {
392 struct group_enable_data *enable_data =
393 g_malloc(sizeof(struct group_enable_data));
394 enable_data->source_widget = main_source;
395 enable_data->trim_widget = main_trim;
396
397 register_mg_callback(
398 card, data.main_elem,
399 group_enable_updated,
400 enable_data, free_group_enable_data
401 );
402
403 if (main_source)
404 gtk_widget_set_sensitive(main_source, value != 0);
405 if (main_trim)
406 gtk_widget_set_sensitive(main_trim, value != 0);
407 }
408 }
409
410 // Alt Source dropdown (create before checkbox so we can track it)
411 GtkWidget *alt_source = NULL;
412 if (data.alt_source_elem && alt_source_col >= 0) {
413 alt_source = make_monitor_group_source_dropdown(
414 data.alt_source_elem, card, linked
415 );
416 gtk_widget_set_size_request(alt_source, 120, -1);
417 gtk_widget_set_hexpand(alt_source, FALSE);
418 gtk_widget_set_valign(alt_source, GTK_ALIGN_CENTER);
419 gtk_grid_attach(
420 GTK_GRID(grid), alt_source, alt_source_col, row, 1, 1
421 );
422 }
423
424 // Alt Trim (create before checkbox so we can track it)
425 GtkWidget *alt_trim = NULL;
426 if (data.alt_trim_elem && alt_trim_col >= 0) {
427 if (linked && data_r.alt_trim_elem) {
428 struct alsa_elem *elems[] = {
429 data.alt_trim_elem, data_r.alt_trim_elem
430 };
431 alt_trim = make_stereo_gain_alsa_elem(
432 elems, 2, 0, WIDGET_GAIN_TAPER_LINEAR, 1, FALSE
433 );
434 } else {
435 alt_trim = make_gain_alsa_elem(
436 data.alt_trim_elem,
437 0, WIDGET_GAIN_TAPER_LINEAR, 1, FALSE
438 );
439 }
440 gtk_widget_set_size_request(alt_trim, 40, 80);
441 gtk_widget_set_hexpand(alt_trim, FALSE);
442 gtk_widget_set_valign(alt_trim, GTK_ALIGN_CENTER);
443 gtk_grid_attach(
444 GTK_GRID(grid), alt_trim, alt_trim_col, row, 1, 1
445 );
446 card->monitor_group_gains = g_list_prepend(
447 card->monitor_group_gains, alt_trim
448 );
449 }
450
451 // Alt checkbox
452 if (data.alt_elem) {
453 GtkWidget *alt_check = gtk_check_button_new();
454 gtk_widget_set_halign(alt_check, GTK_ALIGN_CENTER);
455 gtk_widget_set_valign(alt_check, GTK_ALIGN_CENTER);
456
457 g_signal_connect(
458 alt_check,
459 "toggled",
460 G_CALLBACK(enable_checkbox_toggled),
461 data.alt_elem
462 );
463
464 register_mg_callback(
465 card, data.alt_elem,
466 enable_checkbox_updated, alt_check, NULL
467 );
468
469 int value = alsa_get_elem_value(data.alt_elem);
470 gtk_check_button_set_active(
471 GTK_CHECK_BUTTON(alt_check), value != 0
472 );
473
474 gtk_grid_attach(
475 GTK_GRID(grid), alt_check, alt_col, row, 1, 1
476 );
477
478 // Register callback to enable/disable source and trim
479 if (alt_source || alt_trim) {
480 struct group_enable_data *enable_data =
481 g_malloc(sizeof(struct group_enable_data));
482 enable_data->source_widget = alt_source;
483 enable_data->trim_widget = alt_trim;
484
485 register_mg_callback(
486 card, data.alt_elem,
487 group_enable_updated,
488 enable_data, free_group_enable_data
489 );
490
491 if (alt_source)
492 gtk_widget_set_sensitive(alt_source, value != 0);
493 if (alt_trim)
494 gtk_widget_set_sensitive(alt_trim, value != 0);
495 }
496 }
497
498 row++;
499 }
500
501 // Add vertical separators spanning all data rows
502 int total_rows = row;
503
504 GtkWidget *sep1 = gtk_separator_new(GTK_ORIENTATION_VERTICAL);
505 gtk_widget_set_margin_start(sep1, 10);
506 gtk_widget_set_margin_end(sep1, 10);
507 gtk_grid_attach(GTK_GRID(grid), sep1, sep1_col, 0, 1, total_rows);
508
509 GtkWidget *sep2 = gtk_separator_new(GTK_ORIENTATION_VERTICAL);
510 gtk_widget_set_margin_start(sep2, 10);
511 gtk_widget_set_margin_end(sep2, 10);
512 gtk_grid_attach(GTK_GRID(grid), sep2, sep2_col, 0, 1, total_rows);
513
514 return grid;
515 }
516
517 void add_monitor_groups_tab(GtkWidget *notebook, struct alsa_card *card) {
518 if (!get_elem_by_prefix(card->elems, "Main Group Output"))
519 return;
520
521 GtkWidget *content = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
522 gtk_widget_set_margin_start(content, 20);
523 gtk_widget_set_margin_end(content, 20);
524 gtk_widget_set_margin_top(content, 20);
525 gtk_widget_set_margin_bottom(content, 20);
526
527 gtk_box_append(GTK_BOX(content), config_help_label(
528 "Monitor groups let you organise your outputs into Main and "
529 "Alt sets.\n"
530 "Switch between them using the Alt button on your interface "
531 "or the toggle in the main window.\n"
532 "The main volume knob controls the enabled group; use Trim "
533 "for per-output level calibration.\n\n"
534 "Examples:\n"
535 " \xe2\x80\xa2 Connect two sets of speakers for A/B reference "
536 "mixing\n"
537 " \xe2\x80\xa2 Create a surround setup with all speakers in one "
538 "group\n"
539 " \xe2\x80\xa2 Quick DAW/Direct monitoring switch by assigning "
540 "same output to both groups with different sources"
541 ));
542
543 GtkWidget *grid = create_main_alt_group_grid(card);
544 gtk_box_append(GTK_BOX(content), grid);
545 card->monitor_groups_grid = grid;
546
547 GtkWidget *scrolled = wrap_tab_content_scrolled(content);
548 g_object_set_data(
549 G_OBJECT(scrolled), PAGE_ID_KEY, (gpointer)"monitor-groups"
550 );
551 gtk_notebook_append_page(
552 GTK_NOTEBOOK(notebook), scrolled,
553 gtk_label_new("Monitor Groups")
554 );
555 }
556
557 void rebuild_monitor_groups_grid(struct alsa_card *card) {
558 if (!card || !card->monitor_groups_grid)
559 return;
560
561 GtkWidget *parent = gtk_widget_get_parent(card->monitor_groups_grid);
562 if (!parent)
563 return;
564
565 // Clean up callbacks and gain widgets from old grid
566 cleanup_monitor_group_grid(card);
567
568 // Remove old grid from parent
569 gtk_box_remove(GTK_BOX(parent), card->monitor_groups_grid);
570
571 // Create new grid with current stereo state
572 GtkWidget *grid = create_main_alt_group_grid(card);
573 gtk_box_append(GTK_BOX(parent), grid);
574 card->monitor_groups_grid = grid;
575 }