s/stringcr_t/const string &/
[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(std::string 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 item the ImageFrameView which has been renamed
213                  * @param src the identity of the object that initiated the change
214                  * @param item the renamed item
215                  */
216                 void send_imageframe_view_renamed(const string & new_id, const string & old_id, void* src, ImageFrameView* item) ;
217                 
218                 /**
219                  * Send a message indicating that an ImageFrameView item has been removed message
220                  *
221                  * @param item_id the id of the item that was removed
222                  * @param src the identity of the object that initiated the change
223                  * @param item the removed item
224                  */
225                 void send_imageframe_view_removed(const string & item_id, void* src, ImageFrameView* item) ;
226                 
227                 //---------------------------------
228                 // MarkerView Messages
229                 
230                 /**
231                  * Send a Marker View Item position changed message
232                  *
233                  * @param pos the new position value
234                  * @param src the identity of the object that initiated the change
235                  * @param item the time axis item whos position has changed
236                  */
237                 void send_marker_view_position_change(jack_nframes_t pos, void* src, MarkerView* item) ;
238                 
239                 /**
240                  * Send a Marker View item duration changed message
241                  *
242                  * @param dur the new duration value
243                  * @param src the identity of the object that initiated the change
244                  * @param item the time axis item whos position has changed
245                  */
246                 void send_marker_view_duration_change(jack_nframes_t dur, void* src, MarkerView* item) ;
247                 
248                 /**
249                  * Send a message indicating that a MarkerView has been renamed
250                  *
251                  * @param new_id the new_id of the object
252                  * @param old_id the old_id of the object
253                  * @param src the identity of the object that initiated the change
254                  * @param item the MarkerView which has been renamed
255                  */
256                 void send_marker_view_renamed(const string & new_id, const string & old_id, void* src, MarkerView* item) ;
257                 
258                 /**
259                  * Send a message indicating that a MarkerView  item has been removed message
260                  *
261                  * @param item_id the id of the item that was removed
262                  * @param src the identity of the object that initiated the change
263                  * @param item the MarkerView which has been removed
264                  */
265                 void send_marker_view_removed(const string & item_id, void* src, MarkerView* item) ;
266
267                 
268                 //---------------------------------------------------------------------------------------//
269                 // Emitted Signals
270                 
271                 /** Emitted if the socket connection is shutdown at the other end */
272                 sigc::signal<void> CompositorSocketShutdown ;
273                 
274                 /** Emitted as a generic error is captured from the socket connection to the animatic compositor */
275                 sigc::signal<void> CompositorSocketError ;
276                 
277                 
278         protected:
279                 
280         
281         private:
282                 /* I dont like friends :-( */
283                 friend class Editor;
284                         
285                 /**
286                  * Create an new instance of the ImageFrameSocketHandler, if one does not already exist
287                  *
288                  * @param ed the Ardour PublicEditor
289                  */
290                 static ImageFrameSocketHandler* create_instance(PublicEditor& ed) ;
291         
292                 //---------------------------------------------------------------------------------------//
293                 // Message breakdown ie avoid a big if...then...else
294         
295                 /**
296                  * Handle insert item requests
297                  *
298                  * @param msg the received message
299                  */
300                 void handle_insert_message(const char* msg) ;
301         
302                 /**
303                  * Handle remove item requests
304                  *
305                  * @param msg the received message
306                  */
307                 void handle_remove_message(const char* msg) ;
308         
309                 /**
310                  * Handle rename item requests
311                  *
312                  * @param msg the received message
313                  */
314                 void handle_rename_message(const char* msg) ;
315                 
316                 /**
317                  * Handle a request for session information
318                  *
319                  * @param msg the received message
320                  */
321                 void handle_request_data(const char* msg) ;
322                 
323                 /**
324                  * Handle the update of a particular item
325                  *
326                  * @param msg the received message
327                  */
328                 void handle_item_update_message(const char* msg) ;
329                 
330                 /**
331                  * Handle the selection of an Item
332                  *
333                  * @param msg the received message
334                  */
335                 void handle_item_selected(const char* msg) ;
336                 
337                 /**
338                  * Handle s session action message
339                  *
340                  * @param msg the received message
341                  */
342                 void handle_session_action(const char* msg) ;
343         
344                 //---------------------------------------------------------------------------------------//
345                 // handlers for specific insert procedures
346         
347                 /**
348                  * Handle the insertion of a new ImaegFrameTimeAxis
349                  *
350                  * @param msg the received message
351                  */
352                 void handle_insert_imageframe_time_axis(const char* msg) ;
353                 
354                 /**
355                  * Handle the insertion of a new MarkerTimeAxis
356                  *
357                  * @param msg the received message
358                  */
359                 void handle_insert_marker_time_axis(const char* msg) ;
360                 
361                 /**
362                  * Handle the insertion of a time axis group (a scene)
363                  *
364                  * @param msg the received message
365                  */
366                 void handle_insert_imageframe_group(const char* msg) ;
367                 
368                 /**
369                  * Handle the insertion of a new ImageFrameItem
370                  *
371                  * @param msg the received message
372                  */
373                 void handle_insert_imageframe_view(const char* msg) ;
374                 
375                 /**
376                  * Handle the insertion of a new MarkerItem
377                  *
378                  * @param msg the received message
379                  */
380                 void handle_insert_marker_view(const char* msg) ;
381         
382                 //---------------------------------------------------------------------------------------//
383                 // handlers for specific removal procedures
384         
385                 /**
386                  * Handle the removal of an ImageTimeAxis
387                  *
388                  * @param msg the received message
389                  */
390                 void handle_remove_imageframe_time_axis(const char* msg) ;
391                 
392                 /**
393                  * Handle the removal of an MarkerTimeAxis
394                  *
395                  * @param msg the received message
396                  */
397                 void handle_remove_marker_time_axis(const char* msg) ;
398                 
399                 /**
400                  * Handle the removal of an ImageFrameTimeAxisGroup
401                  *
402                  * @param msg the received message
403                  */
404                 void handle_remove_imageframe_time_axis_group(const char* msg) ;
405                 
406                 /**
407                  * Handle the removal of an ImageFrameItem
408                  *
409                  * @param msg the received message
410                  */
411                 void handle_remove_imageframe_view(const char* msg) ;
412                 
413                 /**
414                  * Handle the removal of an MarkerItem
415                  *
416                  * @param msg the received message
417                  */
418                 void handle_remove_marker_view(const char* msg) ;
419         
420                 //---------------------------------------------------------------------------------------//
421                 // handlers for the specific rename procedures
422         
423                 /**
424                  * Handle the renaming of an ImageTimeAxis
425                  *
426                  * @param msg the received message
427                  */
428                 void handle_rename_imageframe_time_axis(const char* msg) ;
429                 
430                 /**
431                  * Handle the renaming of an MarkerTimeAxis
432                  *
433                  * @param msg the received message
434                  */
435                 void handle_rename_marker_time_axis(const char* msg) ;
436                 
437                 /**
438                  * Handle the renaming of an ImageFrameItem
439                  *
440                  * @param msg the received message
441                  */
442                 void handle_rename_imageframe_time_axis_group(const char* msg) ;
443                 
444                 /**
445                  * Handle the renaming of an ImageFrameItem
446                  *
447                  * @param msg the received message
448                  */
449                 void handle_rename_imageframe_view(const char* msg) ;
450                 
451                 /**
452                  * Handle the renaming of an Marker
453                  *
454                  * @param msg the received message
455                  */
456                 void handle_rename_marker_view(const char* msg) ;
457                 
458                 //---------------------------------------------------------------------------------------//
459                 // handlers for data request
460         
461                 /**
462                  * Handle a request for the sessnio naem fo the current session
463                  * We return a failure state if no session is open
464                  *
465                  * @param msg the received message
466                  */
467                 void handle_session_name_request(const char* msg) ;
468         
469                  
470                 //---------------------------------------------------------------------------------------//
471                 // handlers for specific item update changes
472         
473                 /**
474                  * Handle ImageFrameView positional changes
475                  *
476                  * @param msg the received message
477                  */
478                 void handle_imageframe_view_position_update(const char* msg) ;
479                 
480                 /**
481                  * Handle ImageFrameView Duration changes
482                  *
483                  * @param msg the received message
484                  */
485                 void handle_imageframe_view_duration_update(const char* msg) ;
486                 
487                 /**
488                  * Handle ImageFrameView Position Lock Constraint changes
489                  *
490                  * @param msg the received message
491                  */
492                 void handle_imageframe_position_lock_update(const char* msg) ;
493                 
494                 /**
495                  * Handle ImageFrameView Maximum Duration changes
496                  *
497                  * @param msg the received message
498                  */
499                 void handle_imageframe_view_max_duration_update(const char* msg) ;
500                 
501                 /**
502                  * Handle image frame max duration enable constraint changes
503                  *
504                  * @param msg the received message
505                  */
506                 void handle_imageframe_view_max_duration_enable_update(const char* msg) ;
507                 
508                 /**
509                  * Handle ImageFrameView Minimum Duration changes
510                  *
511                  * @param msg the received message
512                  */
513                 void handle_imageframe_view_min_duration_update(const char* msg) ;
514                 
515                 /**
516                  * Handle image frame min duration enable constraint changes
517                  *
518                  * @param msg the received message
519                  */
520                 void handle_imageframe_view_min_duration_enable_update(const char* msg) ;
521         
522         
523                 /**
524                  * Handle MarkerView position changes
525                  *
526                  * @param msg the received message
527                  */
528                 void handle_marker_view_position_update(const char* msg) ;
529                 
530                 /**
531                  * Handle MarkerView duration changes
532                  *
533                  * @param msg the received message
534                  */
535                 void handle_marker_view_duration_update(const char* msg) ;
536                 
537                 /**
538                  * Handle MarkerView Position Lock Constraint changes
539                  *
540                  * @param msg the received message
541                  */
542                 void handle_marker_view_position_lock_update(const char* msg) ;
543                 
544                 /**
545                  * Handle MarkerView maximum duration changes
546                  *
547                  * @param msg the received message
548                  */
549                 void handle_marker_view_max_duration_update(const char* msg) ;
550                 
551                 /**
552                  * Handle MarkerView minimum duration changes
553                  *
554                  * @param msg the received message
555                  */
556                 void handle_marker_view_min_duration_update(const char* msg) ;
557         
558         
559
560                 //---------------------------------------------------------------------------------------//
561                 // handlers for Session Actions
562         
563                 /**
564                  * Handle the opening of a named audio session
565                  *
566                  * @param msg the received message
567                  */
568                 void handle_open_session(const char* msg) ;
569                 
570                 /**
571                  * Handle the closing of a named audio session
572                  *
573                  * @param msg the received message
574                  */
575                 void handle_closed_session(const char* msg) ;
576         
577                 //---------------------------------------------------------------------------------------//
578                 // handlers for the shutdown of the Image Compositor
579                 
580                 /**
581                  * Handle the shutdown message from the image compositor
582                  *
583                  * @param msg the received message
584                  */
585                 void handle_shutdown(const char* msg) ;
586         
587         
588                 //---------------------------------------------------------------------------------------//
589                 // convenince methods to break up messages
590         
591                 /**
592                  * Returns part of the received message as a std::string
593                  *
594                  * @param start the start character
595                  * @param num_chars the number of characters to read
596                  * @param the message to break apart
597                  * @return the sub string of the message
598                  */
599                 std::string get_message_part(int start, int32_t num_chars, const char* msg) ;
600                 
601                 
602                 /**
603                  * break up am image item description message
604                  * we break the mesage up into the parent Image Track id and size,
605                  * the parent group id and size, and the image id and size
606                  *
607                  * @param track_id
608                  * @param track_id_size
609                  * @param scene_id
610                  * @param scene_id_size
611                  * @param item_id
612                  * @param item_id_size
613                  */
614                 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) ;
615         
616                 /**
617                  * Compose a description of the specified image frame view
618                  * The description consists of the parent track name size and name,
619                  * the parent group name size and name, and the item name size and name
620                  *
621                  * @param ifv the item to compose a description of
622                  * @param buffer the buffer to write the description
623                  */
624                 void compose_imageframe_item_desc(ImageFrameView* ifv, std::ostringstream& buffer) ;
625
626                 /**
627                  * Compose a description of the specified marker view
628                  * The description consists of the parent track name size and name,
629                  * and the item name size and name
630                  *
631                  * @param mv the item to compose a description of
632                  * @param buffer the buffer to write the description
633                  */
634                 void compose_marker_item_desc(MarkerView* mv, std::ostringstream& buffer) ;
635                 
636                 
637                 /**
638                  * Returns the ImageFrameView from the specified description
639                  * The errcode parameter is used to indicate the item which caused
640                  * an error on failure of this method
641                  * 0 = suces
642                  * 1 = the track item was not found
643                  * 2 = the group item was not found
644                  * 3 = the imageframe item was not found
645                  *
646                  * @paran track_id the track on which the item is placed
647                  * @param group_id the group in which the item is a member
648                  * @param item_id the id of the item
649                  * @param int32_t reference used for error codes on failure
650                  * @param errmsg populated with a description of the error on failure
651                  * @return the described item on success, 0 otherwise
652                  */
653                 ImageFrameView* get_imageframe_view_from_desc(const string & track_id, const string & group_ud, const string & item_id, int& errcode, std::string& errmsg) ;
654          
655                 //---------------------------------------------------------------------------------------//
656                 // Convenince Message Send Methods
657                 
658                 /**
659                  * Sends a message throught the socket
660                  *
661                  * @param msg the message to send
662                  * @return the return value of the socket call
663                  */
664                 int send_message(const string & msg) ;
665                 
666                 /**
667                  * Reads a message from the Socket
668                  *
669                  * @param msg a string to populate with the received message
670                  * @return the return value from the socket call
671                  */
672                 int read_message(std::string& msg) ;
673                 
674                 /**
675                  * Convenience method to compose and send a success messasge back to the Image Compositor
676                  *
677                  */
678                 void send_return_success() ;
679                 
680                 /**
681                  * Convenience method to compose and send a failure messasge back to the Image Compositor
682                  *
683                  * @param msg the failure message
684                  */
685                 void send_return_failure(const std::string& msg) ;
686         
687                 //---------------------------------------------------------------------------------------//
688                 // Memebr Data
689                 
690                 /** Our instance of the socket handler, singleton */
691                 static ImageFrameSocketHandler* _instance ;
692                 
693                 /** The Ardour PublicEditor */
694                 PublicEditor& thePublicEditor ;
695                 
696                 /** the socket file descriptor */
697                 int theArdourToCompositorSocket ;
698                 
699                 /** This stores the 'tag' returned from gdk_input_add, which is required for removing the input */
700                 int theGdkInputTag ;
701
702 } ; /* class ImageFrameSocketHandler */
703
704 #endif /* __ardour_gtk_imageframe_socket_handler_h__ */