1 /*****************************************************************************
2 * vlc_object.c: vlc.Object
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 /* We need to access some internal features of VLC */
29 /*****************************************************************************
30 * VLCObject implementation
31 *****************************************************************************/
34 *vlcObject_new( PyTypeObject *p_type, PyObject *p_args, PyObject *p_kwds )
37 vlc_object_t *p_object;
40 self = PyObject_New( vlcObject, &vlcObject_Type );
42 if( !PyArg_ParseTuple( p_args, "i", &i_id ) )
45 /* Maybe we were already initialized */
46 p_object = ( vlc_object_t* )vlc_current_object( i_id );
50 /* Try to initialize */
54 PyErr_SetString( PyExc_StandardError, "Unable to create a VLC instance." );
57 p_object = ( vlc_object_t* )vlc_current_object( i_id );
62 PyErr_SetString( PyExc_StandardError, "Unable to get object." );
66 self->p_object = p_object;
69 Py_INCREF( self ); /* Ah bon ? */
70 return ( PyObject * )self;
74 vlcObject_release( PyObject *self, PyObject *p_args )
76 if( VLCSELF->b_released == 0 )
78 vlc_object_release( VLCSELF->p_object );
79 VLCSELF->b_released = 1;
86 vlcObject_dealloc( PyObject *self )
88 vlcObject_release( self, NULL );
93 vlcObject_find_object( PyObject *self, PyObject *args )
100 if( !PyArg_ParseTuple( args, "s", &psz_name ) )
104 ( aout, decoder, input, httpd, intf, playlist, root, vlc, vout )
106 if( !strncmp( psz_name, "aout", 4 ) )
108 i_object_type = VLC_OBJECT_AOUT;
110 else if (! strncmp( psz_name, "decoder", 7 ) )
112 i_object_type = VLC_OBJECT_DECODER;
114 else if (! strncmp( psz_name, "httpd", 5 ) )
116 i_object_type = VLC_OBJECT_HTTPD;
118 else if (! strncmp( psz_name, "intf", 4 ) )
120 i_object_type = VLC_OBJECT_INTF;
122 else if (! strncmp( psz_name, "input", 5 ) )
124 i_object_type = VLC_OBJECT_INPUT;
126 else if (! strncmp( psz_name, "playlist", 8 ) )
128 i_object_type = VLC_OBJECT_PLAYLIST;
130 else if (! strncmp( psz_name, "libvlc", 6 ) )
132 i_object_type = VLC_OBJECT_LIBVLC;
134 else if (! strncmp( psz_name, "vout", 4 ) )
136 i_object_type = VLC_OBJECT_VOUT;
138 else if (! strncmp( psz_name, "sout", 4 ) )
140 i_object_type = VLC_OBJECT_SOUT;
142 else if (! strncmp( psz_name, "global", 6 ) )
144 i_object_type = VLC_OBJECT_GLOBAL;
146 else if (! strncmp( psz_name, "packetizer", 10 ) )
148 i_object_type = VLC_OBJECT_PACKETIZER;
150 else if (! strncmp( psz_name, "encoder", 7 ) )
152 i_object_type = VLC_OBJECT_ENCODER;
154 else if (! strncmp( psz_name, "vlm", 3 ) )
156 i_object_type = VLC_OBJECT_VLM;
158 else if (! strncmp( psz_name, "announce", 8 ) )
160 i_object_type = VLC_OBJECT_ANNOUNCE;
162 else if (! strncmp( psz_name, "demux", 5 ) )
164 i_object_type = VLC_OBJECT_DEMUX;
166 else if (! strncmp( psz_name, "access", 6 ) )
168 i_object_type = VLC_OBJECT_ACCESS;
170 else if (! strncmp( psz_name, "stream", 6 ) )
172 i_object_type = VLC_OBJECT_STREAM;
174 else if (! strncmp( psz_name, "filter", 6 ) )
176 i_object_type = VLC_OBJECT_FILTER;
178 else if (! strncmp( psz_name, "vod", 3 ) )
180 i_object_type = VLC_OBJECT_VOD;
182 else if (! strncmp( psz_name, "xml", 3 ) )
184 i_object_type = VLC_OBJECT_XML;
186 else if (! strncmp( psz_name, "osdmenu", 7 ) )
188 i_object_type = VLC_OBJECT_OSDMENU;
190 else if (! strncmp( psz_name, "stats", 5 ) )
192 i_object_type = VLC_OBJECT_STATS;
194 else if (! strncmp( psz_name, "metaengine", 10 ) )
196 i_object_type = VLC_OBJECT_META_ENGINE;
200 /* FIXME: raise an exception ? */
201 Py_INCREF( Py_None );
205 p_obj = vlc_object_find( VLCSELF->p_object, i_object_type, FIND_ANYWHERE );
209 Py_INCREF( Py_None );
213 p_retval = PyObject_New( vlcObject, &vlcObject_Type );
215 p_retval->p_object = p_obj;
217 return ( PyObject * )p_retval;
221 vlcObject_info( PyObject *self, PyObject *args )
226 p_obj = VLCSELF->p_object;
228 /* Return information about the object as a dict. */
229 p_retval = PyDict_New();
231 PyDict_SetItemString( p_retval, "object-id",
232 Py_BuildValue( "l", p_obj->i_object_id ) );
233 PyDict_SetItemString( p_retval, "object-type",
234 Py_BuildValue( "s", p_obj->psz_object_type ) );
235 PyDict_SetItemString( p_retval, "object-name",
236 Py_BuildValue( "s", p_obj->psz_object_name ) );
237 PyDict_SetItemString( p_retval, "thread",
238 PyBool_FromLong( p_obj->b_thread ) );
239 PyDict_SetItemString( p_retval, "thread-id",
240 PyLong_FromLongLong( p_obj->thread_id ) );
241 PyDict_SetItemString( p_retval, "refcount",
242 PyInt_FromLong( p_obj->i_refcount ) );
248 vlcObject_find_id( PyObject *self, PyObject *args )
251 vlc_object_t* p_object;
254 if( !PyArg_ParseTuple( args, "i", &i_id ) )
257 p_object = ( vlc_object_t* )vlc_current_object( i_id );
261 Py_INCREF( Py_None );
265 p_retval = PyObject_NEW( vlcObject, &vlcObject_Type );
267 p_retval->p_object = p_object;
269 return ( PyObject * )p_retval;
272 /* Do a var_Get call on the object. Parameter: the variable name. */
274 vlcObject_var_get( PyObject *self, PyObject *args )
281 if( !PyArg_ParseTuple( args, "s", &psz_name ) )
284 if( var_Get( VLCSELF->p_object, psz_name, &value ) != VLC_SUCCESS )
286 PyErr_SetString( PyExc_StandardError,
287 "Error: variable does not exist.\n" );
291 i_type = var_Type ( VLCSELF->p_object, psz_name );
296 p_retval = PyString_FromString( "A void variable" );
299 p_retval = PyBool_FromLong( value.b_bool );
301 case VLC_VAR_INTEGER :
302 p_retval = PyInt_FromLong( ( long )value.i_int );
304 case VLC_VAR_HOTKEY :
305 p_retval = PyString_FromFormat( "A hotkey variable ( %d )", value.i_int );
308 case VLC_VAR_STRING :
309 case VLC_VAR_DIRECTORY :
310 case VLC_VAR_VARIABLE :
311 p_retval = PyString_FromString( value.psz_string );
313 case VLC_VAR_MODULE :
314 p_retval = ( PyObject* )PyObject_New( vlcObject, &vlcObject_Type );
315 ( ( vlcObject* )p_retval )->p_object = value.p_object;
318 p_retval = PyFloat_FromDouble( ( double )value.f_float );
321 p_retval = PyLong_FromLongLong( value.i_time );
323 case VLC_VAR_ADDRESS :
324 p_retval = PyString_FromString( "A VLC address ( not handled yet )" );
327 p_retval = PyString_FromString( "A VLC list ( not handled yet )" );
330 p_retval = PyString_FromString( "A mutex" );
336 Py_INCREF( p_retval );
341 vlcObject_var_type( PyObject *self, PyObject *args )
347 if( !PyArg_ParseTuple( args, "s", &psz_name ) )
350 i_type = var_Type( VLCSELF->p_object, psz_name );
355 p_retval = PyString_FromString( "Void" );
358 p_retval = PyString_FromString( "Boolean" );
360 case VLC_VAR_INTEGER :
361 p_retval = PyString_FromString( "Integer" );
363 case VLC_VAR_HOTKEY :
364 p_retval = PyString_FromString( "Hotkey" );
367 p_retval = PyString_FromString( "File" );
369 case VLC_VAR_STRING :
370 p_retval = PyString_FromString( "String" );
372 case VLC_VAR_DIRECTORY :
373 p_retval = PyString_FromString( "Directory" );
375 case VLC_VAR_VARIABLE :
376 p_retval = PyString_FromString( "Variable" );
378 case VLC_VAR_MODULE :
379 p_retval = PyString_FromString( "Module" );
382 p_retval = PyString_FromString( "Float" );
385 p_retval = PyString_FromString( "Time" );
387 case VLC_VAR_ADDRESS :
388 p_retval = PyString_FromString( "Address" );
391 p_retval = PyString_FromString( "List" );
394 p_retval = PyString_FromString( "Mutex" );
397 p_retval = PyString_FromString( "Unknown" );
402 /* Do a var_Set call on the object. Parameter: the variable name. */
404 vlcObject_var_set( PyObject *self, PyObject *args )
412 if( !PyArg_ParseTuple( args, "sO", &psz_name, &py_value ) )
415 p_obj = VLCSELF->p_object;
416 i_type = var_Type( p_obj, psz_name );
423 value.b_bool = PyInt_AsLong( py_value );
425 case VLC_VAR_INTEGER :
426 case VLC_VAR_HOTKEY :
427 value.i_int = PyInt_AsLong( py_value );
430 case VLC_VAR_STRING :
431 case VLC_VAR_DIRECTORY :
432 case VLC_VAR_VARIABLE :
433 value.psz_string = strdup( PyString_AsString( py_value ) );
435 case VLC_VAR_MODULE :
436 /* FIXME: we should check the PyObject type and get its p_object */
437 value.p_object = ( ( vlcObject* )p_obj )->p_object;
440 value.f_float = PyFloat_AsDouble( py_value );
443 value.i_time = PyLong_AsLongLong( py_value );
445 case VLC_VAR_ADDRESS :
446 value.p_address = ( char* )PyLong_AsVoidPtr( py_value );
456 var_Set( p_obj, psz_name, value );
458 Py_INCREF( Py_None );
463 vlcObject_var_list( PyObject *self, PyObject *args )
469 i_size = VLCSELF->p_object->i_vars;
470 p_retval = PyTuple_New( i_size );
472 for ( i_index = 0 ; i_index < i_size ; i_index++ )
474 PyTuple_SetItem( p_retval, i_index,
475 Py_BuildValue( "s", VLCSELF->p_object->p_vars[i_index].psz_name ) );
481 /* Do a config_Get call on the object. Parameter: the variable name. */
483 vlcObject_config_get( PyObject *self, PyObject *args )
488 module_config_t *p_config;
490 if( !PyArg_ParseTuple( args, "s", &psz_name ) )
493 p_config = config_FindConfig( VLCSELF->p_object, psz_name );
497 PyErr_SetString( PyExc_StandardError,
498 "Error: config variable does not exist.\n" );
502 switch ( p_config->i_type )
504 case CONFIG_ITEM_BOOL :
505 p_retval = PyBool_FromLong( p_config->value.i );
507 case CONFIG_ITEM_INTEGER :
508 p_retval = PyInt_FromLong( ( long )p_config->value.i );
510 case CONFIG_ITEM_KEY :
511 p_retval = PyString_FromFormat( "A hotkey variable ( %d )", p_config->value.i );
513 case CONFIG_ITEM_FILE :
514 case CONFIG_ITEM_STRING :
515 case CONFIG_ITEM_DIRECTORY :
516 case CONFIG_ITEM_MODULE :
517 vlc_mutex_lock( p_config->p_lock );
518 if( p_config->value.psz )
519 p_retval = PyString_FromString( p_config->value.psz );
521 p_retval = PyString_FromString( "" );
522 vlc_mutex_unlock( p_config->p_lock );
524 p_retval = ( PyObject* )PyObject_New( vlcObject, &vlcObject_Type );
525 ( ( vlcObject* )p_retval )->p_object = value.p_object;
527 case CONFIG_ITEM_FLOAT :
528 p_retval = PyFloat_FromDouble( ( double )p_config->value.f );
532 Py_INCREF( p_retval );
538 /* Do a config_put* call on the object. Parameter: the variable name. */
540 vlcObject_config_set( PyObject *self, PyObject *args )
545 module_config_t *p_config;
548 if( !PyArg_ParseTuple( args, "sO", &psz_name, &py_value ) )
551 p_obj = VLCSELF->p_object;
552 p_config = config_FindConfig( p_obj, psz_name );
556 PyErr_SetString( PyExc_StandardError,
557 "Error: option does not exist.\n" );
561 switch ( p_config->i_type )
563 case CONFIG_ITEM_BOOL :
564 case CONFIG_ITEM_INTEGER :
565 case CONFIG_ITEM_KEY :
566 config_PutInt( p_obj, psz_name, PyInt_AsLong( py_value ) );
568 case CONFIG_ITEM_FILE :
569 case CONFIG_ITEM_STRING :
570 case CONFIG_ITEM_DIRECTORY :
571 case CONFIG_ITEM_MODULE :
572 config_PutPsz( p_obj, psz_name, PyString_AsString( py_value ) );
574 case CONFIG_ITEM_FLOAT :
575 config_PutFloat( p_obj, psz_name, PyFloat_AsDouble( py_value ) );
578 Py_INCREF( Py_None );
583 vlcObject_children( PyObject *self, PyObject *args )
589 i_size = VLCSELF->p_object->i_children;
590 p_retval = PyTuple_New( i_size );
592 for ( i_index = 0 ; i_index < i_size ; i_index++ )
594 PyTuple_SetItem( p_retval, i_index,
596 VLCSELF->p_object->pp_children[i_index]->i_object_id ) );
604 static PyMethodDef vlcObject_methods[] =
606 { "get", vlcObject_var_get, METH_VARARGS,
607 "get( str ) -> value Get a variable value."},
608 { "set", vlcObject_var_set, METH_VARARGS,
609 "set( str, value ) Set a variable value" },
610 { "config_get", vlcObject_config_get, METH_VARARGS,
611 "config_get( str ) -> value Get a configuration option." },
612 { "config_set", vlcObject_config_set, METH_VARARGS,
613 "config_set( str, value ) Set a configuration option" },
614 { "type", vlcObject_var_type, METH_VARARGS,
615 "type( str ) -> str Get a variable type" },
616 { "list", vlcObject_var_list, METH_NOARGS,
617 "list( ) List the available variables" },
618 { "children", vlcObject_children, METH_NOARGS,
619 "children( ) List the children ids" },
620 { "find_object", vlcObject_find_object, METH_VARARGS,
621 "find_object( str ) -> Object Find the object of a given type.\n\nAvailable types are : aout, decoder, input, httpd, intf, playlist, root, vlc, vout"},
622 { "find_id", vlcObject_find_id, METH_VARARGS,
623 "find_id( int ) -> Object Find an object by id" },
624 { "info", vlcObject_info, METH_NOARGS,
625 "info( ) -> dict Return information about the object" },
626 { "release", vlcObject_release, METH_NOARGS,
627 "release( ) -> Release the VLC Object" },
628 { NULL, NULL, 0, NULL },
631 static PyTypeObject vlcObject_Type =
633 PyObject_HEAD_INIT( NULL )
635 "vlc.Object", /*tp_name*/
636 sizeof( vlcObject_Type ), /*tp_basicsize*/
638 ( destructor )vlcObject_dealloc, /*tp_dealloc*/
645 0, /*tp_as_sequence*/
653 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
654 "Expose VLC object infrastructure.\n\nConstructor: vlc.Object(object_id)", /* tp_doc */
657 0, /* tp_richcompare */
658 0, /* tp_weaklistoffset */
661 vlcObject_methods, /* tp_methods */
666 0, /* tp_descr_get */
667 0, /* tp_descr_set */
668 0, /* tp_dictoffset */
671 vlcObject_new, /* tp_new */