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