81e439c2a5c21740060a515d64fe9903abce8846
[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 char *service;
44
45 if ( filename[0] == '!' )
46 {
47 flush = 0;
48 filename ++;
49 }
50
51 service = strchr( filename, ':' );
52 if ( service != NULL )
53 {
54 service = filename;
55 filename = strchr( service, ':' );
56 *filename ++ = '\0';
57
58 if ( strlen( cmd_arg->root_dir ) && filename[0] == '/' )
59 filename++;
60
61 snprintf( fullname, 1023, "%s:%s%s", service, cmd_arg->root_dir, filename );
62 }
63 else
64 {
65 if ( strlen( cmd_arg->root_dir ) && filename[0] == '/' )
66 filename++;
67
68 snprintf( fullname, 1023, "%s%s", cmd_arg->root_dir, filename );
69 }
70
71 if (unit == NULL)
72 return RESPONSE_INVALID_UNIT;
73 else
74 {
75 int32_t in = -1, out = -1;
76 if ( valerie_tokeniser_count( cmd_arg->tokeniser ) == 5 )
77 {
78 in = atol( valerie_tokeniser_get_string( cmd_arg->tokeniser, 3 ) );
79 out = atol( valerie_tokeniser_get_string( cmd_arg->tokeniser, 4 ) );
80 }
81 if ( miracle_unit_load( unit, fullname, in, out, flush ) != valerie_ok )
82 return RESPONSE_BAD_FILE;
83 }
84 return RESPONSE_SUCCESS;
85 }
86
87 int miracle_list( command_argument cmd_arg )
88 {
89 miracle_unit unit = miracle_get_unit( cmd_arg->unit );
90
91 if ( unit != NULL )
92 {
93 miracle_unit_report_list( unit, cmd_arg->response );
94 return RESPONSE_SUCCESS;
95 }
96
97 return RESPONSE_INVALID_UNIT;
98 }
99
100 static int parse_clip( command_argument cmd_arg, int arg )
101 {
102 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
103 int clip = miracle_unit_get_current_clip( unit );
104
105 if ( valerie_tokeniser_count( cmd_arg->tokeniser ) > arg )
106 {
107 char *token = valerie_tokeniser_get_string( cmd_arg->tokeniser, arg );
108 if ( token[ 0 ] == '+' )
109 clip += atoi( token + 1 );
110 else if ( token[ 0 ] == '-' )
111 clip -= atoi( token + 1 );
112 else
113 clip = atoi( token );
114 }
115
116 return clip;
117 }
118
119 int miracle_insert( command_argument cmd_arg )
120 {
121 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
122 char *filename = (char*) cmd_arg->argument;
123 char fullname[1024];
124
125 if ( strlen( cmd_arg->root_dir ) && filename[0] == '/' )
126 filename++;
127
128 snprintf( fullname, 1023, "%s%s", cmd_arg->root_dir, filename );
129
130 if (unit == NULL)
131 return RESPONSE_INVALID_UNIT;
132 else
133 {
134 long in = -1, out = -1;
135 int index = parse_clip( cmd_arg, 3 );
136
137 if ( valerie_tokeniser_count( cmd_arg->tokeniser ) == 6 )
138 {
139 in = atoi( valerie_tokeniser_get_string( cmd_arg->tokeniser, 4 ) );
140 out = atoi( valerie_tokeniser_get_string( cmd_arg->tokeniser, 5 ) );
141 }
142
143 switch( miracle_unit_insert( unit, fullname, index, in, out ) )
144 {
145 case valerie_ok:
146 return RESPONSE_SUCCESS;
147 default:
148 return RESPONSE_BAD_FILE;
149 }
150 }
151 return RESPONSE_SUCCESS;
152 }
153
154 int miracle_remove( 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 int index = parse_clip( cmd_arg, 2 );
163
164 if ( miracle_unit_remove( unit, index ) != valerie_ok )
165 return RESPONSE_BAD_FILE;
166 }
167 return RESPONSE_SUCCESS;
168 }
169
170 int miracle_clean( command_argument cmd_arg )
171 {
172 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
173
174 if (unit == NULL)
175 return RESPONSE_INVALID_UNIT;
176 else
177 {
178 if ( miracle_unit_clean( unit ) != valerie_ok )
179 return RESPONSE_BAD_FILE;
180 }
181 return RESPONSE_SUCCESS;
182 }
183
184 int miracle_wipe( command_argument cmd_arg )
185 {
186 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
187
188 if (unit == NULL)
189 return RESPONSE_INVALID_UNIT;
190 else
191 {
192 if ( miracle_unit_wipe( unit ) != valerie_ok )
193 return RESPONSE_BAD_FILE;
194 }
195 return RESPONSE_SUCCESS;
196 }
197
198 int miracle_clear( command_argument cmd_arg )
199 {
200 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
201
202 if (unit == NULL)
203 return RESPONSE_INVALID_UNIT;
204 else
205 {
206 if ( miracle_unit_clear( unit ) != valerie_ok )
207 return RESPONSE_BAD_FILE;
208 }
209 return RESPONSE_SUCCESS;
210 }
211
212 int miracle_move( command_argument cmd_arg )
213 {
214 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
215
216 if ( unit != NULL )
217 {
218 if ( valerie_tokeniser_count( cmd_arg->tokeniser ) > 2 )
219 {
220 int src = parse_clip( cmd_arg, 2 );
221 int dest = parse_clip( cmd_arg, 3 );
222
223 if ( miracle_unit_move( unit, src, dest ) != valerie_ok )
224 return RESPONSE_BAD_FILE;
225 }
226 else
227 {
228 return RESPONSE_MISSING_ARG;
229 }
230 }
231 else
232 {
233 return RESPONSE_INVALID_UNIT;
234 }
235
236 return RESPONSE_SUCCESS;
237 }
238
239 int miracle_append( command_argument cmd_arg )
240 {
241 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
242 char *filename = (char*) cmd_arg->argument;
243 char fullname[1024];
244
245 if ( strlen( cmd_arg->root_dir ) && filename[0] == '/' )
246 filename++;
247
248 snprintf( fullname, 1023, "%s%s", cmd_arg->root_dir, filename );
249
250 if (unit == NULL)
251 return RESPONSE_INVALID_UNIT;
252 else
253 {
254 int32_t in = -1, out = -1;
255 if ( valerie_tokeniser_count( cmd_arg->tokeniser ) == 5 )
256 {
257 in = atol( valerie_tokeniser_get_string( cmd_arg->tokeniser, 3 ) );
258 out = atol( valerie_tokeniser_get_string( cmd_arg->tokeniser, 4 ) );
259 }
260 switch ( miracle_unit_append( unit, fullname, in, out ) )
261 {
262 case valerie_ok:
263 return RESPONSE_SUCCESS;
264 default:
265 return RESPONSE_BAD_FILE;
266 }
267 }
268 return RESPONSE_SUCCESS;
269 }
270
271 int miracle_push( command_argument cmd_arg, mlt_service service )
272 {
273 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
274 if ( unit != NULL && service != NULL )
275 if ( miracle_unit_append_service( unit, service ) == valerie_ok )
276 return RESPONSE_SUCCESS;
277 return RESPONSE_BAD_FILE;
278 }
279
280 int miracle_receive( command_argument cmd_arg, char *doc )
281 {
282 mlt_producer producer = mlt_factory_producer( "westley-xml", doc );
283 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
284 if ( unit != NULL && producer != NULL )
285 {
286 if ( miracle_unit_append_service( unit, MLT_PRODUCER_SERVICE( producer ) ) == valerie_ok )
287 {
288 mlt_producer_close( producer );
289 return RESPONSE_SUCCESS;
290 }
291 }
292 mlt_producer_close( producer );
293 return RESPONSE_BAD_FILE;
294 }
295
296 int miracle_play( command_argument cmd_arg )
297 {
298 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
299
300 if ( unit == NULL )
301 {
302 return RESPONSE_INVALID_UNIT;
303 }
304 else
305 {
306 int speed = 1000;
307 if ( valerie_tokeniser_count( cmd_arg->tokeniser ) == 3 )
308 speed = atoi( valerie_tokeniser_get_string( cmd_arg->tokeniser, 2 ) );
309 miracle_unit_play( unit, speed );
310 }
311
312 return RESPONSE_SUCCESS;
313 }
314
315 int miracle_stop( command_argument cmd_arg )
316 {
317 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
318 if ( unit == NULL )
319 return RESPONSE_INVALID_UNIT;
320 else
321 miracle_unit_terminate( unit );
322 return RESPONSE_SUCCESS;
323 }
324
325 int miracle_pause( command_argument cmd_arg )
326 {
327 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
328 if ( unit == NULL )
329 return RESPONSE_INVALID_UNIT;
330 else
331 miracle_unit_play( unit, 0 );
332 return RESPONSE_SUCCESS;
333 }
334
335 int miracle_rewind( command_argument cmd_arg )
336 {
337 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
338 if ( unit == NULL )
339 return RESPONSE_INVALID_UNIT;
340 else
341 miracle_unit_play( unit, -2000 );
342 return RESPONSE_SUCCESS;
343 }
344
345 int miracle_step( command_argument cmd_arg )
346 {
347 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
348
349 if (unit == NULL)
350 return RESPONSE_INVALID_UNIT;
351 else
352 {
353 miracle_unit_play( unit, 0 );
354 miracle_unit_step( unit, *(int*) cmd_arg->argument );
355 }
356 return RESPONSE_SUCCESS;
357 }
358
359 int miracle_goto( command_argument cmd_arg )
360 {
361 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
362 int clip = parse_clip( cmd_arg, 3 );
363
364 if (unit == NULL || miracle_unit_is_offline(unit))
365 return RESPONSE_INVALID_UNIT;
366 else
367 miracle_unit_change_position( unit, clip, *(int*) cmd_arg->argument );
368 return RESPONSE_SUCCESS;
369 }
370
371 int miracle_ff( command_argument cmd_arg )
372 {
373 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
374 if ( unit == NULL )
375 return RESPONSE_INVALID_UNIT;
376 else
377 miracle_unit_play( unit, 2000 );
378 return RESPONSE_SUCCESS;
379 }
380
381 int miracle_set_in_point( command_argument cmd_arg )
382 {
383 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
384 int clip = parse_clip( cmd_arg, 3 );
385
386 if ( unit == NULL )
387 return RESPONSE_INVALID_UNIT;
388 else
389 {
390 int position = *(int *) cmd_arg->argument;
391
392 switch( miracle_unit_set_clip_in( unit, clip, position ) )
393 {
394 case -1:
395 return RESPONSE_BAD_FILE;
396 case -2:
397 return RESPONSE_OUT_OF_RANGE;
398 }
399 }
400 return RESPONSE_SUCCESS;
401 }
402
403 int miracle_set_out_point( command_argument cmd_arg )
404 {
405 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
406 int clip = parse_clip( cmd_arg, 3 );
407
408 if ( unit == NULL )
409 return RESPONSE_INVALID_UNIT;
410 else
411 {
412 int position = *(int *) cmd_arg->argument;
413
414 switch( miracle_unit_set_clip_out( unit, clip, position ) )
415 {
416 case -1:
417 return RESPONSE_BAD_FILE;
418 case -2:
419 return RESPONSE_OUT_OF_RANGE;
420 }
421 }
422
423 return RESPONSE_SUCCESS;
424 }
425
426 int miracle_get_unit_status( command_argument cmd_arg )
427 {
428 valerie_status_t status;
429 int error = miracle_unit_get_status( miracle_get_unit( cmd_arg->unit ), &status );
430
431 if ( error == -1 )
432 return RESPONSE_INVALID_UNIT;
433 else
434 {
435 char text[ 10240 ];
436 valerie_response_printf( cmd_arg->response, sizeof( text ), valerie_status_serialise( &status, text, sizeof( text ) ) );
437 return RESPONSE_SUCCESS_1;
438 }
439 return 0;
440 }
441
442
443 int miracle_set_unit_property( command_argument cmd_arg )
444 {
445 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
446 char *name_value = (char*) cmd_arg->argument;
447 if (unit == NULL)
448 return RESPONSE_INVALID_UNIT;
449 else
450 miracle_unit_set( unit, name_value );
451 return RESPONSE_SUCCESS;
452 }
453
454 int miracle_get_unit_property( command_argument cmd_arg )
455 {
456 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
457 char *name = (char*) cmd_arg->argument;
458 char *value = miracle_unit_get( unit, name );
459 if (unit == NULL)
460 return RESPONSE_INVALID_UNIT;
461 else if ( value != NULL )
462 valerie_response_printf( cmd_arg->response, 1024, "%s\n", value );
463 return RESPONSE_SUCCESS;
464 }
465
466
467 int miracle_transfer( command_argument cmd_arg )
468 {
469 miracle_unit src_unit = miracle_get_unit(cmd_arg->unit);
470 int dest_unit_id = -1;
471 char *string = (char*) cmd_arg->argument;
472 if ( string != NULL && ( string[ 0 ] == 'U' || string[ 0 ] == 'u' ) && strlen( string ) > 1 )
473 dest_unit_id = atoi( string + 1 );
474
475 if ( src_unit != NULL && dest_unit_id != -1 )
476 {
477 miracle_unit dest_unit = miracle_get_unit( dest_unit_id );
478 if ( dest_unit != NULL && !miracle_unit_is_offline(dest_unit) && dest_unit != src_unit )
479 {
480 miracle_unit_transfer( dest_unit, src_unit );
481 return RESPONSE_SUCCESS;
482 }
483 }
484 return RESPONSE_INVALID_UNIT;
485 }