From c3aa34bb4703940b88c7045b8cb033feccbd6202 Mon Sep 17 00:00:00 2001
From: Julian Scheid <julians37@gmail.com>
Date: Sat, 4 Jan 2014 13:42:40 +0100
Subject: [PATCH] First pass at SDL2 support
---
build/premake/extern_libs4.lua | 6 ++--
build/workspaces/update-workspaces.sh | 2 +-
libraries/osx/build-osx-libs.sh | 6 ++--
source/gui/CInput.cpp | 4 +++
source/gui/scripting/GuiScriptConversions.cpp | 6 +++-
source/ps/CConsole.cpp | 34 +++++++++++++++++++++-
source/ps/Hotkey.cpp | 4 +--
.../atlas/GameInterface/Handlers/MiscHandlers.cpp | 4 +--
8 files changed, 52 insertions(+), 14 deletions(-)
diff --git a/build/premake/extern_libs4.lua b/build/premake/extern_libs4.lua
index 5b67a06..c0ead69 100644
a
|
b
|
extern_lib_defs = {
|
535 | 535 | sdl = { |
536 | 536 | compile_settings = function() |
537 | 537 | if os.is("windows") then |
538 | | includedirs { libraries_dir .. "sdl/include/SDL" } |
| 538 | includedirs { libraries_dir .. "sdl/include/SDL2" } |
539 | 539 | elseif not _OPTIONS["android"] then |
540 | 540 | -- Support SDL_CONFIG for overriding for the default PATH-based sdl-config |
541 | 541 | sdl_config_path = os.getenv("SDL_CONFIG") |
542 | 542 | if not sdl_config_path then |
543 | | sdl_config_path = "sdl-config" |
| 543 | sdl_config_path = "sdl2-config" |
544 | 544 | end |
545 | 545 | |
546 | 546 | -- "pkg-config sdl --libs" appears to include both static and dynamic libs |
… |
… |
extern_lib_defs = {
|
554 | 554 | elseif not _OPTIONS["android"] then |
555 | 555 | sdl_config_path = os.getenv("SDL_CONFIG") |
556 | 556 | if not sdl_config_path then |
557 | | sdl_config_path = "sdl-config" |
| 557 | sdl_config_path = "sdl2-config" |
558 | 558 | end |
559 | 559 | pkgconfig_libs(nil, sdl_config_path.." --libs") |
560 | 560 | end |
diff --git a/build/workspaces/update-workspaces.sh b/build/workspaces/update-workspaces.sh
index 092755b..94ec490 100755
a
|
b
|
cd "$(dirname $0)"
|
67 | 67 | if [ "`uname -s`" = "Darwin" ]; then |
68 | 68 | # Set *_CONFIG variables on OS X, to override the path to e.g. sdl-config |
69 | 69 | export GLOOX_CONFIG=${GLOOX_CONFIG:="$(pwd)/../../libraries/osx/gloox/bin/gloox-config"} |
70 | | export SDL_CONFIG=${SDL_CONFIG:="$(pwd)/../../libraries/osx/sdl/bin/sdl-config"} |
| 70 | export SDL_CONFIG=${SDL_CONFIG:="$(pwd)/../../libraries/osx/sdl/bin/sdl2-config"} |
71 | 71 | export WX_CONFIG=${WX_CONFIG:="$(pwd)/../../libraries/osx/wxwidgets/bin/wx-config"} |
72 | 72 | export XML2_CONFIG=${XML2_CONFIG:="$(pwd)/../../libraries/osx/libxml2/bin/xml2-config"} |
73 | 73 | fi |
diff --git a/libraries/osx/build-osx-libs.sh b/libraries/osx/build-osx-libs.sh
index 8efcc9d..def77e2 100755
a
|
b
|
CURL_VERSION="curl-7.32.0"
|
25 | 25 | ICONV_VERSION="libiconv-1.14" |
26 | 26 | XML2_VERSION="libxml2-2.9.1" |
27 | 27 | # * SDL 1.2.15+ required for Lion support |
28 | | SDL_VERSION="SDL-1.2.15" |
| 28 | SDL_VERSION="SDL2-2.0.1" |
29 | 29 | BOOST_VERSION="boost_1_52_0" |
30 | 30 | # * wxWidgets 2.9+ is necessary for 64-bit OS X build w/ OpenGL support |
31 | 31 | WXWIDGETS_VERSION="wxWidgets-3.0.0" |
… |
… |
then
|
277 | 277 | pushd $LIB_DIRECTORY |
278 | 278 | |
279 | 279 | |
280 | | # patch SDL to fix Mavericks build (fixed upstream, see https://bugzilla.libsdl.org/show_bug.cgi?id=2085 ) |
281 | | # Don't use X11 - we don't need it and Mountain Lion removed it |
282 | | (patch -p0 -i ../../patches/sdl-mavericks-quartz-fix.diff && ./configure CFLAGS="$CFLAGS" CPPFLAGS="$CPPFLAGS" LDFLAGS="$LDFLAGS" --prefix="$INSTALL_DIR" --without-x --enable-shared=no && make $JOBS && make install) || die "SDL build failed" |
| 280 | (./configure CFLAGS="$CFLAGS" CPPFLAGS="$CPPFLAGS" LDFLAGS="$LDFLAGS" --prefix="$INSTALL_DIR" --without-x --enable-shared=no && make $JOBS && make install) || die "SDL build failed" |
283 | 281 | popd |
284 | 282 | touch .already-built |
285 | 283 | else |
diff --git a/source/gui/CInput.cpp b/source/gui/CInput.cpp
index af9fed8..850787e 100644
a
|
b
|
InReaction CInput::ManuallyHandleEvent(const SDL_Event_* ev)
|
93 | 93 | bool shiftKeyPressed = g_keys[SDLK_RSHIFT] || g_keys[SDLK_LSHIFT]; |
94 | 94 | |
95 | 95 | int szChar = ev->ev.key.keysym.sym; |
| 96 | #if SDL_VERSION_ATLEAST(2, 0, 0) |
| 97 | wchar_t cooked = (wchar_t)ev->ev.key.keysym.sym; |
| 98 | #else |
96 | 99 | wchar_t cooked = (wchar_t)ev->ev.key.keysym.unicode; |
| 100 | #endif |
97 | 101 | |
98 | 102 | switch (szChar) |
99 | 103 | { |
diff --git a/source/gui/scripting/GuiScriptConversions.cpp b/source/gui/scripting/GuiScriptConversions.cpp
index 93c2600..130e3f8 100644
a
|
b
|
template<> jsval ScriptInterface::ToJSVal<SDL_Event_>(JSContext* cx, SDL_Event_
|
84 | 84 | // SET(keysym, "scancode", (int)val.ev.key.keysym.scancode); // (not in wsdl.h) |
85 | 85 | SET(keysym, "sym", (int)val.ev.key.keysym.sym); |
86 | 86 | // SET(keysym, "mod", (int)val.ev.key.keysym.mod); // (not in wsdl.h) |
87 | | if (val.ev.key.keysym.unicode) |
| 87 | if (val.ev.key.keysym.sym) |
88 | 88 | { |
| 89 | #if SDL_VERSION_ATLEAST(2, 0, 0) |
| 90 | std::wstring unicode(1, (wchar_t)val.ev.key.keysym.sym); |
| 91 | #else |
89 | 92 | std::wstring unicode(1, (wchar_t)val.ev.key.keysym.unicode); |
| 93 | #endif |
90 | 94 | SET(keysym, "unicode", unicode); |
91 | 95 | } |
92 | 96 | else |
diff --git a/source/ps/CConsole.cpp b/source/ps/CConsole.cpp
index ccdb61a..f01fcba 100644
a
|
b
|
static bool isUnprintableChar(SDL_keysym key)
|
669 | 669 | #endif |
670 | 670 | { |
671 | 671 | // U+0000 to U+001F are control characters |
672 | | if (key.unicode < 0x20) |
| 672 | if (key.sym < 0x20) |
673 | 673 | { |
674 | 674 | switch (key.sym) |
675 | 675 | { |
… |
… |
InReaction conInputHandler(const SDL_Event_* ev)
|
724 | 724 | if (!g_Console->IsActive()) |
725 | 725 | return IN_PASS; |
726 | 726 | |
| 727 | #if SDL_VERSION_ATLEAST(2, 0, 0) |
| 728 | |
| 729 | if (ev->ev.type == SDL_TEXTINPUT) { |
| 730 | if (!HotkeyIsPressed("console.toggle")) { |
| 731 | CStr bytes (ev->ev.text.text, strlen(ev->ev.text.text)); |
| 732 | CStrW str (bytes.FromUTF8()); |
| 733 | for (CStrW::const_iterator it = str.begin(); it != str.end(); ++it) { |
| 734 | wchar_t chr = *it; |
| 735 | g_Console->InsertChar(0, (wchar_t)chr); |
| 736 | } |
| 737 | } |
| 738 | return IN_HANDLED; |
| 739 | } |
| 740 | else if (ev->ev.type == SDL_KEYDOWN) { |
| 741 | int sym = ev->ev.key.keysym.sym; |
| 742 | // Stop unprintable characters (ctrl+, alt+ and escape), |
| 743 | // also prevent ` and/or ~ appearing in console every time it's toggled. |
| 744 | if (!isUnprintableChar(ev->ev.key.keysym) && !HotkeyIsPressed("console.toggle")) { |
| 745 | g_Console->InsertChar(sym, 0); |
| 746 | return IN_HANDLED; |
| 747 | } |
| 748 | else { |
| 749 | return IN_PASS; |
| 750 | } |
| 751 | } |
| 752 | else { |
| 753 | return IN_PASS; |
| 754 | } |
| 755 | |
| 756 | #else // SDL 1.2: |
| 757 | |
727 | 758 | if (ev->ev.type != SDL_KEYDOWN) |
728 | 759 | return IN_PASS; |
729 | 760 | |
… |
… |
InReaction conInputHandler(const SDL_Event_* ev)
|
739 | 770 | } |
740 | 771 | |
741 | 772 | return IN_PASS; |
| 773 | #endif |
742 | 774 | } |
diff --git a/source/ps/Hotkey.cpp b/source/ps/Hotkey.cpp
index b89133d..ab18f7e 100644
a
|
b
|
InReaction HotkeyInputHandler( const SDL_Event_* ev )
|
227 | 227 | |
228 | 228 | // Rather ugly hack to make the '"' key work better on a MacBook Pro on Windows so it doesn't |
229 | 229 | // always close the console. (Maybe this would be better handled in wsdl or something?) |
230 | | if (keycode == SDLK_BACKQUOTE && (ev->ev.key.keysym.unicode == '\'' || ev->ev.key.keysym.unicode == '"')) |
231 | | keycode = ev->ev.key.keysym.unicode; |
| 230 | if (keycode == SDLK_BACKQUOTE && (ev->ev.key.keysym.sym == '\'' || ev->ev.key.keysym.sym == '"')) |
| 231 | keycode = ev->ev.key.keysym.sym; |
232 | 232 | |
233 | 233 | // Somewhat hackish: |
234 | 234 | // Create phantom 'unified-modifier' events when left- or right- modifier keys are pressed |
diff --git a/source/tools/atlas/GameInterface/Handlers/MiscHandlers.cpp b/source/tools/atlas/GameInterface/Handlers/MiscHandlers.cpp
index 161ba47..e82fb9e 100644
a
|
b
|
MESSAGEHANDLER(GuiKeyEvent)
|
189 | 189 | ev.ev.key.keysym.sym = (SDL_Keycode)(int)msg->sdlkey; |
190 | 190 | #else |
191 | 191 | ev.ev.key.keysym.sym = (SDLKey)(int)msg->sdlkey; |
192 | | #endif |
193 | 192 | ev.ev.key.keysym.unicode = msg->unichar; |
| 193 | #endif |
194 | 194 | in_dispatch_event(&ev); |
195 | 195 | } |
196 | 196 | |
… |
… |
MESSAGEHANDLER(GuiCharEvent)
|
206 | 206 | ev.ev.key.keysym.sym = (SDL_Keycode)(int)msg->sdlkey; |
207 | 207 | #else |
208 | 208 | ev.ev.key.keysym.sym = (SDLKey)(int)msg->sdlkey; |
209 | | #endif |
210 | 209 | ev.ev.key.keysym.unicode = msg->unichar; |
| 210 | #endif |
211 | 211 | in_dispatch_event(&ev); |
212 | 212 | |
213 | 213 | ev.ev.type = SDL_KEYUP; |