3 * Copyright (C) 2002-2003 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #include <sys/types.h>
33 #include "miracle_unit.h"
34 #include "miracle_commands.h"
35 #include "miracle_log.h"
37 int miracle_load( command_argument cmd_arg
)
39 miracle_unit unit
= miracle_get_unit(cmd_arg
->unit
);
40 char *filename
= (char*) cmd_arg
->argument
;
44 if ( filename
[0] == '!' )
50 if ( filename
[0] == '/' )
53 snprintf( fullname
, 1023, "%s%s", cmd_arg
->root_dir
, filename
);
56 return RESPONSE_INVALID_UNIT
;
59 double in
= -1, out
= -1;
60 if ( valerie_tokeniser_count( cmd_arg
->tokeniser
) == 5 )
62 in
= atof( valerie_tokeniser_get_string( cmd_arg
->tokeniser
, 3 ) );
63 out
= atof( valerie_tokeniser_get_string( cmd_arg
->tokeniser
, 4 ) );
65 if ( miracle_unit_load( unit
, fullname
, in
, out
, flush
) != valerie_ok
)
66 return RESPONSE_BAD_FILE
;
68 return RESPONSE_SUCCESS
;
71 int miracle_list( command_argument cmd_arg
)
73 miracle_unit unit
= miracle_get_unit( cmd_arg
->unit
);
77 miracle_unit_report_list( unit
, cmd_arg
->response
);
78 return RESPONSE_SUCCESS
;
81 return RESPONSE_INVALID_UNIT
;
84 static int parse_clip( command_argument cmd_arg
, int arg
)
86 miracle_unit unit
= miracle_get_unit(cmd_arg
->unit
);
87 int clip
= miracle_unit_get_current_clip( unit
);
89 if ( valerie_tokeniser_count( cmd_arg
->tokeniser
) > arg
)
91 char *token
= valerie_tokeniser_get_string( cmd_arg
->tokeniser
, arg
);
92 if ( token
[ 0 ] == '+' )
93 clip
+= atoi( token
+ 1 );
94 else if ( token
[ 0 ] == '-' )
95 clip
-= atoi( token
+ 1 );
103 int miracle_insert( command_argument cmd_arg
)
105 miracle_unit unit
= miracle_get_unit(cmd_arg
->unit
);
106 char *filename
= (char*) cmd_arg
->argument
;
109 if ( filename
[0] == '/' )
112 snprintf( fullname
, 1023, "%s%s", cmd_arg
->root_dir
, filename
);
115 return RESPONSE_INVALID_UNIT
;
118 long in
= -1, out
= -1;
119 int index
= parse_clip( cmd_arg
, 3 );
121 if ( valerie_tokeniser_count( cmd_arg
->tokeniser
) == 6 )
123 in
= atoi( valerie_tokeniser_get_string( cmd_arg
->tokeniser
, 4 ) );
124 out
= atoi( valerie_tokeniser_get_string( cmd_arg
->tokeniser
, 5 ) );
127 switch( miracle_unit_insert( unit
, fullname
, index
, in
, out
) )
130 return RESPONSE_SUCCESS
;
132 return RESPONSE_BAD_FILE
;
135 return RESPONSE_SUCCESS
;
138 int miracle_remove( command_argument cmd_arg
)
140 miracle_unit unit
= miracle_get_unit(cmd_arg
->unit
);
143 return RESPONSE_INVALID_UNIT
;
146 int index
= parse_clip( cmd_arg
, 2 );
148 if ( miracle_unit_remove( unit
, index
) != valerie_ok
)
149 return RESPONSE_BAD_FILE
;
151 return RESPONSE_SUCCESS
;
154 int miracle_clean( command_argument cmd_arg
)
156 miracle_unit unit
= miracle_get_unit(cmd_arg
->unit
);
159 return RESPONSE_INVALID_UNIT
;
162 if ( miracle_unit_clean( unit
) != valerie_ok
)
163 return RESPONSE_BAD_FILE
;
165 return RESPONSE_SUCCESS
;
168 int miracle_move( command_argument cmd_arg
)
170 miracle_unit unit
= miracle_get_unit(cmd_arg
->unit
);
174 if ( valerie_tokeniser_count( cmd_arg
->tokeniser
) > 2 )
176 int src
= parse_clip( cmd_arg
, 2 );
177 int dest
= parse_clip( cmd_arg
, 3 );
179 if ( miracle_unit_move( unit
, src
, dest
) != valerie_ok
)
180 return RESPONSE_BAD_FILE
;
184 return RESPONSE_MISSING_ARG
;
189 return RESPONSE_INVALID_UNIT
;
192 return RESPONSE_SUCCESS
;
195 int miracle_append( command_argument cmd_arg
)
197 miracle_unit unit
= miracle_get_unit(cmd_arg
->unit
);
198 char *filename
= (char*) cmd_arg
->argument
;
201 if ( filename
[0] == '/' )
204 snprintf( fullname
, 1023, "%s%s", cmd_arg
->root_dir
, filename
);
207 return RESPONSE_INVALID_UNIT
;
210 double in
= -1, out
= -1;
211 if ( valerie_tokeniser_count( cmd_arg
->tokeniser
) == 5 )
213 in
= atof( valerie_tokeniser_get_string( cmd_arg
->tokeniser
, 3 ) );
214 out
= atof( valerie_tokeniser_get_string( cmd_arg
->tokeniser
, 4 ) );
216 switch ( miracle_unit_append( unit
, fullname
, in
, out
) )
219 return RESPONSE_SUCCESS
;
221 return RESPONSE_BAD_FILE
;
224 return RESPONSE_SUCCESS
;
227 int miracle_play( command_argument cmd_arg
)
229 miracle_unit unit
= miracle_get_unit(cmd_arg
->unit
);
233 return RESPONSE_INVALID_UNIT
;
238 if ( valerie_tokeniser_count( cmd_arg
->tokeniser
) == 3 )
239 speed
= atoi( valerie_tokeniser_get_string( cmd_arg
->tokeniser
, 2 ) );
240 miracle_unit_play( unit
, speed
);
243 return RESPONSE_SUCCESS
;
246 int miracle_stop( command_argument cmd_arg
)
248 miracle_unit unit
= miracle_get_unit(cmd_arg
->unit
);
250 return RESPONSE_INVALID_UNIT
;
252 miracle_unit_play( unit
, 0 );
253 return RESPONSE_SUCCESS
;
256 int miracle_pause( command_argument cmd_arg
)
258 miracle_unit unit
= miracle_get_unit(cmd_arg
->unit
);
260 return RESPONSE_INVALID_UNIT
;
262 miracle_unit_play( unit
, 0 );
263 return RESPONSE_SUCCESS
;
266 int miracle_rewind( command_argument cmd_arg
)
268 miracle_unit unit
= miracle_get_unit(cmd_arg
->unit
);
270 return RESPONSE_INVALID_UNIT
;
272 miracle_unit_play( unit
, -2000 );
273 return RESPONSE_SUCCESS
;
276 int miracle_step( command_argument cmd_arg
)
278 miracle_unit unit
= miracle_get_unit(cmd_arg
->unit
);
281 return RESPONSE_INVALID_UNIT
;
284 miracle_unit_play( unit
, 0 );
285 miracle_unit_step( unit
, *(int*) cmd_arg
->argument
);
287 return RESPONSE_SUCCESS
;
290 int miracle_goto( command_argument cmd_arg
)
292 miracle_unit unit
= miracle_get_unit(cmd_arg
->unit
);
293 int clip
= parse_clip( cmd_arg
, 3 );
295 if (unit
== NULL
|| miracle_unit_is_offline(unit
))
296 return RESPONSE_INVALID_UNIT
;
298 miracle_unit_change_position( unit
, clip
, *(int*) cmd_arg
->argument
);
299 return RESPONSE_SUCCESS
;
302 int miracle_ff( command_argument cmd_arg
)
304 miracle_unit unit
= miracle_get_unit(cmd_arg
->unit
);
306 return RESPONSE_INVALID_UNIT
;
308 miracle_unit_play( unit
, 2000 );
309 return RESPONSE_SUCCESS
;
312 int miracle_set_in_point( command_argument cmd_arg
)
315 dv_unit unit = miracle_get_unit(cmd_arg->unit);
316 int clip = parse_clip( cmd_arg, 3 );
318 if (unit == NULL || dv_unit_is_offline(unit))
319 return RESPONSE_INVALID_UNIT;
322 int position = *(int *) cmd_arg->argument;
324 switch( dv_unit_set_clip_in( unit, clip, position ) )
327 return RESPONSE_BAD_FILE;
329 return RESPONSE_OUT_OF_RANGE;
333 return RESPONSE_SUCCESS
;
336 int miracle_set_out_point( command_argument cmd_arg
)
339 dv_unit unit = miracle_get_unit(cmd_arg->unit);
340 int clip = parse_clip( cmd_arg, 3 );
342 if (unit == NULL || dv_unit_is_offline(unit))
343 return RESPONSE_INVALID_UNIT;
346 int position = *(int *) cmd_arg->argument;
348 switch( dv_unit_set_clip_out( unit, clip, position ) )
351 return RESPONSE_BAD_FILE;
353 return RESPONSE_OUT_OF_RANGE;
357 return RESPONSE_SUCCESS
;
360 int miracle_get_unit_status( command_argument cmd_arg
)
362 valerie_status_t status
;
363 int error
= miracle_unit_get_status( miracle_get_unit( cmd_arg
->unit
), &status
);
366 return RESPONSE_INVALID_UNIT
;
370 valerie_response_printf( cmd_arg
->response
, sizeof( text
), valerie_status_serialise( &status
, text
, sizeof( text
) ) );
371 return RESPONSE_SUCCESS_1
;
377 int miracle_set_unit_property( command_argument cmd_arg
)
380 dv_unit unit = miracle_get_unit(cmd_arg->unit);
383 return RESPONSE_INVALID_UNIT;
386 char *key = (char*) cmd_arg->argument;
389 value = strchr( key, '=' );
391 return RESPONSE_OUT_OF_RANGE;
394 miracle_log( LOG_DEBUG, "USET %s = %s", key, value );
395 if ( strncasecmp( key, "eof", 1024) == 0 )
397 if ( strncasecmp( value, "pause", 1024) == 0)
398 dv_unit_set_eof_action( unit, dv_player_pause );
399 else if ( strncasecmp( value, "loop", 1024) == 0)
400 dv_unit_set_eof_action( unit, dv_player_loop );
401 else if ( strncasecmp( value, "stop", 1024) == 0)
402 dv_unit_set_eof_action( unit, dv_player_terminate );
403 else if ( strncasecmp( value, "clean", 1024) == 0)
404 dv_unit_set_eof_action( unit, dv_player_clean_loop );
406 return RESPONSE_OUT_OF_RANGE;
408 else if ( strncasecmp( key, "points", 1024) == 0 )
410 if ( strncasecmp( value, "use", 1024) == 0)
411 dv_unit_set_mode( unit, dv_clip_mode_restricted );
412 else if ( strncasecmp( value, "ignore", 1024) == 0)
413 dv_unit_set_mode( unit, dv_clip_mode_unrestricted );
415 return RESPONSE_OUT_OF_RANGE;
417 else if ( strncasecmp( key, "syt_offset", 1024) == 0 )
419 dv_unit_set_syt_offset( unit, atoi( value ) );
421 else if ( strncasecmp( key, "cip_n", 1024) == 0 )
423 dv_unit_set_cip_n( unit, atoi( value ) );
425 else if ( strncasecmp( key, "cip_d", 1024) == 0 )
427 dv_unit_set_cip_d( unit, atoi( value ) );
429 else if ( strncasecmp( key, "size", 1024) == 0 )
431 dv_unit_set_buffer_size( unit, atoi( value ) );
433 else if ( strncasecmp( key, "n_frames", 1024) == 0 )
435 dv_unit_set_n_frames( unit, atoi( value ) );
437 else if ( strncasecmp( key, "n_fill", 1024) == 0 )
439 dv_unit_set_n_fill( unit, atoi( value ) );
442 return RESPONSE_OUT_OF_RANGE;
445 return RESPONSE_SUCCESS
;
448 int miracle_get_unit_property( command_argument cmd_arg
)
451 dv_unit unit = miracle_get_unit(cmd_arg->unit);
454 return RESPONSE_INVALID_UNIT;
457 char *key = (char*) cmd_arg->argument;
459 if ( strncasecmp( key, "eof", 1024) == 0 )
461 switch ( dv_unit_get_eof_action( unit ) )
463 case dv_player_pause:
464 dv_response_write( cmd_arg->response, "pause", strlen("pause") );
467 dv_response_write( cmd_arg->response, "loop", strlen("loop") );
469 case dv_player_terminate:
470 dv_response_write( cmd_arg->response, "stop", strlen("stop") );
472 case dv_player_clean_loop:
473 dv_response_write( cmd_arg->response, "clean", strlen("clean") );
476 return RESPONSE_SUCCESS_1;
478 else if ( strncasecmp( key, "points", 1024) == 0 )
480 if ( dv_unit_get_mode( unit ) == dv_clip_mode_restricted )
481 dv_response_write( cmd_arg->response, "use", strlen("use") );
483 dv_response_write( cmd_arg->response, "ignore", strlen("ignore") );
484 return RESPONSE_SUCCESS_1;
486 else if ( strncasecmp( key, "syt_offset", 1024) == 0 )
488 dv_response_printf( cmd_arg->response, 1024, "%d\n",
489 dv_unit_get_syt_offset( unit ) );
490 return RESPONSE_SUCCESS_1;
492 else if ( strncasecmp( key, "cip_n", 1024) == 0 )
494 dv_response_printf( cmd_arg->response, 1024, "%d\n",
495 dv_unit_get_cip_n( unit ) );
496 return RESPONSE_SUCCESS_1;
498 else if ( strncasecmp( key, "cip_d", 1024) == 0 )
500 dv_response_printf( cmd_arg->response, 1024, "%d\n",
501 dv_unit_get_cip_d( unit ) );
502 return RESPONSE_SUCCESS_1;
504 else if ( strncasecmp( key, "size", 1024) == 0 )
506 dv_response_printf( cmd_arg->response, 1024, "%d\n",
507 dv_unit_get_buffer_size( unit ) );
508 return RESPONSE_SUCCESS_1;
510 else if ( strncasecmp( key, "n_frames", 1024) == 0 )
512 dv_response_printf( cmd_arg->response, 1024, "%d\n",
513 dv_unit_get_n_frames( unit ) );
514 return RESPONSE_SUCCESS_1;
516 else if ( strncasecmp( key, "n_fill", 1024) == 0 )
518 dv_response_printf( cmd_arg->response, 1024, "%d\n",
519 dv_unit_get_n_fill( unit ) );
520 return RESPONSE_SUCCESS_1;
522 else if ( strncasecmp( key, "all", 1024 ) == 0 )
524 switch ( dv_unit_get_eof_action( unit ) )
526 case dv_player_pause:
527 dv_response_write( cmd_arg->response, "eof=pause\n", strlen("pause") );
530 dv_response_write( cmd_arg->response, "eof=loop\n", strlen("loop") );
532 case dv_player_terminate:
533 dv_response_write( cmd_arg->response, "eof=stop\n", strlen("stop") );
535 case dv_player_clean_loop:
536 dv_response_write( cmd_arg->response, "eof=clean\n", strlen("clean") );
539 if ( dv_unit_get_mode( unit ) == dv_clip_mode_restricted )
540 dv_response_write( cmd_arg->response, "points=use\n", strlen("use") );
542 dv_response_write( cmd_arg->response, "points=ignore\n", strlen("ignore") );
543 dv_response_printf( cmd_arg->response, 1024, "syt_offset=%d\n", dv_unit_get_syt_offset( unit ) );
544 dv_response_printf( cmd_arg->response, 1024, "cip_n=%d\n", dv_unit_get_cip_n( unit ) );
545 dv_response_printf( cmd_arg->response, 1024, "cip_d=%d\n", dv_unit_get_cip_d( unit ) );
546 dv_response_printf( cmd_arg->response, 1024, "size=%d\n", dv_unit_get_buffer_size( unit ) );
547 dv_response_printf( cmd_arg->response, 1024, "n_frames=%d\n", dv_unit_get_n_frames( unit ) );
548 dv_response_printf( cmd_arg->response, 1024, "n_fill=%d\n", dv_unit_get_n_fill( unit ) );
552 return RESPONSE_SUCCESS
;
556 int miracle_transfer( command_argument cmd_arg
)
559 dv_unit src_unit = miracle_get_unit(cmd_arg->unit);
560 int dest_unit_id = -1;
561 char *string = (char*) cmd_arg->argument;
562 if ( string != NULL && ( string[ 0 ] == 'U' || string[ 0 ] == 'u' ) && strlen( string ) > 1 )
563 dest_unit_id = atoi( string + 1 );
565 if ( src_unit != NULL && dest_unit_id != -1 )
567 dv_unit dest_unit = miracle_get_unit( dest_unit_id );
568 if ( dest_unit != NULL && !dv_unit_is_offline(dest_unit) && dest_unit != src_unit )
570 dv_unit_transfer( dest_unit, src_unit );
571 return RESPONSE_SUCCESS;
575 return RESPONSE_INVALID_UNIT
;