mirror of
https://git.triforceproject.com/triforce/source
synced 2026-11-20 00:29:57 -04:00
Triforce v0.2.0
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2
host/posix/direct.h
Normal file
2
host/posix/direct.h
Normal file
@@ -0,0 +1,2 @@
|
||||
/* (linux) direct.h: _mkdir is in windows.h here */
|
||||
#include <windows.h>
|
||||
343
host/posix/display_sdl.c
Normal file
343
host/posix/display_sdl.c
Normal file
@@ -0,0 +1,343 @@
|
||||
/* (linux) The game's window on SDL (host/hos_sdl.c: SDL3, Wayland when there is one, else X11; or SDL2): what
|
||||
host/display.c is on Windows. The window lives on the process's main thread, as SDL wants it (hos_main.c runs the
|
||||
game on a thread of its own on Linux); the Vulkan back end makes its surface through SDL (gpu/vk/vk_api.h
|
||||
MkwVkWindow), on a thread of its own so the window keeps answering while the shaders are prepared.
|
||||
|
||||
The keyboard's state goes to hos_posix_keys (GetAsyncKeyState, for nn_hid.c and the Insert menu), the window's focus
|
||||
and the screen's size to hos_posix_window (GetForegroundWindow, GetSystemMetrics).
|
||||
HOS_HEADLESS=1: no window. HOS_WINDOW_SIZE=<w>x<h>: its size (default 1280x720). F10 or Alt+Enter: fullscreen (HOS_FULLSCREEN=1 to
|
||||
start so). Mouse: a click in the window captures it (relative motion, for nn_hid.c), Esc or leaving releases it.
|
||||
While the Insert menu is open the mouse is free (the cursor shown, for the menu's clicks: hos_mouse_pointer) and
|
||||
the game gets none of it; the capture it had comes back when the menu closes. */
|
||||
#include "../hos.h"
|
||||
#include "../hos_sdl.h"
|
||||
|
||||
/* (the window's app id: the applications menu's entry names it as its StartupWMClass, builder/linux/hookshot_builder.py) */
|
||||
#define HOS_APP_ID "io.github.NotRexed.Hookshot"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <windows.h>
|
||||
#include "../../gpu/vk/vk_api.h"
|
||||
|
||||
int rx_start(void *hwnd);
|
||||
void rx_resize(int width, int height);
|
||||
void rx_close_asked(void);
|
||||
void hos_sdl_pads_pump(void); /* host/pad_sdl.c: controllers, on this thread, as SDL's queue wants */
|
||||
void hos_relaunch_ask(void); /* (host/hos_relaunch.c: the Insert menu's Restart now) */
|
||||
void hos_window_closed_exit(void);
|
||||
|
||||
volatile LONG64 hos_present_count; /* (nvn_native.c: the game's presents) */
|
||||
__attribute__((visibility("default"))) volatile uint8_t hos_posix_keys[256];
|
||||
__attribute__((visibility("default"))) volatile int hos_posix_window[4]; /* focused, screen width, screen height, fullscreen */
|
||||
|
||||
static volatile LONG mouse_dx, mouse_dy, mouse_captured;
|
||||
int hos_mouse_take(int32_t *dx, int32_t *dy) {
|
||||
*dx = InterlockedExchange(&mouse_dx, 0);
|
||||
*dy = InterlockedExchange(&mouse_dy, 0);
|
||||
return mouse_captured;
|
||||
}
|
||||
|
||||
/* (mouse_native.c) motion taken and not used, put back for the next taker (the player camera's) */
|
||||
void hos_mouse_give_back(int32_t dx, int32_t dy) {
|
||||
InterlockedExchangeAdd(&mouse_dx, dx);
|
||||
InterlockedExchangeAdd(&mouse_dy, dy);
|
||||
}
|
||||
|
||||
/* ---- the free mouse, for the Insert menu (gpu/vk/vk_api.h MkwVkPointer): set on the main thread, read on the renderer's ---- */
|
||||
static volatile LONG ptr_x, ptr_y, ptr_in, ptr_held, ptr_pressed, ptr_wheel; /* (x, y in window units; wheel in 1/120 notches) */
|
||||
static volatile int menu_open; /* (WM_APP + 8: the menu's state, as the window last heard it) */
|
||||
static void *window;
|
||||
int hos_mouse_pointer(MkwVkPointer *out) {
|
||||
memset(out, 0, sizeof *out);
|
||||
if (!window || mouse_captured) return 0;
|
||||
int ww = 0, wh = 0, pw = 0, ph = 0;
|
||||
hos_sdl_window_size(window, &ww, &wh); /* (SDL's position is in the window's units: scaled to its pixels) */
|
||||
hos_sdl_pixel_size(window, &pw, &ph);
|
||||
if (ww <= 0 || wh <= 0 || pw <= 0 || ph <= 0) return 0;
|
||||
out->x = (int32_t)((int64_t)ptr_x * pw / ww), out->y = (int32_t)((int64_t)ptr_y * ph / wh);
|
||||
out->width = pw, out->height = ph;
|
||||
out->held = (uint32_t)ptr_held;
|
||||
out->pressed = (uint32_t)InterlockedExchange(&ptr_pressed, 0);
|
||||
const LONG wheel = InterlockedExchange(&ptr_wheel, 0);
|
||||
out->wheel = wheel / 120;
|
||||
if (wheel % 120) InterlockedExchangeAdd(&ptr_wheel, wheel % 120); /* (a part of a notch: kept for the next) */
|
||||
return ptr_in && hos_posix_window[0];
|
||||
}
|
||||
static uint32_t sdl_button_bit(int b) { return b == 1 ? HOS_MOUSE_LEFT : b == 3 ? HOS_MOUSE_RIGHT : b == 2 ? HOS_MOUSE_MIDDLE : 0; }
|
||||
|
||||
/* ---- keys: SDL's scancodes (USB HID usages) as Windows' virtual keys ---- */
|
||||
static void keys_update(void) {
|
||||
int n = 0;
|
||||
const uint8_t *k = hos_sdl_keys(&n);
|
||||
uint8_t v[256];
|
||||
memset(v, 0, sizeof v);
|
||||
if (k && hos_posix_window[0]) {
|
||||
#define K(sc) ((sc) < n && k[sc])
|
||||
for (int i = 0; i < 26; i++) v['A' + i] = K(4 + i);
|
||||
for (int i = 0; i < 9; i++) v['1' + i] = K(30 + i);
|
||||
v['0'] = K(39);
|
||||
v[VK_RETURN] = K(40) || K(88), v[VK_ESCAPE] = K(41), v[VK_BACK] = K(42), v[VK_TAB] = K(43), v[VK_SPACE] = K(44);
|
||||
v[VK_OEM_MINUS] = K(45), v[VK_OEM_PLUS] = K(46), v[VK_OEM_1] = K(51), v[VK_OEM_3] = K(53);
|
||||
v[VK_OEM_COMMA] = K(54), v[VK_OEM_PERIOD] = K(55), v[VK_OEM_2] = K(56), v[VK_CAPITAL] = K(57);
|
||||
v[VK_OEM_4] = K(47), v[VK_OEM_6] = K(48), v[VK_OEM_5] = K(49), v[VK_OEM_7] = K(52); /* ([ ] \ ') */
|
||||
for (int i = 0; i < 12; i++) v[VK_F1 + i] = K(58 + i);
|
||||
v[VK_PAUSE] = K(72), v[VK_INSERT] = K(73), v[VK_HOME] = K(74), v[VK_PRIOR] = K(75), v[VK_DELETE] = K(76);
|
||||
v[VK_END] = K(77), v[VK_NEXT] = K(78), v[VK_RIGHT] = K(79), v[VK_LEFT] = K(80), v[VK_DOWN] = K(81), v[VK_UP] = K(82);
|
||||
v[VK_DIVIDE] = K(84), v[VK_MULTIPLY] = K(85), v[VK_SUBTRACT] = K(86), v[VK_ADD] = K(87);
|
||||
for (int i = 0; i < 9; i++) v[VK_NUMPAD0 + 1 + i] = K(89 + i);
|
||||
v[VK_NUMPAD0] = K(98), v[VK_DECIMAL] = K(99);
|
||||
v[VK_LCONTROL] = K(224), v[VK_LSHIFT] = K(225), v[VK_LMENU] = K(226);
|
||||
v[VK_RCONTROL] = K(228), v[VK_RSHIFT] = K(229), v[VK_RMENU] = K(230);
|
||||
#undef K
|
||||
v[VK_CONTROL] = v[VK_LCONTROL] || v[VK_RCONTROL];
|
||||
v[VK_SHIFT] = v[VK_LSHIFT] || v[VK_RSHIFT];
|
||||
v[VK_MENU] = v[VK_LMENU] || v[VK_RMENU];
|
||||
const uint32_t b = hos_sdl_mouse_buttons();
|
||||
v[VK_LBUTTON] = (b & 1) != 0, v[VK_MBUTTON] = (b & 2) != 0, v[VK_RBUTTON] = (b & 4) != 0;
|
||||
}
|
||||
for (int i = 0; i < 256; i++) hos_posix_keys[i] = v[i];
|
||||
}
|
||||
|
||||
/* ---- the window ---- */
|
||||
static volatile int renderer; /* the renderer presents into the window */
|
||||
static HANDLE renderer_up; /* (set once rx_start has returned, ready or not: hos_renderer_wait) */
|
||||
static int fullscreen;
|
||||
|
||||
static int create_surface(void *w, void *instance, void *surface) { return hos_sdl_vulkan_surface(w, instance, surface); }
|
||||
static void drawable_size(void *w, int *width, int *height) { hos_sdl_pixel_size(w, width, height); }
|
||||
/* (SDL's display calls belong on this thread: the loop asks every second, the renderer reads what it found) */
|
||||
static volatile int refresh_now;
|
||||
static int refresh_mhz(void *w) { (void)w; return __atomic_load_n(&refresh_now, __ATOMIC_RELAXED); }
|
||||
static MkwVkWindow vk_window = {MKWVK_WINDOW_VERSION, create_surface, drawable_size, NULL, refresh_mhz};
|
||||
|
||||
static void mouse_capture(int on) {
|
||||
if (on == (int)mouse_captured) return;
|
||||
mouse_captured = on;
|
||||
InterlockedExchange(&mouse_dx, 0);
|
||||
InterlockedExchange(&mouse_dy, 0);
|
||||
hos_sdl_relative_mouse(window, on);
|
||||
}
|
||||
static void set_fullscreen(int on) {
|
||||
if (on == fullscreen) return;
|
||||
fullscreen = on;
|
||||
hos_posix_window[3] = on;
|
||||
hos_sdl_fullscreen(window, on);
|
||||
hos_log("display: %s", on ? "fullscreen" : "windowed");
|
||||
}
|
||||
|
||||
static DWORD WINAPI renderer_main(void *p) {
|
||||
(void)p;
|
||||
const int ok = rx_start(&vk_window);
|
||||
if (!ok) hos_log("display: the renderer did not start: the window stays black");
|
||||
renderer = ok;
|
||||
if (ok) { /* (a size change while it started) */
|
||||
int w = 0, h = 0;
|
||||
hos_sdl_pixel_size(window, &w, &h);
|
||||
if (w > 0 && h > 0) rx_resize(w, h);
|
||||
}
|
||||
SetEvent(renderer_up);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* (hookshot) host/display.c's: the game's first work for the GPU waits for the renderer (nvn_native.c), so nothing it submits while
|
||||
the shaders are prepared is lost. Without a window the renderer starts on the game's own thread, before this is asked */
|
||||
void hos_renderer_wait(void) {
|
||||
{ const char *e = getenv("HOS_NO_RENDERER_WAIT"); if (e && atoi(e) != 0) return; }
|
||||
if (renderer_up) WaitForSingleObject(renderer_up, INFINITE);
|
||||
}
|
||||
|
||||
static volatile int start_asked;
|
||||
static volatile int fullscreen_asked = -1; /* (the Insert menu's row, from the renderer's thread: applied on this one) */
|
||||
static volatile int menu_asked = -1; /* (the Insert menu opened or closed, likewise) */
|
||||
static volatile int restart_asked; /* (the Insert menu's Restart now: the window closes as by the player) */
|
||||
static volatile int close_asked; /* (the Insert menu's Quit: likewise, without the relaunch) */
|
||||
|
||||
/* PostMessageA to the window (host/posix/win32_posix.c): WM_APP + 7, the Insert menu's Fullscreen row, WM_APP + 8 the
|
||||
menu opened or closed, WM_APP + 9 its Restart now, WM_CLOSE its Quit (host/display.c's) */
|
||||
__attribute__((visibility("default"))) void hos_posix_post_message(uint32_t msg, uintptr_t wp, intptr_t lp) {
|
||||
(void)lp;
|
||||
if (msg == WM_APP + 7) __atomic_store_n(&fullscreen_asked, wp ? 1 : 0, __ATOMIC_RELEASE);
|
||||
if (msg == WM_APP + 8) __atomic_store_n(&menu_asked, wp ? 1 : 0, __ATOMIC_RELEASE);
|
||||
if (msg == WM_APP + 9) __atomic_store_n(&restart_asked, 1, __ATOMIC_RELEASE);
|
||||
if (msg == WM_CLOSE) __atomic_store_n(&close_asked, 1, __ATOMIC_RELEASE);
|
||||
}
|
||||
/* the menu opened: the mouse let go (its capture kept for later); closed: the capture back, if the window still has the focus */
|
||||
static int captured_before_menu;
|
||||
static void menu_mouse(int open) {
|
||||
if (open == menu_open) return;
|
||||
menu_open = open;
|
||||
if (open) {
|
||||
captured_before_menu = (int)mouse_captured;
|
||||
mouse_capture(0);
|
||||
InterlockedExchange(&ptr_pressed, 0), InterlockedExchange(&ptr_wheel, 0);
|
||||
} else {
|
||||
if (captured_before_menu && hos_posix_window[0]) mouse_capture(1);
|
||||
captured_before_menu = 0;
|
||||
}
|
||||
}
|
||||
static int sdl_ready;
|
||||
int hos_display_owns_sdl(void) { return sdl_ready; } /* (controllers stay on this thread while it runs SDL) */
|
||||
|
||||
/* main(), before the game: SDL's video on this, the main, thread; the screen's size for GetSystemMetrics.
|
||||
Returns 1 when ready, 0 for explicit headless, -1 for startup failure. */
|
||||
int hos_display_init(void) {
|
||||
if (getenv("HOS_HEADLESS")) return 0;
|
||||
if (!hos_sdl_load()) {
|
||||
hos_log("display: no SDL: no window");
|
||||
fprintf(stderr, "display: no SDL: no window\n");
|
||||
return -1;
|
||||
}
|
||||
hos_sdl_identity("Triforce", HOS_APP_ID);
|
||||
hos_sdl_hint("SDL_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR", "0");
|
||||
hos_sdl_hint("SDL_JOYSTICK_ALLOW_BACKGROUND_EVENTS", "1");
|
||||
/* (bundled SDL tries x11 before wayland. NVIDIA's X11 surface has no mailbox, and an immediate
|
||||
present then drops vblank for every output. Wayland has mailbox. SDL_VIDEODRIVER (SDL3: SDL_VIDEO_DRIVER) still wins.
|
||||
Under gamescope (XDG_CURRENT_DESKTOP names it: Steam Deck game mode) SDL keeps its own order.) */
|
||||
const char* desktop = getenv("XDG_CURRENT_DESKTOP");
|
||||
const int gamescope = desktop && strstr(desktop, "gamescope");
|
||||
if (!getenv("SDL_VIDEODRIVER") && !getenv("SDL_VIDEO_DRIVER") && !gamescope)
|
||||
hos_sdl_hint(hos_sdl_load() == 3 ? "SDL_VIDEO_DRIVER" : "SDL_VIDEODRIVER", "wayland,x11");
|
||||
if (!hos_sdl_init(HOS_SDL_VIDEO | HOS_SDL_EVENTS)) {
|
||||
hos_log("display: SDL_Init: %s: no window", hos_sdl_error());
|
||||
fprintf(stderr, "display: SDL_Init: %s: no window\n", hos_sdl_error());
|
||||
return -1;
|
||||
}
|
||||
int w, h;
|
||||
if (hos_sdl_desktop_size(&w, &h)) hos_posix_window[1] = w, hos_posix_window[2] = h;
|
||||
sdl_ready = 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* the game's thread, when NVN is up (hos_main.c): the window is made on the main thread, which waits for this */
|
||||
void hos_display_start(void) {
|
||||
if (getenv("HOS_HEADLESS") || !sdl_ready) { /* no window: the renderer still draws, offscreen (screenshots) */
|
||||
rx_start(NULL);
|
||||
return;
|
||||
}
|
||||
renderer_up = CreateEventA(NULL, TRUE, FALSE, NULL);
|
||||
__atomic_store_n(&start_asked, 1, __ATOMIC_RELEASE);
|
||||
}
|
||||
|
||||
/* the main thread's loop, until the process ends: the window once asked for, then its events */
|
||||
void hos_display_loop(void) {
|
||||
if (!sdl_ready) {
|
||||
for (;;) Sleep(1000);
|
||||
}
|
||||
HosSdlEvent e;
|
||||
while (!__atomic_load_n(&start_asked, __ATOMIC_ACQUIRE)) hos_sdl_wait_event(&e, 10); /* (nothing to show yet) */
|
||||
extern float hos_aspect_ratio;
|
||||
/* (the picture's shape when Monitor's shape set one, wider or narrower than 16:9: 960 x 720 for 4:3, 1720 x 720 for 21:9) */
|
||||
int win_w = hos_aspect_ratio > 0.0f ? (int)(720 * hos_aspect_ratio + 0.5f) : 1280, win_h = 720;
|
||||
if (getenv("HOS_WINDOW_SIZE")) {
|
||||
int w = 0, h = 0;
|
||||
if (sscanf(getenv("HOS_WINDOW_SIZE"), "%dx%d", &w, &h) == 2 && w >= 64 && h >= 64) win_w = w, win_h = h;
|
||||
}
|
||||
window = hos_sdl_window(HOS_GAME_TITLE, win_w, win_h);
|
||||
if (!window) {
|
||||
hos_log("display: SDL_CreateWindow: %s: no window", hos_sdl_error());
|
||||
fprintf(stderr, "display: SDL_CreateWindow: %s: no window\n", hos_sdl_error());
|
||||
hos_log_flush();
|
||||
ExitProcess(1);
|
||||
}
|
||||
vk_window.window = window;
|
||||
hos_posix_window[0] = 1;
|
||||
{ /* the window's icon: hookshot.png beside the game (make_game.py puts it there), for X11 and the compositors that
|
||||
take one from the window; the others find it through the menu entry */
|
||||
char png[MAX_PATH];
|
||||
DWORD n = GetModuleFileNameA(NULL, png, MAX_PATH);
|
||||
while (n && png[n - 1] != '/') n--;
|
||||
snprintf(png + n, sizeof png - n, "triforce.png");
|
||||
hos_sdl_icon(window, png);
|
||||
}
|
||||
if (getenv("HOS_FULLSCREEN") && atoi(getenv("HOS_FULLSCREEN"))) set_fullscreen(1);
|
||||
__atomic_store_n(&refresh_now, hos_sdl_refresh_mhz(window), __ATOMIC_RELAXED);
|
||||
CloseHandle(CreateThread(NULL, 0, renderer_main, NULL, 0, NULL));
|
||||
DWORD refresh_asked = GetTickCount();
|
||||
for (;;) {
|
||||
hos_sdl_pads_pump();
|
||||
if (GetTickCount() - refresh_asked >= 1000) { /* (the window may have moved to another display) */
|
||||
refresh_asked = GetTickCount();
|
||||
__atomic_store_n(&refresh_now, hos_sdl_refresh_mhz(window), __ATOMIC_RELAXED);
|
||||
}
|
||||
if (hos_sdl_wait_event(&e, 4)) {
|
||||
do {
|
||||
switch (e.type) {
|
||||
case HOS_SDL_EV_QUIT:
|
||||
case HOS_SDL_EV_CLOSE:
|
||||
rx_close_asked(); /* (a start still preparing the shaders keeps what it made) */
|
||||
hos_window_closed_exit();
|
||||
case HOS_SDL_EV_RESIZED:
|
||||
if (renderer) {
|
||||
int w = 0, h = 0;
|
||||
hos_sdl_pixel_size(window, &w, &h);
|
||||
rx_resize(w, h);
|
||||
}
|
||||
break;
|
||||
case HOS_SDL_EV_FOCUS_GAINED:
|
||||
hos_posix_window[0] = 1;
|
||||
break;
|
||||
case HOS_SDL_EV_FOCUS_LOST:
|
||||
hos_posix_window[0] = 0;
|
||||
mouse_capture(0);
|
||||
ptr_in = 0, ptr_held = 0;
|
||||
break;
|
||||
case HOS_SDL_EV_MOUSE_LEAVE:
|
||||
/* (Wayland: turning the relative mode on sends a leave in the same millisecond (Mutter), and
|
||||
releasing here dropped the capture before the first motion: the mouse turned nothing and its
|
||||
buttons did nothing. The capture goes with the window's focus, not with the pointer: Windows
|
||||
releases on WM_KILLFOCUS alone, host/display.c.) */
|
||||
ptr_in = 0;
|
||||
break;
|
||||
case HOS_SDL_EV_MOUSE_ENTER:
|
||||
ptr_in = 1;
|
||||
break;
|
||||
case HOS_SDL_EV_KEY_DOWN: {
|
||||
const int scancode = e.scancode, mod = e.mod;
|
||||
if (e.scancode == 41) mouse_capture(0); /* Esc */
|
||||
if (!e.repeat && ((scancode == 67 && !(mod & 0x0FC3)) || (scancode == 40 && (mod & 0x300)))) set_fullscreen(!fullscreen); /* F10, Alt+Enter (Triforce: F11 is off) */
|
||||
break;
|
||||
}
|
||||
case HOS_SDL_EV_MOUSE_DOWN:
|
||||
ptr_x = (LONG)e.x, ptr_y = (LONG)e.y, ptr_in = 1;
|
||||
InterlockedOr(&ptr_held, (LONG)sdl_button_bit(e.button));
|
||||
if (menu_open) InterlockedOr(&ptr_pressed, (LONG)sdl_button_bit(e.button)); /* (the menu's click) */
|
||||
else if (renderer && !mouse_captured) mouse_capture(1);
|
||||
break;
|
||||
case HOS_SDL_EV_MOUSE_UP:
|
||||
ptr_x = (LONG)e.x, ptr_y = (LONG)e.y;
|
||||
InterlockedAnd(&ptr_held, ~(LONG)sdl_button_bit(e.button));
|
||||
break;
|
||||
case HOS_SDL_EV_MOUSE_WHEEL:
|
||||
if (menu_open) InterlockedExchangeAdd(&ptr_wheel, (LONG)(e.wheel * 120));
|
||||
break;
|
||||
case HOS_SDL_EV_MOUSE_MOTION:
|
||||
if (!mouse_captured) ptr_x = (LONG)e.x, ptr_y = (LONG)e.y, ptr_in = 1;
|
||||
if (mouse_captured) { /* (SDL3's motion comes in fractions of a pixel: the remainders kept for the next) */
|
||||
static float rx, ry;
|
||||
rx += e.dx, ry += e.dy;
|
||||
const LONG dx = (LONG)rx, dy = (LONG)ry;
|
||||
rx -= (float)dx, ry -= (float)dy;
|
||||
InterlockedExchangeAdd(&mouse_dx, dx);
|
||||
InterlockedExchangeAdd(&mouse_dy, dy);
|
||||
}
|
||||
break;
|
||||
}
|
||||
} while (hos_sdl_poll_event(&e));
|
||||
}
|
||||
const int asked = __atomic_exchange_n(&fullscreen_asked, -1, __ATOMIC_ACQ_REL);
|
||||
if (asked >= 0) set_fullscreen(asked);
|
||||
const int menu = __atomic_exchange_n(&menu_asked, -1, __ATOMIC_ACQ_REL);
|
||||
if (menu >= 0) menu_mouse(menu);
|
||||
if (__atomic_load_n(&restart_asked, __ATOMIC_ACQUIRE)) {
|
||||
hos_relaunch_ask();
|
||||
rx_close_asked();
|
||||
hos_window_closed_exit();
|
||||
}
|
||||
if (__atomic_load_n(&close_asked, __ATOMIC_ACQUIRE)) {
|
||||
rx_close_asked();
|
||||
hos_window_closed_exit();
|
||||
}
|
||||
keys_update();
|
||||
}
|
||||
}
|
||||
3
host/posix/io.h
Normal file
3
host/posix/io.h
Normal file
@@ -0,0 +1,3 @@
|
||||
/* (linux) io.h: POSIX has these in unistd.h */
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
50
host/posix/linux_stubs.c
Normal file
50
host/posix/linux_stubs.c
Normal file
@@ -0,0 +1,50 @@
|
||||
/* (linux) What the Windows build has and Linux does without:
|
||||
|
||||
- Media Foundation's AV1 decoder (host/mf_av1.c): FFmpeg decodes AV1 itself here, with dav1d (host/nn_video.c asks
|
||||
FFmpeg first), so these are never reached with a dav1d build; without one, AV1 movies are skipped.
|
||||
- The WebGPU renderer (gpu/render, wgr_*): the Vulkan back end is the only renderer on Linux. Should it not start,
|
||||
these leave the window black, as a WebGPU renderer without a device does. */
|
||||
#include "../hos.h"
|
||||
#include "../../gpu/vk/vk_api.h"
|
||||
|
||||
typedef void (*mf_av1_picture_fn)(void *user, const uint8_t *y, const uint8_t *uv, int w, int h, int stride, int64_t pts);
|
||||
void *mf_av1_open(int w, int h) { (void)w, (void)h; return NULL; }
|
||||
void mf_av1_decode(void *m, const uint8_t *data, size_t n, int64_t pts, mf_av1_picture_fn fn, void *user) {
|
||||
(void)m, (void)data, (void)n, (void)pts, (void)fn, (void)user;
|
||||
}
|
||||
void mf_av1_flush(void *m) { (void)m; }
|
||||
void mf_av1_close(void *m) { (void)m; }
|
||||
|
||||
int wgr_start(void *hwnd) { (void)hwnd; hos_log("rx: no WebGPU renderer in the Linux build: nothing is drawn"); return 0; }
|
||||
void wgr_resize(int width, int height) { (void)width, (void)height; }
|
||||
const char *wgr_describe(void) { return "none"; }
|
||||
void wgr_stats(int64_t *draws, int64_t *skipped, int64_t *pipelines) { *draws = *skipped = *pipelines = 0; }
|
||||
void wgr_nvn_submit(const uint64_t *handles, int n) { (void)handles, (void)n; }
|
||||
uint64_t wgr_nvn_fence(void) { return 0; }
|
||||
uint64_t wgr_nvn_pushed(void) { return 0; }
|
||||
uint64_t wgr_nvn_replayed(void) { return 0; }
|
||||
int wgr_nvn_wait(uint64_t fence, uint64_t timeout_ns) { (void)fence, (void)timeout_ns; return 1; }
|
||||
void wgr_wait_writebacks_reading(const void *p, uint64_t n) { (void)p, (void)n; }
|
||||
void wgr_nvn_finish(void) {}
|
||||
void wgr_nvn_present(const nvn_tex *t, const int crop[4]) { (void)t, (void)crop; }
|
||||
void wgr_nvn_register_texture(int id, const nvn_tex *t, const nvn_view *v) { (void)id, (void)t, (void)v; }
|
||||
void wgr_nvn_register_sampler(int id, const nvn_samp_par *p) { (void)id, (void)p; }
|
||||
void wgr_nvn_set_texture_pool_size(int n) { (void)n; }
|
||||
void wgr_nvn_texture_released(const nvn_tex *t) { (void)t; }
|
||||
void wgr_nvn_program_released(const nvn_prog *p) { (void)p; }
|
||||
void wgr_nvn_read_texels(const nvn_tex *t, unsigned level, unsigned layer0, const NVNcopyRegion *r, uint8_t *dst,
|
||||
uint64_t row, uint64_t img) {
|
||||
(void)t, (void)level, (void)layer0, (void)r, (void)dst, (void)row, (void)img;
|
||||
}
|
||||
void wgr_nvn_write_texels(const nvn_tex *t, unsigned level, unsigned layer0, const NVNcopyRegion *r, const uint8_t *src,
|
||||
uint64_t row, uint64_t img) {
|
||||
(void)t, (void)level, (void)layer0, (void)r, (void)src, (void)row, (void)img;
|
||||
}
|
||||
void wgr_nvn_invalidate(uint64_t gpu, uint64_t size) { (void)gpu, (void)size; }
|
||||
/* (gpu/render/rx_core.cpp's, for the soft write watch's other consumer) */
|
||||
uint32_t *rx_collect_written(int64_t *count) { *count = -1; return NULL; }
|
||||
/* (gpu/render's debugging commands: host/nn_hid.c's live input "!dump", "!shots", "!rdoc", "!flasharm") */
|
||||
void rx_request_frame_dump(void) { hos_log("rx: frame dumps are the WebGPU renderer's: not in the Linux build"); }
|
||||
void rx_set_screenshot_ms(int64_t ms) { (void)ms; hos_log("rx: !shots is the WebGPU renderer's: RX_VK_SHOTS instead"); }
|
||||
void rx_request_rdoc_capture(int frames) { (void)frames; }
|
||||
void rx_flash_dark_arm(void) {}
|
||||
42
host/posix/mmsystem.h
Normal file
42
host/posix/mmsystem.h
Normal file
@@ -0,0 +1,42 @@
|
||||
/* (linux) waveOut, the audio renderer's output (host/nn_audio.c), on SDL's audio (win32_posix.c): the blocks written
|
||||
play in order and are marked WHDR_DONE as the device takes them, as on Windows. */
|
||||
#ifndef HOOKSHOT_POSIX_MMSYSTEM_H
|
||||
#define HOOKSHOT_POSIX_MMSYSTEM_H
|
||||
#include <windows.h>
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
typedef UINT MMRESULT;
|
||||
typedef void *HWAVEOUT;
|
||||
#define MMSYSERR_NOERROR 0
|
||||
#define MMSYSERR_ERROR 1
|
||||
#define WAVE_MAPPER ((UINT)-1)
|
||||
#define WAVE_FORMAT_PCM 1
|
||||
#define CALLBACK_NULL 0
|
||||
#define WHDR_DONE 0x1
|
||||
#define WHDR_PREPARED 0x2
|
||||
#define WHDR_INQUEUE 0x10
|
||||
typedef struct {
|
||||
WORD wFormatTag, nChannels;
|
||||
DWORD nSamplesPerSec, nAvgBytesPerSec;
|
||||
WORD nBlockAlign, wBitsPerSample, cbSize;
|
||||
} WAVEFORMATEX;
|
||||
typedef struct wavehdr_tag {
|
||||
LPSTR lpData;
|
||||
DWORD dwBufferLength, dwBytesRecorded;
|
||||
DWORD_PTR dwUser;
|
||||
volatile DWORD dwFlags;
|
||||
DWORD dwLoops;
|
||||
struct wavehdr_tag *lpNext;
|
||||
DWORD_PTR reserved;
|
||||
} WAVEHDR;
|
||||
MMRESULT waveOutOpen(HWAVEOUT *out, UINT device, const WAVEFORMATEX *f, DWORD_PTR cb, DWORD_PTR inst, DWORD flags);
|
||||
MMRESULT waveOutPrepareHeader(HWAVEOUT w, WAVEHDR *h, UINT size);
|
||||
MMRESULT waveOutUnprepareHeader(HWAVEOUT w, WAVEHDR *h, UINT size);
|
||||
MMRESULT waveOutWrite(HWAVEOUT w, WAVEHDR *h, UINT size);
|
||||
MMRESULT waveOutReset(HWAVEOUT w);
|
||||
MMRESULT waveOutClose(HWAVEOUT w);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
507
host/posix/prof.c
Normal file
507
host/posix/prof.c
Normal file
@@ -0,0 +1,507 @@
|
||||
/* (linux) host/mkw_glue.c's profilers, with the same variables and the same lines in the log and files:
|
||||
HOS_PROFILE=<seconds> (the busiest threads), HOS_THREAD_TIMES=<seconds> (threads over 5% of a core), HOS_SAMPLE=<thread
|
||||
name> + HOS_SAMPLE_FILE (one thread's stacks every millisecond), HOS_SAMPLE_ALL=<path> (every guest thread and the
|
||||
renderer's at once, every 2 ms, while <path>.go exists) and HOS_WATCH=<file> (data breakpoints on guest addresses).
|
||||
Windows reads other threads with SuspendThread + GetThreadContext + RtlVirtualUnwind and sets debug registers; here:
|
||||
- CPU time per thread from /proc/self/task/<tid>/schedstat (stat's utime + stime without schedstats);
|
||||
- a thread's stack from inside it: SAMPLE_SIG sent to it with tgkill, its handler takes the interrupted rip from the
|
||||
signal's context and unwinds from there with libgcc's unwinder (the .eh_frame clang writes), the sampler waits;
|
||||
- breakpoints with perf_event_open (PERF_TYPE_BREAKPOINT, sigtrap=1: a SIGTRAP in the thread that touched the
|
||||
address, si_code TRAP_PERF), allowed on our own threads at the default perf_event_paranoid (2).
|
||||
Frames are written as <module>+<offset from its load base>: hookshot and triforce_vk.so,
|
||||
offsets as in their linker maps (position-independent, based at 0). */
|
||||
#include "../hos.h"
|
||||
#include <dirent.h>
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
#include <linux/hw_breakpoint.h>
|
||||
#include <linux/perf_event.h>
|
||||
#include <sched.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <time.h>
|
||||
#include <ucontext.h>
|
||||
#include <unistd.h>
|
||||
#include <unwind.h>
|
||||
#include <windows.h>
|
||||
#ifndef TRAP_PERF /* (the kernel's since 5.13; glibc names it from 2.36) */
|
||||
#define TRAP_PERF 6
|
||||
#endif
|
||||
|
||||
int hos_thread_handles(HANDLE *out, const char **names, int max); /* hos_thread.c */
|
||||
void hos_prof_start(void);
|
||||
|
||||
/* ---- the process's threads: id, CPU time, name (the guest thread's, else the thread's own: SetThreadDescription) ---- */
|
||||
typedef struct { int tid; uint64_t cpu_ns; char name[64]; int guest; } Task;
|
||||
|
||||
static uint64_t task_cpu_ns(int tid) {
|
||||
char p[64];
|
||||
unsigned long long ns = 0, ut = 0, st = 0;
|
||||
snprintf(p, sizeof p, "/proc/self/task/%d/schedstat", tid);
|
||||
FILE *f = fopen(p, "r");
|
||||
if (f) {
|
||||
const int got = fscanf(f, "%llu", &ns);
|
||||
fclose(f);
|
||||
if (got == 1) return ns;
|
||||
}
|
||||
snprintf(p, sizeof p, "/proc/self/task/%d/stat", tid);
|
||||
f = fopen(p, "r");
|
||||
if (!f) return 0;
|
||||
char line[1024];
|
||||
const char *s = fgets(line, sizeof line, f) ? strrchr(line, ')') : NULL;
|
||||
fclose(f);
|
||||
/* (after "(comm)": state is field 3, utime 14, stime 15) */
|
||||
if (!s || sscanf(s + 1, " %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %llu %llu", &ut, &st) != 2) return 0;
|
||||
return (ut + st) * (1000000000ull / (unsigned long long)sysconf(_SC_CLK_TCK));
|
||||
}
|
||||
static void task_comm(int tid, char *out, size_t cap) {
|
||||
char p[64];
|
||||
snprintf(p, sizeof p, "/proc/self/task/%d/comm", tid);
|
||||
out[0] = 0;
|
||||
FILE *f = fopen(p, "r");
|
||||
if (!f) return;
|
||||
if (fgets(out, (int)cap, f)) out[strcspn(out, "\n")] = 0;
|
||||
fclose(f);
|
||||
}
|
||||
static int tasks(Task *out, int max) {
|
||||
static HANDLE gh[1024];
|
||||
static const char *gn[1024];
|
||||
const int ng = hos_thread_handles(gh, gn, 1024);
|
||||
static int gtid[1024];
|
||||
for (int i = 0; i < ng; i++) gtid[i] = (int)GetThreadId(gh[i]);
|
||||
int n = 0;
|
||||
DIR *d = opendir("/proc/self/task");
|
||||
if (!d) return 0;
|
||||
for (struct dirent *e; (e = readdir(d)) && n < max;) {
|
||||
const int tid = atoi(e->d_name);
|
||||
if (tid <= 0) continue;
|
||||
Task *t = &out[n++];
|
||||
t->tid = tid;
|
||||
t->cpu_ns = task_cpu_ns(tid);
|
||||
t->guest = 0;
|
||||
t->name[0] = 0;
|
||||
for (int i = 0; i < ng; i++)
|
||||
if (gtid[i] == tid) { snprintf(t->name, sizeof t->name, "%s", gn[i]); t->guest = 1; break; }
|
||||
if (!t->name[0]) task_comm(tid, t->name, sizeof t->name);
|
||||
if (!t->name[0]) snprintf(t->name, sizeof t->name, "tid %d", tid);
|
||||
}
|
||||
closedir(d);
|
||||
return n;
|
||||
}
|
||||
|
||||
/* HOS_PROFILE=<seconds>: every interval, the busiest threads and their share of a core */
|
||||
static DWORD WINAPI prof_thread(void *p) {
|
||||
const DWORD ms = (DWORD)(uintptr_t)p * 1000;
|
||||
enum { MAXT = 512 };
|
||||
static int ids[MAXT];
|
||||
static uint64_t was[MAXT];
|
||||
int nwas = 0;
|
||||
for (;;) {
|
||||
Sleep(ms);
|
||||
static Task cur[MAXT];
|
||||
static uint64_t delta[MAXT];
|
||||
const int n = tasks(cur, MAXT);
|
||||
for (int i = 0; i < n; i++) {
|
||||
delta[i] = cur[i].cpu_ns;
|
||||
for (int k = 0; k < nwas; k++)
|
||||
if (ids[k] == cur[i].tid) { delta[i] = cur[i].cpu_ns - was[k]; break; }
|
||||
}
|
||||
for (int i = 0; i < n; i++) ids[i] = cur[i].tid, was[i] = cur[i].cpu_ns;
|
||||
nwas = n;
|
||||
for (int i = 0; i < n; i++) /* (busiest first) */
|
||||
for (int j = i + 1; j < n; j++)
|
||||
if (delta[j] > delta[i]) {
|
||||
Task t = cur[i]; cur[i] = cur[j]; cur[j] = t;
|
||||
const uint64_t d = delta[i]; delta[i] = delta[j]; delta[j] = d;
|
||||
}
|
||||
char line[1024];
|
||||
int len = snprintf(line, sizeof line, "prof: busiest threads (%% of a core):");
|
||||
uint64_t total = 0;
|
||||
for (int i = 0; i < n; i++) total += delta[i];
|
||||
for (int i = 0; i < n && i < 10 && len < (int)sizeof line - 80; i++)
|
||||
len += snprintf(line + len, sizeof line - len, " %s %.0f,", cur[i].name, delta[i] / (ms * 10000.0));
|
||||
snprintf(line + len, sizeof line - len, " all %.0f", total / (ms * 10000.0));
|
||||
hos_log("%s", line);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* HOS_THREAD_TIMES=<seconds>: every interval, the threads that used over 5% of a core */
|
||||
static DWORD WINAPI thread_times_thread(void *p) {
|
||||
const DWORD ms = (DWORD)(uintptr_t)p * 1000;
|
||||
enum { MAXT = 512 };
|
||||
static struct { int tid; uint64_t cpu; } last[MAXT];
|
||||
int nlast = 0;
|
||||
for (;;) {
|
||||
Sleep(ms);
|
||||
static Task now[MAXT];
|
||||
const int n = tasks(now, MAXT);
|
||||
char line[4096];
|
||||
int len = snprintf(line, sizeof line, "threads in %u s:", (unsigned)(ms / 1000));
|
||||
for (int i = 0; i < n; i++) {
|
||||
uint64_t before = 0;
|
||||
int seen = 0;
|
||||
for (int k = 0; k < nlast; k++)
|
||||
if (last[k].tid == now[i].tid) before = last[k].cpu, seen = 1;
|
||||
const double pct = seen ? (double)(now[i].cpu_ns - before) / (ms * 1e6) * 100.0 : 0;
|
||||
if (pct >= 5.0 && len < (int)sizeof line - 96) len += snprintf(line + len, sizeof line - len, " %s %.0f%%;", now[i].name, pct);
|
||||
}
|
||||
for (int i = 0; i < n; i++) last[i].tid = now[i].tid, last[i].cpu = now[i].cpu_ns;
|
||||
nlast = n;
|
||||
hos_log("prof: %s", line);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ---- one thread's stack, taken inside it ---- */
|
||||
#define SAMPLE_SIG (SIGRTMIN + 3)
|
||||
enum { DEPTH = 48 };
|
||||
static volatile int sample_tid;
|
||||
static volatile int sample_done;
|
||||
static uintptr_t sample_pcs[DEPTH];
|
||||
static int sample_n;
|
||||
|
||||
typedef struct { uintptr_t *pcs; int n, max; uintptr_t rip; int found; } Walk;
|
||||
static _Unwind_Reason_Code walk_step(struct _Unwind_Context *uc, void *p) {
|
||||
Walk *w = (Walk *)p;
|
||||
int before = 0;
|
||||
const uintptr_t ip = (uintptr_t)_Unwind_GetIPInfo(uc, &before);
|
||||
if (!w->found) { /* (the handler's frames and the signal's trampoline, up to the interrupted frame: frame 0) */
|
||||
if (ip == w->rip) w->found = 1;
|
||||
return _URC_NO_REASON;
|
||||
}
|
||||
if (w->n >= w->max || !ip) return _URC_END_OF_STACK;
|
||||
w->pcs[w->n++] = ip; /* (return addresses, as the Windows sampler writes them) */
|
||||
return _URC_NO_REASON;
|
||||
}
|
||||
static void on_sample(int sig, siginfo_t *si, void *uctx) {
|
||||
(void)sig, (void)si;
|
||||
const int saved = errno;
|
||||
if ((int)syscall(SYS_gettid) == sample_tid) {
|
||||
const ucontext_t *u = (const ucontext_t *)uctx;
|
||||
const uintptr_t rip = (uintptr_t)u->uc_mcontext.gregs[REG_RIP];
|
||||
Walk w = {sample_pcs + 1, 0, DEPTH - 1, rip, 0};
|
||||
sample_pcs[0] = rip;
|
||||
_Unwind_Backtrace(walk_step, &w);
|
||||
sample_n = 1 + w.n;
|
||||
__atomic_store_n(&sample_done, 1, __ATOMIC_RELEASE);
|
||||
}
|
||||
errno = saved;
|
||||
}
|
||||
static void sampling_init(void) {
|
||||
static int done;
|
||||
if (done++) return;
|
||||
struct sigaction sa;
|
||||
memset(&sa, 0, sizeof sa);
|
||||
sa.sa_sigaction = on_sample;
|
||||
sa.sa_flags = SA_SIGINFO | SA_RESTART | SA_ONSTACK;
|
||||
sigemptyset(&sa.sa_mask);
|
||||
sigaction(SAMPLE_SIG, &sa, NULL);
|
||||
Walk w = {sample_pcs, 0, 1, 0, 1};
|
||||
_Unwind_Backtrace(walk_step, &w); /* (the unwinder's first use loads and sets up: not in a handler) */
|
||||
}
|
||||
/* the thread's frames, leaf first, into pcs; 0 when it did not answer within 50 ms or has ended */
|
||||
static int sample_one(int tid, uintptr_t *pcs, int max) {
|
||||
__atomic_store_n(&sample_done, 0, __ATOMIC_RELAXED);
|
||||
__atomic_store_n(&sample_tid, tid, __ATOMIC_RELEASE);
|
||||
if (syscall(SYS_tgkill, getpid(), tid, SAMPLE_SIG)) { sample_tid = 0; return -1; }
|
||||
const int64_t until = hos_now_ns() + 50000000;
|
||||
while (!__atomic_load_n(&sample_done, __ATOMIC_ACQUIRE)) {
|
||||
if (hos_now_ns() > until) { __atomic_store_n(&sample_tid, 0, __ATOMIC_RELEASE); return 0; }
|
||||
sched_yield();
|
||||
}
|
||||
sample_tid = 0;
|
||||
const int n = sample_n < max ? sample_n : max;
|
||||
memcpy(pcs, sample_pcs, sizeof *pcs * (size_t)n);
|
||||
return n;
|
||||
}
|
||||
|
||||
/* a code address as module + offset: the module's file name and load base (dladdr) */
|
||||
typedef struct { uintptr_t base; char name[64]; } Module;
|
||||
static Module mods[64];
|
||||
static int nmods;
|
||||
static int module_of(uintptr_t pc, uintptr_t *base) {
|
||||
Dl_info d;
|
||||
if (!dladdr((void *)pc, &d) || !d.dli_fbase) { *base = 0; return -1; }
|
||||
*base = (uintptr_t)d.dli_fbase;
|
||||
for (int i = 0; i < nmods; i++)
|
||||
if (mods[i].base == *base) return i;
|
||||
if (nmods >= 64) return -1;
|
||||
const char *n = d.dli_fname && d.dli_fname[0] ? d.dli_fname : "hookshot";
|
||||
const char *b = strrchr(n, '/');
|
||||
mods[nmods].base = *base;
|
||||
snprintf(mods[nmods].name, sizeof mods[nmods].name, "%s", b ? b + 1 : n);
|
||||
return nmods++;
|
||||
}
|
||||
static uintptr_t exe_base(void) {
|
||||
uintptr_t base = 0;
|
||||
module_of((uintptr_t)&hos_prof_start, &base);
|
||||
return base;
|
||||
}
|
||||
|
||||
/* HOS_SAMPLE=<thread name> (with HOS_PROFILE=<seconds>): that thread sampled every millisecond; every interval its
|
||||
most frequent places, as module+offset of the leaf and of the first frame in the runtime under it. HOS_SAMPLE_FILE:
|
||||
every sample's whole stack too, a line of frames leaf first (scripts/prof.py) */
|
||||
static DWORD WINAPI sample_thread(void *p) {
|
||||
const char *want = (const char *)p;
|
||||
const DWORD ms = (DWORD)atoi(getenv("HOS_PROFILE")) * 1000;
|
||||
sampling_init();
|
||||
int tid = 0;
|
||||
while (!tid) { /* (the renderer's threads start late) */
|
||||
Sleep(1000);
|
||||
static Task all[512];
|
||||
const int n = tasks(all, 512);
|
||||
for (int i = 0; i < n && !tid; i++)
|
||||
if (strstr(all[i].name, want)) tid = all[i].tid;
|
||||
}
|
||||
hos_log("prof: sampling thread \"%s\"", want);
|
||||
const uintptr_t exe = exe_base();
|
||||
enum { SLOTS = 4096 };
|
||||
static struct { uint64_t key; uint32_t n; } leaf[SLOTS], ours[SLOTS];
|
||||
uint32_t total = 0;
|
||||
DWORD last = GetTickCount();
|
||||
FILE *stacks = getenv("HOS_SAMPLE_FILE") ? fopen(getenv("HOS_SAMPLE_FILE"), "w") : NULL;
|
||||
for (;;) {
|
||||
Sleep(1);
|
||||
uintptr_t pcs[DEPTH];
|
||||
const int n = sample_one(tid, pcs, DEPTH);
|
||||
if (n < 0) return 0; /* (the thread ended) */
|
||||
uint64_t keys[2] = {0, 0};
|
||||
char stack_line[1024];
|
||||
int stack_len = 0;
|
||||
for (int depth = 0; depth < n; depth++) {
|
||||
uintptr_t base;
|
||||
const int mi = module_of(pcs[depth], &base);
|
||||
const uint64_t key = (uint64_t)(mi < 0 ? 63 : mi) << 56 | (pcs[depth] - base);
|
||||
if (stacks && stack_len < (int)sizeof stack_line - 96)
|
||||
stack_len += snprintf(stack_line + stack_len, sizeof stack_line - stack_len, "%s%s+%llx", depth ? ";" : "",
|
||||
mi < 0 ? "?" : mods[mi].name, (unsigned long long)(pcs[depth] - base));
|
||||
if (depth == 0) keys[0] = key;
|
||||
if (base == exe && !keys[1]) keys[1] = key;
|
||||
if (base == exe && !stacks) break;
|
||||
}
|
||||
if (stacks && stack_len) fprintf(stacks, "%s\n", stack_line);
|
||||
if (n > 0) {
|
||||
total++;
|
||||
for (int t = 0; t < 2; t++) {
|
||||
if (!keys[t]) continue;
|
||||
typeof(leaf[0]) *tab = t ? ours : leaf;
|
||||
for (uint32_t s = (uint32_t)(keys[t] * 0x9E3779B97F4A7C15ull >> 52), k = 0; k < SLOTS; k++, s = (s + 1) % SLOTS) {
|
||||
if (tab[s].key == keys[t]) { tab[s].n++; break; }
|
||||
if (!tab[s].key) { tab[s].key = keys[t], tab[s].n = 1; break; }
|
||||
}
|
||||
}
|
||||
}
|
||||
if (GetTickCount() - last >= ms) {
|
||||
last = GetTickCount();
|
||||
for (int t = 0; t < 2; t++) {
|
||||
typeof(leaf[0]) *tab = t ? ours : leaf;
|
||||
char line[2048];
|
||||
int len = snprintf(line, sizeof line, "prof-sample %s (%u samples):", t ? "ours" : "leaf", total);
|
||||
for (int r = 0; r < 30 && len < (int)sizeof line - 64; r++) {
|
||||
int best = -1;
|
||||
for (int s = 0; s < SLOTS; s++) if (tab[s].n && (best < 0 || tab[s].n > tab[best].n)) best = s;
|
||||
if (best < 0) break;
|
||||
const int mi = (int)(tab[best].key >> 56);
|
||||
len += snprintf(line + len, sizeof line - len, " %s+%llx %u,", mi < nmods ? mods[mi].name : "?",
|
||||
(unsigned long long)(tab[best].key & ((1ull << 56) - 1)), tab[best].n);
|
||||
tab[best].n = 0;
|
||||
}
|
||||
hos_log("%s", line);
|
||||
memset(tab, 0, sizeof leaf);
|
||||
}
|
||||
total = 0;
|
||||
if (stacks) fflush(stacks);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* HOS_SAMPLE_ALL=<path> (with HOS_PROFILE): while <path>.go exists, every 2 ms every guest thread and the renderer's
|
||||
threads (names starting "mkw_vk") sampled one after another: a line per thread, "<ms> <name>|<frames>", 16 frames
|
||||
at most (scripts/crit.py, scripts/hotlist.py) */
|
||||
static DWORD WINAPI sample_all_thread(void *p) {
|
||||
const char *path = (const char *)p;
|
||||
char go[MAX_PATH];
|
||||
snprintf(go, sizeof go, "%s.go", path);
|
||||
sampling_init();
|
||||
FILE *out = NULL;
|
||||
enum { MAXT = 128 };
|
||||
static int th[MAXT];
|
||||
static char tn[MAXT][64];
|
||||
int nt = 0;
|
||||
int64_t t0 = 0;
|
||||
const int self = (int)syscall(SYS_gettid);
|
||||
for (;;) {
|
||||
if (access(go, F_OK)) {
|
||||
if (out) { fclose(out); out = NULL; hos_log("prof: all-thread sampling stopped"); }
|
||||
Sleep(200);
|
||||
continue;
|
||||
}
|
||||
if (!out) {
|
||||
out = fopen(path, "a");
|
||||
if (!out) { Sleep(1000); continue; }
|
||||
static Task all[512];
|
||||
const int n = tasks(all, 512);
|
||||
nt = 0;
|
||||
for (int i = 0; i < n && nt < MAXT; i++) {
|
||||
if (all[i].tid == self || (!all[i].guest && strncmp(all[i].name, "mkw_vk", 6))) continue;
|
||||
th[nt] = all[i].tid;
|
||||
snprintf(tn[nt], sizeof tn[nt], "%s", all[i].name);
|
||||
for (char *q = tn[nt]; *q; q++) if (*q == ' ' || *q == '|') *q = '_';
|
||||
nt++;
|
||||
}
|
||||
t0 = hos_now_ns();
|
||||
hos_log("prof: all-thread sampling of %d threads into %s", nt, path);
|
||||
}
|
||||
Sleep(2);
|
||||
const long ms = (long)((hos_now_ns() - t0) / 1000000);
|
||||
for (int i = 0; i < nt; i++) {
|
||||
uintptr_t pcs[16];
|
||||
const int n = sample_one(th[i], pcs, 16);
|
||||
if (n < 0) continue;
|
||||
char line[1024];
|
||||
int len = snprintf(line, sizeof line, "%ld %s|", ms, tn[i]);
|
||||
for (int depth = 0; depth < n && len < (int)sizeof line - 96; depth++) {
|
||||
uintptr_t base;
|
||||
const int mi = module_of(pcs[depth], &base);
|
||||
len += snprintf(line + len, sizeof line - len, "%s%s+%llx", depth ? ";" : "", mi < 0 ? "?" : mods[mi].name,
|
||||
(unsigned long long)(pcs[depth] - base));
|
||||
}
|
||||
fprintf(out, "%s\n", line);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* HOS_WATCH=<file> (diagnostic): when <file> exists (up to 4 guest addresses in hex), data breakpoints on them on every
|
||||
guest thread: 4 s on reads and writes, then 4 s on writes; <file>.out gets, by address and the host place of the
|
||||
access, how many; then <file> is renamed <file>.done and the next one may come. */
|
||||
static uint64_t watch_keys[8192];
|
||||
static volatile LONG watch_counts[8192];
|
||||
static volatile LONG watch_phase;
|
||||
static struct sigaction watch_prev;
|
||||
static void on_watch(int sig, siginfo_t *si, void *uctx) {
|
||||
if (si->si_code != TRAP_PERF) { /* (a breakpoint of the game's or a debugger's: host/linux/win32.c's handlers) */
|
||||
if (watch_prev.sa_flags & SA_SIGINFO) watch_prev.sa_sigaction(sig, si, uctx);
|
||||
else if (watch_prev.sa_handler != SIG_DFL && watch_prev.sa_handler != SIG_IGN) watch_prev.sa_handler(sig);
|
||||
else { signal(sig, SIG_DFL); raise(sig); }
|
||||
return;
|
||||
}
|
||||
/* (si_perf_data, the event's sig_data: the watch's index. glibc's siginfo_t has no name for it: the kernel puts it
|
||||
after si_addr, at 24) */
|
||||
const unsigned long idx = *(const unsigned long *)((const char *)si + 24) & 3;
|
||||
const uint64_t rip = (uint64_t)((const ucontext_t *)uctx)->uc_mcontext.gregs[REG_RIP];
|
||||
const uint64_t key = ((uint64_t)(watch_phase & 1) << 62) | ((uint64_t)idx << 58) | rip;
|
||||
for (uint32_t i = (uint32_t)((key >> 2) * 2654435761u) & 8191, n = 0; n < 8192; n++, i = (i + 1) & 8191) {
|
||||
uint64_t k = __atomic_load_n(&watch_keys[i], __ATOMIC_RELAXED);
|
||||
if (!k) {
|
||||
uint64_t expect = 0;
|
||||
if (__atomic_compare_exchange_n(&watch_keys[i], &expect, key, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED)) k = key;
|
||||
else k = expect;
|
||||
}
|
||||
if (k == key) { InterlockedIncrement(&watch_counts[i]); break; }
|
||||
}
|
||||
}
|
||||
enum { WATCH_FDS = 1024 * 4 };
|
||||
static int watch_fd[WATCH_FDS];
|
||||
static int nwatch_fd;
|
||||
static void watch_arm(const uint64_t *addr, int n, int rw) {
|
||||
for (int i = 0; i < nwatch_fd; i++) close(watch_fd[i]);
|
||||
nwatch_fd = 0;
|
||||
if (!rw) return;
|
||||
static HANDLE gh[1024];
|
||||
static const char *gn[1024];
|
||||
const int ng = hos_thread_handles(gh, gn, 1024);
|
||||
int failed = 0, err = 0;
|
||||
for (int t = 0; t < ng; t++) {
|
||||
const int tid = (int)GetThreadId(gh[t]);
|
||||
if (!tid || tid == (int)GetCurrentThreadId()) continue;
|
||||
for (int k = 0; k < n && nwatch_fd < WATCH_FDS; k++) {
|
||||
struct perf_event_attr a;
|
||||
memset(&a, 0, sizeof a);
|
||||
a.type = PERF_TYPE_BREAKPOINT;
|
||||
a.size = sizeof a;
|
||||
a.bp_type = rw == 1 ? HW_BREAKPOINT_W : HW_BREAKPOINT_RW;
|
||||
a.bp_addr = (uint64_t)(uintptr_t)GP(addr[k]);
|
||||
a.bp_len = HW_BREAKPOINT_LEN_4;
|
||||
a.sample_period = 1;
|
||||
a.exclude_kernel = 1;
|
||||
a.exclude_hv = 1;
|
||||
a.sigtrap = 1;
|
||||
a.remove_on_exec = 1;
|
||||
a.sig_data = (uint64_t)k;
|
||||
const int fd = (int)syscall(SYS_perf_event_open, &a, tid, -1, -1, PERF_FLAG_FD_CLOEXEC);
|
||||
if (fd >= 0) watch_fd[nwatch_fd++] = fd;
|
||||
else failed++, err = errno;
|
||||
}
|
||||
}
|
||||
if (failed) hos_log("watch: %d breakpoints not set (perf_event_open: %s; /proc/sys/kernel/perf_event_paranoid)", failed, strerror(err));
|
||||
}
|
||||
static DWORD WINAPI watch_thread(void *p) {
|
||||
const char *path = (const char *)p;
|
||||
struct sigaction sa;
|
||||
memset(&sa, 0, sizeof sa);
|
||||
sa.sa_sigaction = on_watch;
|
||||
sa.sa_flags = SA_SIGINFO | SA_ONSTACK | SA_NODEFER;
|
||||
sigemptyset(&sa.sa_mask);
|
||||
for (;;) {
|
||||
Sleep(300);
|
||||
FILE *f = fopen(path, "r");
|
||||
if (!f) continue;
|
||||
uint64_t addr[4];
|
||||
int n = 0;
|
||||
unsigned long long v;
|
||||
while (n < 4 && fscanf(f, "%llx", &v) == 1) addr[n++] = v;
|
||||
fclose(f);
|
||||
struct sigaction cur; /* (ours ahead of win32.c's, which it calls for every other SIGTRAP) */
|
||||
sigaction(SIGTRAP, NULL, &cur);
|
||||
if (cur.sa_sigaction != on_watch) sigaction(SIGTRAP, &sa, &watch_prev);
|
||||
memset(watch_keys, 0, sizeof watch_keys);
|
||||
memset((void *)watch_counts, 0, sizeof watch_counts);
|
||||
for (int k = 0; k < n; k++) hos_log("watch: %llx holds %08x", (unsigned long long)addr[k], *(uint32_t *)GP(addr[k]));
|
||||
for (int ph = 0; ph < 2; ph++) {
|
||||
watch_phase = ph;
|
||||
for (int t = 0; t < 4; t++) { /* (threads that started since) */
|
||||
watch_arm(addr, n, ph ? 1 : 3);
|
||||
Sleep(1000);
|
||||
}
|
||||
}
|
||||
watch_arm(addr, 0, 0);
|
||||
char out[MAX_PATH], done[MAX_PATH];
|
||||
snprintf(out, sizeof out, "%s.out", path);
|
||||
snprintf(done, sizeof done, "%s.done", path);
|
||||
f = fopen(out, "a");
|
||||
if (f) {
|
||||
for (int i = 0; i < 8192; i++)
|
||||
if (watch_keys[i]) {
|
||||
const uint64_t k = watch_keys[i], rip = k & ((1ULL << 58) - 1);
|
||||
uintptr_t base;
|
||||
const int mi = module_of((uintptr_t)rip, &base);
|
||||
fprintf(f, "%llx %s %s+%llx %ld\n", (unsigned long long)addr[(k >> 58) & 3], k >> 62 & 1 ? "w" : "rw",
|
||||
mi < 0 ? "?" : mods[mi].name, (unsigned long long)(rip - base), (long)watch_counts[i]);
|
||||
}
|
||||
fclose(f);
|
||||
}
|
||||
unlink(done);
|
||||
rename(path, done);
|
||||
hos_log("watch: done");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void hos_prof_start(void) {
|
||||
const char *tt = getenv("HOS_THREAD_TIMES");
|
||||
if (tt && atoi(tt) > 0) CreateThread(NULL, 0, thread_times_thread, (void *)(uintptr_t)atoi(tt), 0, NULL);
|
||||
const char *e = getenv("HOS_PROFILE");
|
||||
if (e && atoi(e) > 0) CreateThread(NULL, 0, prof_thread, (void *)(uintptr_t)atoi(e), 0, NULL);
|
||||
const char *s = getenv("HOS_SAMPLE");
|
||||
if (e && atoi(e) > 0 && s && *s) CreateThread(NULL, 0, sample_thread, (void *)s, 0, NULL);
|
||||
const char *all = getenv("HOS_SAMPLE_ALL");
|
||||
if (all && *all) CreateThread(NULL, 0, sample_all_thread, (void *)all, 0, NULL);
|
||||
const char *watch = getenv("HOS_WATCH");
|
||||
if (watch && *watch) CreateThread(NULL, 0, watch_thread, (void *)watch, 0, NULL);
|
||||
}
|
||||
35
host/posix/psapi.h
Normal file
35
host/posix/psapi.h
Normal file
@@ -0,0 +1,35 @@
|
||||
/* (linux) psapi: the process's memory counters, from /proc/self/status (the working set is the resident set, private
|
||||
memory the anonymous part of it), for the back end's memory log lines */
|
||||
#ifndef HOOKSHOT_POSIX_PSAPI_H
|
||||
#define HOOKSHOT_POSIX_PSAPI_H
|
||||
#include <windows.h>
|
||||
typedef struct {
|
||||
DWORD cb, PageFaultCount;
|
||||
SIZE_T PeakWorkingSetSize, WorkingSetSize, QuotaPeakPagedPoolUsage, QuotaPagedPoolUsage, QuotaPeakNonPagedPoolUsage,
|
||||
QuotaNonPagedPoolUsage, PagefileUsage, PeakPagefileUsage;
|
||||
} PROCESS_MEMORY_COUNTERS;
|
||||
typedef struct {
|
||||
DWORD cb, PageFaultCount;
|
||||
SIZE_T PeakWorkingSetSize, WorkingSetSize, QuotaPeakPagedPoolUsage, QuotaPagedPoolUsage, QuotaPeakNonPagedPoolUsage,
|
||||
QuotaNonPagedPoolUsage, PagefileUsage, PeakPagefileUsage, PrivateUsage;
|
||||
} PROCESS_MEMORY_COUNTERS_EX;
|
||||
static inline BOOL K32GetProcessMemoryInfo(HANDLE p, PROCESS_MEMORY_COUNTERS *out, DWORD cb) {
|
||||
(void)p;
|
||||
FILE *f = fopen("/proc/self/status", "r");
|
||||
if (!f) return FALSE;
|
||||
SIZE_T rss = 0, hwm = 0, anon = 0;
|
||||
char line[256];
|
||||
while (fgets(line, sizeof line, f)) {
|
||||
unsigned long long kb;
|
||||
if (sscanf(line, "VmRSS: %llu", &kb) == 1) rss = (SIZE_T)kb << 10;
|
||||
else if (sscanf(line, "VmHWM: %llu", &kb) == 1) hwm = (SIZE_T)kb << 10;
|
||||
else if (sscanf(line, "RssAnon: %llu", &kb) == 1) anon = (SIZE_T)kb << 10;
|
||||
}
|
||||
fclose(f);
|
||||
memset(out, 0, cb);
|
||||
out->cb = cb, out->WorkingSetSize = rss, out->PeakWorkingSetSize = hwm, out->PagefileUsage = anon;
|
||||
if (cb >= sizeof(PROCESS_MEMORY_COUNTERS_EX)) ((PROCESS_MEMORY_COUNTERS_EX *)out)->PrivateUsage = anon;
|
||||
return TRUE;
|
||||
}
|
||||
#define GetProcessMemoryInfo K32GetProcessMemoryInfo
|
||||
#endif
|
||||
15
host/posix/tlhelp32.h
Normal file
15
host/posix/tlhelp32.h
Normal file
@@ -0,0 +1,15 @@
|
||||
/* (linux) Tool Help: the thread snapshots the profiler and the stall reports walk. There is no snapshot here
|
||||
(CreateToolhelp32Snapshot fails), so those debugging aids find no threads. */
|
||||
#ifndef HOOKSHOT_POSIX_TLHELP32_H
|
||||
#define HOOKSHOT_POSIX_TLHELP32_H
|
||||
#include <windows.h>
|
||||
#define TH32CS_SNAPTHREAD 0x4
|
||||
typedef struct {
|
||||
DWORD dwSize, cntUsage, th32ThreadID, th32OwnerProcessID;
|
||||
LONG tpBasePri, tpDeltaPri;
|
||||
DWORD dwFlags;
|
||||
} THREADENTRY32;
|
||||
static inline HANDLE CreateToolhelp32Snapshot(DWORD flags, DWORD pid) { (void)flags, (void)pid; return INVALID_HANDLE_VALUE; }
|
||||
static inline BOOL Thread32First(HANDLE s, THREADENTRY32 *te) { (void)s, (void)te; return FALSE; }
|
||||
static inline BOOL Thread32Next(HANDLE s, THREADENTRY32 *te) { (void)s, (void)te; return FALSE; }
|
||||
#endif
|
||||
1320
host/posix/win32_posix.c
Normal file
1320
host/posix/win32_posix.c
Normal file
File diff suppressed because it is too large
Load Diff
562
host/posix/windows.h
Normal file
562
host/posix/windows.h
Normal file
@@ -0,0 +1,562 @@
|
||||
/* (linux) The part of the Win32 API Hookshot's runtime and Vulkan back end use, on POSIX: host/posix is on the include
|
||||
path of Linux builds only (nx2/build.py), so `#include <windows.h>` finds this file and the Windows code paths stay
|
||||
as they are. Implemented in win32_posix.c (threads, events, fibers, files, memory, modules, exceptions, waveOut) and
|
||||
display_sdl.c (the window, keyboard, mouse).
|
||||
|
||||
Not a general emulation: what is here is what the code calls, with the behaviour it relies on. Handles are
|
||||
pointers to objects of this file's own (threads, events, timers, files, find searches); the guest window is one
|
||||
mapping (hos_main.c), so committing memory is a no-op and decommitting zeroes it. */
|
||||
#ifndef HOOKSHOT_POSIX_WINDOWS_H
|
||||
#define HOOKSHOT_POSIX_WINDOWS_H
|
||||
#ifndef _GNU_SOURCE
|
||||
#define _GNU_SOURCE
|
||||
#endif
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
#include <sched.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <wchar.h>
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* ---- types ---- */
|
||||
typedef int BOOL;
|
||||
typedef unsigned char BYTE;
|
||||
typedef uint16_t WORD;
|
||||
typedef uint32_t DWORD;
|
||||
typedef int32_t LONG;
|
||||
typedef uint32_t ULONG;
|
||||
typedef int64_t LONG64, LONGLONG;
|
||||
typedef uint64_t ULONG64, ULONGLONG, DWORD64, DWORDLONG;
|
||||
typedef uintptr_t DWORD_PTR, ULONG_PTR, UINT_PTR, WPARAM;
|
||||
typedef intptr_t LONG_PTR, INT_PTR, LPARAM, LRESULT;
|
||||
typedef size_t SIZE_T;
|
||||
typedef unsigned int UINT;
|
||||
typedef int INT;
|
||||
typedef short SHORT;
|
||||
typedef unsigned short USHORT;
|
||||
typedef char CHAR;
|
||||
typedef wchar_t WCHAR;
|
||||
typedef void *HANDLE, *HMODULE, *HINSTANCE, *HWND, *HDC, *HGDIOBJ, *HBITMAP, *HFONT, *HBRUSH, *HICON, *HCURSOR, *HKEY;
|
||||
typedef void *PVOID, *LPVOID;
|
||||
typedef const void *LPCVOID;
|
||||
typedef char *LPSTR;
|
||||
typedef const char *LPCSTR;
|
||||
typedef wchar_t *PWSTR, *LPWSTR;
|
||||
typedef const wchar_t *PCWSTR, *LPCWSTR;
|
||||
typedef DWORD *LPDWORD;
|
||||
typedef LONG HRESULT;
|
||||
typedef DWORD COLORREF;
|
||||
typedef void (*FARPROC)(void);
|
||||
typedef union { struct { DWORD LowPart; LONG HighPart; } u; int64_t QuadPart; } LARGE_INTEGER;
|
||||
typedef struct { DWORD dwLowDateTime, dwHighDateTime; } FILETIME;
|
||||
typedef struct { LONG left, top, right, bottom; } RECT;
|
||||
typedef struct { LONG x, y; } POINT;
|
||||
typedef struct { LONG cx, cy; } SIZE;
|
||||
|
||||
#define WINAPI
|
||||
#define CALLBACK
|
||||
#define APIENTRY
|
||||
#define WINAPIV
|
||||
#ifndef TRUE
|
||||
#define TRUE 1
|
||||
#define FALSE 0
|
||||
#endif
|
||||
#ifndef MAX_PATH
|
||||
#define MAX_PATH 4096
|
||||
#endif
|
||||
#define INFINITE 0xFFFFFFFFu
|
||||
#define S_OK 0
|
||||
#define S_FALSE 1
|
||||
#define SUCCEEDED(hr) ((HRESULT)(hr) >= 0)
|
||||
#define FAILED(hr) ((HRESULT)(hr) < 0)
|
||||
#define INVALID_HANDLE_VALUE ((HANDLE)(intptr_t)-1)
|
||||
#define LOWORD(l) ((WORD)((uintptr_t)(l) & 0xffff))
|
||||
#define HIWORD(l) ((WORD)(((uintptr_t)(l) >> 16) & 0xffff))
|
||||
#define RGB(r, g, b) ((COLORREF)(((BYTE)(r) | ((WORD)((BYTE)(g)) << 8)) | (((DWORD)(BYTE)(b)) << 16)))
|
||||
#define UNREFERENCED_PARAMETER(x) ((void)(x))
|
||||
#define __declspec(x) HOOKSHOT_DECLSPEC_##x
|
||||
#define HOOKSHOT_DECLSPEC_dllexport __attribute__((visibility("default")))
|
||||
#define HOOKSHOT_DECLSPEC_dllimport
|
||||
#define HOOKSHOT_DECLSPEC_noinline __attribute__((noinline))
|
||||
#define HOOKSHOT_DECLSPEC_thread __thread
|
||||
#define _stricmp strcasecmp
|
||||
#define _strnicmp strncasecmp
|
||||
#define _strdup strdup
|
||||
#define _fseeki64 fseeko
|
||||
#define _ftelli64 ftello
|
||||
#define _mkdir(p) mkdir((p), 0755)
|
||||
#define _getpid getpid
|
||||
#define _countof(a) (sizeof(a) / sizeof((a)[0]))
|
||||
#define _environ environ
|
||||
#define _fileno fileno
|
||||
#define _chsize_s(fd, size) ftruncate((fd), (off_t)(size))
|
||||
#define _atoi64 atoll
|
||||
#define _mkgmtime timegm
|
||||
/* (MSVC's argument order: the result first; 0 on success) */
|
||||
static inline int gmtime_s(struct tm *out, const time_t *t) { return gmtime_r(t, out) ? 0 : EINVAL; }
|
||||
static inline int localtime_s(struct tm *out, const time_t *t) { return localtime_r(t, out) ? 0 : EINVAL; }
|
||||
|
||||
/* ---- errors (GetLastError: errno mapped to the codes the callers test) ---- */
|
||||
#define ERROR_SUCCESS 0L
|
||||
#define ERROR_FILE_NOT_FOUND 2L
|
||||
#define ERROR_PATH_NOT_FOUND 3L
|
||||
#define ERROR_ACCESS_DENIED 5L
|
||||
#define ERROR_INVALID_HANDLE 6L
|
||||
#define ERROR_NOT_ENOUGH_MEMORY 8L
|
||||
#define ERROR_NO_MORE_FILES 18L
|
||||
#define ERROR_HANDLE_EOF 38L
|
||||
#define ERROR_NOT_SUPPORTED 50L
|
||||
#define ERROR_FILE_EXISTS 80L
|
||||
#define ERROR_INVALID_PARAMETER 87L
|
||||
#define ERROR_ALREADY_EXISTS 183L
|
||||
#define ERROR_COMMITMENT_LIMIT 1455L
|
||||
#define ERROR_NOT_ALL_ASSIGNED 1300L
|
||||
#define ERROR_TIMEOUT 1460L
|
||||
#define ERROR_DEVICE_NOT_CONNECTED 1167L
|
||||
DWORD GetLastError(void);
|
||||
void SetLastError(DWORD e);
|
||||
|
||||
/* ---- atomics (type-generic, as MSVC's are per width) ---- */
|
||||
#define InterlockedIncrement(p) __atomic_add_fetch((p), 1, __ATOMIC_SEQ_CST)
|
||||
#define InterlockedDecrement(p) __atomic_sub_fetch((p), 1, __ATOMIC_SEQ_CST)
|
||||
#define InterlockedIncrement64 InterlockedIncrement
|
||||
#define InterlockedDecrement64 InterlockedDecrement
|
||||
#define InterlockedExchange(p, v) __atomic_exchange_n((p), (v), __ATOMIC_SEQ_CST)
|
||||
#define InterlockedExchange64 InterlockedExchange
|
||||
#define InterlockedExchangePointer InterlockedExchange
|
||||
#define InterlockedExchangeAdd(p, v) __atomic_fetch_add((p), (v), __ATOMIC_SEQ_CST)
|
||||
#define InterlockedExchangeAdd64 InterlockedExchangeAdd
|
||||
#define InterlockedAdd(p, v) __atomic_add_fetch((p), (v), __ATOMIC_SEQ_CST)
|
||||
#define InterlockedAdd64 InterlockedAdd
|
||||
#define InterlockedOr(p, v) __atomic_fetch_or((p), (v), __ATOMIC_SEQ_CST)
|
||||
#define InterlockedAnd(p, v) __atomic_fetch_and((p), (v), __ATOMIC_SEQ_CST)
|
||||
#define InterlockedOr64 InterlockedOr
|
||||
#define InterlockedAnd64 InterlockedAnd
|
||||
#define InterlockedCompareExchange(p, x, c) __sync_val_compare_and_swap((p), (c), (x))
|
||||
#define InterlockedCompareExchange64 InterlockedCompareExchange
|
||||
#define InterlockedCompareExchangePointer InterlockedCompareExchange
|
||||
#define MemoryBarrier() __atomic_thread_fence(__ATOMIC_SEQ_CST)
|
||||
#define _ReadWriteBarrier() __atomic_signal_fence(__ATOMIC_SEQ_CST)
|
||||
#define YieldProcessor() __builtin_ia32_pause()
|
||||
|
||||
/* ---- locks: SRW locks are POSIX rwlocks (zero-initialised is valid, as on Windows); condition variables a futex
|
||||
word, so they wait with either an SRW lock or a critical section ---- */
|
||||
typedef pthread_rwlock_t SRWLOCK, *PSRWLOCK;
|
||||
#define SRWLOCK_INIT PTHREAD_RWLOCK_INITIALIZER
|
||||
typedef struct { volatile uint32_t seq; } CONDITION_VARIABLE, *PCONDITION_VARIABLE;
|
||||
#define CONDITION_VARIABLE_INIT {0}
|
||||
#define CONDITION_VARIABLE_LOCKMODE_SHARED 0x1
|
||||
typedef pthread_mutex_t CRITICAL_SECTION, *LPCRITICAL_SECTION;
|
||||
static inline void InitializeSRWLock(SRWLOCK *l) { pthread_rwlock_init(l, NULL); }
|
||||
static inline void AcquireSRWLockExclusive(SRWLOCK *l) { pthread_rwlock_wrlock(l); }
|
||||
static inline void ReleaseSRWLockExclusive(SRWLOCK *l) { pthread_rwlock_unlock(l); }
|
||||
static inline void AcquireSRWLockShared(SRWLOCK *l) { pthread_rwlock_rdlock(l); }
|
||||
static inline void ReleaseSRWLockShared(SRWLOCK *l) { pthread_rwlock_unlock(l); }
|
||||
static inline BOOL TryAcquireSRWLockExclusive(SRWLOCK *l) { return pthread_rwlock_trywrlock(l) == 0; }
|
||||
static inline BOOL TryAcquireSRWLockShared(SRWLOCK *l) { return pthread_rwlock_tryrdlock(l) == 0; }
|
||||
void InitializeCriticalSection(CRITICAL_SECTION *cs);
|
||||
static inline BOOL InitializeCriticalSectionAndSpinCount(CRITICAL_SECTION *cs, DWORD n) { (void)n; InitializeCriticalSection(cs); return TRUE; }
|
||||
static inline void EnterCriticalSection(CRITICAL_SECTION *cs) { pthread_mutex_lock(cs); }
|
||||
static inline void LeaveCriticalSection(CRITICAL_SECTION *cs) { pthread_mutex_unlock(cs); }
|
||||
static inline BOOL TryEnterCriticalSection(CRITICAL_SECTION *cs) { return pthread_mutex_trylock(cs) == 0; }
|
||||
static inline void DeleteCriticalSection(CRITICAL_SECTION *cs) { pthread_mutex_destroy(cs); }
|
||||
static inline void InitializeConditionVariable(CONDITION_VARIABLE *cv) { cv->seq = 0; }
|
||||
void WakeConditionVariable(CONDITION_VARIABLE *cv);
|
||||
void WakeAllConditionVariable(CONDITION_VARIABLE *cv);
|
||||
BOOL SleepConditionVariableSRW(CONDITION_VARIABLE *cv, SRWLOCK *l, DWORD ms, ULONG flags);
|
||||
BOOL SleepConditionVariableCS(CONDITION_VARIABLE *cv, CRITICAL_SECTION *cs, DWORD ms);
|
||||
BOOL WaitOnAddress(volatile void *addr, void *compare, SIZE_T size, DWORD ms);
|
||||
void WakeByAddressSingle(void *addr);
|
||||
void WakeByAddressAll(void *addr);
|
||||
|
||||
/* ---- time ---- */
|
||||
static inline BOOL QueryPerformanceFrequency(LARGE_INTEGER *f) { f->QuadPart = 1000000000LL; return TRUE; }
|
||||
static inline BOOL QueryPerformanceCounter(LARGE_INTEGER *c) {
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
c->QuadPart = (int64_t)ts.tv_sec * 1000000000LL + ts.tv_nsec;
|
||||
return TRUE;
|
||||
}
|
||||
static inline ULONGLONG GetTickCount64(void) {
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
return (ULONGLONG)ts.tv_sec * 1000 + (ULONGLONG)ts.tv_nsec / 1000000;
|
||||
}
|
||||
static inline DWORD GetTickCount(void) { return (DWORD)GetTickCount64(); }
|
||||
void Sleep(DWORD ms);
|
||||
static inline BOOL SwitchToThread(void) { return sched_yield() == 0; }
|
||||
static inline UINT timeBeginPeriod(UINT p) { (void)p; return 0; }
|
||||
static inline UINT timeEndPeriod(UINT p) { (void)p; return 0; }
|
||||
void GetSystemTimeAsFileTime(FILETIME *ft);
|
||||
#define CREATE_WAITABLE_TIMER_HIGH_RESOLUTION 0x2
|
||||
#define TIMER_ALL_ACCESS 0x1F0003
|
||||
HANDLE CreateWaitableTimerExW(void *attr, LPCWSTR name, DWORD flags, DWORD access);
|
||||
BOOL SetWaitableTimerEx(HANDLE t, const LARGE_INTEGER *due, LONG period, void *fn, void *arg, void *ctx, ULONG delay);
|
||||
|
||||
/* ---- handles, threads, events ---- */
|
||||
typedef DWORD (*LPTHREAD_START_ROUTINE)(void *);
|
||||
#define WAIT_OBJECT_0 0u
|
||||
#define WAIT_ABANDONED 0x80u
|
||||
#define WAIT_TIMEOUT 258u
|
||||
#define WAIT_FAILED 0xFFFFFFFFu
|
||||
#define STILL_ACTIVE 259
|
||||
#define CREATE_SUSPENDED 0x4
|
||||
#define STACK_SIZE_PARAM_IS_A_RESERVATION 0x10000
|
||||
#define THREAD_PRIORITY_IDLE -15
|
||||
#define THREAD_PRIORITY_LOWEST -2
|
||||
#define THREAD_PRIORITY_BELOW_NORMAL -1
|
||||
#define THREAD_PRIORITY_NORMAL 0
|
||||
#define THREAD_PRIORITY_ABOVE_NORMAL 1
|
||||
#define THREAD_PRIORITY_HIGHEST 2
|
||||
#define THREAD_PRIORITY_TIME_CRITICAL 15
|
||||
#define DUPLICATE_SAME_ACCESS 0x2
|
||||
#define THREAD_ALL_ACCESS 0x1FFFFF
|
||||
#define THREAD_QUERY_INFORMATION 0x40
|
||||
#define THREAD_QUERY_LIMITED_INFORMATION 0x800
|
||||
#define THREAD_SUSPEND_RESUME 0x2
|
||||
#define THREAD_GET_CONTEXT 0x8
|
||||
#define THREAD_SET_CONTEXT 0x10
|
||||
#define SYNCHRONIZE 0x100000
|
||||
#define EVENT_ALL_ACCESS 0x1F0003
|
||||
HANDLE CreateThread(void *attr, SIZE_T stack, LPTHREAD_START_ROUTINE fn, void *arg, DWORD flags, DWORD *id);
|
||||
HANDLE GetCurrentThread(void);
|
||||
static inline HANDLE GetCurrentProcess(void) { return (HANDLE)(intptr_t)-1; }
|
||||
static inline DWORD GetCurrentProcessId(void) { return (DWORD)getpid(); }
|
||||
DWORD GetCurrentThreadId(void);
|
||||
DWORD GetThreadId(HANDLE t);
|
||||
DWORD GetProcessId(HANDLE p);
|
||||
HANDLE OpenThread(DWORD access, BOOL inherit, DWORD id);
|
||||
BOOL DuplicateHandle(HANDLE sp, HANDLE src, HANDLE tp, HANDLE *out, DWORD access, BOOL inherit, DWORD options);
|
||||
BOOL CloseHandle(HANDLE h);
|
||||
BOOL SetThreadPriority(HANDLE t, int p);
|
||||
int GetThreadPriority(HANDLE t);
|
||||
DWORD_PTR SetThreadAffinityMask(HANDLE t, DWORD_PTR mask);
|
||||
BOOL GetProcessAffinityMask(HANDLE p, DWORD_PTR *proc, DWORD_PTR *sys);
|
||||
HRESULT SetThreadDescription(HANDLE t, PCWSTR name);
|
||||
HRESULT GetThreadDescription(HANDLE t, PWSTR *name);
|
||||
BOOL GetThreadTimes(HANDLE t, FILETIME *create, FILETIME *exit, FILETIME *kernel, FILETIME *user);
|
||||
DWORD SuspendThread(HANDLE t);
|
||||
DWORD ResumeThread(HANDLE t);
|
||||
BOOL GetExitCodeThread(HANDLE t, DWORD *code);
|
||||
__attribute__((noreturn)) void ExitThread(DWORD code);
|
||||
__attribute__((noreturn)) void ExitProcess(UINT code);
|
||||
BOOL TerminateProcess(HANDLE p, UINT code);
|
||||
HANDLE CreateEventA(void *attr, BOOL manual, BOOL initial, LPCSTR name);
|
||||
#define CreateEvent CreateEventA
|
||||
BOOL SetEvent(HANDLE e);
|
||||
BOOL ResetEvent(HANDLE e);
|
||||
HANDLE CreateSemaphoreA(void *attr, LONG initial, LONG max, LPCSTR name);
|
||||
BOOL ReleaseSemaphore(HANDLE s, LONG n, LONG *prev);
|
||||
DWORD WaitForSingleObject(HANDLE h, DWORD ms);
|
||||
DWORD WaitForMultipleObjects(DWORD n, const HANDLE *hs, BOOL all, DWORD ms);
|
||||
void *LocalFree(void *p);
|
||||
|
||||
/* fibers: ucontext, each on its own stack */
|
||||
#define FIBER_FLAG_FLOAT_SWITCH 0x1
|
||||
typedef void (*LPFIBER_START_ROUTINE)(void *);
|
||||
void *ConvertThreadToFiberEx(void *param, DWORD flags);
|
||||
void *ConvertThreadToFiber(void *param);
|
||||
BOOL ConvertFiberToThread(void);
|
||||
void *CreateFiberEx(SIZE_T commit, SIZE_T reserve, DWORD flags, LPFIBER_START_ROUTINE fn, void *param);
|
||||
void *CreateFiber(SIZE_T stack, LPFIBER_START_ROUTINE fn, void *param);
|
||||
void SwitchToFiber(void *fiber);
|
||||
void DeleteFiber(void *fiber);
|
||||
void *GetCurrentFiber(void);
|
||||
void *GetFiberData(void);
|
||||
|
||||
/* processors */
|
||||
typedef enum { RelationProcessorCore = 0, RelationNumaNode = 1, RelationCache = 2, RelationProcessorPackage = 3 } LOGICAL_PROCESSOR_RELATIONSHIP;
|
||||
typedef struct {
|
||||
ULONG_PTR ProcessorMask;
|
||||
LOGICAL_PROCESSOR_RELATIONSHIP Relationship;
|
||||
union { struct { BYTE Flags; } ProcessorCore; ULONGLONG Reserved[2]; };
|
||||
} SYSTEM_LOGICAL_PROCESSOR_INFORMATION, *PSYSTEM_LOGICAL_PROCESSOR_INFORMATION;
|
||||
BOOL GetLogicalProcessorInformation(SYSTEM_LOGICAL_PROCESSOR_INFORMATION *info, DWORD *len);
|
||||
typedef struct {
|
||||
WORD wProcessorArchitecture, wReserved;
|
||||
DWORD dwPageSize;
|
||||
void *lpMinimumApplicationAddress, *lpMaximumApplicationAddress;
|
||||
DWORD_PTR dwActiveProcessorMask;
|
||||
DWORD dwNumberOfProcessors, dwProcessorType, dwAllocationGranularity;
|
||||
WORD wProcessorLevel, wProcessorRevision;
|
||||
} SYSTEM_INFO;
|
||||
void GetSystemInfo(SYSTEM_INFO *si);
|
||||
|
||||
/* ---- strings ---- */
|
||||
#define CP_ACP 0
|
||||
#define CP_UTF8 65001
|
||||
int MultiByteToWideChar(UINT cp, DWORD flags, LPCSTR s, int n, LPWSTR w, int cap);
|
||||
int WideCharToMultiByte(UINT cp, DWORD flags, LPCWSTR w, int n, LPSTR s, int cap, LPCSTR def, BOOL *used);
|
||||
void OutputDebugStringA(LPCSTR s);
|
||||
#define OutputDebugString OutputDebugStringA
|
||||
|
||||
/* ---- memory ---- */
|
||||
#define MEM_COMMIT 0x1000
|
||||
#define MEM_RESERVE 0x2000
|
||||
#define MEM_DECOMMIT 0x4000
|
||||
#define MEM_RELEASE 0x8000
|
||||
#define MEM_FREE 0x10000
|
||||
#define MEM_PRIVATE 0x20000
|
||||
#define MEM_MAPPED 0x40000
|
||||
#define MEM_IMAGE 0x1000000
|
||||
#define MEM_RESET 0x80000
|
||||
#define MEM_WRITE_WATCH 0x200000
|
||||
#define MEM_LARGE_PAGES 0x20000000
|
||||
#define PAGE_NOACCESS 0x01
|
||||
#define PAGE_READONLY 0x02
|
||||
#define PAGE_READWRITE 0x04
|
||||
#define PAGE_EXECUTE 0x10
|
||||
#define PAGE_EXECUTE_READ 0x20
|
||||
#define PAGE_EXECUTE_READWRITE 0x40
|
||||
#define PAGE_WRITECOPY 0x08
|
||||
#define PAGE_GUARD 0x100
|
||||
typedef struct {
|
||||
void *BaseAddress, *AllocationBase;
|
||||
DWORD AllocationProtect;
|
||||
SIZE_T RegionSize;
|
||||
DWORD State, Protect, Type;
|
||||
} MEMORY_BASIC_INFORMATION, *PMEMORY_BASIC_INFORMATION;
|
||||
void *VirtualAlloc(void *addr, SIZE_T size, DWORD type, DWORD protect);
|
||||
BOOL VirtualFree(void *addr, SIZE_T size, DWORD type);
|
||||
BOOL VirtualProtect(void *addr, SIZE_T size, DWORD protect, DWORD *old);
|
||||
SIZE_T VirtualQuery(const void *addr, MEMORY_BASIC_INFORMATION *mbi, SIZE_T size);
|
||||
static inline SIZE_T GetLargePageMinimum(void) { return 0; }
|
||||
|
||||
/* ---- modules: dlopen; ".dll" names map to the shared libraries (win32_posix.c) ---- */
|
||||
#define GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT 0x2
|
||||
#define GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS 0x4
|
||||
HMODULE LoadLibraryA(LPCSTR name);
|
||||
#define LoadLibrary LoadLibraryA
|
||||
BOOL FreeLibrary(HMODULE m);
|
||||
FARPROC GetProcAddress(HMODULE m, LPCSTR name);
|
||||
HMODULE GetModuleHandleA(LPCSTR name);
|
||||
BOOL GetModuleHandleExA(DWORD flags, LPCSTR name, HMODULE *out);
|
||||
DWORD GetModuleFileNameA(HMODULE m, LPSTR out, DWORD cap);
|
||||
BOOL SetCurrentDirectoryA(LPCSTR dir);
|
||||
DWORD GetCurrentDirectoryA(DWORD cap, LPSTR out);
|
||||
|
||||
/* ---- files: paths may use '\' (it is made '/') ---- */
|
||||
#define GENERIC_READ 0x80000000u
|
||||
#define GENERIC_WRITE 0x40000000u
|
||||
#define FILE_SHARE_READ 0x1
|
||||
#define FILE_SHARE_WRITE 0x2
|
||||
#define FILE_SHARE_DELETE 0x4
|
||||
#define CREATE_NEW 1
|
||||
#define CREATE_ALWAYS 2
|
||||
#define OPEN_EXISTING 3
|
||||
#define OPEN_ALWAYS 4
|
||||
#define TRUNCATE_EXISTING 5
|
||||
#define FILE_ATTRIBUTE_READONLY 0x1
|
||||
#define FILE_ATTRIBUTE_HIDDEN 0x2
|
||||
#define FILE_ATTRIBUTE_DIRECTORY 0x10
|
||||
#define FILE_ATTRIBUTE_NORMAL 0x80
|
||||
#define FILE_ATTRIBUTE_REPARSE_POINT 0x400 /* (a symbolic link: find_next sets it) */
|
||||
#define FILE_FLAG_SEQUENTIAL_SCAN 0x08000000
|
||||
#define FILE_FLAG_RANDOM_ACCESS 0x10000000
|
||||
#define FILE_FLAG_OVERLAPPED 0x40000000
|
||||
#define FILE_FLAG_NO_BUFFERING 0x20000000
|
||||
#define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
|
||||
#define INVALID_FILE_SIZE ((DWORD)0xFFFFFFFF)
|
||||
#define FILE_BEGIN 0
|
||||
#define FILE_CURRENT 1
|
||||
#define FILE_END 2
|
||||
#define MOVEFILE_REPLACE_EXISTING 0x1
|
||||
#define MOVEFILE_COPY_ALLOWED 0x2
|
||||
#define MOVEFILE_WRITE_THROUGH 0x8
|
||||
#define STD_INPUT_HANDLE ((DWORD)-10)
|
||||
#define STD_OUTPUT_HANDLE ((DWORD)-11)
|
||||
#define STD_ERROR_HANDLE ((DWORD)-12)
|
||||
#define FILE_TYPE_UNKNOWN 0
|
||||
#define FILE_TYPE_DISK 1
|
||||
#define FILE_TYPE_CHAR 2
|
||||
#define FILE_TYPE_PIPE 3
|
||||
typedef struct {
|
||||
ULONG_PTR Internal, InternalHigh;
|
||||
union { struct { DWORD Offset, OffsetHigh; }; PVOID Pointer; };
|
||||
HANDLE hEvent;
|
||||
} OVERLAPPED, *LPOVERLAPPED;
|
||||
typedef struct {
|
||||
DWORD dwFileAttributes;
|
||||
FILETIME ftCreationTime, ftLastAccessTime, ftLastWriteTime;
|
||||
DWORD nFileSizeHigh, nFileSizeLow, dwReserved0, dwReserved1;
|
||||
CHAR cFileName[MAX_PATH];
|
||||
CHAR cAlternateFileName[14];
|
||||
} WIN32_FIND_DATAA, *LPWIN32_FIND_DATAA;
|
||||
HANDLE CreateFileA(LPCSTR path, DWORD access, DWORD share, void *attr, DWORD disposition, DWORD flags, HANDLE tmpl);
|
||||
#define CreateFile CreateFileA
|
||||
BOOL ReadFile(HANDLE h, void *buf, DWORD n, DWORD *got, OVERLAPPED *ov);
|
||||
BOOL WriteFile(HANDLE h, const void *buf, DWORD n, DWORD *put, OVERLAPPED *ov);
|
||||
BOOL SetFilePointerEx(HANDLE h, LARGE_INTEGER dist, LARGE_INTEGER *pos, DWORD method);
|
||||
BOOL GetFileSizeEx(HANDLE h, LARGE_INTEGER *size);
|
||||
DWORD GetFileSize(HANDLE h, DWORD *high);
|
||||
BOOL SetEndOfFile(HANDLE h);
|
||||
BOOL FlushFileBuffers(HANDLE h);
|
||||
DWORD GetFileType(HANDLE h);
|
||||
HANDLE GetStdHandle(DWORD which);
|
||||
BOOL GetConsoleMode(HANDLE h, DWORD *mode);
|
||||
DWORD GetFileAttributesA(LPCSTR path);
|
||||
#define GetFileAttributes GetFileAttributesA
|
||||
BOOL CreateDirectoryA(LPCSTR path, void *attr);
|
||||
BOOL RemoveDirectoryA(LPCSTR path);
|
||||
BOOL DeleteFileA(LPCSTR path);
|
||||
BOOL MoveFileA(LPCSTR from, LPCSTR to);
|
||||
BOOL MoveFileExA(LPCSTR from, LPCSTR to, DWORD flags);
|
||||
BOOL CopyFileA(LPCSTR from, LPCSTR to, BOOL fail_if_exists);
|
||||
HANDLE FindFirstFileA(LPCSTR pattern, WIN32_FIND_DATAA *fd);
|
||||
BOOL FindNextFileA(HANDLE h, WIN32_FIND_DATAA *fd);
|
||||
BOOL FindClose(HANDLE h);
|
||||
static inline BOOL AllocConsole(void) { return FALSE; }
|
||||
static inline BOOL FreeConsole(void) { return TRUE; }
|
||||
static inline BOOL SetConsoleTitleA(LPCSTR t) { (void)t; return TRUE; }
|
||||
|
||||
/* ---- exceptions: vectored handlers and the unhandled filter on SIGSEGV / SIGBUS / SIGILL / SIGFPE ---- */
|
||||
#define EXCEPTION_ACCESS_VIOLATION 0xC0000005u
|
||||
#define EXCEPTION_ILLEGAL_INSTRUCTION 0xC000001Du
|
||||
#define EXCEPTION_INT_DIVIDE_BY_ZERO 0xC0000094u
|
||||
#define EXCEPTION_STACK_OVERFLOW 0xC00000FDu
|
||||
#define EXCEPTION_BREAKPOINT 0x80000003u
|
||||
#define EXCEPTION_IN_PAGE_ERROR 0xC0000006u
|
||||
#define EXCEPTION_CONTINUE_EXECUTION (-1)
|
||||
#define EXCEPTION_CONTINUE_SEARCH 0
|
||||
#define EXCEPTION_EXECUTE_HANDLER 1
|
||||
#define EXCEPTION_MAXIMUM_PARAMETERS 15
|
||||
typedef struct _EXCEPTION_RECORD {
|
||||
DWORD ExceptionCode, ExceptionFlags;
|
||||
struct _EXCEPTION_RECORD *ExceptionRecord;
|
||||
void *ExceptionAddress;
|
||||
DWORD NumberParameters;
|
||||
ULONG_PTR ExceptionInformation[EXCEPTION_MAXIMUM_PARAMETERS];
|
||||
} EXCEPTION_RECORD, *PEXCEPTION_RECORD;
|
||||
typedef struct {
|
||||
DWORD64 Rax, Rcx, Rdx, Rbx, Rsp, Rbp, Rsi, Rdi, R8, R9, R10, R11, R12, R13, R14, R15, Rip;
|
||||
DWORD EFlags, ContextFlags;
|
||||
DWORD64 Dr0, Dr1, Dr2, Dr3, Dr6, Dr7; /* (no thread's context is read or set here: GetThreadContext fails) */
|
||||
} CONTEXT, *PCONTEXT;
|
||||
#define EXCEPTION_SINGLE_STEP 0x80000004u
|
||||
#define CONTEXT_DEBUG_REGISTERS 0x100010
|
||||
#define CONTEXT_FULL 0x10000B
|
||||
#define CONTEXT_CONTROL 0x100001
|
||||
#define CONTEXT_INTEGER 0x100002
|
||||
typedef struct { EXCEPTION_RECORD *ExceptionRecord; CONTEXT *ContextRecord; } EXCEPTION_POINTERS, *PEXCEPTION_POINTERS;
|
||||
typedef LONG (*PVECTORED_EXCEPTION_HANDLER)(EXCEPTION_POINTERS *);
|
||||
typedef LONG (*LPTOP_LEVEL_EXCEPTION_FILTER)(EXCEPTION_POINTERS *);
|
||||
void *AddVectoredExceptionHandler(ULONG first, PVECTORED_EXCEPTION_HANDLER h);
|
||||
ULONG RemoveVectoredExceptionHandler(void *h);
|
||||
LPTOP_LEVEL_EXCEPTION_FILTER SetUnhandledExceptionFilter(LPTOP_LEVEL_EXCEPTION_FILTER f);
|
||||
BOOL GetThreadContext(HANDLE t, CONTEXT *c);
|
||||
BOOL SetThreadContext(HANDLE t, const CONTEXT *c);
|
||||
/* (no SEH unwind tables: the host backtraces of crash reports stop at the faulting frame) */
|
||||
typedef struct { DWORD BeginAddress, EndAddress, UnwindData; } RUNTIME_FUNCTION, *PRUNTIME_FUNCTION;
|
||||
#define UNW_FLAG_NHANDLER 0
|
||||
static inline PRUNTIME_FUNCTION RtlLookupFunctionEntry(DWORD64 pc, DWORD64 *base, void *table) { (void)pc, (void)table; *base = 0; return NULL; }
|
||||
static inline void *RtlVirtualUnwind(DWORD type, DWORD64 base, DWORD64 pc, PRUNTIME_FUNCTION f, CONTEXT *c, void **hd,
|
||||
DWORD64 *frame, void *ptrs) {
|
||||
(void)type, (void)base, (void)pc, (void)f, (void)c, (void)hd, (void)frame, (void)ptrs;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* ---- the window, keyboard and screen (display_sdl.c) ---- */
|
||||
#define VK_LBUTTON 0x01
|
||||
#define VK_RBUTTON 0x02
|
||||
#define VK_MBUTTON 0x04
|
||||
#define VK_BACK 0x08
|
||||
#define VK_TAB 0x09
|
||||
#define VK_RETURN 0x0D
|
||||
#define VK_SHIFT 0x10
|
||||
#define VK_CONTROL 0x11
|
||||
#define VK_MENU 0x12
|
||||
#define VK_PAUSE 0x13
|
||||
#define VK_CAPITAL 0x14
|
||||
#define VK_ESCAPE 0x1B
|
||||
#define VK_SPACE 0x20
|
||||
#define VK_PRIOR 0x21
|
||||
#define VK_NEXT 0x22
|
||||
#define VK_END 0x23
|
||||
#define VK_HOME 0x24
|
||||
#define VK_LEFT 0x25
|
||||
#define VK_UP 0x26
|
||||
#define VK_RIGHT 0x27
|
||||
#define VK_DOWN 0x28
|
||||
#define VK_INSERT 0x2D
|
||||
#define VK_DELETE 0x2E
|
||||
#define VK_NUMPAD0 0x60
|
||||
#define VK_MULTIPLY 0x6A
|
||||
#define VK_ADD 0x6B
|
||||
#define VK_SUBTRACT 0x6D
|
||||
#define VK_DECIMAL 0x6E
|
||||
#define VK_DIVIDE 0x6F
|
||||
#define VK_F1 0x70
|
||||
#define VK_F2 0x71
|
||||
#define VK_F3 0x72
|
||||
#define VK_F4 0x73
|
||||
#define VK_F5 0x74
|
||||
#define VK_F6 0x75
|
||||
#define VK_F7 0x76
|
||||
#define VK_F8 0x77
|
||||
#define VK_F9 0x78
|
||||
#define VK_F10 0x79
|
||||
#define VK_F11 0x7A
|
||||
#define VK_F12 0x7B
|
||||
#define VK_LSHIFT 0xA0
|
||||
#define VK_RSHIFT 0xA1
|
||||
#define VK_LCONTROL 0xA2
|
||||
#define VK_RCONTROL 0xA3
|
||||
#define VK_LMENU 0xA4
|
||||
#define VK_RMENU 0xA5
|
||||
#define VK_OEM_1 0xBA
|
||||
#define VK_OEM_PLUS 0xBB
|
||||
#define VK_OEM_COMMA 0xBC
|
||||
#define VK_OEM_MINUS 0xBD
|
||||
#define VK_OEM_PERIOD 0xBE
|
||||
#define VK_OEM_2 0xBF
|
||||
#define VK_OEM_3 0xC0
|
||||
#define VK_OEM_4 0xDB
|
||||
#define VK_OEM_5 0xDC
|
||||
#define VK_OEM_6 0xDD
|
||||
#define VK_OEM_7 0xDE
|
||||
/* the window's key state: 0x8000 while down (display_sdl.c fills it; the Vulkan back end reads the executable's) */
|
||||
SHORT GetAsyncKeyState(int vk);
|
||||
SHORT GetKeyState(int vk);
|
||||
#define SM_CXSCREEN 0
|
||||
#define SM_CYSCREEN 1
|
||||
int GetSystemMetrics(int index);
|
||||
HWND GetForegroundWindow(void);
|
||||
#define GWL_STYLE (-16)
|
||||
#define GWL_EXSTYLE (-20)
|
||||
#define WS_EX_TOOLWINDOW 0x80
|
||||
#define WS_EX_NOACTIVATE 0x08000000
|
||||
static inline LONG_PTR GetWindowLongPtrA(HWND w, int i) { (void)w, (void)i; return 0; }
|
||||
/* (the Insert menu's Fullscreen row, gpu/vk/vk_menu.cpp: WM_APP + 7 to the window sets it, GWL_STYLE without WS_CAPTION reads it) */
|
||||
#define WM_CLOSE 0x0010
|
||||
#define WM_APP 0x8000
|
||||
#define WS_CAPTION 0x00C00000L
|
||||
BOOL PostMessageA(HWND w, UINT msg, WPARAM wp, LPARAM lp);
|
||||
LONG GetWindowLongA(HWND w, int index);
|
||||
static inline int GetClassNameA(HWND w, LPSTR s, int n) { (void)w; if (n > 0) s[0] = 0; return 0; }
|
||||
DWORD GetWindowThreadProcessId(HWND w, DWORD *pid);
|
||||
int MessageBoxA(HWND w, LPCSTR text, LPCSTR caption, UINT type);
|
||||
#define MB_OK 0
|
||||
#define MB_ICONERROR 0x10
|
||||
#define MessageBox MessageBoxA
|
||||
#ifndef MB_TOPMOST
|
||||
#define MB_TOPMOST 0x00040000L
|
||||
#endif
|
||||
#ifndef MB_ICONWARNING
|
||||
#define MB_ICONWARNING 0x00000030L
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
29
host/posix/xinput.h
Normal file
29
host/posix/xinput.h
Normal file
@@ -0,0 +1,29 @@
|
||||
/* (linux) XInput is Windows' alone: no pad answers here, and every controller comes through SDL (host/pad_sdl.c). */
|
||||
#ifndef HOOKSHOT_POSIX_XINPUT_H
|
||||
#define HOOKSHOT_POSIX_XINPUT_H
|
||||
#include <windows.h>
|
||||
#define XUSER_MAX_COUNT 4
|
||||
#define XINPUT_GAMEPAD_DPAD_UP 0x0001
|
||||
#define XINPUT_GAMEPAD_DPAD_DOWN 0x0002
|
||||
#define XINPUT_GAMEPAD_DPAD_LEFT 0x0004
|
||||
#define XINPUT_GAMEPAD_DPAD_RIGHT 0x0008
|
||||
#define XINPUT_GAMEPAD_START 0x0010
|
||||
#define XINPUT_GAMEPAD_BACK 0x0020
|
||||
#define XINPUT_GAMEPAD_LEFT_THUMB 0x0040
|
||||
#define XINPUT_GAMEPAD_RIGHT_THUMB 0x0080
|
||||
#define XINPUT_GAMEPAD_LEFT_SHOULDER 0x0100
|
||||
#define XINPUT_GAMEPAD_RIGHT_SHOULDER 0x0200
|
||||
#define XINPUT_GAMEPAD_A 0x1000
|
||||
#define XINPUT_GAMEPAD_B 0x2000
|
||||
#define XINPUT_GAMEPAD_X 0x4000
|
||||
#define XINPUT_GAMEPAD_Y 0x8000
|
||||
typedef struct {
|
||||
WORD wButtons;
|
||||
BYTE bLeftTrigger, bRightTrigger;
|
||||
SHORT sThumbLX, sThumbLY, sThumbRX, sThumbRY;
|
||||
} XINPUT_GAMEPAD;
|
||||
typedef struct { DWORD dwPacketNumber; XINPUT_GAMEPAD Gamepad; } XINPUT_STATE;
|
||||
typedef struct { WORD wLeftMotorSpeed, wRightMotorSpeed; } XINPUT_VIBRATION;
|
||||
static inline DWORD XInputGetState(DWORD user, XINPUT_STATE *s) { (void)user, (void)s; return ERROR_DEVICE_NOT_CONNECTED; }
|
||||
static inline DWORD XInputSetState(DWORD user, XINPUT_VIBRATION *v) { (void)user, (void)v; return ERROR_DEVICE_NOT_CONNECTED; }
|
||||
#endif
|
||||
Reference in New Issue
Block a user