notify.c 10.2 KB
Newer Older
Christophe Mutricy's avatar
Christophe Mutricy committed
1
2
3
/*****************************************************************************
 * notify.c : libnotify notification plugin
 *****************************************************************************
Rafaël Carré's avatar
Rafaël Carré committed
4
 * Copyright (C) 2006-2007 the VideoLAN team
Christophe Mutricy's avatar
Christophe Mutricy committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 * $Id$
 *
 * Authors: Christophe Mutricy <xtophe -at- videolan -dot- org>
 *
 * 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
 *****************************************************************************/
#include <vlc/vlc.h>
zorglub's avatar
zorglub committed
28
29
#include <vlc_interface.h>
#include <vlc_playlist.h>
Christophe Mutricy's avatar
Christophe Mutricy committed
30
31
#include <vlc_meta.h>

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
32
33
34
35
#include <errno.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <libnotify/notify.h>

Christophe Mutricy's avatar
Christophe Mutricy committed
36
37
38
39
40
41
42
43
/*****************************************************************************
 * 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 * );
44
static int Notify( vlc_object_t *, const char *, GdkPixbuf *, intf_thread_t * );
Christophe Mutricy's avatar
Christophe Mutricy committed
45
46
#define MAX_LENGTH 256

47
48
struct intf_sys_t
{
49
    NotifyNotification *notification;
50
51
52
    vlc_mutex_t     lock;
};

Christophe Mutricy's avatar
Christophe Mutricy committed
53
54
55
56
57
58
59
60
61
62
63
64
/*****************************************************************************
 * Module descriptor
 ****************************************************************************/

#define APPLICATION_NAME "VLC media player"

#define TIMEOUT_TEXT N_("Timeout (ms)")
#define TIMEOUT_LONGTEXT N_("How long the notification will be displayed ")

vlc_module_begin();
    set_category( CAT_INTERFACE );
    set_subcategory( SUBCAT_INTERFACE_CONTROL );
65
    set_shortname( _( "Notify" ) );
Christophe Mutricy's avatar
Christophe Mutricy committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    set_description( _("LibNotify Notification Plugin") );

    add_integer( "notify-timeout", 4000,NULL,
                TIMEOUT_TEXT, TIMEOUT_LONGTEXT, VLC_TRUE );

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

/*****************************************************************************
 * Open: initialize and create stuff
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
80
81
82
    intf_thread_t   *p_intf = (intf_thread_t *)p_this;
    playlist_t      *p_playlist;
    intf_sys_t      *p_sys  = malloc( sizeof( intf_sys_t ) );
83

84
85
86
87
88
    if( !p_sys )
    {
        msg_Err( p_intf, "Out of memory" );
        return VLC_ENOMEM;
    }
Christophe Mutricy's avatar
Christophe Mutricy committed
89
90
91
92
93
94

    if( !notify_init( APPLICATION_NAME ) )
    {
        msg_Err( p_intf, "can't find notification daemon" );
        return VLC_EGENERIC;
    }
95

96
97
98
99
    vlc_mutex_init( p_this, &p_sys->lock );

    p_intf->p_sys = p_sys;

100
101
    p_intf->p_sys->notification = NULL;

102
103
104
105
    p_playlist = pl_Yield( p_intf );
    var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
    pl_Release( p_intf );

Christophe Mutricy's avatar
Christophe Mutricy committed
106
107
108
109
110
111
112
113
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close: destroy interface stuff
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
114
115
116
    intf_thread_t   *p_intf = ( intf_thread_t* ) p_this;
    intf_sys_t      *p_sys  = p_intf->p_sys;

zorglub's avatar
Fix    
zorglub committed
117
    playlist_t *p_playlist = pl_Yield( p_this );
118
    var_DelCallback( p_playlist, "playlist-current", ItemChange, p_this );
zorglub's avatar
Fix    
zorglub committed
119
    pl_Release( p_this );
Christophe Mutricy's avatar
Oups    
Christophe Mutricy committed
120

121
122
123
    if( p_intf->p_sys->notification )
        g_object_unref( p_intf->p_sys->notification );

124
125
    vlc_mutex_destroy( &p_sys->lock );
    free( p_sys );
Christophe Mutricy's avatar
Christophe Mutricy committed
126
127
128
129
130
131
132
133
134
    notify_uninit();
}

/*****************************************************************************
 * 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 )
{
135
    char                psz_tmp[MAX_LENGTH];
136
    char                psz_notify[MAX_LENGTH];
137
138
139
    char                *psz_title      = NULL;
    char                *psz_artist     = NULL;
    char                *psz_album      = NULL;
140
    char                *psz_arturl     = NULL;
141
142
143
144
    input_thread_t      *p_input        = NULL;
    playlist_t          * p_playlist    = pl_Yield( p_this );
    intf_thread_t       *p_intf         = ( intf_thread_t* ) param;
    intf_sys_t          *p_sys          = p_intf->p_sys;
zorglub's avatar
zorglub committed
145

Christophe Mutricy's avatar
Christophe Mutricy committed
146
    p_input = p_playlist->p_input;
147
    pl_Release( p_playlist );
148

Christophe Mutricy's avatar
Christophe Mutricy committed
149
150
151
    if( !p_input ) return VLC_SUCCESS;
    vlc_object_yield( p_input );

152
    if( p_input->b_dead )
Christophe Mutricy's avatar
Christophe Mutricy committed
153
154
155
156
157
    {
        /* Not playing anything ... */
        vlc_object_release( p_input );
        return VLC_SUCCESS;
    }
