1 /* -*- Mode: C ; c-basic-offset: 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.
29 #include "graph_canvas.h"
30 #include "../dbus_constants.h"
34 graph_proxy_handle graph
;
36 struct list_head clients
;
41 struct list_head siblings
;
43 canvas_module_handle canvas_module
;
44 struct list_head ports
;
45 struct graph_canvas
* owner_ptr
;
50 struct list_head siblings
;
52 canvas_port_handle canvas_port
;
53 struct graph_canvas
* graph_canvas
;
59 struct graph_canvas
* graph_canvas_ptr
,
62 struct list_head
* node_ptr
;
63 struct client
* client_ptr
;
65 list_for_each(node_ptr
, &graph_canvas_ptr
->clients
)
67 client_ptr
= list_entry(node_ptr
, struct client
, siblings
);
68 if (client_ptr
->id
== id
)
80 struct client
* client_ptr
,
83 struct list_head
* node_ptr
;
84 struct port
* port_ptr
;
86 list_for_each(node_ptr
, &client_ptr
->ports
)
88 port_ptr
= list_entry(node_ptr
, struct port
, siblings
);
89 if (port_ptr
->id
== id
)
98 #define port1_ptr ((struct port *)port1_context)
99 #define port2_ptr ((struct port *)port2_context)
104 void * port1_context
,
105 void * port2_context
)
107 log_info("connect request(%"PRIu64
", %"PRIu64
")", port1_ptr
->id
, port2_ptr
->id
);
109 ASSERT(port1_ptr
->graph_canvas
== port2_ptr
->graph_canvas
);
111 graph_proxy_connect_ports(port1_ptr
->graph_canvas
->graph
, port1_ptr
->id
, port2_ptr
->id
);
116 void * port1_context
,
117 void * port2_context
)
119 log_info("disconnect request(%"PRIu64
", %"PRIu64
")", port1_ptr
->id
, port2_ptr
->id
);
121 ASSERT(port1_ptr
->graph_canvas
== port2_ptr
->graph_canvas
);
123 graph_proxy_disconnect_ports(port1_ptr
->graph_canvas
->graph
, port1_ptr
->id
, port2_ptr
->id
);
129 #define client_ptr ((struct client *)module_context)
132 module_location_changed(
133 void * module_context
,
141 log_info("module_location_changed(id = %3llu, x = %6.1f, y = %6.1f)", (unsigned long long)client_ptr
->id
, x
, y
);
143 locale
= strdup(setlocale(LC_NUMERIC
, NULL
));
146 log_error("strdup() failed for locale string");
150 setlocale(LC_NUMERIC
, "POSIX");
151 sprintf(x_str
, "%f", x
);
152 sprintf(y_str
, "%f", y
);
153 setlocale(LC_NUMERIC
, locale
);
156 graph_proxy_dict_entry_set(
157 client_ptr
->owner_ptr
->graph
,
158 GRAPH_DICT_OBJECT_TYPE_CLIENT
,
163 graph_proxy_dict_entry_set(
164 client_ptr
->owner_ptr
->graph
,
165 GRAPH_DICT_OBJECT_TYPE_CLIENT
,
177 graph_canvas_handle
* graph_canvas_handle_ptr
)
179 struct graph_canvas
* graph_canvas_ptr
;
181 graph_canvas_ptr
= malloc(sizeof(struct graph_canvas
));
182 if (graph_canvas_ptr
== NULL
)
187 if (!canvas_create(width
, height
, connect_request
, disconnect_request
, module_location_changed
, &graph_canvas_ptr
->canvas
))
189 free(graph_canvas_ptr
);
193 graph_canvas_ptr
->graph
= NULL
;
194 INIT_LIST_HEAD(&graph_canvas_ptr
->clients
);
196 *graph_canvas_handle_ptr
= (graph_canvas_handle
)graph_canvas_ptr
;
201 #define graph_canvas_ptr ((struct graph_canvas *)graph_canvas)
208 log_info("canvas::clear()");
209 canvas_clear(graph_canvas_ptr
->canvas
);
219 struct client
* client_ptr
;
228 log_info("canvas::client_appeared(%"PRIu64
", \"%s\")", id
, name
);
230 canvas_get_size(graph_canvas_ptr
->canvas
, &width
, &height
);
231 //log_debug("width %f, height %f", width, height);
233 client_ptr
= malloc(sizeof(struct client
));
234 if (client_ptr
== NULL
)
236 log_error("allocation of memory for struct client failed");
241 INIT_LIST_HEAD(&client_ptr
->ports
);
242 client_ptr
->owner_ptr
= graph_canvas_ptr
;
247 if (!graph_proxy_dict_entry_get(
248 client_ptr
->owner_ptr
->graph
,
249 GRAPH_DICT_OBJECT_TYPE_CLIENT
,
255 x
= width
/ 2 - 100 + rand() % 400;
258 if (!graph_proxy_dict_entry_get(
259 client_ptr
->owner_ptr
->graph
,
260 GRAPH_DICT_OBJECT_TYPE_CLIENT
,
266 y
= height
/ 2 - 100 + rand() % 400;
269 if (x_str
!= NULL
|| y_str
!= NULL
)
271 locale
= strdup(setlocale(LC_NUMERIC
, NULL
));
274 log_error("strdup() failed for locale string");
278 setlocale(LC_NUMERIC
, "POSIX");
281 //log_info("converting %s", x_str);
282 sscanf(x_str
, "%lf", &x
);
286 //log_info("converting %s", y_str);
287 sscanf(y_str
, "%lf", &y
);
289 setlocale(LC_NUMERIC
, locale
);
304 if (!canvas_create_module(graph_canvas_ptr
->canvas
, name
, x
, y
, true, true, client_ptr
, &client_ptr
->canvas_module
))
306 log_error("canvas_create_module(\"%s\") failed", name
);
311 list_add_tail(&client_ptr
->siblings
, &graph_canvas_ptr
->clients
);
320 struct client
* client_ptr
;
322 log_info("canvas::client_disappeared(%"PRIu64
")", id
);
324 client_ptr
= find_client(graph_canvas_ptr
, id
);
325 if (client_ptr
== NULL
)
327 log_error("cannot find disappearing client %"PRIu64
"", id
);
331 list_del(&client_ptr
->siblings
);
332 canvas_destroy_module(graph_canvas_ptr
->canvas
, client_ptr
->canvas_module
);
342 const char * port_name
,
348 struct client
* client_ptr
;
349 struct port
* port_ptr
;
351 log_info("canvas::port_appeared(%"PRIu64
", %"PRIu64
", \"%s\")", client_id
, port_id
, port_name
);
353 client_ptr
= find_client(graph_canvas_ptr
, client_id
);
354 if (client_ptr
== NULL
)
356 log_error("cannot find client %"PRIu64
" of appearing port %"PRIu64
" \"%s\"", client_id
, port_id
, port_name
);
360 port_ptr
= malloc(sizeof(struct port
));
361 if (port_ptr
== NULL
)
363 log_error("allocation of memory for struct port failed");
367 port_ptr
->id
= port_id
;
368 port_ptr
->graph_canvas
= graph_canvas_ptr
;
370 // Darkest tango palette colour, with S -= 6, V -= 6, w/ transparency
380 if (!canvas_create_port(graph_canvas_ptr
->canvas
, client_ptr
->canvas_module
, port_name
, is_input
, color
, port_ptr
, &port_ptr
->canvas_port
))
382 log_error("canvas_create_port(\"%s\") failed", port_name
);
387 list_add_tail(&port_ptr
->siblings
, &client_ptr
->ports
);
397 struct client
* client_ptr
;
398 struct port
* port_ptr
;
400 log_info("canvas::port_disappeared(%"PRIu64
", %"PRIu64
")", client_id
, port_id
);
402 client_ptr
= find_client(graph_canvas_ptr
, client_id
);
403 if (client_ptr
== NULL
)
405 log_error("cannot find client %"PRIu64
" of disappearing port %"PRIu64
"", client_id
, port_id
);
409 port_ptr
= find_port(client_ptr
, port_id
);
410 if (client_ptr
== NULL
)
412 log_error("cannot find disappearing port %"PRIu64
" of client %"PRIu64
"", port_id
, client_id
);
416 list_del(&port_ptr
->siblings
);
417 canvas_destroy_port(graph_canvas_ptr
->canvas
, port_ptr
->canvas_port
);
430 struct client
* client1_ptr
;
431 struct port
* port1_ptr
;
432 struct client
* client2_ptr
;
433 struct port
* port2_ptr
;
435 log_info("canvas::ports_connected(%"PRIu64
", %"PRIu64
", %"PRIu64
", %"PRIu64
")", client1_id
, port1_id
, client2_id
, port2_id
);
437 client1_ptr
= find_client(graph_canvas_ptr
, client1_id
);
438 if (client1_ptr
== NULL
)
440 log_error("cannot find client %"PRIu64
" of connected port %"PRIu64
"", client1_id
, port1_id
);
444 port1_ptr
= find_port(client1_ptr
, port1_id
);
445 if (client1_ptr
== NULL
)
447 log_error("cannot find connected port %"PRIu64
" of client %"PRIu64
"", port1_id
, client1_id
);
451 client2_ptr
= find_client(graph_canvas_ptr
, client2_id
);
452 if (client2_ptr
== NULL
)
454 log_error("cannot find client %"PRIu64
" of connected port %"PRIu64
"", client2_id
, port2_id
);
458 port2_ptr
= find_port(client2_ptr
, port2_id
);
459 if (client2_ptr
== NULL
)
461 log_error("cannot find connected port %"PRIu64
" of client %"PRIu64
"", port2_id
, client2_id
);
465 canvas_add_connection(
466 graph_canvas_ptr
->canvas
,
467 port1_ptr
->canvas_port
,
468 port2_ptr
->canvas_port
,
469 canvas_get_port_color(port1_ptr
->canvas_port
) + 0x22222200);
481 struct client
* client1_ptr
;
482 struct port
* port1_ptr
;
483 struct client
* client2_ptr
;
484 struct port
* port2_ptr
;
486 log_info("canvas::ports_disconnected(%"PRIu64
", %"PRIu64
", %"PRIu64
", %"PRIu64
")", client1_id
, port1_id
, client2_id
, port2_id
);
488 client1_ptr
= find_client(graph_canvas_ptr
, client1_id
);
489 if (client1_ptr
== NULL
)
491 log_error("cannot find client %"PRIu64
" of disconnected port %"PRIu64
"", client1_id
, port1_id
);
495 port1_ptr
= find_port(client1_ptr
, port1_id
);
496 if (client1_ptr
== NULL
)
498 log_error("cannot find disconnected port %"PRIu64
" of client %"PRIu64
"", port1_id
, client1_id
);
502 client2_ptr
= find_client(graph_canvas_ptr
, client2_id
);
503 if (client2_ptr
== NULL
)
505 log_error("cannot find client %"PRIu64
" of disconnected port %"PRIu64
"", client2_id
, port2_id
);
509 port2_ptr
= find_port(client2_ptr
, port2_id
);
510 if (client2_ptr
== NULL
)
512 log_error("cannot find disconnected port %"PRIu64
" of client %"PRIu64
"", port2_id
, client2_id
);
516 canvas_remove_connection(
517 graph_canvas_ptr
->canvas
,
518 port1_ptr
->canvas_port
,
519 port2_ptr
->canvas_port
);
523 graph_canvas_destroy(
524 graph_canvas_handle graph_canvas
)
526 if (graph_canvas_ptr
->graph
!= NULL
)
528 graph_canvas_detach(graph_canvas
);
531 free(graph_canvas_ptr
);
536 graph_canvas_handle graph_canvas
,
537 graph_proxy_handle graph
)
539 ASSERT(graph_canvas_ptr
->graph
== NULL
);
541 if (!graph_proxy_attach(
555 graph_canvas_ptr
->graph
= graph
;
562 graph_canvas_handle graph_canvas
)
564 ASSERT(graph_canvas_ptr
->graph
!= NULL
);
565 graph_proxy_detach(graph_canvas_ptr
->graph
, graph_canvas
);
566 graph_canvas_ptr
->graph
= NULL
;
570 graph_canvas_get_canvas(
571 graph_canvas_handle graph_canvas
)
573 return graph_canvas_ptr
->canvas
;