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