ladishd: clear project even if room project state is 'unloaded'. Fix for #117
[ladish.git] / gui / menu.c
blob8cd5262b6743aa7f781961e2da80fea504557c61
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2010 Nedko Arnaudov <nedko@arnaudov.name>
7 **************************************************************************
8 * This file contains menu related code
9 **************************************************************************
11 * LADI Session Handler is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * LADI Session Handler is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
23 * or write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "internal.h"
28 #include "menu.h"
29 #include "gtk_builder.h"
30 #include "studio.h"
31 #include "dynmenu.h"
33 static GtkWidget * g_menu_item_new_studio;
34 static GtkWidget * g_menu_item_start_studio;
35 static GtkWidget * g_menu_item_stop_studio;
36 static GtkWidget * g_menu_item_save_studio;
37 static GtkWidget * g_menu_item_save_as_studio;
38 static GtkWidget * g_menu_item_unload_studio;
39 static GtkWidget * g_menu_item_rename_studio;
40 static GtkWidget * g_menu_item_create_room;
41 static GtkWidget * g_menu_item_destroy_room;
42 static GtkWidget * g_menu_item_load_project;
43 static GtkWidget * g_menu_item_unload_project;
44 static GtkWidget * g_menu_item_save_project;
45 static GtkWidget * g_menu_item_save_as_project;
46 static GtkWidget * g_menu_item_daemon_exit;
47 static GtkWidget * g_menu_item_jack_configure;
48 static GtkWidget * g_menu_item_settings;
49 static GtkCheckMenuItem * g_menu_item_jack_latency_32;
50 static GtkCheckMenuItem * g_menu_item_jack_latency_64;
51 static GtkCheckMenuItem * g_menu_item_jack_latency_128;
52 static GtkCheckMenuItem * g_menu_item_jack_latency_256;
53 static GtkCheckMenuItem * g_menu_item_jack_latency_512;
54 static GtkCheckMenuItem * g_menu_item_jack_latency_1024;
55 static GtkCheckMenuItem * g_menu_item_jack_latency_2048;
56 static GtkCheckMenuItem * g_menu_item_jack_latency_4096;
57 static GtkCheckMenuItem * g_menu_item_jack_latency_8192;
58 static GtkWidget * g_menu_item_view_toolbar;
59 static GtkWidget * g_menu_item_view_raw_jack;
60 static GtkWidget * g_menu_item_start_app;
62 static bool g_latency_changing;
64 static ladish_dynmenu_handle g_recent_projects_list;
66 typedef void (* menu_request_toggle_func)(bool visible);
68 static void toggled(GtkCheckMenuItem * checkmenuitem, gpointer user_data)
70 ((menu_request_toggle_func)user_data)(gtk_check_menu_item_get_active(checkmenuitem));
73 static void buffer_size_change_request(GtkCheckMenuItem * item_ptr, gpointer user_data)
75 if (g_latency_changing)
76 { /* skip activations because of gtk_check_menu_item_set_active() called from menu_set_jack_latency() */
77 return;
80 if (!item_ptr->active)
81 { /* skip radio button deactivations, we are interested only in activations */
82 return;
85 menu_request_jack_latency_change((uint32_t)(guintptr)user_data);
88 struct ladish_recent_projects_list_closure
90 GtkMenu * menu;
91 void
92 (* callback)(
93 void * context,
94 const char * name,
95 void * data,
96 void (* data_free)());
97 void * context;
100 static void on_load_project_main_menu(const char * name, void * data)
102 log_info("Request to load project \"%s\":\"%s\"", name, (const char *)data);
103 if (!ladish_room_proxy_load_project(graph_view_get_room(get_current_view()), data))
105 error_message_box("Project load failed, please inspect logs.");
109 static void on_load_project_popup_menu(const char * name, void * data)
111 log_info("Request to load project \"%s\":\"%s\"", name, (const char *)data);
112 if (!ladish_room_proxy_load_project(graph_view_get_room(get_current_view()), data))
114 error_message_box("Project load failed, please inspect logs.");
118 #define closure_ptr ((struct ladish_recent_projects_list_closure * )context)
120 static
121 void
122 add_recent_project(
123 void * context,
124 const char * project_name,
125 const char * project_dir)
127 GtkWidget * menuitem;
129 if (closure_ptr->menu != NULL)
131 menuitem = gtk_menu_item_new_with_label(project_name);
132 g_signal_connect_data(
133 menuitem,
134 "activate",
135 (GCallback)on_load_project_popup_menu,
136 strdup(project_dir),
137 (GClosureNotify)free,
138 (GConnectFlags)0);
139 gtk_menu_shell_append(GTK_MENU_SHELL(closure_ptr->menu), menuitem);
141 else
143 closure_ptr->callback(closure_ptr->context, project_name, strdup(project_dir), NULL);
147 #undef closure_ptr
149 bool
150 get_recent_projects_list(
151 void (* callback)(
152 void * context,
153 const char * name,
154 void * data,
155 void (* data_free)()),
156 void * context)
158 struct ladish_recent_projects_list_closure closure;
160 closure.menu = NULL;
161 closure.callback = callback;
162 closure.context = context;
164 return ladish_room_proxy_get_recent_projects(graph_view_get_room(get_current_view()), 10, add_recent_project, &closure);
167 bool menu_init(void)
169 g_menu_item_new_studio = get_gtk_builder_widget("menu_item_new_studio");
170 g_menu_item_start_app = get_gtk_builder_widget("menu_item_start_app");
171 g_menu_item_start_studio = get_gtk_builder_widget("menu_item_start_studio");
172 g_menu_item_stop_studio = get_gtk_builder_widget("menu_item_stop_studio");
173 g_menu_item_save_studio = get_gtk_builder_widget("menu_item_save_studio");
174 g_menu_item_save_as_studio = get_gtk_builder_widget("menu_item_save_as_studio");
175 g_menu_item_unload_studio = get_gtk_builder_widget("menu_item_unload_studio");
176 g_menu_item_rename_studio = get_gtk_builder_widget("menu_item_rename_studio");
177 g_menu_item_create_room = get_gtk_builder_widget("menu_item_create_room");
178 g_menu_item_destroy_room = get_gtk_builder_widget("menu_item_destroy_room");
179 g_menu_item_load_project = get_gtk_builder_widget("menu_item_load_project");
180 g_menu_item_unload_project = get_gtk_builder_widget("menu_item_unload_project");
181 g_menu_item_save_project = get_gtk_builder_widget("menu_item_save_project");
182 g_menu_item_save_as_project = get_gtk_builder_widget("menu_item_save_as_project");
183 g_menu_item_daemon_exit = get_gtk_builder_widget("menu_item_daemon_exit");
184 g_menu_item_jack_configure = get_gtk_builder_widget("menu_item_jack_configure");
185 g_menu_item_settings = get_gtk_builder_widget("menu_item_settings");
186 g_menu_item_view_toolbar = get_gtk_builder_widget("menu_item_view_toolbar");
187 g_menu_item_view_raw_jack = get_gtk_builder_widget("menu_item_view_raw_jack");
189 g_menu_item_jack_latency_32 = GTK_CHECK_MENU_ITEM(get_gtk_builder_widget("menu_item_jack_latency_32"));
190 g_menu_item_jack_latency_64 = GTK_CHECK_MENU_ITEM(get_gtk_builder_widget("menu_item_jack_latency_64"));
191 g_menu_item_jack_latency_128 = GTK_CHECK_MENU_ITEM(get_gtk_builder_widget("menu_item_jack_latency_128"));
192 g_menu_item_jack_latency_256 = GTK_CHECK_MENU_ITEM(get_gtk_builder_widget("menu_item_jack_latency_256"));
193 g_menu_item_jack_latency_512 = GTK_CHECK_MENU_ITEM(get_gtk_builder_widget("menu_item_jack_latency_512"));
194 g_menu_item_jack_latency_1024 = GTK_CHECK_MENU_ITEM(get_gtk_builder_widget("menu_item_jack_latency_1024"));
195 g_menu_item_jack_latency_2048 = GTK_CHECK_MENU_ITEM(get_gtk_builder_widget("menu_item_jack_latency_2048"));
196 g_menu_item_jack_latency_4096 = GTK_CHECK_MENU_ITEM(get_gtk_builder_widget("menu_item_jack_latency_4096"));
197 g_menu_item_jack_latency_8192 = GTK_CHECK_MENU_ITEM(get_gtk_builder_widget("menu_item_jack_latency_8192"));
199 g_signal_connect(G_OBJECT(g_menu_item_view_toolbar), "toggled", G_CALLBACK(toggled), menu_request_toggle_toolbar);
200 g_signal_connect(G_OBJECT(g_menu_item_view_raw_jack), "toggled", G_CALLBACK(toggled), menu_request_toggle_raw_jack);
202 g_signal_connect(G_OBJECT(g_menu_item_new_studio), "activate", G_CALLBACK(menu_request_new_studio), NULL);
203 g_signal_connect(G_OBJECT(g_menu_item_start_studio), "activate", G_CALLBACK(menu_request_start_studio), NULL);
204 g_signal_connect(G_OBJECT(g_menu_item_stop_studio), "activate", G_CALLBACK(menu_request_stop_studio), NULL);
205 g_signal_connect(G_OBJECT(g_menu_item_unload_studio), "activate", G_CALLBACK(menu_request_unload_studio), NULL);
206 g_signal_connect(G_OBJECT(g_menu_item_save_studio), "activate", G_CALLBACK(menu_request_save_studio), NULL);
207 g_signal_connect(G_OBJECT(g_menu_item_save_as_studio), "activate", G_CALLBACK(menu_request_save_as_studio), NULL);
208 g_signal_connect(G_OBJECT(g_menu_item_rename_studio), "activate", G_CALLBACK(menu_request_rename_studio), NULL);
209 g_signal_connect(G_OBJECT(g_menu_item_daemon_exit), "activate", G_CALLBACK(menu_request_daemon_exit), NULL);
210 g_signal_connect(G_OBJECT(g_menu_item_jack_configure), "activate", G_CALLBACK(menu_request_jack_configure), NULL);
211 g_signal_connect(G_OBJECT(g_menu_item_settings), "activate", G_CALLBACK(menu_request_settings), NULL);
212 g_signal_connect(G_OBJECT(g_menu_item_start_app), "activate", G_CALLBACK(menu_request_start_app), NULL);
213 g_signal_connect(G_OBJECT(g_menu_item_create_room), "activate", G_CALLBACK(menu_request_create_room), NULL);
214 g_signal_connect(G_OBJECT(g_menu_item_destroy_room), "activate", G_CALLBACK(menu_request_destroy_room), NULL);
215 g_signal_connect(G_OBJECT(g_menu_item_load_project), "activate", G_CALLBACK(menu_request_load_project), NULL);
216 g_signal_connect(G_OBJECT(g_menu_item_unload_project), "activate", G_CALLBACK(menu_request_unload_project), NULL);
217 g_signal_connect(G_OBJECT(g_menu_item_save_project), "activate", G_CALLBACK(menu_request_save_project), NULL);
218 g_signal_connect(G_OBJECT(g_menu_item_save_as_project), "activate", G_CALLBACK(menu_request_save_as_project), NULL);
220 g_signal_connect(G_OBJECT(g_menu_item_jack_latency_32), "toggled", G_CALLBACK(buffer_size_change_request), (gpointer)32);
221 g_signal_connect(G_OBJECT(g_menu_item_jack_latency_64), "toggled", G_CALLBACK(buffer_size_change_request), (gpointer)64);
222 g_signal_connect(G_OBJECT(g_menu_item_jack_latency_128), "toggled", G_CALLBACK(buffer_size_change_request), (gpointer)128);
223 g_signal_connect(G_OBJECT(g_menu_item_jack_latency_256), "toggled", G_CALLBACK(buffer_size_change_request), (gpointer)256);
224 g_signal_connect(G_OBJECT(g_menu_item_jack_latency_512), "toggled", G_CALLBACK(buffer_size_change_request), (gpointer)512);
225 g_signal_connect(G_OBJECT(g_menu_item_jack_latency_1024), "toggled", G_CALLBACK(buffer_size_change_request), (gpointer)1024);
226 g_signal_connect(G_OBJECT(g_menu_item_jack_latency_2048), "toggled", G_CALLBACK(buffer_size_change_request), (gpointer)2048);
227 g_signal_connect(G_OBJECT(g_menu_item_jack_latency_4096), "toggled", G_CALLBACK(buffer_size_change_request), (gpointer)4096);
228 g_signal_connect(G_OBJECT(g_menu_item_jack_latency_8192), "toggled", G_CALLBACK(buffer_size_change_request), (gpointer)8192);
230 if (!ladish_dynmenu_create(
231 "menu_item_recently_loaded_projects",
232 "recently_loaded_projects_menu",
233 get_recent_projects_list,
234 "recent projects list",
235 on_load_project_main_menu,
236 &g_recent_projects_list))
238 return false;
241 return true;
244 void menu_uninit(void)
246 ladish_dynmenu_destroy(g_recent_projects_list);
249 void menu_studio_state_changed(unsigned int studio_state)
251 gtk_widget_set_sensitive(g_menu_item_start_studio, studio_state == STUDIO_STATE_STOPPED);
252 gtk_widget_set_sensitive(g_menu_item_stop_studio, studio_state == STUDIO_STATE_STARTED);
253 gtk_widget_set_sensitive(g_menu_item_save_studio, studio_state == STUDIO_STATE_STARTED);
254 gtk_widget_set_sensitive(g_menu_item_save_as_studio, studio_state == STUDIO_STATE_STARTED);
255 gtk_widget_set_sensitive(g_menu_item_unload_studio, studio_state != STUDIO_STATE_UNLOADED);
256 gtk_widget_set_sensitive(g_menu_item_rename_studio, studio_state == STUDIO_STATE_STOPPED || studio_state == STUDIO_STATE_STARTED);
257 gtk_widget_set_sensitive(g_menu_item_start_app, studio_state == STUDIO_STATE_STOPPED || studio_state == STUDIO_STATE_STARTED);
258 gtk_widget_set_sensitive(g_menu_item_create_room, studio_state == STUDIO_STATE_STOPPED || studio_state == STUDIO_STATE_STARTED);
261 void menu_set_jack_latency_items_sensivity(bool sensitive)
263 gtk_widget_set_sensitive(GTK_WIDGET(g_menu_item_jack_latency_32), sensitive);
264 gtk_widget_set_sensitive(GTK_WIDGET(g_menu_item_jack_latency_64), sensitive);
265 gtk_widget_set_sensitive(GTK_WIDGET(g_menu_item_jack_latency_128), sensitive);
266 gtk_widget_set_sensitive(GTK_WIDGET(g_menu_item_jack_latency_256), sensitive);
267 gtk_widget_set_sensitive(GTK_WIDGET(g_menu_item_jack_latency_512), sensitive);
268 gtk_widget_set_sensitive(GTK_WIDGET(g_menu_item_jack_latency_1024), sensitive);
269 gtk_widget_set_sensitive(GTK_WIDGET(g_menu_item_jack_latency_2048), sensitive);
270 gtk_widget_set_sensitive(GTK_WIDGET(g_menu_item_jack_latency_4096), sensitive);
271 gtk_widget_set_sensitive(GTK_WIDGET(g_menu_item_jack_latency_8192), sensitive);
274 bool menu_set_jack_latency(uint32_t buffer_size, bool force)
276 GtkCheckMenuItem * item_ptr;
278 switch (buffer_size)
280 case 32:
281 item_ptr = g_menu_item_jack_latency_32;
282 break;
283 case 64:
284 item_ptr = g_menu_item_jack_latency_64;
285 break;
286 case 128:
287 item_ptr = g_menu_item_jack_latency_128;
288 break;
289 case 256:
290 item_ptr = g_menu_item_jack_latency_256;
291 break;
292 case 512:
293 item_ptr = g_menu_item_jack_latency_512;
294 break;
295 case 1024:
296 item_ptr = g_menu_item_jack_latency_1024;
297 break;
298 case 2048:
299 item_ptr = g_menu_item_jack_latency_2048;
300 break;
301 case 4096:
302 item_ptr = g_menu_item_jack_latency_4096;
303 break;
304 case 8192:
305 item_ptr = g_menu_item_jack_latency_8192;
306 break;
307 default:
308 //log_error("unknown jack buffer size %"PRIu32, buffer_size);
309 return false;
312 if (force || !item_ptr->active)
314 log_info("menu_set_jack_latency() detects change");
315 g_latency_changing = true; /* latency has changed externally, don't tell jack to change it again */
316 gtk_check_menu_item_set_active(item_ptr, TRUE);
317 g_latency_changing = false;
320 return true;
323 void menu_set_toolbar_visibility(bool visible)
325 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(g_menu_item_view_toolbar), visible);
328 void menu_view_activated(bool room)
330 gtk_widget_set_sensitive(g_menu_item_destroy_room, room);
331 gtk_widget_set_sensitive(g_menu_item_load_project, room);
332 gtk_widget_set_sensitive(g_menu_item_unload_project, room);
333 gtk_widget_set_sensitive(g_menu_item_save_project, room);
334 gtk_widget_set_sensitive(g_menu_item_save_as_project, room);
335 gtk_widget_set_sensitive(get_gtk_builder_widget("menu_item_recently_loaded_projects"), room);
338 static void on_popup_menu_action_start_app(GtkWidget * menuitem, gpointer userdata)
340 menu_request_start_app();
343 static void on_popup_menu_action_create_room(GtkWidget * menuitem, gpointer userdata)
345 menu_request_create_room();
348 static void on_popup_menu_action_destroy_room(GtkWidget * menuitem, gpointer userdata)
350 menu_request_destroy_room();
353 static void on_popup_menu_action_load_project(GtkWidget * menuitem, gpointer userdata)
355 menu_request_load_project();
358 static void on_popup_menu_action_unload_project(GtkWidget * menuitem, gpointer userdata)
360 menu_request_unload_project();
363 static void on_popup_menu_action_save_project(GtkWidget * menuitem, gpointer userdata)
365 menu_request_save_project();
368 static void on_popup_menu_action_save_project_as(GtkWidget * menuitem, gpointer userdata)
370 menu_request_save_as_project();
373 void fill_view_popup_menu(GtkMenu * menu, graph_view_handle view)
375 GtkWidget * menuitem;
376 struct ladish_recent_projects_list_closure closure;
378 log_info("filling view menu...");
380 if (graph_view_get_app_supervisor(view) != NULL)
382 menuitem = gtk_menu_item_new_with_label("Run...");
383 g_signal_connect(menuitem, "activate", (GCallback)on_popup_menu_action_start_app, NULL);
384 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
387 if (is_room_view(view))
389 closure.menu = menu;
390 closure.callback = NULL;
391 closure.context = NULL;
393 menuitem = gtk_separator_menu_item_new(); /* separator */
394 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
396 if (ladish_room_proxy_get_recent_projects(graph_view_get_room(get_current_view()), 10, add_recent_project, &closure))
398 menuitem = gtk_separator_menu_item_new(); /* separator */
399 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
402 menuitem = gtk_menu_item_new_with_label("Load Project...");
403 g_signal_connect(menuitem, "activate", (GCallback)on_popup_menu_action_load_project, NULL);
404 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
406 menuitem = gtk_menu_item_new_with_label("Unload Project");
407 g_signal_connect(menuitem, "activate", (GCallback)on_popup_menu_action_unload_project, NULL);
408 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
410 menuitem = gtk_menu_item_new_with_label("Save Project...");
411 g_signal_connect(menuitem, "activate", (GCallback)on_popup_menu_action_save_project, NULL);
412 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
414 menuitem = gtk_menu_item_new_with_label("Save Project As...");
415 g_signal_connect(menuitem, "activate", (GCallback)on_popup_menu_action_save_project_as, NULL);
416 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
418 menuitem = gtk_separator_menu_item_new(); /* separator */
419 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
421 menuitem = gtk_menu_item_new_with_label("Destroy Room");
422 g_signal_connect(menuitem, "activate", (GCallback)on_popup_menu_action_destroy_room, NULL);
423 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
425 else
427 menuitem = gtk_menu_item_new_with_label("Create Room...");
428 g_signal_connect(menuitem, "activate", (GCallback)on_popup_menu_action_create_room, NULL);
429 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);