dbus.c 24.3 KB
Newer Older
1 2 3 4 5 6
/*****************************************************************************
 * dbus.c : D-Bus control interface
 *****************************************************************************
 * Copyright (C) 2006 Rafaël Carré
 * $Id$
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
7 8
 * Authors:    Rafaël Carré <funman at videolanorg>
 *             Mirsal Ennaime <mirsal dot ennaime at gmail dot com>
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * 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
26
 * D-Bus Specification:
27 28 29
 *      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
30 31 32 33
 *  extract:
    "If you use this low-level API directly, you're signing up for some pain."
 * MPRIS Specification (still drafting on June, 25 of 2007):
 *      http://wiki.xmms2.xmms.se/index.php/Media_Player_Interfaces
34 35 36 37 38 39 40 41 42 43 44
 */

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

#include <dbus/dbus.h>

#include "dbus.h"

#include <vlc/vlc.h>
Clément Stenac's avatar
Clément Stenac committed
45 46
#include <vlc_aout.h>
#include <vlc_interface.h>
47 48
#include <vlc_meta.h>
#include <vlc_input.h>
Antoine Cellerier's avatar
Antoine Cellerier committed
49
#include <vlc_playlist.h>
50 51 52 53 54 55 56

/*****************************************************************************
 * Local prototypes.
 *****************************************************************************/

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

59
static int TrackChange( vlc_object_t *p_this, const char *psz_var,
60 61
                    vlc_value_t oldval, vlc_value_t newval, void *p_data );

62
static int GetInputMeta ( input_item_t *p_input,
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
63 64
                    DBusMessageIter *args);

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
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
 *****************************************************************************/
86 87

/* Player */
88

Rafaël Carré's avatar
Rafaël Carré committed
89 90 91 92 93 94
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) );
95
    vlc_object_kill(((vlc_object_t*)p_this)->p_libvlc);
Rafaël Carré's avatar
Rafaël Carré committed
96 97 98
    REPLY_SEND;
}

99
DBUS_METHOD( PositionGet )
100
{ /* returns position in milliseconds */
101 102 103
    REPLY_INIT;
    OUT_ARGUMENTS;
    vlc_value_t position;
104
    dbus_int32_t i_pos;
105 106 107 108 109 110 111 112

    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
    {
113 114
        var_Get( p_input, "time", &position );
        i_pos = position.i_time / 1000;
115 116
    }
    pl_Release( ((vlc_object_t*) p_this) );
117
    ADD_INT32( &i_pos );
118 119 120 121
    REPLY_SEND;
}

DBUS_METHOD( PositionSet )
122
{ /* set position in milliseconds */
123 124 125

    REPLY_INIT;
    vlc_value_t position;
126
    playlist_t* p_playlist = NULL;
127
    dbus_int32_t i_pos;
128 129 130 131 132

    DBusError error;
    dbus_error_init( &error );

    dbus_message_get_args( p_from, &error,
133
            DBUS_TYPE_INT32, &i_pos,
134 135 136 137 138 139 140 141 142
            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;
    }
143
    p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
144 145 146 147
    input_thread_t *p_input = p_playlist->p_input;

    if( p_input )
    {
148 149
        position.i_time = i_pos * 1000;
        var_Set( p_input, "time", position );
150 151 152 153 154
    }
    pl_Release( ((vlc_object_t*) p_this) );
    REPLY_SEND;
}

Rafaël Carré's avatar
Rafaël Carré committed
155
DBUS_METHOD( VolumeGet )
156
{ /* returns volume in percentage */
Rafaël Carré's avatar
Rafaël Carré committed
157 158
    REPLY_INIT;
    OUT_ARGUMENTS;
159 160 161
    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
162
    aout_VolumeGet( (vlc_object_t*) p_this, &i_vol );
163 164
    i_dbus_vol = ( 100 * i_vol ) / AOUT_VOLUME_MAX;
    ADD_INT32( &i_dbus_vol );
Rafaël Carré's avatar
Rafaël Carré committed
165 166 167 168 169 170 171 172 173 174
    REPLY_SEND;
}

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

    DBusError error;
    dbus_error_init( &error );

175 176
    dbus_int32_t i_dbus_vol;
    audio_volume_t i_vol;
Rafaël Carré's avatar
Rafaël Carré committed
177 178

    dbus_message_get_args( p_from, &error,
179
            DBUS_TYPE_INT32, &i_dbus_vol,
Rafaël Carré's avatar
Rafaël Carré committed
180 181 182 183 184 185 186 187 188 189
            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;
    }

