dbus.c 19.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*****************************************************************************
 * dbus.c : D-Bus control interface
 *****************************************************************************
 * Copyright (C) 2006 Rafaël Carré
 * $Id$
 *
 * Author:    Rafaël Carré <funman at videolanorg>
 *
 * 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
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

/*
Antoine Cellerier's avatar
Antoine Cellerier committed
25
 * D-Bus Specification:
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
 *      http://dbus.freedesktop.org/doc/dbus-specification.html
 * D-Bus low-level C API (libdbus)
 *      http://dbus.freedesktop.org/doc/dbus/api/html/index.html
 */

/*
 * TODO:
 *  properties ?
 *
 *  macros to read incoming arguments
 *
 *  explore different possible types (arrays..)
 *
 *  what must we do if org.videolan.vlc already exist on the bus ?
 *  ( there is more than one vlc instance )
 */

/*****************************************************************************
 * Preamble
 *****************************************************************************/

#include <dbus/dbus.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "dbus.h"

#include <vlc/vlc.h>
Clément Stenac's avatar
Clément Stenac committed
55 56
#include <vlc_aout.h>
#include <vlc_interface.h>
57 58
#include <vlc_meta.h>
#include <vlc_input.h>
Antoine Cellerier's avatar
Antoine Cellerier committed
59
#include <vlc_playlist.h>
60

61

62 63 64 65 66 67 68 69 70
/*****************************************************************************
 * Local prototypes.
 *****************************************************************************/

static int  Open    ( vlc_object_t * );
static void Close   ( vlc_object_t * );
static void Run        ( intf_thread_t * );


71
static int TrackChange( vlc_object_t *p_this, const char *psz_var,
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
                    vlc_value_t oldval, vlc_value_t newval, void *p_data );

struct intf_sys_t
{
    DBusConnection *p_conn;
};

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/

vlc_module_begin();
    set_shortname( _("dbus"));
    set_category( CAT_INTERFACE );
    set_subcategory( SUBCAT_INTERFACE_CONTROL );
    set_description( _("D-Bus control interface") );
    set_capability( "interface", 0 );
    set_callbacks( Open, Close );
vlc_module_end();

/*****************************************************************************
 * Methods
 *****************************************************************************/
95
#if 0
96
DBUS_METHOD( PlaylistExport_XSPF )
97
{ /*export playlist to an xspf file */
98 99

  /* reads the filename to export to */
100
  /* returns the status as int32:
101 102 103 104 105 106 107 108 109 110 111
   *    0 : success
   *    1 : error
   *    2 : playlist empty
   */
    REPLY_INIT;
    OUT_ARGUMENTS;

    DBusError error; 
    dbus_error_init( &error );

    char *psz_file;
112
    dbus_int32_t i_ret;
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 138 139 140 141 142

    dbus_message_get_args( p_from, &error,
            DBUS_TYPE_STRING, &psz_file,
            DBUS_TYPE_INVALID );

    if( dbus_error_is_set( &error ) )
    {
        msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
                error.message );
        dbus_error_free( &error );
        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
    }

    playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );

    if( ( !playlist_IsEmpty( p_playlist ) ) &&
            ( p_playlist->p_root_category->i_children > 0 ) )
    {
        if( playlist_Export( p_playlist, psz_file,
                         p_playlist->p_root_category->pp_children[0],
                         "export-xspf" ) == VLC_SUCCESS )
            i_ret = 0;
        else
            i_ret = 1;
    }
    else
        i_ret = 2;

    pl_Release( ((vlc_object_t*) p_this ) );

143
    ADD_INT32( &i_ret );
144 145
    REPLY_SEND;
}
146 147 148
#endif

/* Player */
149

Rafaël Carré's avatar
Rafaël Carré committed
150 151 152 153 154 155
DBUS_METHOD( Quit )
{ /* exits vlc */
    REPLY_INIT;
    playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
    playlist_Stop( p_playlist );
    pl_Release( ((vlc_object_t*) p_this) );
Rafaël Carré's avatar
Rafaël Carré committed
156
    ((vlc_object_t*)p_this)->p_libvlc->b_die = VLC_TRUE;
Rafaël Carré's avatar
Rafaël Carré committed
157 158 159
    REPLY_SEND;
}

