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

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

#include <dbus/dbus.h>

#include "dbus.h"

46 47 48 49
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

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

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

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

65 66 67 68 69
static int StateChange( vlc_object_t *, const char *, vlc_value_t,
                        vlc_value_t, void * );

static int TrackChange( vlc_object_t *, const char *, vlc_value_t,
                        vlc_value_t, void * );
70

71 72 73
static int StatusChangeEmit( vlc_object_t *, const char *, vlc_value_t,
                        vlc_value_t, void * );

Mirsal Ennaime's avatar
Mirsal Ennaime committed
74 75 76
static int TrackListChangeEmit( vlc_object_t *, const char *, vlc_value_t,
                        vlc_value_t, void * );

77
static int GetInputMeta ( input_item_t *, DBusMessageIter * );
78
static int MarshalStatus ( intf_thread_t *, DBusMessageIter *, vlc_bool_t );
79
static int UpdateCaps( intf_thread_t*, vlc_bool_t );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
80

81 82 83 84 85 86 87 88 89
/* GetCaps() capabilities */
enum
{
     CAPS_NONE                  = 0,
     CAPS_CAN_GO_NEXT           = 1 << 0,
     CAPS_CAN_GO_PREV           = 1 << 1,
     CAPS_CAN_PAUSE             = 1 << 2,
     CAPS_CAN_PLAY              = 1 << 3,
     CAPS_CAN_SEEK              = 1 << 4,
90 91
     CAPS_CAN_PROVIDE_METADATA  = 1 << 5,
     CAPS_CAN_HAS_TRACKLIST     = 1 << 6
92 93
};

94 95 96
struct intf_sys_t
{
    DBusConnection *p_conn;
97
    vlc_bool_t      b_meta_read;
98
    dbus_int32_t    i_caps;
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
};

/*****************************************************************************
 * 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
 *****************************************************************************/
117 118

/* Player */
119

Rafaël Carré's avatar
Rafaël Carré committed
120 121 122 123 124 125
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) );
126
    vlc_object_kill(((vlc_object_t*)p_this)->p_libvlc);
Rafaël Carré's avatar
Rafaël Carré committed
127 128 129
    REPLY_SEND;
}

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
DBUS_METHOD( MprisVersion )
{ /*implemented version of the mpris spec */
    REPLY_INIT;
    OUT_ARGUMENTS;
    VLC_UNUSED( p_this );
    dbus_uint16_t i_major = VLC_MPRIS_VERSION_MAJOR;
    dbus_uint16_t i_minor = VLC_MPRIS_VERSION_MINOR;
    DBusMessageIter version;

    dbus_message_iter_open_container( &args, DBUS_TYPE_STRUCT, NULL, &version );
    dbus_message_iter_append_basic( &version, DBUS_TYPE_UINT16, &i_major );
    dbus_message_iter_append_basic( &version, DBUS_TYPE_UINT16, &i_minor );
    dbus_message_iter_close_container( &args, &version );
    REPLY_SEND;
}

146
DBUS_METHOD( PositionGet )
147
{ /* returns position in milliseconds */
148 149 150
    REPLY_INIT;
    OUT_ARGUMENTS;
    vlc_value_t position;
151
    dbus_int32_t i_pos;
152 153

    playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
154
    PL_LOCK;
155 156 157 158 159 160
    input_thread_t *p_input = p_playlist->p_input;

    if( !p_input )
        i_pos = 0;
    else
    {
161 162
        var_Get( p_input, "time", &position );
        i_pos = position.i_time / 1000;
163
    }
164
    PL_UNLOCK;
165
    pl_Release( ((vlc_object_t*) p_this) );
166
    ADD_INT32( &i_pos );
167 168 169 170
    REPLY_SEND;
}

