Debian Codegraph @

src/menu.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 "about.h"
5 #include "file.h"
6 #include "menu.h"
7 #include "optional-state.h"
8 #include "window-hardware.h"
9 #include "window-configuration.h"
10
11 // mapping of action names to window offsets within struct alsa_card
12 struct window_action {
13 const char *action_name;
14 size_t window_offset;
15 };
16
17 static const struct window_action window_actions[] = {
18 { "routing", offsetof(struct alsa_card, window_routing) },
19 { "mixer", offsetof(struct alsa_card, window_mixer) },
20 { "levels", offsetof(struct alsa_card, window_levels) },
21 { "configuration", offsetof(struct alsa_card, window_configuration) },
22 { "startup", offsetof(struct alsa_card, window_startup) },
23 { "dsp", offsetof(struct alsa_card, window_dsp) },
24 { "preferences", offsetof(struct alsa_card, window_preferences) },
25 { NULL, 0 }
26 };
27
28 static GtkWidget *get_card_window(struct alsa_card *card, size_t offset) {
29 return *(GtkWidget **)((char *)card + offset);
30 }
31
32 static const struct window_action *find_window_action(const char *action_name) {
33 for (const struct window_action *entry = window_actions;
34 entry->action_name;
35 entry++)
36 if (strcmp(entry->action_name, action_name) == 0)
37 return entry;
38 return NULL;
39 }
40
41 static char *make_window_key(const char *action_name) {
42 return g_strdup_printf("window-%s", action_name);
43 }
44
45 // set action state and window visibility (no persistence)
46 static void set_window_visible(
47 GSimpleAction *action,
48 GtkWidget *window,
49 gboolean visible
50 ) {
51 g_action_change_state(G_ACTION(action), g_variant_new_boolean(visible));
52 gtk_widget_set_visible(window, visible);
53 }
54
55 // toggle visibility; returns new state for callers that need to persist it
56 static gboolean toggle_visibility(GSimpleAction *action, GtkWidget *widget) {
57 GVariant *state = g_action_get_state(G_ACTION(action));
58 gboolean new_state = !g_variant_get_boolean(state);
59 set_window_visible(action, widget, new_state);
60 return new_state;
61 }
62
63 static void save_window_state(
64 struct alsa_card *card,
65 const char *action_name,
66 gboolean visible
67 ) {
68 char *key = make_window_key(action_name);
69 optional_state_save(card, CONFIG_SECTION_UI, key, visible ? "true" : "false");
70 g_free(key);
71 }
72
73 // toggle visibility and save state (for card windows)
74 static void activate_window(
75 GSimpleAction *action,
76 GVariant *parameter,
77 gpointer data
78 ) {
79 struct alsa_card *card = data;
80 const char *action_name = g_action_get_name(G_ACTION(action));
81
82 const struct window_action *entry = find_window_action(action_name);
83 if (!entry)
84 return;
85
86 GtkWidget *window = get_card_window(card, entry->window_offset);
87 gboolean new_state = toggle_visibility(action, window);
88 save_window_state(card, action_name, new_state);
89 }
90
91 static void activate_hardware(
92 GSimpleAction *action,
93 GVariant *parameter,
94 gpointer data
95 ) {
96 toggle_visibility(action, window_hardware);
97 }
98
99 static void activate_quit(
100 GSimpleAction *action,
101 GVariant *parameter,
102 gpointer data
103 ) {
104 g_application_quit(G_APPLICATION(data));
105 }
106
107 static const GActionEntry app_entries[] = {
108 {"hardware", activate_hardware, NULL, "false"},
109 {"quit", activate_quit},
110 };
111
112 struct menu_item {
113 const char *label;
114 const char *action_name;
115 const char *accelerators[2];
116 };
117
118 struct menu_data {
119 const char *label;
120 struct menu_item *items;
121 };
122
123 static const struct menu_data menus[] = {
124 {
125 "_File",
126 (struct menu_item[]){
127 { "_Load Configuration", "win.load", { "<Control>O", NULL } },
128 { "_Save Configuration", "win.save", { "<Control>S", NULL } },
129 { "_Interface Simulation", "win.sim", { "<Control>I", NULL } },
130 { "_Close", "window.close", { "<Control>W", NULL } },
131 { "E_xit", "app.quit", { "<Control>Q", NULL } },
132 {}
133 }
134 },
135 {
136 "_View",
137 (struct menu_item[]){
138 { "_Routing", "win.routing", { "<Control>R", NULL } },
139 { "_Mixer", "win.mixer", { "<Control>M", NULL } },
140 { "_Levels", "win.levels", { "<Control>L", NULL } },
141 { "_DSP", "win.dsp", { "<Control>D", NULL } },
142 { "_Configuration", "win.configuration", { "<Control>G", NULL } },
143 { "_Startup", "win.startup", { "<Control>T", NULL } },
144 { "_Preferences", "win.preferences", { "<Control>P", NULL } },
145 {}
146 }
147 },
148 {
149 "_Help",
150 (struct menu_item[]){
151 { "_Supported Hardware", "app.hardware", { "<Control>H", NULL } },
152 { "_About", "win.about", { "<Control>slash", NULL } },
153 {}
154 }
155 },
156 {}
157 };
158
159 static void populate_submenu(
160 GtkApplication *app,
161 GMenu *menu,
162 const struct menu_data *data
163 ) {
164 GMenu *submenu = g_menu_new();
165 g_menu_append_submenu(menu, data->label, G_MENU_MODEL(submenu));
166
167 // An empty-initialised menu_item marks the end
168 for (struct menu_item *item = data->items; item->label; item++) {
169 g_menu_append(submenu, item->label, item->action_name);
170 gtk_application_set_accels_for_action(
171 app, item->action_name, item->accelerators
172 );
173 }
174 }
175
176 GMenu *create_app_menu(GtkApplication *app) {
177 g_action_map_add_action_entries(
178 G_ACTION_MAP(app), app_entries, G_N_ELEMENTS(app_entries), app
179 );
180
181 GMenu *menu = g_menu_new();
182
183 for (const struct menu_data *menu_data = menus;
184 menu_data->label;
185 menu_data++)
186 populate_submenu(app, menu, menu_data);
187
188 return menu;
189 }
190
191 static const GActionEntry win_entries[] = {
192 {"about", activate_about},
193 {"sim", activate_sim}
194 };
195
196 void add_window_action_map(GtkWindow *w) {
197 g_action_map_add_action_entries(
198 G_ACTION_MAP(w), win_entries, G_N_ELEMENTS(win_entries), w
199 );
200 }
201
202 static const GActionEntry load_save_entries[] = {
203 {"load", activate_load},
204 {"save", activate_save}
205 };
206
207 void add_load_save_action_map(struct alsa_card *card) {
208 g_action_map_add_action_entries(
209 G_ACTION_MAP(card->window_main),
210 load_save_entries,
211 G_N_ELEMENTS(load_save_entries),
212 card
213 );
214 }
215
216 static const GActionEntry startup_entry[] = {
217 {"startup", activate_window, NULL, "false"}
218 };
219
220 void add_startup_action_map(struct alsa_card *card) {
221 g_action_map_add_action_entries(
222 G_ACTION_MAP(card->window_main),
223 startup_entry,
224 G_N_ELEMENTS(startup_entry),
225 card
226 );
227 }
228
229 static const GActionEntry mixer_entries[] = {
230 {"routing", activate_window, NULL, "false"},
231 {"mixer", activate_window, NULL, "false"}
232 };
233
234 static const GActionEntry levels_entries[] = {
235 {"levels", activate_window, NULL, "false"}
236 };
237
238 static const GActionEntry configuration_entries[] = {
239 {"configuration", activate_window, NULL, "false"}
240 };
241
242 void add_mixer_action_map(struct alsa_card *card) {
243 g_action_map_add_action_entries(
244 G_ACTION_MAP(card->window_main),
245 mixer_entries,
246 G_N_ELEMENTS(mixer_entries),
247 card
248 );
249
250 // Hide the levels menu item if there is no level meter support
251 if (card->has_levels) {
252 g_action_map_add_action_entries(
253 G_ACTION_MAP(card->window_main),
254 levels_entries,
255 G_N_ELEMENTS(levels_entries),
256 card
257 );
258 }
259
260 // Always show configuration menu
261 g_action_map_add_action_entries(
262 G_ACTION_MAP(card->window_main),
263 configuration_entries,
264 G_N_ELEMENTS(configuration_entries),
265 card
266 );
267 }
268
269 static const GActionEntry preferences_entry[] = {
270 {"preferences", activate_window, NULL, "false"}
271 };
272
273 void add_preferences_action_map(struct alsa_card *card) {
274 g_action_map_add_action_entries(
275 G_ACTION_MAP(card->window_main),
276 preferences_entry,
277 G_N_ELEMENTS(preferences_entry),
278 card
279 );
280 }
281
282 static const GActionEntry dsp_entries[] = {
283 {"dsp", activate_window, NULL, "false"}
284 };
285
286 void add_dsp_action_map(struct alsa_card *card) {
287 g_action_map_add_action_entries(
288 G_ACTION_MAP(card->window_main),
289 dsp_entries,
290 G_N_ELEMENTS(dsp_entries),
291 card
292 );
293 }
294
295 static gboolean should_restore_window(
296 GHashTable *state,
297 const char *action_name
298 ) {
299 char *key = make_window_key(action_name);
300 const char *value = g_hash_table_lookup(state, key);
301 g_free(key);
302 return value && strcmp(value, "true") == 0;
303 }
304
305 static void restore_single_window(
306 struct alsa_card *card,
307 const struct window_action *entry,
308 GHashTable *state
309 ) {
310 if (!should_restore_window(state, entry->action_name))
311 return;
312
313 GtkWidget *window = get_card_window(card, entry->window_offset);
314 GAction *action = g_action_map_lookup_action(
315 G_ACTION_MAP(card->window_main), entry->action_name
316 );
317 if (action)
318 set_window_visible(G_SIMPLE_ACTION(action), window, TRUE);
319 }
320
321 void restore_window_visibility(struct alsa_card *card) {
322 GHashTable *state = optional_state_load(card, CONFIG_SECTION_UI);
323 if (!state)
324 return;
325
326 for (const struct window_action *entry = window_actions;
327 entry->action_name;
328 entry++)
329 restore_single_window(card, entry, state);
330
331 g_hash_table_destroy(state);
332 }