Skip to content

Commit 2d35963

Browse files
committed
sdlconsole: Console overhaul. Hacks.
1 parent 293d203 commit 2d35963

12 files changed

Lines changed: 404 additions & 95 deletions

library/CMakeLists.txt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ set(MAIN_SOURCES
119119
file(GLOB_RECURSE TEST_SOURCES
120120
LIST_DIRECTORIES false
121121
*test.cpp)
122-
list(APPEND TEST_SOURCES SDLConsole_impl.cpp)
122+
list(APPEND TEST_SOURCES Console-posix.cpp SDLConsole_impl.cpp)
123123
dfhack_test(dfhack-test "${TEST_SOURCES}")
124124

125125
if (NOT DFHACK_SDL_CONSOLE)
@@ -135,7 +135,8 @@ else()
135135
else()
136136
set(DFCLIENT_CONSOLE_SOURCES Console-posix.cpp)
137137
endif()
138-
set(CONSOLE_SOURCES Console-sdl.cpp SDLConsole_impl.cpp)
138+
set(CONSOLE_SOURCES ${DFCLIENT_CONSOLE_SOURCES})
139+
list(APPEND CONSOLE_SOURCES Console-sdl.cpp SDLConsole_impl.cpp)
139140
endif()
140141

141142
set(MAIN_SOURCES_WINDOWS
@@ -386,6 +387,8 @@ add_dependencies(dfhack generate_proto_core)
386387
add_dependencies(dfhack generate_headers)
387388

388389
add_library(dfhack-client SHARED RemoteClient.cpp ColorText.cpp MiscUtils.cpp Error.cpp ${PROJECT_PROTO_SRCS} ${DFCLIENT_CONSOLE_SOURCES})
390+
# SDLConsole requires to be run with df
391+
target_compile_definitions(dfhack-client PUBLIC DISABLE_SDL_CONSOLE)
389392
add_dependencies(dfhack-client dfhack)
390393

391394
add_executable(dfhack-run dfhack-run.cpp)
@@ -433,10 +436,6 @@ set_target_properties(dfhack PROPERTIES INTERFACE_LINK_LIBRARIES "")
433436
target_link_libraries(dfhack-client protobuf-lite clsocket jsoncpp_static)
434437
target_link_libraries(dfhack-run dfhack-client)
435438

436-
# FIXME: to remove
437-
#target_link_libraries(dfhack SDL2_image)
438-
#target_link_libraries(dfhack-client SDL2_image)
439-
440439
if(APPLE)
441440
add_custom_command(TARGET dfhack-run COMMAND ${dfhack_SOURCE_DIR}/package/darwin/fix-libs.sh WORKING_DIRECTORY ../ COMMENT "Fixing library dependencies...")
442441
endif()

library/Console-posix.cpp

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7575
_res; })
7676
#endif
7777

78-
#include "Console.h"
78+
#include "PosixConsole.h"
7979
#include "Hooks.h"
8080
using namespace DFHack;
8181

@@ -833,14 +833,14 @@ namespace DFHack
833833
};
834834
}
835835

836-
Console::Console()
836+
PosixConsole::PosixConsole()
837837
{
838-
d = 0;
838+
d = nullptr;
839839
inited = false;
840840
// we can't create the mutex at this time. the SDL functions aren't hooked yet.
841841
wlock = new std::recursive_mutex();
842842
}
843-
Console::~Console()
843+
PosixConsole::~PosixConsole()
844844
{
845845
assert(!inited);
846846
if(wlock)
@@ -849,7 +849,7 @@ Console::~Console()
849849
delete d;
850850
}
851851

852-
bool Console::init(bool dont_redirect)
852+
bool PosixConsole::init(bool dont_redirect)
853853
{
854854
d = new Private();
855855
// make our own weird streams so our IO isn't redirected
@@ -882,7 +882,12 @@ bool Console::init(bool dont_redirect)
882882
return true;
883883
}
884884

885-
bool Console::shutdown(void)
885+
bool PosixConsole::is_supported()
886+
{
887+
return !isUnsupportedTerm() && isatty(STDIN_FILENO);
888+
}
889+
890+
bool PosixConsole::shutdown(void)
886891
{
887892
if(!d)
888893
return true;
@@ -894,7 +899,7 @@ bool Console::shutdown(void)
894899
return true;
895900
}
896901

897-
void Console::begin_batch()
902+
void PosixConsole::begin_batch()
898903
{
899904
//color_ostream::begin_batch();
900905

@@ -904,22 +909,22 @@ void Console::begin_batch()
904909
d->begin_batch();
905910
}
906911