DBUS_METHOD( PositionSet )
171
{ /* set position in milliseconds */
172 173 174

    REPLY_INIT;
    vlc_value_t position;
175
    playlist_t* p_playlist = NULL;
176
    dbus_int32_t i_pos;
177 178 179 180 181

    DBusError error;
    dbus_error_init( &error );

    dbus_message_get_args( p_from, &error,
182
            DBUS_TYPE_INT32, &i_pos,
183 184 185 186 187 188 189 190 191
            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;
    }
192
    p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
193
    PL_LOCK;
194 195 196 197
    input_thread_t *p_input = p_playlist->p_input;

    if( p_input )
    {
198 199
        position.i_time = i_pos * 1000;
        var_Set( p_input, "time", position );
200
    }
201
    PL_UNLOCK;
202 203 204 205
    pl_Release( ((vlc_object_t*) p_this) );
    REPLY_SEND;
}

Rafaël Carré's avatar
Rafaël Carré committed
206
DBUS_METHOD( VolumeGet )
207
{ /* returns volume in percentage */
Rafaël Carré's avatar
Rafaël Carré committed
208 209
    REPLY_INIT;
    OUT_ARGUMENTS;
210 211 212
    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
213
    aout_VolumeGet( (vlc_object_t*) p_this, &i_vol );
214 215
    i_dbus_vol = ( 100 * i_vol ) / AOUT_VOLUME_MAX;
    ADD_INT32( &i_dbus_vol );
Rafaël Carré's avatar
Rafaël Carré committed
216 217 218 219 220 221 222 223 224 225
    REPLY_SEND;
}

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

    DBusError error;
    dbus_error_init( &error );

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

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

241 242
    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
243 244 245 246

    REPLY_SEND;
}

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

274
DBUS_METHOD( GetStatus )
275 276 277 278 279 280 281
{ /* returns the current status as a struct of 4 ints */
/*
    First   0 = Playing, 1 = Paused, 2 = Stopped.
    Second  0 = Playing linearly , 1 = Playing randomly.
    Third   0 = Go to the next element once the current has finished playing , 1 = Repeat the current element
    Fourth  0 = Stop playing once the last element has been played, 1 = Never give up playing *
 */
282 283 284
    REPLY_INIT;
    OUT_ARGUMENTS;

285
    MarshalStatus( p_this, &args, VLC_TRUE );
286

287 288 289
    REPLY_SEND;
}

290 291
DBUS_METHOD( Pause )
{
292
    REPLY_INIT;
293 294 295 296 297
    playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
    playlist_Pause( p_playlist );
    pl_Release( p_playlist );
    REPLY_SEND;
}
298

299 300 301
DBUS_METHOD( Play )
{
    REPLY_INIT;
302
    playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
303
    playlist_Play( p_playlist );
304
    pl_Release( p_playlist );
305 306
    REPLY_SEND;
}
307

308 309 310 311 312
DBUS_METHOD( GetCurrentMetadata )
{
    REPLY_INIT;
    OUT_ARGUMENTS;
    playlist_t* p_playlist = pl_Yield( (vlc_object_t*) p_this );
313
    PL_LOCK;
314 315
    if( p_playlist->status.p_item )
        GetInputMeta( p_playlist->status.p_item->p_input, &args );
316
    PL_UNLOCK;
317 318 319 320
    pl_Release( p_playlist );
    REPLY_SEND;
}

321 322 323 324 325
DBUS_METHOD( GetCaps )
{
    REPLY_INIT;
    OUT_ARGUMENTS;

326
    ADD_INT32( &((intf_thread_t*)p_this)->p_sys->i_caps );
327 328 329 330

    REPLY_SEND;
}

331 332 333 334
/* Media Player information */

DBUS_METHOD( Identity )
{
335
    VLC_UNUSED(p_this);
336 337
    REPLY_INIT;
    OUT_ARGUMENTS;
338 339 340 341 342 343 344 345 346
    char *psz_identity;
    if( asprintf( &psz_identity, "%s %s", PACKAGE, VERSION ) != -1 )
    {
        ADD_STRING( &psz_identity );
        free( psz_identity );
    }
    else
        return DBUS_HANDLER_RESULT_NEED_MEMORY;

347 348 349
    REPLY_SEND;
}

