minecraft-pi-reborn/mods/src/compat/compat.c

155 lines
4.6 KiB
C
Raw Normal View History

2020-10-04 20:45:00 +00:00
#include <unistd.h>
2021-06-17 21:32:24 +00:00
#include <signal.h>
2020-10-05 22:17:55 +00:00
2021-09-12 03:18:12 +00:00
#include "compat.h"
#include "../init/init.h"
#ifndef MCPI_SERVER_MODE
2020-09-25 16:43:53 +00:00
#include <SDL/SDL.h>
2021-09-12 03:18:12 +00:00
#include <media-layer/core.h>
2021-01-27 21:26:19 +00:00
#include <libreborn/libreborn.h>
2020-09-25 16:43:53 +00:00
2020-12-02 23:18:49 +00:00
#include "../input/input.h"
2021-06-29 02:59:24 +00:00
#include "../sign/sign.h"
2021-02-16 17:26:40 +00:00
#include "../chat/chat.h"
2021-08-27 02:52:18 +00:00
#include "../home/home.h"
2020-09-26 20:44:39 +00:00
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();
2022-03-14 23:09:25 +00:00
(*real_SDL_WM_SetCaption)(GUI_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();
return (*real_SDL_ShowCursor)(toggle == SDL_QUERY ? SDL_QUERY : SDL_DISABLE);
}
2022-03-25 02:47:34 +00:00
// Hook SDL_Quit
HOOK(SDL_Quit, __attribute__((noreturn)) void, ()) {
// Cleanup Executable
{
const char *exe = getenv("MCPI_EXECUTABLE_PATH");
// Check If Executable Is Temporary
if (exe != NULL && starts_with(exe, "/tmp")) {
// Cleanup Temporary File
if (unlink(exe) != 0) {
ERR("Unable To Cleanup Temporary File: %s", strerror(errno));
}
}
}
// Call Original Method
ensure_SDL_Quit();
(*real_SDL_Quit)();
}
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
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
}
2020-09-26 20:44:39 +00:00
// Poll Events
2020-09-25 16:43:53 +00:00
ensure_SDL_PollEvent();
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;
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) {
2021-08-27 02:52:18 +00:00
media_take_screenshot(home_get());
2021-06-17 21:32:24 +00:00
handled = 1;
} else if (event->key.keysym.sym == SDLK_F1) {
input_hide_gui();
handled = 1;
} else if (event->key.keysym.sym == SDLK_F5) {
input_third_person();
handled = 1;
} else if (event->key.keysym.sym == SDLK_t) {
// Only When In-Game With No Other Chat Windows Open
if (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON && chat_get_counter() == 0) {
// Open Chat
chat_open();
}
// Mark Handled
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;
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
2021-06-29 02:59:24 +00:00
sign_key_press((char) event->user.code);
2021-06-17 21:32:24 +00:00
handled = 1;
break;
}
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;
}
2022-03-14 23:09:25 +00:00
#endif
2020-09-25 16:43:53 +00:00
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() {
// Install Exit Handler
signal(SIGINT, exit_handler);
signal(SIGTERM, exit_handler);
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
}