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