dbus.c 14.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*****************************************************************************
 * dbus.c : D-Bus control interface
 *****************************************************************************
 * Copyright (C) 2006 Rafaël Carré
 * $Id$
 *
 * Author:    Rafaël Carré <funman at videolanorg>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

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

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

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

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

#include "dbus.h"

#include <vlc/vlc.h>
zorglub's avatar
zorglub committed
55 56
#include <vlc_aout.h>
#include <vlc_interface.h>
57 58
#include <vlc_meta.h>
#include <vlc_input.h>
dionoea's avatar
dionoea committed
59
#include <vlc_playlist.h>
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

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

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


static int ItemChange( vlc_object_t *p_this, const char *psz_var,
                    vlc_value_t oldval, vlc_value_t newval, void *p_data );

struct intf_sys_t
{
    DBusConnection *p_conn;
};

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

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

/*****************************************************************************
 * Methods
 *****************************************************************************/

DBUS_METHOD( Nothing )
{ /* do nothing */
    REPLY_INIT;
    REPLY_SEND;
}

Rafaël Carré's avatar
Rafaël Carré committed
101 102 103 104 105 106
DBUS_METHOD( Quit )
{ /* exits vlc */
    REPLY_INIT;
    playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
    playlist_Stop( p_playlist );
    pl_Release( ((vlc_object_t*) p_this) );
Rafaël Carré's avatar
Rafaël Carré committed
107
    ((vlc_object_t*)p_this)->p_libvlc->b_die = VLC_TRUE;
Rafaël Carré's avatar
Rafaël Carré committed
108 109 110
    REPLY_SEND;
}

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
DBUS_METHOD( PositionGet )
{ /* returns position as an int in the range [0;1000] */
    REPLY_INIT;
    OUT_ARGUMENTS;
    vlc_value_t position;
    dbus_uint16_t i_pos;

    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 ;
    }
    ADD_UINT16( &i_pos );
    pl_Release( ((vlc_object_t*) p_this) );
    REPLY_SEND;
}

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

    REPLY_INIT;
    vlc_value_t position;
    dbus_uint16_t i_pos;

    DBusError error;
    dbus_error_init( &error );

    dbus_message_get_args( p_from, &error,
            DBUS_TYPE_UINT16, &i_pos,
            DBUS_TYPE_INVALID );

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

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

Rafaël Carré's avatar
Rafaël Carré committed
166
DBUS_METHOD( VolumeGet )
167
{ /* returns volume in percentage */
Rafaël Carré's avatar
Rafaël Carré committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    REPLY_INIT;
    OUT_ARGUMENTS;
    dbus_uint16_t i_vol;
    /* 2nd argument of aout_VolumeGet is uint16 */
    aout_VolumeGet( (vlc_object_t*) p_this, &i_vol );
    i_vol = ( 100 * i_vol ) / AOUT_VOLUME_MAX;
    ADD_UINT16( &i_vol );
    REPLY_SEND;
}

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

    DBusError error;
    dbus_error_init( &error );

    dbus_uint16_t i_vol;

    dbus_message_get_args( p_from, &error,
            DBUS_TYPE_UINT16, &i_vol,
            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;
    }

    aout_VolumeSet( (vlc_object_t*) p_this, ( AOUT_VOLUME_MAX / 100 ) * i_vol );

    REPLY_SEND;
}

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
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
230 231 232 233 234 235 236 237 238

DBUS_METHOD( GetPlayingItem )
{ /* return the current item */
    REPLY_INIT;
    OUT_ARGUMENTS;
    char psz_no_input = '\0';
    char *p_psz_no_input = &psz_no_input;
    playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
    input_thread_t *p_input = p_playlist->p_input;
dionoea's avatar
dionoea committed
239
    ADD_STRING( ( p_input ) ? &input_GetItem(p_input)->psz_name :
Rafaël Carré's avatar
Rafaël Carré committed
240
            &p_psz_no_input );
Rafaël Carré's avatar
Rafaël Carré committed
241 242 243 244
    pl_Release( ((vlc_object_t*) p_this) );
    REPLY_SEND;
}

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
DBUS_METHOD( GetPlayStatus )
{ /* return a string */
    REPLY_INIT;
    OUT_ARGUMENTS;

    char *psz_play;
    vlc_value_t val;
    playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
    input_thread_t *p_input = p_playlist->p_input;

    if( !p_input )
        psz_play = strdup( "stopped" );
    else
    {
        var_Get( p_input, "state", &val );
        if( val.i_int == PAUSE_S )
            psz_play = strdup( "pause" );
        else if( val.i_int == PLAYING_S )
            psz_play = strdup( "playing" );
        else psz_play = strdup( "unknown" );
    }
dionoea's avatar
dionoea committed
266

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
    pl_Release( p_playlist );

    ADD_STRING( &psz_play );
    free( psz_play );
    REPLY_SEND;
}

