Audio track and channel support for JavaScript API. The ActiveX vlc.audio.channel...
[vlc.git] / mozilla / control / npolibvlc.cpp
1 /*****************************************************************************\r
2  * npolibvlc.cpp: official Javascript APIs\r
3  *****************************************************************************\r
4  * Copyright (C) 2002-2006 the VideoLAN team\r
5  *\r
6  * Authors: Damien Fouilleul <Damien.Fouilleul@laposte.net>\r
7  *\r
8  * This program is free software; you can redistribute it and/or modify\r
9  * it under the terms of the GNU General Public License as published by\r
10  * the Free Software Foundation; either version 2 of the License, or\r
11  * (at your option) any later version.\r
12  *\r
13  * This program is distributed in the hope that it will be useful,\r
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
16  * GNU General Public License for more details.\r
17  *\r
18  * You should have received a copy of the GNU General Public License\r
19  * along with this program; if not, write to the Free Software\r
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.\r
21  *****************************************************************************/\r
22 \r
23 #include "config.h"\r
24 \r
25 #include <stdio.h>\r
26 #include <string.h>\r
27 #include <stdlib.h>\r
28 \r
29 /* Mozilla stuff */\r
30 #ifdef HAVE_MOZILLA_CONFIG_H\r
31 #   include <mozilla-config.h>\r
32 #endif\r
33 \r
34 #include "npolibvlc.h"\r
35 #include "vlcplugin.h"\r
36 \r
37 /*\r
38 ** implementation of libvlc root object\r
39 */\r
40 \r
41 LibvlcRootNPObject::LibvlcRootNPObject(NPP instance, const NPClass *aClass) :\r
42         RuntimeNPObject(instance, aClass)\r
43 {\r
44     audioObj = NPN_CreateObject(instance, RuntimeNPClass<LibvlcAudioNPObject>::getClass());\r
45     inputObj = NPN_CreateObject(instance, RuntimeNPClass<LibvlcInputNPObject>::getClass());\r
46     logObj = NPN_CreateObject(instance, RuntimeNPClass<LibvlcLogNPObject>::getClass());\r
47     playlistObj = NPN_CreateObject(instance, RuntimeNPClass<LibvlcPlaylistNPObject>::getClass());\r
48     videoObj = NPN_CreateObject(instance,RuntimeNPClass<LibvlcVideoNPObject>::getClass());\r
49 }\r
50 \r
51 LibvlcRootNPObject::~LibvlcRootNPObject()\r
52 {\r
53     NPN_ReleaseObject(audioObj);\r
54     NPN_ReleaseObject(inputObj);\r
55     NPN_ReleaseObject(logObj);\r
56     NPN_ReleaseObject(playlistObj);\r
57     NPN_ReleaseObject(videoObj);\r
58 }\r
59 \r
60 const NPUTF8 * const LibvlcRootNPObject::propertyNames[] = \r
61 {\r
62     "audio",\r
63     "input",\r
64     "log",\r
65     "playlist",\r
66     "video",\r
67     "VersionInfo",\r
68 };\r
69 \r
70 const int LibvlcRootNPObject::propertyCount = sizeof(LibvlcRootNPObject::propertyNames)/sizeof(NPUTF8 *);\r
71 \r
72 enum LibvlcRootNPObjectPropertyIds\r
73 {\r
74     ID_root_audio = 0,\r
75     ID_root_input,\r
76     ID_root_log,\r
77     ID_root_playlist,\r
78     ID_root_video,\r
79     ID_root_VersionInfo,\r
80 };\r
81 \r
82 RuntimeNPObject::InvokeResult LibvlcRootNPObject::getProperty(int index, NPVariant &result)\r
83 {\r
84     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(_instance->pdata);\r
85     if( p_plugin )\r
86     {\r
87         switch( index )\r
88         {\r
89             case ID_root_audio:\r
90                 OBJECT_TO_NPVARIANT(NPN_RetainObject(audioObj), result);\r
91                 return INVOKERESULT_NO_ERROR;\r
92             case ID_root_input:\r
93                 OBJECT_TO_NPVARIANT(NPN_RetainObject(inputObj), result);\r
94                 return INVOKERESULT_NO_ERROR;\r
95             case ID_root_log:\r
96                 OBJECT_TO_NPVARIANT(NPN_RetainObject(logObj), result);\r
97                 return INVOKERESULT_NO_ERROR;\r
98             case ID_root_playlist:\r
99                 OBJECT_TO_NPVARIANT(NPN_RetainObject(playlistObj), result);\r
100                 return INVOKERESULT_NO_ERROR;\r
101             case ID_root_video:\r
102                 OBJECT_TO_NPVARIANT(NPN_RetainObject(videoObj), result);\r
103                 return INVOKERESULT_NO_ERROR;\r
104             case ID_root_VersionInfo:\r
105             {\r
106                 int len = strlen(VLC_Version());\r
107                 NPUTF8 *retval =(NPUTF8*)NPN_MemAlloc(len);\r
108                 if( retval )\r
109                 {\r
110                     memcpy(retval, VLC_Version(), len);\r
111                     STRINGN_TO_NPVARIANT(retval, len, result);\r
112                 }\r
113                 else\r
114                 {\r
115                     NULL_TO_NPVARIANT(result);\r
116                 }\r
117                 return INVOKERESULT_NO_ERROR;\r
118             }\r
119             default:\r
120                 ;\r
121         }\r
122     }\r
123     return INVOKERESULT_GENERIC_ERROR;\r
124 }\r
125 \r
126 const NPUTF8 * const LibvlcRootNPObject::methodNames[] =\r
127 {\r
128     "versionInfo",\r
129 };\r
130 \r
131 const int LibvlcRootNPObject::methodCount = sizeof(LibvlcRootNPObject::methodNames)/sizeof(NPUTF8 *);\r
132 \r
133 enum LibvlcRootNPObjectMethodIds\r
134 {\r
135     ID_root_versionInfo,\r
136 };\r
137 \r
138 RuntimeNPObject::InvokeResult LibvlcRootNPObject::invoke(int index, const NPVariant *args, uint32_t argCount, NPVariant &result)\r
139 {\r
140     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(_instance->pdata);\r
141     if( p_plugin )\r
142     {\r
143         libvlc_exception_t ex;\r
144         libvlc_exception_init(&ex);\r
145 \r
146         switch( index )\r
147         {\r
148             case ID_root_versionInfo:\r
149                 if( argCount == 0 )\r
150                 {\r
151                     int len = strlen(VLC_Version());\r
152                     NPUTF8 *retval =(NPUTF8*)NPN_MemAlloc(len);\r
153                     if( retval )\r
154                     {\r
155                         memcpy(retval, VLC_Version(), len);\r
156                         STRINGN_TO_NPVARIANT(retval, len, result);\r
157                     }\r
158                     else\r
159                     {\r
160                         NULL_TO_NPVARIANT(result);\r
161                     }\r
162                     return INVOKERESULT_NO_ERROR;\r
163                 }\r
164                 return INVOKERESULT_NO_SUCH_METHOD;\r
165             default:\r
166                 ;\r
167         }\r
168     }\r
169     return INVOKERESULT_GENERIC_ERROR;\r
170 }\r
171 \r
172 /*\r
173 ** implementation of libvlc audio object\r
174 */\r
175 \r
176 const NPUTF8 * const LibvlcAudioNPObject::propertyNames[] = \r
177 {\r
178     "mute",\r
179     "volume",\r
180     "track",\r
181     "channel",\r
182 };\r
183 \r
184 const int LibvlcAudioNPObject::propertyCount = sizeof(LibvlcAudioNPObject::propertyNames)/sizeof(NPUTF8 *);\r
185 \r
186 enum LibvlcAudioNPObjectPropertyIds\r
187 {\r
188     ID_audio_mute,\r
189     ID_audio_volume,\r
190     ID_audio_track,\r
191     ID_audio_channel,\r
192 };\r
193 \r
194 RuntimeNPObject::InvokeResult LibvlcAudioNPObject::getProperty(int index, NPVariant &result)\r
195 {\r
196     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(_instance->pdata);\r
197     if( p_plugin )\r
198     {\r
199         libvlc_exception_t ex;\r
200         libvlc_exception_init(&ex);\r
201 \r
202         libvlc_input_t *p_input = libvlc_playlist_get_input(p_plugin->getVLC(), &ex);\r
203         if( libvlc_exception_raised(&ex) )\r
204         {\r
205             NPN_SetException(this, libvlc_exception_get_message(&ex));\r
206             libvlc_exception_clear(&ex);\r
207             return INVOKERESULT_GENERIC_ERROR;\r
208         }\r
209 \r
210         switch( index )\r
211         {\r
212             case ID_audio_mute:\r
213             {\r
214                 vlc_bool_t muted = libvlc_audio_get_mute(p_plugin->getVLC(), &ex);\r
215                 libvlc_input_free(p_input);\r
216                 if( libvlc_exception_raised(&ex) )\r
217                 {\r
218                     NPN_SetException(this, libvlc_exception_get_message(&ex));\r
219                     libvlc_exception_clear(&ex);\r
220                     return INVOKERESULT_GENERIC_ERROR;\r
221                 }\r
222                 BOOLEAN_TO_NPVARIANT(muted, result);\r
223                 return INVOKERESULT_NO_ERROR;\r
224             }\r
225             case ID_audio_volume:\r
226             {\r
227                 int volume = libvlc_audio_get_volume(p_plugin->getVLC(), &ex);\r
228                 libvlc_input_free(p_input);\r
229                 if( libvlc_exception_raised(&ex) )\r
230                 {\r
231                     NPN_SetException(this, libvlc_exception_get_message(&ex));\r
232                     libvlc_exception_clear(&ex);\r
233                     return INVOKERESULT_GENERIC_ERROR;\r
234                 }\r
235                 INT32_TO_NPVARIANT(volume, result);\r
236                 return INVOKERESULT_NO_ERROR;\r
237             }\r
238             case ID_audio_track:\r
239             {\r
240                 int track = libvlc_audio_get_track(p_input, &ex);\r
241                 libvlc_input_free(p_input);\r
242                 if( libvlc_exception_raised(&ex) )\r
243                 {\r
244                     NPN_SetException(this, libvlc_exception_get_message(&ex));\r
245                     libvlc_exception_clear(&ex);\r
246                     return INVOKERESULT_GENERIC_ERROR;\r
247                 }\r
248                 INT32_TO_NPVARIANT(track, result);\r
249                 return INVOKERESULT_NO_ERROR;\r
250             }\r
251             case ID_audio_channel:\r
252             {\r
253                 NPUTF8 *psz_channel = libvlc_audio_get_channel(p_plugin->getVLC(), &ex);\r
254                 libvlc_input_free(p_input);\r
255                 if( libvlc_exception_raised(&ex) )\r
256                 {\r
257                     NPN_SetException(this, libvlc_exception_get_message(&ex));\r
258                     libvlc_exception_clear(&ex);\r
259                     return INVOKERESULT_GENERIC_ERROR;\r
260                 }\r
261                 if( !psz_channel )\r
262                     return INVOKERESULT_GENERIC_ERROR;\r
263 \r
264                 STRINGZ_TO_NPVARIANT(psz_channel, result);\r
265                 return INVOKERESULT_NO_ERROR;\r
266             }\r
267             default:\r
268                 ;\r
269         }\r
270         libvlc_input_free(p_input);\r
271     }\r
272     return INVOKERESULT_GENERIC_ERROR;\r
273 }\r
274 \r
275 RuntimeNPObject::InvokeResult LibvlcAudioNPObject::setProperty(int index, const NPVariant &value)\r
276 {\r
277     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(_instance->pdata);\r
278     if( p_plugin )\r
279     {\r
280         libvlc_exception_t ex;\r
281         libvlc_exception_init(&ex);\r
282 \r
283         libvlc_input_t *p_input = libvlc_playlist_get_input(p_plugin->getVLC(), &ex);\r
284         if( libvlc_exception_raised(&ex) )\r
285         {\r
286             NPN_SetException(this, libvlc_exception_get_message(&ex));\r
287             libvlc_exception_clear(&ex);\r
288             return INVOKERESULT_GENERIC_ERROR;\r
289         }\r
290 \r
291         switch( index )\r
292         {\r
293             case ID_audio_mute:\r
294                 if( NPVARIANT_IS_BOOLEAN(value) )\r
295                 {\r
296                     libvlc_audio_set_mute(p_plugin->getVLC(),\r
297                                           NPVARIANT_TO_BOOLEAN(value), &ex);\r
298                     libvlc_input_free(p_input);\r
299                     if( libvlc_exception_raised(&ex) )\r
300                     {\r
301                         NPN_SetException(this, libvlc_exception_get_message(&ex));\r
302                         libvlc_exception_clear(&ex);\r
303                         return INVOKERESULT_GENERIC_ERROR;\r
304                     }\r
305                     return INVOKERESULT_NO_ERROR;\r
306                 }\r
307                 return INVOKERESULT_INVALID_VALUE;\r
308             case ID_audio_volume:\r
309                 libvlc_input_free(p_input);\r
310                 if( isNumberValue(value) )\r
311                 {\r
312                     libvlc_audio_set_volume(p_plugin->getVLC(),\r
313                                             numberValue(value), &ex);\r
314                     if( libvlc_exception_raised(&ex) )\r
315                     {\r
316                         NPN_SetException(this, libvlc_exception_get_message(&ex));\r
317                         libvlc_exception_clear(&ex);\r
318                         return INVOKERESULT_GENERIC_ERROR;\r
319                     }\r
320                     return INVOKERESULT_NO_ERROR;\r
321                 }\r
322                 return INVOKERESULT_INVALID_VALUE;\r
323             case ID_audio_track:\r
324                 if( isNumberValue(value) )\r
325                 {\r
326                     libvlc_audio_set_track(p_input,\r
327                                            numberValue(value), &ex);\r
328                     libvlc_input_free(p_input);\r
329                     if( libvlc_exception_raised(&ex) )\r
330                     {\r
331                         NPN_SetException(this, libvlc_exception_get_message(&ex));\r
332                         libvlc_exception_clear(&ex);\r
333                         return INVOKERESULT_GENERIC_ERROR;\r
334                     }\r
335                     return INVOKERESULT_NO_ERROR;\r
336                 }\r
337                 libvlc_input_free(p_input);\r
338                 return INVOKERESULT_INVALID_VALUE;\r
339             case ID_audio_channel:\r
340             {\r
341                 char *psz_channel = NULL;\r
342 \r
343                 libvlc_input_free(p_input);\r
344                 if( ! NPVARIANT_IS_STRING(value) )\r
345                     return INVOKERESULT_INVALID_VALUE;\r
346 \r
347                 psz_channel = stringValue(NPVARIANT_TO_STRING(value));\r
348                 if( !psz_channel )\r
349                     return INVOKERESULT_GENERIC_ERROR;\r
350 \r
351                 libvlc_audio_set_channel(p_plugin->getVLC(), psz_channel, &ex);\r
352                 if( psz_channel )\r
353                     free( psz_channel );\r
354 \r
355                 if( libvlc_exception_raised(&ex) )\r
356                 {\r
357                     NPN_SetException(this, libvlc_exception_get_message(&ex));\r
358                     libvlc_exception_clear(&ex);\r
359                     return INVOKERESULT_GENERIC_ERROR;\r
360                 }\r
361                 return INVOKERESULT_NO_ERROR;\r
362             }\r
363             default:\r
364                 ;\r
365         }\r
366         libvlc_input_free(p_input);\r
367     }\r
368     return INVOKERESULT_GENERIC_ERROR;\r
369 }\r
370 \r
371 const NPUTF8 * const LibvlcAudioNPObject::methodNames[] =\r
372 {\r
373     "toggleMute",\r
374 };\r
375 \r
376 const int LibvlcAudioNPObject::methodCount = sizeof(LibvlcAudioNPObject::methodNames)/sizeof(NPUTF8 *);\r
377 \r
378 enum LibvlcAudioNPObjectMethodIds\r
379 {\r
380     ID_audio_togglemute,\r
381 };\r
382 \r
383 RuntimeNPObject::InvokeResult LibvlcAudioNPObject::invoke(int index, const NPVariant *args, uint32_t argCount, NPVariant &result)\r
384 {\r
385     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(_instance->pdata);\r
386     if( p_plugin )\r
387     {\r
388         libvlc_exception_t ex;\r
389         libvlc_exception_init(&ex);\r
390 \r
391         switch( index )\r
392         {\r
393             case ID_audio_togglemute:\r
394                 if( argCount == 0 )\r
395                 {\r
396                     libvlc_audio_toggle_mute(p_plugin->getVLC(), &ex);\r
397                     if( libvlc_exception_raised(&ex) )\r
398                     {\r
399                         NPN_SetException(this, libvlc_exception_get_message(&ex));\r
400                         libvlc_exception_clear(&ex);\r
401                         return INVOKERESULT_GENERIC_ERROR;\r
402                     }\r
403                     else\r
404                     {\r
405                         VOID_TO_NPVARIANT(result);\r
406                         return INVOKERESULT_NO_ERROR;\r
407                     }\r
408                 }\r
409                 return INVOKERESULT_NO_SUCH_METHOD;\r
410             default:\r
411                 ;\r
412         }\r
413     }\r
414     return INVOKERESULT_GENERIC_ERROR;\r
415 }\r
416 \r
417 /*\r
418 ** implementation of libvlc input object\r
419 */\r
420 \r
421 const NPUTF8 * const LibvlcInputNPObject::propertyNames[] = \r
422 {\r
423     "length",\r
424     "position",\r
425     "time",\r
426     "state",\r
427     "rate",\r
428     "fps",\r
429     "hasVout",\r
430 };\r
431 \r
432 const int LibvlcInputNPObject::propertyCount = sizeof(LibvlcInputNPObject::propertyNames)/sizeof(NPUTF8 *);\r
433 \r
434 enum LibvlcInputNPObjectPropertyIds\r
435 {\r
436     ID_input_length,\r
437     ID_input_position,\r
438     ID_input_time,\r
439     ID_input_state,\r
440     ID_input_rate,\r
441     ID_input_fps,\r
442     ID_input_hasvout,\r
443 };\r
444 \r
445 RuntimeNPObject::InvokeResult LibvlcInputNPObject::getProperty(int index, NPVariant &result)\r
446 {\r
447     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(_instance->pdata);\r
448     if( p_plugin )\r
449     {\r
450         libvlc_exception_t ex;\r
451         libvlc_exception_init(&ex);\r
452 \r
453         libvlc_input_t *p_input = libvlc_playlist_get_input(p_plugin->getVLC(), &ex);\r
454         if( libvlc_exception_raised(&ex) )\r
455         {\r
456             if( index != ID_input_state )\r
457             {\r
458                 NPN_SetException(this, libvlc_exception_get_message(&ex));\r
459                 libvlc_exception_clear(&ex);\r
460                 return INVOKERESULT_GENERIC_ERROR;\r
461             }\r
462             else\r
463             {\r
464                 /* for input state, return CLOSED rather than an exception */\r
465                 INT32_TO_NPVARIANT(0, result);\r
466                 return INVOKERESULT_NO_ERROR;\r
467             }\r
468         }\r
469 \r
470         switch( index )\r
471         {\r
472             case ID_input_length:\r
473             {\r
474                 double val = (double)libvlc_input_get_length(p_input, &ex);\r
475                 libvlc_input_free(p_input);\r
476                 if( libvlc_exception_raised(&ex) )\r
477                 {\r
478                     NPN_SetException(this, libvlc_exception_get_message(&ex));\r
479                     libvlc_exception_clear(&ex);\r
480                     return INVOKERESULT_GENERIC_ERROR;\r
481                 }\r
482                 DOUBLE_TO_NPVARIANT(val, result);\r
483                 return INVOKERESULT_NO_ERROR;\r
484             }\r
485             case ID_input_position:\r
486             {\r
487                 double val = libvlc_input_get_position(p_input, &ex);\r
488                 libvlc_input_free(p_input);\r
489                 if( libvlc_exception_raised(&ex) )\r
490                 {\r
491                     NPN_SetException(this, libvlc_exception_get_message(&ex));\r
492                     libvlc_exception_clear(&ex);\r
493                     return INVOKERESULT_GENERIC_ERROR;\r
494                 }\r
495                 DOUBLE_TO_NPVARIANT(val, result);\r
496                 return INVOKERESULT_NO_ERROR;\r
497             }\r
498             case ID_input_time:\r
499             {\r
500                 double val = (double)libvlc_input_get_time(p_input, &ex);\r
501                 libvlc_input_free(p_input);\r
502                 if( libvlc_exception_raised(&ex) )\r
503                 {\r
504                     NPN_SetException(this, libvlc_exception_get_message(&ex));\r
505                     libvlc_exception_clear(&ex);\r
506                     return INVOKERESULT_GENERIC_ERROR;\r
507                 }\r
508                 DOUBLE_TO_NPVARIANT(val, result);\r
509                 return INVOKERESULT_NO_ERROR;\r
510             }\r
511             case ID_input_state:\r
512             {\r
513                 int val = libvlc_input_get_state(p_input, &ex);\r
514                 libvlc_input_free(p_input);\r
515                 if( libvlc_exception_raised(&ex) )\r
516                 {\r
517                     NPN_SetException(this, libvlc_exception_get_message(&ex));\r
518                     libvlc_exception_clear(&ex);\r
519                     return INVOKERESULT_GENERIC_ERROR;\r
520                 }\r
521                 INT32_TO_NPVARIANT(val, result);\r
522                 return INVOKERESULT_NO_ERROR;\r
523             }\r
524             case ID_input_rate:\r
525             {\r
526                 float val = libvlc_input_get_rate(p_input, &ex);\r
527                 libvlc_input_free(p_input);\r
528                 if( libvlc_exception_raised(&ex) )\r
529                 {\r
530                     NPN_SetException(this, libvlc_exception_get_message(&ex));\r
531                     libvlc_exception_clear(&ex);\r
532                     return INVOKERESULT_GENERIC_ERROR;\r
533                 }\r
534                 DOUBLE_TO_NPVARIANT(val, result);\r
535                 return INVOKERESULT_NO_ERROR;\r
536             }\r
537             case ID_input_fps:\r
538             {\r
539                 double val = libvlc_input_get_fps(p_input, &ex);\r
540                 libvlc_input_free(p_input);\r
541                 if( libvlc_exception_raised(&ex) )\r
542                 {\r
543                     NPN_SetException(this, libvlc_exception_get_message(&ex));\r
544                     libvlc_exception_clear(&ex);\r
545                     return INVOKERESULT_GENERIC_ERROR;\r
546                 }\r
547                 DOUBLE_TO_NPVARIANT(val, result);\r
548                 return INVOKERESULT_NO_ERROR;\r
549             }\r
550             case ID_input_hasvout:\r
551             {\r
552                 vlc_bool_t val = libvlc_input_has_vout(p_input, &ex);\r
553                 libvlc_input_free(p_input);\r
554                 if( libvlc_exception_raised(&ex) )\r
555                 {\r
556                     NPN_SetException(this, libvlc_exception_get_message(&ex));\r
557                     libvlc_exception_clear(&ex);\r
558                     return INVOKERESULT_GENERIC_ERROR;\r
559                 }\r
560                 BOOLEAN_TO_NPVARIANT(val, result);\r
561                 return INVOKERESULT_NO_ERROR;\r
562             }\r
563             default:\r
564                 ;\r
565         }\r
566         libvlc_input_free(p_input);\r
567     }\r
568     return INVOKERESULT_GENERIC_ERROR;\r
569 }\r
570 \r
571 RuntimeNPObject::InvokeResult LibvlcInputNPObject::setProperty(int index, const NPVariant &value)\r
572 {\r
573     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(_instance->pdata);\r
574     if( p_plugin )\r
575     {\r
576         libvlc_exception_t ex;\r
577         libvlc_exception_init(&ex);\r
578 \r
579         libvlc_input_t *p_input = libvlc_playlist_get_input(p_plugin->getVLC(), &ex);\r
580         if( libvlc_exception_raised(&ex) )\r
581         {\r
582             NPN_SetException(this, libvlc_exception_get_message(&ex));\r
583             libvlc_exception_clear(&ex);\r
584             return INVOKERESULT_GENERIC_ERROR;\r
585         }\r
586 \r
587         switch( index )\r
588         {\r
589             case ID_input_position:\r
590             {\r
591                 if( ! NPVARIANT_IS_DOUBLE(value) )\r
592                 {\r
593                     libvlc_input_free(p_input);\r
594                     return INVOKERESULT_INVALID_VALUE;\r
595                 }\r
596 \r
597                 float val = (float)NPVARIANT_TO_DOUBLE(value);\r
598                 libvlc_input_set_position(p_input, val, &ex);\r
599                 libvlc_input_free(p_input);\r
600                 if( libvlc_exception_raised(&ex) )\r
601                 {\r
602                     NPN_SetException(this, libvlc_exception_get_message(&ex));\r
603                     libvlc_exception_clear(&ex);\r
604                     return INVOKERESULT_GENERIC_ERROR;\r
605                 }\r
606                 return INVOKERESULT_NO_ERROR;\r
607             }\r
608             case ID_input_time:\r
609             {\r
610                 vlc_int64_t val;\r
611                 if( NPVARIANT_IS_INT32(value) )\r
612                     val = (vlc_int64_t)NPVARIANT_TO_INT32(value);\r
613                 else if( NPVARIANT_IS_DOUBLE(value) )\r
614                     val = (vlc_int64_t)NPVARIANT_TO_DOUBLE(value);\r
615                 else\r
616                 {\r
617                     libvlc_input_free(p_input);\r
618                     return INVOKERESULT_INVALID_VALUE;\r
619                 }\r
620 \r
621                 libvlc_input_set_time(p_input, val, &ex);\r
622                 libvlc_input_free(p_input);\r
623                 if( libvlc_exception_raised(&ex) )\r
624                 {\r
625                     NPN_SetException(this, libvlc_exception_get_message(&ex));\r
626                     libvlc_exception_clear(&ex);\r
627                     return INVOKERESULT_GENERIC_ERROR;\r
628                 }\r
629                 return INVOKERESULT_NO_ERROR;\r
630             }\r
631             case ID_input_rate:\r
632             {\r
633                 float val;\r
634                 if( NPVARIANT_IS_INT32(value) )\r
635                     val = (float)NPVARIANT_TO_INT32(value);\r
636                 else if( NPVARIANT_IS_DOUBLE(value) )\r
637                     val = (float)NPVARIANT_TO_DOUBLE(value);\r
638                 else\r
639                 {\r
640                     libvlc_input_free(p_input);\r
641                     return INVOKERESULT_INVALID_VALUE;\r
642                 }\r
643 \r
644                 libvlc_input_set_rate(p_input, val, &ex);\r
645                 libvlc_input_free(p_input);\r
646                 if( libvlc_exception_raised(&ex) )\r
647                 {\r
648                     NPN_SetException(this, libvlc_exception_get_message(&ex));\r
649                     libvlc_exception_clear(&ex);\r
650                     return INVOKERESULT_GENERIC_ERROR;\r
651                 }\r
652                 return INVOKERESULT_NO_ERROR;\r
653             }\r
654             default:\r
655                 ;\r
656         }\r
657         libvlc_input_free(p_input);\r
658     }\r
659     return INVOKERESULT_GENERIC_ERROR;\r
660 }\r
661 \r
662 const NPUTF8 * const LibvlcInputNPObject::methodNames[] =\r
663 {\r
664     /* no methods */\r
665 };\r
666 \r
667 const int LibvlcInputNPObject::methodCount = sizeof(LibvlcInputNPObject::methodNames)/sizeof(NPUTF8 *);\r
668 \r
669 /*\r
670 ** implementation of libvlc message object\r
671 */\r
672 \r
673 const NPUTF8 * const LibvlcMessageNPObject::propertyNames[] = \r
674 {\r
675     "severity",\r
676     "type",\r
677     "name",\r
678     "header",\r
679     "message",\r
680 };\r
681 \r
682 const int LibvlcMessageNPObject::propertyCount = sizeof(LibvlcMessageNPObject::propertyNames)/sizeof(NPUTF8 *);\r
683 \r
684 enum LibvlcMessageNPObjectPropertyIds\r
685 {\r
686     ID_message_severity,\r
687     ID_message_type,\r
688     ID_message_name,\r
689     ID_message_header,\r
690     ID_message_message,\r
691 };\r
692 \r
693 RuntimeNPObject::InvokeResult LibvlcMessageNPObject::getProperty(int index, NPVariant &result)\r
694 {\r
695     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(_instance->pdata);\r
696     if( p_plugin )\r
697     {\r
698         switch( index )\r
699         {\r
700             case ID_message_severity:\r
701             {\r
702                 INT32_TO_NPVARIANT(_msg.i_severity, result);\r
703                 return INVOKERESULT_NO_ERROR;\r
704             }\r
705             case ID_message_type:\r
706             {\r
707                 if( _msg.psz_type )\r
708                 {\r
709                     int len = strlen(_msg.psz_type);\r
710                     NPUTF8* retval = (NPUTF8*)NPN_MemAlloc(len);\r
711                     if( retval )\r
712                     {\r
713                         memcpy(retval, _msg.psz_type, len);\r
714                         STRINGN_TO_NPVARIANT(retval, len, result);\r
715                     }\r
716                 }\r
717                 else\r
718                 {\r
719                     NULL_TO_NPVARIANT(result);\r
720                 }\r
721                 return INVOKERESULT_NO_ERROR;\r
722             }\r
723             case ID_message_name:\r
724             {\r
725                 if( _msg.psz_name )\r
726                 {\r
727                     int len = strlen(_msg.psz_name);\r
728                     NPUTF8* retval = (NPUTF8*)NPN_MemAlloc(len);\r
729                     if( retval )\r
730                     {\r
731                         memcpy(retval, _msg.psz_name, len);\r
732                         STRINGN_TO_NPVARIANT(retval, len, result);\r
733                     }\r
734                 }\r
735                 else\r
736                 {\r
737                     NULL_TO_NPVARIANT(result);\r
738                 }\r
739                 return INVOKERESULT_NO_ERROR;\r
740             }\r
741             case ID_message_header:\r
742             {\r
743                 if( _msg.psz_header )\r
744                 {\r
745                     int len = strlen(_msg.psz_header);\r
746                     NPUTF8* retval = (NPUTF8*)NPN_MemAlloc(len);\r
747                     if( retval )\r
748                     {\r
749                         memcpy(retval, _msg.psz_header, len);\r
750                         STRINGN_TO_NPVARIANT(retval, len, result);\r
751                     }\r
752                 }\r
753                 else\r
754                 {\r
755                     NULL_TO_NPVARIANT(result);\r
756                 }\r
757                 return INVOKERESULT_NO_ERROR;\r
758             }\r
759             case ID_message_message:\r
760             {\r
761                 if( _msg.psz_message )\r
762                 {\r
763                     int len = strlen(_msg.psz_message);\r
764                     NPUTF8* retval = (NPUTF8*)NPN_MemAlloc(len);\r
765                     if( retval )\r
766                     {\r
767                         memcpy(retval, _msg.psz_message, len);\r
768                         STRINGN_TO_NPVARIANT(retval, len, result);\r
769                     }\r
770                 }\r
771                 else\r
772                 {\r
773                     NULL_TO_NPVARIANT(result);\r
774                 }\r
775                 return INVOKERESULT_NO_ERROR;\r
776             }\r
777             default:\r
778                 ;\r
779         }\r
780     }\r
781     return INVOKERESULT_GENERIC_ERROR;\r
782 }\r
783 \r
784 const NPUTF8 * const LibvlcMessageNPObject::methodNames[] =\r
785 {\r
786     /* no methods */\r
787 };\r
788 \r
789 const int LibvlcMessageNPObject::methodCount = sizeof(LibvlcMessageNPObject::methodNames)/sizeof(NPUTF8 *);\r
790 \r
791 /*\r
792 ** implementation of libvlc message iterator object\r
793 */\r
794 \r
795 LibvlcMessageIteratorNPObject::LibvlcMessageIteratorNPObject(NPP instance, const NPClass *aClass) :\r
796     RuntimeNPObject(instance, aClass),\r
797     _p_iter(NULL)\r
798 {\r
799     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(_instance->pdata);\r
800     if( p_plugin )\r
801     {\r
802         libvlc_log_t *p_log = p_plugin->getLog();\r
803         if( p_log )\r
804         {\r
805             _p_iter = libvlc_log_get_iterator(p_log, NULL);\r
806         }\r
807     }\r
808 };\r
809 \r
810 LibvlcMessageIteratorNPObject::~LibvlcMessageIteratorNPObject()\r
811 {\r
812     if( _p_iter )\r
813         libvlc_log_iterator_free(_p_iter, NULL);\r
814 }\r
815 \r
816 const NPUTF8 * const LibvlcMessageIteratorNPObject::propertyNames[] = \r
817 {\r
818     "hasNext",\r
819 };\r
820 \r
821 const int LibvlcMessageIteratorNPObject::propertyCount = sizeof(LibvlcMessageIteratorNPObject::propertyNames)/sizeof(NPUTF8 *);\r
822 \r
823 enum LibvlcMessageIteratorNPObjectPropertyIds\r
824 {\r
825     ID_messageiterator_hasNext,\r
826 };\r
827 \r
828 RuntimeNPObject::InvokeResult LibvlcMessageIteratorNPObject::getProperty(int index, NPVariant &result)\r
829 {\r
830     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(_instance->pdata);\r
831     if( p_plugin )\r
832     {\r
833         switch( index )\r
834         {\r
835             case ID_messageiterator_hasNext:\r
836             {\r
837                 if( _p_iter && p_plugin->getLog() )\r
838                 {\r
839                     libvlc_exception_t ex;\r
840                     libvlc_exception_init(&ex);\r
841 \r
842                     BOOLEAN_TO_NPVARIANT(libvlc_log_iterator_has_next(_p_iter, &ex), result);\r
843                     if( libvlc_exception_raised(&ex) )\r
844                     {\r
845                         NPN_SetException(this, libvlc_exception_get_message(&ex));\r
846                         libvlc_exception_clear(&ex);\r
847                         return INVOKERESULT_GENERIC_ERROR;\r
848                     }\r
849                 }\r
850                 else\r
851                 {\r
852                     BOOLEAN_TO_NPVARIANT(0, result);\r
853                 }\r
854                 return INVOKERESULT_NO_ERROR;\r
855             }\r
856             default:\r
857                 ;\r
858         }\r
859     }\r
860     return INVOKERESULT_GENERIC_ERROR;\r
861 }\r
862 \r
863 const NPUTF8 * const LibvlcMessageIteratorNPObject::methodNames[] =\r
864 {\r
865     "next",\r
866 };\r
867 \r
868 const int LibvlcMessageIteratorNPObject::methodCount = sizeof(LibvlcMessageIteratorNPObject::methodNames)/sizeof(NPUTF8 *);\r
869 \r
870 enum LibvlcMessageIteratorNPObjectMethodIds\r
871 {\r
872     ID_messageiterator_next,\r
873 };\r
874 \r
875 RuntimeNPObject::InvokeResult LibvlcMessageIteratorNPObject::invoke(int index, const NPVariant *args, uint32_t argCount, NPVariant &result)\r
876 {\r
877     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(_instance->pdata);\r
878     if( p_plugin )\r
879     {\r
880         libvlc_exception_t ex;\r
881         libvlc_exception_init(&ex);\r
882 \r
883         switch( index )\r
884         {\r
885             case ID_messageiterator_next:\r
886                 if( argCount == 0 )\r
887                 {\r
888                     if( _p_iter && p_plugin->getLog() )\r
889                     {\r
890                         struct libvlc_log_message_t buffer;\r
891 \r
892                         buffer.sizeof_msg = sizeof(buffer);\r
893 \r
894                         libvlc_log_iterator_next(_p_iter, &buffer, &ex);\r
895                         if( libvlc_exception_raised(&ex) )\r
896                         {\r
897                             NPN_SetException(this, libvlc_exception_get_message(&ex));\r
898                             libvlc_exception_clear(&ex);\r
899                             return INVOKERESULT_GENERIC_ERROR;\r
900                         }\r
901                         else\r
902                         {\r
903                             LibvlcMessageNPObject* message =\r
904                                 static_cast<LibvlcMessageNPObject*>(NPN_CreateObject(_instance, RuntimeNPClass<LibvlcMessageNPObject>::getClass()));\r
905                             if( message )\r
906                             {\r
907                                 message->setMessage(buffer);\r
908                                 OBJECT_TO_NPVARIANT(message, result);\r
909                                 return INVOKERESULT_NO_ERROR;\r
910                             }\r
911                             return INVOKERESULT_OUT_OF_MEMORY;\r
912                         }\r
913                     }\r
914                     return INVOKERESULT_GENERIC_ERROR;\r
915                 }\r
916                 return INVOKERESULT_NO_SUCH_METHOD;\r
917             default:\r
918                 ;\r
919         }\r
920     }\r
921     return INVOKERESULT_GENERIC_ERROR;\r
922 }\r
923  \r
924 /*\r
925 ** implementation of libvlc message object\r
926 */\r
927 \r
928 const NPUTF8 * const LibvlcMessagesNPObject::propertyNames[] = \r
929 {\r
930     "count",\r
931 };\r
932 \r
933 const int LibvlcMessagesNPObject::propertyCount = sizeof(LibvlcMessagesNPObject::propertyNames)/sizeof(NPUTF8 *);\r
934 \r
935 enum LibvlcMessagesNPObjectPropertyIds\r
936 {\r
937     ID_messages_count,\r
938 };\r
939 \r
940 RuntimeNPObject::InvokeResult LibvlcMessagesNPObject::getProperty(int index, NPVariant &result)\r
941 {\r
942     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(_instance->pdata);\r
943     if( p_plugin )\r
944     {\r
945         switch( index )\r
946         {\r
947             case ID_messages_count:\r
948             {\r
949                 libvlc_log_t *p_log = p_plugin->getLog();\r
950                 if( p_log )\r
951                 {\r
952                     libvlc_exception_t ex;\r
953                     libvlc_exception_init(&ex);\r
954 \r
955                     INT32_TO_NPVARIANT(libvlc_log_count(p_log, &ex), result);\r
956                     if( libvlc_exception_raised(&ex) )\r
957                     {\r
958                         NPN_SetException(this, libvlc_exception_get_message(&ex));\r
959                         libvlc_exception_clear(&ex);\r
960                         return INVOKERESULT_GENERIC_ERROR;\r
961                     }\r
962                 }\r
963                 else\r
964                 {\r
965                     INT32_TO_NPVARIANT(0, result);\r
966                 }\r
967                 return INVOKERESULT_NO_ERROR;\r
968             }\r
969             default:\r
970                 ;\r
971         }\r
972     }\r
973     return INVOKERESULT_GENERIC_ERROR;\r
974 }\r
975 \r
976 const NPUTF8 * const LibvlcMessagesNPObject::methodNames[] =\r
977 {\r
978     "clear",\r
979     "iterator",\r
980 };\r
981 \r
982 const int LibvlcMessagesNPObject::methodCount = sizeof(LibvlcMessagesNPObject::methodNames)/sizeof(NPUTF8 *);\r
983 \r
984 enum LibvlcMessagesNPObjectMethodIds\r
985 {\r
986     ID_messages_clear,\r
987     ID_messages_iterator,\r
988 };\r
989 \r
990 RuntimeNPObject::InvokeResult LibvlcMessagesNPObject::invoke(int index, const NPVariant *args, uint32_t argCount, NPVariant &result)\r
991 {\r
992     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(_instance->pdata);\r
993     if( p_plugin )\r
994     {\r
995         libvlc_exception_t ex;\r
996         libvlc_exception_init(&ex);\r
997 \r
998         switch( index )\r
999         {\r
1000             case ID_messages_clear:\r
1001                 if( argCount == 0 )\r
1002                 {\r
1003                     libvlc_log_t *p_log = p_plugin->getLog();\r
1004                     if( p_log )\r
1005                     {\r
1006                         libvlc_log_clear(p_log, &ex);\r
1007                         if( libvlc_exception_raised(&ex) )\r
1008                         {\r
1009                             NPN_SetException(this, libvlc_exception_get_message(&ex));\r
1010                             libvlc_exception_clear(&ex);\r
1011                             return INVOKERESULT_GENERIC_ERROR;\r
1012                         }\r
1013                     }\r
1014                     return INVOKERESULT_NO_ERROR;\r
1015                 }\r
1016                 return INVOKERESULT_NO_SUCH_METHOD;\r
1017 \r
1018             case ID_messages_iterator:\r
1019                 if( argCount == 0 )\r
1020                 {\r
1021                     LibvlcMessageIteratorNPObject* iter =\r
1022                         static_cast<LibvlcMessageIteratorNPObject*>(NPN_CreateObject(_instance, RuntimeNPClass<LibvlcMessageIteratorNPObject>::getClass()));\r
1023                     if( iter )\r
1024                     {\r
1025                         OBJECT_TO_NPVARIANT(iter, result);\r
1026                         return INVOKERESULT_NO_ERROR;\r
1027                     }\r
1028                     return INVOKERESULT_OUT_OF_MEMORY;\r
1029                 }\r
1030                 return INVOKERESULT_NO_SUCH_METHOD;\r
1031 \r
1032             default:\r
1033                 ;\r
1034         }\r
1035     }\r
1036     return INVOKERESULT_GENERIC_ERROR;\r
1037 }\r
1038 \r
1039  \r
1040 /*\r
1041 ** implementation of libvlc message object\r
1042 */\r
1043 \r
1044 \r
1045 LibvlcLogNPObject::LibvlcLogNPObject(NPP instance, const NPClass *aClass) :\r
1046     RuntimeNPObject(instance, aClass)\r
1047 {\r
1048     _p_vlcmessages = static_cast<LibvlcMessagesNPObject*>(NPN_CreateObject(instance, RuntimeNPClass<LibvlcMessagesNPObject>::getClass()));\r
1049 };\r
1050     \r
1051 LibvlcLogNPObject::~LibvlcLogNPObject()\r
1052 {\r
1053     NPN_ReleaseObject(_p_vlcmessages);\r
1054 };\r
1055 \r
1056 const NPUTF8 * const LibvlcLogNPObject::propertyNames[] = \r
1057 {\r
1058     "messages",\r
1059     "verbosity",\r
1060 };\r
1061 \r
1062 const int LibvlcLogNPObject::propertyCount = sizeof(LibvlcLogNPObject::propertyNames)/sizeof(NPUTF8 *);\r
1063 \r
1064 enum LibvlcLogNPObjectPropertyIds\r
1065 {\r
1066     ID_log_messages,\r
1067     ID_log_verbosity,\r
1068 };\r
1069 \r
1070 RuntimeNPObject::InvokeResult LibvlcLogNPObject::getProperty(int index, NPVariant &result)\r
1071 {\r
1072     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(_instance->pdata);\r
1073     if( p_plugin )\r
1074     {\r
1075         libvlc_exception_t ex;\r
1076         libvlc_exception_init(&ex);\r
1077 \r
1078         switch( index )\r
1079         {\r
1080             case ID_log_messages:\r
1081             {\r
1082                 OBJECT_TO_NPVARIANT(NPN_RetainObject(_p_vlcmessages), result);\r
1083                 return INVOKERESULT_NO_ERROR;\r
1084             }\r
1085             case ID_log_verbosity:\r
1086             {\r
1087                 if( p_plugin->getLog() )\r
1088                 {\r
1089                     INT32_TO_NPVARIANT(libvlc_get_log_verbosity(p_plugin->getVLC(),\r
1090                                                                     &ex), result);\r
1091                     if( libvlc_exception_raised(&ex) )\r
1092                     {\r
1093                         NPN_SetException(this, libvlc_exception_get_message(&ex));\r
1094                         libvlc_exception_clear(&ex);\r
1095                         return INVOKERESULT_GENERIC_ERROR;\r
1096                     }\r
1097                 }\r
1098                 else\r
1099                 {\r
1100                     /* log is not enabled, return -1 */\r
1101                     DOUBLE_TO_NPVARIANT(-1.0, result);\r
1102                 }\r
1103                 return INVOKERESULT_NO_ERROR;\r
1104             }\r
1105             default:\r
1106                 ;\r
1107         }\r
1108     }\r
1109     return INVOKERESULT_GENERIC_ERROR;\r
1110 }\r
1111 \r
1112 RuntimeNPObject::InvokeResult LibvlcLogNPObject::setProperty(int index, const NPVariant &value)\r
1113 {\r
1114     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(_instance->pdata);\r
1115     if( p_plugin )\r
1116     {\r
1117         libvlc_exception_t ex;\r
1118         libvlc_exception_init(&ex);\r
1119 \r
1120         switch( index )\r
1121         {\r
1122             case ID_log_verbosity:\r
1123                 if( isNumberValue(value) )\r
1124                 {\r
1125                     libvlc_instance_t* p_libvlc = p_plugin->getVLC();\r
1126                     libvlc_log_t *p_log = p_plugin->getLog();\r
1127                     int verbosity = numberValue(value);\r
1128                     if( verbosity >= 0 )\r
1129                     {\r
1130                         if( ! p_log )\r
1131                         {\r
1132                             p_log = libvlc_log_open(p_libvlc, &ex);\r
1133                             if( libvlc_exception_raised(&ex) )\r
1134                             {\r
1135                                 NPN_SetException(this, libvlc_exception_get_message(&ex));\r
1136                                 libvlc_exception_clear(&ex);\r
1137                                 return INVOKERESULT_GENERIC_ERROR;\r
1138                             }\r
1139                             p_plugin->setLog(p_log);\r
1140                         }\r
1141                         libvlc_set_log_verbosity(p_libvlc, (unsigned)verbosity, &ex);\r
1142                         if( libvlc_exception_raised(&ex) )\r
1143                         {\r
1144                             NPN_SetException(this, libvlc_exception_get_message(&ex));\r
1145                             libvlc_exception_clear(&ex);\r
1146                             return INVOKERESULT_GENERIC_ERROR;\r
1147                         }\r
1148                     }\r
1149                     else if( p_log )\r
1150                     {\r
1151                         /* close log  when verbosity is set to -1 */\r
1152                         p_plugin->setLog(NULL);\r
1153                         libvlc_log_close(p_log, &ex);\r
1154                         if( libvlc_exception_raised(&ex) )\r
1155                         {\r
1156                             NPN_SetException(this, libvlc_exception_get_message(&ex));\r
1157                             libvlc_exception_clear(&ex);\r
1158                             return INVOKERESULT_GENERIC_ERROR;\r
1159                         }\r
1160                     }\r
1161                     return INVOKERESULT_NO_ERROR;\r
1162                 }\r
1163                 return INVOKERESULT_INVALID_VALUE;\r
1164             default:\r
1165                 ;\r
1166         }\r
1167     }\r
1168     return INVOKERESULT_GENERIC_ERROR;\r
1169 }\r
1170 \r
1171 const NPUTF8 * const LibvlcLogNPObject::methodNames[] =\r
1172 {\r
1173     /* no methods */\r
1174 };\r
1175 \r
1176 const int LibvlcLogNPObject::methodCount = sizeof(LibvlcLogNPObject::methodNames)/sizeof(NPUTF8 *);\r
1177 \r
1178 /*\r
1179 ** implementation of libvlc playlist items object\r
1180 */\r
1181 \r
1182 const NPUTF8 * const LibvlcPlaylistItemsNPObject::propertyNames[] = \r
1183 {\r
1184     "count",\r
1185 };\r
1186 \r
1187 const int LibvlcPlaylistItemsNPObject::propertyCount = sizeof(LibvlcPlaylistItemsNPObject::propertyNames)/sizeof(NPUTF8 *);\r
1188 \r
1189 enum LibvlcPlaylistItemsNPObjectPropertyIds\r
1190 {\r
1191     ID_playlistitems_count,\r
1192 };\r
1193 \r
1194 RuntimeNPObject::InvokeResult LibvlcPlaylistItemsNPObject::getProperty(int index, NPVariant &result)\r
1195 {\r
1196     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(_instance->pdata);\r
1197     if( p_plugin )\r
1198     {\r
1199         libvlc_exception_t ex;\r
1200         libvlc_exception_init(&ex);\r
1201 \r
1202         switch( index )\r
1203         {\r
1204             case ID_playlistitems_count:\r
1205             {\r
1206                 int val = libvlc_playlist_items_count(p_plugin->getVLC(), &ex);\r
1207                 if( libvlc_exception_raised(&ex) )\r
1208                 {\r
1209                     NPN_SetException(this, libvlc_exception_get_message(&ex));\r
1210                     libvlc_exception_clear(&ex);\r
1211                     return INVOKERESULT_GENERIC_ERROR;\r
1212                 }\r
1213                 INT32_TO_NPVARIANT(val, result);\r
1214                 return INVOKERESULT_NO_ERROR;\r
1215             }\r
1216             default:\r
1217                 ;\r
1218         }\r
1219     }\r
1220     return INVOKERESULT_GENERIC_ERROR;\r
1221 }\r
1222 \r
1223 const NPUTF8 * const LibvlcPlaylistItemsNPObject::methodNames[] =\r
1224 {\r
1225     "clear",\r
1226     "remove",\r
1227 };\r
1228 \r
1229 const int LibvlcPlaylistItemsNPObject::methodCount = sizeof(LibvlcPlaylistItemsNPObject::methodNames)/sizeof(NPUTF8 *);\r
1230 \r
1231 enum LibvlcPlaylistItemsNPObjectMethodIds\r
1232 {\r
1233     ID_playlistitems_clear,\r
1234     ID_playlistitems_remove,\r
1235 };\r
1236 \r
1237 RuntimeNPObject::InvokeResult LibvlcPlaylistItemsNPObject::invoke(int index, const NPVariant *args, uint32_t argCount, NPVariant &result)\r
1238 {\r
1239     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(_instance->pdata);\r
1240     if( p_plugin )\r
1241     {\r
1242         libvlc_exception_t ex;\r
1243         libvlc_exception_init(&ex);\r
1244 \r
1245         switch( index )\r
1246         {\r
1247             case ID_playlistitems_clear:\r
1248                 if( argCount == 0 )\r
1249                 {\r
1250                     libvlc_playlist_clear(p_plugin->getVLC(), &ex);\r
1251                     if( libvlc_exception_raised(&ex) )\r
1252                     {\r
1253                         NPN_SetException(this, libvlc_exception_get_message(&ex));\r
1254                         libvlc_exception_clear(&ex);\r
1255                         return INVOKERESULT_GENERIC_ERROR;\r
1256                     }\r
1257                     else\r
1258                     {\r
1259                         VOID_TO_NPVARIANT(result);\r
1260                         return INVOKERESULT_NO_ERROR;\r
1261                     }\r
1262                 }\r
1263                 return INVOKERESULT_NO_SUCH_METHOD;\r
1264             case ID_playlistitems_remove:\r
1265                 if( (argCount == 1) && isNumberValue(args[0]) )\r
1266                 {\r
1267                     libvlc_playlist_delete_item(p_plugin->getVLC(), numberValue(args[0]), &ex);\r
1268                     if( libvlc_exception_raised(&ex) )\r
1269                     {\r
1270                         NPN_SetException(this, libvlc_exception_get_message(&ex));\r
1271                         libvlc_exception_clear(&ex);\r
1272                         return INVOKERESULT_GENERIC_ERROR;\r
1273                     }\r
1274                     else\r
1275                     {\r
1276                         VOID_TO_NPVARIANT(result);\r
1277                         return INVOKERESULT_NO_ERROR;\r
1278                     }\r
1279                 }\r
1280                 return INVOKERESULT_NO_SUCH_METHOD;\r
1281             default:\r
1282                 ;\r
1283         }\r
1284     }\r
1285     return INVOKERESULT_GENERIC_ERROR;\r
1286 }\r
1287 \r
1288 /*\r
1289 ** implementation of libvlc playlist object\r
1290 */\r
1291 \r
1292 \r
1293 LibvlcPlaylistNPObject::LibvlcPlaylistNPObject(NPP instance, const NPClass *aClass) :\r
1294     RuntimeNPObject(instance, aClass)\r
1295 {\r
1296     _p_vlcplaylistitems = static_cast<LibvlcPlaylistItemsNPObject*>(NPN_CreateObject(instance, RuntimeNPClass<LibvlcPlaylistItemsNPObject>::getClass()));\r
1297 };\r
1298     \r
1299 LibvlcPlaylistNPObject::~LibvlcPlaylistNPObject()\r
1300 {\r
1301     NPN_ReleaseObject(_p_vlcplaylistitems);\r
1302 };\r
1303 \r
1304 const NPUTF8 * const LibvlcPlaylistNPObject::propertyNames[] = \r
1305 {\r
1306     "itemCount", /* deprecated */\r
1307     "isPlaying",\r
1308     "items",\r
1309 };\r
1310 \r
1311 const int LibvlcPlaylistNPObject::propertyCount = sizeof(LibvlcPlaylistNPObject::propertyNames)/sizeof(NPUTF8 *);\r
1312 \r
1313 enum LibvlcPlaylistNPObjectPropertyIds\r
1314 {\r
1315     ID_playlist_itemcount,\r
1316     ID_playlist_isplaying,\r
1317     ID_playlist_items,\r
1318 };\r
1319 \r
1320 RuntimeNPObject::InvokeResult LibvlcPlaylistNPObject::getProperty(int index, NPVariant &result)\r
1321 {\r
1322     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(_instance->pdata);\r
1323     if( p_plugin )\r
1324     {\r
1325         libvlc_exception_t ex;\r
1326         libvlc_exception_init(&ex);\r
1327 \r
1328         switch( index )\r
1329         {\r
1330             case ID_playlist_itemcount: /* deprecated */\r
1331             {\r
1332                 int val = libvlc_playlist_items_count(p_plugin->getVLC(), &ex);\r
1333                 if( libvlc_exception_raised(&ex) )\r
1334                 {\r
1335                     NPN_SetException(this, libvlc_exception_get_message(&ex));\r
1336                     libvlc_exception_clear(&ex);\r
1337                     return INVOKERESULT_GENERIC_ERROR;\r
1338                 }\r
1339                 INT32_TO_NPVARIANT(val, result);\r
1340                 return INVOKERESULT_NO_ERROR;\r
1341             }\r
1342             case ID_playlist_isplaying:\r
1343             {\r
1344                 int val = libvlc_playlist_isplaying(p_plugin->getVLC(), &ex);\r
1345                 if( libvlc_exception_raised(&ex) )\r
1346                 {\r
1347                     NPN_SetException(this, libvlc_exception_get_message(&ex));\r
1348                     libvlc_exception_clear(&ex);\r
1349                     return INVOKERESULT_GENERIC_ERROR;\r
1350                 }\r
1351                 BOOLEAN_TO_NPVARIANT(val, result);\r
1352                 return INVOKERESULT_NO_ERROR;\r
1353             }\r
1354             case ID_playlist_items:\r
1355             {\r
1356                 OBJECT_TO_NPVARIANT(NPN_RetainObject(_p_vlcplaylistitems), result);\r
1357                 return INVOKERESULT_NO_ERROR;\r
1358             }\r
1359             default:\r
1360                 ;\r
1361         }\r
1362     }\r
1363     return INVOKERESULT_GENERIC_ERROR;\r
1364 }\r
1365 \r
1366 const NPUTF8 * const LibvlcPlaylistNPObject::methodNames[] =\r
1367 {\r
1368     "add",\r
1369     "play",\r
1370     "playItem",\r
1371     "togglePause",\r
1372     "stop",\r
1373     "next",\r
1374     "prev",\r
1375     "clear", /* deprecated */\r
1376     "removeItem", /* deprecated */\r
1377 };\r
1378 \r
1379 const int LibvlcPlaylistNPObject::methodCount = sizeof(LibvlcPlaylistNPObject::methodNames)/sizeof(NPUTF8 *);\r
1380 \r
1381 enum LibvlcPlaylistNPObjectMethodIds\r
1382 {\r
1383     ID_playlist_add,\r
1384     ID_playlist_play,\r
1385     ID_playlist_playItem,\r
1386     ID_playlist_togglepause,\r
1387     ID_playlist_stop,\r
1388     ID_playlist_next,\r
1389     ID_playlist_prev,\r
1390     ID_playlist_clear,\r
1391     ID_playlist_removeitem\r
1392 };\r
1393 \r
1394 RuntimeNPObject::InvokeResult LibvlcPlaylistNPObject::invoke(int index, const NPVariant *args, uint32_t argCount, NPVariant &result)\r
1395 {\r
1396     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(_instance->pdata);\r
1397     if( p_plugin )\r
1398     {\r
1399         libvlc_exception_t ex;\r
1400         libvlc_exception_init(&ex);\r
1401 \r
1402         switch( index )\r
1403         {\r
1404             case ID_playlist_add:\r
1405             {\r
1406                 if( (argCount < 1) || (argCount > 3) )\r
1407                     return INVOKERESULT_NO_SUCH_METHOD;\r
1408 \r
1409                 char *url = NULL;\r
1410 \r
1411                 // grab URL\r
1412                 if( NPVARIANT_IS_STRING(args[0]) )\r
1413                 {\r
1414                     char *s = stringValue(NPVARIANT_TO_STRING(args[0]));\r
1415                     if( s )\r
1416                     {\r
1417                         url = p_plugin->getAbsoluteURL(s);\r
1418                         if( url )\r
1419                             delete s;\r
1420                         else\r
1421                             // problem with combining url, use argument\r
1422                             url = s;\r
1423                     }\r
1424                     else\r
1425                         return INVOKERESULT_OUT_OF_MEMORY;\r
1426                 }\r
1427                 else\r
1428                     return INVOKERESULT_NO_SUCH_METHOD;\r
1429 \r
1430                 char *name = NULL;\r
1431 \r
1432                 // grab name if available\r
1433                 if( argCount > 1 )\r
1434                 {\r
1435                     if( NPVARIANT_IS_NULL(args[1]) )\r
1436                     {\r
1437                         // do nothing\r
1438                     }\r
1439                     else if( NPVARIANT_IS_STRING(args[1]) )\r
1440                     {\r
1441                         name = stringValue(NPVARIANT_TO_STRING(args[0]));\r
1442                     }\r
1443                     else\r
1444                         return INVOKERESULT_NO_SUCH_METHOD;\r
1445                 }\r
1446 \r
1447                 int i_options = 0;\r
1448                 char** ppsz_options = NULL;\r
1449 \r
1450                 // grab options if available\r
1451                 if( argCount > 2 )\r
1452                 {\r
1453                     if( NPVARIANT_IS_NULL(args[2]) )\r
1454                     {\r
1455                         // do nothing\r
1456                     }\r
1457                     else if( NPVARIANT_IS_STRING(args[2]) )\r
1458                     {\r
1459                         parseOptions(NPVARIANT_TO_STRING(args[2]), &i_options, &ppsz_options);\r
1460 \r
1461                     }\r
1462                     else if( NPVARIANT_IS_OBJECT(args[2]) )\r
1463                     {\r
1464                         parseOptions(NPVARIANT_TO_OBJECT(args[2]), &i_options, &ppsz_options);\r
1465                     }\r
1466                 }\r
1467 \r
1468                 int item = libvlc_playlist_add_extended(p_plugin->getVLC(),\r
1469                                                         url,\r
1470                                                         name,\r
1471                                                         i_options,\r
1472                                                         const_cast<const char **>(ppsz_options),\r
1473                                                         &ex);\r
1474                 delete url;\r
1475                 delete name;\r
1476                 for( int i=0; i< i_options; ++i )\r
1477                 {\r
1478                     if( ppsz_options[i] )\r
1479                         free(ppsz_options[i]);\r
1480                 }\r
1481                 if( ppsz_options )\r
1482                     free(ppsz_options);\r
1483                 if( libvlc_exception_raised(&ex) )\r
1484                 {\r
1485                     NPN_SetException(this, libvlc_exception_get_message(&ex));\r
1486                     libvlc_exception_clear(&ex);\r
1487                     return INVOKERESULT_GENERIC_ERROR;\r
1488                 }\r
1489                 else\r
1490                 {\r
1491                     INT32_TO_NPVARIANT(item, result);\r
1492                     return INVOKERESULT_NO_ERROR;\r
1493                 }\r
1494             }\r
1495             case ID_playlist_play:\r
1496                 if( argCount == 0 )\r
1497                 {\r
1498                     libvlc_playlist_play(p_plugin->getVLC(), -1, 0, NULL, &ex);\r
1499                     if( libvlc_exception_raised(&ex) )\r
1500                     {\r
1501                         NPN_SetException(this, libvlc_exception_get_message(&ex));\r
1502                         libvlc_exception_clear(&ex);\r
1503                         return INVOKERESULT_GENERIC_ERROR;\r
1504                     }\r
1505                     else\r
1506                     {\r
1507                         VOID_TO_NPVARIANT(result);\r
1508                         return INVOKERESULT_NO_ERROR;\r
1509                     }\r
1510                 }\r
1511                 return INVOKERESULT_NO_SUCH_METHOD;\r
1512             case ID_playlist_playItem:\r
1513                 if( (argCount == 1) && isNumberValue(args[0]) )\r
1514                 {\r
1515                     libvlc_playlist_play(p_plugin->getVLC(), numberValue(args[0]), 0, NULL, &ex);\r
1516                     if( libvlc_exception_raised(&ex) )\r
1517                     {\r
1518                         NPN_SetException(this, libvlc_exception_get_message(&ex));\r
1519                         libvlc_exception_clear(&ex);\r
1520                         return INVOKERESULT_GENERIC_ERROR;\r
1521                     }\r
1522                     else\r
1523                     {\r
1524                         VOID_TO_NPVARIANT(result);\r
1525                         return INVOKERESULT_NO_ERROR;\r
1526                     }\r
1527                 }\r
1528                 return INVOKERESULT_NO_SUCH_METHOD;\r
1529             case ID_playlist_togglepause:\r
1530                 if( argCount == 0 )\r
1531                 {\r
1532                     libvlc_playlist_pause(p_plugin->getVLC(), &ex);\r
1533                     if( libvlc_exception_raised(&ex) )\r
1534                     {\r
1535                         NPN_SetException(this, libvlc_exception_get_message(&ex));\r
1536                         libvlc_exception_clear(&ex);\r
1537                         return INVOKERESULT_GENERIC_ERROR;\r
1538                     }\r
1539                     else\r
1540                     {\r
1541                         VOID_TO_NPVARIANT(result);\r
1542                         return INVOKERESULT_NO_ERROR;\r
1543                     }\r
1544                 }\r
1545                 return INVOKERESULT_NO_SUCH_METHOD;\r
1546             case ID_playlist_stop:\r
1547                 if( argCount == 0 )\r
1548                 {\r
1549                     libvlc_playlist_stop(p_plugin->getVLC(), &ex);\r
1550                     if( libvlc_exception_raised(&ex) )\r
1551                     {\r
1552                         NPN_SetException(this, libvlc_exception_get_message(&ex));\r
1553                         libvlc_exception_clear(&ex);\r
1554                         return INVOKERESULT_GENERIC_ERROR;\r
1555                     }\r
1556                     else\r
1557                     {\r
1558                         VOID_TO_NPVARIANT(result);\r
1559                         return INVOKERESULT_NO_ERROR;\r
1560                     }\r
1561                 }\r
1562                 return INVOKERESULT_NO_SUCH_METHOD;\r
1563             case ID_playlist_next:\r
1564                 if( argCount == 0 )\r
1565                 {\r
1566                     libvlc_playlist_next(p_plugin->getVLC(), &ex);\r
1567                     if( libvlc_exception_raised(&ex) )\r
1568                     {\r
1569                         NPN_SetException(this, libvlc_exception_get_message(&ex));\r
1570                         libvlc_exception_clear(&ex);\r
1571                         return INVOKERESULT_GENERIC_ERROR;\r
1572                     }\r
1573                     else\r
1574                     {\r
1575                         VOID_TO_NPVARIANT(result);\r
1576                         return INVOKERESULT_NO_ERROR;\r
1577                     }\r
1578                 }\r
1579                 return INVOKERESULT_NO_SUCH_METHOD;\r
1580             case ID_playlist_prev:\r
1581                 if( argCount == 0 )\r
1582                 {\r
1583                     libvlc_playlist_prev(p_plugin->getVLC(), &ex);\r
1584                     if( libvlc_exception_raised(&ex) )\r
1585                     {\r
1586                         NPN_SetException(this, libvlc_exception_get_message(&ex));\r
1587                         libvlc_exception_clear(&ex);\r
1588                         return INVOKERESULT_GENERIC_ERROR;\r
1589                     }\r
1590                     else\r
1591                     {\r
1592                         VOID_TO_NPVARIANT(result);\r
1593                         return INVOKERESULT_NO_ERROR;\r
1594                     }\r
1595                 }\r
1596                 return INVOKERESULT_NO_SUCH_METHOD;\r
1597             case ID_playlist_clear: /* deprecated */\r
1598                 if( argCount == 0 )\r
1599                 {\r
1600                     libvlc_playlist_clear(p_plugin->getVLC(), &ex);\r
1601                     if( libvlc_exception_raised(&ex) )\r
1602                     {\r
1603                         NPN_SetException(this, libvlc_exception_get_message(&ex));\r
1604                         libvlc_exception_clear(&ex);\r
1605                         return INVOKERESULT_GENERIC_ERROR;\r
1606                     }\r
1607                     else\r
1608                     {\r
1609                         VOID_TO_NPVARIANT(result);\r
1610                         return INVOKERESULT_NO_ERROR;\r
1611                     }\r
1612                 }\r
1613                 return INVOKERESULT_NO_SUCH_METHOD;\r
1614             case ID_playlist_removeitem: /* deprecated */\r
1615                 if( (argCount == 1) && isNumberValue(args[0]) )\r
1616                 {\r
1617                     libvlc_playlist_delete_item(p_plugin->getVLC(), numberValue(args[0]), &ex);\r
1618                     if( libvlc_exception_raised(&ex) )\r
1619                     {\r
1620                         NPN_SetException(this, libvlc_exception_get_message(&ex));\r
1621                         libvlc_exception_clear(&ex);\r
1622                         return INVOKERESULT_GENERIC_ERROR;\r
1623                     }\r
1624                     else\r
1625                     {\r
1626                         VOID_TO_NPVARIANT(result);\r
1627                         return INVOKERESULT_NO_ERROR;\r
1628                     }\r
1629                 }\r
1630                 return INVOKERESULT_NO_SUCH_METHOD;\r
1631             default:\r
1632                 ;\r
1633         }\r
1634     }\r
1635     return INVOKERESULT_GENERIC_ERROR;\r
1636 }\r
1637 \r
1638 void LibvlcPlaylistNPObject::parseOptions(const NPString &s, int *i_options, char*** ppsz_options)\r
1639 {\r
1640     if( s.utf8length )\r
1641     {\r
1642         char *val = stringValue(s);\r
1643         if( val )\r
1644         {\r
1645             long capacity = 16;\r
1646             char **options = (char **)malloc(capacity*sizeof(char *));\r
1647             if( options )\r
1648             {\r
1649                 int nOptions = 0;\r
1650 \r
1651                 char *end = val + s.utf8length;\r
1652                 while( val < end )\r
1653                 {\r
1654                     // skip leading blanks\r
1655                     while( (val < end)\r
1656                         && ((*val == ' ' ) || (*val == '\t')) )\r
1657                         ++val;\r
1658 \r
1659                     char *start = val;\r
1660                     // skip till we get a blank character\r
1661                     while( (val < end)\r
1662                         && (*val != ' ' )\r
1663                         && (*val != '\t') )\r
1664                     {\r
1665                         char c = *(val++);\r
1666                         if( ('\'' == c) || ('"' == c) )\r
1667                         {\r
1668                             // skip till end of string\r
1669                             while( (val < end) && (*(val++) != c ) );\r
1670                         }\r
1671                     }\r
1672 \r
1673                     if( val > start )\r
1674                     {\r
1675                         if( nOptions == capacity )\r
1676                         {\r
1677                             capacity += 16;\r
1678                             char **moreOptions = (char **)realloc(options, capacity*sizeof(char*)); \r
1679                             if( ! moreOptions )\r
1680                             {\r
1681                                 /* failed to allocate more memory */\r
1682                                 delete val;\r
1683                                 /* return what we got so far */\r
1684                                 *i_options = nOptions;\r
1685                                 *ppsz_options = options;\r
1686                                 break;\r
1687                             }\r
1688                             options = moreOptions;\r
1689                         }\r
1690                         *(val++) = '\0';\r
1691                         options[nOptions++] = strdup(start);\r
1692                     }\r
1693                     else\r
1694                         // must be end of string\r
1695                         break;\r
1696                 }\r
1697                 *i_options = nOptions;\r
1698                 *ppsz_options = options;\r
1699             }\r
1700             delete val;\r
1701         }\r
1702     }\r
1703 }\r
1704 \r
1705 void LibvlcPlaylistNPObject::parseOptions(NPObject *obj, int *i_options, char*** ppsz_options)\r
1706 {\r
1707     /* WARNING: Safari does not implement NPN_HasProperty/NPN_HasMethod */\r
1708 \r
1709     NPVariant value;\r
1710 \r
1711     /* we are expecting to have a Javascript Array object */\r
1712     NPIdentifier propId = NPN_GetStringIdentifier("length");\r
1713     if( NPN_GetProperty(_instance, obj, propId, &value) )\r
1714     {\r
1715         int count = numberValue(value);\r
1716         NPN_ReleaseVariantValue(&value);\r
1717 \r
1718         if( count )\r
1719         {\r
1720             long capacity = 16;\r
1721             char **options = (char **)malloc(capacity*sizeof(char *));\r
1722             if( options )\r
1723             {\r
1724                 int nOptions = 0;\r
1725 \r
1726                 while( nOptions < count )\r
1727                 {\r
1728                     propId = NPN_GetIntIdentifier(nOptions);\r
1729                     if( ! NPN_GetProperty(_instance, obj, propId, &value) )\r
1730                         /* return what we got so far */\r
1731                         break;\r
1732 \r
1733                     if( ! NPVARIANT_IS_STRING(value) )\r
1734                     {\r
1735                         /* return what we got so far */\r
1736                         NPN_ReleaseVariantValue(&value);\r
1737                         break;\r
1738                     }\r
1739 \r
1740                     if( nOptions == capacity )\r
1741                     {\r
1742                         capacity += 16;\r
1743                         char **moreOptions = (char **)realloc(options, capacity*sizeof(char*)); \r
1744                         if( ! moreOptions )\r
1745                         {\r
1746                             /* failed to allocate more memory */\r
1747                             NPN_ReleaseVariantValue(&value);\r
1748                             /* return what we got so far */\r
1749                             *i_options = nOptions;\r
1750                             *ppsz_options = options;\r
1751                             break;\r
1752                         }\r
1753                         options = moreOptions;\r
1754                     }\r
1755 \r
1756                     options[nOptions++] = stringValue(value);\r
1757                 }\r
1758                 *i_options = nOptions;\r
1759                 *ppsz_options = options;\r
1760             }\r
1761         }\r
1762     }\r
1763 }\r
1764 \r
1765 /*\r
1766 ** implementation of libvlc video object\r
1767 */\r
1768 \r
1769 const NPUTF8 * const LibvlcVideoNPObject::propertyNames[] = \r
1770 {\r
1771     "fullscreen",\r
1772     "height",\r
1773     "width",\r
1774     "aspectRatio"\r
1775 };\r
1776 \r
1777 enum LibvlcVideoNPObjectPropertyIds\r
1778 {\r
1779     ID_video_fullscreen,\r
1780     ID_video_height,\r
1781     ID_video_width,\r
1782     ID_video_aspectratio\r
1783 };\r
1784 \r
1785 const int LibvlcVideoNPObject::propertyCount = sizeof(LibvlcVideoNPObject::propertyNames)/sizeof(NPUTF8 *);\r
1786 \r
1787 RuntimeNPObject::InvokeResult LibvlcVideoNPObject::getProperty(int index, NPVariant &result)\r
1788 {\r
1789     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(_instance->pdata);\r
1790     if( p_plugin )\r
1791     {\r
1792         libvlc_exception_t ex;\r
1793         libvlc_exception_init(&ex);\r
1794 \r
1795         libvlc_input_t *p_input = libvlc_playlist_get_input(p_plugin->getVLC(), &ex);\r
1796         if( libvlc_exception_raised(&ex) )\r
1797         {\r
1798             NPN_SetException(this, libvlc_exception_get_message(&ex));\r
1799             libvlc_exception_clear(&ex);\r
1800             return INVOKERESULT_GENERIC_ERROR;\r
1801         }\r
1802 \r
1803         switch( index )\r
1804         {\r
1805             case ID_video_fullscreen:\r
1806             {\r
1807                 int val = libvlc_get_fullscreen(p_input, &ex);\r
1808                 libvlc_input_free(p_input);\r
1809                 if( libvlc_exception_raised(&ex) )\r
1810                 {\r
1811                     NPN_SetException(this, libvlc_exception_get_message(&ex));\r
1812                     libvlc_exception_clear(&ex);\r
1813                     return INVOKERESULT_GENERIC_ERROR;\r
1814                 }\r
1815                 BOOLEAN_TO_NPVARIANT(val, result);\r
1816                 return INVOKERESULT_NO_ERROR;\r
1817             }\r
1818             case ID_video_height:\r
1819             {\r
1820                 int val = libvlc_video_get_height(p_input, &ex);\r
1821                 libvlc_input_free(p_input);\r
1822                 if( libvlc_exception_raised(&ex) )\r
1823                 {\r
1824                     NPN_SetException(this, libvlc_exception_get_message(&ex));\r
1825                     libvlc_exception_clear(&ex);\r
1826                     return INVOKERESULT_GENERIC_ERROR;\r
1827                 }\r
1828                 INT32_TO_NPVARIANT(val, result);\r
1829                 return INVOKERESULT_NO_ERROR;\r
1830             }\r
1831             case ID_video_width:\r
1832             {\r
1833                 int val = libvlc_video_get_width(p_input, &ex);\r
1834                 libvlc_input_free(p_input);\r
1835                 if( libvlc_exception_raised(&ex) )\r
1836                 {\r
1837                     NPN_SetException(this, libvlc_exception_get_message(&ex));\r
1838                     libvlc_exception_clear(&ex);\r
1839                     return INVOKERESULT_GENERIC_ERROR;\r
1840                 }\r
1841                 INT32_TO_NPVARIANT(val, result);\r
1842                 return INVOKERESULT_NO_ERROR;\r
1843             }\r
1844             case ID_video_aspectratio:\r
1845             {\r
1846                 NPUTF8 *psz_aspect = libvlc_video_get_aspect_ratio(p_input, &ex);\r
1847                 libvlc_input_free(p_input);\r
1848                 if( libvlc_exception_raised(&ex) )\r
1849                 {\r
1850                     NPN_SetException(this, libvlc_exception_get_message(&ex));\r
1851                     libvlc_exception_clear(&ex);\r
1852                     return INVOKERESULT_GENERIC_ERROR;\r
1853                 }\r
1854                 if( !psz_aspect )\r
1855                     return INVOKERESULT_GENERIC_ERROR;\r
1856 \r
1857                 STRINGZ_TO_NPVARIANT(psz_aspect, result);\r
1858                 return INVOKERESULT_NO_ERROR;\r
1859             }\r
1860         }\r
1861         libvlc_input_free(p_input);\r
1862     }\r
1863     return INVOKERESULT_GENERIC_ERROR;\r
1864 }\r
1865 \r
1866 RuntimeNPObject::InvokeResult LibvlcVideoNPObject::setProperty(int index, const NPVariant &value)\r
1867 {\r
1868     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(_instance->pdata);\r
1869     if( p_plugin )\r
1870     {\r
1871         libvlc_exception_t ex;\r
1872         libvlc_exception_init(&ex);\r
1873 \r
1874         libvlc_input_t *p_input = libvlc_playlist_get_input(p_plugin->getVLC(), &ex);\r
1875         if( libvlc_exception_raised(&ex) )\r
1876         {\r
1877             NPN_SetException(this, libvlc_exception_get_message(&ex));\r
1878             libvlc_exception_clear(&ex);\r
1879             return INVOKERESULT_GENERIC_ERROR;\r
1880         }\r
1881 \r
1882         switch( index )\r
1883         {\r
1884             case ID_video_fullscreen:\r
1885             {\r
1886                 if( ! NPVARIANT_IS_BOOLEAN(value) )\r
1887                 {\r
1888                     libvlc_input_free(p_input);\r
1889                     return INVOKERESULT_INVALID_VALUE;\r
1890                 }\r
1891 \r
1892                 int val = NPVARIANT_TO_BOOLEAN(value);\r
1893                 libvlc_set_fullscreen(p_input, val, &ex);\r
1894                 libvlc_input_free(p_input);\r
1895                 if( libvlc_exception_raised(&ex) )\r
1896                 {\r
1897                     NPN_SetException(this, libvlc_exception_get_message(&ex));\r
1898                     libvlc_exception_clear(&ex);\r
1899                     return INVOKERESULT_GENERIC_ERROR;\r
1900                 }\r
1901                 return INVOKERESULT_NO_ERROR;\r
1902             }\r
1903             case ID_video_aspectratio:\r
1904             {\r
1905                 char *psz_aspect = NULL;\r
1906 \r
1907                 if( ! NPVARIANT_IS_STRING(value) )\r
1908                 {\r
1909                     libvlc_input_free(p_input);\r
1910                     return INVOKERESULT_INVALID_VALUE;\r
1911                 }\r
1912 \r
1913                 psz_aspect = stringValue(NPVARIANT_TO_STRING(value));\r
1914                 if( !psz_aspect )\r
1915                 {\r
1916                     libvlc_input_free(p_input);\r
1917                     return INVOKERESULT_GENERIC_ERROR;\r
1918                 }\r
1919 \r
1920                 libvlc_video_set_aspect_ratio(p_input, psz_aspect, &ex);\r
1921                 if( psz_aspect )\r
1922                     free(psz_aspect );\r
1923 \r
1924                 libvlc_input_free(p_input);\r
1925                 if( libvlc_exception_raised(&ex) )\r
1926                 {\r
1927                     NPN_SetException(this, libvlc_exception_get_message(&ex));\r
1928                     libvlc_exception_clear(&ex);\r
1929                     return INVOKERESULT_GENERIC_ERROR;\r
1930                 }\r
1931                 return INVOKERESULT_NO_ERROR;\r
1932             }\r
1933         }\r
1934         libvlc_input_free(p_input);\r
1935     }\r
1936     return INVOKERESULT_GENERIC_ERROR;\r
1937 }\r
1938 \r
1939 const NPUTF8 * const LibvlcVideoNPObject::methodNames[] =\r
1940 {\r
1941     "toggleFullscreen",\r
1942 };\r
1943 \r
1944 enum LibvlcVideoNPObjectMethodIds\r
1945 {\r
1946     ID_video_togglefullscreen,\r
1947 };\r
1948 \r
1949 const int LibvlcVideoNPObject::methodCount = sizeof(LibvlcVideoNPObject::methodNames)/sizeof(NPUTF8 *);\r
1950 \r
1951 RuntimeNPObject::InvokeResult LibvlcVideoNPObject::invoke(int index, const NPVariant *args, uint32_t argCount, NPVariant &result)\r
1952 {\r
1953     VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(_instance->pdata);\r
1954     if( p_plugin )\r
1955     {\r
1956         libvlc_exception_t ex;\r
1957         libvlc_exception_init(&ex);\r
1958 \r
1959         libvlc_input_t *p_input = libvlc_playlist_get_input(p_plugin->getVLC(), &ex);\r
1960         if( libvlc_exception_raised(&ex) )\r
1961         {\r
1962             NPN_SetException(this, libvlc_exception_get_message(&ex));\r
1963             libvlc_exception_clear(&ex);\r
1964             return INVOKERESULT_GENERIC_ERROR;\r
1965         }\r
1966 \r
1967         switch( index )\r
1968         {\r
1969             case ID_video_togglefullscreen:\r
1970                 if( argCount == 0 )\r
1971                 {\r
1972                     libvlc_toggle_fullscreen(p_input, &ex);\r
1973                     libvlc_input_free(p_input);\r
1974                     if( libvlc_exception_raised(&ex) )\r
1975                     {\r
1976                         NPN_SetException(this, libvlc_exception_get_message(&ex));\r
1977                         libvlc_exception_clear(&ex);\r
1978                         return INVOKERESULT_GENERIC_ERROR;\r
1979                     }\r
1980                     else\r
1981                     {\r
1982                         VOID_TO_NPVARIANT(result);\r
1983                         return INVOKERESULT_NO_ERROR;\r
1984                     }\r
1985                 }\r
1986                 else\r
1987                 {\r
1988                     /* cannot get input, probably not playing */\r
1989                     if( libvlc_exception_raised(&ex) )\r
1990                     {\r
1991                         NPN_SetException(this, libvlc_exception_get_message(&ex));\r
1992                         libvlc_exception_clear(&ex);\r
1993                     }\r
1994                     return INVOKERESULT_GENERIC_ERROR;\r
1995                 }\r
1996                 return INVOKERESULT_NO_SUCH_METHOD;\r
1997             default:\r
1998                 return INVOKERESULT_NO_SUCH_METHOD;\r
1999         }\r
2000     }\r
2001     return INVOKERESULT_GENERIC_ERROR;\r
2002 }\r
2003 \r