97fb2dd0934aeb3a6e5edc25a08357e682cfcb30
[melted] / src / melted / melted_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 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
283 if ( unit != NULL )
284 {
285 // Get the consumer's profile
286 mlt_consumer consumer = mlt_properties_get_data( unit->properties, "consumer", NULL );
287 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( consumer ) );
288 mlt_producer producer = mlt_factory_producer( profile, "westley-xml", doc );
289 if ( producer != NULL )
290 {
291 if ( miracle_unit_append_service( unit, MLT_PRODUCER_SERVICE( producer ) ) == valerie_ok )
292 {
293 mlt_producer_close( producer );
294 return RESPONSE_SUCCESS;
295 }
296 mlt_producer_close( producer );
297 }
298 }
299 return RESPONSE_BAD_FILE;
300 }
301
302 int miracle_play( command_argument cmd_arg )
303 {
304 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
305
306 if ( unit == NULL )
307 {
308 return RESPONSE_INVALID_UNIT;
309 }
310 else
311 {
312 int speed = 1000;
313 if ( valerie_tokeniser_count( cmd_arg->tokeniser ) == 3 )
314 speed = atoi( valerie_tokeniser_get_string( cmd_arg->tokeniser, 2 ) );
315 miracle_unit_play( unit, speed );
316 }
317
318 return RESPONSE_SUCCESS;
319 }
320
321 int miracle_stop( command_argument cmd_arg )
322 {
323 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
324 if ( unit == NULL )
325 return RESPONSE_INVALID_UNIT;
326 else
327 miracle_unit_terminate( unit );
328 return RESPONSE_SUCCESS;
329 }
330
331 int miracle_pause( command_argument cmd_arg )
332 {
333 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
334 if ( unit == NULL )
335 return RESPONSE_INVALID_UNIT;
336 else
337 miracle_unit_play( unit, 0 );
338 return RESPONSE_SUCCESS;
339 }
340
341 int miracle_rewind( command_argument cmd_arg )
342 {
343 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
344 if ( unit == NULL )
345 return RESPONSE_INVALID_UNIT;
346 else
347 miracle_unit_play( unit, -2000 );
348 return RESPONSE_SUCCESS;
349 }
350
351 int miracle_step( command_argument cmd_arg )
352 {
353 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
354
355 if (unit == NULL)
356 return RESPONSE_INVALID_UNIT;
357 else
358 {
359 miracle_unit_play( unit, 0 );
360 miracle_unit_step( unit, *(int*) cmd_arg->argument );
361 }
362 return RESPONSE_SUCCESS;
363 }
364
365 int miracle_goto( command_argument cmd_arg )
366 {
367 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
368 int clip = parse_clip( cmd_arg, 3 );
369
370 if (unit == NULL || miracle_unit_is_offline(unit))
371 return RESPONSE_INVALID_UNIT;
372 else
373 miracle_unit_change_position( unit, clip, *(int*) cmd_arg->argument );
374 return RESPONSE_SUCCESS;
375 }
376
377 int miracle_ff( command_argument cmd_arg )
378 {
379 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
380 if ( unit == NULL )
381 return RESPONSE_INVALID_UNIT;
382 else
383 miracle_unit_play( unit, 2000 );
384 return RESPONSE_SUCCESS;
385 }
386
387 int miracle_set_in_point( command_argument cmd_arg )
388 {
389 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
390 int clip = parse_clip( cmd_arg, 3 );
391
392 if ( unit == NULL )
393 return RESPONSE_INVALID_UNIT;
394 else
395 {
396 int position = *(int *) cmd_arg->argument;
397
398 switch( miracle_unit_set_clip_in( unit, clip, position ) )
399 {
400 case -1:
401 return RESPONSE_BAD_FILE;
402 case -2:
403 return RESPONSE_OUT_OF_RANGE;
404 }
405 }
406 return RESPONSE_SUCCESS;
407 }
408
409 int miracle_set_out_point( command_argument cmd_arg )
410 {
411 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
412 int clip = parse_clip( cmd_arg, 3 );
413
414 if ( unit == NULL )
415 return RESPONSE_INVALID_UNIT;
416 else
417 {
418 int position = *(int *) cmd_arg->argument;
419
420 switch( miracle_unit_set_clip_out( unit, clip, position ) )
421 {
422 case -1:
423 return RESPONSE_BAD_FILE;
424 case -2:
425 return RESPONSE_OUT_OF_RANGE;
426 }
427 }
428
429 return RESPONSE_SUCCESS;
430 }
431
432 int miracle_get_unit_status( command_argument cmd_arg )
433 {
434 valerie_status_t status;
435 int error = miracle_unit_get_status( miracle_get_unit( cmd_arg->unit ), &status );
436
437 if ( error == -1 )
438 return RESPONSE_INVALID_UNIT;
439 else
440 {
441 char text[ 10240 ];
442 valerie_response_printf( cmd_arg->response, sizeof( text ), valerie_status_serialise( &status, text, sizeof( text ) ) );
443 return RESPONSE_SUCCESS_1;
444 }
445 return 0;
446 }
447
448
449 int miracle_set_unit_property( command_argument cmd_arg )
450 {
451 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
452 char *name_value = (char*) cmd_arg->argument;
453 if (unit == NULL)
454 return RESPONSE_INVALID_UNIT;
455 else
456 miracle_unit_set( unit, name_value );
457 return RESPONSE_SUCCESS;
458 }
459
460 int miracle_get_unit_property( command_argument cmd_arg )
461 {
462 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
463 char *name = (char*) cmd_arg->argument;
464 char *value = miracle_unit_get( unit, name );
465 if (unit == NULL)
466 return RESPONSE_INVALID_UNIT;
467 else if ( value != NULL )
468 valerie_response_printf( cmd_arg->response, 1024, "%s\n", value );
469 return RESPONSE_SUCCESS;
470 }
471
472
473 int miracle_transfer( command_argument cmd_arg )
474 {
475 miracle_unit src_unit = miracle_get_unit(cmd_arg->unit);
476 int dest_unit_id = -1;
477 char *string = (char*) cmd_arg->argument;
478 if ( string != NULL && ( string[ 0 ] == 'U' || string[ 0 ] == 'u' ) && strlen( string ) > 1 )
479 dest_unit_id = atoi( string + 1 );
480
481 if ( src_unit != NULL && dest_unit_id != -1 )
482 {
483 miracle_unit dest_unit = miracle_get_unit( dest_unit_id );
484 if ( dest_unit != NULL && !miracle_unit_is_offline(dest_unit) && dest_unit != src_unit )
485 {
486 miracle_unit_transfer( dest_unit, src_unit );
487 return RESPONSE_SUCCESS;
488 }
489 }
490 return RESPONSE_INVALID_UNIT;
491 }