190 191
    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
192 193 194 195

    REPLY_SEND;
}

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
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
222

223 224
DBUS_METHOD( GetStatus )
{ /* returns an int: 0=playing 1=paused 2=stopped */
225 226 227
    REPLY_INIT;
    OUT_ARGUMENTS;

228
    dbus_int32_t i_status;
229
    vlc_value_t val;
230

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

234 235
    i_status = 2;
    if( p_input )
236 237 238
    {
        var_Get( p_input, "state", &val );
        if( val.i_int == PAUSE_S )
239
            i_status = 1;
240
        else if( val.i_int == PLAYING_S )
241
            i_status = 0;
242
    }
Antoine Cellerier's avatar
Antoine Cellerier committed
243

244 245
    pl_Release( p_playlist );

246
    ADD_INT32( &i_status );
247 248 249
    REPLY_SEND;
}

250 251
DBUS_METHOD( Pause )
{
252
    REPLY_INIT;
253 254 255 256 257
    playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
    playlist_Pause( p_playlist );
    pl_Release( p_playlist );
    REPLY_SEND;
}
258

259 260 261
DBUS_METHOD( Play )
{
    REPLY_INIT;
262
    playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
263
    playlist_Play( p_playlist );
264
    pl_Release( p_playlist );
265 266
    REPLY_SEND;
}
267

268 269 270 271 272 273
DBUS_METHOD( GetCurrentMetadata )
{
    REPLY_INIT;
    OUT_ARGUMENTS;
    playlist_t* p_playlist = pl_Yield( (vlc_object_t*) p_this );

274 275
    if( p_playlist->status.p_item )
        GetInputMeta( p_playlist->status.p_item->p_input, &args );
276 277 278 279 280

    pl_Release( p_playlist );
    REPLY_SEND;
}

281 282 283 284 285 286
/* Media Player information */

DBUS_METHOD( Identity )
{
    REPLY_INIT;
    OUT_ARGUMENTS;
287
    char *psz_identity = malloc( strlen( PACKAGE ) + strlen( VERSION ) + 2 );
288 289 290
    sprintf( psz_identity, "%s %s", PACKAGE, VERSION );
    ADD_STRING( &psz_identity );
    free( psz_identity );
291 292 293
    REPLY_SEND;
}

294 295 296
/* TrackList */

DBUS_METHOD( AddTrack )
297 298 299 300 301
{ /* add the string to the playlist, and play it if the boolean is true */
    REPLY_INIT;

    DBusError error;
    dbus_error_init( &error );
302
    playlist_t* p_playlist = NULL;
303 304 305 306 307 308 309 310 311 312 313

    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
314 315
        msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
                error.message );
316 317 318 319
        dbus_error_free( &error );
        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
    }

320
    p_playlist = pl_Yield( (vlc_object_t*) p_this );
Rafaël Carré's avatar
Rafaël Carré committed
321
    playlist_Add( p_playlist, psz_mrl, NULL, PLAYLIST_APPEND |
322 323
            ( ( b_play == TRUE ) ? PLAYLIST_GO : 0 ) ,
            PLAYLIST_END, VLC_TRUE, VLC_FALSE );
324 325 326 327 328
    pl_Release( p_playlist );

    REPLY_SEND;
}

329
DBUS_METHOD( GetCurrentTrack )
330
{
331 332
    REPLY_INIT;
    OUT_ARGUMENTS;
333 334
    /* FIXME 0 indicates the first item,
     * what to do if we're stopped, or empty ? */
335
    dbus_int32_t i_position = 0;
336 337
    playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
    playlist_item_t* p_tested_item = p_playlist->p_root_onelevel;
338

339 340
    if( p_playlist->status.p_item )
        while ( p_tested_item && p_tested_item->p_input->i_id !=
341
                p_playlist->status.p_item->p_input->i_id )
342 343 344 345
        {
            i_position++;
            TEST_NEXT_ITEM;
        }
346
    /* FIXME if p_tested_item is NULL at that point, what do we do ? */
347 348
    pl_Release( p_playlist );

349 350 351 352 353
    ADD_INT32( &i_position );
    REPLY_SEND;
}

DBUS_METHOD( GetMetadata )
354
{
355 356 357 358 359
    REPLY_INIT;
    OUT_ARGUMENTS;
    DBusError error;
    dbus_error_init( &error );

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
360
    dbus_int32_t i_position, i_count = 0;
361

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
362 363
    playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
    playlist_item_t* p_tested_item = p_playlist->p_root_onelevel;
364 365

    dbus_message_get_args( p_from, &error,
366 367
           DBUS_TYPE_INT32, &i_position,
           DBUS_TYPE_INVALID );
368 369 370 371 372 373 374 375

    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;
    }
