]> git.sesse.net Git - vlc/blob - modules/misc/notify/msn.c
796907d951ef8d2127ea1c1c026ad512e7dc75ef
[vlc] / modules / misc / notify / msn.c
1 /*****************************************************************************
2  * msn.c : msn title plugin
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea -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
28 #include <vlc/vlc.h>
29 #include <vlc_interface.h>
30 #include <vlc_meta.h>
31 #include <vlc_playlist.h>
32 #include <vlc_strings.h>
33
34 /*****************************************************************************
35  * intf_sys_t: description and status of log interface
36  *****************************************************************************/
37 struct intf_sys_t
38 {
39     char *psz_format;
40 };
41
42 /*****************************************************************************
43  * Local prototypes
44  *****************************************************************************/
45 static int  Open    ( vlc_object_t * );
46 static void Close   ( vlc_object_t * );
47
48 static int ItemChange( vlc_object_t *, const char *,
49                        vlc_value_t, vlc_value_t, void * );
50 static int SendToMSN( char * psz_msg );
51
52 #define MSN_MAX_LENGTH 256
53
54 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************
57  * This module should be used on windows with MSN (i think that you need to
58  * have MSN 7 or newer) to "advertise" what you are playing in VLC.
59  * You need to enable the "What I'm Listening To" option in MSN.
60  *****************************************************************************/
61 #define FORMAT_DEFAULT "{0} - {1}"
62 #define FORMAT_TEXT N_("Title format string")
63 #define FORMAT_LONGTEXT N_("Format of the string to send to MSN " \
64 "{0} Artist, {1} Title, {2} Album. Defaults to \"Artist - Title\" ({0} - {1}).")
65
66 vlc_module_begin();
67     set_category( CAT_INTERFACE );
68     set_subcategory( SUBCAT_INTERFACE_CONTROL );
69     set_shortname( "MSN" );
70     set_description( _("MSN Now-Playing") );
71
72     add_string( "msn-format", FORMAT_DEFAULT, NULL,
73                 FORMAT_TEXT, FORMAT_LONGTEXT, VLC_FALSE );
74
75     set_capability( "interface", 0 );
76     set_callbacks( Open, Close );
77 vlc_module_end();
78
79 /*****************************************************************************
80  * Open: initialize and create stuff
81  *****************************************************************************/
82 static int Open( vlc_object_t *p_this )
83 {
84     intf_thread_t *p_intf = (intf_thread_t *)p_this;
85     playlist_t *p_playlist;
86
87     MALLOC_ERR( p_intf->p_sys, intf_sys_t );
88
89     p_intf->p_sys->psz_format = config_GetPsz( p_intf, "msn-format" );
90     if( !p_intf->p_sys->psz_format )
91     {
92         msg_Dbg( p_intf, "no format provided" );
93         p_intf->p_sys->psz_format = strdup( FORMAT_DEFAULT );
94     }
95     msg_Dbg( p_intf, "using format: %s", p_intf->p_sys->psz_format );
96
97     p_playlist = pl_Yield( p_intf );
98     var_AddCallback( p_playlist, "item-change", ItemChange, p_intf );
99     var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
100     pl_Release( p_intf );
101
102     return VLC_SUCCESS;
103 }
104
105 /*****************************************************************************
106  * Close: destroy interface stuff
107  *****************************************************************************/
108 static void Close( vlc_object_t *p_this )
109 {
110     intf_thread_t *p_intf = (intf_thread_t *)p_this;
111     playlist_t *p_playlist = pl_Yield( p_this );
112
113     /* clear the MSN stuff ... else it looks like we're still playing
114      * something although VLC (or the MSN plugin) is closed */
115     SendToMSN( "\\0Music\\01\\0\\0\\0\\0\\0\\0\\0" );
116
117     var_DelCallback( p_playlist, "item-change", ItemChange, p_intf );
118     var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
119     pl_Release( p_this );
120
121     /* Destroy structure */
122     free( p_intf->p_sys->psz_format );
123     free( p_intf->p_sys );
124 }
125
126 /*****************************************************************************
127  * ItemChange: Playlist item change callback
128  *****************************************************************************/
129 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
130                        vlc_value_t oldval, vlc_value_t newval, void *param )
131 {
132     intf_thread_t *p_intf = (intf_thread_t *)param;
133     char psz_tmp[MSN_MAX_LENGTH];
134     char *psz_title = NULL;
135     char *psz_artist = NULL;
136     char *psz_album = NULL;
137     char *psz_buf = NULL;
138     input_thread_t *p_input;
139     playlist_t *p_playlist = pl_Yield( p_this );
140
141     p_input = p_playlist->p_input;
142     pl_Release( p_this );
143
144     if( !p_input ) return VLC_SUCCESS;
145     vlc_object_yield( p_input );
146
147     if( p_input->b_dead || !input_GetItem(p_input)->psz_name )
148     {
149         /* Not playing anything ... */
150         SendToMSN( "\\0Music\\01\\0\\0\\0\\0\\0\\0\\0" );
151         vlc_object_release( p_input );
152         return VLC_SUCCESS;
153     }
154
155     /* Playing something ... */
156     psz_artist = input_item_GetArtist( input_GetItem(p_input) );
157     psz_album = input_item_GetAlbum( input_GetItem(p_input) );
158     psz_title = input_item_GetTitle( input_GetItem(p_input) );
159     if( !psz_artist ) psz_artist = strdup( "" );
160     if( !psz_album ) psz_artist = strdup( "" );
161     if( !strcmp( psz_title, NULL ) )
162         psz_title = strdup( N_("(no title)") );
163
164     psz_buf = str_format_meta( p_this, p_intf->p_sys->psz_format );
165
166     snprintf( psz_tmp,
167               MSN_MAX_LENGTH,
168               "\\0Music\\01\\0%s\\0%s\\0%s\\0%s\\0\\0\\0",
169               psz_buf,
170               psz_artist,
171               psz_title,
172               psz_album );
173     free( psz_buf );
174     free( psz_title );
175     free( psz_artist );
176     free( psz_album );
177
178     SendToMSN( psz_tmp );
179     vlc_object_release( p_input );
180
181     return VLC_SUCCESS;
182 }
183
184 /*****************************************************************************
185  * SendToMSN
186  *****************************************************************************/
187 static int SendToMSN( char *psz_msg )
188 {
189     COPYDATASTRUCT msndata;
190     HWND msnui = NULL;
191
192     wchar_t buffer[MSN_MAX_LENGTH];
193
194     mbstowcs( buffer, psz_msg, MSN_MAX_LENGTH );
195
196     msndata.dwData = 0x547;
197     msndata.lpData = &buffer;
198     msndata.cbData = (lstrlenW(buffer)*2)+2;
199
200     while( ( msnui = FindWindowEx( NULL, msnui, "MsnMsgrUIManager", NULL ) ) )
201     {
202         SendMessage(msnui, WM_COPYDATA, (WPARAM)NULL, (LPARAM)&msndata);
203     }
204
205     return VLC_SUCCESS;
206 }