1 /*****************************************************************************
2 * intf_ctrl.c: interface commands access to control functions
4 *****************************************************************************
5 * Library of functions common to all interfaces, allowing access to various
6 * structures and settings. Interfaces should only use those functions
7 * to read or write informations from other threads.
8 * A control function must be declared in the `local prototypes' section (it's
9 * type is fixed), and copied into the control_command array. Functions should
10 * be listed in alphabetical order, so when `help' is called they are also
11 * displayed in this order.
12 * A control function can use any function of the program, but should respect
13 * two points: first, it should not block, since if it does so, the whole
14 * interface thread will hang and in particular miscelannous interface events
15 * won't be handled. Secondly, it should send it's output messages exclusively
16 * with intf_IntfMsg() function, except particularly critical messages which
17 * can use over intf_*Msg() functions.
18 * Control functions should return 0 (INTF_NO_ERROR) on success, or one of the
19 * error codes defined in command.h. Custom error codes are allowed, but should
21 * More informations about parameters stand in `list of commands' section.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
33 static int Demo ( int i_argc, intf_arg_t *p_argv );
34 static int DisplayImage ( int i_argc, intf_arg_t *p_argv );
35 static int Exec ( int i_argc, intf_arg_t *p_argv );
36 static int Help ( int i_argc, intf_arg_t *p_argv );
37 static int PlayAudio ( int i_argc, intf_arg_t *p_argv );
38 static int PlayVideo ( int i_argc, intf_arg_t *p_argv );
39 static int Quit ( int i_argc, intf_arg_t *p_argv );
40 static int SelectPID ( int i_argc, intf_arg_t *p_argv );
41 static int SpawnInput ( int i_argc, intf_arg_t *p_argv );
43 static int Test ( int i_argc, intf_arg_t *p_argv );
45 static int Vlan ( int i_argc, intf_arg_t *p_argv );
46 static int Psi ( int i_argc, intf_arg_t *p_argv );
50 * This list is used by intf_ExecCommand function to find functions to
51 * execute and prepare its arguments. It is terminated by an element which name
52 * is a null pointer. intf_command_t is defined in command.h.
54 * Here is a description of a command description elements:
55 * name is the name of the function, as it should be typed on command line,
56 * function is a pointer to the control function,
57 * format is an argument descriptor (see below),
58 * summary is a text string displayed in regard of the command name when `help'
59 * is called without parameters, and whith usage on syntax error,
60 * usage is a short syntax indicator displayed with summary when the command
61 * causes a syntax error,
62 * help is a complete help about the command, displayed when `help' is called with
63 * the command name as parameter.
65 * Format string is a list of ' ' separated strings, which have following
70 * ? optionnal argument
71 * * argument can be repeated
72 * name= named argument
73 * Example: "channel=i? s*? i " means that any number of string arguments,
74 * followed by a single mandatory integer argument are waited. A named argument,
75 * which name is `channel' and must have an integer value can be optionnaly
76 * specified at beginning. The last space is mandatory if there is at least one
77 * element, since it acts as an item terminator.
78 * Named arguments MUST be at the beginning of the format string, and in
79 * alphabetic order, but their order on command line has no importance.
80 * The format string can't have more than INTF_MAX_ARGS elements.
82 const intf_command_t control_command[] =
84 { "demo", Demo, /* demo */
86 /* summary: */ "program demonstration",
88 /* help: */ "Start program capabilities demonstration." },
89 { "display", DisplayImage, /* display */
91 /* summary: */ "load and display an image",
92 /* usage: */ "display <file>",
93 /* help: */ "Load and display an image. Image format is automatically "\
94 "identified from file name extension." },
95 { "exec", Exec, /* exec */
97 /* summary: */ "execute a script file",
98 /* usage: */ "exec <file>",
99 /* help: */ "Load an execute a script." },
100 { "exit", Quit, /* exit (quit alias) */
102 /* summary: */ "quit program",
104 /* help: */ "see `quit'." },
105 { "help", Help, /* help */
107 /* summary: */ "list all functions or print help about a specific function",
108 /* usage: */ "help [command]",
109 /* help: */ "If called without argument, list all available " \
110 " functions.\nIf a command name is provided as argument, displays a short "\
111 "inline help about the command.\n" },
112 { "play-audio", PlayAudio, /* play-audio */
113 /* format: */ "channels=i? rate=i? s ",
114 /* summary: */ "play an audio file",
115 /* usage: */ "play-audio [channels=1/2] [rate=r] <file>",
116 /* help: */ "Load and play an audio file." },
117 { "play-video", PlayVideo, /* play-video */
119 /* summary: */ "play a video (.vlp) file",
120 /* usage: */ "play-video <file>",
121 /* help: */ "Load and play a video file." },
122 { "quit", Quit, /* quit */
124 /* summary: */ "quit program",
126 /* help: */ "Terminates the program execution... There is not much to"\
128 { "select-pid", SelectPID, /* select-pid */
129 /* format: */ "i i ",
130 /* summary: */ "spawn a decoder thread for a specified PID",
131 /* summary: */ "select-pid <input> <pid>",
132 /* help: */ "Spawn a decoder thread for <pid>. The stream will be" \
133 " received by <input>." },
134 { "spawn-input", SpawnInput, /* spawn-input */
135 /* format: */ "method=i? filename=s? hostname=s? ip=s? port=i? vlan=i?",
136 /* summary: */ "spawn an input thread",
137 /* summary: */ "spawn-input [method=<method>]\n" \
138 "[filename=<file>|hostname=<hostname>|ip=<ip>]\n" \
139 "[port=<port>] [vlan=<vlan>]",
140 /* help: */ "Spawn an input thread. Method is 10, 20, 21, 22, 32, "\
141 "hostname is the fully-qualified domain name, ip is a dotted-decimal address." },
143 { "test", Test, /* test */
145 /* summary: */ "crazy developper's test",
146 /* usage: */ "depends on the last coder :-)",
147 /* help: */ "`test' works only in DEBUG mode, and is provide for " \
148 "developpers as an easy way to test part of their code. If you don't know "\
149 "what it should do, just try !" },
152 /* format: */ "intf=s? s i? ",
153 /* summary: */ "vlan operations",
154 /* usage: */ "vlan synchro\n" \
155 "vlan [intf=<interface>] request\n" \
156 "vlan [intf=<interface>] join <vlan>\n" \
157 "vlan [intf=<interface>] leave",
158 /* help: */ "Perform various operations on vlans. 'synchro' resynchronize " \
159 "with the server. 'request' ask which is the current vlan (for the default "\
160 "interface or for a given one). 'join' and 'leave' try to change vlan." },
163 /* summary: */ "Dump PSI tables",
164 /* usage: */ "psi <input thread index>",
165 /* help: */ "Display the PSI tables on the console. Warning: this is debug" \
166 "command, it can leads to pb since locks are not taken yet" },
167 { 0, 0, 0, 0, 0 } /* array terminator */
170 /* following functions are local */
172 /*****************************************************************************
174 *****************************************************************************
175 * This function is provided to display a demo of program possibilities.
176 *****************************************************************************/
177 static int Demo( int i_argc, intf_arg_t *p_argv )
179 intf_IntfMsg( COPYRIGHT_MESSAGE );
181 return( INTF_NO_ERROR );
184 /*****************************************************************************
185 * Exec: execute a script
186 *****************************************************************************
187 * This function load and execute a script.
188 *****************************************************************************/
189 static int Exec( int i_argc, intf_arg_t *p_argv )
191 int i_err; /* error code */
193 i_err = intf_ExecScript( p_argv[1].psz_str );
194 return( i_err ? INTF_OTHER_ERROR : INTF_NO_ERROR );
197 /*****************************************************************************
198 * DisplayImage: load and display an image (ok ?)
199 *****************************************************************************
200 * Try to load an image identified by it's filename and displays it as a still
201 * image using interface video heap.
202 *****************************************************************************/
203 static int DisplayImage( int i_argc, intf_arg_t *p_argv )
206 return( INTF_NO_ERROR );
209 /*****************************************************************************
210 * Help: list all available commands (ok ?)
211 *****************************************************************************
212 * This function print a list of available commands
213 *****************************************************************************/
214 static int Help( int i_argc, intf_arg_t *p_argv )
216 int i_index; /* command index */
218 /* If called with an argument: look for the command and display it's help */
221 fprintf( stderr, "maxx debug: coin\n" );
222 for( i_index = 0; control_command[i_index].psz_name
223 && strcmp( control_command[i_index].psz_name, p_argv[1].psz_str );
228 fprintf( stderr, "maxx debug: meuh\n" );
229 /* Command has been found in list */
230 if( control_command[i_index].psz_name )
232 fprintf( stderr, "maxx debug: meow\n" );
233 intf_IntfMsg( control_command[i_index].psz_usage );
234 fprintf( stderr, "maxx debug: blah\n" );
235 intf_IntfMsg( control_command[i_index].psz_help );
236 fprintf( stderr, "maxx debug: blih\n" );
238 /* Command is unknown */
241 intf_IntfMsg("help: don't know command `%s'", p_argv[1].psz_str);
242 return( INTF_OTHER_ERROR );
245 /* If called without argument: print all commands help field */
248 for( i_index = 0; control_command[i_index].psz_name; i_index++ )
250 intf_IntfMsg( "%s: %s", control_command[i_index].psz_name,
251 control_command[i_index].psz_summary );
255 return( INTF_NO_ERROR );
258 /*****************************************************************************
259 * PlayAudio: play an audio file (ok ?)
260 *****************************************************************************
261 * Play a raw audio file from a file, at a given rate.
262 *****************************************************************************/
263 static int PlayAudio( int i_argc, intf_arg_t *p_argv )
265 char * psz_file = NULL; /* name of the audio raw file (s16) */
266 int i_fd; /* file descriptor of the audio file that is to be loaded */
267 aout_fifo_t fifo; /* fifo stores the informations about the file */
268 struct stat stat_buffer; /* needed to find out the size of psz_file */
269 int i_arg; /* argument index */
271 if ( !p_main->b_audio ) /* audio is disabled */
273 intf_IntfMsg("play-audio error: audio is disabled");
274 return( INTF_NO_ERROR );
277 /* Set default configuration */
278 fifo.i_channels = 1 + ( fifo.b_stereo = AOUT_DEFAULT_STEREO );
279 fifo.l_rate = AOUT_DEFAULT_RATE;
281 /* The channels and rate parameters are essential ! */
282 /* Parse parameters - see command list above */
283 for ( i_arg = 1; i_arg < i_argc; i_arg++ )
285 switch( p_argv[i_arg].i_index )
287 case 0: /* channels */
288 fifo.i_channels = p_argv[i_arg].i_num;
291 fifo.l_rate = p_argv[i_arg].i_num;
293 case 2: /* filename */
294 psz_file = p_argv[i_arg].psz_str;
299 /* Setting up the type of the fifo */
300 switch ( fifo.i_channels )
303 fifo.i_type = AOUT_INTF_MONO_FIFO;
307 fifo.i_type = AOUT_INTF_STEREO_FIFO;
311 intf_IntfMsg("play-audio error: channels must be 1 or 2");
312 return( INTF_OTHER_ERROR );
316 i_fd = open( psz_file, O_RDONLY );
317 if ( i_fd < 0 ) /* error */
319 intf_IntfMsg("play-audio error: can't open `%s'", psz_file);
320 return( INTF_OTHER_ERROR );
323 /* Get file size to calculate number of audio units */
324 fstat( i_fd, &stat_buffer );
325 fifo.l_units = ( long )( stat_buffer.st_size / (sizeof(s16) << fifo.b_stereo) );
327 /* Allocate memory, read file and close it */
328 if ( (fifo.buffer = malloc(sizeof(s16)*(fifo.l_units << fifo.b_stereo))) == NULL ) /* !! */
330 intf_IntfMsg("play-audio error: not enough memory to read `%s'", psz_file );
331 close( i_fd ); /* close file */
332 return( INTF_OTHER_ERROR );
334 if ( read(i_fd, fifo.buffer, sizeof(s16)*(fifo.l_units << fifo.b_stereo))
335 != sizeof(s16)*(fifo.l_units << fifo.b_stereo) )
337 intf_IntfMsg("play-audio error: can't read %s", psz_file);
340 return( INTF_OTHER_ERROR );
344 /* Now we can work out how many output units we can compute with the fifo */
345 fifo.l_units = (long)(((s64)fifo.l_units*(s64)p_main->p_aout->l_rate)/(s64)fifo.l_rate);
347 /* Create the fifo */
348 if ( aout_CreateFifo(p_main->p_aout, &fifo) == NULL )
350 intf_IntfMsg("play-audio error: can't create audio fifo");
352 return( INTF_OTHER_ERROR );
355 return( INTF_NO_ERROR );
358 /*****************************************************************************
359 * PlayVideo: play a video sequence from a file
360 *****************************************************************************
362 *****************************************************************************/
363 static int PlayVideo( int i_argc, intf_arg_t *p_argv )
366 return( INTF_NO_ERROR );
369 /*****************************************************************************
370 * Quit: quit program (ok ?)
371 *****************************************************************************
372 * This function set `die' flag of interface, asking the program to terminate.
373 *****************************************************************************/
374 static int Quit( int i_argc, intf_arg_t *p_argv )
376 p_main->p_intf->b_die = 1;
377 return( INTF_NO_ERROR );
381 /*****************************************************************************
383 *****************************************************************************
385 *****************************************************************************/
386 static int SelectPID( int i_argc, intf_arg_t *p_argv )
388 int i_input = -1, i_pid = -1;
391 /* Parse parameters - see command list above */
392 for ( i_arg = 1; i_arg < i_argc; i_arg++ )
394 switch( p_argv[i_arg].i_index )
398 i_input = p_argv[i_arg].i_num;
401 i_pid = p_argv[i_arg].i_num;
407 /* Find to which input this command is destinated */
408 intf_IntfMsg( "Adding PID %d to input %d\n", i_pid, i_input );
409 //???? input_AddPgrmElem( p_main->p_intf->p_x11->p_input,
411 return( INTF_NO_ERROR );
415 /*****************************************************************************
416 * SpawnInput: spawn an input thread (ok ?)
417 *****************************************************************************
418 * Spawn an input thread
419 *****************************************************************************/
420 static int SpawnInput( int i_argc, intf_arg_t *p_argv )
423 int i_method = 0; /* method parameter */
424 char * psz_source = NULL; /* source parameter */
425 int i_port = 0; /* port parameter */
426 int i_vlan = 0; /* vlan parameter */
428 /* Parse parameters - see command list above */
429 for ( i_arg = 1; i_arg < i_argc; i_arg++ )
431 switch( p_argv[i_arg].i_index )
434 i_method = p_argv[i_arg].i_num;
436 case 1: /* filename, hostname, ip */
439 psz_source = p_argv[i_arg].psz_str;
442 i_port = p_argv[i_arg].i_num;
445 i_vlan = p_argv[i_arg].i_num;
450 /* Destroy current input, if any */
451 if( p_main->p_intf->p_input != NULL )
453 input_DestroyThread( p_main->p_intf->p_input, NULL );
456 p_main->p_intf->p_input = input_CreateThread( i_method, psz_source, i_port, i_vlan,
457 p_main->p_intf->p_vout, p_main->p_aout,
459 return( INTF_NO_ERROR );
462 /*****************************************************************************
463 * Test: test function
464 *****************************************************************************
465 * This function is provided to test new functions in the program. Fell free
467 * This function is only defined in DEBUG mode.
468 *****************************************************************************/
470 static int Test( int i_argc, intf_arg_t *p_argv )
474 /*?? if( i_argc == 1 )
476 i_thread = intf_CreateVoutThread( &p_main->intf_thread, NULL, -1, -1);
477 intf_IntfMsg("return value: %d", i_thread );
481 i_thread = p_argv[1].i_num;
482 //?? intf_DestroyVoutThread( &p_main->intf_thread, i_thread );
485 return( INTF_NO_ERROR );
489 /*****************************************************************************
490 * Vlan: vlan operations
491 *****************************************************************************
492 * This function performs various vlan operations.
493 *****************************************************************************/
494 static int Vlan( int i_argc, intf_arg_t *p_argv )
496 int i_command; /* command argument number */
498 /* Do not try anything if vlans are desactivated */
499 if( !p_main->b_vlans )
501 intf_IntfMsg("vlans are desactivated");
502 return( INTF_OTHER_ERROR );
505 /* Look for command in list of arguments - this argument is mandatory and
506 * imposed by the calling function */
507 for( i_command = 1; p_argv[i_command].i_index == 1; i_command++ )
512 /* Command is 'join' */
513 if( !strcmp(p_argv[i_command].psz_str, "join") )
517 /* Command is 'leave' */
518 else if( !strcmp(p_argv[i_command].psz_str, "leave") )
522 /* Command is unknown */
525 intf_IntfMsg("vlan error: unknown command %s", p_argv[i_command].psz_str );
526 return( INTF_USAGE_ERROR );
529 return( INTF_NO_ERROR );
533 /*****************************************************************************
535 *****************************************************************************
536 * This function is provided to display PSI tables.
537 *****************************************************************************/
538 static int Psi( int i_argc, intf_arg_t *p_argv )
540 int i_index = p_argv[1].i_num;
542 intf_IntfMsg("Reading PSI table for input %d\n", i_index);
543 //???? input_PsiRead(p_main->p_intf->p_x11->p_input );
544 return( INTF_NO_ERROR );