]> git.sesse.net Git - vlc/commitdiff
- added log APIs to javascript for mozilla
authorDamien Fouilleul <damienf@videolan.org>
Sat, 28 Oct 2006 23:39:18 +0000 (23:39 +0000)
committerDamien Fouilleul <damienf@videolan.org>
Sat, 28 Oct 2006 23:39:18 +0000 (23:39 +0000)
mozilla/control/npolibvlc.cpp
mozilla/control/npolibvlc.h

index be6b542a0562f7d6d0dd9bff828ea8ab2a468aac..40ef5d045aae11f3cbde50a9df1b3f9140f6ff0d 100755 (executable)
@@ -43,6 +43,7 @@ LibvlcRootNPObject::LibvlcRootNPObject(NPP instance, const NPClass *aClass) :
 {\r
     audioObj = NPN_CreateObject(instance, RuntimeNPClass<LibvlcAudioNPObject>::getClass());\r
     inputObj = NPN_CreateObject(instance, RuntimeNPClass<LibvlcInputNPObject>::getClass());\r
+    logObj = NPN_CreateObject(instance, RuntimeNPClass<LibvlcLogNPObject>::getClass());\r
     playlistObj = NPN_CreateObject(instance, RuntimeNPClass<LibvlcPlaylistNPObject>::getClass());\r
     videoObj = NPN_CreateObject(instance,RuntimeNPClass<LibvlcVideoNPObject>::getClass());\r
 }\r
@@ -51,6 +52,7 @@ LibvlcRootNPObject::~LibvlcRootNPObject()
 {\r
     NPN_ReleaseObject(audioObj);\r
     NPN_ReleaseObject(inputObj);\r
+    NPN_ReleaseObject(logObj);\r
     NPN_ReleaseObject(playlistObj);\r
     NPN_ReleaseObject(videoObj);\r
 }\r
@@ -494,6 +496,459 @@ const NPUTF8 * const LibvlcInputNPObject::methodNames[] =
 \r
 const int LibvlcInputNPObject::methodCount = sizeof(LibvlcInputNPObject::methodNames)/sizeof(NPUTF8 *);\r
 \r
