]> git.sesse.net Git - mlt/blob - src/miracle/miracle_unit_commands.c
enhance miracle LOAD command to accept a service: prefix.
[mlt] / 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_play( command_argument cmd_arg )
258 {
259         miracle_unit unit = miracle_get_unit(cmd_arg->unit);
260         
261         if ( unit == NULL )
262         {
263                 return RESPONSE_INVALID_UNIT;
264         }
265         else
266         {
267                 int speed = 1000;
268                 if ( valerie_tokeniser_count( cmd_arg->tokeniser ) == 3 )
269                         speed = atoi( valerie_tokeniser_get_string( cmd_arg->tokeniser, 2 ) );
270                 miracle_unit_play( unit, speed );
271         }
272
273         return RESPONSE_SUCCESS;
274 }
275
276 int miracle_stop( command_argument cmd_arg )
277 {
278         miracle_unit unit = miracle_get_unit(cmd_arg->unit);
279         if ( unit == NULL )
280                 return RESPONSE_INVALID_UNIT;
281         else 
282                 miracle_unit_terminate( unit );
283         return RESPONSE_SUCCESS;
284 }
285
286 int miracle_pause( command_argument cmd_arg )
287 {
288         miracle_unit unit = miracle_get_unit(cmd_arg->unit);
289         if ( unit == NULL )
290                 return RESPONSE_INVALID_UNIT;
291         else 
292                 miracle_unit_play( unit, 0 );
293         return RESPONSE_SUCCESS;
294 }
295
296 int miracle_rewind( command_argument cmd_arg )
297 {
298         miracle_unit unit = miracle_get_unit(cmd_arg->unit);
299         if ( unit == NULL )
300                 return RESPONSE_INVALID_UNIT;
301         else 
302                 miracle_unit_play( unit, -2000 );
303         return RESPONSE_SUCCESS;
304 }
305
306 int miracle_step( command_argument cmd_arg )
307 {
308         miracle_unit unit = miracle_get_unit(cmd_arg->unit);
309         
310         if (unit == NULL)
311                 return RESPONSE_INVALID_UNIT;
312         else
313         {
314                 miracle_unit_play( unit, 0 );
315                 miracle_unit_step( unit, *(int*) cmd_arg->argument );
316         }
317         return RESPONSE_SUCCESS;
318 }
319
320 int miracle_goto( command_argument cmd_arg )
321 {
322         miracle_unit unit = miracle_get_unit(cmd_arg->unit);
323         int clip = parse_clip( cmd_arg, 3 );
324         
325         if (unit == NULL || miracle_unit_is_offline(unit))
326                 return RESPONSE_INVALID_UNIT;
327         else
328                 miracle_unit_change_position( unit, clip, *(int*) cmd_arg->argument );
329         return RESPONSE_SUCCESS;
330 }
331
332 int miracle_ff( command_argument cmd_arg )
333 {
334         miracle_unit unit = miracle_get_unit(cmd_arg->unit);
335         if ( unit == NULL )
336                 return RESPONSE_INVALID_UNIT;
337         else 
338                 miracle_unit_play( unit, 2000 );
339         return RESPONSE_SUCCESS;
340 }
341
342 int miracle_set_in_point( command_argument cmd_arg )
343 {
344         miracle_unit unit = miracle_get_unit(cmd_arg->unit);
345         int clip = parse_clip( cmd_arg, 3 );
346
347         if ( unit == NULL )
348                 return RESPONSE_INVALID_UNIT;
349         else
350         {
351                 int position = *(int *) cmd_arg->argument;
352
353                 switch( miracle_unit_set_clip_in( unit, clip, position ) )
354                 {
355                         case -1:
356                                 return RESPONSE_BAD_FILE;
357                         case -2:
358                                 return RESPONSE_OUT_OF_RANGE;
359                 }
360         }
361         return RESPONSE_SUCCESS;
362 }
363
364 int miracle_set_out_point( command_argument cmd_arg )
365 {
366         miracle_unit unit = miracle_get_unit(cmd_arg->unit);
367         int clip = parse_clip( cmd_arg, 3 );
368         
369         if ( unit == NULL )
370                 return RESPONSE_INVALID_UNIT;
371         else
372         {
373                 int position = *(int *) cmd_arg->argument;
374
375                 switch( miracle_unit_set_clip_out( unit, clip, position ) )
376                 {
377                         case -1:
378                                 return RESPONSE_BAD_FILE;
379                         case -2:
380                                 return RESPONSE_OUT_OF_RANGE;
381                 }
382         }
383
384         return RESPONSE_SUCCESS;
385 }
386
387 int miracle_get_unit_status( command_argument cmd_arg )
388 {
389         valerie_status_t status;
390         int error = miracle_unit_get_status( miracle_get_unit( cmd_arg->unit ), &status );
391
392         if ( error == -1 )
393                 return RESPONSE_INVALID_UNIT;
394         else
395         {
396                 char text[ 10240 ];
397                 valerie_response_printf( cmd_arg->response, sizeof( text ), valerie_status_serialise( &status, text, sizeof( text ) ) );
398                 return RESPONSE_SUCCESS_1;
399         }
400         return 0;
401 }
402
403
404 int miracle_set_unit_property( command_argument cmd_arg )
405 {
406         miracle_unit unit = miracle_get_unit(cmd_arg->unit);
407         char *name_value = (char*) cmd_arg->argument;
408         if (unit == NULL)
409                 return RESPONSE_INVALID_UNIT;
410         else
411                 miracle_unit_set( unit, name_value );
412         return RESPONSE_SUCCESS;
413 }
414
415 int miracle_get_unit_property( command_argument cmd_arg )
416 {
417         miracle_unit unit = miracle_get_unit(cmd_arg->unit);
418         char *name = (char*) cmd_arg->argument;
419         char *value = miracle_unit_get( unit, name );
420         if (unit == NULL)
421                 return RESPONSE_INVALID_UNIT;
422         else if ( value != NULL )
423                 valerie_response_printf( cmd_arg->response, 1024, "%s\n", value );
424         return RESPONSE_SUCCESS;
425 }
426
427
428 int miracle_transfer( command_argument cmd_arg )
429 {
430         miracle_unit src_unit = miracle_get_unit(cmd_arg->unit);
431         int dest_unit_id = -1;
432         char *string = (char*) cmd_arg->argument;
433         if ( string != NULL && ( string[ 0 ] == 'U' || string[ 0 ] == 'u' ) && strlen( string ) > 1 )
434                 dest_unit_id = atoi( string + 1 );
435         
436         if ( src_unit != NULL && dest_unit_id != -1 )
437         {
438                 miracle_unit dest_unit = miracle_get_unit( dest_unit_id );
439                 if ( dest_unit != NULL && !miracle_unit_is_offline(dest_unit) && dest_unit != src_unit )
440                 {
441                         miracle_unit_transfer( dest_unit, src_unit );
442                         return RESPONSE_SUCCESS;
443                 }
444         }
445         return RESPONSE_INVALID_UNIT;
446 }