]> git.sesse.net Git - vlc/blob - modules/control/dbus/dbus_root.c
Move intf_EJect() out of core
[vlc] / modules / control / dbus / dbus_root.c
1 /*****************************************************************************
2  * dbus-root.c : dbus control module (mpris v1.0) - root 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_interface.h>
33
34 #include <unistd.h>
35 #include <limits.h>
36
37 #include "dbus_root.h"
38 #include "dbus_common.h"
39
40 static const char const ppsz_supported_uri_schemes[][9] = {
41     "file", "http", "https", "rtsp", "realrtsp", "pnm", "ftp", "mtp", "smb",
42     "mms", "mmsu", "mmst", "mmsh", "unsv", "itpc", "icyx", "rtmp", "rtp",
43     "dccp", "dvd", "vcd", "vcdx"
44 };
45
46 static const char const ppsz_supported_mime_types[][26] = {
47     "audio/mpeg", "audio/x-mpeg",
48     "video/mpeg", "video/x-mpeg",
49     "video/mpeg-system", "video/x-mpeg-system",
50     "video/mp4",
51     "audio/mp4",
52     "video/x-msvideo",
53     "video/quicktime",
54     "application/ogg", "application/x-ogg",
55     "video/x-ms-asf",  "video/x-ms-asf-plugin",
56     "application/x-mplayer2",
57     "video/x-ms-wmv",
58     "video/x-google-vlc-plugin",
59     "audio/wav", "audio/x-wav",
60     "audio/3gpp",
61     "video/3gpp",
62     "audio/3gpp2",
63     "video/3gpp2",
64     "video/divx",
65     "video/flv", "video/x-flv",
66     "video/x-matroska",
67     "audio/x-matroska",
68     "application/xspf+xml"
69 };
70
71 DBUS_METHOD( Identity )
72 {
73     VLC_UNUSED(p_this);
74     REPLY_INIT;
75     OUT_ARGUMENTS;
76
77     const char *psz_identity = _("VLC media player");
78
79     DBusMessageIter v;
80     dbus_message_iter_open_container( &args, DBUS_TYPE_VARIANT, "s", &v );
81     dbus_message_iter_append_basic( &v, DBUS_TYPE_STRING, &psz_identity );
82
83     if( !dbus_message_iter_close_container( &args, &v ) )
84         return DBUS_HANDLER_RESULT_NEED_MEMORY;
85
86     REPLY_SEND;
87 }
88
89 DBUS_METHOD( CanQuit )
90 {
91     VLC_UNUSED( p_this );
92     REPLY_INIT;
93     OUT_ARGUMENTS;
94
95     const dbus_bool_t b_ret = TRUE;
96
97     DBusMessageIter v;
98     dbus_message_iter_open_container( &args, DBUS_TYPE_VARIANT, "b", &v );
99     dbus_message_iter_append_basic( &v, DBUS_TYPE_BOOLEAN, &b_ret );
100
101     if( !dbus_message_iter_close_container( &args, &v ) )
102         return DBUS_HANDLER_RESULT_NEED_MEMORY;
103
104     REPLY_SEND;
105 }
106
107 DBUS_METHOD( CanRaise )
108 {
109     VLC_UNUSED( p_this );
110     REPLY_INIT;
111     OUT_ARGUMENTS;
112
113     const dbus_bool_t b_ret = FALSE;
114
115     DBusMessageIter v;
116     dbus_message_iter_open_container( &args, DBUS_TYPE_VARIANT, "b", &v );
117     dbus_message_iter_append_basic( &v, DBUS_TYPE_BOOLEAN, &b_ret );
118
119     if( !dbus_message_iter_close_container( &args, &v ) )
120         return DBUS_HANDLER_RESULT_NEED_MEMORY;
121
122     REPLY_SEND;
123 }
124
125 DBUS_METHOD( HasTrackList )
126 {
127     VLC_UNUSED( p_this );
128     REPLY_INIT;
129     OUT_ARGUMENTS;
130
131     const dbus_bool_t b_ret = FALSE;
132
133     DBusMessageIter v;
134     dbus_message_iter_open_container( &args, DBUS_TYPE_VARIANT, "b", &v );
135     dbus_message_iter_append_basic( &v, DBUS_TYPE_BOOLEAN, &b_ret );
136
137     if( !dbus_message_iter_close_container( &args, &v ) )
138         return DBUS_HANDLER_RESULT_NEED_MEMORY;
139
140     REPLY_SEND;
141 }
142
143 DBUS_METHOD( DesktopEntry )
144 {
145     VLC_UNUSED( p_this );
146     REPLY_INIT;
147     OUT_ARGUMENTS;
148
149     const char* psz_ret = PACKAGE;
150
151     DBusMessageIter v;
152     dbus_message_iter_open_container( &args, DBUS_TYPE_VARIANT, "s", &v );
153     dbus_message_iter_append_basic( &v, DBUS_TYPE_STRING, &psz_ret );
154
155     if( !dbus_message_iter_close_container( &args, &v ) )
156         return DBUS_HANDLER_RESULT_NEED_MEMORY;
157
158     REPLY_SEND;
159 }
160
161 DBUS_METHOD( SupportedMimeTypes )
162 {
163     VLC_UNUSED( p_this );
164     REPLY_INIT;
165     OUT_ARGUMENTS;
166
167     DBusMessageIter ret, v;
168     dbus_message_iter_open_container( &args, DBUS_TYPE_VARIANT, "s", &v );
169     size_t i_len = sizeof( ppsz_supported_mime_types ) / sizeof( char* );
170
171     if( !dbus_message_iter_open_container( &v, DBUS_TYPE_ARRAY, "s", &ret ) )
172         return DBUS_HANDLER_RESULT_NEED_MEMORY;
173
174     for( size_t i = 0; i < i_len; ++i )
175         if( !dbus_message_iter_append_basic( &ret, DBUS_TYPE_STRING,
176                                              &ppsz_supported_mime_types[i] ) )
177             return DBUS_HANDLER_RESULT_NEED_MEMORY;
178
179     if( !dbus_message_iter_close_container( &v, &ret ) )
180         return DBUS_HANDLER_RESULT_NEED_MEMORY;
181
182     if( !dbus_message_iter_close_container( &args, &v ) )
183         return DBUS_HANDLER_RESULT_NEED_MEMORY;
184
185     REPLY_SEND;
186 }
187
188 DBUS_METHOD( SupportedUriSchemes )
189 {
190     VLC_UNUSED( p_this );
191     REPLY_INIT;
192     OUT_ARGUMENTS;
193
194     DBusMessageIter ret, v;
195     dbus_message_iter_open_container( &args, DBUS_TYPE_VARIANT, "s", &v );
196     size_t i_len = sizeof( ppsz_supported_uri_schemes ) / sizeof( char* );
197
198     if( !dbus_message_iter_open_container( &v, DBUS_TYPE_ARRAY, "s", &ret ) )
199         return DBUS_HANDLER_RESULT_NEED_MEMORY;
200
201     for( size_t i = 0; i < i_len; ++i )
202         if( !dbus_message_iter_append_basic( &ret, DBUS_TYPE_STRING,
203                                              &ppsz_supported_uri_schemes[i] ) )
204             return DBUS_HANDLER_RESULT_NEED_MEMORY;
205
206     if( !dbus_message_iter_close_container( &v, &ret ) )
207         return DBUS_HANDLER_RESULT_NEED_MEMORY;
208
209     if( !dbus_message_iter_close_container( &args, &v ) )
210         return DBUS_HANDLER_RESULT_NEED_MEMORY;
211
212     REPLY_SEND;
213 }
214
215 DBUS_METHOD( Quit )
216 { /* exits vlc */
217     REPLY_INIT;
218     libvlc_Quit(INTF->p_libvlc);
219     REPLY_SEND;
220 }
221
222 DBUS_METHOD( Raise )
223 {/* shows vlc's main window */
224     REPLY_INIT;
225     var_ToggleBool( INTF->p_libvlc, "intf-show" );
226     REPLY_SEND;
227 }
228
229 #define PROPERTY_MAPPING_BEGIN if( 0 ) {}
230 #define PROPERTY_FUNC( interface, property, function ) \
231     else if( !strcmp( psz_interface_name, interface ) && \
232              !strcmp( psz_property_name,  property ) ) \
233         return function( p_conn, p_from, p_this );
234 #define PROPERTY_MAPPING_END return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
235
236 DBUS_METHOD( GetProperty )
237 {
238     DBusError error;
239
240     char *psz_interface_name = NULL;
241     char *psz_property_name  = NULL;
242
243     dbus_error_init( &error );
244     dbus_message_get_args( p_from, &error,
245             DBUS_TYPE_STRING, &psz_interface_name,
246             DBUS_TYPE_STRING, &psz_property_name,
247             DBUS_TYPE_INVALID );
248
249     if( dbus_error_is_set( &error ) )
250     {
251         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s",
252                                          error.message );
253         dbus_error_free( &error );
254         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
255     }
256
257     msg_Dbg( (vlc_object_t*) p_this, "Getting property %s",
258                                      psz_property_name );
259
260     PROPERTY_MAPPING_BEGIN
261     PROPERTY_FUNC( DBUS_MPRIS_ROOT_INTERFACE, "Identity",            Identity )
262     PROPERTY_FUNC( DBUS_MPRIS_ROOT_INTERFACE, "DesktopEntry",        DesktopEntry )
263     PROPERTY_FUNC( DBUS_MPRIS_ROOT_INTERFACE, "SupportedMimeTypes",  SupportedMimeTypes )
264     PROPERTY_FUNC( DBUS_MPRIS_ROOT_INTERFACE, "SupportedUriSchemes", SupportedUriSchemes )
265     PROPERTY_FUNC( DBUS_MPRIS_ROOT_INTERFACE, "HasTrackList",        HasTrackList )
266     PROPERTY_FUNC( DBUS_MPRIS_ROOT_INTERFACE, "CanQuit",             CanQuit )
267     PROPERTY_FUNC( DBUS_MPRIS_ROOT_INTERFACE, "CanRaise",            CanRaise )
268     PROPERTY_MAPPING_END
269 }
270
271 #undef PROPERTY_MAPPING_BEGIN
272 #undef PROPERTY_GET_FUNC
273 #undef PROPERTY_MAPPING_END
274
275 #define METHOD_MAPPING_BEGIN if( 0 ) {}
276 #define METHOD_FUNC( interface, method, function ) \
277     else if( dbus_message_is_method_call( p_from, interface, method ) )\
278         return function( p_conn, p_from, p_this )
279 #define METHOD_MAPPING_END return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
280
281 DBusHandlerResult
282 handle_root ( DBusConnection *p_conn, DBusMessage *p_from, void *p_this )
283 {
284     METHOD_MAPPING_BEGIN
285     METHOD_FUNC( DBUS_INTERFACE_PROPERTIES, "Get",          GetProperty );
286     METHOD_FUNC( DBUS_MPRIS_ROOT_INTERFACE, "Quit",         Quit );
287     METHOD_FUNC( DBUS_MPRIS_ROOT_INTERFACE, "Raise",        Raise );
288     METHOD_MAPPING_END
289 }
290
291 #undef METHOD_MAPPING_BEGIN
292 #undef METHOD_FUNC
293 #undef METHOD_MAPPING_END