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>
60 /*****************************************************************************
62 *****************************************************************************/
64 static int Open ( vlc_object_t * );
65 static void Close ( vlc_object_t * );
66 static void Run ( intf_thread_t * );
69 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
70 vlc_value_t oldval, vlc_value_t newval, void *p_data );
74 DBusConnection *p_conn;
77 /*****************************************************************************
79 *****************************************************************************/
82 set_shortname( _("dbus"));
83 set_category( CAT_INTERFACE );
84 set_subcategory( SUBCAT_INTERFACE_CONTROL );
85 set_description( _("D-Bus control interface") );
86 set_capability( "interface", 0 );
87 set_callbacks( Open, Close );
90 /*****************************************************************************
92 *****************************************************************************/
94 DBUS_METHOD( Nothing )
103 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
104 playlist_Stop( p_playlist );
105 pl_Release( ((vlc_object_t*) p_this) );
106 ((vlc_object_t*)p_this)->p_libvlc->b_die = VLC_TRUE;
110 DBUS_METHOD( PositionGet )
111 { /* returns position as an int in the range [0;1000] */
114 vlc_value_t position;
117 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
118 input_thread_t *p_input = p_playlist->p_input;
124 var_Get( p_input, "position", &position );
125 i_pos = position.f_float * 1000 ;
127 ADD_UINT16( &i_pos );
128 pl_Release( ((vlc_object_t*) p_this) );
132 DBUS_METHOD( PositionSet )
133 { /* set position from an int in the range [0;1000] */
136 vlc_value_t position;
140 dbus_error_init( &error );
142 dbus_message_get_args( p_from, &error,
143 DBUS_TYPE_UINT16, &i_pos,
146 if( dbus_error_is_set( &error ) )
148 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
150 dbus_error_free( &error );
151 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
153 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
154 input_thread_t *p_input = p_playlist->p_input;
158 position.f_float = ((float)i_pos) / 1000;
159 var_Set( p_input, "position", position );
161 pl_Release( ((vlc_object_t*) p_this) );
165 DBUS_METHOD( VolumeGet )
166 { /* returns volume in percentage */
170 /* 2nd argument of aout_VolumeGet is uint16 */
171 aout_VolumeGet( (vlc_object_t*) p_this, &i_vol );
172 i_vol = ( 100 * i_vol ) / AOUT_VOLUME_MAX;
173 ADD_UINT16( &i_vol );
177 DBUS_METHOD( VolumeSet )
178 { /* set volume in percentage */
182 dbus_error_init( &error );
186 dbus_message_get_args( p_from, &error,
187 DBUS_TYPE_UINT16, &i_vol,
190 if( dbus_error_is_set( &error ) )
192 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
194 dbus_error_free( &error );
195 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
198 aout_VolumeSet( (vlc_object_t*) p_this, ( AOUT_VOLUME_MAX / 100 ) * i_vol );
204 { /* next playlist item */
206 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
207 playlist_Next( p_playlist );
208 pl_Release( ((vlc_object_t*) p_this) );
213 { /* previous playlist item */
215 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
216 playlist_Prev( p_playlist );
217 pl_Release( ((vlc_object_t*) p_this) );
224 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
225 playlist_Stop( p_playlist );
226 pl_Release( ((vlc_object_t*) p_this) );
230 DBUS_METHOD( GetPlayingItem )
231 { /* return the current item */
234 char psz_no_input = '\0';
235 char *p_psz_no_input = &psz_no_input;
236 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
237 input_thread_t *p_input = p_playlist->p_input;
238 ADD_STRING( ( p_input ) ? &p_input->input.p_item->psz_name :
240 pl_Release( ((vlc_object_t*) p_this) );
244 DBUS_METHOD( GetPlayStatus )
245 { /* return a string */
251 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
252 input_thread_t *p_input = p_playlist->p_input;
255 psz_play = strdup( "stopped" );
258 var_Get( p_input, "state", &val );
259 if( val.i_int == PAUSE_S )
260 psz_play = strdup( "pause" );
261 else if( val.i_int == PLAYING_S )
262 psz_play = strdup( "playing" );
263 else psz_play = strdup( "unknown" );
266 pl_Release( p_playlist );
268 ADD_STRING( &psz_play );
273 DBUS_METHOD( TogglePause )
274 { /* return a bool: true if playing */
279 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
280 input_thread_t *p_input = p_playlist->p_input;
281 if( p_input != NULL )
283 var_Get( p_input, "state", &val );
284 if( val.i_int != PAUSE_S )
287 playlist_Pause( p_playlist );
291 val.i_int = PLAYING_S;
292 playlist_Play( p_playlist );
297 val.i_int = PLAYING_S;
298 playlist_Play( p_playlist );
300 pl_Release( p_playlist );
302 dbus_bool_t pause = ( val.i_int == PLAYING_S ) ? TRUE : FALSE;
307 DBUS_METHOD( AddMRL )
308 { /* add the string to the playlist, and play it if the boolean is true */
312 dbus_error_init( &error );
317 dbus_message_get_args( p_from, &error,
318 DBUS_TYPE_STRING, &psz_mrl,
319 DBUS_TYPE_BOOLEAN, &b_play,
322 if( dbus_error_is_set( &error ) )
324 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
326 dbus_error_free( &error );
327 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
330 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
331 playlist_Add( p_playlist, psz_mrl, NULL, PLAYLIST_APPEND |
332 ( ( b_play == TRUE ) ? PLAYLIST_GO : 0 ) , PLAYLIST_END, VLC_TRUE );
333 pl_Release( p_playlist );
338 /*****************************************************************************
339 * Introspection method
340 *****************************************************************************/
342 DBUS_METHOD( handle_introspect )
343 { /* handles introspection of /org/videolan/vlc */
346 ADD_STRING( &psz_introspection_xml_data );
350 /*****************************************************************************
351 * handle_messages: answer to incoming messages
352 *****************************************************************************/
354 #define METHOD_FUNC( method, function ) \
355 else if( dbus_message_is_method_call( p_from, VLC_DBUS_INTERFACE, method ) )\
356 return function( p_conn, p_from, p_this )
358 DBUS_METHOD( handle_messages )
359 { /* the main handler, that call methods */
361 if( dbus_message_is_method_call( p_from,
362 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
363 return handle_introspect( p_conn, p_from, p_this );
365 /* here D-Bus method's names are associated to an handler */
367 METHOD_FUNC( "GetPlayStatus", GetPlayStatus );
368 METHOD_FUNC( "GetPlayingItem", GetPlayingItem );
369 METHOD_FUNC( "AddMRL", AddMRL );
370 METHOD_FUNC( "TogglePause", TogglePause );
371 METHOD_FUNC( "Nothing", Nothing );
372 METHOD_FUNC( "Prev", Prev );
373 METHOD_FUNC( "Next", Next );
374 METHOD_FUNC( "Quit", Quit );
375 METHOD_FUNC( "Stop", Stop );
376 METHOD_FUNC( "VolumeSet", VolumeSet );
377 METHOD_FUNC( "VolumeGet", VolumeGet );
378 METHOD_FUNC( "PositionSet", PositionSet );
379 METHOD_FUNC( "PositionGet", PositionGet );
381 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
384 /*****************************************************************************
385 * Open: initialize interface
386 *****************************************************************************/
388 static int Open( vlc_object_t *p_this )
389 { /* initialisation of the connection */
390 intf_thread_t *p_intf = (intf_thread_t*)p_this;
391 intf_sys_t *p_sys = malloc( sizeof( intf_sys_t ) );
392 playlist_t *p_playlist;
393 DBusConnection *p_conn;
399 dbus_threads_init_default();
401 dbus_error_init( &error );
403 /* connect to the session bus */
404 p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
407 msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
409 dbus_error_free( &error );
414 /* we unregister the object /, registered by libvlc */
415 dbus_connection_unregister_object_path( p_conn, "/" );
417 /* we register the object /org/videolan/vlc */
418 dbus_connection_register_object_path( p_conn, VLC_DBUS_OBJECT_PATH,
419 &vlc_dbus_vtable, p_this );
421 dbus_connection_flush( p_conn );
423 p_playlist = pl_Yield( p_intf );
425 var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
427 pl_Release( p_playlist );
429 p_intf->pf_run = Run;
430 p_intf->p_sys = p_sys;
431 p_sys->p_conn = p_conn;
436 /*****************************************************************************
437 * Close: destroy interface
438 *****************************************************************************/
440 static void Close ( vlc_object_t *p_this )
442 intf_thread_t *p_intf = (intf_thread_t*) p_this;
443 playlist_t *p_playlist = pl_Yield( p_intf );;
446 var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
448 pl_Release( p_playlist );
450 dbus_connection_unref( p_intf->p_sys->p_conn );
452 free( p_intf->p_sys );
455 /*****************************************************************************
457 *****************************************************************************/
459 static void Run ( intf_thread_t *p_intf )
461 while( !p_intf->b_die )
463 msleep( INTF_IDLE_SLEEP );
464 dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
468 /*****************************************************************************
469 * ItemChange: Playlist item change callback
470 *****************************************************************************/
472 DBUS_SIGNAL( ItemChangeSignal )
473 { /* emit the name of the new item */
474 SIGNAL_INIT( "ItemChange" );
477 input_thread_t *p_input = (input_thread_t*) p_data;
478 ADD_STRING( &p_input->input.p_item->psz_name );
483 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
484 vlc_value_t oldval, vlc_value_t newval, void *p_data )
486 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
487 intf_sys_t *p_sys = p_intf->p_sys;
488 playlist_t *p_playlist;
489 input_thread_t *p_input = NULL;
490 (void)p_this; (void)psz_var; (void)oldval; (void)newval;
492 p_playlist = pl_Yield( p_intf );
494 p_input = p_playlist->p_input;
499 pl_Release( p_playlist );
503 vlc_object_yield( p_input );
505 pl_Release( p_playlist );
507 ItemChangeSignal( p_sys->p_conn, p_input );
509 vlc_object_release( p_input );