prevent double taps by not sending the same key press twice within 200ms
authorLars Op den Kamp <lars@opdenkamp.eu>
Sun, 7 Oct 2012 10:07:47 +0000 (12:07 +0200)
committerLars Op den Kamp <lars@opdenkamp.eu>
Sun, 7 Oct 2012 10:09:09 +0000 (12:09 +0200)
include/cectypes.h
src/lib/CECClient.cpp
src/lib/CECClient.h

index d05a392dda39d8dbc2cd1963b46a440320f975d6..c80354e580284b06fcee86b9e21e373ea6eecdfe 100644 (file)
@@ -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
  */
index 3ae420ef4d09277eb0504a668165bbb9e3feb723..cafcd700d0ab8c40a484477d22721b25da3da878 100644 (file)
@@ -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)
index e757464f887fe32de2f3218aa8d3c5a17bfec891..11a6504d826aaa14b3f59e7cec1100f55bc2e249 100644 (file)
@@ -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 */
   };
 }