376

377
    while ( p_tested_item && ( i_count < i_position ) )
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
378 379
    {
        i_count++;
380
        TEST_NEXT_ITEM;
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
381
    }
382

383 384
    if( p_tested_item )
        GetInputMeta ( p_tested_item->p_input, &args );
385

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
386
    pl_Release( p_playlist );
387 388 389 390
    REPLY_SEND;
}

DBUS_METHOD( GetLength )
391
{
392 393
    REPLY_INIT;
    OUT_ARGUMENTS;
394

395
    dbus_int32_t i_elements = 0;
396 397
    playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
    playlist_item_t* p_tested_item = p_playlist->p_root_onelevel;
398
    playlist_item_t* p_last_item = playlist_GetLastLeaf( p_playlist,
399
                    p_playlist->p_root_onelevel );
400

401 402
    while ( p_tested_item &&
               ( p_tested_item->p_input->i_id != p_last_item->p_input->i_id ) )
403 404
    {
        i_elements++;
405
        TEST_NEXT_ITEM;
406 407 408
    }

    pl_Release( p_playlist );
409

410 411 412 413 414
    ADD_INT32( &i_elements );
    REPLY_SEND;
}

DBUS_METHOD( DelTrack )
415
{
416 417 418 419 420
    REPLY_INIT;

    DBusError error;
    dbus_error_init( &error );

421 422 423
    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;
424 425 426 427 428 429 430 431 432 433 434 435

    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;
    }
436

437
    while ( p_tested_item && ( i_count < i_position ) )
438 439
    {
        i_count++;
440
        TEST_NEXT_ITEM;
441
    }
442

443 444 445 446 447 448 449 450
    if( p_tested_item )
    {
        PL_LOCK;
        playlist_DeleteFromInput( p_playlist,
            p_tested_item->p_input->i_id,
            VLC_TRUE );
        PL_UNLOCK;
    }
451

452
    pl_Release( p_playlist );
453

454 455 456
    REPLY_SEND;
}

457 458 459 460 461 462 463 464 465
DBUS_METHOD( Loop )
{
    REPLY_INIT;
    OUT_ARGUMENTS;

    DBusError error;
    dbus_bool_t b_loop;
    vlc_value_t val;
    playlist_t* p_playlist = NULL;
466

467 468 469 470
    dbus_error_init( &error );
    dbus_message_get_args( p_from, &error,
            DBUS_TYPE_BOOLEAN, &b_loop,
            DBUS_TYPE_INVALID );
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
    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;
    }

    val.b_bool = ( b_loop == TRUE ) ? VLC_TRUE : VLC_FALSE ;
    p_playlist = pl_Yield( (vlc_object_t*) p_this );
    var_Set ( p_playlist, "loop", val );
    pl_Release( ((vlc_object_t*) p_this) );

    REPLY_SEND;
}

DBUS_METHOD( Repeat )
{
    REPLY_INIT;
    OUT_ARGUMENTS;

    DBusError error;
    dbus_bool_t b_repeat;
    vlc_value_t val;
    playlist_t* p_playlist = NULL;
497

498 499 500 501
    dbus_error_init( &error );
    dbus_message_get_args( p_from, &error,
            DBUS_TYPE_BOOLEAN, &b_repeat,
            DBUS_TYPE_INVALID );
502

503 504 505 506 507 508 509 510 511
    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;
    }

    val.b_bool = ( b_repeat == TRUE ) ? VLC_TRUE : VLC_FALSE ;
512

513 514 515 516 517 518 519
    p_playlist = pl_Yield( (vlc_object_t*) p_this );
    var_Set ( p_playlist, "repeat", val );
    pl_Release( ((vlc_object_t*) p_this) );

    REPLY_SEND;
}

520 521 522 523 524 525 526 527 528
DBUS_METHOD( Random )
{
    REPLY_INIT;
    OUT_ARGUMENTS;

    DBusError error;
    dbus_bool_t b_random;
    vlc_value_t val;
    playlist_t* p_playlist = NULL;
529
 
530 531 532 533
    dbus_error_init( &error );
    dbus_message_get_args( p_from, &error,
            DBUS_TYPE_BOOLEAN, &b_random,
            DBUS_TYPE_INVALID );
534
 
535 536 537 538 539 540 541 542 543
    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;
    }

    val.b_bool = ( b_random == TRUE ) ? VLC_TRUE : VLC_FALSE ;
