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)->b_die = VLC_TRUE;
112 DBUS_METHOD( GetPlayStatus )
113 { /* return a string */
119 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
121 input_thread_t *p_input = p_playlist->p_input;
124 psz_play = strdup( "stopped" );
127 var_Get( p_input, "state", &val );
128 if( val.i_int == PAUSE_S )
129 psz_play = strdup( "pause" );
130 else if( val.i_int == PLAYING_S )
131 psz_play = strdup( "playing" );
132 else psz_play = strdup( "unknown" );
136 pl_Release( p_playlist );
138 ADD_STRING( &psz_play );
143 DBUS_METHOD( TogglePause )
144 { /* return a bool: true if playing */
149 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
151 input_thread_t *p_input = p_playlist->p_input;
155 var_Get( p_input, "state", &val );
156 if( val.i_int != PAUSE_S )
162 val.i_int = PLAYING_S;
164 var_Set( p_input, "state", val );
167 pl_Release( p_playlist );
169 dbus_bool_t pause = ( val.i_int == PLAYING_S ) ? TRUE : FALSE;
174 DBUS_SIGNAL( NewInstance )
175 { /* emits a signal with vlc pid */
176 SIGNAL_INIT( "NewInstance" );
178 dbus_uint32_t i_pid = (dbus_uint32_t) getpid();
179 ADD_UINT32( &i_pid );
183 DBUS_METHOD( AddMRL )
184 { /* add the string to the playlist, and play it if the boolean is true */
188 dbus_error_init( &error );
190 intf_thread_t *p_intf = (intf_thread_t*) p_this;
194 dbus_message_get_args( p_from, &error,
195 DBUS_TYPE_STRING, &psz_mrl,
196 DBUS_TYPE_BOOLEAN, &b_play,
199 if( dbus_error_is_set( &error ) )
201 printf("error: %s\n", error.message );
202 dbus_error_free( &error );
203 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
206 playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
207 input_item_t *p_item = input_ItemNew( p_intf, psz_mrl, NULL );
210 playlist_AddInput( p_playlist, p_item,
211 ( b_play == TRUE ) ? PLAYLIST_GO|PLAYLIST_APPEND : PLAYLIST_APPEND,
212 PLAYLIST_END, VLC_TRUE );
214 pl_Release( p_playlist );
219 /*****************************************************************************
220 * Introspection method
221 *****************************************************************************/
223 DBUS_METHOD( handle_introspect )
224 { /* handles introspection of /org/videolan/vlc */
227 ADD_STRING( &psz_introspection_xml_data );
231 /*****************************************************************************
232 * handle_messages: answer to incoming messages
233 *****************************************************************************/
235 #define METHOD_FUNC( method, function ) \
236 else if( dbus_message_is_method_call( p_from, VLC_DBUS_INTERFACE, method ) )\
237 return function( p_conn, p_from, p_this )
239 DBUS_METHOD( handle_messages )
240 { /* the main handler, that call methods */
242 if( dbus_message_is_method_call( p_from,
243 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
244 return handle_introspect( p_conn, p_from, p_this );
246 /* here D-Bus method's names are associated to an handler */
248 METHOD_FUNC( "GetPlayStatus", GetPlayStatus );
249 METHOD_FUNC( "AddMRL", AddMRL );
250 METHOD_FUNC( "TogglePause", TogglePause );
251 METHOD_FUNC( "Nothing", Nothing );
252 METHOD_FUNC( "Quit", Quit );
254 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
257 /*****************************************************************************
258 * Open: initialize interface
259 *****************************************************************************/
261 static int Open( vlc_object_t *p_this )
262 { /* initialisation of the connection */
263 intf_thread_t *p_intf = (intf_thread_t*)p_this;
264 intf_sys_t *p_sys = malloc( sizeof( intf_sys_t ) );
265 playlist_t *p_playlist;
266 DBusConnection *p_conn;
272 dbus_threads_init_default();
274 dbus_error_init( &error );
276 /* connect to the session bus */
277 p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
280 msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
282 dbus_error_free( &error );
287 /* we request the service org.videolan.vlc */
288 dbus_bus_request_name( p_conn, VLC_DBUS_SERVICE,
289 DBUS_NAME_FLAG_REPLACE_EXISTING , &error );
290 if (dbus_error_is_set( &error ) )
292 msg_Err( p_this, "Error requesting %s service: %s\n", VLC_DBUS_SERVICE,
294 dbus_error_free( &error );
299 /* we register the object /org/videolan/vlc */
300 dbus_connection_register_object_path( p_conn, VLC_DBUS_OBJECT_PATH,
301 &vlc_dbus_vtable, p_this );
303 dbus_connection_flush( p_conn );
305 p_playlist = pl_Yield( p_intf );
307 var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
309 pl_Release( p_playlist );
311 p_intf->pf_run = Run;
312 p_intf->p_sys = p_sys;
313 p_sys->p_conn = p_conn;
318 /*****************************************************************************
319 * Close: destroy interface
320 *****************************************************************************/
322 static void Close ( vlc_object_t *p_this )
324 intf_thread_t *p_intf = (intf_thread_t*) p_this;
325 playlist_t *p_playlist = pl_Yield( p_intf );;
328 var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
330 pl_Release( p_playlist );
332 free( p_intf->p_sys );
335 /*****************************************************************************
337 *****************************************************************************/
339 static void Run ( intf_thread_t *p_intf )
341 NewInstance( p_intf->p_sys->p_conn, NULL );
343 while( !p_intf->b_die )
345 msleep( INTF_IDLE_SLEEP );
346 dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
350 /*****************************************************************************
351 * ItemChange: Playlist item change callback
352 *****************************************************************************/
354 DBUS_SIGNAL( ItemChangeSignal )
355 { /* emit the name of the new item */
356 SIGNAL_INIT( "ItemChange" );
359 input_thread_t *p_input = (input_thread_t*) p_data;
360 ADD_STRING( &p_input->input.p_item->psz_name );
365 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
366 vlc_value_t oldval, vlc_value_t newval, void *p_data )
368 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
369 intf_sys_t *p_sys = p_intf->p_sys;
370 playlist_t *p_playlist;
371 input_thread_t *p_input = NULL;
372 (void)p_this; (void)psz_var; (void)oldval; (void)newval;
374 p_playlist = pl_Yield( p_intf );
376 p_input = p_playlist->p_input;
381 pl_Release( p_playlist );
385 vlc_object_yield( p_input );
387 pl_Release( p_playlist );
389 ItemChangeSignal( p_sys->p_conn, p_input );
391 vlc_object_release( p_input );