]> git.sesse.net Git - vlc/blob - mozilla/vlcruntime.cpp
The Debian packaging data have been moved to svn.v.o/pkg-multimedia
[vlc] / mozilla / vlcruntime.cpp
1 /*****************************************************************************
2  * vlcruntime.cpp: support for NPRuntime API for Netscape Script-able plugins
3  *                 FYI: http://www.mozilla.org/projects/plugins/npruntime.html
4  *****************************************************************************
5  * Copyright (C) 2005 the VideoLAN team
6  *
7  * Authors: Damien Fouilleul <Damien.Fouilleul@laposte.net>
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 #include "config.h"
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29
30 /* vlc stuff */
31 #ifdef USE_LIBVLC
32 #   include <vlc/vlc.h>
33 #endif
34
35 /* Mozilla stuff */
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 #include <npruntime.h>
43
44 #include "vlcplugin.h"
45 #include "vlcruntime.h"
46
47 /*
48 ** utility functions
49 */
50
51 static PRInt64 NPVariantToPRInt64(const NPVariant &v)
52 {
53     switch( v.type ) {
54         case NPVariantType_Bool:
55             return static_cast<PRInt64>(NPVARIANT_TO_BOOLEAN(v));
56         case NPVariantType_Int32:
57             return static_cast<PRInt64>(NPVARIANT_TO_INT32(v));
58         case NPVariantType_Double:
59             return static_cast<PRInt64>(NPVARIANT_TO_DOUBLE(v));
60         default:
61             return 0;
62     }
63 }
64
65 /*
66 ** implementation root object
67 */
68
69 const NPUTF8 * const VlcRuntimeRootObject::propertyNames[] = { };
70 const NPUTF8 * const VlcRuntimeRootObject::methodNames[] =
71 {
72     "play",
73     "pause",
74     "stop",
75     "fullscreen",
76     "set_volume",
77     "get_volume",
78     "mute",
79     "get_int_variable",
80     "set_int_variable",
81     "get_bool_variable",
82     "set_bool_variable",
83     "get_str_variable",
84     "set_str_variable",
85     "clear_playlist",
86     "add_item",
87     "next",
88     "previous",
89     "isplaying",
90     "get_length",
91     "get_position",
92     "get_time",
93     "seek",
94 };
95
96 enum VlcRuntimeRootObjectMethodIds
97 {
98     ID_play = 0,
99     ID_pause,
100     ID_stop,
101     ID_fullscreen,
102     ID_set_volume,
103     ID_get_volume,
104     ID_mute,
105     ID_get_int_variable,
106     ID_set_int_variable,
107     ID_get_bool_variable,
108     ID_set_bool_variable,
109     ID_get_str_variable,
110     ID_set_str_variable,
111     ID_clear_playlist,
112     ID_add_item,
113     ID_next,
114     ID_previous,
115     ID_isplaying,
116     ID_get_length,
117     ID_get_position,
118     ID_get_time,
119     ID_seek,
120 };
121
122 const int VlcRuntimeRootObject::propertyCount = sizeof(VlcRuntimeRootObject::propertyNames)/sizeof(NPUTF8 *);
123 const int VlcRuntimeRootObject::methodCount = sizeof(VlcRuntimeRootObject::methodNames)/sizeof(NPUTF8 *);
124
125 bool VlcRuntimeRootObject::getProperty(int index, NPVariant *result)
126 {
127     return false;
128 }
129
130 bool VlcRuntimeRootObject::setProperty(int index, const NPVariant *value)
131 {
132     return false;
133 }
134
135 bool VlcRuntimeRootObject::removeProperty(int index)
136 {
137     return false;
138 }
139
140 bool VlcRuntimeRootObject::invoke(int index, const NPVariant *args, uint32_t argCount, NPVariant *result)
141 {
142     VlcPlugin *plugin = (VlcPlugin *)(_instance->pdata);
143     if( plugin )
144     {
145         VlcIntf *peer = plugin->GetPeer();
146         switch( index )
147         {
148             case ID_play:
149                 peer->Play();
150                 VOID_TO_NPVARIANT(*result);
151                 return true;
152             case ID_pause:
153                 peer->Pause();
154                 VOID_TO_NPVARIANT(*result);
155                 return true;
156             case ID_stop:
157                 peer->Stop();
158                 VOID_TO_NPVARIANT(*result);
159                 return true;
160             case ID_fullscreen:
161                 peer->Fullscreen();
162                 VOID_TO_NPVARIANT(*result);
163                 return true;
164             case ID_set_volume:
165                 if( argCount == 1 )
166                 {
167                     peer->Set_volume(NPVariantToPRInt64(args[0]));
168                     VOID_TO_NPVARIANT(*result);
169                     return true;
170                 }
171                 return false;
172             case ID_get_volume:
173                 {
174                     PRInt64 val;
175                     peer->Get_volume(&val);
176                     INT32_TO_NPVARIANT(val, *result);
177                     return true;
178                 }
179             case ID_mute:
180                 peer->Mute();
181                 VOID_TO_NPVARIANT(*result);
182                 return true;
183             case ID_get_int_variable:
184                 if( (argCount == 1)
185                     && NPVARIANT_IS_STRING(args[0]) )
186                 {
187                     const NPString &name = NPVARIANT_TO_STRING(args[0]);
188                     NPUTF8 *s = new NPUTF8[name.utf8length+1];
189                     if( s )
190                     {
191                         PRInt64 val;
192                         strncpy(s, name.utf8characters, name.utf8length);
193                         s[name.utf8length] = '\0';
194                         peer->Get_int_variable(s, &val);
195                         INT32_TO_NPVARIANT(val, *result);
196                         delete s;
197                         return true;
198                     }
199                 }
200                 return false;
201             case ID_set_int_variable:
202                 if( (argCount == 2)
203                     && NPVARIANT_IS_STRING(args[0]) )
204                 {
205                     const NPString &name = NPVARIANT_TO_STRING(args[0]);
206                     NPUTF8 *s = new NPUTF8[name.utf8length+1];
207                     if( s )
208                     {
209                         strncpy(s, name.utf8characters, name.utf8length);
210                         s[name.utf8length] = '\0';
211                         peer->Set_int_variable(s, NPVariantToPRInt64(args[1]));
212                         delete s;
213                         VOID_TO_NPVARIANT(*result);
214                         return true;
215                     }
216                 }
217                 return false;
218             case ID_get_bool_variable:
219                 if( (argCount == 1)
220                     && NPVARIANT_IS_STRING(args[0]) )
221                 {
222                     const NPString &name = NPVARIANT_TO_STRING(args[0]);
223                     NPUTF8 *s = new NPUTF8[name.utf8length+1];
224                     if( s )
225                     {
226                         PRBool val;
227                         strncpy(s, name.utf8characters, name.utf8length);
228                         s[name.utf8length] = '\0';
229                         peer->Get_bool_variable(s, &val);
230                         BOOLEAN_TO_NPVARIANT(val, *result);
231                         delete s;
232                         return true;
233                     }
234                 }
235                 return false;
236             case ID_set_bool_variable:
237                 if( (argCount == 2)
238                     && NPVARIANT_IS_STRING(args[0])
239                     && NPVARIANT_IS_BOOLEAN(args[1]) )
240                 {
241                     const NPString &name = NPVARIANT_TO_STRING(args[0]);
242                     NPUTF8 *s = new NPUTF8[name.utf8length+1];
243                     if( s )
244                     {
245                         strncpy(s, name.utf8characters, name.utf8length);
246                         s[name.utf8length] = '\0';
247                         peer->Set_bool_variable(s, NPVARIANT_TO_BOOLEAN(args[1]));
248                         delete s;
249                         VOID_TO_NPVARIANT(*result);
250                         return true;
251                     }
252                 }
253                 return false;
254             case ID_get_str_variable:
255                 if( (argCount == 1)
256                     && NPVARIANT_IS_STRING(args[0]) )
257                 {
258                     const NPString &name = NPVARIANT_TO_STRING(args[0]);
259                     NPUTF8 *s = new NPUTF8[name.utf8length+1];
260                     if( s )
261                     {
262                         char *val;
263                         strncpy(s, name.utf8characters, name.utf8length);
264                         s[name.utf8length] = '\0';
265                         peer->Get_str_variable(s, &val);
266                         delete s;
267                         int len = strlen(val);
268                         NPUTF8 *retval = (NPUTF8 *)NPN_MemAlloc(len);
269                         if( retval )
270                         {
271                             memcpy(retval, val, len);
272                             STRINGN_TO_NPVARIANT(retval, len, *result);
273                             free(val);
274                             return true;
275                         }
276                         free(val);
277                     }
278                 }
279                 return false;
280             case ID_set_str_variable:
281                 if( (argCount == 2)
282                     && NPVARIANT_IS_STRING(args[0])
283                     && NPVARIANT_IS_STRING(args[1]) )
284                 {
285                     const NPString &name = NPVARIANT_TO_STRING(args[0]);
286                     NPUTF8 *s = new NPUTF8[name.utf8length+1];
287                     if( s )
288                     {
289                         strncpy(s, name.utf8characters, name.utf8length);
290                         s[name.utf8length] = '\0';
291                         const NPString &val = NPVARIANT_TO_STRING(args[1]);
292                         NPUTF8 *v = new NPUTF8[val.utf8length+1];
293                         if( v )
294                         {
295                             strncpy(v, val.utf8characters, val.utf8length);
296                             v[val.utf8length] = '\0';
297                             peer->Set_str_variable(s, v);
298                             delete s;
299                             delete v;
300                             VOID_TO_NPVARIANT(*result);
301                             return true;
302                         }
303                         delete s;
304                     }
305                 }
306                 return false;
307             case ID_clear_playlist:
308                 peer->Clear_playlist();
309                 VOID_TO_NPVARIANT(*result);
310                 return true;
311             case ID_add_item:
312                 if( (argCount == 1)
313                     && NPVARIANT_IS_STRING(args[0]) )
314                 {
315                     const NPString &name = NPVARIANT_TO_STRING(args[0]);
316                     NPUTF8 *s = new NPUTF8[name.utf8length+1];
317                     if( s )
318                     {
319                         strncpy(s, name.utf8characters, name.utf8length);
320                         s[name.utf8length] = '\0';
321                         peer->Add_item(s);
322                         delete s;
323                         return true;
324                     }
325                 }
326                 return false;
327             case ID_next:
328                 peer->Next();
329                 VOID_TO_NPVARIANT(*result);
330                 return true;
331             case ID_previous:
332                 peer->Previous();
333                 VOID_TO_NPVARIANT(*result);
334                 return true;
335             case ID_isplaying:
336                 {
337                     PRBool val;
338                     peer->Isplaying(&val);
339                     BOOLEAN_TO_NPVARIANT(val, *result);
340                     return true;
341                 }
342             case ID_get_length:
343                 {
344                     PRInt64 val;
345                     peer->Get_length(&val);
346                     DOUBLE_TO_NPVARIANT(val, *result);
347                     return true;
348                 }
349             case ID_get_position:
350                 {
351                     PRInt64 val;
352                     peer->Get_position(&val);
353                     INT32_TO_NPVARIANT(val, *result);
354                     return true;
355                 }
356             case ID_get_time:
357                 {
358                     PRInt64 val;
359                     peer->Get_time(&val);
360                     INT32_TO_NPVARIANT(val, *result);
361                     return true;
362                 }
363             case ID_seek:
364                 if( argCount == 2 )
365                 {
366                     peer->Seek(NPVariantToPRInt64(args[0]), NPVariantToPRInt64(args[1]));
367                     VOID_TO_NPVARIANT(*result);
368                     return true;
369                 }
370                 return false;
371         }
372         NS_RELEASE(peer);
373     }
374     return false;
375 }
376
377 bool VlcRuntimeRootObject::invokeDefault(const NPVariant *args, uint32_t argCount, NPVariant *result)
378 {
379     return false;
380 }
381