1 /*****************************************************************************
2 * notify.c : libnotify notification plugin
3 *****************************************************************************
4 * Copyright (C) 2006-2009 the VideoLAN team
6 * Authors: Christophe Mutricy <xtophe -at- videolan -dot- org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
30 #define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_interface.h>
34 #include <vlc_player.h>
35 #include <vlc_playlist.h>
38 #include <gdk-pixbuf/gdk-pixbuf.h>
39 #include <libnotify/notify.h>
41 typedef struct GtkIconTheme GtkIconTheme
;
42 enum GtkIconLookupFlags
{ dummy
= 0x7fffffff };
44 VLC_WEAK GtkIconTheme
*gtk_icon_theme_get_default(void);
45 VLC_WEAK GdkPixbuf
*gtk_icon_theme_load_icon(GtkIconTheme
*,
46 const char *icon_name
, int size
, enum GtkIconLookupFlags
, GError
**);
48 #ifndef NOTIFY_CHECK_VERSION
49 # define NOTIFY_CHECK_VERSION(x,y,z) 0
52 /*****************************************************************************
54 ****************************************************************************/
55 static int Open ( vlc_object_t
* );
56 static void Close ( vlc_object_t
* );
58 #define APPLICATION_NAME "VLC media player"
60 #define TIMEOUT_TEXT N_("Timeout (ms)")
61 #define TIMEOUT_LONGTEXT N_("How long the notification will be displayed.")
64 set_category( CAT_INTERFACE
)
65 set_subcategory( SUBCAT_INTERFACE_CONTROL
)
66 set_shortname( N_( "Notify" ) )
67 set_description( N_("LibNotify Notification Plugin") )
69 add_integer( "notify-timeout", 4000,
70 TIMEOUT_TEXT
, TIMEOUT_LONGTEXT
, true )
72 set_capability( "interface", 0 )
73 set_callbacks( Open
, Close
)
77 /*****************************************************************************
79 *****************************************************************************/
80 static void on_current_media_changed(vlc_player_t
*player
,
81 input_item_t
*new_media
, void *data
);
82 static int Notify( vlc_object_t
*, const char *, GdkPixbuf
*, intf_thread_t
* );
83 #define MAX_LENGTH 256
87 NotifyNotification
*notification
;
90 vlc_playlist_t
*playlist
;
91 struct vlc_player_listener_id
*player_listener
;
94 static void foreach_g_free(void *data
, void *userdata
)
100 /*****************************************************************************
101 * Open: initialize and create stuff
102 *****************************************************************************/
103 static int Open( vlc_object_t
*p_this
)
105 intf_thread_t
*p_intf
= (intf_thread_t
*)p_this
;
106 intf_sys_t
*p_sys
= malloc( sizeof( *p_sys
) );
111 p_sys
->playlist
= vlc_intf_GetMainPlaylist(p_intf
);
112 vlc_player_t
*player
= vlc_playlist_GetPlayer(p_sys
->playlist
);
113 static const struct vlc_player_cbs player_cbs
=
115 .on_current_media_changed
= on_current_media_changed
117 vlc_player_Lock(player
);
118 p_sys
->player_listener
= vlc_player_AddListener(player
, &player_cbs
, p_intf
);
119 vlc_player_Unlock(player
);
120 if (!p_sys
->player_listener
)
126 if( !notify_init( APPLICATION_NAME
) )
128 vlc_player_Lock(player
);
129 vlc_player_RemoveListener(player
, p_sys
->player_listener
);
130 vlc_player_Unlock(player
);
132 msg_Err( p_intf
, "can't find notification daemon" );
135 p_intf
->p_sys
= p_sys
;
137 vlc_mutex_init( &p_sys
->lock
);
138 p_sys
->notification
= NULL
;
139 p_sys
->b_has_actions
= false;
141 GList
*p_caps
= notify_get_server_caps ();
144 for( GList
*c
= p_caps
; c
!= NULL
; c
= c
->next
)
146 if( !strcmp( (char*)c
->data
, "actions" ) )
148 p_sys
->b_has_actions
= true;
152 g_list_foreach( p_caps
, foreach_g_free
, NULL
);
153 g_list_free( p_caps
);
159 /*****************************************************************************
160 * Close: destroy interface stuff
161 *****************************************************************************/
162 static void Close( vlc_object_t
*p_this
)
164 intf_thread_t
*p_intf
= ( intf_thread_t
* ) p_this
;
165 intf_sys_t
*p_sys
= p_intf
->p_sys
;
167 vlc_player_t
*player
= vlc_playlist_GetPlayer(p_sys
->playlist
);
168 vlc_player_Lock(player
);
169 vlc_player_RemoveListener(player
, p_sys
->player_listener
);
170 vlc_player_Unlock(player
);
172 if( p_sys
->notification
)
174 GError
*p_error
= NULL
;
175 notify_notification_close( p_sys
->notification
, &p_error
);
176 g_object_unref( p_sys
->notification
);
183 static void on_current_media_changed(vlc_player_t
*player
,
184 input_item_t
*p_input_item
, void *data
)
187 intf_thread_t
*p_intf
= data
;
188 intf_sys_t
*p_sys
= p_intf
->p_sys
;
189 char psz_tmp
[MAX_LENGTH
];
190 char psz_notify
[MAX_LENGTH
];
199 /* Checking for click on directories */
200 if(p_input_item
->i_type
== ITEM_TYPE_DIRECTORY
|| p_input_item
->i_type
== ITEM_TYPE_PLAYLIST
201 || p_input_item
->i_type
== ITEM_TYPE_NODE
|| p_input_item
->i_type
== ITEM_TYPE_UNKNOWN
202 || p_input_item
->i_type
== ITEM_TYPE_CARD
){
206 psz_title
= input_item_GetTitleFbName( p_input_item
);
207 /* We need at least a title */
208 if( EMPTY_STR( psz_title
) )
214 psz_artist
= input_item_GetArtist( p_input_item
);
215 psz_album
= input_item_GetAlbum( p_input_item
);
217 if( !EMPTY_STR( psz_artist
) )
219 if( !EMPTY_STR( psz_album
) )
220 snprintf( psz_tmp
, MAX_LENGTH
, "<b>%s</b>\n%s\n[%s]",
221 psz_title
, psz_artist
, psz_album
);
223 snprintf( psz_tmp
, MAX_LENGTH
, "<b>%s</b>\n%s",
224 psz_title
, psz_artist
);
227 snprintf( psz_tmp
, MAX_LENGTH
, "<b>%s</b>", psz_title
);
233 GdkPixbuf
*pix
= NULL
;
234 psz_arturl
= input_item_GetArtURL( p_input_item
);
238 char *psz
= vlc_uri2path( psz_arturl
);
244 { /* scale the art to show it in notify popup */
245 GError
*p_error
= NULL
;
246 pix
= gdk_pixbuf_new_from_file_at_scale( psz_arturl
,
247 72, 72, TRUE
, &p_error
);
251 /* else we show state-of-the art logo */
252 if( gtk_icon_theme_get_default
!= NULL
253 && gtk_icon_theme_load_icon
!= NULL
)
255 /* First try to get an icon from the current theme. */
256 GtkIconTheme
* p_theme
= gtk_icon_theme_get_default();
257 pix
= gtk_icon_theme_load_icon( p_theme
, "vlc", 72, 0, NULL
);
260 { /* Load icon from share/ */
261 GError
*p_error
= NULL
;
262 char *psz_pixbuf
= config_GetSysPath(VLC_SYSDATA_DIR
,
263 "icons/hicolor/48x48/"PACKAGE_NAME
".png");
264 if (psz_pixbuf
!= NULL
)
266 pix
= gdk_pixbuf_new_from_file( psz_pixbuf
, &p_error
);
271 /* we need to replace '&' with '&' because '&' is a keyword of
272 * notification-daemon parser */
273 const int i_len
= strlen( psz_tmp
);
275 for( int i
= 0; i
< i_len
&& i_notify
< ( MAX_LENGTH
- 5 ); i
++ )
276 { /* we use MAX_LENGTH - 5 because if the last char of psz_tmp is '&'
277 * we will need 5 more characters: 'amp;\0' .
278 * however that's unlikely to happen because the last char is '\0' */
279 if( psz_tmp
[i
] != '&' )
281 psz_notify
[i_notify
] = psz_tmp
[i
];
285 strcpy( &psz_notify
[i_notify
], "&" );
290 psz_notify
[i_notify
] = '\0';
292 vlc_mutex_lock( &p_sys
->lock
);
294 Notify( VLC_OBJECT(p_intf
), psz_notify
, pix
, p_intf
);
296 vlc_mutex_unlock( &p_sys
->lock
);
299 /* libnotify callback, called when the "Next" button is pressed */
300 static void Next( NotifyNotification
*notification
, gchar
*psz
, gpointer p
)
302 intf_thread_t
*p_intf
= (intf_thread_t
*)p
;
303 intf_sys_t
*p_sys
= p_intf
->p_sys
;
306 notify_notification_close( notification
, NULL
);
307 vlc_playlist_Lock( p_sys
->playlist
);
308 vlc_playlist_Next( p_sys
->playlist
);
309 vlc_playlist_Unlock( p_sys
->playlist
);
312 /* libnotify callback, called when the "Previous" button is pressed */
313 static void Prev( NotifyNotification
*notification
, gchar
*psz
, gpointer p
)
315 intf_thread_t
*p_intf
= (intf_thread_t
*)p
;
316 intf_sys_t
*p_sys
= p_intf
->p_sys
;
319 notify_notification_close( notification
, NULL
);
320 vlc_playlist_Lock( p_sys
->playlist
);
321 vlc_playlist_Prev( p_sys
->playlist
);
322 vlc_playlist_Unlock( p_sys
->playlist
);
325 static int Notify( vlc_object_t
*p_this
, const char *psz_temp
, GdkPixbuf
*pix
,
326 intf_thread_t
*p_intf
)
328 intf_sys_t
*p_sys
= p_intf
->p_sys
;
330 NotifyNotification
* notification
;
332 /* Close previous notification if still active */
333 if( p_sys
->notification
)
335 GError
*p_error
= NULL
;
336 notify_notification_close( p_sys
->notification
, &p_error
);
337 g_object_unref( p_sys
->notification
);
340 notification
= notify_notification_new( _("Now Playing"),
342 #if NOTIFY_CHECK_VERSION (0, 7, 0)
347 notify_notification_set_timeout( notification
,
348 var_InheritInteger(p_this
, "notify-timeout") );
349 notify_notification_set_urgency( notification
, NOTIFY_URGENCY_LOW
);
352 notify_notification_set_icon_from_pixbuf( notification
, pix
);
353 g_object_unref( pix
);
356 /* Adds previous and next buttons in the notification if actions are supported. */
357 if( p_sys
->b_has_actions
)
359 notify_notification_add_action( notification
, "previous", _("Previous"), Prev
,
360 (gpointer
*)p_intf
, NULL
);
361 notify_notification_add_action( notification
, "next", _("Next"), Next
,
362 (gpointer
*)p_intf
, NULL
);
365 notify_notification_show( notification
, NULL
);
367 /* Stores the notification to be able to close it */
368 p_sys
->notification
= notification
;