+/*\r
+** implementation of libvlc message object\r
+*/\r
+\r
+const NPUTF8 * const LibvlcMessageNPObject::propertyNames[] = \r
+{\r
+    "severity",\r
+    "type",\r
+    "name",\r
+    "header",\r
+    "message",\r
+};\r
+\r
+const int LibvlcMessageNPObject::propertyCount = sizeof(LibvlcMessageNPObject::propertyNames)/sizeof(NPUTF8 *);\r
+\r
+enum LibvlcMessageNPObjectPropertyIds\r
+{\r
+    ID_severity,\r
+    ID_type,\r
+    ID_name,\r
+    ID_header,\r
+    ID_message,\r
+};\r
+\r
+RuntimeNPObject::InvokeResult LibvlcMessageNPObject::getProperty(int index, NPVariant &result)\r
+{\r
+    switch( index )\r
+    {\r
+        case ID_severity:\r
+        {\r
+            INT32_TO_NPVARIANT(_msg.i_severity, result);\r
+            return INVOKERESULT_NO_ERROR;\r
+        }\r
+        case ID_type:\r
+        {\r
+            if( _msg.psz_type )\r
+            {\r
+                int len = strlen(_msg.psz_type);\r
+                NPUTF8* retval = (NPUTF8*)NPN_MemAlloc(len);\r
+                if( retval )\r
+                {\r
+                    memcpy(retval, _msg.psz_type, len);\r
+                    STRINGN_TO_NPVARIANT(retval, len, result);\r
+                }\r
+            }\r
+            else\r
+            {\r
+                NULL_TO_NPVARIANT(result);\r
+            }\r
+            return INVOKERESULT_NO_ERROR;\r
+        }\r
+        case ID_name:\r
+        {\r
+            if( _msg.psz_name )\r
+            {\r
+                int len = strlen(_msg.psz_name);\r
+                NPUTF8* retval = (NPUTF8*)NPN_MemAlloc(len);\r
+                if( retval )\r
+                {\r
+                    memcpy(retval, _msg.psz_name, len);\r
+                    STRINGN_TO_NPVARIANT(retval, len, result);\r
+                }\r
+            }\r
+            else\r
+            {\r
+                NULL_TO_NPVARIANT(result);\r
+            }\r
+            return INVOKERESULT_NO_ERROR;\r
+        }\r
+        case ID_header:\r
+        {\r
+            if( _msg.psz_header )\r
+            {\r
+                int len = strlen(_msg.psz_header);\r
+                NPUTF8* retval = (NPUTF8*)NPN_MemAlloc(len);\r
+                if( retval )\r
+                {\r
+                    memcpy(retval, _msg.psz_header, len);\r
+                    STRINGN_TO_NPVARIANT(retval, len, result);\r
+                }\r
+            }\r
+            else\r
+            {\r
+                NULL_TO_NPVARIANT(result);\r
+            }\r
+            return INVOKERESULT_NO_ERROR;\r
+        }\r
+        case ID_message:\r
+        {\r
+            if( _msg.psz_message )\r
+            {\r
+                int len = strlen(_msg.psz_message);\r
+                NPUTF8* retval = (NPUTF8*)NPN_MemAlloc(len);\r
+                if( retval )\r
+                {\r
+                    memcpy(retval, _msg.psz_message, len);\r
+                    STRINGN_TO_NPVARIANT(retval, len, result);\r
+                }\r
+            }\r
+            else\r
+            {\r
+                NULL_TO_NPVARIANT(result);\r
+            }\r
+            return INVOKERESULT_NO_ERROR;\r
+        }\r
+    }\r
+    return INVOKERESULT_GENERIC_ERROR;\r
+}\r
+\r
+const NPUTF8 * const LibvlcMessageNPObject::methodNames[] =\r
+{\r
+    /* no methods */\r
+};\r
+\r
+const int LibvlcMessageNPObject::methodCount = sizeof(LibvlcMessageNPObject::methodNames)/sizeof(NPUTF8 *);\r
+\r
+/*\r
+** implementation of libvlc message iterator object\r
+*/\r
+\r
+void LibvlcMessageIteratorNPObject::setLog(LibvlcLogNPObject* p_vlclog)\r
+{\r
+    _p_vlclog = p_vlclog;\r
+    if( p_vlclog->_p_log )\r
+    {\r
+        _p_iter = libvlc_log_get_iterator(p_vlclog->_p_log, NULL);\r
+    }\r
+    else\r
+        _p_iter = NULL;\r
+};\r
+\r
+const NPUTF8 * const LibvlcMessageIteratorNPObject::propertyNames[] = \r
+{\r
+    "hasNext",\r
+};\r
+\r
+const int LibvlcMessageIteratorNPObject::propertyCount = sizeof(LibvlcMessageIteratorNPObject::propertyNames)/sizeof(NPUTF8 *);\r
+\r
+enum LibvlcMessageIteratorNPObjectPropertyIds\r
+{\r
+    ID_hasNext,\r
+};\r
+\r
+RuntimeNPObject::InvokeResult LibvlcMessageIteratorNPObject::getProperty(int index, NPVariant &result)\r
+{\r
+    switch( index )\r
+    {\r
+        case ID_hasNext:\r
+        {\r
+            if( _p_iter &&  _p_vlclog->_p_log )\r
+            {\r
+                libvlc_exception_t ex;\r
+                libvlc_exception_init(&ex);\r
+\r
+                BOOLEAN_TO_NPVARIANT(libvlc_log_iterator_has_next(_p_iter, &ex), result);\r
+                if( libvlc_exception_raised(&ex) )\r
+                {\r
+                    NPN_SetException(this, libvlc_exception_get_message(&ex));\r
+                    libvlc_exception_clear(&ex);\r
+                    return INVOKERESULT_GENERIC_ERROR;\r
+                }\r
+            }\r
+            else\r
+            {\r
+                BOOLEAN_TO_NPVARIANT(0, result);\r
+            }\r
+            return INVOKERESULT_NO_ERROR;\r
+        }\r
+    }\r
+    return INVOKERESULT_GENERIC_ERROR;\r
+}\r
+\r
+const NPUTF8 * const LibvlcMessageIteratorNPObject::methodNames[] =\r
+{\r
+    "next",\r
+};\r
+\r
+const int LibvlcMessageIteratorNPObject::methodCount = sizeof(LibvlcMessageIteratorNPObject::methodNames)/sizeof(NPUTF8 *);\r
+\r
+enum LibvlcMessageIteratorNPObjectMethodIds\r
+{\r
+    ID_messageiterator_next,\r
+};\r
+\r
+RuntimeNPObject::InvokeResult LibvlcMessageIteratorNPObject::invoke(int index, const NPVariant *args, uint32_t argCount, NPVariant &result)\r
+{\r
+    if( _p_iter &&  _p_vlclog->_p_log )\r
+    {\r
+        libvlc_exception_t ex;\r
+        libvlc_exception_init(&ex);\r
+\r
+        switch( index )\r
+        {\r
+            case ID_messageiterator_next:\r
+                if( argCount == 0 )\r
+                {\r
+                    struct libvlc_log_message_t buffer;\r
+\r
+                    buffer.sizeof_msg = sizeof(buffer);\r
+\r
+                    libvlc_log_iterator_next(_p_iter, &buffer, &ex);\r
+                    if( libvlc_exception_raised(&ex) )\r
+                    {\r
+                        NPN_SetException(this, libvlc_exception_get_message(&ex));\r
+                        libvlc_exception_clear(&ex);\r
+                        return INVOKERESULT_GENERIC_ERROR;\r
+                    }\r
+                    else\r
+                    {\r
+                        LibvlcMessageNPObject* message =\r
+                            static_cast<LibvlcMessageNPObject*>(NPN_CreateObject(_instance, RuntimeNPClass<LibvlcMessageNPObject>::getClass()));\r
+                        if( message )\r
+                        {\r
+                            message->setMessage(buffer);\r
+                            OBJECT_TO_NPVARIANT(message, result);\r
+                            return INVOKERESULT_NO_ERROR;\r
+                        }\r
+                        return INVOKERESULT_OUT_OF_MEMORY;\r
+                    }\r
+                }\r
+            default:\r
+                return INVOKERESULT_NO_SUCH_METHOD;\r
+        }\r
+    }\r
+    return INVOKERESULT_GENERIC_ERROR;\r
+}\r
\r
+/*\r
+** implementation of libvlc message object\r
+*/\r
+\r
+const NPUTF8 * const LibvlcMessagesNPObject::propertyNames[] = \r
+{\r
+    "count",\r
+};\r
+\r
+const int LibvlcMessagesNPObject::propertyCount = sizeof(LibvlcMessagesNPObject::propertyNames)/sizeof(NPUTF8 *);\r
+\r
+enum LibvlcMessagesNPObjectPropertyIds\r
+{\r
+    ID_count,\r
+};\r
+\r
+RuntimeNPObject::InvokeResult LibvlcMessagesNPObject::getProperty(int index, NPVariant &result)\r
+{\r
+    switch( index )\r
+    {\r
+        case ID_count:\r
+        {\r
+            libvlc_log_t *p_log = _p_vlclog->_p_log;\r
+            if( p_log )\r
+            {\r
+                libvlc_exception_t ex;\r
+                libvlc_exception_init(&ex);\r
+\r
+                INT32_TO_NPVARIANT(libvlc_log_count(p_log, &ex), result);\r
+                if( libvlc_exception_raised(&ex) )\r
+                {\r
+                    NPN_SetException(this, libvlc_exception_get_message(&ex));\r
+                    libvlc_exception_clear(&ex);\r
+                    return INVOKERESULT_GENERIC_ERROR;\r
+                }\r
+            }\r
+            else\r
+            {\r
+                INT32_TO_NPVARIANT(0, result);\r
+            }\r
+            return INVOKERESULT_NO_ERROR;\r
+        }\r
+    }\r
+    return INVOKERESULT_GENERIC_ERROR;\r
+}\r
+\r
+const NPUTF8 * const LibvlcMessagesNPObject::methodNames[] =\r
+{\r
+    "clear",\r
+    "iterator",\r
+};\r
+\r
+const int LibvlcMessagesNPObject::methodCount = sizeof(LibvlcMessagesNPObject::methodNames)/sizeof(NPUTF8 *);\r
+\r
+enum LibvlcMessagesNPObjectMethodIds\r
+{\r
+    ID_messages_clear,\r
+    ID_iterator,\r
+};\r
+\r
+RuntimeNPObject::InvokeResult LibvlcMessagesNPObject::invoke(int index, const NPVariant *args, uint32_t argCount, NPVariant &result)\r
+{\r
+    libvlc_exception_t ex;\r
+    libvlc_exception_init(&ex);\r
+\r
+    switch( index )\r
+    {\r
+        case ID_messages_clear:\r
+            if( argCount == 0 )\r
+            {\r
+                libvlc_log_t *p_log = _p_vlclog->_p_log;\r
+                if( p_log )\r
+                {\r
+                    libvlc_log_clear(p_log, &ex);\r
+                    if( libvlc_exception_raised(&ex) )\r
+                    {\r
+                        NPN_SetException(this, libvlc_exception_get_message(&ex));\r
+                        libvlc_exception_clear(&ex);\r
+                        return INVOKERESULT_GENERIC_ERROR;\r
+                    }\r
+                }\r
+                return INVOKERESULT_NO_ERROR;\r
+            }\r
+            return INVOKERESULT_NO_SUCH_METHOD;\r
+\r
+        case ID_iterator:\r
+            if( argCount == 0 )\r
+            {\r
+                LibvlcMessageIteratorNPObject* iter =\r
+                    static_cast<LibvlcMessageIteratorNPObject*>(NPN_CreateObject(_instance, RuntimeNPClass<LibvlcMessageIteratorNPObject>::getClass()));\r
+                if( iter )\r
+                {\r
+                    iter->setLog(_p_vlclog);\r
+                    OBJECT_TO_NPVARIANT(iter, result);\r
+                    return INVOKERESULT_NO_ERROR;\r
+                }\r
+                return INVOKERESULT_OUT_OF_MEMORY;\r
+            }\r
+            return INVOKERESULT_NO_SUCH_METHOD;\r
+\r
+        default:\r
+            return INVOKERESULT_NO_SUCH_METHOD;\r
+    }\r
+    return INVOKERESULT_GENERIC_ERROR;\r
+}\r
\r
+/*\r
+** implementation of libvlc message object\r
+*/\r
+\r
+const NPUTF8 * const LibvlcLogNPObject::propertyNames[] = \r
+{\r
+    "messages",\r
+    "verbosity",\r
+};\r
+\r
+const int LibvlcLogNPObject::propertyCount = sizeof(LibvlcLogNPObject::propertyNames)/sizeof(NPUTF8 *);\r
+\r
+enum LibvlcLogNPObjectPropertyIds\r
+{\r
+    ID_messages,\r
+    ID_verbosity,\r
+};\r
+\r
+RuntimeNPObject::InvokeResult LibvlcLogNPObject::getProperty(int index, NPVariant &result)\r
+{\r
+    VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(_instance->pdata);\r
+    if( p_plugin )\r
+    {\r
+        libvlc_exception_t ex;\r
+        libvlc_exception_init(&ex);\r
+\r
+        switch( index )\r
+        {\r
+            case ID_messages:\r
+            {\r
+                OBJECT_TO_NPVARIANT(NPN_RetainObject(_p_vlcmessages), result);\r
+                return INVOKERESULT_NO_ERROR;\r
+            }\r
+            case ID_verbosity:\r
+            {\r
+                if( _p_log )\r
+                {\r
+                    INT32_TO_NPVARIANT(libvlc_get_log_verbosity(p_plugin->getVLC(),\r
+                                                                    &ex), result);\r
+                    if( libvlc_exception_raised(&ex) )\r
+                    {\r
+                        NPN_SetException(this, libvlc_exception_get_message(&ex));\r
+                        libvlc_exception_clear(&ex);\r
+                        return INVOKERESULT_GENERIC_ERROR;\r
+                    }\r
+                }\r
+                else\r
+                {\r
+                    /* log is not enabled, return -1 */\r
+                    DOUBLE_TO_NPVARIANT(-1.0, result);\r
+                }\r
+                return INVOKERESULT_NO_ERROR;\r
+            }\r
+        }\r
+    }\r
+    return INVOKERESULT_GENERIC_ERROR;\r
+}\r
+\r
+RuntimeNPObject::InvokeResult LibvlcLogNPObject::setProperty(int index, const NPVariant &value)\r
+{\r
+    VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(_instance->pdata);\r
+    if( p_plugin )\r
+    {\r
+        libvlc_exception_t ex;\r
+        libvlc_exception_init(&ex);\r
+\r
+        switch( index )\r
+        {\r
+            case ID_verbosity:\r
+                if( isNumberValue(value) )\r
+                {\r
+                    libvlc_instance_t* p_libvlc = p_plugin->getVLC();\r
+                    int verbosity = numberValue(value);\r
+                    if( verbosity >= 0 )\r
+                    {\r
+                        if( ! _p_log )\r
+                        {\r
+                            _p_log = libvlc_log_open(p_libvlc, &ex);\r
+                            if( libvlc_exception_raised(&ex) )\r
+                            {\r
+                                NPN_SetException(this, libvlc_exception_get_message(&ex));\r
+                                libvlc_exception_clear(&ex);\r
+                                return INVOKERESULT_GENERIC_ERROR;\r
+                            }\r
+                        }\r
+                        libvlc_set_log_verbosity(p_libvlc, (unsigned)verbosity, &ex);\r
+                        if( libvlc_exception_raised(&ex) )\r
+                        {\r
+                            NPN_SetException(this, libvlc_exception_get_message(&ex));\r
+                            libvlc_exception_clear(&ex);\r
+                            return INVOKERESULT_GENERIC_ERROR;\r
+                        }\r
+                    }\r
+                    else if( _p_log )\r
+                    {\r
+                        /* close log  when verbosity is set to -1 */\r
+                        libvlc_log_close(_p_log, &ex);\r
+                        _p_log = NULL;\r
+                        if( libvlc_exception_raised(&ex) )\r
+                        {\r
+                            NPN_SetException(this, libvlc_exception_get_message(&ex));\r
+                            libvlc_exception_clear(&ex);\r
+                            return INVOKERESULT_GENERIC_ERROR;\r
+                        }\r
+                    }\r
+                    return INVOKERESULT_NO_ERROR;\r
+                }\r
+                return INVOKERESULT_INVALID_VALUE;\r
+        }\r
+    }\r
+    return INVOKERESULT_GENERIC_ERROR;\r
+}\r
+\r
+const NPUTF8 * const LibvlcLogNPObject::methodNames[] =\r
+{\r
+    /* no methods */\r
+};\r
+\r
+const int LibvlcLogNPObject::methodCount = sizeof(LibvlcLogNPObject::methodNames)/sizeof(NPUTF8 *);\r
+\r
 /*\r
 ** implementation of libvlc playlist object\r
 */\r
