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() */
60 #include <vlc_input.h>
62 /*****************************************************************************
64 *****************************************************************************/
66 static int Open ( vlc_object_t * );
67 static void Close ( vlc_object_t * );
68 static void Run ( intf_thread_t * );
71 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
72 vlc_value_t oldval, vlc_value_t newval, void *p_data );
76 DBusConnection *p_conn;
79 /*****************************************************************************
81 *****************************************************************************/
84 set_shortname( _("dbus"));
85 set_category( CAT_INTERFACE );
86 set_subcategory( SUBCAT_INTERFACE_CONTROL );
87 set_description( _("D-Bus control interface") );
88 set_capability( "interface", 0 );
89 set_callbacks( Open, Close );
92 /*****************************************************************************
94 *****************************************************************************/
96 DBUS_METHOD( Nothing )
105 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
106 playlist_Stop( p_playlist );
107 pl_Release( ((vlc_object_t*) p_this) );
108 ((vlc_object_t*)p_this)->p_libvlc->b_die = VLC_TRUE;
113 { /* next playlist item */
115 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
116 playlist_Next( p_playlist );
117 pl_Release( ((vlc_object_t*) p_this) );
122 { /* previous playlist item */
124 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
125 playlist_Prev( p_playlist );
126 pl_Release( ((vlc_object_t*) p_this) );
133 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
134 playlist_Stop( p_playlist );
135 pl_Release( ((vlc_object_t*) p_this) );
139 DBUS_METHOD( GetPlayingItem )
140 { /* return the current item */
143 char psz_no_input = '\0';
144 char *p_psz_no_input = &psz_no_input;
145 playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
146 input_thread_t *p_input = p_playlist->p_input;
147 ADD_STRING( ( p_input ) ? &p_input->input.p_item->psz_name : &p_psz_no_input );
148 pl_Release( ((vlc_object_t*) p_this) );
152 DBUS_METHOD( GetPlayStatus )
153 { /* return a string */
159 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
161 input_thread_t *p_input = p_playlist->p_input;
164 psz_play = strdup( "stopped" );
167 var_Get( p_input, "state", &val );
168 if( val.i_int == PAUSE_S )
169 psz_play = strdup( "pause" );
170 else if( val.i_int == PLAYING_S )
171 psz_play = strdup( "playing" );
172 else psz_play = strdup( "unknown" );
176 pl_Release( p_playlist );
178 ADD_STRING( &psz_play );
183 DBUS_METHOD( TogglePause )
184 { /* return a bool: true if playing */
189 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
190 input_thread_t *p_input = p_playlist->p_input;
191 if( p_input != NULL )
193 var_Get( p_input, "state", &val );
194 if( val.i_int != PAUSE_S )
197 playlist_Pause( p_playlist );
201 val.i_int = PLAYING_S;
202 playlist_Play( p_playlist );
207 val.i_int = PLAYING_S;
208 playlist_Play( p_playlist );
210 pl_Release( p_playlist );
212 dbus_bool_t pause = ( val.i_int == PLAYING_S ) ? TRUE : FALSE;
217 DBUS_SIGNAL( NewInstance )
218 { /* emits a signal with vlc pid */
219 SIGNAL_INIT( "NewInstance" );
221 dbus_uint32_t i_pid = (dbus_uint32_t) getpid();
222 ADD_UINT32( &i_pid );
226 DBUS_METHOD( AddMRL )
227 { /* add the string to the playlist, and play it if the boolean is true */
231 dbus_error_init( &error );
233 intf_thread_t *p_intf = (intf_thread_t*) p_this;
237 dbus_message_get_args( p_from, &error,
238 DBUS_TYPE_STRING, &psz_mrl,
239 DBUS_TYPE_BOOLEAN, &b_play,
242 if( dbus_error_is_set( &error ) )
244 printf("error: %s\n", error.message );
245 dbus_error_free( &error );
246 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
249 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
250 input_item_t *p_item = input_ItemNew( p_intf, psz_mrl, NULL );
253 playlist_AddInput( p_playlist, p_item,
254 PLAYLIST_APPEND | ( ( b_play == TRUE ) ? PLAYLIST_GO : 0 ) ,
255 PLAYLIST_END, VLC_TRUE );
257 pl_Release( p_playlist );
262 /*****************************************************************************
263 * Introspection method
264 *****************************************************************************/
266 DBUS_METHOD( handle_introspect )
267 { /* handles introspection of /org/videolan/vlc */
270 ADD_STRING( &psz_introspection_xml_data );
274 /*****************************************************************************
275 * handle_messages: answer to incoming messages
276 *****************************************************************************/
278 #define METHOD_FUNC( method, function ) \
279 else if( dbus_message_is_method_call( p_from, VLC_DBUS_INTERFACE, method ) )\
280 return function( p_conn, p_from, p_this )
282 DBUS_METHOD( handle_messages )
283 { /* the main handler, that call methods */
285 if( dbus_message_is_method_call( p_from,
286 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
287 return handle_introspect( p_conn, p_from, p_this );
289 /* here D-Bus method's names are associated to an handler */
291 METHOD_FUNC( "GetPlayStatus", GetPlayStatus );
292 METHOD_FUNC( "GetPlayingItem", GetPlayingItem );
293 METHOD_FUNC( "AddMRL", AddMRL );
294 METHOD_FUNC( "TogglePause", TogglePause );
295 METHOD_FUNC( "Nothing", Nothing );
296 METHOD_FUNC( "Prev", Prev );
297 METHOD_FUNC( "Next", Next );
298 METHOD_FUNC( "Quit", Quit );
299 METHOD_FUNC( "Stop", Stop );
301 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
304 /*****************************************************************************
305 * Open: initialize interface
306 *****************************************************************************/
308 static int Open( vlc_object_t *p_this )
309 { /* initialisation of the connection */
310 intf_thread_t *p_intf = (intf_thread_t*)p_this;
311 intf_sys_t *p_sys = malloc( sizeof( intf_sys_t ) );
312 playlist_t *p_playlist;
313 DBusConnection *p_conn;
319 dbus_threads_init_default();
321 dbus_error_init( &error );
323 /* connect to the session bus */
324 p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
327 msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
329 dbus_error_free( &error );
334 /* we unregister the object /, registered by libvlc */
335 dbus_connection_unregister_object_path( p_conn, "/" );
337 /* we register the object /org/videolan/vlc */
338 dbus_connection_register_object_path( p_conn, VLC_DBUS_OBJECT_PATH,
339 &vlc_dbus_vtable, p_this );
341 dbus_connection_flush( p_conn );
343 p_playlist = pl_Yield( p_intf );
345 var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
347 pl_Release( p_playlist );
349 p_intf->pf_run = Run;
350 p_intf->p_sys = p_sys;
351 p_sys->p_conn = p_conn;
356 /*****************************************************************************
357 * Close: destroy interface
358 *****************************************************************************/
360 static void Close ( vlc_object_t *p_this )
362 intf_thread_t *p_intf = (intf_thread_t*) p_this;
363 playlist_t *p_playlist = pl_Yield( p_intf );;
366 var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
368 pl_Release( p_playlist );
370 free( p_intf->p_sys );
373 /*****************************************************************************
375 *****************************************************************************/
377 static void Run ( intf_thread_t *p_intf )
379 NewInstance( p_intf->p_sys->p_conn, NULL );
381 while( !p_intf->b_die )
383 msleep( INTF_IDLE_SLEEP );
384 dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
388 /*****************************************************************************
389 * ItemChange: Playlist item change callback
390 *****************************************************************************/
392 DBUS_SIGNAL( ItemChangeSignal )
393 { /* emit the name of the new item */
394 SIGNAL_INIT( "ItemChange" );
397 input_thread_t *p_input = (input_thread_t*) p_data;
398 ADD_STRING( &p_input->input.p_item->psz_name );
403 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
404 vlc_value_t oldval, vlc_value_t newval, void *p_data )
406 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
407 intf_sys_t *p_sys = p_intf->p_sys;
408 playlist_t *p_playlist;
409 input_thread_t *p_input = NULL;
410 (void)p_this; (void)psz_var; (void)oldval; (void)newval;
412 p_playlist = pl_Yield( p_intf );
414 p_input = p_playlist->p_input;
419 pl_Release( p_playlist );
423 vlc_object_yield( p_input );
425 pl_Release( p_playlist );
427 ItemChangeSignal( p_sys->p_conn, p_input );
429 vlc_object_release( p_input );