#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <unistd.h>

#include <linux/input.h>
#include <linux/uinput.h>

static void insertEvent(int fdunsigned short typeunsigned short codeint value) {
        struct input_event e;
        memset(&e0sizeof(e));
        e.type = type;
        e.code = code;
        e.value = value;
        write(fd, &esizeof(e));
}

static int uifd;
static void inputEvent(int fdunsigned short keyint value) {
        insertEvent(fdEV_KEYkeyvalue);
        insertEvent(fdEV_SYNSYN_REPORT0);
}

void eventPress(unsigned short key){ inputEvent(uifdkey1); }
void eventRelease(unsigned short key){ inputEvent(uifdkey0); }

int main(int argcchar **argvchar **envp) {
        int fd=open("/dev/input/uinput"O_RDWR);
        if(fd<0) {
                perror("Opening uinput device");
                exit(-1);
        }
        uifd=fd;
        struct uinput_user_dev *dev_setup=malloc(sizeof(struct uinput_user_dev));
        strncpy(dev_setup->name"Twin WiFi remote controller"UINPUT_MAX_NAME_SIZE);
        dev_setup->id.bustype=0x6;
        dev_setup->id.vendor=1;
        dev_setup->id.product=1;
        dev_setup->id.version=1;
        dev_setup->ff_effects_max=0;
        write(fddev_setupsizeof(*dev_setup));
        if(ioctl(fdUI_SET_EVBITEV_KEY) < 0) {
                perror("error on uinput SET_EVBIT");
                close(fd);
                return -1;
        }
        if(ioctl(fdUI_SET_PHYS"/I/Y/A") < 0) {
                perror("error on uinput SET_PHYS");
                close(fd);
                return -1;
        }
        int i;
        if (ioctl(fdUI_SET_KEYBITKEY_UP)<0) {
                perror("error on uinput SET_KEYBIT");
                close(fd);
        }
        ioctl(fdUI_DEV_CREATENULL);
        //Let's send some keys
        //struct input_event ev;
        for(i=0;i<100;i++) {
                eventPress(KEY_UP);
                eventRelease(KEY_UP);
                sleep(1);
        }
        ioctl(fdUI_DEV_DESTROYNULL);
        return 0;
}