]> git.sesse.net Git - vlc/blob - modules/misc/notify/telepathy.c
Merge branch 'master' of git@git.videolan.org:vlc
[vlc] / modules / misc / notify / telepathy.c
1 /*****************************************************************************
2  * telepathy.c : changes Telepathy Presence information using MissionControl
3  *****************************************************************************
4  * Copyright © 2007 the VideoLAN team
5  * $Id$
6  *
7  * Author: Rafaël Carré <funman@videoanorg>
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  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc/vlc.h>
33 #include <vlc_interface.h>
34 #include <vlc_meta.h>
35 #include <vlc_playlist.h>
36 #include <vlc_strings.h>
37 #include <dbus/dbus.h>
38
39 /*****************************************************************************
40  * intf_sys_t: description and status of log interface
41  *****************************************************************************/
42 struct intf_sys_t
43 {
44     char            *psz_format;
45     DBusConnection  *p_conn;
46     int             i_id;
47     int             i_item_changes;
48 };
49
50 /*****************************************************************************
51  * Local prototypes
52  *****************************************************************************/
53 static int  Open    ( vlc_object_t * );
54 static void Close   ( vlc_object_t * );
55
56 static int ItemChange( vlc_object_t *, const char *,
57                        vlc_value_t, vlc_value_t, void * );
58 static int StateChange( vlc_object_t *, const char *,
59                         vlc_value_t, vlc_value_t, void * );
60 static int SendToTelepathy( intf_thread_t *, const char * );
61
62 /*****************************************************************************
63  * Module descriptor
64  *****************************************************************************/
65 #define FORMAT_DEFAULT "$a - $t"
66 #define FORMAT_TEXT N_("Title format string")
67 #define FORMAT_LONGTEXT N_("Format of the string to send to Telepathy." \
68 "Defaults to \"Artist - Title\" ($a - $t). " \
69 "You can use the following substitutions: " \
70 "$a Artist, $b Album, $c Copyright, $d Description, $e Encoder, $g Genre, " \
71 "$l Language, $n number, $p Now Playing, $r Rating, $s Subtitles language, " \
72 "$t Title, $u URL, $A Date, $B Bitrate, $C Chapter, $D Duration, $F URI, " \
73 "$I Video Title, $L Time Remaining, $N Name, $O Audio language, $P Position, " \
74 "$R Rate, $S Sample rate, $T Time elapsed, $U Publisher, $V Volume")
75
76 vlc_module_begin();
77     set_category( CAT_INTERFACE );
78     set_subcategory( SUBCAT_INTERFACE_CONTROL );
79     set_shortname( "Telepathy" );
80     set_description( _("Telepathy \"Now Playing\" using MissionControl") );
81
82     add_string( "telepathy-format", FORMAT_DEFAULT, NULL,
83                 FORMAT_TEXT, FORMAT_LONGTEXT, false );
84
85     set_capability( "interface", 0 );
86     set_callbacks( Open, Close );
87 vlc_module_end();
88
89 /*****************************************************************************
90  * Open: initialize and create stuff
91  *****************************************************************************/
92 static int Open( vlc_object_t *p_this )
93 {
94     intf_thread_t *p_intf = (intf_thread_t *)p_this;
95     playlist_t *p_playlist;
96     DBusConnection  *p_conn;
97     DBusError       error;
98
99     MALLOC_ERR( p_intf->p_sys, intf_sys_t );
100
101     /* connect to the session bus */
102     dbus_error_init( &error );
103     p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
104     if( !p_conn )
105     {
106         msg_Err( p_this, "Failed to connect to the DBus session daemon: %s",
107                 error.message );
108         dbus_error_free( &error );
109         free( p_intf->p_sys );
110         return VLC_EGENERIC;
111     }
112     p_intf->p_sys->p_conn = p_conn;
113
114     p_intf->p_sys->psz_format = config_GetPsz( p_intf, "telepathy-format" );
115     if( !p_intf->p_sys->psz_format )
116     {
117         msg_Dbg( p_intf, "no format provided" );
118         p_intf->p_sys->psz_format = strdup( FORMAT_DEFAULT );
119     }
120     msg_Dbg( p_intf, "using format: %s", p_intf->p_sys->psz_format );
121
122     p_intf->p_sys->i_id = -1;
123
124     p_playlist = pl_Yield( p_intf );
125     var_AddCallback( p_playlist, "item-change", ItemChange, p_intf );
126     var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
127     pl_Release( p_intf );
128
129     return VLC_SUCCESS;
130 }
131
132 /*****************************************************************************
133  * Close: destroy interface stuff
134  *****************************************************************************/
135 static void Close( vlc_object_t *p_this )
136 {
137     p_this->b_dead = true;
138     intf_thread_t *p_intf = (intf_thread_t *)p_this;
139     playlist_t *p_playlist = pl_Yield( p_this );
140
141     /* Clears the Presence message ... else it looks like we're still playing
142      * something although VLC (or the Telepathy plugin) is closed */
143
144     /* Do not check for VLC_ENOMEM as we're closing */
145     SendToTelepathy( p_intf, "" );
146
147     PL_LOCK;
148     var_DelCallback( p_playlist, "item-change", ItemChange, p_intf );
149     var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
150     if( p_playlist->p_input )
151         var_DelCallback( p_playlist->p_input, "state", StateChange, p_intf );
152     PL_UNLOCK;
153     pl_Release( p_this );
154
155     /* we won't use the DBus connection anymore */
156     dbus_connection_unref( p_intf->p_sys->p_conn );
157
158     /* Destroy structure */
159     free( p_intf->p_sys->psz_format );
160     free( p_intf->p_sys );
161 }
162
163 /*****************************************************************************
164  * ItemChange: Playlist item change callback
165  *****************************************************************************/
166 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
167                        vlc_value_t oldval, vlc_value_t newval, void *param )
168 {
169     VLC_UNUSED(oldval);
170     intf_thread_t *p_intf = (intf_thread_t *)param;
171     char *psz_buf = NULL;
172     input_thread_t *p_input;
173
174     if( p_intf->b_dead )
175         return VLC_EGENERIC;
176
177     /* Don't update Telepathy presence each time an item has been preparsed */
178     if( !strncmp( "playlist-current", psz_var, 16 ) )
179     { /* stores the current input item id */
180         p_intf->p_sys->i_id = newval.i_int;
181         p_intf->p_sys->i_item_changes = 0;
182     }
183     else
184     {
185         if( newval.i_int != p_intf->p_sys->i_id ) /* "item-change" */
186             return VLC_SUCCESS;
187         /* Some variable bitrate inputs call "item-change callbacks each time
188          * their length is updated, that is several times per second.
189          * We'll limit the number of changes to 10 per input. */
190         if( p_intf->p_sys->i_item_changes > 10 )
191             return VLC_SUCCESS;
192         p_intf->p_sys->i_item_changes++;
193     }
194
195
196     playlist_t *p_playlist = pl_Yield( p_this );
197
198     p_input = p_playlist->p_input;
199     pl_Release( p_this );
200
201     if( !p_input ) return VLC_SUCCESS;
202     vlc_object_yield( p_input );
203
204     if( p_input->b_dead || !input_GetItem(p_input)->psz_name )
205     {
206         vlc_object_release( p_input );
207         /* Not playing anything ... */
208         switch( SendToTelepathy( p_intf, "" ) )
209         {
210             case VLC_ENOMEM:
211                 return VLC_ENOMEM;
212             default:
213                 return VLC_SUCCESS;
214         }
215     }
216
217     if( !strncmp( "playlist-current", psz_var, 16 ) )
218         var_AddCallback( p_input, "state", StateChange, p_intf );
219
220     /* We format the string to be displayed */
221     psz_buf = str_format_meta( p_this, p_intf->p_sys->psz_format );
222
223     /* We don't need the input anymore */
224     vlc_object_release( p_input );
225
226     if( SendToTelepathy( p_intf, psz_buf ) == VLC_ENOMEM )
227     {
228         free( psz_buf );
229         return VLC_ENOMEM;
230     }
231     free( psz_buf );
232     return VLC_SUCCESS;
233 }
234
235 /*****************************************************************************
236  * StateChange: State change callback
237  *****************************************************************************/
238 static int StateChange( vlc_object_t *p_this, const char *psz_var,
239                        vlc_value_t oldval, vlc_value_t newval, void *param )
240 {
241     VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
242     intf_thread_t *p_intf = (intf_thread_t *)param;
243     if( p_intf->b_dead )
244         return VLC_EGENERIC;
245     if( newval.i_int >= END_S )
246         return SendToTelepathy( p_intf, "" );
247     return VLC_SUCCESS;
248 }
249
250 /*****************************************************************************
251  * SendToTelepathy
252  *****************************************************************************/
253 static int SendToTelepathy( intf_thread_t *p_intf, const char *psz_msg )
254 {
255     DBusConnection *p_conn;
256     DBusMessage *p_msg;
257     DBusMessage *p_reply;
258     DBusMessageIter args;
259     DBusError error;
260     dbus_error_init( &error );
261     dbus_uint32_t i_status;
262
263     p_conn = p_intf->p_sys->p_conn;
264
265     /* first we need to get the actual status */
266     p_msg = dbus_message_new_method_call(
267             "org.freedesktop.Telepathy.MissionControl",
268            "/org/freedesktop/Telepathy/MissionControl",
269             "org.freedesktop.Telepathy.MissionControl",
270             "GetPresence" );
271     if( !p_msg )
272         return VLC_ENOMEM;
273
274     p_reply = dbus_connection_send_with_reply_and_block( p_conn, p_msg,
275         50, &error ); /* blocks 50ms maximum */
276
277     dbus_message_unref( p_msg );
278     if( p_reply == NULL )
279     {   /* MC is not active, or too slow. Better luck next time? */
280         return VLC_SUCCESS;
281     }
282
283     /* extract the status from the reply */
284     if( dbus_message_get_args( p_reply, &error,
285             DBUS_TYPE_UINT32, &i_status,
286             DBUS_TYPE_INVALID ) == FALSE )
287     {
288         return VLC_ENOMEM;
289     }
290
291     p_msg = dbus_message_new_method_call(
292             "org.freedesktop.Telepathy.MissionControl",
293            "/org/freedesktop/Telepathy/MissionControl",
294             "org.freedesktop.Telepathy.MissionControl",
295             "SetPresence" );
296     if( !p_msg )
297         return VLC_ENOMEM;
298
299     dbus_message_iter_init_append( p_msg, &args );
300
301     /* first argument is the status */
302     if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_UINT32, &i_status ) )
303     {
304         dbus_message_unref( p_msg );
305         return VLC_ENOMEM;
306     }
307     /* second argument is the message */
308     if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_STRING, &psz_msg ) )
309     {
310         dbus_message_unref( p_msg );
311         return VLC_ENOMEM;
312     }
313
314
315     if( !dbus_connection_send( p_conn, p_msg, NULL ) )
316         return VLC_ENOMEM;
317
318     dbus_connection_flush( p_conn );
319     dbus_message_unref( p_msg );
320
321     return VLC_SUCCESS;
322 }