]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
util: add virtkeycode module
authorLai Jiangshan <laijs@cn.fujitsu.com>
Thu, 21 Jul 2011 07:32:34 +0000 (15:32 +0800)
committerDaniel Veillard <veillard@redhat.com>
Thu, 21 Jul 2011 07:57:47 +0000 (15:57 +0800)
Add virtkey lib for usage-improvment and keycode translating.
Add 4 internal API for the aim

const char *virKeycodeSetTypeToString(int codeset);
int virKeycodeSetTypeFromString(const char *name);
int virKeycodeValueFromString(virKeycodeSet codeset, const char *keyname);
int virKeycodeValueTranslate(virKeycodeSet from_codeset,
                             virKeycodeSet to_offset,
                             int key_value);

* include/libvirt/libvirt.h.in: extend virKeycodeSet enum
* src/Makefile.am: add new virtkeycode module and rule to generate
  virkeymaps.h
* src/util/virkeycode.c src/util/virkeycode.h: new module
* src/util/virkeycode-mapgen.py: python generator for virkeymaps.h
  out of keymaps.csv
* src/libvirt_private.syms: extend private symbols for new module
* .gitignore: add generated virkeymaps.h

.gitignore
include/libvirt/libvirt.h.in
src/Makefile.am
src/libvirt_private.syms
src/util/virkeycode-mapgen.py [new file with mode: 0644]
src/util/virkeycode.c [new file with mode: 0644]
src/util/virkeycode.h [new file with mode: 0644]

