vlcglue.c 43 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * vlcglue.c: VLC Module
 *****************************************************************************
 * Copyright (C) 1998-2004 the VideoLAN team
5
 * $Id$
6 7
 *
 * Authors: Olivier Aubert <oaubert at bat710.univ-lyon1.fr>
8
 *          Clément Stenac <zorglub@videolan.org>
9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24 25 26 27 28 29 30 31 32 33 34
 *****************************************************************************/
#include "vlcglue.h"

/**************************************************************************
 * VLC Module
 **************************************************************************/

#ifndef vlcMODINIT_FUNC /* declarations for DLL import/export */
#define vlcMODINIT_FUNC void
#endif

static PyMethodDef vlc_methods[] = {
35
    { NULL }  /* Sentinel */
36
};
zorglub's avatar
zorglub committed
37 38

/* Module globals */
39 40 41 42 43
PyObject* MediaControl_InternalException          = NULL;
PyObject* MediaControl_PositionKeyNotSupported    = NULL;
PyObject *MediaControl_PositionOriginNotSupported = NULL;
PyObject* MediaControl_InvalidPosition            = NULL;
PyObject *MediaControl_PlaylistException          = NULL;
zorglub's avatar
zorglub committed
44

45 46
vlcMODINIT_FUNC
initvlc( void )
47
{
48
    PyObject* p_module;
zorglub's avatar
zorglub committed
49

50 51 52
    PyPosition_Type.tp_new = PyType_GenericNew;
    PyPosition_Type.tp_alloc = PyType_GenericAlloc;

53 54 55 56
    p_module = Py_InitModule3( "vlc", vlc_methods,
                               "VLC media player embedding module." );

    if( !p_module )
57 58
      return;

59
    if( PyType_Ready( &PyPosition_Type ) < 0 )
60
        return;
61
    if( PyType_Ready( &MediaControl_Type ) < 0 )
62
        return;
63
    if( PyType_Ready( &vlcObject_Type ) < 0 )
64 65 66 67
        return;

    /* Exceptions */
    MediaControl_InternalException =
68 69 70 71
            PyErr_NewException( "vlc.InternalException", NULL, NULL );
    Py_INCREF( MediaControl_InternalException );
    PyModule_AddObject( p_module, "InternalException",
                        MediaControl_InternalException );
zorglub's avatar
zorglub committed
72

73
    MediaControl_PositionKeyNotSupported =
74 75 76 77
            PyErr_NewException( "vlc.PositionKeyNotSupported", NULL, NULL );
    Py_INCREF( MediaControl_PositionKeyNotSupported );
    PyModule_AddObject( p_module, "PositionKeyNotSupported",
                        MediaControl_PositionKeyNotSupported );
zorglub's avatar
zorglub committed
78

79
    MediaControl_PositionOriginNotSupported=
80 81 82 83
            PyErr_NewException( "vlc.InvalidPosition", NULL, NULL );
    Py_INCREF( MediaControl_PositionOriginNotSupported );
    PyModule_AddObject( p_module, "PositionOriginNotSupported",
                        MediaControl_PositionOriginNotSupported );
zorglub's avatar
zorglub committed
84

85
    MediaControl_InvalidPosition =
86 87 88 89
            PyErr_NewException( "vlc.InvalidPosition", NULL, NULL );
    Py_INCREF( MediaControl_InvalidPosition );
    PyModule_AddObject( p_module, "InvalidPosition",
                        MediaControl_InvalidPosition );
90 91

    MediaControl_PlaylistException =
92 93 94 95
            PyErr_NewException( "vlc.PlaylistException", NULL, NULL );
    Py_INCREF( MediaControl_PlaylistException );
    PyModule_AddObject( p_module, "PlaylistException",
                        MediaControl_PlaylistException );
96 97

    /* Types */
98 99 100
    Py_INCREF( &PyPosition_Type );
    PyModule_AddObject( p_module, "Position",
                        ( PyObject * )&PyPosition_Type );
101

102 103 104
    Py_INCREF( &MediaControl_Type );
    PyModule_AddObject( p_module, "MediaControl",
                        ( PyObject * )&MediaControl_Type );
105

106 107 108
    Py_INCREF( &vlcObject_Type );
    PyModule_AddObject( p_module, "Object",
                        ( PyObject * )&vlcObject_Type );
zorglub's avatar
zorglub committed
109

110
    /* Constants */
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    PyModule_AddIntConstant( p_module, "AbsolutePosition",
                             mediacontrol_AbsolutePosition );
    PyModule_AddIntConstant( p_module, "RelativePosition",
                             mediacontrol_RelativePosition );
    PyModule_AddIntConstant( p_module, "ModuloPosition",
                             mediacontrol_ModuloPosition );

    PyModule_AddIntConstant( p_module, "ByteCount",
                             mediacontrol_ByteCount );
    PyModule_AddIntConstant( p_module, "SampleCount",
                             mediacontrol_SampleCount );
    PyModule_AddIntConstant( p_module, "MediaTime",
                             mediacontrol_MediaTime );
    PyModule_AddIntConstant( p_module, "PlayingStatus",
                             mediacontrol_PlayingStatus );
    PyModule_AddIntConstant( p_module, "PauseStatus",
                             mediacontrol_PauseStatus );
    PyModule_AddIntConstant( p_module, "ForwardStatus",
                             mediacontrol_ForwardStatus );
    PyModule_AddIntConstant( p_module, "BackwardStatus",
                             mediacontrol_BackwardStatus );
    PyModule_AddIntConstant( p_module, "InitStatus",
                             mediacontrol_InitStatus );
    PyModule_AddIntConstant( p_module, "EndStatus",
                             mediacontrol_EndStatus );
    PyModule_AddIntConstant( p_module, "UndefinedStatus",
                             mediacontrol_UndefinedStatus );
138 139 140 141
}


/* Make libpostproc happy... */
142
void * fast_memcpy( void * to, const void * from, size_t len )
143
{
144
  return memcpy( to, from, len );
145 146
}

zorglub's avatar
zorglub committed
147

148 149 150
/*****************************************************************************
 * VLCObject implementation
 *****************************************************************************/
zorglub's avatar
zorglub committed
151

152 153
static PyObject
*vlcObject_new( PyTypeObject *p_type, PyObject *p_args, PyObject *p_kwds )
zorglub's avatar
zorglub committed
154
{
155 156 157 158
    vlcObject *self;
    vlc_object_t *p_object;
    int i_id;

159
    self = PyObject_New( vlcObject, &vlcObject_Type );
160

161
    if( !PyArg_ParseTuple( p_args, "i", &i_id ) )
162
      return NULL;
zorglub's avatar
zorglub committed
163

164
    /* Maybe we were already initialized */
165
    p_object = ( vlc_object_t* )vlc_current_object( i_id );
166

167
    if( !p_object )
168 169 170
    {
        /* Try to initialize */
        i_id = VLC_Create();
171
        p_object = ( vlc_object_t* )vlc_current_object( i_id );
172 173
    }

174
    if( !p_object )
175
    {
176
        PyErr_SetString( PyExc_StandardError, "Unable to get object." );
177 178 179 180 181 182
        return NULL;
    }

    self->p_object = p_object;
    self->b_released = 0;

183 184
    Py_INCREF(  self ); /* Ah bon ? */
    return ( PyObject * )self;
zorglub's avatar
zorglub committed
185 186
}

187 188
static PyObject *
vlcObject_release(  PyObject *self, PyObject *p_args )
189 190 191 192 193 194
{
    if( VLCSELF->b_released == 0 )
    {
        vlc_object_release( VLCSELF->p_object );
        VLCSELF->b_released = 1;
    }
195
    Py_INCREF(  Py_None );
196 197 198
    return Py_None;
}

199 200
static void
vlcObject_dealloc( PyObject *self )
201
{
202
    vlcObject_release( self, NULL );
203
    PyMem_DEL( self );
204 205
}

