dbus.c 25.5 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 45
 */

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

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

#include "dbus.h"

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

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

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

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

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

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
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
 *****************************************************************************/
87
#if 0
88
DBUS_METHOD( PlaylistExport_XSPF )
89
{ /*export playlist to an xspf file */
90 91

  /* reads the filename to export to */
92
  /* returns the status as int32:
93 94 95 96 97 98 99 100 101 102 103
   *    0 : success
   *    1 : error
   *    2 : playlist empty
   */
    REPLY_INIT;
    OUT_ARGUMENTS;

    DBusError error; 
    dbus_error_init( &error );

    char *psz_file;
104
    dbus_int32_t i_ret;
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

    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 ) );

135
    ADD_INT32( &i_ret );
136 137
    REPLY_SEND;
}
138 139 140
#endif

/* Player */
141

Rafaël Carré's avatar
Rafaël Carré committed
142 143 144 145 146 147
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) );
148
    vlc_object_kill(((vlc_object_t*)p_this)->p_libvlc);
Rafaël Carré's avatar
Rafaël Carré committed
149 150 151
    REPLY_SEND;
}

152 153 154 155 156
DBUS_METHOD( PositionGet )
{ /* returns position as an int in the range [0;1000] */
    REPLY_INIT;
    OUT_ARGUMENTS;
    vlc_value_t position;
157
    dbus_int32_t i_pos;
158 159 160 161 162 163 164 165 166 167 168 169

    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 ;
    }
    pl_Release( ((vlc_object_t*) p_this) );
170
    ADD_INT32( &i_pos );
171 172 173 174 175 176 177 178
    REPLY_SEND;
}

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

    REPLY_INIT;
    vlc_value_t position;
179
    playlist_t* p_playlist = NULL;
180
    dbus_int32_t i_pos;
181 182 183 184 185

    DBusError error;
    dbus_error_init( &error );

    dbus_message_get_args( p_from, &error,
186
            DBUS_TYPE_INT32, &i_pos,
187 188 189 190 191 192 193 194 195
            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;
    }
196
    p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
197 198 199 200 201 202 203 204 205 206 207
    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
208
DBUS_METHOD( VolumeGet )
209
{ /* returns volume in percentage */
Rafaël Carré's avatar
Rafaël Carré committed
210 211
    REPLY_INIT;
    OUT_ARGUMENTS;
212 213 214
    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
215
    aout_VolumeGet( (vlc_object_t*) p_this, &i_vol );
216 217
    i_dbus_vol = ( 100 * i_vol ) / AOUT_VOLUME_MAX;
    ADD_INT32( &i_dbus_vol );
Rafaël Carré's avatar
Rafaël Carré committed
218 219 220 221 222 223 224 225 226 227
    REPLY_SEND;
}

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

    DBusError error;
    dbus_error_init( &error );

228 229
    dbus_int32_t i_dbus_vol;
    audio_volume_t i_vol;
Rafaël Carré's avatar
Rafaël Carré committed
230 231

    dbus_message_get_args( p_from, &error,
232
            DBUS_TYPE_INT32, &i_dbus_vol,
Rafaël Carré's avatar
Rafaël Carré committed
233 234 235 236 237 238 239 240 241 242
            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;
    }

243 244
    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
245 246 247 248

    REPLY_SEND;
}

249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
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
275

276 277
DBUS_METHOD( GetStatus )
{ /* returns an int: 0=playing 1=paused 2=stopped */
278 279 280
    REPLY_INIT;
    OUT_ARGUMENTS;

281
    dbus_int32_t i_status;
282
    vlc_value_t val;
283

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

287 288
    i_status = 2;
    if( p_input )
289 290 291
    {
        var_Get( p_input, "state", &val );
        if( val.i_int == PAUSE_S )
292
            i_status = 1;
293
        else if( val.i_int == PLAYING_S )
294
            i_status = 0;
295
    }
Antoine Cellerier's avatar
Antoine Cellerier committed
296

297 298
    pl_Release( p_playlist );

299
    ADD_INT32( &i_status );
300 301 302
    REPLY_SEND;
}

