Implement graph_canvas::attach()
[ladish.git] / gui / graph_canvas.c
blob4e8e5e50c5a2cefe7ec045e3bb0ebd0426f1d09d
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2009 Nedko Arnaudov <nedko@arnaudov.name>
7 **************************************************************************
8 * This file contains implementation of graph canvas object
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 <stdlib.h>
28 #include <inttypes.h>
30 #include "graph_canvas.h"
31 #include "../common/debug.h"
33 struct graph_canvas
35 graph_handle graph;
36 canvas_handle canvas;
39 bool
40 graph_canvas_create(
41 int width,
42 int height,
43 graph_canvas_handle * graph_canvas_handle_ptr)
45 struct graph_canvas * graph_canvas_ptr;
47 graph_canvas_ptr = malloc(sizeof(struct graph_canvas));
48 if (graph_canvas_ptr == NULL)
50 return false;
53 if (!canvas_create(width, height, &graph_canvas_ptr->canvas))
55 free(graph_canvas_ptr);
56 return false;
59 graph_canvas_ptr->graph = NULL;
61 *graph_canvas_handle_ptr = (graph_canvas_handle)graph_canvas_ptr;
63 return true;
66 #define graph_canvas_ptr ((struct graph_canvas *)graph_canvas)
68 static
69 void
70 clear(
71 void * graph_canvas)
73 lash_info("canvas::clear()");
76 static
77 void
78 client_appeared(
79 void * graph_canvas,
80 uint64_t id,
81 const char * name)
83 lash_info("canvas::client_appeared(%"PRIu64", \"%s\")", id, name);
86 static
87 void
88 client_disappeared(
89 void * graph_canvas,
90 uint64_t id)
92 lash_info("canvas::client_disappeared(%"PRIu64")", id);
95 static
96 void
97 port_appeared(
98 void * graph_canvas,
99 uint64_t client_id,
100 uint64_t port_id,
101 const char * port_name,
102 bool is_input,
103 bool is_terminal,
104 bool is_midi)
106 lash_info("canvas::port_appeared(%"PRIu64", %"PRIu64", \"%s\")", client_id, port_id, port_name);
109 static
110 void
111 port_disappeared(
112 void * graph_canvas,
113 uint64_t client_id,
114 uint64_t port_id)
116 lash_info("canvas::port_disappeared(%"PRIu64", %"PRIu64")", client_id, port_id);
119 static
120 void
121 ports_connected(
122 void * graph_canvas,
123 uint64_t client1_id,
124 uint64_t port1_id,
125 uint64_t client2_id,
126 uint64_t port2_id)
128 lash_info("canvas::ports_connected(%"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64")", client1_id, port1_id, client2_id, port2_id);
131 static
132 void
133 ports_disconnected(
134 void * graph_canvas,
135 uint64_t client1_id,
136 uint64_t port1_id,
137 uint64_t client2_id,
138 uint64_t port2_id)
140 lash_info("canvas::ports_disconnected(%"PRIu64", %"PRIu64", %"PRIu64", %"PRIu64")", client1_id, port1_id, client2_id, port2_id);
143 void
144 graph_canvas_destroy(
145 graph_canvas_handle graph_canvas)
147 if (graph_canvas_ptr->graph != NULL)
149 graph_canvas_detach(graph_canvas);
152 free(graph_canvas_ptr);
155 bool
156 graph_canvas_attach(
157 graph_canvas_handle graph_canvas,
158 graph_handle graph)
160 assert(graph_canvas_ptr->graph == NULL);
162 if (!graph_attach(
163 graph,
164 graph_canvas,
165 clear,
166 client_appeared,
167 client_disappeared,
168 port_appeared,
169 port_disappeared,
170 ports_connected,
171 ports_disconnected))
173 return false;
176 graph_canvas_ptr->graph = graph;
178 return true;
181 void
182 graph_canvas_detach(
183 graph_canvas_handle graph_canvas)
185 assert(graph_canvas_ptr->graph != NULL);
186 graph_detach(graph_canvas_ptr->graph, graph_canvas);
187 graph_canvas_ptr->graph = NULL;
190 canvas_handle
191 graph_canvas_get_canvas(
192 graph_canvas_handle graph_canvas)
194 return graph_canvas_ptr->canvas;