c810c0a3bfa882cea56c25b428c00bbfb506e34c
[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_clear( 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_clear( unit ) != valerie_ok )
193 return RESPONSE_BAD_FILE;
194 }
195 return RESPONSE_SUCCESS;
196 }
197
198 int miracle_move( command_argument cmd_arg )
199 {
200 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
201
202 if ( unit != NULL )
203 {
204 if ( valerie_tokeniser_count( cmd_arg->tokeniser ) > 2 )
205 {
206 int src = parse_clip( cmd_arg, 2 );
207 int dest = parse_clip( cmd_arg, 3 );
208
209 if ( miracle_unit_move( unit, src, dest ) != valerie_ok )
210 return RESPONSE_BAD_FILE;
211 }
212 else
213 {
214 return RESPONSE_MISSING_ARG;
215 }
216 }
217 else
218 {
219 return RESPONSE_INVALID_UNIT;
220 }
221
222 return RESPONSE_SUCCESS;
223 }
224
225 int miracle_append( command_argument cmd_arg )
226 {
227 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
228 char *filename = (char*) cmd_arg->argument;
229 char fullname[1024];
230
231 if ( strlen( cmd_arg->root_dir ) && filename[0] == '/' )
232 filename++;
233
234 snprintf( fullname, 1023, "%s%s", cmd_arg->root_dir, filename );
235
236 if (unit == NULL)
237 return RESPONSE_INVALID_UNIT;
238 else
239 {
240 int32_t in = -1, out = -1;
241 if ( valerie_tokeniser_count( cmd_arg->tokeniser ) == 5 )
242 {
243 in = atol( valerie_tokeniser_get_string( cmd_arg->tokeniser, 3 ) );
244 out = atol( valerie_tokeniser_get_string( cmd_arg->tokeniser, 4 ) );
245 }
246 switch ( miracle_unit_append( unit, fullname, in, out ) )
247 {
248 case valerie_ok:
249 return RESPONSE_SUCCESS;
250 default:
251 return RESPONSE_BAD_FILE;
252 }
253 }
254 return RESPONSE_SUCCESS;
255 }
256
257 int miracle_push( command_argument cmd_arg, mlt_service service )
258 {
259 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
260 if ( unit != NULL && service != NULL )
261 if ( miracle_unit_append_service( unit, service ) == valerie_ok )
262 return RESPONSE_SUCCESS;
263 return RESPONSE_BAD_FILE;
264 }
265
266 int miracle_play( command_argument cmd_arg )
267 {
268 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
269
270 if ( unit == NULL )
271 {
272 return RESPONSE_INVALID_UNIT;
273 }
274 else
275 {
276 int speed = 1000;
277 if ( valerie_tokeniser_count( cmd_arg->tokeniser ) == 3 )
278 speed = atoi( valerie_tokeniser_get_string( cmd_arg->tokeniser, 2 ) );
279 miracle_unit_play( unit, speed );
280 }
281
282 return RESPONSE_SUCCESS;
283 }
284
285 int miracle_stop( command_argument cmd_arg )
286 {
287 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
288 if ( unit == NULL )
289 return RESPONSE_INVALID_UNIT;
290 else
291 miracle_unit_terminate( unit );
292 return RESPONSE_SUCCESS;
293 }
294
295 int miracle_pause( command_argument cmd_arg )
296 {
297 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
298 if ( unit == NULL )
299 return RESPONSE_INVALID_UNIT;
300 else
301 miracle_unit_play( unit, 0 );
302 return RESPONSE_SUCCESS;
303 }
304
305 int miracle_rewind( command_argument cmd_arg )
306 {
307 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
308 if ( unit == NULL )
309 return RESPONSE_INVALID_UNIT;
310 else
311 miracle_unit_play( unit, -2000 );
312 return RESPONSE_SUCCESS;
313 }
314
315 int miracle_step( command_argument cmd_arg )
316 {
317 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
318
319 if (unit == NULL)
320 return RESPONSE_INVALID_UNIT;
321 else
322 {
323 miracle_unit_play( unit, 0 );
324 miracle_unit_step( unit, *(int*) cmd_arg->argument );
325 }
326 return RESPONSE_SUCCESS;
327 }
328
329 int miracle_goto( command_argument cmd_arg )
330 {
331 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
332 int clip = parse_clip( cmd_arg, 3 );
333
334 if (unit == NULL || miracle_unit_is_offline(unit))
335 return RESPONSE_INVALID_UNIT;
336 else
337 miracle_unit_change_position( unit, clip, *(int*) cmd_arg->argument );
338 return RESPONSE_SUCCESS;
339 }
340
341 int miracle_ff( 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_set_in_point( command_argument cmd_arg )
352 {
353 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
354 int clip = parse_clip( cmd_arg, 3 );
355
356 if ( unit == NULL )
357 return RESPONSE_INVALID_UNIT;
358 else
359 {
360 int position = *(int *) cmd_arg->argument;
361
362 switch( miracle_unit_set_clip_in( unit, clip, position ) )
363 {
364 case -1:
365 return RESPONSE_BAD_FILE;
366 case -2:
367 return RESPONSE_OUT_OF_RANGE;
368 }
369 }
370 return RESPONSE_SUCCESS;
371 }
372
373 int miracle_set_out_point( command_argument cmd_arg )
374 {
375 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
376 int clip = parse_clip( cmd_arg, 3 );
377
378 if ( unit == NULL )
379 return RESPONSE_INVALID_UNIT;
380 else
381 {
382 int position = *(int *) cmd_arg->argument;
383
384 switch( miracle_unit_set_clip_out( unit, clip, position ) )
385 {
386 case -1:
387 return RESPONSE_BAD_FILE;
388 case -2:
389 return RESPONSE_OUT_OF_RANGE;
390 }
391 }
392
393 return RESPONSE_SUCCESS;
394 }
395
396 int miracle_get_unit_status( command_argument cmd_arg )
397 {
398 valerie_status_t status;
399 int error = miracle_unit_get_status( miracle_get_unit( cmd_arg->unit ), &status );
400
401 if ( error == -1 )
402 return RESPONSE_INVALID_UNIT;
403 else
404 {
405 char text[ 10240 ];
406 valerie_response_printf( cmd_arg->response, sizeof( text ), valerie_status_serialise( &status, text, sizeof( text ) ) );
407 return RESPONSE_SUCCESS_1;
408 }
409 return 0;
410 }
411
412
413 int miracle_set_unit_property( command_argument cmd_arg )
414 {
415 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
416 char *name_value = (char*) cmd_arg->argument;
417 if (unit == NULL)
418 return RESPONSE_INVALID_UNIT;
419 else
420 miracle_unit_set( unit, name_value );
421 return RESPONSE_SUCCESS;
422 }
423
424 int miracle_get_unit_property( command_argument cmd_arg )
425 {
426 miracle_unit unit = miracle_get_unit(cmd_arg->unit);
427 char *name = (char*) cmd_arg->argument;
428 char *value = miracle_unit_get( unit, name );
429 if (unit == NULL)
430 return RESPONSE_INVALID_UNIT;
431 else if ( value != NULL )
432 valerie_response_printf( cmd_arg->response, 1024, "%s\n", value );
433 return RESPONSE_SUCCESS;
434 }
435
436
437 int miracle_transfer( command_argument cmd_arg )
438 {
439 miracle_unit src_unit = miracle_get_unit(cmd_arg->unit);
440 int dest_unit_id = -1;
441 char *string = (char*) cmd_arg->argument;
442 if ( string != NULL && ( string[ 0 ] == 'U' || string[ 0 ] == 'u' ) && strlen( string ) > 1 )
443 dest_unit_id = atoi( string + 1 );
444
445 if ( src_unit != NULL && dest_unit_id != -1 )
446 {
447 miracle_unit dest_unit = miracle_get_unit( dest_unit_id );
448 if ( dest_unit != NULL && !miracle_unit_is_offline(dest_unit) && dest_unit != src_unit )
449 {
450 miracle_unit_transfer( dest_unit, src_unit );
451 return RESPONSE_SUCCESS;
452 }
453 }
454 return RESPONSE_INVALID_UNIT;
455 }