File: telepathy.c

package info (click to toggle)
vlc 1.1.3-1squeeze6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 134,496 kB
  • ctags: 53,762
  • sloc: ansic: 341,893; cpp: 80,372; objc: 23,171; sh: 12,102; makefile: 5,035; xml: 1,351; asm: 524; python: 257; perl: 76; sed: 16
file content (317 lines) | stat: -rw-r--r-- 11,254 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*****************************************************************************
 * telepathy.c : changes Telepathy Presence information using MissionControl
 *****************************************************************************
 * Copyright © 2007-2009 the VideoLAN team
 * $Id: 4f538c858116f34c24b455d1b13d5364d389473b $
 *
 * Author: Rafaël Carré <funman@videoanorg>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_interface.h>
#include <vlc_playlist.h>
#include <vlc_strings.h>

#include <dbus/dbus.h>

/*****************************************************************************
 * intf_sys_t: description and status of log interface
 *****************************************************************************/
struct intf_sys_t
{
    char            *psz_format;
    DBusConnection  *p_conn;
    int             i_id;
    int             i_item_changes;
};

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  Open    ( vlc_object_t * );
static void Close   ( vlc_object_t * );

static int ItemChange( vlc_object_t *, const char *,
                       vlc_value_t, vlc_value_t, void * );
static int StateChange( vlc_object_t *, const char *,
                        vlc_value_t, vlc_value_t, void * );
static int SendToTelepathy( intf_thread_t *, const char * );

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
#define FORMAT_DEFAULT "$a - $t"
#define FORMAT_TEXT N_("Title format string")
#define FORMAT_LONGTEXT N_("Format of the string to send to Telepathy." \
"Defaults to \"Artist - Title\" ($a - $t). " \
"You can use the following substitutions: " \
"$a Artist, $b Album, $c Copyright, $d Description, $e Encoder, $g Genre, " \
"$l Language, $n number, $p Now Playing, $r Rating, $s Subtitles language, " \
"$t Title, $u URL, $A Date, $B Bitrate, $C Chapter, $D Duration, $F URI, " \
"$I Video Title, $L Time Remaining, $N Name, $O Audio language, $P Position, " \
"$R Rate, $S Sample rate, $T Time elapsed, $U Publisher, $V Volume")

vlc_module_begin ()
    set_category( CAT_INTERFACE )
    set_subcategory( SUBCAT_INTERFACE_CONTROL )
    set_shortname( "Telepathy" )
    set_description( N_("Telepathy \"Now Playing\" (MissionControl)") )

    add_string( "telepathy-format", FORMAT_DEFAULT, NULL,
                FORMAT_TEXT, FORMAT_LONGTEXT, false )

    set_capability( "interface", 0 )
    set_callbacks( Open, Close )
vlc_module_end ()

/*****************************************************************************
 * Open: initialize and create stuff
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    intf_thread_t *p_intf = (intf_thread_t *)p_this;
    playlist_t *p_playlist;
    DBusError       error;

    p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
    if( !p_intf->p_sys )
        return VLC_ENOMEM;

    /* connect to the session bus */
    dbus_error_init( &error );
    p_intf->p_sys->p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
    if( !p_intf->p_sys->p_conn )
    {
        msg_Err( p_this, "Failed to connect to the DBus session daemon: %s",
                error.message );
        dbus_error_free( &error );
        free( p_intf->p_sys );
        return VLC_EGENERIC;
    }

    p_intf->p_sys->psz_format = var_InheritString( p_intf, "telepathy-format" );
    if( !p_intf->p_sys->psz_format )
    {
        msg_Dbg( p_intf, "no format provided" );
        p_intf->p_sys->psz_format = strdup( FORMAT_DEFAULT );
    }
    msg_Dbg( p_intf, "using format: %s", p_intf->p_sys->psz_format );

    p_intf->p_sys->i_id = -1;

    p_playlist = pl_Get( p_intf );
    var_AddCallback( p_playlist, "item-change", ItemChange, p_intf );
    var_AddCallback( p_playlist, "item-current", ItemChange, p_intf );

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close: destroy interface stuff
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
    intf_thread_t *p_intf = (intf_thread_t *)p_this;
    playlist_t *p_playlist = pl_Get( p_this );
    input_thread_t *p_input = NULL;

    var_DelCallback( p_playlist, "item-change", ItemChange, p_intf );
    var_DelCallback( p_playlist, "item-current", ItemChange, p_intf );
    if( (p_input = playlist_CurrentInput( p_playlist )) )
    {
        var_DelCallback( p_input, "state", StateChange, p_intf );
        vlc_object_release( p_input );
    }

    /* Clears the Presence message ... else it looks like we're still playing
     * something although VLC (or the Telepathy plugin) is closed */

    /* Do not check for VLC_ENOMEM as we're closing */
    SendToTelepathy( p_intf, "" );

    /* we won't use the DBus connection anymore */
    dbus_connection_unref( p_intf->p_sys->p_conn );

    /* Destroy structure */
    free( p_intf->p_sys->psz_format );
    free( p_intf->p_sys );
}