160 161 162 163 164
DBUS_METHOD( PositionGet )
{ /* returns position as an int in the range [0;1000] */
    REPLY_INIT;
    OUT_ARGUMENTS;
    vlc_value_t position;
165
    dbus_int32_t i_pos;
166 167 168 169 170 171 172 173 174 175 176

    playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
    input_thread_t *p_input = p_playlist->p_input;

    if( !p_input )
        i_pos = 0;
    else
    {
        var_Get( p_input, "position", &position );
        i_pos = position.f_float * 1000 ;
    }
177
    ADD_INT32( &i_pos );
178 179 180 181 182 183 184 185 186
    pl_Release( ((vlc_object_t*) p_this) );
    REPLY_SEND;
}

DBUS_METHOD( PositionSet )
{ /* set position from an int in the range [0;1000] */

    REPLY_INIT;
    vlc_value_t position;
187
    dbus_int32_t i_pos;
188 189 190 191 192

    DBusError error;
    dbus_error_init( &error );

    dbus_message_get_args( p_from, &error,
193
            DBUS_TYPE_INT32, &i_pos,
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
            DBUS_TYPE_INVALID );

    if( dbus_error_is_set( &error ) )
    {
        msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
                error.message );
        dbus_error_free( &error );
        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
    }
    playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
    input_thread_t *p_input = p_playlist->p_input;

    if( p_input )
    {
        position.f_float = ((float)i_pos) / 1000;
        var_Set( p_input, "position", position );
    }
    pl_Release( ((vlc_object_t*) p_this) );
    REPLY_SEND;
}

Rafaël Carré's avatar
Rafaël Carré committed
215
DBUS_METHOD( VolumeGet )
216
{ /* returns volume in percentage */
Rafaël Carré's avatar
Rafaël Carré committed
217 218
    REPLY_INIT;
    OUT_ARGUMENTS;
219 220 221
    dbus_int32_t i_dbus_vol;
    audio_volume_t i_vol;
    /* 2nd argument of aout_VolumeGet is int32 */
Rafaël Carré's avatar
Rafaël Carré committed
222
    aout_VolumeGet( (vlc_object_t*) p_this, &i_vol );
223 224
    i_dbus_vol = ( 100 * i_vol ) / AOUT_VOLUME_MAX;
    ADD_INT32( &i_dbus_vol );
Rafaël Carré's avatar
Rafaël Carré committed
225 226 227 228 229 230 231 232 233 234
    REPLY_SEND;
}

DBUS_METHOD( VolumeSet )
{ /* set volume in percentage */
    REPLY_INIT;

    DBusError error;
    dbus_error_init( &error );

235 236
    dbus_int32_t i_dbus_vol;
    audio_volume_t i_vol;
Rafaël Carré's avatar
Rafaël Carré committed
237 238

    dbus_message_get_args( p_from, &error,
239
            DBUS_TYPE_INT32, &i_dbus_vol,
Rafaël Carré's avatar
Rafaël Carré committed
240 241 242 243 244 245 246 247 248 249
            DBUS_TYPE_INVALID );

    if( dbus_error_is_set( &error ) )
    {
        msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
                error.message );
        dbus_error_free( &error );
        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
    }

250 251
    i_vol = ( AOUT_VOLUME_MAX / 100 ) *i_dbus_vol;
    aout_VolumeSet( (vlc_object_t*) p_this, i_vol );
Rafaël Carré's avatar
Rafaël Carré committed
252 253 254 255

    REPLY_SEND;
}

256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
DBUS_METHOD( Next )
{ /* next playlist item */
    REPLY_INIT;
    playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
    playlist_Next( p_playlist );
    pl_Release( ((vlc_object_t*) p_this) );
    REPLY_SEND;
}

DBUS_METHOD( Prev )
{ /* previous playlist item */
    REPLY_INIT;
    playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
    playlist_Prev( p_playlist );
    pl_Release( ((vlc_object_t*) p_this) );
    REPLY_SEND;
}

DBUS_METHOD( Stop )
{ /* stop playing */
    REPLY_INIT;
    playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
    playlist_Stop( p_playlist );
    pl_Release( ((vlc_object_t*) p_this) );
    REPLY_SEND;
}
Rafaël Carré's avatar
Rafaël Carré committed
282

283 284
DBUS_METHOD( GetStatus )
{ /* returns an int: 0=playing 1=paused 2=stopped */
285 286 287
    REPLY_INIT;
    OUT_ARGUMENTS;

288
    dbus_int32_t i_status;
289
    vlc_value_t val;
290

291 292 293
    playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
    input_thread_t *p_input = p_playlist->p_input;

294 295
    i_status = 2;
    if( p_input )
296 297 298
    {
        var_Get( p_input, "state", &val );
        if( val.i_int == PAUSE_S )
299
            i_status = 1;
300
        else if( val.i_int == PLAYING_S )
301
            i_status = 0;
302
    }
Antoine Cellerier's avatar
Antoine Cellerier committed
303

304 305
    pl_Release( p_playlist );

306
    ADD_INT32( &i_status );
307 308 309
    REPLY_SEND;
}