206 207
static PyObject *
vlcObject_find_object( PyObject *self, PyObject *args )
208
{
209
    vlcObject *p_retval;
210 211 212 213
    vlc_object_t *p_obj;
    char *psz_name;
    int i_object_type;

214 215
    if( !PyArg_ParseTuple( args, "s", &psz_name ) )
        return NULL;
zorglub's avatar
zorglub committed
216

217
    /* psz_name is in
218
       ( aout, decoder, input, httpd, intf, playlist, root, vlc, vout )
219
    */
220
    switch ( psz_name[0] )
221 222 223 224 225 226 227 228 229 230 231
    {
        case 'a':
            i_object_type = VLC_OBJECT_AOUT;
            break;
        case 'd':
            i_object_type = VLC_OBJECT_DECODER;
            break;
        case 'h':
            i_object_type = VLC_OBJECT_HTTPD;
            break;
        case 'i':
232
            if( strlen( psz_name ) < 3 )
233
                    i_object_type = VLC_OBJECT_INTF;
234
            else if( psz_name[2] == 't' )
235 236 237 238 239 240 241 242 243 244 245
                    i_object_type = VLC_OBJECT_INTF;
            else
                    i_object_type = VLC_OBJECT_INPUT;
            break;
        case 'p':
            i_object_type = VLC_OBJECT_PLAYLIST;
            break;
        case 'r':
            i_object_type = VLC_OBJECT_ROOT;
            break;
        case 'v':
246
            if( strlen( psz_name ) < 3 )
247
                    i_object_type = VLC_OBJECT_VLC;
248
            else if( psz_name[1] == 'l' )
249 250 251 252 253
                    i_object_type = VLC_OBJECT_VLC;
            else
                    i_object_type = VLC_OBJECT_VOUT;
            break;
        default:
254
            /* FIXME: raise an exception ? */
255 256 257 258 259
            return Py_None;
    }

    p_obj = vlc_object_find( VLCSELF->p_object, i_object_type, FIND_ANYWHERE );

260
    if( !p_obj )
261
    {
262
        Py_INCREF( Py_None );
263 264 265
        return Py_None;
    }

266
    p_retval = PyObject_New( vlcObject, &vlcObject_Type );
267

268
    p_retval->p_object = p_obj;
269

270
    return ( PyObject * )p_retval;
271 272
}

273 274
static PyObject *
vlcObject_info( PyObject *self, PyObject *args )
275
{
276
    PyObject *p_retval;
277 278 279 280 281
    vlc_object_t *p_obj;

    p_obj = VLCSELF->p_object;

    /* Return information about the object as a dict. */
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
    p_retval = PyDict_New();

    PyDict_SetItemString( p_retval, "object-id",
                          Py_BuildValue( "l", p_obj->i_object_id ) );
    PyDict_SetItemString( p_retval, "object-type",
                          Py_BuildValue( "s", p_obj->psz_object_type ) );
    PyDict_SetItemString( p_retval, "object-name",
                          Py_BuildValue( "s", p_obj->psz_object_name ) );
    PyDict_SetItemString( p_retval, "thread",
                          PyBool_FromLong( p_obj->b_thread ) );
    PyDict_SetItemString( p_retval, "thread-id",
                          PyLong_FromLongLong( p_obj->thread_id ) );
    PyDict_SetItemString( p_retval, "refcount",
                          PyInt_FromLong( p_obj->i_refcount ) );

    return p_retval;
298 299
}

300 301
static PyObject *
vlcObject_find_id( PyObject *self, PyObject *args )
302
{
303
    vlcObject *p_retval;
304 305 306
    vlc_object_t* p_object;
    int i_id;

307
    if( !PyArg_ParseTuple( args, "i", &i_id ) )
308
        return NULL;
309

310
    p_object = ( vlc_object_t* )vlc_current_object( i_id );
311

312
    if( !p_object )
313
    {
314
        Py_INCREF( Py_None );
315 316 317
        return Py_None;
    }

318
    p_retval = PyObject_NEW( vlcObject, &vlcObject_Type );
319

320
    p_retval->p_object = p_object;
321

322
    return ( PyObject * )p_retval;
323 324 325
}

