From: Lars Op den Kamp Date: Thu, 12 Jan 2012 11:30:07 +0000 (+0100) Subject: cec: added callback methods to libCEC. enable them by calling EnableCallbacks(ICECCal... X-Git-Tag: upstream/2.2.0~1^2~41^2~10 X-Git-Url: https://git.piment-noir.org/?p=deb_libcec.git;a=commitdiff_plain;h=fa4798bd538f6114390884ad6f2f9c44c9f77e09 cec: added callback methods to libCEC. enable them by calling EnableCallbacks(ICECCallbacks *callbacks) / cec_enable_callbacks(ICECCallbacks *callbacks). after this method is called, the GetNext...() methods will not return any data --- diff --git a/include/cec.h b/include/cec.h index 61e4bb3..0e463eb 100644 --- a/include/cec.h +++ b/include/cec.h @@ -59,7 +59,14 @@ namespace CEC virtual void Close(void) = 0; /*! - * @brief Try to find all connected CEC adapters. Only implemented on Linux at the moment. + * @brief Set and enable the callback methods. If this method is not called, the GetNext...() methods will have to be used. + * @param callbacks The callbacks to set. + * @return True when enabled, false otherwise. + */ + virtual bool EnableCallbacks(ICECCallbacks *callbacks) = 0; + + /*! + * @brief Try to find all connected CEC adapters. Only implemented on Linux and Windows at the moment. * @param deviceList The vector to store device descriptors in. * @param iBufSize The size of the deviceList buffer. * @param strDevicePath Optional device path. Only adds device descriptors that match the given device path. @@ -352,7 +359,6 @@ namespace CEC virtual const char *ToString(const cec_system_audio_status mode) = 0; virtual const char *ToString(const cec_audio_status status) = 0; virtual const char *ToString(const cec_vendor_id vendor) = 0; - }; }; diff --git a/include/cecc.h b/include/cecc.h index ddb141b..c8e0d0e 100644 --- a/include/cecc.h +++ b/include/cecc.h @@ -59,6 +59,12 @@ extern DECLSPEC int cec_open(const char *strPort, uint32_t iTimeout); extern DECLSPEC void cec_close(void); +#ifdef __cplusplus +extern DECLSPEC int cec_enable_callbacks(CEC::ICECCallbacks *callbacks); +#else +extern DECLSPEC int cec_enable_callbacks(ICECCallbacks *callbacks); +#endif + #ifdef __cplusplus extern DECLSPEC int8_t cec_find_adapters(CEC::cec_adapter *deviceList, uint8_t iBufSize, const char *strDevicePath); #else diff --git a/include/cectypes.h b/include/cectypes.h index 10f8a56..3b81bdc 100644 --- a/include/cectypes.h +++ b/include/cectypes.h @@ -866,6 +866,30 @@ typedef struct cec_logical_addresses #endif } cec_logical_addresses; +struct ICECCallbacks +{ + /*! + * @brief Transfer a log message from libCEC to the client. + * @param message The message to transfer. + * @return 1 when ok, 0 otherwise. + */ + int (*CecLogMessage)(const cec_log_message &message); + + /*! + * @brief Transfer a keypress from libCEC to the client. + * @param key The keypress to transfer. + * @return 1 when ok, 0 otherwise. + */ + int (*CecKeyPress)(const cec_keypress &key); + + /*! + * @brief Transfer a CEC command from libCEC to the client. + * @param command The command to transfer. + * @return 1 when ok, 0 otherwise. + */ + int (*CecCommand)(const cec_command &command); +}; + #ifdef UNUSED #elif defined(__GNUC__) #define UNUSED(x) UNUSED_ ## x __attribute__((unused)) diff --git a/src/lib/LibCEC.cpp b/src/lib/LibCEC.cpp index 1366ff6..a8b8c70 100644 --- a/src/lib/LibCEC.cpp +++ b/src/lib/LibCEC.cpp @@ -45,7 +45,8 @@ using namespace CEC; CLibCEC::CLibCEC(const char *strDeviceName, cec_device_type_list types) : m_iStartTime(GetTimeMs()), m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN), - m_buttontime(0) + m_buttontime(0), + m_callbacks(NULL) { m_cec = new CCECProcessor(this, strDeviceName, types); } @@ -53,7 +54,8 @@ CLibCEC::CLibCEC(const char *strDeviceName, cec_device_type_list types) : CLibCEC::CLibCEC(const char *strDeviceName, cec_logical_address iLogicalAddress /* = CECDEVICE_PLAYBACKDEVICE1 */, uint16_t iPhysicalAddress /* = CEC_DEFAULT_PHYSICAL_ADDRESS */) : m_iStartTime(GetTimeMs()), m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN), - m_buttontime(0) + m_buttontime(0), + m_callbacks(NULL) { m_cec = new CCECProcessor(this, strDeviceName, iLogicalAddress, iPhysicalAddress); } @@ -87,6 +89,14 @@ void CLibCEC::Close(void) m_cec->StopThread(); } +bool CLibCEC::EnableCallbacks(ICECCallbacks *callbacks) +{ + CLockObject lock(&m_mutex); + if (m_cec) + m_callbacks = callbacks; + return false; +} + int8_t CLibCEC::FindAdapters(cec_adapter *deviceList, uint8_t iBufSize, const char *strDevicePath /* = NULL */) { CStdString strDebug; @@ -328,32 +338,46 @@ cec_osd_name CLibCEC::GetDeviceOSDName(cec_logical_address iAddress) void CLibCEC::AddLog(cec_log_level level, const string &strMessage) { + CLockObject lock(&m_mutex); if (m_cec) { cec_log_message message; message.level = level; message.time = GetTimeMs() - m_iStartTime; snprintf(message.message, sizeof(message.message), "%s", strMessage.c_str()); - m_logBuffer.Push(message); + + if (m_callbacks) + m_callbacks->CecLogMessage(message); + else + m_logBuffer.Push(message); } } void CLibCEC::AddKey(cec_keypress &key) { - m_keyBuffer.Push(key); + CLockObject lock(&m_mutex); + if (m_callbacks) + m_callbacks->CecKeyPress(key); + else + m_keyBuffer.Push(key); m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN; m_buttontime = 0; } void CLibCEC::AddKey(void) { + CLockObject lock(&m_mutex); if (m_iCurrentButton != CEC_USER_CONTROL_CODE_UNKNOWN) { cec_keypress key; key.duration = (unsigned int) (GetTimeMs() - m_buttontime); key.keycode = m_iCurrentButton; - m_keyBuffer.Push(key); + + if (m_callbacks) + m_callbacks->CecKeyPress(key); + else + m_keyBuffer.Push(key); m_iCurrentButton = CEC_USER_CONTROL_CODE_UNKNOWN; } m_buttontime = 0; @@ -361,7 +385,12 @@ void CLibCEC::AddKey(void) void CLibCEC::AddCommand(const cec_command &command) { - if (m_commandBuffer.Push(command)) + CLockObject lock(&m_mutex); + if (m_callbacks) + { + m_callbacks->CecCommand(command); + } + else if (m_commandBuffer.Push(command)) { CStdString strDebug; strDebug.Format("stored command '%2x' in the command buffer. buffer size = %d", command.opcode, m_commandBuffer.Size()); diff --git a/src/lib/LibCEC.h b/src/lib/LibCEC.h index 9c3e8f1..dde69f4 100644 --- a/src/lib/LibCEC.h +++ b/src/lib/LibCEC.h @@ -53,6 +53,7 @@ namespace CEC virtual bool Open(const char *strPort, uint32_t iTimeout = 10000); virtual void Close(void); + virtual bool EnableCallbacks(ICECCallbacks *callbacks); virtual int8_t FindAdapters(cec_adapter *deviceList, uint8_t iBufSize, const char *strDevicePath = NULL); virtual bool PingAdapter(void); virtual bool StartBootloader(void); @@ -126,5 +127,7 @@ namespace CEC CecBuffer m_logBuffer; CecBuffer m_keyBuffer; CecBuffer m_commandBuffer; + ICECCallbacks *m_callbacks; + CMutex m_mutex; }; }; diff --git a/src/lib/LibCECC.cpp b/src/lib/LibCECC.cpp index 579d796..5ca2885 100644 --- a/src/lib/LibCECC.cpp +++ b/src/lib/LibCECC.cpp @@ -74,6 +74,13 @@ void cec_close(void) cec_parser->Close(); } +int cec_enable_callbacks(ICECCallbacks *callbacks) +{ + if (cec_parser) + return cec_parser->EnableCallbacks(callbacks) ? 1 : 0; + return -1; +} + int8_t cec_find_adapters(cec_adapter *deviceList, uint8_t iBufSize, const char *strDevicePath /* = NULL */) { if (cec_parser)