index d6728b75e455c577ba48a8aee30a9765c10250ba..4beb637c688a9a6b19e3684732a5ec1ed8ec4778 100755 (executable)
@@ -23,6 +23,7 @@
 /*\r
 ** defined runtime script objects\r
 */\r
+#include <vlc/libvlc.h>\r
 \r
 #include "nporuntime.h"\r
 \r
@@ -44,6 +45,7 @@ protected:
 \r
     NPObject *audioObj;\r
     NPObject *inputObj;\r
+    NPObject *logObj;\r
     NPObject *playlistObj;\r
     NPObject *videoObj;\r
 };\r
@@ -76,7 +78,7 @@ protected:
 \r
     LibvlcInputNPObject(NPP instance, const NPClass *aClass) :\r
         RuntimeNPObject(instance, aClass) {};\r
-       \r
+        \r
     virtual ~LibvlcInputNPObject() {};\r
 \r
     static const int propertyCount;\r
@@ -89,6 +91,130 @@ protected:
     static const NPUTF8 * const methodNames[];\r
 };\r
 \r
+class LibvlcMessageNPObject: public RuntimeNPObject\r
+{\r
+public:\r
+    void setMessage(struct libvlc_log_message_t &msg)\r
+    {\r
+        _msg = msg;\r
+    };\r
+\r
+protected:\r
+    friend class RuntimeNPClass<LibvlcMessageNPObject>;\r
+\r
+    LibvlcMessageNPObject(NPP instance, const NPClass *aClass) :\r
+        RuntimeNPObject(instance, aClass) {};\r
+        \r
+    virtual ~LibvlcMessageNPObject() {};\r
+\r
+    static const int propertyCount;\r
+    static const NPUTF8 * const propertyNames[];\r
+\r
+    InvokeResult getProperty(int index, NPVariant &result);\r
+\r
+    static const int methodCount;\r
+    static const NPUTF8 * const methodNames[];\r
+\r
+private:\r
+    struct libvlc_log_message_t _msg;\r
+};\r
+\r
+class LibvlcLogNPObject;\r
+\r
+class LibvlcMessageIteratorNPObject: public RuntimeNPObject\r
+{\r
+public:\r
+    void setLog(LibvlcLogNPObject* p_vlclog);\r
+\r
+protected:\r
+    friend class RuntimeNPClass<LibvlcMessageIteratorNPObject>;\r
+\r
+    LibvlcMessageIteratorNPObject(NPP instance, const NPClass *aClass) :\r
+        RuntimeNPObject(instance, aClass) {};\r
+        \r
+    virtual ~LibvlcMessageIteratorNPObject() {};\r
+\r
+    static const int propertyCount;\r
+    static const NPUTF8 * const propertyNames[];\r
+\r
+    InvokeResult getProperty(int index, NPVariant &result);\r
+\r
+    static const int methodCount;\r
+    static const NPUTF8 * const methodNames[];\r
+\r
+    InvokeResult invoke(int index, const NPVariant *args, uint32_t argCount, NPVariant &result);\r
+\r
+private:\r
+    LibvlcLogNPObject*      _p_vlclog;\r
+    libvlc_log_iterator_t*  _p_iter;\r
+};\r
+\r
+class LibvlcMessagesNPObject: public RuntimeNPObject\r
+{\r
+public:\r
+    void setLog(LibvlcLogNPObject *p_log)\r
+    {\r
+        _p_vlclog = p_log;\r
+    }\r
+    \r
+protected:\r
+    friend class RuntimeNPClass<LibvlcMessagesNPObject>;\r
+\r
+    LibvlcMessagesNPObject(NPP instance, const NPClass *aClass) :\r
+        _p_vlclog(NULL),\r
+        RuntimeNPObject(instance, aClass) {};\r
+        \r
+    virtual ~LibvlcMessagesNPObject() {};\r
+\r
+    static const int propertyCount;\r
+    static const NPUTF8 * const propertyNames[];\r
+\r
+    InvokeResult getProperty(int index, NPVariant &result);\r
+\r
+    static const int methodCount;\r
+    static const NPUTF8 * const methodNames[];\r
+\r
+    InvokeResult invoke(int index, const NPVariant *args, uint32_t argCount, NPVariant &result);\r
+\r
+private:\r
+    LibvlcLogNPObject* _p_vlclog;\r
+};\r
+\r
+class LibvlcLogNPObject: public RuntimeNPObject\r
+{\r
+protected:\r
+    friend class RuntimeNPClass<LibvlcLogNPObject>;\r
+    friend class LibvlcMessagesNPObject;\r
+    friend class LibvlcMessageIteratorNPObject;\r
+\r
+    libvlc_log_t    *_p_log;\r
+\r
+    LibvlcLogNPObject(NPP instance, const NPClass *aClass) :\r
+        RuntimeNPObject(instance, aClass),\r
+        _p_log(NULL)\r
+    {\r
+        _p_vlcmessages = static_cast<LibvlcMessagesNPObject*>(NPN_CreateObject(instance, RuntimeNPClass<LibvlcMessagesNPObject>::getClass()));\r
+        _p_vlcmessages->setLog(this);\r
+    };\r
+        \r
+    virtual ~LibvlcLogNPObject()\r
+    {\r
+        NPN_ReleaseObject(_p_vlcmessages);\r
+    };\r
+\r
+    static const int propertyCount;\r
+    static const NPUTF8 * const propertyNames[];\r
+\r
+    InvokeResult getProperty(int index, NPVariant &result);\r
+    InvokeResult setProperty(int index, const NPVariant &value);\r
+\r
+    static const int methodCount;\r
+    static const NPUTF8 * const methodNames[];\r
+\r
+private:\r
+    LibvlcMessagesNPObject* _p_vlcmessages;\r
+};\r
+\r
 class LibvlcPlaylistNPObject: public RuntimeNPObject\r
 {\r
 protected:\r