544
 
545 546 547 548 549 550
    p_playlist = pl_Yield( (vlc_object_t*) p_this );
    var_Set ( p_playlist, "random", val );
    pl_Release( ((vlc_object_t*) p_this) );

    REPLY_SEND;
}
551 552 553 554
/*****************************************************************************
 * Introspection method
 *****************************************************************************/

555
DBUS_METHOD( handle_introspect_root )
556
{ /* handles introspection of root object */
557 558
    REPLY_INIT;
    OUT_ARGUMENTS;
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
    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 );
576 577 578 579
    REPLY_SEND;
}

/*****************************************************************************
580
 * handle_*: answer to incoming messages
581 582 583
 *****************************************************************************/

#define METHOD_FUNC( method, function ) \
584
    else if( dbus_message_is_method_call( p_from, MPRIS_DBUS_INTERFACE, method ) )\
585 586
        return function( p_conn, p_from, p_this )

587 588
DBUS_METHOD( handle_root )
{
589 590 591

    if( dbus_message_is_method_call( p_from,
                DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
        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 );
607 608 609

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

610 611 612 613
    METHOD_FUNC( "Prev",                    Prev );
    METHOD_FUNC( "Next",                    Next );
    METHOD_FUNC( "Quit",                    Quit );
    METHOD_FUNC( "Stop",                    Stop );
614 615
    METHOD_FUNC( "Play",                    Play );
    METHOD_FUNC( "Pause",                   Pause );
616
    METHOD_FUNC( "Repeat",                  Repeat );
617 618 619 620
    METHOD_FUNC( "VolumeSet",               VolumeSet );
    METHOD_FUNC( "VolumeGet",               VolumeGet );
    METHOD_FUNC( "PositionSet",             PositionSet );
    METHOD_FUNC( "PositionGet",             PositionGet );
621
    METHOD_FUNC( "GetStatus",               GetStatus );
622
    METHOD_FUNC( "GetMetadata",             GetCurrentMetadata );
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639

    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 );
640
    METHOD_FUNC( "Loop",                    Loop );
641
    METHOD_FUNC( "Random",                  Random );
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661

    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_error_init( &error );
Antoine Cellerier's avatar
Antoine Cellerier committed
662

663 664 665 666 667 668 669 670 671 672 673
    /* 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;
    }

674
    /* register a well-known name on the bus */
675
    dbus_bus_request_name( p_conn, VLC_MPRIS_DBUS_SERVICE, 0, &error );
676 677
    if( dbus_error_is_set( &error ) )
    {
678 679
        msg_Err( p_this, "Error requesting service " VLC_MPRIS_DBUS_SERVICE
                 ": %s", error.message );
680 681 682 683 684 685
        dbus_error_free( &error );
        free( p_sys );
        return VLC_EGENERIC;
    }

    /* we register the objects */
686
    dbus_connection_register_object_path( p_conn, MPRIS_DBUS_ROOT_PATH,
687
            &vlc_dbus_root_vtable, p_this );
688
    dbus_connection_register_object_path( p_conn, MPRIS_DBUS_PLAYER_PATH,
689
            &vlc_dbus_player_vtable, p_this );
690
    dbus_connection_register_object_path( p_conn, MPRIS_DBUS_TRACKLIST_PATH,
691
            &vlc_dbus_tracklist_vtable, p_this );
692 693 694 695 696

    dbus_connection_flush( p_conn );

    p_playlist = pl_Yield( p_intf );
    PL_LOCK;
697
    var_AddCallback( p_playlist, "playlist-current", TrackChange, p_intf );
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
    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;
718
    var_DelCallback( p_playlist, "playlist-current", TrackChange, p_intf );
719 720 721
    PL_UNLOCK;
    pl_Release( p_playlist );

722 723
    dbus_connection_unref( p_intf->p_sys->p_conn );

724 725 726 727
    free( p_intf->p_sys );
}

/*****************************************************************************
Antoine Cellerier's avatar
Antoine Cellerier committed
728
 * Run: main loop
729 730 731 732
 *****************************************************************************/

static void Run          ( intf_thread_t *p_intf )
{
Rafaël Carré's avatar
Rafaël Carré committed
733
    while( !intf_ShouldDie( p_intf ) )
734 735 736 737 738 739 740
    {
        msleep( INTF_IDLE_SLEEP );
        dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
    }
}

/*****************************************************************************
741
 * TrackChange: Playlist item change callback
742 743
 *****************************************************************************/

