Debian Codegraph @

src/alsa.h

raw symbols SCIP
1 // SPDX-FileCopyrightText: 2022-2025 Geoffrey D. Bennett <[email protected]>
2 // SPDX-License-Identifier: GPL-3.0-or-later
3
4 #pragma once
5
6 #include <alsa/asoundlib.h>
7 #include <gtk/gtk.h>
8
9 #include "const.h"
10
11 // simulated cards have card->num set to -1
12 #define SIMULATED_CARD_NUM -1
13
14 // forward definitions
15 struct alsa_elem;
16 struct alsa_card;
17
18 // typedef for callbacks to update widgets when the alsa element
19 // notifies of a change
20 typedef void (AlsaElemCallback)(struct alsa_elem *, void *);
21
22 // port categories for routing_src and routing_snk entries
23 // must match the level meter ordering from the driver
24 enum {
25 PC_OFF, // Off (the source when a sink is not connected)
26 PC_HW, // Hardware inputs/outputs
27 PC_MIX, // Mixer inputs/outputs
28 PC_DSP, // DSP inputs/outputs
29 PC_PCM, // PCM inputs/outputs
30 PC_COUNT // number of port categories
31 };
32
33 // names for the port categories
34 extern const char *port_category_names[PC_COUNT];
35 extern const char *port_category_short_names[PC_COUNT];
36
37 // hardware types
38 enum {
39 HW_TYPE_ANALOGUE,
40 HW_TYPE_SPDIF,
41 HW_TYPE_ADAT,
42 HW_TYPE_COUNT
43 };
44
45 // check if hardware type is digital I/O (S/PDIF or ADAT)
46 static inline int is_digital_io_type(int hw_type) {
47 return hw_type == HW_TYPE_SPDIF || hw_type == HW_TYPE_ADAT;
48 }
49
50 // driver types
51 // NONE is 1st Gen or Scarlett2 before hwdep support was added
52 // (no erase config or firmware update support)
53 // HWDEP is the Scarlett2 driver after hwdep support was added
54 // SOCKET is the FCP driver
55 enum {
56 DRIVER_TYPE_NONE,
57 DRIVER_TYPE_HWDEP,
58 DRIVER_TYPE_SOCKET,
59 DRIVER_TYPE_SOCKET_UNINIT,
60 DRIVER_TYPE_COUNT
61 };
62
63 // names for the hardware types
64 extern const char *hw_type_names[HW_TYPE_COUNT];
65
66 // is a drag active, and whether dragging from a routing source or a
67 // routing sink
68 enum {
69 DRAG_TYPE_NONE = 0,
70 DRAG_TYPE_SRC = 1,
71 DRAG_TYPE_SNK = 2,
72 };
73
74 // entry in alsa_card routing_srcs (routing sources) array
75 // list of enums that are in the Mixer Input X Capture Enum elements
76 struct routing_src {
77
78 // pointer back to the card this entry is associated with
79 struct alsa_card *card;
80
81 // the enum id of the alsa item
82 int id;
83
84 // PC_OFF, PC_DSP, PC_MIX, PC_PCM, or PC_HW
85 int port_category;
86
87 // 0-based count within port_category
88 int port_num;
89
90 // the alsa item name
91 char *name;
92
93 // for PC_HW, the hardware type
94 int hw_type;
95
96 // the number (or translated letter; A = 1) in the item name
97 int lr_num;
98
99 // on the routing page, the box widget containing the text and the
100 // "socket" widget for this routing source
101 GtkWidget *widget;
102
103 // the socket widget
104 GtkWidget *widget2;
105
106 // the label widget (for updating text, e.g. PCM availability)
107 GtkWidget *label_widget;
108
109 // optional/simulated element for custom name
110 struct alsa_elem *custom_name_elem;
111
112 // optional/simulated element for enable/disable
113 struct alsa_elem *enable_elem;
114
115 // optional/simulated element for stereo link state (stored on left channel)
116 struct alsa_elem *link_elem;
117
118 // optional/simulated element for stereo pair name (stored on left channel)
119 struct alsa_elem *pair_name_elem;
120
121 // cached pointer to stereo partner (adjacent L/R channel)
122 struct routing_src *partner;
123
124 // cached display name (either custom or default)
125 // updated by callback when custom name changes
126 char *display_name;
127
128 // mixer window labels for mixer outputs (Mix A, Mix B, etc.)
129 GtkWidget *mixer_label_left;
130 GtkWidget *mixer_label_right;
131
132 // routing window talkback button widget (for mixer outputs with talkback)
133 GtkWidget *talkback_widget;
134
135 // talkback element for this mixer output (if talkback is supported)
136 struct alsa_elem *talkback_elem;
137
138 // cached level meter index (-1 if none)
139 int level_index;
140 };
141
142 // entry in alsa_card routing_snks (routing sinks) array for alsa
143 // elements that are routing sinks like Analogue Output 01 Playback
144 // Enum
145 struct routing_snk {
146
147 // location within the array
148 int idx;
149
150 // pointer back to the element this entry is associated with
151 struct alsa_elem *elem;
152
153 // box widget on the routing page
154 GtkWidget *box_widget;
155
156 // socket widget on the routing page
157 GtkWidget *socket_widget;
158
159 // label widget on the routing page (for updating text)
160 GtkWidget *label_widget;
161
162 // the mixer label widgets for this sink
163 GtkWidget *mixer_label_top;
164 GtkWidget *mixer_label_bottom;
165
166 // config-io pair label (stored on left channel only)
167 GtkWidget *config_io_pair_label;
168
169 // optional/simulated element for custom name
170 struct alsa_elem *custom_name_elem;
171
172 // optional/simulated element for enable/disable
173 struct alsa_elem *enable_elem;
174
175 // optional/simulated element for stereo link state (stored on left channel)
176 struct alsa_elem *link_elem;
177
178 // optional/simulated element for stereo pair name (stored on left channel)
179 struct alsa_elem *pair_name_elem;
180
181 // cached pointer to stereo partner (adjacent L/R channel)
182 struct routing_snk *partner;
183
184 // cached display name (either custom or default)
185 // updated by callback when custom name changes
186 char *display_name;
187
188 // cached effective source index (accounting for speaker switching)
189 // updated by callback when monitor group controls change
190 int effective_source_idx;
191
192 // cached left channel status (for fixed mixer inputs, based on source)
193 int is_left;
194
195 // pointers to monitor group controls (NULL if not applicable)
196 struct alsa_elem *main_group_switch;
197 struct alsa_elem *alt_group_switch;
198 struct alsa_elem *main_group_source;
199 struct alsa_elem *alt_group_source;
200 struct alsa_elem *main_group_trim;
201 struct alsa_elem *alt_group_trim;
202
203 // cached level meter index (-1 if none)
204 int level_index;
205 };
206
207 // hold one callback & its data
208 struct alsa_elem_callback {
209 AlsaElemCallback *callback;
210 void *data;
211 GDestroyNotify destroy; // cleanup function, NULL if no cleanup needed
212 };
213
214 // entry in alsa_card elems (ALSA control elements) array
215 struct alsa_elem {
216
217 // pointer back to the card
218 struct alsa_card *card;
219
220 // ALSA element information
221 int numid;
222 char *name;
223 int type;
224 int count;
225 int index;
226
227 // for gain/volume elements, the value range, dB type, and dB range
228 int min_val;
229 int max_val;
230 int dB_type;
231 int min_cdB;
232 int max_cdB;
233
234 // level meter labels
235 char **meter_labels;
236
237 // for routing sinks
238 int is_routing_snk;
239 int port_category;
240 int port_num;
241 int hw_type;
242 int lr_num;
243
244 // the callback functions for this ALSA control element
245 GList *callbacks;
246
247 // for simulated elements, the current state
248 int is_simulated;
249 int is_writable;
250 int is_volatile;
251 long value;
252 long *values; // cached multi-value integer state
253
254 // for simulated enumerated elements, the items
255 int item_count;
256 char **item_names;
257
258 // for BYTES type elements
259 void *bytes_value;
260 size_t bytes_size;
261
262 // pending idle callback for change notification
263 guint pending_idle;
264 };
265
266 struct alsa_card {
267 int num;
268 char *device;
269 uint32_t pid;
270 char *serial;
271 char *name;
272 int driver_type;
273 char *fcp_socket;
274 int best_firmware_version;
275
276 // For scarlett4 (big Gen 4) devices with 4-valued versions
277 uint32_t firmware_version_4[4];
278 uint32_t esp_firmware_version[4];
279 uint32_t *best_firmware_version_4;
280 snd_ctl_t *handle;
281 struct pollfd pfd;
282 GPtrArray *elems;
283 struct alsa_elem *sample_capture_elem;
284 struct alsa_elem *level_meter_elem;
285 double *routing_levels;
286 int routing_levels_count;
287 GArray *routing_srcs;
288 GArray *routing_snks;
289 int *monitor_group_src_map;
290 int monitor_group_src_map_count;
291 GIOChannel *io_channel;
292 guint event_source_id;
293 GtkWidget *window_main;
294 GtkWidget *window_routing;
295 GtkWidget *window_mixer;
296 GtkWidget *window_levels;
297 GtkWidget *window_configuration;
298 GtkWidget *window_startup;
299 GtkWidget *window_modal;
300 GtkWidget *window_dsp;
301 GtkWidget *window_preferences;
302 GtkWidget *window_main_contents;
303 GtkWidget *routing_grid;
304 GtkWidget *mixer_grid;
305 GtkWidget *monitor_groups_grid;
306 GList *monitor_group_cbs;
307 GList *monitor_group_gains;
308 GtkWidget *mixer_overlay;
309 GtkWidget *mixer_glow;
310 GtkWidget *mixer_label_overlay;
311 GtkWidget *mixer_right_spacer;
312 GtkWidget *mixer_unavailable_label;
313 GtkWidget *mixer_corner_label;
314 GList *mixer_gain_widgets;
315 GList *input_gain_widgets;
316 GList *output_gain_widgets;
317 GList *dsp_comp_widgets;
318 GtkWidget *routing_lines;
319 GtkWidget *routing_hw_in_grid;
320 GtkWidget *routing_hw_out_grid;
321 GtkWidget *routing_pcm_in_grid;
322 GtkWidget *routing_pcm_out_grid;
323 GtkWidget *routing_dsp_in_grid;
324 GtkWidget *routing_dsp_out_grid;
325 GtkWidget *routing_mixer_in_grid;
326 GtkWidget *routing_mixer_out_grid;
327 GtkWidget *routing_mixer_in_heading;
328 GtkWidget *routing_mixer_out_heading;
329 GtkWidget *routing_src_label;
330 GtkWidget *routing_snk_label;
331 int has_speaker_switching;
332 int has_talkback;
333 int has_fixed_mixer_inputs;
334 int mixer_has_mix_srcs;
335 int routing_out_count[PC_COUNT];
336 int routing_in_count[PC_COUNT];
337 GtkWidget *drag_line;
338 int drag_type;
339 struct routing_src *src_drag;
340 struct routing_snk *snk_drag;
341 double drag_x, drag_y;
342 struct routing_src *hovered_src;
343 struct routing_snk *hovered_snk;
344 int pending_ui_updates;
345 gboolean pending_ui_update_idle;
346 guint levels_timer;
347
348 // PCM channel availability based on sample rate
349 int playback_altset_channels[4]; // channels per altset (1-3)
350 int capture_altset_channels[4]; // channels per altset (1-3)
351 int altset_count; // number of altsets
352 int pcm_playback_channels; // current available (0=all)
353 int pcm_capture_channels; // current available (0=all)
354
355 // HW I/O availability based on digital I/O mode and sample rate
356 struct alsa_elem *digital_io_mode_elem; // "Digital I/O Mode", "S/PDIF Mode", or "S/PDIF Source"
357 int digital_io_mode; // cached mode index at init time
358 int digital_io_mode_live; // mode changes take effect immediately
359 int current_sample_rate; // current sample rate (Hz)
360 int max_spdif_in; // max S/PDIF input ports
361 int max_spdif_out; // max S/PDIF output ports
362 int max_adat_in; // max ADAT input ports
363 int max_adat_out; // max ADAT output ports
364
365 struct alsa_elem *mixer_gains[MAX_MIX_OUT][MAX_MUX_IN];
366
367 // level meter support (set at init, not persisted)
368 int has_levels;
369
370 // per-card preferences (persisted via optional-state [ui] section)
371 int pref_show_bottom_right_labels;
372 int pref_levels_interval_ms;
373
374 // levels timer callback data (for restart_levels_timer)
375 void *levels_data;
376 };
377
378 // flags for pending_ui_updates
379 #define PENDING_UI_UPDATE_MIXER_GRID (1 << 0)
380 #define PENDING_UI_UPDATE_MONITOR_GROUPS (1 << 1)
381
382 // utility
383 void fatal_alsa_error(const char *msg, int err);
384
385 // locate elements or get information about them
386 struct alsa_elem *get_elem_by_name(GPtrArray *elems, const char *name);
387 struct alsa_elem *get_elem_by_prefix(GPtrArray *elems, const char *prefix);
388 struct alsa_elem *get_elem_by_substr(GPtrArray *elems, const char *substr);
389 int get_max_elem_by_name(
390 GPtrArray *elems,
391 const char *prefix,
392 const char *needle
393 );
394
395 // add callback to alsa_elem callback list
396 void alsa_elem_add_callback(
397 struct alsa_elem *elem,
398 AlsaElemCallback *callback,
399 void *data,
400 GDestroyNotify destroy // cleanup function, NULL if no cleanup needed
401 );
402
403 // remove all callbacks with matching data pointer from an element
404 void alsa_elem_remove_callbacks_by_data(struct alsa_elem *elem, void *data);
405
406 // trigger callbacks for an element (notify of value change)
407 void alsa_elem_change(struct alsa_elem *elem);
408
409 // alsa snd_ctl_elem_*() functions
410 int alsa_get_elem_type(struct alsa_elem *elem);
411 char *alsa_get_elem_name(struct alsa_elem *elem);
412 long alsa_get_elem_value(struct alsa_elem *elem);
413 long *alsa_get_elem_int_values(struct alsa_elem *elem);
414 void alsa_set_elem_int_values(struct alsa_elem *elem, const long *values, int count);
415 void alsa_set_elem_value(struct alsa_elem *elem, long value);
416 int alsa_get_elem_writable(struct alsa_elem *elem);
417 int alsa_get_elem_volatile(struct alsa_elem *elem);
418 int alsa_get_elem_count(struct alsa_elem *elem);
419 int alsa_get_item_count(struct alsa_elem *elem);
420 char *alsa_get_item_name(struct alsa_elem *elem, int i);
421
422 // BYTES element support
423 const void *alsa_get_elem_bytes(struct alsa_elem *elem, size_t *size);
424 void alsa_set_elem_bytes(struct alsa_elem *elem, const void *data, size_t size);
425
426 // create simulated optional element
427 struct alsa_elem *alsa_create_optional_elem(
428 struct alsa_card *card,
429 const char *name,
430 int type,
431 size_t max_size
432 );
433
434 // create simulated optional enumerated element with item names
435 struct alsa_elem *alsa_create_optional_enum_elem(
436 struct alsa_card *card,
437 const char *name,
438 const char **item_names,
439 int item_count
440 );
441
442 // add to alsa_cards array
443 struct alsa_card *card_create(int card_num);
444
445 // parse elements (used by alsa-sim.c)
446 void alsa_set_lr_nums(struct alsa_card *card);
447 void alsa_get_routing_controls(struct alsa_card *card);
448 void alsa_init_mixer_gains_cache(struct alsa_card *card);
449
450 // init
451 void alsa_init(void);
452
453 // register re-open callback
454 typedef void (ReOpenCallback)(void *);
455 void alsa_register_reopen_callback(
456 const char *serial,
457 ReOpenCallback *callback,
458 void *data
459 );
460 void alsa_unregister_reopen_callback(const char *serial);
461 int alsa_has_reopen_callbacks(void);