Newer
Older
honey-os / src / userland / mouse / main.c
@biosfood biosfood on 16 Sep 2023 1 KB hlib: monopolize checkFocus function
#include <hlib.h>

volatile int32_t x = 0, y = 0;
volatile uint32_t updateEvent;
volatile bool buttons[5];

void moveAbsolute(int32_t newX, int32_t newY) {
    x = newX;
    y = newY;
    fireEvent(updateEvent, 0);
}

void moveRelative(int32_t dX, int32_t dY) {
    x += dX;
    y += dY;
    fireEvent(updateEvent, 0);
}

void updateButton(uint8_t button, bool newState) {
    if (button > 5 || button == 0) {
        // hid button 0 means no button is pressed
        // TODO: set all buttons to not pressed when button = 0?
        return;
    }
    buttons[button-1] = newState;
    fireEvent(updateEvent, 0);
}


bool initialized = false, updated = true;

void onUpdate() { updated = true; }

void initialize() {
    initialized = true;
    createFunction("moveAbsolute", (void *)moveAbsolute);
    createFunction("moveRelative", (void *)moveRelative);
    createFunction("updateButton", (void *)updateButton);
    updateEvent = createEvent("update");
}

int32_t main() {
    if (!initialized) { initialize(); }
    if (!checkFocus()) {
        return 0;
    }
    uint32_t serviceId = getServiceId();
    printf("service: %i, event: %i\n", serviceId, updateEvent);
    subscribeEvent(serviceId, updateEvent, (void *)onUpdate);
    while (1) {
        if (updated) {
            printf("mouse info: x: %i, y: %i, buttons: %i %i %i         \r", x, y, buttons[0], buttons[1], buttons[2]);
            updated = false;
        }
        sleep(200);
    }
}