155 lines
4.6 KiB
C
Raw Normal View History

2020-10-04 16:45:00 -04:00
#include <unistd.h>
2021-06-17 17:32:24 -04:00
#include <signal.h>
2020-10-05 18:17:55 -04:00
2021-09-11 23:18:12 -04:00
#include "compat.h"
#include "../init/init.h"
#ifndef MCPI_SERVER_MODE
2020-09-25 12:43:53 -04:00
#include <SDL/SDL.h>
2021-09-11 23:18:12 -04:00
#include <media-layer/core.h>
2021-01-27 16:26:19 -05:00
#include <libreborn/libreborn.h>
2020-09-25 12:43:53 -04:00
2020-12-02 18:18:49 -05:00
#include "../input/input.h"
2021-06-28 22:59:24 -04:00
#include "../sign/sign.h"
2021-02-16 12:26:40 -05:00
#include "../chat/chat.h"
2021-08-26 22:52:18 -04:00
#include "../home/home.h"
2020-09-26 16:44:39 -04:00
2021-06-28 16:00:52 -04:00
// Custom Title
HOOK(SDL_WM_SetCaption, void, (__attribute__((unused)) const char *title, const char *icon)) {
ensure_SDL_WM_SetCaption();
2022-03-14 19:09:25 -04:00
(*real_SDL_WM_SetCaption)(GUI_TITLE, icon);
2021-06-28 16:00:52 -04:00
}
2021-06-19 19:07:09 -04: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-24 22:47:34 -04: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 18:17:55 -04:00
// Intercept SDL Events
2020-09-25 12:43:53 -04:00
HOOK(SDL_PollEvent, int, (SDL_Event *event)) {
2021-06-17 17:32:24 -04: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-04 21:23:12 -04:00
SDL_Event new_event;
new_event.type = SDL_QUIT;
SDL_PushEvent(&new_event);
2020-10-05 18:17:55 -04:00
}
2020-09-26 16:44:39 -04:00
// Poll Events
2020-09-25 12:43:53 -04:00
ensure_SDL_PollEvent();
int ret = (*real_SDL_PollEvent)(event);
2020-09-26 16:44:39 -04:00
2020-10-05 18:17:55 -04:00
// Handle Events
if (ret == 1 && event != NULL) {
2020-09-26 16:44:39 -04:00
int handled = 0;
2021-06-17 17:32:24 -04: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-26 22:52:18 -04:00
media_take_screenshot(home_get());
2021-06-17 17:32:24 -04: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-28 22:59:24 -04:00
} else if (event->key.keysym.sym == SDLK_ESCAPE) {
// Treat Escape As Back Button Press (This Fixes Issues With Signs)
2021-07-04 19:02:45 -04: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-28 22:59:24 -04:00
handled = 1;
2021-02-16 17:08:43 -05:00
}
2021-06-17 17:32:24 -04: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-03 20:30:15 -04:00
}
2021-06-17 17:32:24 -04:00
case SDL_USEREVENT: {
// SDL_UserEvent Is Never Used In MCPI, So It Is Repurposed For Character Events
2021-06-28 22:59:24 -04:00
sign_key_press((char) event->user.code);
2021-06-17 17:32:24 -04:00
handled = 1;
break;
}
2020-09-26 16:44:39 -04:00
}
if (handled) {
// Event Was Handled
return SDL_PollEvent(event);
}
2020-09-25 12:43:53 -04:00
}
2020-09-26 16:44:39 -04:00
2020-09-25 12:43:53 -04:00
return ret;
}
2022-03-14 19:09:25 -04:00
#endif
2020-09-25 12:43:53 -04:00
2021-06-17 17:32:24 -04:00
// Exit Handler
static void exit_handler(__attribute__((unused)) int data) {
// Request Exit
compat_request_exit();
2020-10-05 18:17:55 -04:00
}
2021-06-17 17:32:24 -04:00
void init_compat() {
// Install Exit Handler
signal(SIGINT, exit_handler);
signal(SIGTERM, exit_handler);
2020-10-06 11:08:10 -04:00
}
2021-06-17 17:32:24 -04:00
// Store Exit Requests
static int exit_requested = 0;
int compat_check_exit_requested() {
if (exit_requested) {
exit_requested = 0;
2020-10-30 18:25:08 -04:00
return 1;
2020-09-25 12:43:53 -04:00
} else {
2021-06-17 17:32:24 -04:00
return 0;
2020-09-25 12:43:53 -04:00
}
}
2021-06-17 17:32:24 -04:00
void compat_request_exit() {
// Request
exit_requested = 1;
2020-10-06 11:08:10 -04:00
}