OXYGENHUB
◆ main openmsx / src / events / TclCallbackMessages.cc
1.7 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
e8555c89 Andy McCall 1
#include "TclCallbackMessages.hh"
2
3
#include "GlobalCliComm.hh"
4
5
namespace openmsx {
6
7
TclCallbackMessages::TclCallbackMessages(GlobalCliComm& cliComm_,
8
                                         CommandController& controller)
9
	: cliComm(cliComm_)
10
	, messageCallback(
11
		controller, "message_callback",
12
		"Tcl proc called when a new message is available",
13
		"",
14
		Setting::Save::YES, // the user must be able to override
15
		true) // this is a message callback (so the TclCallback must prevent recursion)
16
{
17
	cliComm.addListener(std::unique_ptr<CliListener>(this)); // wrap in unique_ptr
18
}
19
20
TclCallbackMessages::~TclCallbackMessages()
21
{
22
	std::unique_ptr<CliListener> ptr = cliComm.removeListener(*this);
23
	(void)ptr.release();
24
}
25
26
void TclCallbackMessages::log(CliComm::LogLevel level, std::string_view message, float fraction) noexcept
27
{
28
	// TODO Possibly remove this?  No longer needed now that ImGui displays messages?
29
	try {
30
		if (level == CliComm::LogLevel::PROGRESS && fraction >= 0.0f) {
31
			messageCallback.execute(tmpStrCat(message, "... ", int(100.0f * fraction), '%'),
32
			                        toString(level));
33
		} else {
34
			messageCallback.execute(message, toString(level));
35
		}
36
	} catch (TclObject& command) {
37
		// Command for this message could not be executed yet.
38
		// Buffer until we can redo them.
39
		postponedCommands.push_back(command);
40
	}
41
}
42
43
void TclCallbackMessages::update(
44
	CliComm::UpdateType /*type*/, std::string_view /*machine*/,
45
	std::string_view /*name*/, std::string_view /*value*/) noexcept
46
{
47
	// ignore
48
}
49
50
void TclCallbackMessages::redoPostponedCallbacks()
51
{
52
	for (auto& command: postponedCommands) {
53
		messageCallback.executeCommon(command);
54
	}
55
	postponedCommands.clear();
56
}
57
58
} // namespace openmsx