907-
void Console::end_batch()
912+
void PosixConsole::end_batch()
908913
{
909914
if (inited)
910915
d->end_batch();
911916

912917
wlock->unlock();
913918
}
914919

915-
void Console::flush_proxy()
920+
void PosixConsole::flush_proxy()
916921
{
917922
std::lock_guard<std::recursive_mutex> lock{*wlock};
918923
if (inited)
919924
d->flush();
920925
}
921926

922-
void Console::add_text(color_value color, const std::string &text)
927+
void PosixConsole::add_text(color_value color, const std::string &text)
923928
{
924929
std::lock_guard<std::recursive_mutex> lock{*wlock};
925930
if (inited)
@@ -928,7 +933,7 @@ void Console::add_text(color_value color, const std::string &text)
928933
fwrite(text.data(), 1, text.size(), stderr);
929934
}
930935

931-
int Console::get_columns(void)
936+
int PosixConsole::get_columns(void)
932937
{
933938
std::lock_guard<std::recursive_mutex> lock{*wlock};
934939
int ret = Console::FAILURE;
@@ -937,7 +942,7 @@ int Console::get_columns(void)
937942
return ret;
938943
}
939944

940-
int Console::get_rows(void)
945+
int PosixConsole::get_rows(void)
941946
{
942947
std::lock_guard<std::recursive_mutex> lock{*wlock};
943948
int ret = Console::FAILURE;
@@ -946,28 +951,28 @@ int Console::get_rows(void)
946951
return ret;
947952
}
948953

949-
void Console::clear()
954+
void PosixConsole::clear()
950955
{
951956
std::lock_guard<std::recursive_mutex> lock{*wlock};
952957
if(inited)
953958
d->clear();
954959
}
955960

956-
void Console::gotoxy(int x, int y)
961+
void PosixConsole::gotoxy(int x, int y)
957962
{
958963
std::lock_guard<std::recursive_mutex> lock{*wlock};
959964
if(inited)
960965
d->gotoxy(x,y);
961966
}
962967

963-
void Console::cursor(bool enable)
968+
void PosixConsole::cursor(bool enable)
964969
{
965970
std::lock_guard<std::recursive_mutex> lock{*wlock};
966971
if(inited)
967972
d->cursor(enable);
968973
}
969974

970-
int Console::lineedit(const std::string & prompt, std::string & output, CommandHistory & ch)
975+
int PosixConsole::lineedit(const std::string & prompt, std::string & output, CommandHistory & ch)
971976
{
972977
std::lock_guard<std::recursive_mutex> lock{*wlock};
973978
int ret = Console::SHUTDOWN;
@@ -984,19 +989,19 @@ int Console::lineedit(const std::string & prompt, std::string & output, CommandH
984989
return ret;
985990
}
986991

987-
void Console::msleep (unsigned int msec)
992+
void PosixConsole::msleep (unsigned int msec)
988993
{
989994
if (msec > 1000) sleep(msec/1000000);
990995
usleep((msec % 1000000) * 1000);
991996
}
992997

993-
bool Console::hide()
998+
bool PosixConsole::hide()
994999
{
9951000
//Warmist: don't know if it's possible...
9961001
return false;
9971002
}
9981003

999-
bool Console::show()
1004+
bool PosixConsole::show()
10001005
{
10011006
//Warmist: don't know if it's possible...
10021007
return false;

library/Console-sdl.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5353
#include "df/renderer_2d.h"
5454
#include <VTableInterpose.h>
5555

56-
#include "Console.h"
56+
#include "SDLConsoleDriver.h"
5757
#include "SDLConsole.h"
5858

5959
using namespace DFHack;
@@ -232,14 +232,14 @@ namespace DFHack
232232
};
233233
}
234234

