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