350 351 352
/* TrackList */

DBUS_METHOD( AddTrack )
353 354 355 356 357
{ /* add the string to the playlist, and play it if the boolean is true */
    REPLY_INIT;

    DBusError error;
    dbus_error_init( &error );
358
    playlist_t* p_playlist = NULL;
359 360 361 362 363 364 365 366 367 368 369

    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
370 371
        msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
                error.message );
372 373 374 375
        dbus_error_free( &error );
        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
    }

376
    p_playlist = pl_Yield( (vlc_object_t*) p_this );
Rafaël Carré's avatar
Rafaël Carré committed
377
    playlist_Add( p_playlist, psz_mrl, NULL, PLAYLIST_APPEND |
378 379
            ( ( b_play == TRUE ) ? PLAYLIST_GO : 0 ) ,
            PLAYLIST_END, VLC_TRUE, VLC_FALSE );
380 381 382 383 384
    pl_Release( p_playlist );

    REPLY_SEND;
}

385
DBUS_METHOD( GetCurrentTrack )
386
{
387 388
    REPLY_INIT;
    OUT_ARGUMENTS;
389

390 391
    playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
    dbus_int32_t i_position = p_playlist->i_current_index;
392 393
    pl_Release( p_playlist );

394 395 396 397 398
    ADD_INT32( &i_position );
    REPLY_SEND;
}

DBUS_METHOD( GetMetadata )
399
{
400 401 402 403 404
    REPLY_INIT;
    OUT_ARGUMENTS;
    DBusError error;
    dbus_error_init( &error );

405
    dbus_int32_t i_position;
406

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
407
    playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
408
    PL_LOCK;
409 410

    dbus_message_get_args( p_from, &error,
411 412
           DBUS_TYPE_INT32, &i_position,
           DBUS_TYPE_INVALID );
413 414 415

    if( dbus_error_is_set( &error ) )
    {
416 417
        PL_UNLOCK;
        pl_Release( p_playlist );
418 419 420 421 422
        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;
    }
423

424
    if( i_position <= p_playlist->items.i_size / 2 )
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
425
    {
426
        GetInputMeta( p_playlist->items.p_elems[i_position*2-1]->p_input, &args );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
427
    }
428

429
    PL_UNLOCK;
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
430
    pl_Release( p_playlist );
431 432 433 434
    REPLY_SEND;
}

DBUS_METHOD( GetLength )
435
{
436 437
    REPLY_INIT;
    OUT_ARGUMENTS;
438 439

    playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
440
    dbus_int32_t i_elements = p_playlist->items.i_size / 2;
441
    pl_Release( p_playlist );
442

443 444 445 446 447
    ADD_INT32( &i_elements );
    REPLY_SEND;
}

DBUS_METHOD( DelTrack )
448
{
449 450 451 452 453
    REPLY_INIT;

    DBusError error;
    dbus_error_init( &error );

454
    dbus_int32_t i_position;
455
    playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
456 457 458 459 460 461 462 463 464 465 466 467

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

Rafaël Carré's avatar
Rafaël Carré committed
469
    if( i_position <= p_playlist->items.i_size / 2 )
470 471
    {
        playlist_DeleteFromInput( p_playlist,
472 473
            p_playlist->items.p_elems[i_position*2-1]->i_id,
            VLC_FALSE );
474
    }
475

476
    pl_Release( p_playlist );
477

478 479 480
    REPLY_SEND;
}

