2020-09-17 15:56:41 +10:00
|
|
|
// SPDX-License-Identifier: MIT
|
2013-05-27 14:59:41 +10:00
|
|
|
/*
|
|
|
|
|
* Copyright © 2013 Red Hat, Inc.
|
|
|
|
|
*/
|
|
|
|
|
|
2013-10-08 14:22:27 +10:00
|
|
|
#ifndef LIBEVDEV_INT_H
|
|
|
|
|
#define LIBEVDEV_INT_H
|
2013-05-27 14:59:41 +10:00
|
|
|
|
2020-02-14 10:13:18 +10:00
|
|
|
#include "config.h"
|
2013-06-04 15:44:51 +10:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
2013-10-08 15:16:32 +10:00
|
|
|
#include <stdbool.h>
|
2013-06-04 15:44:51 +10:00
|
|
|
#include <errno.h>
|
2013-05-27 14:59:41 +10:00
|
|
|
#include "libevdev.h"
|
2014-03-06 10:22:20 +10:00
|
|
|
#include "libevdev-util.h"
|
2013-05-27 14:59:41 +10:00
|
|
|
|
|
|
|
|
#define MAX_NAME 256
|
|
|
|
|
#define ABS_MT_MIN ABS_MT_SLOT
|
|
|
|
|
#define ABS_MT_MAX ABS_MT_TOOL_Y
|
|
|
|
|
#define ABS_MT_CNT (ABS_MT_MAX - ABS_MT_MIN + 1)
|
2013-08-29 11:50:09 +10:00
|
|
|
#define LIBEVDEV_EXPORT __attribute__((visibility("default")))
|
2013-08-27 13:33:37 +10:00
|
|
|
#define ALIAS(_to) __attribute__((alias(#_to)))
|
2013-05-27 14:59:41 +10:00
|
|
|
|
2013-07-02 10:50:37 +10:00
|
|
|
/**
|
|
|
|
|
* Sync state machine:
|
|
|
|
|
* default state: SYNC_NONE
|
|
|
|
|
*
|
|
|
|
|
* SYNC_NONE → SYN_DROPPED or forced sync → SYNC_NEEDED
|
2013-09-11 11:04:54 +10:00
|
|
|
* SYNC_NEEDED → libevdev_next_event(LIBEVDEV_READ_FLAG_SYNC) → SYNC_IN_PROGRESS
|
|
|
|
|
* SYNC_NEEDED → libevdev_next_event(LIBEVDEV_READ_FLAG_SYNC_NONE) → SYNC_NONE
|
|
|
|
|
* SYNC_IN_PROGRESS → libevdev_next_event(LIBEVDEV_READ_FLAG_SYNC_NONE) → SYNC_NONE
|
2013-07-02 10:50:37 +10:00
|
|
|
* SYNC_IN_PROGRESS → no sync events left → SYNC_NONE
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
enum SyncState {
|
|
|
|
|
SYNC_NONE,
|
|
|
|
|
SYNC_NEEDED,
|
|
|
|
|
SYNC_IN_PROGRESS,
|
|
|
|
|
};
|
|
|
|
|
|
2014-05-14 16:36:49 +10:00
|
|
|
/**
|
|
|
|
|
* Internal only: log data used to send messages to the respective log
|
|
|
|
|
* handler. We re-use the same struct for a global and inside
|
|
|
|
|
* struct libevdev.
|
|
|
|
|
* For the global, device_handler is NULL, for per-device instance
|
|
|
|
|
* global_handler is NULL.
|
|
|
|
|
*/
|
|
|
|
|
struct logdata {
|
|
|
|
|
enum libevdev_log_priority priority; /** minimum logging priority */
|
|
|
|
|
libevdev_log_func_t global_handler; /** global handler function */
|
|
|
|
|
libevdev_device_log_func_t device_handler; /** per-device handler function */
|
|
|
|
|
void *userdata; /** user-defined data pointer */
|
|
|
|
|
};
|
|
|
|
|
|
2013-05-27 14:59:41 +10:00
|
|
|
struct libevdev {
|
|
|
|
|
int fd;
|
2013-10-08 15:16:32 +10:00
|
|
|
bool initialized;
|
2013-06-03 14:20:34 +10:00
|
|
|
char *name;
|
2013-06-03 13:59:06 +10:00
|
|
|
char *phys;
|
|
|
|
|
char *uniq;
|
2013-05-27 14:59:41 +10:00
|
|
|
struct input_id ids;
|
2013-06-03 13:35:28 +10:00
|
|
|
int driver_version;
|
2013-05-27 14:59:41 +10:00
|
|
|
unsigned long bits[NLONGS(EV_CNT)];
|
|
|
|
|
unsigned long props[NLONGS(INPUT_PROP_CNT)];
|
|
|
|
|
unsigned long key_bits[NLONGS(KEY_CNT)];
|
|
|
|
|
unsigned long rel_bits[NLONGS(REL_CNT)];
|
|
|
|
|
unsigned long abs_bits[NLONGS(ABS_CNT)];
|
|
|
|
|
unsigned long led_bits[NLONGS(LED_CNT)];
|
2013-06-05 16:30:00 +10:00
|
|
|
unsigned long msc_bits[NLONGS(MSC_CNT)];
|
|
|
|
|
unsigned long sw_bits[NLONGS(SW_CNT)];
|
|
|
|
|
unsigned long rep_bits[NLONGS(REP_CNT)]; /* convenience, always 1 */
|
|
|
|
|
unsigned long ff_bits[NLONGS(FF_CNT)];
|
|
|
|
|
unsigned long snd_bits[NLONGS(SND_CNT)];
|
2013-05-27 14:59:41 +10:00
|
|
|
unsigned long key_values[NLONGS(KEY_CNT)];
|
2013-08-14 09:29:41 +10:00
|
|
|
unsigned long led_values[NLONGS(LED_CNT)];
|
2013-08-14 19:52:25 +10:00
|
|
|
unsigned long sw_values[NLONGS(SW_CNT)];
|
2013-05-27 14:59:41 +10:00
|
|
|
struct input_absinfo abs_info[ABS_CNT];
|
2014-02-27 11:34:47 +10:00
|
|
|
int *mt_slot_vals; /* [num_slots * ABS_MT_CNT] */
|
2013-05-27 14:59:41 +10:00
|
|
|
int num_slots; /**< valid slots in mt_slot_vals */
|
2013-05-30 13:58:45 +10:00
|
|
|
int current_slot;
|
2013-07-29 11:45:30 +10:00
|
|
|
int rep_values[REP_CNT];
|
2013-05-27 14:59:41 +10:00
|
|
|
|
2013-07-02 10:50:37 +10:00
|
|
|
enum SyncState sync_state;
|
2013-08-29 11:53:59 +10:00
|
|
|
enum libevdev_grab_mode grabbed;
|
2013-05-27 14:59:41 +10:00
|
|
|
|
|
|
|
|
struct input_event *queue;
|
|
|
|
|
size_t queue_size; /**< size of queue in elements */
|
|
|
|
|
size_t queue_next; /**< next event index */
|
2013-05-30 10:42:24 +10:00
|
|
|
size_t queue_nsync; /**< number of sync events */
|
2013-06-03 16:20:35 +10:00
|
|
|
|
|
|
|
|
struct timeval last_event_time;
|
2014-04-01 15:32:46 +10:00
|
|
|
|
2014-05-14 16:36:49 +10:00
|
|
|
struct logdata log;
|
2013-08-26 13:24:26 +10:00
|
|
|
};
|
|
|
|
|
|
2014-05-14 16:36:49 +10:00
|
|
|
#define log_msg_cond(dev, priority, ...) \
|
2013-08-26 13:24:26 +10:00
|
|
|
do { \
|
2014-08-18 14:03:43 +10:00
|
|
|
if (_libevdev_log_priority(dev) >= priority) \
|
|
|
|
|
_libevdev_log_msg(dev, priority, __FILE__, __LINE__, __func__, __VA_ARGS__); \
|
2013-08-26 13:24:26 +10:00
|
|
|
} while(0)
|
|
|
|
|
|
2014-05-14 16:36:49 +10:00
|
|
|
#define log_error(dev, ...) log_msg_cond(dev, LIBEVDEV_LOG_ERROR, __VA_ARGS__)
|
|
|
|
|
#define log_info(dev, ...) log_msg_cond(dev, LIBEVDEV_LOG_INFO, __VA_ARGS__)
|
|
|
|
|
#define log_dbg(dev, ...) log_msg_cond(dev, LIBEVDEV_LOG_DEBUG, __VA_ARGS__)
|
|
|
|
|
#define log_bug(dev, ...) log_msg_cond(dev, LIBEVDEV_LOG_ERROR, "BUG: "__VA_ARGS__)
|
2013-08-26 13:24:26 +10:00
|
|
|
|
|
|
|
|
extern void
|
2014-08-18 14:03:43 +10:00
|
|
|
_libevdev_log_msg(const struct libevdev *dev,
|
|
|
|
|
enum libevdev_log_priority priority,
|
|
|
|
|
const char *file, int line, const char *func,
|
|
|
|
|
const char *format, ...) LIBEVDEV_ATTRIBUTE_PRINTF(6, 7);
|
2014-05-14 16:36:49 +10:00
|
|
|
extern enum libevdev_log_priority
|
2014-08-18 14:03:43 +10:00
|
|
|
_libevdev_log_priority(const struct libevdev *dev);
|
2013-08-26 13:24:26 +10:00
|
|
|
|
2020-02-11 13:51:17 +10:00
|
|
|
static inline void
|
|
|
|
|
init_event(struct libevdev *dev, struct input_event *ev, int type, int code, int value)
|
|
|
|
|
{
|
|
|
|
|
ev->input_event_sec = dev->last_event_time.tv_sec;
|
|
|
|
|
ev->input_event_usec = dev->last_event_time.tv_usec;
|
|
|
|
|
ev->type = type;
|
|
|
|
|
ev->code = code;
|
|
|
|
|
ev->value = value;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-30 11:05:24 +10:00
|
|
|
/**
|
|
|
|
|
* @return a pointer to the next element in the queue, or NULL if the queue
|
|
|
|
|
* is full.
|
|
|
|
|
*/
|
|
|
|
|
static inline struct input_event*
|
|
|
|
|
queue_push(struct libevdev *dev)
|
|
|
|
|
{
|
|
|
|
|
if (dev->queue_next >= dev->queue_size)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
return &dev->queue[dev->queue_next++];
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-11 13:51:17 +10:00
|
|
|
static inline bool
|
|
|
|
|
queue_push_event(struct libevdev *dev, unsigned int type,
|
|
|
|
|
unsigned int code, int value)
|
|
|
|
|
{
|
|
|
|
|
struct input_event *ev = queue_push(dev);
|
|
|
|
|
|
|
|
|
|
if (ev)
|
|
|
|
|
init_event(dev, ev, type, code, value);
|
|
|
|
|
|
|
|
|
|
return ev != NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-30 11:05:24 +10:00
|
|
|
/**
|
|
|
|
|
* Set ev to the last element in the queue, removing it from the queue.
|
|
|
|
|
*
|
|
|
|
|
* @return 0 on success, 1 if the queue is empty.
|
|
|
|
|
*/
|
|
|
|
|
static inline int
|
|
|
|
|
queue_pop(struct libevdev *dev, struct input_event *ev)
|
|
|
|
|
{
|
|
|
|
|
if (dev->queue_next == 0)
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
*ev = dev->queue[--dev->queue_next];
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-30 12:20:21 +10:00
|
|
|
static inline int
|
|
|
|
|
queue_peek(struct libevdev *dev, size_t idx, struct input_event *ev)
|
|
|
|
|
{
|
2013-06-26 16:47:43 +10:00
|
|
|
if (dev->queue_next == 0 || idx > dev->queue_next)
|
2013-05-30 12:20:21 +10:00
|
|
|
return 1;
|
|
|
|
|
*ev = dev->queue[idx];
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-30 11:05:24 +10:00
|
|
|
/**
|
2013-05-30 12:20:21 +10:00
|
|
|
* Shift the first n elements into ev and return the number of elements
|
|
|
|
|
* shifted.
|
|
|
|
|
* ev must be large enough to store n elements.
|
2013-05-30 11:05:24 +10:00
|
|
|
*
|
2013-05-30 12:20:21 +10:00
|
|
|
* @param ev The buffer to copy into, or NULL
|
|
|
|
|
* @return The number of elements in ev.
|
2013-05-30 11:05:24 +10:00
|
|
|
*/
|
|
|
|
|
static inline int
|
2013-06-09 20:03:50 +10:00
|
|
|
queue_shift_multiple(struct libevdev *dev, size_t n, struct input_event *ev)
|
2013-05-30 11:05:24 +10:00
|
|
|
{
|
2015-02-01 15:58:36 +01:00
|
|
|
size_t remaining;
|
2013-05-30 11:05:24 +10:00
|
|
|
|
|
|
|
|
if (dev->queue_next == 0)
|
2013-05-30 12:20:21 +10:00
|
|
|
return 0;
|
2013-05-30 11:05:24 +10:00
|
|
|
|
2015-02-01 15:58:36 +01:00
|
|
|
remaining = dev->queue_next;
|
|
|
|
|
n = min(n, remaining);
|
|
|
|
|
remaining -= n;
|
2013-05-30 11:05:24 +10:00
|
|
|
|
2015-02-01 15:58:36 +01:00
|
|
|
if (ev)
|
|
|
|
|
memcpy(ev, dev->queue, n * sizeof(*ev));
|
2013-05-30 11:05:24 +10:00
|
|
|
|
2015-02-01 15:58:36 +01:00
|
|
|
memmove(dev->queue, &dev->queue[n], remaining * sizeof(*dev->queue));
|
2013-05-30 11:05:24 +10:00
|
|
|
|
2015-02-01 15:58:36 +01:00
|
|
|
dev->queue_next = remaining;
|
2013-05-30 12:20:21 +10:00
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set ev to the first element in the queue, shifting everything else
|
|
|
|
|
* forward by one.
|
|
|
|
|
*
|
|
|
|
|
* @return 0 on success, 1 if the queue is empty.
|
|
|
|
|
*/
|
|
|
|
|
static inline int
|
|
|
|
|
queue_shift(struct libevdev *dev, struct input_event *ev)
|
|
|
|
|
{
|
|
|
|
|
return queue_shift_multiple(dev, 1, ev) == 1 ? 0 : 1;
|
2013-05-30 11:05:24 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline int
|
2013-06-09 20:03:50 +10:00
|
|
|
queue_alloc(struct libevdev *dev, size_t size)
|
2013-05-30 11:05:24 +10:00
|
|
|
{
|
2013-06-09 20:07:14 +10:00
|
|
|
if (size == 0)
|
2013-08-31 12:23:44 +10:00
|
|
|
return -ENOMEM;
|
2013-06-09 20:07:14 +10:00
|
|
|
|
2013-05-30 11:05:24 +10:00
|
|
|
dev->queue = calloc(size, sizeof(struct input_event));
|
|
|
|
|
if (!dev->queue)
|
2013-08-31 12:23:44 +10:00
|
|
|
return -ENOMEM;
|
2013-05-30 11:05:24 +10:00
|
|
|
|
|
|
|
|
dev->queue_size = size;
|
|
|
|
|
dev->queue_next = 0;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-31 15:24:39 +10:00
|
|
|
static inline void
|
|
|
|
|
queue_free(struct libevdev *dev)
|
|
|
|
|
{
|
|
|
|
|
free(dev->queue);
|
|
|
|
|
dev->queue_size = 0;
|
|
|
|
|
dev->queue_next = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-09 20:03:50 +10:00
|
|
|
static inline size_t
|
2013-05-30 11:05:24 +10:00
|
|
|
queue_num_elements(struct libevdev *dev)
|
|
|
|
|
{
|
|
|
|
|
return dev->queue_next;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-09 20:03:50 +10:00
|
|
|
static inline size_t
|
2013-05-30 11:05:24 +10:00
|
|
|
queue_size(struct libevdev *dev)
|
|
|
|
|
{
|
|
|
|
|
return dev->queue_size;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-09 20:03:50 +10:00
|
|
|
static inline size_t
|
2013-05-30 11:05:24 +10:00
|
|
|
queue_num_free_elements(struct libevdev *dev)
|
|
|
|
|
{
|
2013-06-09 20:11:39 +10:00
|
|
|
if (dev->queue_size == 0)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
return dev->queue_size - dev->queue_next;
|
2013-05-30 11:05:24 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline struct input_event *
|
|
|
|
|
queue_next_element(struct libevdev *dev)
|
|
|
|
|
{
|
2013-06-26 17:26:50 +10:00
|
|
|
if (dev->queue_next == dev->queue_size)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
2013-05-30 11:05:24 +10:00
|
|
|
return &dev->queue[dev->queue_next];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline int
|
2013-06-09 20:03:50 +10:00
|
|
|
queue_set_num_elements(struct libevdev *dev, size_t nelem)
|
2013-05-30 11:05:24 +10:00
|
|
|
{
|
|
|
|
|
if (nelem > dev->queue_size)
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
dev->queue_next = nelem;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2014-03-06 10:22:20 +10:00
|
|
|
|
|
|
|
|
#define max_mask(uc, lc) \
|
|
|
|
|
case EV_##uc: \
|
|
|
|
|
*mask = dev->lc##_bits; \
|
|
|
|
|
max = libevdev_event_type_get_max(type); \
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
|
type_to_mask_const(const struct libevdev *dev, unsigned int type, const unsigned long **mask)
|
|
|
|
|
{
|
|
|
|
|
int max;
|
|
|
|
|
|
|
|
|
|
switch(type) {
|
|
|
|
|
max_mask(ABS, abs);
|
|
|
|
|
max_mask(REL, rel);
|
|
|
|
|
max_mask(KEY, key);
|
|
|
|
|
max_mask(LED, led);
|
|
|
|
|
max_mask(MSC, msc);
|
|
|
|
|
max_mask(SW, sw);
|
|
|
|
|
max_mask(FF, ff);
|
|
|
|
|
max_mask(REP, rep);
|
|
|
|
|
max_mask(SND, snd);
|
|
|
|
|
default:
|
|
|
|
|
max = -1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return max;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
|
type_to_mask(struct libevdev *dev, unsigned int type, unsigned long **mask)
|
|
|
|
|
{
|
|
|
|
|
int max;
|
|
|
|
|
|
|
|
|
|
switch(type) {
|
|
|
|
|
max_mask(ABS, abs);
|
|
|
|
|
max_mask(REL, rel);
|
|
|
|
|
max_mask(KEY, key);
|
|
|
|
|
max_mask(LED, led);
|
|
|
|
|
max_mask(MSC, msc);
|
|
|
|
|
max_mask(SW, sw);
|
|
|
|
|
max_mask(FF, ff);
|
|
|
|
|
max_mask(REP, rep);
|
|
|
|
|
max_mask(SND, snd);
|
|
|
|
|
default:
|
|
|
|
|
max = -1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return max;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#undef max_mask
|
2013-05-27 14:59:41 +10:00
|
|
|
#endif
|