303 304
DBUS_METHOD( Pause )
{
305
    REPLY_INIT;
306 307 308 309 310
    playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
    playlist_Pause( p_playlist );
    pl_Release( p_playlist );
    REPLY_SEND;
}
311

312 313 314
DBUS_METHOD( Play )
{
    REPLY_INIT;
315
    playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
316
    playlist_Play( p_playlist );
317
    pl_Release( p_playlist );
318 319
    REPLY_SEND;
}
320

321 322 323 324 325 326
DBUS_METHOD( Disconnect )
{
    REPLY_INIT;
    DBusError error;
    int i;
    dbus_error_init( &error );
327
    i = dbus_bus_release_name( p_conn, "org.mpris.vlc", &error );
328 329 330 331 332 333 334 335 336
    if( ( i == -1 ) && ( dbus_error_is_set( &error ) ) )
    {
        msg_Err( (vlc_object_t*) p_this, "D-Bus disconnection failed : %s\n",
            error.message );
        dbus_error_free( &error );
    }
    REPLY_SEND;
}

337 338 339 340 341 342 343 344 345 346 347 348
DBUS_METHOD( GetCurrentMetadata )
{
    REPLY_INIT;
    OUT_ARGUMENTS;
    playlist_t* p_playlist = pl_Yield( (vlc_object_t*) p_this );

    GetInputMeta( p_playlist->status.p_item->p_input, &args );

    pl_Release( p_playlist );
    REPLY_SEND;
}

349 350 351 352 353 354 355 356 357 358
/* 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 );
359 360 361
    REPLY_SEND;
}

362 363 364
/* TrackList */

DBUS_METHOD( AddTrack )
365 366 367 368 369
{ /* add the string to the playlist, and play it if the boolean is true */
    REPLY_INIT;

    DBusError error;
    dbus_error_init( &error );
370
    playlist_t* p_playlist = NULL;
371 372 373 374 375 376 377 378 379 380 381

    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
382 383
        msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
                error.message );
384 385 386 387
        dbus_error_free( &error );
        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
    }

388
    p_playlist = pl_Yield( (vlc_object_t*) p_this );
Rafaël Carré's avatar
Rafaël Carré committed
389
    playlist_Add( p_playlist, psz_mrl, NULL, PLAYLIST_APPEND |
390 391
            ( ( b_play == TRUE ) ? PLAYLIST_GO : 0 ) ,
            PLAYLIST_END, VLC_TRUE, VLC_FALSE );
392 393 394 395 396
    pl_Release( p_playlist );

    REPLY_SEND;
}

397
DBUS_METHOD( GetCurrentTrack )
398
{
399 400 401
    REPLY_INIT;
    OUT_ARGUMENTS;
    dbus_int32_t i_position = 0;
402 403
    playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
    playlist_item_t* p_tested_item = p_playlist->p_root_onelevel;
404 405

    while ( p_tested_item->p_input->i_id !=
406
                    p_playlist->status.p_item->p_input->i_id )
407 408
    {
        i_position++;
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
409
        TEST_NEXT;
410 411 412 413
    }

    pl_Release( p_playlist );

414 415 416 417 418
    ADD_INT32( &i_position );
    REPLY_SEND;
}

DBUS_METHOD( GetMetadata )
419
{
420 421 422 423 424
    REPLY_INIT;
    OUT_ARGUMENTS;
    DBusError error;
    dbus_error_init( &error );

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
425
    dbus_int32_t i_position, i_count = 0;
426

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
427 428
    playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
    playlist_item_t* p_tested_item = p_playlist->p_root_onelevel;
429 430 431 432 433 434 435 436 437 438 439 440

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

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
442 443 444
    while ( i_count < i_position ) 
    {
        i_count++;
445
        TEST_NEXT;
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
446
    }
447

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
448
    GetInputMeta ( p_tested_item->p_input, &args );
449

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
450
    pl_Release( p_playlist );
451 452 453 454
    REPLY_SEND;
}

