2021-08-27 00:10:21 +00:00
|
|
|
/*
|
|
|
|
in_evdev.c
|
|
|
|
|
|
|
|
general evdev input driver
|
|
|
|
|
|
|
|
Copyright (C) 2021 Bill Currie <bill@taniwha.org>
|
|
|
|
Please see the file "AUTHORS" for a list of contributors
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program 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 General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "QF/cvar.h"
|
|
|
|
#include "QF/input.h"
|
2021-08-30 05:40:19 +00:00
|
|
|
#include "QF/progs.h" // for PR_RESMAP
|
2021-08-27 00:10:21 +00:00
|
|
|
#include "QF/sys.h"
|
|
|
|
|
2021-11-01 04:05:05 +00:00
|
|
|
#include "QF/input/event.h"
|
|
|
|
|
2021-08-27 00:10:21 +00:00
|
|
|
#include "compat.h"
|
2021-09-28 01:53:51 +00:00
|
|
|
#include "qfselect.h"
|
2021-08-27 00:10:21 +00:00
|
|
|
#include "evdev/inputlib.h"
|
|
|
|
|
2021-09-25 06:42:54 +00:00
|
|
|
typedef struct devmap_s {
|
|
|
|
struct devmap_s *next;
|
|
|
|
struct devmap_s **prev;
|
2021-09-20 06:21:11 +00:00
|
|
|
device_t *device;
|
2021-11-09 13:31:09 +00:00
|
|
|
void *event_data;
|
2021-08-30 05:40:19 +00:00
|
|
|
int devid;
|
|
|
|
} devmap_t;
|
|
|
|
|
|
|
|
static int evdev_driver_handle = -1;
|
2021-11-22 05:32:19 +00:00
|
|
|
static int evdev_have_focus;
|
2021-08-30 05:40:19 +00:00
|
|
|
static PR_RESMAP (devmap_t) devmap;
|
2021-09-25 06:42:54 +00:00
|
|
|
static devmap_t *devmap_list;
|
2021-08-30 05:40:19 +00:00
|
|
|
|
2021-08-27 00:10:21 +00:00
|
|
|
static void
|
2021-09-28 01:53:51 +00:00
|
|
|
in_evdev_add_select (qf_fd_set *fdset, int *maxfd, void *data)
|
2021-08-27 00:10:21 +00:00
|
|
|
{
|
2021-09-28 01:53:51 +00:00
|
|
|
inputlib_add_select (&fdset->fdset, maxfd);
|
2021-09-26 06:11:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-09-28 01:53:51 +00:00
|
|
|
in_evdev_check_select (qf_fd_set *fdset, void *data)
|
2021-09-26 06:11:07 +00:00
|
|
|
{
|
2021-09-28 01:53:51 +00:00
|
|
|
inputlib_check_select (&fdset->fdset);
|
2021-08-27 00:10:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-08-30 00:54:36 +00:00
|
|
|
in_evdev_shutdown (void *data)
|
2021-08-27 00:10:21 +00:00
|
|
|
{
|
|
|
|
inputlib_close ();
|
|
|
|
}
|
|
|
|
|
2021-11-09 13:31:09 +00:00
|
|
|
static void
|
|
|
|
in_evdev_set_device_event_data (void *device, void *event_data, void *data)
|
|
|
|
{
|
|
|
|
device_t *dev = device;
|
|
|
|
devmap_t *dm = dev->data;
|
|
|
|
dm->event_data = event_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
in_evdev_get_device_event_data (void *device, void *data)
|
|
|
|
{
|
|
|
|
device_t *dev = device;
|
|
|
|
devmap_t *dm = dev->data;
|
|
|
|
return dm->event_data;
|
|
|
|
}
|
|
|
|
|
2021-08-30 05:40:19 +00:00
|
|
|
static void
|
|
|
|
in_evdev_axis_event (axis_t *axis, void *_dm)
|
|
|
|
{
|
2021-11-22 05:32:19 +00:00
|
|
|
if (!evdev_have_focus) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-30 05:40:19 +00:00
|
|
|
devmap_t *dm = _dm;
|
|
|
|
//Sys_Printf ("in_evdev_axis_event: %d %d\n", axis->num, axis->value);
|
|
|
|
|
|
|
|
IE_event_t event = {
|
|
|
|
.type = ie_axis,
|
|
|
|
.when = Sys_LongTime (),
|
|
|
|
.axis = {
|
2021-11-09 13:31:09 +00:00
|
|
|
.data = dm->event_data,
|
2021-08-30 05:40:19 +00:00
|
|
|
.devid = dm->devid,
|
|
|
|
.axis = axis->num,
|
|
|
|
.value = axis->value,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
IE_Send_Event (&event);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
in_evdev_button_event (button_t *button, void *_dm)
|
|
|
|
{
|
2021-11-22 05:32:19 +00:00
|
|
|
if (!evdev_have_focus) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-30 05:40:19 +00:00
|
|
|
devmap_t *dm = _dm;
|
|
|
|
//Sys_Printf ("in_evdev_button_event: %d %d\n", button->num, button->state);
|
|
|
|
|
|
|
|
IE_event_t event = {
|
|
|
|
.type = ie_button,
|
|
|
|
.when = Sys_LongTime (),
|
|
|
|
.button = {
|
2021-11-09 13:31:09 +00:00
|
|
|
.data = dm->event_data,
|
2021-08-30 05:40:19 +00:00
|
|
|
.devid = dm->devid,
|
|
|
|
.button = button->num,
|
|
|
|
.state = button->state,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
IE_Send_Event (&event);
|
|
|
|
}
|
|
|
|
|
2021-08-27 00:10:21 +00:00
|
|
|
static void
|
|
|
|
device_add (device_t *dev)
|
|
|
|
{
|
2021-09-20 06:21:11 +00:00
|
|
|
const char *name = dev->name;
|
|
|
|
// prefer device unique string if available, otherwise fall back to
|
|
|
|
// the physical path
|
|
|
|
const char *id = dev->uniq;
|
|
|
|
if (!id || !*id) {
|
|
|
|
id = dev->phys;
|
|
|
|
}
|
|
|
|
|
2021-08-30 05:40:19 +00:00
|
|
|
devmap_t *dm = PR_RESNEW (devmap);
|
2021-09-25 06:42:54 +00:00
|
|
|
dm->next = devmap_list;
|
|
|
|
dm->prev = &devmap_list;
|
|
|
|
if (devmap_list) {
|
|
|
|
devmap_list->prev = &dm->next;
|
|
|
|
}
|
|
|
|
devmap_list = dm;
|
|
|
|
|
2021-08-30 05:40:19 +00:00
|
|
|
dev->data = dm;
|
|
|
|
dev->axis_event = in_evdev_axis_event;
|
|
|
|
dev->button_event = in_evdev_button_event;
|
|
|
|
|
2021-11-10 04:22:44 +00:00
|
|
|
dm->device = dev;
|
|
|
|
dm->devid = IN_AddDevice (evdev_driver_handle, dev, name, id);
|
|
|
|
|
2021-08-30 05:40:19 +00:00
|
|
|
#if 0
|
2021-08-30 00:54:36 +00:00
|
|
|
Sys_Printf ("in_evdev: add %s\n", dev->path);
|
|
|
|
Sys_Printf (" %s\n", dev->name);
|
|
|
|
Sys_Printf (" %s\n", dev->phys);
|
2021-08-30 05:40:19 +00:00
|
|
|
for (int i = 0; i < dev->num_axes; i++) {
|
|
|
|
axis_t *axis = dev->axes + i;
|
|
|
|
Sys_Printf ("axis: %d %d\n", axis->num, axis->value);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < dev->num_buttons; i++) {
|
|
|
|
button_t *button = dev->buttons + i;
|
|
|
|
Sys_Printf ("button: %d %d\n", button->num, button->state);
|
|
|
|
}
|
|
|
|
#endif
|
2021-08-27 00:10:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
device_remove (device_t *dev)
|
|
|
|
{
|
2021-09-25 06:42:54 +00:00
|
|
|
for (devmap_t *dm = devmap_list; dm; dm = dm->next) {
|
2021-09-20 06:21:11 +00:00
|
|
|
if (dm->device == dev) {
|
2021-08-30 05:40:19 +00:00
|
|
|
IN_RemoveDevice (dm->devid);
|
2021-09-25 06:42:54 +00:00
|
|
|
|
|
|
|
if (dm->next) {
|
|
|
|
dm->next->prev = dm->prev;
|
|
|
|
}
|
|
|
|
*dm->prev = dm->next;
|
|
|
|
|
2021-08-30 05:40:19 +00:00
|
|
|
PR_RESFREE (devmap, dm);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-08-27 00:10:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-08-30 00:54:36 +00:00
|
|
|
in_evdev_init (void *data)
|
2021-08-27 00:10:21 +00:00
|
|
|
{
|
|
|
|
inputlib_init (device_add, device_remove);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-08-30 00:54:36 +00:00
|
|
|
in_evdev_clear_states (void *data)
|
2021-08-27 00:10:21 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-09-20 06:21:11 +00:00
|
|
|
static void
|
|
|
|
in_evdev_axis_info (void *data, void *device, in_axisinfo_t *axes,
|
|
|
|
int *numaxes)
|
|
|
|
{
|
|
|
|
device_t *dev = device;
|
|
|
|
if (!axes) {
|
|
|
|
*numaxes = dev->num_axes;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (*numaxes > dev->num_axes) {
|
|
|
|
*numaxes = dev->num_axes;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < *numaxes; i++) {
|
|
|
|
axes[i].axis = dev->axes[i].num;
|
|
|
|
axes[i].value = dev->axes[i].value;
|
|
|
|
axes[i].min = dev->axes[i].min;
|
|
|
|
axes[i].max = dev->axes[i].max;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
in_evdev_button_info (void *data, void *device, in_buttoninfo_t *buttons,
|
2021-12-21 08:42:38 +00:00
|
|
|
int *numbuttons)
|
2021-09-20 06:21:11 +00:00
|
|
|
{
|
|
|
|
device_t *dev = device;
|
|
|
|
if (!buttons) {
|
|
|
|
*numbuttons = dev->num_buttons;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (*numbuttons > dev->num_buttons) {
|
|
|
|
*numbuttons = dev->num_buttons;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < *numbuttons; i++) {
|
|
|
|
buttons[i].button = dev->buttons[i].num;
|
|
|
|
buttons[i].state = dev->buttons[i].state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-21 08:42:38 +00:00
|
|
|
static int
|
|
|
|
in_evdev_get_axis_info (void *data, void *device, int axis_num,
|
|
|
|
in_axisinfo_t *info)
|
|
|
|
{
|
|
|
|
device_t *dev = device;
|
|
|
|
if (axis_num < 0 || axis_num > dev->num_axes) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
info->axis = dev->axes[axis_num].num;
|
|
|
|
info->value = dev->axes[axis_num].value;
|
|
|
|
info->min = dev->axes[axis_num].min;
|
|
|
|
info->max = dev->axes[axis_num].max;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
in_evdev_get_button_info (void *data, void *device, int button_num,
|
|
|
|
in_buttoninfo_t *info)
|
|
|
|
{
|
|
|
|
device_t *dev = device;
|
|
|
|
if (button_num < 0 || button_num > dev->num_buttons) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
info->button = dev->buttons[button_num].num;
|
|
|
|
info->state = dev->buttons[button_num].state;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-08-27 00:10:21 +00:00
|
|
|
static in_driver_t in_evdev_driver = {
|
|
|
|
.init = in_evdev_init,
|
|
|
|
.shutdown = in_evdev_shutdown,
|
2021-11-09 13:31:09 +00:00
|
|
|
.set_device_event_data = in_evdev_set_device_event_data,
|
|
|
|
.get_device_event_data = in_evdev_get_device_event_data,
|
2021-09-26 06:11:07 +00:00
|
|
|
.add_select = in_evdev_add_select,
|
|
|
|
.check_select = in_evdev_check_select,
|
2021-08-27 00:10:21 +00:00
|
|
|
.clear_states = in_evdev_clear_states,
|
2021-09-20 06:21:11 +00:00
|
|
|
|
|
|
|
.axis_info = in_evdev_axis_info,
|
|
|
|
.button_info = in_evdev_button_info,
|
2021-12-21 08:42:38 +00:00
|
|
|
|
|
|
|
.get_axis_info = in_evdev_get_axis_info,
|
|
|
|
.get_button_info = in_evdev_get_button_info,
|
2021-08-27 00:10:21 +00:00
|
|
|
};
|
|
|
|
|
2021-11-22 05:32:19 +00:00
|
|
|
static int
|
|
|
|
in_evdev_evend_handler (const IE_event_t *event, void *data)
|
|
|
|
{
|
|
|
|
if (event->type == ie_app_gain_focus) {
|
|
|
|
evdev_have_focus = 1;
|
|
|
|
return 1;
|
|
|
|
} else if (event->type == ie_app_lose_focus) {
|
|
|
|
evdev_have_focus = 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-27 00:10:21 +00:00
|
|
|
static void __attribute__((constructor))
|
|
|
|
in_evdev_register_driver (void)
|
|
|
|
{
|
2021-08-30 05:40:19 +00:00
|
|
|
evdev_driver_handle = IN_RegisterDriver (&in_evdev_driver, 0);
|
2021-11-22 05:32:19 +00:00
|
|
|
//FIXME probably shouldn't be here
|
|
|
|
IE_Add_Handler (in_evdev_evend_handler, 0);
|
2021-08-27 00:10:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int in_evdev_force_link;
|