mirror of
https://git.sr.ht/~thestr4ng3r/chiaki
synced 2025-08-19 21:13:12 -07:00
Better Polling in Setsu
This commit is contained in:
parent
9c91843d98
commit
092355ff00
3 changed files with 94 additions and 62 deletions
|
@ -18,10 +18,10 @@
|
||||||
#ifndef _SETSU_H
|
#ifndef _SETSU_H
|
||||||
#define _SETSU_H
|
#define _SETSU_H
|
||||||
|
|
||||||
typedef struct setsu_ctx_t SetsuCtx;
|
typedef struct setsu_t Setsu;
|
||||||
|
|
||||||
SetsuCtx *setsu_ctx_new();
|
Setsu *setsu_new();
|
||||||
void setsu_ctx_free(SetsuCtx *ctx);
|
void setsu_free(Setsu *setsu);
|
||||||
void setsu_ctx_run(SetsuCtx *ctx);
|
void setsu_poll(Setsu *setsu);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -30,6 +30,10 @@
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
|
||||||
|
#define SETSU_LOG(...) fprintf(stderr, __VA_ARGS__)
|
||||||
|
|
||||||
|
|
||||||
typedef struct setsu_device_t
|
typedef struct setsu_device_t
|
||||||
{
|
{
|
||||||
struct setsu_device_t *next;
|
struct setsu_device_t *next;
|
||||||
|
@ -38,51 +42,53 @@ typedef struct setsu_device_t
|
||||||
struct libevdev *evdev;
|
struct libevdev *evdev;
|
||||||
} SetsuDevice;
|
} SetsuDevice;
|
||||||
|
|
||||||
struct setsu_ctx_t
|
struct setsu_t
|
||||||
{
|
{
|
||||||
struct udev *udev_ctx;
|
struct udev *udev_setsu;
|
||||||
SetsuDevice *dev;
|
SetsuDevice *dev;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static void scan(SetsuCtx *ctx);
|
static void scan(Setsu *setsu);
|
||||||
static void update_device(SetsuCtx *ctx, struct udev_device *dev, bool added);
|
static void update_device(Setsu *setsu, struct udev_device *dev, bool added);
|
||||||
static SetsuDevice *connect(SetsuCtx *ctx, const char *path);
|
static SetsuDevice *connect(Setsu *setsu, const char *path);
|
||||||
static void disconnect(SetsuCtx *ctx, SetsuDevice *dev);
|
static void disconnect(Setsu *setsu, SetsuDevice *dev);
|
||||||
|
static void poll_device(Setsu *setsu, SetsuDevice *dev);
|
||||||
|
static void device_event(Setsu *setsu, SetsuDevice *dev, struct input_event *ev);
|
||||||
|
|
||||||
SetsuCtx *setsu_ctx_new()
|
Setsu *setsu_new()
|
||||||
{
|
{
|
||||||
SetsuCtx *ctx = malloc(sizeof(SetsuCtx));
|
Setsu *setsu = malloc(sizeof(Setsu));
|
||||||
if(!ctx)
|
if(!setsu)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
ctx->dev = NULL;
|
setsu->dev = NULL;
|
||||||
|
|
||||||
ctx->udev_ctx = udev_new();
|
setsu->udev_setsu = udev_new();
|
||||||
if(!ctx->udev_ctx)
|
if(!setsu->udev_setsu)
|
||||||
{
|
{
|
||||||
free(ctx);
|
free(setsu);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: monitor
|
// TODO: monitor
|
||||||
|
|
||||||
scan(ctx);
|
scan(setsu);
|
||||||
|
|
||||||
return ctx;
|
return setsu;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setsu_ctx_free(SetsuCtx *ctx)
|
void setsu_free(Setsu *setsu)
|
||||||
{
|
{
|
||||||
while(ctx->dev)
|
while(setsu->dev)
|
||||||
disconnect(ctx, ctx->dev);
|
disconnect(setsu, setsu->dev);
|
||||||
udev_unref(ctx->udev_ctx);
|
udev_unref(setsu->udev_setsu);
|
||||||
free(ctx);
|
free(setsu);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void scan(SetsuCtx *ctx)
|
static void scan(Setsu *setsu)
|
||||||
{
|
{
|
||||||
struct udev_enumerate *udev_enum = udev_enumerate_new(ctx->udev_ctx);
|
struct udev_enumerate *udev_enum = udev_enumerate_new(setsu->udev_setsu);
|
||||||
if(!udev_enum)
|
if(!udev_enum)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -100,10 +106,11 @@ static void scan(SetsuCtx *ctx)
|
||||||
const char *path = udev_list_entry_get_name(entry);
|
const char *path = udev_list_entry_get_name(entry);
|
||||||
if(!path)
|
if(!path)
|
||||||
continue;
|
continue;
|
||||||
struct udev_device *dev = udev_device_new_from_syspath(ctx->udev_ctx, path);
|
struct udev_device *dev = udev_device_new_from_syspath(setsu->udev_setsu, path);
|
||||||
if(!dev)
|
if(!dev)
|
||||||
continue;
|
continue;
|
||||||
update_device(ctx, dev, true);
|
SETSU_LOG("enum device: %s\n", path);
|
||||||
|
update_device(setsu, dev, true);
|
||||||
udev_device_unref(dev);
|
udev_device_unref(dev);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,7 +122,8 @@ static bool is_device_interesting(struct udev_device *dev)
|
||||||
{
|
{
|
||||||
static const char *device_ids[] = {
|
static const char *device_ids[] = {
|
||||||
// vendor id, model id
|
// vendor id, model id
|
||||||
"054c", "05c4", // DualShock 4 USB
|
"054c", "05c4", // DualShock 4 Gen 1 USB
|
||||||
|
"054c", "09cc", // DualShock 4 Gen 2 USB
|
||||||
NULL
|
NULL
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -136,7 +144,7 @@ static bool is_device_interesting(struct udev_device *dev)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void update_device(SetsuCtx *ctx, struct udev_device *dev, bool added)
|
static void update_device(Setsu *setsu, struct udev_device *dev, bool added)
|
||||||
{
|
{
|
||||||
if(!is_device_interesting(dev))
|
if(!is_device_interesting(dev))
|
||||||
return;
|
return;
|
||||||
|
@ -144,19 +152,19 @@ static void update_device(SetsuCtx *ctx, struct udev_device *dev, bool added)
|
||||||
if(!path)
|
if(!path)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
for(SetsuDevice *dev = ctx->dev; dev; dev=dev->next)
|
for(SetsuDevice *dev = setsu->dev; dev; dev=dev->next)
|
||||||
{
|
{
|
||||||
if(!strcmp(dev->path, path))
|
if(!strcmp(dev->path, path))
|
||||||
{
|
{
|
||||||
if(added)
|
if(added)
|
||||||
return; // already added, do nothing
|
return; // already added, do nothing
|
||||||
disconnect(ctx, dev);
|
disconnect(setsu, dev);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
connect(ctx, path);
|
connect(setsu, path);
|
||||||
}
|
}
|
||||||
|
|
||||||
static SetsuDevice *connect(SetsuCtx *ctx, const char *path)
|
static SetsuDevice *connect(Setsu *setsu, const char *path)
|
||||||
{
|
{
|
||||||
SetsuDevice *dev = malloc(sizeof(SetsuDevice));
|
SetsuDevice *dev = malloc(sizeof(SetsuDevice));
|
||||||
if(!dev)
|
if(!dev)
|
||||||
|
@ -174,10 +182,10 @@ static SetsuDevice *connect(SetsuCtx *ctx, const char *path)
|
||||||
if(libevdev_new_from_fd(dev->fd, &dev->evdev) < 0)
|
if(libevdev_new_from_fd(dev->fd, &dev->evdev) < 0)
|
||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
printf("connected to %s\n", libevdev_get_name(dev->evdev));
|
SETSU_LOG("connected to %s\n", libevdev_get_name(dev->evdev));
|
||||||
|
|
||||||
dev->next = ctx->dev;
|
dev->next = setsu->dev;
|
||||||
ctx->dev = dev;
|
setsu->dev = dev;
|
||||||
return dev;
|
return dev;
|
||||||
error:
|
error:
|
||||||
if(dev->evdev)
|
if(dev->evdev)
|
||||||
|
@ -189,13 +197,13 @@ error:
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void disconnect(SetsuCtx *ctx, SetsuDevice *dev)
|
static void disconnect(Setsu *setsu, SetsuDevice *dev)
|
||||||
{
|
{
|
||||||
if(ctx->dev == dev)
|
if(setsu->dev == dev)
|
||||||
ctx->dev = dev->next;
|
setsu->dev = dev->next;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
for(SetsuDevice *pdev = ctx->dev; pdev; pdev=pdev->next)
|
for(SetsuDevice *pdev = setsu->dev; pdev; pdev=pdev->next)
|
||||||
{
|
{
|
||||||
if(pdev->next == dev)
|
if(pdev->next == dev)
|
||||||
{
|
{
|
||||||
|
@ -209,22 +217,45 @@ static void disconnect(SetsuCtx *ctx, SetsuDevice *dev)
|
||||||
free(dev);
|
free(dev);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setsu_ctx_run(SetsuCtx *ctx)
|
void setsu_poll(Setsu *setsu)
|
||||||
{
|
{
|
||||||
SetsuDevice *dev = ctx->dev;
|
for(SetsuDevice *dev = setsu->dev; dev; dev = dev->next)
|
||||||
if(!dev)
|
poll_device(setsu, dev);
|
||||||
return;
|
}
|
||||||
|
|
||||||
int rc;
|
static void poll_device(Setsu *setsu, SetsuDevice *dev)
|
||||||
do
|
{
|
||||||
{
|
bool sync = false;
|
||||||
struct input_event ev;
|
while(true)
|
||||||
rc = libevdev_next_event(dev->evdev, LIBEVDEV_READ_FLAG_NORMAL, &ev);
|
{
|
||||||
if (rc == 0)
|
struct input_event ev;
|
||||||
printf("Event: %s %s %d\n",
|
int r = libevdev_next_event(dev->evdev, sync ? LIBEVDEV_READ_FLAG_SYNC : LIBEVDEV_READ_FLAG_NORMAL, &ev);
|
||||||
libevdev_event_type_get_name(ev.type),
|
if(r == -EAGAIN)
|
||||||
libevdev_event_code_get_name(ev.type, ev.code),
|
{
|
||||||
ev.value);
|
if(sync)
|
||||||
} while (rc == 1 || rc == 0 || rc == -EAGAIN);
|
{
|
||||||
|
sync = false;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if(r == LIBEVDEV_READ_STATUS_SUCCESS || (sync && r == LIBEVDEV_READ_STATUS_SYNC))
|
||||||
|
device_event(setsu, dev, &ev);
|
||||||
|
else if(r == LIBEVDEV_READ_STATUS_SYNC)
|
||||||
|
sync = true;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
SETSU_LOG("evdev poll failed\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void device_event(Setsu *setsu, SetsuDevice *dev, struct input_event *ev)
|
||||||
|
{
|
||||||
|
printf("Event: %s %s %d\n",
|
||||||
|
libevdev_event_type_get_name(ev->type),
|
||||||
|
libevdev_event_code_get_name(ev->type, ev->code),
|
||||||
|
ev->value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,22 +20,23 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
SetsuCtx *ctx;
|
Setsu *setsu;
|
||||||
|
|
||||||
void quit()
|
void quit()
|
||||||
{
|
{
|
||||||
setsu_ctx_free(ctx);
|
setsu_free(setsu);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
ctx = setsu_ctx_new();
|
setsu = setsu_new();
|
||||||
if(!ctx)
|
if(!setsu)
|
||||||
{
|
{
|
||||||
printf("Failed to init setsu\n");
|
printf("Failed to init setsu\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
setsu_ctx_run(ctx);
|
while(1)
|
||||||
|
setsu_poll(setsu);
|
||||||
atexit(quit);
|
atexit(quit);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue