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 * );
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 /// \bug [String] REmove all "Growl" in short desc
57 #define SERVER_DEFAULT "127.0.0.1"
58 #define SERVER_TEXT N_("Growl server")
59 #define SERVER_LONGTEXT N_("This is the host to which Growl notifications " \
60 "will be sent. By default, notifications are sent locally." )
61 #define PASS_DEFAULT ""
62 #define PASS_TEXT N_("Growl password")
63 /// \bug [String] Password on the Growl server.
64 #define PASS_LONGTEXT N_("Growl password on the server.")
65 #define PORT_TEXT N_("Growl UDP port")
66 /// \bug [String] UDP port on the Growl server
67 #define PORT_LONGTEXT N_("Growl UDP port on the server.")
70 set_category( CAT_INTERFACE );
71 set_subcategory( SUBCAT_INTERFACE_CONTROL );
72 set_shortname( _( "Growl" ) );
73 set_description( _("Growl Notification Plugin") );
75 add_string( "growl-server", SERVER_DEFAULT, NULL,
76 SERVER_TEXT, SERVER_LONGTEXT, VLC_FALSE );
77 add_string( "growl-password", PASS_DEFAULT, NULL,
78 PASS_TEXT, PASS_LONGTEXT, VLC_FALSE );
79 add_integer( "growl-port", 9887, NULL,
80 PORT_TEXT, PORT_LONGTEXT, VLC_TRUE );
82 set_capability( "interface", 0 );
83 set_callbacks( Open, Close );
86 /*****************************************************************************
87 * Open: initialize and create stuff
88 *****************************************************************************/
89 static int Open( vlc_object_t *p_this )
91 intf_thread_t *p_intf = (intf_thread_t *)p_this;
93 playlist_t *p_playlist = pl_Yield( p_intf );
94 var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
97 RegisterToGrowl( p_this );
101 /*****************************************************************************
102 * Close: destroy interface stuff
103 *****************************************************************************/
104 static void Close( vlc_object_t *p_this )
106 playlist_t *p_playlist = pl_Yield( p_this );
107 var_DelCallback( p_playlist, "playlist-current", ItemChange, p_this );
108 pl_Release( p_this );
111 /*****************************************************************************
112 * ItemChange: Playlist item change callback
113 *****************************************************************************/
114 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
115 vlc_value_t oldval, vlc_value_t newval, void *param )
117 char psz_tmp[GROWL_MAX_LENGTH];
118 char *psz_title = NULL;
119 char *psz_artist = NULL;
120 char *psz_album = NULL;
121 input_thread_t *p_input;
122 playlist_t *p_playlist = pl_Yield( p_this );
124 p_input = p_playlist->p_input;
125 vlc_object_release( p_playlist );
127 if( !p_input ) return VLC_SUCCESS;
128 vlc_object_yield( p_input );
130 if( p_input->b_dead || !p_input->input.p_item->psz_name )
132 /* Not playing anything ... */
133 vlc_object_release( p_input );
137 /* Playing something ... */
138 psz_artist = p_input->input.p_item->p_meta->psz_artist ?
139 strdup( p_input->input.p_item->p_meta->psz_artist ) :
141 psz_album = p_input->input.p_item->p_meta->psz_album ?
142 strdup( p_input->input.p_item->p_meta->psz_album ) :
144 psz_title = strdup( p_input->input.p_item->psz_name );
145 if( psz_title == NULL ) psz_title = strdup( N_("(no title)") );
146 if( psz_artist == NULL ) psz_artist = strdup( N_("(no artist)") );
147 if( psz_album == NULL ) psz_album = strdup( N_("(no album)") );
148 snprintf( psz_tmp, GROWL_MAX_LENGTH, "%s %s %s",
149 psz_title, psz_artist, psz_album );
154 NotifyToGrowl( p_this, "Now Playing", "Now Playing", psz_tmp );
156 vlc_object_release( p_input );
160 /*****************************************************************************
161 * Growl specific functions
162 *****************************************************************************/
163 #define GROWL_PROTOCOL_VERSION (1)
164 #define GROWL_TYPE_REGISTRATION (0)
165 #define GROWL_TYPE_NOTIFICATION (1)
166 #define APPLICATION_NAME "VLC media player"
168 #define insertstrlen( psz ) \
170 uint16_t i_size = strlen( psz ); \
171 psz_encoded[i++] = (i_size>>8)&0xFF; \
172 psz_encoded[i++] = i_size&0xFF; \
174 /*****************************************************************************
176 *****************************************************************************/
177 static int RegisterToGrowl( vlc_object_t *p_this )
179 uint8_t *psz_encoded = malloc(100);
180 uint8_t i_defaults = 0;
181 char *psz_notifications[] = {"Now Playing", NULL};
182 vlc_bool_t pb_defaults[] = {VLC_TRUE, VLC_FALSE};
184 if( psz_encoded == NULL )
187 memset( psz_encoded, 0, sizeof(psz_encoded) );
188 psz_encoded[i++] = GROWL_PROTOCOL_VERSION;
189 psz_encoded[i++] = GROWL_TYPE_REGISTRATION;
190 insertstrlen(APPLICATION_NAME);
192 strcpy( (char*)(psz_encoded+i), APPLICATION_NAME );
193 i += strlen(APPLICATION_NAME);
194 for( j = 0 ; psz_notifications[j] != NULL ; j++)
196 insertstrlen(psz_notifications[j]);
197 strcpy( (char*)(psz_encoded+i), psz_notifications[j] );
198 i += strlen(psz_notifications[j]);
201 for( j = 0 ; psz_notifications[j] != NULL ; j++)
202 if(pb_defaults[j] == VLC_TRUE)
204 psz_encoded[i++] = (uint8_t)j;
207 psz_encoded[5] = i_defaults;
209 CheckAndSend(p_this, psz_encoded, i);
214 static int NotifyToGrowl( vlc_object_t *p_this, char *psz_type,
215 char *psz_title, char *psz_desc )
217 uint8_t *psz_encoded = malloc(GROWL_MAX_LENGTH + 42);
220 if( psz_encoded == NULL )
223 memset( psz_encoded, 0, sizeof(psz_encoded) );
224 psz_encoded[i++] = GROWL_PROTOCOL_VERSION;
225 psz_encoded[i++] = GROWL_TYPE_NOTIFICATION;
227 psz_encoded[i++] = (flags>>8)&0xFF;
228 psz_encoded[i++] = flags&0xFF;
229 insertstrlen(psz_type);
230 insertstrlen(psz_title);
231 insertstrlen(psz_desc);
232 insertstrlen(APPLICATION_NAME);
233 strcpy( (char*)(psz_encoded+i), psz_type );
234 i += strlen(psz_type);
235 strcpy( (char*)(psz_encoded+i), psz_title );
236 i += strlen(psz_title);
237 strcpy( (char*)(psz_encoded+i), psz_desc );
238 i += strlen(psz_desc);
239 strcpy( (char*)(psz_encoded+i), APPLICATION_NAME );
240 i += strlen(APPLICATION_NAME);
242 CheckAndSend(p_this, psz_encoded, i);
247 static int CheckAndSend( vlc_object_t *p_this, uint8_t* p_data, int i_offset )
251 intf_thread_t *p_intf = (intf_thread_t *)p_this;
252 char *psz_password = config_GetPsz( p_intf, "growl-password" );
253 char *psz_server = config_GetPsz( p_intf, "growl-server" );
254 int i_port = config_GetInt( p_intf, "growl-port" );
255 strcpy( (char*)(p_data+i_offset), psz_password );
256 i = i_offset + strlen(psz_password);
259 AddMD5( &md5, p_data, i );
262 for( i = 0 ; i < 4 ; i++ )
264 md5.p_digest[i] = md5.p_digest[i];
265 p_data[i_offset++] = md5.p_digest[i] &0xFF;
266 p_data[i_offset++] = (md5.p_digest[i]>> 8)&0xFF;
267 p_data[i_offset++] = (md5.p_digest[i]>>16)&0xFF;
268 p_data[i_offset++] = (md5.p_digest[i]>>24)&0xFF;
271 i_handle = net_ConnectUDP( p_this, psz_server, i_port, 0 );
274 msg_Err( p_this, "failed to open a connection (udp)" );
280 net_StopRecv( i_handle );
281 if( send( i_handle, p_data, i_offset, 0 )
284 msg_Warn( p_this, "send error: %s", strerror(errno) );
286 net_Close( i_handle );
293 #undef GROWL_PROTOCOL_VERSION
294 #undef GROWL_TYPE_REGISTRATION
295 #undef GROWL_TYPE_NOTIFICATION
296 #undef APPLICATION_NAME