Fumofumotris/source/io/platforms/win.c

173 lines
3.9 KiB
C
Raw Normal View History

2024-03-25 05:34:59 +00:00
#include <iso646.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
2024-04-11 19:57:22 +00:00
#include <windows.h>
2024-04-03 23:31:47 +00:00
#include <time.h>
2024-03-25 05:34:59 +00:00
2024-04-11 19:57:22 +00:00
#include "fumotris.h"
#include "ctrl.h"
2024-03-25 05:34:59 +00:00
2024-04-15 19:29:51 +00:00
static struct Windows {
2024-04-11 19:57:22 +00:00
HANDLE in_handle;
HANDLE timer;
DWORD in_len;
INPUT_RECORD in_buf[IO_BUF_SIZE];
2024-04-15 19:29:51 +00:00
} win;
2024-04-11 19:57:22 +00:00
2024-04-15 19:29:51 +00:00
bool init_handles()
2024-03-25 05:34:59 +00:00
{
2024-04-15 19:29:51 +00:00
win.in_handle = GetStdHandle(STD_INPUT_HANDLE);
if (win.in_handle == INVALID_HANDLE_VALUE)
2024-03-25 05:34:59 +00:00
return false;
2024-04-15 19:29:51 +00:00
win.timer = CreateWaitableTimer(
2024-04-11 19:57:22 +00:00
NULL, // Timer attributes
TRUE, // Manual reset
NULL // Name
);
2024-04-15 19:29:51 +00:00
if (win.timer == NULL)
2024-04-11 19:57:22 +00:00
return false;
return true;
}
2024-04-15 19:29:51 +00:00
bool init_console()
2024-04-11 19:57:22 +00:00
{
DWORD mode = ENABLE_EXTENDED_FLAGS
| ENABLE_PROCESSED_INPUT
| ENABLE_PROCESSED_OUTPUT
| ENABLE_MOUSE_INPUT
| ENABLE_WINDOW_INPUT;
2024-04-15 19:29:51 +00:00
return SetConsoleMode(win.in_handle, mode) != 0;
}
bool PlatformInit()
{
if (!init_handles())
return false;
if (!init_console())
return false;
return true;
2024-04-11 19:57:22 +00:00
}
2024-04-15 19:29:51 +00:00
bool PlatformGetRefreshRate(u16f *out)
2024-04-11 19:57:22 +00:00
{
DEVMODE mode;
mode.dmSize = sizeof(DEVMODE);
mode.dmDriverExtra = 0;
if(!EnumDisplaySettingsA(
NULL, // Device name (null for current)
ENUM_CURRENT_SETTINGS, // Mode
&mode // Out
))
return false;
*out = mode.dmDisplayFrequency;
return true;
}
void read_key_rec(struct InputRecord *rec, KEY_EVENT_RECORD win_key)
2024-03-25 05:34:59 +00:00
{
2024-04-11 19:57:22 +00:00
rec->type = BUTTON;
rec->bind = win_key.wVirtualKeyCode;
rec->is_down = win_key.bKeyDown;
rec->is_up = !win_key.bKeyDown;
2024-03-25 05:34:59 +00:00
}
2024-04-11 19:57:22 +00:00
bool read_mouse_rec(struct InputRecord *rec, MOUSE_EVENT_RECORD win_mouse)
2024-03-25 05:34:59 +00:00
{
2024-04-11 19:57:22 +00:00
if (win_mouse.dwEventFlags == MOUSE_MOVED) {
rec->type = JOYSTICK;
rec->bind = 0;
rec->js.x = win_mouse.dwMousePosition.X;
rec->js.y = win_mouse.dwMousePosition.Y;
return true;
}
if (win_mouse.dwEventFlags == MOUSE_WHEELED) {
rec->bind = 0;
} else if (win_mouse.dwEventFlags == MOUSE_HWHEELED) {
rec->bind = 1;
} else {
return false;
}
rec->type = AXIS;
rec->axis.value = win_mouse.dwButtonState;
2024-04-03 23:31:47 +00:00
2024-04-11 19:57:22 +00:00
return true;
}
bool read_rec(struct InputRecord *rec, INPUT_RECORD win_rec)
{
switch (win_rec.EventType) {
case KEY_EVENT:
read_key_rec(rec, win_rec.Event.KeyEvent);
return true;
case MOUSE_EVENT:
return read_mouse_rec(rec, win_rec.Event.MouseEvent);
case WINDOW_BUFFER_SIZE_EVENT:
return false;
// TODO: Handle window resizing
}
rec->type = ESCAPE;
return false;
}
2024-04-15 19:29:51 +00:00
bool PlatformBlockInput(struct InputBuffer *buf)
2024-04-11 19:57:22 +00:00
{
if (!ReadConsoleInput(
2024-04-15 19:29:51 +00:00
win.in_handle, // Input handle
win.in_buf + buf->len, // Record buffer
2024-04-11 19:57:22 +00:00
IO_BUF_SIZE - buf->len, // Record buffer length
2024-04-15 19:29:51 +00:00
&win.in_len // Out number of records
2024-04-11 19:57:22 +00:00
))
return false;
struct timespec now;
timespec_get(&now, TIME_UTC);
2024-04-15 19:29:51 +00:00
for (size_t i = 0; i < win.in_len; i++) {
2024-04-11 19:57:22 +00:00
struct InputRecord *rec = &buf->records[buf->len];
2024-04-15 19:29:51 +00:00
if (!read_rec(rec, win.in_buf[i]))
2024-04-11 19:57:22 +00:00
continue;
rec->timestamp = now;
buf->len += 1;
}
return true;
}
2024-04-15 19:29:51 +00:00
bool PlatformWait(struct timespec relative)
2024-04-11 19:57:22 +00:00
{
LARGE_INTEGER duration;
duration.QuadPart = -10000000 * relative.tv_sec - relative.tv_nsec / 100;
if (!SetWaitableTimer(
2024-04-15 19:29:51 +00:00
win.timer, // Timer
2024-04-11 19:57:22 +00:00
&duration, // Duration
0, // Period
NULL, // Completion coroutine
NULL, // Completion coroutine arg
FALSE // Resume
))
return false;
2024-04-15 19:29:51 +00:00
DWORD result = WaitForSingleObject(win.timer, INFINITE);
2024-04-11 19:57:22 +00:00
if (result != WAIT_OBJECT_0)
return false;
return true;
2024-03-25 05:34:59 +00:00
}