DBUS_METHOD( GetLength )
455
{
456 457
    REPLY_INIT;
    OUT_ARGUMENTS;
458

459
    dbus_int32_t i_elements = 0;
460 461
    playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
    playlist_item_t* p_tested_item = p_playlist->p_root_onelevel;
462
    playlist_item_t* p_last_item = playlist_GetLastLeaf( p_playlist,
463
                    p_playlist->p_root_onelevel ); 
464

465
    while ( p_tested_item->p_input->i_id != p_last_item->p_input->i_id )
466 467
    {
        i_elements++;
468
        TEST_NEXT;
469 470 471
    }

    pl_Release( p_playlist );
472

473 474 475 476 477
    ADD_INT32( &i_elements );
    REPLY_SEND;
}

DBUS_METHOD( DelTrack )
478
{
479 480 481 482 483
    REPLY_INIT;

    DBusError error;
    dbus_error_init( &error );

484 485 486
    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;
487 488 489 490 491 492 493 494 495 496 497 498

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

500 501 502
    while ( i_count < i_position ) 
    {
        i_count++;
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
503
        TEST_NEXT;
504
    }
505

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
506
    PL_LOCK; 
507 508 509
    playlist_DeleteFromInput( p_playlist,
        p_tested_item->p_input->i_id,
        VLC_TRUE );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
510
    PL_UNLOCK;
511

512
    pl_Release( p_playlist );
513

514 515 516
    REPLY_SEND;
}

517 518 519 520 521 522 523 524 525
DBUS_METHOD( Loop )
{
    REPLY_INIT;
    OUT_ARGUMENTS;

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

527 528 529 530
    dbus_error_init( &error );
    dbus_message_get_args( p_from, &error,
            DBUS_TYPE_BOOLEAN, &b_loop,
            DBUS_TYPE_INVALID );
531

532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
    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;
557

558 559 560 561
    dbus_error_init( &error );
    dbus_message_get_args( p_from, &error,
            DBUS_TYPE_BOOLEAN, &b_repeat,
            DBUS_TYPE_INVALID );
562

563 564 565 566 567 568 569 570 571
    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 ;
572

573 574 575 576 577 578 579
    p_playlist = pl_Yield( (vlc_object_t*) p_this );
    var_Set ( p_playlist, "repeat", val );
    pl_Release( ((vlc_object_t*) p_this) );

    REPLY_SEND;
}

580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
DBUS_METHOD( Random )
{
    REPLY_INIT;
    OUT_ARGUMENTS;

    DBusError error;
    dbus_bool_t b_random;
    vlc_value_t val;
    playlist_t* p_playlist = NULL;
    
    dbus_error_init( &error );
    dbus_message_get_args( p_from, &error,
            DBUS_TYPE_BOOLEAN, &b_random,
            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;
    }

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

    REPLY_SEND;
}
611 612 613 614
/*****************************************************************************
 * Introspection method
 *****************************************************************************/

615
DBUS_METHOD( handle_introspect_root )
616
{ /* handles introspection of root object */
617 618
    REPLY_INIT;
    OUT_ARGUMENTS;
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
    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 );
636 637 638 639
    REPLY_SEND;
}

/*****************************************************************************
640
 * handle_*: answer to incoming messages
641 642 643 644 645 646
 *****************************************************************************/

#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 )