index 486e1028eae4364474787268f6c2a9e3e6533666..dd64ca59d3c3b63c4bf019d72945197ee35f6304 100644 (file)
@@ -55,6 +55,7 @@
 /src/remote/*_client_bodies.h
 /src/remote/*_protocol.[ch]
 /src/rpc/virnetprotocol.[ch]
+/src/util/virkeymaps.h
 /tests/*.log
 /tests/cputest
 /tests/hashtest
index 40ce0fc670423f922a9c4f4ef0495f9a4a7739b7..6afd59160ee9180809b6d61a84e53eb260f97c60 100644 (file)
@@ -1788,6 +1788,12 @@ typedef enum {
     VIR_KEYCODE_SET_ATSET1         = 2,
     VIR_KEYCODE_SET_ATSET2         = 3,
     VIR_KEYCODE_SET_ATSET3         = 4,
+    VIR_KEYCODE_SET_OSX            = 5,
+    VIR_KEYCODE_SET_XT_KBD         = 6,
+    VIR_KEYCODE_SET_USB            = 7,
+    VIR_KEYCODE_SET_WIN32          = 8,
+
+    VIR_KEYCODE_SET_LAST,
 } virKeycodeSet;
 
 /**
index 2a6b0e43c37d42f7c2f3af35ab23f373a83437f4..fadde28a6c2f19746142565055a974a76e1db4e6 100644 (file)
@@ -81,7 +81,18 @@ UTIL_SOURCES =                                                       \
                util/util.c util/util.h                         \
                util/viraudit.c util/viraudit.h                 \
                util/xml.c util/xml.h                           \
-               util/virterror.c util/virterror_internal.h
+               util/virterror.c util/virterror_internal.h      \
+               util/virkeycode.c util/virkeycode.h             \
+               util/virkeymaps.h
+
+EXTRA_DIST += $(srcdir)/util/virkeymaps.h $(srcdir)/util/keymaps.csv \
+               $(srcdir)/util/virkeycode-mapgen.py
+
+$(srcdir)/util/virkeymaps.h: $(srcdir)/util/keymaps.csv        \
+               $(srcdir)/util/virkeycode-mapgen.py
+       python $(srcdir)/util/virkeycode-mapgen.py <$(srcdir)/util/keymaps.csv >$@
+
+$(srcdir)/util/virkeycode.c: $(srcdir)/util/virkeycode.h $(srcdir)/util/virkeymaps.h
 
 EXTRA_DIST += util/threads-pthread.c util/threads-win32.c
 
index 3e3b1dd8d83622a583a05c87ac9e4ea02512dd7d..e9ae0181e0ec2d205404452023c006a6a90c3d38 100644 (file)
@@ -1097,6 +1097,11 @@ virSetError;
 virSetErrorLogPriorityFunc;
 virStrerror;
 
+# virkeycode.h
+virKeycodeSetTypeToString;
+virKeycodeSetTypeFromString;
+virKeycodeValueFromString;
+virKeycodeValueTranslate;
 
 # xml.h
 virXMLParseFileHelper;
diff --git a/src/util/virkeycode-mapgen.py b/src/util/virkeycode-mapgen.py
new file mode 100644 (file)
index 0000000..f0a4290
--- /dev/null
@@ -0,0 +1,44 @@
+#!/bin/python
+
+"""
+Generate the big keycodes table for virkeys.
+It read keymaps.csv from stdin and put the generated code to stdout.
+
+Please keep keymaps.csv be exactly the same as:
+http://git.gnome.org/browse/gtk-vnc/plain/src/keymaps.csv.
+If anything inconsistent happens, please change this file
+instead of keymaps.csv which is a mirror.
+"""
+
+import sys
+import re
+
+namecolums = (0,2,10)
+
+def quotestring(str):
+    if str[0] != '"':
+        return '"' + str + '"'
+    return str
+
+print '''
+/* Generated file, DON'T edit it */
+
+#ifndef VIRT_KEY_INTERNAL
+# error do not use this; it is not a public header
+#endif
+
+struct keycode virKeycodes[] = {
+'''
+
+sys.stdin.readline() # eat the fist line.
+
+for line in sys.stdin.xreadlines():
+    a = re.match("([^,]*)," * 13 + "([^,]*)$", line[0:-1]).groups()
+    b = ""
+    for i in namecolums:
+        b = b + (a[i] and quotestring(a[i]) or 'NULL') + ','
+    for i in [ x for x in range(12) if not x in namecolums ]:
+        b = b + (a[i] or '0') + ','
+    print "    { " + b + "},"
+
+print '};'
diff --git a/src/util/virkeycode.c b/src/util/virkeycode.c
new file mode 100644 (file)
index 0000000..4d54060
--- /dev/null
@@ -0,0 +1,143 @@
+/*
+ * Copyright (c) 2011 Lai Jiangshan
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ *
+ */
+
+#include <config.h>
+#include "virkeycode.h"
+#include <string.h>
+#include <stddef.h>
+
+#define getfield(object, field_type, field_offset) \
+    (*(typeof(field_type) *)((char *)(object) + field_offset))
+
+struct keycode {
+    const char *linux_name;
+    const char *os_x_name;
+    const char *win32_name;
+    unsigned short linux_keycode;
+    unsigned short os_x;
+    unsigned short atset1;
+    unsigned short atset2;
+    unsigned short atset3;
+    unsigned short xt;
+    unsigned short xt_kbd;
+    unsigned short usb;
+    unsigned short win32;
+};
+
+#define VIRT_KEY_INTERNAL
+#include "virkeymaps.h"
+
+static unsigned int codeOffset[] = {
+    [VIR_KEYCODE_SET_LINUX] =
+        offsetof(struct keycode, linux_keycode),
+    [VIR_KEYCODE_SET_XT] =
+        offsetof(struct keycode, xt),
+    [VIR_KEYCODE_SET_ATSET1] =
+        offsetof(struct keycode, atset1),
+    [VIR_KEYCODE_SET_ATSET2] =
+        offsetof(struct keycode, atset2),
+    [VIR_KEYCODE_SET_ATSET3] =
+        offsetof(struct keycode, atset3),
+    [VIR_KEYCODE_SET_OSX] =
+        offsetof(struct keycode, os_x),
+    [VIR_KEYCODE_SET_XT_KBD] =
+        offsetof(struct keycode, xt_kbd),
+    [VIR_KEYCODE_SET_USB] =
+        offsetof(struct keycode, usb),
+    [VIR_KEYCODE_SET_WIN32] =
+        offsetof(struct keycode, win32),
+};
+
+VIR_ENUM_IMPL(virKeycodeSet, VIR_KEYCODE_SET_LAST,
+    "linux",
+    "xt",
+    "atset1",
+    "atset2",
+    "atset3",
+    "os_x",
+    "xt_kbd",
+    "usb",
+    "win32",
+);
+
+static int __virKeycodeValueFromString(unsigned int name_offset,
+                                           unsigned int code_offset,
+                                           const char *keyname)
+{
+    int i;
+
+    for (i = 0; i < ARRAY_CARDINALITY(virKeycodes); i++) {
+        const char *name = getfield(virKeycodes + i, const char *, name_offset);
+
+        if (name && STREQ(name, keyname))
+            return getfield(virKeycodes + i, unsigned short, code_offset);
+    }
+
+    return -1;
+}
+
+int virKeycodeValueFromString(virKeycodeSet codeset, const char *keyname)
+{
+    switch (codeset) {
+    case VIR_KEYCODE_SET_LINUX:
+        return __virKeycodeValueFromString(offsetof(struct keycode, linux_name),
+                                           offsetof(struct keycode, linux_keycode),
+                                           keyname);
+    case VIR_KEYCODE_SET_OSX:
+        return __virKeycodeValueFromString(offsetof(struct keycode, os_x_name),
+                                           offsetof(struct keycode, os_x),
+                                           keyname);
+    case VIR_KEYCODE_SET_WIN32:
+        return __virKeycodeValueFromString(offsetof(struct keycode, win32_name),
+                                           offsetof(struct keycode, win32),
+                                           keyname);
+    default:
+        return -1;
+    }
+}
+
+static int __virKeycodeValueTranslate(unsigned int from_offset,
+                                      unsigned int to_offset,
+                                      int key_value)
+{
+    int i;
+
+    for (i = 0; ARRAY_CARDINALITY(virKeycodes); i++) {
+        if (getfield(virKeycodes + i, unsigned short, from_offset) == key_value)
+            return getfield(virKeycodes + i, unsigned short, to_offset);
+    }
+
+    return -1;
+}
+
+int virKeycodeValueTranslate(virKeycodeSet from_codeset,
+                             virKeycodeSet to_codeset,
+                             int key_value)
+{
+    if (key_value <= 0)
+        return -1;
+
+    key_value = __virKeycodeValueTranslate(codeOffset[from_codeset],
+                                           codeOffset[to_codeset],
+                                           key_value);
+    if (key_value <= 0)
+        return -1;
+
+    return key_value;
+}
diff --git a/src/util/virkeycode.h b/src/util/virkeycode.h
new file mode 100644 (file)
index 0000000..17e1e9d
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * virkeycode.h: keycodes definitions and declarations
+ *
+ * Copyright (c) 2011 Lai Jiangshan
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ *
+ */
+
+#ifndef __VIR_UTIL_VIRTKEYCODE_H__
+# define __VIR_UTIL_VIRTKEYCODE_H__
+
+# include "util.h"
+# include "libvirt/libvirt.h"
+
+VIR_ENUM_DECL(virKeycodeSet);
+int virKeycodeValueFromString(virKeycodeSet codeset, const char *keyname);
+int virKeycodeValueTranslate(virKeycodeSet from_codeset,
+                        virKeycodeSet to_offset,
+                        int key_value);
+
+#endif