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 *****************************************************************************
55 * This module should be used on windows with MSN (i think that you need to
56 * have MSN 7 or newer) to "advertise" what you are playing in VLC.
57 * You need to enable the "What I'm Listening To" option in MSN.
58 *****************************************************************************/
59 #define PORT_TEXT N_("Growl UDP port")
60 #define PORT_LONGTEXT N_("Growl UPD port on the server.")
61 #define SERVER_DEFAULT "127.0.0.1"
62 #define SERVER_TEXT N_("Growl server")
63 #define SERVER_LONGTEXT N_("Growl server receiving notifications.")
64 #define PASS_DEFAULT ""
65 #define PASS_TEXT N_("Growl password")
66 #define PASS_LONGTEXT N_("Growl password on the server.")
69 set_category( CAT_INTERFACE );
70 set_subcategory( SUBCAT_INTERFACE_CONTROL );
71 set_shortname( N_( "growl" ) );
72 set_description( _("Growl Notification Plugin") );
74 add_integer( "growl-port", 9887, NULL,
75 PORT_TEXT, PORT_LONGTEXT, VLC_TRUE );
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 );
81 set_capability( "interface", 0 );
82 set_callbacks( Open, Close );
85 /*****************************************************************************
86 * Open: initialize and create stuff
87 *****************************************************************************/
88 static int Open( vlc_object_t *p_this )
90 intf_thread_t *p_intf = (intf_thread_t *)p_this;
92 playlist_t *p_playlist = (playlist_t *)vlc_object_find(
93 p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
97 msg_Err( p_intf, "could not find playlist object" );
101 var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
102 vlc_object_release( p_playlist );
104 RegisterToGrowl( p_this );
105 p_intf->pf_run = Run;
110 /*****************************************************************************
111 * Close: destroy interface stuff
112 *****************************************************************************/
113 static void Close( vlc_object_t *p_this )
117 /*****************************************************************************
119 *****************************************************************************/
120 static void Run( intf_thread_t *p_intf )
122 msleep( INTF_IDLE_SLEEP );
125 /*****************************************************************************
126 * ItemChange: Playlist item change callback
127 *****************************************************************************/
128 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
129 vlc_value_t oldval, vlc_value_t newval, void *param )
131 char psz_tmp[GROWL_MAX_LENGTH];
132 char *psz_title = NULL;
133 char *psz_artist = NULL;
134 char *psz_album = NULL;
136 input_thread_t *p_input =
137 (input_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
139 if( !p_input || p_input->b_dead || !p_input->input.p_item->psz_name )
141 /* Not playing anything ... */
145 /* Playing something ... */
146 psz_artist = vlc_input_item_GetInfo( p_input->input.p_item,
147 _("Meta-information"),
149 psz_album = vlc_input_item_GetInfo( p_input->input.p_item,
150 _("Meta-information"),
151 _("Album/movie/show title" ) );
152 psz_title = strdup( p_input->input.p_item->psz_name );
153 if( psz_title == NULL ) psz_title = strdup( N_("(no title)") );
154 if( psz_artist == NULL ) psz_artist = strdup( N_("(no artist)") );
155 if( psz_album == NULL ) psz_album = strdup( N_("(no album)") );
156 snprintf( psz_tmp, GROWL_MAX_LENGTH, "%s %s %s",
157 psz_title, psz_artist, psz_album );
162 NotifyToGrowl( p_this, "Now Playing", "Now Playing", psz_tmp );
164 vlc_object_release( p_input );
168 /*****************************************************************************
169 * Growl specific functions
170 *****************************************************************************/
171 #define GROWL_PROTOCOL_VERSION (1)
172 #define GROWL_TYPE_REGISTRATION (0)
173 #define GROWL_TYPE_NOTIFICATION (1)
174 #define APPLICATION_NAME "VLC media player"
176 #define insertstrlen( psz ) \
178 uint16_t i_size = htons(strlen( psz )); \
179 psz_encoded[i++] = (i_size>>8)&0xFF; \
180 psz_encoded[i++] = i_size&0xFF; \
182 /*****************************************************************************
184 *****************************************************************************/
185 static int RegisterToGrowl( vlc_object_t *p_this )
187 uint8_t *psz_encoded = malloc(100);
188 uint8_t i_defaults = 0;
189 char *psz_notifications[] = {"Now Playing", NULL};
190 vlc_bool_t pb_defaults[] = {VLC_TRUE, VLC_FALSE};
192 if( psz_encoded == NULL )
195 memset( psz_encoded, 0, sizeof(psz_encoded) );
196 psz_encoded[i++] = GROWL_PROTOCOL_VERSION;
197 psz_encoded[i++] = GROWL_TYPE_REGISTRATION;
198 insertstrlen(APPLICATION_NAME);
200 strcpy( (char*)(psz_encoded+i), APPLICATION_NAME );
201 i += strlen(APPLICATION_NAME);
202 for( j = 0 ; psz_notifications[j] != NULL ; j++)
204 insertstrlen(psz_notifications[j]);
205 strcpy( (char*)(psz_encoded+i), psz_notifications[j] );
206 i += strlen(psz_notifications[j]);
209 for( j = 0 ; psz_notifications[j] != NULL ; j++)
210 if(pb_defaults[j] == VLC_TRUE)
212 psz_encoded[i++] = (uint8_t)j;
215 psz_encoded[5] = i_defaults;
217 CheckAndSend(p_this, psz_encoded, i);
222 static int NotifyToGrowl( vlc_object_t *p_this, char *psz_type,
223 char *psz_title, char *psz_desc )
225 uint8_t *psz_encoded = malloc(GROWL_MAX_LENGTH + 42);
228 if( psz_encoded == NULL )
231 memset( psz_encoded, 0, sizeof(psz_encoded) );
232 psz_encoded[i++] = GROWL_PROTOCOL_VERSION;
233 psz_encoded[i++] = GROWL_TYPE_NOTIFICATION;
235 psz_encoded[i++] = (flags>>8)&0xFF;
236 psz_encoded[i++] = flags&0xFF;
237 insertstrlen(psz_type);
238 insertstrlen(psz_title);
239 insertstrlen(psz_desc);
240 insertstrlen(APPLICATION_NAME);
241 strcpy( (char*)(psz_encoded+i), psz_type );
242 i += strlen(psz_type);
243 strcpy( (char*)(psz_encoded+i), psz_title );
244 i += strlen(psz_title);
245 strcpy( (char*)(psz_encoded+i), psz_desc );
246 i += strlen(psz_desc);
247 strcpy( (char*)(psz_encoded+i), APPLICATION_NAME );
248 i += strlen(APPLICATION_NAME);
250 CheckAndSend(p_this, psz_encoded, i);
255 static int CheckAndSend( vlc_object_t *p_this, uint8_t* p_data, int i_offset )
259 intf_thread_t *p_intf = (intf_thread_t *)p_this;
260 char *psz_password = config_GetPsz( p_intf, "growl-password" );
261 char *psz_server = config_GetPsz( p_intf, "growl-server" );
262 int i_port = config_GetInt( p_intf, "growl-port" );
263 strcpy( (char*)(p_data+i_offset), psz_password );
264 i = i_offset + strlen(psz_password);
267 AddMD5( &md5, p_data, i );
270 for( i = 0 ; i < 4 ; i++ )
272 md5.p_digest[i] = htonl(md5.p_digest[i]);
273 p_data[i_offset++] = md5.p_digest[i] &0xFF;
274 p_data[i_offset++] = (md5.p_digest[i]>> 8)&0xFF;
275 p_data[i_offset++] = (md5.p_digest[i]>>16)&0xFF;
276 p_data[i_offset++] = (md5.p_digest[i]>>24)&0xFF;
279 i_handle = net_ConnectUDP( p_this, psz_server, i_port, 42 /*TTL*/ );
282 msg_Err( p_this, "failed to open a connection (udp)" );
288 net_StopRecv( i_handle );
289 if( send( i_handle, p_data, i_offset, 0 )
292 msg_Warn( p_this, "send error: %s", strerror(errno) );
294 net_Close( i_handle );
301 #undef GROWL_PROTOCOL_VERSION
302 #undef GROWL_TYPE_REGISTRATION
303 #undef GROWL_TYPE_NOTIFICATION
304 #undef APPLICATION_NAME