Skip to content

Experimental SDL Console #5027

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 35 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
8bb0241
Initial
dhthwy Nov 15, 2024
9941d07
Initial
dhthwy Nov 15, 2024
9537846
sdlconsole: trying to compile
dhthwy Nov 15, 2024
2e9432b
sdlconsole: fix compile
dhthwy Nov 15, 2024
48993df
sdlconsole: fix inconsistent use of class vs struct
dhthwy Nov 15, 2024
5024de4
sdlconsole: use DFSDL to resolve symbols
dhthwy Nov 16, 2024
63ac52b
Merge branch 'develop' into sdl_console
dhthwy Nov 16, 2024
60e5739
sdlconsole: fix msleep
dhthwy Nov 16, 2024
1f14688
sdlconsole: some cleaning
dhthwy Nov 16, 2024
16f2bde
sdlconsole: support two click select word, three click select line mo…
dhthwy Nov 16, 2024
29a0550
cleanup some scrollbar, and fix nassy bug in SignalEmitter disconnect()
dhthwy Nov 18, 2024
433879e
sdlconsole: some housekeeping. some performance tweaks.
dhthwy Nov 18, 2024
42a4da1
Merge branch 'develop' into sdl_console
dhthwy Nov 18, 2024
58d95ab
sdlconsole: inform SDL to pass mouse clicks when gaining window focus
dhthwy Nov 19, 2024
06363e2
sdlconsole: safer text processing. add more comments. make ctrl-C int…
dhthwy Nov 19, 2024
b5015b5
sdlconsole: support load from saved command history
dhthwy Nov 19, 2024
b7fe35b
sdlconsole: cleanup text_selection and some other bits
dhthwy Nov 20, 2024
b44a35a
sdlconsole: fix disabled cursor on focus gain. plus tweaks to prompt.
dhthwy Nov 20, 2024
8ddaa80
sdlconsole: attempt to fix glyph rendering
dhthwy Nov 20, 2024
6f43361
sdlconsole: test remove fringe pixel artifacts from font. By creating…
dhthwy Nov 21, 2024
2648864
more housekeeping
dhthwy Nov 23, 2024
81eaeb9
sdlconsole: refactoring
dhthwy Nov 27, 2024
8a53e3c
try to fix tests
dhthwy Nov 27, 2024
293d203
Merge branch 'develop' into sdl_console
dhthwy Nov 27, 2024
2d35963
sdlconsole: Console overhaul. Hacks.
dhthwy Nov 28, 2024
dc7a79e
Merge
dhthwy Dec 4, 2024
c9ab1d5
sdlconsole: Console hierarchy things
dhthwy Dec 5, 2024
a27af58
sdlconsole: fixup Windows console
dhthwy Dec 5, 2024
6e7ed2a
sdlconsole: test color on Windows
dhthwy Dec 6, 2024
5f791a6
Merge branch 'develop' into sdl_console
dhthwy Dec 6, 2024
fb010a5
Merge branch 'develop' into sdl_console
dhthwy Dec 9, 2024
28338bd
Merge branch 'develop' into sdl_console
dhthwy Dec 9, 2024
25b4ddc
various
dhthwy Jan 30, 2025
1a48796
Merge branch 'develop' into sdl_console
dhthwy Jan 30, 2025
9fe9295
lint. fix build
dhthwy Jan 31, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ endif(CMAKE_CONFIGURATION_TYPES)
option(BUILD_DOCS "Choose whether to build the documentation (requires python and Sphinx)." OFF)
option(BUILD_DOCS_NO_HTML "Don't build the HTML docs, only the in-game docs." OFF)
option(REMOVE_SYMBOLS_FROM_DF_STUBS "Remove debug symbols from DF stubs. (Reduces libdfhack size to about half but removes a few useful symbols)" ON)
option(DFHACK_SDL_CONSOLE "Use experimental SDL console" ON)

macro(CHECK_GCC compiler_path)
execute_process(COMMAND ${compiler_path} -dumpversion OUTPUT_VARIABLE GCC_VERSION_OUT)
Expand Down
22 changes: 19 additions & 3 deletions library/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ if(UNIX)
option(CONSOLE_NO_CATCH "Make the console not catch 'CTRL+C' events for easier debugging." OFF)
endif()

option(DFHACK_SDL_CONSOLE "Use experimental SDL console" ON)

# Generation
set(CODEGEN_OUT ${dfapi_SOURCE_DIR}/include/df/codegen.out.xml)

