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