158
159
    /*Wait a tad so the meta has been fetched*/
    msleep( 1000*4 );
Christophe Mutricy's avatar
Christophe Mutricy committed
160
161

    /* Playing something ... */
Rafaël Carré's avatar
Rafaël Carré committed
162
163
    psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
    psz_album = input_item_GetAlbum( input_GetItem( p_input ) ) ;
164
    psz_title = input_item_GetTitle( input_GetItem( p_input ) );
165
166
167
    if( ( psz_title == NULL ) || EMPTY_STR( psz_title ) )
    {
        free( psz_title );
168
        psz_title = input_item_GetName( input_GetItem( p_input ) );
169
170
    }
    if( ( psz_title == NULL ) || EMPTY_STR( psz_title ) )
171
    {  /* Not enough metadata ... */
172
        free( psz_title );
173
174
175
176
177
        free( psz_artist );
        free( psz_album );
        vlc_object_release( p_input );
        return VLC_SUCCESS;
    }
178
179
180
181
182
183
184
185
186
187
    if( EMPTY_STR( psz_artist ) )
    {
        free( psz_artist );
        psz_artist = NULL;
    }
    if( EMPTY_STR( psz_album ) )
    {
        free( psz_album );
        psz_album = NULL;
    }
188
189
190

    vlc_object_release( p_input );

191
    if( psz_artist && psz_album )
192
        snprintf( psz_tmp, MAX_LENGTH, "<b>%s</b>\n%s\n[%s]",
193
194
                  psz_title, psz_artist, psz_album );
    else if( psz_artist )
195
        snprintf( psz_tmp, MAX_LENGTH, "<b>%s</b>\n%s",
196
197
198
                  psz_title, psz_artist );
    else
        snprintf( psz_tmp, MAX_LENGTH, "<b>%s</b>", psz_title );
199

Christophe Mutricy's avatar
Christophe Mutricy committed
200
201
202
203
    free( psz_title );
    free( psz_artist );
    free( psz_album );

204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
    GdkPixbuf *pix = NULL;
    GError *p_error = NULL;

    psz_arturl = input_item_GetArtURL( input_GetItem( p_input ) );
    if( psz_arturl && !strncmp( psz_arturl, "file://", 7 ) &&
                strlen( psz_arturl ) > 7 )
    { /* scale the art to show it in notify popup */
        gboolean b = TRUE;
        pix = gdk_pixbuf_new_from_file_at_scale(
                (psz_arturl + 7), 72, 72, b, &p_error );
        free( psz_arturl );
    }
    else /* else we show state-of-the art logo */
        pix = gdk_pixbuf_new_from_file( DATA_PATH "/vlc48x48.png", &p_error );

