Skip to content

Commit c9ab1d5

Browse files
committed
sdlconsole: Console hierarchy things
1 parent dc7a79e commit c9ab1d5

10 files changed

Lines changed: 73 additions & 96 deletions

File tree

library/CMakeLists.txt

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

125125
if (NOT DFHACK_SDL_CONSOLE)
126126
if(WIN32)
127-
set(CONSOLE_SOURCES Console-windows.cpp)
127+
set(CONSOLE_SOURCES Console-windows.cpp Console.cpp)
128128
else()
129-
set(CONSOLE_SOURCES Console-posix.cpp)
129+
set(CONSOLE_SOURCES Console-posix.cpp Console.cpp)
130130
endif()
131131
set(DFCLIENT_CONSOLE_SOURCES ${CONSOLE_SOURCES})
132132
else()
133133
if(WIN32)
134-
set(DFCLIENT_CONSOLE_SOURCES Console-windows.cpp)
134+
set(DFCLIENT_CONSOLE_SOURCES Console-windows.cpp Console.cpp)
135135
else()
136-
set(DFCLIENT_CONSOLE_SOURCES Console-posix.cpp)
136+
set(DFCLIENT_CONSOLE_SOURCES Console-posix.cpp Console.cpp)
137137
endif()
138138
set(CONSOLE_SOURCES ${DFCLIENT_CONSOLE_SOURCES})
139139
list(APPEND CONSOLE_SOURCES Console-sdl.cpp SDLConsole_impl.cpp)

library/Console-posix.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7676
#endif
7777

7878
#include "PosixConsole.h"
79+
7980
#include "Hooks.h"
8081
using namespace DFHack;
8182

@@ -833,7 +834,7 @@ namespace DFHack
833834
};
834835
}
835836

836-
PosixConsole::PosixConsole()
837+
PosixConsole::PosixConsole() : Console(Console::Type::Posix)
837838
{
838839
d = nullptr;
839840
inited = false;

library/Console-sdl.cpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ namespace DFHack
232232
};
233233
}
234234

