2020-10-04 20:45:00 +00:00
|
|
|
#include <unistd.h>
|
2021-06-17 21:32:24 +00:00
|
|
|
#include <signal.h>
|
2022-03-25 03:03:59 +00:00
|
|
|
#include <errno.h>
|
2020-10-05 22:17:55 +00:00
|
|
|
|
2022-06-25 21:30:08 +00:00
|
|
|
#include <mods/compat/compat.h>
|
2022-07-13 20:46:33 +00:00
|
|
|
#include <mods/screenshot/screenshot.h>
|
2022-06-25 21:30:08 +00:00
|
|
|
#include <mods/init/init.h>
|
2024-02-12 05:44:38 +00:00
|
|
|
#include "compat-internal.h"
|
2021-09-12 03:18:12 +00:00
|
|
|
|
2022-03-25 03:03:59 +00:00
|
|
|
#include <libreborn/libreborn.h>
|
|
|
|
|
2020-09-25 16:43:53 +00:00
|
|
|
#include <SDL/SDL.h>
|
|
|
|
|
2022-07-08 04:25:01 +00:00
|
|
|
#ifndef MCPI_HEADLESS_MODE
|
2023-11-13 22:45:05 +00:00
|
|
|
#include <media-layer/core.h>
|
|
|
|
|
|
|
|
#include <mods/input/input.h>
|
|
|
|
#include <mods/sign/sign.h>
|
|
|
|
#include <mods/chat/chat.h>
|
|
|
|
#include <mods/home/home.h>
|
|
|
|
#endif
|
|
|
|
|
2021-06-28 20:00:52 +00:00
|
|
|
// Custom Title
|
|
|
|
HOOK(SDL_WM_SetCaption, void, (__attribute__((unused)) const char *title, const char *icon)) {
|
|
|
|
ensure_SDL_WM_SetCaption();
|
2024-01-07 08:23:43 +00:00
|
|
|
real_SDL_WM_SetCaption(MCPI_APP_TITLE, icon);
|
2021-06-28 20:00:52 +00:00
|
|
|
}
|
|
|
|
|
2021-06-19 23:07:09 +00:00
|
|
|
// Mouse Cursor Is Always Invisible In Vanilla MCPI
|
|
|
|
// Because In Vanilla MCPI, The GPU Overlay Covered The Normal Mouse Cursor
|
|
|
|
HOOK(SDL_ShowCursor, int, (int toggle)) {
|
|
|
|
ensure_SDL_ShowCursor();
|
2024-01-07 08:23:43 +00:00
|
|
|
return real_SDL_ShowCursor(toggle == SDL_QUERY ? SDL_QUERY : SDL_DISABLE);
|
2021-06-19 23:07:09 +00:00
|
|
|
}
|
|
|
|
|
2020-10-05 22:17:55 +00:00
|
|
|
// Intercept SDL Events
|
2020-09-25 16:43:53 +00:00
|
|
|
HOOK(SDL_PollEvent, int, (SDL_Event *event)) {
|
2021-06-17 21:32:24 +00:00
|
|
|
// In Server Mode, Exit Requests Are Handled In src/server/server.cpp
|
|
|
|
// Check If Exit Is Requested
|
2023-11-13 15:25:57 +00:00
|
|
|
#ifndef MCPI_SERVER_MODE
|
2021-06-17 21:32:24 +00:00
|
|
|
if (compat_check_exit_requested()) {
|
|
|
|
// Send SDL_QUIT
|
2021-07-05 01:23:12 +00:00
|
|
|
SDL_Event new_event;
|
|
|
|
new_event.type = SDL_QUIT;
|
|
|
|
SDL_PushEvent(&new_event);
|
2020-10-05 22:17:55 +00:00
|
|
|
}
|
2023-11-13 15:25:57 +00:00
|
|
|
#endif
|
2020-10-05 22:17:55 +00:00
|
|
|
|
2020-09-26 20:44:39 +00:00
|
|
|
// Poll Events
|
2020-09-25 16:43:53 +00:00
|
|
|
ensure_SDL_PollEvent();
|
2024-01-07 08:23:43 +00:00
|
|
|
int ret = real_SDL_PollEvent(event);
|
2020-09-26 20:44:39 +00:00
|
|
|
|
2020-10-05 22:17:55 +00:00
|
|
|
// Handle Events
|
|
|
|
if (ret == 1 && event != NULL) {
|
2020-09-26 20:44:39 +00:00
|
|
|
int handled = 0;
|
|
|
|
|
2023-11-13 22:45:05 +00:00
|
|
|
#ifndef MCPI_HEADLESS_MODE
|
2021-06-17 21:32:24 +00:00
|
|
|
switch (event->type) {
|
|
|
|
case SDL_KEYDOWN: {
|
|
|
|
// Handle Key Presses
|
|
|
|
if (event->key.keysym.sym == SDLK_F11) {
|
|
|
|
media_toggle_fullscreen();
|
|
|
|
handled = 1;
|
|
|
|
} else if (event->key.keysym.sym == SDLK_F2) {
|
2022-07-13 20:46:33 +00:00
|
|
|
screenshot_take(home_get());
|
2021-06-17 21:32:24 +00:00
|
|
|
handled = 1;
|
2021-06-29 02:59:24 +00:00
|
|
|
} else if (event->key.keysym.sym == SDLK_ESCAPE) {
|
|
|
|
// Treat Escape As Back Button Press (This Fixes Issues With Signs)
|
2021-07-04 23:02:45 +00:00
|
|
|
handled = input_back();
|
|
|
|
} else if (event->key.keysym.sym == SDLK_q) {
|
|
|
|
// Drop Item
|
|
|
|
input_drop((event->key.keysym.mod & KMOD_CTRL) != 0);
|
2021-06-29 02:59:24 +00:00
|
|
|
handled = 1;
|
2022-10-07 04:06:50 +00:00
|
|
|
} else if (event->key.keysym.sym == SDLK_WORLD_0) {
|
|
|
|
// Crafting
|
|
|
|
input_open_crafting();
|
|
|
|
handled = 1;
|
2021-02-16 22:08:43 +00:00
|
|
|
}
|
2021-06-17 21:32:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SDL_MOUSEBUTTONDOWN:
|
|
|
|
case SDL_MOUSEBUTTONUP: {
|
|
|
|
// Track Right-Click State
|
|
|
|
if (event->button.button == SDL_BUTTON_RIGHT) {
|
|
|
|
input_set_is_right_click(event->button.state != SDL_RELEASED);
|
|
|
|
} else if (event->button.button == SDL_BUTTON_LEFT) {
|
|
|
|
input_set_is_left_click(event->button.state != SDL_RELEASED);
|
|
|
|
}
|
|
|
|
break;
|
2020-10-04 00:30:15 +00:00
|
|
|
}
|
2021-06-17 21:32:24 +00:00
|
|
|
case SDL_USEREVENT: {
|
|
|
|
// SDL_UserEvent Is Never Used In MCPI, So It Is Repurposed For Character Events
|
2023-05-28 04:45:58 +00:00
|
|
|
if (event->user.code == USER_EVENT_CHARACTER) {
|
|
|
|
sign_key_press((char) event->user.data1);
|
|
|
|
handled = 1;
|
|
|
|
}
|
2021-06-17 21:32:24 +00:00
|
|
|
break;
|
2020-11-21 21:52:27 +00:00
|
|
|
}
|
2020-09-26 20:44:39 +00:00
|
|
|
}
|
2023-11-13 22:45:05 +00:00
|
|
|
#endif
|
2020-09-26 20:44:39 +00:00
|
|
|
|
|
|
|
if (handled) {
|
|
|
|
// Event Was Handled
|
|
|
|
return SDL_PollEvent(event);
|
|
|
|
}
|
2020-09-25 16:43:53 +00:00
|
|
|
}
|
2020-09-26 20:44:39 +00:00
|
|
|
|
2020-09-25 16:43:53 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-06-17 21:32:24 +00:00
|
|
|
// Exit Handler
|
|
|
|
static void exit_handler(__attribute__((unused)) int data) {
|
|
|
|
// Request Exit
|
|
|
|
compat_request_exit();
|
2020-10-05 22:17:55 +00:00
|
|
|
}
|
2021-06-17 21:32:24 +00:00
|
|
|
void init_compat() {
|
2022-05-14 02:36:12 +00:00
|
|
|
// Install Signal Handlers
|
2022-07-13 20:46:33 +00:00
|
|
|
signal(SIGINT, SIG_IGN);
|
2022-05-14 02:36:12 +00:00
|
|
|
struct sigaction act_sigterm;
|
|
|
|
memset((void *) &act_sigterm, 0, sizeof (struct sigaction));
|
|
|
|
act_sigterm.sa_flags = SA_RESTART;
|
|
|
|
act_sigterm.sa_handler = &exit_handler;
|
|
|
|
sigaction(SIGTERM, &act_sigterm, NULL);
|
2024-02-12 05:44:38 +00:00
|
|
|
// Patches
|
|
|
|
_patch_egl_calls();
|
|
|
|
_patch_x11_calls();
|
|
|
|
_patch_bcm_host_calls();
|
2020-10-06 15:08:10 +00:00
|
|
|
}
|
|
|
|
|
2021-06-17 21:32:24 +00:00
|
|
|
// Store Exit Requests
|
|
|
|
static int exit_requested = 0;
|
|
|
|
int compat_check_exit_requested() {
|
|
|
|
if (exit_requested) {
|
|
|
|
exit_requested = 0;
|
2020-10-30 22:25:08 +00:00
|
|
|
return 1;
|
2020-09-25 16:43:53 +00:00
|
|
|
} else {
|
2021-06-17 21:32:24 +00:00
|
|
|
return 0;
|
2020-09-25 16:43:53 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-17 21:32:24 +00:00
|
|
|
void compat_request_exit() {
|
|
|
|
// Request
|
|
|
|
exit_requested = 1;
|
2020-10-06 15:08:10 +00:00
|
|
|
}
|