]> git.sesse.net Git - vlc/blob - modules/misc/notify/growl.c
Input access locking, part 2.
[vlc] / modules / misc / notify / growl.c
1 /*****************************************************************************
2  * growl.c : growl notification plugin
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jérôme Decoodt <djc -at- videolan -dot- org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc_interface.h>
31 #include <vlc_playlist.h>
32 #include <vlc_meta.h>
33 #include <vlc_network.h>
34 #include <errno.h>
35 #include <vlc_md5.h>
36
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40 static int  Open    ( vlc_object_t * );
41 static void Close   ( vlc_object_t * );
42
43 static int ItemChange( vlc_object_t *, const char *,
44                        vlc_value_t, vlc_value_t, void * );
45
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
51
52 /*****************************************************************************
53  * Module descriptor
54  ****************************************************************************/
55
56 #define SERVER_DEFAULT "127.0.0.1"
57 #define SERVER_TEXT N_("Server")
58 #define SERVER_LONGTEXT N_("This is the host to which Growl notifications " \
59    "will be sent. By default, notifications are sent locally." )
60 #define PASS_DEFAULT ""
61 #define PASS_TEXT N_("Password")
62 #define PASS_LONGTEXT N_("Growl password on the Growl server.")
63 #define PORT_TEXT N_("UDP port")
64 #define PORT_LONGTEXT N_("Growl UDP port on the Growl server.")
65
66 vlc_module_begin();
67     set_category( CAT_INTERFACE );
68     set_subcategory( SUBCAT_INTERFACE_CONTROL );
69     set_shortname( "Growl" );
70     set_description( _("Growl Notification Plugin") );
71
72     add_string( "growl-server", SERVER_DEFAULT, NULL,
73                 SERVER_TEXT, SERVER_LONGTEXT, VLC_FALSE );
74     add_string( "growl-password", PASS_DEFAULT, NULL,
75                 PASS_TEXT, PASS_LONGTEXT, VLC_FALSE );
76     add_integer( "growl-port", 9887, NULL,
77                 PORT_TEXT, PORT_LONGTEXT, VLC_TRUE );
78
79     set_capability( "interface", 0 );
80     set_callbacks( Open, Close );
81 vlc_module_end();
82
83 /*****************************************************************************
84  * Open: initialize and create stuff
85  *****************************************************************************/
86 static int Open( vlc_object_t *p_this )
87 {
88     intf_thread_t *p_intf = (intf_thread_t *)p_this;
89
90     playlist_t *p_playlist = pl_Yield( p_intf );
91     var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
92     pl_Release( p_intf );
93
94     RegisterToGrowl( p_this );
95     return VLC_SUCCESS;
96 }
97
98 /*****************************************************************************
99  * Close: destroy interface stuff
100  *****************************************************************************/
101 static void Close( vlc_object_t *p_this )
102 {
103     playlist_t *p_playlist = pl_Yield( p_this );
104     var_DelCallback( p_playlist, "playlist-current", ItemChange, p_this );
105     pl_Release( p_this );
106 }
107
108 /*****************************************************************************
109  * ItemChange: Playlist item change callback
110  *****************************************************************************/
111 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
112                        vlc_value_t oldval, vlc_value_t newval, void *param )
113 {
114     char psz_tmp[GROWL_MAX_LENGTH];
115     char *psz_title = NULL;
116     char *psz_artist = NULL;
117     char *psz_album = NULL;
118     input_thread_t *p_input;
119     playlist_t *p_playlist = pl_Yield( p_this );
120
121     p_input = p_playlist->p_input;
122     vlc_object_release( p_playlist );
123
124     if( !p_input ) return VLC_SUCCESS;
125     vlc_object_yield( p_input );
126
127     char *psz_name = input_item_GetName( input_GetItem( p_input ) );
128     if( p_input->b_dead || !psz_name )
129     {
130         /* Not playing anything ... */
131         free( psz_name );
132         vlc_object_release( p_input );
133         return VLC_SUCCESS;
134     }
135     free( psz_name );
136
137     /* Playing something ... */
138     psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
139     if( psz_artist == NULL ) psz_artist = strdup( "" );
140     psz_album = input_item_GetAlbum( input_GetItem( p_input ) ) ;
141     if( psz_album == NULL ) psz_album = strdup( "" );
142     psz_title = input_item_GetName( input_GetItem( p_input ) );
143     if( psz_title == NULL ) psz_title = strdup( N_("(no title)") );
144     snprintf( psz_tmp, GROWL_MAX_LENGTH, "%s %s %s",
145               psz_title, psz_artist, psz_album );
146     free( psz_title );
147     free( psz_artist );
148     free( psz_album );
149
150     NotifyToGrowl( p_this, "Now Playing", "Now Playing", psz_tmp );
151
152     vlc_object_release( p_input );
153     return VLC_SUCCESS;
154 }
155
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"
163
164 #define insertstrlen( psz ) \
165 { \
166     uint16_t i_size = strlen( psz ); \
167     psz_encoded[i++] = (i_size>>8)&0xFF; \
168     psz_encoded[i++] = i_size&0xFF; \
169 }
170 /*****************************************************************************
171  * RegisterToGrowl
172  *****************************************************************************/
173 static int RegisterToGrowl( vlc_object_t *p_this )
174 {
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};
179     int i = 0, j;
180     if( psz_encoded == NULL )
181         return VLC_FALSE;
182
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);
187     i+=2;
188     strcpy( (char*)(psz_encoded+i), APPLICATION_NAME );
189     i += strlen(APPLICATION_NAME);
190     for( j = 0 ; psz_notifications[j] != NULL ; j++)
191     {
192         insertstrlen(psz_notifications[j]);
193         strcpy( (char*)(psz_encoded+i), psz_notifications[j] );
194         i += strlen(psz_notifications[j]);
195     }
196     psz_encoded[4] = j;
197     for( j = 0 ; psz_notifications[j] != NULL ; j++)
198         if(pb_defaults[j] == VLC_TRUE)
199         {
200             psz_encoded[i++] = (uint8_t)j;
201             i_defaults++;
202         }
203     psz_encoded[5] = i_defaults;
204
205     CheckAndSend(p_this, psz_encoded, i);
206     free( psz_encoded );
207     return VLC_SUCCESS;
208 }
209
210 static int NotifyToGrowl( vlc_object_t *p_this, char *psz_type,
211                             char *psz_title, char *psz_desc )
212 {
213     uint8_t *psz_encoded = malloc(GROWL_MAX_LENGTH + 42);
214     uint16_t flags;
215     int i = 0;
216     if( psz_encoded == NULL )
217         return VLC_FALSE;
218
219     memset( psz_encoded, 0, sizeof(psz_encoded) );
220     psz_encoded[i++] = GROWL_PROTOCOL_VERSION;
221     psz_encoded[i++] = GROWL_TYPE_NOTIFICATION;
222     flags = 0;
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);
237
238     CheckAndSend(p_this, psz_encoded, i);
239     free( psz_encoded );
240     return VLC_SUCCESS;
241 }
242
243 static int CheckAndSend( vlc_object_t *p_this, uint8_t* p_data, int i_offset )
244 {
245     int i, i_handle;
246     struct md5_s md5;
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);
253
254     InitMD5( &md5 );
255     AddMD5( &md5, p_data, i );
256     EndMD5( &md5 );
257
258     for( i = 0 ; i < 4 ; i++ )
259     {
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;
265     }
266
267     i_handle = net_ConnectUDP( p_this, psz_server, i_port, 0 );
268     if( i_handle == -1 )
269     {
270          msg_Err( p_this, "failed to open a connection (udp)" );
271          free( psz_password);
272          free( psz_server);
273          return VLC_EGENERIC;
274     }
275
276     shutdown( i_handle, SHUT_RD );
277     if( send( i_handle, p_data, i_offset, 0 )
278           == -1 )
279     {
280         msg_Warn( p_this, "send error: %s", strerror(errno) );
281     }
282     net_Close( i_handle );
283
284     free( psz_password);
285     free( psz_server);
286     return VLC_SUCCESS;
287 }
288
289 #undef GROWL_PROTOCOL_VERSION
290 #undef GROWL_TYPE_REGISTRATION
291 #undef GROWL_TYPE_NOTIFICATION
292 #undef APPLICATION_NAME
293 #undef insertstrlen