7
7
8
8
#include < QApplication>
9
9
10
+ #include " common/ratekeeper.h"
10
11
#include " common/util.h"
11
12
#include " common/version.h"
12
13
@@ -57,7 +58,7 @@ void add_str(WINDOW *w, const char *str, Color color = Color::Default, bool bold
57
58
58
59
} // namespace
59
60
60
- ConsoleUI::ConsoleUI (Replay *replay, QObject *parent ) : replay(replay), sm({" carState" , " liveParameters" }), QObject(parent ) {
61
+ ConsoleUI::ConsoleUI (Replay *replay) : replay(replay), sm({" carState" , " liveParameters" }) {
61
62
// Initialize curses
62
63
initscr ();
63
64
clear ();
@@ -80,24 +81,16 @@ ConsoleUI::ConsoleUI(Replay *replay, QObject *parent) : replay(replay), sm({"car
80
81
81
82
initWindows ();
82
83
83
- qRegisterMetaType<uint64_t >(" uint64_t" );
84
- qRegisterMetaType<ReplyMsgType>(" ReplyMsgType" );
85
84
installMessageHandler ([this ](ReplyMsgType type, const std::string msg) {
86
- emit logMessageSignal (type, QString::fromStdString (msg));
85
+ std::scoped_lock lock (mutex);
86
+ logs.emplace_back (type, msg);
87
87
});
88
88
installDownloadProgressHandler ([this ](uint64_t cur, uint64_t total, bool success) {
89
- emit updateProgressBarSignal (cur, total, success);
89
+ std::scoped_lock lock (mutex);
90
+ progress_cur = cur;
91
+ progress_total = total;
92
+ download_success = success;
90
93
});
91
-
92
- QObject::connect (replay, &Replay::streamStarted, this , &ConsoleUI::updateSummary);
93
- QObject::connect (¬ifier, SIGNAL (activated (int )), SLOT (readyRead ()));
94
- QObject::connect (this , &ConsoleUI::updateProgressBarSignal, this , &ConsoleUI::updateProgressBar);
95
- QObject::connect (this , &ConsoleUI::logMessageSignal, this , &ConsoleUI::logMessage);
96
-
97
- sm_timer.callOnTimeout (this , &ConsoleUI::updateStatus);
98
- sm_timer.start (100 );
99
- getch_timer.start (1000 , this );
100
- readyRead ();
101
94
}
102
95
103
96
ConsoleUI::~ConsoleUI () {
@@ -136,9 +129,7 @@ void ConsoleUI::initWindows() {
136
129
}
137
130
}
138
131
139
- void ConsoleUI::timerEvent (QTimerEvent *ev) {
140
- if (ev->timerId () != getch_timer.timerId ()) return ;
141
-
132
+ void ConsoleUI::updateSize () {
142
133
if (is_term_resized (max_height, max_width)) {
143
134
for (auto win : w) {
144
135
if (win) delwin (win);
@@ -149,7 +140,6 @@ void ConsoleUI::timerEvent(QTimerEvent *ev) {
149
140
initWindows ();
150
141
rWarning (" resize term %dx%d" , max_height, max_width);
151
142
}
152
- updateTimeline ();
153
143
}
154
144
155
145
void ConsoleUI::updateStatus () {
@@ -169,12 +159,6 @@ void ConsoleUI::updateStatus() {
169
159
170
160
sm.update (0 );
171
161
172
- if (status != Status::Paused) {
173
- auto events = replay->events ();
174
- uint64_t current_mono_time = replay->routeStartNanos () + replay->currentSeconds () * 1e9 ;
175
- bool playing = !events->empty () && events->back ().mono_time > current_mono_time;
176
- status = playing ? Status::Playing : Status::Waiting;
177
- }
178
162
auto [status_str, status_color] = status_text[status];
179
163
write_item (0 , 0 , " STATUS: " , status_str, " " , false , status_color);
180
164
std::string current_segment = " - " + std::to_string ((int )(replay->currentSeconds () / 60 ));
@@ -218,7 +202,7 @@ void ConsoleUI::displayTimelineDesc() {
218
202
}
219
203
}
220
204
221
- void ConsoleUI::logMessage (ReplyMsgType type, const QString &msg) {
205
+ void ConsoleUI::logMessage (ReplyMsgType type, const std::string &msg) {
222
206
if (auto win = w[Win::Log]) {
223
207
Color color = Color::Default;
224
208
if (type == ReplyMsgType::Debug) {
@@ -228,19 +212,19 @@ void ConsoleUI::logMessage(ReplyMsgType type, const QString &msg) {
228
212
} else if (type == ReplyMsgType::Critical) {
229
213
color = Color::Red;
230
214
}
231
- add_str (win, qPrintable (msg + " \n " ), color);
215
+ add_str (win, (msg + " \n " ). c_str ( ), color);
232
216
wrefresh (win);
233
217
}
234
218
}
235
219
236
- void ConsoleUI::updateProgressBar (uint64_t cur, uint64_t total, bool success ) {
220
+ void ConsoleUI::updateProgressBar () {
237
221
werase (w[Win::DownloadBar]);
238
- if (success && cur < total ) {
222
+ if (download_success && progress_cur < progress_total ) {
239
223
const int width = 35 ;
240
- const float progress = cur / (double )total ;
224
+ const float progress = progress_cur / (double )progress_total ;
241
225
const int pos = width * progress;
242
226
wprintw (w[Win::DownloadBar], " Downloading [%s>%s] %d%% %s" , std::string (pos, ' =' ).c_str (),
243
- std::string (width - pos, ' ' ).c_str (), int (progress * 100.0 ), formattedDataSize (total ).c_str ());
227
+ std::string (width - pos, ' ' ).c_str (), int (progress * 100.0 ), formattedDataSize (progress_total ).c_str ());
244
228
}
245
229
wrefresh (w[Win::DownloadBar]);
246
230
}
@@ -288,24 +272,16 @@ void ConsoleUI::updateTimeline() {
288
272
wrefresh (win);
289
273
}
290
274
291
- void ConsoleUI::readyRead () {
292
- int c;
293
- while ((c = getch ()) != ERR) {
294
- handleKey (c);
295
- }
296
- }
297
-
298
275
void ConsoleUI::pauseReplay (bool pause) {
299
276
replay->pause (pause);
300
- status = pause ? Status::Paused : Status::Waiting ;
277
+ status = pause ? Status::Paused : Status::Playing ;
301
278
}
302
279
303
280
void ConsoleUI::handleKey (char c) {
304
281
if (c == ' \n ' ) {
305
282
// pause the replay and blocking getchar()
306
283
pauseReplay (true );
307
284
updateStatus ();
308
- getch_timer.stop ();
309
285
curs_set (true );
310
286
nodelay (stdscr, false );
311
287
@@ -330,7 +306,6 @@ void ConsoleUI::handleKey(char c) {
330
306
nodelay (stdscr, true );
331
307
curs_set (false );
332
308
refresh ();
333
- getch_timer.start (1000 , this );
334
309
335
310
} else if (c == ' +' || c == ' =' ) {
336
311
auto it = std::upper_bound (speed_array.begin (), speed_array.end (), replay->getSpeed ());
@@ -367,7 +342,37 @@ void ConsoleUI::handleKey(char c) {
367
342
replay->seekTo (-10 , true );
368
343
} else if (c == ' ' ) {
369
344
pauseReplay (!replay->isPaused ());
370
- } else if (c == ' q' || c == ' Q' ) {
371
- qApp->exit ();
372
345
}
373
346
}
347
+
348
+ int ConsoleUI::exec () {
349
+ RateKeeper rk (" Replay" , 20 );
350
+ while (true ) {
351
+ int c = getch ();
352
+ if (c == ' q' || c == ' Q' ) {
353
+ break ;
354
+ }
355
+ handleKey (c);
356
+
357
+ if (rk.frame () % 25 ) {
358
+ updateSize ();
359
+ updateSummary ();
360
+ }
361
+
362
+ updateTimeline ();
363
+ updateStatus ();
364
+
365
+ {
366
+ std::scoped_lock lock (mutex);
367
+ updateProgressBar ();
368
+ for (auto &[type, msg] : logs) {
369
+ logMessage (type, msg);
370
+ }
371
+ logs.clear ();
372
+ }
373
+
374
+ qApp->processEvents ();
375
+ rk.keepTime ();
376
+ }
377
+ return 0 ;
378
+ }
0 commit comments