1 /*****************************************************************************
2 * growl.c : growl notification plugin
3 *****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
7 * Authors: Jérôme Decoodt <djc -at- videolan -dot- org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
29 #include <vlc_interface.h>
30 #include <vlc_playlist.h>
32 #include <vlc_network.h>
36 /*****************************************************************************
38 *****************************************************************************/
39 static int Open ( vlc_object_t * );
40 static void Close ( vlc_object_t * );
42 static int ItemChange( vlc_object_t *, const char *,
43 vlc_value_t, vlc_value_t, void * );
45 static int RegisterToGrowl( vlc_object_t *p_this );
46 static int NotifyToGrowl( vlc_object_t *p_this, char *psz_type,
47 char *psz_title, char *psz_desc );
48 static int CheckAndSend( vlc_object_t *p_this, uint8_t* p_data, int i_offset );
49 #define GROWL_MAX_LENGTH 256
51 /*****************************************************************************
53 ****************************************************************************/
55 #define SERVER_DEFAULT "127.0.0.1"
56 #define SERVER_TEXT N_("Server")
57 #define SERVER_LONGTEXT N_("This is the host to which Growl notifications " \
58 "will be sent. By default, notifications are sent locally." )
59 #define PASS_DEFAULT ""
60 #define PASS_TEXT N_("Password")
61 #define PASS_LONGTEXT N_("Growl password on the Growl server.")
62 #define PORT_TEXT N_("UDP port")
63 #define PORT_LONGTEXT N_("Growl UDP port on the Growl server.")
66 set_category( CAT_INTERFACE );
67 set_subcategory( SUBCAT_INTERFACE_CONTROL );
68 set_shortname( "Growl" );
69 set_description( _("Growl Notification Plugin") );
71 add_string( "growl-server", SERVER_DEFAULT, NULL,
72 SERVER_TEXT, SERVER_LONGTEXT, VLC_FALSE );
73 add_password( "growl-password", PASS_DEFAULT, NULL,
74 PASS_TEXT, PASS_LONGTEXT, VLC_FALSE );
75 add_integer( "growl-port", 9887, NULL,
76 PORT_TEXT, PORT_LONGTEXT, VLC_TRUE );
78 set_capability( "interface", 0 );
79 set_callbacks( Open, Close );
82 /*****************************************************************************
83 * Open: initialize and create stuff
84 *****************************************************************************/
85 static int Open( vlc_object_t *p_this )
87 intf_thread_t *p_intf = (intf_thread_t *)p_this;
89 playlist_t *p_playlist = pl_Yield( p_intf );
90 var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
93 RegisterToGrowl( p_this );
97 /*****************************************************************************
98 * Close: destroy interface stuff
99 *****************************************************************************/
100 static void Close( vlc_object_t *p_this )
102 playlist_t *p_playlist = pl_Yield( p_this );
103 var_DelCallback( p_playlist, "playlist-current", ItemChange, p_this );
104 pl_Release( p_this );
107 /*****************************************************************************
108 * ItemChange: Playlist item change callback
109 *****************************************************************************/
110 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
111 vlc_value_t oldval, vlc_value_t newval, void *param )
113 char psz_tmp[GROWL_MAX_LENGTH];
114 char *psz_title = NULL;
115 char *psz_artist = NULL;
116 char *psz_album = NULL;
117 input_thread_t *p_input;
118 playlist_t *p_playlist = pl_Yield( p_this );
120 p_input = p_playlist->p_input;
121 vlc_object_release( p_playlist );
123 if( !p_input ) return VLC_SUCCESS;
124 vlc_object_yield( p_input );
126 char *psz_name = input_item_GetName( input_GetItem( p_input ) );
127 if( p_input->b_dead || !psz_name )
129 /* Not playing anything ... */
131 vlc_object_release( p_input );
136 /* Playing something ... */
137 psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
138 if( psz_artist == NULL ) psz_artist = strdup( "" );
139 psz_album = input_item_GetAlbum( input_GetItem( p_input ) ) ;
140 if( psz_album == NULL ) psz_album = strdup( "" );
141 psz_title = input_item_GetTitle( input_GetItem( p_input ) );
142 if( psz_title == NULL )
143 psz_title = input_item_GetName( input_GetItem( p_input ) );
144 snprintf( psz_tmp, GROWL_MAX_LENGTH, "%s %s %s",
145 psz_title, psz_artist, psz_album );
150 NotifyToGrowl( p_this, "Now Playing", "Now Playing", psz_tmp );
152 vlc_object_release( p_input );
156 /*****************************************************************************
157 * Growl specific functions
158 *****************************************************************************/
159 #define GROWL_PROTOCOL_VERSION (1)
160 #define GROWL_TYPE_REGISTRATION (0)
161 #define GROWL_TYPE_NOTIFICATION (1)
162 #define APPLICATION_NAME "VLC media player"
164 #define insertstrlen( psz ) \
166 uint16_t i_size = strlen( psz ); \
167 psz_encoded[i++] = (i_size>>8)&0xFF; \
168 psz_encoded[i++] = i_size&0xFF; \
170 /*****************************************************************************
172 *****************************************************************************/
173 static int RegisterToGrowl( vlc_object_t *p_this )
175 uint8_t *psz_encoded = malloc(100);
176 uint8_t i_defaults = 0;
177 char *psz_notifications[] = {"Now Playing", NULL};
178 vlc_bool_t pb_defaults[] = {VLC_TRUE, VLC_FALSE};
180 if( psz_encoded == NULL )
183 memset( psz_encoded, 0, sizeof(psz_encoded) );
184 psz_encoded[i++] = GROWL_PROTOCOL_VERSION;
185 psz_encoded[i++] = GROWL_TYPE_REGISTRATION;
186 insertstrlen(APPLICATION_NAME);
188 strcpy( (char*)(psz_encoded+i), APPLICATION_NAME );
189 i += strlen(APPLICATION_NAME);
190 for( j = 0 ; psz_notifications[j] != NULL ; j++)
192 insertstrlen(psz_notifications[j]);
193 strcpy( (char*)(psz_encoded+i), psz_notifications[j] );
194 i += strlen(psz_notifications[j]);
197 for( j = 0 ; psz_notifications[j] != NULL ; j++)
198 if(pb_defaults[j] == VLC_TRUE)
200 psz_encoded[i++] = (uint8_t)j;
203 psz_encoded[5] = i_defaults;
205 CheckAndSend(p_this, psz_encoded, i);
210 static int NotifyToGrowl( vlc_object_t *p_this, char *psz_type,
211 char *psz_title, char *psz_desc )
213 uint8_t *psz_encoded = malloc(GROWL_MAX_LENGTH + 42);
216 if( psz_encoded == NULL )
219 memset( psz_encoded, 0, sizeof(psz_encoded) );
220 psz_encoded[i++] = GROWL_PROTOCOL_VERSION;
221 psz_encoded[i++] = GROWL_TYPE_NOTIFICATION;
223 psz_encoded[i++] = (flags>>8)&0xFF;
224 psz_encoded[i++] = flags&0xFF;
225 insertstrlen(psz_type);
226 insertstrlen(psz_title);
227 insertstrlen(psz_desc);
228 insertstrlen(APPLICATION_NAME);
229 strcpy( (char*)(psz_encoded+i), psz_type );
230 i += strlen(psz_type);
231 strcpy( (char*)(psz_encoded+i), psz_title );
232 i += strlen(psz_title);
233 strcpy( (char*)(psz_encoded+i), psz_desc );
234 i += strlen(psz_desc);
235 strcpy( (char*)(psz_encoded+i), APPLICATION_NAME );
236 i += strlen(APPLICATION_NAME);
238 CheckAndSend(p_this, psz_encoded, i);
243 static int CheckAndSend( vlc_object_t *p_this, uint8_t* p_data, int i_offset )
247 intf_thread_t *p_intf = (intf_thread_t *)p_this;
248 char *psz_password = config_GetPsz( p_intf, "growl-password" );
249 char *psz_server = config_GetPsz( p_intf, "growl-server" );
250 int i_port = config_GetInt( p_intf, "growl-port" );
251 strcpy( (char*)(p_data+i_offset), psz_password );
252 i = i_offset + strlen(psz_password);
255 AddMD5( &md5, p_data, i );
258 for( i = 0 ; i < 4 ; i++ )
260 md5.p_digest[i] = md5.p_digest[i];
261 p_data[i_offset++] = md5.p_digest[i] &0xFF;
262 p_data[i_offset++] = (md5.p_digest[i]>> 8)&0xFF;
263 p_data[i_offset++] = (md5.p_digest[i]>>16)&0xFF;
264 p_data[i_offset++] = (md5.p_digest[i]>>24)&0xFF;
267 i_handle = net_ConnectUDP( p_this, psz_server, i_port, 0 );
270 msg_Err( p_this, "failed to open a connection (udp)" );
276 shutdown( i_handle, SHUT_RD );
277 if( send( i_handle, p_data, i_offset, 0 )
280 msg_Warn( p_this, "send error: %m" );
282 net_Close( i_handle );
289 #undef GROWL_PROTOCOL_VERSION
290 #undef GROWL_TYPE_REGISTRATION
291 #undef GROWL_TYPE_NOTIFICATION
292 #undef APPLICATION_NAME