mirror of
https://github.com/mpv-player/mpv.git
synced 2025-12-28 05:33:14 +00:00
input: use an input thread for joystick
This commit is contained in:
@@ -1557,7 +1557,7 @@ struct input_ctx *mp_input_init(struct mpv_global *global)
|
|||||||
|
|
||||||
#if HAVE_JOYSTICK
|
#if HAVE_JOYSTICK
|
||||||
if (input_conf->use_joystick)
|
if (input_conf->use_joystick)
|
||||||
mp_input_joystick_init(ictx, ictx->log, input_conf->js_dev);
|
mp_input_joystick_add(ictx, input_conf->js_dev);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if HAVE_LIRC
|
#if HAVE_LIRC
|
||||||
|
|||||||
@@ -29,6 +29,7 @@
|
|||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <poll.h>
|
||||||
|
|
||||||
#include "common/msg.h"
|
#include "common/msg.h"
|
||||||
#include "keycodes.h"
|
#include "keycodes.h"
|
||||||
@@ -43,21 +44,21 @@
|
|||||||
|
|
||||||
#include <linux/joystick.h>
|
#include <linux/joystick.h>
|
||||||
|
|
||||||
static int mp_input_joystick_read(void *ctx, int fd);
|
|
||||||
|
|
||||||
struct ctx {
|
struct ctx {
|
||||||
struct mp_log *log;
|
struct mp_log *log;
|
||||||
int axis[256];
|
int axis[256];
|
||||||
int btns;
|
int btns;
|
||||||
|
int fd;
|
||||||
};
|
};
|
||||||
|
|
||||||
static int close_js(void *ctx, int fd)
|
static int close_js(void *ctx, int fd)
|
||||||
{
|
{
|
||||||
|
close(fd);
|
||||||
talloc_free(ctx);
|
talloc_free(ctx);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int mp_input_joystick_init(struct input_ctx *ictx, struct mp_log *log, char *dev)
|
static struct ctx *joystick_init(struct input_ctx *ictx, struct mp_log *log, char *dev)
|
||||||
{
|
{
|
||||||
int fd,l=0;
|
int fd,l=0;
|
||||||
int initialized = 0;
|
int initialized = 0;
|
||||||
@@ -68,7 +69,7 @@ int mp_input_joystick_init(struct input_ctx *ictx, struct mp_log *log, char *dev
|
|||||||
fd = open( dev ? dev : JS_DEV , O_RDONLY | O_NONBLOCK );
|
fd = open( dev ? dev : JS_DEV , O_RDONLY | O_NONBLOCK );
|
||||||
if(fd < 0) {
|
if(fd < 0) {
|
||||||
mp_err(log, "Can't open joystick device %s: %s\n",dev ? dev : JS_DEV,strerror(errno));
|
mp_err(log, "Can't open joystick device %s: %s\n",dev ? dev : JS_DEV,strerror(errno));
|
||||||
return -1;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ctx *ctx = talloc_ptrtype(NULL, ctx);
|
struct ctx *ctx = talloc_ptrtype(NULL, ctx);
|
||||||
@@ -88,7 +89,7 @@ int mp_input_joystick_init(struct input_ctx *ictx, struct mp_log *log, char *dev
|
|||||||
MP_ERR(ctx, "Error while reading joystick device: %s\n",strerror(errno));
|
MP_ERR(ctx, "Error while reading joystick device: %s\n",strerror(errno));
|
||||||
close(fd);
|
close(fd);
|
||||||
talloc_free(ctx);
|
talloc_free(ctx);
|
||||||
return -1;
|
return NULL;
|
||||||
}
|
}
|
||||||
l += r;
|
l += r;
|
||||||
}
|
}
|
||||||
@@ -103,8 +104,8 @@ int mp_input_joystick_init(struct input_ctx *ictx, struct mp_log *log, char *dev
|
|||||||
ctx->axis[ev.number] = ev.value;
|
ctx->axis[ev.number] = ev.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
mp_input_add_fd(ictx, fd, 1, NULL, mp_input_joystick_read, close_js, ctx);
|
ctx->fd = fd;
|
||||||
return fd;
|
return ctx;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int mp_input_joystick_read(void *pctx, int fd) {
|
static int mp_input_joystick_read(void *pctx, int fd) {
|
||||||
@@ -118,12 +119,12 @@ static int mp_input_joystick_read(void *pctx, int fd) {
|
|||||||
if(errno == EINTR)
|
if(errno == EINTR)
|
||||||
continue;
|
continue;
|
||||||
else if(errno == EAGAIN)
|
else if(errno == EAGAIN)
|
||||||
return MP_INPUT_NOTHING;
|
return 0;
|
||||||
if( r < 0)
|
if( r < 0)
|
||||||
MP_ERR(ctx, "Error while reading joystick device: %s\n",strerror(errno));
|
MP_ERR(ctx, "Error while reading joystick device: %s\n",strerror(errno));
|
||||||
else
|
else
|
||||||
MP_ERR(ctx, "Error while reading joystick device: %s\n","EOF");
|
MP_ERR(ctx, "Error while reading joystick device: %s\n","EOF");
|
||||||
return MP_INPUT_DEAD;
|
return -1;
|
||||||
}
|
}
|
||||||
l += r;
|
l += r;
|
||||||
}
|
}
|
||||||
@@ -131,7 +132,7 @@ static int mp_input_joystick_read(void *pctx, int fd) {
|
|||||||
if((unsigned int)l < sizeof(struct js_event)) {
|
if((unsigned int)l < sizeof(struct js_event)) {
|
||||||
if(l > 0)
|
if(l > 0)
|
||||||
MP_WARN(ctx, "Joystick: We lose %d bytes of data\n",l);
|
MP_WARN(ctx, "Joystick: We lose %d bytes of data\n",l);
|
||||||
return MP_INPUT_NOTHING;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(ev.type & JS_EVENT_INIT) {
|
if(ev.type & JS_EVENT_INIT) {
|
||||||
@@ -140,14 +141,14 @@ static int mp_input_joystick_read(void *pctx, int fd) {
|
|||||||
if(ev.type == JS_EVENT_BUTTON) {
|
if(ev.type == JS_EVENT_BUTTON) {
|
||||||
int s = (ctx->btns >> ev.number) & 1;
|
int s = (ctx->btns >> ev.number) & 1;
|
||||||
if(s == ev.value) // State is the same : ignore
|
if(s == ev.value) // State is the same : ignore
|
||||||
return MP_INPUT_NOTHING;
|
return 0;
|
||||||
}
|
}
|
||||||
if(ev.type == JS_EVENT_AXIS) {
|
if(ev.type == JS_EVENT_AXIS) {
|
||||||
if( ( ctx->axis[ev.number] == 1 && ev.value > JOY_AXIS_DELTA) ||
|
if( ( ctx->axis[ev.number] == 1 && ev.value > JOY_AXIS_DELTA) ||
|
||||||
(ctx->axis[ev.number] == -1 && ev.value < -JOY_AXIS_DELTA) ||
|
(ctx->axis[ev.number] == -1 && ev.value < -JOY_AXIS_DELTA) ||
|
||||||
(ctx->axis[ev.number] == 0 && ev.value >= -JOY_AXIS_DELTA && ev.value <= JOY_AXIS_DELTA)
|
(ctx->axis[ev.number] == 0 && ev.value >= -JOY_AXIS_DELTA && ev.value <= JOY_AXIS_DELTA)
|
||||||
) // State is the same : ignore
|
) // State is the same : ignore
|
||||||
return MP_INPUT_NOTHING;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -170,11 +171,44 @@ static int mp_input_joystick_read(void *pctx, int fd) {
|
|||||||
ctx->axis[ev.number] = 0;
|
ctx->axis[ev.number] = 0;
|
||||||
return r | MP_KEY_STATE_UP;
|
return r | MP_KEY_STATE_UP;
|
||||||
} else
|
} else
|
||||||
return MP_INPUT_NOTHING;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
MP_WARN(ctx, "Joystick warning unknown event type %d\n",ev.type);
|
MP_WARN(ctx, "Joystick warning unknown event type %d\n",ev.type);
|
||||||
return MP_INPUT_ERROR;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return MP_INPUT_NOTHING;
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void read_joystick_thread(struct mp_input_src *src, void *param)
|
||||||
|
{
|
||||||
|
int wakeup_fd = mp_input_src_get_wakeup_fd(src);
|
||||||
|
struct ctx *ctx = joystick_init(src->input_ctx, src->log, param);
|
||||||
|
|
||||||
|
if (!ctx)
|
||||||
|
return;
|
||||||
|
|
||||||
|
mp_input_src_init_done(src);
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
struct pollfd fds[2] = {
|
||||||
|
{ .fd = ctx->fd, .events = POLLIN },
|
||||||
|
{ .fd = wakeup_fd, .events = POLLIN },
|
||||||
|
};
|
||||||
|
poll(fds, 2, -1);
|
||||||
|
if (!(fds[0].revents & POLLIN))
|
||||||
|
break;
|
||||||
|
int r = mp_input_joystick_read(ctx, ctx->fd);
|
||||||
|
if (r < 0)
|
||||||
|
break;
|
||||||
|
if (r > 0)
|
||||||
|
mp_input_put_key(src->input_ctx, r);
|
||||||
|
}
|
||||||
|
|
||||||
|
close_js(ctx, ctx->fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mp_input_joystick_add(struct input_ctx *ictx, char *dev)
|
||||||
|
{
|
||||||
|
mp_input_add_thread_src(ictx, (void *)dev, read_joystick_thread);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,6 @@
|
|||||||
|
|
||||||
struct input_ctx;
|
struct input_ctx;
|
||||||
struct mp_log;
|
struct mp_log;
|
||||||
int mp_input_joystick_init(struct input_ctx *ictx, struct mp_log *log, char *dev);
|
void mp_input_joystick_add(struct input_ctx *ictx, char *dev);
|
||||||
|
|
||||||
#endif /* MPLAYER_JOYSTICK_H */
|
#endif /* MPLAYER_JOYSTICK_H */
|
||||||
|
|||||||
Reference in New Issue
Block a user