2 * Copyright © 2013 David Herrmann <dh.herrmann@gmail.com>
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
30 #include "libevdev-int.h"
31 #include "libevdev-util.h"
32 #include "event-names.h"
39 static int cmp_entry(const void *vlookup, const void *ventry)
41 const struct name_lookup *lookup = vlookup;
42 const struct name_entry *entry = ventry;
45 r = strncmp(lookup->name, entry->name, lookup->len);
47 if (entry->name[lookup->len])
56 static const struct name_entry*
57 lookup_name(const struct name_entry *array, size_t asize,
58 struct name_lookup *lookup)
60 const struct name_entry *entry;
62 entry = bsearch(lookup, array, asize, sizeof(*array), cmp_entry);
70 libevdev_event_type_from_name(const char *name)
72 return libevdev_event_type_from_name_n(name, strlen(name));
76 libevdev_event_type_from_name_n(const char *name, size_t len)
78 struct name_lookup lookup;
79 const struct name_entry *entry;
84 entry = lookup_name(ev_names, ARRAY_LENGTH(ev_names), &lookup);
86 return entry ? (int)entry->value : -1;
89 static int type_from_prefix(const char *name, ssize_t len)
95 /* MAX_ is not allowed, even though EV_MAX exists */
96 if (startswith(name, len, "MAX_", 4))
98 /* BTN_ is special as there is no EV_BTN type */
99 if (startswith(name, len, "BTN_", 4))
101 /* FF_STATUS_ is special as FF_ is a prefix of it, so test it first */
102 if (startswith(name, len, "FF_STATUS_", 10))
105 for (i = 0; i < ARRAY_LENGTH(ev_names); ++i) {
106 /* skip EV_ prefix so @e is suffix of [EV_]XYZ */
107 e = &ev_names[i].name[3];
110 /* compare prefix and test for trailing _ */
111 if (len > l && startswith(name, len, e, l) && name[l] == '_')
112 return ev_names[i].value;
119 libevdev_event_code_from_name(unsigned int type, const char *name)
121 return libevdev_event_code_from_name_n(type, name, strlen(name));
125 libevdev_event_code_from_name_n(unsigned int type, const char *name, size_t len)
127 struct name_lookup lookup;
128 const struct name_entry *entry;
131 /* verify that @name is really of type @type */
132 real_type = type_from_prefix(name, len);
133 if (real_type < 0 || (unsigned int)real_type != type)
136 /* now look up the name @name and return the constant */
140 entry = lookup_name(code_names, ARRAY_LENGTH(code_names), &lookup);
142 return entry ? (int)entry->value : -1;