]> git.sesse.net Git - vlc/blob - modules/misc/msn.c
[patch] unifying meta-information access, the 2nd by Daniel Stränger
[vlc] / modules / misc / 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/intf.h>
31 #include <vlc_meta.h>
32
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 static void Run     ( intf_thread_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_("MSN Title format string")
64 #define FORMAT_LONGTEXT N_("MSN Title format string. " \
65 "{0} artist, {1} title, {2} album")
66
67 vlc_module_begin();
68     set_category( CAT_INTERFACE );
69     set_subcategory( SUBCAT_INTERFACE_CONTROL );
70     set_shortname( N_( "MSN" ) );
71     set_description( _("MSN Title Plugin") );
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     /* Allocate instance and initialize some members */
89     p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
90     if( p_intf->p_sys == NULL )
91     {
92         msg_Err( p_intf, "out of memory" );
93         return -1;
94     }
95
96     p_intf->p_sys->psz_format = config_GetPsz( 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 = (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
105                                                 FIND_ANYWHERE );
106
107     if( !p_playlist )
108     {
109         msg_Err( p_intf, "could not find playlist object" );
110         free( p_intf->p_sys->psz_format );
111         free( p_intf->p_sys );
112         return VLC_ENOOBJ;
113     }
114
115     var_AddCallback( p_playlist, "item-change", ItemChange, p_intf );
116     vlc_object_release( p_playlist );
117
118     p_intf->pf_run = Run;
119
120     return VLC_SUCCESS;
121 }
122
123 /*****************************************************************************
124  * Close: destroy interface stuff
125  *****************************************************************************/
126 static void Close( vlc_object_t *p_this )
127 {
128     intf_thread_t *p_intf = (intf_thread_t *)p_this;
129     playlist_t *p_playlist = (playlist_t *)vlc_object_find(
130                       p_this, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
131
132     /* clear the MSN stuff ... else it looks like we're still playing
133      * something although VLC (or the MSN plugin) is closed */
134     SendToMSN( "\\0Music\\01\\0\\0\\0\\0\\0\\0\\0" );
135
136     if( p_playlist )
137     {
138         var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
139     }
140
141
142     /* Destroy structure */
143     free( p_intf->p_sys->psz_format );
144     free( p_intf->p_sys );
145 }
146
147 /*****************************************************************************
148  * Run
149  *****************************************************************************/
150 static void Run( intf_thread_t *p_intf )
151 {
152     msleep( INTF_IDLE_SLEEP );
153 }
154
155 /*****************************************************************************
156  * ItemChange: Playlist item change callback
157  *****************************************************************************/
158 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
159                        vlc_value_t oldval, vlc_value_t newval, void *param )
160 {
161     intf_thread_t *p_intf = (intf_thread_t *)param;
162     playlist_t *p_playlist;
163     char psz_tmp[MSN_MAX_LENGTH];
164     char *psz_title = NULL;
165     char *psz_artist = NULL;
166     char *psz_album = NULL;
167     input_thread_t *p_input;
168
169     int i,j;
170
171     if( !p_intf->p_sys ) return VLC_SUCCESS;
172
173      p_playlist = (playlist_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
174                                                  FIND_ANYWHERE );
175
176      if( !p_playlist ) return VLC_EGENERIC;
177
178      p_input = p_playlist->p_input;
179      vlc_object_release( p_playlist );
180      if( !p_input ) return VLC_SUCCESS;
181      vlc_object_yield( p_input );
182
183     if( p_input->b_dead || !p_input->input.p_item->psz_name )
184     {
185         /* Not playing anything ... */
186         SendToMSN( "\\0Music\\01\\0\\0\\0\\0\\0\\0\\0" );
187         vlc_object_release( p_input );
188         return VLC_SUCCESS;
189     }
190
191     /* Playing something ... */
192     psz_artist = vlc_input_item_GetInfo( p_input->input.p_item,
193                                          _(VLC_META_INFO_CAT),
194                                          _(VLC_META_ARTIST) );
195     psz_album = vlc_input_item_GetInfo( p_input->input.p_item,
196                                          _(VLC_META_INFO_CAT),
197                                          _(VLC_META_COLLECTION) );
198     psz_title = strdup( p_input->input.p_item->psz_name );
199     if( psz_title == NULL ) psz_title = strdup( N_("(no title)") );
200     if( psz_artist == NULL ) psz_artist = strdup( N_("(no artist)") );
201     if( psz_album == NULL ) psz_album = strdup( N_("(no album)") );
202     snprintf( psz_tmp,
203               MSN_MAX_LENGTH,
204               "\\0Music\\01\\0%s\\0%s\\0%s\\0%s\\0\\0\\0",
205               p_intf->p_sys->psz_format,
206               //FORMAT_DEFAULT,
207               psz_title,
208               psz_artist,
209               psz_album );
210     free( psz_title );
211     free( psz_artist );
212     free( psz_album );
213
214     SendToMSN( psz_tmp );
215     vlc_object_release( p_input );
216
217     return VLC_SUCCESS;
218 }
219
220 /*****************************************************************************
221  * SendToMSN
222  *****************************************************************************/
223 static int SendToMSN( char *psz_msg )
224 {
225     COPYDATASTRUCT msndata;
226     HWND msnui = NULL;
227
228     wchar_t buffer[MSN_MAX_LENGTH];
229
230     mbstowcs( buffer, psz_msg, MSN_MAX_LENGTH );
231
232     msndata.dwData = 0x547;
233     msndata.lpData = &buffer;
234     msndata.cbData = (lstrlenW(buffer)*2)+2;
235
236     while( ( msnui = FindWindowEx( NULL, msnui, "MsnMsgrUIManager", NULL ) ) )
237     {
238         SendMessage(msnui, WM_COPYDATA, (WPARAM)NULL, (LPARAM)&msndata);
239     }
240
241     return VLC_SUCCESS;
242 }