/* Do a var_Get call on the object. Parameter: the variable name. */
326 327
static PyObject *
vlcObject_var_get( PyObject *self, PyObject *args )
328
{
329
    PyObject *p_retval;
330 331 332 333
    vlc_value_t value;
    char *psz_name;
    int i_type;

334
    if( !PyArg_ParseTuple( args, "s", &psz_name ) )
335
        return NULL;
336 337 338

    if( var_Get( VLCSELF->p_object, psz_name, &value ) != VLC_SUCCESS )
    {
339 340 341
        PyErr_SetString( PyExc_StandardError,
                         "Error: variable does not exist.\n" );
        return NULL;
342 343
    }

344
    i_type = var_Type ( VLCSELF->p_object, psz_name );
345

346
    switch ( i_type )
347
    {
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
    case VLC_VAR_VOID      :
        p_retval = PyString_FromString( "A void variable" );
        break;
    case VLC_VAR_BOOL      :
        p_retval = PyBool_FromLong( value.b_bool );
        break;
    case VLC_VAR_INTEGER   :
        p_retval = PyInt_FromLong( ( long )value.i_int );
        break;
    case VLC_VAR_HOTKEY    :
        p_retval = PyString_FromFormat( "A hotkey variable ( %d )", value.i_int );
        break;
    case VLC_VAR_FILE      :
    case VLC_VAR_STRING    :
    case VLC_VAR_DIRECTORY :
    case VLC_VAR_VARIABLE  :
        p_retval = PyString_FromString( value.psz_string );
        break;
    case VLC_VAR_MODULE   :
        p_retval = ( PyObject* )PyObject_New( vlcObject, &vlcObject_Type );
        ( ( vlcObject* )p_retval )->p_object = value.p_object;
        break;
    case VLC_VAR_FLOAT     :
        p_retval = PyFloat_FromDouble( ( double )value.f_float );
        break;
    case VLC_VAR_TIME      :
        p_retval = PyLong_FromLongLong( value.i_time );
        break;
    case VLC_VAR_ADDRESS   :
        p_retval = PyString_FromString( "A VLC address ( not handled yet )" );
        break;
    case VLC_VAR_LIST      :
        p_retval = PyString_FromString( "A VLC list ( not handled yet )" );
        break;
    case VLC_VAR_MUTEX :
        p_retval = PyString_FromString( "A mutex" );
        break;
    default:
        p_retval = Py_None;
387 388
    }

389 390
    Py_INCREF( p_retval );
    return p_retval;
391 392
}

393 394
static PyObject *
vlcObject_var_type( PyObject *self, PyObject *args )
395
{
396 397 398
    char *psz_name;
    PyObject *p_retval;
    int i_type;
399

400 401
    if( !PyArg_ParseTuple( args, "s", &psz_name ) )
        return NULL;
402

403
    i_type = var_Type( VLCSELF->p_object, psz_name );
404

405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
    switch ( i_type )
    {
    case VLC_VAR_VOID   :
        p_retval = PyString_FromString( "Void" );
        break;
    case VLC_VAR_BOOL      :
        p_retval = PyString_FromString( "Boolean" );
        break;
    case VLC_VAR_INTEGER   :
        p_retval = PyString_FromString( "Integer" );
        break;
    case VLC_VAR_HOTKEY   :
        p_retval = PyString_FromString( "Hotkey" );
        break;
    case VLC_VAR_FILE      :
        p_retval = PyString_FromString( "File" );
        break;
    case VLC_VAR_STRING    :
        p_retval = PyString_FromString( "String" );
        break;
    case VLC_VAR_DIRECTORY :
        p_retval = PyString_FromString( "Directory" );
        break;
    case VLC_VAR_VARIABLE  :
        p_retval = PyString_FromString( "Variable" );
        break;
    case VLC_VAR_MODULE   :
        p_retval = PyString_FromString( "Module" );
        break;
    case VLC_VAR_FLOAT     :
        p_retval = PyString_FromString( "Float" );
        break;
    case VLC_VAR_TIME      :
        p_retval = PyString_FromString( "Time" );
        break;
    case VLC_VAR_ADDRESS   :
        p_retval = PyString_FromString( "Address" );
        break;
    case VLC_VAR_LIST      :
        p_retval = PyString_FromString( "List" );
        break;
    case VLC_VAR_MUTEX :
        p_retval = PyString_FromString( "Mutex" );
        break;
    default:
        p_retval = PyString_FromString( "Unknown" );
    }
    return p_retval;
453 454 455
}

/* Do a var_Set call on the object. Parameter: the variable name. */
456 457
static PyObject *
vlcObject_var_set( PyObject *self, PyObject *args )
458
{
459 460 461 462 463
    vlc_value_t value;
    char *psz_name;
    PyObject *py_value;
    int i_type;
    vlc_object_t *p_obj;
464

465 466
    if( !PyArg_ParseTuple( args, "sO", &psz_name, &py_value ) )
        return NULL;
467

468 469
    p_obj = VLCSELF->p_object;
    i_type = var_Type( p_obj, psz_name );
470

471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
    switch ( i_type )
    {
    case VLC_VAR_VOID   :
        break;
    case VLC_VAR_BOOL      :
        value.b_bool = PyInt_AsLong( py_value );
        break;
    case VLC_VAR_INTEGER   :
    case VLC_VAR_HOTKEY   :
        value.i_int = PyInt_AsLong( py_value );
        break;
    case VLC_VAR_FILE      :
    case VLC_VAR_STRING    :
    case VLC_VAR_DIRECTORY :
    case VLC_VAR_VARIABLE  :
        value.psz_string = strdup( PyString_AsString( py_value ) );
        break;
    case VLC_VAR_MODULE   :
        /* FIXME: we should check the PyObject type and get its p_object */
        value.p_object = ( ( vlcObject* )p_obj )->p_object;
        break;
    case VLC_VAR_FLOAT     :
        value.f_float = PyFloat_AsDouble( py_value );
        break;
    case VLC_VAR_TIME      :
        value.i_time = PyLong_AsLongLong( py_value );
        break;
    case VLC_VAR_ADDRESS   :
        value.p_address = ( char* )PyLong_AsVoidPtr( py_value );
        break;
    case VLC_VAR_LIST      :
        /* FIXME */
        value.p_list = NULL;
        break;
    case VLC_VAR_MUTEX :
        break;
    }
508

509
    var_Set( p_obj, psz_name, value );
510

511 512
    Py_INCREF( Py_None );
    return Py_None;
513 514
}