481 482 483 484 485 486 487 488 489
DBUS_METHOD( Loop )
{
    REPLY_INIT;
    OUT_ARGUMENTS;

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

491 492 493 494
    dbus_error_init( &error );
    dbus_message_get_args( p_from, &error,
            DBUS_TYPE_BOOLEAN, &b_loop,
            DBUS_TYPE_INVALID );
495

496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
    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;
521

522 523 524 525
    dbus_error_init( &error );
    dbus_message_get_args( p_from, &error,
            DBUS_TYPE_BOOLEAN, &b_repeat,
            DBUS_TYPE_INVALID );
526

527 528 529 530 531 532 533 534 535
    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 ;
536

537 538 539 540 541 542 543
    p_playlist = pl_Yield( (vlc_object_t*) p_this );
    var_Set ( p_playlist, "repeat", val );
    pl_Release( ((vlc_object_t*) p_this) );

    REPLY_SEND;
}

544 545 546 547 548 549 550 551 552
DBUS_METHOD( Random )
{
    REPLY_INIT;
    OUT_ARGUMENTS;

    DBusError error;
    dbus_bool_t b_random;
    vlc_value_t val;
    playlist_t* p_playlist = NULL;
Rafaël Carré's avatar
Rafaël Carré committed
553

554 555 556 557
    dbus_error_init( &error );
    dbus_message_get_args( p_from, &error,
            DBUS_TYPE_BOOLEAN, &b_random,
            DBUS_TYPE_INVALID );
Rafaël Carré's avatar
Rafaël Carré committed
558

559 560 561 562 563 564 565 566 567
    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 ;
Rafaël Carré's avatar
Rafaël Carré committed
568

569 570 571 572 573 574
    p_playlist = pl_Yield( (vlc_object_t*) p_this );
    var_Set ( p_playlist, "random", val );
    pl_Release( ((vlc_object_t*) p_this) );

    REPLY_SEND;
}
575 576 577 578
/*****************************************************************************
 * Introspection method
 *****************************************************************************/

579
DBUS_METHOD( handle_introspect_root )
580
{ /* handles introspection of root object */
581
    VLC_UNUSED(p_this);
582 583
    REPLY_INIT;
    OUT_ARGUMENTS;
584 585 586 587 588 589
    ADD_STRING( &psz_introspection_xml_data_root );
    REPLY_SEND;
}

DBUS_METHOD( handle_introspect_player )
{
590
    VLC_UNUSED(p_this);
591 592 593 594 595 596 597 598
    REPLY_INIT;
    OUT_ARGUMENTS;
    ADD_STRING( &psz_introspection_xml_data_player );
    REPLY_SEND;
}

DBUS_METHOD( handle_introspect_tracklist )
{
599
    VLC_UNUSED(p_this);
600 601 602
    REPLY_INIT;
    OUT_ARGUMENTS;
    ADD_STRING( &psz_introspection_xml_data_tracklist );
603 604 605 606
    REPLY_SEND;
}

/*****************************************************************************
607
 * handle_*: answer to incoming messages
608 609 610
 *****************************************************************************/

#define METHOD_FUNC( method, function ) \
611
    else if( dbus_message_is_method_call( p_from, MPRIS_DBUS_INTERFACE, method ) )\
612 613
        return function( p_conn, p_from, p_this )

