Merged with trunk R1612.
[ardour.git] / gtk2_ardour / imageframe_socket_handler.h
1 /*
2     Copyright (C) 2003 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifndef __ardour_gtk_imageframe_socket_handler_h__
21 #define __ardour_gtk_imageframe_socket_handler_h__
22
23 #include <string>
24 #include <list>
25 #include "editor.h"
26 #include "ardour_image_compositor_socket.h"
27
28 class TimeAxisViewItem ;
29 class ImageFrameView ;
30 class MarkerView ;
31 class ImageFrameTimeAxisGroup ;
32
33 /**
34  * ImageFrameSocketHandler defines the handler between Ardour and an Image Compositor
35  * As this is purely visual, we do all processing within the main gtk loop via
36  * message passing through a socket.
37  *
38  */
39 class ImageFrameSocketHandler : public sigc::trackable
40 {
41         public:
42                 /**
43                  * Constructs a new ImageFrameSocketHandler to handle communication between Ardour and the Image Compositor
44                  *
45                  * @param ed the PublicEditor
46                  */
47                 ImageFrameSocketHandler(PublicEditor& ed) ;
48                 
49                 /**
50                  * Descructor
51                  * this will shutdown the socket if open
52                  */
53                 virtual ~ImageFrameSocketHandler() ;
54
55                 /**
56                  * Returns the instance of the ImageFrameSocketHandler
57                  * the instance should first be created with createInstance
58                  *
59                  * @return the instance of the ImageFrameSocketHandler
60                  */
61                 static ImageFrameSocketHandler* get_instance() ;
62                 
63                 /**
64                  * call back to handle doing the processing work
65                  * This method is added to the gdk main loop and called when there is data
66                  * upon the socket.
67                  *
68                  */
69                 static void image_socket_callback(void *arg, int32_t fd, GdkInputCondition cond) ;
70                 
71                 /**
72                  * Attempt to connect to the image compositor on the specified host and port
73                  *
74                  * @param hostIp the ip address of the image compositor host
75                  * @param port the oprt number to attemp the connection on
76                  * @return true if the connection was a succees
77                  *         false otherwise
78                  */
79                 bool connect(const char * hostIp, int32_t port) ;
80                 
81                 /**
82                  * Closes the connection to th Image Compositor
83                  *
84                  */
85                  void close_connection() ;              
86                 /**
87                  * Returns true if this ImagFrameSocketHandler is currently connected to rthe image compositor
88                  *
89                  * @return true if connected to the image compositor
90                  */
91                 bool is_connected() ;
92                 
93                 /**
94                  * Sets the tag used to describe this input within gtk
95                  * this is returned when gdk_input_add is called and is required to remove the input
96                  *
97                  * @param tag the gdk input tag of this input
98                  */
99                 void set_gdk_input_tag(int tag) ;
100                 
101                 /**
102                  * Returns the gdk input tag of this input
103                  *
104                  * @return the gdk input tag of this input
105                  * @see setGdkInputTag
106                  */
107                 int get_gdk_input_tag() ;
108                 
109                 
110                 /**
111                  * Returns the socket file descriptor
112                  *
113                  * @return the Sockt file descriptor
114                  */
115                 int get_socket_descriptor() ;
116                 
117                 
118                 //---------------------------------------------------------------------------------------//
119                 // Handle Sending messages to the Image Compositor
120                 
121                 //----------------------------
122                 // ImageFrameTimeAxis Messages
123                 
124                 /**
125                  * Sends a message stating that the named image frame time axis has been removed
126                  *
127                  * @param track_id the unique id of the removed image frame time axis
128                  * @param src the identity of the object that initiated the change
129                  */
130                 void send_imageframe_time_axis_removed(const string & track_id, void* src) ;
131                 
132                 /**
133                  * Sends a message indicating that an ImageFrameTimeAxis has been renamed
134                  *
135                  * @param new_id the new name, or Id, of the track
136                  * @param old_id the old name, or Id, of the track
137                  * @param src the identity of the object that initiated the change
138                  * @param time_axis the time axis that has changed
139                  */
140                 void send_imageframe_time_axis_renamed(const string & new_id, const string & old_id, void* src, ImageFrameTimeAxis* time_axis) ;
141                 
142                 //------------------------
143                 // MarkerTimeAxis Messages
144                 
145                 /**
146                  * Sends a message stating that the named marker time axis has been removed
147                  *
148                  * @param track_id the unique id of the removed image frame time axis
149                  * @param src the identity of the object that initiated the change
150                  */
151                 void send_marker_time_axis_removed(const string & track_id, void* src) ;
152                 
153                 /**
154                  * Sends a message indicating that an MarkerTimeAxis has been renamed
155                  *
156                  * @param new_id the new name, or Id, of the track
157                  * @param old_id the old name, or Id, of the track
158                  * @param src the identity of the object that initiated the change
159                  * @param time_axis the time axis that has changed
160                  */
161                 void send_marker_time_axis_renamed(const string & new_id, const string & old_id, void* src, MarkerTimeAxis* time_axis) ;
162                 
163                 
164                 //---------------------------------
165                 // ImageFrameTimeAxisGroup Messages
166                 
167                 /**
168                  * Sends a message stating that the group has been removed
169                  *
170                  * @param group_id the unique id of the removed image frame time axis
171                  * @param src the identity of the object that initiated the change
172                  * @param group the group that has changed
173                  */
174                 void send_imageframe_time_axis_group_removed(const string & group_id, void* src, ImageFrameTimeAxisGroup* group) ;
175                 
176                 /**
177                  * Send a message indicating that an ImageFrameTimeAxisGroup has been renamed
178                  *
179                  * @param new_id the new name, or Id, of the group
180                  * @param old_id the old name, or Id, of the group
181                  * @param src the identity of the object that initiated the change
182                  * @param group the group that has changed
183                  */
184                 void send_imageframe_time_axis_group_renamed(const string & new_id, const string & old_id, void* src, ImageFrameTimeAxisGroup* group) ;
185
186
187                 //---------------------------------
188                 // ImageFrameView Messages
189                 
190                 /**
191                  * Send an Image Frame View Item position changed message
192                  *
193                  * @param pos the new position value
194                  * @param src the identity of the object that initiated the change
195                  * @param item the time axis item whos position has changed
196                  */
197                 void send_imageframe_view_position_change(nframes_t pos, void* src, ImageFrameView* item) ;
198                 
199                 /**
200                  * Send a Image Frame View item duration changed message
201                  *
202                  * @param dur the the new duration value
203                  * @param src the identity of the object that initiated the change
204                  * @param item the item which has had a duration change
205                  */
206                 void send_imageframe_view_duration_change(nframes_t dur, void* src, ImageFrameView* item) ;
207                 
208                 /**
209                  * Send a message indicating that an ImageFrameView has been renamed
210                  *
211                  * @param item the ImageFrameView which has been renamed
212                  * @param src the identity of the object that initiated the change
213                  * @param item the renamed item
214                  */
215                 void send_imageframe_view_renamed(const string & new_id, const string & old_id, void* src, ImageFrameView* item) ;
216                 
217                 /**
218                  * Send a message indicating that an ImageFrameView item has been removed message
219                  *
220                  * @param item_id the id of the item that was removed
221                  * @param src the identity of the object that initiated the change
222                  * @param item the removed item
223                  */
224                 void send_imageframe_view_removed(const string & item_id, void* src, ImageFrameView* item) ;
225                 
226                 //---------------------------------
227                 // MarkerView Messages
228                 
229                 /**
230                  * Send a Marker View Item position changed message
231                  *
232                  * @param pos the new position value
233                  * @param src the identity of the object that initiated the change
234                  * @param item the time axis item whos position has changed
235                  */
236                 void send_marker_view_position_change(nframes_t pos, void* src, MarkerView* item) ;
237                 
238                 /**
239                  * Send a Marker View item duration changed message
240                  *
241                  * @param dur the new duration value
242                  * @param src the identity of the object that initiated the change
243                  * @param item the time axis item whos position has changed
244                  */
245                 void send_marker_view_duration_change(nframes_t dur, void* src, MarkerView* item) ;
246                 
247                 /**
248                  * Send a message indicating that a MarkerView has been renamed
249                  *
250                  * @param new_id the new_id of the object
251                  * @param old_id the old_id of the object
252                  * @param src the identity of the object that initiated the change
253                  * @param item the MarkerView which has been renamed
254                  */
255                 void send_marker_view_renamed(const string & new_id, const string & old_id, void* src, MarkerView* item) ;
256                 
257                 /**
258                  * Send a message indicating that a MarkerView  item has been removed message
259                  *
260                  * @param item_id the id of the item that was removed
261                  * @param src the identity of the object that initiated the change
262                  * @param item the MarkerView which has been removed
263                  */
264                 void send_marker_view_removed(const string & item_id, void* src, MarkerView* item) ;
265
266                 
267                 //---------------------------------------------------------------------------------------//
268                 // Emitted Signals
269                 
270                 /** Emitted if the socket connection is shutdown at the other end */
271                 sigc::signal<void> CompositorSocketShutdown ;
272                 
273                 /** Emitted as a generic error is captured from the socket connection to the animatic compositor */
274                 sigc::signal<void> CompositorSocketError ;
275                 
276                 
277         protected:
278                 
279         
280         private:
281                 /* I dont like friends :-( */
282                 friend class Editor;
283                         
284                 /**
285                  * Create an new instance of the ImageFrameSocketHandler, if one does not already exist
286                  *
287                  * @param ed the Ardour PublicEditor
288                  */
289                 static ImageFrameSocketHandler* create_instance(PublicEditor& ed) ;
290         
291                 //---------------------------------------------------------------------------------------//
292                 // Message breakdown ie avoid a big if...then...else
293         
294                 /**
295                  * Handle insert item requests
296                  *
297                  * @param msg the received message
298                  */
299                 void handle_insert_message(const char* msg) ;
300         
301                 /**
302                  * Handle remove item requests
303                  *
304                  * @param msg the received message
305                  */
306                 void handle_remove_message(const char* msg) ;
307         
308                 /**
309                  * Handle rename item requests
310                  *
311                  * @param msg the received message
312                  */
313                 void handle_rename_message(const char* msg) ;
314                 
315                 /**
316                  * Handle a request for session information
317                  *
318                  * @param msg the received message
319                  */
320                 void handle_request_data(const char* msg) ;
321                 
322                 /**
323                  * Handle the update of a particular item
324                  *
325                  * @param msg the received message
326                  */
327                 void handle_item_update_message(const char* msg) ;
328                 
329                 /**
330                  * Handle the selection of an Item
331                  *
332                  * @param msg the received message
333                  */
334                 void handle_item_selected(const char* msg) ;
335                 
336                 /**
337                  * Handle s session action message
338                  *
339                  * @param msg the received message
340                  */
341                 void handle_session_action(const char* msg) ;
342         
343                 //---------------------------------------------------------------------------------------//
344                 // handlers for specific insert procedures
345         
346                 /**
347                  * Handle the insertion of a new ImaegFrameTimeAxis
348                  *
349                  * @param msg the received message
350                  */
351                 void handle_insert_imageframe_time_axis(const char* msg) ;
352                 
353                 /**
354                  * Handle the insertion of a new MarkerTimeAxis
355                  *
356                  * @param msg the received message
357                  */
358                 void handle_insert_marker_time_axis(const char* msg) ;
359                 
360                 /**
361                  * Handle the insertion of a time axis group (a scene)
362                  *
363                  * @param msg the received message
364                  */
365                 void handle_insert_imageframe_group(const char* msg) ;
366                 
367                 /**
368                  * Handle the insertion of a new ImageFrameItem
369                  *
370                  * @param msg the received message
371                  */
372                 void handle_insert_imageframe_view(const char* msg) ;
373                 
374                 /**
375                  * Handle the insertion of a new MarkerItem
376                  *
377                  * @param msg the received message
378                  */
379                 void handle_insert_marker_view(const char* msg) ;
380         
381                 //---------------------------------------------------------------------------------------//
382                 // handlers for specific removal procedures
383         
384                 /**
385                  * Handle the removal of an ImageTimeAxis
386                  *
387                  * @param msg the received message
388                  */
389                 void handle_remove_imageframe_time_axis(const char* msg) ;
390                 
391                 /**
392                  * Handle the removal of an MarkerTimeAxis
393                  *
394                  * @param msg the received message
395                  */
396                 void handle_remove_marker_time_axis(const char* msg) ;
397                 
398                 /**
399                  * Handle the removal of an ImageFrameTimeAxisGroup
400                  *
401                  * @param msg the received message
402                  */
403                 void handle_remove_imageframe_time_axis_group(const char* msg) ;
404                 
405                 /**
406                  * Handle the removal of an ImageFrameItem
407                  *
408                  * @param msg the received message
409                  */
410                 void handle_remove_imageframe_view(const char* msg) ;
411                 
412                 /**
413                  * Handle the removal of an MarkerItem
414                  *
415                  * @param msg the received message
416                  */
417                 void handle_remove_marker_view(const char* msg) ;
418         
419                 //---------------------------------------------------------------------------------------//
420                 // handlers for the specific rename procedures
421         
422                 /**
423                  * Handle the renaming of an ImageTimeAxis
424                  *
425                  * @param msg the received message
426                  */
427                 void handle_rename_imageframe_time_axis(const char* msg) ;
428                 
429                 /**
430                  * Handle the renaming of an MarkerTimeAxis
431                  *
432                  * @param msg the received message
433                  */
434                 void handle_rename_marker_time_axis(const char* msg) ;
435                 
436                 /**
437                  * Handle the renaming of an ImageFrameItem
438                  *
439                  * @param msg the received message
440                  */
441                 void handle_rename_imageframe_time_axis_group(const char* msg) ;
442                 
443                 /**
444                  * Handle the renaming of an ImageFrameItem
445                  *
446                  * @param msg the received message
447                  */
448                 void handle_rename_imageframe_view(const char* msg) ;
449                 
450                 /**
451                  * Handle the renaming of an Marker
452                  *
453                  * @param msg the received message
454                  */
455                 void handle_rename_marker_view(const char* msg) ;
456                 
457                 //---------------------------------------------------------------------------------------//
458                 // handlers for data request
459         
460                 /**
461                  * Handle a request for the sessnio naem fo the current session
462                  * We return a failure state if no session is open
463                  *
464                  * @param msg the received message
465                  */
466                 void handle_session_name_request(const char* msg) ;
467         
468                  
469                 //---------------------------------------------------------------------------------------//
470                 // handlers for specific item update changes
471         
472                 /**
473                  * Handle ImageFrameView positional changes
474                  *
475                  * @param msg the received message
476                  */
477                 void handle_imageframe_view_position_update(const char* msg) ;
478                 
479                 /**
480                  * Handle ImageFrameView Duration changes
481                  *
482                  * @param msg the received message
483                  */
484                 void handle_imageframe_view_duration_update(const char* msg) ;
485                 
486                 /**
487                  * Handle ImageFrameView Position Lock Constraint changes
488                  *
489                  * @param msg the received message
490                  */
491                 void handle_imageframe_position_lock_update(const char* msg) ;
492                 
493                 /**
494                  * Handle ImageFrameView Maximum Duration changes
495                  *
496                  * @param msg the received message
497                  */
498                 void handle_imageframe_view_max_duration_update(const char* msg) ;
499                 
500                 /**
501                  * Handle image frame max duration enable constraint changes
502                  *
503                  * @param msg the received message
504                  */
505                 void handle_imageframe_view_max_duration_enable_update(const char* msg) ;
506                 
507                 /**
508                  * Handle ImageFrameView Minimum Duration changes
509                  *
510                  * @param msg the received message
511                  */
512                 void handle_imageframe_view_min_duration_update(const char* msg) ;
513                 
514                 /**
515                  * Handle image frame min duration enable constraint changes
516                  *
517                  * @param msg the received message
518                  */
519                 void handle_imageframe_view_min_duration_enable_update(const char* msg) ;
520         
521         
522                 /**
523                  * Handle MarkerView position changes
524                  *
525                  * @param msg the received message
526                  */
527                 void handle_marker_view_position_update(const char* msg) ;
528                 
529                 /**
530                  * Handle MarkerView duration changes
531                  *
532                  * @param msg the received message
533                  */
534                 void handle_marker_view_duration_update(const char* msg) ;
535                 
536                 /**
537                  * Handle MarkerView Position Lock Constraint changes
538                  *
539                  * @param msg the received message
540                  */
541                 void handle_marker_view_position_lock_update(const char* msg) ;
542                 
543                 /**
544                  * Handle MarkerView maximum duration changes
545                  *
546                  * @param msg the received message
547                  */
548                 void handle_marker_view_max_duration_update(const char* msg) ;
549                 
550                 /**
551                  * Handle MarkerView minimum duration changes
552                  *
553                  * @param msg the received message
554                  */
555                 void handle_marker_view_min_duration_update(const char* msg) ;
556         
557         
558
559                 //---------------------------------------------------------------------------------------//
560                 // handlers for Session Actions
561         
562                 /**
563                  * Handle the opening of a named audio session
564                  *
565                  * @param msg the received message
566                  */
567                 void handle_open_session(const char* msg) ;
568                 
569                 /**
570                  * Handle the closing of a named audio session
571                  *
572                  * @param msg the received message
573                  */
574                 void handle_closed_session(const char* msg) ;
575         
576                 //---------------------------------------------------------------------------------------//
577                 // handlers for the shutdown of the Image Compositor
578                 
579                 /**
580                  * Handle the shutdown message from the image compositor
581                  *
582                  * @param msg the received message
583                  */
584                 void handle_shutdown(const char* msg) ;
585         
586         
587                 //---------------------------------------------------------------------------------------//
588                 // convenince methods to break up messages
589         
590                 /**
591                  * Returns part of the received message as a std::string
592                  *
593                  * @param start the start character
594                  * @param num_chars the number of characters to read
595                  * @param the message to break apart
596                  * @return the sub string of the message
597                  */
598                 std::string get_message_part(int start, int32_t num_chars, const char* msg) ;
599                 
600                 
601                 /**
602                  * break up am image item description message
603                  * we break the mesage up into the parent Image Track id and size,
604                  * the parent group id and size, and the image id and size
605                  *
606                  * @param track_id
607                  * @param track_id_size
608                  * @param scene_id
609                  * @param scene_id_size
610                  * @param item_id
611                  * @param item_id_size
612                  */
613                 void decompose_imageframe_item_desc(const char* msg, int& position, std::string& track_id, int& track_id_size, std::string& scene_id, int& scene_id_size, std::string& item_id, int& item_id_size) ;
614         
615                 /**
616                  * Compose a description of the specified image frame view
617                  * The description consists of the parent track name size and name,
618                  * the parent group name size and name, and the item name size and name
619                  *
620                  * @param ifv the item to compose a description of
621                  * @param buffer the buffer to write the description
622                  */
623                 void compose_imageframe_item_desc(ImageFrameView* ifv, std::ostringstream& buffer) ;
624
625                 /**
626                  * Compose a description of the specified marker view
627                  * The description consists of the parent track name size and name,
628                  * and the item name size and name
629                  *
630                  * @param mv the item to compose a description of
631                  * @param buffer the buffer to write the description
632                  */
633                 void compose_marker_item_desc(MarkerView* mv, std::ostringstream& buffer) ;
634                 
635                 
636                 /**
637                  * Returns the ImageFrameView from the specified description
638                  * The errcode parameter is used to indicate the item which caused
639                  * an error on failure of this method
640                  * 0 = suces
641                  * 1 = the track item was not found
642                  * 2 = the group item was not found
643                  * 3 = the imageframe item was not found
644                  *
645                  * @paran track_id the track on which the item is placed
646                  * @param group_id the group in which the item is a member
647                  * @param item_id the id of the item
648                  * @param int32_t reference used for error codes on failure
649                  * @param errmsg populated with a description of the error on failure
650                  * @return the described item on success, 0 otherwise
651                  */
652                 ImageFrameView* get_imageframe_view_from_desc(const string & track_id, const string & group_ud, const string & item_id, int& errcode, std::string& errmsg) ;
653          
654                 //---------------------------------------------------------------------------------------//
655                 // Convenince Message Send Methods
656                 
657                 /**
658                  * Sends a message throught the socket
659                  *
660                  * @param msg the message to send
661                  * @return the return value of the socket call
662                  */
663                 int send_message(const string & msg) ;
664                 
665                 /**
666                  * Reads a message from the Socket
667                  *
668                  * @param msg a string to populate with the received message
669                  * @return the return value from the socket call
670                  */
671                 int read_message(std::string& msg) ;
672                 
673                 /**
674                  * Convenience method to compose and send a success messasge back to the Image Compositor
675                  *
676                  */
677                 void send_return_success() ;
678                 
679                 /**
680                  * Convenience method to compose and send a failure messasge back to the Image Compositor
681                  *
682                  * @param msg the failure message
683                  */
684                 void send_return_failure(const std::string& msg) ;
685         
686                 //---------------------------------------------------------------------------------------//
687                 // Memebr Data
688                 
689                 /** Our instance of the socket handler, singleton */
690                 static ImageFrameSocketHandler* _instance ;
691                 
692                 /** The Ardour PublicEditor */
693                 PublicEditor& thePublicEditor ;
694                 
695                 /** the socket file descriptor */
696                 int theArdourToCompositorSocket ;
697                 
698                 /** This stores the 'tag' returned from gdk_input_add, which is required for removing the input */
699                 int theGdkInputTag ;
700
701 } ; /* class ImageFrameSocketHandler */
702
703 #endif /* __ardour_gtk_imageframe_socket_handler_h__ */