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( VolumeGet )
114 { /* get volume in percentage */
118 /* 2nd argument of aout_VolumeGet is uint16 */
119 aout_VolumeGet( (vlc_object_t*) p_this, &i_vol );
120 i_vol = ( 100 * i_vol ) / AOUT_VOLUME_MAX;
121 ADD_UINT16( &i_vol );
125 DBUS_METHOD( VolumeSet )
126 { /* set volume in percentage */
130 dbus_error_init( &error );
134 dbus_message_get_args( p_from, &error,
135 DBUS_TYPE_UINT16, &i_vol,
138 if( dbus_error_is_set( &error ) )
140 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
142 dbus_error_free( &error );
143 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
146 aout_VolumeSet( (vlc_object_t*) p_this, ( AOUT_VOLUME_MAX / 100 ) * i_vol );
152 { /* next playlist item */
154 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
155 playlist_Next( p_playlist );
156 pl_Release( ((vlc_object_t*) p_this) );
161 { /* previous playlist item */
163 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
164 playlist_Prev( p_playlist );
165 pl_Release( ((vlc_object_t*) p_this) );
172 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
173 playlist_Stop( p_playlist );
174 pl_Release( ((vlc_object_t*) p_this) );
178 DBUS_METHOD( GetPlayingItem )
179 { /* return the current item */
182 char psz_no_input = '\0';
183 char *p_psz_no_input = &psz_no_input;
184 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
185 input_thread_t *p_input = p_playlist->p_input;
186 ADD_STRING( ( p_input ) ? &p_input->input.p_item->psz_name :
188 pl_Release( ((vlc_object_t*) p_this) );
192 DBUS_METHOD( GetPlayStatus )
193 { /* return a string */
199 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
200 input_thread_t *p_input = p_playlist->p_input;
203 psz_play = strdup( "stopped" );
206 var_Get( p_input, "state", &val );
207 if( val.i_int == PAUSE_S )
208 psz_play = strdup( "pause" );
209 else if( val.i_int == PLAYING_S )
210 psz_play = strdup( "playing" );
211 else psz_play = strdup( "unknown" );
214 pl_Release( p_playlist );
216 ADD_STRING( &psz_play );
221 DBUS_METHOD( TogglePause )
222 { /* return a bool: true if playing */
227 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
228 input_thread_t *p_input = p_playlist->p_input;
229 if( p_input != NULL )
231 var_Get( p_input, "state", &val );
232 if( val.i_int != PAUSE_S )
235 playlist_Pause( p_playlist );
239 val.i_int = PLAYING_S;
240 playlist_Play( p_playlist );
245 val.i_int = PLAYING_S;
246 playlist_Play( p_playlist );
248 pl_Release( p_playlist );
250 dbus_bool_t pause = ( val.i_int == PLAYING_S ) ? TRUE : FALSE;
255 DBUS_SIGNAL( NewInstance )
256 { /* emits a signal with vlc pid */
257 SIGNAL_INIT( "NewInstance" );
259 dbus_uint32_t i_pid = (dbus_uint32_t) getpid();
260 ADD_UINT32( &i_pid );
264 DBUS_METHOD( AddMRL )
265 { /* add the string to the playlist, and play it if the boolean is true */
269 dbus_error_init( &error );
274 dbus_message_get_args( p_from, &error,
275 DBUS_TYPE_STRING, &psz_mrl,
276 DBUS_TYPE_BOOLEAN, &b_play,
279 if( dbus_error_is_set( &error ) )
281 msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
283 dbus_error_free( &error );
284 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
287 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
288 playlist_Add( p_playlist, psz_mrl, NULL, PLAYLIST_APPEND |
289 ( ( b_play == TRUE ) ? PLAYLIST_GO : 0 ) , PLAYLIST_END, VLC_TRUE );
290 pl_Release( p_playlist );
295 /*****************************************************************************
296 * Introspection method
297 *****************************************************************************/
299 DBUS_METHOD( handle_introspect )
300 { /* handles introspection of /org/videolan/vlc */
303 ADD_STRING( &psz_introspection_xml_data );
307 /*****************************************************************************
308 * handle_messages: answer to incoming messages
309 *****************************************************************************/
311 #define METHOD_FUNC( method, function ) \
312 else if( dbus_message_is_method_call( p_from, VLC_DBUS_INTERFACE, method ) )\
313 return function( p_conn, p_from, p_this )
315 DBUS_METHOD( handle_messages )
316 { /* the main handler, that call methods */
318 if( dbus_message_is_method_call( p_from,
319 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
320 return handle_introspect( p_conn, p_from, p_this );
322 /* here D-Bus method's names are associated to an handler */
324 METHOD_FUNC( "GetPlayStatus", GetPlayStatus );
325 METHOD_FUNC( "GetPlayingItem", GetPlayingItem );
326 METHOD_FUNC( "AddMRL", AddMRL );
327 METHOD_FUNC( "TogglePause", TogglePause );
328 METHOD_FUNC( "Nothing", Nothing );
329 METHOD_FUNC( "Prev", Prev );
330 METHOD_FUNC( "Next", Next );
331 METHOD_FUNC( "Quit", Quit );
332 METHOD_FUNC( "Stop", Stop );
333 METHOD_FUNC( "VolumeSet", VolumeSet );
334 METHOD_FUNC( "VolumeGet", VolumeGet );
336 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
339 /*****************************************************************************
340 * Open: initialize interface
341 *****************************************************************************/
343 static int Open( vlc_object_t *p_this )
344 { /* initialisation of the connection */
345 intf_thread_t *p_intf = (intf_thread_t*)p_this;
346 intf_sys_t *p_sys = malloc( sizeof( intf_sys_t ) );
347 playlist_t *p_playlist;
348 DBusConnection *p_conn;
354 dbus_threads_init_default();
356 dbus_error_init( &error );
358 /* connect to the session bus */
359 p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
362 msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
364 dbus_error_free( &error );
369 /* we unregister the object /, registered by libvlc */
370 dbus_connection_unregister_object_path( p_conn, "/" );
372 /* we register the object /org/videolan/vlc */
373 dbus_connection_register_object_path( p_conn, VLC_DBUS_OBJECT_PATH,
374 &vlc_dbus_vtable, p_this );
376 dbus_connection_flush( p_conn );
378 p_playlist = pl_Yield( p_intf );
380 var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
382 pl_Release( p_playlist );
384 p_intf->pf_run = Run;
385 p_intf->p_sys = p_sys;
386 p_sys->p_conn = p_conn;
391 /*****************************************************************************
392 * Close: destroy interface
393 *****************************************************************************/
395 static void Close ( vlc_object_t *p_this )
397 intf_thread_t *p_intf = (intf_thread_t*) p_this;
398 playlist_t *p_playlist = pl_Yield( p_intf );;
401 var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
403 pl_Release( p_playlist );
405 free( p_intf->p_sys );
408 /*****************************************************************************
410 *****************************************************************************/
412 static void Run ( intf_thread_t *p_intf )
414 NewInstance( p_intf->p_sys->p_conn, NULL );
416 while( !p_intf->b_die )
418 msleep( INTF_IDLE_SLEEP );
419 dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
423 /*****************************************************************************
424 * ItemChange: Playlist item change callback
425 *****************************************************************************/
427 DBUS_SIGNAL( ItemChangeSignal )
428 { /* emit the name of the new item */
429 SIGNAL_INIT( "ItemChange" );
432 input_thread_t *p_input = (input_thread_t*) p_data;
433 ADD_STRING( &p_input->input.p_item->psz_name );
438 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
439 vlc_value_t oldval, vlc_value_t newval, void *p_data )
441 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
442 intf_sys_t *p_sys = p_intf->p_sys;
443 playlist_t *p_playlist;
444 input_thread_t *p_input = NULL;
445 (void)p_this; (void)psz_var; (void)oldval; (void)newval;
447 p_playlist = pl_Yield( p_intf );
449 p_input = p_playlist->p_input;
454 pl_Release( p_playlist );
458 vlc_object_yield( p_input );
460 pl_Release( p_playlist );
462 ItemChangeSignal( p_sys->p_conn, p_input );
464 vlc_object_release( p_input );