Debian Codegraph @

src/device-reset-config.c

raw symbols SCIP
1 // SPDX-FileCopyrightText: 2024-2025 Geoffrey D. Bennett <[email protected]>
2 // SPDX-License-Identifier: GPL-3.0-or-later
3
4 #include <gtk/gtk.h>
5 #include "device-reset-config.h"
6 #include "fcp-socket.h"
7 #include "optional-state.h"
8 #include "scarlett2.h"
9 #include "scarlett2-ioctls.h"
10 #include "window-modal.h"
11
12 static gpointer update_progress(
13 struct modal_data *modal_data,
14 char *text,
15 int progress
16 ) {
17 struct progress_data *progress_data = g_new0(struct progress_data, 1);
18 progress_data->modal_data = modal_data;
19 progress_data->text = text;
20 progress_data->progress = progress;
21
22 g_main_context_invoke(NULL, modal_update_progress, progress_data);
23 return NULL;
24 }
25
26 // Progress callback for FCP socket
27 static void fcp_progress_callback(int percent, void *user_data) {
28 struct modal_data *modal_data = user_data;
29 update_progress(modal_data, NULL, percent);
30 }
31
32 // HWDEP (Scarlett2) reset config implementation
33 static gpointer reset_config_hwdep(struct modal_data *modal_data) {
34 snd_hwdep_t *hwdep = NULL;
35
36 int err = scarlett2_open_card(modal_data->card->device, &hwdep);
37 if (err < 0) {
38 return update_progress(
39 modal_data,
40 g_strdup_printf("Unable to open hwdep interface: %s", snd_strerror(err)),
41 -1
42 );
43 }
44
45 err = scarlett2_erase_config(hwdep);
46 if (err < 0) {
47 scarlett2_close(hwdep);
48 return update_progress(
49 modal_data,
50 g_strdup_printf("Unable to reset configuration: %s", snd_strerror(err)),
51 -1
52 );
53 }
54
55 while (1) {
56 g_usleep(50000);
57
58 err = scarlett2_get_erase_progress(hwdep);
59 if (err < 0) {
60 scarlett2_close(hwdep);
61 return update_progress(
62 modal_data,
63 g_strdup_printf("Unable to get erase progress: %s", snd_strerror(err)),
64 -1
65 );
66 }
67 if (err == 255)
68 break;
69
70 update_progress(modal_data, NULL, err);
71 }
72
73 g_main_context_invoke(NULL, modal_start_reboot_progress, modal_data);
74 scarlett2_reboot(hwdep);
75 scarlett2_close(hwdep);
76
77 return NULL;
78 }
79
80 // FCP socket reset config implementation
81 static gpointer reset_config_socket(struct modal_data *modal_data) {
82 int err = fcp_socket_reset_config(
83 modal_data->card, fcp_progress_callback, modal_data
84 );
85 if (err < 0) {
86 return update_progress(
87 modal_data,
88 g_strdup("Unable to reset configuration via FCP socket"),
89 -1
90 );
91 }
92
93 g_main_context_invoke(NULL, modal_start_reboot_progress, modal_data);
94 fcp_socket_reboot_device(modal_data->card);
95
96 return NULL;
97 }
98
99 gpointer reset_config_thread(gpointer user_data) {
100 struct modal_data *modal_data = user_data;
101
102 update_progress(modal_data, g_strdup("Resetting configuration..."), 0);
103
104 // Remove the application config file for this device
105 optional_state_remove(modal_data->card->serial);
106
107 if (modal_data->card->driver_type == DRIVER_TYPE_HWDEP)
108 return reset_config_hwdep(modal_data);
109
110 if (modal_data->card->driver_type == DRIVER_TYPE_SOCKET)
111 return reset_config_socket(modal_data);
112
113 return update_progress(
114 modal_data,
115 g_strdup("Unsupported driver type for reset configuration"),
116 -1
117 );
118 }
119
120 static void join_thread(gpointer thread) {
121 g_thread_join(thread);
122 }
123
124 static void reset_config_yes_callback(struct modal_data *modal_data) {
125 GThread *thread = g_thread_new(
126 "reset_config_thread", reset_config_thread, modal_data
127 );
128 g_object_set_data_full(
129 G_OBJECT(modal_data->button_box), "thread", thread, join_thread
130 );
131 }
132
133 void create_reset_config_window(GtkWidget *w, struct alsa_card *card) {
134 create_modal_window(
135 w, card,
136 "Confirm Reset Configuration",
137 "Resetting Configuration",
138 "Are you sure you want to reset the configuration?",
139 reset_config_yes_callback
140 );
141 }