310 311
DBUS_METHOD( Pause )
{
312
    REPLY_INIT;
313 314 315 316 317
    playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
    playlist_Pause( p_playlist );
    pl_Release( p_playlist );
    REPLY_SEND;
}
318

319 320 321
DBUS_METHOD( Play )
{
    REPLY_INIT;
322
    playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
323
    playlist_Play( p_playlist );
324
    pl_Release( p_playlist );
325 326
    REPLY_SEND;
}
327

328 329 330 331 332 333 334 335 336 337
/* Media Player information */

DBUS_METHOD( Identity )
{
    REPLY_INIT;
    OUT_ARGUMENTS;
    char *psz_identity = malloc( strlen( PACKAGE ) + strlen( VERSION ) + 1 );
    sprintf( psz_identity, "%s %s", PACKAGE, VERSION );
    ADD_STRING( &psz_identity );
    free( psz_identity );
338 339 340
    REPLY_SEND;
}

341 342 343
/* TrackList */

DBUS_METHOD( AddTrack )
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
{ /* add the string to the playlist, and play it if the boolean is true */
    REPLY_INIT;

    DBusError error;
    dbus_error_init( &error );

    char *psz_mrl;
    dbus_bool_t b_play;

    dbus_message_get_args( p_from, &error,
            DBUS_TYPE_STRING, &psz_mrl,
            DBUS_TYPE_BOOLEAN, &b_play,
            DBUS_TYPE_INVALID );

    if( dbus_error_is_set( &error ) )
    {
Rafaël Carré's avatar
Rafaël Carré committed
360 361
        msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
                error.message );
362 363 364 365 366
        dbus_error_free( &error );
        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
    }

    playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
Rafaël Carré's avatar
Rafaël Carré committed
367 368
    playlist_Add( p_playlist, psz_mrl, NULL, PLAYLIST_APPEND |
            ( ( b_play == TRUE ) ? PLAYLIST_GO : 0 ) , PLAYLIST_END, VLC_TRUE );
369 370 371 372 373
    pl_Release( p_playlist );

    REPLY_SEND;
}

374
DBUS_METHOD( GetCurrentTrack )
375
{
376 377 378
    REPLY_INIT;
    OUT_ARGUMENTS;
    dbus_int32_t i_position = 0;
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
    playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
    playlist_item_t* p_tested_item = p_playlist->p_root_onelevel;
    
    while ( p_tested_item->i_id != p_playlist->status.p_item->i_id )
    {
        i_position++;
        p_tested_item = playlist_GetNextLeaf( p_playlist, 
                        p_playlist->p_root_onelevel, 
                        p_tested_item,
                        VLC_FALSE,
                        VLC_FALSE );
    }

    pl_Release( p_playlist );

394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
    ADD_INT32( &i_position );
    REPLY_SEND;
}

DBUS_METHOD( GetMetadata )
{ //TODO reads int, returns a{sv}
    REPLY_INIT;
    OUT_ARGUMENTS;
    DBusError error;
    dbus_error_init( &error );

    dbus_int32_t i_position;

    dbus_message_get_args( p_from, &error,
            DBUS_TYPE_INT32, &i_position,
            DBUS_TYPE_INVALID );

    if( dbus_error_is_set( &error ) )
    {
        msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
                error.message );
        dbus_error_free( &error );
        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
    }

    //TODO return a{sv}

    REPLY_SEND;
}

DBUS_METHOD( GetLength )
425
{ 
426 427
    REPLY_INIT;
    OUT_ARGUMENTS;
428

429
    dbus_int32_t i_elements = 0;
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
    playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
    playlist_item_t* p_tested_item = p_playlist->p_root_onelevel;
    playlist_item_t* p_last_item = playlist_GetLastLeaf( p_playlist, p_playlist->p_root_onelevel ); 

    while ( p_tested_item->i_id != p_last_item->i_id )
    {
        i_elements++;
        p_tested_item = playlist_GetNextLeaf( p_playlist, 
                        p_playlist->p_root_onelevel, 
                        p_tested_item,
                        VLC_FALSE,
                        VLC_FALSE );
    }

    pl_Release( p_playlist );
    
446 447 448 449 450
    ADD_INT32( &i_elements );
    REPLY_SEND;
}