515 516
static PyObject *
vlcObject_var_list( PyObject *self, PyObject *args )
517
{
518 519 520
    PyObject *p_retval;
    int i_size;
    int i_index;
521

522 523
    i_size = VLCSELF->p_object->i_vars;
    p_retval = PyTuple_New( i_size );
524

525 526 527 528 529
    for ( i_index = 0 ; i_index < i_size ; i_index++ )
    {
        PyTuple_SetItem( p_retval, i_index,
                         Py_BuildValue( "s", VLCSELF->p_object->p_vars[i_index].psz_name ) );
    }
530

531
    return p_retval;
532 533 534
}

/* Do a config_Get call on the object. Parameter: the variable name. */
535 536
static PyObject *
vlcObject_config_get( PyObject *self, PyObject *args )
537
{
538 539 540 541
    PyObject *p_retval;
    vlc_value_t value;
    char *psz_name;
    module_config_t *p_config;
542

543 544
    if( !PyArg_ParseTuple( args, "s", &psz_name ) )
        return NULL;
545

546
    p_config = config_FindConfig( VLCSELF->p_object, psz_name );
547

548 549 550 551 552 553
    if( !p_config )
    {
        PyErr_SetString( PyExc_StandardError,
                         "Error: config variable does not exist.\n" );
        return NULL;
    }
554

555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
    switch ( p_config->i_type )
    {
    case CONFIG_ITEM_BOOL      :
        p_retval = PyBool_FromLong( p_config->i_value );
        break;
    case CONFIG_ITEM_INTEGER   :
        p_retval = PyInt_FromLong( ( long )p_config->i_value );
        break;
    case CONFIG_ITEM_KEY   :
        p_retval = PyString_FromFormat( "A hotkey variable ( %d )", p_config->i_value );
        break;
    case CONFIG_ITEM_FILE      :
    case CONFIG_ITEM_STRING    :
    case CONFIG_ITEM_DIRECTORY :
    case CONFIG_ITEM_MODULE    :
        vlc_mutex_lock( p_config->p_lock );
        if( p_config->psz_value )
            p_retval = PyString_FromString( p_config->psz_value );
        else
            p_retval = PyString_FromString( "" );
        vlc_mutex_unlock( p_config->p_lock );
        break;
        p_retval = ( PyObject* )PyObject_New( vlcObject, &vlcObject_Type );
        ( ( vlcObject* )p_retval )->p_object = value.p_object;
        break;
    case CONFIG_ITEM_FLOAT     :
        p_retval = PyFloat_FromDouble( ( double )p_config->f_value );
        break;
    default:
        p_retval = Py_None;
        Py_INCREF( p_retval );
    }
587

588
    return p_retval;
589 590
}

591 592 593
/* Do a config_put* call on the object. Parameter: the variable name. */
static PyObject *
vlcObject_config_set( PyObject *self, PyObject *args )
594
{
595 596 597 598
    char *psz_name;
    PyObject *py_value;
    vlc_object_t *p_obj;
    module_config_t *p_config;
599 600


601 602
    if( !PyArg_ParseTuple( args, "sO", &psz_name, &py_value ) )
        return NULL;
603

604 605 606 607 608 609 610 611 612
    p_obj = VLCSELF->p_object;
    p_config = config_FindConfig( p_obj, psz_name );
    /* sanity checks */
    if( !p_config )
    {
        PyErr_SetString( PyExc_StandardError,
                         "Error: option does not exist.\n" );
        return NULL;
    }
613

614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
    switch ( p_config->i_type )
    {
    case CONFIG_ITEM_BOOL      :
    case CONFIG_ITEM_INTEGER   :
    case CONFIG_ITEM_KEY       :
        config_PutInt( p_obj, psz_name, PyInt_AsLong( py_value ) );
        break;
    case CONFIG_ITEM_FILE      :
    case CONFIG_ITEM_STRING    :
    case CONFIG_ITEM_DIRECTORY :
    case CONFIG_ITEM_MODULE   :
        config_PutPsz( p_obj, psz_name, PyString_AsString( py_value ) );
        break;
    case CONFIG_ITEM_FLOAT     :
        config_PutFloat( p_obj, psz_name, PyFloat_AsDouble( py_value ) );
        break;
    }
    Py_INCREF( Py_None );
    return Py_None;
633 634
}

