Skip to content

Instantly share code, notes, and snippets.

@kangear
Created April 26, 2015 14:35
Show Gist options
  • Save kangear/8823740acf5802ce5afc to your computer and use it in GitHub Desktop.
Save kangear/8823740acf5802ce5afc to your computer and use it in GitHub Desktop.
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>
#include <linux/uinput.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#define ACTION_DOWN 1
#define ACTION_UP 0
/* Globals */
int uinp_fd;
struct uinput_user_dev uinp; // uInput device structure
struct input_event event; // Input device structure
/* Setup the uinput device */
int setup_uinput_device()
{
// Temporary variable
int i=0;
// Open the input device
uinp_fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
if (uinp_fd<0)
{
printf("Unable to open /dev/uinput\n");
//die("error: open");
return -1;
}
// Setup the uinput device
if(ioctl(uinp_fd, UI_SET_EVBIT, EV_KEY)<0)
printf("unable to write");
if(ioctl(uinp_fd, UI_SET_EVBIT, EV_REL)<0)
printf("unable to write");
if(ioctl(uinp_fd, UI_SET_RELBIT, REL_X)<0)
printf("unable to write");
if(ioctl(uinp_fd, UI_SET_RELBIT, REL_Y)<0)
printf("unable to write");
for (i=0; i < 256; i++) {
ioctl(uinp_fd, UI_SET_KEYBIT, i);
}
//ioctl(uinp_fd, UI_SET_KEYBIT, BTN_MOUSE);
//ioctl(uinp_fd, UI_SET_KEYBIT, BTN_TOUCH);
//ioctl(uinp_fd, UI_SET_KEYBIT, BTN_MOUSE);
//ioctl(uinp_fd, UI_SET_KEYBIT, BTN_LEFT);
//ioctl(uinp_fd, UI_SET_KEYBIT, BTN_MIDDLE);
//ioctl(uinp_fd, UI_SET_KEYBIT, BTN_RIGHT);
//ioctl(uinp_fd, UI_SET_KEYBIT, BTN_FORWARD);
//ioctl(uinp_fd, UI_SET_KEYBIT, BTN_BACK);
memset(&uinp,0,sizeof(uinp)); // Intialize the uInput device to NULL
snprintf(uinp.name, UINPUT_MAX_NAME_SIZE, "uinput-sample");
uinp.id.bustype = BUS_USB;
uinp.id.vendor = 0x1;
uinp.id.product = 0x1;
uinp.id.version = 1;
/* Create input device into input sub-system */
if(write(uinp_fd, &uinp, sizeof(uinp))< 0)
printf("Unable to write UINPUT device.");
if (ioctl(uinp_fd, UI_DEV_CREATE)< 0)
{
printf("Unable to create UINPUT device.");
//die("error: ioctl");
return -1;
}
return 1;
}
void send_click_events( )
{
// Move pointer to (0,0) location
memset(&event, 0, sizeof(event));
gettimeofday(&event.time, NULL);
event.type = EV_REL;
event.code = REL_X;
event.value = 100;
write(uinp_fd, &event, sizeof(event));
event.type = EV_REL;
event.code = REL_Y;
event.value = 100;
write(uinp_fd, &event, sizeof(event));
event.type = EV_SYN;
event.code = SYN_REPORT;
event.value = 0;
write(uinp_fd, &event, sizeof(event));
// Report BUTTON CLICK - PRESS event
memset(&event, 0, sizeof(event));
gettimeofday(&event.time, NULL);
event.type = EV_KEY;
event.code = BTN_LEFT;
event.value = 1;
write(uinp_fd, &event, sizeof(event));
event.type = EV_SYN;
event.code = SYN_REPORT;
event.value = 0;
write(uinp_fd, &event, sizeof(event));
// Report BUTTON CLICK - RELEASE event
memset(&event, 0, sizeof(event));
gettimeofday(&event.time, NULL);
event.type = EV_KEY;
event.code = BTN_LEFT;
event.value = 0;
write(uinp_fd, &event, sizeof(event));
event.type = EV_SYN;
event.code = SYN_REPORT;
event.value = 0;
write(uinp_fd, &event, sizeof(event));
}
/**
* send_button
* key_code
* is_down
* Return EXIT_SUCCESS, or EXIT_FAILURE.
*/
int send_keyevent(int key_code, int is_down) {
int ret;
// Report BUTTON CLICK - event
memset(&event, 0, sizeof(event));
gettimeofday(&event.time, NULL);
event.type = EV_KEY;
event.code = key_code;
event.value = is_down;
ret = write(uinp_fd, &event, sizeof(event));
if(ret == -1)
goto err;
event.type = EV_SYN;
event.code = SYN_REPORT;
event.value = 0;
ret = write(uinp_fd, &event, sizeof(event));
if(ret == -1)
goto err;
return EXIT_SUCCESS;
err:
return EXIT_FAILURE;
}
void send_a_button()
{
send_keyevent(KEY_LEFT, ACTION_DOWN);
send_keyevent(KEY_LEFT, ACTION_UP);
}
/* This function will open the uInput device. Please make
sure that you have inserted the uinput.ko into kernel. */
int main()
{
// Return an error if device not found.
if (setup_uinput_device() < 0)
{
printf("Unable to find uinput device\n");
return -1;
}
sleep(1);
send_a_button(); // Send a "A" key
// send_click_events(); // Send mouse event
/* Destroy the input device */
ioctl(uinp_fd, UI_DEV_DESTROY);
/* Close the UINPUT device */
close(uinp_fd);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment