diff mbox

[v3,5/5] adb.c: add power key support

Message ID CD03863E-1429-4009-9B02-FCFFBEE30A35@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Programmingkid May 6, 2016, 2:39 a.m. UTC
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.

Signed-off-by: John Arbuckle <programmingkidx@gmail.com>
---
v3 change
Add several suggested comments.
Moved the location of an else statement in the adb_keyboard_event() function.

 hw/input/adb.c | 45 ++++++++++++++++++++++++++++++++++-----------
 1 file changed, 34 insertions(+), 11 deletions(-)

Comments

Peter Maydell May 16, 2016, 5:55 p.m. UTC | #1
On 6 May 2016 at 03:39, Programmingkid <programmingkidx@gmail.com> wrote:
> 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.
>
> Signed-off-by: John Arbuckle <programmingkidx@gmail.com>
> ---
> v3 change
> Add several suggested comments.
> Moved the location of an else statement in the adb_keyboard_event() function.
>
>  hw/input/adb.c | 45 ++++++++++++++++++++++++++++++++++-----------
>  1 file changed, 34 insertions(+), 11 deletions(-)
>
> diff --git a/hw/input/adb.c b/hw/input/adb.c
> index 37728b3..b5e53c7 100644
> --- a/hw/input/adb.c
> +++ b/hw/input/adb.c
> @@ -343,10 +343,25 @@ 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.
> +     * 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."

Stray " at end of comment.

> +     */
> +    if (keycode == 0x7f) {
> +        obuf[0] = 0x7f;
> +        obuf[1] = 0x7f;
> +        olen = 2;
> +    } else {
> +        obuf[0] = keycode;
> +        /* 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.
> +         */
> +        obuf[1] = 0xff;
> +        olen = 2;
> +    }
>
>      return olen;
>  }
> @@ -424,15 +439,23 @@ static void adb_keyboard_event(DeviceState *dev, QemuConsole *src,
>          return;
>      }
>      keycode = qcode_to_adb_keycode[qcode];
> -    if (keycode == NO_KEY) {  /* We don't want to send this to the guest */
> -        return;
> -    }
>
> -    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) {
> +        printf("Power Key detected\n");

Debug printf left in code.

> +        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);
> +        }
> +    } else if (keycode == NO_KEY) { /* NO_KEY shouldn't be sent to guest */
> +        return;
> +    } else {   /* For all non-power keys - safe for 8-bit keycodes */
> +        if (evt->u.key.data->down == false) { /* if key release event */
> +            keycode = keycode | 0x80;   /* create keyboard break code */
> +        }
> +        adb_kbd_put_keycode(s, keycode);
>      }
> -
> -    adb_kbd_put_keycode(s, keycode);
>  }
>
>  static const VMStateDescription vmstate_adb_kbd = {
> --
> 2.7.2

Otherwise
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>

thanks
-- PMM
diff mbox

Patch

diff --git a/hw/input/adb.c b/hw/input/adb.c
index 37728b3..b5e53c7 100644
--- a/hw/input/adb.c
+++ b/hw/input/adb.c
@@ -343,10 +343,25 @@  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.
+     * 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."
+     */
+    if (keycode == 0x7f) {
+        obuf[0] = 0x7f;
+        obuf[1] = 0x7f;
+        olen = 2;
+    } else {
+        obuf[0] = keycode;
+        /* 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.
+         */
+        obuf[1] = 0xff;
+        olen = 2;
+    }
 
     return olen;
 }
@@ -424,15 +439,23 @@  static void adb_keyboard_event(DeviceState *dev, QemuConsole *src,
         return;
     }
     keycode = qcode_to_adb_keycode[qcode];
-    if (keycode == NO_KEY) {  /* We don't want to send this to the guest */
-        return;
-    }
 
-    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) {
+        printf("Power Key detected\n");
+        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);
+        }
+    } else if (keycode == NO_KEY) { /* NO_KEY shouldn't be sent to guest */
+        return;
+    } else {   /* For all non-power keys - safe for 8-bit keycodes */
+        if (evt->u.key.data->down == false) { /* if key release event */
+            keycode = keycode | 0x80;   /* create keyboard break code */
+        }
+        adb_kbd_put_keycode(s, keycode);
     }
-
-    adb_kbd_put_keycode(s, keycode);
 }
 
 static const VMStateDescription vmstate_adb_kbd = {