]> git.sesse.net Git - vlc/blob - modules/control/dbus/dbus_tracklist.c
dbus: Add missing memory allocation failure handling
[vlc] / modules / control / dbus / dbus_tracklist.c
1 /*****************************************************************************
2  * dbus_tracklist.c : dbus control module (mpris v2.1) - TrackList interface
3  *****************************************************************************
4  * Copyright © 2006-2011 Rafaël Carré
5  * Copyright © 2007-2011 Mirsal Ennaime
6  * Copyright © 2009-2011 The VideoLAN team
7  * Copyright © 2013      Alex Merry
8  * $Id$
9  *
10  * Authors:    Mirsal Ennaime <mirsal at mirsal fr>
11  *             Rafaël Carré <funman at videolanorg>
12  *             Alex Merry <dev at randomguy3 me uk>
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_playlist.h>
35
36 #include <assert.h>
37
38 #include "dbus_tracklist.h"
39 #include "dbus_common.h"
40
41 /**
42  * Retrieves the position of an input item in the playlist, given its id
43  *
44  * This function must be called with the playlist locked
45  *
46  * @param playlist_t*   p_playlist The playlist
47  * @param input_item_t* i_input_id An input item ID
48  *
49  * @return int The position of the input item or a VLC error constant
50  */
51 static int getInputPosition( playlist_t* p_playlist, int i_input_id )
52 {
53     input_item_t* p_input = NULL;
54
55     assert( p_playlist );
56     assert( i_input_id >= 0 );
57
58     playlist_AssertLocked( p_playlist );
59
60     for( int i = 0; i < playlist_CurrentSize( p_playlist ); i++ )
61     {
62         p_input = p_playlist->current.p_elems[i]->p_input;
63
64         if( !p_input )
65             return VLC_EGENERIC;
66
67         if( p_input->i_id == i_input_id )
68             return i;
69     }
70
71     return VLC_ENOITEM;
72 }
73
74 DBUS_METHOD( AddTrack )
75 {
76     REPLY_INIT;
77
78     DBusError error;
79     dbus_error_init( &error );
80
81     char *psz_mrl, *psz_aftertrack;
82     playlist_t *p_playlist = PL;
83     dbus_bool_t b_play;
84
85     int i_input_id = -1;
86     int i_mode = PLAYLIST_APPEND;
87     int i_pos  = PLAYLIST_END;
88
89     size_t i_append_len  = sizeof( DBUS_MPRIS_APPEND );
90     size_t i_notrack_len = sizeof( DBUS_MPRIS_NOTRACK );
91
92     dbus_message_get_args( p_from, &error,
93             DBUS_TYPE_STRING, &psz_mrl,
94             DBUS_TYPE_OBJECT_PATH, &psz_aftertrack,
95             DBUS_TYPE_BOOLEAN, &b_play,
96             DBUS_TYPE_INVALID );
97
98     if( dbus_error_is_set( &error ) )
99     {
100         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s",
101                 error.message );
102
103         dbus_error_free( &error );
104         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
105     }
106
107     if( !strncmp( DBUS_MPRIS_APPEND, psz_aftertrack, i_append_len ) )
108     {
109         i_mode = PLAYLIST_APPEND;
110         i_pos  = PLAYLIST_END;
111     }
112     else if( !strncmp( DBUS_MPRIS_NOTRACK, psz_aftertrack, i_notrack_len ) )
113     {
114         i_mode = PLAYLIST_INSERT;
115         i_pos  = 0;
116     }
117     else if( 1 == sscanf( psz_aftertrack, MPRIS_TRACKID_FORMAT, &i_input_id ) )
118     {
119         PL_LOCK;
120         int i_res = getInputPosition( p_playlist, i_input_id );
121         PL_UNLOCK;
122
123         if( i_res < 0 )
124             goto invalidTrackID;
125
126         i_mode = PLAYLIST_INSERT;
127         i_pos  = i_res + 1;
128     }
129     else
130     {
131 invalidTrackID:
132         msg_Warn( (vlc_object_t *) p_this,
133                 "AfterTrack: Invalid track ID \"%s\", appending instead",
134                 psz_aftertrack );
135     }
136
137     i_mode |= ( TRUE == b_play ) ? PLAYLIST_GO : 0;
138     playlist_Add( PL, psz_mrl, NULL, i_mode, i_pos, true, false );
139
140     REPLY_SEND;
141 }
142
143 DBUS_METHOD( GetTracksMetadata )
144 {
145     REPLY_INIT;
146     OUT_ARGUMENTS;
147
148     int i_track_id = -1;
149     const char *psz_track_id = NULL;
150
151     playlist_t   *p_playlist = PL;
152     input_item_t *p_input = NULL;
153
154     DBusMessageIter in_args, track_ids, meta;
155     dbus_message_iter_init( p_from, &in_args );
156
157     if( DBUS_TYPE_ARRAY != dbus_message_iter_get_arg_type( &in_args ) )
158     {
159         msg_Err( (vlc_object_t*) p_this, "Invalid arguments" );
160         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
161     }
162
163     dbus_message_iter_recurse( &in_args, &track_ids );
164     dbus_message_iter_open_container( &args, DBUS_TYPE_ARRAY, "a{sv}", &meta );
165
166     while( DBUS_TYPE_OBJECT_PATH ==
167            dbus_message_iter_get_arg_type( &track_ids ) )
168     {
169         dbus_message_iter_get_basic( &track_ids, &psz_track_id );
170
171         if( 1 != sscanf( psz_track_id, MPRIS_TRACKID_FORMAT, &i_track_id ) )
172         {
173             msg_Err( (vlc_object_t*) p_this, "Invalid track id: %s",
174                                              psz_track_id );
175             continue;
176         }
177
178         PL_LOCK;
179         for( int i = 0; i < playlist_CurrentSize( p_playlist ); i++ )
180         {
181             p_input = p_playlist->current.p_elems[i]->p_input;
182
183             if( i_track_id == p_input->i_id )
184             {
185                 GetInputMeta( p_input, &meta );
186                 break;
187             }
188         }
189         PL_UNLOCK;
190
191         dbus_message_iter_next( &track_ids );
192     }
193
194     dbus_message_iter_close_container( &args, &meta );
195     REPLY_SEND;
196 }
197
198 DBUS_METHOD( GoTo )
199 {
200     REPLY_INIT;
201
202     int i_track_id = -1;
203     const char *psz_track_id = NULL;
204     playlist_t *p_playlist = PL;
205
206     DBusError error;
207     dbus_error_init( &error );
208
209     dbus_message_get_args( p_from, &error,
210             DBUS_TYPE_OBJECT_PATH, &psz_track_id,
211             DBUS_TYPE_INVALID );
212
213     if( dbus_error_is_set( &error ) )
214     {
215         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s",
216                 error.message );
217         dbus_error_free( &error );
218         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
219     }
220
221     if( 1 != sscanf( psz_track_id, MPRIS_TRACKID_FORMAT, &i_track_id ) )
222     {
223         msg_Err( (vlc_object_t*) p_this, "Invalid track id %s", psz_track_id );
224         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
225     }
226
227     PL_LOCK;
228
229     for( int i = 0; i < playlist_CurrentSize( p_playlist ); i++ )
230     {
231         if( i_track_id == p_playlist->current.p_elems[i]->p_input->i_id )
232         {
233             playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, true,
234                               p_playlist->current.p_elems[i]->p_parent,
235                               p_playlist->current.p_elems[i] );
236             break;
237         }
238     }
239
240     PL_UNLOCK;
241     REPLY_SEND;
242 }
243
244 DBUS_METHOD( RemoveTrack )
245 {
246     REPLY_INIT;
247
248     DBusError error;
249     dbus_error_init( &error );
250
251     int   i_id = -1, i;
252     char *psz_id = NULL;
253     playlist_t *p_playlist = PL;
254     input_item_t *p_input  = NULL;
255
256     dbus_message_get_args( p_from, &error,
257             DBUS_TYPE_OBJECT_PATH, &psz_id,
258             DBUS_TYPE_INVALID );
259
260     if( dbus_error_is_set( &error ) )
261     {
262         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s",
263                 error.message );
264         dbus_error_free( &error );
265         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
266     }
267
268     if( 1 != sscanf( psz_id, MPRIS_TRACKID_FORMAT, &i_id ) )
269     {
270         msg_Err( (vlc_object_t*) p_this, "Invalid track id: %s", psz_id );
271         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
272     }
273
274     PL_LOCK;
275
276     for( i = 0; i < playlist_CurrentSize( p_playlist ); i++ )
277     {
278         p_input = p_playlist->current.p_elems[i]->p_input;
279
280         if( i_id == p_input->i_id )
281         {
282             playlist_DeleteFromInput( p_playlist, p_input, true );
283             break;
284         }
285     }
286
287     PL_UNLOCK;
288     REPLY_SEND;
289 }
290
291 static int
292 MarshalTracks( intf_thread_t *p_intf, DBusMessageIter *container )
293 {
294     DBusMessageIter tracks;
295     char         *psz_track_id = NULL;
296     playlist_t   *p_playlist   = p_intf->p_sys->p_playlist;
297     input_item_t *p_input      = NULL;
298
299     dbus_message_iter_open_container( container, DBUS_TYPE_ARRAY, "o",
300                                       &tracks );
301
302     PL_LOCK;
303
304     for( int i = 0; i < playlist_CurrentSize( p_playlist ); i++ )
305     {
306         p_input = p_playlist->current.p_elems[i]->p_input;
307
308         if( ( -1 == asprintf( &psz_track_id,
309                               MPRIS_TRACKID_FORMAT,
310                               p_input->i_id ) ) ||
311             !dbus_message_iter_append_basic( &tracks,
312                                              DBUS_TYPE_OBJECT_PATH,
313                                              &psz_track_id ) )
314         {
315             PL_UNLOCK;
316             dbus_message_iter_abandon_container( container, &tracks );
317             return VLC_ENOMEM;
318         }
319
320         free( psz_track_id );
321     }
322
323     PL_UNLOCK;
324
325     if( !dbus_message_iter_close_container( container, &tracks ) )
326         return VLC_ENOMEM;
327
328     return VLC_SUCCESS;
329 }
330
331 static int
332 MarshalCanEditTracks( intf_thread_t *p_intf, DBusMessageIter *container )
333 {
334     VLC_UNUSED( p_intf );
335     const dbus_bool_t b_ret = TRUE;
336
337     if( !dbus_message_iter_append_basic( container, DBUS_TYPE_BOOLEAN, &b_ret ) )
338         return VLC_ENOMEM;
339
340     return VLC_SUCCESS;
341 }
342
343 #define PROPERTY_MAPPING_BEGIN if( 0 ) {}
344 #define PROPERTY_GET_FUNC( prop, signature ) \
345     else if( !strcmp( psz_property_name,  #prop ) ) { \
346         if( !dbus_message_iter_open_container( &args, DBUS_TYPE_VARIANT, signature, &v ) ) \
347             return DBUS_HANDLER_RESULT_NEED_MEMORY; \
348         if( VLC_SUCCESS != Marshal##prop( p_this, &v ) ) { \
349             dbus_message_iter_abandon_container( &args, &v ); \
350             return DBUS_HANDLER_RESULT_NEED_MEMORY; \
351         } \
352         if( !dbus_message_iter_close_container( &args, &v ) ) \
353             return DBUS_HANDLER_RESULT_NEED_MEMORY; \
354     }
355
356 #define PROPERTY_SET_FUNC( prop ) \
357     else if( !strcmp( psz_property_name,  #prop ) ) { \
358         return prop##Set( p_conn, p_from, p_this ); \
359     }
360 #define PROPERTY_MAPPING_END else { return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; }
361
362
363 DBUS_METHOD( GetProperty )
364 {
365     DBusError error;
366
367     char *psz_interface_name = NULL;
368     char *psz_property_name  = NULL;
369
370     dbus_error_init( &error );
371     dbus_message_get_args( p_from, &error,
372             DBUS_TYPE_STRING, &psz_interface_name,
373             DBUS_TYPE_STRING, &psz_property_name,
374             DBUS_TYPE_INVALID );
375
376     if( dbus_error_is_set( &error ) )
377     {
378         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s",
379                                          error.message );
380         dbus_error_free( &error );
381         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
382     }
383
384     msg_Dbg( (vlc_object_t*) p_this, "Getting property %s",
385                                      psz_property_name );
386
387     if( strcmp( psz_interface_name, DBUS_MPRIS_TRACKLIST_INTERFACE ) ) {
388         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
389     }
390
391     REPLY_INIT;
392     OUT_ARGUMENTS;
393     DBusMessageIter v;
394
395     PROPERTY_MAPPING_BEGIN
396     PROPERTY_GET_FUNC( Tracks, "ao" )
397     PROPERTY_GET_FUNC( CanEditTracks, "b" )
398     PROPERTY_MAPPING_END
399
400     REPLY_SEND;
401 }
402
403 #undef PROPERTY_MAPPING_BEGIN
404 #undef PROPERTY_GET_FUNC
405 #undef PROPERTY_SET_FUNC
406 #undef PROPERTY_MAPPING_END
407
408 #define ADD_PROPERTY( prop, signature ) \
409     if( VLC_SUCCESS != AddProperty( (intf_thread_t*) p_this, \
410                 &dict, #prop, signature, Marshal##prop ) ) { \
411         dbus_message_iter_abandon_container( &args, &dict ); \
412         return VLC_ENOMEM; \
413     }
414
415 DBUS_METHOD( GetAllProperties )
416 {
417     REPLY_INIT;
418     OUT_ARGUMENTS;
419
420     DBusError error;
421     DBusMessageIter dict;
422
423     char *const psz_interface_name = NULL;
424
425     dbus_error_init( &error );
426     dbus_message_get_args( p_from, &error,
427             DBUS_TYPE_STRING, &psz_interface_name,
428             DBUS_TYPE_INVALID );
429
430     if( dbus_error_is_set( &error ) )
431     {
432         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s",
433                                          error.message );
434         dbus_error_free( &error );
435         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
436     }
437
438     msg_Dbg( (vlc_object_t*) p_this, "Getting All properties" );
439
440     if( !dbus_message_iter_open_container( &args, DBUS_TYPE_ARRAY, "{sv}", &dict ) )
441         return DBUS_HANDLER_RESULT_NEED_MEMORY;
442
443     ADD_PROPERTY ( Tracks,        "ao" )
444     ADD_PROPERTY ( CanEditTracks, "b"  )
445
446     if( !dbus_message_iter_close_container( &args, &dict ))
447         return DBUS_HANDLER_RESULT_NEED_MEMORY;
448
449     REPLY_SEND;
450 }
451
452 #undef ADD_PROPERTY
453
454 #define METHOD_FUNC( interface, method, function ) \
455     else if( dbus_message_is_method_call( p_from, interface, method ) )\
456         return function( p_conn, p_from, p_this )
457
458 DBusHandlerResult
459 handle_tracklist ( DBusConnection *p_conn, DBusMessage *p_from, void *p_this )
460 {
461     if(0);
462
463     METHOD_FUNC( DBUS_INTERFACE_PROPERTIES, "Get",    GetProperty );
464     METHOD_FUNC( DBUS_INTERFACE_PROPERTIES, "GetAll", GetAllProperties );
465
466     /* here D-Bus method names are associated to an handler */
467
468     METHOD_FUNC( DBUS_MPRIS_TRACKLIST_INTERFACE, "GoTo",        GoTo );
469     METHOD_FUNC( DBUS_MPRIS_TRACKLIST_INTERFACE, "AddTrack",    AddTrack );
470     METHOD_FUNC( DBUS_MPRIS_TRACKLIST_INTERFACE, "RemoveTrack", RemoveTrack );
471     METHOD_FUNC( DBUS_MPRIS_TRACKLIST_INTERFACE, "GetTracksMetadata",
472                                                   GetTracksMetadata );
473
474     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
475 }
476
477 #undef METHOD_FUNC
478
479 /**
480  * PropertiesChangedSignal: synthetizes and sends the
481  * org.freedesktop.DBus.Properties.PropertiesChanged signal
482  */
483 static DBusHandlerResult
484 PropertiesChangedSignal( intf_thread_t    *p_intf,
485                          vlc_dictionary_t *p_changed_properties )
486 {
487     DBusConnection  *p_conn = p_intf->p_sys->p_conn;
488     DBusMessageIter changed_properties, invalidated_properties;
489     const char *psz_interface_name = DBUS_MPRIS_TRACKLIST_INTERFACE;
490     char **ppsz_properties = NULL;
491     int i_properties = 0;
492
493     SIGNAL_INIT( DBUS_INTERFACE_PROPERTIES,
494                  DBUS_MPRIS_OBJECT_PATH,
495                  "PropertiesChanged" );
496
497     OUT_ARGUMENTS;
498     ADD_STRING( &psz_interface_name );
499
500     if( unlikely(!dbus_message_iter_open_container( &args,
501                                                     DBUS_TYPE_ARRAY, "{sv}",
502                                                     &changed_properties )) )
503         return DBUS_HANDLER_RESULT_NEED_MEMORY;
504
505     if( unlikely(!dbus_message_iter_close_container( &args,
506                                                      &changed_properties )) )
507         return DBUS_HANDLER_RESULT_NEED_MEMORY;
508
509     if( unlikely(!dbus_message_iter_open_container( &args, DBUS_TYPE_ARRAY, "s",
510                                                     &invalidated_properties )) )
511         return DBUS_HANDLER_RESULT_NEED_MEMORY;
512
513     i_properties    = vlc_dictionary_keys_count( p_changed_properties );
514     ppsz_properties = vlc_dictionary_all_keys( p_changed_properties );
515
516     if( unlikely(!ppsz_properties) )
517     {
518         dbus_message_iter_abandon_container( &args, &invalidated_properties );
519         return DBUS_HANDLER_RESULT_NEED_MEMORY;
520     }
521
522     for( int i = 0; i < i_properties; i++ )
523     {
524         if( !strcmp( ppsz_properties[i], "Tracks" ) )
525             dbus_message_iter_append_basic( &invalidated_properties,
526                                             DBUS_TYPE_STRING,
527                                             &ppsz_properties[i] );
528
529         free( ppsz_properties[i] );
530     }
531
532     free( ppsz_properties );
533
534     if( unlikely(!dbus_message_iter_close_container( &args,
535                     &invalidated_properties )) )
536         return DBUS_HANDLER_RESULT_NEED_MEMORY;
537
538     SIGNAL_SEND;
539 }
540
541 /**
542  * TrackListPropertiesChangedEmit: Emits the
543  * org.freedesktop.DBus.Properties.PropertiesChanged signal
544  */
545 int TrackListPropertiesChangedEmit( intf_thread_t    * p_intf,
546                                     vlc_dictionary_t * p_changed_properties )
547 {
548     if( p_intf->p_sys->b_dead )
549         return VLC_SUCCESS;
550
551     PropertiesChangedSignal( p_intf, p_changed_properties );
552     return VLC_SUCCESS;
553 }