Skip to contentSkip to conversation

adb.c: add power key support

PATCH Superseded Programmingkid 2 participants 2 messages Mar 24, 2016 – May 5, 2016

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 4/4 adb.c: add power key support

Add support for the power key. It has to be handled differently from the other keys because it is the only 16-bit value key.

---
 hw/input/adb.c | 32 +++++++++++++++++++++++++-------
 1 file changed, 25 insertions(+), 7 deletions(-)
hw/input/adb.c +25 -7
index 8413db9..fab241b 100644
@@ -358,10 +358,17 @@ static int adb_kbd_poll(ADBDevice *d, uint8_t *obuf)
         s->rptr = 0;
     }
     s->count--;
-    obuf[0] = keycode;
-    /* NOTE: could put a second keycode if needed */
-    obuf[1] = 0xff;
-    olen = 2;
+    /* The power key is the only two byte value key, so it is a special case. */
+    if (keycode == 0x7f) {
+        obuf[0] = 0x7f;
+        obuf[1] = 0x7f;
+        olen = 2;
+    } else {
+        obuf[0] = keycode;
+        /* NOTE: could put a second keycode if needed */
+        obuf[1] = 0xff;
+        olen = 2;
+    }
 
     return olen;
 }
@@ -440,11 +447,22 @@ static void adb_keyboard_event(DeviceState *dev, QemuConsole *src,
     }
     keycode = qcode_to_adb_keycode[qcode];
 
-    if (evt->u.key.data->down == false) { /* if key release event */
-        keycode = keycode | 0x80;   /* create keyboard break code */
+    /* The power button is a special case because it is a 16-bit value */
+    if (qcode == Q_KEY_CODE_POWER) {
+        if (evt->u.key.data->down == true) { /* Power button pushed keycode */
+            adb_kbd_put_keycode(s, 0x7f);
+        } else {                           /* Power button released keycode */
+            adb_kbd_put_keycode(s, 0xff);
+        }
     }
 
-    adb_kbd_put_keycode(s, keycode);
+    /* For all non-power keys - safe for 8-bit keycodes */
+    else {
+        if (evt->u.key.data->down == false) { /* if key release event */
+            keycode = keycode | 0x80;   /* create keyboard break code */
+        }
+        adb_kbd_put_keycode(s, keycode);
+    }
 }
 
 static const VMStateDescription vmstate_adb_kbd = {
Signature · 1 line
2.7.2
#1 lore raw
Quoted 19 lines · Programmingkid

Add support for the power key. It has to be handled differently from the other keys because it is the only 16-bit value key.

---
 hw/input/adb.c | 32 +++++++++++++++++++++++++-------
 1 file changed, 25 insertions(+), 7 deletions(-)
hw/input/adb.c +0 -3
index 8413db9..fab241b 100644
@@ -358,10 +358,17 @@ static int adb_kbd_poll(ADBDevice *d, uint8_t *obuf)
         s->rptr = 0;
     }
     s->count--;
-    obuf[0] = keycode;
-    /* NOTE: could put a second keycode if needed */
-    obuf[1] = 0xff;

hw/input/adb.c static int adb_kbd_poll(ADBDevice *d, uint8_t *obuf) · line 369

Quoted 10 lines
hw/input/adb.c +9 -1
-    olen = 2;
+    /* The power key is the only two byte value key, so it is a special case. */
+    if (keycode == 0x7f) {
+        obuf[0] = 0x7f;
+        obuf[1] = 0x7f;
+        olen = 2;
+    } else {
+        obuf[0] = keycode;
+        /* NOTE: could put a second keycode if needed */
+        obuf[1] = 0xff;

This codepath is also now handling "power key key-up" (which is 0xff 0xff), so that should have a comment. I suggest changing the NOTE to something like:
/* NOTE: the power key key-up is the two byte sequence 0xff 0xff; otherwise
* we could in theory send a second keycode in the second byte, but choose
* not to bother.
*/

hw/input/adb.c static void adb_keyboard_event(DeviceState *dev, QemuConsole *src, · line 450

Quoted 12 lines
hw/input/adb.c +3 -2
+        olen = 2;
+    }
 
     return olen;
 }
@@ -440,11 +447,22 @@ static void adb_keyboard_event(DeviceState *dev, QemuConsole *src,
     }
     keycode = qcode_to_adb_keycode[qcode];
 
-    if (evt->u.key.data->down == false) { /* if key release event */
-        keycode = keycode | 0x80;   /* create keyboard break code */
+    /* The power button is a special case because it is a 16-bit value */

useful also to say "Since 0x7f is not a used keycode for ADB we overload it to indicate the power button when we're storing keycodes in our internal buffer, and expand it out to two bytes when we send to the guest."

hw/input/adb.c static void adb_keyboard_event(DeviceState *dev, QemuConsole *src, · line 460

Quoted 11 lines
hw/input/adb.c +8 -1
+    if (qcode == Q_KEY_CODE_POWER) {
+        if (evt->u.key.data->down == true) { /* Power button pushed keycode */
+            adb_kbd_put_keycode(s, 0x7f);
+        } else {                           /* Power button released keycode */
+            adb_kbd_put_keycode(s, 0xff);
+        }
     }
 
-    adb_kbd_put_keycode(s, keycode);
+    /* For all non-power keys - safe for 8-bit keycodes */
+    else {

Your braces here are wrong: the "else" should be on the same line as the closing } of the preceding if clause (so "} else {".)

hw/input/adb.c static void adb_keyboard_event(DeviceState *dev, QemuConsole *src, · line 468

Quoted 8 lines
hw/input/adb.c +5 -0
+        if (evt->u.key.data->down == false) { /* if key release event */
+            keycode = keycode | 0x80;   /* create keyboard break code */
+        }
+        adb_kbd_put_keycode(s, keycode);
+    }
 }
 
 static const VMStateDescription vmstate_adb_kbd = {

Otherwise looks good.

thanks
-- PMM