Debian Codegraph @

src/window-mixer.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 <graphene.h>
5 #include <gtk/gtk.h>
6 #include <math.h>
7
8 #include "alsa.h"
9 #include "custom-names.h"
10 #include "glow.h"
11 #include "gtkhelper.h"
12 #include "hw-io-availability.h"
13 #include "port-enable.h"
14 #include "stereo-link.h"
15 #include "stringhelper.h"
16 #include "widget-gain.h"
17 #include "window-mixer.h"
18
19 // data structure for rotated label info
20 struct rotated_label_info {
21 char *text;
22 int text_w;
23 int text_h;
24 gboolean hover;
25 cairo_surface_t *glow_cache;
26 int glow_cache_w;
27 int glow_cache_h;
28 };
29
30 static void rotated_label_info_free(gpointer data) {
31 struct rotated_label_info *info = data;
32 if (info->glow_cache)
33 cairo_surface_destroy(info->glow_cache);
34 g_free(info->text);
35 g_free(info);
36 }
37
38 // rotation angle for mixer input labels
39 #define LABEL_ANGLE (G_PI / 6.0) // 30°
40 #define LABEL_SIN 0.5 // sin(30°)
41 #define LABEL_COS 0.86602540378443864676 // cos(30°)
42
43 // vertical padding for rotated label anchor
44 #define LABEL_VERTICAL_PAD 5
45
46 // fraction of cell width to inset rotated labels from cell edge
47 #define LABEL_INSET_FRACTION 0.2
48
49 // mixer glow bar sizing
50 #define MIXER_GLOW_MAX_WIDTH 35.0
51 #define MIXER_GLOW_WIDTH_SCALE(intensity) \
52 (0.75 + 0.75 * (intensity))
53 // total glow extent including round cap overshoot
54 #define MIXER_GLOW_FULL_EXTENT \
55 (MIXER_GLOW_MAX_WIDTH + \
56 GLOW_MAX_WIDTH * MIXER_GLOW_WIDTH_SCALE(1) /* max */ / 2)
57 #define MIXER_LABEL_MIN_WIDTH \
58 ((int)(MIXER_GLOW_FULL_EXTENT * 2))
59
60 // draw a horizontal glow bar at a specific position
61 static void draw_glow_at(
62 cairo_t *cr,
63 double cx,
64 double cy,
65 double max_extent,
66 double level_db
67 ) {
68 double intensity = get_glow_intensity(level_db);
69 if (intensity <= 0)
70 return;
71
72 double r, g, b;
73 level_to_colour(level_db, &r, &g, &b);
74
75 double extent = max_extent * intensity;
76 if (extent < 5.0)
77 extent = 5.0;
78
79 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
80
81 for (int layer = GLOW_LAYERS - 1; layer >= 0; layer--) {
82 double width, alpha;
83 get_glow_layer_params(layer, intensity, &width, &alpha);
84
85 width *= MIXER_GLOW_WIDTH_SCALE(intensity);
86
87 cairo_set_source_rgba(cr, r, g, b, alpha);
88 cairo_set_line_width(cr, width);
89 cairo_move_to(cr, cx - extent, cy);
90 cairo_line_to(cr, cx + extent, cy);
91 cairo_stroke(cr);
92 }
93 }
94
95 // get label centre coordinates relative to parent
96 static gboolean get_label_centre(
97 GtkWidget *label,
98 GtkWidget *parent,
99 double *cx,
100 double *cy
101 ) {
102 if (!label || !gtk_widget_get_visible(label))
103 return FALSE;
104
105 graphene_point_t src = GRAPHENE_POINT_INIT(
106 gtk_widget_get_width(label) / 2.0,
107 gtk_widget_get_height(label) / 2.0
108 );
109 graphene_point_t dest;
110 if (!gtk_widget_compute_point(label, parent, &src, &dest))
111 return FALSE;
112
113 *cx = dest.x;
114 *cy = dest.y;
115 return TRUE;
116 }
117
118 // result of computing the anchor point for a rotated label
119 struct label_anchor {
120 double anchor_x, anchor_y;
121 graphene_point_t tl, br;
122 };
123
124 // compute the cell corners and anchor point for a rotated label.
125 // text_h is needed to offset the anchor from the cell edge.
126 static gboolean compute_label_anchor(
127 GtkWidget *label_widget,
128 GtkWidget *overlay_widget,
129 gboolean is_bottom,
130 int text_h,
131 struct label_anchor *out
132 ) {
133 // get cell corners in overlay coordinates
134 graphene_point_t src_tl = GRAPHENE_POINT_INIT(0, 0);
135 graphene_point_t src_br = GRAPHENE_POINT_INIT(
136 gtk_widget_get_width(label_widget),
137 gtk_widget_get_height(label_widget)
138 );
139 if (!gtk_widget_compute_point(
140 label_widget, overlay_widget, &src_tl, &out->tl) ||
141 !gtk_widget_compute_point(
142 label_widget, overlay_widget, &src_br, &out->br))
143 return FALSE;
144
145 double cell_w = out->br.x - out->tl.x;
146 double inset = cell_w * LABEL_INSET_FRACTION;
147
148 if (is_bottom) {
149 // text top-right at cell top-right; body extends right by
150 // text_h * sin, so shift anchor left to stay within cell;
151 // additional inset moves text further left within cell
152 out->anchor_x =
153 out->br.x - text_h * LABEL_SIN - inset;
154 out->anchor_y = out->tl.y + LABEL_VERTICAL_PAD;
155 } else {
156 // text bottom-left at cell bottom-left; body extends down
157 // by text_h * cos, so shift anchor up to stay within cell;
158 // additional inset moves text further right within cell
159 out->anchor_x = out->tl.x + inset;
160 out->anchor_y = out->br.y - text_h * LABEL_COS;
161 }
162
163 return TRUE;
164 }
165
166 // get the centre of a rotated mixer input label in overlay
167 // coordinates, for positioning glow bars behind the text
168 static gboolean get_rotated_label_centre(
169 GtkWidget *label_widget,
170 GtkWidget *overlay_widget,
171 gboolean is_bottom,
172 double *cx,
173 double *cy
174 ) {
175 if (!label_widget || !gtk_widget_get_visible(label_widget))
176 return FALSE;
177
178 struct rotated_label_info *info = g_object_get_data(
179 G_OBJECT(label_widget), "label_info"
180 );
181 if (!info || !info->text || !*info->text)
182 return FALSE;
183
184 struct label_anchor anchor;
185 if (!compute_label_anchor(
186 label_widget, overlay_widget,
187 is_bottom, info->text_h, &anchor))
188 return FALSE;
189
190 // text centre in text space, accounting for the extra
191 // -text_w shift applied to bottom labels
192 double tx = is_bottom
193 ? -info->text_w / 2.0 : info->text_w / 2.0;
194 double ty = info->text_h / 2.0;
195
196 // rotate by -LABEL_ANGLE and translate to anchor
197 *cx = anchor.anchor_x + tx * LABEL_COS + ty * LABEL_SIN;
198 *cy = anchor.anchor_y - tx * LABEL_SIN + ty * LABEL_COS;
199 return TRUE;
200 }
201
202 // draw a single mono glow bar centred on a label
203 static void draw_label_glow(
204 cairo_t *cr,
205 GtkWidget *label,
206 GtkWidget *parent,
207 double level_db
208 ) {
209 double cx, cy;
210 if (!get_label_centre(label, parent, &cx, &cy))
211 return;
212
213 draw_glow_at(cr, cx, cy, MIXER_GLOW_MAX_WIDTH, level_db);
214 }
215
216 // draw one side of a stereo glow bar growing outward from centre;
217 // round cap on outer end only, flat on inner end
218 static void draw_glow_bar_outward(
219 cairo_t *cr,
220 double inner_x,
221 double cy,
222 double max_extent,
223 double level_db,
224 int direction // -1 = left, +1 = right
225 ) {
226 double intensity = get_glow_intensity(level_db);
227 if (intensity <= 0)
228 return;
229
230 double r, g, b;
231 level_to_colour(level_db, &r, &g, &b);
232
233 double extent = max_extent * intensity;
234 if (extent < 5.0)
235 extent = 5.0;
236
237 double outer_x = inner_x + direction * extent;
238
239 for (int layer = GLOW_LAYERS - 1; layer >= 0; layer--) {
240 double width, alpha;
241 get_glow_layer_params(layer, intensity, &width, &alpha);
242
243 width *= MIXER_GLOW_WIDTH_SCALE(intensity);
244
245 double half = width / 2.0;
246
247 cairo_set_source_rgba(cr, r, g, b, alpha);
248 cairo_set_line_width(cr, width);
249
250 // flat inner end, extend to outer edge of round cap
251 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
252 cairo_move_to(cr, inner_x, cy);
253 cairo_line_to(cr, outer_x, cy);
254 cairo_stroke(cr);
255
256 // round cap as semicircle on outer end
257 double a_start = direction > 0 ? -G_PI / 2 : G_PI / 2;
258 cairo_arc(
259 cr, outer_x, cy, half, a_start, a_start + G_PI
260 );
261 cairo_fill(cr);
262 }
263 }
264
265 // draw split L/R glow bars growing outward from label centre
266 static void draw_stereo_label_glow(
267 cairo_t *cr,
268 GtkWidget *label,
269 GtkWidget *parent,
270 double level_db_l,
271 double level_db_r
272 ) {
273 double cx, cy;
274 if (!get_label_centre(label, parent, &cx, &cy))
275 return;
276
277 draw_glow_bar_outward(cr, cx, cy, MIXER_GLOW_MAX_WIDTH, level_db_l, -1);
278 draw_glow_bar_outward(cr, cx, cy, MIXER_GLOW_MAX_WIDTH, level_db_r, +1);
279 }
280
281 // draw a mono glow bar centred on a rotated mixer input label
282 static void draw_rotated_label_glow(
283 cairo_t *cr,
284 GtkWidget *label,
285 GtkWidget *parent,
286 gboolean is_bottom,
287 double level_db
288 ) {
289 double cx, cy;
290 if (!get_rotated_label_centre(label, parent, is_bottom, &cx, &cy))
291 return;
292
293 cairo_save(cr);
294 cairo_translate(cr, cx, cy);
295 cairo_rotate(cr, -LABEL_ANGLE);
296 draw_glow_at(cr, 0, 0, MIXER_GLOW_MAX_WIDTH, level_db);
297 cairo_restore(cr);
298 }
299
300 // draw split L/R glow bars centred on a rotated mixer input label
301 static void draw_rotated_stereo_label_glow(
302 cairo_t *cr,
303 GtkWidget *label,
304 GtkWidget *parent,
305 gboolean is_bottom,
306 double level_db_l,
307 double level_db_r
308 ) {
309 double cx, cy;
310 if (!get_rotated_label_centre(label, parent, is_bottom, &cx, &cy))
311 return;
312
313 cairo_save(cr);
314 cairo_translate(cr, cx, cy);
315 cairo_rotate(cr, -LABEL_ANGLE);
316 draw_glow_bar_outward(cr, 0, 0, MIXER_GLOW_MAX_WIDTH, level_db_l, -1);
317 draw_glow_bar_outward(cr, 0, 0, MIXER_GLOW_MAX_WIDTH, level_db_r, +1);
318 cairo_restore(cr);
319 }
320
321 // draw function for mixer label glow overlay
322 static void draw_mixer_glow(
323 GtkDrawingArea *drawing_area,
324 cairo_t *cr,
325 int width,
326 int height,
327 void *user_data
328 ) {
329 struct alsa_card *card = user_data;
330 GtkWidget *parent = card->mixer_glow;
331
332 if (!card->routing_srcs)
333 return;
334
335 // draw glow behind mixer output labels (Mix A, Mix B, etc.)
336 for (int i = 0; i < card->routing_srcs->len; i++) {
337 struct routing_src *r_src = &g_array_index(
338 card->routing_srcs, struct routing_src, i
339 );
340
341 if (r_src->port_category != PC_MIX)
342 continue;
343
344 if (!is_routing_src_enabled(r_src) || !should_display_src(r_src))
345 continue;
346
347 double level_l = get_routing_src_level_db(card, r_src);
348
349 if (is_src_linked(r_src)) {
350 struct routing_src *r_src_r = get_src_partner(r_src);
351 double level_r = r_src_r ?
352 get_routing_src_level_db(card, r_src_r) : level_l;
353
354 draw_stereo_label_glow(
355 cr, r_src->mixer_label_left, parent, level_l, level_r
356 );
357 if (card->pref_show_bottom_right_labels)
358 draw_stereo_label_glow(
359 cr, r_src->mixer_label_right, parent, level_l, level_r
360 );
361 } else {
362 draw_label_glow(
363 cr, r_src->mixer_label_left, parent, level_l
364 );
365 if (card->pref_show_bottom_right_labels)
366 draw_label_glow(
367 cr, r_src->mixer_label_right, parent, level_l
368 );
369 }
370 }
371
372 // draw glow behind mixer input labels (top/bottom)
373 for (int i = 0; i < card->routing_snks->len; i++) {
374 struct routing_snk *r_snk = &g_array_index(
375 card->routing_snks, struct routing_snk, i
376 );
377
378 if (!r_snk->elem || r_snk->elem->port_category != PC_MIX)
379 continue;
380
381 if (!is_routing_snk_enabled(r_snk) || !should_display_snk(r_snk))
382 continue;
383
384 // get the source connected to this mixer input
385 int r_src_idx = alsa_get_elem_value(r_snk->elem);
386 if (!r_src_idx)
387 continue;
388
389 struct routing_src *r_src = &g_array_index(
390 card->routing_srcs, struct routing_src, r_src_idx
391 );
392
393 double level_l = get_routing_src_level_db(card, r_src);
394
395 if (is_snk_linked(r_snk)) {
396 struct routing_snk *r_snk_r = get_snk_partner(r_snk);
397 double level_r = level_l;
398
399 if (r_snk_r && r_snk_r->elem) {
400 int r_src_idx_r = alsa_get_elem_value(r_snk_r->elem);
401 if (r_src_idx_r) {
402 struct routing_src *r_src_r = &g_array_index(
403 card->routing_srcs, struct routing_src, r_src_idx_r
404 );
405 level_r = get_routing_src_level_db(card, r_src_r);
406 }
407 }
408
409 draw_rotated_stereo_label_glow(
410 cr, r_snk->mixer_label_top, parent,
411 FALSE, level_l, level_r
412 );
413 if (card->pref_show_bottom_right_labels)
414 draw_rotated_stereo_label_glow(
415 cr, r_snk->mixer_label_bottom, parent,
416 TRUE, level_l, level_r
417 );
418 } else {
419 draw_rotated_label_glow(
420 cr, r_snk->mixer_label_top, parent,
421 FALSE, level_l
422 );
423 if (card->pref_show_bottom_right_labels)
424 draw_rotated_label_glow(
425 cr, r_snk->mixer_label_bottom, parent,
426 TRUE, level_l
427 );
428 }
429 }
430 }
431
432 // mixer_gain_widgets is stored in card->mixer_gain_widgets
433 // struct mixer_gain_widget is declared in window-mixer.h
434
435 // apply a single-pass box blur to the alpha channel of an ARGB32
436 // image surface. operates on raw pixel data in-place. uses a
437 // temporary buffer to avoid read-after-write hazards.
438 static void box_blur_alpha(
439 unsigned char *data,
440 int width,
441 int height,
442 int stride,
443 int radius
444 ) {
445 // horizontal pass — blur each row into a temp buffer,
446 // then copy back
447 unsigned char *tmp = g_malloc(
448 MAX(width, height) * sizeof(unsigned char)
449 );
450
451 for (int y = 0; y < height; y++) {
452 unsigned char *row = data + y * stride;
453
454 // build initial window for x=0: [0, min(radius, width-1)]
455 int right = MIN(radius, width - 1);
456 int sum = 0;
457 for (int x = 0; x <= right; x++)
458 sum += row[x * 4 + 3];
459
460 for (int x = 0; x < width; x++) {
461 int count = MIN(x, radius) + MIN(width - 1 - x, radius) + 1;
462 tmp[x] = sum / count;
463
464 // slide window: remove departing left, add arriving right
465 int rem = x - radius;
466 int add = x + radius + 1;
467 if (rem >= 0)
468 sum -= row[rem * 4 + 3];
469 if (add < width)
470 sum += row[add * 4 + 3];
471 }
472
473 for (int x = 0; x < width; x++)
474 row[x * 4 + 3] = tmp[x];
475 }
476
477 // vertical pass — blur each column into a temp buffer,
478 // then copy back
479 for (int x = 0; x < width; x++) {
480 int off = x * 4 + 3;
481
482 // build initial window for y=0: [0, min(radius, height-1)]
483 int bottom = MIN(radius, height - 1);
484 int sum = 0;
485 for (int y = 0; y <= bottom; y++)
486 sum += data[y * stride + off];
487
488 for (int y = 0; y < height; y++) {
489 int count =
490 MIN(y, radius) + MIN(height - 1 - y, radius) + 1;
491 tmp[y] = sum / count;
492
493 // slide window: remove departing top, add arriving bottom
494 int rem = y - radius;
495 int add = y + radius + 1;
496 if (rem >= 0)
497 sum -= data[rem * stride + off];
498 if (add < height)
499 sum += data[add * stride + off];
500 }
501
502 for (int y = 0; y < height; y++)
503 data[y * stride + off] = tmp[y];
504 }
505
506 g_free(tmp);
507 }
508
509 // build the blurred green text-shadow glow surface for a label.
510 // matches CSS: text-shadow: 0 0 5px #00c000, 0 0 15px #00c000.
511 // CSS blur-radius ≈ 2σ; 3 box-blur passes at radius r give
512 // σ ≈ 0.87r, so r = CSS_radius / (2 × 0.87) ≈ CSS_radius / 1.73.
513 static cairo_surface_t *build_glow_surface(
514 PangoLayout *layout,
515 int text_w,
516 int text_h,
517 int pad
518 ) {
519 int surf_w = text_w + pad * 2;
520 int surf_h = text_h + pad * 2;
521
522 static const int radii[] = { 3, 9 };
523
524 // render text mask once, reuse for each blur radius
525 unsigned char *text_mask = g_malloc(surf_w * surf_h);
526
527 {
528 cairo_surface_t *mask_surf = cairo_image_surface_create(
529 CAIRO_FORMAT_ARGB32, surf_w, surf_h
530 );
531 cairo_t *gc = cairo_create(mask_surf);
532 cairo_set_source_rgba(gc, 1, 1, 1, 1);
533 cairo_move_to(gc, pad, pad);
534 pango_cairo_show_layout(gc, layout);
535 cairo_destroy(gc);
536
537 cairo_surface_flush(mask_surf);
538 unsigned char *mdata =
539 cairo_image_surface_get_data(mask_surf);
540 int mstride =
541 cairo_image_surface_get_stride(mask_surf);
542 for (int y = 0; y < surf_h; y++)
543 for (int x = 0; x < surf_w; x++)
544 text_mask[y * surf_w + x] =
545 mdata[y * mstride + x * 4 + 3];
546 cairo_surface_destroy(mask_surf);
547 }
548
549 // composite surface: accumulate both blur passes
550 cairo_surface_t *result = cairo_image_surface_create(
551 CAIRO_FORMAT_ARGB32, surf_w, surf_h
552 );
553 cairo_t *gc = cairo_create(result);
554
555 for (int r = 0; r < 2; r++) {
556 cairo_surface_t *surf = cairo_image_surface_create(
557 CAIRO_FORMAT_ARGB32, surf_w, surf_h
558 );
559 cairo_surface_flush(surf);
560 unsigned char *data =
561 cairo_image_surface_get_data(surf);
562 int stride = cairo_image_surface_get_stride(surf);
563
564 // seed alpha from text mask
565 for (int y = 0; y < surf_h; y++)
566 for (int x = 0; x < surf_w; x++)
567 data[y * stride + x * 4 + 3] =
568 text_mask[y * surf_w + x];
569
570 // 3 box-blur passes ≈ Gaussian
571 for (int pass = 0; pass < 3; pass++)
572 box_blur_alpha(
573 data, surf_w, surf_h, stride, radii[r]
574 );
575
576 // recolour to premultiplied #00c000. restore full
577 // opacity where the original text was so the glow is
578 // bright green right at the text edge (matching CSS
579 // text-shadow which paints the colour at full opacity
580 // then blurs outward).
581 for (int y = 0; y < surf_h; y++) {
582 unsigned char *row = data + y * stride;
583 for (int x = 0; x < surf_w; x++) {
584 int a = row[x * 4 + 3];
585 int orig = text_mask[y * surf_w + x];
586 if (orig > a)
587 a = orig;
588 row[x * 4 + 0] = 0; // B
589 row[x * 4 + 1] = 0xC0 * a / 255; // G
590 row[x * 4 + 2] = 0; // R
591 row[x * 4 + 3] = a;
592 }
593 }
594 cairo_surface_mark_dirty(surf);
595
596 cairo_set_source_surface(gc, surf, 0, 0);
597 cairo_paint(gc);
598 cairo_surface_destroy(surf);
599 }
600
601 cairo_destroy(gc);
602 g_free(text_mask);
603
604 return result;
605 }
606
607 // draw cached blurred green text-shadow glow behind a label.
608 // caller must set cairo position to text origin.
609 static void draw_text_shadow_glow(
610 cairo_t *cr,
611 struct rotated_label_info *info,
612 PangoLayout *layout,
613 int text_w,
614 int text_h
615 ) {
616 if (text_w <= 0 || text_h <= 0)
617 return;
618
619 static const int max_radius = 9;
620 int pad = max_radius * 3 + 2;
621
622 // rebuild cache if dimensions changed
623 if (!info->glow_cache ||
624 info->glow_cache_w != text_w ||
625 info->glow_cache_h != text_h) {
626 if (info->glow_cache)
627 cairo_surface_destroy(info->glow_cache);
628 info->glow_cache =
629 build_glow_surface(layout, text_w, text_h, pad);
630 info->glow_cache_w = text_w;
631 info->glow_cache_h = text_h;
632 }
633
634 cairo_set_source_surface(
635 cr, info->glow_cache, -pad, -pad
636 );
637 cairo_paint(cr);
638 }
639
640 // draw a single rotated mixer input label.
641 // for top labels: text bottom-left touches cell bottom-left corner.
642 // for bottom labels: text bottom-right touches cell top-right corner.
643 //
644 // with rotation angle θ, the text bottom edge (y = text_h in text
645 // space) maps to (+text_h × sinθ, +text_h × cosθ) in screen space
646 // relative to the text origin. the anchor is therefore offset from
647 // the cell corner by (-text_h × sinθ, -text_h × cosθ).
648 static void draw_snk_label(
649 cairo_t *cr,
650 GtkWidget *label_widget,
651 GtkWidget *overlay_widget,
652 gboolean is_bottom
653 ) {
654 struct rotated_label_info *info = g_object_get_data(
655 G_OBJECT(label_widget), "label_info"
656 );
657 if (!info || !info->text || !*info->text)
658 return;
659
660 // use the widget's own Pango context to get the system theme
661 // font; bold on hover
662 PangoContext *pango_ctx =
663 gtk_widget_get_pango_context(overlay_widget);
664 PangoLayout *layout = pango_layout_new(pango_ctx);
665
666 if (info->hover) {
667 PangoFontDescription *bold = pango_font_description_copy(
668 pango_context_get_font_description(pango_ctx)
669 );
670 pango_font_description_set_weight(bold, PANGO_WEIGHT_BOLD);
671 pango_layout_set_font_description(layout, bold);
672 pango_font_description_free(bold);
673 }
674
675 pango_layout_set_text(layout, info->text, -1);
676
677 int text_w, text_h;
678 pango_layout_get_pixel_size(layout, &text_w, &text_h);
679
680 struct label_anchor anchor;
681 if (!compute_label_anchor(
682 label_widget, overlay_widget,
683 is_bottom, text_h, &anchor)) {
684 g_object_unref(layout);
685 return;
686 }
687
688 cairo_save(cr);
689 cairo_translate(cr, anchor.anchor_x, anchor.anchor_y);
690 cairo_rotate(cr, -LABEL_ANGLE);
691
692 // shift text left so it ends (right edge) at the anchor column
693 if (is_bottom)
694 cairo_translate(cr, -text_w, 0);
695
696 if (info->hover) {
697 draw_text_shadow_glow(cr, info, layout, text_w, text_h);
698 cairo_set_source_rgb(cr, 1, 1, 1);
699 } else {
700 GdkRGBA color;
701 gtk_widget_get_color(overlay_widget, &color);
702 gdk_cairo_set_source_rgba(cr, &color);
703 }
704
705 pango_cairo_show_layout(cr, layout);
706 cairo_restore(cr);
707 g_object_unref(layout);
708 }
709
710 static void draw_mixer_labels(
711 GtkDrawingArea *drawing_area,
712 cairo_t *cr,
713 int width,
714 int height,
715 void *user_data
716 ) {
717 struct alsa_card *card = user_data;
718
719 if (!card->routing_snks)
720 return;
721
722 GtkWidget *overlay = GTK_WIDGET(drawing_area);
723
724 for (int i = 0; i < card->routing_snks->len; i++) {
725 struct routing_snk *r_snk = &g_array_index(
726 card->routing_snks, struct routing_snk, i
727 );
728
729 if (!r_snk->elem || r_snk->elem->port_category != PC_MIX)
730 continue;
731
732 if (!is_routing_snk_enabled(r_snk) || !should_display_snk(r_snk))
733 continue;
734
735 if (r_snk->mixer_label_top)
736 draw_snk_label(cr, r_snk->mixer_label_top, overlay, FALSE);
737
738 if (r_snk->mixer_label_bottom && card->pref_show_bottom_right_labels)
739 draw_snk_label(cr, r_snk->mixer_label_bottom, overlay, TRUE);
740 }
741 }
742
743 static void set_rotated_label_text(
744 GtkWidget *widget,
745 const char *text
746 ) {
747 struct rotated_label_info *info = g_object_get_data(
748 G_OBJECT(widget), "label_info"
749 );
750 if (!info) {
751 info = g_malloc0(sizeof(struct rotated_label_info));
752 g_object_set_data_full(
753 G_OBJECT(widget), "label_info",
754 info, rotated_label_info_free
755 );
756 }
757
758 g_free(info->text);
759 info->text = g_strdup(text);
760
761 // invalidate glow cache (dimensions may have changed)
762 if (info->glow_cache) {
763 cairo_surface_destroy(info->glow_cache);
764 info->glow_cache = NULL;
765 }
766
767 // trigger redraw of label overlay
768 struct alsa_card *card = g_object_get_data(G_OBJECT(widget), "card");
769 if (card && card->mixer_label_overlay)
770 gtk_widget_queue_draw(card->mixer_label_overlay);
771
772 // measure text using the overlay's Pango context (system theme font)
773 // to calculate the required box height for this rotation angle
774 if (!card || !card->mixer_label_overlay)
775 return;
776
777 PangoContext *pango_ctx =
778 gtk_widget_get_pango_context(card->mixer_label_overlay);
779 PangoLayout *layout = pango_layout_new(pango_ctx);
780
781 // measure with bold weight so the reserved height covers
782 // the hover state where text becomes bold
783 PangoFontDescription *bold = pango_font_description_copy(
784 pango_context_get_font_description(pango_ctx)
785 );
786 pango_font_description_set_weight(bold, PANGO_WEIGHT_BOLD);
787 pango_layout_set_font_description(layout, bold);
788 pango_font_description_free(bold);
789
790 pango_layout_set_text(layout, text, -1);
791
792 pango_layout_get_pixel_size(layout, &info->text_w, &info->text_h);
793 g_object_unref(layout);
794
795 // use the wider of text width and glow bar extent for height;
796 // width uses text only (glow overflows horizontally by design)
797 double glow_w = MIXER_GLOW_FULL_EXTENT;
798 double effective_w = MAX(info->text_w, glow_w);
799
800 int required_h = (int)ceil(
801 effective_w * LABEL_SIN +
802 info->text_h * LABEL_COS
803 );
804 gtk_widget_set_size_request(widget, 1, required_h);
805 }
806
807 static void set_rotated_label_hover(
808 GtkWidget *widget,
809 gboolean hover
810 ) {
811 struct rotated_label_info *info = g_object_get_data(
812 G_OBJECT(widget), "label_info"
813 );
814 if (info) {
815 info->hover = hover;
816
817 // trigger redraw of label overlay
818 struct alsa_card *card = g_object_get_data(G_OBJECT(widget), "card");
819 if (card && card->mixer_label_overlay)
820 gtk_widget_queue_draw(card->mixer_label_overlay);
821 }
822 }
823
824 static GtkWidget *create_rotated_label(
825 const char *text,
826 struct alsa_card *card
827 ) {
828 // create box for positioning; size set by set_rotated_label_text
829 GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
830
831 g_object_set_data(G_OBJECT(box), "card", card);
832 set_rotated_label_text(box, text);
833
834 return box;
835 }
836
837 static void mixer_gain_enter(
838 GtkEventControllerMotion *controller,
839 double x, double y,
840 gpointer user_data
841 ) {
842 GtkWidget *widget = GTK_WIDGET(user_data);
843 GtkWidget *mix_left = g_object_get_data(G_OBJECT(widget), "mix_label_left");
844 GtkWidget *mix_right = g_object_get_data(G_OBJECT(widget), "mix_label_right");
845 GtkWidget *source_top = g_object_get_data(G_OBJECT(widget), "source_label_top");
846 GtkWidget *source_bottom = g_object_get_data(G_OBJECT(widget), "source_label_bottom");
847
848 if (mix_left)
849 gtk_widget_add_css_class(mix_left, "mixer-label-hover");
850 if (mix_right)
851 gtk_widget_add_css_class(mix_right, "mixer-label-hover");
852 if (source_top)
853 set_rotated_label_hover(source_top, TRUE);
854 if (source_bottom)
855 set_rotated_label_hover(source_bottom, TRUE);
856 }
857
858 static void mixer_gain_leave(
859 GtkEventControllerMotion *controller,
860 gpointer user_data
861 ) {
862 GtkWidget *widget = GTK_WIDGET(user_data);
863 GtkWidget *mix_left = g_object_get_data(G_OBJECT(widget), "mix_label_left");
864 GtkWidget *mix_right = g_object_get_data(G_OBJECT(widget), "mix_label_right");
865 GtkWidget *source_top = g_object_get_data(G_OBJECT(widget), "source_label_top");
866 GtkWidget *source_bottom = g_object_get_data(G_OBJECT(widget), "source_label_bottom");
867
868 if (mix_left)
869 gtk_widget_remove_css_class(mix_left, "mixer-label-hover");
870 if (mix_right)
871 gtk_widget_remove_css_class(mix_right, "mixer-label-hover");
872 if (source_top)
873 set_rotated_label_hover(source_top, FALSE);
874 if (source_bottom)
875 set_rotated_label_hover(source_bottom, FALSE);
876 }
877
878 static void add_mixer_hover_controller(GtkWidget *widget) {
879 GtkEventController *motion = gtk_event_controller_motion_new();
880 g_signal_connect(motion, "enter", G_CALLBACK(mixer_gain_enter), widget);
881 g_signal_connect(motion, "leave", G_CALLBACK(mixer_gain_leave), widget);
882 gtk_widget_add_controller(widget, motion);
883 }
884
885 static struct routing_snk *get_mixer_r_snk(
886 struct alsa_card *card,
887 int input_num
888 ) {
889 for (int i = 0; i < card->routing_snks->len; i++) {
890 struct routing_snk *r_snk = &g_array_index(
891 card->routing_snks, struct routing_snk, i
892 );
893 struct alsa_elem *elem = r_snk->elem;
894
895 if (elem->port_category != PC_MIX)
896 continue;
897
898 if (elem->lr_num == input_num)
899 return r_snk;
900 }
901 return NULL;
902 }
903
904 // Get mixer routing source by mix number (0-based: A=0, B=1, etc.)
905 static struct routing_src *get_mixer_r_src(
906 struct alsa_card *card,
907 int mix_num
908 ) {
909 for (int i = 0; i < card->routing_srcs->len; i++) {
910 struct routing_src *r_src = &g_array_index(
911 card->routing_srcs, struct routing_src, i
912 );
913 if (r_src->port_category == PC_MIX && r_src->port_num == mix_num)
914 return r_src;
915 }
916 return NULL;
917 }
918
919 static void populate_mixer_gain_widgets(struct alsa_card *card) {
920 int num_mixes = card->routing_in_count[PC_MIX];
921 int num_inputs = card->routing_out_count[PC_MIX];
922
923 for (int mix_num = 0; mix_num < num_mixes; mix_num++) {
924 struct routing_src *mix_src = get_mixer_r_src(card, mix_num);
925 if (!mix_src)
926 continue;
927
928 if (!should_display_src(mix_src))
929 continue;
930
931 int output_linked = is_src_linked(mix_src);
932 struct routing_src *mix_src_r =
933 output_linked ? get_src_partner(mix_src) : NULL;
934 int mix_num_r = mix_src_r ? mix_src_r->port_num : -1;
935
936 for (int input_num = 0; input_num < num_inputs; input_num++) {
937 struct routing_snk *r_snk = get_mixer_r_snk(card, input_num + 1);
938 if (!r_snk)
939 continue;
940
941 if (!should_display_snk(r_snk))
942 continue;
943
944 int input_linked = is_snk_linked(r_snk);
945 struct routing_snk *r_snk_r =
946 input_linked ? get_snk_partner(r_snk) : NULL;
947 int input_num_r = r_snk_r && r_snk_r->elem ?
948 r_snk_r->elem->lr_num - 1 : -1;
949
950 struct alsa_elem *elem_arr[4];
951 int elem_count = 0;
952
953 if (!input_linked && !output_linked) {
954 struct alsa_elem *e = card->mixer_gains[mix_num][input_num];
955 if (e)
956 elem_arr[elem_count++] = e;
957 } else if (input_linked && !output_linked) {
958 struct alsa_elem *e1 = card->mixer_gains[mix_num][input_num];
959 struct alsa_elem *e2 = card->mixer_gains[mix_num][input_num_r];
960 if (e1) elem_arr[elem_count++] = e1;
961 if (e2) elem_arr[elem_count++] = e2;
962 } else if (!input_linked && output_linked) {
963 struct alsa_elem *e1 = card->mixer_gains[mix_num][input_num];
964 struct alsa_elem *e2 = card->mixer_gains[mix_num_r][input_num];
965 if (e1) elem_arr[elem_count++] = e1;
966 if (e2) elem_arr[elem_count++] = e2;
967 } else {
968 struct alsa_elem *e1 = card->mixer_gains[mix_num][input_num];
969 struct alsa_elem *e2 = card->mixer_gains[mix_num_r][input_num_r];
970 if (e1) elem_arr[elem_count++] = e1;
971 if (e2) elem_arr[elem_count++] = e2;
972 }
973
974 if (elem_count == 0)
975 continue;
976
977 // Create the gain widget
978 GtkWidget *w;
979 if (elem_count == 1) {
980 w = make_gain_alsa_elem(
981 elem_arr[0], 1, WIDGET_GAIN_TAPER_LOG, 0, TRUE
982 );
983 } else {
984 w = make_stereo_gain_alsa_elem(
985 elem_arr, elem_count, 1,
986 WIDGET_GAIN_TAPER_LOG, 0, TRUE
987 );
988 }
989
990 if (!w)
991 continue;
992
993 // keep alive when removed from grid
994 g_object_ref(w);
995
996 struct mixer_gain_widget *mg =
997 g_malloc0(sizeof(struct mixer_gain_widget));
998 mg->widget = w;
999 mg->mix_num = mix_num;
1000 mg->input_num = input_num;
1001 mg->r_snk = r_snk;
1002 mg->elem = elem_arr[0];
1003
1004 mg->elem_count = elem_count;
1005 for (int i = 0; i < elem_count; i++)
1006 mg->elems[i] = elem_arr[i];
1007
1008 mg->r_snks[0] = r_snk;
1009 mg->r_snk_count = 1;
1010 if (input_linked && r_snk_r) {
1011 mg->r_snks[1] = r_snk_r;
1012 mg->r_snk_count = 2;
1013 }
1014
1015 card->mixer_gain_widgets =
1016 g_list_prepend(card->mixer_gain_widgets, mg);
1017
1018 // Store label references for hover effect
1019 g_object_set_data(
1020 G_OBJECT(w), "mix_label_left",
1021 mix_src->mixer_label_left
1022 );
1023 g_object_set_data(
1024 G_OBJECT(w), "mix_label_right",
1025 mix_src->mixer_label_right
1026 );
1027 g_object_set_data(
1028 G_OBJECT(w), "source_label_top",
1029 r_snk->mixer_label_top
1030 );
1031 g_object_set_data(
1032 G_OBJECT(w), "source_label_bottom",
1033 r_snk->mixer_label_bottom
1034 );
1035
1036 add_mixer_hover_controller(w);
1037 }
1038 }
1039
1040 card->mixer_gain_widgets =
1041 g_list_reverse(card->mixer_gain_widgets);
1042 }
1043
1044 GtkWidget *create_mixer_controls(struct alsa_card *card) {
1045 GtkWidget *top = gtk_frame_new(NULL);
1046 gtk_widget_add_css_class(top, "window-frame");
1047
1048 // clear any existing mixer gain widgets from previous window
1049 for (GList *l = card->mixer_gain_widgets; l != NULL; l = l->next) {
1050 struct mixer_gain_widget *mg = l->data;
1051 if (mg->widget) {
1052 cleanup_gain_widget(mg->widget);
1053 g_object_unref(mg->widget);
1054 }
1055 g_free(mg);
1056 }
1057 g_list_free(card->mixer_gain_widgets);
1058 card->mixer_gain_widgets = NULL;
1059
1060 // create overlay to hold the grid and glow layer
1061 GtkWidget *mixer_overlay = card->mixer_overlay = gtk_overlay_new();
1062 gtk_widget_add_css_class(mixer_overlay, "window-content");
1063 gtk_widget_add_css_class(mixer_overlay, "top-level-content");
1064 gtk_widget_add_css_class(mixer_overlay, "window-mixer");
1065 gtk_frame_set_child(GTK_FRAME(top), mixer_overlay);
1066
1067 // create grid as base child (determines size)
1068 GtkWidget *mixer_top = gtk_grid_new();
1069 gtk_overlay_set_child(GTK_OVERLAY(mixer_overlay), mixer_top);
1070
1071 // create unavailable overlay label (hidden by default)
1072 GtkWidget *unavail = card->mixer_unavailable_label = gtk_label_new(
1073 "Mixer unavailable at\ncurrent sample rate"
1074 );
1075 gtk_widget_add_css_class(unavail, "mixer-unavailable");
1076 gtk_widget_set_halign(unavail, GTK_ALIGN_CENTER);
1077 gtk_widget_set_valign(unavail, GTK_ALIGN_CENTER);
1078 gtk_widget_set_can_target(unavail, FALSE);
1079 gtk_widget_set_visible(unavail, FALSE);
1080 gtk_overlay_add_overlay(GTK_OVERLAY(mixer_overlay), unavail);
1081
1082 gtk_widget_set_halign(mixer_top, GTK_ALIGN_CENTER);
1083 gtk_widget_set_valign(mixer_top, GTK_ALIGN_CENTER);
1084
1085 // store the grid for later access
1086 card->mixer_grid = mixer_top;
1087
1088 // create drawing area for glow effects as underlay
1089 // use measure callback to not affect sizing
1090 card->mixer_glow = gtk_drawing_area_new();
1091 gtk_widget_set_can_target(card->mixer_glow, FALSE);
1092 gtk_drawing_area_set_draw_func(
1093 GTK_DRAWING_AREA(card->mixer_glow), draw_mixer_glow, card, NULL
1094 );
1095
1096 // insert at position 0 to be behind the grid
1097 // but GtkOverlay doesn't support ordering, so we need another approach
1098 gtk_overlay_add_overlay(GTK_OVERLAY(mixer_overlay), card->mixer_glow);
1099
1100 // lower the glow below the grid by reordering
1101 gtk_widget_insert_before(card->mixer_glow, mixer_overlay, mixer_top);
1102
1103 // create drawing area for rotated label overlay
1104 card->mixer_label_overlay = gtk_drawing_area_new();
1105 gtk_widget_set_can_target(card->mixer_label_overlay, FALSE);
1106 gtk_drawing_area_set_draw_func(
1107 GTK_DRAWING_AREA(card->mixer_label_overlay), draw_mixer_labels, card, NULL
1108 );
1109 gtk_overlay_add_overlay(GTK_OVERLAY(mixer_overlay), card->mixer_label_overlay);
1110
1111 // create the Mix X labels on the left and right of the grid
1112 for (int i = 0; i < card->routing_in_count[PC_MIX]; i++) {
1113 struct routing_src *r_src = get_mixer_r_src(card, i);
1114
1115 char *name = r_src ?
1116 get_mixer_output_label_for_mixer_window(r_src) :
1117 g_strdup_printf("Mix %c", i + 'A');
1118
1119 GtkWidget *l_left = gtk_label_new(name);
1120 gtk_label_set_ellipsize(GTK_LABEL(l_left), PANGO_ELLIPSIZE_END);
1121 gtk_label_set_max_width_chars(GTK_LABEL(l_left), 12);
1122 gtk_widget_set_size_request(l_left, MIXER_LABEL_MIN_WIDTH, -1);
1123 gtk_widget_set_tooltip_text(l_left, name);
1124 g_object_ref(l_left);
1125 gtk_grid_attach(
1126 GTK_GRID(mixer_top), l_left, 0, i + 1, 1, 1
1127 );
1128
1129 GtkWidget *l_right = gtk_label_new(name);
1130 gtk_label_set_ellipsize(GTK_LABEL(l_right), PANGO_ELLIPSIZE_END);
1131 gtk_label_set_max_width_chars(GTK_LABEL(l_right), 12);
1132 gtk_widget_set_size_request(l_right, MIXER_LABEL_MIN_WIDTH, -1);
1133 gtk_widget_set_tooltip_text(l_right, name);
1134 g_object_ref(l_right);
1135 gtk_grid_attach(
1136 GTK_GRID(mixer_top), l_right,
1137 card->routing_out_count[PC_MIX] + 1, i + 1, 1, 1
1138 );
1139
1140 g_free(name);
1141
1142 if (r_src) {
1143 r_src->mixer_label_left = l_left;
1144 r_src->mixer_label_right = l_right;
1145 }
1146 }
1147
1148 // Create corner label for mixer inputs/outputs legend
1149 card->mixer_corner_label = gtk_label_new(NULL);
1150 gtk_label_set_markup(
1151 GTK_LABEL(card->mixer_corner_label),
1152 "<span line_height=\"1.8\">Inputs →</span>\nOutputs ↓"
1153 );
1154 gtk_label_set_justify(GTK_LABEL(card->mixer_corner_label), GTK_JUSTIFY_CENTER);
1155 gtk_widget_set_valign(card->mixer_corner_label, GTK_ALIGN_END);
1156 gtk_widget_add_css_class(card->mixer_corner_label, "mixer-corner-label");
1157 g_object_ref(card->mixer_corner_label);
1158
1159 // spacer to reserve width for the rightmost top label overflow
1160 card->mixer_right_spacer = gtk_box_new(
1161 GTK_ORIENTATION_HORIZONTAL, 0
1162 );
1163 g_object_ref(card->mixer_right_spacer);
1164
1165 // Create all mixer input labels upfront (top and bottom)
1166 for (int i = 0; i < card->routing_snks->len; i++) {
1167 struct routing_snk *r_snk = &g_array_index(
1168 card->routing_snks, struct routing_snk, i
1169 );
1170
1171 if (!r_snk->elem || r_snk->elem->port_category != PC_MIX)
1172 continue;
1173
1174 // Create the labels if they don't exist
1175 if (!r_snk->mixer_label_top) {
1176 r_snk->mixer_label_top = create_rotated_label("", card);
1177 r_snk->mixer_label_bottom = create_rotated_label("", card);
1178 g_object_ref(r_snk->mixer_label_top);
1179 g_object_ref(r_snk->mixer_label_bottom);
1180
1181 // Attach to grid initially (repositioned during rebuild)
1182 int input_num = r_snk->elem->lr_num - 1;
1183 gtk_grid_attach(
1184 GTK_GRID(mixer_top), r_snk->mixer_label_top,
1185 input_num + 1, 0, 1, 1
1186 );
1187 gtk_grid_attach(
1188 GTK_GRID(mixer_top), r_snk->mixer_label_bottom,
1189 input_num + 1, card->routing_in_count[PC_MIX] + 1, 1, 1
1190 );
1191 }
1192 }
1193
1194 populate_mixer_gain_widgets(card);
1195
1196 update_mixer_labels(card);
1197
1198 // rebuild grid layout based on port enable states
1199 rebuild_mixer_grid(card);
1200
1201 // set initial availability state
1202 int available = get_sample_rate_category(card->current_sample_rate) != SR_HIGH;
1203 update_mixer_availability(card, available);
1204
1205 return top;
1206 }
1207
1208 void update_mixer_labels(struct alsa_card *card) {
1209 // Update mixer output labels (Mix A, Mix B, etc.) for stereo linking
1210 for (int i = 0; i < card->routing_srcs->len; i++) {
1211 struct routing_src *r_src = &g_array_index(
1212 card->routing_srcs, struct routing_src, i
1213 );
1214
1215 if (r_src->port_category != PC_MIX)
1216 continue;
1217
1218 // Skip R channel of linked pair
1219 if (!should_display_src(r_src))
1220 continue;
1221
1222 char *name;
1223 if (is_src_linked(r_src)) {
1224 name = get_src_pair_display_name(r_src);
1225 } else {
1226 name = get_mixer_output_label_for_mixer_window(r_src);
1227 }
1228
1229 if (r_src->mixer_label_left) {
1230 gtk_label_set_text(GTK_LABEL(r_src->mixer_label_left), name);
1231 gtk_widget_set_tooltip_text(r_src->mixer_label_left, name);
1232 }
1233 if (r_src->mixer_label_right) {
1234 gtk_label_set_text(GTK_LABEL(r_src->mixer_label_right), name);
1235 gtk_widget_set_tooltip_text(r_src->mixer_label_right, name);
1236 }
1237
1238 g_free(name);
1239 }
1240
1241 // Update mixer input labels (source names)
1242 for (int i = 0; i < card->routing_snks->len; i++) {
1243 struct routing_snk *r_snk = &g_array_index(
1244 card->routing_snks, struct routing_snk, i
1245 );
1246 struct alsa_elem *elem = r_snk->elem;
1247
1248 if (elem->port_category != PC_MIX)
1249 continue;
1250
1251 // Skip R channel of linked pair (label handled by L channel)
1252 if (!should_display_snk(r_snk))
1253 continue;
1254
1255 int routing_src_idx = alsa_get_elem_value(elem);
1256
1257 struct routing_src *r_src = &g_array_index(
1258 card->routing_srcs, struct routing_src, routing_src_idx
1259 );
1260
1261 if (r_snk->mixer_label_top) {
1262 char *display_name;
1263
1264 // If this mixer input is linked and the connected source is also linked,
1265 // show the stereo pair name
1266 if (is_snk_linked(r_snk) && is_src_linked(r_src)) {
1267 display_name = get_src_pair_display_name(r_src);
1268 } else {
1269 display_name = g_strdup(get_routing_src_display_name(r_src));
1270 }
1271
1272 set_rotated_label_text(r_snk->mixer_label_top, display_name);
1273 set_rotated_label_text(r_snk->mixer_label_bottom, display_name);
1274 g_free(display_name);
1275 }
1276 }
1277 }
1278
1279 // Rebuild the mixer grid layout based on current port enable states
1280 void rebuild_mixer_grid(struct alsa_card *card) {
1281 if (!card || !card->mixer_grid)
1282 return;
1283
1284 GtkGrid *grid = GTK_GRID(card->mixer_grid);
1285
1286 // Remove all widgets from the grid (but keep references)
1287 GtkWidget *child = gtk_widget_get_first_child(GTK_WIDGET(grid));
1288 while (child) {
1289 GtkWidget *next = gtk_widget_get_next_sibling(child);
1290 gtk_grid_remove(grid, child);
1291 child = next;
1292 }
1293
1294 // Build list of visible mixer outputs (sources)
1295 int visible_mix_count = 0;
1296 int mix_num_to_row[MAX_MIX_OUT]; // map mix_num to row
1297
1298 // initialize all to -1 (not visible)
1299 for (int i = 0; i < MAX_MIX_OUT; i++)
1300 mix_num_to_row[i] = -1;
1301
1302 for (int i = 0; i < card->routing_srcs->len; i++) {
1303 struct routing_src *src = &g_array_index(
1304 card->routing_srcs, struct routing_src, i
1305 );
1306
1307 if (src->port_category != PC_MIX)
1308 continue;
1309
1310 // bounds check
1311 if (src->port_num < 0 || src->port_num >= MAX_MIX_OUT)
1312 continue;
1313
1314 // Visible if enabled AND should be displayed (not R of linked pair)
1315 if (is_routing_src_enabled(src) && should_display_src(src)) {
1316 mix_num_to_row[src->port_num] = visible_mix_count;
1317 visible_mix_count++;
1318 } else {
1319 mix_num_to_row[src->port_num] = -1; // not visible
1320 }
1321 }
1322
1323 // Build list of visible mixer inputs (sinks)
1324 int visible_input_count = 0;
1325 int max_mixer_inputs = card->routing_out_count[PC_MIX]; // actual number of mixer inputs
1326 int input_num_to_col[max_mixer_inputs]; // map input_num to column
1327
1328 // initialize all to -1 (not visible)
1329 for (int i = 0; i < max_mixer_inputs; i++)
1330 input_num_to_col[i] = -1;
1331
1332 for (int i = 0; i < card->routing_snks->len; i++) {
1333 struct routing_snk *snk = &g_array_index(
1334 card->routing_snks, struct routing_snk, i
1335 );
1336
1337 if (!snk->elem || snk->elem->port_category != PC_MIX)
1338 continue;
1339
1340 int input_num = snk->elem->lr_num - 1; // convert to 0-based
1341
1342 // bounds check
1343 if (input_num < 0 || input_num >= max_mixer_inputs)
1344 continue;
1345
1346 // Visible if enabled AND should be displayed (not R of linked pair)
1347 int visible = is_routing_snk_enabled(snk) && should_display_snk(snk);
1348
1349 if (visible) {
1350 input_num_to_col[input_num] = visible_input_count;
1351 visible_input_count++;
1352 } else {
1353 input_num_to_col[input_num] = -1; // not visible
1354 }
1355 }
1356
1357 // Re-attach mixer output labels (left and right)
1358 int row_offset = 1; // row 0 is for rotated input labels
1359 for (int i = 0; i < card->routing_srcs->len; i++) {
1360 struct routing_src *src = &g_array_index(
1361 card->routing_srcs, struct routing_src, i
1362 );
1363
1364 if (src->port_category != PC_MIX)
1365 continue;
1366
1367 // bounds check
1368 if (src->port_num < 0 || src->port_num >= MAX_MIX_OUT)
1369 continue;
1370
1371 int row = mix_num_to_row[src->port_num];
1372 if (row < 0) // not visible
1373 continue;
1374
1375 if (src->mixer_label_left) {
1376 gtk_grid_attach(
1377 grid, src->mixer_label_left,
1378 0, row + row_offset, 1, 1
1379 );
1380 }
1381
1382 if (src->mixer_label_right && card->pref_show_bottom_right_labels) {
1383 gtk_grid_attach(
1384 grid, src->mixer_label_right,
1385 visible_input_count + 1, row + row_offset, 1, 1
1386 );
1387 }
1388 }
1389
1390 // Attach corner label
1391 if (card->mixer_corner_label) {
1392 gtk_grid_attach(grid, card->mixer_corner_label, 0, 0, 1, 1);
1393 }
1394
1395 // Re-attach mixer input labels (top and bottom)
1396 for (int i = 0; i < card->routing_snks->len; i++) {
1397 struct routing_snk *snk = &g_array_index(
1398 card->routing_snks, struct routing_snk, i
1399 );
1400
1401 if (!snk->elem || snk->elem->port_category != PC_MIX)
1402 continue;
1403
1404 int input_num = snk->elem->lr_num - 1;
1405
1406 // bounds check
1407 if (input_num < 0 || input_num >= max_mixer_inputs)
1408 continue;
1409
1410 int col = input_num_to_col[input_num];
1411 if (col < 0) // not visible
1412 continue;
1413
1414 if (snk->mixer_label_top) {
1415 gtk_grid_attach(
1416 grid, snk->mixer_label_top,
1417 col + 1, 0, 1, 1
1418 );
1419 }
1420
1421 if (snk->mixer_label_bottom && card->pref_show_bottom_right_labels) {
1422 gtk_grid_attach(
1423 grid, snk->mixer_label_bottom,
1424 col + 1, visible_mix_count + row_offset, 1, 1
1425 );
1426 }
1427 }
1428
1429 // Re-attach gain widgets
1430 for (GList *l = card->mixer_gain_widgets; l != NULL; l = l->next) {
1431 struct mixer_gain_widget *mg = l->data;
1432
1433 // bounds check before accessing arrays
1434 if (mg->mix_num < 0 || mg->mix_num >= MAX_MIX_OUT ||
1435 mg->input_num < 0 || mg->input_num >= max_mixer_inputs)
1436 continue;
1437
1438 int row = mix_num_to_row[mg->mix_num];
1439 int col = input_num_to_col[mg->input_num];
1440
1441 // Only attach if both mixer output and input are visible
1442 if (row >= 0 && col >= 0) {
1443 gtk_grid_attach(
1444 grid, mg->widget,
1445 col + 1, row + row_offset, 1, 1
1446 );
1447 }
1448 }
1449
1450 // when bottom/right labels are hidden, add a spacer to the right
1451 // so the rightmost top rotated label has room to extend
1452 if (!card->pref_show_bottom_right_labels && card->mixer_right_spacer) {
1453 // find the rightmost visible top label's text width
1454 int last_text_w = 0;
1455 int last_col = -1;
1456
1457 for (int i = 0; i < card->routing_snks->len; i++) {
1458 struct routing_snk *snk = &g_array_index(
1459 card->routing_snks, struct routing_snk, i
1460 );
1461 if (!snk->elem || snk->elem->port_category != PC_MIX)
1462 continue;
1463 int input_num = snk->elem->lr_num - 1;
1464 if (input_num < 0 || input_num >= max_mixer_inputs)
1465 continue;
1466 int col = input_num_to_col[input_num];
1467 if (col < 0 || !snk->mixer_label_top)
1468 continue;
1469 struct rotated_label_info *info = g_object_get_data(
1470 G_OBJECT(snk->mixer_label_top), "label_info"
1471 );
1472 if (info && col > last_col) {
1473 last_col = col;
1474 last_text_w = info->text_w;
1475 }
1476 }
1477
1478 int spacer_w = (int)ceil(
1479 last_text_w * LABEL_COS - 37 // 75% of 50, the minimum dial width
1480 );
1481 if (spacer_w < 0)
1482 spacer_w = 0;
1483 gtk_widget_set_size_request(
1484 card->mixer_right_spacer, spacer_w, 1
1485 );
1486 gtk_grid_attach(
1487 grid, card->mixer_right_spacer,
1488 visible_input_count + 1, 0, 1, 1
1489 );
1490 }
1491
1492 // force full relayout and redraw of overlay drawing areas
1493 if (card->mixer_overlay)
1494 gtk_widget_queue_resize(card->mixer_overlay);
1495 if (card->mixer_label_overlay)
1496 gtk_widget_queue_draw(card->mixer_label_overlay);
1497 if (card->mixer_glow)
1498 gtk_widget_queue_draw(card->mixer_glow);
1499 }
1500
1501 // Update mixer window availability indication
1502 void update_mixer_availability(struct alsa_card *card, int available) {
1503 if (!card->mixer_grid)
1504 return;
1505
1506 gtk_widget_set_opacity(card->mixer_grid, available ? 1.0 : 0.3);
1507
1508 if (card->mixer_unavailable_label)
1509 gtk_widget_set_visible(card->mixer_unavailable_label, !available);
1510 }
1511
1512 // Recreate mixer widgets when stereo state changes
1513 // This destroys existing widgets and creates new ones based on current stereo
1514 void recreate_mixer_widgets(struct alsa_card *card) {
1515 if (!card || !card->mixer_grid)
1516 return;
1517
1518 GtkGrid *grid = GTK_GRID(card->mixer_grid);
1519
1520 // Clear existing mixer gain widgets
1521 for (GList *l = card->mixer_gain_widgets; l != NULL; l = l->next) {
1522 struct mixer_gain_widget *mg = l->data;
1523 if (mg->widget) {
1524 if (gtk_widget_get_parent(mg->widget) == GTK_WIDGET(grid))
1525 gtk_grid_remove(grid, mg->widget);
1526 cleanup_gain_widget(mg->widget);
1527 g_object_unref(mg->widget);
1528 }
1529 g_free(mg);
1530 }
1531 g_list_free(card->mixer_gain_widgets);
1532 card->mixer_gain_widgets = NULL;
1533
1534 populate_mixer_gain_widgets(card);
1535
1536 update_mixer_labels(card);
1537 rebuild_mixer_grid(card);
1538 }