Debian Codegraph @

src/port-enable.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 <alsa/asoundlib.h>
5 #include <string.h>
6
7 #include "port-enable.h"
8 #include "config-io.h"
9 #include "config-monitor-groups.h"
10 #include "optional-state.h"
11 #include "alsa.h"
12 #include "config-monitor-groups.h"
13 #include "stereo-link.h"
14 #include "window-mixer.h"
15 #include "window-routing.h"
16
17 // Callback structure to pass data to save callback
18 struct port_enable_save_data {
19 struct alsa_card *card;
20 char *config_key;
21 };
22
23 // Callback when a port enable value changes
24 // Saves the new value to the state file
25 static void port_enable_changed(
26 struct alsa_elem *elem,
27 void *private
28 ) {
29 struct port_enable_save_data *data = private;
30
31 // get the boolean value
32 long value = alsa_get_elem_value(elem);
33
34 // save as "1" or "0"
35 optional_state_save(
36 data->card, CONFIG_SECTION_CONTROLS, data->config_key, value ? "1" : "0"
37 );
38 }
39
40 // Flush pending UI updates for a card
41 static gboolean flush_pending_ui_updates(gpointer user_data) {
42 struct alsa_card *card = user_data;
43
44 card->pending_ui_update_idle = FALSE;
45
46 if (card->pending_ui_updates & PENDING_UI_UPDATE_MIXER_GRID) {
47 update_mixer_labels(card);
48 update_config_io_mixer_labels(card);
49 rebuild_mixer_grid(card);
50 }
51
52 if (card->pending_ui_updates & PENDING_UI_UPDATE_MONITOR_GROUPS)
53 rebuild_monitor_groups_grid(card);
54
55 card->pending_ui_updates = 0;
56
57 return G_SOURCE_REMOVE;
58 }
59
60 // Schedule a UI update for a card (runs at idle)
61 void schedule_ui_update(struct alsa_card *card, int flags) {
62 if (!card)
63 return;
64
65 card->pending_ui_updates |= flags;
66
67 if (!card->pending_ui_update_idle) {
68 card->pending_ui_update_idle = TRUE;
69 g_idle_add_full(
70 G_PRIORITY_HIGH_IDLE, flush_pending_ui_updates, card, NULL
71 );
72 }
73 }
74
75 // Update the Sources label arrows based on which sections are visible
76 static void update_sources_label(struct alsa_card *card) {
77 if (!card || !card->routing_src_label)
78 return;
79
80 // Check if we have any enabled sources in each category
81 int has_hw_or_pcm_sources =
82 !all_sources_disabled(card, PC_HW) ||
83 !all_sources_disabled(card, PC_PCM);
84 int has_mixer_or_dsp_sources =
85 !all_sources_disabled(card, PC_MIX) ||
86 !all_sources_disabled(card, PC_DSP);
87
88 // Hide the label if there are no arrows (no sources enabled)
89 if (!has_hw_or_pcm_sources && !has_mixer_or_dsp_sources) {
90 gtk_widget_set_visible(card->routing_src_label, FALSE);
91 return;
92 }
93
94 // Show the label and set text based on what's visible
95 gtk_widget_set_visible(card->routing_src_label, TRUE);
96
97 char *text;
98 if (has_hw_or_pcm_sources && has_mixer_or_dsp_sources) {
99 text = "↑\nSources →";
100 } else if (has_hw_or_pcm_sources) {
101 text = "↑\nSources";
102 } else {
103 text = "Sources →";
104 }
105
106 gtk_label_set_text(GTK_LABEL(card->routing_src_label), text);
107 }
108
109 // Update the Sinks label arrows based on which sections are visible
110 static void update_sinks_label(struct alsa_card *card) {
111 if (!card || !card->routing_snk_label)
112 return;
113
114 // Check if we have any enabled sinks in each category
115 int has_hw_or_pcm_sinks =
116 !all_sinks_disabled(card, PC_HW) ||
117 !all_sinks_disabled(card, PC_PCM);
118
119 // Don't count mixer sinks if they're fixed (not configurable)
120 int has_mixer_sinks = card->has_fixed_mixer_inputs ? 0 : !all_sinks_disabled(card, PC_MIX);
121 int has_mixer_or_dsp_sinks =
122 has_mixer_sinks ||
123 !all_sinks_disabled(card, PC_DSP);
124
125 // Hide the label if there are no arrows (no sinks enabled)
126 if (!has_hw_or_pcm_sinks && !has_mixer_or_dsp_sinks) {
127 gtk_widget_set_visible(card->routing_snk_label, FALSE);
128 return;
129 }
130
131 // Show the label and set text based on what's visible
132 gtk_widget_set_visible(card->routing_snk_label, TRUE);
133
134 char *text;
135 if (has_hw_or_pcm_sinks && has_mixer_or_dsp_sinks) {
136 text = "← Sinks\n↓";
137 } else if (has_hw_or_pcm_sinks) {
138 text = "Sinks\n↓";
139 } else {
140 text = "← Sinks";
141 }
142
143 gtk_label_set_text(GTK_LABEL(card->routing_snk_label), text);
144 }
145
146 // Helper to set visibility on a grid's parent container
147 static void set_grid_container_visible(GtkWidget *grid, int visible) {
148 if (!grid)
149 return;
150 GtkWidget *container = gtk_widget_get_parent(grid);
151 if (container)
152 gtk_widget_set_visible(container, visible);
153 }
154
155 // Update visibility of routing section grids based on port enable states
156 void update_routing_section_visibility(struct alsa_card *card) {
157 if (!card)
158 return;
159
160 // Hardware Inputs
161 set_grid_container_visible(
162 card->routing_hw_in_grid, !all_sources_disabled(card, PC_HW)
163 );
164
165 // Hardware Outputs
166 set_grid_container_visible(
167 card->routing_hw_out_grid, !all_sinks_disabled(card, PC_HW)
168 );
169
170 // PCM Inputs (sources - to PC)
171 set_grid_container_visible(
172 card->routing_pcm_in_grid, !all_sources_disabled(card, PC_PCM)
173 );
174
175 // PCM Outputs (sinks - from PC)
176 set_grid_container_visible(
177 card->routing_pcm_out_grid, !all_sinks_disabled(card, PC_PCM)
178 );
179
180 // DSP Inputs (sinks)
181 set_grid_container_visible(
182 card->routing_dsp_in_grid, !all_sinks_disabled(card, PC_DSP)
183 );
184
185 // DSP Outputs (sources)
186 set_grid_container_visible(
187 card->routing_dsp_out_grid, !all_sources_disabled(card, PC_DSP)
188 );
189
190 // Mixer Inputs (sinks)
191 set_grid_container_visible(
192 card->routing_mixer_in_grid, !all_sinks_disabled(card, PC_MIX)
193 );
194
195 // Mixer Outputs (sources)
196 set_grid_container_visible(
197 card->routing_mixer_out_grid, !all_sources_disabled(card, PC_MIX)
198 );
199
200 // Update the Sources and Sinks label arrows
201 update_sources_label(card);
202 update_sinks_label(card);
203 }
204
205 // Callback to update routing source visibility
206 static void src_visibility_changed(
207 struct alsa_elem *elem,
208 void *private
209 ) {
210 struct routing_src *src = private;
211
212 // widget might not exist yet if routing window hasn't been created
213 if (!src->widget)
214 return;
215
216 // Rearrange grid to add/remove widgets
217 arrange_src_grid(src->card, src->port_category);
218
219 // update section visibility
220 update_routing_section_visibility(src->card);
221
222 // schedule expensive updates
223 int flags = PENDING_UI_UPDATE_MONITOR_GROUPS;
224 if (src->port_category == PC_MIX)
225 flags |= PENDING_UI_UPDATE_MIXER_GRID;
226 schedule_ui_update(src->card, flags);
227 }
228
229 // Callback to update routing sink visibility
230 static void snk_visibility_changed(
231 struct alsa_elem *elem,
232 void *private
233 ) {
234 struct routing_snk *snk = private;
235 struct alsa_card *card = snk->elem ? snk->elem->card : NULL;
236 int port_category = snk->elem->port_category;
237
238 // Rearrange grid if routing widgets exist
239 // (fixed mixer inputs don't have routing widgets)
240 if (snk->socket_widget) {
241 arrange_snk_grid(card, port_category);
242
243 if (card)
244 update_routing_section_visibility(card);
245 }
246
247 if (card) {
248 // rebuild mixer grid for mixer inputs
249 // (needed for fixed mixer inputs which don't have routing widgets)
250 if (port_category == PC_MIX)
251 schedule_ui_update(card, PENDING_UI_UPDATE_MIXER_GRID);
252
253 // rebuild monitor groups for analogue output sinks
254 if (port_category == PC_HW)
255 schedule_ui_update(card, PENDING_UI_UPDATE_MONITOR_GROUPS);
256 }
257 }
258
259 // Free port enable callback data
260 void port_enable_free_callback_data(void *data) {
261 if (!data)
262 return;
263
264 struct port_enable_save_data *save_data = data;
265 g_free(save_data->config_key);
266 g_free(save_data);
267 }
268
269 // Generate ALSA element name for a routing source
270 // Returns newly allocated string that must be freed
271 static char *get_src_enable_elem_name(struct routing_src *src) {
272 const char *category_name = NULL;
273
274 switch (src->port_category) {
275 case PC_HW:
276 category_name = hw_type_names[src->hw_type];
277 return g_strdup_printf(
278 "%s In %d Switch", category_name, src->lr_num
279 );
280
281 case PC_PCM:
282 return g_strdup_printf("PCM Out %d Switch", src->lr_num);
283
284 case PC_MIX:
285 return g_strdup_printf("Mixer Out %d Switch", src->lr_num);
286
287 case PC_DSP:
288 return g_strdup_printf("DSP Out %d Switch", src->lr_num);
289
290 default:
291 return NULL;
292 }
293 }
294
295 // Generate ALSA element name for a routing sink
296 // Returns newly allocated string that must be freed
297 static char *get_snk_enable_elem_name(struct routing_snk *snk) {
298 struct alsa_elem *elem = snk->elem;
299 const char *category_name = NULL;
300
301 switch (elem->port_category) {
302 case PC_HW:
303 category_name = hw_type_names[elem->hw_type];
304 return g_strdup_printf(
305 "%s Out %d Switch", category_name, elem->lr_num
306 );
307
308 case PC_PCM:
309 return g_strdup_printf("PCM In %d Switch", elem->lr_num);
310
311 case PC_MIX:
312 return g_strdup_printf("Mixer In %d Switch", elem->lr_num);
313
314 case PC_DSP:
315 return g_strdup_printf("DSP In %d Switch", elem->lr_num);
316
317 default:
318 return NULL;
319 }
320 }
321
322 // Create simulated enable element for a routing source
323 static void create_src_enable_elem(
324 struct alsa_card *card,
325 struct routing_src *src,
326 GHashTable *state
327 ) {
328 // skip PC_OFF
329 if (src->port_category == PC_OFF)
330 return;
331
332 // generate element name
333 char *elem_name = get_src_enable_elem_name(src);
334 if (!elem_name)
335 return;
336
337 // check if real element exists (for forward compatibility)
338 struct alsa_elem *elem = get_elem_by_name(card->elems, elem_name);
339
340 if (elem) {
341 // real element exists, use it directly
342 src->enable_elem = elem;
343 g_free(elem_name);
344 return;
345 }
346
347 // create simulated boolean element
348 elem = alsa_create_optional_elem(
349 card,
350 elem_name,
351 SND_CTL_ELEM_TYPE_BOOLEAN,
352 0 // size not used for boolean
353 );
354 g_free(elem_name);
355
356 if (!elem) {
357 fprintf(stderr, "Failed to create enable element for source\n");
358 return;
359 }
360
361 src->enable_elem = elem;
362
363 // use element name as config key
364 char *config_key = get_src_enable_elem_name(src);
365 if (!config_key)
366 return;
367
368 // load value from state file (default to enabled)
369 const char *value = g_hash_table_lookup(state, config_key);
370 long enabled = 1; // default to enabled
371
372 if (value && *value) {
373 enabled = (strcmp(value, "1") == 0) ? 1 : 0;
374 }
375
376 // set the initial value
377 elem->value = enabled;
378
379 // register callback to save state on changes
380 struct port_enable_save_data *callback_data =
381 g_malloc0(sizeof(struct port_enable_save_data));
382 callback_data->card = card;
383 callback_data->config_key = config_key; // transfer ownership
384
385 alsa_elem_add_callback(
386 elem, port_enable_changed, callback_data,
387 port_enable_free_callback_data
388 );
389
390 // register callback to update widget visibility
391 alsa_elem_add_callback(elem, src_visibility_changed, src, NULL);
392 }
393
394 // Create simulated enable element for a routing sink
395 static void create_snk_enable_elem(
396 struct alsa_card *card,
397 struct routing_snk *snk,
398 GHashTable *state
399 ) {
400 // skip sinks without valid port category
401 if (snk->elem->port_category == PC_OFF)
402 return;
403
404 // generate element name
405 char *elem_name = get_snk_enable_elem_name(snk);
406 if (!elem_name)
407 return;
408
409 // check if real element exists (for forward compatibility)
410 struct alsa_elem *elem = get_elem_by_name(card->elems, elem_name);
411
412 if (elem) {
413 // real element exists, use it directly
414 snk->enable_elem = elem;
415 g_free(elem_name);
416 return;
417 }
418
419 // create simulated boolean element
420 elem = alsa_create_optional_elem(
421 card,
422 elem_name,
423 SND_CTL_ELEM_TYPE_BOOLEAN,
424 0 // size not used for boolean
425 );
426 g_free(elem_name);
427
428 if (!elem) {
429 fprintf(stderr, "Failed to create enable element for sink\n");
430 return;
431 }
432
433 snk->enable_elem = elem;
434
435 // use element name as config key
436 char *config_key = get_snk_enable_elem_name(snk);
437 if (!config_key)
438 return;
439
440 // load value from state file (default to enabled)
441 const char *value = g_hash_table_lookup(state, config_key);
442 long enabled = 1; // default to enabled
443
444 if (value && *value) {
445 enabled = (strcmp(value, "1") == 0) ? 1 : 0;
446 }
447
448 // set the initial value
449 elem->value = enabled;
450
451 // register callback to save state on changes
452 struct port_enable_save_data *callback_data =
453 g_malloc0(sizeof(struct port_enable_save_data));
454 callback_data->card = card;
455 callback_data->config_key = config_key; // transfer ownership
456
457 alsa_elem_add_callback(
458 elem, port_enable_changed, callback_data,
459 port_enable_free_callback_data
460 );
461
462 // register callback to update widget visibility
463 alsa_elem_add_callback(elem, snk_visibility_changed, snk, NULL);
464 }
465
466 // Initialise port enable elements for all routing sources and sinks
467 void port_enable_init(struct alsa_card *card) {
468 if (!card->serial || !*card->serial) {
469 // no serial number, can't persist state
470 return;
471 }
472
473 // check if card has routing controls
474 if (!card->routing_srcs || !card->routing_snks) {
475 // card doesn't have routing controls
476 return;
477 }
478
479 // load existing state from [controls] section
480 GHashTable *state = optional_state_load(card, CONFIG_SECTION_CONTROLS);
481 if (!state) {
482 state = g_hash_table_new_full(
483 g_str_hash, g_str_equal, g_free, g_free
484 );
485 }
486
487 // create enable elements for all routing sources
488 for (int i = 0; i < card->routing_srcs->len; i++) {
489 struct routing_src *src = &g_array_index(
490 card->routing_srcs, struct routing_src, i
491 );
492 create_src_enable_elem(card, src, state);
493 }
494
495 // create enable elements for all routing sinks
496 for (int i = 0; i < card->routing_snks->len; i++) {
497 struct routing_snk *snk = &g_array_index(
498 card->routing_snks, struct routing_snk, i
499 );
500 create_snk_enable_elem(card, snk, state);
501 }
502
503 g_hash_table_destroy(state);
504 }
505
506 // Check if a routing source is enabled
507 int is_routing_src_enabled(struct routing_src *src) {
508 if (!src || !src->enable_elem)
509 return 1; // default to enabled if no element
510
511 return alsa_get_elem_value(src->enable_elem) != 0;
512 }
513
514 // Check if a routing sink is enabled
515 int is_routing_snk_enabled(struct routing_snk *snk) {
516 if (!snk || !snk->enable_elem)
517 return 1; // default to enabled if no element
518
519 return alsa_get_elem_value(snk->enable_elem) != 0;
520 }
521
522 // Get the enable element for a routing source
523 struct alsa_elem *get_src_enable_elem(struct routing_src *src) {
524 if (!src)
525 return NULL;
526 return src->enable_elem;
527 }
528
529 // Get the enable element for a routing sink
530 struct alsa_elem *get_snk_enable_elem(struct routing_snk *snk) {
531 if (!snk)
532 return NULL;
533 return snk->enable_elem;
534 }
535
536 // Check if all sources of a given category are disabled
537 int all_sources_disabled(struct alsa_card *card, int port_category) {
538 if (!card || !card->routing_srcs)
539 return 1;
540
541 for (int i = 1; i < card->routing_srcs->len; i++) { // start at 1 to skip "Off"
542 struct routing_src *src = &g_array_index(
543 card->routing_srcs, struct routing_src, i
544 );
545
546 if (src->port_category == port_category) {
547 if (is_routing_src_enabled(src))
548 return 0; // found at least one enabled
549 }
550 }
551
552 return 1; // all disabled (or none exist)
553 }
554
555 // Check if all sinks of a given category are disabled
556 int all_sinks_disabled(struct alsa_card *card, int port_category) {
557 if (!card || !card->routing_snks)
558 return 1;
559
560 for (int i = 0; i < card->routing_snks->len; i++) {
561 struct routing_snk *snk = &g_array_index(
562 card->routing_snks, struct routing_snk, i
563 );
564
565 if (snk->elem && snk->elem->port_category == port_category) {
566 if (is_routing_snk_enabled(snk))
567 return 0; // found at least one enabled
568 }
569 }
570
571 return 1; // all disabled (or none exist)
572 }