cave-story-solaris/src/Backends/Platform/X11.cpp
John Lorentzson a4479f9c07 Reimplement Backend_Delay to fix threads freezing
So our workaround for making usleep thread-safe mostly worked in that
it fixed one of the things making it thread-unsafe, but it seems that
sigsuspend, which usleep uses, is also not thread-safe, since
sometimes one of our two threads would freeze, stuck inside
sigsuspend. So instead of usleep, we're using a trick that Stacken
member map suggested: using select. Calling select with no file
descriptors to work on will have it block on the kernel level until
its timeout is hit. This acts as a sleep. As it turns out, this seems
to completely solve our sleep-related problems, and massively reduces
the amount of audio buffer underruns as a nice bonus!
2025-04-26 13:54:12 +02:00

166 lines
4.6 KiB
C++

// Released under the MIT license. X11 backend by Duuqnd.
// See LICENSE.txt for details.
#include "../Misc.h"
#include "../Rendering.h"
#include "../../Attributes.h"
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <signal.h>
#include <cstring>
#include <sys/time.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
static bool keyboardState[BACKEND_KEYBOARD_TOTAL];
Display* xDisplay;
Visual* xVisual;
static XVisualInfo xvisinfo;
static unsigned long startTime;
static void nophandler(int signo) {}
bool Backend_Init(void (*drag_and_drop_callback)(const char *path), void (*window_focus_callback)(bool focus)) {
// we're ignoring the hell out of focus and drag & drop
xDisplay = XOpenDisplay(NULL);
XMatchVisualInfo(xDisplay, DefaultScreen(xDisplay), 24, TrueColor, &xvisinfo);
xVisual = xvisinfo.visual;
printf("Using X visual with ID 0x%lx\n", xVisual->visualid);
struct timeval time; // time.tv_usec is microseconds
gettimeofday(&time, NULL);
startTime = (time.tv_sec * 1000) + (time.tv_usec / 1000);
// Disable SIGALRM to compensate for thread-unsafe usleep in Solaris
sigset_t mask;
struct sigaction act;
struct sigaction oldact;
act.sa_handler = nophandler;
act.sa_flags = 0;
sigaction(SIGALRM, &act, &oldact);
sigemptyset(&mask);
sigaddset(&mask, SIGALRM);
sigprocmask(SIG_BLOCK, &mask, 0);
return true;
}
void Backend_Deinit(void) {
XCloseDisplay(xDisplay);
}
void Backend_PostWindowCreation(void) {}
bool Backend_GetPaths(std::string *module_path, std::string *data_path) {
return false; // I assume this means "do it yourself, lazy"
}
void Backend_HideMouse(void) {}
void Backend_SetWindowIcon(const unsigned char *rgb_pixels, size_t width, size_t height) {
// Oh we'll want this for sure!
}
void Backend_SetCursor(const unsigned char *rgb_pixels, size_t width, size_t height) {}
void Backend_EnableDragAndDrop(void) {}
#define DOKEY(keych, keymac) { if(keysym == keych) keyboardState[keymac] = press; }
static void DoKeys(KeySym keysym, bool press) {
// X keysyms are case sensitive. Isn't it great?!
DOKEY(XK_z, BACKEND_KEYBOARD_Z);
DOKEY(XK_x, BACKEND_KEYBOARD_X);
DOKEY(XK_Z, BACKEND_KEYBOARD_Z);
DOKEY(XK_X, BACKEND_KEYBOARD_X);
DOKEY(XK_a, BACKEND_KEYBOARD_A);
DOKEY(XK_s, BACKEND_KEYBOARD_S);
DOKEY(XK_A, BACKEND_KEYBOARD_A);
DOKEY(XK_S, BACKEND_KEYBOARD_S);
DOKEY(XK_q, BACKEND_KEYBOARD_Q);
DOKEY(XK_w, BACKEND_KEYBOARD_W);
DOKEY(XK_Q, BACKEND_KEYBOARD_Q);
DOKEY(XK_W, BACKEND_KEYBOARD_W);
DOKEY(XK_Escape, BACKEND_KEYBOARD_ESCAPE);
DOKEY(XK_F1, BACKEND_KEYBOARD_F1);
DOKEY(XK_F2, BACKEND_KEYBOARD_F2);
DOKEY(XK_Left, BACKEND_KEYBOARD_LEFT);
DOKEY(XK_Right, BACKEND_KEYBOARD_RIGHT);
DOKEY(XK_Up, BACKEND_KEYBOARD_UP);
DOKEY(XK_Down, BACKEND_KEYBOARD_DOWN);
}
bool Backend_SystemTask(bool active) {
if(!active) {
return false;
}
XEvent e;
while(XPending(xDisplay) > 0) {
XNextEvent(xDisplay, &e);
if(e.type == KeyPress) {
KeySym keysym = XLookupKeysym((XKeyEvent*)&e, 0);
if(keysym == XK_Insert) {
return false;
}
DoKeys(keysym, true);
}
if(e.type == KeyRelease) {
KeySym keysym = XLookupKeysym((XKeyEvent*)&e, 0);
DoKeys(keysym, false);
}
}
return true;
}
void Backend_GetKeyboardState(bool *out_keyboard_state) {
memcpy(out_keyboard_state, keyboardState, sizeof(keyboardState));
}
ATTRIBUTE_FORMAT_PRINTF(1, 2) void Backend_PrintError(const char *format, ...) {
va_list argumentList;
va_start(argumentList, format);
fputs("ERROR: ", stderr);
vfprintf(stderr, format, argumentList);
fputc('\n', stderr);
va_end(argumentList);
}
ATTRIBUTE_FORMAT_PRINTF(1, 2) void Backend_PrintInfo(const char *format, ...) {
va_list argumentList;
va_start(argumentList, format);
fputs("INFO: ", stdout);
vfprintf(stdout, format, argumentList);
fputc('\n', stdout);
va_end(argumentList);
}
void Backend_ShowMessageBox(const char* title, const char* message) {
printf("ShowMessageBox: %s - %s\n", title, message);
}
unsigned long Backend_GetTicks(void) {
struct timeval time; // time.tv_usec is microseconds
gettimeofday(&time, NULL);
unsigned long ticktime = ((time.tv_sec * 1000) + (time.tv_usec / 1000)) - startTime;
return ticktime;
}
void Backend_Delay(unsigned int ticks) {
struct timeval timeout;
timeout.tv_sec = ticks / 1000;
timeout.tv_usec = (ticks * 1000) % 1000000;
select(0, NULL, NULL, NULL, &timeout);
}