Debian Codegraph @

src/custom-names.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 "custom-names.h"
8 #include "device-port-names.h"
9 #include "optional-state.h"
10 #include "alsa.h"
11 #include "stereo-link.h"
12 #include "widget-boolean.h"
13 #include "window-mixer.h"
14 #include "window-routing.h"
15
16 // Maximum length for custom names
17 #define MAX_CUSTOM_NAME_LEN 32
18
19 // Callback structure to pass data to save callback
20 struct custom_name_save_data {
21 struct alsa_card *card;
22 char *config_key;
23 };
24
25 // Callback when a custom name value changes
26 // Saves the new value to the state file
27 static void custom_name_changed(
28 struct alsa_elem *elem,
29 void *private
30 ) {
31 struct custom_name_save_data *data = private;
32
33 // get the string value
34 size_t size;
35 const void *bytes = alsa_get_elem_bytes(elem, &size);
36
37 if (bytes && size > 0) {
38 // find actual string length (up to first null byte)
39 size_t str_len = strnlen((const char *)bytes, size);
40
41 // only save if valid UTF-8
42 if (str_len > 0 && g_utf8_validate((const char *)bytes, str_len, NULL)) {
43 char *str = g_strndup((const char *)bytes, str_len);
44 optional_state_save(
45 data->card, CONFIG_SECTION_CONTROLS, data->config_key, str
46 );
47 g_free(str);
48 } else {
49 optional_state_save(
50 data->card, CONFIG_SECTION_CONTROLS, data->config_key, ""
51 );
52 }
53 } else {
54 optional_state_save(
55 data->card, CONFIG_SECTION_CONTROLS, data->config_key, ""
56 );
57 }
58 }
59
60 // Free custom name callback data
61 void custom_names_free_callback_data(void *data) {
62 if (!data)
63 return;
64
65 struct custom_name_save_data *save_data = data;
66 g_free(save_data->config_key);
67 g_free(save_data);
68 }
69
70 // Generate ALSA element name for a routing source
71 // Returns newly allocated string that must be freed
72 static char *get_src_elem_name(struct routing_src *src) {
73 const char *category_name = NULL;
74
75 switch (src->port_category) {
76 case PC_HW:
77 category_name = hw_type_names[src->hw_type];
78 return g_strdup_printf(
79 "%s In %d Name", category_name, src->lr_num
80 );
81
82 case PC_PCM:
83 return g_strdup_printf("PCM Out %d Name", src->lr_num);
84
85 case PC_MIX:
86 return g_strdup_printf("Mixer Out %d Name", src->lr_num);
87
88 case PC_DSP:
89 return g_strdup_printf("DSP Out %d Name", src->lr_num);
90
91 default:
92 return NULL;
93 }
94 }
95
96 // Generate ALSA element name for a routing sink
97 // Returns newly allocated string that must be freed
98 static char *get_snk_elem_name(struct routing_snk *snk) {
99 struct alsa_elem *elem = snk->elem;
100 const char *category_name = NULL;
101
102 switch (elem->port_category) {
103 case PC_HW:
104 category_name = hw_type_names[elem->hw_type];
105 return g_strdup_printf(
106 "%s Out %d Name", category_name, elem->lr_num
107 );
108
109 case PC_PCM:
110 return g_strdup_printf("PCM In %d Name", elem->lr_num);
111
112 case PC_MIX:
113 return g_strdup_printf("Mixer In %d Name", elem->lr_num);
114
115 case PC_DSP:
116 return g_strdup_printf("DSP In %d Name", elem->lr_num);
117
118 default:
119 return NULL;
120 }
121 }
122
123 // Update the cached display name for a routing source
124 static void update_src_display_name(struct routing_src *src) {
125 if (!src)
126 return;
127
128 // free old cached name
129 g_free(src->display_name);
130 src->display_name = NULL;
131
132 // check if custom name is set
133 if (src->custom_name_elem) {
134 size_t size;
135 const void *bytes = alsa_get_elem_bytes(src->custom_name_elem, &size);
136
137 if (bytes && size > 0) {
138 size_t str_len = strnlen((const char *)bytes, size);
139 if (str_len > 0 && g_utf8_validate((const char *)bytes, str_len, NULL)) {
140 // create properly null-terminated copy
141 src->display_name = g_strndup((const char *)bytes, str_len);
142 return;
143 }
144 }
145 }
146
147 // fall back to default name (device-specific or generic)
148 src->display_name = get_src_default_name_formatted(src, 0);
149 }
150
151 // Update the cached display name for a routing sink
152 static void update_snk_display_name(struct routing_snk *snk) {
153 if (!snk || !snk->elem)
154 return;
155
156 // free old cached name
157 g_free(snk->display_name);
158 snk->display_name = NULL;
159
160 // check if custom name is set
161 if (snk->custom_name_elem) {
162 size_t size;
163 const void *bytes = alsa_get_elem_bytes(snk->custom_name_elem, &size);
164
165 if (bytes && size > 0) {
166 size_t str_len = strnlen((const char *)bytes, size);
167 if (str_len > 0 && g_utf8_validate((const char *)bytes, str_len, NULL)) {
168 // create properly null-terminated copy
169 snk->display_name = g_strndup((const char *)bytes, str_len);
170 return;
171 }
172 }
173 }
174
175 // fall back to default name (device-specific or generic)
176 snk->display_name = get_snk_default_name_formatted(snk, 0);
177 }
178
179 // Callback when a routing source's custom name changes
180 // Updates the cached display name and triggers UI updates
181 static void src_custom_name_display_changed(
182 struct alsa_elem *elem,
183 void *private
184 ) {
185 struct routing_src *src = private;
186 update_src_display_name(src);
187
188 // update mixer window labels if mixer window exists
189 struct alsa_card *card = src->card;
190 if (card->window_mixer) {
191 // update mixer input labels (showing which source is connected)
192 update_mixer_labels(card);
193
194 // update mixer output labels (Mix A, Mix B, etc.) if this is a mixer source
195 if (src->port_category == PC_MIX) {
196 char *label = get_mixer_output_label_for_mixer_window(src);
197 if (src->mixer_label_left) {
198 gtk_label_set_text(GTK_LABEL(src->mixer_label_left), label);
199 gtk_widget_set_tooltip_text(src->mixer_label_left, label);
200 }
201 if (src->mixer_label_right) {
202 gtk_label_set_text(GTK_LABEL(src->mixer_label_right), label);
203 gtk_widget_set_tooltip_text(src->mixer_label_right, label);
204 }
205 g_free(label);
206 }
207 }
208
209 // update talkback button label in routing window if it exists
210 // talkback buttons use abbreviated stereo-aware names (A, B, A–B)
211 if (src->talkback_widget && src->port_category == PC_MIX) {
212 char *formatted_name;
213
214 if (is_src_linked(src) && is_src_left_channel(src)) {
215 // linked: show "A–B" format
216 formatted_name = g_strdup_printf(
217 "%c\xe2\x80\x93%c", src->port_num + 'A', src->port_num + 'B'
218 );
219 } else {
220 // not linked: show single letter
221 formatted_name = g_strdup_printf("%c", src->port_num + 'A');
222 }
223
224 boolean_widget_update_labels(
225 src->talkback_widget, formatted_name, formatted_name
226 );
227 g_free(formatted_name);
228 }
229
230 update_routing_src_label(src);
231 }
232
233 // Get generic hardware name for a routing source (no device-specific names)
234 // Returns newly allocated string that must be freed
235 char *get_src_generic_name(struct routing_src *src) {
236 if (!src)
237 return g_strdup("");
238
239 switch (src->port_category) {
240 case PC_HW:
241 return g_strdup_printf(
242 "%s %d", hw_type_names[src->hw_type], src->lr_num
243 );
244
245 case PC_PCM:
246 return g_strdup_printf("PCM %d", src->lr_num);
247
248 case PC_MIX:
249 return g_strdup_printf("Mix %c", src->port_num + 'A');
250
251 case PC_DSP:
252 return g_strdup_printf("DSP %d", src->lr_num);
253
254 default:
255 return g_strdup(src->name ? src->name : "");
256 }
257 }
258
259 // Get formatted default name for a routing source (ignoring custom name)
260 // Returns newly allocated string that must be freed
261 char *get_src_default_name_formatted(struct routing_src *src, int abbreviated) {
262 if (!src)
263 return g_strdup("");
264
265 // for abbreviated mode, always use short form for Mix/DSP
266 if (abbreviated) {
267 switch (src->port_category) {
268 case PC_MIX:
269 return g_strdup_printf("%c", src->port_num + 'A');
270 case PC_DSP:
271 return g_strdup_printf("%d", src->lr_num);
272 default:
273 break;
274 }
275 }
276
277 // check device-specific default name
278 const char *device_default = get_device_port_name(
279 src->card->pid,
280 src->port_category,
281 src->hw_type,
282 0, // is_snk = false for sources
283 src->port_num
284 );
285 if (device_default)
286 return g_strdup(device_default);
287
288 return get_src_generic_name(src);
289 }
290
291 // Get the formatted name to display for a routing source
292 // Returns newly allocated string that must be freed
293 char *get_src_display_name_formatted(struct routing_src *src) {
294 // mixer/DSP always show abbreviated form, others show display_name
295 if (src->port_category == PC_MIX || src->port_category == PC_DSP)
296 return get_src_default_name_formatted(src, 1);
297
298 return g_strdup(src->display_name);
299 }
300
301 // Get generic hardware name for a routing sink (no device-specific names)
302 // Returns newly allocated string that must be freed
303 char *get_snk_generic_name(struct routing_snk *snk) {
304 if (!snk || !snk->elem)
305 return g_strdup("");
306
307 struct alsa_elem *elem = snk->elem;
308
309 switch (elem->port_category) {
310 case PC_HW:
311 return g_strdup_printf(
312 "%s %d", hw_type_names[elem->hw_type], elem->lr_num
313 );
314
315 case PC_PCM:
316 return g_strdup_printf("PCM %d", elem->lr_num);
317
318 case PC_MIX:
319 return g_strdup_printf("Mixer %d", elem->lr_num);
320
321 case PC_DSP:
322 return g_strdup_printf("DSP %d", elem->lr_num);
323
324 default:
325 return g_strdup(elem->name ? elem->name : "");
326 }
327 }
328
329 // Get formatted default name for a routing sink (ignoring custom name)
330 // If abbreviated is true, Mix/DSP use short form ("1") for routing window
331 // Returns newly allocated string that must be freed
332 char *get_snk_default_name_formatted(struct routing_snk *snk, int abbreviated) {
333 if (!snk || !snk->elem)
334 return g_strdup("");
335
336 struct alsa_elem *elem = snk->elem;
337
338 // for abbreviated mode, always use short form for Mix/DSP
339 if (abbreviated) {
340 switch (elem->port_category) {
341 case PC_MIX:
342 case PC_DSP:
343 return g_strdup_printf("%d", elem->lr_num);
344 default:
345 break;
346 }
347 }
348
349 // check device-specific default name
350 const char *device_default = get_device_port_name(
351 elem->card->pid,
352 elem->port_category,
353 elem->hw_type,
354 1, // is_snk = true for sinks
355 elem->port_num
356 );
357 if (device_default)
358 return g_strdup(device_default);
359
360 return get_snk_generic_name(snk);
361 }
362
363 // Get formatted display name for a routing sink (for UI display)
364 // Returns custom name if set, otherwise formatted default name
365 // Mix/DSP sinks use abbreviated form for routing window
366 // Returns newly allocated string that must be freed
367 char *get_snk_display_name_formatted(struct routing_snk *snk) {
368 if (!snk || !snk->elem)
369 return g_strdup("");
370
371 struct alsa_elem *elem = snk->elem;
372
373 // Mix/DSP always show abbreviated form, others show display_name
374 if (elem->port_category == PC_MIX || elem->port_category == PC_DSP)
375 return get_snk_default_name_formatted(snk, 1);
376
377 return g_strdup(snk->display_name);
378 }
379
380 // Callback when a routing sink's custom name changes
381 // Updates the cached display name and triggers UI updates
382 static void snk_custom_name_display_changed(
383 struct alsa_elem *elem,
384 void *private
385 ) {
386 struct routing_snk *snk = private;
387 update_snk_display_name(snk);
388
389 // update routing window label - this handles monitor group indicators too
390 update_hw_output_label(snk);
391 }
392
393 // Create simulated element for a routing source
394 static void create_src_custom_name_elem(
395 struct alsa_card *card,
396 struct routing_src *src,
397 GHashTable *state
398 ) {
399 // skip PC_OFF
400 if (src->port_category == PC_OFF)
401 return;
402
403 // generate element name
404 char *elem_name = get_src_elem_name(src);
405 if (!elem_name)
406 return;
407
408 // check if real element exists (for forward compatibility)
409 struct alsa_elem *elem = get_elem_by_name(card->elems, elem_name);
410
411 if (elem) {
412 // real element exists, store the pointer and set up callbacks
413 src->custom_name_elem = elem;
414 g_free(elem_name);
415 alsa_elem_add_callback(elem, src_custom_name_display_changed, src, NULL);
416 update_src_display_name(src);
417 return;
418 }
419
420 // create simulated element
421 elem = alsa_create_optional_elem(
422 card,
423 elem_name,
424 SND_CTL_ELEM_TYPE_BYTES,
425 MAX_CUSTOM_NAME_LEN
426 );
427 g_free(elem_name);
428
429 if (!elem) {
430 fprintf(stderr, "Failed to create custom name element for source\n");
431 return;
432 }
433
434 src->custom_name_elem = elem;
435
436 // use element name as config key
437 char *config_key = get_src_elem_name(src);
438 if (!config_key)
439 return;
440
441 // load value from state file
442 const char *value = g_hash_table_lookup(state, config_key);
443 if (value && *value)
444 alsa_set_elem_bytes(elem, value, strlen(value));
445
446 // register callback to save state on changes
447 struct custom_name_save_data *callback_data =
448 g_malloc0(sizeof(struct custom_name_save_data));
449 callback_data->card = card;
450 callback_data->config_key = config_key;
451
452 alsa_elem_add_callback(
453 elem, custom_name_changed, callback_data,
454 custom_names_free_callback_data
455 );
456
457 // register callback to update display name and UI
458 alsa_elem_add_callback(elem, src_custom_name_display_changed, src, NULL);
459
460 // initialise cached display name
461 update_src_display_name(src);
462 }
463
464 // Create simulated element for a routing sink
465 static void create_snk_custom_name_elem(
466 struct alsa_card *card,
467 struct routing_snk *snk,
468 GHashTable *state
469 ) {
470 // skip sinks without valid port category
471 if (snk->elem->port_category == PC_OFF)
472 return;
473
474 // skip DSP sinks (DSP inputs just show numbers on routing window)
475 if (snk->elem->port_category == PC_DSP)
476 return;
477
478 // generate element name
479 char *elem_name = get_snk_elem_name(snk);
480 if (!elem_name)
481 return;
482
483 // check if real element exists (for forward compatibility)
484 struct alsa_elem *elem = get_elem_by_name(card->elems, elem_name);
485
486 if (elem) {
487 // real element exists, store the pointer and set up callbacks
488 snk->custom_name_elem = elem;
489 g_free(elem_name);
490 alsa_elem_add_callback(elem, snk_custom_name_display_changed, snk, NULL);
491 update_snk_display_name(snk);
492 return;
493 }
494
495 // create simulated element
496 elem = alsa_create_optional_elem(
497 card,
498 elem_name,
499 SND_CTL_ELEM_TYPE_BYTES,
500 MAX_CUSTOM_NAME_LEN
501 );
502 g_free(elem_name);
503
504 if (!elem) {
505 fprintf(stderr, "Failed to create custom name element for sink\n");
506 return;
507 }
508
509 snk->custom_name_elem = elem;
510
511 // use element name as config key
512 char *config_key = get_snk_elem_name(snk);
513 if (!config_key)
514 return;
515
516 // load value from state file
517 const char *value = g_hash_table_lookup(state, config_key);
518 if (value && *value)
519 alsa_set_elem_bytes(elem, value, strlen(value));
520
521 // register callback to save state on changes
522 struct custom_name_save_data *callback_data =
523 g_malloc0(sizeof(struct custom_name_save_data));
524 callback_data->card = card;
525 callback_data->config_key = config_key;
526
527 alsa_elem_add_callback(
528 elem, custom_name_changed, callback_data,
529 custom_names_free_callback_data
530 );
531
532 // register callback to update display name and UI
533 alsa_elem_add_callback(elem, snk_custom_name_display_changed, snk, NULL);
534
535 // initialise cached display name
536 update_snk_display_name(snk);
537 }
538
539 // Initialise custom name elements for all routing sources and sinks
540 void custom_names_init(struct alsa_card *card) {
541 if (!card->serial || !*card->serial) {
542 // no serial number, can't persist state
543 return;
544 }
545
546 // check if card has routing controls
547 if (!card->routing_srcs || !card->routing_snks) {
548 // card doesn't have routing controls
549 return;
550 }
551
552 // load existing state
553 GHashTable *state = optional_state_load(card, CONFIG_SECTION_CONTROLS);
554 if (!state) {
555 state = g_hash_table_new_full(
556 g_str_hash, g_str_equal, g_free, g_free
557 );
558 }
559
560 // create custom name elements for all routing sources
561 for (int i = 0; i < card->routing_srcs->len; i++) {
562 struct routing_src *src = &g_array_index(
563 card->routing_srcs, struct routing_src, i
564 );
565 create_src_custom_name_elem(card, src, state);
566 }
567
568 // create custom name elements for all routing sinks
569 for (int i = 0; i < card->routing_snks->len; i++) {
570 struct routing_snk *snk = &g_array_index(
571 card->routing_snks, struct routing_snk, i
572 );
573 create_snk_custom_name_elem(card, snk, state);
574 }
575
576 g_hash_table_destroy(state);
577
578 // initialise display names for sources/sinks without custom name elements
579 for (int i = 0; i < card->routing_srcs->len; i++) {
580 struct routing_src *src = &g_array_index(
581 card->routing_srcs, struct routing_src, i
582 );
583 if (!src->display_name)
584 update_src_display_name(src);
585 }
586
587 for (int i = 0; i < card->routing_snks->len; i++) {
588 struct routing_snk *snk = &g_array_index(
589 card->routing_snks, struct routing_snk, i
590 );
591 if (!snk->display_name)
592 update_snk_display_name(snk);
593 }
594 }
595
596 // Get display name for a routing source (returns cached value)
597 const char *get_routing_src_display_name(struct routing_src *src) {
598 if (!src)
599 return "";
600 return src->display_name ? src->display_name : "";
601 }
602
603 // Get mixer output label for mixer window
604 // Returns newly allocated string that must be freed
605 char *get_mixer_output_label_for_mixer_window(struct routing_src *src) {
606 if (!src || src->port_category != PC_MIX)
607 return g_strdup("");
608
609 // display_name already has: custom name > device default > generic default
610 if (src->display_name)
611 return g_strdup(src->display_name);
612
613 return get_src_default_name_formatted(src, 0);
614 }
615
616 // Get display name for a routing sink (returns cached value)
617 const char *get_routing_snk_display_name(struct routing_snk *snk) {
618 if (!snk)
619 return "";
620 return snk->display_name ? snk->display_name : "";
621 }