#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <inttypes.h>
#include <fcntl.h>
#include <linux/input.h>
#include <unistd.h>

int main(int argc, char* argv[]) {
    while(true) {
        int c_iHd;
        while((c_iHd = open("/dev/input/event2", O_RDONLY /*| O_NONBLOCK*/)) == -1) {
            printf("trying... ");
            sleep(1);
        }
        /*unsigned long c_uEventBits;
        ioctl(c_iHd, EVIOCGBIT(0, EV_MAX), &c_uEventBits);
        int hasButtonsEvent = (c_uEventBits >> EV_KEY) & 1;
        printf("hasButtonsEvent=%i\n", hasButtonsEvent);*/

        struct input_event ie;
        while(read(c_iHd, &ie, sizeof(struct input_event))>0) {
            if(argc>1) printf("input_event.type=%i, .code=%i, .value=%i\n", ie.type, ie.code, ie.value);
            if(ie.type==EV_KEY && (ie.code==BTN_MIDDLE || ie.code==BTN_EXTRA) && ie.value==0)
        //        system("am start -a android.intent.action.MAIN -n com.android.launcher3/.Launcher");
                system("input keyevent KEYCODE_HOME");
        }
        printf("retrying...\n");
        sleep(1);
    }
}
