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