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

437 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 <time.h>
#include <unistd.h>
#include <FreeImage.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 <EGL/egl.h>
#include <GLES/gl.h>
#include <X11/Xlib.h>
#include <libcore/libcore.h>
2020-10-04 00:30:15 +00:00
#include "extra.h"
2020-10-05 22:17:55 +00:00
static GLFWwindow *glfw_window;
2020-09-25 16:43:53 +00:00
static Display *x11_display;
static Window x11_window;
static Window x11_root_window;
static int window_loaded = 0;
HOOK(eglGetDisplay, EGLDisplay, (__attribute__((unused)) NativeDisplayType native_display)) {
// Handled In ensure_x11_window()
return 0;
}
// Get Reference To X Window
2020-10-05 22:17:55 +00:00
static void store_x11_window() {
x11_display = glfwGetX11Display();
x11_window = glfwGetX11Window(glfw_window);
x11_root_window = RootWindow(x11_display, DefaultScreen(x11_display));
window_loaded = 1;
2020-09-25 16:43:53 +00:00
}
// Handled In SDL_WM_SetCaption
HOOK(eglInitialize, EGLBoolean, (__attribute__((unused)) EGLDisplay display, __attribute__((unused)) EGLint *major, __attribute__((unused)) EGLint *minor)) {
return EGL_TRUE;
}
// Handled In SDL_WM_SetCaption
HOOK(eglChooseConfig, EGLBoolean, (__attribute__((unused)) EGLDisplay display, __attribute__((unused)) EGLint const *attrib_list, __attribute__((unused)) EGLConfig *configs, __attribute__((unused)) EGLint config_size, __attribute__((unused)) EGLint *num_config)) {
return EGL_TRUE;
}
// Handled In SDL_WM_SetCaption
HOOK(eglBindAPI, EGLBoolean, (__attribute__((unused)) EGLenum api)) {
return EGL_TRUE;
}
// Handled In SDL_WM_SetCaption
HOOK(eglCreateContext, EGLContext, (__attribute__((unused)) EGLDisplay display, __attribute__((unused)) EGLConfig config, __attribute__((unused)) EGLContext share_context, __attribute__((unused)) EGLint const *attrib_list)) {
return 0;
}
// Handled In SDL_WM_SetCaption
HOOK(eglCreateWindowSurface, EGLSurface, (__attribute__((unused)) EGLDisplay display, __attribute__((unused)) EGLConfig config, __attribute__((unused)) NativeWindowType native_window, __attribute__((unused)) EGLint const *attrib_list)) {
return 0;
}
// Handled In SDL_WM_SetCaption
HOOK(eglMakeCurrent, EGLBoolean, (__attribute__((unused)) EGLDisplay display, __attribute__((unused)) EGLSurface draw, __attribute__((unused)) EGLSurface read, __attribute__((unused)) EGLContext context)) {
return EGL_TRUE;
}
// Handled In SDL_Quit
HOOK(eglDestroySurface, EGLBoolean, (__attribute__((unused)) EGLDisplay display, __attribute__((unused)) EGLSurface surface)) {
return EGL_TRUE;
}
// Handled In SDL_Quit
HOOK(eglDestroyContext, EGLBoolean, (__attribute__((unused)) EGLDisplay display, __attribute__((unused)) EGLContext context)) {
return EGL_TRUE;
}
// Handled In SDL_Quit
HOOK(eglTerminate, EGLBoolean, (__attribute__((unused)) EGLDisplay display)) {
return EGL_TRUE;
}
// Handled In SDL_WM_SetCaption
2020-10-04 20:45:00 +00:00
HOOK(SDL_SetVideoMode, SDL_Surface *, (__attribute__((unused)) int width, __attribute__((unused)) int height, __attribute__((unused)) int bpp, __attribute__((unused)) uint32_t flags)) {
2020-09-25 16:43:53 +00:00
// Return Value Is Only Used For A NULL-Check
return (SDL_Surface *) 1;
}
2020-10-05 22:17:55 +00:00
// Handle GLFW Error
static void glfw_error(__attribute__((unused)) int error, const char *description) {
fprintf(stderr, "GLFW Error: %s\n", description);
exit(1);
}
// 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;
// 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) {
key_press((char) '\b');
}
}
// Pass Text To Minecraft
static void glfw_char(__attribute__((unused)) GLFWwindow *window, unsigned int codepoint) {
key_press((char) codepoint);
}
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;
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;
event.motion.xrel = (xpos - last_mouse_x);
event.motion.yrel = (ypos - last_mouse_y);
last_mouse_x = xpos;
last_mouse_y = ypos;
SDL_PushEvent(&event);
}
2020-10-04 20:45:00 +00:00
2020-10-05 22:17:55 +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;
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;
event.button.button = button == GLFW_MOUSE_BUTTON_RIGHT ? SDL_BUTTON_RIGHT : (button == GLFW_MOUSE_BUTTON_LEFT ? SDL_BUTTON_LEFT : SDL_BUTTON_MIDDLE);
SDL_PushEvent(&event);
}
2020-10-04 00:30:15 +00:00
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-04 20:45:00 +00:00
FreeImage_Initialise(0);
2020-10-05 22:17:55 +00:00
glfwSetErrorCallback(glfw_error);
2020-09-25 16:43:53 +00:00
2020-10-05 22:17:55 +00:00
if (!glfwInit()) {
fprintf(stderr, "Unable To Initialize GLFW\n");
exit(1);
}
2020-09-25 16:43:53 +00:00
2020-10-05 22:17:55 +00:00
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);
2020-09-25 16:43:53 +00:00
2020-10-05 22:17:55 +00:00
glfw_window = glfwCreateWindow(840, 480, title, NULL, NULL);
if (!glfw_window) {
fprintf(stderr, "Unable To Create GLFW Window\n");
exit(1);
}
2020-09-25 16:43:53 +00:00
2020-10-05 22:17:55 +00:00
glfwSetKeyCallback(glfw_window, glfw_key);
glfwSetCharCallback(glfw_window, glfw_char);
glfwSetCursorPosCallback(glfw_window, glfw_motion);
glfwSetMouseButtonCallback(glfw_window, glfw_click);
2020-09-25 16:43:53 +00:00
2020-10-05 22:17:55 +00:00
store_x11_window();
2020-09-25 16:43:53 +00:00
2020-10-05 22:17:55 +00:00
glfwMakeContextCurrent(glfw_window);
2020-09-25 16:43:53 +00:00
}
HOOK(eglSwapBuffers, EGLBoolean, (__attribute__((unused)) EGLDisplay display, __attribute__((unused)) EGLSurface surface)) {
2020-10-05 22:17:55 +00:00
glfwSwapBuffers(glfw_window);
2020-09-26 20:44:39 +00:00
2020-10-05 22:17:55 +00:00
return EGL_TRUE;
2020-09-26 20:44:39 +00:00
}
static int is_fullscreen = 0;
2020-10-05 22:17:55 +00:00
static int old_width = -1;
static int old_height = -1;
2020-09-26 20:44:39 +00:00
2020-10-05 22:17:55 +00:00
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-09-26 20:44:39 +00:00
Screen *screen = DefaultScreenOfDisplay(x11_display);
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-04 20:45:00 +00:00
// 4 (Year + 1 (Hyphen) + 2 (Month) + 1 (Hyphen) + 2 (Day) + 1 (Underscore) + 2 (Hour) + 1 (Period) + 2 (Minute) + 1 (Period) + 2 (Second) + 1 (Terminator)
#define TIME_SIZE 20
2020-10-05 22:17:55 +00:00
// Take Screenshot
2020-10-04 20:45:00 +00:00
static void screenshot() {
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
char time[TIME_SIZE];
strftime(time, TIME_SIZE, "%Y-%m-%d_%H.%M.%S", timeinfo);
char *screenshots = NULL;
asprintf(&screenshots, "%s/.minecraft/screenshots", getenv("HOME"));
int num = 1;
char *file = NULL;
asprintf(&file, "%s/%s.png", screenshots, time);
while (access(file, F_OK) != -1) {
asprintf(&file, "%s/%s-%i.png", screenshots, time, num);
num++;
}
2020-10-05 22:17:55 +00:00
int width;
int height;
glfwGetWindowSize(glfw_window, &width, &height);
int line_size = width * 3;
int size = height * line_size;
2020-10-04 20:45:00 +00:00
unsigned char pixels[size];
2020-10-05 22:17:55 +00:00
glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels);
2020-10-04 20:45:00 +00:00
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
// Swap Red And Blue
for (int i = 0; i < (size / 3); i++) {
int pixel = i * 3;
int red = pixels[pixel];
int blue = pixels[pixel + 2];
pixels[pixel] = blue;
pixels[pixel + 2] = red;
}
#endif
2020-10-05 22:17:55 +00:00
FIBITMAP *image = FreeImage_ConvertFromRawBits(pixels, width, height, line_size, 24, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, 0);
2020-10-04 20:45:00 +00:00
if (!FreeImage_Save(FIF_PNG, image, file, 0)) {
fprintf(stderr, "Screenshot Failed: %s\n", file);
} else {
fprintf(stderr, "Screenshot Saved: %s\n", file);
}
FreeImage_Unload(image);
free(file);
free(screenshots);
}
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();
// Close Window
if (glfwWindowShouldClose(glfw_window)) {
SDL_Event event;
event.type = SDL_QUIT;
SDL_PushEvent(&event);
glfwSetWindowShouldClose(glfw_window, 0);
}
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) {
screenshot();
handled = 1;
2020-10-04 00:30:15 +00:00
}
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;
}
// Terminate EGL
HOOK(SDL_Quit, void, ()) {
ensure_SDL_Quit();
(*real_SDL_Quit)();
2020-10-05 22:17:55 +00:00
glfwDestroyWindow(glfw_window);
glfwTerminate();
}
// Fix SDL Cursor Visibility/Grabbing
HOOK(SDL_WM_GrabInput, SDL_GrabMode, (SDL_GrabMode mode)) {
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
if (window_loaded) {
if (mode == SDL_GRAB_OFF) {
XFixesShowCursor(x11_display, x11_window);
} else {
XFixesHideCursor(x11_display, x11_window);
}
XFlush(x11_display);
}
}
return mode == SDL_GRAB_QUERY ? (glfwGetInputMode(glfw_window, GLFW_CURSOR) == GLFW_CURSOR_NORMAL ? SDL_GRAB_OFF : SDL_GRAB_ON) : mode;
}
// Stub SDL Cursor Visibility
HOOK(SDL_ShowCursor, int, (int toggle)) {
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
}
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)) {
ensure_XTranslateCoordinates();
if (window_loaded) {
return (*real_XTranslateCoordinates)(x11_display, x11_window, x11_root_window, src_x, src_y, dest_x_return, dest_y_return, child_return);
} else {
return (*real_XTranslateCoordinates)(display, src_w, dest_w, src_x, src_y, dest_x_return, dest_y_return, child_return);
}
}
HOOK(XGetWindowAttributes, int, (Display *display, Window w, XWindowAttributes *window_attributes_return)) {
ensure_XGetWindowAttributes();
if (window_loaded) {
return (*real_XGetWindowAttributes)(x11_display, x11_window, window_attributes_return);
} else {
return (*real_XGetWindowAttributes)(display, w, window_attributes_return);
}
}
#include <stdlib.h>
// Use VirGL
__attribute__((constructor)) static void init() {
setenv("LIBGL_ALWAYS_SOFTWARE", "1", 1);
setenv("GALLIUM_DRIVER", "virpipe", 1);
}