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