1 /*****************************************************************************
2 * npovlc.cpp: deprecated VLC apis implemented in late XPCOM interface
3 *****************************************************************************
4 * Copyright (C) 2002-2006 the VideoLAN team
6 * Authors: Damien Fouilleul <Damien.Fouilleul@laposte.net>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
36 #ifdef HAVE_MOZILLA_CONFIG_H
37 # include <mozilla-config.h>
41 #include "vlcplugin.h"
44 ** implementation of vlc root object
47 const NPUTF8 * const VlcNPObject::propertyNames[] =
52 const int VlcNPObject::propertyCount = sizeof(VlcNPObject::propertyNames)/sizeof(NPUTF8 *);
54 const NPUTF8 * const VlcNPObject::methodNames[] =
56 "play", /* deprecated */
57 "pause", /* deprecated */
58 "stop", /* deprecated */
59 "fullscreen", /* deprecated */
60 "set_volume", /* deprecated */
61 "get_volume", /* deprecated */
62 "mute", /* deprecated */
63 "get_int_variable", /* deprecated */
64 "set_int_variable", /* deprecated */
65 "get_bool_variable", /* deprecated */
66 "set_bool_variable", /* deprecated */
67 "get_str_variable", /* deprecated */
68 "set_str_variable", /* deprecated */
69 "clear_playlist", /* deprecated */
70 "add_item", /* deprecated */
71 "next", /* deprecated */
72 "previous", /* deprecated */
73 "isplaying", /* deprecated */
74 "get_length", /* deprecated */
75 "get_position", /* deprecated */
76 "get_time", /* deprecated */
77 "seek", /* deprecated */
80 enum VlcNPObjectMethodIds
106 const int VlcNPObject::methodCount = sizeof(VlcNPObject::methodNames)/sizeof(NPUTF8 *);
108 RuntimeNPObject::InvokeResult VlcNPObject::invoke(int index, const NPVariant *args, uint32_t argCount, NPVariant &result)
110 VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(_instance->pdata);
113 libvlc_exception_t ex;
114 libvlc_exception_init(&ex);
118 case ID_play: /* deprecated */
121 libvlc_playlist_play(p_plugin->getVLC(), -1, 0, NULL, &ex);
122 if( libvlc_exception_raised(&ex) )
124 NPN_SetException(this, libvlc_exception_get_message(&ex));
125 libvlc_exception_clear(&ex);
126 return INVOKERESULT_GENERIC_ERROR;
130 VOID_TO_NPVARIANT(result);
131 return INVOKERESULT_NO_ERROR;
134 return INVOKERESULT_NO_SUCH_METHOD;
135 case ID_pause: /* deprecated */
138 libvlc_playlist_pause(p_plugin->getVLC(), &ex);
139 if( libvlc_exception_raised(&ex) )
141 NPN_SetException(this, libvlc_exception_get_message(&ex));
142 libvlc_exception_clear(&ex);
143 return INVOKERESULT_GENERIC_ERROR;
147 VOID_TO_NPVARIANT(result);
148 return INVOKERESULT_NO_ERROR;
151 return INVOKERESULT_NO_SUCH_METHOD;
152 case ID_stop: /* deprecated */
155 libvlc_playlist_stop(p_plugin->getVLC(), &ex);
156 if( libvlc_exception_raised(&ex) )
158 NPN_SetException(this, libvlc_exception_get_message(&ex));
159 libvlc_exception_clear(&ex);
160 return INVOKERESULT_GENERIC_ERROR;
164 VOID_TO_NPVARIANT(result);
165 return INVOKERESULT_NO_ERROR;
168 return INVOKERESULT_NO_SUCH_METHOD;
169 case ID_fullscreen: /* deprecated */
172 libvlc_media_player_t *p_md = libvlc_playlist_get_media_player(p_plugin->getVLC(), &ex);
175 libvlc_toggle_fullscreen(p_md, &ex);
176 libvlc_media_player_release(p_md);
177 if( libvlc_exception_raised(&ex) )
179 NPN_SetException(this, libvlc_exception_get_message(&ex));
180 libvlc_exception_clear(&ex);
181 return INVOKERESULT_GENERIC_ERROR;
185 VOID_TO_NPVARIANT(result);
186 return INVOKERESULT_NO_ERROR;
191 /* cannot get input, probably not playing */
192 if( libvlc_exception_raised(&ex) )
194 NPN_SetException(this, libvlc_exception_get_message(&ex));
195 libvlc_exception_clear(&ex);
197 return INVOKERESULT_GENERIC_ERROR;
200 return INVOKERESULT_NO_SUCH_METHOD;
201 case ID_set_volume: /* deprecated */
202 if( (argCount == 1) && isNumberValue(args[0]) )
204 libvlc_audio_set_volume(p_plugin->getVLC(), numberValue(args[0]), &ex);
205 if( libvlc_exception_raised(&ex) )
207 NPN_SetException(this, libvlc_exception_get_message(&ex));
208 libvlc_exception_clear(&ex);
209 return INVOKERESULT_GENERIC_ERROR;
213 VOID_TO_NPVARIANT(result);
214 return INVOKERESULT_NO_ERROR;
217 return INVOKERESULT_NO_SUCH_METHOD;
218 case ID_get_volume: /* deprecated */
221 int val = libvlc_audio_get_volume(p_plugin->getVLC(), &ex);
222 if( libvlc_exception_raised(&ex) )
224 NPN_SetException(this, libvlc_exception_get_message(&ex));
225 libvlc_exception_clear(&ex);
226 return INVOKERESULT_GENERIC_ERROR;
230 INT32_TO_NPVARIANT(val, result);
231 return INVOKERESULT_NO_ERROR;
234 return INVOKERESULT_NO_SUCH_METHOD;
235 case ID_mute: /* deprecated */
238 libvlc_audio_toggle_mute(p_plugin->getVLC(), &ex);
239 if( libvlc_exception_raised(&ex) )
241 NPN_SetException(this, libvlc_exception_get_message(&ex));
242 libvlc_exception_clear(&ex);
243 return INVOKERESULT_GENERIC_ERROR;
247 VOID_TO_NPVARIANT(result);
248 return INVOKERESULT_NO_ERROR;
251 return INVOKERESULT_NO_SUCH_METHOD;
252 case ID_get_int_variable: /* deprecated */
253 if( (argCount == 1) && NPVARIANT_IS_STRING(args[0]) )
255 char *s = stringValue(NPVARIANT_TO_STRING(args[0]));
258 int vlc_id = libvlc_get_vlc_id(p_plugin->getVLC());
260 if( VLC_SUCCESS == VLC_VariableGet(vlc_id, s, &val) )
263 INT32_TO_NPVARIANT(val.i_int, result);
264 return INVOKERESULT_NO_ERROR;
269 return INVOKERESULT_INVALID_ARGS;
273 return INVOKERESULT_OUT_OF_MEMORY;
275 return INVOKERESULT_NO_SUCH_METHOD;
276 case ID_set_int_variable: /* deprecated */
278 && NPVARIANT_IS_STRING(args[0])
279 && isNumberValue(args[1]) )
281 char *s = stringValue(NPVARIANT_TO_STRING(args[0]));
284 int vlc_id = libvlc_get_vlc_id(p_plugin->getVLC());
286 val.i_int = numberValue(args[1]);
287 if( VLC_SUCCESS == VLC_VariableSet(vlc_id, s, val) )
290 VOID_TO_NPVARIANT(result);
291 return INVOKERESULT_NO_ERROR;
296 return INVOKERESULT_INVALID_ARGS;
300 return INVOKERESULT_OUT_OF_MEMORY;
302 return INVOKERESULT_NO_SUCH_METHOD;
303 case ID_get_bool_variable: /* deprecated */
304 if( (argCount == 1) && NPVARIANT_IS_STRING(args[0]) )
306 char *s = stringValue(NPVARIANT_TO_STRING(args[0]));
309 int vlc_id = libvlc_get_vlc_id(p_plugin->getVLC());
311 if( VLC_SUCCESS == VLC_VariableGet(vlc_id, s, &val) )
314 BOOLEAN_TO_NPVARIANT(val.b_bool, result);
315 return INVOKERESULT_NO_ERROR;
320 return INVOKERESULT_INVALID_ARGS;
324 return INVOKERESULT_OUT_OF_MEMORY;
326 return INVOKERESULT_NO_SUCH_METHOD;
327 case ID_set_bool_variable: /* deprecated */
329 && NPVARIANT_IS_STRING(args[0])
330 && NPVARIANT_IS_BOOLEAN(args[1]) )
332 char *s = stringValue(NPVARIANT_TO_STRING(args[0]));
335 int vlc_id = libvlc_get_vlc_id(p_plugin->getVLC());
337 val.b_bool = NPVARIANT_TO_BOOLEAN(args[1]);
338 if( VLC_SUCCESS == VLC_VariableSet(vlc_id, s, val) )
341 VOID_TO_NPVARIANT(result);
342 return INVOKERESULT_NO_ERROR;
347 return INVOKERESULT_INVALID_ARGS;
351 return INVOKERESULT_OUT_OF_MEMORY;
353 return INVOKERESULT_NO_SUCH_METHOD;
354 case ID_get_str_variable: /* deprecated */
355 if( (argCount == 1) && NPVARIANT_IS_STRING(args[0]) )
357 char *s = stringValue(NPVARIANT_TO_STRING(args[0]));
360 int vlc_id = libvlc_get_vlc_id(p_plugin->getVLC());
362 if( VLC_SUCCESS == VLC_VariableGet(vlc_id, s, &val) )
367 int len = strlen(val.psz_string);
368 NPUTF8 *retval = (NPUTF8 *)NPN_MemAlloc(len);
371 memcpy(retval, val.psz_string, len);
372 STRINGN_TO_NPVARIANT(retval, len, result);
373 free(val.psz_string);
374 return INVOKERESULT_NO_ERROR;
378 return INVOKERESULT_OUT_OF_MEMORY;
384 NULL_TO_NPVARIANT(result);
385 return INVOKERESULT_NO_ERROR;
391 return INVOKERESULT_INVALID_ARGS;
395 return INVOKERESULT_OUT_OF_MEMORY;
397 return INVOKERESULT_NO_SUCH_METHOD;
398 case ID_set_str_variable: /* deprecated */
400 && NPVARIANT_IS_STRING(args[0])
401 && NPVARIANT_IS_STRING(args[1]) )
403 char *s = stringValue(NPVARIANT_TO_STRING(args[0]));
406 int vlc_id = libvlc_get_vlc_id(p_plugin->getVLC());
408 val.psz_string = stringValue(NPVARIANT_TO_STRING(args[1]));
411 if( VLC_SUCCESS == VLC_VariableSet(vlc_id, s, val) )
414 delete val.psz_string;
415 VOID_TO_NPVARIANT(result);
416 return INVOKERESULT_NO_ERROR;
421 delete val.psz_string;
422 return INVOKERESULT_INVALID_ARGS;
428 return INVOKERESULT_OUT_OF_MEMORY;
432 return INVOKERESULT_OUT_OF_MEMORY;
434 return INVOKERESULT_NO_SUCH_METHOD;
435 case ID_clear_playlist: /* deprecated */
438 libvlc_playlist_clear(p_plugin->getVLC(), &ex);
439 if( libvlc_exception_raised(&ex) )
441 NPN_SetException(this, libvlc_exception_get_message(&ex));
442 libvlc_exception_clear(&ex);
443 return INVOKERESULT_GENERIC_ERROR;
447 VOID_TO_NPVARIANT(result);
448 return INVOKERESULT_NO_ERROR;
451 return INVOKERESULT_NO_SUCH_METHOD;
452 case ID_add_item: /* deprecated */
453 if( (argCount == 1) && NPVARIANT_IS_STRING(args[0]) )
455 char *s = stringValue(NPVARIANT_TO_STRING(args[0]));
458 char *url = p_plugin->getAbsoluteURL(s);
462 return INVOKERESULT_GENERIC_ERROR;
464 int item = libvlc_playlist_add(p_plugin->getVLC(), url, NULL, &ex);
466 if( libvlc_exception_raised(&ex) )
468 NPN_SetException(this, libvlc_exception_get_message(&ex));
469 libvlc_exception_clear(&ex);
470 return INVOKERESULT_GENERIC_ERROR;
474 INT32_TO_NPVARIANT(item, result);
475 return INVOKERESULT_NO_ERROR;
479 return INVOKERESULT_OUT_OF_MEMORY;
481 return INVOKERESULT_NO_SUCH_METHOD;
482 case ID_next: /* deprecated */
485 libvlc_playlist_next(p_plugin->getVLC(), &ex);
486 if( libvlc_exception_raised(&ex) )
488 NPN_SetException(this, libvlc_exception_get_message(&ex));
489 libvlc_exception_clear(&ex);
490 return INVOKERESULT_GENERIC_ERROR;
494 VOID_TO_NPVARIANT(result);
495 return INVOKERESULT_NO_ERROR;
498 return INVOKERESULT_NO_SUCH_METHOD;
499 case ID_previous: /* deprecated */
502 libvlc_playlist_prev(p_plugin->getVLC(), &ex);
503 if( libvlc_exception_raised(&ex) )
505 NPN_SetException(this, libvlc_exception_get_message(&ex));
506 libvlc_exception_clear(&ex);
507 return INVOKERESULT_GENERIC_ERROR;
511 VOID_TO_NPVARIANT(result);
512 return INVOKERESULT_NO_ERROR;
515 return INVOKERESULT_NO_SUCH_METHOD;
516 case ID_isplaying: /* deprecated */
519 int isplaying = libvlc_playlist_isplaying(p_plugin->getVLC(), &ex);
520 if( libvlc_exception_raised(&ex) )
522 NPN_SetException(this, libvlc_exception_get_message(&ex));
523 libvlc_exception_clear(&ex);
524 return INVOKERESULT_GENERIC_ERROR;
528 BOOLEAN_TO_NPVARIANT(isplaying, result);
529 return INVOKERESULT_NO_ERROR;
532 return INVOKERESULT_NO_SUCH_METHOD;
533 case ID_get_length: /* deprecated */
536 libvlc_media_player_t *p_md = libvlc_playlist_get_media_player(p_plugin->getVLC(), &ex);
539 vlc_int64_t val = libvlc_media_player_get_length(p_md, &ex);
540 libvlc_media_player_release(p_md);
541 if( libvlc_exception_raised(&ex) )
543 NPN_SetException(this, libvlc_exception_get_message(&ex));
544 libvlc_exception_clear(&ex);
545 return INVOKERESULT_GENERIC_ERROR;
549 INT32_TO_NPVARIANT((uint32_t)(val/1000LL), result);
550 return INVOKERESULT_NO_ERROR;
555 /* cannot get input, probably not playing */
556 if( libvlc_exception_raised(&ex) )
558 NPN_SetException(this, libvlc_exception_get_message(&ex));
559 libvlc_exception_clear(&ex);
561 return INVOKERESULT_GENERIC_ERROR;
564 return INVOKERESULT_NO_SUCH_METHOD;
565 case ID_get_position: /* deprecated */
568 libvlc_media_player_t *p_md = libvlc_playlist_get_media_player(p_plugin->getVLC(), &ex);
571 float val = libvlc_media_player_get_position(p_md, &ex);
572 libvlc_media_player_release(p_md);
573 if( libvlc_exception_raised(&ex) )
575 NPN_SetException(this, libvlc_exception_get_message(&ex));
576 libvlc_exception_clear(&ex);
577 return INVOKERESULT_GENERIC_ERROR;
581 DOUBLE_TO_NPVARIANT((double)val, result);
582 return INVOKERESULT_NO_ERROR;
587 /* cannot get input, probably not playing */
588 if( libvlc_exception_raised(&ex) )
590 NPN_SetException(this, libvlc_exception_get_message(&ex));
591 libvlc_exception_clear(&ex);
593 return INVOKERESULT_GENERIC_ERROR;
596 return INVOKERESULT_NO_SUCH_METHOD;
597 case ID_get_time: /* deprecated */
600 libvlc_media_player_t *p_md = libvlc_playlist_get_media_player(p_plugin->getVLC(), &ex);
603 vlc_int64_t val = libvlc_media_player_get_time(p_md, &ex);
604 libvlc_media_player_release(p_md);
605 if( libvlc_exception_raised(&ex) )
607 NPN_SetException(this, libvlc_exception_get_message(&ex));
608 libvlc_exception_clear(&ex);
609 return INVOKERESULT_GENERIC_ERROR;
613 DOUBLE_TO_NPVARIANT((uint32_t)(val/1000LL), result);
614 return INVOKERESULT_NO_ERROR;
619 /* cannot get input, probably not playing */
620 if( libvlc_exception_raised(&ex) )
622 NPN_SetException(this, libvlc_exception_get_message(&ex));
623 libvlc_exception_clear(&ex);
625 return INVOKERESULT_GENERIC_ERROR;
628 return INVOKERESULT_NO_SUCH_METHOD;
629 case ID_seek: /* deprecated */
631 && isNumberValue(args[0])
632 && NPVARIANT_IS_BOOLEAN(args[1]) )
634 libvlc_media_player_t *p_md = libvlc_playlist_get_media_player(p_plugin->getVLC(), &ex);
638 if( NPVARIANT_IS_INT32(args[0]) )
639 pos = (vlc_int64_t)NPVARIANT_TO_INT32(args[0]);
641 pos = (vlc_int64_t)NPVARIANT_TO_DOUBLE(args[0]);
643 if( NPVARIANT_TO_BOOLEAN(args[1]) )
646 vlc_int64_t from = libvlc_media_player_get_time(p_md, &ex);
647 if( libvlc_exception_raised(&ex) )
649 libvlc_media_player_release(p_md);
650 NPN_SetException(this, libvlc_exception_get_message(&ex));
651 libvlc_exception_clear(&ex);
652 return INVOKERESULT_GENERIC_ERROR;
657 libvlc_media_player_set_time(p_md, pos, &ex);
658 libvlc_media_player_release(p_md);
659 if( libvlc_exception_raised(&ex) )
661 NPN_SetException(this, libvlc_exception_get_message(&ex));
662 libvlc_exception_clear(&ex);
663 return INVOKERESULT_GENERIC_ERROR;
665 VOID_TO_NPVARIANT(result);
666 return INVOKERESULT_NO_ERROR;
670 /* cannot get input, probably not playing */
671 if( libvlc_exception_raised(&ex) )
673 NPN_SetException(this, libvlc_exception_get_message(&ex));
674 libvlc_exception_clear(&ex);
676 return INVOKERESULT_GENERIC_ERROR;
679 return INVOKERESULT_NO_SUCH_METHOD;
681 return INVOKERESULT_NO_SUCH_METHOD;
684 return INVOKERESULT_GENERIC_ERROR;