744
DBUS_SIGNAL( TrackChangeSignal )
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
745
{ /* emit the metadata of the new item */
746
    SIGNAL_INIT( "TrackChange" );
747 748 749
    OUT_ARGUMENTS;

    input_thread_t *p_input = (input_thread_t*) p_data;
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
750
    GetInputMeta ( input_GetItem(p_input), &args );
751 752 753 754

    SIGNAL_SEND;
}

755
static int TrackChange( vlc_object_t *p_this, const char *psz_var,
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
            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 );

779
    TrackChangeSignal( p_sys->p_conn, p_input );
780 781 782 783 784

    vlc_object_release( p_input );
    return VLC_SUCCESS;
}

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
785 786 787 788 789 790 791 792 793 794
/*****************************************************************************
 * GetInputMeta: Fill a DBusMessage with the given input item metadata
 *****************************************************************************/

#define ADD_META( entry, type, data ) \
    if( data ) { \
        dbus_message_iter_open_container( &dict, DBUS_TYPE_DICT_ENTRY, \
                NULL, &dict_entry ); \
        dbus_message_iter_append_basic( &dict_entry, DBUS_TYPE_STRING, \
                &ppsz_meta_items[entry] ); \
795
        dbus_message_iter_open_container( &dict_entry, DBUS_TYPE_VARIANT, \
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
796 797 798 799 800
                type##_AS_STRING, &variant ); \
        dbus_message_iter_append_basic( &variant, \
                type, \
                & data ); \
        dbus_message_iter_close_container( &dict_entry, &variant ); \
801
        dbus_message_iter_close_container( &dict, &dict_entry ); }
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
802 803

#define ADD_VLC_META_STRING( entry, item ) \
804
    { \
805
        char * psz = input_item_Get##item( p_input );\
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
806
        ADD_META( entry, DBUS_TYPE_STRING, \
807
                  psz ); \
808
        free( psz ); \
809
    }
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
810

811
static int GetInputMeta( input_item_t* p_input,
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
812
                        DBusMessageIter *args )
813 814
{ /*FIXME: Works only for already read metadata. */

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
815
    DBusMessageIter dict, dict_entry, variant;
816 817
    /* We need the track length to be expressed in seconds
     * instead of milliseconds */
818
    dbus_int64_t i_length = ( input_item_GetDuration( p_input ) / 1000 );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
819

820
    const char* ppsz_meta_items[] =
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
821 822 823 824 825 826 827
    {
    "title", "artist", "genre", "copyright", "album", "tracknum",
    "description", "rating", "date", "setting", "url", "language",
    "nowplaying", "publisher", "encodedby", "arturl", "trackid",
    "status", "URI", "length", "video-codec", "audio-codec",
    "video-bitrate", "audio-bitrate", "audio-samplerate"
    };
828

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
829 830
    dbus_message_iter_open_container( args, DBUS_TYPE_ARRAY, "{sv}", &dict );

831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
    ADD_VLC_META_STRING( 0,  Title );
    ADD_VLC_META_STRING( 1,  Artist );
    ADD_VLC_META_STRING( 2,  Genre );
    ADD_VLC_META_STRING( 3,  Copyright );
    ADD_VLC_META_STRING( 4,  Album );
    ADD_VLC_META_STRING( 5,  TrackNum );
    ADD_VLC_META_STRING( 6,  Description );
    ADD_VLC_META_STRING( 7,  Rating );
    ADD_VLC_META_STRING( 8,  Date );
    ADD_VLC_META_STRING( 9,  Setting );
    ADD_VLC_META_STRING( 10, URL );
    ADD_VLC_META_STRING( 11, Language );
    ADD_VLC_META_STRING( 12, NowPlaying );
    ADD_VLC_META_STRING( 13, Publisher );
    ADD_VLC_META_STRING( 14, EncodedBy );
    ADD_VLC_META_STRING( 15, ArtURL );
847
    ADD_VLC_META_STRING( 16, TrackID );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
848

849
    vlc_mutex_lock( &p_input->lock );
850 851
    if( p_input->p_meta )
        ADD_META( 17, DBUS_TYPE_INT32, p_input->p_meta->i_status );
852 853 854
    vlc_mutex_unlock( &p_input->lock );

    ADD_VLC_META_STRING( 18, URI );
855
    ADD_META( 19, DBUS_TYPE_INT64, i_length );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
856 857 858 859 860 861 862

    dbus_message_iter_close_container( args, &dict );
    return VLC_SUCCESS;
}

#undef ADD_META
#undef ADD_VLC_META_STRING