<commit_before>/*
 * waysome - wayland based window manager
 *
 * Copyright in alphabetical order:
 *
 * Copyright (C) 2014-2015 <NAME>
 * Copyright (C) 2014-2015 <NAME>
 * Copyright (C) 2014-2015 <NAME>
 * Copyright (C) 2014-2015 <NAME>
 * Copyright (C) 2014-2015 <NAME>
 *
 * This file is part of waysome.
 *
 * waysome is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * waysome is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with waysome. If not, see <http://www.gnu.org/licenses/>.
 */

#include <dirent.h>
#include <errno.h>
#include <ev.h>
#include <fcntl.h>
#include <libevdev-1.0/libevdev/libevdev.h>
#include <linux/input.h>
#include <stdlib.h>
#include <string.h>
#include <sys/inotify.h>
#include <unistd.h>

#include "input/hotkeys.h"
#include "input/input_device.h"
#include "input/module.h"
#include "input/utils.h"
#include "logger/module.h"
#include "objects/set.h"
#include "util/cleaner.h"

static struct ws_logger_context log_ctx = { .prefix = "[Input/Device] " };

#define INPUT_PATH "/dev/input/"
#define MAX_FULL_PATH_LEN 100

/**
 * The callback used by libev to watch the INPUT_PATH directory
 */
static void
watch_devices(
    struct ev_loop* loop, //!< loop on which the callback was called
    ev_io* watcher, //!< watcher which triggered the update
    int revents //!< events
) {
    char buffer[1024]
        __attribute__((aligned(__alignof__(struct inotify_event))));

    struct inotify_event* event = (struct inotify_event*) buffer;
    ssize_t len = read(watcher->fd, buffer, sizeof(buffer));

    if (len == -1 && errno != EAGAIN) {
        ws_log(&log_ctx, LOG_CRIT,
                "Could not read from libev filedescriptor");
        return;
    }

    if (len <= 0) {
        return;
    }

    while (event < (struct inotify_event*) &buffer[len]) {
        if (event->mask & IN_CREATE) {
            ws_log(&log_ctx, LOG_DEBUG, "New file was created %s", event->name);
        }
        event = (struct inotify_event*)
            (((char*) event) + event->len + sizeof(*event));
    }


}

/**
 * Iterates over the existing devices at startup to make a list which can be
 * complete later
 */
static int
find_initial_devices(void) {
    DIR* d;
    struct dirent* dir;

    d = opendir(INPUT_PATH);

    if (!d) {
        ws_log(&log_ctx, LOG_CRIT, "Could not open input device directory");
        return -1;
    }

    ws_log(&log_ctx, LOG_DEBUG, "Starting listing of existing devices");


    while ((dir = readdir(d)) != NULL) {
        if (!ws_input_filter_event_device_name(dir->d_name)) {
            ws_log(&log_ctx, LOG_DEBUG, "Skipping directory %s", dir->d_name);
            continue;
        }
        char full_path[MAX_FULL_PATH_LEN] = INPUT_PATH;
        strncat(full_path, dir->d_name,
                MAX_FULL_PATH_LEN - 1 - sizeof(INPUT_PATH));

        int fd = open(full_path, O_RDONLY | O_NONBLOCK);

        if (fd < 0) {
            ws_log(&log_ctx, LOG_ERR, "Could not open event file %s %s",
                    full_path, strerror(errno));
            continue;
        }

        struct ws_input_device* new_dev = ws_input_device_new(fd);

        ws_set_insert(&ws_input_ctx.devices, (struct ws_object*) new_dev);

    }
    closedir(d);
    return 0;
}

static void
cleanup_input(
    void* dummy
) {
    int size = ws_set_cardinality(&ws_input_ctx.devices);

    while (size--) {
        struct ws_input_device* dev =
            (struct ws_input_device*) ws_set_select_any(&ws_input_ctx.devices);

        ws_set_remove(&ws_input_ctx.devices, (struct ws_object*) dev);

        libevdev_free(dev->dev);
        ev_io_stop(ev_default_loop(EVFLAG_AUTO), &dev->watcher);
        close(dev->fd);
        ws_object_unref((struct ws_object*) dev);
    }

    ws_object_deinit((struct ws_object*) &ws_input_ctx.devices);
}

