-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole_window.cpp
More file actions
281 lines (248 loc) · 8.89 KB
/
console_window.cpp
File metadata and controls
281 lines (248 loc) · 8.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// In-emulator UART console.
//
// A second SDL window backed by its own ImGui context. The main window
// toggles it via the C key. The window:
//
// 1. Lets you pick which Tegra UART port to inspect (UART_A..UART_E).
// 2. Scrolls the selected port's TX history (every byte the payload
// writes to that port's THR is appended to EmuState::uart_tx_log[idx]
// by the mmio dispatcher).
// 3. Lets you type bytes that get pushed into EmuState::uart_rx_fifo[idx]
// so the next CPU-side `uart_recv()` returns them. There's a "Send"
// button plus shortcut buttons for the keys hwtest's pager listens
// for (n / p / r / s / q).
//
// All TX append / RX pop is single-threaded — both the CPU emulation and
// ImGui rendering run on the main thread between SDL polls — so no
// locking. The TX log is trimmed if it grows past ~64 KB to keep imgui
// rendering snappy.
#include "console_window.h"
#include "emu_state.h"
#include "imgui.h"
#include "backends/imgui_impl_sdl2.h"
#include "backends/imgui_impl_sdlrenderer2.h"
#include <cstdio>
#include <cstring>
namespace {
SDL_Window *g_window = nullptr;
SDL_Renderer *g_renderer = nullptr;
ImGuiContext *g_imgui = nullptr;
bool g_visible = false;
Uint32 g_window_id = 0;
bool g_sdl2_inited = false;
bool g_renderer_inited = false;
// Currently-displayed UART index (0..4).
int g_port = 1; // UART_B by default — that's hwtest's debug port.
// Send-line buffer. Cleared on submit.
char g_send_buf[256] = {0};
const char *kPortNames[] = {"UART_A", "UART_B", "UART_C", "UART_D", "UART_E"};
void inject_bytes(EmuState *state, const uint8_t *data, size_t n)
{
if (g_port < 0 || g_port >= (int)EmuState::N_UARTS) return;
auto &fifo = state->uart_rx_fifo[g_port];
for (size_t i = 0; i < n; i++)
fifo.push_back(data[i]);
}
void inject_str(EmuState *state, const char *s)
{
inject_bytes(state, (const uint8_t *)s, strlen(s));
}
void build_ui(EmuState *state)
{
int win_w = 0, win_h = 0;
SDL_GetWindowSize(g_window, &win_w, &win_h);
ImGui::SetNextWindowPos(ImVec2(0, 0));
ImGui::SetNextWindowSize(ImVec2((float)win_w, (float)win_h));
ImGuiWindowFlags flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize |
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoCollapse |
ImGuiWindowFlags_NoBringToFrontOnFocus;
ImGui::Begin("##console", nullptr, flags);
// --- Top row: port selector + clear button ---
ImGui::SetNextItemWidth(140);
ImGui::Combo("Port", &g_port, kPortNames, IM_ARRAYSIZE(kPortNames));
ImGui::SameLine();
if (ImGui::Button("Clear")) {
state->uart_tx_log[g_port].clear();
}
ImGui::SameLine();
ImGui::Text("rx fifo: %d byte(s)", (int)state->uart_rx_fifo[g_port].size());
// --- Scrolling output area ---
ImGui::Separator();
{
const float input_h = ImGui::GetFrameHeightWithSpacing() * 3 + 4;
ImGui::BeginChild("scroll", ImVec2(0, -input_h), true,
ImGuiWindowFlags_HorizontalScrollbar);
const std::string &log = state->uart_tx_log[g_port];
if (!log.empty()) {
ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(0xCC, 0xCC, 0xCC, 0xFF));
ImGui::TextUnformatted(log.data(), log.data() + log.size());
ImGui::PopStyleColor();
}
// Auto-scroll to bottom unless the user has scrolled up manually.
if (ImGui::GetScrollY() >= ImGui::GetScrollMaxY() - 1.0f)
ImGui::SetScrollHereY(1.0f);
ImGui::EndChild();
}
// --- Send line + shortcuts ---
ImGui::Separator();
bool focus_input = false;
if (ImGui::InputText("##send", g_send_buf, sizeof(g_send_buf),
ImGuiInputTextFlags_EnterReturnsTrue)) {
if (g_send_buf[0]) {
inject_str(state, g_send_buf);
g_send_buf[0] = 0;
}
focus_input = true;
}
ImGui::SameLine();
if (ImGui::Button("Send")) {
if (g_send_buf[0]) {
inject_str(state, g_send_buf);
g_send_buf[0] = 0;
}
focus_input = true;
}
ImGui::SameLine();
if (ImGui::Button("+ LF")) {
if (g_send_buf[0]) {
inject_str(state, g_send_buf);
g_send_buf[0] = 0;
}
const uint8_t lf = '\n';
inject_bytes(state, &lf, 1);
focus_input = true;
}
// Shortcut row — hwtest's pager keys.
ImGui::TextDisabled("Shortcuts:");
ImGui::SameLine();
static const struct { const char *label; char key; } kShortcuts[] = {
{"n (next)", 'n'},
{"p (prev)", 'p'},
{"r (refresh)", 'r'},
{"s (save)", 's'},
{"q (off)", 'q'},
{"CR", '\r'},
{"LF", '\n'},
};
for (size_t i = 0; i < sizeof(kShortcuts) / sizeof(kShortcuts[0]); i++) {
if (i > 0) ImGui::SameLine();
if (ImGui::Button(kShortcuts[i].label)) {
uint8_t b = (uint8_t)kShortcuts[i].key;
inject_bytes(state, &b, 1);
}
}
if (focus_input)
ImGui::SetKeyboardFocusHere(-1);
ImGui::End();
}
} // namespace
bool console_window_init()
{
g_window = SDL_CreateWindow("rcm_emu - UART Console",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
720, 540,
SDL_WINDOW_HIDDEN | SDL_WINDOW_RESIZABLE);
if (!g_window) {
fprintf(stderr, "[console_window] SDL_CreateWindow failed: %s\n", SDL_GetError());
return false;
}
g_renderer = SDL_CreateRenderer(g_window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (!g_renderer) {
fprintf(stderr, "[console_window] SDL_CreateRenderer failed: %s\n", SDL_GetError());
SDL_DestroyWindow(g_window);
g_window = nullptr;
return false;
}
IMGUI_CHECKVERSION();
g_imgui = ImGui::CreateContext();
ImGui::SetCurrentContext(g_imgui);
ImGui::StyleColorsDark();
ImGuiIO &io = ImGui::GetIO();
io.IniFilename = nullptr; // don't write a sibling imgui.ini
if (!ImGui_ImplSDL2_InitForSDLRenderer(g_window, g_renderer)) {
fprintf(stderr, "[console_window] ImGui SDL2 backend init failed\n");
ImGui::DestroyContext(g_imgui);
g_imgui = nullptr;
return false;
}
g_sdl2_inited = true;
if (!ImGui_ImplSDLRenderer2_Init(g_renderer)) {
fprintf(stderr, "[console_window] ImGui SDLRenderer2 backend init failed\n");
ImGui_ImplSDL2_Shutdown();
g_sdl2_inited = false;
ImGui::DestroyContext(g_imgui);
g_imgui = nullptr;
return false;
}
g_renderer_inited = true;
g_window_id = SDL_GetWindowID(g_window);
return true;
}
void console_window_shutdown()
{
if (g_imgui) {
ImGui::SetCurrentContext(g_imgui);
// Backend Shutdown asserts on `bd != nullptr`. Check the per-context
// backend pointers directly so we never call Shutdown on a context
// whose backend data was somehow already cleared (defensive — this
// handles the case where another ImGui context-switch elsewhere
// perturbed the global state).
ImGuiIO &io = ImGui::GetIO();
if (io.BackendRendererUserData) {
ImGui_ImplSDLRenderer2_Shutdown();
}
if (io.BackendPlatformUserData) {
ImGui_ImplSDL2_Shutdown();
}
g_renderer_inited = false;
g_sdl2_inited = false;
ImGui::DestroyContext(g_imgui);
g_imgui = nullptr;
}
if (g_renderer) {
SDL_DestroyRenderer(g_renderer);
g_renderer = nullptr;
}
if (g_window) {
SDL_DestroyWindow(g_window);
g_window = nullptr;
}
g_window_id = 0;
}
bool console_window_handle_event(const SDL_Event &ev)
{
if (!g_imgui) return false;
ImGui::SetCurrentContext(g_imgui);
ImGui_ImplSDL2_ProcessEvent(&ev);
// Hide on close.
if (ev.type == SDL_WINDOWEVENT &&
ev.window.windowID == g_window_id &&
ev.window.event == SDL_WINDOWEVENT_CLOSE) {
SDL_HideWindow(g_window);
g_visible = false;
}
return true;
}
void console_window_toggle()
{
if (!g_window) return;
g_visible = !g_visible;
if (g_visible) SDL_ShowWindow(g_window);
else SDL_HideWindow(g_window);
}
bool console_window_is_visible() { return g_visible; }
void console_window_render(EmuState *state)
{
if (!g_visible || !g_imgui) return;
ImGui::SetCurrentContext(g_imgui);
SDL_SetRenderDrawColor(g_renderer, 0x10, 0x10, 0x14, 0xFF);
SDL_RenderClear(g_renderer);
ImGui_ImplSDLRenderer2_NewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();
build_ui(state);
ImGui::Render();
ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData(), g_renderer);
SDL_RenderPresent(g_renderer);
}
Uint32 console_window_id() { return g_window_id; }