]> git.sesse.net Git - vlc/blob - modules/control/dbus.c
Initial support of D-Bus based control interface
[vlc] / modules / control / dbus.c
1 /*****************************************************************************
2  * dbus.c : D-Bus control interface
3  *****************************************************************************
4  * Copyright (C) 2006 Rafaël Carré
5  * $Id$
6  *
7  * Author:    Rafaël Carré <funman at videolanorg>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*
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
29  */
30
31 /*
32  * TODO:
33  *  properties ?
34  *
35  *  macros to read incoming arguments
36  *
37  *  explore different possible types (arrays..)
38  *
39  *  what must we do if org.videolan.vlc already exist on the bus ?
40  *  ( there is more than one vlc instance )
41  */
42
43 /*****************************************************************************
44  * Preamble
45  *****************************************************************************/
46
47 #include <dbus/dbus.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51
52 #include <sys/types.h> /* getpid() */
53 #include <unistd.h> /* getpid() */
54
55 #include "dbus.h"
56
57 #include <vlc/vlc.h>
58 #include <vlc/intf.h>
59 #include <vlc_meta.h>
60 #include <vlc_input.h>
61
62 /*****************************************************************************
63  * Local prototypes.
64  *****************************************************************************/
65
66 static int  Open    ( vlc_object_t * );
67 static void Close   ( vlc_object_t * );
68 static void Run        ( intf_thread_t * );
69
70
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 );
73
74 struct intf_sys_t
75 {
76     DBusConnection *p_conn;
77 };
78
79 /*****************************************************************************
80  * Module descriptor
81  *****************************************************************************/
82
83 vlc_module_begin();
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 );
90 vlc_module_end();
91
92 /*****************************************************************************
93  * Methods
94  *****************************************************************************/
95
96 DBUS_METHOD( Nothing )
97 { /* do nothing */
98     REPLY_INIT;
99     REPLY_SEND;
100 }
101
102 DBUS_METHOD( GetPlayStatus )
103 { /* return a string */
104     REPLY_INIT;
105     OUT_ARGUMENTS;
106
107     char *psz_play;
108     vlc_value_t val;
109     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
110     PL_LOCK;
111     input_thread_t *p_input = p_playlist->p_input;
112
113     if( !p_input )
114         psz_play = strdup( "stopped" );
115     else
116     {
117         var_Get( p_input, "state", &val );
118         if( val.i_int == PAUSE_S )
119             psz_play = strdup( "pause" );
120         else if( val.i_int == PLAYING_S )
121             psz_play = strdup( "playing" );
122         else psz_play = strdup( "unknown" );
123     }
124     
125     PL_UNLOCK;
126     pl_Release( p_playlist );
127
128     ADD_STRING( &psz_play );
129     free( psz_play );
130     REPLY_SEND;
131 }
132
133 DBUS_METHOD( TogglePause )
134 { /* return a bool: true if playing */
135     REPLY_INIT;
136     OUT_ARGUMENTS;
137
138     vlc_value_t val;
139     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
140     PL_LOCK;
141     input_thread_t *p_input = p_playlist->p_input;
142
143     if( p_input )
144     {
145         var_Get( p_input, "state", &val );
146         if( val.i_int != PAUSE_S )
147         {
148             val.i_int = PAUSE_S;
149         }
150         else
151         {
152             val.i_int = PLAYING_S;
153         }
154         var_Set( p_input, "state", val );
155     }
156     PL_UNLOCK;
157     pl_Release( p_playlist );
158
159     dbus_bool_t pause = ( val.i_int == PLAYING_S ) ? TRUE : FALSE;
160     ADD_BOOL( &pause );
161     REPLY_SEND;
162 }
163
164 DBUS_SIGNAL( NewInstance )
165 { /* emits a signal with vlc pid */
166     SIGNAL_INIT( "NewInstance" );
167     OUT_ARGUMENTS;
168     dbus_uint32_t i_pid = (dbus_uint32_t) getpid();
169     ADD_UINT32( &i_pid );
170     SIGNAL_SEND;
171 }
172
173 DBUS_METHOD( AddMRL )
174 { /* add the string to the playlist, and play it if the boolean is true */
175     REPLY_INIT;
176
177     DBusError error;
178     dbus_error_init( &error );
179
180     intf_thread_t *p_intf = (intf_thread_t*) p_this;
181     char *psz_mrl;
182     dbus_bool_t b_play;
183
184     dbus_message_get_args( p_from, &error,
185             DBUS_TYPE_STRING, &psz_mrl,
186             DBUS_TYPE_BOOLEAN, &b_play,
187             DBUS_TYPE_INVALID );
188
189     if( dbus_error_is_set( &error ) )
190     {
191         printf("error: %s\n", error.message );
192         dbus_error_free( &error );
193         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
194     }
195
196     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
197     input_item_t *p_item = input_ItemNew( p_intf, psz_mrl, NULL );
198     if( p_item )
199     {
200         playlist_AddInput( p_playlist, p_item,
201             ( b_play == TRUE ) ? PLAYLIST_GO|PLAYLIST_APPEND : PLAYLIST_APPEND,
202             PLAYLIST_END, VLC_TRUE );
203     }
204     pl_Release( p_playlist );
205
206     REPLY_SEND;
207 }
208
209 /*****************************************************************************
210  * Introspection method
211  *****************************************************************************/
212
213 DBUS_METHOD( handle_introspect )
214 { /* handles introspection of /org/videolan/vlc */
215     REPLY_INIT;
216     OUT_ARGUMENTS;
217     ADD_STRING( &psz_introspection_xml_data );
218     REPLY_SEND;
219 }
220
221 /*****************************************************************************
222  * handle_messages: answer to incoming messages
223  *****************************************************************************/
224
225 #define METHOD_FUNC( method, function ) \
226     else if( dbus_message_is_method_call( p_from, VLC_DBUS_INTERFACE, method ) )\
227         return function( p_conn, p_from, p_this )
228
229 DBUS_METHOD( handle_messages )
230 { /* the main handler, that call methods */
231
232     if( dbus_message_is_method_call( p_from,
233                 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
234         return handle_introspect( p_conn, p_from, p_this );
235
236     /* here D-Bus method's names are associated to an handler */
237
238     METHOD_FUNC( "GetPlayStatus",   GetPlayStatus );
239     METHOD_FUNC( "AddMRL",          AddMRL );
240     METHOD_FUNC( "TogglePause",     TogglePause );
241     METHOD_FUNC( "Nothing",         Nothing );
242
243     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
244 }
245
246 /*****************************************************************************
247  * Open: initialize interface
248  *****************************************************************************/
249
250 static int Open( vlc_object_t *p_this )
251 { /* initialisation of the connection */
252     intf_thread_t   *p_intf = (intf_thread_t*)p_this;
253     intf_sys_t      *p_sys  = malloc( sizeof( intf_sys_t ) );
254     playlist_t      *p_playlist;
255     DBusConnection  *p_conn;
256     DBusError       error;
257
258     if( !p_sys )
259         return VLC_ENOMEM;
260
261     dbus_threads_init_default();
262     
263     dbus_error_init( &error );
264     
265     /* connect to the session bus */
266     p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
267     if( !p_conn )
268     {
269         msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
270                 error.message );
271         dbus_error_free( &error );
272         free( p_sys );
273         return VLC_EGENERIC;
274     }
275
276     /* we request the service org.videolan.vlc */    
277     dbus_bus_request_name( p_conn, VLC_DBUS_SERVICE,
278             DBUS_NAME_FLAG_REPLACE_EXISTING , &error );
279     if (dbus_error_is_set( &error ) )
280     { 
281         msg_Err( p_this, "Error requesting %s service: %s\n", VLC_DBUS_SERVICE,
282                 error.message );
283         dbus_error_free( &error );
284         free( p_sys );
285         return VLC_EGENERIC;
286     }
287
288     /* we register the object /org/videolan/vlc */
289     dbus_connection_register_object_path( p_conn, VLC_DBUS_OBJECT_PATH,
290             &vlc_dbus_vtable, p_this );
291
292     dbus_connection_flush( p_conn );
293
294     p_playlist = pl_Yield( p_intf );
295     PL_LOCK;
296     var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
297     PL_UNLOCK;
298     pl_Release( p_playlist );
299
300     p_intf->pf_run = Run;
301     p_intf->p_sys = p_sys;
302     p_sys->p_conn = p_conn;
303
304     return VLC_SUCCESS;
305 }
306
307 /*****************************************************************************
308  * Close: destroy interface
309  *****************************************************************************/
310
311 static void Close   ( vlc_object_t *p_this )
312 {
313     intf_thread_t   *p_intf     = (intf_thread_t*) p_this;
314     playlist_t      *p_playlist = pl_Yield( p_intf );;
315
316     PL_LOCK;
317     var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
318     PL_UNLOCK;
319     pl_Release( p_playlist );
320
321     free( p_intf->p_sys );
322 }
323
324 /*****************************************************************************
325  * Run: main loop    
326  *****************************************************************************/
327
328 static void Run          ( intf_thread_t *p_intf )
329 {
330     NewInstance( p_intf->p_sys->p_conn, NULL );
331
332     while( !p_intf->b_die )
333     {
334         msleep( INTF_IDLE_SLEEP );
335         dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
336     }
337 }
338
339 /*****************************************************************************
340  * ItemChange: Playlist item change callback
341  *****************************************************************************/
342
343 DBUS_SIGNAL( ItemChangeSignal )
344 { /* emit the name of the new item */
345     SIGNAL_INIT( "ItemChange" );
346     OUT_ARGUMENTS;
347
348     input_thread_t *p_input = (input_thread_t*) p_data;
349     ADD_STRING( &p_input->input.p_item->psz_name );
350
351     SIGNAL_SEND;
352 }
353
354 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
355             vlc_value_t oldval, vlc_value_t newval, void *p_data )
356 {
357     intf_thread_t       *p_intf     = ( intf_thread_t* ) p_data;
358     intf_sys_t          *p_sys      = p_intf->p_sys;
359     playlist_t          *p_playlist;
360     input_thread_t      *p_input    = NULL;
361     (void)p_this; (void)psz_var; (void)oldval; (void)newval;
362
363     p_playlist = pl_Yield( p_intf );
364     PL_LOCK;
365     p_input = p_playlist->p_input;
366
367     if( !p_input )
368     {
369         PL_UNLOCK;
370         pl_Release( p_playlist );
371         return VLC_SUCCESS;
372     }
373
374     vlc_object_yield( p_input );
375     PL_UNLOCK;
376     pl_Release( p_playlist );
377
378     ItemChangeSignal( p_sys->p_conn, p_input );
379
380     vlc_object_release( p_input );
381     return VLC_SUCCESS;
382 }
383