]> git.sesse.net Git - vlc/blob - modules/control/dbus/dbus_tracklist.c
dbus fixes
[vlc] / modules / control / dbus / dbus_tracklist.c
1 /*****************************************************************************
2  * dbus-tracklist.c : dbus control module (mpris v1.0) - /TrackList object
3  *****************************************************************************
4  * Copyright © 2006-2008 Rafaël Carré
5  * Copyright © 2007-2010 Mirsal Ennaime
6  * Copyright © 2009-2010 The VideoLAN team
7  * $Id$
8  *
9  * Authors:    Mirsal Ennaime <mirsal at mirsal fr>
10  *             Rafaël Carré <funman at videolanorg>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_playlist.h>
33
34 #include <assert.h>
35
36 #include "dbus_tracklist.h"
37 #include "dbus_common.h"
38
39 DBUS_METHOD( AddTrack )
40 { /* add the string to the playlist, and play it if the boolean is true */
41     REPLY_INIT;
42
43     DBusError error;
44     dbus_error_init( &error );
45
46     char *psz_mrl, *psz_aftertrack;
47     dbus_bool_t b_play;
48
49     dbus_message_get_args( p_from, &error,
50             DBUS_TYPE_STRING, &psz_mrl,
51             DBUS_TYPE_OBJECT_PATH, &psz_aftertrack,
52             DBUS_TYPE_BOOLEAN, &b_play,
53             DBUS_TYPE_INVALID );
54
55     if( dbus_error_is_set( &error ) )
56     {
57         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s",
58                 error.message );
59         dbus_error_free( &error );
60         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
61     }
62
63 #warning psz_aftertrack is not used
64     playlist_Add( PL, psz_mrl, NULL, PLAYLIST_APPEND |
65             ( ( b_play == TRUE ) ? PLAYLIST_GO : 0 ) ,
66             PLAYLIST_END, true, false );
67
68     REPLY_SEND;
69 }
70
71 DBUS_METHOD( GetCurrentTrack )
72 {
73     REPLY_INIT;
74     OUT_ARGUMENTS;
75
76     playlist_t *p_playlist = PL;
77
78     PL_LOCK;
79     dbus_int32_t i_position = PL->i_current_index;
80     PL_UNLOCK;
81
82     ADD_INT32( &i_position );
83     REPLY_SEND;
84 }
85
86 DBUS_METHOD( GetMetadata )
87 {
88     REPLY_INIT;
89     OUT_ARGUMENTS;
90     DBusError error;
91     dbus_error_init( &error );
92
93     dbus_int32_t i_position;
94     playlist_t *p_playlist = PL;
95
96     dbus_message_get_args( p_from, &error,
97            DBUS_TYPE_INT32, &i_position,
98            DBUS_TYPE_INVALID );
99
100     if( dbus_error_is_set( &error ) )
101     {
102         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s",
103                 error.message );
104         dbus_error_free( &error );
105         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
106     }
107
108     PL_LOCK;
109     if( i_position < p_playlist->current.i_size )
110     {
111         GetInputMeta( p_playlist->current.p_elems[i_position]->p_input, &args );
112     }
113
114     PL_UNLOCK;
115     REPLY_SEND;
116 }
117
118 DBUS_METHOD( GetLength )
119 {
120     REPLY_INIT;
121     OUT_ARGUMENTS;
122     playlist_t *p_playlist = PL;
123
124     PL_LOCK;
125     dbus_int32_t i_elements = PL->current.i_size;
126     PL_UNLOCK;
127
128     ADD_INT32( &i_elements );
129     REPLY_SEND;
130 }
131
132 DBUS_METHOD( DelTrack )
133 {
134     REPLY_INIT;
135
136     DBusError error;
137     dbus_error_init( &error );
138
139     dbus_int32_t i_position;
140     playlist_t *p_playlist = PL;
141
142     dbus_message_get_args( p_from, &error,
143             DBUS_TYPE_INT32, &i_position,
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",
149                 error.message );
150         dbus_error_free( &error );
151         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
152     }
153
154     PL_LOCK;
155     if( i_position < p_playlist->current.i_size )
156     {
157         playlist_DeleteFromInput( p_playlist,
158             p_playlist->current.p_elems[i_position]->p_input,
159             pl_Locked );
160     }
161     PL_UNLOCK;
162
163     REPLY_SEND;
164 }
165
166 DBUS_METHOD( SetLoop )
167 {
168     REPLY_INIT;
169     OUT_ARGUMENTS;
170
171     DBusError error;
172     dbus_bool_t b_loop;
173
174     dbus_error_init( &error );
175     dbus_message_get_args( p_from, &error,
176             DBUS_TYPE_BOOLEAN, &b_loop,
177             DBUS_TYPE_INVALID );
178
179     if( dbus_error_is_set( &error ) )
180     {
181         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s",
182                 error.message );
183         dbus_error_free( &error );
184         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
185     }
186
187     var_SetBool( PL, "loop", ( b_loop == TRUE ) );
188
189     REPLY_SEND;
190 }
191
192 DBUS_METHOD( SetRandom )
193 {
194     REPLY_INIT;
195     OUT_ARGUMENTS;
196
197     DBusError error;
198     dbus_bool_t b_random;
199
200     dbus_error_init( &error );
201     dbus_message_get_args( p_from, &error,
202             DBUS_TYPE_BOOLEAN, &b_random,
203             DBUS_TYPE_INVALID );
204
205     if( dbus_error_is_set( &error ) )
206     {
207         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s",
208                 error.message );
209         dbus_error_free( &error );
210         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
211     }
212
213     var_SetBool( PL, "random", ( b_random == TRUE ) );
214
215     REPLY_SEND;
216 }
217
218 /******************************************************************************
219  * TrackListChange: tracklist order / length change signal
220  *****************************************************************************/
221 DBUS_SIGNAL( TrackListChangeSignal )
222 { /* emit the new tracklist lengh */
223     SIGNAL_INIT( DBUS_MPRIS_TRACKLIST_INTERFACE,
224                  DBUS_MPRIS_TRACKLIST_PATH,
225                  "TrackListChange");
226
227     OUT_ARGUMENTS;
228
229     playlist_t *p_playlist = ((intf_thread_t*)p_data)->p_sys->p_playlist;
230     PL_LOCK;
231     dbus_int32_t i_elements = p_playlist->current.i_size;
232     PL_UNLOCK;
233
234     ADD_INT32( &i_elements );
235     SIGNAL_SEND;
236 }
237
238 #define METHOD_FUNC( interface, method, function ) \
239     else if( dbus_message_is_method_call( p_from, interface, method ) )\
240         return function( p_conn, p_from, p_this )
241
242 DBusHandlerResult
243 handle_tracklist ( DBusConnection *p_conn, DBusMessage *p_from, void *p_this )
244 {
245     if(0);
246
247 /*  METHOD_FUNC( DBUS_INTERFACE_PROPERTIES, "Get",    GetProperty );
248     METHOD_FUNC( DBUS_INTERFACE_PROPERTIES, "Set",    SetProperty );
249     METHOD_FUNC( DBUS_INTERFACE_PROPERTIES, "GetAll", GetAllProperties ); */
250
251     /* here D-Bus method names are associated to an handler */
252
253     METHOD_FUNC( DBUS_MPRIS_TRACKLIST_INTERFACE, "GetMetadata",     GetMetadata );
254     METHOD_FUNC( DBUS_MPRIS_TRACKLIST_INTERFACE, "GetCurrentTrack", GetCurrentTrack );
255     METHOD_FUNC( DBUS_MPRIS_TRACKLIST_INTERFACE, "GetLength",       GetLength );
256     METHOD_FUNC( DBUS_MPRIS_TRACKLIST_INTERFACE, "AddTrack",        AddTrack );
257     METHOD_FUNC( DBUS_MPRIS_TRACKLIST_INTERFACE, "DelTrack",        DelTrack );
258     METHOD_FUNC( DBUS_MPRIS_TRACKLIST_INTERFACE, "SetLoop",         SetLoop );
259     METHOD_FUNC( DBUS_MPRIS_TRACKLIST_INTERFACE, "SetRandom",       SetRandom );
260
261     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
262 }
263
264 /*****************************************************************************
265  * TrackListChangeEmit: Emits the TrackListChange signal
266  *****************************************************************************/
267 /* FIXME: It is not called on tracklist reordering */
268 int TrackListChangeEmit( intf_thread_t *p_intf, int signal, int i_node )
269 {
270     // "playlist-item-append"
271     if( signal == SIGNAL_PLAYLIST_ITEM_APPEND )
272     {
273         /* don't signal when items are added/removed in p_category */
274         playlist_t *p_playlist = p_intf->p_sys->p_playlist;
275         PL_LOCK;
276         playlist_item_t *p_item = playlist_ItemGetById( p_playlist, i_node );
277         assert( p_item );
278         while( p_item->p_parent )
279             p_item = p_item->p_parent;
280         if( p_item == p_playlist->p_root_category )
281         {
282             PL_UNLOCK;
283             return VLC_SUCCESS;
284         }
285         PL_UNLOCK;
286     }
287
288     if( p_intf->p_sys->b_dead )
289         return VLC_SUCCESS;
290
291     TrackListChangeSignal( p_intf->p_sys->p_conn, p_intf );
292     return VLC_SUCCESS;
293 }
294
295 #undef METHOD_FUNC