1 /*****************************************************************************
2 * vlc_internal.c: vlcinternal python binding module
3 *****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
7 * Authors: Olivier Aubert <oaubert at bat710.univ-lyon1.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #include "vlc_internal.h"
26 /**************************************************************************
28 **************************************************************************/
30 #ifndef vlcinternalMODINIT_FUNC /* declarations for DLL import/export */
31 #define vlcinternalMODINIT_FUNC void
34 static PyMethodDef vlcinternal_methods[] = {
35 { NULL } /* Sentinel */
38 vlcinternalMODINIT_FUNC
39 initvlcinternal( void )
43 p_module = Py_InitModule3( "vlcinternal", vlcinternal_methods,
44 "VLC media player internal module" );
49 if( PyType_Ready( &vlcObject_Type ) < 0 )
53 Py_INCREF( &vlcObject_Type );
54 PyModule_AddObject( p_module, "Object",
55 ( PyObject * )&vlcObject_Type );
59 /* Make libpostproc happy... */
60 void * fast_memcpy( void * to, const void * from, size_t len )
62 return memcpy( to, from, len );
65 /*****************************************************************************
66 * VLCObject implementation
67 *****************************************************************************/
70 *vlcObject_new( PyTypeObject *p_type, PyObject *p_args, PyObject *p_kwds )
73 vlc_object_t *p_object;
76 self = PyObject_New( vlcObject, &vlcObject_Type );
78 if( !PyArg_ParseTuple( p_args, "i", &i_id ) )
81 /* Maybe we were already initialized */
82 p_object = ( vlc_object_t* )vlc_current_object( i_id );
86 /* Try to initialize */
90 PyErr_SetString( PyExc_StandardError, "Unable to create a VLC instance." );
93 p_object = ( vlc_object_t* )vlc_current_object( i_id );
98 PyErr_SetString( PyExc_StandardError, "Unable to get object." );
102 self->p_object = p_object;
103 self->b_released = 0;
105 Py_INCREF( self ); /* Ah bon ? */
106 return ( PyObject * )self;
110 vlcObject_release( PyObject *self, PyObject *p_args )
112 if( VLCSELF->b_released == 0 )
114 vlc_object_release( VLCSELF->p_object );
115 VLCSELF->b_released = 1;
117 Py_INCREF( Py_None );
122 vlcObject_dealloc( PyObject *self )
124 vlcObject_release( self, NULL );
129 vlcObject_find_object( PyObject *self, PyObject *args )
136 if( !PyArg_ParseTuple( args, "s", &psz_name ) )
140 ( aout, decoder, input, httpd, intf, playlist, root, vlc, vout )
142 if( !strncmp( psz_name, "aout", 4 ) )
144 i_object_type = VLC_OBJECT_AOUT;
146 else if (! strncmp( psz_name, "decoder", 7 ) )
148 i_object_type = VLC_OBJECT_DECODER;
150 else if (! strncmp( psz_name, "httpd", 5 ) )
152 i_object_type = VLC_OBJECT_HTTPD;
154 else if (! strncmp( psz_name, "intf", 4 ) )
156 i_object_type = VLC_OBJECT_INTF;
158 else if (! strncmp( psz_name, "input", 5 ) )
160 i_object_type = VLC_OBJECT_INPUT;
162 else if (! strncmp( psz_name, "playlist", 8 ) )
164 i_object_type = VLC_OBJECT_PLAYLIST;
166 else if (! strncmp( psz_name, "libvlc", 6 ) )
168 i_object_type = VLC_OBJECT_LIBVLC;
170 else if (! strncmp( psz_name, "vout", 4 ) )
172 i_object_type = VLC_OBJECT_VOUT;
174 else if (! strncmp( psz_name, "sout", 4 ) )
176 i_object_type = VLC_OBJECT_SOUT;
178 else if (! strncmp( psz_name, "global", 6 ) )
180 i_object_type = VLC_OBJECT_GLOBAL;
182 else if (! strncmp( psz_name, "packetizer", 10 ) )
184 i_object_type = VLC_OBJECT_PACKETIZER;
186 else if (! strncmp( psz_name, "encoder", 7 ) )
188 i_object_type = VLC_OBJECT_ENCODER;
190 else if (! strncmp( psz_name, "vlm", 3 ) )
192 i_object_type = VLC_OBJECT_VLM;
194 else if (! strncmp( psz_name, "announce", 8 ) )
196 i_object_type = VLC_OBJECT_ANNOUNCE;
198 else if (! strncmp( psz_name, "demux", 5 ) )
200 i_object_type = VLC_OBJECT_DEMUX;
202 else if (! strncmp( psz_name, "access", 6 ) )
204 i_object_type = VLC_OBJECT_ACCESS;
206 else if (! strncmp( psz_name, "stream", 6 ) )
208 i_object_type = VLC_OBJECT_STREAM;
210 else if (! strncmp( psz_name, "filter", 6 ) )
212 i_object_type = VLC_OBJECT_FILTER;
214 else if (! strncmp( psz_name, "vod", 3 ) )
216 i_object_type = VLC_OBJECT_VOD;
218 else if (! strncmp( psz_name, "xml", 3 ) )
220 i_object_type = VLC_OBJECT_XML;
222 else if (! strncmp( psz_name, "osdmenu", 7 ) )
224 i_object_type = VLC_OBJECT_OSDMENU;
226 else if (! strncmp( psz_name, "stats", 5 ) )
228 i_object_type = VLC_OBJECT_STATS;
230 else if (! strncmp( psz_name, "metaengine", 10 ) )
232 i_object_type = VLC_OBJECT_META_ENGINE;
236 /* FIXME: raise an exception ? */
237 Py_INCREF( Py_None );
241 p_obj = vlc_object_find( VLCSELF->p_object, i_object_type, FIND_ANYWHERE );
245 Py_INCREF( Py_None );
249 p_retval = PyObject_New( vlcObject, &vlcObject_Type );
251 p_retval->p_object = p_obj;
253 return ( PyObject * )p_retval;
257 vlcObject_info( PyObject *self, PyObject *args )
262 p_obj = VLCSELF->p_object;
264 /* Return information about the object as a dict. */
265 p_retval = PyDict_New();
267 PyDict_SetItemString( p_retval, "object-id",
268 Py_BuildValue( "l", p_obj->i_object_id ) );
269 PyDict_SetItemString( p_retval, "object-type",
270 Py_BuildValue( "s", p_obj->psz_object_type ) );
271 PyDict_SetItemString( p_retval, "object-name",
272 Py_BuildValue( "s", p_obj->psz_object_name ) );
273 PyDict_SetItemString( p_retval, "thread",
274 PyBool_FromLong( p_obj->b_thread ) );
275 PyDict_SetItemString( p_retval, "thread-id",
276 PyLong_FromLongLong( p_obj->thread_id ) );
277 PyDict_SetItemString( p_retval, "refcount",
278 PyInt_FromLong( p_obj->i_refcount ) );
284 vlcObject_find_id( PyObject *self, PyObject *args )
287 vlc_object_t* p_object;
290 if( !PyArg_ParseTuple( args, "i", &i_id ) )
293 p_object = ( vlc_object_t* )vlc_current_object( i_id );
297 Py_INCREF( Py_None );
301 p_retval = PyObject_NEW( vlcObject, &vlcObject_Type );
303 p_retval->p_object = p_object;
305 return ( PyObject * )p_retval;
308 /* Do a var_Get call on the object. Parameter: the variable name. */
310 vlcObject_var_get( PyObject *self, PyObject *args )
317 if( !PyArg_ParseTuple( args, "s", &psz_name ) )
320 if( var_Get( VLCSELF->p_object, psz_name, &value ) != VLC_SUCCESS )
322 PyErr_SetString( PyExc_StandardError,
323 "Error: variable does not exist.\n" );
327 i_type = var_Type ( VLCSELF->p_object, psz_name );
332 p_retval = PyString_FromString( "A void variable" );
335 p_retval = PyBool_FromLong( value.b_bool );
337 case VLC_VAR_INTEGER :
338 p_retval = PyInt_FromLong( ( long )value.i_int );
340 case VLC_VAR_HOTKEY :
341 p_retval = PyString_FromFormat( "A hotkey variable ( %d )", value.i_int );
344 case VLC_VAR_STRING :
345 case VLC_VAR_DIRECTORY :
346 case VLC_VAR_VARIABLE :
347 p_retval = PyString_FromString( value.psz_string );
349 case VLC_VAR_MODULE :
350 p_retval = ( PyObject* )PyObject_New( vlcObject, &vlcObject_Type );
351 ( ( vlcObject* )p_retval )->p_object = value.p_object;
354 p_retval = PyFloat_FromDouble( ( double )value.f_float );
357 p_retval = PyLong_FromLongLong( value.i_time );
359 case VLC_VAR_ADDRESS :
360 p_retval = PyString_FromString( "A VLC address ( not handled yet )" );
363 p_retval = PyString_FromString( "A VLC list ( not handled yet )" );
366 p_retval = PyString_FromString( "A mutex" );
372 Py_INCREF( p_retval );
377 vlcObject_var_type( PyObject *self, PyObject *args )
383 if( !PyArg_ParseTuple( args, "s", &psz_name ) )
386 i_type = var_Type( VLCSELF->p_object, psz_name );
391 p_retval = PyString_FromString( "Void" );
394 p_retval = PyString_FromString( "Boolean" );
396 case VLC_VAR_INTEGER :
397 p_retval = PyString_FromString( "Integer" );
399 case VLC_VAR_HOTKEY :
400 p_retval = PyString_FromString( "Hotkey" );
403 p_retval = PyString_FromString( "File" );
405 case VLC_VAR_STRING :
406 p_retval = PyString_FromString( "String" );
408 case VLC_VAR_DIRECTORY :
409 p_retval = PyString_FromString( "Directory" );
411 case VLC_VAR_VARIABLE :
412 p_retval = PyString_FromString( "Variable" );
414 case VLC_VAR_MODULE :
415 p_retval = PyString_FromString( "Module" );
418 p_retval = PyString_FromString( "Float" );
421 p_retval = PyString_FromString( "Time" );
423 case VLC_VAR_ADDRESS :
424 p_retval = PyString_FromString( "Address" );
427 p_retval = PyString_FromString( "List" );
430 p_retval = PyString_FromString( "Mutex" );
433 p_retval = PyString_FromString( "Unknown" );
438 /* Do a var_Set call on the object. Parameter: the variable name. */
440 vlcObject_var_set( PyObject *self, PyObject *args )
448 if( !PyArg_ParseTuple( args, "sO", &psz_name, &py_value ) )
451 p_obj = VLCSELF->p_object;
452 i_type = var_Type( p_obj, psz_name );
459 value.b_bool = PyInt_AsLong( py_value );
461 case VLC_VAR_INTEGER :
462 case VLC_VAR_HOTKEY :
463 value.i_int = PyInt_AsLong( py_value );
466 case VLC_VAR_STRING :
467 case VLC_VAR_DIRECTORY :
468 case VLC_VAR_VARIABLE :
469 value.psz_string = strdup( PyString_AsString( py_value ) );
471 case VLC_VAR_MODULE :
472 /* FIXME: we should check the PyObject type and get its p_object */
473 value.p_object = ( ( vlcObject* )p_obj )->p_object;
476 value.f_float = PyFloat_AsDouble( py_value );
479 value.i_time = PyLong_AsLongLong( py_value );
481 case VLC_VAR_ADDRESS :
482 value.p_address = ( char* )PyLong_AsVoidPtr( py_value );
492 var_Set( p_obj, psz_name, value );
494 Py_INCREF( Py_None );
499 vlcObject_var_list( PyObject *self, PyObject *args )
505 i_size = VLCSELF->p_object->i_vars;
506 p_retval = PyTuple_New( i_size );
508 for ( i_index = 0 ; i_index < i_size ; i_index++ )
510 PyTuple_SetItem( p_retval, i_index,
511 Py_BuildValue( "s", VLCSELF->p_object->p_vars[i_index].psz_name ) );
517 /* Do a config_Get call on the object. Parameter: the variable name. */
519 vlcObject_config_get( PyObject *self, PyObject *args )
524 module_config_t *p_config;
526 if( !PyArg_ParseTuple( args, "s", &psz_name ) )
529 p_config = config_FindConfig( VLCSELF->p_object, psz_name );
533 PyErr_SetString( PyExc_StandardError,
534 "Error: config variable does not exist.\n" );
538 switch ( p_config->i_type )
540 case CONFIG_ITEM_BOOL :
541 p_retval = PyBool_FromLong( p_config->value.i );
543 case CONFIG_ITEM_INTEGER :
544 p_retval = PyInt_FromLong( ( long )p_config->value.i );
546 case CONFIG_ITEM_KEY :
547 p_retval = PyString_FromFormat( "A hotkey variable ( %d )", p_config->value.i );
549 case CONFIG_ITEM_FILE :
550 case CONFIG_ITEM_STRING :
551 case CONFIG_ITEM_DIRECTORY :
552 case CONFIG_ITEM_MODULE :
553 vlc_mutex_lock( p_config->p_lock );
554 if( p_config->value.psz )
555 p_retval = PyString_FromString( p_config->value.psz );
557 p_retval = PyString_FromString( "" );
558 vlc_mutex_unlock( p_config->p_lock );
560 p_retval = ( PyObject* )PyObject_New( vlcObject, &vlcObject_Type );
561 ( ( vlcObject* )p_retval )->p_object = value.p_object;
563 case CONFIG_ITEM_FLOAT :
564 p_retval = PyFloat_FromDouble( ( double )p_config->value.f );
568 Py_INCREF( p_retval );
574 /* Do a config_put* call on the object. Parameter: the variable name. */
576 vlcObject_config_set( PyObject *self, PyObject *args )
581 module_config_t *p_config;
584 if( !PyArg_ParseTuple( args, "sO", &psz_name, &py_value ) )
587 p_obj = VLCSELF->p_object;
588 p_config = config_FindConfig( p_obj, psz_name );
592 PyErr_SetString( PyExc_StandardError,
593 "Error: option does not exist.\n" );
597 switch ( p_config->i_type )
599 case CONFIG_ITEM_BOOL :
600 case CONFIG_ITEM_INTEGER :
601 case CONFIG_ITEM_KEY :
602 config_PutInt( p_obj, psz_name, PyInt_AsLong( py_value ) );
604 case CONFIG_ITEM_FILE :
605 case CONFIG_ITEM_STRING :
606 case CONFIG_ITEM_DIRECTORY :
607 case CONFIG_ITEM_MODULE :
608 config_PutPsz( p_obj, psz_name, PyString_AsString( py_value ) );
610 case CONFIG_ITEM_FLOAT :
611 config_PutFloat( p_obj, psz_name, PyFloat_AsDouble( py_value ) );
614 Py_INCREF( Py_None );
619 vlcObject_children( PyObject *self, PyObject *args )
625 i_size = VLCSELF->p_object->i_children;
626 p_retval = PyTuple_New( i_size );
628 for ( i_index = 0 ; i_index < i_size ; i_index++ )
630 PyTuple_SetItem( p_retval, i_index,
632 VLCSELF->p_object->pp_children[i_index]->i_object_id ) );
640 static PyMethodDef vlcObject_methods[] =
642 { "get", vlcObject_var_get, METH_VARARGS,
643 "get( str ) -> value Get a variable value."},
644 { "set", vlcObject_var_set, METH_VARARGS,
645 "set( str, value ) Set a variable value" },
646 { "config_get", vlcObject_config_get, METH_VARARGS,
647 "config_get( str ) -> value Get a configuration option." },
648 { "config_set", vlcObject_config_set, METH_VARARGS,
649 "config_set( str, value ) Set a configuration option" },
650 { "type", vlcObject_var_type, METH_VARARGS,
651 "type( str ) -> str Get a variable type" },
652 { "list", vlcObject_var_list, METH_NOARGS,
653 "list( ) List the available variables" },
654 { "children", vlcObject_children, METH_NOARGS,
655 "children( ) List the children ids" },
656 { "find_object", vlcObject_find_object, METH_VARARGS,
657 "find_object( str ) -> Object Find the object of a given type.\n\nAvailable types are : aout, decoder, input, httpd, intf, playlist, root, vlc, vout"},
658 { "find_id", vlcObject_find_id, METH_VARARGS,
659 "find_id( int ) -> Object Find an object by id" },
660 { "info", vlcObject_info, METH_NOARGS,
661 "info( ) -> dict Return information about the object" },
662 { "release", vlcObject_release, METH_NOARGS,
663 "release( ) -> Release the VLC Object" },
664 { NULL, NULL, 0, NULL },
667 static PyTypeObject vlcObject_Type =
669 PyObject_HEAD_INIT( NULL )
671 "vlc.Object", /*tp_name*/
672 sizeof( vlcObject_Type ), /*tp_basicsize*/
674 ( destructor )vlcObject_dealloc, /*tp_dealloc*/
681 0, /*tp_as_sequence*/
689 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
690 "Expose VLC object infrastructure.\n\nConstructor: vlc.Object(object_id)", /* tp_doc */
693 0, /* tp_richcompare */
694 0, /* tp_weaklistoffset */
697 vlcObject_methods, /* tp_methods */
702 0, /* tp_descr_get */
703 0, /* tp_descr_set */
704 0, /* tp_dictoffset */
707 vlcObject_new, /* tp_new */