219
    /* we need to replace '&' with '&amp;' because '&' is a keyword of
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
     * notification-daemon parser */
    int i_notify, i_len, i;
    i_len = strlen( psz_tmp );
    i_notify = 0;
    for( i = 0; ( ( i < i_len ) && ( i_notify < ( MAX_LENGTH - 5 ) ) ); i++ )
    { /* we use MAX_LENGTH - 5 because if the last char of psz_tmp is '&'
       * we will need 5 more characters: 'amp;\0' .
       * however that's unlikely to happen because the last char is '\0' */
        if( psz_tmp[i] != '&' )
            psz_notify[i_notify] = psz_tmp[i];
        else
        {
            snprintf( psz_notify + i_notify, 6, "&amp;" );
            i_notify += 4;
        }
        i_notify++;
    }
    psz_notify[i_notify] = '\0';

239
240
    vlc_mutex_lock( &p_sys->lock );

241
    Notify( p_this, psz_notify, pix, p_intf );
Christophe Mutricy's avatar
Christophe Mutricy committed
242

243
244
    vlc_mutex_unlock( &p_sys->lock );

Christophe Mutricy's avatar
Christophe Mutricy committed
245
246
247
    return VLC_SUCCESS;
}

248
249
static void Next( NotifyNotification *notification, gchar *psz, gpointer p )
{ /* libnotify callback, called when the "Next" button is pressed */
250
251
252
253
254
255
    notify_notification_close (notification, NULL);
    playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p) );
    playlist_Next( p_playlist );
    pl_Release( ((vlc_object_t*) p) );
}

256
257
258
259
260
261
262
263
static void Prev( NotifyNotification *notification, gchar *psz, gpointer p )
{ /* libnotify callback, called when the "Previous" button is pressed */
    notify_notification_close (notification, NULL);
    playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p) );
    playlist_Prev( p_playlist );
    pl_Release( ((vlc_object_t*) p) );
}

264
static int Notify( vlc_object_t *p_this, const char *psz_temp, GdkPixbuf *pix,
265
                   intf_thread_t *p_intf )
Christophe Mutricy's avatar
Christophe Mutricy committed
266
267
{
    NotifyNotification * notification;
268
269
270
271
    GError *p_error = NULL;

    /* Close previous notification if still active */
    if( p_intf->p_sys->notification )
272
    {
273
        notify_notification_close( p_intf->p_sys->notification, &p_error );
274
275
        g_object_unref( p_intf->p_sys->notification );
    }
276

Christophe Mutricy's avatar
Christophe Mutricy committed
277
    notification = notify_notification_new( _("Now Playing"),
278
            psz_temp, NULL, NULL);
Christophe Mutricy's avatar
Christophe Mutricy committed
279
280
281
    notify_notification_set_timeout( notification,
                                     config_GetInt(p_this, "notify-timeout") );
    notify_notification_set_urgency( notification, NOTIFY_URGENCY_LOW );
282
    if( pix )
283
    {
284
        notify_notification_set_icon_from_pixbuf( notification, pix );
285
286
287
        gdk_pixbuf_unref( pix );
    }

288
    /* Adds previous and next buttons in the notification */
289
    notify_notification_add_action( notification, "previous", _("Previous"), Prev,
290
291
                                    (gpointer*) p_intf, NULL );
    notify_notification_add_action( notification, "next", _("Next"), Next,
292
293
                                    (gpointer*) p_intf, NULL );

Christophe Mutricy's avatar
Christophe Mutricy committed
294
    notify_notification_show( notification, NULL);
295
296
297

    /* Stores the notification to be able to close it */
    p_intf->p_sys->notification = notification;
Christophe Mutricy's avatar
Christophe Mutricy committed
298
299
300
    return VLC_SUCCESS;
}