Expand Down Expand Up @@ -115,14 +117,24 @@ set(MAIN_SOURCES
file(GLOB_RECURSE TEST_SOURCES
LIST_DIRECTORIES false
*test.cpp)

if (DFHACK_SDL_CONSOLE)
list(APPEND TEST_SOURCES SDLConsole_impl.cpp)
endif()
dfhack_test(dfhack-test "${TEST_SOURCES}")

if(WIN32)
set(CONSOLE_SOURCES Console-windows.cpp)
set(CONSOLE_SOURCES Console-windows.cpp)
else()
set(CONSOLE_SOURCES Console-posix.cpp)
set(CONSOLE_SOURCES Console-posix.cpp)
endif()
list(APPEND CONSOLE_SOURCES Console.cpp)
if (DFHACK_SDL_CONSOLE)
list(APPEND CONSOLE_SOURCES Console-sdl.cpp SDLConsole_impl.cpp)
endif()

set(DFCLIENT_CONSOLE_SOURCES Console.cpp)

set(MAIN_SOURCES_WINDOWS
${CONSOLE_SOURCES}
Hooks.cpp
Expand Down Expand Up @@ -374,8 +386,10 @@ target_include_directories(dfhack PRIVATE ${xlsxio_INCLUDES} ${SDL2_INCLUDE_DIRS
add_dependencies(dfhack generate_proto_core)
add_dependencies(dfhack generate_headers)

add_library(dfhack-client SHARED RemoteClient.cpp ColorText.cpp MiscUtils.cpp Error.cpp ${PROJECT_PROTO_SRCS} ${CONSOLE_SOURCES})
add_library(dfhack-client SHARED RemoteClient.cpp ColorText.cpp MiscUtils.cpp Error.cpp ${PROJECT_PROTO_SRCS} ${DFCLIENT_CONSOLE_SOURCES})
target_include_directories(dfhack-client PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/proto)
target_compile_definitions(dfhack-client PUBLIC BUILD_DFHACK_CLIENT)

add_dependencies(dfhack-client dfhack)

add_executable(dfhack-run dfhack-run.cpp)
Expand All @@ -384,6 +398,8 @@ target_include_directories(dfhack-run PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/includ
add_executable(binpatch binpatch.cpp)
target_link_libraries(binpatch dfhack-md5)

target_include_directories(dfhack-test PRIVATE ${SDL2_INCLUDE_DIRS})

if(WIN32)
set_target_properties(dfhack PROPERTIES OUTPUT_NAME "dfhooks_dfhack" )
set_target_properties(dfhack PROPERTIES COMPILE_FLAGS "/FI\"Export.h\"" )
Expand Down
97 changes: 26 additions & 71 deletions library/Console-posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
_res; })
#endif

#include "Console.h"
#include "PosixConsole.h"

#include "Hooks.h"
using namespace DFHack;

Expand All @@ -91,51 +92,6 @@ static int isUnsupportedTerm(void)
return 0;
}

const char * ANSI_CLS = "\033[2J";
const char * ANSI_BLACK = "\033[22;30m";
const char * ANSI_RED = "\033[22;31m";
const char * ANSI_GREEN = "\033[22;32m";
const char * ANSI_BROWN = "\033[22;33m";
const char * ANSI_BLUE = "\033[22;34m";
const char * ANSI_MAGENTA = "\033[22;35m";
const char * ANSI_CYAN = "\033[22;36m";
const char * ANSI_GREY = "\033[22;37m";
const char * ANSI_DARKGREY = "\033[01;30m";
const char * ANSI_LIGHTRED = "\033[01;31m";
const char * ANSI_LIGHTGREEN = "\033[01;32m";
const char * ANSI_YELLOW = "\033[01;33m";
const char * ANSI_LIGHTBLUE = "\033[01;34m";
const char * ANSI_LIGHTMAGENTA = "\033[01;35m";
const char * ANSI_LIGHTCYAN = "\033[01;36m";
const char * ANSI_WHITE = "\033[01;37m";
const char * RESETCOLOR = "\033[0m";

const char * getANSIColor(const int c)
{
switch (c)
{
case -1: return RESETCOLOR; // HACK! :P
case 0 : return ANSI_BLACK;
case 1 : return ANSI_BLUE; // non-ANSI
case 2 : return ANSI_GREEN;
case 3 : return ANSI_CYAN; // non-ANSI
case 4 : return ANSI_RED; // non-ANSI
case 5 : return ANSI_MAGENTA;
case 6 : return ANSI_BROWN;
case 7 : return ANSI_GREY;
case 8 : return ANSI_DARKGREY;
case 9 : return ANSI_LIGHTBLUE; // non-ANSI
case 10: return ANSI_LIGHTGREEN;
case 11: return ANSI_LIGHTCYAN; // non-ANSI;
case 12: return ANSI_LIGHTRED; // non-ANSI;
case 13: return ANSI_LIGHTMAGENTA;
case 14: return ANSI_YELLOW; // non-ANSI
case 15: return ANSI_WHITE;
default: return "";
}
}


#ifdef HAVE_CUCHAR
// Use u32string for GCC 6 and later and msvc to allow potable implementation
using u32string = std::u32string;
Expand Down Expand Up @@ -330,10 +286,10 @@ namespace DFHack
void color(Console::color_value index)
{
if(!rawmode)
fprintf(dfout_C, "%s", getANSIColor(index));
fprintf(dfout_C, "%s", Console::getANSIColor(index));
else
{
const char * colstr = getANSIColor(index);
const char * colstr = Console::getANSIColor(index);
int lstr = strlen(colstr);
if (::write(STDIN_FILENO,colstr,lstr) == -1)
;
Expand Down Expand Up @@ -833,14 +789,14 @@ namespace DFHack
};
}

Console::Console()
PosixConsole::PosixConsole() : Console(this)
{
d = 0;
d = nullptr;
inited = false;
// we can't create the mutex at this time. the SDL functions aren't hooked yet.
wlock = new std::recursive_mutex();
}
Console::~Console()
PosixConsole::~PosixConsole()
{
assert(!inited);
if(wlock)
Expand All @@ -849,7 +805,7 @@ Console::~Console()
delete d;
}

bool Console::init(bool dont_redirect)
bool PosixConsole::init(bool dont_redirect)
{
d = new Private();
// make our own weird streams so our IO isn't redirected
Expand Down Expand Up @@ -882,7 +838,12 @@ bool Console::init(bool dont_redirect)
return true;
}

bool Console::shutdown(void)
bool PosixConsole::is_supported()
{
return !isUnsupportedTerm() && isatty(STDIN_FILENO);
}

bool PosixConsole::shutdown(void)
{
if(!d)
return true;
Expand All @@ -894,7 +855,7 @@ bool Console::shutdown(void)
return true;
}

void Console::begin_batch()
void PosixConsole::begin_batch()
{
//color_ostream::begin_batch();

Expand All @@ -904,22 +865,22 @@ void Console::begin_batch()
d->begin_batch();
}

void Console::end_batch()
void PosixConsole::end_batch()
{
if (inited)
d->end_batch();

wlock->unlock();
}

void Console::flush_proxy()
void PosixConsole::flush_proxy()
{
std::lock_guard<std::recursive_mutex> lock{*wlock};
if (inited)
d->flush();
}

void Console::add_text(color_value color, const std::string &text)
void PosixConsole::add_text(color_value color, const std::string &text)
{
std::lock_guard<std::recursive_mutex> lock{*wlock};
if (inited)
Expand All @@ -928,7 +889,7 @@ void Console::add_text(color_value color, const std::string &text)
fwrite(text.data(), 1, text.size(), stderr);
}

int Console::get_columns(void)
int PosixConsole::get_columns(void)
{
std::lock_guard<std::recursive_mutex> lock{*wlock};
int ret = Console::FAILURE;
Expand All @@ -937,7 +898,7 @@ int Console::get_columns(void)
return ret;
}

int Console::get_rows(void)
int PosixConsole::get_rows(void)
{
std::lock_guard<std::recursive_mutex> lock{*wlock};
int ret = Console::FAILURE;
Expand All @@ -946,28 +907,28 @@ int Console::get_rows(void)
return ret;
}

void Console::clear()
void PosixConsole::clear()
{
std::lock_guard<std::recursive_mutex> lock{*wlock};
if(inited)
d->clear();
}

void Console::gotoxy(int x, int y)
void PosixConsole::gotoxy(int x, int y)
{
std::lock_guard<std::recursive_mutex> lock{*wlock};
if(inited)
d->gotoxy(x,y);
}

void Console::cursor(bool enable)
void PosixConsole::cursor(bool enable)
{
std::lock_guard<std::recursive_mutex> lock{*wlock};
if(inited)
d->cursor(enable);
}

int Console::lineedit(const std::string & prompt, std::string & output, CommandHistory & ch)
int PosixConsole::lineedit(const std::string & prompt, std::string & output, CommandHistory & ch)
{
std::lock_guard<std::recursive_mutex> lock{*wlock};
int ret = Console::SHUTDOWN;
Expand All @@ -984,19 +945,13 @@ int Console::lineedit(const std::string & prompt, std::string & output, CommandH
return ret;
}

void Console::msleep (unsigned int msec)
{
if (msec > 1000) sleep(msec/1000000);
usleep((msec % 1000000) * 1000);
}

bool Console::hide()
bool PosixConsole::hide()
{
//Warmist: don't know if it's possible...
return false;
}

bool Console::show()
bool PosixConsole::show()
{
//Warmist: don't know if it's possible...
return false;
Expand Down
Loading
Loading