DBUS_METHOD( DelTrack )
451 452
{
  /*FIXME: Doesn't work.*/
453 454 455 456 457
    REPLY_INIT;

    DBusError error;
    dbus_error_init( &error );

458 459 460
    dbus_int32_t i_position, i_count = 0;
    playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
    playlist_item_t* p_tested_item = p_playlist->p_root_onelevel;
461 462 463 464 465 466 467 468 469 470 471 472

    dbus_message_get_args( p_from, &error,
            DBUS_TYPE_INT32, &i_position,
            DBUS_TYPE_INVALID );

    if( dbus_error_is_set( &error ) )
    {
        msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
                error.message );
        dbus_error_free( &error );
        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
    }
473 474 475 476 477 478 479 480 481 482
    
    while ( i_count < i_position ) 
    {
        i_count++;
        p_tested_item = playlist_GetNextLeaf( p_playlist, 
                        p_playlist->p_root_onelevel, 
                        p_tested_item,
                        VLC_FALSE,
                        VLC_FALSE );
    }
483

484 485 486 487
    playlist_NodeRemoveItem( p_playlist, 
		    p_tested_item, 
		    p_playlist->p_root_onelevel );
    pl_Release( p_playlist );
488 489 490 491

    REPLY_SEND;
}

492 493 494 495
/*****************************************************************************
 * Introspection method
 *****************************************************************************/

496
DBUS_METHOD( handle_introspect_root )
497 498 499
{ /* handles introspection of /org/videolan/vlc */
    REPLY_INIT;
    OUT_ARGUMENTS;
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
    ADD_STRING( &psz_introspection_xml_data_root );
    REPLY_SEND;
}

DBUS_METHOD( handle_introspect_player )
{
    REPLY_INIT;
    OUT_ARGUMENTS;
    ADD_STRING( &psz_introspection_xml_data_player );
    REPLY_SEND;
}

DBUS_METHOD( handle_introspect_tracklist )
{
    REPLY_INIT;
    OUT_ARGUMENTS;
    ADD_STRING( &psz_introspection_xml_data_tracklist );
517 518 519 520
    REPLY_SEND;
}

/*****************************************************************************
521
 * handle_*: answer to incoming messages
522 523 524 525 526 527
 *****************************************************************************/

#define METHOD_FUNC( method, function ) \
    else if( dbus_message_is_method_call( p_from, VLC_DBUS_INTERFACE, method ) )\
        return function( p_conn, p_from, p_this )

