Debian Codegraph @

src/window-startup.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 "device-reset-config.h"
5 #include "device-update-firmware.h"
6 #include "fcp-socket.h"
7 #include "gtkhelper.h"
8 #include "scarlett2.h"
9 #include "scarlett2-ioctls.h"
10 #include "widget-boolean.h"
11 #include "widget-drop-down.h"
12 #include "window-startup.h"
13
14 // Wrapper for create_update_firmware_window (passes NULL for parent_label)
15 static void update_firmware_clicked(GtkWidget *w, struct alsa_card *card) {
16 create_update_firmware_window(w, card, NULL);
17 }
18
19 static GtkWidget *small_label(const char *text) {
20 GtkWidget *w = gtk_label_new(NULL);
21
22 char *s = g_strdup_printf("<b>%s</b>", text);
23 gtk_label_set_markup(GTK_LABEL(w), s);
24 free(s);
25
26 gtk_widget_set_valign(w, GTK_ALIGN_START);
27
28 return w;
29 }
30
31 static GtkWidget *big_label(const char *text) {
32 GtkWidget *view = gtk_text_view_new ();
33 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
34
35 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (view), GTK_WRAP_WORD);
36 gtk_widget_set_size_request (view, 600, -1);
37 gtk_widget_set_sensitive (view, FALSE);
38
39 gtk_text_buffer_set_text (buffer, text, -1);
40
41 return view;
42 }
43
44 static void add_sep(GtkWidget *grid, int *grid_y) {
45 if (!*grid_y)
46 return;
47
48 GtkWidget *sep = gtk_separator_new(GTK_ORIENTATION_HORIZONTAL);
49 gtk_widget_set_margin_top(sep, 10);
50 gtk_widget_set_margin_bottom(sep, 10);
51 gtk_widget_set_margin_start(sep, 20);
52 gtk_widget_set_margin_end(sep, 20);
53 gtk_grid_attach(GTK_GRID(grid), sep, 0, (*grid_y)++, 3, 1);
54 }
55
56 static void add_standalone_control(
57 GPtrArray *elems,
58 GtkWidget *grid,
59 int *grid_y
60 ) {
61 struct alsa_elem *standalone = get_elem_by_name(elems, "Standalone Switch");
62
63 if (!standalone)
64 return;
65
66 add_sep(grid, grid_y);
67
68 GtkWidget *w;
69
70 w = small_label("Standalone");
71 gtk_grid_attach(GTK_GRID(grid), w, 0, *grid_y, 1, 1);
72
73 w = make_boolean_alsa_elem(standalone, "Disabled", "Enabled");
74 gtk_widget_set_valign(w, GTK_ALIGN_START);
75 gtk_grid_attach(GTK_GRID(grid), w, 0, *grid_y + 1, 1, 1);
76
77 w = big_label(
78 "When Standalone mode is enabled, the interface will continue to "
79 "route audio as per the previous routing and mixer settings "
80 "after it has been disconnected from a computer. By configuring "
81 "the routing between the hardware and mixer inputs and outputs "
82 "appropriately, the interface can act as a standalone preamp or "
83 "mixer."
84 );
85 gtk_grid_attach(GTK_GRID(grid), w, 1, *grid_y, 1, 2);
86
87 *grid_y += 2;
88 }
89
90 static void add_phantom_persistence_control(
91 GPtrArray *elems,
92 GtkWidget *grid,
93 int *grid_y
94 ) {
95 struct alsa_elem *phantom = get_elem_by_name(
96 elems, "Phantom Power Persistence Capture Switch"
97 );
98
99 if (!phantom)
100 return;
101
102 add_sep(grid, grid_y);
103
104 GtkWidget *w;
105
106 w = small_label("Phantom Power Persistence");
107 gtk_grid_attach(GTK_GRID(grid), w, 0, *grid_y, 1, 1);
108
109 w = make_boolean_alsa_elem(phantom, "Disabled", "Enabled");
110 gtk_widget_set_valign(w, GTK_ALIGN_START);
111 gtk_grid_attach(GTK_GRID(grid), w, 0, *grid_y + 1, 1, 1);
112
113 w = big_label(
114 "When Phantom Power Persistence is enabled, the interface will "
115 "restore the previous Phantom Power/48V setting when the "
116 "interface is turned on. For the safety of microphones which can "
117 "be damaged by phantom power, the interface defaults to having "
118 "phantom power disabled when it is turned on."
119 );
120 gtk_grid_attach(GTK_GRID(grid), w, 1, *grid_y, 1, 2);
121
122 *grid_y += 2;
123 }
124
125 static int add_msd_control(
126 GPtrArray *elems,
127 GtkWidget *grid,
128 int *grid_y
129 ) {
130 struct alsa_elem *msd = get_elem_by_name(
131 elems, "MSD Mode Switch"
132 );
133
134 if (!msd)
135 return 0;
136
137 add_sep(grid, grid_y);
138
139 GtkWidget *w;
140
141 w = small_label("MSD (Mass Storage Device) Mode");
142 gtk_grid_attach(GTK_GRID(grid), w, 0, *grid_y, 1, 1);
143
144 w = make_boolean_alsa_elem(msd, "Disabled", "Enabled");
145 gtk_widget_set_valign(w, GTK_ALIGN_START);
146 gtk_grid_attach(GTK_GRID(grid), w, 0, *grid_y + 1, 1, 1);
147
148 w = big_label(
149 "When MSD Mode is enabled (as it is from the factory), the "
150 "interface has reduced functionality. You’ll want to have this "
151 "disabled. On the other hand, when MSD Mode is enabled, the "
152 "interface presents itself as a Mass Storage Device (like a USB "
153 "stick), containing a link to the Focusrite web site encouraging "
154 "you to register your product and download the proprietary "
155 "drivers which can’t be used on Linux."
156 );
157 gtk_grid_attach(GTK_GRID(grid), w, 1, *grid_y, 1, 2);
158
159 *grid_y += 2;
160
161 return 1;
162 }
163
164 // Check if any enum item contains the given substring
165 static int has_mode_option(struct alsa_elem *elem, const char *substring) {
166 int count = alsa_get_item_count(elem);
167
168 for (int i = 0; i < count; i++) {
169 char *name = alsa_get_item_name(elem, i);
170 if (name && strstr(name, substring)) {
171 free(name);
172 return 1;
173 }
174 free(name);
175 }
176 return 0;
177 }
178
179 static int add_spdif_mode_control(
180 GPtrArray *elems,
181 GtkWidget *grid,
182 int *grid_y
183 ) {
184 const char * const mode_names[] = {
185 "S/PDIF Mode",
186 "Digital I/O Mode",
187 NULL
188 };
189
190 struct alsa_elem *spdif = NULL;
191 int i = 0;
192
193 while (mode_names[i]) {
194 if ((spdif = get_elem_by_prefix(elems, mode_names[i])))
195 break;
196
197 i++;
198 }
199
200 if (!spdif)
201 return 0;
202
203 add_sep(grid, grid_y);
204
205 GtkWidget *w;
206
207 w = small_label(mode_names[i]);
208 gtk_grid_attach(GTK_GRID(grid), w, 0, *grid_y, 1, 1);
209
210 w = make_drop_down_alsa_elem(spdif, NULL);
211 gtk_widget_set_valign(w, GTK_ALIGN_START);
212 gtk_grid_attach(GTK_GRID(grid), w, 0, *grid_y + 1, 1, 1);
213
214 // Generate description based on actual enum options
215 const char *description;
216
217 if (i == 0) {
218 // S/PDIF Mode (Gen 3 18i8): RCA vs Optical for S/PDIF input
219 description =
220 "S/PDIF Mode selects whether the interface receives S/PDIF "
221 "input from the coaxial (RCA) connector or the optical "
222 "(TOSLINK) connector. This requires a reboot to take effect.";
223 } else {
224 // Digital I/O Mode: check which options are available
225 int has_dual_adat = has_mode_option(spdif, "Dual ADAT");
226
227 if (has_dual_adat) {
228 // Gen 3 18i20, Gen 4 18i20: RCA, Optical, and Dual ADAT
229 description =
230 "Digital I/O Mode selects whether the interface receives "
231 "S/PDIF input from the coaxial (RCA) connector, the optical "
232 "connector, or whether Dual ADAT mode is enabled. This "
233 "requires a reboot to take effect.";
234 } else {
235 // Gen 4 16i16, 18i16: ADAT vs Optical S/PDIF only
236 description =
237 "Digital I/O Mode selects whether the optical ports are "
238 "used for ADAT or S/PDIF. In ADAT mode, S/PDIF is available "
239 "on the coaxial (RCA) connectors. In Optical S/PDIF mode, "
240 "the RCA S/PDIF input is disabled. This requires a reboot "
241 "to take effect.";
242 }
243 }
244
245 w = big_label(description);
246 gtk_grid_attach(GTK_GRID(grid), w, 1, *grid_y, 1, 2);
247
248 *grid_y += 2;
249
250 return 1;
251 }
252
253 static void add_reset_action(
254 struct alsa_card *card,
255 GtkWidget *grid,
256 int *grid_y,
257 const char *label,
258 const char *button_label,
259 const char *description,
260 GCallback callback
261 ) {
262 add_sep(grid, grid_y);
263
264 GtkWidget *w;
265
266 w = small_label(label);
267 gtk_grid_attach(GTK_GRID(grid), w, 0, *grid_y, 1, 1);
268
269 w = gtk_button_new_with_label(button_label);
270 gtk_grid_attach(GTK_GRID(grid), w, 0, *grid_y + 1, 1, 1);
271 g_signal_connect(w, "clicked", callback, card);
272
273 w = big_label(description);
274 gtk_grid_attach(GTK_GRID(grid), w, 1, *grid_y, 1, 2);
275
276 *grid_y += 2;
277 }
278
279 static void reboot_device(GtkWidget *button, struct alsa_card *card) {
280 int err = 0;
281
282 // HWDEP (Scarlett2) driver type
283 if (card->driver_type == DRIVER_TYPE_HWDEP) {
284 snd_hwdep_t *hwdep;
285
286 err = scarlett2_open_card(card->device, &hwdep);
287 if (err < 0) {
288 fprintf(stderr, "unable to open hwdep interface: %s\n", snd_strerror(err));
289 return;
290 }
291
292 err = scarlett2_reboot(hwdep);
293 if (err < 0) {
294 fprintf(stderr, "unable to reboot device: %s\n", snd_strerror(err));
295 return;
296 }
297
298 scarlett2_close(hwdep);
299
300 // Socket (FCP) driver type
301 } else if (card->driver_type == DRIVER_TYPE_SOCKET) {
302 err = fcp_socket_reboot_device(card);
303 if (err < 0)
304 fprintf(stderr, "unable to reboot device via socket\n");
305 }
306 }
307
308 static void add_reset_actions(
309 struct alsa_card *card,
310 GtkWidget *grid,
311 int *grid_y,
312 int show_reboot_option
313 ) {
314 if (card->driver_type != DRIVER_TYPE_HWDEP &&
315 card->driver_type != DRIVER_TYPE_SOCKET)
316 return;
317
318 // Add reboot action if there is a control that requires a reboot
319 // to take effect
320 if (show_reboot_option) {
321 add_reset_action(
322 card,
323 grid,
324 grid_y,
325 "Reboot Device",
326 "Reboot",
327 "Rebooting the interface will apply changes made to the "
328 "startup configuration. This will take a few seconds.",
329 G_CALLBACK(reboot_device)
330 );
331 }
332
333 // Reset Configuration
334 add_reset_action(
335 card,
336 grid,
337 grid_y,
338 "Reset Configuration",
339 "Reset",
340 "Resetting the configuration will reset the interface to its "
341 "factory default settings. The firmware will be left unchanged.",
342 G_CALLBACK(create_reset_config_window)
343 );
344
345 // Update Firmware
346 struct alsa_elem *firmware_elem =
347 get_elem_by_name(card->elems, "Firmware Version");
348
349 if (!firmware_elem)
350 return;
351
352 char *s;
353
354 // FCP/Scarlett4 devices use 4-valued firmware versions
355 if (card->driver_type == DRIVER_TYPE_SOCKET && card->best_firmware_version_4) {
356 long *current = alsa_get_elem_int_values(firmware_elem);
357 if (!current)
358 return;
359
360 // Compare 4-valued versions
361 int needs_update = 0;
362 for (int i = 0; i < 4; i++) {
363 if ((uint32_t)current[i] < card->best_firmware_version_4[i]) {
364 needs_update = 1;
365 break;
366 }
367 if ((uint32_t)current[i] > card->best_firmware_version_4[i])
368 break;
369 }
370
371 if (!needs_update) {
372 g_free(current);
373 return;
374 }
375
376 s = g_strdup_printf(
377 "Updating the firmware will reset the interface to its "
378 "factory default settings and update the firmware from version "
379 "%ld.%ld.%ld.%ld to %u.%u.%u.%u.",
380 current[0], current[1], current[2], current[3],
381 card->best_firmware_version_4[0], card->best_firmware_version_4[1],
382 card->best_firmware_version_4[2], card->best_firmware_version_4[3]
383 );
384 g_free(current);
385 } else {
386 // Scarlett2 devices use single-valued firmware versions
387 int firmware_version = alsa_get_elem_value(firmware_elem);
388
389 if (firmware_version >= card->best_firmware_version)
390 return;
391
392 s = g_strdup_printf(
393 "Updating the firmware will reset the interface to its "
394 "factory default settings and update the firmware from version "
395 "%d to %d.",
396 firmware_version,
397 card->best_firmware_version
398 );
399 }
400
401 add_reset_action(
402 card,
403 grid,
404 grid_y,
405 "Update Firmware",
406 "Update",
407 s,
408 G_CALLBACK(update_firmware_clicked)
409 );
410
411 g_free(s);
412 }
413
414 static void add_no_startup_controls_msg(GtkWidget *grid) {
415 GtkWidget *w = big_label(
416 "This device/driver combo appears to have no startup controls."
417 );
418 gtk_grid_attach(GTK_GRID(grid), w, 0, 0, 1, 1);
419 }
420
421 GtkWidget *create_startup_controls(struct alsa_card *card) {
422 GPtrArray *elems = card->elems;
423
424 GtkWidget *top = gtk_frame_new(NULL);
425 gtk_widget_add_css_class(top, "window-frame");
426
427 int grid_y = 0;
428
429 GtkWidget *grid = gtk_grid_new();
430 gtk_widget_add_css_class(grid, "window-content");
431 gtk_widget_add_css_class(grid, "top-level-content");
432 gtk_widget_add_css_class(grid, "window-startup");
433 gtk_grid_set_column_spacing(GTK_GRID(grid), 20);
434 gtk_grid_set_row_spacing(GTK_GRID(grid), 10);
435 gtk_frame_set_child(GTK_FRAME(top), grid);
436
437 add_standalone_control(elems, grid, &grid_y);
438 add_phantom_persistence_control(elems, grid, &grid_y);
439 int has_msd = add_msd_control(elems, grid, &grid_y);
440 int has_spdif_mode = add_spdif_mode_control(elems, grid, &grid_y);
441 int show_reboot_option = has_msd || has_spdif_mode;
442 add_reset_actions(card, grid, &grid_y, show_reboot_option);
443
444 if (!grid_y)
445 add_no_startup_controls_msg(grid);
446
447 return top;
448 }