Skip to content

Commit

Permalink
Merge pull request #3500 from DFHack/sdl2
Browse files Browse the repository at this point in the history
Sdl2 compatibility
  • Loading branch information
myk002 committed Jun 26, 2023
2 parents d9d0482 + e308d32 commit c8c8f2e
Show file tree
Hide file tree
Showing 25 changed files with 199 additions and 911 deletions.
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# linux backup files
*~

#Kdevelop project files
# Kdevelop project files
*.kdev4
.kdev4

Expand Down Expand Up @@ -70,7 +70,7 @@ tags
# Mac OS X .DS_Store files
.DS_Store

#VS is annoying about this one.
# VS is annoying about this one.
/build/win64/DF_PATH.txt
/build/win32/DF_PATH.txt
/.vs
Expand All @@ -81,5 +81,6 @@ tags
# external plugins
/plugins/CMakeLists.custom.txt

# steam api
# 3rd party downloads
depends/steam
depends/SDL2
37 changes: 17 additions & 20 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ cmake_policy(SET CMP0074 NEW)
project(dfhack)

# set up versioning.
set(DF_VERSION "50.08")
set(DFHACK_RELEASE "r4")
set(DFHACK_PRERELEASE FALSE)
set(DF_VERSION "50.09sdl2-3")
set(DFHACK_RELEASE "rc1")
set(DFHACK_PRERELEASE TRUE)

set(DFHACK_VERSION "${DF_VERSION}-${DFHACK_RELEASE}")
set(DFHACK_ABI_VERSION 1)
Expand Down Expand Up @@ -292,23 +292,20 @@ endif()
find_package(ZLIB REQUIRED)
include_directories(${ZLIB_INCLUDE_DIRS})

if(WIN32)
# Do the same for SDL.dll
# (DFHack doesn't require this at build time, so no need to move it to the build folder)
# TODO: remove SDL.dll from our distribution once DF moves to SDL2. we only
# continue to include it so we don't break Steam players on update by removing
# the SDL.dll that DF needs.
set(SDL_DOWNLOAD_DIR ${dfhack_SOURCE_DIR}/package/windows/win${DFHACK_BUILD_ARCH})
if(${DFHACK_BUILD_ARCH} STREQUAL "64")
download_file("https://github.com/DFHack/dfhack-bin/releases/download/0.44.09/win64-SDL.dll"
${SDL_DOWNLOAD_DIR}/SDL.dll
"1ae242c4b94cb03756a1288122a66faf")
else()
download_file("https://github.com/DFHack/dfhack-bin/releases/download/0.44.09/win32-SDL.dll"
${SDL_DOWNLOAD_DIR}/SDL.dll
"5a09604daca6b2b5ce049d79af935d6a")
endif()
endif()
# Download SDL release and extract into depends in the build dir
# all we need are the header files (including generated headers), so the same release package
# will work for all platforms
# (the above statement is untested for OSX)
set(SDL_VERSION 2.26.2)
set(SDL_ZIP_MD5 574daf26d48de753d0b1e19823c9d8bb)
set(SDL_ZIP_FILE SDL2-devel-${SDL_VERSION}-VC.zip)
set(SDL_ZIP_PATH ${dfhack_SOURCE_DIR}/depends/SDL2/)
download_file("https://github.com/libsdl-org/SDL/releases/download/release-${SDL_VERSION}/${SDL_ZIP_FILE}"
${SDL_ZIP_PATH}${SDL_ZIP_FILE}
${SDL_ZIP_MD5})
file(ARCHIVE_EXTRACT INPUT ${SDL_ZIP_PATH}${SDL_ZIP_FILE}
DESTINATION ${SDL_ZIP_PATH})
include_directories(${SDL_ZIP_PATH}/SDL2-${SDL_VERSION}/include)

if(APPLE)
# libstdc++ (GCC 4.8.5 for OS X 10.6)
Expand Down
Binary file added data/art/pathable.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/art/unsuspend.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 0 additions & 4 deletions library/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -431,10 +431,6 @@ if(UNIX)
install(TARGETS dfhooks
LIBRARY DESTINATION .
RUNTIME DESTINATION .)
else()
# On windows, copy SDL.dll so DF can still run.
install(PROGRAMS ${dfhack_SOURCE_DIR}/package/windows/win${DFHACK_BUILD_ARCH}/SDL.dll
DESTINATION ${DFHACK_LIBRARY_DESTINATION})
endif()

# install the main lib
Expand Down
114 changes: 26 additions & 88 deletions library/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ using namespace DFHack;
#include <condition_variable>
#include "md5wrapper.h"

