diff mbox

[2/4] vnc: Support for LED state extension

Message ID 1366867752-11578-3-git-send-email-lilei@linux.vnet.ibm.com
State New
Headers show

Commit Message

Lei Li April 25, 2013, 5:29 a.m. UTC
Signed-off-by: Lei Li <lilei@linux.vnet.ibm.com>
---
 ui/vnc.c |   45 +++++++++++++++++++++++++++++++++++++++++++++
 ui/vnc.h |    3 +++
 2 files changed, 48 insertions(+), 0 deletions(-)

Comments

Gerd Hoffmann May 14, 2013, 11:35 a.m. UTC | #1
On 04/25/13 07:29, Lei Li wrote:
> +    /* Sending the current led state message to the client */
> +    if (ledstate != current_led_state(vs)) {
> +        vnc_led_state_change(vs);
> +    }

This check never becomes true as the vnc modifier/led state just got
updated to match ledstate ...

cheers,
  Gerd
Lei Li May 15, 2013, 6:05 a.m. UTC | #2
On 05/14/2013 07:35 PM, Gerd Hoffmann wrote:
> On 04/25/13 07:29, Lei Li wrote:
>> +    /* Sending the current led state message to the client */
>> +    if (ledstate != current_led_state(vs)) {
>> +        vnc_led_state_change(vs);
>> +    }
> This check never becomes true as the vnc modifier/led state just got
> updated to match ledstate ...

Oh... then how about get rid of this check, let vnc_led_state_change send
the current led state message directly?

>
> cheers,
>    Gerd
>
Gerd Hoffmann May 15, 2013, 6:44 a.m. UTC | #3
On 05/15/13 08:05, Lei Li wrote:
> On 05/14/2013 07:35 PM, Gerd Hoffmann wrote:
>> On 04/25/13 07:29, Lei Li wrote:
>>> +    /* Sending the current led state message to the client */
>>> +    if (ledstate != current_led_state(vs)) {
>>> +        vnc_led_state_change(vs);
>>> +    }
>> This check never becomes true as the vnc modifier/led state just got
>> updated to match ledstate ...
> 
> Oh... then how about get rid of this check, let vnc_led_state_change send
> the current led state message directly?

Only sending an update if something did actually change makes sense I
think, but you need to check before updating the state, i.e. something
like this:

  bool has_changed = ledstate != current_led_state(vs);

  [ update modifiers/led state ]

  if (has_changed)
     vnc_led_state_change()

cheers,
  Gerd
Laszlo Ersek May 15, 2013, 6:46 a.m. UTC | #4
On 05/15/13 08:05, Lei Li wrote:
> On 05/14/2013 07:35 PM, Gerd Hoffmann wrote:
>> On 04/25/13 07:29, Lei Li wrote:
>>> +    /* Sending the current led state message to the client */
>>> +    if (ledstate != current_led_state(vs)) {
>>> +        vnc_led_state_change(vs);
>>> +    }
>> This check never becomes true as the vnc modifier/led state just got
>> updated to match ledstate ...
> 
> Oh... then how about get rid of this check, let vnc_led_state_change send
> the current led state message directly?

You could do the comparison and save the result before updating the
modifiers array, and call vnc_led_state_change() in its current
location, dependent on the saved comparison result. Just my two cents.

Laszlo
Lei Li May 15, 2013, 7:18 a.m. UTC | #5
On 05/15/2013 02:44 PM, Gerd Hoffmann wrote:
> On 05/15/13 08:05, Lei Li wrote:
>> On 05/14/2013 07:35 PM, Gerd Hoffmann wrote:
>>> On 04/25/13 07:29, Lei Li wrote:
>>>> +    /* Sending the current led state message to the client */
>>>> +    if (ledstate != current_led_state(vs)) {
>>>> +        vnc_led_state_change(vs);
>>>> +    }
>>> This check never becomes true as the vnc modifier/led state just got
>>> updated to match ledstate ...
>> Oh... then how about get rid of this check, let vnc_led_state_change send
>> the current led state message directly?
> Only sending an update if something did actually change makes sense I
> think, but you need to check before updating the state, i.e. something
> like this:
>
>    bool has_changed = ledstate != current_led_state(vs);
>
>    [ update modifiers/led state ]
>
>    if (has_changed)
>       vnc_led_state_change()

I see. Thanks for your very good suggestions!
I'll send the fix soon.

> cheers,
>    Gerd
>
>
diff mbox

Patch