614 615
DBUS_METHOD( handle_root )
{
616 617 618

    if( dbus_message_is_method_call( p_from,
                DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
619 620 621 622 623
        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 );
624
    METHOD_FUNC( "MprisVersion",            MprisVersion );
625 626 627 628 629 630 631 632 633

    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}


DBUS_METHOD( handle_player )
{
    if( dbus_message_is_method_call( p_from,
                DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
634
        return handle_introspect_player( p_conn, p_from, p_this );
635 636 637

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

638 639 640 641
    METHOD_FUNC( "Prev",                    Prev );
    METHOD_FUNC( "Next",                    Next );
    METHOD_FUNC( "Quit",                    Quit );
    METHOD_FUNC( "Stop",                    Stop );
642 643
    METHOD_FUNC( "Play",                    Play );
    METHOD_FUNC( "Pause",                   Pause );
644
    METHOD_FUNC( "Repeat",                  Repeat );
645 646 647 648
    METHOD_FUNC( "VolumeSet",               VolumeSet );
    METHOD_FUNC( "VolumeGet",               VolumeGet );
    METHOD_FUNC( "PositionSet",             PositionSet );
    METHOD_FUNC( "PositionGet",             PositionGet );
649
    METHOD_FUNC( "GetStatus",               GetStatus );
650
    METHOD_FUNC( "GetMetadata",             GetCurrentMetadata );
651
    METHOD_FUNC( "GetCaps",                 GetCaps );
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668

    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 );
669
    METHOD_FUNC( "Loop",                    Loop );
670
    METHOD_FUNC( "Random",                  Random );
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689

    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;

690
    p_sys->b_meta_read = VLC_FALSE;
691
    p_sys->i_caps = CAPS_NONE;
692

693
    dbus_error_init( &error );
Antoine Cellerier's avatar
Antoine Cellerier committed
694

695 696 697 698 699 700 701 702 703 704 705
    /* 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;
    }

706
    /* register a well-known name on the bus */
707
    dbus_bus_request_name( p_conn, VLC_MPRIS_DBUS_SERVICE, 0, &error );
708 709
    if( dbus_error_is_set( &error ) )
    {
710 711
        msg_Err( p_this, "Error requesting service " VLC_MPRIS_DBUS_SERVICE
                 ": %s", error.message );
712 713 714 715 716 717
        dbus_error_free( &error );
        free( p_sys );
        return VLC_EGENERIC;
    }

    /* we register the objects */
718
    dbus_connection_register_object_path( p_conn, MPRIS_DBUS_ROOT_PATH,
719
            &vlc_dbus_root_vtable, p_this );
720
    dbus_connection_register_object_path( p_conn, MPRIS_DBUS_PLAYER_PATH,
721
            &vlc_dbus_player_vtable, p_this );
722
    dbus_connection_register_object_path( p_conn, MPRIS_DBUS_TRACKLIST_PATH,
723
            &vlc_dbus_tracklist_vtable, p_this );
724 725 726 727 728

    dbus_connection_flush( p_conn );

    p_playlist = pl_Yield( p_intf );
    PL_LOCK;
729
    var_AddCallback( p_playlist, "playlist-current", TrackChange, p_intf );
Mirsal Ennaime's avatar
Mirsal Ennaime committed
730 731 732
    var_AddCallback( p_playlist, "intf-change", TrackListChangeEmit, p_intf );
    var_AddCallback( p_playlist, "item-append", TrackListChangeEmit, p_intf );
    var_AddCallback( p_playlist, "item-deleted", TrackListChangeEmit, p_intf );
733 734 735
    var_AddCallback( p_playlist, "random", StatusChangeEmit, p_intf );
    var_AddCallback( p_playlist, "repeat", StatusChangeEmit, p_intf );
    var_AddCallback( p_playlist, "loop", StatusChangeEmit, p_intf );
736 737 738 739 740 741 742
    PL_UNLOCK;
    pl_Release( p_playlist );

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

743
    UpdateCaps( p_intf, VLC_FALSE );
744

745 746 747 748 749 750 751 752 753 754 755
    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 );;
756 757 758
    input_thread_t  *p_input;

    p_this->b_dead = VLC_TRUE;
759 760

    PL_LOCK;
761
    var_DelCallback( p_playlist, "playlist-current", TrackChange, p_intf );
Mirsal Ennaime's avatar
Mirsal Ennaime committed
762 763 764
    var_DelCallback( p_playlist, "intf-change", TrackListChangeEmit, p_intf );
    var_DelCallback( p_playlist, "item-append", TrackListChangeEmit, p_intf );
    var_DelCallback( p_playlist, "item-deleted", TrackListChangeEmit, p_intf );
765 766 767 768 769 770 771 772 773 774 775 776
    var_DelCallback( p_playlist, "random", StatusChangeEmit, p_intf );
    var_DelCallback( p_playlist, "repeat", StatusChangeEmit, p_intf );
    var_DelCallback( p_playlist, "loop", StatusChangeEmit, p_intf );

    p_input = p_playlist->p_input;
    if ( p_input )
    {
        vlc_object_yield( p_input );
        var_DelCallback( p_input, "state", StateChange, p_intf );
        vlc_object_release( p_input );
    }

777 778 779
    PL_UNLOCK;
    pl_Release( p_playlist );

780 781
    dbus_connection_unref( p_intf->p_sys->p_conn );

782 783 784 785
    free( p_intf->p_sys );
}

/*****************************************************************************
Antoine Cellerier's avatar
Antoine Cellerier committed
786
 * Run: main loop
787 788 789 790
 *****************************************************************************/

static void Run          ( intf_thread_t *p_intf )
{
Rafaël Carré's avatar
Rafaël Carré committed
791
    while( !intf_ShouldDie( p_intf ) )
792 793 794 795 796 797
    {
        msleep( INTF_IDLE_SLEEP );
        dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
    }
}

798 799 800 801 802 803 804 805 806 807 808 809
/******************************************************************************
 * CapsChange: player capabilities change signal
 *****************************************************************************/
DBUS_SIGNAL( CapsChangeSignal )
{
    SIGNAL_INIT( "CapsChange" );
    OUT_ARGUMENTS;

    ADD_INT32( &((intf_thread_t*)p_data)->p_sys->i_caps );
    SIGNAL_SEND;
}

Mirsal Ennaime's avatar
Mirsal Ennaime committed
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
/******************************************************************************
 * TrackListChange: tracklist order / length change signal 
 *****************************************************************************/
DBUS_SIGNAL( TrackListChangeSignal )
{ /* emit the new tracklist lengh */
    SIGNAL_INIT("TrackListChange");
    OUT_ARGUMENTS;

    playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_data );
    dbus_int32_t i_elements = p_playlist->items.i_size / 2;
    pl_Release( p_playlist );

    ADD_INT32( &i_elements );
    SIGNAL_SEND;
}

