/*
InsomniaEXE v20230518
By DarkRanger <darkranger.no-ip.org>
License: MIT License
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>

#define APP_NAME "InsomniaEXE"
#define APP_VERSION "20230518"
#define DEFAULT_INTERVAL 60

char *get_time(void) {
    time_t rawtime;
    struct tm *timeinfo;
    static char current_time[30];
    
    time(&rawtime);
    timeinfo = localtime(&rawtime);
    strftime(current_time, 30, "%Y-%m-%d %H:%M:%S", timeinfo);
    
    return current_time;
}

int insomnia(void) {
    SetForegroundWindow(GetConsoleWindow());
    int not = 2;
    int keydown = 0;
    int keyup = 0;
    INPUT ie;
    ie.type = INPUT_KEYBOARD;
    ie.ki.wVk = VK_LWIN;

    printf("%s ", get_time());

    for (int counter = 0; counter < not; counter++) {
        ie.ki.dwFlags = 0;
        keydown = SendInput(1, &ie, sizeof(INPUT));
        ie.ki.dwFlags = KEYEVENTF_KEYUP;
        keyup = SendInput(1, &ie, sizeof(INPUT));
        printf("%d %d ", keydown, keyup);
        Sleep(500);
    }

    printf("\n");
    ShowWindow(GetConsoleWindow(), 6);
}

int main(int argc, char *argv[]) {
    printf("%s v%s\n", APP_NAME, APP_VERSION);
    int second = DEFAULT_INTERVAL;

    if (argc > 1) {
        second = atoi(argv[1]);
    }

    while (1) {
        insomnia();
        Sleep(second * 1000);
    }

    return 0;
}