Skip to content

Commit

Permalink
Merge remote-tracking branch 'tomblind/touchscreen'
Browse files Browse the repository at this point in the history
  • Loading branch information
irtimmer committed Apr 18, 2020
2 parents b22b07e + 5beb32d commit 4711129
Showing 1 changed file with 67 additions and 4 deletions.
71 changes: 67 additions & 4 deletions src/input/evdev.c
Expand Up @@ -57,14 +57,20 @@ struct input_device {
struct libevdev *dev;
bool is_keyboard;
bool is_mouse;
<<<<<<< HEAD
int rotate;
=======
bool is_touchscreen;
>>>>>>> tomblind/touchscreen
struct mapping* map;
int key_map[KEY_MAX];
int abs_map[ABS_MAX];
int hats_state[3][2];
int fd;
char modifiers;
__s32 mouseDeltaX, mouseDeltaY, mouseScroll;
__s32 touchDownX, touchDownY, touchX, touchY;
struct timeval touchDownTime;
short controllerId;
int haptic_effect_id;
int buttonFlags;
Expand All @@ -83,6 +89,11 @@ static const int hat_constants[3][3] = {{HAT_UP | HAT_LEFT, HAT_UP, HAT_UP | HAT

#define set_hat(flags, flag, hat, hat_flag) flags = (hat & hat_flag) == hat_flag ? flags | flag : flags & ~flag

#define TOUCH_UP -1
#define TOUCH_CLICK_RADIUS 10
#define TOUCH_CLICK_DELAY 100000 // microseconds
#define TOUCH_RCLICK_TIME 750 // milliseconds

static struct input_device* devices = NULL;
static int numDevices = 0;
static int assignedControllerIds = 0;
Expand Down Expand Up @@ -273,6 +284,27 @@ static bool evdev_handle_event(struct input_event *ev, struct input_device *dev)
case BTN_EXTRA:
mouseCode = BUTTON_X2;
break;
case BTN_TOUCH:
if (ev->value == 1) {
dev->touchDownTime = ev->time;
} else {
if (dev->touchDownX != TOUCH_UP && dev->touchDownY != TOUCH_UP) {
int deltaX = dev->touchX - dev->touchDownX;
int deltaY = dev->touchY - dev->touchDownY;
if (deltaX * deltaX + deltaY * deltaY < TOUCH_CLICK_RADIUS * TOUCH_CLICK_RADIUS) {
struct timeval elapsedTime;
timersub(&ev->time, &dev->touchDownTime, &elapsedTime);
int holdTimeMs = elapsedTime.tv_sec * 1000 + elapsedTime.tv_usec / 1000;
int button = holdTimeMs >= TOUCH_RCLICK_TIME ? BUTTON_RIGHT : BUTTON_LEFT;
LiSendMouseButtonEvent(BUTTON_ACTION_PRESS, button);
usleep(TOUCH_CLICK_DELAY);
LiSendMouseButtonEvent(BUTTON_ACTION_RELEASE, button);
}
}
dev->touchDownX = TOUCH_UP;
dev->touchDownY = TOUCH_UP;
}
break;
default:
gamepadModified = true;
if (dev->map == NULL)
Expand Down Expand Up @@ -343,6 +375,30 @@ static bool evdev_handle_event(struct input_event *ev, struct input_device *dev)
}
break;
case EV_ABS:
if (dev->is_touchscreen) {
switch (ev->code) {
case ABS_X:
if (dev->touchDownX == TOUCH_UP) {
dev->touchDownX = ev->value;
dev->touchX = ev->value;
} else {
dev->mouseDeltaX += (ev->value - dev->touchX);
dev->touchX = ev->value;
}
break;
case ABS_Y:
if (dev->touchDownY == TOUCH_UP) {
dev->touchDownY = ev->value;
dev->touchY = ev->value;
} else {
dev->mouseDeltaY += (ev->value - dev->touchY);
dev->touchY = ev->value;
}
break;
}
break;
}

if (dev->map == NULL)
break;

Expand Down Expand Up @@ -518,13 +574,14 @@ void evdev_create(const char* device, struct mapping* mappings, bool verbose, in

bool is_keyboard = libevdev_has_event_code(evdev, EV_KEY, KEY_Q);
bool is_mouse = libevdev_has_event_type(evdev, EV_REL) || libevdev_has_event_code(evdev, EV_KEY, BTN_LEFT);
bool is_touchscreen = libevdev_has_event_code(evdev, EV_KEY, BTN_TOUCH);

if (mappings == NULL && !(is_keyboard || is_mouse)) {
if (mappings == NULL && !(is_keyboard || is_mouse || is_touchscreen)) {
fprintf(stderr, "No mapping available for %s (%s) on %s\n", name, str_guid, device);
mappings = default_mapping;
}

if (!is_keyboard && !is_mouse)
if (!is_keyboard && !is_mouse && !is_touchscreen)
evdev_gamepads++;

int dev = numDevices;
Expand All @@ -549,7 +606,13 @@ void evdev_create(const char* device, struct mapping* mappings, bool verbose, in
memset(&devices[dev].abs_map, -2, sizeof(devices[dev].abs_map));
devices[dev].is_keyboard = is_keyboard;
devices[dev].is_mouse = is_mouse;
<<<<<<< HEAD
devices[dev].rotate = rotate;
=======
devices[dev].is_touchscreen = is_touchscreen;
devices[dev].touchDownX = TOUCH_UP;
devices[dev].touchDownY = TOUCH_UP;
>>>>>>> tomblind/touchscreen

int nbuttons = 0;
for (int i = BTN_JOYSTICK; i < KEY_MAX; ++i) {
Expand Down Expand Up @@ -584,7 +647,7 @@ void evdev_create(const char* device, struct mapping* mappings, bool verbose, in
fprintf(stderr, "Mapping for %s (%s) on %s is incorrect\n", name, str_guid, device);
}

if (grabbingDevices && (is_keyboard || is_mouse)) {
if (grabbingDevices && (is_keyboard || is_mouse || is_touchscreen)) {
if (ioctl(fd, EVIOCGRAB, 1) < 0) {
fprintf(stderr, "EVIOCGRAB failed with error %d\n", errno);
}
Expand Down Expand Up @@ -711,7 +774,7 @@ void evdev_start() {
// we're ready to take input events. Ctrl+C works up until
// this point.
for (int i = 0; i < numDevices; i++) {
if ((devices[i].is_keyboard || devices[i].is_mouse) && ioctl(devices[i].fd, EVIOCGRAB, 1) < 0) {
if ((devices[i].is_keyboard || devices[i].is_mouse || devices[i].is_touchscreen) && ioctl(devices[i].fd, EVIOCGRAB, 1) < 0) {
fprintf(stderr, "EVIOCGRAB failed with error %d\n", errno);
}
}
Expand Down

0 comments on commit 4711129

Please sign in to comment.