2 * valerie.c -- High Level Client API for miracle
3 * Copyright (C) 2002-2003 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 /* System header files */
27 /* Application header files */
29 #include "valerie_tokeniser.h"
30 #include "valerie_util.h"
32 /** Initialise the valerie structure.
35 valerie valerie_init( valerie_parser parser )
37 valerie this = malloc( sizeof( valerie_t ) );
40 memset( this, 0, sizeof( valerie_t ) );
41 this->parser = parser;
46 /** Set the response structure associated to the last command.
49 static void valerie_set_last_response( valerie this, valerie_response response )
53 if ( this->last_response != NULL )
54 valerie_response_close( this->last_response );
55 this->last_response = response;
59 /** Connect to the parser.
62 valerie_error_code valerie_connect( valerie this )
64 valerie_error_code error = valerie_server_unavailable;
65 valerie_response response = valerie_parser_connect( this->parser );
66 if ( response != NULL )
68 valerie_set_last_response( this, response );
69 if ( valerie_response_get_error_code( response ) == 100 )
75 /** Interpret a non-context sensitive error code.
78 static valerie_error_code valerie_get_error_code( valerie this, valerie_response response )
80 valerie_error_code error = valerie_server_unavailable;
81 switch( valerie_response_get_error_code( response ) )
84 error = valerie_server_unavailable;
87 error = valerie_no_response;
95 error = valerie_invalid_command;
98 error = valerie_server_timeout;
101 error = valerie_missing_argument;
104 error = valerie_unit_unavailable;
107 error = valerie_invalid_file;
111 error = valerie_unknown_error;
117 /** Execute a command.
120 valerie_error_code valerie_execute( valerie this, size_t size, char *format, ... )
122 valerie_error_code error = valerie_server_unavailable;
123 char *command = malloc( size );
124 if ( this != NULL && command != NULL )
127 va_start( list, format );
128 if ( vsnprintf( command, size, format, list ) != 0 )
130 valerie_response response = valerie_parser_execute( this->parser, command );
131 valerie_set_last_response( this, response );
132 error = valerie_get_error_code( this, response );
136 error = valerie_invalid_command;
142 error = valerie_malloc_failed;
148 /** Set a global property.
151 valerie_error_code valerie_set( valerie this, char *property, char *value )
153 return valerie_execute( this, 1024, "SET %s=%s", property, value );
156 /** Get a global property.
159 valerie_error_code valerie_get( valerie this, char *property, char *value, int length )
161 valerie_error_code error = valerie_execute( this, 1024, "GET %s", property );
162 if ( error == valerie_ok )
164 valerie_response response = valerie_get_last_response( this );
165 strncpy( value, valerie_response_get_line( response, 1 ), length );
173 valerie_error_code valerie_run( valerie this, char *file )
175 return valerie_execute( this, 10240, "RUN \"%s\"", file );
181 valerie_error_code valerie_unit_add( valerie this, char *guid, int *unit )
183 valerie_error_code error = valerie_execute( this, 1024, "UADD %s", guid );
184 if ( error == valerie_ok )
186 int length = valerie_response_count( this->last_response );
187 char *line = valerie_response_get_line( this->last_response, length - 2 );
188 if ( line == NULL || sscanf( line, "U%d", unit ) != 1 )
189 error = valerie_unit_creation_failed;
193 if ( error == valerie_unknown_error )
194 error = valerie_unit_creation_failed;
199 /** Load a file on the specified unit.
202 valerie_error_code valerie_unit_load( valerie this, int unit, char *file )
204 return valerie_execute( this, 10240, "LOAD U%d \"%s\"", unit, file );
207 static void valerie_interpret_clip_offset( char *output, valerie_clip_offset offset, int clip )
211 case valerie_absolute:
212 sprintf( output, "%d", clip );
214 case valerie_relative:
216 sprintf( output, "%d", clip );
218 sprintf( output, "+%d", clip );
223 /** Load a file on the specified unit with the specified in/out points.
226 valerie_error_code valerie_unit_load_clipped( valerie this, int unit, char *file, double in, double out )
228 return valerie_execute( this, 10240, "LOAD U%d \"%s\" %e %e", unit, file, in, out );
231 /** Load a file on the specified unit at the end of the current pump.
234 valerie_error_code valerie_unit_load_back( valerie this, int unit, char *file )
236 return valerie_execute( this, 10240, "LOAD U%d \"!%s\"", unit, file );
239 /** Load a file on the specified unit at the end of the pump with the specified in/out points.
242 valerie_error_code valerie_unit_load_back_clipped( valerie this, int unit, char *file, double in, double out )
244 return valerie_execute( this, 10240, "LOAD U%d \"!%s\" %e %e", unit, file, in, out );
247 /** Append a file on the specified unit.
250 valerie_error_code valerie_unit_append( valerie this, int unit, char *file, double in, double out )
252 return valerie_execute( this, 10240, "APND U%d \"%s\" %e %e", unit, file, in, out );
255 /** Clean the unit - this function removes all but the currently playing clip.
258 valerie_error_code valerie_unit_clean( valerie this, int unit )
260 return valerie_execute( this, 1024, "CLEAN U%d", unit );
263 /** Move clips on the units playlist.
266 valerie_error_code valerie_unit_clip_move( valerie this, int unit, valerie_clip_offset src_offset, int src, valerie_clip_offset dest_offset, int dest )
270 valerie_interpret_clip_offset( temp1, src_offset, src );
271 valerie_interpret_clip_offset( temp2, dest_offset, dest );
272 return valerie_execute( this, 1024, "MOVE U%d %s %s", unit, temp1, temp2 );
275 /** Remove clip at the specified position.
278 valerie_error_code valerie_unit_clip_remove( valerie this, int unit, valerie_clip_offset offset, int clip )
281 valerie_interpret_clip_offset( temp, offset, clip );
282 return valerie_execute( this, 1024, "REMOVE U%d %s", unit, temp );
285 /** Remove the currently playing clip.
288 valerie_error_code valerie_unit_remove_current_clip( valerie this, int unit )
290 return valerie_execute( this, 1024, "REMOVE U%d", unit );
293 /** Insert clip at the specified position.
296 valerie_error_code valerie_unit_clip_insert( valerie this, int unit, valerie_clip_offset offset, int clip, char *file, double in, double out )
299 valerie_interpret_clip_offset( temp, offset, clip );
300 return valerie_execute( this, 1024, "INSERT U%d %s %s %e %e", unit, file, temp, in, out );
303 /** Play the unit at normal speed.
306 valerie_error_code valerie_unit_play( valerie this, int unit )
308 return valerie_execute( this, 1024, "PLAY U%d 1000", unit );
311 /** Play the unit at specified speed.
314 valerie_error_code valerie_unit_play_at_speed( valerie this, int unit, int speed )
316 return valerie_execute( this, 10240, "PLAY U%d %d", unit, speed );
319 /** Stop playback on the specified unit.
322 valerie_error_code valerie_unit_stop( valerie this, int unit )
324 return valerie_execute( this, 1024, "STOP U%d", unit );
327 /** Pause playback on the specified unit.
330 valerie_error_code valerie_unit_pause( valerie this, int unit )
332 return valerie_execute( this, 1024, "PAUSE U%d", unit );
335 /** Rewind the specified unit.
338 valerie_error_code valerie_unit_rewind( valerie this, int unit )
340 return valerie_execute( this, 1024, "REW U%d", unit );
343 /** Fast forward the specified unit.
346 valerie_error_code valerie_unit_fast_forward( valerie this, int unit )
348 return valerie_execute( this, 1024, "FF U%d", unit );
351 /** Step by the number of frames on the specified unit.
354 valerie_error_code valerie_unit_step( valerie this, int unit, double step )
356 return valerie_execute( this, 1024, "STEP U%d %e", unit, step );
359 /** Goto the specified frame on the specified unit.
362 valerie_error_code valerie_unit_goto( valerie this, int unit, double position )
364 return valerie_execute( this, 1024, "GOTO U%d %e", unit, position );
367 /** Goto the specified frame in the clip on the specified unit.
370 valerie_error_code valerie_unit_clip_goto( valerie this, int unit, valerie_clip_offset offset, int clip, double position )
373 valerie_interpret_clip_offset( temp, offset, clip );
374 return valerie_execute( this, 1024, "GOTO U%d %e %s", unit, position, temp );
377 /** Set the in point of the loaded file on the specified unit.
380 valerie_error_code valerie_unit_set_in( valerie this, int unit, double in )
382 return valerie_execute( this, 1024, "SIN U%d %e", unit, in );
385 /** Set the in point of the clip on the specified unit.
388 valerie_error_code valerie_unit_clip_set_in( valerie this, int unit, valerie_clip_offset offset, int clip, double in )
391 valerie_interpret_clip_offset( temp, offset, clip );
392 return valerie_execute( this, 1024, "SIN U%d %e %s", unit, in, temp );
395 /** Set the out point of the loaded file on the specified unit.
398 valerie_error_code valerie_unit_set_out( valerie this, int unit, double out )
400 return valerie_execute( this, 1024, "SOUT U%d %e", unit, out );
403 /** Set the out point of the clip on the specified unit.
406 valerie_error_code valerie_unit_clip_set_out( valerie this, int unit, valerie_clip_offset offset, int clip, double in )
409 valerie_interpret_clip_offset( temp, offset, clip );
410 return valerie_execute( this, 1024, "SOUT U%d %e %s", unit, in, temp );
413 /** Clear the in point of the loaded file on the specified unit.
416 valerie_error_code valerie_unit_clear_in( valerie this, int unit )
418 return valerie_execute( this, 1024, "SIN U%d -1", unit );
421 /** Clear the out point of the loaded file on the specified unit.
424 valerie_error_code valerie_unit_clear_out( valerie this, int unit )
426 return valerie_execute( this, 1024, "SOUT U%d -1", unit );
429 /** Clear the in and out points on the loaded file on the specified unit.
432 valerie_error_code valerie_unit_clear_in_out( valerie this, int unit )
434 valerie_error_code error = valerie_unit_clear_out( this, unit );
435 if ( error == valerie_ok )
436 error = valerie_unit_clear_in( this, unit );
440 /** Set a unit configuration property.
443 valerie_error_code valerie_unit_set( valerie this, int unit, char *name, char *value )
445 return valerie_execute( this, 1024, "USET U%d %s=%s", unit, name, value );
448 /** Get a unit configuration property.
451 valerie_error_code valerie_unit_get( valerie this, int unit, char *name )
453 return valerie_execute( this, 1024, "UGET U%d %s", unit, name );
456 /** Get a units status.
459 valerie_error_code valerie_unit_status( valerie this, int unit, valerie_status status )
461 valerie_error_code error = valerie_execute( this, 1024, "USTA U%d", unit );
462 int error_code = valerie_response_get_error_code( this->last_response );
464 memset( status, 0, sizeof( valerie_status_t ) );
466 if ( error_code == 202 && valerie_response_count( this->last_response ) == 2 )
467 valerie_status_parse( status, valerie_response_get_line( this->last_response, 1 ) );
468 else if ( error_code == 403 )
469 status->status = unit_undefined;
474 /** Transfer the current settings of unit src to unit dest.
477 valerie_error_code valerie_unit_transfer( valerie this, int src, int dest )
479 return valerie_execute( this, 1024, "XFER U%d U%d", src, dest );
482 /** Obtain the parsers notifier.
485 valerie_notifier valerie_get_notifier( valerie this )
488 return valerie_parser_get_notifier( this->parser );
493 /** List the contents of the specified directory.
496 valerie_dir valerie_dir_init( valerie this, char *directory )
498 valerie_dir dir = malloc( sizeof( valerie_dir_t ) );
501 memset( dir, 0, sizeof( valerie_dir_t ) );
502 dir->directory = strdup( directory );
503 dir->response = valerie_parser_executef( this->parser, "CLS \"%s\"", directory );
508 /** Return the error code associated to the dir.
511 valerie_error_code valerie_dir_get_error_code( valerie_dir dir )
514 return valerie_get_error_code( NULL, dir->response );
516 return valerie_malloc_failed;
519 /** Get a particular file entry in the directory.
522 valerie_error_code valerie_dir_get( valerie_dir dir, int index, valerie_dir_entry entry )
524 valerie_error_code error = valerie_ok;
525 memset( entry, 0, sizeof( valerie_dir_entry_t ) );
526 if ( index < valerie_dir_count( dir ) )
528 char *line = valerie_response_get_line( dir->response, index + 1 );
529 valerie_tokeniser tokeniser = valerie_tokeniser_init( );
530 valerie_tokeniser_parse_new( tokeniser, line, " " );
532 if ( valerie_tokeniser_count( tokeniser ) > 0 )
534 valerie_util_strip( valerie_tokeniser_get_string( tokeniser, 0 ), '\"' );
535 strcpy( entry->full, dir->directory );
536 if ( entry->full[ strlen( entry->full ) - 1 ] != '/' )
537 strcat( entry->full, "/" );
538 strcpy( entry->name, valerie_tokeniser_get_string( tokeniser, 0 ) );
539 strcat( entry->full, entry->name );
541 switch ( valerie_tokeniser_count( tokeniser ) )
547 entry->size = strtoull( valerie_tokeniser_get_string( tokeniser, 1 ), NULL, 10 );
550 error = valerie_invalid_file;
554 valerie_tokeniser_close( tokeniser );
559 /** Get the number of entries in the directory
562 int valerie_dir_count( valerie_dir dir )
564 if ( dir != NULL && valerie_response_count( dir->response ) >= 2 )
565 return valerie_response_count( dir->response ) - 2;
570 /** Close the directory structure.
573 void valerie_dir_close( valerie_dir dir )
577 free( dir->directory );
578 valerie_response_close( dir->response );
583 /** List the playlist of the specified unit.
586 valerie_list valerie_list_init( valerie this, int unit )
588 valerie_list list = calloc( 1, sizeof( valerie_list_t ) );
591 list->response = valerie_parser_executef( this->parser, "LIST U%d", unit );
592 if ( valerie_response_count( list->response ) >= 2 )
593 list->generation = atoi( valerie_response_get_line( list->response, 1 ) );
598 /** Return the error code associated to the list.
601 valerie_error_code valerie_list_get_error_code( valerie_list list )
604 return valerie_get_error_code( NULL, list->response );
606 return valerie_malloc_failed;
609 /** Get a particular file entry in the list.
612 valerie_error_code valerie_list_get( valerie_list list, int index, valerie_list_entry entry )
614 valerie_error_code error = valerie_ok;
615 memset( entry, 0, sizeof( valerie_list_entry_t ) );
616 if ( index < valerie_list_count( list ) )
618 char *line = valerie_response_get_line( list->response, index + 2 );
619 valerie_tokeniser tokeniser = valerie_tokeniser_init( );
620 valerie_tokeniser_parse_new( tokeniser, line, " " );
622 if ( valerie_tokeniser_count( tokeniser ) > 0 )
624 entry->clip = atoi( valerie_tokeniser_get_string( tokeniser, 0 ) );
625 valerie_util_strip( valerie_tokeniser_get_string( tokeniser, 1 ), '\"' );
626 strcpy( entry->full, valerie_tokeniser_get_string( tokeniser, 1 ) );
627 entry->in = atof( valerie_tokeniser_get_string( tokeniser, 2 ) );
628 entry->out = atof( valerie_tokeniser_get_string( tokeniser, 3 ) );
629 entry->max = atof( valerie_tokeniser_get_string( tokeniser, 4 ) );
630 entry->size = atof( valerie_tokeniser_get_string( tokeniser, 5 ) );
631 entry->fps = atof( valerie_tokeniser_get_string( tokeniser, 6 ) );
633 valerie_tokeniser_close( tokeniser );
638 /** Get the number of entries in the list
641 int valerie_list_count( valerie_list list )
643 if ( list != NULL && valerie_response_count( list->response ) >= 3 )
644 return valerie_response_count( list->response ) - 3;
649 /** Close the list structure.
652 void valerie_list_close( valerie_list list )
656 valerie_response_close( list->response );
661 /** List the currently connected nodes.
664 valerie_nodes valerie_nodes_init( valerie this )
666 valerie_nodes nodes = malloc( sizeof( valerie_nodes_t ) );
669 memset( nodes, 0, sizeof( valerie_nodes_t ) );
670 nodes->response = valerie_parser_executef( this->parser, "NLS" );
675 /** Return the error code associated to the nodes list.
678 valerie_error_code valerie_nodes_get_error_code( valerie_nodes nodes )
681 return valerie_get_error_code( NULL, nodes->response );
683 return valerie_malloc_failed;
686 /** Get a particular node entry.
689 valerie_error_code valerie_nodes_get( valerie_nodes nodes, int index, valerie_node_entry entry )
691 valerie_error_code error = valerie_ok;
692 memset( entry, 0, sizeof( valerie_node_entry_t ) );
693 if ( index < valerie_nodes_count( nodes ) )
695 char *line = valerie_response_get_line( nodes->response, index + 1 );
696 valerie_tokeniser tokeniser = valerie_tokeniser_init( );
697 valerie_tokeniser_parse_new( tokeniser, line, " " );
699 if ( valerie_tokeniser_count( tokeniser ) == 3 )
701 entry->node = atoi( valerie_tokeniser_get_string( tokeniser, 0 ) );
702 strncpy( entry->guid, valerie_tokeniser_get_string( tokeniser, 1 ), sizeof( entry->guid ) );
703 valerie_util_strip( valerie_tokeniser_get_string( tokeniser, 2 ), '\"' );
704 strncpy( entry->name, valerie_tokeniser_get_string( tokeniser, 2 ), sizeof( entry->name ) );
707 valerie_tokeniser_close( tokeniser );
712 /** Get the number of nodes
715 int valerie_nodes_count( valerie_nodes nodes )
717 if ( nodes != NULL && valerie_response_count( nodes->response ) >= 2 )
718 return valerie_response_count( nodes->response ) - 2;
723 /** Close the nodes structure.
726 void valerie_nodes_close( valerie_nodes nodes )
730 valerie_response_close( nodes->response );
735 /** List the currently defined units.
738 valerie_units valerie_units_init( valerie this )
740 valerie_units units = malloc( sizeof( valerie_units_t ) );
743 memset( units, 0, sizeof( valerie_units_t ) );
744 units->response = valerie_parser_executef( this->parser, "ULS" );
749 /** Return the error code associated to the nodes list.
752 valerie_error_code valerie_units_get_error_code( valerie_units units )
755 return valerie_get_error_code( NULL, units->response );
757 return valerie_malloc_failed;
760 /** Get a particular unit entry.
763 valerie_error_code valerie_units_get( valerie_units units, int index, valerie_unit_entry entry )
765 valerie_error_code error = valerie_ok;
766 memset( entry, 0, sizeof( valerie_unit_entry_t ) );
767 if ( index < valerie_units_count( units ) )
769 char *line = valerie_response_get_line( units->response, index + 1 );
770 valerie_tokeniser tokeniser = valerie_tokeniser_init( );
771 valerie_tokeniser_parse_new( tokeniser, line, " " );
773 if ( valerie_tokeniser_count( tokeniser ) == 4 )
775 entry->unit = atoi( valerie_tokeniser_get_string( tokeniser, 0 ) + 1 );
776 entry->node = atoi( valerie_tokeniser_get_string( tokeniser, 1 ) );
777 strncpy( entry->guid, valerie_tokeniser_get_string( tokeniser, 2 ), sizeof( entry->guid ) );
778 entry->online = atoi( valerie_tokeniser_get_string( tokeniser, 3 ) );
781 valerie_tokeniser_close( tokeniser );
786 /** Get the number of units
789 int valerie_units_count( valerie_units units )
791 if ( units != NULL && valerie_response_count( units->response ) >= 2 )
792 return valerie_response_count( units->response ) - 2;
797 /** Close the units structure.
800 void valerie_units_close( valerie_units units )
804 valerie_response_close( units->response );
809 /** Get the response of the last command executed.
812 valerie_response valerie_get_last_response( valerie this )
814 return this->last_response;
817 /** Obtain a printable message associated to the error code provided.
820 char *valerie_error_description( valerie_error_code error )
822 char *msg = "Unrecognised error";
828 case valerie_malloc_failed:
829 msg = "Memory allocation error";
831 case valerie_unknown_error:
832 msg = "Unknown error";
834 case valerie_no_response:
835 msg = "No response obtained";
837 case valerie_invalid_command:
838 msg = "Invalid command";
840 case valerie_server_timeout:
841 msg = "Communications with server timed out";
843 case valerie_missing_argument:
844 msg = "Missing argument";
846 case valerie_server_unavailable:
847 msg = "Unable to communicate with server";
849 case valerie_unit_creation_failed:
850 msg = "Unit creation failed";
852 case valerie_unit_unavailable:
853 msg = "Unit unavailable";
855 case valerie_invalid_file:
856 msg = "Invalid file";
858 case valerie_invalid_position:
859 msg = "Invalid position";
865 /** Close the valerie structure.
868 void valerie_close( valerie this )
872 valerie_set_last_response( this, NULL );