528 529
DBUS_METHOD( handle_root )
{
530 531 532

    if( dbus_message_is_method_call( p_from,
                DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
        return handle_introspect_root( p_conn, p_from, p_this );

    /* here D-Bus method's names are associated to an handler */

    METHOD_FUNC( "Identity",                Identity );

    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}


DBUS_METHOD( handle_player )
{
    if( dbus_message_is_method_call( p_from,
                DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
    return handle_introspect_player( p_conn, p_from, p_this );
548 549 550

    /* here D-Bus method's names are associated to an handler */

551 552 553 554
    METHOD_FUNC( "Prev",                    Prev );
    METHOD_FUNC( "Next",                    Next );
    METHOD_FUNC( "Quit",                    Quit );
    METHOD_FUNC( "Stop",                    Stop );
555 556
    METHOD_FUNC( "Play",                    Play );
    METHOD_FUNC( "Pause",                   Pause );
557 558 559 560
    METHOD_FUNC( "VolumeSet",               VolumeSet );
    METHOD_FUNC( "VolumeGet",               VolumeGet );
    METHOD_FUNC( "PositionSet",             PositionSet );
    METHOD_FUNC( "PositionGet",             PositionGet );
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
    METHOD_FUNC( "GetStatus",               GetStatus );

    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

DBUS_METHOD( handle_tracklist )
{
    if( dbus_message_is_method_call( p_from,
                DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
    return handle_introspect_tracklist( p_conn, p_from, p_this );

    /* here D-Bus method's names are associated to an handler */

    METHOD_FUNC( "GetMetadata",             GetMetadata );
    METHOD_FUNC( "GetCurrentTrack",         GetCurrentTrack );
    METHOD_FUNC( "GetLength",               GetLength );
    METHOD_FUNC( "AddTrack",                AddTrack );
    METHOD_FUNC( "DelTrack",                DelTrack );
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598

    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

/*****************************************************************************
 * Open: initialize interface
 *****************************************************************************/

static int Open( vlc_object_t *p_this )
{ /* initialisation of the connection */
    intf_thread_t   *p_intf = (intf_thread_t*)p_this;
    intf_sys_t      *p_sys  = malloc( sizeof( intf_sys_t ) );
    playlist_t      *p_playlist;
    DBusConnection  *p_conn;
    DBusError       error;

    if( !p_sys )
        return VLC_ENOMEM;

    dbus_threads_init_default();
Antoine Cellerier's avatar
Antoine Cellerier committed
599

600
    dbus_error_init( &error );
Antoine Cellerier's avatar
Antoine Cellerier committed
601

602 603 604 605 606 607 608 609 610 611 612
    /* connect to the session bus */
    p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
    if( !p_conn )
    {
        msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
                error.message );
        dbus_error_free( &error );
        free( p_sys );
        return VLC_EGENERIC;
    }

613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
    /* register a well-known name on the bus */
    dbus_bus_request_name( p_conn, "org.freedesktop.MediaPlayer", 0, &error );
    if( dbus_error_is_set( &error ) )
    {
        msg_Err( p_this, "Error requesting org.freedesktop.MediaPlayer service:"                " %s\n", error.message );
        dbus_error_free( &error );
        free( p_sys );
        return VLC_EGENERIC;
    }

    /* we register the objects */
    dbus_connection_register_object_path( p_conn, VLC_DBUS_ROOT_PATH,
            &vlc_dbus_root_vtable, p_this );
    dbus_connection_register_object_path( p_conn, VLC_DBUS_PLAYER_PATH,
            &vlc_dbus_player_vtable, p_this );
    dbus_connection_register_object_path( p_conn, VLC_DBUS_TRACKLIST_PATH,
            &vlc_dbus_tracklist_vtable, p_this );
630 631 632 633 634

    dbus_connection_flush( p_conn );

    p_playlist = pl_Yield( p_intf );
    PL_LOCK;
635
    var_AddCallback( p_playlist, "playlist-current", TrackChange, p_intf );
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
    PL_UNLOCK;
    pl_Release( p_playlist );

    p_intf->pf_run = Run;
    p_intf->p_sys = p_sys;
    p_sys->p_conn = p_conn;

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close: destroy interface
 *****************************************************************************/

static void Close   ( vlc_object_t *p_this )
{
    intf_thread_t   *p_intf     = (intf_thread_t*) p_this;
    playlist_t      *p_playlist = pl_Yield( p_intf );;

    PL_LOCK;
656
    var_DelCallback( p_playlist, "playlist-current", TrackChange, p_intf );
657 658 659
    PL_UNLOCK;
    pl_Release( p_playlist );

660 661
    dbus_connection_unref( p_intf->p_sys->p_conn );

662 663 664 665
    free( p_intf->p_sys );
}

/*****************************************************************************
Antoine Cellerier's avatar
Antoine Cellerier committed
666
 * Run: main loop
667 668 669 670 671 672 673 674 675 676 677 678
 *****************************************************************************/

static void Run          ( intf_thread_t *p_intf )
{
    while( !p_intf->b_die )
    {
        msleep( INTF_IDLE_SLEEP );
        dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
    }
}

/*****************************************************************************
679
 * TrackChange: Playlist item change callback
680 681
 *****************************************************************************/

682
DBUS_SIGNAL( TrackChangeSignal )
683
{ /* emit the name of the new item */
684
    SIGNAL_INIT( "TrackChange" );
685 686 687
    OUT_ARGUMENTS;

    input_thread_t *p_input = (input_thread_t*) p_data;
Antoine Cellerier's avatar
Antoine Cellerier committed
688
    ADD_STRING( &input_GetItem(p_input)->psz_name );
689 690 691 692

    SIGNAL_SEND;
}

693
static int TrackChange( vlc_object_t *p_this, const char *psz_var,
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
            vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
    intf_thread_t       *p_intf     = ( intf_thread_t* ) p_data;
    intf_sys_t          *p_sys      = p_intf->p_sys;
    playlist_t          *p_playlist;
    input_thread_t      *p_input    = NULL;
    (void)p_this; (void)psz_var; (void)oldval; (void)newval;

    p_playlist = pl_Yield( p_intf );
    PL_LOCK;
    p_input = p_playlist->p_input;

    if( !p_input )
    {
        PL_UNLOCK;
        pl_Release( p_playlist );
        return VLC_SUCCESS;
    }

    vlc_object_yield( p_input );
    PL_UNLOCK;
    pl_Release( p_playlist );

717
    TrackChangeSignal( p_sys->p_conn, p_input );
718 719 720 721 722

    vlc_object_release( p_input );
    return VLC_SUCCESS;
}