]> git.sesse.net Git - vlc/blob - modules/control/dbus.h
dbus: simplify playlist operations
[vlc] / modules / control / dbus.h
1 /*****************************************************************************
2  * dbus.h : D-Bus control interface
3  *****************************************************************************
4  * Copyright (C) 2006 Rafaël Carré
5  * $Id$
6  *
7  * Authors:    Rafaël Carré <funman at videolanorg>
8  *             Mirsal ENNAIME <mirsal dot ennaime at gmail dot com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /* DBUS IDENTIFIERS */
26
27 /* name registered on the session bus */
28 #define VLC_MPRIS_DBUS_SERVICE      "org.mpris.vlc"
29 #define MPRIS_DBUS_INTERFACE        "org.freedesktop.MediaPlayer"
30 #define MPRIS_DBUS_ROOT_PATH        "/"
31 #define MPRIS_DBUS_PLAYER_PATH      "/Player"
32 #define MPRIS_DBUS_TRACKLIST_PATH   "/TrackList"
33
34 /* MACROS */
35
36 /* DBus related */
37 #define DBUS_METHOD( method_function ) \
38     static DBusHandlerResult method_function \
39             ( DBusConnection *p_conn, DBusMessage *p_from, void *p_this )
40
41 #define DBUS_SIGNAL( signal_function ) \
42     static DBusHandlerResult signal_function \
43             ( DBusConnection *p_conn, void *p_data )
44
45 #define REPLY_INIT \
46     DBusMessage* p_msg = dbus_message_new_method_return( p_from ); \
47     if( !p_msg ) return DBUS_HANDLER_RESULT_NEED_MEMORY; \
48
49 #define REPLY_SEND \
50     if( !dbus_connection_send( p_conn, p_msg, NULL ) ) \
51         return DBUS_HANDLER_RESULT_NEED_MEMORY; \
52     dbus_connection_flush( p_conn ); \
53     dbus_message_unref( p_msg ); \
54     return DBUS_HANDLER_RESULT_HANDLED
55
56 #define SIGNAL_INIT( signal ) \
57     DBusMessage *p_msg = dbus_message_new_signal( MPRIS_DBUS_PLAYER_PATH, \
58         MPRIS_DBUS_INTERFACE, signal ); \
59     if( !p_msg ) return DBUS_HANDLER_RESULT_NEED_MEMORY; \
60
61 #define SIGNAL_SEND \
62     if( !dbus_connection_send( p_conn, p_msg, NULL ) ) \
63         return DBUS_HANDLER_RESULT_NEED_MEMORY; \
64     dbus_message_unref( p_msg ); \
65     dbus_connection_flush( p_conn ); \
66     return DBUS_HANDLER_RESULT_HANDLED
67
68 #define OUT_ARGUMENTS \
69     DBusMessageIter args; \
70     dbus_message_iter_init_append( p_msg, &args )
71
72 #define DBUS_ADD( dbus_type, value ) \
73     if( !dbus_message_iter_append_basic( &args, dbus_type, value ) ) \
74         return DBUS_HANDLER_RESULT_NEED_MEMORY
75
76 #define ADD_STRING( s ) DBUS_ADD( DBUS_TYPE_STRING, s )
77 #define ADD_BOOL( b ) DBUS_ADD( DBUS_TYPE_BOOLEAN, b )
78 #define ADD_INT32( i ) DBUS_ADD( DBUS_TYPE_INT32, i )
79 #define ADD_BYTE( b ) DBUS_ADD( DBUS_TYPE_BYTE, b )
80
81 /* XML data to answer org.freedesktop.DBus.Introspectable.Introspect requests */
82
83 const char* psz_introspection_xml_data_root =
84 "<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
85 "\"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
86 "<node>\n"
87 "  <node name=\"Player\"/>\n"
88 "  <node name=\"TrackList\"/>\n"
89 "  <interface name=\"org.freedesktop.DBus.Introspectable\">\n"
90 "    <method name=\"Introspect\">\n"
91 "      <arg name=\"data\" direction=\"out\" type=\"s\"/>\n"
92 "    </method>\n"
93 "  </interface>\n"
94 "  <interface name=\"org.freedesktop.MediaPlayer\">\n"
95 "    <method name=\"Identity\">\n"
96 "      <arg type=\"s\" direction=\"out\" />\n"
97 "    </method>\n"
98 "  </interface>\n"
99 "</node>\n"
100 ;
101
102 const char* psz_introspection_xml_data_player =
103 "<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
104 "\"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
105 "<node>"
106 "  <interface name=\"org.freedesktop.DBus.Introspectable\">\n"
107 "    <method name=\"Introspect\">\n"
108 "      <arg name=\"data\" direction=\"out\" type=\"s\"/>\n"
109 "    </method>\n"
110 "  </interface>\n"
111 "  <interface name=\"org.freedesktop.MediaPlayer\">\n"
112 "    <method name=\"GetStatus\">\n"
113 "      <arg type=\"i\" direction=\"out\" />\n"
114 "    </method>\n"
115 "    <method name=\"Quit\">\n"
116 "    </method>\n"
117 "    <method name=\"Prev\">\n"
118 "    </method>\n"
119 "    <method name=\"Next\">\n"
120 "    </method>\n"
121 "    <method name=\"Stop\">\n"
122 "    </method>\n"
123 "    <method name=\"Play\">\n"
124 "    </method>\n"
125 "    <method name=\"Pause\">\n"
126 "    </method>\n"
127 "    <method name=\"Repeat\">\n"
128 "      <arg type=\"b\" direction=\"in\" />\n"
129 "    </method>\n"
130 "    <method name=\"VolumeSet\">\n"
131 "      <arg type=\"i\" direction=\"in\" />\n"
132 "    </method>\n"
133 "    <method name=\"VolumeGet\">\n"
134 "      <arg type=\"i\" direction=\"out\" />\n"
135 "    </method>\n"
136 "    <method name=\"PositionSet\">\n"
137 "      <arg type=\"i\" direction=\"in\" />\n"
138 "    </method>\n"
139 "    <method name=\"PositionGet\">\n"
140 "      <arg type=\"i\" direction=\"out\" />\n"
141 "    </method>\n"
142 "    <method name=\"GetMetadata\">\n"
143 "      <arg type=\"a{sv}\" direction=\"out\" />\n"
144 "    </method>\n"
145 "    <signal name=\"TrackChange\">\n"
146 "      <arg type=\"a{sv}\"/>\n"
147 "    </signal>\n"
148 "    <signal name=\"StatusChange\">\n"
149 "      <arg type=\"i\"/>\n"
150 "    </signal>\n"
151 "    <signal name=\"CapsChange\">\n"
152 "      <arg type=\"i\"/>\n"
153 "    </signal>\n"
154 "  </interface>\n"
155 "</node>\n"
156 ;
157
158 const char* psz_introspection_xml_data_tracklist =
159 "<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
160 "\"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
161 "<node>"
162 "  <interface name=\"org.freedesktop.DBus.Introspectable\">\n"
163 "    <method name=\"Introspect\">\n"
164 "      <arg name=\"data\" direction=\"out\" type=\"s\"/>\n"
165 "    </method>\n"
166 "  </interface>\n"
167 "  <interface name=\"org.freedesktop.MediaPlayer\">\n"
168 "    <method name=\"AddTrack\">\n"
169 "      <arg type=\"s\" direction=\"in\" />\n"
170 "      <arg type=\"b\" direction=\"in\" />\n"
171 "    </method>\n"
172 "    <method name=\"DelTrack\">\n"
173 "      <arg type=\"i\" direction=\"in\" />\n"
174 "    </method>\n"
175 "    <method name=\"GetMetadata\">\n"
176 "      <arg type=\"i\" direction=\"in\" />\n"
177 "      <arg type=\"a{sv}\" direction=\"out\" />\n"
178 "    </method>\n"
179 "    <method name=\"GetCurrentTrack\">\n"
180 "      <arg type=\"i\" direction=\"out\" />\n"
181 "    </method>\n"
182 "    <method name=\"GetLength\">\n"
183 "      <arg type=\"i\" direction=\"out\" />\n"
184 "    </method>\n"
185 "    <method name=\"Loop\">\n"
186 "      <arg type=\"b\" direction=\"in\" />\n"
187 "    </method>\n"
188 "    <method name=\"Random\">\n"
189 "      <arg type=\"b\" direction=\"in\" />\n"
190 "    </method>\n"
191 "  </interface>\n"
192 "</node>\n"
193 ;
194
195 #define MPRIS_DBUS_ROOT_PATH      "/"
196 #define MPRIS_DBUS_PLAYER_PATH    "/Player"
197 #define MPRIS_DBUS_TRACKLIST_PATH "/TrackList"
198
199 /* Handle  messages reception */
200 DBUS_METHOD( handle_root );
201 DBUS_METHOD( handle_player );
202 DBUS_METHOD( handle_tracklist );
203
204 static DBusObjectPathVTable vlc_dbus_root_vtable = {
205         NULL, handle_root, /* handler function */
206         NULL, NULL, NULL, NULL
207 };
208
209 static DBusObjectPathVTable vlc_dbus_player_vtable = {
210         NULL, handle_player, /* handler function */
211         NULL, NULL, NULL, NULL
212 };
213
214 static DBusObjectPathVTable vlc_dbus_tracklist_vtable = {
215         NULL, handle_tracklist, /* handler function */
216         NULL, NULL, NULL, NULL
217 };
218