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 virParseKeyName(virKeycodeSet codeset, const char *keyname); int virTranslateKeyCode(virKeycodeSet from_codeset, virKeycodeSet to_offset, int key_value); Signed-off-by: Lai Jiangshan <laijs@xxxxxxxxxxxxxx> --- include/libvirt/libvirt.h.in | 8 +++ src/Makefile.am | 11 ++++- src/libvirt_private.syms | 5 ++ src/util/virtkey.c | 117 ++++++++++++++++++++++++++++++++++++++++++ src/util/virtkey.h | 41 +++++++++++++++ src/util/virtkeymap-gen.py | 47 +++++++++++++++++ 6 files changed, 228 insertions(+), 1 deletions(-) create mode 100644 src/util/virtkey.c create mode 100644 src/util/virtkey.h create mode 100644 src/util/virtkeymap-gen.py diff --git a/include/libvirt/libvirt.h.in b/include/libvirt/libvirt.h.in index d5a7105..acfe9d9 100644 --- a/include/libvirt/libvirt.h.in +++ b/include/libvirt/libvirt.h.in @@ -1778,6 +1778,14 @@ 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_XWIN_XT = 9, + VIR_KEYCODE_SET_XFREE86_KBD_XT = 10, + + VIR_KEYCODE_SET_LAST, } virKeycodeSet; /** diff --git a/src/Makefile.am b/src/Makefile.am index 39f0cf8..90b0743 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -81,7 +81,16 @@ 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/virtkey.c util/virtkey.h \ + util/virtkeymaps.c + +EXTRA_DIST += $(srcdir)/util/virtkeymaps.c $(srcdir)/util/keymaps.csv \ + $(srcdir)/util/virtkeymap-gen.py + +$(srcdir)/util/virtkeymaps.c: $(srcdir)/util/keymaps.csv \ + $(srcdir)/util/virtkeymap-gen.py + python $(srcdir)/util/virtkeymap-gen.py <$(srcdir)/util/keymaps.csv >$@ EXTRA_DIST += util/threads-pthread.c util/threads-win32.c diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 3237d18..6611471 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1094,6 +1094,11 @@ virSetError; virSetErrorLogPriorityFunc; virStrerror; +# virtkey.h +virKeycodeSetTypeToString; +virKeycodeSetTypeFromString; +virParseKeyName; +virTranslateKeyCode; # xml.h virXMLParseFileHelper; diff --git a/src/util/virtkey.c b/src/util/virtkey.c new file mode 100644 index 0000000..1eb3c62 --- /dev/null +++ b/src/util/virtkey.c @@ -0,0 +1,117 @@ + +/* + * Copyright (c) 2011 Lai Jiangshan + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published by + * the Free Software Foundation. + */ + +#include "virtkey.h" +#include <string.h> +#include <stddef.h> + +#define getfield(object, field_type, field_offset) \ + (*(typeof(field_type) *)((char *)(object) + field_offset)) + +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_KEYCODE_SET_XWIN_XT] = + offsetof(struct keycode, xwin_xt), + [VIR_KEYCODE_SET_XFREE86_KBD_XT] = + offsetof(struct keycode, xfree86_kbd_xt), +}; + +VIR_ENUM_IMPL(virKeycodeSet, VIR_KEYCODE_SET_LAST, + "linux", + "xt", + "atset1", + "atset2", + "atset3", + "os_x", + "xt_kbd", + "usb", + "win32", + "xwin_xt", + "xfree86_kbd_xt"); + +static int virParseKeyNameOffset(unsigned int name_offset, + unsigned int code_offset, + const char *keyname) +{ + int i; + + for (i = 0; i < virtKeycodesSize; i++) { + const char *name = getfield(virtKeycodes + i, const char *, name_offset); + + if (name && !strcmp(name, keyname)) + return getfield(virtKeycodes + i, unsigned short, code_offset); + } + + return -1; +} + +int virParseKeyName(virKeycodeSet codeset, const char *keyname) +{ + switch (codeset) { + case VIR_KEYCODE_SET_LINUX: + return virParseKeyNameOffset(offsetof(struct keycode, linux_name), + offsetof(struct keycode, linux_keycode), keyname); + case VIR_KEYCODE_SET_OSX: + return virParseKeyNameOffset(offsetof(struct keycode, os_x_name), + offsetof(struct keycode, os_x), keyname); + case VIR_KEYCODE_SET_WIN32: + return virParseKeyNameOffset(offsetof(struct keycode, win32_name), + offsetof(struct keycode, win32), keyname); + default: + return -1; + } +} + +static int virTranslateKeyCodeOffset(unsigned int from_offset, + unsigned int to_offset, + int key_value) +{ + int i; + + for (i = 0; i < virtKeycodesSize; i++) { + if (getfield(virtKeycodes + i, unsigned short, from_offset) == key_value) + return getfield(virtKeycodes + i, unsigned short, to_offset); + } + + return -1; +} + +int virTranslateKeyCode(virKeycodeSet from_codeset, + virKeycodeSet to_codeset, + int key_value) +{ + if (key_value <= 0) + return -1; + + key_value = virTranslateKeyCodeOffset(codeOffset[from_codeset], + codeOffset[to_codeset], + key_value); + if (key_value <= 0) + return -1; + + return key_value; +} + diff --git a/src/util/virtkey.h b/src/util/virtkey.h new file mode 100644 index 0000000..45c5f08 --- /dev/null +++ b/src/util/virtkey.h @@ -0,0 +1,41 @@ +#ifndef __UTIL_VIRTKEY_H__ +#define __UTIL_VIRTKEY_H__ + +/* + * Copyright (c) 2011 Lai Jiangshan + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published by + * the Free Software Foundation. + */ + +#include <config.h> +#include "util.h" +#include "libvirt/libvirt.h" + +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; + unsigned short xwin_xt; + unsigned short xfree86_kbd_xt; +}; +extern struct keycode virtKeycodes[]; +extern int virtKeycodesSize; + +VIR_ENUM_DECL(virKeycodeSet); +int virParseKeyName(virKeycodeSet codeset, const char *keyname); +int virTranslateKeyCode(virKeycodeSet from_codeset, + virKeycodeSet to_offset, + int key_value); + +#endif diff --git a/src/util/virtkeymap-gen.py b/src/util/virtkeymap-gen.py new file mode 100644 index 0000000..69ddec1 --- /dev/null +++ b/src/util/virtkeymap-gen.py @@ -0,0 +1,47 @@ +#!/bin/python + +""" +Generate the big keycodes table for virtkeys. +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 '#include "virtkey.h"' +print + +print "/* Generated file, DON'T edit it */" +print + +print 'struct keycode virtKeycodes[] = {' + +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(14) if not x in namecolums ]: + b = b + (a[i] or '0') + ',' + print " { " + b + "}," + +print '};' + +print +print 'int virtKeycodesSize = ARRAY_CARDINALITY(virtKeycodes);' +print + -- 1.7.4.4 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list