/*****************************************************************************
 * ItemChange: Playlist item change callback
 *****************************************************************************/
static int ItemChange( vlc_object_t *p_this, const char *psz_var,
                       vlc_value_t oldval, vlc_value_t newval, void *param )
{
    VLC_UNUSED(oldval);
    intf_thread_t *p_intf = (intf_thread_t *)param;
    playlist_t* p_playlist = (playlist_t*) p_this;
    char *psz_buf = NULL;
    input_thread_t *p_input;
    input_item_t *p_item = newval.p_address;
    bool b_is_item_current = !strcmp( "item-current", psz_var );

    /* Don't update Telepathy presence each time an item has been preparsed */
    if( b_is_item_current )
    { /* stores the current input item id */
        p_intf->p_sys->i_id = p_item->i_id;
        p_intf->p_sys->i_item_changes = 0;
    }
    else
    {

        if( p_item->i_id != p_intf->p_sys->i_id ) /* "item-change" */
            return VLC_SUCCESS;
        /* Some variable bitrate inputs call "item-change callbacks each time
         * their length is updated, that is several times per second.
         * We'll limit the number of changes to 10 per input. */
        if( p_intf->p_sys->i_item_changes > 10 )
            return VLC_SUCCESS;
        p_intf->p_sys->i_item_changes++;
    }

    p_input = playlist_CurrentInput( p_playlist );

    if( !p_input ) return VLC_SUCCESS;

    if( p_input->b_dead || !input_GetItem(p_input)->psz_name )
    {
        vlc_object_release( p_input );
        /* Not playing anything ... */
        switch( SendToTelepathy( p_intf, "" ) )
        {
            case VLC_ENOMEM:
                return VLC_ENOMEM;
            default:
                return VLC_SUCCESS;
        }
    }

    if( b_is_item_current )
        var_AddCallback( p_input, "state", StateChange, p_intf );

    /* We format the string to be displayed */
    psz_buf = str_format_meta( (vlc_object_t*) p_intf,
            p_intf->p_sys->psz_format );

    /* We don't need the input anymore */
    vlc_object_release( p_input );

    if( SendToTelepathy( p_intf, psz_buf ) == VLC_ENOMEM )
    {
        free( psz_buf );
        return VLC_ENOMEM;
    }
    free( psz_buf );
    return VLC_SUCCESS;
}

/*****************************************************************************
 * StateChange: State change callback
 *****************************************************************************/
static int StateChange( vlc_object_t *p_this, const char *psz_var,
                       vlc_value_t oldval, vlc_value_t newval, void *param )
{
    VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
    intf_thread_t *p_intf = (intf_thread_t *)param;
    if( newval.i_int >= END_S )
        return SendToTelepathy( p_intf, "" );
    return VLC_SUCCESS;
}

/*****************************************************************************
 * SendToTelepathy
 *****************************************************************************/
static int SendToTelepathy( intf_thread_t *p_intf, const char *psz_msg )
{
    DBusConnection *p_conn;
    DBusMessage *p_msg;
    DBusMessage *p_reply;
    DBusMessageIter args;
    DBusError error;
    dbus_error_init( &error );
    dbus_uint32_t i_status;

    p_conn = p_intf->p_sys->p_conn;

    /* first we need to get the actual status */
    p_msg = dbus_message_new_method_call(
            "org.freedesktop.Telepathy.MissionControl",
           "/org/freedesktop/Telepathy/MissionControl",
            "org.freedesktop.Telepathy.MissionControl",
            "GetPresence" );
    if( !p_msg )
        return VLC_ENOMEM;

    p_reply = dbus_connection_send_with_reply_and_block( p_conn, p_msg,
        50, &error ); /* blocks 50ms maximum */

    dbus_message_unref( p_msg );
    if( p_reply == NULL )
    {   /* MC is not active, or too slow. Better luck next time? */
        return VLC_SUCCESS;
    }

    /* extract the status from the reply */
    if( dbus_message_get_args( p_reply, &error,
            DBUS_TYPE_UINT32, &i_status,
            DBUS_TYPE_INVALID ) == FALSE )
    {
        return VLC_ENOMEM;
    }

    p_msg = dbus_message_new_method_call(
            "org.freedesktop.Telepathy.MissionControl",
           "/org/freedesktop/Telepathy/MissionControl",
            "org.freedesktop.Telepathy.MissionControl",
            "SetPresence" );
    if( !p_msg )
        return VLC_ENOMEM;

    dbus_message_iter_init_append( p_msg, &args );

    /* first argument is the status */
    if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_UINT32, &i_status ) )
    {
        dbus_message_unref( p_msg );
        return VLC_ENOMEM;
    }
    /* second argument is the message */
    if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_STRING, &psz_msg ) )
    {
        dbus_message_unref( p_msg );
        return VLC_ENOMEM;
    }


    if( !dbus_connection_send( p_conn, p_msg, NULL ) )
        return VLC_ENOMEM;

    dbus_connection_flush( p_conn );
    dbus_message_unref( p_msg );

    return VLC_SUCCESS;
}