#include "SDL_events.h"
#include <SDL_events.h>

#ifdef LINUX_BUILD
#include <dlfcn.h>
Expand Down Expand Up @@ -2347,99 +2347,37 @@ bool Core::DFH_ncurses_key(int key)
return ncurses_wgetch(key, dummy);
}

int UnicodeAwareSym(const SDL::KeyboardEvent& ke)
{
// Assume keyboard layouts don't change the order of numbers:
if( '0' <= ke.ksym.sym && ke.ksym.sym <= '9') return ke.ksym.sym;
if(SDL::K_F1 <= ke.ksym.sym && ke.ksym.sym <= SDL::K_F12) return ke.ksym.sym;

// These keys are mapped to the same control codes as Ctrl-?
switch (ke.ksym.sym)
{
case SDL::K_RETURN:
case SDL::K_KP_ENTER:
case SDL::K_TAB:
case SDL::K_ESCAPE:
case SDL::K_DELETE:
return ke.ksym.sym;
default:
break;
}

int unicode = ke.ksym.unicode;

// convert Ctrl characters to their 0x40-0x5F counterparts:
if (unicode < ' ')
{
unicode += 'A' - 1;
}

// convert A-Z to their a-z counterparts:
if('A' <= unicode && unicode <= 'Z')
{
unicode += 'a' - 'A';
}

// convert various other punctuation marks:
if('\"' == unicode) unicode = '\'';
if('+' == unicode) unicode = '=';
if(':' == unicode) unicode = ';';
if('<' == unicode) unicode = ',';
if('>' == unicode) unicode = '.';
if('?' == unicode) unicode = '/';
if('{' == unicode) unicode = '[';
if('|' == unicode) unicode = '\\';
if('}' == unicode) unicode = ']';
if('~' == unicode) unicode = '`';

return unicode;
}

