]> git.sesse.net Git - vlc/blob - modules/control/dbus.c
Uses playlist facilities
[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( Quit )
103 { /* exits vlc */
104     REPLY_INIT;
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;
109     REPLY_SEND;
110 }
111
112 DBUS_METHOD( Next )
113 { /* next playlist item */
114     REPLY_INIT;
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) );
118     REPLY_SEND;
119 }
120
121 DBUS_METHOD( Prev )
122 { /* previous playlist item */
123     REPLY_INIT;
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) );
127     REPLY_SEND;
128 }
129
130 DBUS_METHOD( Stop )
131 { /* stop playing */
132     REPLY_INIT;
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) );
136     REPLY_SEND;
137 }
138
139 DBUS_METHOD( GetPlayingItem )
140 { /* return the current item */
141     REPLY_INIT;
142     OUT_ARGUMENTS;
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 :
148             &p_psz_no_input );
149     pl_Release( ((vlc_object_t*) p_this) );
150     REPLY_SEND;
151 }
152
153 DBUS_METHOD( GetPlayStatus )
154 { /* return a string */
155     REPLY_INIT;
156     OUT_ARGUMENTS;
157
158     char *psz_play;
159     vlc_value_t val;
160     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
161     input_thread_t *p_input = p_playlist->p_input;
162
163     if( !p_input )
164         psz_play = strdup( "stopped" );
165     else
166     {
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" );
173     }
174     
175     pl_Release( p_playlist );
176
177     ADD_STRING( &psz_play );
178     free( psz_play );
179     REPLY_SEND;
180 }
181
182 DBUS_METHOD( TogglePause )
183 { /* return a bool: true if playing */
184     REPLY_INIT;
185     OUT_ARGUMENTS;
186
187     vlc_value_t val;
188     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
189     input_thread_t *p_input = p_playlist->p_input;
190     if( p_input != NULL )
191     {
192         var_Get( p_input, "state", &val );
193         if( val.i_int != PAUSE_S )
194         {
195             val.i_int = PAUSE_S;
196             playlist_Pause( p_playlist );
197         }
198         else
199         {
200             val.i_int = PLAYING_S;
201             playlist_Play( p_playlist );
202         }
203     }
204     else
205     {
206         val.i_int = PLAYING_S;
207         playlist_Play( p_playlist );
208     }
209     pl_Release( p_playlist );
210
211     dbus_bool_t pause = ( val.i_int == PLAYING_S ) ? TRUE : FALSE;
212     ADD_BOOL( &pause );
213     REPLY_SEND;
214 }
215
216 DBUS_SIGNAL( NewInstance )
217 { /* emits a signal with vlc pid */
218     SIGNAL_INIT( "NewInstance" );
219     OUT_ARGUMENTS;
220     dbus_uint32_t i_pid = (dbus_uint32_t) getpid();
221     ADD_UINT32( &i_pid );
222     SIGNAL_SEND;
223 }
224
225 DBUS_METHOD( AddMRL )
226 { /* add the string to the playlist, and play it if the boolean is true */
227     REPLY_INIT;
228
229     DBusError error;
230     dbus_error_init( &error );
231
232     char *psz_mrl;
233     dbus_bool_t b_play;
234
235     dbus_message_get_args( p_from, &error,
236             DBUS_TYPE_STRING, &psz_mrl,
237             DBUS_TYPE_BOOLEAN, &b_play,
238             DBUS_TYPE_INVALID );
239
240     if( dbus_error_is_set( &error ) )
241     {
242         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
243                 error.message );
244         dbus_error_free( &error );
245         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
246     }
247
248     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
249     playlist_Add( p_playlist, psz_mrl, NULL, PLAYLIST_APPEND |
250             ( ( b_play == TRUE ) ? PLAYLIST_GO : 0 ) , PLAYLIST_END, VLC_TRUE );
251     pl_Release( p_playlist );
252
253     REPLY_SEND;
254 }
255
256 /*****************************************************************************
257  * Introspection method
258  *****************************************************************************/
259
260 DBUS_METHOD( handle_introspect )
261 { /* handles introspection of /org/videolan/vlc */
262     REPLY_INIT;
263     OUT_ARGUMENTS;
264     ADD_STRING( &psz_introspection_xml_data );
265     REPLY_SEND;
266 }
267
268 /*****************************************************************************
269  * handle_messages: answer to incoming messages
270  *****************************************************************************/
271
272 #define METHOD_FUNC( method, function ) \
273     else if( dbus_message_is_method_call( p_from, VLC_DBUS_INTERFACE, method ) )\
274         return function( p_conn, p_from, p_this )
275
276 DBUS_METHOD( handle_messages )
277 { /* the main handler, that call methods */
278
279     if( dbus_message_is_method_call( p_from,
280                 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
281         return handle_introspect( p_conn, p_from, p_this );
282
283     /* here D-Bus method's names are associated to an handler */
284
285     METHOD_FUNC( "GetPlayStatus",   GetPlayStatus );
286     METHOD_FUNC( "GetPlayingItem",  GetPlayingItem );
287     METHOD_FUNC( "AddMRL",          AddMRL );
288     METHOD_FUNC( "TogglePause",     TogglePause );
289     METHOD_FUNC( "Nothing",         Nothing );
290     METHOD_FUNC( "Prev",            Prev );
291     METHOD_FUNC( "Next",            Next );
292     METHOD_FUNC( "Quit",            Quit );
293     METHOD_FUNC( "Stop",            Stop );
294
295     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
296 }
297
298 /*****************************************************************************
299  * Open: initialize interface
300  *****************************************************************************/
301
302 static int Open( vlc_object_t *p_this )
303 { /* initialisation of the connection */
304     intf_thread_t   *p_intf = (intf_thread_t*)p_this;
305     intf_sys_t      *p_sys  = malloc( sizeof( intf_sys_t ) );
306     playlist_t      *p_playlist;
307     DBusConnection  *p_conn;
308     DBusError       error;
309
310     if( !p_sys )
311         return VLC_ENOMEM;
312
313     dbus_threads_init_default();
314     
315     dbus_error_init( &error );
316     
317     /* connect to the session bus */
318     p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
319     if( !p_conn )
320     {
321         msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
322                 error.message );
323         dbus_error_free( &error );
324         free( p_sys );
325         return VLC_EGENERIC;
326     }
327
328     /* we unregister the object /, registered by libvlc */
329     dbus_connection_unregister_object_path( p_conn, "/" );
330
331     /* we register the object /org/videolan/vlc */
332     dbus_connection_register_object_path( p_conn, VLC_DBUS_OBJECT_PATH,
333             &vlc_dbus_vtable, p_this );
334
335     dbus_connection_flush( p_conn );
336
337     p_playlist = pl_Yield( p_intf );
338     PL_LOCK;
339     var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
340     PL_UNLOCK;
341     pl_Release( p_playlist );
342
343     p_intf->pf_run = Run;
344     p_intf->p_sys = p_sys;
345     p_sys->p_conn = p_conn;
346
347     return VLC_SUCCESS;
348 }
349
350 /*****************************************************************************
351  * Close: destroy interface
352  *****************************************************************************/
353
354 static void Close   ( vlc_object_t *p_this )
355 {
356     intf_thread_t   *p_intf     = (intf_thread_t*) p_this;
357     playlist_t      *p_playlist = pl_Yield( p_intf );;
358
359     PL_LOCK;
360     var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
361     PL_UNLOCK;
362     pl_Release( p_playlist );
363
364     free( p_intf->p_sys );
365 }
366
367 /*****************************************************************************
368  * Run: main loop    
369  *****************************************************************************/
370
371 static void Run          ( intf_thread_t *p_intf )
372 {
373     NewInstance( p_intf->p_sys->p_conn, NULL );
374
375     while( !p_intf->b_die )
376     {
377         msleep( INTF_IDLE_SLEEP );
378         dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
379     }
380 }
381
382 /*****************************************************************************
383  * ItemChange: Playlist item change callback
384  *****************************************************************************/
385
386 DBUS_SIGNAL( ItemChangeSignal )
387 { /* emit the name of the new item */
388     SIGNAL_INIT( "ItemChange" );
389     OUT_ARGUMENTS;
390
391     input_thread_t *p_input = (input_thread_t*) p_data;
392     ADD_STRING( &p_input->input.p_item->psz_name );
393
394     SIGNAL_SEND;
395 }
396
397 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
398             vlc_value_t oldval, vlc_value_t newval, void *p_data )
399 {
400     intf_thread_t       *p_intf     = ( intf_thread_t* ) p_data;
401     intf_sys_t          *p_sys      = p_intf->p_sys;
402     playlist_t          *p_playlist;
403     input_thread_t      *p_input    = NULL;
404     (void)p_this; (void)psz_var; (void)oldval; (void)newval;
405
406     p_playlist = pl_Yield( p_intf );
407     PL_LOCK;
408     p_input = p_playlist->p_input;
409
410     if( !p_input )
411     {
412         PL_UNLOCK;
413         pl_Release( p_playlist );
414         return VLC_SUCCESS;
415     }
416
417     vlc_object_yield( p_input );
418     PL_UNLOCK;
419     pl_Release( p_playlist );
420
421     ItemChangeSignal( p_sys->p_conn, p_input );
422
423     vlc_object_release( p_input );
424     return VLC_SUCCESS;
425 }
426