]> git.sesse.net Git - vlc/blob - modules/misc/notify/msn.c
Fix the strings.
[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
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     input_thread_t *p_input;
138     playlist_t *p_playlist = pl_Yield( p_this );
139
140     p_input = p_playlist->p_input;
141     pl_Release( p_this );
142
143     if( !p_input ) return VLC_SUCCESS;
144     vlc_object_yield( p_input );
145
146     if( p_input->b_dead || !input_GetItem(p_input)->psz_name )
147     {
148         /* Not playing anything ... */
149         SendToMSN( "\\0Music\\01\\0\\0\\0\\0\\0\\0\\0" );
150         vlc_object_release( p_input );
151         return VLC_SUCCESS;
152     }
153
154     /* Playing something ... */
155     psz_artist = input_GetItem(p_input)->p_meta->psz_artist ?
156                   strdup( input_GetItem(p_input)->p_meta->psz_artist ) :
157                   strdup( "" );
158     psz_album = input_GetItem(p_input)->p_meta->psz_album ?
159                   strdup( input_GetItem(p_input)->p_meta->psz_album ) :
160                   strdup( "" );
161     psz_title = strdup( input_GetItem(p_input)->psz_name );
162     if( psz_title == NULL ) psz_title = strdup( N_("(no title)") );
163     if( psz_artist == NULL ) psz_artist = strdup( N_("(no artist)") );
164     if( psz_album == NULL ) psz_album = strdup( N_("(no album)") );
165     snprintf( psz_tmp,
166               MSN_MAX_LENGTH,
167               "\\0Music\\01\\0%s\\0%s\\0%s\\0%s\\0\\0\\0",
168               p_intf->p_sys->psz_format,
169               psz_artist,
170               psz_title,
171               psz_album );
172     free( psz_title );
173     free( psz_artist );
174     free( psz_album );
175
176     SendToMSN( psz_tmp );
177     vlc_object_release( p_input );
178
179     return VLC_SUCCESS;
180 }
181
182 /*****************************************************************************
183  * SendToMSN
184  *****************************************************************************/
185 static int SendToMSN( char *psz_msg )
186 {
187     COPYDATASTRUCT msndata;
188     HWND msnui = NULL;
189
190     wchar_t buffer[MSN_MAX_LENGTH];
191
192     mbstowcs( buffer, psz_msg, MSN_MAX_LENGTH );
193
194     msndata.dwData = 0x547;
195     msndata.lpData = &buffer;
196     msndata.cbData = (lstrlenW(buffer)*2)+2;
197
198     while( ( msnui = FindWindowEx( NULL, msnui, "MsnMsgrUIManager", NULL ) ) )
199     {
200         SendMessage(msnui, WM_COPYDATA, (WPARAM)NULL, (LPARAM)&msndata);
201     }
202
203     return VLC_SUCCESS;
204 }