int
ws_input_init(void)
{
    static bool is_init = false;

    if (is_init) {
        return 0;<commit_msg>Replace casts in input/module.c.c<commit_after>/*
 * waysome - wayland based window manager
 *
 * Copyright in alphabetical order:
 *
 * Copyright (C) 2014-2015 <NAME>
 * Copyright (C) 2014-2015 <NAME>
 * Copyright (C) 2014-2015 <NAME>
 * Copyright (C) 2014-2015 <NAME>
 * Copyright (C) 2014-2015 <NAME>
 *
 * This file is part of waysome.
 *
 * waysome is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * waysome is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with waysome. If not, see <http://www.gnu.org/licenses/>.
 */

#include <dirent.h>
#include <errno.h>
#include <ev.h>
#include <fcntl.h>
#include <libevdev-1.0/libevdev/libevdev.h>
#include <linux/input.h>
#include <stdlib.h>
#include <string.h>
#include <sys/inotify.h>
#include <unistd.h>

#include "input/hotkeys.h"
#include "input/input_device.h"
#include "input/module.h"
#include "input/utils.h"
#include "logger/module.h"
#include "objects/set.h"
#include "util/cleaner.h"

static struct ws_logger_context log_ctx = { .prefix = "[Input/Device] " };

#define INPUT_PATH "/dev/input/"
#define MAX_FULL_PATH_LEN 100

/**
 * The callback used by libev to watch the INPUT_PATH directory
 */
static void
watch_devices(
    struct ev_loop* loop, //!< loop on which the callback was called
    ev_io* watcher, //!< watcher which triggered the update
    int revents //!< events
) {
    char buffer[1024]
        __attribute__((aligned(__alignof__(struct inotify_event))));

    struct inotify_event* event = (struct inotify_event*) buffer;
    ssize_t len = read(watcher->fd, buffer, sizeof(buffer));

    if (len == -1 && errno != EAGAIN) {
        ws_log(&log_ctx, LOG_CRIT,
                "Could not read from libev filedescriptor");
        return;
    }

    if (len <= 0) {
        return;
    }

    while (event < (struct inotify_event*) &buffer[len]) {
        if (event->mask & IN_CREATE) {
            ws_log(&log_ctx, LOG_DEBUG, "New file was created %s", event->name);
        }
        event = (struct inotify_event*)
            (((char*) event) + event->len + sizeof(*event));
    }


}

/**
 * Iterates over the existing devices at startup to make a list which can be
 * complete later
 */
static int
find_initial_devices(void) {
    DIR* d;
    struct dirent* dir;

    d = opendir(INPUT_PATH);

    if (!d) {
        ws_log(&log_ctx, LOG_CRIT, "Could not open input device directory");
        return -1;
    }

    ws_log(&log_ctx, LOG_DEBUG, "Starting listing of existing devices");


    while ((dir = readdir(d)) != NULL) {
        if (!ws_input_filter_event_device_name(dir->d_name)) {
            ws_log(&log_ctx, LOG_DEBUG, "Skipping directory %s", dir->d_name);
            continue;
        }
        char full_path[MAX_FULL_PATH_LEN] = INPUT_PATH;
        strncat(full_path, dir->d_name,
                MAX_FULL_PATH_LEN - 1 - sizeof(INPUT_PATH));

        int fd = open(full_path, O_RDONLY | O_NONBLOCK);

        if (fd < 0) {
            ws_log(&log_ctx, LOG_ERR, "Could not open event file %s %s",
                    full_path, strerror(errno));
            continue;
        }

        struct ws_input_device* new_dev = ws_input_device_new(fd);

        ws_set_insert(&ws_input_ctx.devices, &new_dev->obj);

    }
    closedir(d);
    return 0;
}

static void
cleanup_input(
    void* dummy
) {
    int size = ws_set_cardinality(&ws_input_ctx.devices);

    while (size--) {
        struct ws_input_device* dev =
            (struct ws_input_device*) ws_set_select_any(&ws_input_ctx.devices);

        ws_set_remove(&ws_input_ctx.devices, &dev->obj);

        libevdev_free(dev->dev);
        ev_io_stop(ev_default_loop(EVFLAG_AUTO), &dev->watcher);
        close(dev->fd);
        ws_object_unref(&dev->obj);
    }

    ws_object_deinit(&ws_input_ctx.devices.obj);
}

int
ws_input_init(void)
{
    static bool is_init = false;

    if (is_init) {
        return 0;
{'fraction_non_alphanumeric': 0.12015148037640579, 'fraction_numerical': 0.013311911865962818, 'mean_word_length': 2.9631650750341065, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 58, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3054344', 'n_tokens_mistral': 3026, 'n_tokens_neox': 2837, 'n_words': 808}