635 636
static PyObject *
vlcObject_children( PyObject *self, PyObject *args )
637
{
638 639 640
    PyObject *p_retval;
    int i_size;
    int i_index;
641

642 643
    i_size = VLCSELF->p_object->i_children;
    p_retval = PyTuple_New( i_size );
644

645 646 647 648 649 650
    for ( i_index = 0 ; i_index < i_size ; i_index++ )
    {
        PyTuple_SetItem( p_retval, i_index,
                         Py_BuildValue( "i",
                                        VLCSELF->p_object->pp_children[i_index]->i_object_id ) );
    }
651

652
    return p_retval;
653 654 655 656 657 658 659
}


/* Method table */
static PyMethodDef vlcObject_methods[] =
{
    { "get", vlcObject_var_get, METH_VARARGS,
660
      "get( str ) -> value   Get a variable value."},
661
    { "set", vlcObject_var_set, METH_VARARGS,
662
      "set( str, value )     Set a variable value" },
663
    { "config_get", vlcObject_config_get, METH_VARARGS,
664
      "config_get( str ) -> value   Get a configuration option." },
665
    { "config_set", vlcObject_config_set, METH_VARARGS,
666
      "config_set( str, value )     Set a configuration option" },
667
    { "type", vlcObject_var_type, METH_VARARGS,
668
      "type( str ) -> str     Get a variable type" },
669
    { "list", vlcObject_var_list, METH_NOARGS,
670
      "list( )             List the available variables" },
671
    { "children", vlcObject_children, METH_NOARGS,
672
      "children( )             List the children ids" },
673
    { "find_object", vlcObject_find_object, METH_VARARGS,
674
      "find_object( str ) -> Object     Find the object of a given type.\n\nAvailable types are : aout, decoder, input, httpd, intf, playlist, root, vlc, vout"},
675
    { "find_id", vlcObject_find_id, METH_VARARGS,
676
      "find_id( int ) -> Object      Find an object by id" },
677
    { "info", vlcObject_info, METH_NOARGS,
678
       "info( ) -> dict    Return information about the object" },
679
    { "release", vlcObject_release, METH_NOARGS,
680
      "release( ) ->     Release the VLC Object" },
681
    { NULL, NULL, 0, NULL },
zorglub's avatar
zorglub committed
682 683
};

684 685
static PyTypeObject vlcObject_Type =
{
686
    PyObject_HEAD_INIT( NULL )
zorglub's avatar
zorglub committed
687
    0,                         /*ob_size*/
688
    "vlc.Object",       /*tp_name*/
689
    sizeof( vlcObject_Type ), /*tp_basicsize*/
zorglub's avatar
zorglub committed
690
    0,                         /*tp_itemsize*/
691
    ( destructor )vlcObject_dealloc,      /*tp_dealloc*/
zorglub's avatar
zorglub committed
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
    0,                         /*tp_print*/
    0,                         /*tp_getattr*/
    0,                         /*tp_setattr*/
    0,                         /*tp_compare*/
    0,                         /*tp_repr*/
    0,                         /*tp_as_number*/
    0,                         /*tp_as_sequence*/
    0,                         /*tp_as_mapping*/
    0,                         /*tp_hash */
    0,                         /*tp_call*/
    0,                         /*tp_str*/
    0,                         /*tp_getattro*/
    0,                         /*tp_setattro*/
    0,                         /*tp_as_buffer*/
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
707
    "Expose VLC object infrastructure.",  /* tp_doc */
708 709 710 711 712 713 714 715
    0,                     /* tp_traverse */
    0,                     /* tp_clear */
    0,                     /* tp_richcompare */
    0,                     /* tp_weaklistoffset */
    0,                     /* tp_iter */
    0,                     /* tp_iternext */
    vlcObject_methods,             /* tp_methods */
    0,             /* tp_members */
zorglub's avatar
zorglub committed
716 717 718 719 720 721
    0,                         /* tp_getset */
    0,                         /* tp_base */
    0,                         /* tp_dict */
    0,                         /* tp_descr_get */
    0,                         /* tp_descr_set */
    0,                         /* tp_dictoffset */
722
    0,                         /* tp_init */
zorglub's avatar
zorglub committed
723
    0,                         /* tp_alloc */
724
    vlcObject_new,          /* tp_new */
zorglub's avatar
zorglub committed
725 726
};

