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