<commit_before>/*
 * What had happened to him? Why was his brow clammy with drops of moisture,
 * his knees shaking beneath him, and his soul oppressed with a cold gloom?
 * Was it because he had just seen these dreadful eyes again? Why, he had
 * left the Summer Garden on purpose to see them; that had been his "idea."
 * He had wished to assure himself that he would see them once more at that
 * house. Then why was he so overwhelmed now, having seen them as he expected?
 *
 * "The Idiot" (pt. II, ch. V) - <NAME>
 */

#include <X11/extensions/Xrandr.h>
#include <X11/extensions/Xfixes.h>
#include <X11/extensions/XInput2.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <stdbool.h>
#include <stdlib.h>
#include <getopt.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <stdio.h>
#include <fcntl.h>

void
check_extensions (Display *dpy) {
    int maj = 2,
        min = 2,
        opc = -1,
        ev, err;

    if (!XQueryExtension(dpy, "XInputExtension", &opc, &ev, &err)) {
        printf("Extension XInput not found.");
        exit(0);
    }

    if (XIQueryVersion(dpy, &maj, &min) != Success) {
        printf("Extension XI2 not found, or unsupported version");
        exit(0);
    }
}

static long
get_brightness (Display *dpy, Atom backlight, RROutput out) {

    unsigned long after, items;
    unsigned char *property;
    long brightness;
    int format;
    Atom type;

    if (XRRGetOutputProperty(dpy, out, backlight, 0, 4, False, False, None, &type, &format, &items, &after, &property) != Success) {
        return -1;
    }

    brightness = *((long *) property);
    XFree(property);
    return brightness;
}

static void
set_brightness (Display *dpy, Atom backlight, RROutput out, long brightness) {

    XRRChangeOutputProperty(dpy, out, backlight, XA_INTEGER, 32, PropModeReplace, (unsigned char *) &brightness, 1);
    XFlush(dpy);
}

static bool
battery_status () {
    FILE *f;
    char stat[12];
    if ((f = fopen("/sys/class/power_supply/BAT0/status", "r")) == NULL) {
        return false;
    }
    fgets(stat, 12, f);
    fclose(f);
    return (strcmp(stat, "Discharging") == 0) ? true : false;
}

