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 )
104 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
105 playlist_Stop( p_playlist );
106 pl_Release( ((vlc_object_t*) p_this) );
107 ((vlc_object_t*)p_this)->p_libvlc->b_die = VLC_TRUE;
111 DBUS_METHOD( PositionGet )
112 { /* returns position as an int in the range [0;1000] */
115 vlc_value_t position;
118 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
119 input_thread_t *p_input = p_playlist->p_input;
125 var_Get( p_input, "position", &position );
126 i_pos = position.f_float * 1000 ;
128 ADD_UINT16( &i_pos );
129 pl_Release( ((vlc_object_t*) p_this) );
133 DBUS_METHOD( PositionSet )
134 { /* set position from an int in the range [0;1000] */
137 vlc_value_t position;
141 dbus_error_init( &error );
143 dbus_message_get_args( p_from, &error,
144 DBUS_TYPE_UINT16, &i_pos,
147 if( dbus_error_is_set( &error ) )
149 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
151 dbus_error_free( &error );
152 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
154 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
155 input_thread_t *p_input = p_playlist->p_input;
159 position.f_float = ((float)i_pos) / 1000;
160 var_Set( p_input, "position", position );
162 pl_Release( ((vlc_object_t*) p_this) );
166 DBUS_METHOD( VolumeGet )
167 { /* returns volume in percentage */
171 /* 2nd argument of aout_VolumeGet is uint16 */
172 aout_VolumeGet( (vlc_object_t*) p_this, &i_vol );
173 i_vol = ( 100 * i_vol ) / AOUT_VOLUME_MAX;
174 ADD_UINT16( &i_vol );
178 DBUS_METHOD( VolumeSet )
179 { /* set volume in percentage */
183 dbus_error_init( &error );
187 dbus_message_get_args( p_from, &error,
188 DBUS_TYPE_UINT16, &i_vol,
191 if( dbus_error_is_set( &error ) )
193 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
195 dbus_error_free( &error );
196 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
199 aout_VolumeSet( (vlc_object_t*) p_this, ( AOUT_VOLUME_MAX / 100 ) * i_vol );
205 { /* next playlist item */
207 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
208 playlist_Next( p_playlist );
209 pl_Release( ((vlc_object_t*) p_this) );
214 { /* previous playlist item */
216 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
217 playlist_Prev( p_playlist );
218 pl_Release( ((vlc_object_t*) p_this) );
225 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
226 playlist_Stop( p_playlist );
227 pl_Release( ((vlc_object_t*) p_this) );
231 DBUS_METHOD( GetPlayingItem )
232 { /* return the current item */
235 char psz_no_input = '\0';
236 char *p_psz_no_input = &psz_no_input;
237 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
238 input_thread_t *p_input = p_playlist->p_input;
239 ADD_STRING( ( p_input ) ? &input_GetItem(p_input)->psz_name :
241 pl_Release( ((vlc_object_t*) p_this) );
245 DBUS_METHOD( GetPlayStatus )
246 { /* return a string */
252 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
253 input_thread_t *p_input = p_playlist->p_input;
256 psz_play = strdup( "stopped" );
259 var_Get( p_input, "state", &val );
260 if( val.i_int == PAUSE_S )
261 psz_play = strdup( "pause" );
262 else if( val.i_int == PLAYING_S )
263 psz_play = strdup( "playing" );
264 else psz_play = strdup( "unknown" );
267 pl_Release( p_playlist );
269 ADD_STRING( &psz_play );
274 DBUS_METHOD( TogglePause )
275 { /* return a bool: true if playing */
280 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
281 input_thread_t *p_input = p_playlist->p_input;
282 if( p_input != NULL )
284 var_Get( p_input, "state", &val );
285 if( val.i_int != PAUSE_S )
288 playlist_Pause( p_playlist );
292 val.i_int = PLAYING_S;
293 playlist_Play( p_playlist );
298 val.i_int = PLAYING_S;
299 playlist_Play( p_playlist );
301 pl_Release( p_playlist );
303 dbus_bool_t pause = ( val.i_int == PLAYING_S ) ? TRUE : FALSE;
308 DBUS_METHOD( AddMRL )
309 { /* add the string to the playlist, and play it if the boolean is true */
313 dbus_error_init( &error );
318 dbus_message_get_args( p_from, &error,
319 DBUS_TYPE_STRING, &psz_mrl,
320 DBUS_TYPE_BOOLEAN, &b_play,
323 if( dbus_error_is_set( &error ) )
325 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
327 dbus_error_free( &error );
328 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
331 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
332 playlist_Add( p_playlist, psz_mrl, NULL, PLAYLIST_APPEND |
333 ( ( b_play == TRUE ) ? PLAYLIST_GO : 0 ) , PLAYLIST_END, VLC_TRUE );
334 pl_Release( p_playlist );
339 /*****************************************************************************
340 * Introspection method
341 *****************************************************************************/
343 DBUS_METHOD( handle_introspect )
344 { /* handles introspection of /org/videolan/vlc */
347 ADD_STRING( &psz_introspection_xml_data );
351 /*****************************************************************************
352 * handle_messages: answer to incoming messages
353 *****************************************************************************/
355 #define METHOD_FUNC( method, function ) \
356 else if( dbus_message_is_method_call( p_from, VLC_DBUS_INTERFACE, method ) )\
357 return function( p_conn, p_from, p_this )
359 DBUS_METHOD( handle_messages )
360 { /* the main handler, that call methods */
362 if( dbus_message_is_method_call( p_from,
363 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
364 return handle_introspect( p_conn, p_from, p_this );
366 /* here D-Bus method's names are associated to an handler */
368 METHOD_FUNC( "GetPlayStatus", GetPlayStatus );
369 METHOD_FUNC( "GetPlayingItem", GetPlayingItem );
370 METHOD_FUNC( "AddMRL", AddMRL );
371 METHOD_FUNC( "TogglePause", TogglePause );
372 METHOD_FUNC( "Nothing", Nothing );
373 METHOD_FUNC( "Prev", Prev );
374 METHOD_FUNC( "Next", Next );
375 METHOD_FUNC( "Quit", Quit );
376 METHOD_FUNC( "Stop", Stop );
377 METHOD_FUNC( "VolumeSet", VolumeSet );
378 METHOD_FUNC( "VolumeGet", VolumeGet );
379 METHOD_FUNC( "PositionSet", PositionSet );
380 METHOD_FUNC( "PositionGet", PositionGet );
382 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
385 /*****************************************************************************
386 * Open: initialize interface
387 *****************************************************************************/
389 static int Open( vlc_object_t *p_this )
390 { /* initialisation of the connection */
391 intf_thread_t *p_intf = (intf_thread_t*)p_this;
392 intf_sys_t *p_sys = malloc( sizeof( intf_sys_t ) );
393 playlist_t *p_playlist;
394 DBusConnection *p_conn;
400 dbus_threads_init_default();
402 dbus_error_init( &error );
404 /* connect to the session bus */
405 p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
408 msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
410 dbus_error_free( &error );
415 /* we unregister the object /, registered by libvlc */
416 dbus_connection_unregister_object_path( p_conn, "/" );
418 /* we register the object /org/videolan/vlc */
419 dbus_connection_register_object_path( p_conn, VLC_DBUS_OBJECT_PATH,
420 &vlc_dbus_vtable, p_this );
422 dbus_connection_flush( p_conn );
424 p_playlist = pl_Yield( p_intf );
426 var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
428 pl_Release( p_playlist );
430 p_intf->pf_run = Run;
431 p_intf->p_sys = p_sys;
432 p_sys->p_conn = p_conn;
437 /*****************************************************************************
438 * Close: destroy interface
439 *****************************************************************************/
441 static void Close ( vlc_object_t *p_this )
443 intf_thread_t *p_intf = (intf_thread_t*) p_this;
444 playlist_t *p_playlist = pl_Yield( p_intf );;
447 var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
449 pl_Release( p_playlist );
451 dbus_connection_unref( p_intf->p_sys->p_conn );
453 free( p_intf->p_sys );
456 /*****************************************************************************
458 *****************************************************************************/
460 static void Run ( intf_thread_t *p_intf )
462 while( !p_intf->b_die )
464 msleep( INTF_IDLE_SLEEP );
465 dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
469 /*****************************************************************************
470 * ItemChange: Playlist item change callback
471 *****************************************************************************/
473 DBUS_SIGNAL( ItemChangeSignal )
474 { /* emit the name of the new item */
475 SIGNAL_INIT( "ItemChange" );
478 input_thread_t *p_input = (input_thread_t*) p_data;
479 ADD_STRING( &input_GetItem(p_input)->psz_name );
484 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
485 vlc_value_t oldval, vlc_value_t newval, void *p_data )
487 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
488 intf_sys_t *p_sys = p_intf->p_sys;
489 playlist_t *p_playlist;
490 input_thread_t *p_input = NULL;
491 (void)p_this; (void)psz_var; (void)oldval; (void)newval;
493 p_playlist = pl_Yield( p_intf );
495 p_input = p_playlist->p_input;
500 pl_Release( p_playlist );
504 vlc_object_yield( p_input );
506 pl_Release( p_playlist );
508 ItemChangeSignal( p_sys->p_conn, p_input );
510 vlc_object_release( p_input );