647 648
DBUS_METHOD( handle_root )
{
649 650 651

    if( dbus_message_is_method_call( p_from,
                DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
        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 );
667 668 669

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

670 671 672 673
    METHOD_FUNC( "Prev",                    Prev );
    METHOD_FUNC( "Next",                    Next );
    METHOD_FUNC( "Quit",                    Quit );
    METHOD_FUNC( "Stop",                    Stop );
674 675
    METHOD_FUNC( "Play",                    Play );
    METHOD_FUNC( "Pause",                   Pause );
676
    METHOD_FUNC( "Repeat",                  Repeat );
677
    METHOD_FUNC( "Disconnect",              Disconnect );
678 679 680 681
    METHOD_FUNC( "VolumeSet",               VolumeSet );
    METHOD_FUNC( "VolumeGet",               VolumeGet );
    METHOD_FUNC( "PositionSet",             PositionSet );
    METHOD_FUNC( "PositionGet",             PositionGet );
682
    METHOD_FUNC( "GetStatus",               GetStatus );
683
    METHOD_FUNC( "GetMetadata",             GetCurrentMetadata );
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700

    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 );
701
    METHOD_FUNC( "Loop",                    Loop );
702
    METHOD_FUNC( "Random",                  Random );
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722

    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
723

724
    dbus_error_init( &error );
Antoine Cellerier's avatar
Antoine Cellerier committed
725

726 727 728 729 730 731 732 733 734 735 736
    /* 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;
    }

737
    /* register a well-known name on the bus */
738
    dbus_bus_request_name( p_conn, "org.mpris.vlc", 0, &error );
739 740
    if( dbus_error_is_set( &error ) )
    {
741
        msg_Err( p_this, "Error requesting org.mpris.vlc service:"                " %s\n", error.message );
742 743 744 745 746 747 748 749 750 751 752 753
        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 );
754 755 756 757 758

    dbus_connection_flush( p_conn );

    p_playlist = pl_Yield( p_intf );
    PL_LOCK;
759
    var_AddCallback( p_playlist, "playlist-current", TrackChange, p_intf );
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
    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;
780
    var_DelCallback( p_playlist, "playlist-current", TrackChange, p_intf );
781 782 783
    PL_UNLOCK;
    pl_Release( p_playlist );

784 785
    dbus_connection_unref( p_intf->p_sys->p_conn );

786 787 788 789
    free( p_intf->p_sys );
}

/*****************************************************************************
Antoine Cellerier's avatar
Antoine Cellerier committed
790
 * Run: main loop
791 792 793 794 795 796 797 798 799 800 801 802
 *****************************************************************************/

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

/*****************************************************************************
803
 * TrackChange: Playlist item change callback
804 805
 *****************************************************************************/

806
DBUS_SIGNAL( TrackChangeSignal )
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
807
{ /* emit the metadata of the new item */
808
    SIGNAL_INIT( "TrackChange" );
809 810 811
    OUT_ARGUMENTS;

    input_thread_t *p_input = (input_thread_t*) p_data;
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
812
    GetInputMeta ( input_GetItem(p_input), &args );
813 814 815 816

    SIGNAL_SEND;
}

817
static int TrackChange( vlc_object_t *p_this, const char *psz_var,
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
            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 );

841
    TrackChangeSignal( p_sys->p_conn, p_input );
842 843 844 845 846

    vlc_object_release( p_input );
    return VLC_SUCCESS;
}

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
847 848 849 850 851 852 853 854 855 856
/*****************************************************************************
 * 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] ); \
857
        dbus_message_iter_open_container( &dict_entry, DBUS_TYPE_VARIANT, \
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
858 859 860 861 862
                type##_AS_STRING, &variant ); \
        dbus_message_iter_append_basic( &variant, \
                type, \
                & data ); \
        dbus_message_iter_close_container( &dict_entry, &variant ); \
863
        dbus_message_iter_close_container( &dict, &dict_entry ); }
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
864 865

#define ADD_VLC_META_STRING( entry, item ) \
866
    { \
867
        char * psz = input_item_Get##item( p_input );\
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
868
        ADD_META( entry, DBUS_TYPE_STRING, \
869
                  psz ); \
870
        free( psz ); \
871
    }
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
872

873
static int GetInputMeta( input_item_t* p_input,
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
874
                        DBusMessageIter *args )
875 876
{ /*FIXME: Works only for already read metadata. */

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
877
    DBusMessageIter dict, dict_entry, variant;
878 879 880 881
    /* We need the track length to be expressed in seconds
     * instead of milliseconds */
    dbus_int64_t i_length = (p_input->i_duration / 1000);

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
882 883 884 885 886 887 888 889 890

    const char* ppsz_meta_items[] = 
    {
    "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"
    };
891

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

894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
    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 );
    ADD_VLC_META_STRING( 16, TrackID ); 
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
911 912

    ADD_META( 17, DBUS_TYPE_INT32, p_input->p_meta->i_status );
913 914
    ADD_META( 18, DBUS_TYPE_STRING, p_input->psz_uri );
    ADD_META( 19, DBUS_TYPE_INT64, i_length );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
915 916 917 918 919 920 921

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

#undef ADD_META
#undef ADD_VLC_META_STRING