727 728 729
/*****************************************************************************
 * VLC MediaControl object implementation
 *****************************************************************************/
zorglub's avatar
zorglub committed
730

731 732
static PyObject *
MediaControl_new( PyTypeObject *type, PyObject *args, PyObject *kwds )
zorglub's avatar
zorglub committed
733
{
734 735 736 737
    MediaControl *self;
    mediacontrol_Exception *exception = NULL;
    PyObject* py_list = NULL;
    char** ppsz_args = NULL;
zorglub's avatar
zorglub committed
738

739
    self = PyObject_New( MediaControl, &MediaControl_Type );
zorglub's avatar
zorglub committed
740

741
    if( PyArg_ParseTuple( args, "O", &py_list ) )
zorglub's avatar
zorglub committed
742
    {
743 744 745
        int i_size;
        int i_index;

746 747
        Py_INCREF( py_list );
        if( ! PySequence_Check( py_list ) )
748
        {
749
            PyErr_SetString( PyExc_TypeError, "Parameter must be a sequence." );
750 751
            return NULL;
        }
752 753 754
        i_size = PySequence_Size( py_list );
        ppsz_args = malloc( i_size + 1 );
        if( ! ppsz_args )
755
        {
756
            PyErr_SetString( PyExc_MemoryError, "Out of memory" );
757 758 759 760 761 762
            return NULL;
        }

        for ( i_index = 0; i_index < i_size; i_index++ )
        {
            ppsz_args[i_index] =
763 764 765
                strdup( PyString_AsString( PyObject_Str(
                                               PySequence_GetItem( py_list,
                                                                   i_index ) ) ) );
766 767
        }
        ppsz_args[i_size] = NULL;
768
        Py_DECREF( py_list );
zorglub's avatar
zorglub committed
769
    }
770 771 772
    else
    {
        /* No arguments were given. Clear the exception raised
773
           by PyArg_ParseTuple. */
774
        PyErr_Clear( );
775
    }
776 777 778 779 780 781 782

    Py_BEGIN_ALLOW_THREADS
    MC_TRY;
    self->mc = mediacontrol_new( ppsz_args, exception );
    MC_EXCEPT;
    Py_END_ALLOW_THREADS

783 784
    Py_INCREF( self );
    return ( PyObject * )self;
zorglub's avatar
zorglub committed
785 786
}

787 788
static void
MediaControl_dealloc( PyObject *self )
zorglub's avatar
zorglub committed
789
{
790
    PyMem_DEL( self );
zorglub's avatar
zorglub committed
791 792
}

793
/**
794 795
 *  Return the current position in the stream. The returned value can
   be relative or absolute ( according to PositionOrigin ) and the unit
796 797
   is set by PositionKey
 */
798 799
static PyObject *
MediaControl_get_media_position( PyObject *self, PyObject *args )
zorglub's avatar
zorglub committed
800
{
801 802 803 804 805 806 807 808 809 810 811
    mediacontrol_Position* pos;
    mediacontrol_Exception* exception = NULL;
    PyObject *py_origin;
    PyObject *py_key;
    PyObject *py_retval;
    mediacontrol_PositionOrigin origin;
    mediacontrol_PositionKey key;

    if( !PyArg_ParseTuple( args, "OO", &py_origin, &py_key ) )
        return NULL;

812 813
    origin = positionOrigin_py_to_c( py_origin );
    key    = positionKey_py_to_c( py_key );
814 815 816

    Py_BEGIN_ALLOW_THREADS
    MC_TRY;
817
    pos = mediacontrol_get_media_position( SELF->mc, origin, key, exception );
818 819 820
    Py_END_ALLOW_THREADS
    MC_EXCEPT;

821
    py_retval = ( PyObject* )position_c_to_py( pos );
822 823
    free( pos );
    return py_retval;
zorglub's avatar
zorglub committed
824 825
}

