From 69d3832815ad7bd4a6936dac7fd7a9319d65ec92 Mon Sep 17 00:00:00 2001 From: TheBrokenRail Date: Wed, 13 Jul 2022 16:46:33 -0400 Subject: [PATCH] Move Screenshot Code To ARM --- CMakeLists.txt | 1 + dependencies/CMakeLists.txt | 4 +- dependencies/libpng/CMakeLists.txt | 8 +++- dependencies/libpng/zlib/CMakeLists.txt | 3 ++ dependencies/zenity/CMakeLists.txt | 3 ++ launcher/dependencies/patchelf/CMakeLists.txt | 3 ++ media-layer/core/CMakeLists.txt | 4 +- .../core/dependencies/glfw/CMakeLists.txt | 3 ++ media-layer/include/media-layer/core.h | 1 - media-layer/proxy/src/GLESv1_CM.c | 44 +++++++++++++++++++ media-layer/proxy/src/common/common.c | 1 + media-layer/proxy/src/media-layer-core.c | 19 -------- mods/CMakeLists.txt | 31 ++++++++++--- mods/include/mods/screenshot/screenshot.h | 11 +++++ mods/src/camera/camera.cpp | 6 ++- mods/src/compat/compat.c | 11 +++-- mods/src/screenshot/README.md | 2 + .../src => mods/src/screenshot}/screenshot.c | 17 ++----- 18 files changed, 119 insertions(+), 53 deletions(-) create mode 100644 mods/include/mods/screenshot/screenshot.h create mode 100644 mods/src/screenshot/README.md rename {media-layer/core/src => mods/src/screenshot}/screenshot.c (94%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 22b334fd5..a64a561cf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -62,6 +62,7 @@ endif() # Specify Installation Paths set(MCPI_INSTALL_DIR "lib/${MCPI_VARIANT_NAME}") set(MCPI_BIN_DIR "${MCPI_INSTALL_DIR}/bin") +set(MCPI_LEGAL_DIR "${MCPI_INSTALL_DIR}/legal") # For Software Licenses set(MCPI_SDK_DIR "${MCPI_INSTALL_DIR}/sdk") set(MCPI_SDK_LIB_DIR "${MCPI_SDK_DIR}/lib") set(MCPI_SDK_INCLUDE_DIR "${MCPI_SDK_DIR}/include") diff --git a/dependencies/CMakeLists.txt b/dependencies/CMakeLists.txt index 4ccd87197..68689240d 100644 --- a/dependencies/CMakeLists.txt +++ b/dependencies/CMakeLists.txt @@ -1,7 +1,9 @@ project(dependencies) # LibPNG -add_subdirectory(libpng) +if(BUILD_ARM_COMPONENTS) + add_subdirectory(libpng) +endif() # Minecraft: Pi Edition if(BUILD_ARM_COMPONENTS AND NOT MCPI_OPEN_SOURCE_ONLY) add_subdirectory(minecraft-pi) diff --git a/dependencies/libpng/CMakeLists.txt b/dependencies/libpng/CMakeLists.txt index a77779b17..1bec6b186 100644 --- a/dependencies/libpng/CMakeLists.txt +++ b/dependencies/libpng/CMakeLists.txt @@ -16,7 +16,7 @@ set(PNG_SHARED TRUE CACHE BOOL "" FORCE) # Download set(ZLIB_LIBRARY zlibstatic) -set(ZLIB_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/zlib/src" "${CMAKE_CURRENT_BINARY_DIR}/zlib/src") +set(ZLIB_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/zlib/src") set(CMAKE_POLICY_DEFAULT_CMP0054 OLD) # Silence Warning set(CMAKE_POLICY_DEFAULT_CMP0003 NEW) # Silence Warning set(CMAKE_POLICY_DEFAULT_CMP0022 NEW) # Fix Error @@ -28,6 +28,9 @@ set(CMAKE_POLICY_DEFAULT_CMP0054 NEW) # Re-Enable New Behavior set_target_properties(png12 PROPERTIES LINK_OPTIONS "LINKER:--version-script=${CMAKE_CURRENT_SOURCE_DIR}/libpng.vers") # Use Symbol Versioning set_target_properties(png12 PROPERTIES DEBUG_POSTFIX "") # Fix LibPNG Suffix In Debug Mode target_include_directories(png12 PUBLIC "$") +foreach(zlib_include_dir IN ITEMS "${ZLIB_INCLUDE_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/zlib/src") + target_include_directories(png12 PUBLIC "$") +endforeach() # Ensure Build add_custom_target(png12-build ALL DEPENDS png12) @@ -36,3 +39,6 @@ install(TARGETS png12 DESTINATION "${MCPI_LIB_DIR}") if(BUILD_ARM_COMPONENTS) install(TARGETS png12 zlibstatic EXPORT sdk DESTINATION "${MCPI_SDK_LIB_DIR}") endif() + +# License +install(FILES src/LICENSE DESTINATION "${MCPI_LEGAL_DIR}/libpng") diff --git a/dependencies/libpng/zlib/CMakeLists.txt b/dependencies/libpng/zlib/CMakeLists.txt index 0e25b1abe..d4b3d879d 100644 --- a/dependencies/libpng/zlib/CMakeLists.txt +++ b/dependencies/libpng/zlib/CMakeLists.txt @@ -11,3 +11,6 @@ add_subdirectory(src EXCLUDE_FROM_ALL) # Ensure Build add_custom_target(zlib-build ALL DEPENDS zlibstatic) + +# License +install(FILES src/README DESTINATION "${MCPI_LEGAL_DIR}/zlib") diff --git a/dependencies/zenity/CMakeLists.txt b/dependencies/zenity/CMakeLists.txt index 7356ab09f..df761c16f 100644 --- a/dependencies/zenity/CMakeLists.txt +++ b/dependencies/zenity/CMakeLists.txt @@ -13,3 +13,6 @@ add_custom_target(zenity-build ALL DEPENDS zenity) # Install install(TARGETS zenity DESTINATION "${MCPI_BIN_DIR}") + +# License +install(FILES src/COPYING DESTINATION "${MCPI_LEGAL_DIR}/zenity") diff --git a/launcher/dependencies/patchelf/CMakeLists.txt b/launcher/dependencies/patchelf/CMakeLists.txt index 3936cdb18..8475bbd65 100644 --- a/launcher/dependencies/patchelf/CMakeLists.txt +++ b/launcher/dependencies/patchelf/CMakeLists.txt @@ -12,3 +12,6 @@ set_target_properties(patchelf PROPERTIES CXX_STANDARD 17) # Install install(TARGETS patchelf DESTINATION "${MCPI_BIN_DIR}") + +# License +install(FILES src/COPYING DESTINATION "${MCPI_LEGAL_DIR}/patchelf") diff --git a/media-layer/core/CMakeLists.txt b/media-layer/core/CMakeLists.txt index 9c20a4ea8..ec598ba54 100644 --- a/media-layer/core/CMakeLists.txt +++ b/media-layer/core/CMakeLists.txt @@ -7,7 +7,7 @@ add_subdirectory(dependencies) add_subdirectory(gles) # Configuration -set(CORE_SRC src/base.cpp src/media.c src/screenshot.c $) # SDL Re-Implementation Using GLFW +set(CORE_SRC src/base.cpp src/media.c $) # SDL Re-Implementation Using GLFW if(NOT MCPI_HEADLESS_MODE) list(APPEND CORE_SRC src/audio/api.cpp src/audio/engine.c src/audio/file.cpp) else() @@ -28,5 +28,5 @@ if(NOT MCPI_HEADLESS_MODE) # OpenAL find_library(OPENAL_LIBRARY NAMES openal REQUIRED) # Link - target_link_libraries(media-layer-core PRIVATE png12 PRIVATE "${OPENAL_LIBRARY}" PRIVATE m PRIVATE glfw) + target_link_libraries(media-layer-core PRIVATE "${OPENAL_LIBRARY}" PRIVATE m PRIVATE glfw) endif() diff --git a/media-layer/core/dependencies/glfw/CMakeLists.txt b/media-layer/core/dependencies/glfw/CMakeLists.txt index d014d536e..3d9aa60ff 100644 --- a/media-layer/core/dependencies/glfw/CMakeLists.txt +++ b/media-layer/core/dependencies/glfw/CMakeLists.txt @@ -26,3 +26,6 @@ install(TARGETS glfw DESTINATION "${MCPI_LIB_DIR}") if(BUILD_ARM_COMPONENTS) install(TARGETS glfw EXPORT sdk DESTINATION "${MCPI_SDK_LIB_DIR}") endif() + +# License +install(FILES src/LICENSE.md DESTINATION "${MCPI_LEGAL_DIR}/glfw") diff --git a/media-layer/include/media-layer/core.h b/media-layer/include/media-layer/core.h index 58933617f..14c10e0ac 100644 --- a/media-layer/include/media-layer/core.h +++ b/media-layer/include/media-layer/core.h @@ -10,7 +10,6 @@ extern "C" { void media_ensure_loaded(); -void media_take_screenshot(char *home); void media_toggle_fullscreen(); void media_swap_buffers(); void media_cleanup(); diff --git a/media-layer/proxy/src/GLESv1_CM.c b/media-layer/proxy/src/GLESv1_CM.c index 6c3888e21..2f9f69746 100644 --- a/media-layer/proxy/src/GLESv1_CM.c +++ b/media-layer/proxy/src/GLESv1_CM.c @@ -1160,9 +1160,13 @@ CALL(58, glIsEnabled, GLboolean, (GLenum cap)) { static int get_glGetIntegerv_params_size(GLenum pname) { switch (pname) { case GL_TEXTURE_BINDING_2D: + case GL_PACK_ALIGNMENT: case GL_UNPACK_ALIGNMENT: { return 1; } + case GL_VIEWPORT: { + return 4; + } default: { PROXY_ERR("Unsupported glGetIntegerv Property: %u", pname); } @@ -1198,3 +1202,43 @@ CALL(61, glGetIntegerv, void, (GLenum pname, GLint *params)) { safe_write((void *) params, sizeof (GLint) * size); #endif } + +CALL(65, glReadPixels, void, (GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void *data)) { +#if defined(MEDIA_LAYER_PROXY_SERVER) + // Get Texture Size + int size = get_texture_size(width, height, format, type); + + // Lock Proxy + start_proxy_call(); + + // Arguments + write_int((uint32_t) x); + write_int((uint32_t) y); + write_int((uint32_t) width); + write_int((uint32_t) height); + write_int((uint32_t) format); + write_int((uint32_t) type); + + // Get Return Value + safe_read((void *) data, size); + + // Release Proxy + end_proxy_call(); +#else + GLint x = (GLint) read_int(); + GLint y = (GLint) read_int(); + GLsizei width = (GLsizei) read_int(); + GLsizei height = (GLsizei) read_int(); + GLenum format = (GLenum) read_int(); + GLenum type = (GLenum) read_int(); + int size = get_texture_size(width, height, format, type); + void *pixels = malloc(size); + ALLOC_CHECK(pixels); + // Run + glReadPixels(x, y, width, height, format, type, pixels); + // Return Value + safe_write((void *) pixels, (size_t) size); + // Free + free(pixels); +#endif +} diff --git a/media-layer/proxy/src/common/common.c b/media-layer/proxy/src/common/common.c index 5595ead02..e69b626ef 100644 --- a/media-layer/proxy/src/common/common.c +++ b/media-layer/proxy/src/common/common.c @@ -56,6 +56,7 @@ void safe_read(void *buf, size_t len) { // Read Remaining Data size_t to_read_to_cache = 0; while (to_read_to_cache < 1) { + CHECK_CONNECTION(); int bytes_available; if (ioctl(get_connection_read(), FIONREAD, &bytes_available) == -1) { bytes_available = 0; diff --git a/media-layer/proxy/src/media-layer-core.c b/media-layer/proxy/src/media-layer-core.c index dfc4cdd62..17c7b9343 100644 --- a/media-layer/proxy/src/media-layer-core.c +++ b/media-layer/proxy/src/media-layer-core.c @@ -176,25 +176,6 @@ CALL(6, SDL_ShowCursor, int, (int32_t toggle)) { #endif } -CALL(7, media_take_screenshot, void, (char *home)) { -#if defined(MEDIA_LAYER_PROXY_SERVER) - // Lock Proxy - start_proxy_call(); - - // Arguments - write_string(home); - - // Release Proxy - end_proxy_call(); -#else - char *home = read_string(); - // Run - media_take_screenshot(home); - // Free - free(home); -#endif -} - CALL(8, media_swap_buffers, void, ()) { #if defined(MEDIA_LAYER_PROXY_SERVER) // Lock Proxy diff --git a/mods/CMakeLists.txt b/mods/CMakeLists.txt index 4164d94bf..16b9a6365 100644 --- a/mods/CMakeLists.txt +++ b/mods/CMakeLists.txt @@ -16,6 +16,12 @@ install(DIRECTORY "include/" DESTINATION "${MCPI_SDK_INCLUDE_DIR}/mods") add_library(compat SHARED src/compat/compat.c src/compat/egl.c src/compat/x11.c src/compat/bcm_host.c) target_link_libraries(compat mods-headers reborn-patch media-layer-core) +if(NOT MCPI_HEADLESS_MODE) + target_link_libraries(compat screenshot) +endif() +if(NOT MCPI_SERVER_MODE) + target_link_libraries(compat input sign chat home dl) +endif() add_library(readdir SHARED src/readdir/readdir.c) @@ -27,23 +33,23 @@ target_link_libraries(version mods-headers reborn-patch symbols) add_library(chat SHARED src/chat/chat.cpp src/chat/ui.c) target_link_libraries(chat mods-headers reborn-patch symbols feature) +if(NOT MCPI_SERVER_MODE) + target_link_libraries(chat input media-layer-core pthread) +endif() add_library(creative SHARED src/creative/creative.cpp) target_link_libraries(creative mods-headers reborn-patch symbols feature misc) add_library(game-mode SHARED src/game-mode/game-mode.c src/game-mode/ui.cpp) target_link_libraries(game-mode mods-headers reborn-patch symbols feature) +if(NOT MCPI_SERVER_MODE) + target_link_libraries(game-mode pthread media-layer-core) +endif() if(MCPI_SERVER_MODE) add_library(server SHARED src/server/server.cpp src/server/server_properties.cpp) target_link_libraries(server mods-headers reborn-patch symbols feature home misc compat dl media-layer-core pthread) else() - target_link_libraries(compat input sign chat home dl) - - target_link_libraries(chat input media-layer-core pthread) - - target_link_libraries(game-mode pthread media-layer-core) - add_library(multiplayer SHARED src/multiplayer/multiplayer.cpp) target_link_libraries(multiplayer mods-headers reborn-patch symbols home feature) @@ -51,7 +57,10 @@ else() target_link_libraries(sound mods-headers reborn-patch symbols feature override media-layer-core) add_library(camera SHARED src/camera/camera.cpp) - target_link_libraries(camera mods-headers reborn-patch symbols media-layer-core feature home) + target_link_libraries(camera mods-headers reborn-patch symbols feature home) + if(NOT MCPI_HEADLESS_MODE) + target_link_libraries(camera screenshot) + endif() add_library(input SHARED src/input/input.cpp src/input/bow.c src/input/attack.c src/input/toggle.c src/input/misc.c src/input/drop.cpp) target_link_libraries(input mods-headers reborn-patch symbols creative feature misc media-layer-core) @@ -87,6 +96,11 @@ target_link_libraries(options mods-headers reborn-patch symbols feature home) add_library(bucket SHARED src/bucket/bucket.cpp) target_link_libraries(bucket mods-headers reborn-patch symbols feature misc) +if(NOT MCPI_HEADLESS_MODE) + add_library(screenshot SHARED src/screenshot/screenshot.c) + target_link_libraries(screenshot mods-headers reborn-util media-layer-core png12) +endif() + add_library(home SHARED src/home/home.c) target_link_libraries(home mods-headers reborn-patch symbols) @@ -108,6 +122,9 @@ if(MCPI_SERVER_MODE) else() list(APPEND MODS_TO_INSTALL multiplayer sound camera input sign touch textures atlas benchmark) endif() +if(NOT MCPI_HEADLESS_MODE) + list(APPEND MODS_TO_INSTALL screenshot) +endif() install(TARGETS ${MODS_TO_INSTALL} DESTINATION "${MCPI_INSTALL_DIR}/mods") # SDK install(TARGETS ${MODS_TO_INSTALL} EXPORT sdk DESTINATION "${MCPI_SDK_LIB_DIR}") diff --git a/mods/include/mods/screenshot/screenshot.h b/mods/include/mods/screenshot/screenshot.h new file mode 100644 index 000000000..e72cb0331 --- /dev/null +++ b/mods/include/mods/screenshot/screenshot.h @@ -0,0 +1,11 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +void screenshot_take(char *home); + +#ifdef __cplusplus +} +#endif diff --git a/mods/src/camera/camera.cpp b/mods/src/camera/camera.cpp index f00b69328..6319b215b 100644 --- a/mods/src/camera/camera.cpp +++ b/mods/src/camera/camera.cpp @@ -1,14 +1,16 @@ #include -#include #include #include +#include #include #include // Take Screenshot Using TripodCamera static void AppPlatform_linux_saveScreenshot_injection(__attribute__((unused)) unsigned char *app_platform, __attribute__((unused)) std::string const& path, __attribute__((unused)) int32_t width, __attribute__((unused)) int32_t height) { - media_take_screenshot(home_get()); +#ifndef MCPI_HEADLESS_MODE + screenshot_take(home_get()); +#endif } // Enable TripodCameraRenderer diff --git a/mods/src/compat/compat.c b/mods/src/compat/compat.c index 7a9ade9a2..29e8f5f7b 100644 --- a/mods/src/compat/compat.c +++ b/mods/src/compat/compat.c @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -58,7 +59,9 @@ HOOK(SDL_PollEvent, int, (SDL_Event *event)) { media_toggle_fullscreen(); handled = 1; } else if (event->key.keysym.sym == SDLK_F2) { - media_take_screenshot(home_get()); +#ifndef MCPI_HEADLESS_MODE + screenshot_take(home_get()); +#endif handled = 1; } else if (event->key.keysym.sym == SDLK_F1) { input_hide_gui(); @@ -119,11 +122,7 @@ static void exit_handler(__attribute__((unused)) int data) { } void init_compat() { // Install Signal Handlers - struct sigaction act_sigint; - memset((void *) &act_sigint, 0, sizeof (struct sigaction)); - act_sigint.sa_flags = SA_RESTART; - act_sigint.sa_handler = &exit_handler; - sigaction(SIGINT, &act_sigint, NULL); + signal(SIGINT, SIG_IGN); struct sigaction act_sigterm; memset((void *) &act_sigterm, 0, sizeof (struct sigaction)); act_sigterm.sa_flags = SA_RESTART; diff --git a/mods/src/screenshot/README.md b/mods/src/screenshot/README.md new file mode 100644 index 000000000..41d40d608 --- /dev/null +++ b/mods/src/screenshot/README.md @@ -0,0 +1,2 @@ +# ``screenshot`` Mod +This mod implements the core screenshot logic. diff --git a/media-layer/core/src/screenshot.c b/mods/src/screenshot/screenshot.c similarity index 94% rename from media-layer/core/src/screenshot.c rename to mods/src/screenshot/screenshot.c index 075021858..40f352703 100644 --- a/media-layer/core/src/screenshot.c +++ b/mods/src/screenshot/screenshot.c @@ -1,9 +1,3 @@ -// Config Needs To Load First -#include - -// Screenshot Code Is Useless In Headless Mode -#ifndef MCPI_HEADLESS_MODE - #include #include #include @@ -14,8 +8,9 @@ #include +#include #include -#include +#include // Ensure Screenshots Folder Exists static void ensure_screenshots_folder(char *screenshots) { @@ -99,7 +94,7 @@ static int save_png(const char *filename, unsigned char *pixels, int line_size, // Return return ret; } -void media_take_screenshot(char *home) { +void screenshot_take(char *home) { // Get Directory char *screenshots = NULL; safe_asprintf(&screenshots, "%s/screenshots", home); @@ -163,9 +158,3 @@ void media_take_screenshot(char *home) { free(file); free(screenshots); } - -#else -void media_take_screenshot() { - // NOP -} -#endif