Initial commit

This is a minimal subset of libevdev functions needed on OpenBSD
by Wayland applications.
This commit is contained in:
Matthieu Herrb 2023-07-06 11:00:51 +02:00
commit ee36daecae
6 changed files with 1898 additions and 0 deletions

58
Makefile Normal file
View file

@ -0,0 +1,58 @@
# $OpenBSD$
INSTALL_DATA= /usr/bin/install -c -m 644
NOPROFILE=
LIB= evdev
PREFIX= /usr/local
INCSDIR= ${PREFIX}/include
LIBDIR= ${PREFIX}/lib
CPPFLAGS+= -I${.CURDIR} \
-I${INCSDIR} \
INCS= libevdev.h
SRCS= libevdev.c
PKGCONFIG= libevdev.pc
includes: _SUBDIRUSE
mkdir -p ${DESTDIR}${INCSDIR}/libevdev
cd ${.CURDIR}; for i in ${INCS}; do \
j="cmp -s $$i ${DESTDIR}${INCSDIR}/$$i || \
${INSTALL_DATA} $$i ${DESTDIR}${INCSDIR}/libevdev"; \
echo "\tinstalling $$i"; \
eval "$$j"; \
done
# pkgconfig
PACKAGE_VERSION = 1.13.1
PKGCONFIG = libevdev.pc
.SUFFIXES: .in .pc
all: ${PKGCONFIG}
CLEANFILES += ${PKGCONFIG}
${PKGCONFIG}: ${PKGCONFIG}.in
@sed -e 's#@prefix@#${PREFIX}#g' \
-e 's#@datarootdir@#$${prefix}/share#g' \
-e 's#@datadir@#$${datarootdir}#g' \
-e 's#@exec_prefix@#$${prefix}#g' \
-e 's#@libdir@#$${exec_prefix}/lib#g' \
-e 's#@includedir@#$${prefix}/include#g' \
-e 's#@PACKAGE_VERSION@#'${PACKAGE_VERSION}'#g' \
${EXTRA_PKGCONFIG_SUBST} \
< $? > $@
install-pc: ${PKGCONFIG}
${INSTALL_DATA} ${PKGCONFIG} ${DESTDIR}${LIBDIR}/pkgconfig
realinstall: install-pc
.include <bsd.obj.mk>
.include <bsd.lib.mk>

1656
event-names.h Normal file

File diff suppressed because it is too large Load diff

142
libevdev.c Normal file
View file

@ -0,0 +1,142 @@
/*
* Copyright © 2013 David Herrmann <dh.herrmann@gmail.com>
* Copyright © 2013 Red Hat, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <libevdev/libevdev.h>
#include <linux/input.h>
#include "event-names.h"
#define ARRAY_LENGTH(a) (sizeof(a) / (sizeof((a)[0])))
struct name_lookup {
const char *name;
size_t len;
};
int
libevdev_event_type_get_max(unsigned int type)
{
if (type > EV_MAX)
return -1;
return ev_max[type];
}
const char *
libevdev_event_code_get_name(unsigned int type, unsigned int code)
{
int max = libevdev_event_type_get_max(type);
if (max == -1 || code > (unsigned int)max)
return NULL;
return event_type_map[type][code];
}
static inline bool
startswith(const char *str, size_t len, const char *prefix, size_t plen)
{
return len >= plen && !strncmp(str, prefix, plen);
}
static int type_from_prefix(const char *name, ssize_t len)
{
const char *e;
size_t i;
ssize_t l;
/* MAX_ is not allowed, even though EV_MAX exists */
if (startswith(name, len, "MAX_", 4))
return -1;
/* BTN_ is special as there is no EV_BTN type */
if (startswith(name, len, "BTN_", 4))
return EV_KEY;
/* FF_STATUS_ is special as FF_ is a prefix of it, so test it first */
if (startswith(name, len, "FF_STATUS_", 10))
return EV_FF_STATUS;
for (i = 0; i < ARRAY_LENGTH(ev_names); ++i) {
/* skip EV_ prefix so @e is suffix of [EV_]XYZ */
e = &ev_names[i].name[3];
l = strlen(e);
/* compare prefix and test for trailing _ */
if (len > l && startswith(name, len, e, l) && name[l] == '_')
return ev_names[i].value;
}
return -1;
}
static int cmp_entry(const void *vlookup, const void *ventry)
{
const struct name_lookup *lookup = vlookup;
const struct name_entry *entry = ventry;
int r;
r = strncmp(lookup->name, entry->name, lookup->len);
if (!r) {
if (entry->name[lookup->len])
r = -1;
else
r = 0;
}
return r;
}
static const struct name_entry*
lookup_name(const struct name_entry *array, size_t asize,
struct name_lookup *lookup)
{
const struct name_entry *entry;
entry = bsearch(lookup, array, asize, sizeof(*array), cmp_entry);
if (!entry)
return NULL;
return entry;
}
int
libevdev_event_code_from_name(unsigned int type, const char *name)
{
struct name_lookup lookup;
const struct name_entry *entry;
int real_type;
size_t len = strlen(name);
real_type = type_from_prefix(name, len);
if (real_type < 0 || (unsigned int)real_type != type)
return -1;
lookup.name = name;
lookup.len = len;
entry = lookup_name(code_names, ARRAY_LENGTH(code_names), &lookup);
return entry ? (int)entry->value : -1;
}

11
libevdev.pc.in Normal file
View file

@ -0,0 +1,11 @@
prefix=@prefix@
exec_prefix=@exec_prefix@
datarootdir=@datarootdir@
libdir=@libdir@
includedir=@includedir@
Name: Libevdev
Description: Event device library
Version: @PACKAGE_VERSION@
Cflags: -I${includedir}
Libs: -L${libdir} -levdev

29
libevdev/libevdev.h Normal file
View file

@ -0,0 +1,29 @@
/*
* Copyright © 2013 Red Hat, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
*/
#ifndef LIBEVDEV_H
#define LIBEVDEV_H
extern int libevdev_event_code_from_name(unsigned int type, const char *name);
extern const char * libevdev_event_code_get_name(unsigned int type, unsigned int code);
#endif

2
shlib_version Normal file
View file

@ -0,0 +1,2 @@
major=0
minor=0