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

431 lines
13 KiB
C
Raw Normal View History

2020-09-25 16:43:53 +00:00
#define _GNU_SOURCE
2020-10-04 20:45:00 +00:00
#include <unistd.h>
2020-10-05 22:17:55 +00:00
#define GLFW_EXPOSE_NATIVE_X11
#define GLFW_INCLUDE_ES1
#include <GLFW/glfw3.h>
#include <GLFW/glfw3native.h>
#include <X11/extensions/Xfixes.h>
2020-09-25 16:43:53 +00:00
#include <SDL/SDL.h>
#include <SDL/SDL_syswm.h>
#include <GLES/gl.h>
#include <X11/Xlib.h>
2021-02-21 19:53:17 +00:00
#include <EGL/egl.h>
2020-09-25 16:43:53 +00:00
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 "../feature/feature.h"
#include "../input/input.h"
#include "../screenshot/screenshot.h"
2021-02-16 17:26:40 +00:00
#include "../chat/chat.h"
2020-12-02 23:18:49 +00:00
#include "../init/init.h"
2020-10-04 00:30:15 +00:00
2020-10-05 22:17:55 +00:00
static GLFWwindow *glfw_window;
2020-09-25 16:43:53 +00:00
2020-10-10 23:02:13 +00:00
static int is_server = 0;
2020-10-05 22:17:55 +00:00
// Handle GLFW Error
static void glfw_error(__attribute__((unused)) int error, const char *description) {
2020-10-26 19:58:28 +00:00
ERR("GLFW Error: %s", description);
2020-10-05 22:17:55 +00:00
}
// Convert GLFW Key To SDL Key
static SDLKey glfw_key_to_sdl_key(int key) {
switch (key) {
// Movement
case GLFW_KEY_W:
return SDLK_w;
case GLFW_KEY_A:
return SDLK_a;
case GLFW_KEY_S:
return SDLK_s;
case GLFW_KEY_D:
return SDLK_d;
case GLFW_KEY_SPACE:
return SDLK_SPACE;
case GLFW_KEY_LEFT_SHIFT:
return SDLK_LSHIFT;
case GLFW_KEY_RIGHT_SHIFT:
return SDLK_RSHIFT;
// Inventory
case GLFW_KEY_E:
return SDLK_e;
// Hotbar
case GLFW_KEY_1:
return SDLK_1;
case GLFW_KEY_2:
return SDLK_2;
case GLFW_KEY_3:
return SDLK_3;
case GLFW_KEY_4:
return SDLK_4;
case GLFW_KEY_5:
return SDLK_5;
case GLFW_KEY_6:
return SDLK_6;
case GLFW_KEY_7:
return SDLK_7;
case GLFW_KEY_8:
return SDLK_8;
// UI Control
case GLFW_KEY_ESCAPE:
return SDLK_ESCAPE;
case GLFW_KEY_UP:
return SDLK_UP;
case GLFW_KEY_DOWN:
return SDLK_DOWN;
case GLFW_KEY_LEFT:
return SDLK_LEFT;
case GLFW_KEY_RIGHT:
return SDLK_RIGHT;
case GLFW_KEY_TAB:
return SDLK_TAB;
case GLFW_KEY_ENTER:
return SDLK_RETURN;
case GLFW_KEY_BACKSPACE:
return SDLK_BACKSPACE;
// Fullscreen
case GLFW_KEY_F11:
return SDLK_F11;
// Screenshot
case GLFW_KEY_F2:
return SDLK_F2;
// Hide GUI
case GLFW_KEY_F1:
return SDLK_F1;
// Third Person
case GLFW_KEY_F5:
return SDLK_F5;
2021-02-16 17:26:40 +00:00
// Chat
case GLFW_KEY_T:
return SDLK_t;
2020-10-05 22:17:55 +00:00
// Unknown
default:
return SDLK_UNKNOWN;
}
}
// Pass Key Presses To SDL
static void glfw_key(__attribute__((unused)) GLFWwindow *window, int key, int scancode, int action, __attribute__((unused)) int mods) {
SDL_Event event;
int up = action == GLFW_RELEASE;
event.type = up ? SDL_KEYUP : SDL_KEYDOWN;
event.key.state = up ? SDL_RELEASED : SDL_PRESSED;
event.key.keysym.scancode = scancode;
event.key.keysym.mod = KMOD_NONE;
event.key.keysym.sym = glfw_key_to_sdl_key(key);
SDL_PushEvent(&event);
if (key == GLFW_KEY_BACKSPACE && !up) {
2020-12-02 23:18:49 +00:00
input_key_press((char) '\b');
2020-10-05 22:17:55 +00:00
}
}
// Pass Text To Minecraft
static void glfw_char(__attribute__((unused)) GLFWwindow *window, unsigned int codepoint) {
2020-12-02 23:18:49 +00:00
input_key_press((char) codepoint);
2020-10-05 22:17:55 +00:00
}
2020-09-25 16:43:53 +00:00
2020-10-05 22:17:55 +00:00
static double last_mouse_x = 0;
static double last_mouse_y = 0;
2021-02-11 21:02:46 +00:00
static int ignore_relative_mouse = 1;
2020-09-26 20:44:39 +00:00
2020-10-05 22:17:55 +00:00
// Pass Mouse Movement To SDL
static void glfw_motion(__attribute__((unused)) GLFWwindow *window, double xpos, double ypos) {
SDL_Event event;
event.type = SDL_MOUSEMOTION;
event.motion.x = xpos;
event.motion.y = ypos;
2021-02-11 21:02:46 +00:00
event.motion.xrel = !ignore_relative_mouse ? (xpos - last_mouse_x) : 0;
event.motion.yrel = !ignore_relative_mouse ? (ypos - last_mouse_y) : 0;
ignore_relative_mouse = 0;
2020-10-05 22:17:55 +00:00
last_mouse_x = xpos;
last_mouse_y = ypos;
SDL_PushEvent(&event);
}
2020-10-04 20:45:00 +00:00
2020-10-06 13:35:47 +00:00
// Create And Push SDL Mouse Click Event
static void click(int button, int up) {
2020-10-05 22:17:55 +00:00
SDL_Event event;
event.type = up ? SDL_MOUSEBUTTONUP : SDL_MOUSEBUTTONDOWN;
event.button.x = last_mouse_x;
event.button.y = last_mouse_y;
event.button.state = up ? SDL_RELEASED : SDL_PRESSED;
2020-10-06 13:35:47 +00:00
event.button.button = button;
2020-10-05 22:17:55 +00:00
SDL_PushEvent(&event);
}
2020-10-04 00:30:15 +00:00
2020-10-06 13:35:47 +00:00
// Pass Mouse Click To SDL
static void glfw_click(__attribute__((unused)) GLFWwindow *window, int button, int action, __attribute__((unused)) int mods) {
int up = action == GLFW_RELEASE;
int sdl_button = button == GLFW_MOUSE_BUTTON_RIGHT ? SDL_BUTTON_RIGHT : (button == GLFW_MOUSE_BUTTON_LEFT ? SDL_BUTTON_LEFT : SDL_BUTTON_MIDDLE);
click(sdl_button, up);
}
// Pass Mouse Scroll To SDL
static void glfw_scroll(__attribute__((unused)) GLFWwindow *window, __attribute__((unused)) double xoffset, double yoffset) {
if (yoffset != 0) {
int sdl_button = yoffset > 0 ? SDL_BUTTON_WHEELUP : SDL_BUTTON_WHEELDOWN;
click(sdl_button, 0);
click(sdl_button, 1);
}
}
2020-10-30 22:44:47 +00:00
// Default Window Size
#define DEFAULT_WIDTH 840
#define DEFAULT_HEIGHT 480
2020-10-05 22:17:55 +00:00
// Init GLFW
HOOK(SDL_WM_SetCaption, void, (const char *title, __attribute__((unused)) const char *icon)) {
2020-10-30 22:25:08 +00:00
// Don't Enable GLFW In Server Mode
if (!is_server) {
glfwSetErrorCallback(glfw_error);
2020-09-25 16:43:53 +00:00
2020-10-30 22:25:08 +00:00
if (!glfwInit()) {
2020-11-10 20:06:29 +00:00
ERR("%s", "Unable To Initialize GLFW");
2020-10-30 22:25:08 +00:00
}
2020-09-25 16:43:53 +00:00
2020-10-10 23:02:13 +00:00
// Create OpenGL ES 1.1 Context
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 1);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
2021-02-16 17:26:40 +00:00
// Extra Settings
glfwWindowHint(GLFW_AUTO_ICONIFY, GLFW_FALSE);
2020-09-25 16:43:53 +00:00
2020-10-30 22:44:47 +00:00
glfw_window = glfwCreateWindow(DEFAULT_WIDTH, DEFAULT_HEIGHT, title, NULL, NULL);
2020-10-30 22:25:08 +00:00
if (!glfw_window) {
2020-11-10 20:06:29 +00:00
ERR("%s", "Unable To Create GLFW Window");
2020-10-30 22:25:08 +00:00
}
2020-09-25 16:43:53 +00:00
2020-10-10 23:02:13 +00:00
// Don't Process Events In Server Mode
glfwSetKeyCallback(glfw_window, glfw_key);
glfwSetCharCallback(glfw_window, glfw_char);
glfwSetCursorPosCallback(glfw_window, glfw_motion);
glfwSetMouseButtonCallback(glfw_window, glfw_click);
glfwSetScrollCallback(glfw_window, glfw_scroll);
2020-09-25 16:43:53 +00:00
2020-10-10 23:02:13 +00:00
glfwMakeContextCurrent(glfw_window);
}
2020-09-25 16:43:53 +00:00
}
2021-02-21 19:53:17 +00:00
HOOK(eglSwapBuffers, EGLBoolean, (__attribute__((unused)) EGLDisplay display, __attribute__((unused)) EGLSurface surface)) {
2020-10-10 23:02:13 +00:00
if (!is_server) {
// Don't Swap Buffers In A Context-Less Window
glfwSwapBuffers(glfw_window);
}
2021-02-21 19:53:17 +00:00
return EGL_TRUE;
2020-09-26 20:44:39 +00:00
}
static int is_fullscreen = 0;
2020-10-30 22:44:47 +00:00
// Old Size And Position To Use When Exiting Fullscreen
2020-10-05 22:17:55 +00:00
static int old_width = -1;
static int old_height = -1;
static int old_x = -1;
static int old_y = -1;
2020-09-26 20:44:39 +00:00
2020-10-05 22:17:55 +00:00
// Toggle Fullscreen
2020-09-26 20:44:39 +00:00
static void toggle_fullscreen() {
if (is_fullscreen) {
2020-10-05 22:17:55 +00:00
glfwSetWindowMonitor(glfw_window, NULL, old_x, old_y, old_width, old_height, GLFW_DONT_CARE);
2020-09-26 20:44:39 +00:00
2020-10-05 22:17:55 +00:00
old_width = -1;
old_height = -1;
old_x = -1;
old_y = -1;
2020-09-26 20:44:39 +00:00
} else {
2020-10-05 22:17:55 +00:00
glfwGetWindowSize(glfw_window, &old_width, &old_height);
glfwGetWindowPos(glfw_window, &old_x, &old_y);
2020-12-04 21:27:28 +00:00
Screen *screen = DefaultScreenOfDisplay(glfwGetX11Display());
2020-10-05 22:17:55 +00:00
glfwSetWindowMonitor(glfw_window, glfwGetPrimaryMonitor(), 0, 0, WidthOfScreen(screen), HeightOfScreen(screen), GLFW_DONT_CARE);
2020-09-26 20:44:39 +00:00
}
is_fullscreen = !is_fullscreen;
}
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)) {
2020-10-05 22:17:55 +00:00
// Process GLFW Events
glfwPollEvents();
2020-10-30 22:25:08 +00:00
// Close Window (Ignore In Server Mode)
if (!is_server && glfwWindowShouldClose(glfw_window)) {
2020-10-05 22:17:55 +00:00
SDL_Event event;
event.type = SDL_QUIT;
SDL_PushEvent(&event);
2020-10-14 17:40:32 +00:00
glfwSetWindowShouldClose(glfw_window, GLFW_FALSE);
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;
2020-10-05 22:17:55 +00:00
if (event->type == SDL_KEYDOWN) {
2020-10-04 00:30:15 +00:00
if (event->key.keysym.sym == SDLK_F11) {
toggle_fullscreen();
handled = 1;
2020-10-04 20:45:00 +00:00
} else if (event->key.keysym.sym == SDLK_F2) {
2020-11-24 22:12:23 +00:00
take_screenshot();
2020-10-04 20:45:00 +00:00
handled = 1;
} else if (event->key.keysym.sym == SDLK_F1) {
2020-12-02 23:18:49 +00:00
input_hide_gui();
handled = 1;
} else if (event->key.keysym.sym == SDLK_F5) {
2020-12-02 23:18:49 +00:00
input_third_person();
handled = 1;
2021-02-16 17:26:40 +00:00
} else if (event->key.keysym.sym == SDLK_t) {
2021-02-16 22:08:43 +00:00
// Only When In-Game With No Other Chat Windows Open
if (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON && chat_get_counter() == 0) {
// Release Mouse
input_set_mouse_grab_state(1);
// Open Chat
chat_open();
}
// Mark Handled
2021-02-16 17:26:40 +00:00
handled = 1;
2020-10-04 00:30:15 +00:00
}
} else if (event->type == SDL_MOUSEBUTTONDOWN || event->type == SDL_MOUSEBUTTONUP) {
if (event->button.button == SDL_BUTTON_RIGHT) {
2020-12-02 23:18:49 +00:00
input_set_is_right_click(event->button.state != SDL_RELEASED);
} else if (event->button.button == SDL_BUTTON_LEFT) {
2020-12-02 23:18:49 +00:00
input_set_is_left_click(event->button.state != SDL_RELEASED);
}
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;
}
2020-10-06 15:08:10 +00:00
// Terminate GLFW
2020-09-25 16:43:53 +00:00
HOOK(SDL_Quit, void, ()) {
ensure_SDL_Quit();
(*real_SDL_Quit)();
2020-10-30 22:25:08 +00:00
// GLFW Is Disabled In Server Mode
if (!is_server) {
glfwDestroyWindow(glfw_window);
glfwTerminate();
}
2020-10-05 22:17:55 +00:00
}
2020-10-10 23:02:13 +00:00
static SDL_GrabMode fake_grab_mode = SDL_GRAB_OFF;
2020-10-05 22:17:55 +00:00
// Fix SDL Cursor Visibility/Grabbing
HOOK(SDL_WM_GrabInput, SDL_GrabMode, (SDL_GrabMode mode)) {
2020-10-10 23:02:13 +00:00
if (is_server) {
2020-10-30 22:25:08 +00:00
// Don't Grab Input In Server Mode
2020-10-10 23:02:13 +00:00
if (mode != SDL_GRAB_QUERY) {
fake_grab_mode = mode;
}
return fake_grab_mode;
} else {
if (mode != SDL_GRAB_QUERY && mode != SDL_WM_GrabInput(SDL_GRAB_QUERY)) {
glfwSetInputMode(glfw_window, GLFW_CURSOR, mode == SDL_GRAB_OFF ? GLFW_CURSOR_NORMAL : GLFW_CURSOR_DISABLED);
glfwSetInputMode(glfw_window, GLFW_RAW_MOUSE_MOTION, mode == SDL_GRAB_OFF ? GLFW_FALSE : GLFW_TRUE);
// GLFW Cursor Hiding is Broken
2020-12-04 21:27:28 +00:00
Display *x11_display = glfwGetX11Display();
Window x11_window = glfwGetX11Window(glfw_window);
if (mode == SDL_GRAB_OFF) {
XFixesShowCursor(x11_display, x11_window);
} else {
XFixesHideCursor(x11_display, x11_window);
2020-10-05 22:17:55 +00:00
}
2020-12-04 21:27:28 +00:00
XFlush(x11_display);
2021-02-01 02:00:52 +00:00
2021-02-11 21:02:46 +00:00
// Reset Last Mouse Position
ignore_relative_mouse = 1;
2020-10-05 22:17:55 +00:00
}
2020-10-10 23:02:13 +00:00
return mode == SDL_GRAB_QUERY ? (glfwGetInputMode(glfw_window, GLFW_CURSOR) == GLFW_CURSOR_NORMAL ? SDL_GRAB_OFF : SDL_GRAB_ON) : mode;
2020-10-05 22:17:55 +00:00
}
}
// Stub SDL Cursor Visibility
HOOK(SDL_ShowCursor, int, (int toggle)) {
2020-10-10 23:02:13 +00:00
if (is_server) {
return toggle == SDL_QUERY ? (fake_grab_mode == SDL_GRAB_OFF ? SDL_ENABLE : SDL_DISABLE) : toggle;
} else {
return toggle == SDL_QUERY ? (glfwGetInputMode(glfw_window, GLFW_CURSOR) == GLFW_CURSOR_NORMAL ? SDL_ENABLE : SDL_DISABLE) : toggle;
}
2020-09-25 16:43:53 +00:00
}
2020-10-06 15:08:10 +00:00
// SDL Stub
HOOK(SDL_SetVideoMode, SDL_Surface *, (__attribute__((unused)) int width, __attribute__((unused)) int height, __attribute__((unused)) int bpp, __attribute__((unused)) uint32_t flags)) {
// Return Value Is Only Used For A NULL-Check
return (SDL_Surface *) 1;
}
2020-09-25 16:43:53 +00:00
HOOK(XTranslateCoordinates, int, (Display *display, Window src_w, Window dest_w, int src_x, int src_y, int *dest_x_return, int *dest_y_return, Window *child_return)) {
2020-10-30 22:25:08 +00:00
if (!is_server) {
2020-12-04 21:27:28 +00:00
// Use X11
2020-10-30 22:25:08 +00:00
ensure_XTranslateCoordinates();
2020-12-04 21:27:28 +00:00
return (*real_XTranslateCoordinates)(display, src_w, dest_w, src_x, src_y, dest_x_return, dest_y_return, child_return);
2020-09-25 16:43:53 +00:00
} else {
2020-10-30 22:25:08 +00:00
// No X11
*dest_x_return = src_x;
*dest_y_return = src_y;
return 1;
2020-09-25 16:43:53 +00:00
}
}
HOOK(XGetWindowAttributes, int, (Display *display, Window w, XWindowAttributes *window_attributes_return)) {
2020-10-30 22:25:08 +00:00
if (!is_server) {
2020-12-04 21:27:28 +00:00
// Use X11
2020-10-30 22:25:08 +00:00
ensure_XGetWindowAttributes();
2020-12-04 21:27:28 +00:00
return (*real_XGetWindowAttributes)(display, w, window_attributes_return);
2020-09-25 16:43:53 +00:00
} else {
2020-10-30 22:25:08 +00:00
// No X11
XWindowAttributes attributes;
attributes.x = 0;
attributes.y = 0;
2020-10-30 22:44:47 +00:00
attributes.width = DEFAULT_WIDTH;
attributes.height = DEFAULT_HEIGHT;
2020-10-30 22:25:08 +00:00
*window_attributes_return = attributes;
return 1;
2020-09-25 16:43:53 +00:00
}
}
2020-10-30 22:25:08 +00:00
static void x11_nop() {
// NOP
2020-10-06 15:08:10 +00:00
}
2020-10-30 22:25:08 +00:00
HOOK(SDL_GetWMInfo, int, (SDL_SysWMinfo *info)) {
2020-11-02 01:16:30 +00:00
// Return Fake Lock Functions In Server Mode Since SDL X11 Is Disabled
SDL_SysWMinfo ret;
ret.info.x11.lock_func = x11_nop;
ret.info.x11.unlock_func = x11_nop;
2020-12-04 21:27:28 +00:00
ret.info.x11.display = glfwGetX11Display();
ret.info.x11.window = glfwGetX11Window(glfw_window);
ret.info.x11.wmwindow = ret.info.x11.window;
2020-11-02 01:16:30 +00:00
*info = ret;
return 1;
2020-10-06 15:08:10 +00:00
}
2020-09-25 16:43:53 +00:00
#include <stdlib.h>
// Use VirGL
2020-12-02 23:18:49 +00:00
void init_compat() {
int mode = feature_get_mode();
2020-10-17 20:37:41 +00:00
if (mode != 1) {
2020-10-30 22:25:08 +00:00
// Force Software Rendering When Not In Native Mode
2020-10-16 19:39:04 +00:00
setenv("LIBGL_ALWAYS_SOFTWARE", "1", 1);
2020-10-17 20:37:41 +00:00
}
if (mode == 0) {
2020-10-30 22:25:08 +00:00
// Use VirGL When In VirGL Mode
2020-10-17 20:37:41 +00:00
setenv("GALLIUM_DRIVER", "virpipe", 1);
2020-10-10 23:02:13 +00:00
}
2020-10-16 19:39:04 +00:00
is_server = mode == 2;
2020-10-30 22:44:47 +00:00
// Video is Handled By GLFW Not SDL
setenv("SDL_VIDEODRIVER", "dummy", 1);
2020-11-11 15:22:14 +00:00
}