<commit_before>#include <module.h>
#include <kernel/rendezvous.h>
#include <base/base.h>
#include <base/keyboard.h>
#include <arch/x86/pic/pic.h>
#include "ps2.h"
#include "keyboard_driver.h"

static unsigned int read_stream(struct ps2_keyboard_stream *stream, unsigned int count, void *buffer)
{

    char *b = buffer;
    unsigned int i;

    for (i = 0; i < count; i++)
    {

        unsigned int tail = (stream->tail + 1) % 512;

        if (stream->head == stream->tail)
            break;

        b[i] = stream->buffer[stream->tail];
        stream->tail = tail;

    }

    return i;

}

static unsigned int write_stream(struct ps2_keyboard_stream *stream, unsigned int count, void *buffer)
{

    char *b = buffer;
    unsigned int i;

    for (i = 0; i < count; i++)
    {

        unsigned int head = (stream->head + 1) % 512;

        if (head == stream->tail)
            break;

        stream->buffer[stream->head] = b[i];
        stream->head = head;

    }

    return i;

}

static void handle_irq(struct base_device *device)
{

    struct ps2_bus *bus = (struct ps2_bus *)device->bus;
    struct ps2_keyboard_driver *driver = (struct ps2_keyboard_driver *)device->driver;
    unsigned char scancode = ps2_bus_read_data_async(bus);

    if (driver->escaped)
        driver->escaped = 0;

    if (scancode == 0xE0)
        driver->escaped = 1;

    if (scancode & 0x80)
    {

        scancode &= ~0x80;

        if (scancode == 0x1D)
            driver->ctrl = 0;

        if (scancode == 0x2A)
            driver->shift = 0;

        if (scancode == 0x38)
            driver->alt = 0;

    }

    else
    {

        if (scancode == 0x1D)
            driver->ctrl = 1;

        if (scancode == 0x2A)
            driver->shift = 1;

        if (scancode == 0x38)
            driver->alt = 1;

        if (driver->ctrl)
            scancode = 0;

        if (driver->alt)
            scancode = 0;

        if (driver->shift)
            scancode += 128;

        write_stream(&driver->stream, 1, driver->ikeyboard.keymap + scancode);
        rendezvous_unsleep(&driver->rdata, 1);

    }

}

static void attach(struct base_device *device)
{

    struct ps2_bus *bus = (struct ps2_bus *)device->bus;
    struct ps2_keyboard_driver *driver = (struct ps2_keyboard_driver *)device->driver;
    unsigned char status;

    base_keyboard_register_interface(&driver->ikeyboard, device);
    pic_set_routine(device, handle_irq);
    ps2_bus_write_command(bus, 0xAE);
    ps2_bus_write_command(bus, 0x20);

    status = ps2_bus_read_data(bus) | 1;<commit_msg>more utf8 work<commit_after>#include <module.h>
#include <kernel/rendezvous.h>
#include <base/base.h>
#include <base/keyboard.h>
#include <arch/x86/pic/pic.h>
#include "ps2.h"
#include "keyboard_driver.h"

static unsigned int read_stream(struct ps2_keyboard_stream *stream, unsigned int count, void *buffer)
{

    char *b = buffer;
    unsigned int i;

    for (i = 0; i < count; i++)
    {

        unsigned int tail = (stream->tail + 1) % 512;

        if (stream->head == stream->tail)
            break;

        b[i] = stream->buffer[stream->tail];
        stream->tail = tail;

    }

    return i;

}

static unsigned int write_stream(struct ps2_keyboard_stream *stream, unsigned int count, void *buffer)
{

    char *b = buffer;
    unsigned int i;

    for (i = 0; i < count; i++)
    {

        unsigned int head = (stream->head + 1) % 512;

        if (head == stream->tail)
            break;

        stream->buffer[stream->head] = b[i];
        stream->head = head;

    }

    return i;

}

static void handle_irq(struct base_device *device)
{

    struct ps2_bus *bus = (struct ps2_bus *)device->bus;
    struct ps2_keyboard_driver *driver = (struct ps2_keyboard_driver *)device->driver;
    unsigned char scancode = ps2_bus_read_data_async(bus);

    if (driver->escaped)
        driver->escaped = 0;

    if (scancode == 0xE0)
        driver->escaped = 1;

    if (scancode & 0x80)
    {

        scancode &= ~0x80;

        if (scancode == 0x1D)
            driver->ctrl = 0;

        if (scancode == 0x2A)
            driver->shift = 0;

        if (scancode == 0x38)
            driver->alt = 0;

    }

    else
    {

        if (scancode == 0x1D)
            driver->ctrl = 1;

        if (scancode == 0x2A)
            driver->shift = 1;

        if (scancode == 0x38)
            driver->alt = 1;

        if (driver->ctrl)
            scancode = 0;

        if (driver->alt)
            scancode = 0;

        if (driver->shift)
            scancode += 128;

        write_stream(&driver->stream, 4, &driver->ikeyboard.keymap[scancode]);
        rendezvous_unsleep(&driver->rdata, 1);

    }

}

static void attach(struct base_device *device)
{

    struct ps2_bus *bus = (struct ps2_bus *)device->bus;
    struct ps2_keyboard_driver *driver = (struct ps2_keyboard_driver *)device->driver;
    unsigned char status;

    base_keyboard_register_interface(&driver->ikeyboard, device);
    pic_set_routine(device, handle_irq);
    ps2_bus_write_command(bus, 0xAE);
    ps2_bus_write_command(bus, 0x20);

    status = ps2_bus_read_data(bus) | 1;
{'fraction_non_alphanumeric': 0.12731166050223866, 'fraction_numerical': 0.02705859451041464, 'mean_word_length': 2.266369993642721, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 75, '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': '6873107', 'n_tokens_mistral': 1909, 'n_tokens_neox': 1743, 'n_words': 414}