]> git.sesse.net Git - vlc/blob - modules/control/dbus.c
A bit of headers cleanup
[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 "dbus.h"
53
54 #include <vlc/vlc.h>
55 #include <vlc_aout.h>
56 #include <vlc_interface.h>
57 #include <vlc_meta.h>
58 #include <vlc_input.h>
59
60 /*****************************************************************************
61  * Local prototypes.
62  *****************************************************************************/
63
64 static int  Open    ( vlc_object_t * );
65 static void Close   ( vlc_object_t * );
66 static void Run        ( intf_thread_t * );
67
68
69 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
70                     vlc_value_t oldval, vlc_value_t newval, void *p_data );
71
72 struct intf_sys_t
73 {
74     DBusConnection *p_conn;
75 };
76
77 /*****************************************************************************
78  * Module descriptor
79  *****************************************************************************/
80
81 vlc_module_begin();
82     set_shortname( _("dbus"));
83     set_category( CAT_INTERFACE );
84     set_subcategory( SUBCAT_INTERFACE_CONTROL );
85     set_description( _("D-Bus control interface") );
86     set_capability( "interface", 0 );
87     set_callbacks( Open, Close );
88 vlc_module_end();
89
90 /*****************************************************************************
91  * Methods
92  *****************************************************************************/
93
94 DBUS_METHOD( Nothing )
95 { /* do nothing */
96     REPLY_INIT;
97     REPLY_SEND;
98 }
99
100 DBUS_METHOD( Quit )
101 { /* exits vlc */
102     REPLY_INIT;
103     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
104     playlist_Stop( p_playlist );
105     pl_Release( ((vlc_object_t*) p_this) );
106     ((vlc_object_t*)p_this)->p_libvlc->b_die = VLC_TRUE;
107     REPLY_SEND;
108 }
109
110 DBUS_METHOD( PositionGet )
111 { /* returns position as an int in the range [0;1000] */
112     REPLY_INIT;
113     OUT_ARGUMENTS;
114     vlc_value_t position;
115     dbus_uint16_t i_pos;
116
117     playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
118     input_thread_t *p_input = p_playlist->p_input;
119
120     if( !p_input )
121         i_pos = 0;
122     else
123     {
124         var_Get( p_input, "position", &position );
125         i_pos = position.f_float * 1000 ;
126     }
127     ADD_UINT16( &i_pos );
128     pl_Release( ((vlc_object_t*) p_this) );
129     REPLY_SEND;
130 }
131
132 DBUS_METHOD( PositionSet )
133 { /* set position from an int in the range [0;1000] */
134
135     REPLY_INIT;
136     vlc_value_t position;
137     dbus_uint16_t i_pos;
138
139     DBusError error;
140     dbus_error_init( &error );
141
142     dbus_message_get_args( p_from, &error,
143             DBUS_TYPE_UINT16, &i_pos,
144             DBUS_TYPE_INVALID );
145
146     if( dbus_error_is_set( &error ) )
147     {
148         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
149                 error.message );
150         dbus_error_free( &error );
151         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
152     }
153     playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
154     input_thread_t *p_input = p_playlist->p_input;
155
156     if( p_input )
157     {
158         position.f_float = ((float)i_pos) / 1000;
159         var_Set( p_input, "position", position );
160     }
161     pl_Release( ((vlc_object_t*) p_this) );
162     REPLY_SEND;
163 }
164
165 DBUS_METHOD( VolumeGet )
166 { /* returns volume in percentage */
167     REPLY_INIT;
168     OUT_ARGUMENTS;
169     dbus_uint16_t i_vol;
170     /* 2nd argument of aout_VolumeGet is uint16 */
171     aout_VolumeGet( (vlc_object_t*) p_this, &i_vol );
172     i_vol = ( 100 * i_vol ) / AOUT_VOLUME_MAX;
173     ADD_UINT16( &i_vol );
174     REPLY_SEND;
175 }
176
177 DBUS_METHOD( VolumeSet )
178 { /* set volume in percentage */
179     REPLY_INIT;
180
181     DBusError error;
182     dbus_error_init( &error );
183
184     dbus_uint16_t i_vol;
185
186     dbus_message_get_args( p_from, &error,
187             DBUS_TYPE_UINT16, &i_vol,
188             DBUS_TYPE_INVALID );
189
190     if( dbus_error_is_set( &error ) )
191     {
192         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
193                 error.message );
194         dbus_error_free( &error );
195         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
196     }
197
198     aout_VolumeSet( (vlc_object_t*) p_this, ( AOUT_VOLUME_MAX / 100 ) * i_vol );
199
200     REPLY_SEND;
201 }
202
203 DBUS_METHOD( Next )
204 { /* next playlist item */
205     REPLY_INIT;
206     playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
207     playlist_Next( p_playlist );
208     pl_Release( ((vlc_object_t*) p_this) );
209     REPLY_SEND;
210 }
211
212 DBUS_METHOD( Prev )
213 { /* previous playlist item */
214     REPLY_INIT;
215     playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
216     playlist_Prev( p_playlist );
217     pl_Release( ((vlc_object_t*) p_this) );
218     REPLY_SEND;
219 }
220
221 DBUS_METHOD( Stop )
222 { /* stop playing */
223     REPLY_INIT;
224     playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
225     playlist_Stop( p_playlist );
226     pl_Release( ((vlc_object_t*) p_this) );
227     REPLY_SEND;
228 }
229
230 DBUS_METHOD( GetPlayingItem )
231 { /* return the current item */
232     REPLY_INIT;
233     OUT_ARGUMENTS;
234     char psz_no_input = '\0';
235     char *p_psz_no_input = &psz_no_input;
236     playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
237     input_thread_t *p_input = p_playlist->p_input;
238     ADD_STRING( ( p_input ) ? &p_input->input.p_item->psz_name :
239             &p_psz_no_input );
240     pl_Release( ((vlc_object_t*) p_this) );
241     REPLY_SEND;
242 }
243
244 DBUS_METHOD( GetPlayStatus )
245 { /* return a string */
246     REPLY_INIT;
247     OUT_ARGUMENTS;
248
249     char *psz_play;
250     vlc_value_t val;
251     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
252     input_thread_t *p_input = p_playlist->p_input;
253
254     if( !p_input )
255         psz_play = strdup( "stopped" );
256     else
257     {
258         var_Get( p_input, "state", &val );
259         if( val.i_int == PAUSE_S )
260             psz_play = strdup( "pause" );
261         else if( val.i_int == PLAYING_S )
262             psz_play = strdup( "playing" );
263         else psz_play = strdup( "unknown" );
264     }
265     
266     pl_Release( p_playlist );
267
268     ADD_STRING( &psz_play );
269     free( psz_play );
270     REPLY_SEND;
271 }
272
273 DBUS_METHOD( TogglePause )
274 { /* return a bool: true if playing */
275     REPLY_INIT;
276     OUT_ARGUMENTS;
277
278     vlc_value_t val;
279     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
280     input_thread_t *p_input = p_playlist->p_input;
281     if( p_input != NULL )
282     {
283         var_Get( p_input, "state", &val );
284         if( val.i_int != PAUSE_S )
285         {
286             val.i_int = PAUSE_S;
287             playlist_Pause( p_playlist );
288         }
289         else
290         {
291             val.i_int = PLAYING_S;
292             playlist_Play( p_playlist );
293         }
294     }
295     else
296     {
297         val.i_int = PLAYING_S;
298         playlist_Play( p_playlist );
299     }
300     pl_Release( p_playlist );
301
302     dbus_bool_t pause = ( val.i_int == PLAYING_S ) ? TRUE : FALSE;
303     ADD_BOOL( &pause );
304     REPLY_SEND;
305 }
306
307 DBUS_METHOD( AddMRL )
308 { /* add the string to the playlist, and play it if the boolean is true */
309     REPLY_INIT;
310
311     DBusError error;
312     dbus_error_init( &error );
313
314     char *psz_mrl;
315     dbus_bool_t b_play;
316
317     dbus_message_get_args( p_from, &error,
318             DBUS_TYPE_STRING, &psz_mrl,
319             DBUS_TYPE_BOOLEAN, &b_play,
320             DBUS_TYPE_INVALID );
321
322     if( dbus_error_is_set( &error ) )
323     {
324         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
325                 error.message );
326         dbus_error_free( &error );
327         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
328     }
329
330     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
331     playlist_Add( p_playlist, psz_mrl, NULL, PLAYLIST_APPEND |
332             ( ( b_play == TRUE ) ? PLAYLIST_GO : 0 ) , PLAYLIST_END, VLC_TRUE );
333     pl_Release( p_playlist );
334
335     REPLY_SEND;
336 }
337
338 /*****************************************************************************
339  * Introspection method
340  *****************************************************************************/
341
342 DBUS_METHOD( handle_introspect )
343 { /* handles introspection of /org/videolan/vlc */
344     REPLY_INIT;
345     OUT_ARGUMENTS;
346     ADD_STRING( &psz_introspection_xml_data );
347     REPLY_SEND;
348 }
349
350 /*****************************************************************************
351  * handle_messages: answer to incoming messages
352  *****************************************************************************/
353
354 #define METHOD_FUNC( method, function ) \
355     else if( dbus_message_is_method_call( p_from, VLC_DBUS_INTERFACE, method ) )\
356         return function( p_conn, p_from, p_this )
357
358 DBUS_METHOD( handle_messages )
359 { /* the main handler, that call methods */
360
361     if( dbus_message_is_method_call( p_from,
362                 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
363         return handle_introspect( p_conn, p_from, p_this );
364
365     /* here D-Bus method's names are associated to an handler */
366
367     METHOD_FUNC( "GetPlayStatus",   GetPlayStatus );
368     METHOD_FUNC( "GetPlayingItem",  GetPlayingItem );
369     METHOD_FUNC( "AddMRL",          AddMRL );
370     METHOD_FUNC( "TogglePause",     TogglePause );
371     METHOD_FUNC( "Nothing",         Nothing );
372     METHOD_FUNC( "Prev",            Prev );
373     METHOD_FUNC( "Next",            Next );
374     METHOD_FUNC( "Quit",            Quit );
375     METHOD_FUNC( "Stop",            Stop );
376     METHOD_FUNC( "VolumeSet",       VolumeSet );
377     METHOD_FUNC( "VolumeGet",       VolumeGet );
378     METHOD_FUNC( "PositionSet",     PositionSet );
379     METHOD_FUNC( "PositionGet",     PositionGet );
380
381     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
382 }
383
384 /*****************************************************************************
385  * Open: initialize interface
386  *****************************************************************************/
387
388 static int Open( vlc_object_t *p_this )
389 { /* initialisation of the connection */
390     intf_thread_t   *p_intf = (intf_thread_t*)p_this;
391     intf_sys_t      *p_sys  = malloc( sizeof( intf_sys_t ) );
392     playlist_t      *p_playlist;
393     DBusConnection  *p_conn;
394     DBusError       error;
395
396     if( !p_sys )
397         return VLC_ENOMEM;
398
399     dbus_threads_init_default();
400     
401     dbus_error_init( &error );
402     
403     /* connect to the session bus */
404     p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
405     if( !p_conn )
406     {
407         msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
408                 error.message );
409         dbus_error_free( &error );
410         free( p_sys );
411         return VLC_EGENERIC;
412     }
413
414     /* we unregister the object /, registered by libvlc */
415     dbus_connection_unregister_object_path( p_conn, "/" );
416
417     /* we register the object /org/videolan/vlc */
418     dbus_connection_register_object_path( p_conn, VLC_DBUS_OBJECT_PATH,
419             &vlc_dbus_vtable, p_this );
420
421     dbus_connection_flush( p_conn );
422
423     p_playlist = pl_Yield( p_intf );
424     PL_LOCK;
425     var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
426     PL_UNLOCK;
427     pl_Release( p_playlist );
428
429     p_intf->pf_run = Run;
430     p_intf->p_sys = p_sys;
431     p_sys->p_conn = p_conn;
432
433     return VLC_SUCCESS;
434 }
435
436 /*****************************************************************************
437  * Close: destroy interface
438  *****************************************************************************/
439
440 static void Close   ( vlc_object_t *p_this )
441 {
442     intf_thread_t   *p_intf     = (intf_thread_t*) p_this;
443     playlist_t      *p_playlist = pl_Yield( p_intf );;
444
445     PL_LOCK;
446     var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
447     PL_UNLOCK;
448     pl_Release( p_playlist );
449
450     dbus_connection_unref( p_intf->p_sys->p_conn );
451
452     free( p_intf->p_sys );
453 }
454
455 /*****************************************************************************
456  * Run: main loop    
457  *****************************************************************************/
458
459 static void Run          ( intf_thread_t *p_intf )
460 {
461     while( !p_intf->b_die )
462     {
463         msleep( INTF_IDLE_SLEEP );
464         dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
465     }
466 }
467
468 /*****************************************************************************
469  * ItemChange: Playlist item change callback
470  *****************************************************************************/
471
472 DBUS_SIGNAL( ItemChangeSignal )
473 { /* emit the name of the new item */
474     SIGNAL_INIT( "ItemChange" );
475     OUT_ARGUMENTS;
476
477     input_thread_t *p_input = (input_thread_t*) p_data;
478     ADD_STRING( &p_input->input.p_item->psz_name );
479
480     SIGNAL_SEND;
481 }
482
483 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
484             vlc_value_t oldval, vlc_value_t newval, void *p_data )
485 {
486     intf_thread_t       *p_intf     = ( intf_thread_t* ) p_data;
487     intf_sys_t          *p_sys      = p_intf->p_sys;
488     playlist_t          *p_playlist;
489     input_thread_t      *p_input    = NULL;
490     (void)p_this; (void)psz_var; (void)oldval; (void)newval;
491
492     p_playlist = pl_Yield( p_intf );
493     PL_LOCK;
494     p_input = p_playlist->p_input;
495
496     if( !p_input )
497     {
498         PL_UNLOCK;
499         pl_Release( p_playlist );
500         return VLC_SUCCESS;
501     }
502
503     vlc_object_yield( p_input );
504     PL_UNLOCK;
505     pl_Release( p_playlist );
506
507     ItemChangeSignal( p_sys->p_conn, p_input );
508
509     vlc_object_release( p_input );
510     return VLC_SUCCESS;
511 }
512