]> git.sesse.net Git - vlc/blob - modules/misc/notify/telepathy.c
Move text_renderer/ out of misc/
[vlc] / modules / misc / notify / telepathy.c
1 /*****************************************************************************
2  * telepathy.c : changes Telepathy Presence information using MissionControl
3  *****************************************************************************
4  * Copyright © 2007-2009 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_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_interface.h>
35 #include <vlc_playlist.h>
36 #include <vlc_strings.h>                                /* str_format_meta */
37
38 #include <dbus/dbus.h>
39
40 #define FORMAT_DEFAULT "$a - $t"
41 /*****************************************************************************
42  * intf_sys_t: description and status of log interface
43  *****************************************************************************/
44 struct intf_sys_t
45 {
46     char            *psz_format;
47     DBusConnection  *p_conn;
48     int             i_id;
49     int             i_item_changes;
50 };
51
52 /*****************************************************************************
53  * Local prototypes
54  *****************************************************************************/
55 static int  Open    ( vlc_object_t * );
56 static void Close   ( vlc_object_t * );
57
58 static int ItemChange( vlc_object_t *, const char *,
59                        vlc_value_t, vlc_value_t, void * );
60 static int StateChange( vlc_object_t *, const char *,
61                         vlc_value_t, vlc_value_t, void * );
62 static int SendToTelepathy( intf_thread_t *, const char * );
63
64 /*****************************************************************************
65  * Module descriptor
66  *****************************************************************************/
67 vlc_module_begin ()
68     set_category( CAT_INTERFACE )
69     set_subcategory( SUBCAT_INTERFACE_CONTROL )
70     set_shortname( "Telepathy" )
71     set_description( N_("Telepathy \"Now Playing\" (MissionControl)") )
72
73     add_obsolete_string( "telepathy-format")
74
75     set_capability( "interface", 0 )
76     set_callbacks( Open, Close )
77 vlc_module_end ()
78
79 /*****************************************************************************
80  * Open: initialize and create stuff
81  *****************************************************************************/
82 static int Open( vlc_object_t *p_this )
83 {
84     intf_thread_t *p_intf = (intf_thread_t *)p_this;
85     playlist_t *p_playlist;
86     DBusError       error;
87
88     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
89     if( !p_intf->p_sys )
90         return VLC_ENOMEM;
91
92     /* connect to the session bus */
93     dbus_error_init( &error );
94     p_intf->p_sys->p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
95     if( !p_intf->p_sys->p_conn )
96     {
97         msg_Err( p_this, "Failed to connect to the DBus session daemon: %s",
98                 error.message );
99         dbus_error_free( &error );
100         free( p_intf->p_sys );
101         return VLC_EGENERIC;
102     }
103
104     p_intf->p_sys->psz_format = var_InheritString( p_intf, "input-title-format" );
105     if( !p_intf->p_sys->psz_format )
106     {
107         p_intf->p_sys->psz_format = strdup( FORMAT_DEFAULT );
108     }
109     msg_Dbg( p_intf, "using format: %s", p_intf->p_sys->psz_format );
110
111     p_intf->p_sys->i_id = -1;
112
113     p_playlist = pl_Get( p_intf );
114     var_AddCallback( p_playlist, "item-change", ItemChange, p_intf );
115     var_AddCallback( p_playlist, "item-current", ItemChange, p_intf );
116
117     return VLC_SUCCESS;
118 }
119
120 /*****************************************************************************
121  * Close: destroy interface stuff
122  *****************************************************************************/
123 static void Close( vlc_object_t *p_this )
124 {
125     intf_thread_t *p_intf = (intf_thread_t *)p_this;
126     playlist_t *p_playlist = pl_Get( p_this );
127     input_thread_t *p_input = NULL;
128
129     var_DelCallback( p_playlist, "item-change", ItemChange, p_intf );
130     var_DelCallback( p_playlist, "item-current", ItemChange, p_intf );
131     if( (p_input = playlist_CurrentInput( p_playlist )) )
132     {
133         var_DelCallback( p_input, "state", StateChange, p_intf );
134         vlc_object_release( p_input );
135     }
136
137     /* Clears the Presence message ... else it looks like we're still playing
138      * something although VLC (or the Telepathy plugin) is closed */
139
140     /* Do not check for VLC_ENOMEM as we're closing */
141     SendToTelepathy( p_intf, "" );
142
143     /* we won't use the DBus connection anymore */
144     dbus_connection_unref( p_intf->p_sys->p_conn );
145
146     /* Destroy structure */
147     free( p_intf->p_sys->psz_format );
148     free( p_intf->p_sys );
149 }
150
151 /*****************************************************************************
152  * ItemChange: Playlist item change callback
153  *****************************************************************************/
154 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
155                        vlc_value_t oldval, vlc_value_t newval, void *param )
156 {
157     VLC_UNUSED(oldval);
158     intf_thread_t *p_intf = (intf_thread_t *)param;
159     playlist_t* p_playlist = (playlist_t*) p_this;
160     char *psz_buf = NULL;
161     input_thread_t *p_input;
162     input_item_t *p_item = newval.p_address;
163     bool b_is_item_current = !strcmp( "item-current", psz_var );
164
165     /* Don't update Telepathy presence each time an item has been preparsed */
166     if( b_is_item_current )
167     { /* stores the current input item id */
168         p_intf->p_sys->i_id = p_item->i_id;
169         p_intf->p_sys->i_item_changes = 0;
170     }
171     else
172     {
173
174         if( p_item->i_id != p_intf->p_sys->i_id ) /* "item-change" */
175             return VLC_SUCCESS;
176         /* Some variable bitrate inputs call "item-change callbacks each time
177          * their length is updated, that is several times per second.
178          * We'll limit the number of changes to 10 per input. */
179         if( p_intf->p_sys->i_item_changes > 10 )
180             return VLC_SUCCESS;
181         p_intf->p_sys->i_item_changes++;
182     }
183
184     p_input = playlist_CurrentInput( p_playlist );
185
186     if( !p_input ) return VLC_SUCCESS;
187
188     if( p_input->b_dead || !input_GetItem(p_input)->psz_name )
189     {
190         vlc_object_release( p_input );
191         /* Not playing anything ... */
192         switch( SendToTelepathy( p_intf, "" ) )
193         {
194             case VLC_ENOMEM:
195                 return VLC_ENOMEM;
196             default:
197                 return VLC_SUCCESS;
198         }
199     }
200
201     if( b_is_item_current )
202         var_AddCallback( p_input, "state", StateChange, p_intf );
203
204     /* We format the string to be displayed */
205     psz_buf = str_format_meta( (vlc_object_t*) p_intf,
206             p_intf->p_sys->psz_format );
207
208     /* We don't need the input anymore */
209     vlc_object_release( p_input );
210
211     if( SendToTelepathy( p_intf, psz_buf ) == VLC_ENOMEM )
212     {
213         free( psz_buf );
214         return VLC_ENOMEM;
215     }
216     free( psz_buf );
217     return VLC_SUCCESS;
218 }
219
220 /*****************************************************************************
221  * StateChange: State change callback
222  *****************************************************************************/
223 static int StateChange( vlc_object_t *p_this, const char *psz_var,
224                        vlc_value_t oldval, vlc_value_t newval, void *param )
225 {
226     VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
227     intf_thread_t *p_intf = (intf_thread_t *)param;
228     if( newval.i_int >= END_S )
229         return SendToTelepathy( p_intf, "" );
230     return VLC_SUCCESS;
231 }
232
233 /*****************************************************************************
234  * SendToTelepathy
235  *****************************************************************************/
236 static int SendToTelepathy( intf_thread_t *p_intf, const char *psz_msg )
237 {
238     DBusConnection *p_conn;
239     DBusMessage *p_msg;
240     DBusMessage *p_reply;
241     DBusMessageIter args;
242     DBusError error;
243     dbus_error_init( &error );
244     dbus_uint32_t i_status;
245
246     p_conn = p_intf->p_sys->p_conn;
247
248     /* first we need to get the actual status */
249     p_msg = dbus_message_new_method_call(
250             "org.freedesktop.Telepathy.MissionControl",
251            "/org/freedesktop/Telepathy/MissionControl",
252             "org.freedesktop.Telepathy.MissionControl",
253             "GetPresence" );
254     if( !p_msg )
255         return VLC_ENOMEM;
256
257     p_reply = dbus_connection_send_with_reply_and_block( p_conn, p_msg,
258         50, &error ); /* blocks 50ms maximum */
259
260     if( dbus_error_is_set( &error ) )
261         dbus_error_free( &error );
262
263     dbus_message_unref( p_msg );
264     if( p_reply == NULL )
265     {   /* MC is not active, or too slow. Better luck next time? */
266         return VLC_SUCCESS;
267     }
268
269     /* extract the status from the reply */
270     if( dbus_message_get_args( p_reply, &error,
271             DBUS_TYPE_UINT32, &i_status,
272             DBUS_TYPE_INVALID ) == FALSE )
273     {
274         return VLC_ENOMEM;
275     }
276
277     p_msg = dbus_message_new_method_call(
278             "org.freedesktop.Telepathy.MissionControl",
279            "/org/freedesktop/Telepathy/MissionControl",
280             "org.freedesktop.Telepathy.MissionControl",
281             "SetPresence" );
282     if( !p_msg )
283         return VLC_ENOMEM;
284
285     dbus_message_iter_init_append( p_msg, &args );
286
287     /* first argument is the status */
288     if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_UINT32, &i_status ) )
289     {
290         dbus_message_unref( p_msg );
291         return VLC_ENOMEM;
292     }
293     /* second argument is the message */
294     if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_STRING, &psz_msg ) )
295     {
296         dbus_message_unref( p_msg );
297         return VLC_ENOMEM;
298     }
299
300
301     if( !dbus_connection_send( p_conn, p_msg, NULL ) )
302         return VLC_ENOMEM;
303
304     dbus_connection_flush( p_conn );
305     dbus_message_unref( p_msg );
306
307     return VLC_SUCCESS;
308 }