Migrate x52d_mouse_evdev.c to use vkm

virtual-keyboard-mouse
nirenjan 2026-03-20 00:12:47 -07:00
parent facbd79a3b
commit c00576b9bd
6 changed files with 82 additions and 90 deletions

View File

@ -28,7 +28,7 @@ x52d_sources = [
]
dep_threads = dependency('threads')
x52d_linkwith = [lib_libx52, lib_libx52dcomm]
x52d_linkwith = [lib_libx52, lib_libx52dcomm, lib_vkm]
x52d_deps = [dep_pinelog, dep_inih, dep_threads, dep_intl]
x52d_cflags = []
if dep_evdev.found()

View File

@ -12,9 +12,8 @@
#include <pthread.h>
#include <unistd.h>
#include "libevdev/libevdev.h"
#include "libevdev/libevdev-uinput.h"
#include "libx52io.h"
#include "vkm.h"
#include "pinelog.h"
#include "x52d_config.h"
@ -24,36 +23,37 @@
static pthread_t mouse_thr;
static bool mouse_thr_enabled = false;
static struct libevdev_uinput *mouse_uidev;
static bool mouse_uidev_created = false;
static vkm_context *mouse_context;
static volatile libx52io_report old_report;
static volatile libx52io_report new_report;
static int report_button_change(int button, int index)
static int report_button_change(vkm_mouse_button button, int index)
{
int rc = 1;
vkm_result rc;
bool old_button = old_report.button[index];
bool new_button = new_report.button[index];
vkm_button_state state;
if (old_button != new_button) {
rc = libevdev_uinput_write_event(mouse_uidev, EV_KEY, button,
(int)new_button);
if (rc != 0) {
state = new_button ? VKM_BUTTON_PRESSED : VKM_BUTTON_RELEASED;
rc = vkm_mouse_click(mouse_context, button, state);
if (rc != VKM_SUCCESS) {
PINELOG_ERROR(_("Error writing mouse button event (button %d, state %d)"),
button, (int)new_button);
}
}
return rc;
return (rc == VKM_SUCCESS);
}
static int report_wheel(void)
{
int rc = 1;
vkm_result rc;
int wheel = 0;
bool scroll_up = new_report.button[LIBX52IO_BTN_MOUSE_SCROLL_UP];
bool scroll_dn = new_report.button[LIBX52IO_BTN_MOUSE_SCROLL_DN];
vkm_mouse_scroll_direction dir;
if (scroll_up) {
// Scroll up event
@ -64,19 +64,18 @@ static int report_wheel(void)
}
if (wheel != 0) {
rc = libevdev_uinput_write_event(mouse_uidev, EV_REL, REL_WHEEL, wheel);
if (rc != 0) {
PINELOG_ERROR(_("Error writing mouse wheel event %d"), wheel);
dir = (wheel == 1) ? VKM_MOUSE_SCROLL_UP : VKM_MOUSE_SCROLL_DOWN;
rc = vkm_mouse_scroll(mouse_context, dir);
if (rc != VKM_SUCCESS) {
PINELOG_ERROR(_("Error writing mouse wheel event %d"), dir);
}
}
return rc;
return (rc == VKM_SUCCESS);
}
static int report_axis(int axis, int index)
static int get_axis_val(int index)
{
int rc = 1;
int axis_val = new_report.axis[index];
/*
@ -96,22 +95,29 @@ static int report_axis(int axis, int index)
*/
axis_val = (axis_val * mouse_mult) / MOUSE_MULT_FACTOR;
if (axis_val) {
rc = libevdev_uinput_write_event(mouse_uidev, EV_REL, axis, axis_val);
if (rc != 0) {
PINELOG_ERROR(_("Error writing mouse axis event (axis %d, value %d)"),
axis, axis_val);
}
return axis_val;
}
static int report_axis(void)
{
vkm_result rc;
int dx = get_axis_val(LIBX52IO_AXIS_THUMBX);
int dy = get_axis_val(LIBX52IO_AXIS_THUMBY);
rc = vkm_mouse_move(mouse_context, dx, dy);
if (rc != VKM_SUCCESS) {
PINELOG_ERROR(_("Error writing mouse axis event (dx %d, dy %d)"),
dx, dy);
}
return rc;
return (rc == VKM_SUCCESS);
}
static void report_sync(void)
{
int rc;
rc = libevdev_uinput_write_event(mouse_uidev, EV_SYN, SYN_REPORT, 0);
if (rc != 0) {
vkm_result rc;
rc = vkm_sync(mouse_context);
if (rc != VKM_SUCCESS) {
PINELOG_ERROR(_("Error writing mouse sync event"));
} else {
memcpy((void *)&old_report, (void *)&new_report, sizeof(old_report));
@ -129,16 +135,11 @@ static void reset_reports(void)
static void * x52_mouse_thr(void *param)
{
bool state_changed;
(void)param;
PINELOG_INFO(_("Starting X52 virtual mouse driver thread"));
for (;;) {
state_changed = false;
state_changed |= (0 == report_axis(REL_X, LIBX52IO_AXIS_THUMBX));
state_changed |= (0 == report_axis(REL_Y, LIBX52IO_AXIS_THUMBY));
if (state_changed) {
if (report_axis()) {
report_sync();
}
@ -168,7 +169,7 @@ static void x52d_mouse_thr_exit(void)
void x52d_mouse_evdev_thread_control(bool enabled)
{
if (!mouse_uidev_created) {
if (!vkm_is_ready(mouse_context)) {
PINELOG_INFO(_("Virtual mouse not created. Ignoring thread state change"));
return;
}
@ -198,13 +199,13 @@ void x52d_mouse_report_event(libx52io_report *report)
if (report) {
memcpy((void *)&new_report, report, sizeof(new_report));
if (!mouse_uidev_created || !mouse_thr_enabled) {
if (!vkm_is_ready(mouse_context) || !mouse_thr_enabled) {
return;
}
state_changed = false;
state_changed |= (0 == report_button_change(BTN_LEFT, LIBX52IO_BTN_MOUSE_PRIMARY));
state_changed |= (0 == report_button_change(BTN_RIGHT, LIBX52IO_BTN_MOUSE_SECONDARY));
state_changed |= (0 == report_button_change(VKM_MOUSE_BTN_LEFT, LIBX52IO_BTN_MOUSE_PRIMARY));
state_changed |= (0 == report_button_change(VKM_MOUSE_BTN_RIGHT, LIBX52IO_BTN_MOUSE_SECONDARY));
state_changed |= (0 == report_wheel());
if (state_changed) {
@ -217,33 +218,25 @@ void x52d_mouse_report_event(libx52io_report *report)
void x52d_mouse_evdev_init(void)
{
int rc;
struct libevdev *dev;
vkm_result rc;
/* Create a new mouse device */
dev = libevdev_new();
libevdev_set_name(dev, "X52 virtual mouse");
libevdev_enable_event_type(dev, EV_REL);
libevdev_enable_event_code(dev, EV_REL, REL_X, NULL);
libevdev_enable_event_code(dev, EV_REL, REL_Y, NULL);
libevdev_enable_event_code(dev, EV_REL, REL_WHEEL, NULL);
libevdev_enable_event_type(dev, EV_KEY);
libevdev_enable_event_code(dev, EV_KEY, BTN_LEFT, NULL);
libevdev_enable_event_code(dev, EV_KEY, BTN_RIGHT, NULL);
rc = vkm_init(&mouse_context);
if (rc != VKM_SUCCESS) {
PINELOG_ERROR(_("Error %d creating X52 virtual mouse"), rc);
return;
}
rc = libevdev_uinput_create_from_device(dev, LIBEVDEV_UINPUT_OPEN_MANAGED,
&mouse_uidev);
if (rc != 0) {
PINELOG_ERROR(_("Error %d creating X52 virtual mouse: %s"),
-rc, strerror(-rc));
} else {
mouse_uidev_created = true;
vkm_set_option(mouse_context, VKM_OPT_DEVICE_NAME, "X52 virtual mouse");
rc = vkm_start(mouse_context);
if (rc != VKM_SUCCESS) {
PINELOG_ERROR(_("Error %d creating X52 virtual mouse"), rc);
}
}
void x52d_mouse_evdev_exit(void)
{
x52d_mouse_evdev_thread_control(false);
mouse_uidev_created = false;
libevdev_uinput_destroy(mouse_uidev);
vkm_exit(mouse_context);
mouse_context = NULL;
}

View File

@ -112,7 +112,7 @@ dep_inih = dependency('inih')
# Shared libraries and programs
#######################################################################
# Includes
includes = include_directories('.', 'libx52', 'libx52io', 'libx52util')
includes = include_directories('.', 'libx52', 'libx52io', 'libx52util', 'vkm')
subdir('libx52')
subdir('libx52io')

View File

@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: libx52 0.3.3\n"
"Report-Msgid-Bugs-To: https://github.com/nirenjan/libx52/issues\n"
"POT-Creation-Date: 2026-03-19 00:09-0700\n"
"POT-Creation-Date: 2026-03-19 23:20-0700\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@ -856,45 +856,45 @@ msgstr ""
msgid "Setting mouse reverse scroll to %s"
msgstr ""
#: daemon/x52d_mouse_evdev.c:43
#: daemon/x52d_mouse_evdev.c:42
#, c-format
msgid "Error writing mouse button event (button %d, state %d)"
msgstr ""
#: daemon/x52d_mouse_evdev.c:69
#: daemon/x52d_mouse_evdev.c:70
#, c-format
msgid "Error writing mouse wheel event %d"
msgstr ""
#: daemon/x52d_mouse_evdev.c:102
#: daemon/x52d_mouse_evdev.c:109
#, c-format
msgid "Error writing mouse axis event (axis %d, value %d)"
msgid "Error writing mouse axis event (dx %d, dy %d)"
msgstr ""
#: daemon/x52d_mouse_evdev.c:115
#: daemon/x52d_mouse_evdev.c:121
msgid "Error writing mouse sync event"
msgstr ""
#: daemon/x52d_mouse_evdev.c:135
#: daemon/x52d_mouse_evdev.c:140
msgid "Starting X52 virtual mouse driver thread"
msgstr ""
#: daemon/x52d_mouse_evdev.c:158
#: daemon/x52d_mouse_evdev.c:159
#, c-format
msgid "Error %d initializing mouse thread: %s"
msgstr ""
#: daemon/x52d_mouse_evdev.c:165
#: daemon/x52d_mouse_evdev.c:166
msgid "Shutting down X52 virtual mouse driver thread"
msgstr ""
#: daemon/x52d_mouse_evdev.c:172
#: daemon/x52d_mouse_evdev.c:173
msgid "Virtual mouse not created. Ignoring thread state change"
msgstr ""
#: daemon/x52d_mouse_evdev.c:237
#: daemon/x52d_mouse_evdev.c:225 daemon/x52d_mouse_evdev.c:233
#, c-format
msgid "Error %d creating X52 virtual mouse: %s"
msgid "Error %d creating X52 virtual mouse"
msgstr ""
#: daemon/x52d_notify.c:46

View File

@ -7,15 +7,15 @@ msgid ""
msgstr ""
"Project-Id-Version: libx52 0.2.3\n"
"Report-Msgid-Bugs-To: https://github.com/nirenjan/libx52/issues\n"
"POT-Creation-Date: 2026-03-19 00:09-0700\n"
"PO-Revision-Date: 2023-01-04 08:40-0800\n"
"POT-Creation-Date: 2026-03-19 23:20-0700\n"
"PO-Revision-Date: 2026-03-19 23:21-0700\n"
"Last-Translator: Nirenjan Krishnan <nirenjan@gmail.com>\n"
"Language-Team: Dummy Language for testing i18n\n"
"Language: xx_PL\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 3.0.1\n"
"X-Generator: Poedit 3.4.2\n"
#: libx52/x52_strerror.c:23 libx52io/io_strings.c:101
msgid "Success"
@ -909,46 +909,46 @@ msgstr "Ettingsay ousemay eedspay otay %d (elayday %d ms, ultipliermay %f)"
msgid "Setting mouse reverse scroll to %s"
msgstr "Ettingsay ousemay everseray ollscray otay %s"
#: daemon/x52d_mouse_evdev.c:43
#: daemon/x52d_mouse_evdev.c:42
#, c-format
msgid "Error writing mouse button event (button %d, state %d)"
msgstr "Erroray itingwray ousemay uttonbay eventay (uttonbay %d, atestay %d)"
#: daemon/x52d_mouse_evdev.c:69
#: daemon/x52d_mouse_evdev.c:70
#, c-format
msgid "Error writing mouse wheel event %d"
msgstr "Erroray itingwray ousemay eelwhay eventay %d"
#: daemon/x52d_mouse_evdev.c:102
#: daemon/x52d_mouse_evdev.c:109
#, c-format
msgid "Error writing mouse axis event (axis %d, value %d)"
msgstr "Erroray itingwray ousemay axisay eventay (axisay %d, aluevay %d)"
msgid "Error writing mouse axis event (dx %d, dy %d)"
msgstr "Erroray itingwray ousemay axisay eventay (xday %d, yday %d)"
#: daemon/x52d_mouse_evdev.c:115
#: daemon/x52d_mouse_evdev.c:121
msgid "Error writing mouse sync event"
msgstr "Erroray itingwray ousemay yncsay eventay"
#: daemon/x52d_mouse_evdev.c:135
#: daemon/x52d_mouse_evdev.c:140
msgid "Starting X52 virtual mouse driver thread"
msgstr "Artingstay X52 irtualvay ousemay iverdray eadthray"
#: daemon/x52d_mouse_evdev.c:158
#: daemon/x52d_mouse_evdev.c:159
#, c-format
msgid "Error %d initializing mouse thread: %s"
msgstr "Erroray %d initializingay ousemay eadthray: %s"
#: daemon/x52d_mouse_evdev.c:165
#: daemon/x52d_mouse_evdev.c:166
msgid "Shutting down X52 virtual mouse driver thread"
msgstr "Uttingshay ownday X52 irtualvay ousemay iverdray eadthray"
#: daemon/x52d_mouse_evdev.c:172
#: daemon/x52d_mouse_evdev.c:173
msgid "Virtual mouse not created. Ignoring thread state change"
msgstr "Irtualvay ousemay otnay eatedcray. Ignoringa eadthray atestay angechay"
#: daemon/x52d_mouse_evdev.c:237
#: daemon/x52d_mouse_evdev.c:225 daemon/x52d_mouse_evdev.c:233
#, c-format
msgid "Error %d creating X52 virtual mouse: %s"
msgstr "Erroray %d eatingcray X52 irtualvay ousemay: %s"
msgid "Error %d creating X52 virtual mouse"
msgstr "Erroray %d eatingcray X52 irtualvay ousemay"
#: daemon/x52d_notify.c:46
#, c-format

View File

@ -328,7 +328,6 @@ vkm_result vkm_mouse_click(vkm_context *ctx, vkm_mouse_button button, vkm_button
*
* @param[in] ctx Context pointer
* @param[in] dir Scroll direction
* @param[in] state Button state (press or release)
*
* @returns
* - \ref VKM_SUCCESS on successful move