Connect Motion Devices in Setsu

This commit is contained in:
Florian Märkl 2021-01-06 13:04:24 +01:00
parent 20c54b05ad
commit 698bce8022
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
4 changed files with 114 additions and 2 deletions

View file

@ -20,5 +20,7 @@ target_link_libraries(setsu Udev::libudev Evdev::libevdev)
if(SETSU_BUILD_DEMOS)
add_executable(setsu-demo-touchpad demo/touchpad.c)
target_link_libraries(setsu-demo-touchpad setsu)
add_executable(setsu-demo-motion demo/motion.c)
target_link_libraries(setsu-demo-motion setsu)
endif()

101
setsu/demo/motion.c Normal file
View file

@ -0,0 +1,101 @@
// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL
#include <setsu.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>
#include <signal.h>
Setsu *setsu;
bool dirty = false;
bool log_mode;
volatile bool should_quit;
#define LOG(...) do { if(log_mode) fprintf(stderr, __VA_ARGS__); } while(0)
void sigint(int s)
{
should_quit = true;
}
void print_state()
{
#if 0
char buf[256];
*buf = 0;
printf("\033[2J%s", buf);
fflush(stdout);
#endif
}
void event(SetsuEvent *event, void *user)
{
dirty = true;
switch(event->type)
{
case SETSU_EVENT_DEVICE_ADDED: {
if(event->dev_type != SETSU_DEVICE_TYPE_MOTION)
break;
SetsuDevice *dev = setsu_connect(setsu, event->path, SETSU_DEVICE_TYPE_MOTION);
LOG("Device added: %s, connect %s\n", event->path, dev ? "succeeded" : "FAILED!");
break;
}
case SETSU_EVENT_DEVICE_REMOVED:
if(event->dev_type != SETSU_DEVICE_TYPE_MOTION)
break;
LOG("Device removed: %s\n", event->path);
break;
// TODO: motion events
default:
break;
}
}
void usage(const char *prog)
{
printf("usage: %s [-l]\n -l log mode\n", prog);
exit(1);
}
int main(int argc, const char *argv[])
{
log_mode = false;
if(argc == 2)
{
if(!strcmp(argv[1], "-l"))
log_mode = true;
else
usage(argv[0]);
}
else if(argc != 1)
usage(argv[0]);
setsu = setsu_new();
if(!setsu)
{
printf("Failed to init setsu\n");
return 1;
}
struct sigaction sa = {0};
sa.sa_handler = sigint;
sigemptyset(&sa.sa_mask);
sigaction(SIGINT, &sa, NULL);
dirty = true;
while(!should_quit)
{
if(dirty && !log_mode)
print_state();
dirty = false;
setsu_poll(setsu, event, NULL);
}
setsu_free(setsu);
printf("\nさよなら!\n");
return 0;
}

View file

@ -122,6 +122,8 @@ void event(SetsuEvent *event, void *user)
LOG("Button for %s: %llu %s\n", setsu_device_get_path(event->dev),
(unsigned long long)event->button, event->type == SETSU_EVENT_BUTTON_DOWN ? "down" : "up");
break;
default:
break;
}
}

View file

@ -178,13 +178,18 @@ static bool is_device_interesting(struct udev_device *dev, SetsuDeviceType *type
const char *accel_str = udev_device_get_property_value(dev, "ID_INPUT_ACCELEROMETER");
if(touchpad_str && !strcmp(touchpad_str, "1"))
{
// Filter mouse-device (/dev/input/mouse*) away and only keep the evdev (/dev/input/event*) one:
// Filter mouse-device (/dev/input/mouse*) away and only keep the evdev (/dev/input/event*) one:
if(!udev_device_get_property_value(dev, "ID_INPUT_TOUCHPAD_INTEGRATION"))
return false;
*type = SETSU_DEVICE_TYPE_TOUCHPAD;
}
else if(touchpad_str && !strcmp(touchpad_str, "1"))
else if(accel_str && !strcmp(accel_str, "1"))
{
// Filter /dev/input/js* away and keep /dev/input/event*
if(!udev_device_get_property_value(dev, "ID_INPUT_WIDTH_MM"))
return false;
*type = SETSU_DEVICE_TYPE_MOTION;
}
else
return false;
@ -435,6 +440,7 @@ void setsu_poll(Setsu *setsu, SetsuEventCb cb, void *user)
SetsuEvent event = { 0 };
event.type = SETSU_EVENT_DEVICE_ADDED;
event.path = adev->path;
event.dev_type = adev->type;
cb(&event, user);
adev->connect_dirty = false;
}
@ -443,6 +449,7 @@ void setsu_poll(Setsu *setsu, SetsuEventCb cb, void *user)
SetsuEvent event = { 0 };
event.type = SETSU_EVENT_DEVICE_REMOVED;
event.path = adev->path;
event.dev_type = adev->type;
cb(&event, user);
// kill the device only after sending the event
SetsuAvailDevice *next = adev->next;