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 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
36 /*****************************************************************************
38 *****************************************************************************/
39 static int Open ( vlc_object_t * );
40 static void Close ( vlc_object_t * );
41 static void Run ( intf_thread_t * );
43 static int ItemChange( vlc_object_t *, const char *,
44 vlc_value_t, vlc_value_t, void * );
46 static int RegisterToGrowl( vlc_object_t *p_this );
47 static int NotifyToGrowl( vlc_object_t *p_this, char *psz_type,
48 char *psz_title, char *psz_desc );
49 static int CheckAndSend( vlc_object_t *p_this, uint8_t* p_data, int i_offset );
50 #define GROWL_MAX_LENGTH 256
52 /*****************************************************************************
54 ****************************************************************************/
56 /// \bug [String] REmove all "Growl" in short desc
58 #define SERVER_DEFAULT "127.0.0.1"
59 #define SERVER_TEXT N_("Growl server")
60 #define SERVER_LONGTEXT N_("This is the host to which Growl notifications " \
61 "will be sent. By default, notifications are sent locally." )
62 #define PASS_DEFAULT ""
63 #define PASS_TEXT N_("Growl password")
64 /// \bug [String] Password on the Growl server.
65 #define PASS_LONGTEXT N_("Growl password on the server.")
66 #define PORT_TEXT N_("Growl UDP port")
67 /// \bug [String] UDP port on the Growl server
68 #define PORT_LONGTEXT N_("Growl UDP port on the server.")
71 set_category( CAT_INTERFACE );
72 set_subcategory( SUBCAT_INTERFACE_CONTROL );
73 set_shortname( N_( "Growl" ) );
74 set_description( _("Growl Notification Plugin") );
76 add_string( "growl-server", SERVER_DEFAULT, NULL,
77 SERVER_TEXT, SERVER_LONGTEXT, VLC_FALSE );
78 add_string( "growl-password", PASS_DEFAULT, NULL,
79 PASS_TEXT, PASS_LONGTEXT, VLC_FALSE );
80 add_integer( "growl-port", 9887, NULL,
81 PORT_TEXT, PORT_LONGTEXT, VLC_TRUE );
83 set_capability( "interface", 0 );
84 set_callbacks( Open, Close );
87 /*****************************************************************************
88 * Open: initialize and create stuff
89 *****************************************************************************/
90 static int Open( vlc_object_t *p_this )
92 intf_thread_t *p_intf = (intf_thread_t *)p_this;
94 playlist_t *p_playlist = (playlist_t *)vlc_object_find(
95 p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
99 msg_Err( p_intf, "could not find playlist object" );
103 var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
104 vlc_object_release( p_playlist );
106 RegisterToGrowl( p_this );
107 p_intf->pf_run = Run;
112 /*****************************************************************************
113 * Close: destroy interface stuff
114 *****************************************************************************/
115 static void Close( vlc_object_t *p_this )
117 playlist_t *p_playlist = (playlist_t *)vlc_object_find(
118 p_this, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
122 var_DelCallback( p_playlist, "playlist-current", ItemChange, p_this );
123 vlc_object_release( p_playlist );
127 /*****************************************************************************
129 *****************************************************************************/
130 static void Run( intf_thread_t *p_intf )
132 msleep( INTF_IDLE_SLEEP );
135 /*****************************************************************************
136 * ItemChange: Playlist item change callback
137 *****************************************************************************/
138 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
139 vlc_value_t oldval, vlc_value_t newval, void *param )
141 char psz_tmp[GROWL_MAX_LENGTH];
142 playlist_t *p_playlist;
143 char *psz_title = NULL;
144 char *psz_artist = NULL;
145 char *psz_album = NULL;
146 input_thread_t *p_input;
148 p_playlist = (playlist_t *)vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
150 if( !p_playlist ) return VLC_EGENERIC;
152 p_input = p_playlist->p_input;
153 vlc_object_release( p_playlist );
154 if( !p_input ) return VLC_SUCCESS;
155 vlc_object_yield( p_input );
157 if( p_input->b_dead || !p_input->input.p_item->psz_name )
159 /* Not playing anything ... */
160 vlc_object_release( p_input );
164 /* Playing something ... */
165 psz_artist = p_input->input.p_item->p_meta->psz_artist ?
166 strdup( p_input->input.p_item->p_meta->psz_artist ) :
168 psz_album = p_input->input.p_item->p_meta->psz_album ?
169 strdup( p_input->input.p_item->p_meta->psz_album ) :
171 psz_title = strdup( p_input->input.p_item->psz_name );
172 if( psz_title == NULL ) psz_title = strdup( N_("(no title)") );
173 if( psz_artist == NULL ) psz_artist = strdup( N_("(no artist)") );
174 if( psz_album == NULL ) psz_album = strdup( N_("(no album)") );
175 snprintf( psz_tmp, GROWL_MAX_LENGTH, "%s %s %s",
176 psz_title, psz_artist, psz_album );
181 NotifyToGrowl( p_this, "Now Playing", "Now Playing", psz_tmp );
183 vlc_object_release( p_input );
187 /*****************************************************************************
188 * Growl specific functions
189 *****************************************************************************/
190 #define GROWL_PROTOCOL_VERSION (1)
191 #define GROWL_TYPE_REGISTRATION (0)
192 #define GROWL_TYPE_NOTIFICATION (1)
193 #define APPLICATION_NAME "VLC media player"
195 #define insertstrlen( psz ) \
197 uint16_t i_size = strlen( psz ); \
198 psz_encoded[i++] = (i_size>>8)&0xFF; \
199 psz_encoded[i++] = i_size&0xFF; \
201 /*****************************************************************************
203 *****************************************************************************/
204 static int RegisterToGrowl( vlc_object_t *p_this )
206 uint8_t *psz_encoded = malloc(100);
207 uint8_t i_defaults = 0;
208 char *psz_notifications[] = {"Now Playing", NULL};
209 vlc_bool_t pb_defaults[] = {VLC_TRUE, VLC_FALSE};
211 if( psz_encoded == NULL )
214 memset( psz_encoded, 0, sizeof(psz_encoded) );
215 psz_encoded[i++] = GROWL_PROTOCOL_VERSION;
216 psz_encoded[i++] = GROWL_TYPE_REGISTRATION;
217 insertstrlen(APPLICATION_NAME);
219 strcpy( (char*)(psz_encoded+i), APPLICATION_NAME );
220 i += strlen(APPLICATION_NAME);
221 for( j = 0 ; psz_notifications[j] != NULL ; j++)
223 insertstrlen(psz_notifications[j]);
224 strcpy( (char*)(psz_encoded+i), psz_notifications[j] );
225 i += strlen(psz_notifications[j]);
228 for( j = 0 ; psz_notifications[j] != NULL ; j++)
229 if(pb_defaults[j] == VLC_TRUE)
231 psz_encoded[i++] = (uint8_t)j;
234 psz_encoded[5] = i_defaults;
236 CheckAndSend(p_this, psz_encoded, i);
241 static int NotifyToGrowl( vlc_object_t *p_this, char *psz_type,
242 char *psz_title, char *psz_desc )
244 uint8_t *psz_encoded = malloc(GROWL_MAX_LENGTH + 42);
247 if( psz_encoded == NULL )
250 memset( psz_encoded, 0, sizeof(psz_encoded) );
251 psz_encoded[i++] = GROWL_PROTOCOL_VERSION;
252 psz_encoded[i++] = GROWL_TYPE_NOTIFICATION;
254 psz_encoded[i++] = (flags>>8)&0xFF;
255 psz_encoded[i++] = flags&0xFF;
256 insertstrlen(psz_type);
257 insertstrlen(psz_title);
258 insertstrlen(psz_desc);
259 insertstrlen(APPLICATION_NAME);
260 strcpy( (char*)(psz_encoded+i), psz_type );
261 i += strlen(psz_type);
262 strcpy( (char*)(psz_encoded+i), psz_title );
263 i += strlen(psz_title);
264 strcpy( (char*)(psz_encoded+i), psz_desc );
265 i += strlen(psz_desc);
266 strcpy( (char*)(psz_encoded+i), APPLICATION_NAME );
267 i += strlen(APPLICATION_NAME);
269 CheckAndSend(p_this, psz_encoded, i);
274 static int CheckAndSend( vlc_object_t *p_this, uint8_t* p_data, int i_offset )
278 intf_thread_t *p_intf = (intf_thread_t *)p_this;
279 char *psz_password = config_GetPsz( p_intf, "growl-password" );
280 char *psz_server = config_GetPsz( p_intf, "growl-server" );
281 int i_port = config_GetInt( p_intf, "growl-port" );
282 strcpy( (char*)(p_data+i_offset), psz_password );
283 i = i_offset + strlen(psz_password);
286 AddMD5( &md5, p_data, i );
289 for( i = 0 ; i < 4 ; i++ )
291 md5.p_digest[i] = md5.p_digest[i];
292 p_data[i_offset++] = md5.p_digest[i] &0xFF;
293 p_data[i_offset++] = (md5.p_digest[i]>> 8)&0xFF;
294 p_data[i_offset++] = (md5.p_digest[i]>>16)&0xFF;
295 p_data[i_offset++] = (md5.p_digest[i]>>24)&0xFF;
298 i_handle = net_ConnectUDP( p_this, psz_server, i_port, 0 );
301 msg_Err( p_this, "failed to open a connection (udp)" );
307 net_StopRecv( i_handle );
308 if( send( i_handle, p_data, i_offset, 0 )
311 msg_Warn( p_this, "send error: %s", strerror(errno) );
313 net_Close( i_handle );
320 #undef GROWL_PROTOCOL_VERSION
321 #undef GROWL_TYPE_REGISTRATION
322 #undef GROWL_TYPE_NOTIFICATION
323 #undef APPLICATION_NAME