[libvirt] [PATCH 2/4 V5] util: add virtkey
Daniel P. Berrange
berrange at redhat.com
Thu Jul 21 15:46:55 UTC 2011
On Thu, Jul 21, 2011 at 11:26:59AM +0800, Lai Jiangshan wrote:
> 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);
>
> Signed-off-by: Lai Jiangshan <laijs at cn.fujitsu.com>
> ---
> include/libvirt/libvirt.h.in | 6 ++
> src/Makefile.am | 12 +++-
> src/libvirt_private.syms | 5 ++
> src/util/virkey.c | 145 +++++++++++++++++++++++++++++++++++++++++
> src/util/virkeycode-mapgen.py | 45 +++++++++++++
> src/util/virkeycode.h | 35 ++++++++++
> 6 files changed, 247 insertions(+), 1 deletions(-)
> create mode 100644 src/util/virkey.c
> create mode 100644 src/util/virkeycode-mapgen.py
> create mode 100644 src/util/virkeycode.h
Slight mistake there I think. s/virkey.c/virtkeycode.c/
> diff --git a/include/libvirt/libvirt.h.in b/include/libvirt/libvirt.h.in
> index 40ce0fc..6afd591 100644
> --- a/include/libvirt/libvirt.h.in
> +++ b/include/libvirt/libvirt.h.in
> @@ -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;
>
> /**
> diff --git a/src/Makefile.am b/src/Makefile.am
> index 2a6b0e4..87117de 100644
> --- a/src/Makefile.am
> +++ b/src/Makefile.am
> @@ -81,7 +81,17 @@ 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/virkey.c util/virkeycode.h util/virkeymaps.h
Also s/virkey.c/virkeycode.c/ here
> +
> +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/virkey.c: $(srcdir)/util/virkeycode.h $(srcdir)/util/virkeymaps.h
I don't think you need the explicit dep rule here. The automake
rules will already detect that virkeycode.c has a include of
virkeymaps.h and rebuild. You should add virkeymaps.h to the
BUILT_SOURCES variable & MAINTAINERCLEANFILES though
>
> EXTRA_DIST += util/threads-pthread.c util/threads-win32.c
>
> diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
> index 3e3b1dd..e9ae018 100644
> --- a/src/libvirt_private.syms
> +++ b/src/libvirt_private.syms
> @@ -1097,6 +1097,11 @@ virSetError;
> virSetErrorLogPriorityFunc;
> virStrerror;
>
> +# virkeycode.h
> +virKeycodeSetTypeToString;
> +virKeycodeSetTypeFromString;
> +virKeycodeValueFromString;
> +virKeycodeValueTranslate;
>
> # xml.h
> virXMLParseFileHelper;
> diff --git a/src/util/virkey.c b/src/util/virkey.c
> new file mode 100644
> index 0000000..e94768a
> --- /dev/null
> +++ b/src/util/virkey.c
> @@ -0,0 +1,145 @@
> +
> +/*
> + * 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 && !strcmp(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;
> +}
There's no need for the "__" prefix on static functions here.
It just causes code-churn, if we ever decide to then make some of
the functions non-static.
Regards,
Daniel
--
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org -o- http://virt-manager.org :|
|: http://autobuild.org -o- http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
More information about the libvir-list
mailing list