repeat for uinput, use execl to start agetty.

This commit is contained in:
sorgelig
2019-05-28 07:14:25 +08:00
parent b1a1f16575
commit ce7bda68dc
2 changed files with 102 additions and 70 deletions

168
input.cpp
View File

@@ -9,12 +9,12 @@
#include <sys/sysinfo.h>
#include <dirent.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/input.h>
#include <linux/uinput.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/input.h>
#include <linux/uinput.h>
#include <sys/time.h>
#include <sys/types.h>
#include "input.h"
#include "user_io.h"
@@ -1376,68 +1376,98 @@ static uint32_t mouse_timer = 0;
#define BTN_TGL 100
#define BTN_OSD 101
static int uinp_fd = -1;
static int input_uinp_setup()
{
if (uinp_fd <= 0)
{
struct uinput_user_dev uinp;
if (!(uinp_fd = open("/dev/uinput", O_WRONLY | O_NDELAY)))
{
printf("Unable to open /dev/uinput\n");
uinp_fd = -1;
return 0;
}
memset(&uinp, 0, sizeof(uinp));
strncpy(uinp.name, UINPUT_NAME, UINPUT_MAX_NAME_SIZE);
uinp.id.version = 4;
uinp.id.bustype = BUS_USB;
ioctl(uinp_fd, UI_SET_EVBIT, EV_KEY);
for (int i = 0; i < 256; i++) ioctl(uinp_fd, UI_SET_KEYBIT, i);
write(uinp_fd, &uinp, sizeof(uinp));
if (ioctl(uinp_fd, UI_DEV_CREATE))
{
printf("Unable to create UINPUT device.");
close(uinp_fd);
uinp_fd = -1;
return 0;
}
}
return 1;
}
void input_uinp_destroy()
{
if (uinp_fd > 0)
{
ioctl(uinp_fd, UI_DEV_DESTROY);
close(uinp_fd);
uinp_fd = -1;
}
}
static void uinp_send_key(uint16_t key, int press)
{
if (uinp_fd > 0)
{
static struct input_event event;
memset(&event, 0, sizeof(event));
gettimeofday(&event.time, NULL);
event.type = EV_KEY;
event.code = key;
event.value = press ? 1 : 0;
write(uinp_fd, &event, sizeof(event));
event.type = EV_SYN;
event.code = SYN_REPORT;
event.value = 0;
write(uinp_fd, &event, sizeof(event));
}
}
static int uinp_fd = -1;
static int input_uinp_setup()
{
if (uinp_fd <= 0)
{
struct uinput_user_dev uinp;
if (!(uinp_fd = open("/dev/uinput", O_WRONLY | O_NDELAY)))
{
printf("Unable to open /dev/uinput\n");
uinp_fd = -1;
return 0;
}
memset(&uinp, 0, sizeof(uinp));
strncpy(uinp.name, UINPUT_NAME, UINPUT_MAX_NAME_SIZE);
uinp.id.version = 4;
uinp.id.bustype = BUS_USB;
ioctl(uinp_fd, UI_SET_EVBIT, EV_KEY);
for (int i = 0; i < 256; i++) ioctl(uinp_fd, UI_SET_KEYBIT, i);
write(uinp_fd, &uinp, sizeof(uinp));
if (ioctl(uinp_fd, UI_DEV_CREATE))
{
printf("Unable to create UINPUT device.");
close(uinp_fd);
uinp_fd = -1;
return 0;
}
}
return 1;
}
void input_uinp_destroy()
{
if (uinp_fd > 0)
{
ioctl(uinp_fd, UI_DEV_DESTROY);
close(uinp_fd);
uinp_fd = -1;
}
}
static unsigned long uinp_repeat = 0;
static struct input_event uinp_ev;
static void uinp_send_key(uint16_t key, int press)
{
if (uinp_fd > 0)
{
if (!uinp_ev.value && press)
{
uinp_repeat = GetTimer(REPEATDELAY);
}
memset(&uinp_ev, 0, sizeof(uinp_ev));
gettimeofday(&uinp_ev.time, NULL);
uinp_ev.type = EV_KEY;
uinp_ev.code = key;
uinp_ev.value = press;
write(uinp_fd, &uinp_ev, sizeof(uinp_ev));
static struct input_event ev;
ev.time = uinp_ev.time;
ev.type = EV_SYN;
ev.code = SYN_REPORT;
ev.value = 0;
write(uinp_fd, &ev, sizeof(ev));
}
}
static void uinp_check_key()
{
if (uinp_fd > 0)
{
if (!grabbed)
{
if (uinp_ev.value && CheckTimer(uinp_repeat))
{
uinp_repeat = GetTimer(REPEATRATE);
uinp_send_key(uinp_ev.code, 2);
}
}
else
{
if (uinp_ev.value)
{
uinp_send_key(uinp_ev.code, 0);
}
}
}
}
static void mouse_cb(unsigned char b, int16_t x = 0, int16_t y = 0, int16_t w = 0)
{
@@ -2971,6 +3001,8 @@ int input_poll(int getchar)
int ret = input_test(getchar);
if (getchar) return ret;
uinp_check_key();
static int prev_dx = 0;
static int prev_dy = 0;

View File

@@ -3952,8 +3952,8 @@ void HandleUI(void)
ttypid = fork();
if (!ttypid)
{
system("/sbin/agetty -a root -l /tmp/script --nohostname -L tty2 xterm");
exit(0);
execl("/sbin/agetty", "/sbin/agetty", "-a", "root", "-l", "/tmp/script", "--nohostname", "-L", "tty2", "xterm", NULL);
exit(0); //should never be reached
}
}
else