Don't use vlc_int64_t. It no longer exist.
[vlc.git] / projects / mozilla / control / npolibvlc.cpp
blobbf10d9c6ccce41162a5c50b1be46f7ae643b0ed8
1 /*****************************************************************************
2 * npolibvlc.cpp: official Javascript APIs
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 *****************************************************************************/
23 #include "config.h"
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
29 /* Mozilla stuff */
30 #ifdef HAVE_MOZILLA_CONFIG_H
31 # include <mozilla-config.h>
32 #endif
34 #include "vlcplugin.h"
35 #include "npolibvlc.h"
38 ** implementation of libvlc root object
41 LibvlcRootNPObject::~LibvlcRootNPObject()
44 ** when plugin is destroyed, firefox takes upon itself to destroy all 'live' script objects
45 ** and ignores refcounting. Therefore we cannot safely assume that refcounting will control
46 ** lifespan of objects. Hence they are only lazily created on request, so that firefox can
47 ** take ownership, and are not released when plugin is being destroyed.
49 if( isValid() )
51 if( audioObj ) NPN_ReleaseObject(audioObj);
52 if( inputObj ) NPN_ReleaseObject(inputObj);
53 if( logObj ) NPN_ReleaseObject(logObj);
54 if( playlistObj ) NPN_ReleaseObject(playlistObj);
55 if( videoObj ) NPN_ReleaseObject(videoObj);
59 const NPUTF8 * const LibvlcRootNPObject::propertyNames[] =
61 "audio",
62 "input",
63 "log",
64 "playlist",
65 "video",
66 "VersionInfo",
69 const int LibvlcRootNPObject::propertyCount = sizeof(LibvlcRootNPObject::propertyNames)/sizeof(NPUTF8 *);
71 enum LibvlcRootNPObjectPropertyIds
73 ID_root_audio = 0,
74 ID_root_input,
75 ID_root_log,
76 ID_root_playlist,
77 ID_root_video,
78 ID_root_VersionInfo,
81 RuntimeNPObject::InvokeResult LibvlcRootNPObject::getProperty(int index, NPVariant &result)
83 /* is plugin still running */
84 if( _instance->pdata )
86 switch( index )
88 case ID_root_audio:
89 // create child object in lazyman fashion to avoid ownership problem with firefox
90 if( ! audioObj )
91 audioObj = NPN_CreateObject(_instance, RuntimeNPClass<LibvlcAudioNPObject>::getClass());
92 OBJECT_TO_NPVARIANT(NPN_RetainObject(audioObj), result);
93 return INVOKERESULT_NO_ERROR;
94 case ID_root_input:
95 // create child object in lazyman fashion to avoid ownership problem with firefox
96 if( ! inputObj )
97 inputObj = NPN_CreateObject(_instance, RuntimeNPClass<LibvlcInputNPObject>::getClass());
98 OBJECT_TO_NPVARIANT(NPN_RetainObject(inputObj), result);
99 return INVOKERESULT_NO_ERROR;
100 case ID_root_log:
101 // create child object in lazyman fashion to avoid ownership problem with firefox
102 if( ! logObj )
103 logObj = NPN_CreateObject(_instance, RuntimeNPClass<LibvlcLogNPObject>::getClass());
104 OBJECT_TO_NPVARIANT(NPN_RetainObject(logObj), result);
105 return INVOKERESULT_NO_ERROR;
106 case ID_root_playlist:
107 // create child object in lazyman fashion to avoid ownership problem with firefox
108 if( ! playlistObj )
109 playlistObj = NPN_CreateObject(_instance, RuntimeNPClass<LibvlcPlaylistNPObject>::getClass());
110 OBJECT_TO_NPVARIANT(NPN_RetainObject(playlistObj), result);
111 return INVOKERESULT_NO_ERROR;
112 case ID_root_video:
113 // create child object in lazyman fashion to avoid ownership problem with firefox
114 if( ! videoObj )
115 videoObj = NPN_CreateObject(_instance,RuntimeNPClass<LibvlcVideoNPObject>::getClass());
116 OBJECT_TO_NPVARIANT(NPN_RetainObject(videoObj), result);
117 return INVOKERESULT_NO_ERROR;
118 case ID_root_VersionInfo:
120 int len = strlen(VLC_Version());
121 NPUTF8 *retval =(NPUTF8*)NPN_MemAlloc(len);
122 if( retval )
124 memcpy(retval, VLC_Version(), len);
125 STRINGN_TO_NPVARIANT(retval, len, result);
127 else
129 NULL_TO_NPVARIANT(result);
131 return INVOKERESULT_NO_ERROR;
133 default:
137 return INVOKERESULT_GENERIC_ERROR;
140 const NPUTF8 * const LibvlcRootNPObject::methodNames[] =
142 "versionInfo",
145 const int LibvlcRootNPObject::methodCount = sizeof(LibvlcRootNPObject::methodNames)/sizeof(NPUTF8 *);
147 enum LibvlcRootNPObjectMethodIds
149 ID_root_versionInfo,
152 RuntimeNPObject::InvokeResult LibvlcRootNPObject::invoke(int index, const NPVariant *args, uint32_t argCount, NPVariant &result)
154 /* is plugin still running */
155 if( _instance->pdata )
157 libvlc_exception_t ex;
158 libvlc_exception_init(&ex);
160 switch( index )
162 case ID_root_versionInfo:
163 if( argCount == 0 )
165 int len = strlen(VLC_Version());
166 NPUTF8 *retval =(NPUTF8*)NPN_MemAlloc(len);
167 if( retval )
169 memcpy(retval, VLC_Version(), len);
170 STRINGN_TO_NPVARIANT(retval, len, result);
172 else
174 NULL_TO_NPVARIANT(result);
176 return INVOKERESULT_NO_ERROR;
178 return INVOKERESULT_NO_SUCH_METHOD;
179 default:
183 return INVOKERESULT_GENERIC_ERROR;
187 ** implementation of libvlc audio object
190 const NPUTF8 * const LibvlcAudioNPObject::propertyNames[] =
192 "mute",
193 "volume",
194 "track",
195 "channel",
198 const int LibvlcAudioNPObject::propertyCount = sizeof(LibvlcAudioNPObject::propertyNames)/sizeof(NPUTF8 *);
200 enum LibvlcAudioNPObjectPropertyIds
202 ID_audio_mute,
203 ID_audio_volume,
204 ID_audio_track,
205 ID_audio_channel,
208 RuntimeNPObject::InvokeResult LibvlcAudioNPObject::getProperty(int index, NPVariant &result)
210 /* is plugin still running */
211 if( _instance->pdata )
213 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(_instance->pdata);
214 libvlc_exception_t ex;
215 libvlc_exception_init(&ex);
217 switch( index )
219 case ID_audio_mute:
221 bool muted = libvlc_audio_get_mute(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;
228 BOOLEAN_TO_NPVARIANT(muted, result);
229 return INVOKERESULT_NO_ERROR;
231 case ID_audio_volume:
233 int volume = libvlc_audio_get_volume(p_plugin->getVLC(), &ex);
234 if( libvlc_exception_raised(&ex) )
236 NPN_SetException(this, libvlc_exception_get_message(&ex));
237 libvlc_exception_clear(&ex);
238 return INVOKERESULT_GENERIC_ERROR;
240 INT32_TO_NPVARIANT(volume, result);
241 return INVOKERESULT_NO_ERROR;
243 case ID_audio_track:
245 libvlc_media_player_t *p_md = libvlc_playlist_get_media_player(p_plugin->getVLC(), &ex);
246 if( libvlc_exception_raised(&ex) )
248 NPN_SetException(this, libvlc_exception_get_message(&ex));
249 libvlc_exception_clear(&ex);
250 return INVOKERESULT_GENERIC_ERROR;
252 int track = libvlc_audio_get_track(p_md, &ex);
253 libvlc_media_player_release(p_md);
254 if( libvlc_exception_raised(&ex) )
256 NPN_SetException(this, libvlc_exception_get_message(&ex));
257 libvlc_exception_clear(&ex);
258 return INVOKERESULT_GENERIC_ERROR;
260 INT32_TO_NPVARIANT(track, result);
261 return INVOKERESULT_NO_ERROR;
263 case ID_audio_channel:
265 int channel = libvlc_audio_get_channel(p_plugin->getVLC(), &ex);
266 if( libvlc_exception_raised(&ex) )
268 NPN_SetException(this, libvlc_exception_get_message(&ex));
269 libvlc_exception_clear(&ex);
270 return INVOKERESULT_GENERIC_ERROR;
272 INT32_TO_NPVARIANT(channel, result);
273 return INVOKERESULT_NO_ERROR;
275 default:
279 return INVOKERESULT_GENERIC_ERROR;
282 RuntimeNPObject::InvokeResult LibvlcAudioNPObject::setProperty(int index, const NPVariant &value)
284 /* is plugin still running */
285 if( _instance->pdata )
287 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(_instance->pdata);
288 libvlc_exception_t ex;
289 libvlc_exception_init(&ex);
291 switch( index )
293 case ID_audio_mute:
294 if( NPVARIANT_IS_BOOLEAN(value) )
296 libvlc_audio_set_mute(p_plugin->getVLC(),
297 NPVARIANT_TO_BOOLEAN(value), &ex);
298 if( libvlc_exception_raised(&ex) )
300 NPN_SetException(this, libvlc_exception_get_message(&ex));
301 libvlc_exception_clear(&ex);
302 return INVOKERESULT_GENERIC_ERROR;
304 return INVOKERESULT_NO_ERROR;
306 return INVOKERESULT_INVALID_VALUE;
307 case ID_audio_volume:
308 if( isNumberValue(value) )
310 libvlc_audio_set_volume(p_plugin->getVLC(),
311 numberValue(value), &ex);
312 if( libvlc_exception_raised(&ex) )
314 NPN_SetException(this, libvlc_exception_get_message(&ex));
315 libvlc_exception_clear(&ex);
316 return INVOKERESULT_GENERIC_ERROR;
318 return INVOKERESULT_NO_ERROR;
320 return INVOKERESULT_INVALID_VALUE;
321 case ID_audio_track:
322 if( isNumberValue(value) )
324 libvlc_media_player_t *p_md = libvlc_playlist_get_media_player(p_plugin->getVLC(), &ex);
325 if( libvlc_exception_raised(&ex) )
327 NPN_SetException(this, libvlc_exception_get_message(&ex));
328 libvlc_exception_clear(&ex);
329 return INVOKERESULT_GENERIC_ERROR;
331 libvlc_audio_set_track(p_md,
332 numberValue(value), &ex);
333 libvlc_media_player_release(p_md);
334 if( libvlc_exception_raised(&ex) )
336 NPN_SetException(this, libvlc_exception_get_message(&ex));
337 libvlc_exception_clear(&ex);
338 return INVOKERESULT_GENERIC_ERROR;
340 return INVOKERESULT_NO_ERROR;
342 return INVOKERESULT_INVALID_VALUE;
343 case ID_audio_channel:
344 if( isNumberValue(value) )
346 libvlc_audio_set_channel(p_plugin->getVLC(),
347 numberValue(value), &ex);
348 if( libvlc_exception_raised(&ex) )
350 NPN_SetException(this, libvlc_exception_get_message(&ex));
351 libvlc_exception_clear(&ex);
352 return INVOKERESULT_GENERIC_ERROR;
354 return INVOKERESULT_NO_ERROR;
356 return INVOKERESULT_INVALID_VALUE;
357 default:
361 return INVOKERESULT_GENERIC_ERROR;
364 const NPUTF8 * const LibvlcAudioNPObject::methodNames[] =
366 "toggleMute",
369 const int LibvlcAudioNPObject::methodCount = sizeof(LibvlcAudioNPObject::methodNames)/sizeof(NPUTF8 *);
371 enum LibvlcAudioNPObjectMethodIds
373 ID_audio_togglemute,
376 RuntimeNPObject::InvokeResult LibvlcAudioNPObject::invoke(int index, const NPVariant *args, uint32_t argCount, NPVariant &result)
378 /* is plugin still running */
379 if( _instance->pdata )
381 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(_instance->pdata);
382 libvlc_exception_t ex;
383 libvlc_exception_init(&ex);
385 switch( index )
387 case ID_audio_togglemute:
388 if( argCount == 0 )
390 libvlc_audio_toggle_mute(p_plugin->getVLC(), &ex);
391 if( libvlc_exception_raised(&ex) )
393 NPN_SetException(this, libvlc_exception_get_message(&ex));
394 libvlc_exception_clear(&ex);
395 return INVOKERESULT_GENERIC_ERROR;
397 else
399 VOID_TO_NPVARIANT(result);
400 return INVOKERESULT_NO_ERROR;
403 return INVOKERESULT_NO_SUCH_METHOD;
404 default:
408 return INVOKERESULT_GENERIC_ERROR;
412 ** implementation of libvlc input object
415 const NPUTF8 * const LibvlcInputNPObject::propertyNames[] =
417 "length",
418 "position",
419 "time",
420 "state",
421 "rate",
422 "fps",
423 "hasVout",
426 const int LibvlcInputNPObject::propertyCount = sizeof(LibvlcInputNPObject::propertyNames)/sizeof(NPUTF8 *);
428 enum LibvlcInputNPObjectPropertyIds
430 ID_input_length,
431 ID_input_position,
432 ID_input_time,
433 ID_input_state,
434 ID_input_rate,
435 ID_input_fps,
436 ID_input_hasvout,
439 RuntimeNPObject::InvokeResult LibvlcInputNPObject::getProperty(int index, NPVariant &result)
441 /* is plugin still running */
442 if( _instance->pdata )
444 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(_instance->pdata);
445 libvlc_exception_t ex;
446 libvlc_exception_init(&ex);
448 libvlc_media_player_t *p_md = libvlc_playlist_get_media_player(p_plugin->getVLC(), &ex);
449 if( libvlc_exception_raised(&ex) )
451 if( index != ID_input_state )
453 NPN_SetException(this, libvlc_exception_get_message(&ex));
454 libvlc_exception_clear(&ex);
455 return INVOKERESULT_GENERIC_ERROR;
457 else
459 /* for input state, return CLOSED rather than an exception */
460 INT32_TO_NPVARIANT(0, result);
461 return INVOKERESULT_NO_ERROR;
465 switch( index )
467 case ID_input_length:
469 double val = (double)libvlc_media_player_get_length(p_md, &ex);
470 libvlc_media_player_release(p_md);
471 if( libvlc_exception_raised(&ex) )
473 NPN_SetException(this, libvlc_exception_get_message(&ex));
474 libvlc_exception_clear(&ex);
475 return INVOKERESULT_GENERIC_ERROR;
477 DOUBLE_TO_NPVARIANT(val, result);
478 return INVOKERESULT_NO_ERROR;
480 case ID_input_position:
482 double val = libvlc_media_player_get_position(p_md, &ex);
483 libvlc_media_player_release(p_md);
484 if( libvlc_exception_raised(&ex) )
486 NPN_SetException(this, libvlc_exception_get_message(&ex));
487 libvlc_exception_clear(&ex);
488 return INVOKERESULT_GENERIC_ERROR;
490 DOUBLE_TO_NPVARIANT(val, result);
491 return INVOKERESULT_NO_ERROR;
493 case ID_input_time:
495 double val = (double)libvlc_media_player_get_time(p_md, &ex);
496 libvlc_media_player_release(p_md);
497 if( libvlc_exception_raised(&ex) )
499 NPN_SetException(this, libvlc_exception_get_message(&ex));
500 libvlc_exception_clear(&ex);
501 return INVOKERESULT_GENERIC_ERROR;
503 DOUBLE_TO_NPVARIANT(val, result);
504 return INVOKERESULT_NO_ERROR;
506 case ID_input_state:
508 int val = libvlc_media_player_get_state(p_md, &ex);
509 libvlc_media_player_release(p_md);
510 if( libvlc_exception_raised(&ex) )
512 NPN_SetException(this, libvlc_exception_get_message(&ex));
513 libvlc_exception_clear(&ex);
514 return INVOKERESULT_GENERIC_ERROR;
516 INT32_TO_NPVARIANT(val, result);
517 return INVOKERESULT_NO_ERROR;
519 case ID_input_rate:
521 float val = libvlc_media_player_get_rate(p_md, &ex);
522 libvlc_media_player_release(p_md);
523 if( libvlc_exception_raised(&ex) )
525 NPN_SetException(this, libvlc_exception_get_message(&ex));
526 libvlc_exception_clear(&ex);
527 return INVOKERESULT_GENERIC_ERROR;
529 DOUBLE_TO_NPVARIANT(val, result);
530 return INVOKERESULT_NO_ERROR;
532 case ID_input_fps:
534 double val = libvlc_media_player_get_fps(p_md, &ex);
535 libvlc_media_player_release(p_md);
536 if( libvlc_exception_raised(&ex) )
538 NPN_SetException(this, libvlc_exception_get_message(&ex));
539 libvlc_exception_clear(&ex);
540 return INVOKERESULT_GENERIC_ERROR;
542 DOUBLE_TO_NPVARIANT(val, result);
543 return INVOKERESULT_NO_ERROR;
545 case ID_input_hasvout:
547 bool val = libvlc_media_player_has_vout(p_md, &ex);
548 libvlc_media_player_release(p_md);
549 if( libvlc_exception_raised(&ex) )
551 NPN_SetException(this, libvlc_exception_get_message(&ex));
552 libvlc_exception_clear(&ex);
553 return INVOKERESULT_GENERIC_ERROR;
555 BOOLEAN_TO_NPVARIANT(val, result);
556 return INVOKERESULT_NO_ERROR;
558 default:
561 libvlc_media_player_release(p_md);
563 return INVOKERESULT_GENERIC_ERROR;
566 RuntimeNPObject::InvokeResult LibvlcInputNPObject::setProperty(int index, const NPVariant &value)
568 /* is plugin still running */
569 if( _instance->pdata )
571 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(_instance->pdata);
572 libvlc_exception_t ex;
573 libvlc_exception_init(&ex);
575 libvlc_media_player_t *p_md = libvlc_playlist_get_media_player(p_plugin->getVLC(), &ex);
576 if( libvlc_exception_raised(&ex) )
578 NPN_SetException(this, libvlc_exception_get_message(&ex));
579 libvlc_exception_clear(&ex);
580 return INVOKERESULT_GENERIC_ERROR;
583 switch( index )
585 case ID_input_position:
587 if( ! NPVARIANT_IS_DOUBLE(value) )
589 libvlc_media_player_release(p_md);
590 return INVOKERESULT_INVALID_VALUE;
593 float val = (float)NPVARIANT_TO_DOUBLE(value);
594 libvlc_media_player_set_position(p_md, val, &ex);
595 libvlc_media_player_release(p_md);
596 if( libvlc_exception_raised(&ex) )
598 NPN_SetException(this, libvlc_exception_get_message(&ex));
599 libvlc_exception_clear(&ex);
600 return INVOKERESULT_GENERIC_ERROR;
602 return INVOKERESULT_NO_ERROR;
604 case ID_input_time:
606 int64_t val;
607 if( NPVARIANT_IS_INT32(value) )
608 val = (int64_t)NPVARIANT_TO_INT32(value);
609 else if( NPVARIANT_IS_DOUBLE(value) )
610 val = (int64_t)NPVARIANT_TO_DOUBLE(value);
611 else
613 libvlc_media_player_release(p_md);
614 return INVOKERESULT_INVALID_VALUE;
617 libvlc_media_player_set_time(p_md, val, &ex);
618 libvlc_media_player_release(p_md);
619 if( libvlc_exception_raised(&ex) )
621 NPN_SetException(this, libvlc_exception_get_message(&ex));
622 libvlc_exception_clear(&ex);
623 return INVOKERESULT_GENERIC_ERROR;
625 return INVOKERESULT_NO_ERROR;
627 case ID_input_rate:
629 float val;
630 if( NPVARIANT_IS_INT32(value) )
631 val = (float)NPVARIANT_TO_INT32(value);
632 else if( NPVARIANT_IS_DOUBLE(value) )
633 val = (float)NPVARIANT_TO_DOUBLE(value);
634 else
636 libvlc_media_player_release(p_md);
637 return INVOKERESULT_INVALID_VALUE;
640 libvlc_media_player_set_rate(p_md, val, &ex);
641 libvlc_media_player_release(p_md);
642 if( libvlc_exception_raised(&ex) )
644 NPN_SetException(this, libvlc_exception_get_message(&ex));
645 libvlc_exception_clear(&ex);
646 return INVOKERESULT_GENERIC_ERROR;
648 return INVOKERESULT_NO_ERROR;
650 default:
653 libvlc_media_player_release(p_md);
655 return INVOKERESULT_GENERIC_ERROR;
658 const NPUTF8 * const LibvlcInputNPObject::methodNames[] =
660 /* no methods */
663 const int LibvlcInputNPObject::methodCount = sizeof(LibvlcInputNPObject::methodNames)/sizeof(NPUTF8 *);
666 ** implementation of libvlc message object
669 const NPUTF8 * const LibvlcMessageNPObject::propertyNames[] =
671 "severity",
672 "type",
673 "name",
674 "header",
675 "message",
678 const int LibvlcMessageNPObject::propertyCount = sizeof(LibvlcMessageNPObject::propertyNames)/sizeof(NPUTF8 *);
680 enum LibvlcMessageNPObjectPropertyIds
682 ID_message_severity,
683 ID_message_type,
684 ID_message_name,
685 ID_message_header,
686 ID_message_message,
689 RuntimeNPObject::InvokeResult LibvlcMessageNPObject::getProperty(int index, NPVariant &result)
691 /* is plugin still running */
692 if( _instance->pdata )
694 switch( index )
696 case ID_message_severity:
698 INT32_TO_NPVARIANT(_msg.i_severity, result);
699 return INVOKERESULT_NO_ERROR;
701 case ID_message_type:
703 if( _msg.psz_type )
705 int len = strlen(_msg.psz_type);
706 NPUTF8* retval = (NPUTF8*)NPN_MemAlloc(len);
707 if( retval )
709 memcpy(retval, _msg.psz_type, len);
710 STRINGN_TO_NPVARIANT(retval, len, result);
713 else
715 NULL_TO_NPVARIANT(result);
717 return INVOKERESULT_NO_ERROR;
719 case ID_message_name:
721 if( _msg.psz_name )
723 int len = strlen(_msg.psz_name);
724 NPUTF8* retval = (NPUTF8*)NPN_MemAlloc(len);
725 if( retval )
727 memcpy(retval, _msg.psz_name, len);
728 STRINGN_TO_NPVARIANT(retval, len, result);
731 else
733 NULL_TO_NPVARIANT(result);
735 return INVOKERESULT_NO_ERROR;
737 case ID_message_header:
739 if( _msg.psz_header )
741 int len = strlen(_msg.psz_header);
742 NPUTF8* retval = (NPUTF8*)NPN_MemAlloc(len);
743 if( retval )
745 memcpy(retval, _msg.psz_header, len);
746 STRINGN_TO_NPVARIANT(retval, len, result);
749 else
751 NULL_TO_NPVARIANT(result);
753 return INVOKERESULT_NO_ERROR;
755 case ID_message_message:
757 if( _msg.psz_message )
759 int len = strlen(_msg.psz_message);
760 NPUTF8* retval = (NPUTF8*)NPN_MemAlloc(len);
761 if( retval )
763 memcpy(retval, _msg.psz_message, len);
764 STRINGN_TO_NPVARIANT(retval, len, result);
767 else
769 NULL_TO_NPVARIANT(result);
771 return INVOKERESULT_NO_ERROR;
773 default:
777 return INVOKERESULT_GENERIC_ERROR;
780 const NPUTF8 * const LibvlcMessageNPObject::methodNames[] =
782 /* no methods */
785 const int LibvlcMessageNPObject::methodCount = sizeof(LibvlcMessageNPObject::methodNames)/sizeof(NPUTF8 *);
788 ** implementation of libvlc message iterator object
791 LibvlcMessageIteratorNPObject::LibvlcMessageIteratorNPObject(NPP instance, const NPClass *aClass) :
792 RuntimeNPObject(instance, aClass),
793 _p_iter(NULL)
795 /* is plugin still running */
796 if( instance->pdata )
798 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
799 libvlc_log_t *p_log = p_plugin->getLog();
800 if( p_log )
802 _p_iter = libvlc_log_get_iterator(p_log, NULL);
807 LibvlcMessageIteratorNPObject::~LibvlcMessageIteratorNPObject()
809 if( _p_iter )
810 libvlc_log_iterator_free(_p_iter, NULL);
813 const NPUTF8 * const LibvlcMessageIteratorNPObject::propertyNames[] =
815 "hasNext",
818 const int LibvlcMessageIteratorNPObject::propertyCount = sizeof(LibvlcMessageIteratorNPObject::propertyNames)/sizeof(NPUTF8 *);
820 enum LibvlcMessageIteratorNPObjectPropertyIds
822 ID_messageiterator_hasNext,
825 RuntimeNPObject::InvokeResult LibvlcMessageIteratorNPObject::getProperty(int index, NPVariant &result)
827 /* is plugin still running */
828 if( _instance->pdata )
830 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(_instance->pdata);
831 switch( index )
833 case ID_messageiterator_hasNext:
835 if( _p_iter && p_plugin->getLog() )
837 libvlc_exception_t ex;
838 libvlc_exception_init(&ex);
840 BOOLEAN_TO_NPVARIANT(libvlc_log_iterator_has_next(_p_iter, &ex), result);
841 if( libvlc_exception_raised(&ex) )
843 NPN_SetException(this, libvlc_exception_get_message(&ex));
844 libvlc_exception_clear(&ex);
845 return INVOKERESULT_GENERIC_ERROR;
848 else
850 BOOLEAN_TO_NPVARIANT(0, result);
852 return INVOKERESULT_NO_ERROR;
854 default:
858 return INVOKERESULT_GENERIC_ERROR;
861 const NPUTF8 * const LibvlcMessageIteratorNPObject::methodNames[] =
863 "next",
866 const int LibvlcMessageIteratorNPObject::methodCount = sizeof(LibvlcMessageIteratorNPObject::methodNames)/sizeof(NPUTF8 *);
868 enum LibvlcMessageIteratorNPObjectMethodIds
870 ID_messageiterator_next,
873 RuntimeNPObject::InvokeResult LibvlcMessageIteratorNPObject::invoke(int index, const NPVariant *args, uint32_t argCount, NPVariant &result)
875 /* is plugin still running */
876 if( _instance->pdata )
878 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(_instance->pdata);
879 libvlc_exception_t ex;
880 libvlc_exception_init(&ex);
882 switch( index )
884 case ID_messageiterator_next:
885 if( argCount == 0 )
887 if( _p_iter && p_plugin->getLog() )
889 struct libvlc_log_message_t buffer;
891 buffer.sizeof_msg = sizeof(buffer);
893 libvlc_log_iterator_next(_p_iter, &buffer, &ex);
894 if( libvlc_exception_raised(&ex) )
896 NPN_SetException(this, libvlc_exception_get_message(&ex));
897 libvlc_exception_clear(&ex);
898 return INVOKERESULT_GENERIC_ERROR;
900 else
902 LibvlcMessageNPObject* message =
903 static_cast<LibvlcMessageNPObject*>(NPN_CreateObject(_instance, RuntimeNPClass<LibvlcMessageNPObject>::getClass()));
904 if( message )
906 message->setMessage(buffer);
907 OBJECT_TO_NPVARIANT(message, result);
908 return INVOKERESULT_NO_ERROR;
910 return INVOKERESULT_OUT_OF_MEMORY;
913 return INVOKERESULT_GENERIC_ERROR;
915 return INVOKERESULT_NO_SUCH_METHOD;
916 default:
920 return INVOKERESULT_GENERIC_ERROR;
924 ** implementation of libvlc message object
927 const NPUTF8 * const LibvlcMessagesNPObject::propertyNames[] =
929 "count",
932 const int LibvlcMessagesNPObject::propertyCount = sizeof(LibvlcMessagesNPObject::propertyNames)/sizeof(NPUTF8 *);
934 enum LibvlcMessagesNPObjectPropertyIds
936 ID_messages_count,
939 RuntimeNPObject::InvokeResult LibvlcMessagesNPObject::getProperty(int index, NPVariant &result)
941 /* is plugin still running */
942 if( _instance->pdata )
944 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(_instance->pdata);
945 switch( index )
947 case ID_messages_count:
949 libvlc_log_t *p_log = p_plugin->getLog();
950 if( p_log )
952 libvlc_exception_t ex;
953 libvlc_exception_init(&ex);
955 INT32_TO_NPVARIANT(libvlc_log_count(p_log, &ex), result);
956 if( libvlc_exception_raised(&ex) )
958 NPN_SetException(this, libvlc_exception_get_message(&ex));
959 libvlc_exception_clear(&ex);
960 return INVOKERESULT_GENERIC_ERROR;
963 else
965 INT32_TO_NPVARIANT(0, result);
967 return INVOKERESULT_NO_ERROR;
969 default:
973 return INVOKERESULT_GENERIC_ERROR;
976 const NPUTF8 * const LibvlcMessagesNPObject::methodNames[] =
978 "clear",
979 "iterator",
982 const int LibvlcMessagesNPObject::methodCount = sizeof(LibvlcMessagesNPObject::methodNames)/sizeof(NPUTF8 *);
984 enum LibvlcMessagesNPObjectMethodIds
986 ID_messages_clear,
987 ID_messages_iterator,
990 RuntimeNPObject::InvokeResult LibvlcMessagesNPObject::invoke(int index, const NPVariant *args, uint32_t argCount, NPVariant &result)
992 /* is plugin still running */
993 if( _instance->pdata )
995 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(_instance->pdata);
996 libvlc_exception_t ex;
997 libvlc_exception_init(&ex);
999 switch( index )
1001 case ID_messages_clear:
1002 if( argCount == 0 )
1004 libvlc_log_t *p_log = p_plugin->getLog();
1005 if( p_log )
1007 libvlc_log_clear(p_log, &ex);
1008 if( libvlc_exception_raised(&ex) )
1010 NPN_SetException(this, libvlc_exception_get_message(&ex));
1011 libvlc_exception_clear(&ex);
1012 return INVOKERESULT_GENERIC_ERROR;
1015 return INVOKERESULT_NO_ERROR;
1017 return INVOKERESULT_NO_SUCH_METHOD;
1019 case ID_messages_iterator:
1020 if( argCount == 0 )
1022 LibvlcMessageIteratorNPObject* iter =
1023 static_cast<LibvlcMessageIteratorNPObject*>(NPN_CreateObject(_instance, RuntimeNPClass<LibvlcMessageIteratorNPObject>::getClass()));
1024 if( iter )
1026 OBJECT_TO_NPVARIANT(iter, result);
1027 return INVOKERESULT_NO_ERROR;
1029 return INVOKERESULT_OUT_OF_MEMORY;
1031 return INVOKERESULT_NO_SUCH_METHOD;
1033 default:
1037 return INVOKERESULT_GENERIC_ERROR;
1042 ** implementation of libvlc message object
1046 LibvlcLogNPObject::~LibvlcLogNPObject()
1048 if( isValid() )
1050 if( messagesObj ) NPN_ReleaseObject(messagesObj);
1054 const NPUTF8 * const LibvlcLogNPObject::propertyNames[] =
1056 "messages",
1057 "verbosity",
1060 const int LibvlcLogNPObject::propertyCount = sizeof(LibvlcLogNPObject::propertyNames)/sizeof(NPUTF8 *);
1062 enum LibvlcLogNPObjectPropertyIds
1064 ID_log_messages,
1065 ID_log_verbosity,
1068 RuntimeNPObject::InvokeResult LibvlcLogNPObject::getProperty(int index, NPVariant &result)
1070 /* is plugin still running */
1071 if( _instance->pdata )
1073 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(_instance->pdata);
1074 libvlc_exception_t ex;
1075 libvlc_exception_init(&ex);
1077 switch( index )
1079 case ID_log_messages:
1081 // create child object in lazyman fashion to avoid ownership problem with firefox
1082 if( ! messagesObj )
1083 messagesObj = NPN_CreateObject(_instance, RuntimeNPClass<LibvlcMessagesNPObject>::getClass());
1084 OBJECT_TO_NPVARIANT(NPN_RetainObject(messagesObj), result);
1085 return INVOKERESULT_NO_ERROR;
1087 case ID_log_verbosity:
1089 if( p_plugin->getLog() )
1091 INT32_TO_NPVARIANT(libvlc_get_log_verbosity(p_plugin->getVLC(),
1092 &ex), result);
1093 if( libvlc_exception_raised(&ex) )
1095 NPN_SetException(this, libvlc_exception_get_message(&ex));
1096 libvlc_exception_clear(&ex);
1097 return INVOKERESULT_GENERIC_ERROR;
1100 else
1102 /* log is not enabled, return -1 */
1103 DOUBLE_TO_NPVARIANT(-1.0, result);
1105 return INVOKERESULT_NO_ERROR;
1107 default:
1111 return INVOKERESULT_GENERIC_ERROR;
1114 RuntimeNPObject::InvokeResult LibvlcLogNPObject::setProperty(int index, const NPVariant &value)
1116 /* is plugin still running */
1117 if( _instance->pdata )
1119 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(_instance->pdata);
1120 libvlc_exception_t ex;
1121 libvlc_exception_init(&ex);
1123 switch( index )
1125 case ID_log_verbosity:
1126 if( isNumberValue(value) )
1128 libvlc_instance_t* p_libvlc = p_plugin->getVLC();
1129 libvlc_log_t *p_log = p_plugin->getLog();
1130 int verbosity = numberValue(value);
1131 if( verbosity >= 0 )
1133 if( ! p_log )
1135 p_log = libvlc_log_open(p_libvlc, &ex);
1136 if( libvlc_exception_raised(&ex) )
1138 NPN_SetException(this, libvlc_exception_get_message(&ex));
1139 libvlc_exception_clear(&ex);
1140 return INVOKERESULT_GENERIC_ERROR;
1142 p_plugin->setLog(p_log);
1144 libvlc_set_log_verbosity(p_libvlc, (unsigned)verbosity, &ex);
1145 if( libvlc_exception_raised(&ex) )
1147 NPN_SetException(this, libvlc_exception_get_message(&ex));
1148 libvlc_exception_clear(&ex);
1149 return INVOKERESULT_GENERIC_ERROR;
1152 else if( p_log )
1154 /* close log when verbosity is set to -1 */
1155 p_plugin->setLog(NULL);
1156 libvlc_log_close(p_log, &ex);
1157 if( libvlc_exception_raised(&ex) )
1159 NPN_SetException(this, libvlc_exception_get_message(&ex));
1160 libvlc_exception_clear(&ex);
1161 return INVOKERESULT_GENERIC_ERROR;
1164 return INVOKERESULT_NO_ERROR;
1166 return INVOKERESULT_INVALID_VALUE;
1167 default:
1171 return INVOKERESULT_GENERIC_ERROR;
1174 const NPUTF8 * const LibvlcLogNPObject::methodNames[] =
1176 /* no methods */
1179 const int LibvlcLogNPObject::methodCount = sizeof(LibvlcLogNPObject::methodNames)/sizeof(NPUTF8 *);
1182 ** implementation of libvlc playlist items object
1185 const NPUTF8 * const LibvlcPlaylistItemsNPObject::propertyNames[] =
1187 "count",
1190 const int LibvlcPlaylistItemsNPObject::propertyCount = sizeof(LibvlcPlaylistItemsNPObject::propertyNames)/sizeof(NPUTF8 *);
1192 enum LibvlcPlaylistItemsNPObjectPropertyIds
1194 ID_playlistitems_count,
1197 RuntimeNPObject::InvokeResult LibvlcPlaylistItemsNPObject::getProperty(int index, NPVariant &result)
1199 /* is plugin still running */
1200 if( _instance->pdata )
1202 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(_instance->pdata);
1203 libvlc_exception_t ex;
1204 libvlc_exception_init(&ex);
1206 switch( index )
1208 case ID_playlistitems_count:
1210 int val = libvlc_playlist_items_count(p_plugin->getVLC(), &ex);
1211 if( libvlc_exception_raised(&ex) )
1213 NPN_SetException(this, libvlc_exception_get_message(&ex));
1214 libvlc_exception_clear(&ex);
1215 return INVOKERESULT_GENERIC_ERROR;
1217 INT32_TO_NPVARIANT(val, result);
1218 return INVOKERESULT_NO_ERROR;
1220 default:
1224 return INVOKERESULT_GENERIC_ERROR;
1227 const NPUTF8 * const LibvlcPlaylistItemsNPObject::methodNames[] =
1229 "clear",
1230 "remove",
1233 const int LibvlcPlaylistItemsNPObject::methodCount = sizeof(LibvlcPlaylistItemsNPObject::methodNames)/sizeof(NPUTF8 *);
1235 enum LibvlcPlaylistItemsNPObjectMethodIds
1237 ID_playlistitems_clear,
1238 ID_playlistitems_remove,
1241 RuntimeNPObject::InvokeResult LibvlcPlaylistItemsNPObject::invoke(int index, const NPVariant *args, uint32_t argCount, NPVariant &result)
1243 /* is plugin still running */
1244 if( _instance->pdata )
1246 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(_instance->pdata);
1247 libvlc_exception_t ex;
1248 libvlc_exception_init(&ex);
1250 switch( index )
1252 case ID_playlistitems_clear:
1253 if( argCount == 0 )
1255 libvlc_playlist_clear(p_plugin->getVLC(), &ex);
1256 if( libvlc_exception_raised(&ex) )
1258 NPN_SetException(this, libvlc_exception_get_message(&ex));
1259 libvlc_exception_clear(&ex);
1260 return INVOKERESULT_GENERIC_ERROR;
1262 else
1264 VOID_TO_NPVARIANT(result);
1265 return INVOKERESULT_NO_ERROR;
1268 return INVOKERESULT_NO_SUCH_METHOD;
1269 case ID_playlistitems_remove:
1270 if( (argCount == 1) && isNumberValue(args[0]) )
1272 libvlc_playlist_delete_item(p_plugin->getVLC(), numberValue(args[0]), &ex);
1273 if( libvlc_exception_raised(&ex) )
1275 NPN_SetException(this, libvlc_exception_get_message(&ex));
1276 libvlc_exception_clear(&ex);
1277 return INVOKERESULT_GENERIC_ERROR;
1279 else
1281 VOID_TO_NPVARIANT(result);
1282 return INVOKERESULT_NO_ERROR;
1285 return INVOKERESULT_NO_SUCH_METHOD;
1286 default:
1290 return INVOKERESULT_GENERIC_ERROR;
1294 ** implementation of libvlc playlist object
1298 LibvlcPlaylistNPObject::~LibvlcPlaylistNPObject()
1300 if( isValid() )
1302 if( playlistItemsObj ) NPN_ReleaseObject(playlistItemsObj);
1306 const NPUTF8 * const LibvlcPlaylistNPObject::propertyNames[] =
1308 "itemCount", /* deprecated */
1309 "isPlaying",
1310 "items",
1313 const int LibvlcPlaylistNPObject::propertyCount = sizeof(LibvlcPlaylistNPObject::propertyNames)/sizeof(NPUTF8 *);
1315 enum LibvlcPlaylistNPObjectPropertyIds
1317 ID_playlist_itemcount,
1318 ID_playlist_isplaying,
1319 ID_playlist_items,
1322 RuntimeNPObject::InvokeResult LibvlcPlaylistNPObject::getProperty(int index, NPVariant &result)
1324 /* is plugin still running */
1325 if( _instance->pdata )
1327 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(_instance->pdata);
1328 libvlc_exception_t ex;
1329 libvlc_exception_init(&ex);
1331 switch( index )
1333 case ID_playlist_itemcount: /* deprecated */
1335 int val = libvlc_playlist_items_count(p_plugin->getVLC(), &ex);
1336 if( libvlc_exception_raised(&ex) )
1338 NPN_SetException(this, libvlc_exception_get_message(&ex));
1339 libvlc_exception_clear(&ex);
1340 return INVOKERESULT_GENERIC_ERROR;
1342 INT32_TO_NPVARIANT(val, result);
1343 return INVOKERESULT_NO_ERROR;
1345 case ID_playlist_isplaying:
1347 int val = libvlc_playlist_isplaying(p_plugin->getVLC(), &ex);
1348 if( libvlc_exception_raised(&ex) )
1350 NPN_SetException(this, libvlc_exception_get_message(&ex));
1351 libvlc_exception_clear(&ex);
1352 return INVOKERESULT_GENERIC_ERROR;
1354 BOOLEAN_TO_NPVARIANT(val, result);
1355 return INVOKERESULT_NO_ERROR;
1357 case ID_playlist_items:
1359 // create child object in lazyman fashion to avoid ownership problem with firefox
1360 if( ! playlistItemsObj )
1361 playlistItemsObj = NPN_CreateObject(_instance, RuntimeNPClass<LibvlcPlaylistItemsNPObject>::getClass());
1362 OBJECT_TO_NPVARIANT(NPN_RetainObject(playlistItemsObj), result);
1363 return INVOKERESULT_NO_ERROR;
1365 default:
1369 return INVOKERESULT_GENERIC_ERROR;
1372 const NPUTF8 * const LibvlcPlaylistNPObject::methodNames[] =
1374 "add",
1375 "play",
1376 "playItem",
1377 "togglePause",
1378 "stop",
1379 "next",
1380 "prev",
1381 "clear", /* deprecated */
1382 "removeItem", /* deprecated */
1385 const int LibvlcPlaylistNPObject::methodCount = sizeof(LibvlcPlaylistNPObject::methodNames)/sizeof(NPUTF8 *);
1387 enum LibvlcPlaylistNPObjectMethodIds
1389 ID_playlist_add,
1390 ID_playlist_play,
1391 ID_playlist_playItem,
1392 ID_playlist_togglepause,
1393 ID_playlist_stop,
1394 ID_playlist_next,
1395 ID_playlist_prev,
1396 ID_playlist_clear,
1397 ID_playlist_removeitem
1400 RuntimeNPObject::InvokeResult LibvlcPlaylistNPObject::invoke(int index, const NPVariant *args, uint32_t argCount, NPVariant &result)
1402 /* is plugin still running */
1403 if( _instance->pdata )
1405 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(_instance->pdata);
1406 libvlc_exception_t ex;
1407 libvlc_exception_init(&ex);
1409 switch( index )
1411 case ID_playlist_add:
1413 if( (argCount < 1) || (argCount > 3) )
1414 return INVOKERESULT_NO_SUCH_METHOD;
1416 char *url = NULL;
1418 // grab URL
1419 if( NPVARIANT_IS_STRING(args[0]) )
1421 char *s = stringValue(NPVARIANT_TO_STRING(args[0]));
1422 if( s )
1424 url = p_plugin->getAbsoluteURL(s);
1425 if( url )
1426 delete s;
1427 else
1428 // problem with combining url, use argument
1429 url = s;
1431 else
1432 return INVOKERESULT_OUT_OF_MEMORY;
1434 else
1435 return INVOKERESULT_NO_SUCH_METHOD;
1437 char *name = NULL;
1439 // grab name if available
1440 if( argCount > 1 )
1442 if( NPVARIANT_IS_NULL(args[1]) )
1444 // do nothing
1446 else if( NPVARIANT_IS_STRING(args[1]) )
1448 name = stringValue(NPVARIANT_TO_STRING(args[1]));
1450 else
1452 delete url;
1453 return INVOKERESULT_INVALID_VALUE;
1457 int i_options = 0;
1458 char** ppsz_options = NULL;
1460 // grab options if available
1461 if( argCount > 2 )
1463 if( NPVARIANT_IS_NULL(args[2]) )
1465 // do nothing
1467 else if( NPVARIANT_IS_STRING(args[2]) )
1469 parseOptions(NPVARIANT_TO_STRING(args[2]), &i_options, &ppsz_options);
1472 else if( NPVARIANT_IS_OBJECT(args[2]) )
1474 parseOptions(NPVARIANT_TO_OBJECT(args[2]), &i_options, &ppsz_options);
1476 else
1478 delete url;
1479 delete name;
1480 return INVOKERESULT_INVALID_VALUE;
1484 int item = libvlc_playlist_add_extended(p_plugin->getVLC(),
1485 url,
1486 name,
1487 i_options,
1488 const_cast<const char **>(ppsz_options),
1489 &ex);
1490 delete url;
1491 delete name;
1492 for( int i=0; i< i_options; ++i )
1494 delete ppsz_options[i];
1496 delete ppsz_options;
1498 if( libvlc_exception_raised(&ex) )
1500 NPN_SetException(this, libvlc_exception_get_message(&ex));
1501 libvlc_exception_clear(&ex);
1502 return INVOKERESULT_GENERIC_ERROR;
1504 else
1506 INT32_TO_NPVARIANT(item, result);
1507 return INVOKERESULT_NO_ERROR;
1510 case ID_playlist_play:
1511 if( argCount == 0 )
1513 libvlc_playlist_play(p_plugin->getVLC(), -1, 0, NULL, &ex);
1514 if( libvlc_exception_raised(&ex) )
1516 NPN_SetException(this, libvlc_exception_get_message(&ex));
1517 libvlc_exception_clear(&ex);
1518 return INVOKERESULT_GENERIC_ERROR;
1520 else
1522 VOID_TO_NPVARIANT(result);
1523 return INVOKERESULT_NO_ERROR;
1526 return INVOKERESULT_NO_SUCH_METHOD;
1527 case ID_playlist_playItem:
1528 if( (argCount == 1) && isNumberValue(args[0]) )
1530 libvlc_playlist_play(p_plugin->getVLC(), numberValue(args[0]), 0, NULL, &ex);
1531 if( libvlc_exception_raised(&ex) )
1533 NPN_SetException(this, libvlc_exception_get_message(&ex));
1534 libvlc_exception_clear(&ex);
1535 return INVOKERESULT_GENERIC_ERROR;
1537 else
1539 VOID_TO_NPVARIANT(result);
1540 return INVOKERESULT_NO_ERROR;
1543 return INVOKERESULT_NO_SUCH_METHOD;
1544 case ID_playlist_togglepause:
1545 if( argCount == 0 )
1547 libvlc_playlist_pause(p_plugin->getVLC(), &ex);
1548 if( libvlc_exception_raised(&ex) )
1550 NPN_SetException(this, libvlc_exception_get_message(&ex));
1551 libvlc_exception_clear(&ex);
1552 return INVOKERESULT_GENERIC_ERROR;
1554 else
1556 VOID_TO_NPVARIANT(result);
1557 return INVOKERESULT_NO_ERROR;
1560 return INVOKERESULT_NO_SUCH_METHOD;
1561 case ID_playlist_stop:
1562 if( argCount == 0 )
1564 libvlc_playlist_stop(p_plugin->getVLC(), &ex);
1565 if( libvlc_exception_raised(&ex) )
1567 NPN_SetException(this, libvlc_exception_get_message(&ex));
1568 libvlc_exception_clear(&ex);
1569 return INVOKERESULT_GENERIC_ERROR;
1571 else
1573 VOID_TO_NPVARIANT(result);
1574 return INVOKERESULT_NO_ERROR;
1577 return INVOKERESULT_NO_SUCH_METHOD;
1578 case ID_playlist_next:
1579 if( argCount == 0 )
1581 libvlc_playlist_next(p_plugin->getVLC(), &ex);
1582 if( libvlc_exception_raised(&ex) )
1584 NPN_SetException(this, libvlc_exception_get_message(&ex));
1585 libvlc_exception_clear(&ex);
1586 return INVOKERESULT_GENERIC_ERROR;
1588 else
1590 VOID_TO_NPVARIANT(result);
1591 return INVOKERESULT_NO_ERROR;
1594 return INVOKERESULT_NO_SUCH_METHOD;
1595 case ID_playlist_prev:
1596 if( argCount == 0 )
1598 libvlc_playlist_prev(p_plugin->getVLC(), &ex);
1599 if( libvlc_exception_raised(&ex) )
1601 NPN_SetException(this, libvlc_exception_get_message(&ex));
1602 libvlc_exception_clear(&ex);
1603 return INVOKERESULT_GENERIC_ERROR;
1605 else
1607 VOID_TO_NPVARIANT(result);
1608 return INVOKERESULT_NO_ERROR;
1611 return INVOKERESULT_NO_SUCH_METHOD;
1612 case ID_playlist_clear: /* deprecated */
1613 if( argCount == 0 )
1615 libvlc_playlist_clear(p_plugin->getVLC(), &ex);
1616 if( libvlc_exception_raised(&ex) )
1618 NPN_SetException(this, libvlc_exception_get_message(&ex));
1619 libvlc_exception_clear(&ex);
1620 return INVOKERESULT_GENERIC_ERROR;
1622 else
1624 VOID_TO_NPVARIANT(result);
1625 return INVOKERESULT_NO_ERROR;
1628 return INVOKERESULT_NO_SUCH_METHOD;
1629 case ID_playlist_removeitem: /* deprecated */
1630 if( (argCount == 1) && isNumberValue(args[0]) )
1632 libvlc_playlist_delete_item(p_plugin->getVLC(), numberValue(args[0]), &ex);
1633 if( libvlc_exception_raised(&ex) )
1635 NPN_SetException(this, libvlc_exception_get_message(&ex));
1636 libvlc_exception_clear(&ex);
1637 return INVOKERESULT_GENERIC_ERROR;
1639 else
1641 VOID_TO_NPVARIANT(result);
1642 return INVOKERESULT_NO_ERROR;
1645 return INVOKERESULT_NO_SUCH_METHOD;
1646 default:
1650 return INVOKERESULT_GENERIC_ERROR;
1653 void LibvlcPlaylistNPObject::parseOptions(const NPString &nps, int *i_options, char*** ppsz_options)
1655 if( nps.utf8length )
1657 char *s = stringValue(nps);
1658 char *val = s;
1659 if( val )
1661 long capacity = 16;
1662 char **options = (char **)malloc(capacity*sizeof(char *));
1663 if( options )
1665 int nOptions = 0;
1667 char *end = val + nps.utf8length;
1668 while( val < end )
1670 // skip leading blanks
1671 while( (val < end)
1672 && ((*val == ' ' ) || (*val == '\t')) )
1673 ++val;
1675 char *start = val;
1676 // skip till we get a blank character
1677 while( (val < end)
1678 && (*val != ' ' )
1679 && (*val != '\t') )
1681 char c = *(val++);
1682 if( ('\'' == c) || ('"' == c) )
1684 // skip till end of string
1685 while( (val < end) && (*(val++) != c ) );
1689 if( val > start )
1691 if( nOptions == capacity )
1693 capacity += 16;
1694 char **moreOptions = (char **)realloc(options, capacity*sizeof(char*));
1695 if( ! moreOptions )
1697 /* failed to allocate more memory */
1698 delete s;
1699 /* return what we got so far */
1700 *i_options = nOptions;
1701 *ppsz_options = options;
1702 return;
1704 options = moreOptions;
1706 *(val++) = '\0';
1707 options[nOptions++] = strdup(start);
1709 else
1710 // must be end of string
1711 break;
1713 *i_options = nOptions;
1714 *ppsz_options = options;
1716 delete s;
1721 void LibvlcPlaylistNPObject::parseOptions(NPObject *obj, int *i_options, char*** ppsz_options)
1723 /* WARNING: Safari does not implement NPN_HasProperty/NPN_HasMethod */
1725 NPVariant value;
1727 /* we are expecting to have a Javascript Array object */
1728 NPIdentifier propId = NPN_GetStringIdentifier("length");
1729 if( NPN_GetProperty(_instance, obj, propId, &value) )
1731 int count = numberValue(value);
1732 NPN_ReleaseVariantValue(&value);
1734 if( count )
1736 long capacity = 16;
1737 char **options = (char **)malloc(capacity*sizeof(char *));
1738 if( options )
1740 int nOptions = 0;
1742 while( nOptions < count )
1744 propId = NPN_GetIntIdentifier(nOptions);
1745 if( ! NPN_GetProperty(_instance, obj, propId, &value) )
1746 /* return what we got so far */
1747 break;
1749 if( ! NPVARIANT_IS_STRING(value) )
1751 /* return what we got so far */
1752 NPN_ReleaseVariantValue(&value);
1753 break;
1756 if( nOptions == capacity )
1758 capacity += 16;
1759 char **moreOptions = (char **)realloc(options, capacity*sizeof(char*));
1760 if( ! moreOptions )
1762 /* failed to allocate more memory */
1763 NPN_ReleaseVariantValue(&value);
1764 /* return what we got so far */
1765 *i_options = nOptions;
1766 *ppsz_options = options;
1767 break;
1769 options = moreOptions;
1772 options[nOptions++] = stringValue(value);
1774 *i_options = nOptions;
1775 *ppsz_options = options;
1782 ** implementation of libvlc video object
1785 const NPUTF8 * const LibvlcVideoNPObject::propertyNames[] =
1787 "fullscreen",
1788 "height",
1789 "width",
1790 "aspectRatio",
1791 "subtitle",
1792 "crop",
1793 "teletext"
1796 enum LibvlcVideoNPObjectPropertyIds
1798 ID_video_fullscreen,
1799 ID_video_height,
1800 ID_video_width,
1801 ID_video_aspectratio,
1802 ID_video_subtitle,
1803 ID_video_crop,
1804 ID_video_teletext
1807 const int LibvlcVideoNPObject::propertyCount = sizeof(LibvlcVideoNPObject::propertyNames)/sizeof(NPUTF8 *);
1809 RuntimeNPObject::InvokeResult LibvlcVideoNPObject::getProperty(int index, NPVariant &result)
1811 /* is plugin still running */
1812 if( _instance->pdata )
1814 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(_instance->pdata);
1815 libvlc_exception_t ex;
1816 libvlc_exception_init(&ex);
1818 libvlc_media_player_t *p_md = libvlc_playlist_get_media_player(p_plugin->getVLC(), &ex);
1819 if( libvlc_exception_raised(&ex) )
1821 NPN_SetException(this, libvlc_exception_get_message(&ex));
1822 libvlc_exception_clear(&ex);
1823 return INVOKERESULT_GENERIC_ERROR;
1826 switch( index )
1828 case ID_video_fullscreen:
1830 int val = libvlc_get_fullscreen(p_md, &ex);
1831 libvlc_media_player_release(p_md);
1832 if( libvlc_exception_raised(&ex) )
1834 NPN_SetException(this, libvlc_exception_get_message(&ex));
1835 libvlc_exception_clear(&ex);
1836 return INVOKERESULT_GENERIC_ERROR;
1838 BOOLEAN_TO_NPVARIANT(val, result);
1839 return INVOKERESULT_NO_ERROR;
1841 case ID_video_height:
1843 int val = libvlc_video_get_height(p_md, &ex);
1844 libvlc_media_player_release(p_md);
1845 if( libvlc_exception_raised(&ex) )
1847 NPN_SetException(this, libvlc_exception_get_message(&ex));
1848 libvlc_exception_clear(&ex);
1849 return INVOKERESULT_GENERIC_ERROR;
1851 INT32_TO_NPVARIANT(val, result);
1852 return INVOKERESULT_NO_ERROR;
1854 case ID_video_width:
1856 int val = libvlc_video_get_width(p_md, &ex);
1857 libvlc_media_player_release(p_md);
1858 if( libvlc_exception_raised(&ex) )
1860 NPN_SetException(this, libvlc_exception_get_message(&ex));
1861 libvlc_exception_clear(&ex);
1862 return INVOKERESULT_GENERIC_ERROR;
1864 INT32_TO_NPVARIANT(val, result);
1865 return INVOKERESULT_NO_ERROR;
1867 case ID_video_aspectratio:
1869 NPUTF8 *psz_aspect = libvlc_video_get_aspect_ratio(p_md, &ex);
1870 libvlc_media_player_release(p_md);
1871 if( libvlc_exception_raised(&ex) )
1873 NPN_SetException(this, libvlc_exception_get_message(&ex));
1874 libvlc_exception_clear(&ex);
1875 return INVOKERESULT_GENERIC_ERROR;
1877 if( !psz_aspect )
1878 return INVOKERESULT_GENERIC_ERROR;
1880 STRINGZ_TO_NPVARIANT(psz_aspect, result);
1881 return INVOKERESULT_NO_ERROR;
1883 case ID_video_subtitle:
1885 int i_spu = libvlc_video_get_spu(p_md, &ex);
1886 libvlc_media_player_release(p_md);
1887 if( libvlc_exception_raised(&ex) )
1889 NPN_SetException(this, libvlc_exception_get_message(&ex));
1890 libvlc_exception_clear(&ex);
1891 return INVOKERESULT_GENERIC_ERROR;
1893 INT32_TO_NPVARIANT(i_spu, result);
1894 return INVOKERESULT_NO_ERROR;
1896 case ID_video_crop:
1898 NPUTF8 *psz_geometry = libvlc_video_get_crop_geometry(p_md, &ex);
1899 libvlc_media_player_release(p_md);
1900 if( libvlc_exception_raised(&ex) )
1902 NPN_SetException(this, libvlc_exception_get_message(&ex));
1903 libvlc_exception_clear(&ex);
1904 return INVOKERESULT_GENERIC_ERROR;
1906 if( !psz_geometry )
1907 return INVOKERESULT_GENERIC_ERROR;
1909 STRINGZ_TO_NPVARIANT(psz_geometry, result);
1910 return INVOKERESULT_NO_ERROR;
1912 case ID_video_teletext:
1914 int i_page = libvlc_video_get_teletext(p_md, &ex);
1915 libvlc_media_player_release(p_md);
1916 if( libvlc_exception_raised(&ex) )
1918 NPN_SetException(this, libvlc_exception_get_message(&ex));
1919 libvlc_exception_clear(&ex);
1920 return INVOKERESULT_GENERIC_ERROR;
1922 INT32_TO_NPVARIANT(i_page, result);
1923 return INVOKERESULT_NO_ERROR;
1926 libvlc_media_player_release(p_md);
1928 return INVOKERESULT_GENERIC_ERROR;
1931 RuntimeNPObject::InvokeResult LibvlcVideoNPObject::setProperty(int index, const NPVariant &value)
1933 /* is plugin still running */
1934 if( _instance->pdata )
1936 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(_instance->pdata);
1937 libvlc_exception_t ex;
1938 libvlc_exception_init(&ex);
1940 libvlc_media_player_t *p_md = libvlc_playlist_get_media_player(p_plugin->getVLC(), &ex);
1941 if( libvlc_exception_raised(&ex) )
1943 NPN_SetException(this, libvlc_exception_get_message(&ex));
1944 libvlc_exception_clear(&ex);
1945 return INVOKERESULT_GENERIC_ERROR;
1948 switch( index )
1950 case ID_video_fullscreen:
1952 if( ! NPVARIANT_IS_BOOLEAN(value) )
1954 libvlc_media_player_release(p_md);
1955 return INVOKERESULT_INVALID_VALUE;
1958 int val = NPVARIANT_TO_BOOLEAN(value);
1959 libvlc_set_fullscreen(p_md, val, &ex);
1960 libvlc_media_player_release(p_md);
1962 if( libvlc_exception_raised(&ex) )
1964 NPN_SetException(this, libvlc_exception_get_message(&ex));
1965 libvlc_exception_clear(&ex);
1966 return INVOKERESULT_GENERIC_ERROR;
1968 return INVOKERESULT_NO_ERROR;
1970 case ID_video_aspectratio:
1972 char *psz_aspect = NULL;
1974 if( ! NPVARIANT_IS_STRING(value) )
1976 libvlc_media_player_release(p_md);
1977 return INVOKERESULT_INVALID_VALUE;
1980 psz_aspect = stringValue(NPVARIANT_TO_STRING(value));
1981 if( !psz_aspect )
1983 libvlc_media_player_release(p_md);
1984 return INVOKERESULT_GENERIC_ERROR;
1987 libvlc_video_set_aspect_ratio(p_md, psz_aspect, &ex);
1988 delete psz_aspect;
1989 libvlc_media_player_release(p_md);
1991 if( libvlc_exception_raised(&ex) )
1993 NPN_SetException(this, libvlc_exception_get_message(&ex));
1994 libvlc_exception_clear(&ex);
1995 return INVOKERESULT_GENERIC_ERROR;
1997 return INVOKERESULT_NO_ERROR;
1999 case ID_video_subtitle:
2001 if( isNumberValue(value) )
2003 libvlc_video_set_spu(p_md,
2004 numberValue(value), &ex);
2005 libvlc_media_player_release(p_md);
2006 if( libvlc_exception_raised(&ex) )
2008 NPN_SetException(this, libvlc_exception_get_message(&ex));
2009 libvlc_exception_clear(&ex);
2010 return INVOKERESULT_GENERIC_ERROR;
2012 return INVOKERESULT_NO_ERROR;
2014 libvlc_media_player_release(p_md);
2015 return INVOKERESULT_INVALID_VALUE;
2017 case ID_video_crop:
2019 char *psz_geometry = NULL;
2021 if( ! NPVARIANT_IS_STRING(value) )
2023 libvlc_media_player_release(p_md);
2024 return INVOKERESULT_INVALID_VALUE;
2027 psz_geometry = stringValue(NPVARIANT_TO_STRING(value));
2028 if( !psz_geometry )
2030 libvlc_media_player_release(p_md);
2031 return INVOKERESULT_GENERIC_ERROR;
2034 libvlc_video_set_crop_geometry(p_md, psz_geometry, &ex);
2035 delete psz_geometry;
2036 libvlc_media_player_release(p_md);
2038 if( libvlc_exception_raised(&ex) )
2040 NPN_SetException(this, libvlc_exception_get_message(&ex));
2041 libvlc_exception_clear(&ex);
2042 return INVOKERESULT_GENERIC_ERROR;
2044 return INVOKERESULT_NO_ERROR;
2046 case ID_video_teletext:
2048 if( isNumberValue(value) )
2050 libvlc_video_set_teletext(p_md,
2051 numberValue(value), &ex);
2052 libvlc_media_player_release(p_md);
2053 if( libvlc_exception_raised(&ex) )
2055 NPN_SetException(this, libvlc_exception_get_message(&ex));
2056 libvlc_exception_clear(&ex);
2057 return INVOKERESULT_GENERIC_ERROR;
2059 return INVOKERESULT_NO_ERROR;
2061 libvlc_media_player_release(p_md);
2062 return INVOKERESULT_INVALID_VALUE;
2065 libvlc_media_player_release(p_md);
2067 return INVOKERESULT_GENERIC_ERROR;
2070 const NPUTF8 * const LibvlcVideoNPObject::methodNames[] =
2072 "toggleFullscreen",
2073 "toggleTeletext"
2076 enum LibvlcVideoNPObjectMethodIds
2078 ID_video_togglefullscreen,
2079 ID_video_toggleteletext
2082 const int LibvlcVideoNPObject::methodCount = sizeof(LibvlcVideoNPObject::methodNames)/sizeof(NPUTF8 *);
2084 RuntimeNPObject::InvokeResult LibvlcVideoNPObject::invoke(int index, const NPVariant *args, uint32_t argCount, NPVariant &result)
2086 /* is plugin still running */
2087 if( _instance->pdata )
2089 VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(_instance->pdata);
2090 libvlc_exception_t ex;
2091 libvlc_exception_init(&ex);
2093 libvlc_media_player_t *p_md = libvlc_playlist_get_media_player(p_plugin->getVLC(), &ex);
2094 if( libvlc_exception_raised(&ex) )
2096 NPN_SetException(this, libvlc_exception_get_message(&ex));
2097 libvlc_exception_clear(&ex);
2098 return INVOKERESULT_GENERIC_ERROR;
2101 switch( index )
2103 case ID_video_togglefullscreen:
2104 if( argCount == 0 )
2106 libvlc_toggle_fullscreen(p_md, &ex);
2107 libvlc_media_player_release(p_md);
2108 if( libvlc_exception_raised(&ex) )
2110 NPN_SetException(this, libvlc_exception_get_message(&ex));
2111 libvlc_exception_clear(&ex);
2112 return INVOKERESULT_GENERIC_ERROR;
2114 else
2116 VOID_TO_NPVARIANT(result);
2117 return INVOKERESULT_NO_ERROR;
2120 else
2122 /* cannot get md, probably not playing */
2123 if( libvlc_exception_raised(&ex) )
2125 NPN_SetException(this, libvlc_exception_get_message(&ex));
2126 libvlc_exception_clear(&ex);
2128 return INVOKERESULT_GENERIC_ERROR;
2130 return INVOKERESULT_NO_SUCH_METHOD;
2131 case ID_video_toggleteletext:
2132 if( argCount == 0 )
2134 libvlc_toggle_teletext(p_md, &ex);
2135 libvlc_media_player_release(p_md);
2136 if( libvlc_exception_raised(&ex) )
2138 NPN_SetException(this, libvlc_exception_get_message(&ex));
2139 libvlc_exception_clear(&ex);
2140 return INVOKERESULT_GENERIC_ERROR;
2142 else
2144 VOID_TO_NPVARIANT(result);
2145 return INVOKERESULT_NO_ERROR;
2148 else
2150 /* cannot get md, probably not playing */
2151 if( libvlc_exception_raised(&ex) )
2153 NPN_SetException(this, libvlc_exception_get_message(&ex));
2154 libvlc_exception_clear(&ex);
2156 return INVOKERESULT_GENERIC_ERROR;
2158 return INVOKERESULT_NO_SUCH_METHOD;
2159 default:
2160 return INVOKERESULT_NO_SUCH_METHOD;
2163 return INVOKERESULT_GENERIC_ERROR;