// returns true if the event is handled
bool Core::DFH_SDL_Event(SDL::Event* ev)
bool Core::DFH_SDL_Event(SDL_Event* ev)
{
// do NOT process events before we are ready.
if(!started || !ev)
return false;

if(ev->type == SDL::ET_ACTIVEEVENT && ev->active.gain)
{
if (ev->type == SDL_WINDOWEVENT && ev->window.event == SDL_WINDOWEVENT_FOCUS_GAINED) {
// clear modstate when gaining focus in case alt-tab was used when
// losing focus and modstate is now incorrectly set
modstate = 0;
return false;
}

if(ev->type == SDL::ET_KEYDOWN || ev->type == SDL::ET_KEYUP)
{
auto ke = (SDL::KeyboardEvent *)ev;
if (ev->type == SDL_KEYDOWN || ev->type == SDL_KEYUP) {
auto &ke = ev->key;

if (ke->ksym.sym == SDL::K_LSHIFT || ke->ksym.sym == SDL::K_RSHIFT)
modstate = (ev->type == SDL::ET_KEYDOWN) ? modstate | DFH_MOD_SHIFT : modstate & ~DFH_MOD_SHIFT;
else if (ke->ksym.sym == SDL::K_LCTRL || ke->ksym.sym == SDL::K_RCTRL)
modstate = (ev->type == SDL::ET_KEYDOWN) ? modstate | DFH_MOD_CTRL : modstate & ~DFH_MOD_CTRL;
else if (ke->ksym.sym == SDL::K_LALT || ke->ksym.sym == SDL::K_RALT)
modstate = (ev->type == SDL::ET_KEYDOWN) ? modstate | DFH_MOD_ALT : modstate & ~DFH_MOD_ALT;
else if(ke->state == SDL::BTN_PRESSED && !hotkey_states[ke->ksym.sym])
if (ke.keysym.sym == SDLK_LSHIFT || ke.keysym.sym == SDLK_RSHIFT)
modstate = (ev->type == SDL_KEYDOWN) ? modstate | DFH_MOD_SHIFT : modstate & ~DFH_MOD_SHIFT;
else if (ke.keysym.sym == SDLK_LCTRL || ke.keysym.sym == SDLK_RCTRL)
modstate = (ev->type == SDL_KEYDOWN) ? modstate | DFH_MOD_CTRL : modstate & ~DFH_MOD_CTRL;
else if (ke.keysym.sym == SDLK_LALT || ke.keysym.sym == SDLK_RALT)
modstate = (ev->type == SDL_KEYDOWN) ? modstate | DFH_MOD_ALT : modstate & ~DFH_MOD_ALT;
else if (ke.state == SDL_PRESSED && !hotkey_states[ke.keysym.sym])
{
hotkey_states[ke->ksym.sym] = true;

// Use unicode so Windows gives the correct value for the
// user's Input Language
if(ke->ksym.unicode && ((ke->ksym.unicode & 0xff80) == 0))
{
int key = UnicodeAwareSym(*ke);
SelectHotkey(key, modstate);
}
else
{
// Pretend non-ascii characters don't happen:
SelectHotkey(ke->ksym.sym, modstate);
}
hotkey_states[ke.keysym.sym] = true;
SelectHotkey(ke.keysym.sym, modstate);
}
else if(ke->state == SDL::BTN_RELEASED)
else if(ke.state == SDL_RELEASED)
{
hotkey_states[ke->ksym.sym] = false;
hotkey_states[ke.keysym.sym] = false;
}
}
return false;
Expand All @@ -2455,12 +2393,12 @@ bool Core::SelectHotkey(int sym, int modifiers)
while (screen->child)
screen = screen->child;

if (sym == SDL::K_KP_ENTER)
sym = SDL::K_RETURN;
if (sym == SDLK_KP_ENTER)
sym = SDLK_RETURN;

std::string cmd;

DEBUG(keybinding).print("checking hotkeys for sym=%d, modifiers=%x\n", sym, modifiers);
DEBUG(keybinding).print("checking hotkeys for sym=%d (%c), modifiers=%x\n", sym, sym, modifiers);

{
std::lock_guard<std::mutex> lock(HotkeyMutex);
Expand Down Expand Up @@ -2497,7 +2435,7 @@ bool Core::SelectHotkey(int sym, int modifiers)

if (cmd.empty()) {
// Check the hotkey keybindings
int idx = sym - SDL::K_F1;
int idx = sym - SDLK_F1;
if(idx >= 0 && idx < 8)
{
/* TODO: understand how this changes for v50
Expand Down Expand Up @@ -2555,22 +2493,22 @@ static bool parseKeySpec(std::string keyspec, int *psym, int *pmod, std::string
}

if (keyspec.size() == 1 && keyspec[0] >= 'A' && keyspec[0] <= 'Z') {
*psym = SDL::K_a + (keyspec[0]-'A');
*psym = SDLK_a + (keyspec[0]-'A');
return true;
} else if (keyspec.size() == 1 && keyspec[0] == '`') {
*psym = SDL::K_BACKQUOTE;
*psym = SDLK_BACKQUOTE;
return true;
} else if (keyspec.size() == 1 && keyspec[0] >= '0' && keyspec[0] <= '9') {
*psym = SDL::K_0 + (keyspec[0]-'0');
*psym = SDLK_0 + (keyspec[0]-'0');
return true;
} else if (keyspec.size() == 2 && keyspec[0] == 'F' && keyspec[1] >= '1' && keyspec[1] <= '9') {
*psym = SDL::K_F1 + (keyspec[1]-'1');
*psym = SDLK_F1 + (keyspec[1]-'1');
return true;
} else if (keyspec.size() == 3 && keyspec.substr(0, 2) == "F1" && keyspec[2] >= '0' && keyspec[2] <= '2') {
*psym = SDL::K_F10 + (keyspec[2]-'0');
*psym = SDLK_F10 + (keyspec[2]-'0');
return true;
} else if (keyspec == "Enter") {
*psym = SDL::K_RETURN;
*psym = SDLK_RETURN;
return true;
} else
return false;
Expand Down
2 changes: 1 addition & 1 deletion library/Hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ DFhackCExport void dfhooks_prerender() {

// called from the main thread for each SDL event. if true is returned, then
// the event has been consumed and further processing shouldn't happen
DFhackCExport bool dfhooks_sdl_event(SDL::Event* event) {
DFhackCExport bool dfhooks_sdl_event(SDL_Event* event) {
if (disabled)
return false;
return DFHack::Core::getInstance().DFH_SDL_Event(event);
Expand Down
5 changes: 4 additions & 1 deletion library/LuaApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1715,6 +1715,7 @@ static const LuaWrapper::FunctionReg dfhack_textures_module[] = {
WRAPM(Textures, getRedPinTexposStart),
WRAPM(Textures, getIconsTexposStart),
WRAPM(Textures, getOnOffTexposStart),
WRAPM(Textures, getMapUnsuspendTexposStart),
WRAPM(Textures, getControlPanelTexposStart),
WRAPM(Textures, getThinBordersTexposStart),
WRAPM(Textures, getMediumBordersTexposStart),
Expand Down Expand Up @@ -2635,6 +2636,7 @@ static int screen_charToKey(lua_State *L)
return 1;
}

/*
static int screen_zoom(lua_State *L)
{
using df::global::enabler;
Expand All @@ -2651,6 +2653,7 @@ static int screen_zoom(lua_State *L)
enabler->zoom_display(cmd);
return 0;
}
*/

}

Expand All @@ -2671,7 +2674,7 @@ static const luaL_Reg dfhack_screen_funcs[] = {
{ "_doSimulateInput", screen_doSimulateInput },
{ "keyToChar", screen_keyToChar },
{ "charToKey", screen_charToKey },
{ "zoom", screen_zoom },
//{ "zoom", screen_zoom },
{ NULL, NULL }
};

Expand Down
4 changes: 2 additions & 2 deletions library/include/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ namespace DFHack
friend void ::dfhooks_shutdown();
friend void ::dfhooks_update();
friend void ::dfhooks_prerender();
friend bool ::dfhooks_sdl_event(SDL::Event* event);
friend bool ::dfhooks_sdl_event(SDL_Event* event);
friend bool ::dfhooks_ncurses_key(int key);
public:
/// Get the single Core instance or make one.
Expand Down Expand Up @@ -195,7 +195,7 @@ namespace DFHack
bool InitSimulationThread();
int Update (void);
int Shutdown (void);
bool DFH_SDL_Event(SDL::Event* event);
bool DFH_SDL_Event(SDL_Event* event);
bool ncurses_wgetch(int in, int & out);
bool DFH_ncurses_key(int key);

Expand Down
54 changes: 2 additions & 52 deletions library/include/Hooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,61 +24,11 @@ distribution.

#pragma once

/*
* Some much needed SDL fakery.
*/

#include "Pragma.h"
#include "Export.h"
#include <string>
#include <stdint.h>

#include "modules/Graphic.h"

// function and variable pointer... we don't try to understand what SDL does here
typedef void * fPtr;
typedef void * vPtr;
struct WINDOW;
namespace SDL
{
union Event;
}

// these functions are here because they call into DFHack::Core and therefore need to
// be declared as friend functions/known
#ifdef _DARWIN
DFhackCExport int DFH_SDL_NumJoysticks(void);
DFhackCExport void DFH_SDL_Quit(void);
DFhackCExport int DFH_SDL_PollEvent(SDL::Event* event);
DFhackCExport int DFH_SDL_Init(uint32_t flags);
DFhackCExport int DFH_wgetch(WINDOW * win);
#endif
DFhackCExport int SDL_NumJoysticks(void);
DFhackCExport void SDL_Quit(void);
DFhackCExport int SDL_PollEvent(SDL::Event* event);
DFhackCExport int SDL_PushEvent(SDL::Event* event);
DFhackCExport int SDL_Init(uint32_t flags);
DFhackCExport int wgetch(WINDOW * win);

DFhackCExport int SDL_UpperBlit(DFHack::DFSDL_Surface* src, DFHack::DFSDL_Rect* srcrect, DFHack::DFSDL_Surface* dst, DFHack::DFSDL_Rect* dstrect);
DFhackCExport vPtr SDL_CreateRGBSurface(uint32_t flags, int width, int height, int depth,
uint32_t Rmask, uint32_t Gmask, uint32_t Bmask, uint32_t Amask);
DFhackCExport vPtr SDL_CreateRGBSurfaceFrom(vPtr pixels, int width, int height, int depth, int pitch,
uint32_t Rmask, uint32_t Gmask, uint32_t Bmask, uint32_t Amask);
DFhackCExport void SDL_FreeSurface(vPtr surface);
DFhackCExport vPtr SDL_ConvertSurface(vPtr surface, vPtr format, uint32_t flags);
DFhackCExport int SDL_LockSurface(vPtr surface);
DFhackCExport void SDL_UnlockSurface(vPtr surface);
DFhackCExport uint8_t SDL_GetMouseState(int *x, int *y);
DFhackCExport void * SDL_GetVideoSurface(void);

DFhackCExport int SDL_SemWait(vPtr sem);
DFhackCExport int SDL_SemPost(vPtr sem);
union SDL_Event;

// new Hooks API
DFhackCExport void dfhooks_init();
DFhackCExport void dfhooks_shutdown();
DFhackCExport void dfhooks_update();
DFhackCExport void dfhooks_prerender();
DFhackCExport bool dfhooks_sdl_event(SDL::Event* event);
DFhackCExport bool dfhooks_sdl_event(SDL_Event* event);
DFhackCExport bool dfhooks_ncurses_key(int key);

0 comments on commit c8c8f2e

Please sign in to comment.