diff --git a/ui/vnc.c b/ui/vnc.c
index f574962..44189d7 100644
--- a/ui/vnc.c
+++ b/ui/vnc.c
@@ -1522,6 +1522,42 @@  static void press_key(VncState *vs, int keysym)
     kbd_put_keycode(keycode | SCANCODE_UP);
 }
 
+static int current_led_state(VncState *vs)
+{
+    int ledstate = 0;
+
+    if (vs->modifiers_state[0x46]) {
+        ledstate |= QEMU_SCROLL_LOCK_LED;
+    }
+    if (vs->modifiers_state[0x45]) {
+        ledstate |= QEMU_NUM_LOCK_LED;
+    }
+    if (vs->modifiers_state[0x3a]) {
+        ledstate |= QEMU_CAPS_LOCK_LED;
+    }
+
+    return ledstate;
+}
+
+static void vnc_led_state_change(VncState *vs)
+{
+    int ledstate = 0;
+
+    if (!vnc_has_feature(vs, VNC_FEATURE_LED_STATE)) {
+        return;
+    }
+
+    ledstate = current_led_state(vs);
+    vnc_lock_output(vs);
+    vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE);
+    vnc_write_u8(vs, 0);
+    vnc_write_u16(vs, 1);
+    vnc_framebuffer_update(vs, 0, 0, 1, 1, VNC_ENCODING_LED_STATE);
+    vnc_write_u8(vs, ledstate);
+    vnc_unlock_output(vs);
+    vnc_flush(vs);
+}
+
 static void kbd_leds(void *opaque, int ledstate)
 {
     VncState *vs = opaque;
@@ -1540,6 +1576,11 @@  static void kbd_leds(void *opaque, int ledstate)
     if (vs->modifiers_state[0x46] != scr) {
         vs->modifiers_state[0x46] = scr;
     }
+
+    /* Sending the current led state message to the client */
+    if (ledstate != current_led_state(vs)) {
+        vnc_led_state_change(vs);
+    }
 }
 
 static void do_key_event(VncState *vs, int down, int keycode, int sym)
@@ -1893,6 +1934,9 @@  static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings)
         case VNC_ENCODING_WMVi:
             vs->features |= VNC_FEATURE_WMVI_MASK;
             break;
+        case VNC_ENCODING_LED_STATE:
+            vs->features |= VNC_FEATURE_LED_STATE_MASK;
+            break;
         case VNC_ENCODING_COMPRESSLEVEL0 ... VNC_ENCODING_COMPRESSLEVEL0 + 9:
             vs->tight.compression = (enc & 0x0F);
             break;
@@ -1908,6 +1952,7 @@  static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings)
     }
     vnc_desktop_resize(vs);
     check_pointer_type_change(&vs->mouse_mode_notifier, NULL);
+    vnc_led_state_change(vs);
 }
 
 static void set_pixel_conversion(VncState *vs)
diff --git a/ui/vnc.h b/ui/vnc.h
index ad1dec2..fea39ad 100644
--- a/ui/vnc.h
+++ b/ui/vnc.h
@@ -384,6 +384,7 @@  enum {
 #define VNC_ENCODING_EXT_KEY_EVENT        0XFFFFFEFE /* -258 */
 #define VNC_ENCODING_AUDIO                0XFFFFFEFD /* -259 */
 #define VNC_ENCODING_TIGHT_PNG            0xFFFFFEFC /* -260 */
+#define VNC_ENCODING_LED_STATE            0XFFFFFEFB /* -261 */
 #define VNC_ENCODING_WMVi                 0x574D5669
 
 /*****************************************************************************
@@ -422,6 +423,7 @@  enum {
 #define VNC_FEATURE_TIGHT_PNG                8
 #define VNC_FEATURE_ZRLE                     9
 #define VNC_FEATURE_ZYWRLE                  10
+#define VNC_FEATURE_LED_STATE               11
 
 #define VNC_FEATURE_RESIZE_MASK              (1 << VNC_FEATURE_RESIZE)
 #define VNC_FEATURE_HEXTILE_MASK             (1 << VNC_FEATURE_HEXTILE)
@@ -434,6 +436,7 @@  enum {
 #define VNC_FEATURE_TIGHT_PNG_MASK           (1 << VNC_FEATURE_TIGHT_PNG)
 #define VNC_FEATURE_ZRLE_MASK                (1 << VNC_FEATURE_ZRLE)
 #define VNC_FEATURE_ZYWRLE_MASK              (1 << VNC_FEATURE_ZYWRLE)
+#define VNC_FEATURE_LED_STATE_MASK           (1 << VNC_FEATURE_LED_STATE)
 
 
 /* Client -> Server message IDs */