1 /*****************************************************************************
2 * growl.m : growl notification plugin
3 *****************************************************************************
6 * Copyright © 2008,2011 the VideoLAN team
9 * Authors: Rafaël Carré <funman@videolanorg>
10 * Felix Paul Kühne <fkuehne@videolan.org
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 * Growl specific code, ripped from growlnotify:
28 * Copyright (c) The Growl Project, 2004-2005
29 * All rights reserved.
31 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
33 * 1. Redistributions of source code must retain the above copyright
34 notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 notice, this list of conditions and the following disclaimer in the
37 documentation and/or other materials provided with the distribution.
38 * 3. Neither the name of Growl nor the names of its contributors
39 may be used to endorse or promote products derived from this software
40 without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 *****************************************************************************/
46 /*****************************************************************************
48 *****************************************************************************/
54 #import <CoreFoundation/CoreFoundation.h>
55 #import <Growl/GrowlDefines.h>
57 #include <vlc_common.h>
58 #include <vlc_plugin.h>
59 #include <vlc_playlist.h>
61 #include <vlc_interface.h>
65 /*****************************************************************************
67 *****************************************************************************/
70 CFDataRef default_icon;
72 CFStringRef notification_type;
77 /*****************************************************************************
79 *****************************************************************************/
80 static int Open ( vlc_object_t * );
81 static void Close ( vlc_object_t * );
83 static int ItemChange( vlc_object_t *, const char *,
84 vlc_value_t, vlc_value_t, void * );
86 static void RegisterToGrowl( vlc_object_t * );
87 static void NotifyToGrowl( intf_thread_t *, const char *, CFDataRef );
89 static CFDataRef readFile(const char *);
91 /*****************************************************************************
93 ****************************************************************************/
96 set_category( CAT_INTERFACE )
97 set_subcategory( SUBCAT_INTERFACE_CONTROL )
98 set_shortname( "Growl" )
99 set_description( N_("Growl Notification Plugin") )
100 set_capability( "interface", 0 )
101 set_callbacks( Open, Close )
104 /*****************************************************************************
105 * Open: initialize and create stuff
106 *****************************************************************************/
107 static int Open( vlc_object_t *p_this )
109 intf_thread_t *p_intf = (intf_thread_t *)p_this;
110 playlist_t *p_playlist;
113 p_sys = p_intf->p_sys = calloc( 1, sizeof(intf_sys_t) );
117 p_sys->app_name = CFSTR( "VLC media player" );
118 p_sys->notification_type = CFSTR( "New input playing" );
120 char *data_path = config_GetDataDir ( p_this );
121 char buf[strlen (data_path) + sizeof ("/vlc512x512.png")];
122 snprintf (buf, sizeof (buf), "%s/vlc512x512.png", data_path);
123 msg_Dbg( p_this, "looking for icon at %s", buf );
125 p_sys->default_icon = (CFDataRef) readFile( buf );
127 p_playlist = pl_Get( p_intf );
128 var_AddCallback( p_playlist, "item-change", ItemChange, p_intf );
129 var_AddCallback( p_playlist, "item-current", ItemChange, p_intf );
131 RegisterToGrowl( p_this );
135 /*****************************************************************************
136 * Close: destroy interface stuff
137 *****************************************************************************/
138 static void Close( vlc_object_t *p_this )
140 intf_thread_t *p_intf = (intf_thread_t *)p_this;
141 playlist_t *p_playlist = pl_Get( p_this );
142 intf_sys_t *p_sys = p_intf->p_sys;
144 var_DelCallback( p_playlist, "item-change", ItemChange, p_intf );
145 var_DelCallback( p_playlist, "item-current", ItemChange, p_intf );
147 CFRelease( p_sys->default_icon );
148 CFRelease( p_sys->app_name );
149 CFRelease( p_sys->notification_type );
153 /*****************************************************************************
154 * ItemChange: Playlist item change callback
155 *****************************************************************************/
156 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
157 vlc_value_t oldval, vlc_value_t newval, void *param )
161 intf_thread_t *p_intf = (intf_thread_t *)param;
162 char *psz_tmp = NULL;
163 char *psz_title = NULL;
164 char *psz_artist = NULL;
165 char *psz_album = NULL;
166 input_item_t *p_item = newval.p_address;
168 bool b_is_item_current = !strcmp( "item-current", psz_var );
170 /* Don't update each time an item has been preparsed */
171 if( b_is_item_current )
172 { /* stores the current input item id */
173 p_intf->p_sys->i_id = p_item->i_id;
174 p_intf->p_sys->i_item_changes = 0;
177 /* ignore items which weren't pre-parsed yet */
178 else if( !input_item_IsPreparsed(p_item) )
182 if( p_item->i_id != p_intf->p_sys->i_id ) { /* "item-change" */
183 p_intf->p_sys->i_item_changes = 0;
186 /* Some variable bitrate inputs call "item-change" callbacks each time
187 * their length is updated, that is several times per second.
188 * We'll limit the number of changes to 1 per input. */
189 if( p_intf->p_sys->i_item_changes > 0 )
192 p_intf->p_sys->i_item_changes++;
196 input_thread_t *p_input = playlist_CurrentInput( (playlist_t*)p_this );
198 if( !p_input ) return VLC_SUCCESS;
200 if( p_input->b_dead || !input_GetItem(p_input)->psz_name )
202 /* Not playing anything ... */
203 vlc_object_release( p_input );
207 /* Playing something ... */
208 if( input_item_GetNowPlaying( p_item ) )
209 psz_title = input_item_GetNowPlaying( p_item );
211 psz_title = input_item_GetTitleFbName( p_item );
212 if( EMPTY_STR( psz_title ) )
215 vlc_object_release( p_input );
219 psz_artist = input_item_GetArtist( p_item );
220 if( EMPTY_STR( psz_artist ) ) FREENULL( psz_artist );
221 psz_album = input_item_GetAlbum( p_item ) ;
222 if( EMPTY_STR( psz_album ) ) FREENULL( psz_album );
225 if( psz_artist && psz_album )
226 i_ret = asprintf( &psz_tmp, "%s\n%s [%s]",
227 psz_title, psz_artist, psz_album );
228 else if( psz_artist )
229 i_ret = asprintf( &psz_tmp, "%s\n%s", psz_title, psz_artist );
231 i_ret = asprintf(&psz_tmp, "%s", psz_title );
238 vlc_object_release( p_input );
242 char *psz_arturl = input_item_GetArtURL( p_item );
245 char *psz = make_path( psz_arturl );
249 CFDataRef art = NULL;
251 art = (CFDataRef) readFile( psz_arturl );
258 NotifyToGrowl( p_intf, psz_tmp, art );
260 if( art ) CFRelease( art );
263 vlc_object_release( p_input );
267 /*****************************************************************************
269 *****************************************************************************/
270 static void RegisterToGrowl( vlc_object_t *p_this )
272 intf_sys_t *p_sys = ((intf_thread_t *)p_this)->p_sys;
274 CFArrayRef defaultAndAllNotifications = CFArrayCreate(
275 kCFAllocatorDefault, (const void **)&(p_sys->notification_type), 1,
276 &kCFTypeArrayCallBacks );
278 CFTypeRef registerKeys[4] = {
280 GROWL_NOTIFICATIONS_ALL,
281 GROWL_NOTIFICATIONS_DEFAULT,
285 CFTypeRef registerValues[4] = {
287 defaultAndAllNotifications,
288 defaultAndAllNotifications,
292 CFDictionaryRef registerInfo = CFDictionaryCreate(
293 kCFAllocatorDefault, registerKeys, registerValues, 4,
294 &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks );
296 CFRelease( defaultAndAllNotifications );
298 CFNotificationCenterPostNotificationWithOptions(
299 CFNotificationCenterGetDistributedCenter(),
300 (CFStringRef)GROWL_APP_REGISTRATION, NULL, registerInfo,
301 kCFNotificationPostToAllSessions );
302 CFRelease( registerInfo );
305 static void NotifyToGrowl( intf_thread_t *p_intf, const char *psz_desc, CFDataRef art )
307 intf_sys_t *p_sys = p_intf->p_sys;
309 CFStringRef title = CFStringCreateWithCString( kCFAllocatorDefault, _("Now playing"), kCFStringEncodingUTF8 );
310 CFStringRef desc = CFStringCreateWithCString( kCFAllocatorDefault, psz_desc, kCFStringEncodingUTF8 );
312 CFMutableDictionaryRef notificationInfo = CFDictionaryCreateMutable(
313 kCFAllocatorDefault, 5, &kCFTypeDictionaryKeyCallBacks,
314 &kCFTypeDictionaryValueCallBacks);
316 CFDictionarySetValue( notificationInfo, GROWL_NOTIFICATION_NAME, p_sys->notification_type );
317 CFDictionarySetValue( notificationInfo, GROWL_APP_NAME, p_sys->app_name );
318 CFDictionarySetValue( notificationInfo, GROWL_NOTIFICATION_TITLE, title );
319 CFDictionarySetValue( notificationInfo, GROWL_NOTIFICATION_DESCRIPTION, desc );
321 CFDictionarySetValue( notificationInfo, GROWL_NOTIFICATION_ICON,
322 art ? art : p_sys->default_icon );
327 CFNotificationCenterPostNotificationWithOptions(
328 CFNotificationCenterGetDistributedCenter(),
329 (CFStringRef)GROWL_NOTIFICATION, NULL, notificationInfo,
330 kCFNotificationPostToAllSessions );
332 CFRelease( notificationInfo );
335 /* Ripped from CFGrowlAdditions.c
336 * Strangely, this function does exist in Growl shared library, but is not
337 * defined in public header files */
338 static CFDataRef readFile(const char *filename)
341 // read the file into a CFDataRef
342 FILE *fp = fopen(filename, "r");
346 fseek(fp, 0, SEEK_END);
347 long dataLength = ftell(fp);
348 fseek(fp, 0, SEEK_SET);
349 unsigned char *fileData = malloc(dataLength);
350 fread(fileData, 1, dataLength, fp);
352 return CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, fileData,
353 dataLength, kCFAllocatorMalloc);