DBUS_METHOD( TogglePause )
{ /* return a bool: true if playing */
    REPLY_INIT;
    OUT_ARGUMENTS;

    vlc_value_t val;
    playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
    input_thread_t *p_input = p_playlist->p_input;
Rafaël Carré's avatar
Rafaël Carré committed
282
    if( p_input != NULL )
283 284 285 286 287
    {
        var_Get( p_input, "state", &val );
        if( val.i_int != PAUSE_S )
        {
            val.i_int = PAUSE_S;
Rafaël Carré's avatar
Rafaël Carré committed
288
            playlist_Pause( p_playlist );
289 290 291 292
        }
        else
        {
            val.i_int = PLAYING_S;
Rafaël Carré's avatar
Rafaël Carré committed
293
            playlist_Play( p_playlist );
294 295
        }
    }
Rafaël Carré's avatar
Rafaël Carré committed
296 297 298 299 300
    else
    {
        val.i_int = PLAYING_S;
        playlist_Play( p_playlist );
    }
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
    pl_Release( p_playlist );

    dbus_bool_t pause = ( val.i_int == PLAYING_S ) ? TRUE : FALSE;
    ADD_BOOL( &pause );
    REPLY_SEND;
}

DBUS_METHOD( AddMRL )
{ /* add the string to the playlist, and play it if the boolean is true */
    REPLY_INIT;

    DBusError error;
    dbus_error_init( &error );

    char *psz_mrl;
    dbus_bool_t b_play;

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

    if( dbus_error_is_set( &error ) )
    {
Rafaël Carré's avatar
Rafaël Carré committed
325 326
        msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
                error.message );
327 328 329 330 331
        dbus_error_free( &error );
        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
    }

    playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
Rafaël Carré's avatar
Rafaël Carré committed
332 333
    playlist_Add( p_playlist, psz_mrl, NULL, PLAYLIST_APPEND |
            ( ( b_play == TRUE ) ? PLAYLIST_GO : 0 ) , PLAYLIST_END, VLC_TRUE );
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
    pl_Release( p_playlist );

    REPLY_SEND;
}

/*****************************************************************************
 * Introspection method
 *****************************************************************************/

DBUS_METHOD( handle_introspect )
{ /* handles introspection of /org/videolan/vlc */
    REPLY_INIT;
    OUT_ARGUMENTS;
    ADD_STRING( &psz_introspection_xml_data );
    REPLY_SEND;
}

/*****************************************************************************
 * handle_messages: answer to incoming messages
 *****************************************************************************/

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

DBUS_METHOD( handle_messages )
{ /* the main handler, that call methods */

    if( dbus_message_is_method_call( p_from,
                DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
        return handle_introspect( p_conn, p_from, p_this );

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

    METHOD_FUNC( "GetPlayStatus",   GetPlayStatus );
Rafaël Carré's avatar
Rafaël Carré committed
369
    METHOD_FUNC( "GetPlayingItem",  GetPlayingItem );
370 371 372
    METHOD_FUNC( "AddMRL",          AddMRL );
    METHOD_FUNC( "TogglePause",     TogglePause );
    METHOD_FUNC( "Nothing",         Nothing );
373 374
    METHOD_FUNC( "Prev",            Prev );
    METHOD_FUNC( "Next",            Next );
Rafaël Carré's avatar
Rafaël Carré committed
375
    METHOD_FUNC( "Quit",            Quit );
376
    METHOD_FUNC( "Stop",            Stop );
Rafaël Carré's avatar
Rafaël Carré committed
377 378
    METHOD_FUNC( "VolumeSet",       VolumeSet );
    METHOD_FUNC( "VolumeGet",       VolumeGet );
379 380
    METHOD_FUNC( "PositionSet",     PositionSet );
    METHOD_FUNC( "PositionGet",     PositionGet );
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400

    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();
dionoea's avatar
dionoea committed
401

402
    dbus_error_init( &error );
dionoea's avatar
dionoea committed
403

404 405 406 407 408 409 410 411 412 413 414
    /* 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;
    }

415 416
    /* we unregister the object /, registered by libvlc */
    dbus_connection_unregister_object_path( p_conn, "/" );
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450

    /* we register the object /org/videolan/vlc */
    dbus_connection_register_object_path( p_conn, VLC_DBUS_OBJECT_PATH,
            &vlc_dbus_vtable, p_this );

    dbus_connection_flush( p_conn );

    p_playlist = pl_Yield( p_intf );
    PL_LOCK;
    var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
    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;
    var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
    PL_UNLOCK;
    pl_Release( p_playlist );

451 452
    dbus_connection_unref( p_intf->p_sys->p_conn );

453 454 455 456
    free( p_intf->p_sys );
}

/*****************************************************************************
dionoea's avatar
dionoea committed
457
 * Run: main loop
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
 *****************************************************************************/

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

/*****************************************************************************
 * ItemChange: Playlist item change callback
 *****************************************************************************/

DBUS_SIGNAL( ItemChangeSignal )
{ /* emit the name of the new item */
    SIGNAL_INIT( "ItemChange" );
    OUT_ARGUMENTS;

    input_thread_t *p_input = (input_thread_t*) p_data;
dionoea's avatar
dionoea committed
479
    ADD_STRING( &input_GetItem(p_input)->psz_name );
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513

    SIGNAL_SEND;
}

static int ItemChange( vlc_object_t *p_this, const char *psz_var,
            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 );

    ItemChangeSignal( p_sys->p_conn, p_input );

    vlc_object_release( p_input );
    return VLC_SUCCESS;
}