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