[Spice-devel] [PATCH spice-gtk 1/5] util-priv: factor out spice_make_scancode()

Marc-André Lureau marcandre.lureau at gmail.com
Thu Aug 16 08:35:05 PDT 2012


Factor out the keyboard scancode manipulation function, to be reusable
by newer code.
---
 gtk/channel-inputs.c  |   22 ++++------------------
 gtk/spice-util-priv.h |    1 +
 gtk/spice-util.c      |   21 +++++++++++++++++++++
 3 files changed, 26 insertions(+), 18 deletions(-)

diff --git a/gtk/channel-inputs.c b/gtk/channel-inputs.c
index 02ac026..3259da8 100644
--- a/gtk/channel-inputs.c
+++ b/gtk/channel-inputs.c
@@ -483,15 +483,8 @@ void spice_inputs_key_press(SpiceInputsChannel *channel, guint scancode)
     if (spice_channel_get_read_only(SPICE_CHANNEL(channel)))
         return;
 
-    SPICE_DEBUG("%s: scancode %d", __FUNCTION__, scancode);
-    if (scancode < 0x100) {
-        down.code = scancode;
-    } else {
-        down.code = 0xe0 | ((scancode - 0x100) << 8);
-    }
-
-    msg = spice_msg_out_new(SPICE_CHANNEL(channel),
-                            SPICE_MSGC_INPUTS_KEY_DOWN);
+    down.code = spice_make_scancode(scancode, FALSE);
+    msg = spice_msg_out_new(SPICE_CHANNEL(channel), SPICE_MSGC_INPUTS_KEY_DOWN);
     msg->marshallers->msgc_inputs_key_down(msg->marshaller, &down);
     spice_msg_out_send(msg);
 }
@@ -515,15 +508,8 @@ void spice_inputs_key_release(SpiceInputsChannel *channel, guint scancode)
     if (spice_channel_get_read_only(SPICE_CHANNEL(channel)))
         return;
 
-    SPICE_DEBUG("%s: scancode %d", __FUNCTION__, scancode);
-    if (scancode < 0x100) {
-        up.code = scancode | 0x80;
-    } else {
-        up.code = 0x80e0 | ((scancode - 0x100) << 8);
-    }
-
-    msg = spice_msg_out_new(SPICE_CHANNEL(channel),
-                            SPICE_MSGC_INPUTS_KEY_UP);
+    up.code = spice_make_scancode(scancode, TRUE);
+    msg = spice_msg_out_new(SPICE_CHANNEL(channel), SPICE_MSGC_INPUTS_KEY_UP);
     msg->marshallers->msgc_inputs_key_up(msg->marshaller, &up);
     spice_msg_out_send(msg);
 }
diff --git a/gtk/spice-util-priv.h b/gtk/spice-util-priv.h
index c2022a3..ee5a42d 100644
--- a/gtk/spice-util-priv.h
+++ b/gtk/spice-util-priv.h
@@ -28,6 +28,7 @@ G_BEGIN_DECLS
 gboolean spice_strv_contains(const GStrv strv, const gchar *str);
 gchar* spice_uuid_to_string(const guint8 uuid[16]);
 const gchar* spice_yes_no(gboolean value);
+guint16 spice_make_scancode(guint scancode, gboolean release);
 
 #if GLIB_CHECK_VERSION(2,32,0)
 #define STATIC_MUTEX            GMutex
diff --git a/gtk/spice-util.c b/gtk/spice-util.c
index 8f4f1dc..774a145 100644
--- a/gtk/spice-util.c
+++ b/gtk/spice-util.c
@@ -224,3 +224,24 @@ const gchar* spice_yes_no(gboolean value)
 {
     return value ? "yes" : "no";
 }
+
+G_GNUC_INTERNAL
+guint16 spice_make_scancode(guint scancode, gboolean release)
+{
+    SPICE_DEBUG("%s: %s scancode %d",
+                __FUNCTION__, release ? "release" : "", scancode);
+
+    if (release) {
+        if (scancode < 0x100)
+            return scancode | 0x80;
+        else
+            return 0x80e0 | ((scancode - 0x100) << 8);
+    } else {
+        if (scancode < 0x100)
+            return scancode;
+        else
+            return 0xe0 | ((scancode - 0x100) << 8);
+    }
+
+    g_return_val_if_reached(0);
+}
-- 
1.7.10.4



More information about the Spice-devel mailing list