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>
52 #include <sys/types.h> /* getpid() */
53 #include <unistd.h> /* getpid() */
61 #include <vlc_input.h>
63 /*****************************************************************************
65 *****************************************************************************/
67 static int Open ( vlc_object_t * );
68 static void Close ( vlc_object_t * );
69 static void Run ( intf_thread_t * );
72 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
73 vlc_value_t oldval, vlc_value_t newval, void *p_data );
77 DBusConnection *p_conn;
80 /*****************************************************************************
82 *****************************************************************************/
85 set_shortname( _("dbus"));
86 set_category( CAT_INTERFACE );
87 set_subcategory( SUBCAT_INTERFACE_CONTROL );
88 set_description( _("D-Bus control interface") );
89 set_capability( "interface", 0 );
90 set_callbacks( Open, Close );
93 /*****************************************************************************
95 *****************************************************************************/
97 DBUS_METHOD( Nothing )
106 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
107 playlist_Stop( p_playlist );
108 pl_Release( ((vlc_object_t*) p_this) );
109 ((vlc_object_t*)p_this)->p_libvlc->b_die = VLC_TRUE;
113 DBUS_METHOD( PositionGet )
114 { /* returns position as an int in the range [0;1000] */
117 vlc_value_t position;
120 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
121 input_thread_t *p_input = p_playlist->p_input;
127 var_Get( p_input, "position", &position );
128 i_pos = position.f_float * 1000 ;
130 ADD_UINT16( &i_pos );
131 pl_Release( ((vlc_object_t*) p_this) );
135 DBUS_METHOD( PositionSet )
136 { /* set position from an int in the range [0;1000] */
139 vlc_value_t position;
143 dbus_error_init( &error );
145 dbus_message_get_args( p_from, &error,
146 DBUS_TYPE_UINT16, &i_pos,
149 if( dbus_error_is_set( &error ) )
151 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
153 dbus_error_free( &error );
154 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
156 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
157 input_thread_t *p_input = p_playlist->p_input;
161 position.f_float = ((float)i_pos) / 1000;
162 var_Set( p_input, "position", position );
164 pl_Release( ((vlc_object_t*) p_this) );
168 DBUS_METHOD( VolumeGet )
169 { /* returns volume in percentage */
173 /* 2nd argument of aout_VolumeGet is uint16 */
174 aout_VolumeGet( (vlc_object_t*) p_this, &i_vol );
175 i_vol = ( 100 * i_vol ) / AOUT_VOLUME_MAX;
176 ADD_UINT16( &i_vol );
180 DBUS_METHOD( VolumeSet )
181 { /* set volume in percentage */
185 dbus_error_init( &error );
189 dbus_message_get_args( p_from, &error,
190 DBUS_TYPE_UINT16, &i_vol,
193 if( dbus_error_is_set( &error ) )
195 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
197 dbus_error_free( &error );
198 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
201 aout_VolumeSet( (vlc_object_t*) p_this, ( AOUT_VOLUME_MAX / 100 ) * i_vol );
207 { /* next playlist item */
209 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
210 playlist_Next( p_playlist );
211 pl_Release( ((vlc_object_t*) p_this) );
216 { /* previous playlist item */
218 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
219 playlist_Prev( p_playlist );
220 pl_Release( ((vlc_object_t*) p_this) );
227 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
228 playlist_Stop( p_playlist );
229 pl_Release( ((vlc_object_t*) p_this) );
233 DBUS_METHOD( GetPlayingItem )
234 { /* return the current item */
237 char psz_no_input = '\0';
238 char *p_psz_no_input = &psz_no_input;
239 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
240 input_thread_t *p_input = p_playlist->p_input;
241 ADD_STRING( ( p_input ) ? &p_input->input.p_item->psz_name :
243 pl_Release( ((vlc_object_t*) p_this) );
247 DBUS_METHOD( GetPlayStatus )
248 { /* return a string */
254 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
255 input_thread_t *p_input = p_playlist->p_input;
258 psz_play = strdup( "stopped" );
261 var_Get( p_input, "state", &val );
262 if( val.i_int == PAUSE_S )
263 psz_play = strdup( "pause" );
264 else if( val.i_int == PLAYING_S )
265 psz_play = strdup( "playing" );
266 else psz_play = strdup( "unknown" );
269 pl_Release( p_playlist );
271 ADD_STRING( &psz_play );
276 DBUS_METHOD( TogglePause )
277 { /* return a bool: true if playing */
282 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
283 input_thread_t *p_input = p_playlist->p_input;
284 if( p_input != NULL )
286 var_Get( p_input, "state", &val );
287 if( val.i_int != PAUSE_S )
290 playlist_Pause( p_playlist );
294 val.i_int = PLAYING_S;
295 playlist_Play( p_playlist );
300 val.i_int = PLAYING_S;
301 playlist_Play( p_playlist );
303 pl_Release( p_playlist );
305 dbus_bool_t pause = ( val.i_int == PLAYING_S ) ? TRUE : FALSE;
310 DBUS_SIGNAL( NewInstance )
311 { /* emits a signal with vlc pid */
312 SIGNAL_INIT( "NewInstance" );
314 dbus_uint32_t i_pid = (dbus_uint32_t) getpid();
315 ADD_UINT32( &i_pid );
319 DBUS_METHOD( AddMRL )
320 { /* add the string to the playlist, and play it if the boolean is true */
324 dbus_error_init( &error );
329 dbus_message_get_args( p_from, &error,
330 DBUS_TYPE_STRING, &psz_mrl,
331 DBUS_TYPE_BOOLEAN, &b_play,
334 if( dbus_error_is_set( &error ) )
336 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
338 dbus_error_free( &error );
339 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
342 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
343 playlist_Add( p_playlist, psz_mrl, NULL, PLAYLIST_APPEND |
344 ( ( b_play == TRUE ) ? PLAYLIST_GO : 0 ) , PLAYLIST_END, VLC_TRUE );
345 pl_Release( p_playlist );
350 /*****************************************************************************
351 * Introspection method
352 *****************************************************************************/
354 DBUS_METHOD( handle_introspect )
355 { /* handles introspection of /org/videolan/vlc */
358 ADD_STRING( &psz_introspection_xml_data );
362 /*****************************************************************************
363 * handle_messages: answer to incoming messages
364 *****************************************************************************/
366 #define METHOD_FUNC( method, function ) \
367 else if( dbus_message_is_method_call( p_from, VLC_DBUS_INTERFACE, method ) )\
368 return function( p_conn, p_from, p_this )
370 DBUS_METHOD( handle_messages )
371 { /* the main handler, that call methods */
373 if( dbus_message_is_method_call( p_from,
374 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
375 return handle_introspect( p_conn, p_from, p_this );
377 /* here D-Bus method's names are associated to an handler */
379 METHOD_FUNC( "GetPlayStatus", GetPlayStatus );
380 METHOD_FUNC( "GetPlayingItem", GetPlayingItem );
381 METHOD_FUNC( "AddMRL", AddMRL );
382 METHOD_FUNC( "TogglePause", TogglePause );
383 METHOD_FUNC( "Nothing", Nothing );
384 METHOD_FUNC( "Prev", Prev );
385 METHOD_FUNC( "Next", Next );
386 METHOD_FUNC( "Quit", Quit );
387 METHOD_FUNC( "Stop", Stop );
388 METHOD_FUNC( "VolumeSet", VolumeSet );
389 METHOD_FUNC( "VolumeGet", VolumeGet );
390 METHOD_FUNC( "PositionSet", PositionSet );
391 METHOD_FUNC( "PositionGet", PositionGet );
393 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
396 /*****************************************************************************
397 * Open: initialize interface
398 *****************************************************************************/
400 static int Open( vlc_object_t *p_this )
401 { /* initialisation of the connection */
402 intf_thread_t *p_intf = (intf_thread_t*)p_this;
403 intf_sys_t *p_sys = malloc( sizeof( intf_sys_t ) );
404 playlist_t *p_playlist;
405 DBusConnection *p_conn;
411 dbus_threads_init_default();
413 dbus_error_init( &error );
415 /* connect to the session bus */
416 p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
419 msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
421 dbus_error_free( &error );
426 /* we unregister the object /, registered by libvlc */
427 dbus_connection_unregister_object_path( p_conn, "/" );
429 /* we register the object /org/videolan/vlc */
430 dbus_connection_register_object_path( p_conn, VLC_DBUS_OBJECT_PATH,
431 &vlc_dbus_vtable, p_this );
433 dbus_connection_flush( p_conn );
435 p_playlist = pl_Yield( p_intf );
437 var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
439 pl_Release( p_playlist );
441 p_intf->pf_run = Run;
442 p_intf->p_sys = p_sys;
443 p_sys->p_conn = p_conn;
448 /*****************************************************************************
449 * Close: destroy interface
450 *****************************************************************************/
452 static void Close ( vlc_object_t *p_this )
454 intf_thread_t *p_intf = (intf_thread_t*) p_this;
455 playlist_t *p_playlist = pl_Yield( p_intf );;
458 var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
460 pl_Release( p_playlist );
462 dbus_connection_unref( p_intf->p_sys->p_conn );
464 free( p_intf->p_sys );
467 /*****************************************************************************
469 *****************************************************************************/
471 static void Run ( intf_thread_t *p_intf )
473 NewInstance( p_intf->p_sys->p_conn, NULL );
475 while( !p_intf->b_die )
477 msleep( INTF_IDLE_SLEEP );
478 dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
482 /*****************************************************************************
483 * ItemChange: Playlist item change callback
484 *****************************************************************************/
486 DBUS_SIGNAL( ItemChangeSignal )
487 { /* emit the name of the new item */
488 SIGNAL_INIT( "ItemChange" );
491 input_thread_t *p_input = (input_thread_t*) p_data;
492 ADD_STRING( &p_input->input.p_item->psz_name );
497 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
498 vlc_value_t oldval, vlc_value_t newval, void *p_data )
500 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
501 intf_sys_t *p_sys = p_intf->p_sys;
502 playlist_t *p_playlist;
503 input_thread_t *p_input = NULL;
504 (void)p_this; (void)psz_var; (void)oldval; (void)newval;
506 p_playlist = pl_Yield( p_intf );
508 p_input = p_playlist->p_input;
513 pl_Release( p_playlist );
517 vlc_object_yield( p_input );
519 pl_Release( p_playlist );
521 ItemChangeSignal( p_sys->p_conn, p_input );
523 vlc_object_release( p_input );