From: Lars Op den Kamp Date: Sun, 7 Oct 2012 10:07:47 +0000 (+0200) Subject: prevent double taps by not sending the same key press twice within 200ms X-Git-Tag: upstream/2.2.0~1^2~17^2^2~19 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=dbf90acfa110122cb4e993babb0f75a411355795;p=deb_libcec.git prevent double taps by not sending the same key press twice within 200ms --- diff --git a/include/cectypes.h b/include/cectypes.h index d05a392..c80354e 100644 --- a/include/cectypes.h +++ b/include/cectypes.h @@ -79,6 +79,11 @@ namespace CEC { */ #define CEC_BUTTON_TIMEOUT 500 +/*! + * don't send the same key twice within this timeout in milliseconds + */ +#define CEC_DOUBLE_TAP_TIMEOUT_MS 200 + /*! * don't query the power state for the same device within this timeout in milliseconds */ diff --git a/src/lib/CECClient.cpp b/src/lib/CECClient.cpp index 3ae420e..cafcd70 100644 --- a/src/lib/CECClient.cpp +++ b/src/lib/CECClient.cpp @@ -56,8 +56,11 @@ CCECClient::CCECClient(CCECProcessor *processor, const libcec_configuration &con m_bRegistered(false), m_iCurrentButton(CEC_USER_CONTROL_CODE_UNKNOWN), m_buttontime(0), - m_iPreventForwardingPowerOffCommand(0) + m_iPreventForwardingPowerOffCommand(0), + m_iLastKeypressTime(0) { + m_lastKeypress.keycode = CEC_USER_CONTROL_CODE_UNKNOWN; + m_lastKeypress.duration = 0; m_configuration.Clear(); // set the initial configuration SetConfiguration(configuration); @@ -1424,7 +1427,20 @@ void CCECClient::CallbackAddKey(const cec_keypress &key) { CLockObject lock(m_cbMutex); if (m_configuration.callbacks && m_configuration.callbacks->CBCecKeyPress) - m_configuration.callbacks->CBCecKeyPress(m_configuration.callbackParam, key); + { + // prevent double taps + int64_t now = GetTimeMs(); + if (m_lastKeypress.keycode != key.keycode || + key.duration > 0 || + now - m_iLastKeypressTime >= CEC_DOUBLE_TAP_TIMEOUT_MS) + { + // no double tap + if (key.duration == 0) + m_iLastKeypressTime = now; + m_lastKeypress = key; + m_configuration.callbacks->CBCecKeyPress(m_configuration.callbackParam, key); + } + } } void CCECClient::CallbackAddLog(const cec_log_message &message) diff --git a/src/lib/CECClient.h b/src/lib/CECClient.h index e757464..11a6504 100644 --- a/src/lib/CECClient.h +++ b/src/lib/CECClient.h @@ -307,5 +307,7 @@ namespace CEC cec_user_control_code m_iCurrentButton; /**< the control code of the button that's currently held down (if any) */ int64_t m_buttontime; /**< the timestamp when the button was pressed (in seconds since epoch), or 0 if none was pressed. */ int64_t m_iPreventForwardingPowerOffCommand; /**< prevent forwarding standby commands until this time */ + int64_t m_iLastKeypressTime; /**< last time a key press was sent to the client */ + cec_keypress m_lastKeypress; /**< the last key press that was sent to the client */ }; }