root/gui/graph_view.c @ e74f5abccbef74c6b76383e022750bfc7ea3e488

Revision e74f5abccbef74c6b76383e022750bfc7ea3e488, 7.9 KB (checked in by Nedko Arnaudov <nedko@…>, 3 years ago)

gui: add/remove room graph views on room appear/disappear

  • Property mode set to 100644
Line 
1/* -*- Mode: C ; c-basic-offset: 2 -*- */
2/*
3 * LADI Session Handler (ladish)
4 *
5 * Copyright (C) 2009, 2010 Nedko Arnaudov <nedko@arnaudov.name>
6 *
7 **************************************************************************
8 * This file contains implementation of the graph view object
9 **************************************************************************
10 *
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.
15 *
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.
20 *
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.
25 */
26
27#include "common.h"
28#include "graph_view.h"
29#include "gtk_builder.h"
30#include "world_tree.h"
31
32struct graph_view
33{
34  struct list_head siblings;
35  char * name;
36  graph_canvas_handle graph_canvas;
37  graph_proxy_handle graph;
38  GtkWidget * canvas_widget;
39  ladish_app_supervisor_proxy_handle app_supervisor;
40};
41
42struct list_head g_views;
43
44GtkScrolledWindow * g_main_scrolledwin;
45static struct graph_view * g_current_view;
46GtkWidget * g_view_label;
47
48const char * g_view_lavel_text =
49  "If you've started ladish for the first time, you should:\n\n"
50  " 1. Create a new studio (in the menu, Studio -> New Studio)\n"
51  " 2. Configure JACK (in the menu, Tools -> Configure JACK)\n"
52  " 3. Start the studio (in the menu, Studio -> Start Studio)\n"
53  " 4. Start apps (in the menu, Application -> Run)\n"
54  " 5. Connect their ports by click & drag on canvas\n"
55  " 6. Save the studio (in the menu, Studio -> Save Studio)\n";
56
57void view_init(void)
58{
59  g_main_scrolledwin = GTK_SCROLLED_WINDOW(get_gtk_builder_widget("main_scrolledwin"));
60  INIT_LIST_HEAD(&g_views);
61
62  g_view_label = gtk_label_new(g_view_lavel_text);
63  g_object_ref(g_view_label);
64  gtk_widget_show(g_view_label);
65
66  g_current_view = NULL;
67  gtk_scrolled_window_add_with_viewport(g_main_scrolledwin, g_view_label);
68}
69
70static void app_added(void * context, uint64_t id, const char * name, bool running, bool terminal, uint8_t level)
71{
72  //log_info("app added. id=%"PRIu64", name='%s', %srunning, %s, level %u", id, name, running ? "" : "not ", terminal ? "terminal" : "shell", (unsigned int)level);
73  world_tree_add_app(context, id, name, running, terminal, level);
74}
75
76static void app_state_changed(void * context, uint64_t id, const char * name, bool running, bool terminal, uint8_t level)
77{
78  //log_info("app state changed. id=%"PRIu64", name='%s', %srunning, %s, level %u", id, name, running ? "" : "not ", terminal ? "terminal" : "shell", (unsigned int)level);
79  world_tree_app_state_changed(context, id, name, running, terminal, level);
80}
81
82static void app_removed(void * context, uint64_t id)
83{
84  //log_info("app removed. id=%"PRIu64, id);
85  world_tree_remove_app(context, id);
86}
87
88bool
89create_view(
90  const char * name,
91  const char * service,
92  const char * object,
93  bool graph_dict_supported,
94  bool app_supervisor_supported,
95  bool force_activate,
96  graph_view_handle * handle_ptr)
97{
98  struct graph_view * view_ptr;
99
100  view_ptr = malloc(sizeof(struct graph_view));
101  if (view_ptr == NULL)
102  {
103    log_error("malloc() failed for struct graph_view");
104    goto fail;
105  }
106
107  view_ptr->name = strdup(name);
108  if (view_ptr->name == NULL)
109  {
110    log_error("strdup() failed for \"%s\"", name);
111    goto free_view;
112  }
113
114  view_ptr->app_supervisor = NULL;
115
116  if (!graph_proxy_create(service, object, graph_dict_supported, &view_ptr->graph))
117  {
118    goto free_name;
119  }
120
121  if (!graph_canvas_create(1600 * 2, 1200 * 2, &view_ptr->graph_canvas))
122  {
123    goto destroy_graph;
124  }
125
126  if (!graph_canvas_attach(view_ptr->graph_canvas, view_ptr->graph))
127  {
128    goto destroy_graph_canvas;
129  }
130
131  view_ptr->canvas_widget = canvas_get_widget(graph_canvas_get_canvas(view_ptr->graph_canvas));
132
133  list_add_tail(&view_ptr->siblings, &g_views);
134
135  gtk_widget_show(view_ptr->canvas_widget);
136
137  world_tree_add((graph_view_handle)view_ptr, force_activate);
138
139  if (app_supervisor_supported)
140  {
141    if (!ladish_app_supervisor_proxy_create(service, object, view_ptr, app_added, app_state_changed, app_removed, &view_ptr->app_supervisor))
142    {
143      goto detach_graph_canvas;
144    }
145  }
146
147  if (!graph_proxy_activate(view_ptr->graph))
148  {
149    goto free_app_supervisor;
150  }
151
152  *handle_ptr = (graph_view_handle)view_ptr;
153
154  return true;
155
156free_app_supervisor:
157  if (view_ptr->app_supervisor != NULL)
158  {
159    ladish_app_supervisor_proxy_destroy(view_ptr->app_supervisor);
160  }
161detach_graph_canvas:
162  graph_canvas_detach(view_ptr->graph_canvas);
163destroy_graph_canvas:
164  graph_canvas_destroy(view_ptr->graph_canvas);
165destroy_graph:
166  graph_proxy_destroy(view_ptr->graph);
167free_name:
168  free(view_ptr->name);
169free_view:
170  free(view_ptr);
171fail:
172  return false;
173}
174
175static void attach_canvas(struct graph_view * view_ptr)
176{
177  GtkWidget * child;
178
179  child = gtk_bin_get_child(GTK_BIN(g_main_scrolledwin));
180
181  if (child == view_ptr->canvas_widget)
182  {
183    return;
184  }
185
186  if (child != NULL)
187  {
188    gtk_container_remove(GTK_CONTAINER(g_main_scrolledwin), child);
189  }
190
191  g_current_view = view_ptr;
192  gtk_container_add(GTK_CONTAINER(g_main_scrolledwin), view_ptr->canvas_widget);
193
194  //_main_scrolledwin->property_hadjustment().get_value()->set_step_increment(10);
195  //_main_scrolledwin->property_vadjustment().get_value()->set_step_increment(10);
196}
197
198static void detach_canvas(struct graph_view * view_ptr)
199{
200  GtkWidget * child;
201
202  child = gtk_bin_get_child(GTK_BIN(g_main_scrolledwin));
203  if (child == view_ptr->canvas_widget)
204  {
205    gtk_container_remove(GTK_CONTAINER(g_main_scrolledwin), view_ptr->canvas_widget);
206    g_current_view = NULL;
207    gtk_widget_show(g_view_label);
208    gtk_scrolled_window_add_with_viewport(g_main_scrolledwin, g_view_label);
209  }
210}
211
212#define view_ptr ((struct graph_view *)view)
213
214void destroy_view(graph_view_handle view)
215{
216  list_del(&view_ptr->siblings);
217  if (!list_empty(&g_views))
218  {
219    world_tree_activate((graph_view_handle)list_entry(g_views.next, struct graph_view, siblings));
220  }
221  else
222  {
223    set_main_window_title(NULL);
224  }
225
226  detach_canvas(view_ptr);
227
228  world_tree_remove(view);
229
230  graph_canvas_detach(view_ptr->graph_canvas);
231  graph_canvas_destroy(view_ptr->graph_canvas);
232  graph_proxy_destroy(view_ptr->graph);
233
234  if (view_ptr->app_supervisor != NULL)
235  {
236    ladish_app_supervisor_proxy_destroy(view_ptr->app_supervisor);
237  }
238
239  free(view_ptr->name);
240  free(view_ptr);
241}
242
243void activate_view(graph_view_handle view)
244{
245  attach_canvas(view_ptr);
246  set_main_window_title(view);
247}
248
249const char * get_view_name(graph_view_handle view)
250{
251  return view_ptr->name;
252}
253
254const char * get_view_opath(graph_view_handle view)
255{
256  return graph_proxy_get_object(view_ptr->graph);
257}
258
259bool set_view_name(graph_view_handle view, const char * cname)
260{
261  char * name;
262
263  name = strdup(cname);
264  if (name == NULL)
265  {
266    log_error("strdup() failed for \"%s\"", name);
267    return false;
268  }
269
270  free(view_ptr->name);
271  view_ptr->name = name;
272
273  world_tree_name_changed(view);
274
275  if (g_current_view == view_ptr)
276  {
277    set_main_window_title(view);
278  }
279
280  return true;
281}
282
283canvas_handle get_current_canvas()
284{
285  if (g_current_view == NULL)
286  {
287    return NULL;
288  }
289
290  return graph_canvas_get_canvas(g_current_view->graph_canvas);
291}
292
293bool app_run_custom(graph_view_handle view, const char * command, const char * name, bool run_in_terminal, uint8_t level)
294{
295  return ladish_app_supervisor_proxy_run_custom(view_ptr->app_supervisor, command, name, run_in_terminal, level);
296}
297
298ladish_app_supervisor_proxy_handle graph_view_get_app_supervisor(graph_view_handle view)
299{
300  return view_ptr->app_supervisor;
301}
Note: See TracBrowser for help on using the browser.