#include <unistd.h>
#include <stdio.h> 
#include <string.h>
#include <stdlib.h>
#include <errno.h>

/*unsigned char button_name(unsigned char code){
    switch(code) {
        case 0x74: return 'P';
        case 0x73: return 'U';
        case 0x72: return 'D';
        case 0xd7: return 'p';
        case 0xd4: return 'P';
        case 0x9e: return 'B';
        case 0x66: return 'H';
        case 0xe5: return 'M';
    }
}*/

const char* scriptpath;
unsigned char buttonstates[0x100] = {0};

void runscript(char* scripname) {
    char buf[1024];
    snprintf(buf, 1024, "%s/%s", scriptpath, scripname);
    buf[1024-1]=0;
    system(buf);
}

int main(int argc, char* argv[]) {
    char buf[16];
    int disabled = 3;
    unsigned char d_seconds = 0;
    int keybutton;
    
    char buffer[256];
    int cursor = 0;
    
    
    if (argc<3) {
       fprintf(stderr, "Usage: andrhard keybutton /path/to/scripts/directory < /dev/input/eventX\n"
       "You should press keybutton, release it and press again fast enough to"
       "activate andrhard (it will call script $SCRIPTPATH/enabled). Then"
       " you should press some keystroke (without releasing keybutton) "
       "and andrhard will call $SCRIPTPATH/$PRESSED_BUTTONS.\n"
       "$RRESSED_BUTTONS is concatenated hex codes of buttons you pressed before releasing the keybutton\n"
        "You can just start the program and see \"/path/to/scripts/7272D7: not found\" replies.\n"
        );
        return 1;
    }
    
    sscanf(argv[1], "%02x", &keybutton);
    scriptpath = argv[2];
    
    for(;;) {
        int ret = read(0, buf, 16);
        if( ret == 0 || ret == -1) {
            if (errno == EINTR || errno == EAGAIN) continue;
            break;
        }
        unsigned char button = buf[10];
        unsigned char state = buf[12];
        unsigned char seconds = buf[0];
        
        // Enable when "Home" button is pressed twice (and holded)
        if (button == keybutton && state == 1 && disabled == 3) {
            d_seconds = seconds;
            disabled = 2;
        } else
        if (button == keybutton && state == 0 && d_seconds == seconds && disabled == 2) {
            disabled = 1;
        } else
        if (button == keybutton && state != 0 && d_seconds == seconds && disabled == 1) {
            disabled = 0;
            runscript("enabled");
        } else 
        if (button == keybutton && state == 0 && (disabled < 2 || d_seconds != seconds)) {
            disabled = 3;
            memset(buttonstates, 0, sizeof buttonstates);
            if(cursor) {
                buffer[cursor]=0;
                runscript(buffer);
                cursor=0;
            }
            runscript("disabled");
        } 
        
        if (button != keybutton && disabled == 0) {
            if (state != 0) {
                buttonstates[button] = 1;
            } else {
                if (buttonstates[button]) {
                    buttonstates[button] = 2;
                    unsigned char upper_nibble = button>>4;
                    unsigned char lower_nibble = button&0xF;
                    buffer[cursor++] = (upper_nibble<10)?upper_nibble+'0':upper_nibble-10+'A';
                    buffer[cursor++] = (lower_nibble<10)?lower_nibble+'0':lower_nibble-10+'A';
                    //fprintf(stderr, "%c%c  %02x%02x\n", buffer[cursor-2], buffer[cursor-1], buffer[cursor-2], buffer[cursor-1]);
                }
            }
        }
        
    }
    return 0;
}