826 827 828
/** Set the media position */
static PyObject *
MediaControl_set_media_position( PyObject *self, PyObject *args )
zorglub's avatar
zorglub committed
829
{
830 831 832
    mediacontrol_Exception* exception = NULL;
    mediacontrol_Position *a_position;
    PyObject *py_pos;
zorglub's avatar
zorglub committed
833

834 835
    if( !PyArg_ParseTuple( args, "O", &py_pos ) )
        return NULL;
zorglub's avatar
zorglub committed
836

837 838
    a_position = position_py_to_c( py_pos );
    if( !a_position )
zorglub's avatar
zorglub committed
839
    {
840
        PyErr_SetString( PyExc_MemoryError, "Out of memory" );
841
        return NULL;
zorglub's avatar
zorglub committed
842 843
    }

844 845 846 847 848 849
    Py_BEGIN_ALLOW_THREADS
    MC_TRY;
    mediacontrol_set_media_position( SELF->mc, a_position, exception );
    free( a_position );
    Py_END_ALLOW_THREADS
    MC_EXCEPT;
zorglub's avatar
zorglub committed
850

851
    Py_INCREF( Py_None );
852
    return Py_None;
zorglub's avatar
zorglub committed
853 854
}

855 856
static PyObject *
MediaControl_start( PyObject *self, PyObject *args )
zorglub's avatar
zorglub committed
857
{
858 859 860 861
    mediacontrol_Position *a_position;
    mediacontrol_Exception *exception = NULL;
    PyObject *py_pos;

862
    if( !PyArg_ParseTuple( args, "O", &py_pos ) )
863 864
    {
        /* No argument. Use a default 0 value. */
865
        PyErr_Clear( );
866 867
        py_pos = NULL;
    }
868 869
    a_position = position_py_to_c( py_pos );
    if( !a_position )
870 871 872 873
        return NULL;

    Py_BEGIN_ALLOW_THREADS
    MC_TRY;
874 875
    mediacontrol_start( SELF->mc, a_position, exception );
    free( a_position );
876 877 878
    Py_END_ALLOW_THREADS
    MC_EXCEPT;

879
    Py_INCREF( Py_None );
880
    return Py_None;
zorglub's avatar
zorglub committed
881 882
}

883 884
static PyObject *
MediaControl_pause( PyObject *self, PyObject *args )
zorglub's avatar
zorglub committed
885
{
886 887 888 889
    mediacontrol_Position *a_position;
    mediacontrol_Exception *exception = NULL;
    PyObject *py_pos;

890
    if( !PyArg_ParseTuple( args, "O", &py_pos ) )
891 892
    {
        /* No argument. Use a default 0 value. */
893
        PyErr_Clear( );
894 895
        py_pos = NULL;
    }
896 897
    a_position = position_py_to_c( py_pos );
    if( !a_position )
898
        return NULL;
899 900 901

    Py_BEGIN_ALLOW_THREADS
    MC_TRY;
902 903
    mediacontrol_pause( SELF->mc, a_position, exception );
    free( a_position );
904 905 906 907 908
    Py_END_ALLOW_THREADS
    MC_EXCEPT;

  Py_INCREF( Py_None );
  return Py_None;
zorglub's avatar
zorglub committed
909 910
}

911 912
static PyObject *
MediaControl_resume( PyObject *self, PyObject *args )
zorglub's avatar
zorglub committed
913
{
914 915 916 917
    mediacontrol_Position *a_position;
    mediacontrol_Exception *exception = NULL;
    PyObject *py_pos;

918
    if( !PyArg_ParseTuple( args, "O", &py_pos ) )
919 920
    {
        /* No argument. Use a default 0 value. */
921
        PyErr_Clear( );
922 923
        py_pos = NULL;
    }
924 925
    a_position = position_py_to_c( py_pos );
    if( !a_position )
926
        return NULL;
927 928 929

    Py_BEGIN_ALLOW_THREADS
    MC_TRY;
930 931
    mediacontrol_start( SELF->mc, a_position, exception );
    free( a_position );
932 933 934
    Py_END_ALLOW_THREADS
    MC_EXCEPT;

935
    Py_INCREF( Py_None );
936
    return Py_None;
zorglub's avatar
zorglub committed
937 938
}

939 940
static PyObject *
MediaControl_stop( PyObject *self, PyObject *args )
zorglub's avatar
zorglub committed
941
{
942 943 944 945
    mediacontrol_Position *a_position;
    mediacontrol_Exception *exception =