]> git.sesse.net Git - vlc/blob - mozilla/vlcpeer.cpp
backport of r13238
[vlc] / mozilla / vlcpeer.cpp
1 /*****************************************************************************
2  * vlcpeer.cpp: scriptable peer descriptor
3  *****************************************************************************
4  * Copyright (C) 2002-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include "config.h"
28
29 #include <vlc/vlc.h>
30
31 #ifdef DEBUG
32 /* We do not want to use nsDebug.h */
33 #   undef DEBUG
34 #endif
35
36 #ifdef HAVE_MOZILLA_CONFIG_H
37 #   include <mozilla-config.h>
38 #endif
39 #include <nsISupports.h>
40 #include <nsMemory.h>
41 #include <npapi.h>
42
43 #if !defined(XP_MACOSX) && !defined(XP_UNIX) && !defined(XP_WIN)
44 #define XP_UNIX 1
45 #elif defined(XP_MACOSX)
46 #undef XP_UNIX
47 #endif
48
49 #include "vlcpeer.h"
50 #include "vlcplugin.h"
51
52 NS_IMPL_ISUPPORTS2( VlcPeer, VlcIntf, nsIClassInfo )
53
54 /*****************************************************************************
55  * Scriptable peer constructor and destructor
56  *****************************************************************************/
57 VlcPeer::VlcPeer()
58 {
59     NS_INIT_ISUPPORTS();
60 }
61
62 VlcPeer::VlcPeer( VlcPlugin * plugin )
63 {
64     NS_INIT_ISUPPORTS();
65     p_plugin = plugin;
66 }
67
68 VlcPeer::~VlcPeer()
69 {
70     ;
71 }
72
73 /*****************************************************************************
74  * Scriptable peer methods
75  *****************************************************************************/
76 void VlcPeer::Disable()
77 {
78     p_plugin = NULL;
79 }
80
81 /*****************************************************************************
82  * Scriptable peer plugin methods
83  *****************************************************************************/
84 NS_IMETHODIMP VlcPeer::Play()
85 {
86     if( p_plugin )
87     {
88         if( !p_plugin->b_stream && p_plugin->psz_target )
89         {
90             VLC_AddTarget( p_plugin->i_vlc, p_plugin->psz_target, 0, 0,
91                            PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
92             p_plugin->b_stream = 1;
93         }
94
95         VLC_Play( p_plugin->i_vlc );
96     }
97     return NS_OK;
98 }
99
100 NS_IMETHODIMP VlcPeer::Pause()
101 {
102     if( p_plugin )
103     {
104         VLC_Pause( p_plugin->i_vlc );
105     }
106     return NS_OK;
107 }
108
109 NS_IMETHODIMP VlcPeer::Stop()
110 {
111     if( p_plugin )
112     {
113         VLC_Stop( p_plugin->i_vlc );
114         p_plugin->b_stream = 0;
115     }
116     return NS_OK;
117 }
118
119 NS_IMETHODIMP VlcPeer::Fullscreen()
120 {
121     if( p_plugin )
122     {
123 #ifdef XP_MACOSX
124 #else
125         VLC_FullScreen( p_plugin->i_vlc );
126 #endif
127     }
128     return NS_OK;
129 }
130
131 /* Set/Get vlc variables */
132 NS_IMETHODIMP VlcPeer::Set_int_variable(const char *psz_var, PRInt64 value )
133 {
134     vlc_value_t val;
135     val.i_int = value;
136     if( p_plugin )
137     {
138         VLC_VariableSet( p_plugin->i_vlc, psz_var, val );
139     }
140     return NS_OK;
141 }
142
143 NS_IMETHODIMP VlcPeer::Set_str_variable(const char *psz_var, const char *value )
144 {
145     vlc_value_t val;
146     val.psz_string = strdup( value );
147     if( p_plugin )
148     {
149         VLC_VariableSet( p_plugin->i_vlc, psz_var, val );
150     }
151     return NS_OK;
152 }
153
154 NS_IMETHODIMP VlcPeer::Set_bool_variable(const char *psz_var, PRBool value )
155 {
156     vlc_value_t val;
157     val.b_bool = value >= 1 ? VLC_TRUE : VLC_FALSE;
158     if( p_plugin )
159     {
160         VLC_VariableSet( p_plugin->i_vlc, psz_var, val );
161     }
162     return NS_OK;
163 }
164
165 NS_IMETHODIMP VlcPeer::Get_int_variable( const char *psz_var, PRInt64 *result )
166 {
167     vlc_value_t val;
168     if( p_plugin )
169     {
170         fprintf(stderr, "Choppage de %s\n", psz_var );
171         VLC_VariableGet( p_plugin->i_vlc, psz_var, &val );
172         fprintf(stderr, "Valeur %i\n", val.i_int );
173         *result = (PRInt64)val.i_int;
174     }
175     return NS_OK;
176 }
177
178 NS_IMETHODIMP VlcPeer::Get_bool_variable(  const char *psz_var,PRBool *result )
179 {
180     vlc_value_t val;
181     if( p_plugin )
182     {
183         VLC_VariableGet( p_plugin->i_vlc, psz_var, &val );
184         *result = (PRBool)val.b_bool;
185     }
186     return NS_OK;
187 }
188
189 NS_IMETHODIMP VlcPeer::Get_str_variable( const char *psz_var, char **result )
190 {
191     vlc_value_t val;
192     if( p_plugin )
193     {
194         fprintf(stderr, "Choppage de %s\n", psz_var );
195         VLC_VariableGet( p_plugin->i_vlc, psz_var, &val );
196         if( val.psz_string )
197         {
198             *result = strdup( val.psz_string );
199         }
200         else
201         {
202             *result = strdup( "" );
203         }
204     }
205     return NS_OK;
206 }
207
208 /* Playlist control */
209 NS_IMETHODIMP VlcPeer::Clear_playlist()
210 {
211     if( p_plugin )
212     {
213         VLC_PlaylistClear( p_plugin->i_vlc );
214     }
215     return NS_OK;
216 }
217
218 NS_IMETHODIMP VlcPeer::Add_item( const char *psz_item )
219 {
220      if( p_plugin )
221      {
222           VLC_AddTarget( p_plugin->i_vlc, psz_item, NULL, 0,
223                          PLAYLIST_APPEND, PLAYLIST_END);
224      }
225      return NS_OK;
226 }
227
228
229 NS_IMETHODIMP VlcPeer::Isplaying( PRBool *b_playing )
230 {
231     if( p_plugin->i_vlc )
232     {
233         *b_playing = VLC_IsPlaying( p_plugin->i_vlc );
234     }
235     return NS_OK;
236 }
237
238 NS_IMETHODIMP VlcPeer::Get_position( PRInt64 *i_position )
239 {
240     if( p_plugin->i_vlc )
241     {
242         *i_position = (PRInt64)VLC_PositionGet( p_plugin->i_vlc );
243     }
244     return NS_OK;
245 }
246
247 NS_IMETHODIMP VlcPeer::Get_time( PRInt64 *i_time )
248 {
249     if( p_plugin->i_vlc )
250     {
251         *i_time = VLC_TimeGet( p_plugin->i_vlc );
252     }
253     return NS_OK;
254 }
255
256 NS_IMETHODIMP VlcPeer::Get_length( PRInt64 *i_length )
257 {
258     if( p_plugin->i_vlc )
259     {
260         *i_length = VLC_LengthGet( p_plugin->i_vlc );
261     }
262     return NS_OK;
263 }
264
265 NS_IMETHODIMP VlcPeer::Seek( PRInt64 i_secs, PRInt64 b_relative )
266 {
267     if( p_plugin->i_vlc )
268     {
269         VLC_TimeSet( p_plugin->i_vlc, i_secs, b_relative );
270     }
271     return NS_OK;
272 }
273
274 NS_IMETHODIMP VlcPeer::Next()
275 {
276     if( p_plugin->i_vlc )
277     {
278         VLC_PlaylistNext( p_plugin->i_vlc);
279     }
280     return NS_OK;
281 }
282
283 NS_IMETHODIMP VlcPeer::Previous()
284 {
285     if( p_plugin->i_vlc )
286     {
287         VLC_PlaylistPrev( p_plugin->i_vlc );
288     }
289     return NS_OK;
290 }
291
292 NS_IMETHODIMP VlcPeer::Set_volume( PRInt64 i_volume )
293 {
294     if( p_plugin->i_vlc )
295     {
296         VLC_VolumeSet( p_plugin->i_vlc, i_volume );
297     }
298     return NS_OK;
299 }
300
301 NS_IMETHODIMP VlcPeer::Get_volume( PRInt64 *i_volume )
302 {
303     if( p_plugin->i_vlc )
304     {
305         *i_volume = VLC_VolumeGet( p_plugin->i_vlc );
306     }
307     return NS_OK;
308 }
309
310 NS_IMETHODIMP VlcPeer::Mute()
311 {
312     if( p_plugin->i_vlc )
313     {
314         VLC_VolumeMute( p_plugin->i_vlc );
315     }
316     return NS_OK;
317 }