93a82b63bb88a2da12b1504577ca145ca0dbde10
[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 <gtk--.h>
26 #include <list>
27 #include "editor.h"
28 #include "ardour_image_compositor_socket.h"
29
30 class TimeAxisViewItem ;
31 class ImageFrameView ;
32 class MarkerView ;
33 class ImageFrameTimeAxisGroup ;
34
35 /**
36  * ImageFrameSocketHandler defines the handler between Ardour and an Image Compositor
37  * As this is purely visual, we do all processing within the main gtk loop via
38  * message passing through a socket.
39  *
40  */
41 class ImageFrameSocketHandler : public SigC::Object
42 {
43         public:
44                 /**
45                  * Constructs a new ImageFrameSocketHandler to handle communication between Ardour and the Image Compositor
46                  *
47                  * @param ed the PublicEditor
48                  */
49                 ImageFrameSocketHandler(PublicEditor& ed) ;
50                 
51                 /**
52                  * Descructor
53                  * this will shutdown the socket if open
54                  */
55                 virtual ~ImageFrameSocketHandler() ;
56
57                 /**
58                  * Returns the instance of the ImageFrameSocketHandler
59                  * the instance should first be created with createInstance
60                  *
61                  * @return the instance of the ImageFrameSocketHandler
62                  */
63                 static ImageFrameSocketHandler* get_instance() ;
64                 
65                 /**
66                  * call back to handle doing the processing work
67                  * This method is added to the gdk main loop and called when there is data
68                  * upon the socket.
69                  *
70                  */
71                 static void image_socket_callback(void *arg, int32_t fd, GdkInputCondition cond) ;
72                 
73                 /**
74                  * Attempt to connect to the image compositor on the specified host and port
75                  *
76                  * @param hostIp the ip address of the image compositor host
77                  * @param port the oprt number to attemp the connection on
78                  * @return true if the connection was a succees
79                  *         false otherwise
80                  */
81                 bool connect(std::string hostIp, int32_t port) ;
82                 
83                 /**
84                  * Closes the connection to th Image Compositor
85                  *
86                  */
87                  void close_connection() ;              
88                 /**
89                  * Returns true if this ImagFrameSocketHandler is currently connected to rthe image compositor
90                  *
91                  * @return true if connected to the image compositor
92                  */
93                 bool is_connected() ;
94                 
95                 /**
96                  * Sets the tag used to describe this input within gtk
97                  * this is returned when gdk_input_add is called and is required to remove the input
98                  *
99                  * @param tag the gdk input tag of this input
100                  */
101                 void set_gdk_input_tag(int tag) ;
102                 
103                 /**
104                  * Returns the gdk input tag of this input
105                  *
106                  * @return the gdk input tag of this input
107                  * @see setGdkInputTag
108                  */
109                 int get_gdk_input_tag() ;
110                 
111                 
112                 /**
113                  * Returns the socket file descriptor
114                  *
115                  * @return the Sockt file descriptor
116                  */
117                 int get_socket_descriptor() ;
118                 
119                 
120                 //---------------------------------------------------------------------------------------//
121                 // Handle Sending messages to the Image Compositor
122                 
123                 //----------------------------
124                 // ImageFrameTimeAxis Messages
125                 
126                 /**
127                  * Sends a message stating that the named image frame time axis has been removed
128                  *
129                  * @param track_id the unique id of the removed image frame time axis
130                  * @param src the identity of the object that initiated the change
131                  */
132                 void send_imageframe_time_axis_removed(std::string track_id, void* src) ;
133                 
134                 /**
135                  * Sends a message indicating that an ImageFrameTimeAxis has been renamed
136                  *
137                  * @param new_id the new name, or Id, of the track
138                  * @param old_id the old name, or Id, of the track
139                  * @param src the identity of the object that initiated the change
140                  * @param time_axis the time axis that has changed
141                  */
142                 void send_imageframe_time_axis_renamed(std::string new_id, std::string old_id, void* src, ImageFrameTimeAxis* time_axis) ;
143                 
144                 //------------------------
145                 // MarkerTimeAxis Messages
146                 
147                 /**
148                  * Sends a message stating that the named marker time axis has been removed
149                  *
150                  * @param track_id the unique id of the removed image frame time axis
151                  * @param src the identity of the object that initiated the change
152                  */
153                 void send_marker_time_axis_removed(std::string track_id, void* src) ;
154                 
155                 /**
156                  * Sends a message indicating that an MarkerTimeAxis has been renamed
157                  *
158                  * @param new_id the new name, or Id, of the track
159                  * @param old_id the old name, or Id, of the track
160                  * @param src the identity of the object that initiated the change
161                  * @param time_axis the time axis that has changed
162                  */
163                 void send_marker_time_axis_renamed(std::string new_id, std::string old_id, void* src, MarkerTimeAxis* time_axis) ;
164                 
165                 
166                 //---------------------------------
167                 // ImageFrameTimeAxisGroup Messages
168                 
169                 /**
170                  * Sends a message stating that the group has been removed
171                  *
172                  * @param group_id the unique id of the removed image frame time axis
173                  * @param src the identity of the object that initiated the change
174                  * @param group the group that has changed
175                  */
176                 void send_imageframe_time_axis_group_removed(std::string group_id, void* src, ImageFrameTimeAxisGroup* group) ;
177                 
178                 /**
179                  * Send a message indicating that an ImageFrameTimeAxisGroup has been renamed
180                  *
181                  * @param new_id the new name, or Id, of the group
182                  * @param old_id the old name, or Id, of the group
183                  * @param src the identity of the object that initiated the change
184                  * @param group the group that has changed
185                  */
186                 void send_imageframe_time_axis_group_renamed(std::string new_id, std::string old_id, void* src, ImageFrameTimeAxisGroup* group) ;
187
188
189                 //---------------------------------
190                 // ImageFrameView Messages
191                 
192                 /**
193                  * Send an Image Frame View Item position changed message
194                  *
195                  * @param pos the new position value
196                  * @param src the identity of the object that initiated the change
197                  * @param item the time axis item whos position has changed
198                  */
199                 void send_imageframe_view_position_change(jack_nframes_t pos, void* src, ImageFrameView* item) ;
200                 
201                 /**
202                  * Send a Image Frame View item duration changed message
203                  *
204                  * @param dur the the new duration value
205                  * @param src the identity of the object that initiated the change
206                  * @param item the item which has had a duration change
207                  */
208                 void send_imageframe_view_duration_change(jack_nframes_t dur, void* src, ImageFrameView* item) ;
209                 
210                 /**
211                  * Send a message indicating that an ImageFrameView has been renamed
212                  *
213                  * @param item the ImageFrameView which has been renamed
214                  * @param src the identity of the object that initiated the change
215                  * @param item the renamed item
216                  */
217                 void send_imageframe_view_renamed(std::string new_id, std::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(std::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(std::string new_id, std::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(std::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::Signal0<void> CompositorSocketShutdown ;
274                 
275                 /** Emitted as a generic error is captured from the socket connection to the animatic compositor */
276                 SigC::Signal0<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 std::string& track_id, const std::string& group_ud, const std::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 std::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__ */