/*****************************************************************************
 * TrackListChangeEmit: Emits the TrackListChange signal
 *****************************************************************************/
829
/* FIXME: It is not called on tracklist reordering */
Mirsal Ennaime's avatar
Mirsal Ennaime committed
830 831 832
static int TrackListChangeEmit( vlc_object_t *p_this, const char *psz_var,
            vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
833
    VLC_UNUSED(oldval);
Mirsal Ennaime's avatar
Mirsal Ennaime committed
834 835
    intf_thread_t *p_intf = p_data;

836 837 838 839 840 841 842 843 844 845 846 847 848 849
    if( !strcmp( psz_var, "item-append" ) || !strcmp( psz_var, "item-remove" ) )
    {
        /* don't signal when items are added/removed in p_category */
        playlist_t *p_playlist = (playlist_t*)p_this;
        playlist_add_t *p_add = newval.p_address;
        playlist_item_t *p_item;
        p_item = playlist_ItemGetById( p_playlist, p_add->i_node, VLC_TRUE );
        assert( p_item );
        while( p_item->p_parent )
            p_item = p_item->p_parent;
        if( p_item == p_playlist->p_root_category )
            return VLC_SUCCESS;
    }

Mirsal Ennaime's avatar
Mirsal Ennaime committed
850 851 852
    if( p_intf->b_dead )
        return VLC_SUCCESS;

853
    UpdateCaps( p_intf, VLC_TRUE );
Mirsal Ennaime's avatar
Mirsal Ennaime committed
854 855 856
    TrackListChangeSignal( p_intf->p_sys->p_conn, p_data );
    return VLC_SUCCESS;
}
857
/*****************************************************************************
858
 * TrackChange: Playlist item change callback
859 860
 *****************************************************************************/