235-
Console::Console()
235+
SDLConsoleDriver::SDLConsoleDriver()
236236
{
237237
d = new Private();
238238
inited.store(false);
239239
// we can't create the mutex at this time. the SDL functions aren't hooked yet.
240240
wlock = new std::recursive_mutex();
241241
}
242-
Console::~Console()
242+
SDLConsoleDriver::~SDLConsoleDriver()
243243
{
244244
assert(!inited);
245245
if(wlock)
@@ -252,7 +252,7 @@ Console::~Console()
252252
* FIXME: Two-stage init because we need to initialize on
253253
* the main thread, but interpose isn't available until later.
254254
*/
255-
bool Console::init(bool dont_redirect)
255+
bool SDLConsoleDriver::init(bool dont_redirect)
256256
{
257257
static int init_stage = 0;
258258

@@ -276,7 +276,7 @@ bool Console::init(bool dont_redirect)
276276
return inited.load();
277277
}
278278

279-
bool Console::shutdown(void)
279+
bool SDLConsoleDriver::shutdown(void)
280280
{
281281
if (!inited.load()) return true;
282282
d->con.shutdown();
@@ -288,22 +288,22 @@ bool Console::shutdown(void)
288288
* This should be for guarding against interleaving prints to the console.
289289
* The begin_batch() and end_batch() pair does the job on its own.
290290
*/
291-
void Console::begin_batch()
291+
void SDLConsoleDriver::begin_batch()
292292
{
293293
wlock->lock();
294294
}
295295

296-
void Console::end_batch()
296+
void SDLConsoleDriver::end_batch()
297297
{
298298
wlock->unlock();
299299
}
300300

301301
/* Don't think we need this? */
302-
void Console::flush_proxy()
302+
void SDLConsoleDriver::flush_proxy()
303303
{
304304
}
305305

306-
void Console::add_text(color_value color, const std::string &text)
306+
void SDLConsoleDriver::add_text(color_value color, const std::string &text)
307307
{
308308
// I don't think this lock is needed, unless to prevent
309309
// interleaving prints. But we have batch for that?
@@ -314,33 +314,33 @@ void Console::add_text(color_value color, const std::string &text)
314314
fwrite(text.data(), 1, text.size(), stderr);
315315
}
316316

317-
int Console::get_columns(void)
317+
int SDLConsoleDriver::get_columns(void)
318318
{
319319
// returns Console::FAILURE if inactive
320320
return d->con.get_columns();
321321
}
322322

323-
int Console::get_rows(void)
323+
int SDLConsoleDriver::get_rows(void)
324324
{
325325
// returns Console::FAILURE if inactive
326326
return d->con.get_rows();
327327
}
328328

329-
void Console::clear()
329+
void SDLConsoleDriver::clear()
330330
{
331331
d->con.clear();
332332
}
333333
/* XXX: Not implemented */
334-
void Console::gotoxy(int x, int y)
334+
void SDLConsoleDriver::gotoxy(int x, int y)
335335
{
336336
}
337337
/* XXX: Not implemented */
338-
void Console::cursor(bool enable)
338+
void SDLConsoleDriver::cursor(bool enable)
339339
{
340340
d->cursor(enable);
341341
}
342342

343-
int Console::lineedit(const std::string & prompt, std::string & output, CommandHistory & ch)
343+
int SDLConsoleDriver::lineedit(const std::string & prompt, std::string & output, CommandHistory & ch)
344344
{
345345
// Tell fiothread we are done.
346346
if(!inited.load())
@@ -349,18 +349,18 @@ int Console::lineedit(const std::string & prompt, std::string & output, CommandH
349349
return d->lineedit(prompt,output,ch);
350350
}
351351

352-
void Console::msleep (unsigned int msec)
352+
void SDLConsoleDriver::msleep (unsigned int msec)
353353
{
354354
std::this_thread::sleep_for(std::chrono::milliseconds(msec));
355355
}
356356

357-
bool Console::hide()
357+
bool SDLConsoleDriver::hide()
358358
{
359359
d->con.hide_window();
360360
return true;
361361
}
362362

363-
bool Console::show()
363+
bool SDLConsoleDriver::show()
364364
{
365365
d->con.show_window();
366366
return true;
@@ -373,7 +373,7 @@ bool Console::show()
373373
* NOTE: We do not absolutely have to clean up here. It can be done at exit.
374374
* This is for the ability to shutdown and restart the console at run time.
375375
*/
376-
bool Console::sdl_event_hook(SDL_Event &e)
376+
bool SDLConsoleDriver::sdl_event_hook(SDL_Event &e)
377377
{
378378
auto& con = d->con;
379379
if (con.state.is_active()) {
@@ -388,7 +388,7 @@ bool Console::sdl_event_hook(SDL_Event &e)
388388
* Cleanup must be done from the main thread.
389389
* NOTE: may be able to do this in the destructor instead.
390390
*/
391-
void Console::cleanup()
391+
void SDLConsoleDriver::cleanup()
392392
{
393393
INTERPOSE_HOOK(con_render_hook,render).apply(false);
394394
// destroy() will change console's state to inactive

0 commit comments

Comments
 (0)