e0f60031ea2f010d3be2f8ab11681f2bdebc81e0
[melted] / src / miracle / miracle_unit_commands.c
1 /*
2 * unit_commands.c
3 * Copyright (C) 2002-2003 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
5 *
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.
10 *
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.
15 *
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.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32
33 #include "miracle_unit.h"
34 #include "miracle_commands.h"
35 #include "miracle_log.h"
36
37 int miracle_load( command_argument cmd_arg )
38 {
39 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
40 char *filename = (char*) cmd_arg->argument;
41 char fullname[1024];
42 int flush = 1;
43
44 if ( filename[0] == '!' )
45 {
46 flush = 0;
47 filename ++;
48 }
49
50 if ( filename[0] == '/' )
51 filename++;
52
53 snprintf( fullname, 1023, "%s%s", cmd_arg->root_dir, filename );
54
55 if (unit == NULL)
56 return RESPONSE_INVALID_UNIT;
57 else
58 {
59 int64_t in = -1, out = -1;
60 if ( valerie_tokeniser_count( cmd_arg->tokeniser ) == 5 )
61 {
62 in = atol( valerie_tokeniser_get_string( cmd_arg->tokeniser, 3 ) );
63 out = atol( valerie_tokeniser_get_string( cmd_arg->tokeniser, 4 ) );
64 }
65 if ( miracle_unit_load( unit, fullname, in, out, flush ) != valerie_ok )
66 return RESPONSE_BAD_FILE;
67 }
68 return RESPONSE_SUCCESS;
69 }
70
71 int miracle_list( command_argument cmd_arg )
72 {
73 miracle_unit unit = miracle_get_unit( cmd_arg->unit );
74
75 if ( unit != NULL )
76 {
77 miracle_unit_report_list( unit, cmd_arg->response );
78 return RESPONSE_SUCCESS;
79 }
80
81 return RESPONSE_INVALID_UNIT;
82 }
83
84 static int parse_clip( command_argument cmd_arg, int arg )
85 {
86 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
87 int clip = miracle_unit_get_current_clip( unit );
88
89 if ( valerie_tokeniser_count( cmd_arg->tokeniser ) > arg )
90 {
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 );
96 else
97 clip = atoi( token );
98 }
99
100 return clip;
101 }
102
103 int miracle_insert( command_argument cmd_arg )
104 {
105 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
106 char *filename = (char*) cmd_arg->argument;
107 char fullname[1024];
108
109 if ( filename[0] == '/' )
110 filename++;
111
112 snprintf( fullname, 1023, "%s%s", cmd_arg->root_dir, filename );
113
114 if (unit == NULL)
115 return RESPONSE_INVALID_UNIT;
116 else
117 {
118 long in = -1, out = -1;
119 int index = parse_clip( cmd_arg, 3 );
120
121 if ( valerie_tokeniser_count( cmd_arg->tokeniser ) == 6 )
122 {
123 in = atoi( valerie_tokeniser_get_string( cmd_arg->tokeniser, 4 ) );
124 out = atoi( valerie_tokeniser_get_string( cmd_arg->tokeniser, 5 ) );
125 }
126
127 switch( miracle_unit_insert( unit, fullname, index, in, out ) )
128 {
129 case valerie_ok:
130 return RESPONSE_SUCCESS;
131 default:
132 return RESPONSE_BAD_FILE;
133 }
134 }
135 return RESPONSE_SUCCESS;
136 }
137
138 int miracle_remove( command_argument cmd_arg )
139 {
140 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
141
142 if (unit == NULL)
143 return RESPONSE_INVALID_UNIT;
144 else
145 {
146 int index = parse_clip( cmd_arg, 2 );
147
148 if ( miracle_unit_remove( unit, index ) != valerie_ok )
149 return RESPONSE_BAD_FILE;
150 }
151 return RESPONSE_SUCCESS;
152 }
153
154 int miracle_clean( command_argument cmd_arg )
155 {
156 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
157
158 if (unit == NULL)
159 return RESPONSE_INVALID_UNIT;
160 else
161 {
162 if ( miracle_unit_clean( unit ) != valerie_ok )
163 return RESPONSE_BAD_FILE;
164 }
165 return RESPONSE_SUCCESS;
166 }
167
168 int miracle_move( command_argument cmd_arg )
169 {
170 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
171
172 if ( unit != NULL )
173 {
174 if ( valerie_tokeniser_count( cmd_arg->tokeniser ) > 2 )
175 {
176 int src = parse_clip( cmd_arg, 2 );
177 int dest = parse_clip( cmd_arg, 3 );
178
179 if ( miracle_unit_move( unit, src, dest ) != valerie_ok )
180 return RESPONSE_BAD_FILE;
181 }
182 else
183 {
184 return RESPONSE_MISSING_ARG;
185 }
186 }
187 else
188 {
189 return RESPONSE_INVALID_UNIT;
190 }
191
192 return RESPONSE_SUCCESS;
193 }
194
195 int miracle_append( command_argument cmd_arg )
196 {
197 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
198 char *filename = (char*) cmd_arg->argument;
199 char fullname[1024];
200
201 if ( filename[0] == '/' )
202 filename++;
203
204 snprintf( fullname, 1023, "%s%s", cmd_arg->root_dir, filename );
205
206 if (unit == NULL)
207 return RESPONSE_INVALID_UNIT;
208 else
209 {
210 int64_t in = -1, out = -1;
211 if ( valerie_tokeniser_count( cmd_arg->tokeniser ) == 5 )
212 {
213 in = atol( valerie_tokeniser_get_string( cmd_arg->tokeniser, 3 ) );
214 out = atol( valerie_tokeniser_get_string( cmd_arg->tokeniser, 4 ) );
215 }
216 switch ( miracle_unit_append( unit, fullname, in, out ) )
217 {
218 case valerie_ok:
219 return RESPONSE_SUCCESS;
220 default:
221 return RESPONSE_BAD_FILE;
222 }
223 }
224 return RESPONSE_SUCCESS;
225 }
226
227 int miracle_play( command_argument cmd_arg )
228 {
229 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
230
231 if ( unit == NULL )
232 {
233 return RESPONSE_INVALID_UNIT;
234 }
235 else
236 {
237 int speed = 1000;
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 );
241 }
242
243 return RESPONSE_SUCCESS;
244 }
245
246 int miracle_stop( command_argument cmd_arg )
247 {
248 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
249 if ( unit == NULL )
250 return RESPONSE_INVALID_UNIT;
251 else
252 miracle_unit_play( unit, 0 );
253 return RESPONSE_SUCCESS;
254 }
255
256 int miracle_pause( command_argument cmd_arg )
257 {
258 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
259 if ( unit == NULL )
260 return RESPONSE_INVALID_UNIT;
261 else
262 miracle_unit_play( unit, 0 );
263 return RESPONSE_SUCCESS;
264 }
265
266 int miracle_rewind( command_argument cmd_arg )
267 {
268 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
269 if ( unit == NULL )
270 return RESPONSE_INVALID_UNIT;
271 else
272 miracle_unit_play( unit, -2000 );
273 return RESPONSE_SUCCESS;
274 }
275
276 int miracle_step( command_argument cmd_arg )
277 {
278 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
279
280 if (unit == NULL)
281 return RESPONSE_INVALID_UNIT;
282 else
283 {
284 miracle_unit_play( unit, 0 );
285 miracle_unit_step( unit, *(int*) cmd_arg->argument );
286 }
287 return RESPONSE_SUCCESS;
288 }
289
290 int miracle_goto( command_argument cmd_arg )
291 {
292 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
293 int clip = parse_clip( cmd_arg, 3 );
294
295 if (unit == NULL || miracle_unit_is_offline(unit))
296 return RESPONSE_INVALID_UNIT;
297 else
298 miracle_unit_change_position( unit, clip, *(int*) cmd_arg->argument );
299 return RESPONSE_SUCCESS;
300 }
301
302 int miracle_ff( command_argument cmd_arg )
303 {
304 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
305 if ( unit == NULL )
306 return RESPONSE_INVALID_UNIT;
307 else
308 miracle_unit_play( unit, 2000 );
309 return RESPONSE_SUCCESS;
310 }
311
312 int miracle_set_in_point( command_argument cmd_arg )
313 {
314 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
315 int clip = parse_clip( cmd_arg, 3 );
316
317 if ( unit == NULL )
318 return RESPONSE_INVALID_UNIT;
319 else
320 {
321 int position = *(int *) cmd_arg->argument;
322
323 switch( miracle_unit_set_clip_in( unit, clip, position ) )
324 {
325 case -1:
326 return RESPONSE_BAD_FILE;
327 case -2:
328 return RESPONSE_OUT_OF_RANGE;
329 }
330 }
331 return RESPONSE_SUCCESS;
332 }
333
334 int miracle_set_out_point( command_argument cmd_arg )
335 {
336 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
337 int clip = parse_clip( cmd_arg, 3 );
338
339 if ( unit == NULL )
340 return RESPONSE_INVALID_UNIT;
341 else
342 {
343 int position = *(int *) cmd_arg->argument;
344
345 switch( miracle_unit_set_clip_out( unit, clip, position ) )
346 {
347 case -1:
348 return RESPONSE_BAD_FILE;
349 case -2:
350 return RESPONSE_OUT_OF_RANGE;
351 }
352 }
353
354 return RESPONSE_SUCCESS;
355 }
356
357 int miracle_get_unit_status( command_argument cmd_arg )
358 {
359 valerie_status_t status;
360 int error = miracle_unit_get_status( miracle_get_unit( cmd_arg->unit ), &status );
361
362 if ( error == -1 )
363 return RESPONSE_INVALID_UNIT;
364 else
365 {
366 char text[ 10240 ];
367 valerie_response_printf( cmd_arg->response, sizeof( text ), valerie_status_serialise( &status, text, sizeof( text ) ) );
368 return RESPONSE_SUCCESS_1;
369 }
370 return 0;
371 }
372
373
374 int miracle_set_unit_property( command_argument cmd_arg )
375 {
376 /*
377 dv_unit unit = miracle_get_unit(cmd_arg->unit);
378
379 if (unit == NULL)
380 return RESPONSE_INVALID_UNIT;
381 else
382 {
383 char *key = (char*) cmd_arg->argument;
384 char *value = NULL;
385
386 value = strchr( key, '=' );
387 if (value == NULL)
388 return RESPONSE_OUT_OF_RANGE;
389 value[0] = 0;
390 value++;
391 miracle_log( LOG_DEBUG, "USET %s = %s", key, value );
392 if ( strncasecmp( key, "eof", 1024) == 0 )
393 {
394 if ( strncasecmp( value, "pause", 1024) == 0)
395 dv_unit_set_eof_action( unit, dv_player_pause );
396 else if ( strncasecmp( value, "loop", 1024) == 0)
397 dv_unit_set_eof_action( unit, dv_player_loop );
398 else if ( strncasecmp( value, "stop", 1024) == 0)
399 dv_unit_set_eof_action( unit, dv_player_terminate );
400 else if ( strncasecmp( value, "clean", 1024) == 0)
401 dv_unit_set_eof_action( unit, dv_player_clean_loop );
402 else
403 return RESPONSE_OUT_OF_RANGE;
404 }
405 else if ( strncasecmp( key, "points", 1024) == 0 )
406 {
407 if ( strncasecmp( value, "use", 1024) == 0)
408 dv_unit_set_mode( unit, dv_clip_mode_restricted );
409 else if ( strncasecmp( value, "ignore", 1024) == 0)
410 dv_unit_set_mode( unit, dv_clip_mode_unrestricted );
411 else
412 return RESPONSE_OUT_OF_RANGE;
413 }
414 else if ( strncasecmp( key, "syt_offset", 1024) == 0 )
415 {
416 dv_unit_set_syt_offset( unit, atoi( value ) );
417 }
418 else if ( strncasecmp( key, "cip_n", 1024) == 0 )
419 {
420 dv_unit_set_cip_n( unit, atoi( value ) );
421 }
422 else if ( strncasecmp( key, "cip_d", 1024) == 0 )
423 {
424 dv_unit_set_cip_d( unit, atoi( value ) );
425 }
426 else if ( strncasecmp( key, "size", 1024) == 0 )
427 {
428 dv_unit_set_buffer_size( unit, atoi( value ) );
429 }
430 else if ( strncasecmp( key, "n_frames", 1024) == 0 )
431 {
432 dv_unit_set_n_frames( unit, atoi( value ) );
433 }
434 else if ( strncasecmp( key, "n_fill", 1024) == 0 )
435 {
436 dv_unit_set_n_fill( unit, atoi( value ) );
437 }
438 else
439 return RESPONSE_OUT_OF_RANGE;
440 }
441 */
442 return RESPONSE_SUCCESS;
443 }
444
445 int miracle_get_unit_property( command_argument cmd_arg )
446 {
447 /*
448 dv_unit unit = miracle_get_unit(cmd_arg->unit);
449
450 if (unit == NULL)
451 return RESPONSE_INVALID_UNIT;
452 else
453 {
454 char *key = (char*) cmd_arg->argument;
455
456 if ( strncasecmp( key, "eof", 1024) == 0 )
457 {
458 switch ( dv_unit_get_eof_action( unit ) )
459 {
460 case dv_player_pause:
461 dv_response_write( cmd_arg->response, "pause", strlen("pause") );
462 break;
463 case dv_player_loop:
464 dv_response_write( cmd_arg->response, "loop", strlen("loop") );
465 break;
466 case dv_player_terminate:
467 dv_response_write( cmd_arg->response, "stop", strlen("stop") );
468 break;
469 case dv_player_clean_loop:
470 dv_response_write( cmd_arg->response, "clean", strlen("clean") );
471 break;
472 }
473 return RESPONSE_SUCCESS_1;
474 }
475 else if ( strncasecmp( key, "points", 1024) == 0 )
476 {
477 if ( dv_unit_get_mode( unit ) == dv_clip_mode_restricted )
478 dv_response_write( cmd_arg->response, "use", strlen("use") );
479 else
480 dv_response_write( cmd_arg->response, "ignore", strlen("ignore") );
481 return RESPONSE_SUCCESS_1;
482 }
483 else if ( strncasecmp( key, "syt_offset", 1024) == 0 )
484 {
485 dv_response_printf( cmd_arg->response, 1024, "%d\n",
486 dv_unit_get_syt_offset( unit ) );
487 return RESPONSE_SUCCESS_1;
488 }
489 else if ( strncasecmp( key, "cip_n", 1024) == 0 )
490 {
491 dv_response_printf( cmd_arg->response, 1024, "%d\n",
492 dv_unit_get_cip_n( unit ) );
493 return RESPONSE_SUCCESS_1;
494 }
495 else if ( strncasecmp( key, "cip_d", 1024) == 0 )
496 {
497 dv_response_printf( cmd_arg->response, 1024, "%d\n",
498 dv_unit_get_cip_d( unit ) );
499 return RESPONSE_SUCCESS_1;
500 }
501 else if ( strncasecmp( key, "size", 1024) == 0 )
502 {
503 dv_response_printf( cmd_arg->response, 1024, "%d\n",
504 dv_unit_get_buffer_size( unit ) );
505 return RESPONSE_SUCCESS_1;
506 }
507 else if ( strncasecmp( key, "n_frames", 1024) == 0 )
508 {
509 dv_response_printf( cmd_arg->response, 1024, "%d\n",
510 dv_unit_get_n_frames( unit ) );
511 return RESPONSE_SUCCESS_1;
512 }
513 else if ( strncasecmp( key, "n_fill", 1024) == 0 )
514 {
515 dv_response_printf( cmd_arg->response, 1024, "%d\n",
516 dv_unit_get_n_fill( unit ) );
517 return RESPONSE_SUCCESS_1;
518 }
519 else if ( strncasecmp( key, "all", 1024 ) == 0 )
520 {
521 switch ( dv_unit_get_eof_action( unit ) )
522 {
523 case dv_player_pause:
524 dv_response_write( cmd_arg->response, "eof=pause\n", strlen("pause") );
525 break;
526 case dv_player_loop:
527 dv_response_write( cmd_arg->response, "eof=loop\n", strlen("loop") );
528 break;
529 case dv_player_terminate:
530 dv_response_write( cmd_arg->response, "eof=stop\n", strlen("stop") );
531 break;
532 case dv_player_clean_loop:
533 dv_response_write( cmd_arg->response, "eof=clean\n", strlen("clean") );
534 break;
535 }
536 if ( dv_unit_get_mode( unit ) == dv_clip_mode_restricted )
537 dv_response_write( cmd_arg->response, "points=use\n", strlen("use") );
538 else
539 dv_response_write( cmd_arg->response, "points=ignore\n", strlen("ignore") );
540 dv_response_printf( cmd_arg->response, 1024, "syt_offset=%d\n", dv_unit_get_syt_offset( unit ) );
541 dv_response_printf( cmd_arg->response, 1024, "cip_n=%d\n", dv_unit_get_cip_n( unit ) );
542 dv_response_printf( cmd_arg->response, 1024, "cip_d=%d\n", dv_unit_get_cip_d( unit ) );
543 dv_response_printf( cmd_arg->response, 1024, "size=%d\n", dv_unit_get_buffer_size( unit ) );
544 dv_response_printf( cmd_arg->response, 1024, "n_frames=%d\n", dv_unit_get_n_frames( unit ) );
545 dv_response_printf( cmd_arg->response, 1024, "n_fill=%d\n", dv_unit_get_n_fill( unit ) );
546 }
547 }
548 */
549 return RESPONSE_SUCCESS;
550 }
551
552
553 int miracle_transfer( command_argument cmd_arg )
554 {
555 /*
556 dv_unit src_unit = miracle_get_unit(cmd_arg->unit);
557 int dest_unit_id = -1;
558 char *string = (char*) cmd_arg->argument;
559 if ( string != NULL && ( string[ 0 ] == 'U' || string[ 0 ] == 'u' ) && strlen( string ) > 1 )
560 dest_unit_id = atoi( string + 1 );
561
562 if ( src_unit != NULL && dest_unit_id != -1 )
563 {
564 dv_unit dest_unit = miracle_get_unit( dest_unit_id );
565 if ( dest_unit != NULL && !dv_unit_is_offline(dest_unit) && dest_unit != src_unit )
566 {
567 dv_unit_transfer( dest_unit, src_unit );
568 return RESPONSE_SUCCESS;
569 }
570 }
571 */
572 return RESPONSE_INVALID_UNIT;
573 }