Handle events

This commit is contained in:
Juhani Krekelä 2021-11-16 08:18:16 +02:00
parent 6318896f2d
commit d97249c891
1 changed files with 104 additions and 14 deletions

118
sortix.c
View File

@ -8,6 +8,8 @@
#ifdef GRDRV_SORTIX
#include <sys/keycodes.h>
#include <err.h> //debg
#include <errno.h>
#include <stdint.h>
@ -17,6 +19,12 @@
#include "links.h"
struct window_data
{
uint32_t window_id;
int modifiers;
};
static int default_window_width = 600;
static int default_window_height = 500;
@ -25,10 +33,95 @@ static struct display_event_handlers event_handlers;
struct graphics_driver sortix_driver;
static struct graphics_device *current_dev;
static void on_disconnect(void *ctx)
{
if (current_dev->keyboard_handler)
current_dev->keyboard_handler(current_dev, KBD_CTRL_C, 0);
}
static void on_quit(void *ctx, uint32_t window_id)
{
struct window_data *window_data = current_dev->driver_data;
if (window_id != window_data->window_id) return;
if (current_dev->keyboard_handler)
current_dev->keyboard_handler(current_dev, KBD_CLOSE, 0);
}
static void on_keyboard(void *ctx, uint32_t window_id, uint32_t codepoint)
{
(void) ctx;
struct window_data *window_data = current_dev->driver_data;
if (window_id != window_data->window_id) return;
int kbkey = KBKEY_DECODE(codepoint);
if (kbkey) {
switch (kbkey) {
case KBKEY_LALT: window_data->modifiers |= KBD_ALT; break;
case -KBKEY_LALT: window_data->modifiers &= ~KBD_ALT; break;
case KBKEY_LSHIFT: case KBKEY_RSHIFT: window_data->modifiers |= KBD_SHIFT; break;
case -KBKEY_LSHIFT: case -KBKEY_RSHIFT: window_data->modifiers &= ~KBD_SHIFT; break;
case KBKEY_LCTRL: case KBKEY_RCTRL: window_data->modifiers |= KBD_CTRL; break;
case -KBKEY_LCTRL: case -KBKEY_RCTRL: window_data->modifiers &= ~KBD_CTRL; break;
}
}
if (current_dev->keyboard_handler) {
if (kbkey) codepoint = 0;
switch (codepoint) {
case '\n': codepoint = KBD_ENTER; break;
case 127: codepoint = KBD_BS; break;
case '\t': codepoint = KBD_TAB; break;
}
switch (kbkey) {
case KBKEY_ESC: codepoint = KBD_ESC; break;
case KBKEY_LEFT: codepoint = KBD_LEFT; break;
case KBKEY_RIGHT: codepoint = KBD_RIGHT; break;
case KBKEY_UP: codepoint = KBD_UP; break;
case KBKEY_DOWN: codepoint = KBD_DOWN; break;
case KBKEY_INSERT: codepoint = KBD_INS; break;
case KBKEY_DELETE: codepoint = KBD_DEL; break;
case KBKEY_HOME: codepoint = KBD_HOME; break;
case KBKEY_END: codepoint = KBD_END; break;
case KBKEY_PGUP: codepoint = KBD_PAGE_UP; break;
case KBKEY_PGDOWN: codepoint = KBD_PAGE_DOWN; break;
case KBKEY_F1: codepoint = KBD_F1; break;
case KBKEY_F2: codepoint = KBD_F2; break;
case KBKEY_F3: codepoint = KBD_F3; break;
case KBKEY_F4: codepoint = KBD_F4; break;
case KBKEY_F5: codepoint = KBD_F5; break;
case KBKEY_F6: codepoint = KBD_F6; break;
case KBKEY_F7: codepoint = KBD_F7; break;
case KBKEY_F8: codepoint = KBD_F8; break;
case KBKEY_F9: codepoint = KBD_F9; break;
case KBKEY_F10: codepoint = KBD_F10; break;
case KBKEY_F11: codepoint = KBD_F11; break;
case KBKEY_F12: codepoint = KBD_F12; break;
}
if (codepoint)
current_dev->keyboard_handler(current_dev, codepoint, window_data->modifiers);
}
}
static void on_resize(void *ctx, uint32_t window_id, uint32_t width, uint32_t height)
{
(void) ctx;
struct window_data *window_data = current_dev->driver_data;
if (window_id != window_data->window_id) return;
if (!width || !height) return;
current_dev->size.x2 = width;
current_dev->size.y2 = height;
if (current_dev->resize_handler)
current_dev->resize_handler(current_dev);
}
static void sortix_process_events(void *data)
{
(void) data;
warnx("sortix_process_events");//debg
if (!current_dev) return;
while (display_poll_event(connection, &event_handlers) == 0);
}
@ -48,19 +141,20 @@ static unsigned char *sortix_init_driver(unsigned char *param, unsigned char *di
sortix_driver.get_color = get_color_fn(sortix_driver.depth);
event_handlers.disconnect_handler = on_disconnect;
event_handlers.keyboard_handler = on_keyboard;
event_handlers.quit_handler = on_quit;
event_handlers.resize_handler = on_resize;
set_handlers(display_connection_fd(connection), sortix_process_events, NULL, NULL);
return NULL;
}
struct window_data
{
uint32_t window_id;
struct framebuffer fb;
};
static struct graphics_device *sortix_init_device(void)
{
if (current_dev)
errx(1, "Attempting to create second window");
struct graphics_device *dev = mem_calloc(sizeof(struct graphics_device));
dev->size.x1 = 0;
@ -74,16 +168,12 @@ static struct graphics_device *sortix_init_device(void)
dev->driver_data = window_data;
window_data->window_id = 0;
// TODO: Overflow when multiplying
window_data->fb.buffer = mem_calloc(dev->size.x2 * dev->size.y2 * sizeof(uint32_t));
window_data->fb.xres = dev->size.x2;
window_data->fb.yres = dev->size.y2;
window_data->fb.pitch = dev->size.x2;
display_create_window(connection, window_data->window_id);
display_resize_window(connection, window_data->window_id, dev->size.x2, dev->size.y2);
display_show_window(connection, window_data->window_id);
current_dev = dev;
return dev;
}
@ -244,7 +334,7 @@ struct graphics_driver sortix_driver =
NULL, // get_clipboard_text
(24 << 3) | 4, // depth: 24bpp, 4 bytes per pixel
0, 0, // size (x, y), unused
GD_DONT_USE_SCROLL, //flags
GD_DONT_USE_SCROLL | GD_UNICODE_KEYS, //flags
NULL, // param
};