1 /*****************************************************************************
2 * dbus.c : D-Bus control interface
3 *****************************************************************************
4 * Copyright (C) 2006 Rafaël Carré
7 * Author: Rafaël Carré <funman at videolanorg>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
25 * D-Bus Specification:
26 * http://dbus.freedesktop.org/doc/dbus-specification.html
27 * D-Bus low-level C API (libdbus)
28 * http://dbus.freedesktop.org/doc/dbus/api/html/index.html
35 * macros to read incoming arguments
37 * explore different possible types (arrays..)
39 * what must we do if org.videolan.vlc already exist on the bus ?
40 * ( there is more than one vlc instance )
43 /*****************************************************************************
45 *****************************************************************************/
47 #include <dbus/dbus.h>
56 #include <vlc_interface.h>
58 #include <vlc_input.h>
59 #include <vlc_playlist.h>
61 /*****************************************************************************
63 *****************************************************************************/
65 static int Open ( vlc_object_t * );
66 static void Close ( vlc_object_t * );
67 static void Run ( intf_thread_t * );
70 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
71 vlc_value_t oldval, vlc_value_t newval, void *p_data );
75 DBusConnection *p_conn;
78 /*****************************************************************************
80 *****************************************************************************/
83 set_shortname( _("dbus"));
84 set_category( CAT_INTERFACE );
85 set_subcategory( SUBCAT_INTERFACE_CONTROL );
86 set_description( _("D-Bus control interface") );
87 set_capability( "interface", 0 );
88 set_callbacks( Open, Close );
91 /*****************************************************************************
93 *****************************************************************************/
95 DBUS_METHOD( Nothing )
101 DBUS_METHOD( PlaylistExport_XSPF )
102 { /* export playlist to an xspf file */
104 /* reads the filename to export to */
105 /* returns the status as uint16:
114 dbus_error_init( &error );
119 dbus_message_get_args( p_from, &error,
120 DBUS_TYPE_STRING, &psz_file,
123 if( dbus_error_is_set( &error ) )
125 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
127 dbus_error_free( &error );
128 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
131 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
133 if( ( !playlist_IsEmpty( p_playlist ) ) &&
134 ( p_playlist->p_root_category->i_children > 0 ) )
136 if( playlist_Export( p_playlist, psz_file,
137 p_playlist->p_root_category->pp_children[0],
138 "export-xspf" ) == VLC_SUCCESS )
146 pl_Release( ((vlc_object_t*) p_this ) );
148 ADD_UINT16( &i_ret );
155 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
156 playlist_Stop( p_playlist );
157 pl_Release( ((vlc_object_t*) p_this) );
158 ((vlc_object_t*)p_this)->p_libvlc->b_die = VLC_TRUE;
162 DBUS_METHOD( PositionGet )
163 { /* returns position as an int in the range [0;1000] */
166 vlc_value_t position;
169 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
170 input_thread_t *p_input = p_playlist->p_input;
176 var_Get( p_input, "position", &position );
177 i_pos = position.f_float * 1000 ;
179 ADD_UINT16( &i_pos );
180 pl_Release( ((vlc_object_t*) p_this) );
184 DBUS_METHOD( PositionSet )
185 { /* set position from an int in the range [0;1000] */
188 vlc_value_t position;
192 dbus_error_init( &error );
194 dbus_message_get_args( p_from, &error,
195 DBUS_TYPE_UINT16, &i_pos,
198 if( dbus_error_is_set( &error ) )
200 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
202 dbus_error_free( &error );
203 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
205 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
206 input_thread_t *p_input = p_playlist->p_input;
210 position.f_float = ((float)i_pos) / 1000;
211 var_Set( p_input, "position", position );
213 pl_Release( ((vlc_object_t*) p_this) );
217 DBUS_METHOD( VolumeGet )
218 { /* returns volume in percentage */
222 /* 2nd argument of aout_VolumeGet is uint16 */
223 aout_VolumeGet( (vlc_object_t*) p_this, &i_vol );
224 i_vol = ( 100 * i_vol ) / AOUT_VOLUME_MAX;
225 ADD_UINT16( &i_vol );
229 DBUS_METHOD( VolumeSet )
230 { /* set volume in percentage */
234 dbus_error_init( &error );
238 dbus_message_get_args( p_from, &error,
239 DBUS_TYPE_UINT16, &i_vol,
242 if( dbus_error_is_set( &error ) )
244 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
246 dbus_error_free( &error );
247 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
250 aout_VolumeSet( (vlc_object_t*) p_this, ( AOUT_VOLUME_MAX / 100 ) * i_vol );
256 { /* next playlist item */
258 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
259 playlist_Next( p_playlist );
260 pl_Release( ((vlc_object_t*) p_this) );
265 { /* previous playlist item */
267 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
268 playlist_Prev( p_playlist );
269 pl_Release( ((vlc_object_t*) p_this) );
276 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
277 playlist_Stop( p_playlist );
278 pl_Release( ((vlc_object_t*) p_this) );
282 DBUS_METHOD( GetPlayingItem )
283 { /* return the current item */
286 char psz_no_input = '\0';
287 char *p_psz_no_input = &psz_no_input;
288 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
289 input_thread_t *p_input = p_playlist->p_input;
290 ADD_STRING( ( p_input ) ? &input_GetItem(p_input)->psz_name :
292 pl_Release( ((vlc_object_t*) p_this) );
296 DBUS_METHOD( GetPlayStatus )
297 { /* return a string */
303 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
304 input_thread_t *p_input = p_playlist->p_input;
307 psz_play = strdup( "stopped" );
310 var_Get( p_input, "state", &val );
311 if( val.i_int == PAUSE_S )
312 psz_play = strdup( "pause" );
313 else if( val.i_int == PLAYING_S )
314 psz_play = strdup( "playing" );
315 else psz_play = strdup( "unknown" );
318 pl_Release( p_playlist );
320 ADD_STRING( &psz_play );
325 DBUS_METHOD( TogglePause )
326 { /* return a bool: true if playing */
331 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
332 input_thread_t *p_input = p_playlist->p_input;
333 if( p_input != NULL )
335 var_Get( p_input, "state", &val );
336 if( val.i_int != PAUSE_S )
339 playlist_Pause( p_playlist );
343 val.i_int = PLAYING_S;
344 playlist_Play( p_playlist );
349 val.i_int = PLAYING_S;
350 playlist_Play( p_playlist );
352 pl_Release( p_playlist );
354 dbus_bool_t pause = ( val.i_int == PLAYING_S ) ? TRUE : FALSE;
359 DBUS_METHOD( AddMRL )
360 { /* add the string to the playlist, and play it if the boolean is true */
364 dbus_error_init( &error );
369 dbus_message_get_args( p_from, &error,
370 DBUS_TYPE_STRING, &psz_mrl,
371 DBUS_TYPE_BOOLEAN, &b_play,
374 if( dbus_error_is_set( &error ) )
376 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
378 dbus_error_free( &error );
379 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
382 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
383 playlist_Add( p_playlist, psz_mrl, NULL, PLAYLIST_APPEND |
384 ( ( b_play == TRUE ) ? PLAYLIST_GO : 0 ) , PLAYLIST_END, VLC_TRUE );
385 pl_Release( p_playlist );
390 /*****************************************************************************
391 * Introspection method
392 *****************************************************************************/
394 DBUS_METHOD( handle_introspect )
395 { /* handles introspection of /org/videolan/vlc */
398 ADD_STRING( &psz_introspection_xml_data );
402 /*****************************************************************************
403 * handle_messages: answer to incoming messages
404 *****************************************************************************/
406 #define METHOD_FUNC( method, function ) \
407 else if( dbus_message_is_method_call( p_from, VLC_DBUS_INTERFACE, method ) )\
408 return function( p_conn, p_from, p_this )
410 DBUS_METHOD( handle_messages )
411 { /* the main handler, that call methods */
413 if( dbus_message_is_method_call( p_from,
414 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
415 return handle_introspect( p_conn, p_from, p_this );
417 /* here D-Bus method's names are associated to an handler */
419 METHOD_FUNC( "GetPlayStatus", GetPlayStatus );
420 METHOD_FUNC( "GetPlayingItem", GetPlayingItem );
421 METHOD_FUNC( "AddMRL", AddMRL );
422 METHOD_FUNC( "TogglePause", TogglePause );
423 METHOD_FUNC( "Nothing", Nothing );
424 METHOD_FUNC( "Prev", Prev );
425 METHOD_FUNC( "Next", Next );
426 METHOD_FUNC( "Quit", Quit );
427 METHOD_FUNC( "Stop", Stop );
428 METHOD_FUNC( "VolumeSet", VolumeSet );
429 METHOD_FUNC( "VolumeGet", VolumeGet );
430 METHOD_FUNC( "PositionSet", PositionSet );
431 METHOD_FUNC( "PositionGet", PositionGet );
432 METHOD_FUNC( "PlaylistExport_XSPF", PlaylistExport_XSPF );
434 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
437 /*****************************************************************************
438 * Open: initialize interface
439 *****************************************************************************/
441 static int Open( vlc_object_t *p_this )
442 { /* initialisation of the connection */
443 intf_thread_t *p_intf = (intf_thread_t*)p_this;
444 intf_sys_t *p_sys = malloc( sizeof( intf_sys_t ) );
445 playlist_t *p_playlist;
446 DBusConnection *p_conn;
452 dbus_threads_init_default();
454 dbus_error_init( &error );
456 /* connect to the session bus */
457 p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
460 msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
462 dbus_error_free( &error );
467 /* we unregister the object /, registered by libvlc */
468 dbus_connection_unregister_object_path( p_conn, "/" );
470 /* we register the object /org/videolan/vlc */
471 dbus_connection_register_object_path( p_conn, VLC_DBUS_OBJECT_PATH,
472 &vlc_dbus_vtable, p_this );
474 dbus_connection_flush( p_conn );
476 p_playlist = pl_Yield( p_intf );
478 var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
480 pl_Release( p_playlist );
482 p_intf->pf_run = Run;
483 p_intf->p_sys = p_sys;
484 p_sys->p_conn = p_conn;
489 /*****************************************************************************
490 * Close: destroy interface
491 *****************************************************************************/
493 static void Close ( vlc_object_t *p_this )
495 intf_thread_t *p_intf = (intf_thread_t*) p_this;
496 playlist_t *p_playlist = pl_Yield( p_intf );;
499 var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
501 pl_Release( p_playlist );
503 dbus_connection_unref( p_intf->p_sys->p_conn );
505 free( p_intf->p_sys );
508 /*****************************************************************************
510 *****************************************************************************/
512 static void Run ( intf_thread_t *p_intf )
514 while( !p_intf->b_die )
516 msleep( INTF_IDLE_SLEEP );
517 dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
521 /*****************************************************************************
522 * ItemChange: Playlist item change callback
523 *****************************************************************************/
525 DBUS_SIGNAL( ItemChangeSignal )
526 { /* emit the name of the new item */
527 SIGNAL_INIT( "ItemChange" );
530 input_thread_t *p_input = (input_thread_t*) p_data;
531 ADD_STRING( &input_GetItem(p_input)->psz_name );
536 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
537 vlc_value_t oldval, vlc_value_t newval, void *p_data )
539 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
540 intf_sys_t *p_sys = p_intf->p_sys;
541 playlist_t *p_playlist;
542 input_thread_t *p_input = NULL;
543 (void)p_this; (void)psz_var; (void)oldval; (void)newval;
545 p_playlist = pl_Yield( p_intf );
547 p_input = p_playlist->p_input;
552 pl_Release( p_playlist );
556 vlc_object_yield( p_input );
558 pl_Release( p_playlist );
560 ItemChangeSignal( p_sys->p_conn, p_input );
562 vlc_object_release( p_input );