Skip to content

Commit

Permalink
Revert 5b9de6c
Browse files Browse the repository at this point in the history
Revert 5b9de6c to avoid xinput issues this commit mitigates against.
While such a race condition is theoretically possible, evidence for it
has yet to emerge, and routing events through the virutal keyboard causes
the xinput issue fixed in 88c8475.
  • Loading branch information
rvaiya committed Jun 22, 2022
1 parent bb49007 commit 393aefa
Showing 1 changed file with 33 additions and 24 deletions.
57 changes: 33 additions & 24 deletions src/vkbd/uinput.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,6 @@ static int create_virtual_keyboard(const char *name)
}
}

for (code = BTN_LEFT; code <= BTN_TASK; code++)
ioctl(fd, UI_SET_KEYBIT, code);

for (code = BTN_0; code <= BTN_9; code++)
ioctl(fd, UI_SET_KEYBIT, code);

udev.id.bustype = BUS_USB;
udev.id.vendor = 0x0FAC;
udev.id.product = 0x0ADE;
Expand Down Expand Up @@ -148,6 +142,15 @@ static int create_virtual_pointer(const char *name)
return fd;
}

static void init_pfd(const struct vkbd *vkbd)
{
if (vkbd->pfd == -1) {
((struct vkbd *)vkbd)->pfd = create_virtual_pointer("keyd virtual pointer");
/* Give the device time to propagate up the input stack. */
usleep(100000);
}
}

static long get_time_ms()
{
struct timespec ts;
Expand Down Expand Up @@ -176,35 +179,51 @@ void write_key_event(const struct vkbd *vkbd, uint8_t code, int state)
{
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
struct input_event ev;
int fd;
int is_btn;

pthread_mutex_lock(&mtx);


fd = vkbd->fd;

ev.type = EV_KEY;

is_btn = 1;
switch (code) {
case KEYD_LEFT_MOUSE: ev.code = BTN_LEFT; break;
case KEYD_MIDDLE_MOUSE: ev.code = BTN_MIDDLE; break;
case KEYD_RIGHT_MOUSE: ev.code = BTN_RIGHT; break;
case KEYD_MOUSE_1: ev.code = BTN_SIDE; break;
case KEYD_MOUSE_2: ev.code = BTN_EXTRA; break;
default: ev.code = code; break;
default:
ev.code = code;
is_btn = 0;
break;
}

/*
* Send all buttons through the virtual pointer
* to prevent X from identifying the virtual
* keyboard as a mouse.
*/
if (is_btn) {
init_pfd(vkbd);
fd = vkbd->pfd;
}

ev.value = state;

ev.time.tv_sec = 0;
ev.time.tv_usec = 0;

write(vkbd->fd, &ev, sizeof(ev));
write(fd, &ev, sizeof(ev));

ev.type = EV_SYN;
ev.code = 0;
ev.value = 0;


write(vkbd->fd, &ev, sizeof(ev));
write(fd, &ev, sizeof(ev));

pthread_mutex_unlock(&mtx);
}
Expand Down Expand Up @@ -254,13 +273,9 @@ struct vkbd *vkbd_init(const char *name)

void vkbd_mouse_move(const struct vkbd *vkbd, int x, int y)
{
struct input_event ev;
init_pfd(vkbd);

if (vkbd->pfd == -1) {
((struct vkbd *)vkbd)->pfd = create_virtual_pointer("keyd virtual pointer");
/* Give the device time to propagate up the input stack. */
usleep(100000);
}
struct input_event ev;

if (x) {
ev.type = EV_REL;
Expand Down Expand Up @@ -293,10 +308,7 @@ void vkbd_mouse_move(const struct vkbd *vkbd, int x, int y)

void vkbd_mouse_scroll(const struct vkbd *vkbd, int x, int y)
{
if (vkbd->pfd == -1) {
((struct vkbd *)vkbd)->pfd = create_virtual_pointer("keyd virtual pointer");
usleep(100000);
}
init_pfd(vkbd);

struct input_event ev;

Expand Down Expand Up @@ -327,10 +339,7 @@ void vkbd_mouse_scroll(const struct vkbd *vkbd, int x, int y)

void vkbd_mouse_move_abs(const struct vkbd *vkbd, int x, int y)
{
if (vkbd->pfd == -1) {
((struct vkbd *)vkbd)->pfd = create_virtual_pointer("keyd virtual pointer");
usleep(100000);
}
init_pfd(vkbd);

struct input_event ev;

Expand Down

0 comments on commit 393aefa

Please sign in to comment.