861
DBUS_SIGNAL( TrackChangeSignal )
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
862
{ /* emit the metadata of the new item */
863
    SIGNAL_INIT( "TrackChange" );
864 865
    OUT_ARGUMENTS;

866 867
    input_item_t *p_item = (input_item_t*) p_data;
    GetInputMeta ( p_item, &args );
868 869 870 871

    SIGNAL_SEND;
}

872 873 874 875 876 877 878 879 880
/*****************************************************************************
 * StatusChange: Player status change signal
 *****************************************************************************/

DBUS_SIGNAL( StatusChangeSignal )
{ /* send the updated status info on the bus */
    SIGNAL_INIT( "StatusChange" );
    OUT_ARGUMENTS;

881 882 883
    /* we're called from a callback of input_thread_t, so it can not be
     * destroyed before we return */
    MarshalStatus( (intf_thread_t*) p_data, &args, VLC_FALSE );
884 885 886 887

    SIGNAL_SEND;
}

888 889 890 891 892 893
/*****************************************************************************
 * StateChange: callback on input "state"
 *****************************************************************************/
static int StateChange( vlc_object_t *p_this, const char* psz_var,
            vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
894
    VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
895 896 897
    intf_thread_t       *p_intf     = ( intf_thread_t* ) p_data;
    intf_sys_t          *p_sys      = p_intf->p_sys;

898
    if( p_intf->b_dead )
899 900
        return VLC_SUCCESS;

901
    UpdateCaps( p_intf, VLC_FALSE );
902

903 904 905 906 907 908 909 910 911 912
    if( !p_sys->b_meta_read && newval.i_int == PLAYING_S )
    {
        input_item_t *p_item = input_GetItem( (input_thread_t*)p_this );
        if( p_item )
        {
            p_sys->b_meta_read = VLC_TRUE;
            TrackChangeSignal( p_sys->p_conn, p_item );
        }
    }

913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
    if( newval.i_int == PLAYING_S || newval.i_int == PAUSE_S ||
        newval.i_int == END_S )
    {
        StatusChangeSignal( p_sys->p_conn, (void*) p_intf );
    }

    return VLC_SUCCESS;
}

/*****************************************************************************
 * StatusChangeEmit: Emits the StatusChange signal
 *****************************************************************************/
static int StatusChangeEmit( vlc_object_t *p_this, const char *psz_var,
            vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
928 929
    VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
    VLC_UNUSED(oldval); VLC_UNUSED(newval);
930 931 932
    intf_thread_t *p_intf = p_data;

    if( p_intf->b_dead )
933 934
        return VLC_SUCCESS;

935
    UpdateCaps( p_intf, VLC_FALSE );
936
    StatusChangeSignal( p_intf->p_sys->p_conn, p_data );
937 938 939 940 941 942
    return VLC_SUCCESS;
}

/*****************************************************************************
 * TrackChange: callback on playlist "playlist-current"
 *****************************************************************************/
943
static int TrackChange( vlc_object_t *p_this, const char *psz_var,
944 945 946 947 948 949
            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;
950 951 952 953
    input_item_t        *p_item     = NULL;
    VLC_UNUSED( p_this ); VLC_UNUSED( psz_var );
    VLC_UNUSED( oldval ); VLC_UNUSED( newval );

954
    if( p_intf->b_dead )
955 956
        return VLC_SUCCESS;

957
    p_sys->b_meta_read = VLC_FALSE;
958 959

    p_playlist = pl_Yield( p_intf );
960
    PL_LOCK;
961 962 963 964 965 966 967 968 969 970
    p_input = p_playlist->p_input;

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

    vlc_object_yield( p_input );
971
    PL_UNLOCK;
972 973
    pl_Release( p_playlist );

974 975 976 977 978 979 980 981 982 983 984 985
    p_item = input_GetItem( p_input );
    if( !p_item )
    {
        vlc_object_release( p_input );
        return VLC_EGENERIC;
    }

    if( input_item_IsPreparsed( p_item ) )
    {
        p_sys->b_meta_read = VLC_TRUE;
        TrackChangeSignal( p_sys->p_conn, p_item );
    }
986

987 988 989 990 991
    var_AddCallback( p_input, "state", StateChange, p_intf );

    vlc_object_release( p_input );
    return VLC_SUCCESS;
}
992

993 994 995
/*****************************************************************************
 * UpdateCaps: update p_sys->i_caps
 ****************************************************************************/
996
static int UpdateCaps( intf_thread_t* p_intf, vlc_bool_t b_playlist_locked )
997
{
998
    intf_sys_t* p_sys = p_intf->p_sys;
999
    dbus_int32_t i_caps = CAPS_CAN_HAS_TRACKLIST;
1000
    playlist_t* p_playlist = pl_Yield( (vlc_object_t*)p_intf );
1001
    if( !b_playlist_locked ) PL_LOCK;
1002
    
1003 1004 1005 1006 1007
    if( p_playlist->items.i_size > 0 )
        i_caps |= CAPS_CAN_PLAY | CAPS_CAN_GO_PREV | CAPS_CAN_GO_NEXT;

    if( p_playlist->p_input )
    {
1008 1009
        /* XXX: if UpdateCaps() is called too early, these are
         * unconditionnaly true */
Rafaël Carré's avatar
Typo  
Rafaël Carré committed
1010
        if( var_GetBool( p_playlist->p_input, "can-pause" ) )
Rafaël Carré's avatar
Rafaël Carré committed
1011
            i_caps |= CAPS_CAN_PAUSE;
1012 1013
        if( var_GetBool( p_playlist->p_input, "seekable" ) )
            i_caps |= CAPS_CAN_SEEK;
1014 1015
    }

1016
    if( !b_playlist_locked ) PL_UNLOCK;
1017
    pl_Release( p_playlist );
1018

1019
    if( p_sys->b_meta_read )
1020
        i_caps |= CAPS_CAN_PROVIDE_METADATA;
1021

1022 1023
    if( i_caps != p_intf->p_sys->i_caps )
    {
1024
        p_sys->i_caps = i_caps;
1025 1026
        CapsChangeSignal( p_intf->p_sys->p_conn, (vlc_object_t*)p_intf );
    }
1027 1028 1029 1030

    return VLC_SUCCESS;
}

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
/*****************************************************************************
 * 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] ); \
1041
        dbus_message_iter_open_container( &dict_entry, DBUS_TYPE_VARIANT, \
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
1042 1043 1044 1045 1046
                type##_AS_STRING, &variant ); \
        dbus_message_iter_append_basic( &variant, \
                type, \
                & data ); \
        dbus_message_iter_close_container( &dict_entry, &variant ); \
1047
        dbus_message_iter_close_container( &dict, &dict_entry ); }
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
1048 1049

#define ADD_VLC_META_STRING( entry, item ) \
1050
    { \
1051
        char * psz = input_item_Get##item( p_input );\
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
1052
        ADD_META( entry, DBUS_TYPE_STRING, \
1053
                  psz ); \
1054
        free( psz ); \
1055
    }
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
1056

1057
static int GetInputMeta( input_item_t* p_input,
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
1058
                        DBusMessageIter *args )
1059
{
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
1060
    DBusMessageIter dict, dict_entry, variant;
1061 1062
    /* We need the track length to be expressed in milli-seconds
     * instead of µ-seconds */
1063
    dbus_int64_t i_length = ( input_item_GetDuration( p_input ) / 1000 );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
1064

1065
    const char* ppsz_meta_items[] =
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
1066 1067 1068 1069 1070 1071 1072
    {
    "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"
    };
1073

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

1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
    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 );
1092
    ADD_VLC_META_STRING( 16, TrackID );
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
1093