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