Skip to content

Instantly share code, notes, and snippets.

@amitn
Created March 20, 2014 15:39
Show Gist options
  • Save amitn/9666613 to your computer and use it in GitHub Desktop.
Save amitn/9666613 to your computer and use it in GitHub Desktop.
look ma no hands (and cables)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/input.h>
#include <errno.h>
#include <time.h>
//adb shell getevent -pl
#define INPUT_DEV_PATH "/dev/input/event4"
#define ABS_MT_SLOT 0x2f /* MT slot being modified */
#define ABS_MT_TOUCH_MAJOR 0x30 /* Major axis of touching ellipse */
#define ABS_MT_TOUCH_MINOR 0x31 /* Minor axis (omit if circular) */
#define ABS_MT_WIDTH_MAJOR 0x32 /* Major axis of approaching ellipse */
#define ABS_MT_WIDTH_MINOR 0x33 /* Minor axis (omit if circular) */
#define ABS_MT_ORIENTATION 0x34 /* Ellipse orientation */
#define ABS_MT_POSITION_X 0x35 /* Center X touch position */
#define ABS_MT_POSITION_Y 0x36 /* Center Y touch position */
#define ABS_MT_TOOL_TYPE 0x37 /* Type of touching device */
#define ABS_MT_BLOB_ID 0x38 /* Group a set of packets as a blob */
#define ABS_MT_TRACKING_ID 0x39 /* Unique ID of initiated contact */
#define ABS_MT_PRESSURE 0x3a /* Pressure on contact area */
#define ABS_MT_DISTANCE 0x3b /* Contact hover distance */
#define ABS_MT_TOOL_X 0x3c /* Center X tool position */
#define ABS_MT_TOOL_Y 0x3d /* Center Y tool position */
static struct input_event syn_report;
inline void send_syn_report(int fd){
write(fd, &syn_report, sizeof(syn_report));
}
inline long nanoseconds_to_milliseconds(long secs)
{
return secs/1000000;
}
static struct timespec t_time_now;
inline long get_now(){
clock_gettime(CLOCK_MONOTONIC, &t_time_now);
return t_time_now.tv_sec*1000 + nanoseconds_to_milliseconds(t_time_now.tv_nsec);
}
inline int lerp(int a, int b, float alpha){
return (b-a) * alpha + a;
}
static void send_swipe(int fd, int x1, int y1, int x2, int y2){
struct input_event event;
long time_start;
long time_now;
long duration = 300;
memset(&event, 0, sizeof(event));
event.type = EV_ABS;
event.code = ABS_MT_SLOT;
event.value = 0;
write(fd, &event, sizeof(event));
event.code = ABS_MT_TRACKING_ID;
event.value = 5959;
write(fd, &event, sizeof(event));
event.code = ABS_MT_TOUCH_MAJOR;
event.value = 60;
write(fd, &event, sizeof(event));
event.code = ABS_MT_WIDTH_MAJOR;
event.value = 20;
write(fd, &event, sizeof(event));
event.code = ABS_MT_PRESSURE;
event.value = 20;
write(fd, &event, sizeof(event));
event.code = ABS_MT_POSITION_X;
event.value = x1;
write(fd, &event, sizeof(event));
event.code = ABS_MT_POSITION_Y;
event.value = y1;
write(fd, &event, sizeof(event));
send_syn_report(fd);
time_now = time_start = get_now();
long time_end = time_start + duration;
while (time_now < time_end) {
long time_elapsed = time_now - time_start;
float alpha = (float) time_elapsed / duration;
event.code = ABS_MT_POSITION_X;
event.value = lerp(x1, x2, alpha);
write(fd, &event, sizeof(event));
event.code = ABS_MT_POSITION_Y;
event.value = lerp(y1, y2, alpha);
write(fd, &event, sizeof(event));
send_syn_report(fd);
time_now = get_now();
}
event.code = ABS_MT_TRACKING_ID;
event.value = -1;
write(fd, &event, sizeof(event));
send_syn_report(fd);
}
//ABS_MT_POSITION_X : value 0, min 0, max 1088, fuzz 0, flat 0, resolution 0
//ABS_MT_POSITION_Y : value 0, min 0, max 1770,
inline void swipe_r(int fd){
send_swipe(fd, 544, 885, 544+300, 885);
}
inline void swipe_l(int fd){
send_swipe(fd, 544, 885, 544-300, 885);
}
inline void swipe_u(int fd){
send_swipe(fd, 544, 885, 544, 885-300);
}
inline void swipe_d(int fd){
send_swipe(fd, 544, 885, 544, 885+300);
}
int main()
{
int fd;
int version;
struct input_event event;
syn_report.type = EV_SYN;
syn_report.code = SYN_REPORT;
syn_report.value = 0;
fd = open(INPUT_DEV_PATH, O_RDWR);
memset(&event, 0, sizeof(event));
int d;
int c = 50;
while(c-- > 0){
d = rand() % 4;
switch(d){
case 0:
swipe_d(fd);
break;
case 1:
swipe_l(fd);
break;
case 2:
swipe_r(fd);
break;
case 3:
swipe_u(fd);
break;
}
usleep(500000);
}
rand();
close(fd);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment