1 /*****************************************************************************
2 * msn.c : msn title plugin
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea -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() */
34 /*****************************************************************************
35 * intf_sys_t: description and status of log interface
36 *****************************************************************************/
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Open ( vlc_object_t * );
46 static void Close ( vlc_object_t * );
47 static void Run ( intf_thread_t * );
49 static int ItemChange( vlc_object_t *, const char *,
50 vlc_value_t, vlc_value_t, void * );
51 static int SendToMSN( char * psz_msg );
53 #define MSN_MAX_LENGTH 256
55 /*****************************************************************************
57 *****************************************************************************
58 * This module should be used on windows with MSN (i think that you need to
59 * have MSN 7 or newer) to "advertise" what you are playing in VLC.
60 * You need to enable the "What I'm Listening To" option in MSN.
61 *****************************************************************************/
62 #define FORMAT_DEFAULT "{0} - {1}"
63 /// \bug [String] MSN is useless
64 #define FORMAT_TEXT N_("MSN Title format string")
65 #define FORMAT_LONGTEXT N_("Format of the string to send to MSN " \
66 "{0} Artist, {1} Title, {2} Album. Defaults to \"Artist - Title\" ({0} - {1}).")
69 set_category( CAT_INTERFACE );
70 set_subcategory( SUBCAT_INTERFACE_CONTROL );
71 set_shortname( N_( "MSN" ) );
72 set_description( _("MSN Now-Playing") );
74 add_string( "msn-format", FORMAT_DEFAULT, NULL,
75 FORMAT_TEXT, FORMAT_LONGTEXT, VLC_FALSE );
77 set_capability( "interface", 0 );
78 set_callbacks( Open, Close );
81 /*****************************************************************************
82 * Open: initialize and create stuff
83 *****************************************************************************/
84 static int Open( vlc_object_t *p_this )
86 intf_thread_t *p_intf = (intf_thread_t *)p_this;
87 playlist_t *p_playlist;
89 /* Allocate instance and initialize some members */
90 p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
91 if( p_intf->p_sys == NULL )
93 msg_Err( p_intf, "out of memory" );
97 p_intf->p_sys->psz_format = config_GetPsz( p_intf, "msn-format" );
98 if( !p_intf->p_sys->psz_format )
100 msg_Dbg( p_intf, "no format provided" );
101 p_intf->p_sys->psz_format = strdup( FORMAT_DEFAULT );
103 msg_Dbg( p_intf, "using format: %s", p_intf->p_sys->psz_format );
105 p_playlist = (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
110 msg_Err( p_intf, "could not find playlist object" );
111 free( p_intf->p_sys->psz_format );
112 free( p_intf->p_sys );
116 /* Item's info changes */
117 var_AddCallback( p_playlist, "item-change", ItemChange, p_intf );
118 /* We're playing a new item */
119 var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
120 vlc_object_release( p_playlist );
122 p_intf->pf_run = Run;
127 /*****************************************************************************
128 * Close: destroy interface stuff
129 *****************************************************************************/
130 static void Close( vlc_object_t *p_this )
132 intf_thread_t *p_intf = (intf_thread_t *)p_this;
133 playlist_t *p_playlist = (playlist_t *)vlc_object_find(
134 p_this, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
136 /* clear the MSN stuff ... else it looks like we're still playing
137 * something although VLC (or the MSN plugin) is closed */
138 SendToMSN( "\\0Music\\01\\0\\0\\0\\0\\0\\0\\0" );
142 var_DelCallback( p_playlist, "item-change", ItemChange, p_intf );
143 var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
147 /* Destroy structure */
148 free( p_intf->p_sys->psz_format );
149 free( p_intf->p_sys );
152 /*****************************************************************************
154 *****************************************************************************/
155 static void Run( intf_thread_t *p_intf )
157 msleep( INTF_IDLE_SLEEP );
160 /*****************************************************************************
161 * ItemChange: Playlist item change callback
162 *****************************************************************************/
163 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
164 vlc_value_t oldval, vlc_value_t newval, void *param )
166 intf_thread_t *p_intf = (intf_thread_t *)param;
167 playlist_t *p_playlist;
168 char psz_tmp[MSN_MAX_LENGTH];
169 char *psz_title = NULL;
170 char *psz_artist = NULL;
171 char *psz_album = NULL;
172 input_thread_t *p_input;
174 if( !p_intf->p_sys ) return VLC_SUCCESS;
176 p_playlist = (playlist_t *)vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
179 if( !p_playlist ) return VLC_EGENERIC;
181 p_input = p_playlist->p_input;
182 vlc_object_release( p_playlist );
183 if( !p_input ) return VLC_SUCCESS;
184 vlc_object_yield( p_input );
186 if( p_input->b_dead || !p_input->input.p_item->psz_name )
188 /* Not playing anything ... */
189 SendToMSN( "\\0Music\\01\\0\\0\\0\\0\\0\\0\\0" );
190 vlc_object_release( p_input );
194 /* Playing something ... */
195 psz_artist = vlc_input_item_GetInfo( p_input->input.p_item,
196 _(VLC_META_INFO_CAT),
197 _(VLC_META_ARTIST) );
198 psz_album = vlc_input_item_GetInfo( p_input->input.p_item,
199 _(VLC_META_INFO_CAT),
200 _(VLC_META_COLLECTION) );
201 psz_title = strdup( p_input->input.p_item->psz_name );
202 if( psz_title == NULL ) psz_title = strdup( N_("(no title)") );
203 if( psz_artist == NULL ) psz_artist = strdup( N_("(no artist)") );
204 if( psz_album == NULL ) psz_album = strdup( N_("(no album)") );
207 "\\0Music\\01\\0%s\\0%s\\0%s\\0%s\\0\\0\\0",
208 p_intf->p_sys->psz_format,
216 SendToMSN( psz_tmp );
217 vlc_object_release( p_input );
222 /*****************************************************************************
224 *****************************************************************************/
225 static int SendToMSN( char *psz_msg )
227 COPYDATASTRUCT msndata;
230 wchar_t buffer[MSN_MAX_LENGTH];
232 mbstowcs( buffer, psz_msg, MSN_MAX_LENGTH );
234 msndata.dwData = 0x547;
235 msndata.lpData = &buffer;
236 msndata.cbData = (lstrlenW(buffer)*2)+2;
238 while( ( msnui = FindWindowEx( NULL, msnui, "MsnMsgrUIManager", NULL ) ) )
240 SendMessage(msnui, WM_COPYDATA, (WPARAM)NULL, (LPARAM)&msndata);