235-
SDLConsoleDriver::SDLConsoleDriver()
235+
SDLConsoleDriver::SDLConsoleDriver() : Console(Console::Type::SDL)
236236
{
237237
d = new Private();
238238
inited.store(false);
@@ -254,18 +254,8 @@ SDLConsoleDriver::~SDLConsoleDriver()
254254
*/
255255
bool SDLConsoleDriver::init(bool dont_redirect)
256256
{
257-
static int init_stage = 0;
258-
259-
if (init_stage == -1)
260-
return false;
261-
262-
if (init_stage == 1) {
257+
if (inited)
263258
INTERPOSE_HOOK(con_render_hook,render).apply(true);
264-
return true;
265-
}
266-
267-
inited.store(d->con.init());
268-
init_stage = inited.load() ? 1 : -1;
269259

270260
if (!dont_redirect)
271261
{
@@ -276,6 +266,12 @@ bool SDLConsoleDriver::init(bool dont_redirect)
276266
return inited.load();
277267
}
278268

269+
bool SDLConsoleDriver::init_sdl()
270+
{
271+
inited.store(d->con.init());
272+
return inited.load();
273+
}
274+
279275
bool SDLConsoleDriver::shutdown(void)
280276
{
281277
if (!inited.load()) return true;
@@ -376,7 +372,7 @@ bool SDLConsoleDriver::show()
376372
bool SDLConsoleDriver::sdl_event_hook(SDL_Event &e)
377373
{
378374
auto& con = d->con;
379-
if (con.state.is_active()) {
375+
if (con.state.is_active()) [[likely]] {
380376
return con.sdl_event_hook(e);
381377
} else if (con.state.is_shutdown()) {
382378
cleanup();

library/Console-windows.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ namespace DFHack
409409
}
410410

411411

412-
WindowsConsole::WindowsConsole()
412+
WindowsConsole::WindowsConsole() : Console(Console::Type::Windows)
413413
{
414414
d = 0;
415415
wlock = 0;

library/Console.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "Console.h"
2+
#include "PosixConsole.h"
3+
#include "WindowsConsole.h"
4+
#include "SDLConsoleDriver.h"
5+
6+
using namespace DFHack;
7+
8+
std::unique_ptr<Console> Console::makeConsole() {
9+
10+
// Return the Windows console
11+
#if defined(_WIN32) || defined(_WIN64)
12+
return std::make_unique<WindowsConsole>();
13+
14+
// Return Posix console if launched from a supported terminal
15+
#elif defined(__linux__) || defined(__unix__) || defined(__APPLE__)
16+
if (PosixConsole::is_supported())
17+
return std::make_unique<PosixConsole>();
18+
#endif
19+
20+
// Return the SDL console if not launched from dfhack_run
21+
#ifndef DISABLE_SDL_CONSOLE
22+
return std::make_unique<SDLConsoleDriver>();
23+
#endif
24+
25+
// Return the DUMMY do nothing console
26+
// Should not get here when the SDL console is available
27+
return std::make_unique<Console>();
28+
}

library/Core.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ distribution.
3030
#include "DataDefs.h"
3131
#include "Debug.h"
3232
#include "Console.h"
33+
#include "SDLConsoleDriver.h"
3334
#include "MiscUtils.h"
3435
#include "Module.h"
3536
#include "VersionInfoFactory.h"
@@ -1700,9 +1701,9 @@ bool Core::InitMainThread() {
17001701

17011702
perf_counters.reset();
17021703

1703-
// SDL_CONSOLE FIXME
1704-
if(con.init(false))
1705-
std::cerr << "Console init(first).\n";
1704+
if (con.get_type() == Console::Type::SDL
1705+
&& !con.as<SDLConsoleDriver>().init_sdl())
1706+
std::cerr << "SDLConsole: failed to initialize.\n";
17061707

17071708
return true;
17081709
}
@@ -2518,7 +2519,9 @@ void Core::setArmokTools(const std::vector<std::string> &tool_names) {
25182519
// returns true if the event is handled
25192520
bool Core::DFH_SDL_Event(SDL_Event* ev) {
25202521
uint32_t start_ms = p->getTickCount();
2521-
if (getConsole().sdl_event_hook(*ev)) return true;
2522+
if (con.get_type() == Console::Type::SDL
2523+
&& con.as<SDLConsoleDriver>().sdl_event_hook(*ev))
2524+
return true;
25222525
bool ret = doSdlInputEvent(ev);
25232526
perf_counters.incCounter(perf_counters.total_keybinding_ms, start_ms);
25242527
return ret;

library/include/Console.h

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ distribution.
2323
*/
2424

2525
#pragma once
26+
2627
#include "Export.h"
2728
#include "ColorText.h"
2829
#include <deque>
@@ -31,13 +32,8 @@ distribution.
3132
#include <iostream>
3233
#include <string>
3334
#include <vector>
34-
#include <algorithm>
35-
#include <functional>
36-
#include <map>
3735
#include <memory>
3836

39-
union SDL_Event;
40-
4137
namespace DFHack
4238
{
4339
class CommandHistory
@@ -126,38 +122,17 @@ namespace DFHack
126122

127123
class DFHACK_EXPORT Console : public color_ostream
128124
{
129-
protected:
130-
using FactoryFunc = std::function<std::unique_ptr<Console>()>;
131-
struct RegistryEntry {
132-
FactoryFunc func;
133-
int priority;
125+
public:
126+
enum class Type {
127+
Posix,
128+
SDL,
129+
Windows,
130+
DUMMY
134131
};
135132

136-
static void registerConsole(const std::string &name, FactoryFunc func, int priority) {
137-
getRegistry().emplace(name, RegistryEntry{func, priority});
138-
}
139-
private:
140-
static std::map<std::string, RegistryEntry> &getRegistry() {
141-
static std::map<std::string, RegistryEntry> registry;
142-
return registry;
143-
}
144-
145-
static auto getAvailableConsoles() {
146-
std::vector<std::pair<std::string, RegistryEntry>> result;
147-
auto& registry = getRegistry();
148-
149-
for (const auto &entry : registry) {
150-
result.emplace_back(entry.first, entry.second);
151-
}
152-
153-
std::sort(result.begin(), result.end(), [](const auto& a, const auto& b) {
154-
return a.second.priority > b.second.priority;
155-
});
156-
157-
return result;
158-
}
159-
160133
protected:
134+
Type con_type{Type::DUMMY};
135+
161136
virtual void begin_batch() {};
162137
virtual void add_text(color_value color, const std::string &text) {};
163138
virtual void end_batch() {};
@@ -166,6 +141,7 @@ namespace DFHack
166141
public:
167142
///ctor, NOT thread-safe
168143
Console() = default;
144+
Console(Type type) : con_type(type) {}
169145
///dtor, NOT thread-safe
170146
virtual ~Console() = default;
171147
/// initialize the console. NOT thread-safe
@@ -202,15 +178,14 @@ namespace DFHack
202178
virtual bool hide() { return false; };
203179
virtual bool show() { return false; };
204180

205-
virtual bool sdl_event_hook(SDL_Event& event) { return false; };
206181
virtual void cleanup() {};
182+
Type get_type() const { return con_type; }
207183

208-
static std::unique_ptr<Console> makeConsole() {
209-
auto availableConsoles = getAvailableConsoles();
210-
if (!availableConsoles.empty()) {
211-
return availableConsoles[0].second.func();
212-
}
213-
return std::make_unique<Console>();
184+
template <typename T>
185+
T& as() {
186+
return static_cast<T&>(*this);
214187
}
188+
189+
static std::unique_ptr<Console> makeConsole();
215190
};
216191
}

library/include/PosixConsole.h

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ distribution.
3030
#include <mutex>
3131
#include <string>
3232

33-
union SDL_Event;
34-
3533
namespace DFHack
3634
{
3735
class Private;
@@ -43,8 +41,6 @@ namespace DFHack
4341
void end_batch() override;
4442
void flush_proxy() override;
4543
public:
46-
static bool is_supported();
47-
4844
PosixConsole();
4945
~PosixConsole();
5046

@@ -68,17 +64,12 @@ namespace DFHack
6864
bool hide() override;
6965
bool show() override;
7066

67+
static bool is_supported();
68+
7169
private:
7270
Private * d;
7371
std::recursive_mutex * wlock;
7472
std::atomic<bool> inited;
75-
76-
static const inline bool registered = [] {
77-
if (is_supported()) {
78-
registerConsole("PosixConsole", [] { return std::make_unique<PosixConsole>(); }, 100);
79-
return true;
80-
}
81-
return false;
82-
}();
8373
};
74+
8475
}

library/include/SDLConsoleDriver.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,12 @@ namespace DFHack
7777
bool hide() override;
7878
bool show() override;
7979

80-
bool sdl_event_hook(SDL_Event& event) override;
80+
bool sdl_event_hook(SDL_Event& event);
81+
bool init_sdl();
8182
void cleanup() override;
8283
private:
8384
Private * d;
8485
std::recursive_mutex * wlock;
8586
std::atomic<bool> inited;
86-
87-
static const inline bool registered = [] {
88-
if (is_supported()) {
89-
registerConsole("SDLConsole", [] { return std::make_unique<SDLConsoleDriver>(); }, 0);
90-
return true;
91-
}
92-
return false;
93-
}();
9487
};
9588
}

library/include/WindowsConsole.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ distribution.
3131
#include <string>
3232
#include <memory>
3333

34-
union SDL_Event;
35-
3634
namespace DFHack
3735
{
3836
class Private;
@@ -73,13 +71,5 @@ namespace DFHack
7371
Private * d;
7472
std::recursive_mutex * wlock;
7573
std::atomic<bool> inited;
76-
77-
static const inline bool registered = [] {
78-
if (is_supported()) {
79-
registerConsole("WindowsConsole", [] { return std::make_unique<WindowsConsole>(); }, 100);
80-
return true;
81-
}
82-
return false;
83-
}();
8474
};
8575
}

0 commit comments

Comments
 (0)