int
main (int argc, char *argv[]) {

    int current, arg_index = 0;
    unsigned int cursor_idle = 3,
                 screen_idle = 45,
                 screen_fade = 50,
                 lock_idle = 600,
                 battery_int = 5,
                 bidle = 0,
                 kidle = 0,
                 lidle = 0,
                 midle = 0;

    bool cursor_conf = false,
         screen_conf = false,
         lock_conf = false,
         battery_conf = true,
         battery = false,
         dim = false,
         cursor = true,
         locked = false,
         paused = false;

    void pause() { paused = !paused; }
    signal(SIGUSR1, pause);

    long brightness;

    static struct option long_options[] = {
        { "cursor", optional_argument, 0, 'c' },
        { "screen", optional_argument, 0, 's' },
        { "fadeto", optional_argument, 0, 'f' },
        { "lock", optional_argument, 0, 'l' },
        { "locker", optional_argument, 0, 'k' },
        { "always", no_argument, 0, 'a' },
        { "help", no_argument, 0, 'h' },
        { 0, 0, 0, 0}
    };

    if (argc == 1) {
        printf("Too few arguments! -h for help\n");
        return (1);
    } else {
        while ((current = getopt_long(argc, argv, "c::s::l::ah", long_options, &arg_index)) != -1) {

            switch (current) {
                // set cursor timeout
                case 'c':
                    cursor_conf = true;
                    cursor_idle = (optarg == NULL) ? cursor_idle : strtol(optarg, NULL, 10);
                    break;
                // set screen timeout
                case 's':
                    screen_conf = true;
                    screen_idle = (optarg == NULL) ? screen_idle : strtol(optarg, NULL, 10);
                    break;
                // set lock timeout
                case 'l':
                    lock_conf = true;
                    lock_idle = (optarg == NULL) ? lock_idle : strtol(optarg, NULL, 10);
                    break;
                // set locker
                case 'k':
                    lock_conf = true;
                    // get locking script
                    break;
                // set screen brightness
                case 'f':
                    screen_conf = true;
                    screen_fade = (optarg == NULL) ? screen_fade : strtol(optarg, NULL, 10);
                    break;
                // always dim
                case 'a':
                    battery_conf = false;
                    battery = true;
                    break;
                // show help
                case 'h':
                default:
                    printf("Usage: gloom [-c|--cursor <secs>] [-s|--screen <secs>] [-f|--fadeto <brightness percent> ] [-a|--always] [-h|--help]");
                    break;
            }
        }
    }

    Display *dpy = XOpenDisplay(NULL);
    if (!dpy) {
        printf("Cannot connect to the X Server\n");
        exit(0);
    }
    check_extensions(dpy);

    Window w = DefaultRootWindow(dpy);
    Atom backlight = XInternAtom(dpy, "Backlight", True);
    XRRScreenResources *resources = XRRGetScreenResources(dpy, w);
    XIEventMask masks[1];
    unsigned char mask[(XI_LASTEVENT + 7)/8];
    memset(mask, 0, sizeof(mask));
    XISetMask(mask, XI_RawMotion);
    XISetMask(mask, XI_RawButtonPress);
    XISetMask(mask, XI_RawKeyPress);
    masks[0].deviceid = XIAllMasterDevices;<commit_msg> getopt begone!<commit_after>/*
 * What had happened to him? Why was his brow clammy with drops of moisture,
 * his knees shaking beneath him, and his soul oppressed with a cold gloom?
 * Was it because he had just seen these dreadful eyes again? Why, he had
 * left the Summer Garden on purpose to see them; that had been his "idea."
 * He had wished to assure himself that he would see them once more at that
 * house. Then why was he so overwhelmed now, having seen them as he expected?
 *
 * "The Idiot" (pt. II, ch. V) - <NAME>
 */

#include <X11/extensions/Xrandr.h>
#include <X11/extensions/Xfixes.h>
#include <X11/extensions/XInput2.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <stdio.h>
#include <fcntl.h>

void
check_extensions (Display *dpy) {
    int maj = 2,
        min = 2,
        opc = -1,
        ev, err;

    if (!XQueryExtension(dpy, "XInputExtension", &opc, &ev, &err)) {
        printf("Extension XInput not found.");
        exit(0);
    }

    if (XIQueryVersion(dpy, &maj, &min) != Success) {
        printf("Extension XI2 not found, or unsupported version");
        exit(0);
    }
}

static long
get_brightness (Display *dpy, Atom backlight, RROutput out) {

    unsigned long after, items;
    unsigned char *property;
    long brightness;
    int format;
    Atom type;

    if (XRRGetOutputProperty(dpy, out, backlight, 0, 4, False, False, None, &type, &format, &items, &after, &property) != Success) {
        return -1;
    }

    brightness = *((long *) property);
    XFree(property);
    return brightness;
}

static void
set_brightness (Display *dpy, Atom backlight, RROutput out, long brightness) {

    XRRChangeOutputProperty(dpy, out, backlight, XA_INTEGER, 32, PropModeReplace, (unsigned char *) &brightness, 1);
    XFlush(dpy);
}

static bool
battery_status () {
    FILE *f;
    char stat[12];
    if ((f = fopen("/sys/class/power_supply/BAT0/status", "r")) == NULL) {
        return false;
    }
    fgets(stat, 12, f);
    fclose(f);
    return (strcmp(stat, "Discharging") == 0) ? true : false;
}

int
main (int argc, char *argv[]) {

    //execl("/bin/sh", "/bin/sh", "/usr/sbin/i3lock-fancy", NULL);

    unsigned int cursor_idle = 3,
                 screen_idle = 45,
                 screen_fade = 50,
                 lock_idle = 600,
                 battery_int = 5,
                 bidle = 0,
                 kidle = 0,
                 lidle = 0,
                 midle = 0;

    bool cursor_conf = false,
         screen_conf = false,
         lock_conf = false,
         battery_conf = true,
         battery = false,
         dim = false,
         cursor = true, // check first?
         locked = false,
         paused = false;

    char locker[256] = "";

    void pause() { paused = !paused; }
    signal(SIGUSR1, pause);

    long brightness;

    for (int i = 1; i < argc; i++) {
        // set cursor timeout
        if (!strcmp(argv[i], "-c")) {
            cursor_conf = true;
            cursor_idle = (optarg == NULL) ? cursor_idle : strtol(optarg, NULL, 10);
        }
        // set screen timeout
        if (!strcmp(argv[i], "-s")) {
            screen_conf = true;
            screen_idle = (optarg == NULL) ? screen_idle : strtol(optarg, NULL, 10);
        }
        // set locker timeout
        if (!strcmp(argv[i], "-l")) {
            lock_conf = true;
            lock_idle = (optarg == NULL) ? lock_idle : strtol(optarg, NULL, 10);
        }

        // set brightness
        if (!strcmp(argv[i], "-f")) {
            screen_conf = true;
            screen_fade = (optarg == NULL) ? screen_fade : strtol(optarg, NULL, 10);
        }
        // set locker
        if (!strcmp(argv[i], "-k")) {
            lock_conf = true;
            strcpy(locker, argv[++i]);
        }
        // set always dim
        if (!strcmp(argv[i], "-a")) {
            battery_conf = false;
            battery = true;
        }

        // show help
        if (!strcmp(argv[i], "-h")) {
            printf("Usage: gloom [-c <cursor timeout>] [-s <screen timeout>] [-l <lock timeout>] [-f <brightness percent>] [-k <command>] [-a] [-h]");
            exit(0);
        }
    }
    if (lock_conf && !strcmp(locker, "")) {
        printf("Error: -l requires a locker to be set with -k\n");
        exit(0);
    }

    Display *dpy = XOpenDisplay(NULL);
    if (!dpy) {
        printf("Cannot connect to the X Server\n");
        exit(0);
    }
    check_extensions(dpy);

    Window w = DefaultRootWindow(dpy);
    Atom backlight = XInternAtom(dpy, "Backlight", True);
    XRRScreenResources *resources = XRRGetScreenResources(dpy, w);
    XIEventMask masks[1];
    unsigned char mask[(XI_LASTEVENT + 7)/8];
    memset(mask, 0, sizeof(mask));
    XISetMask(mask, XI_RawMotion);
    XISetMask(mask, XI_RawButtonPress);
    XISetMask(mask, XI_RawKeyPress);
    masks[0].deviceid = XIAllMasterDevices;
{'fraction_non_alphanumeric': 0.13161632421172237, 'fraction_numerical': 0.012877568412082189, 'mean_word_length': 1.9844588866911557, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '559176', 'n_tokens_mistral': 3512, 'n_tokens_neox': 3327, 'n_words': 970}