-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_linux.c
More file actions
1056 lines (924 loc) · 43.1 KB
/
app_linux.c
File metadata and controls
1056 lines (924 loc) · 43.1 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* app_linux.c - Linux Native Application (GTK3 + Cairo)
*
* Full-featured Linux port of the KMBox Trace Analyzer with:
* - GTK3 window with dark instrumentation aesthetic
* - Cairo trace rendering with speed coloring
* - POSIX serial port (/dev/ttyUSB*, /dev/ttyACM*) support
* - clock_gettime high-res timing
* - Pan/zoom canvas, sidebar with stats
*
* Build:
* gcc -O2 -o kmbox_tester app_linux.c app_common.c protocols.c \
* $(pkg-config --cflags --libs gtk+-3.0) -lm -lpthread
*/
#define _GNU_SOURCE
#include <gtk/gtk.h>
#include <cairo.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <math.h>
#include <signal.h>
#include <dirent.h>
#include "app_common.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
// ============================================================================
// Platform Implementation: Timing
// ============================================================================
static struct timespec g_time_start;
void plat_time_init(void) {
clock_gettime(CLOCK_MONOTONIC, &g_time_start);
}
uint64_t plat_time_us(void) {
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
uint64_t sec = (uint64_t)(now.tv_sec - g_time_start.tv_sec);
int64_t nsec = now.tv_nsec - g_time_start.tv_nsec;
return sec * 1000000ULL + (uint64_t)(nsec / 1000);
}
// ============================================================================
// Platform Implementation: Serial Port
// ============================================================================
plat_serial_t plat_serial_open(const char* port, int baud) {
int fd = open(port, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd < 0) return PLAT_SERIAL_INVALID;
struct termios tty;
if (tcgetattr(fd, &tty) != 0) { close(fd); return PLAT_SERIAL_INVALID; }
speed_t speed;
switch (baud) {
case 9600: speed = B9600; break;
case 19200: speed = B19200; break;
case 38400: speed = B38400; break;
case 57600: speed = B57600; break;
case 115200: speed = B115200; break;
case 230400: speed = B230400; break;
case 460800: speed = B460800; break;
case 500000: speed = B500000; break;
case 576000: speed = B576000; break;
case 921600: speed = B921600; break;
case 1000000: speed = B1000000; break;
case 1500000: speed = B1500000; break;
case 2000000: speed = B2000000; break;
case 3000000: speed = B3000000; break;
case 4000000: speed = B4000000; break;
default: speed = B115200; break;
}
cfsetospeed(&tty, speed);
cfsetispeed(&tty, speed);
tty.c_cflag = (tty.c_cflag & ~(PARENB|CSTOPB|CSIZE|CRTSCTS)) | CS8 | CREAD | CLOCAL;
tty.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHONL|ISIG);
tty.c_iflag &= ~(IXON|IXOFF|IXANY|IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL);
tty.c_oflag &= ~(OPOST|ONLCR);
tty.c_cc[VTIME] = 1;
tty.c_cc[VMIN] = 0;
if (tcsetattr(fd, TCSANOW, &tty) != 0) { close(fd); return PLAT_SERIAL_INVALID; }
tcflush(fd, TCIOFLUSH);
return fd;
}
void plat_serial_close(plat_serial_t s) {
if (s != PLAT_SERIAL_INVALID) close(s);
}
int plat_serial_write(plat_serial_t s, const uint8_t* data, int len) {
ssize_t w = write(s, data, (size_t)len);
if (w < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
usleep(200);
w = write(s, data, (size_t)len);
}
return (int)w;
}
int plat_serial_read(plat_serial_t s, uint8_t* buf, int buflen) {
ssize_t n = read(s, buf, (size_t)buflen);
if (n < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) return 0;
return -1;
}
return (int)n;
}
// ============================================================================
// Platform Implementation: Cursor Position
// ============================================================================
bool plat_get_cursor_pos(plat_cursor_pos_t* pos) {
/* Use GDK to get cursor position — works on X11 and Wayland */
GdkDisplay* display = gdk_display_get_default();
if (!display) return false;
GdkSeat* seat = gdk_display_get_default_seat(display);
if (!seat) return false;
GdkDevice* pointer = gdk_seat_get_pointer(seat);
if (!pointer) return false;
int x, y;
gdk_device_get_position(pointer, NULL, &x, &y);
pos->x = (double)x;
pos->y = (double)y;
return true;
}
// ============================================================================
// Application State
// ============================================================================
static GtkWidget* g_window;
static GtkWidget* g_canvas;
static GtkWidget* g_sidebar;
static GtkWidget* g_combo_proto;
static GtkWidget* g_combo_test;
static GtkWidget* g_combo_port;
static GtkWidget* g_combo_baud;
static GtkWidget* g_btn_connect;
static GtkWidget* g_btn_run;
static GtkWidget* g_label_status;
static GtkWidget* g_debug_textview;
static GtkWidget* g_debug_scroll;
static uint32_t g_last_debug_count = 0;
static gboolean g_app_connected = FALSE;
static gboolean g_app_test_running = FALSE;
/* Canvas view state */
static double g_view_x = 0, g_view_y = 0, g_view_scale = 1.0;
static gboolean g_show_cmd = TRUE, g_show_obs = TRUE, g_show_dots = TRUE;
static gboolean g_show_dev = FALSE, g_show_grid = TRUE, g_show_speed = TRUE;
static gboolean g_dragging = FALSE;
static double g_drag_sx, g_drag_sy, g_drag_vx, g_drag_vy;
// ============================================================================
// Drawing Helpers
// ============================================================================
static void speed_color(double s, double mx, double* r, double* g, double* b) {
double t = fmin(s / (mx * 0.5 + 0.001), 1.0);
if (t < 0.33) {
*r = (70 + t*3*100)/255.0; *g = (140 + t*3*60)/255.0; *b = 240/255.0;
} else if (t < 0.66) {
double u = (t - 0.33) * 3;
*r = (70 + u*180)/255.0; *g = (200 - u*20)/255.0; *b = (240 - u*200)/255.0;
} else {
double u = (t - 0.66) * 3;
*r = 240/255.0; *g = (180 - u*140)/255.0; *b = (40 - u*20)/255.0;
}
}
static void screen_pt(double px, double py, double* sx, double* sy) {
*sx = px * g_view_scale + g_view_x;
*sy = py * g_view_scale + g_view_y;
}
static void fit_view(int W, int H) {
double rx = g_trace.all_max_x - g_trace.all_min_x;
double ry = g_trace.all_max_y - g_trace.all_min_y;
if (rx < 10) rx = 10; if (ry < 10) ry = 10;
double pad = 60;
g_view_scale = fmin((W - pad*2) / rx, (H - pad*2) / ry) * 0.9;
double cx = g_trace.all_min_x + rx/2, cy = g_trace.all_min_y + ry/2;
g_view_x = W/2.0 - cx * g_view_scale;
g_view_y = H/2.0 - cy * g_view_scale;
}
// ============================================================================
// Canvas Drawing
// ============================================================================
static gboolean on_canvas_draw(GtkWidget* widget, cairo_t* cr, gpointer data) {
(void)data;
int W = gtk_widget_get_allocated_width(widget);
int H = gtk_widget_get_allocated_height(widget);
/* Background */
cairo_set_source_rgb(cr, 6/255.0, 6/255.0, 10/255.0);
cairo_rectangle(cr, 0, 0, W, H);
cairo_fill(cr);
uint32_t ncmd = g_trace.cmd_count;
uint32_t nobs = g_trace.obs_count;
/* Grid */
if (g_show_grid && g_view_scale > 0) {
double gs = pow(10, floor(log10(100.0 / g_view_scale)));
double d0x = (0 - g_view_x) / g_view_scale;
double d1x = (W - g_view_x) / g_view_scale;
double d0y = (0 - g_view_y) / g_view_scale;
double d1y = (H - g_view_y) / g_view_scale;
cairo_select_font_face(cr, "monospace", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
cairo_set_font_size(cr, 9);
for (double gx = floor(d0x/gs)*gs; gx <= d1x; gx += gs) {
double sx = gx * g_view_scale + g_view_x;
if (fabs(gx) < 0.01) {
cairo_set_source_rgb(cr, 37/255.0, 37/255.0, 53/255.0);
cairo_set_line_width(cr, 1);
} else {
cairo_set_source_rgb(cr, 22/255.0, 22/255.0, 32/255.0);
cairo_set_line_width(cr, 0.5);
}
cairo_move_to(cr, sx, 0); cairo_line_to(cr, sx, H); cairo_stroke(cr);
cairo_set_source_rgb(cr, 42/255.0, 42/255.0, 54/255.0);
char lbl[32]; snprintf(lbl, sizeof(lbl), "%.0f", gx);
cairo_move_to(cr, sx+2, 12);
cairo_show_text(cr, lbl);
}
for (double gy = floor(d0y/gs)*gs; gy <= d1y; gy += gs) {
double sy = gy * g_view_scale + g_view_y;
if (fabs(gy) < 0.01) {
cairo_set_source_rgb(cr, 37/255.0, 37/255.0, 53/255.0);
cairo_set_line_width(cr, 1);
} else {
cairo_set_source_rgb(cr, 22/255.0, 22/255.0, 32/255.0);
cairo_set_line_width(cr, 0.5);
}
cairo_move_to(cr, 0, sy); cairo_line_to(cr, W, sy); cairo_stroke(cr);
cairo_set_source_rgb(cr, 42/255.0, 42/255.0, 54/255.0);
char lbl[32]; snprintf(lbl, sizeof(lbl), "%.0f", gy);
cairo_move_to(cr, 2, sy + 12);
cairo_show_text(cr, lbl);
}
}
/* Origin crosshair */
{
double ox, oy;
screen_pt(0, 0, &ox, &oy);
cairo_set_source_rgb(cr, 51/255.0, 51/255.0, 64/255.0);
cairo_set_line_width(cr, 1);
cairo_arc(cr, ox, oy, 6, 0, 2*M_PI); cairo_stroke(cr);
cairo_move_to(cr, ox-8, oy); cairo_line_to(cr, ox+8, oy); cairo_stroke(cr);
cairo_move_to(cr, ox, oy-8); cairo_line_to(cr, ox, oy+8); cairo_stroke(cr);
}
if (ncmd < 2 && nobs < 2) {
cairo_select_font_face(cr, "monospace", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
cairo_set_font_size(cr, 14);
cairo_set_source_rgb(cr, 72/255.0, 72/255.0, 96/255.0);
const char* msg = "Run a test to see trace data";
cairo_text_extents_t ext;
cairo_text_extents(cr, msg, &ext);
cairo_move_to(cr, W/2.0 - ext.width/2, H/2.0);
cairo_show_text(cr, msg);
return TRUE;
}
/* Compute max speeds */
double cmdMaxSpd = 0, obsMaxSpd = 0;
if (g_show_speed) {
for (uint32_t i = 1; i < ncmd; i++) {
double dt = (double)(g_trace.cmds[i].time_us - g_trace.cmds[i-1].time_us);
if (dt <= 0) continue;
double s = hypot(g_trace.cmds[i].cum_x - g_trace.cmds[i-1].cum_x,
g_trace.cmds[i].cum_y - g_trace.cmds[i-1].cum_y) / (dt/1000.0);
if (s > cmdMaxSpd) cmdMaxSpd = s;
}
for (uint32_t i = 1; i < nobs; i++) {
double dt = (double)(g_trace.obs[i].time_us - g_trace.obs[i-1].time_us);
if (dt <= 0) continue;
double s = hypot(g_trace.obs[i].rel_x - g_trace.obs[i-1].rel_x,
g_trace.obs[i].rel_y - g_trace.obs[i-1].rel_y) / (dt/1000.0);
if (s > obsMaxSpd) obsMaxSpd = s;
}
}
double globalMaxSpd = fmax(cmdMaxSpd, obsMaxSpd);
/* Draw paths */
for (int pass = 0; pass < 2; pass++) {
gboolean isCmd = (pass == 1);
if (isCmd && !g_show_cmd) continue;
if (!isCmd && !g_show_obs) continue;
uint32_t n = isCmd ? ncmd : nobs;
if (n < 2) continue;
cairo_set_line_width(cr, 1.5);
cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
double prevSpd = 0;
for (uint32_t i = 1; i < n; i++) {
double x0, y0, x1, y1;
if (isCmd) {
screen_pt(g_trace.cmds[i-1].cum_x, g_trace.cmds[i-1].cum_y, &x0, &y0);
screen_pt(g_trace.cmds[i].cum_x, g_trace.cmds[i].cum_y, &x1, &y1);
} else {
screen_pt(g_trace.obs[i-1].rel_x, g_trace.obs[i-1].rel_y, &x0, &y0);
screen_pt(g_trace.obs[i].rel_x, g_trace.obs[i].rel_y, &x1, &y1);
}
if (g_show_speed) {
double dt, dx, dy;
if (isCmd) {
dt = (double)(g_trace.cmds[i].time_us - g_trace.cmds[i-1].time_us);
dx = g_trace.cmds[i].cum_x - g_trace.cmds[i-1].cum_x;
dy = g_trace.cmds[i].cum_y - g_trace.cmds[i-1].cum_y;
} else {
dt = (double)(g_trace.obs[i].time_us - g_trace.obs[i-1].time_us);
dx = g_trace.obs[i].rel_x - g_trace.obs[i-1].rel_x;
dy = g_trace.obs[i].rel_y - g_trace.obs[i-1].rel_y;
}
double spd = (dt > 0) ? hypot(dx,dy)/(dt/1000.0) : prevSpd;
prevSpd = spd;
double r, g, b; speed_color(spd, globalMaxSpd, &r, &g, &b);
cairo_set_source_rgb(cr, r, g, b);
} else {
if (isCmd) cairo_set_source_rgb(cr, 78/255.0, 154/255.0, 240/255.0);
else cairo_set_source_rgb(cr, 240/255.0, 128/255.0, 64/255.0);
}
cairo_move_to(cr, x0, y0);
cairo_line_to(cr, x1, y1);
cairo_stroke(cr);
}
/* Start/end markers */
double sx, sy, ex, ey;
if (isCmd) {
screen_pt(g_trace.cmds[0].cum_x, g_trace.cmds[0].cum_y, &sx, &sy);
screen_pt(g_trace.cmds[n-1].cum_x, g_trace.cmds[n-1].cum_y, &ex, &ey);
} else {
screen_pt(g_trace.obs[0].rel_x, g_trace.obs[0].rel_y, &sx, &sy);
screen_pt(g_trace.obs[n-1].rel_x, g_trace.obs[n-1].rel_y, &ex, &ey);
}
cairo_set_source_rgb(cr, 64/255.0, 216/255.0, 128/255.0);
cairo_arc(cr, sx, sy, 4, 0, 2*M_PI); cairo_fill(cr);
cairo_set_source_rgb(cr, 240/255.0, 64/255.0, 96/255.0);
cairo_arc(cr, ex, ey, 4, 0, 2*M_PI); cairo_fill(cr);
}
return TRUE;
}
// ============================================================================
// Sidebar Drawing
// ============================================================================
static gboolean on_sidebar_draw(GtkWidget* widget, cairo_t* cr, gpointer data) {
(void)data;
int W = gtk_widget_get_allocated_width(widget);
int H = gtk_widget_get_allocated_height(widget);
cairo_set_source_rgb(cr, 11/255.0, 11/255.0, 18/255.0);
cairo_rectangle(cr, 0, 0, W, H);
cairo_fill(cr);
/* Border */
cairo_set_source_rgb(cr, 30/255.0, 30/255.0, 48/255.0);
cairo_set_line_width(cr, 1);
cairo_move_to(cr, 0, 0); cairo_line_to(cr, 0, H); cairo_stroke(cr);
cairo_select_font_face(cr, "monospace", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
double y = 16;
double w = W - 24;
trace_analysis_t* a = &g_trace.analysis;
#define SB_HEADER(text) do { \
cairo_set_font_size(cr, 10); \
cairo_set_source_rgb(cr, 72/255.0, 72/255.0, 96/255.0); \
cairo_move_to(cr, 12, y); cairo_show_text(cr, text); \
y += 18; \
} while(0)
#define SB_ROW(label, value, r, g, b) do { \
cairo_set_font_size(cr, 11); \
cairo_set_source_rgb(cr, 112/255.0, 112/255.0, 136/255.0); \
cairo_move_to(cr, 12, y); cairo_show_text(cr, label); \
cairo_set_source_rgb(cr, r, g, b); \
cairo_text_extents_t _ext; cairo_text_extents(cr, value, &_ext); \
cairo_move_to(cr, 12 + w - _ext.width, y); cairo_show_text(cr, value); \
y += 17; \
} while(0)
if (!g_trace.analysis_valid) {
SB_HEADER("NO DATA");
cairo_set_font_size(cr, 11);
cairo_set_source_rgb(cr, 72/255.0, 72/255.0, 96/255.0);
cairo_move_to(cr, 12, y);
cairo_show_text(cr, "Connect and run a test");
} else {
char buf[128];
SB_HEADER("MOVEMENT");
snprintf(buf, sizeof(buf), "%u", g_trace.cmd_count);
SB_ROW("Commands", buf, 232/255.0, 232/255.0, 240/255.0);
snprintf(buf, sizeof(buf), "%u", g_trace.obs_count);
SB_ROW("Observations", buf, 232/255.0, 232/255.0, 240/255.0);
snprintf(buf, sizeof(buf), "%.1f ms", a->total_ms);
SB_ROW("Duration", buf, 232/255.0, 232/255.0, 240/255.0);
snprintf(buf, sizeof(buf), "%.0f Hz", a->cmd_rate);
SB_ROW("Cmd rate", buf, 232/255.0, 232/255.0, 240/255.0);
snprintf(buf, sizeof(buf), "%.0f Hz", a->obs_rate);
SB_ROW("Obs rate", buf, 232/255.0, 232/255.0, 240/255.0);
y += 6;
SB_HEADER("COMMAND FIDELITY");
double dr, dg, db;
if (a->dev_avg < 2) { dr=64/255.0; dg=216/255.0; db=128/255.0; }
else if (a->dev_avg < 5) { dr=232/255.0; dg=200/255.0; db=64/255.0; }
else { dr=240/255.0; dg=64/255.0; db=96/255.0; }
snprintf(buf, sizeof(buf), "%.2f px", a->dev_avg);
SB_ROW("Avg gap", buf, dr, dg, db);
snprintf(buf, sizeof(buf), "%.2f px", a->dev_max);
SB_ROW("Max gap", buf, 232/255.0, 232/255.0, 240/255.0);
snprintf(buf, sizeof(buf), "%.0f%%", a->cmd_rep_pct);
SB_ROW("Delta reps", buf, 232/255.0, 232/255.0, 240/255.0);
snprintf(buf, sizeof(buf), "%.3f", a->int_cv);
SB_ROW("Interval CV", buf, 232/255.0, 232/255.0, 240/255.0);
snprintf(buf, sizeof(buf), "%.1f us", a->int_std_us);
SB_ROW("Int std dev", buf, 232/255.0, 232/255.0, 240/255.0);
snprintf(buf, sizeof(buf), "%.3f", a->int_skewness);
if (a->int_skewness < 0.1) { SB_ROW("Int skew", buf, 240/255.0, 64/255.0, 96/255.0); }
else { SB_ROW("Int skew", buf, 232/255.0, 232/255.0, 240/255.0); }
snprintf(buf, sizeof(buf), "%.3f", a->int_bimodality);
if (a->int_bimodality > 0.555) { SB_ROW("Int bimod", buf, 240/255.0, 64/255.0, 96/255.0); }
else { SB_ROW("Int bimod", buf, 232/255.0, 232/255.0, 240/255.0); }
snprintf(buf, sizeof(buf), "%.0f", a->int_dominant_hz);
SB_ROW("Dom Hz", buf, 232/255.0, 232/255.0, 240/255.0);
y += 6;
SB_HEADER("HUMANIZATION");
double hr, hg, hb;
if (a->h_score < 35) { hr=240/255.0; hg=64/255.0; hb=96/255.0; }
else if (a->h_score < 55) { hr=232/255.0; hg=200/255.0; hb=64/255.0; }
else { hr=64/255.0; hg=216/255.0; hb=128/255.0; }
cairo_set_source_rgb(cr, hr, hg, hb);
cairo_set_font_size(cr, 22);
snprintf(buf, sizeof(buf), "%d / 100", (int)a->h_score);
cairo_move_to(cr, 12, y + 4); cairo_show_text(cr, buf);
cairo_set_font_size(cr, 11);
cairo_text_extents_t ext; cairo_text_extents(cr, a->h_grade, &ext);
cairo_move_to(cr, 12 + w - ext.width, y); cairo_show_text(cr, a->h_grade);
y += 28;
snprintf(buf, sizeof(buf), "%.3f px", a->perp_scatter);
SB_ROW("Perp scatter", buf, 232/255.0, 232/255.0, 240/255.0);
snprintf(buf, sizeof(buf), "%.3f px", a->jit_avg);
SB_ROW("Jitter avg", buf, 232/255.0, 232/255.0, 240/255.0);
snprintf(buf, sizeof(buf), "%.1f%%", a->dir_flip_rate);
SB_ROW("Dir reversals", buf, 232/255.0, 232/255.0, 240/255.0);
snprintf(buf, sizeof(buf), "%.3f", a->speed_cv);
SB_ROW("Speed CV", buf, 232/255.0, 232/255.0, 240/255.0);
snprintf(buf, sizeof(buf), "%.1f%%", a->sub_px_pct);
SB_ROW("Sub-pixel", buf, 232/255.0, 232/255.0, 240/255.0);
snprintf(buf, sizeof(buf), "%.4f", a->accel_jerk);
SB_ROW("Accel jerk", buf, 232/255.0, 232/255.0, 240/255.0);
snprintf(buf, sizeof(buf), "%.4f", a->path_eff);
SB_ROW("Path eff", buf, 232/255.0, 232/255.0, 240/255.0);
}
#undef SB_HEADER
#undef SB_ROW
return TRUE;
}
// ============================================================================
// Canvas Mouse Events
// ============================================================================
static gboolean on_canvas_button_press(GtkWidget* w, GdkEventButton* ev, gpointer data) {
(void)w; (void)data;
if (ev->button == 1) {
if (ev->type == GDK_2BUTTON_PRESS) {
int W = gtk_widget_get_allocated_width(g_canvas);
int H = gtk_widget_get_allocated_height(g_canvas);
fit_view(W, H);
gtk_widget_queue_draw(g_canvas);
} else {
g_dragging = TRUE;
g_drag_sx = ev->x; g_drag_sy = ev->y;
g_drag_vx = g_view_x; g_drag_vy = g_view_y;
}
}
return TRUE;
}
static gboolean on_canvas_button_release(GtkWidget* w, GdkEventButton* ev, gpointer data) {
(void)w; (void)ev; (void)data;
g_dragging = FALSE;
return TRUE;
}
static gboolean on_canvas_motion(GtkWidget* w, GdkEventMotion* ev, gpointer data) {
(void)w; (void)data;
if (g_dragging) {
g_view_x = g_drag_vx + (ev->x - g_drag_sx);
g_view_y = g_drag_vy + (ev->y - g_drag_sy);
gtk_widget_queue_draw(g_canvas);
}
return TRUE;
}
static gboolean on_canvas_scroll(GtkWidget* w, GdkEventScroll* ev, gpointer data) {
(void)w; (void)data;
double factor;
if (ev->direction == GDK_SCROLL_UP) factor = 1.15;
else if (ev->direction == GDK_SCROLL_DOWN) factor = 1.0/1.15;
else {
/* Smooth scrolling */
double dy = 0;
gdk_event_get_scroll_deltas((GdkEvent*)ev, NULL, &dy);
factor = (dy < 0) ? 1.15 : (1.0/1.15);
}
g_view_x = ev->x - (ev->x - g_view_x) * factor;
g_view_y = ev->y - (ev->y - g_view_y) * factor;
g_view_scale *= factor;
gtk_widget_queue_draw(g_canvas);
return TRUE;
}
// ============================================================================
// Serial Port Enumeration
// ============================================================================
static void populate_port_combo(void) {
gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT(g_combo_port));
DIR* d = opendir("/dev");
if (!d) return;
struct dirent* ent;
int count = 0;
while ((ent = readdir(d)) != NULL) {
if (strncmp(ent->d_name, "ttyUSB", 6) == 0 ||
strncmp(ent->d_name, "ttyACM", 6) == 0 ||
strncmp(ent->d_name, "ttyAMA", 6) == 0 ||
strncmp(ent->d_name, "ttyS", 4) == 0) {
char path[280];
snprintf(path, sizeof(path), "/dev/%s", ent->d_name);
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(g_combo_port), path);
count++;
}
}
closedir(d);
if (count > 0)
gtk_combo_box_set_active(GTK_COMBO_BOX(g_combo_port), 0);
else
gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(g_combo_port))), "/dev/ttyUSB0");
debug_log_append("Enumerated %d serial ports", count);
}
// ============================================================================
// Callbacks
// ============================================================================
static void on_proto_changed(GtkComboBox* combo, gpointer data) {
(void)data;
int idx = gtk_combo_box_get_active(combo);
device_type_t type = (idx == 0) ? DEV_KMBOX : (idx == 1) ? DEV_FERRUM : DEV_MAKCU;
proto_init(&g_proto, type);
int baud = g_baud_override > 0 ? g_baud_override : g_proto.profile->default_baud;
char msg[128];
snprintf(msg, sizeof(msg), "Protocol: %s (baud: %d)", g_proto.profile->name, baud);
gtk_label_set_text(GTK_LABEL(g_label_status), msg);
debug_log_append("Protocol changed: %s (default baud: %d)", g_proto.profile->name, g_proto.profile->default_baud);
}
static void on_baud_changed(GtkComboBox* combo, gpointer data) {
(void)data;
int idx = gtk_combo_box_get_active(combo);
if (idx == 0) {
g_baud_override = 0;
debug_log_append("Baud rate: using protocol default (%d)", g_proto.profile->default_baud);
} else if (idx > 0 && idx <= g_baud_rate_count) {
g_baud_override = g_baud_rates[idx - 1];
debug_log_append("Baud rate override: %d", g_baud_override);
}
if (!g_app_connected) {
int baud = g_baud_override > 0 ? g_baud_override : g_proto.profile->default_baud;
char msg[128];
snprintf(msg, sizeof(msg), "Protocol: %s (baud: %d)", g_proto.profile->name, baud);
gtk_label_set_text(GTK_LABEL(g_label_status), msg);
}
}
static void on_connect_clicked(GtkButton* btn, gpointer data) {
(void)data;
if (g_app_connected) {
debug_log_append("Disconnecting...");
serial_reader_stop();
plat_serial_close(g_serial_fd);
g_serial_fd = PLAT_SERIAL_INVALID;
g_app_connected = FALSE;
gtk_button_set_label(btn, "Connect");
gtk_label_set_text(GTK_LABEL(g_label_status), "Disconnected");
debug_log_append("Disconnected");
return;
}
populate_port_combo();
const char* port = gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(g_combo_port))));
int baud = g_baud_override > 0 ? g_baud_override : g_proto.profile->default_baud;
debug_log_append("Connecting to %s @ %d baud (%s)...", port, baud, g_proto.profile->name);
g_serial_fd = plat_serial_open(port, baud);
if (g_serial_fd == PLAT_SERIAL_INVALID) {
char msg[256];
snprintf(msg, sizeof(msg), "Failed: %s", strerror(errno));
gtk_label_set_text(GTK_LABEL(g_label_status), msg);
debug_log_append("CONNECT FAILED: %s (errno=%d)", strerror(errno), errno);
return;
}
serial_reader_start();
g_app_connected = TRUE;
gtk_button_set_label(btn, "Disconnect");
char msg[256];
snprintf(msg, sizeof(msg), "Connected: %s @ %d", g_proto.profile->name, baud);
gtk_label_set_text(GTK_LABEL(g_label_status), msg);
debug_log_append("Connected successfully: fd=%d", g_serial_fd);
}
static gboolean on_test_done(gpointer data) {
(void)data;
g_app_test_running = FALSE;
gtk_button_set_label(GTK_BUTTON(g_btn_run), "\u25B6 Run");
gtk_widget_set_sensitive(g_btn_run, TRUE);
int W = gtk_widget_get_allocated_width(g_canvas);
int H = gtk_widget_get_allocated_height(g_canvas);
fit_view(W, H);
gtk_widget_queue_draw(g_canvas);
gtk_widget_queue_draw(g_sidebar);
int idx = gtk_combo_box_get_active(GTK_COMBO_BOX(g_combo_test));
char msg[256];
snprintf(msg, sizeof(msg), "Done: %s - %lld sent, %lld ok, %lld err",
all_tests[idx].name, (long long)g_stat_sent, (long long)g_stat_ok, (long long)g_stat_err);
gtk_label_set_text(GTK_LABEL(g_label_status), msg);
return G_SOURCE_REMOVE;
}
static int g_run_test_idx = -1;
static void* test_thread_fn(void* arg) {
(void)arg;
int idx = g_run_test_idx;
if (idx >= 0 && idx < NUM_TESTS) {
all_tests[idx].run();
trace_analyze();
debug_log_append("Test complete: %s - %lld sent, %lld ok, %lld err, score=%d (%s)",
all_tests[idx].name, (long long)g_stat_sent, (long long)g_stat_ok, (long long)g_stat_err,
(int)g_trace.analysis.h_score, g_trace.analysis.h_grade);
}
g_idle_add(on_test_done, NULL);
return NULL;
}
static void on_fit_view_clicked(GtkButton* btn, gpointer data) {
(void)btn; (void)data;
int W = gtk_widget_get_allocated_width(g_canvas);
int H = gtk_widget_get_allocated_height(g_canvas);
fit_view(W, H);
gtk_widget_queue_draw(g_canvas);
}
static void do_run_test(void) {
if (g_app_test_running) return;
int idx = gtk_combo_box_get_active(GTK_COMBO_BOX(g_combo_test));
if (idx < 0 || idx >= NUM_TESTS) return;
g_app_test_running = TRUE;
g_run_test_idx = idx;
g_stat_ok = 0; g_stat_err = 0; g_stat_sent = 0;
debug_log_append("Starting test: %s", all_tests[idx].name);
gtk_widget_set_sensitive(g_btn_run, FALSE);
gtk_button_set_label(GTK_BUTTON(g_btn_run), "Running...");
pthread_t t;
pthread_create(&t, NULL, test_thread_fn, NULL);
pthread_detach(t);
}
static void on_run_clicked(GtkButton* btn, gpointer data) {
(void)btn; (void)data;
do_run_test();
}
static gboolean on_key_press(GtkWidget* widget, GdkEventKey* event, gpointer data) {
(void)widget; (void)data;
// Ctrl+R to run test
if ((event->state & GDK_CONTROL_MASK) && event->keyval == GDK_KEY_r) {
do_run_test();
return TRUE;
}
return FALSE;
}
static gboolean on_refresh_tick(gpointer data) {
(void)data;
if (g_app_test_running && g_trace.recording) {
uint32_t ncmd = g_trace.cmd_count, nobs = g_trace.obs_count;
double mnx=0,mxx=0,mny=0,mxy=0;
for (uint32_t i = 0; i < ncmd; i++) {
if (g_trace.cmds[i].cum_x < mnx) mnx = g_trace.cmds[i].cum_x;
if (g_trace.cmds[i].cum_x > mxx) mxx = g_trace.cmds[i].cum_x;
if (g_trace.cmds[i].cum_y < mny) mny = g_trace.cmds[i].cum_y;
if (g_trace.cmds[i].cum_y > mxy) mxy = g_trace.cmds[i].cum_y;
}
for (uint32_t i = 0; i < nobs; i++) {
if (g_trace.obs[i].rel_x < mnx) mnx = g_trace.obs[i].rel_x;
if (g_trace.obs[i].rel_x > mxx) mxx = g_trace.obs[i].rel_x;
if (g_trace.obs[i].rel_y < mny) mny = g_trace.obs[i].rel_y;
if (g_trace.obs[i].rel_y > mxy) mxy = g_trace.obs[i].rel_y;
}
g_trace.all_min_x = mnx; g_trace.all_max_x = mxx;
g_trace.all_min_y = mny; g_trace.all_max_y = mxy;
int W = gtk_widget_get_allocated_width(g_canvas);
int H = gtk_widget_get_allocated_height(g_canvas);
fit_view(W, H);
gtk_widget_queue_draw(g_canvas);
char msg[256];
snprintf(msg, sizeof(msg), "Recording: %u cmd, %u obs - %lld sent",
ncmd, nobs, (long long)g_stat_sent);
gtk_label_set_text(GTK_LABEL(g_label_status), msg);
}
// Update debug log view
uint32_t current_count = g_debug_log.count;
if (current_count != g_last_debug_count && g_debug_textview) {
g_last_debug_count = current_count;
GtkTextBuffer* buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(g_debug_textview));
GString* text = g_string_new(NULL);
plat_mutex_lock(&g_debug_log.mutex);
uint32_t total = g_debug_log.head;
uint32_t start = (total > DEBUG_LOG_LINES) ? total - DEBUG_LOG_LINES : 0;
for (uint32_t i = start; i < total; i++) {
uint32_t idx = i % DEBUG_LOG_LINES;
g_string_append(text, g_debug_log.lines[idx]);
g_string_append_c(text, '\n');
}
plat_mutex_unlock(&g_debug_log.mutex);
gtk_text_buffer_set_text(buf, text->str, (gint)text->len);
g_string_free(text, TRUE);
// Auto-scroll to bottom
GtkTextIter end;
gtk_text_buffer_get_end_iter(buf, &end);
gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(g_debug_textview), &end, 0, FALSE, 0, 0);
}
return G_SOURCE_CONTINUE;
}
// ============================================================================
// Display Toggle Callbacks
// ============================================================================
static void on_chk_cmd(GtkToggleButton* t, gpointer d) { (void)d; g_show_cmd = gtk_toggle_button_get_active(t); gtk_widget_queue_draw(g_canvas); }
static void on_chk_obs(GtkToggleButton* t, gpointer d) { (void)d; g_show_obs = gtk_toggle_button_get_active(t); gtk_widget_queue_draw(g_canvas); }
static void on_chk_dots(GtkToggleButton* t, gpointer d) { (void)d; g_show_dots = gtk_toggle_button_get_active(t); gtk_widget_queue_draw(g_canvas); }
static void on_chk_dev(GtkToggleButton* t, gpointer d) { (void)d; g_show_dev = gtk_toggle_button_get_active(t); gtk_widget_queue_draw(g_canvas); }
static void on_chk_grid(GtkToggleButton* t, gpointer d) { (void)d; g_show_grid = gtk_toggle_button_get_active(t); gtk_widget_queue_draw(g_canvas); }
static void on_chk_speed(GtkToggleButton* t, gpointer d) { (void)d; g_show_speed = gtk_toggle_button_get_active(t); gtk_widget_queue_draw(g_canvas); }
// ============================================================================
// CSS for Dark Theme
// ============================================================================
static const char* css_style =
"window { background-color: #06060a; }"
"label { color: #b0b0c0; font-family: monospace; font-size: 11px; }"
"entry { background-color: #16161a; color: #e8e8f0; border: 1px solid #1e1e30;"
" font-family: monospace; font-size: 11px; padding: 2px 4px; }"
"button { background-color: #16161a; color: #e8e8f0; border: 1px solid #1e1e30;"
" font-size: 11px; padding: 2px 8px; }"
"button:hover { background-color: #22222a; }"
"combobox button { background-color: #16161a; color: #e8e8f0; border: 1px solid #1e1e30; }"
"checkbutton label { color: #7070a0; font-size: 11px; }"
".topbar { background-color: #0b0b12; border-bottom: 1px solid #1e1e30; }"
".debuglog { background-color: #10101a; color: #b0b0c0; font-family: monospace; font-size: 10px; }"
".debuglog text { background-color: #10101a; color: #b0b0c0; }";
// ============================================================================
// Build GUI
// ============================================================================
static void activate_app(GtkApplication* app, gpointer user_data) {
(void)user_data;
/* CSS */
GtkCssProvider* css = gtk_css_provider_new();
gtk_css_provider_load_from_data(css, css_style, -1, NULL);
gtk_style_context_add_provider_for_screen(gdk_screen_get_default(),
GTK_STYLE_PROVIDER(css), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
/* Window */
g_window = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW(g_window), "KMBox Trace Analyzer");
gtk_window_set_default_size(GTK_WINDOW(g_window), 1400, 900);
gtk_widget_add_events(g_window, GDK_KEY_PRESS_MASK);
g_signal_connect(g_window, "key-press-event", G_CALLBACK(on_key_press), NULL);
/* Main vertical layout */
GtkWidget* vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_container_add(GTK_CONTAINER(g_window), vbox);
/* Top bar */
GtkWidget* topbar = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
GtkStyleContext* tctx = gtk_widget_get_style_context(topbar);
gtk_style_context_add_class(tctx, "topbar");
gtk_widget_set_margin_start(topbar, 8);
gtk_widget_set_margin_end(topbar, 8);
gtk_widget_set_margin_top(topbar, 6);
gtk_widget_set_margin_bottom(topbar, 6);
gtk_box_pack_start(GTK_BOX(vbox), topbar, FALSE, FALSE, 0);
/* Protocol combo */
gtk_box_pack_start(GTK_BOX(topbar), gtk_label_new("Protocol:"), FALSE, FALSE, 0);
g_combo_proto = gtk_combo_box_text_new();
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(g_combo_proto), "KMBox B+");
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(g_combo_proto), "Ferrum One");
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(g_combo_proto), "MAKCU");
gtk_combo_box_set_active(GTK_COMBO_BOX(g_combo_proto), 0);
g_signal_connect(g_combo_proto, "changed", G_CALLBACK(on_proto_changed), NULL);
gtk_box_pack_start(GTK_BOX(topbar), g_combo_proto, FALSE, FALSE, 0);
/* Port combo (editable dropdown with auto-enumerated serial ports) */
gtk_box_pack_start(GTK_BOX(topbar), gtk_label_new("Port:"), FALSE, FALSE, 0);
g_combo_port = gtk_combo_box_text_new_with_entry();
gtk_entry_set_width_chars(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(g_combo_port))), 20);
gtk_box_pack_start(GTK_BOX(topbar), g_combo_port, FALSE, FALSE, 0);
populate_port_combo();
/* Baud rate combo */
gtk_box_pack_start(GTK_BOX(topbar), gtk_label_new("Baud:"), FALSE, FALSE, 0);
g_combo_baud = gtk_combo_box_text_new();
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(g_combo_baud), "Default");
for (int i = 0; i < g_baud_rate_count; i++) {
char buf[32];
snprintf(buf, sizeof(buf), "%d", g_baud_rates[i]);
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(g_combo_baud), buf);
}
gtk_combo_box_set_active(GTK_COMBO_BOX(g_combo_baud), 0);
g_signal_connect(g_combo_baud, "changed", G_CALLBACK(on_baud_changed), NULL);
gtk_box_pack_start(GTK_BOX(topbar), g_combo_baud, FALSE, FALSE, 0);
/* Connect button */
g_btn_connect = gtk_button_new_with_label("Connect");
g_signal_connect(g_btn_connect, "clicked", G_CALLBACK(on_connect_clicked), NULL);
gtk_box_pack_start(GTK_BOX(topbar), g_btn_connect, FALSE, FALSE, 0);
/* Test combo */
gtk_box_pack_start(GTK_BOX(topbar), gtk_label_new("Test:"), FALSE, FALSE, 0);
g_combo_test = gtk_combo_box_text_new();
for (int i = 0; all_tests[i].name; i++)
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(g_combo_test), all_tests[i].name);
gtk_combo_box_set_active(GTK_COMBO_BOX(g_combo_test), 0);
gtk_box_pack_start(GTK_BOX(topbar), g_combo_test, FALSE, FALSE, 0);
/* Run button */
g_btn_run = gtk_button_new_with_label("\u25B6 Run");
g_signal_connect(g_btn_run, "clicked", G_CALLBACK(on_run_clicked), NULL);
gtk_box_pack_start(GTK_BOX(topbar), g_btn_run, FALSE, FALSE, 0);
/* Fit View button */
GtkWidget* btn_fit = gtk_button_new_with_label("Fit View");
g_signal_connect(btn_fit, "clicked", G_CALLBACK(on_fit_view_clicked), NULL);
gtk_box_pack_start(GTK_BOX(topbar), btn_fit, FALSE, FALSE, 0);
/* Status label */
g_label_status = gtk_label_new("Disconnected");
gtk_label_set_xalign(GTK_LABEL(g_label_status), 0);
gtk_box_pack_start(GTK_BOX(topbar), g_label_status, TRUE, TRUE, 0);
/* Main horizontal split: (Canvas + Debug Log) | Sidebar */
GtkWidget* hpaned = gtk_paned_new(GTK_ORIENTATION_HORIZONTAL);
gtk_paned_set_position(GTK_PANED(hpaned), 1080);
gtk_box_pack_start(GTK_BOX(vbox), hpaned, TRUE, TRUE, 0);
/* Left side: Canvas on top, Debug Log on bottom */
GtkWidget* left_vpaned = gtk_paned_new(GTK_ORIENTATION_VERTICAL);
gtk_paned_set_position(GTK_PANED(left_vpaned), 600);
gtk_paned_pack1(GTK_PANED(hpaned), left_vpaned, TRUE, TRUE);
/* Canvas */
g_canvas = gtk_drawing_area_new();
gtk_widget_set_size_request(g_canvas, 400, 200);
gtk_widget_add_events(g_canvas, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
GDK_POINTER_MOTION_MASK | GDK_SCROLL_MASK | GDK_SMOOTH_SCROLL_MASK);
g_signal_connect(g_canvas, "draw", G_CALLBACK(on_canvas_draw), NULL);
g_signal_connect(g_canvas, "button-press-event", G_CALLBACK(on_canvas_button_press), NULL);
g_signal_connect(g_canvas, "button-release-event", G_CALLBACK(on_canvas_button_release), NULL);
g_signal_connect(g_canvas, "motion-notify-event", G_CALLBACK(on_canvas_motion), NULL);
g_signal_connect(g_canvas, "scroll-event", G_CALLBACK(on_canvas_scroll), NULL);
gtk_paned_pack1(GTK_PANED(left_vpaned), g_canvas, TRUE, TRUE);
/* Debug log panel */
GtkWidget* debug_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_widget_set_size_request(debug_box, -1, 120);
GtkWidget* debug_label = gtk_label_new("DEBUG LOG");
gtk_label_set_xalign(GTK_LABEL(debug_label), 0);
gtk_widget_set_margin_start(debug_label, 8);
gtk_widget_set_margin_top(debug_label, 4);
gtk_box_pack_start(GTK_BOX(debug_box), debug_label, FALSE, FALSE, 0);
g_debug_scroll = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(g_debug_scroll),
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
g_debug_textview = gtk_text_view_new();
gtk_text_view_set_editable(GTK_TEXT_VIEW(g_debug_textview), FALSE);
gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(g_debug_textview), FALSE);
gtk_text_view_set_left_margin(GTK_TEXT_VIEW(g_debug_textview), 6);
gtk_text_view_set_right_margin(GTK_TEXT_VIEW(g_debug_textview), 6);
GtkStyleContext* dbg_ctx = gtk_widget_get_style_context(g_debug_textview);
gtk_style_context_add_class(dbg_ctx, "debuglog");
gtk_container_add(GTK_CONTAINER(g_debug_scroll), g_debug_textview);
gtk_box_pack_start(GTK_BOX(debug_box), g_debug_scroll, TRUE, TRUE, 0);
gtk_paned_pack2(GTK_PANED(left_vpaned), debug_box, FALSE, TRUE);
/* Sidebar container */
GtkWidget* sidebar_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_widget_set_size_request(sidebar_box, 320, -1);
/* Sidebar drawing area */
g_sidebar = gtk_drawing_area_new();
gtk_widget_set_size_request(g_sidebar, 320, 500);
g_signal_connect(g_sidebar, "draw", G_CALLBACK(on_sidebar_draw), NULL);
gtk_box_pack_start(GTK_BOX(sidebar_box), g_sidebar, TRUE, TRUE, 0);
/* Display checkboxes */
GtkWidget* chk_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 2);
gtk_widget_set_margin_start(chk_box, 12);
gtk_widget_set_margin_top(chk_box, 8);
gtk_widget_set_margin_bottom(chk_box, 8);
struct { const char* label; GCallback cb; gboolean active; } checks[] = {
{"Commanded path (blue)", G_CALLBACK(on_chk_cmd), TRUE},
{"Observed path (orange)", G_CALLBACK(on_chk_obs), TRUE},
{"Sample dots", G_CALLBACK(on_chk_dots), TRUE},
{"Deviation lines", G_CALLBACK(on_chk_dev), FALSE},
{"Grid", G_CALLBACK(on_chk_grid), TRUE},
{"Color by speed", G_CALLBACK(on_chk_speed), TRUE},
};
for (int i = 0; i < 6; i++) {
GtkWidget* chk = gtk_check_button_new_with_label(checks[i].label);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(chk), checks[i].active);
g_signal_connect(chk, "toggled", checks[i].cb, NULL);
gtk_box_pack_start(GTK_BOX(chk_box), chk, FALSE, FALSE, 0);
}
gtk_box_pack_start(GTK_BOX(sidebar_box), chk_box, FALSE, FALSE, 0);
gtk_paned_pack2(GTK_PANED(hpaned), sidebar_box, FALSE, FALSE);
/* Refresh timer */
g_timeout_add(50, on_refresh_tick, NULL);
gtk_widget_show_all(g_window);
}
// ============================================================================
// Main Entry Point
// ============================================================================
int main(int argc, char* argv[]) {
trace_alloc();
debug_log_init();
srand((unsigned)time(NULL));
proto_init(&g_proto, DEV_KMBOX);
/* Check for CLI mode */
const char* cli_port = NULL;