forked from cy384/ssheven
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconsole.c
More file actions
2307 lines (1988 loc) · 60.2 KB
/
Copy pathconsole.c
File metadata and controls
2307 lines (1988 loc) · 60.2 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
/*
* SevenTTY (based on ssheven by cy384)
*
* Copyright (c) 2020 by cy384 <cy384@cy384.com>
* See LICENSE file for details
*/
#include "app.h"
#include "console.h"
#include "net.h"
#include "telnet.h"
#include "unicode.h"
#include <string.h>
#include <Sound.h>
#include <Resources.h>
#include <QDOffscreen.h>
#include <vterm.h>
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
/* ---- dirty region tracking ---- */
static void mark_dirty(struct session* s, int start_row, int end_row)
{
if (s->dirty_start_row < 0)
{
s->dirty_start_row = start_row;
s->dirty_end_row = end_row;
}
else
{
if (start_row < s->dirty_start_row) s->dirty_start_row = start_row;
if (end_row > s->dirty_end_row) s->dirty_end_row = end_row;
}
}
static void mark_full_dirty(struct session* s)
{
s->force_full_redraw = 1;
}
static void clear_dirty(struct session* s)
{
s->dirty_start_row = -1;
s->dirty_end_row = -1;
s->force_full_redraw = 0;
}
void console_mark_full_dirty(int session_idx)
{
if (session_idx >= 0 && session_idx < MAX_SESSIONS && sessions[session_idx].in_use)
mark_full_dirty(&sessions[session_idx]);
}
/* ---- GWorld row buffer for flicker-free rendering ---- */
static GWorldPtr g_row_gworld = NULL;
static int g_row_gw_width = 0;
static int g_row_gw_height = 0;
static int ensure_row_gworld(int width, int height)
{
if (g_row_gworld != NULL && g_row_gw_width >= width && g_row_gw_height >= height)
return 1;
if (g_row_gworld != NULL)
{
DisposeGWorld(g_row_gworld);
g_row_gworld = NULL;
}
{
Rect bounds;
QDErr err;
bounds.top = 0;
bounds.left = 0;
bounds.bottom = height;
bounds.right = width;
/* Depth 0 = match deepest screen. On an 8-bit display this creates
an 8-bit GWorld so CopyBits is a fast byte copy (no depth
conversion). With SetGWorld(gw, screen_gd), Color2Index uses
the screen's CLUT, producing correct pixel values for both
the GWorld and the screen. */
err = NewGWorld(&g_row_gworld, 0, &bounds, NULL, NULL, 0);
if (err != noErr || g_row_gworld == NULL)
{
g_row_gworld = NULL;
g_row_gw_width = 0;
g_row_gw_height = 0;
return 0;
}
}
g_row_gw_width = width;
g_row_gw_height = height;
return 1;
}
void cleanup_row_gworld(void)
{
if (g_row_gworld != NULL)
{
DisposeGWorld(g_row_gworld);
g_row_gworld = NULL;
}
g_row_gw_width = 0;
g_row_gw_height = 0;
}
/* active session for a given window context */
#define WC_S(wc) sessions[(wc)->session_ids[(wc)->active_session_idx]]
/* sentinel values for default colors — negative to avoid colliding with
xterm 256-color palette indices (0-255) */
#define COLOR_DEFAULT_FG (-1)
#define COLOR_DEFAULT_BG (-2)
/* packed RGB: bit 24 set = true-color RGB, bits 16-23=R, 8-15=G, 0-7=B.
always positive and > 255, so no collision with sentinels or palette. */
#define COLOR_IS_RGB(c) ((c) > 255)
#define COLOR_PACK_RGB(r,g,b) (0x01000000 | ((r) << 16) | ((g) << 8) | (b))
/* extract abstract color ID from a VTermScreenCell color field.
returns sentinel for default colors, palette index for indexed colors,
or packed RGB for true-color. */
static int extract_fg(VTermScreenCell* cell)
{
if (VTERM_COLOR_IS_DEFAULT_FG(&cell->fg)) return COLOR_DEFAULT_FG;
if (VTERM_COLOR_IS_INDEXED(&cell->fg))
return cell->fg.indexed.idx;
/* true-color RGB — pack into int */
return COLOR_PACK_RGB(cell->fg.rgb.red, cell->fg.rgb.green, cell->fg.rgb.blue);
}
static int extract_bg(VTermScreenCell* cell)
{
if (VTERM_COLOR_IS_DEFAULT_BG(&cell->bg)) return COLOR_DEFAULT_BG;
if (VTERM_COLOR_IS_INDEXED(&cell->bg))
return cell->bg.indexed.idx;
/* true-color RGB — pack into int */
return COLOR_PACK_RGB(cell->bg.rgb.red, cell->bg.rgb.green, cell->bg.rgb.blue);
}
/* compute RGBColor for xterm 256-color palette index 16-255.
indices 16-231 = 6x6x6 RGB cube, 232-255 = 24-step grayscale ramp.
Mac RGBColor uses 16-bit per channel (0-65535). */
static void xterm256_to_rgb(int idx, RGBColor* c)
{
if (idx < 232)
{
/* 6x6x6 color cube: index = 16 + 36*r + 6*g + b */
static const unsigned short cube[] = {0, 0x5F5F, 0x8787, 0xAFAF, 0xD7D7, 0xFFFF};
int i = idx - 16;
c->red = cube[i / 36];
c->green = cube[(i / 6) % 6];
c->blue = cube[i % 6];
}
else
{
/* grayscale ramp: 232=rgb(8,8,8) .. 255=rgb(238,238,238) */
unsigned short g = (8 + (idx - 232) * 10) * 257;
c->red = g;
c->green = g;
c->blue = g;
}
}
/* convert 8-bit RGB to nearest xterm 256-color index (for scrollback storage) */
static unsigned char rgb_to_xterm256(unsigned char r, unsigned char g, unsigned char b)
{
int ri, gi, bi;
if (r == g && g == b)
{
if (r < 8) return 16;
if (r > 248) return 231;
return 232 + (r - 8) / 10;
}
ri = (r > 47) ? (r - 35) / 40 : 0;
gi = (g > 47) ? (g - 35) / 40 : 0;
bi = (b > 47) ? (b - 35) / 40 : 0;
return 16 + 36 * ri + 6 * gi + bi;
}
/* extract color byte for scrollback storage (0-255 index) */
static unsigned char vtcolor_to_byte(const VTermColor* vc)
{
if (VTERM_COLOR_IS_INDEXED(vc))
return vc->indexed.idx;
return rgb_to_xterm256(vc->rgb.red, vc->rgb.green, vc->rgb.blue);
}
/* resolve abstract color ID to drawing color.
sentinels map to theme_fg/theme_bg, 0-15 to palette, 16-255 to xterm cube/gray,
packed RGB (> 255) to direct true-color. */
static void set_draw_fg(int idx)
{
RGBColor c;
if (idx == COLOR_DEFAULT_FG)
RGBForeColor(&prefs.theme_fg);
else if (idx == COLOR_DEFAULT_BG)
RGBForeColor(&prefs.theme_bg);
else if (COLOR_IS_RGB(idx))
{
c.red = ((idx >> 16) & 0xFF) * 0x0101;
c.green = ((idx >> 8) & 0xFF) * 0x0101;
c.blue = (idx & 0xFF) * 0x0101;
RGBForeColor(&c);
}
else if (idx < 16)
RGBForeColor(&prefs.palette[idx]);
else
{
xterm256_to_rgb(idx, &c);
RGBForeColor(&c);
}
}
static void set_draw_bg(int idx)
{
RGBColor c;
if (idx == COLOR_DEFAULT_BG)
RGBBackColor(&prefs.theme_bg);
else if (idx == COLOR_DEFAULT_FG)
RGBBackColor(&prefs.theme_fg);
else if (COLOR_IS_RGB(idx))
{
c.red = ((idx >> 16) & 0xFF) * 0x0101;
c.green = ((idx >> 8) & 0xFF) * 0x0101;
c.blue = (idx & 0xFF) * 0x0101;
RGBBackColor(&c);
}
else if (idx < 16)
RGBBackColor(&prefs.palette[idx]);
else
{
xterm256_to_rgb(idx, &c);
RGBBackColor(&c);
}
}
/* Fill a rect with a resolved color index using PaintRect.
This avoids EraseRect entirely — EraseRect depends on bkColor/bkPat
port state which behaves unpredictably in offscreen GWorlds.
PaintRect with qd.white pen pattern forces foreground color into
every pixel deterministically. */
static void fill_rect_color(int color_idx, Rect* r)
{
set_draw_fg(color_idx);
PenPat(&qd.white);
PaintRect(r);
PenNormal();
}
static int symbol_font_lookup(uint32_t cp);
/* convert UTF-8 string to Mac Roman, writing to out buffer.
returns number of bytes written (not including null terminator).
unknown chars become '?' */
int utf8_to_macroman(const char* utf8, int utf8_len, char* out, int out_max)
{
int i = 0, o = 0;
while (i < utf8_len && o < out_max - 1)
{
unsigned char b = (unsigned char)utf8[i];
uint32_t cp;
if (b < 0x80)
{
cp = b;
i++;
}
else if ((b & 0xE0) == 0xC0 && i + 1 < utf8_len)
{
cp = ((b & 0x1F) << 6) | (utf8[i+1] & 0x3F);
i += 2;
}
else if ((b & 0xF0) == 0xE0 && i + 2 < utf8_len)
{
cp = ((b & 0x0F) << 12) | ((utf8[i+1] & 0x3F) << 6) | (utf8[i+2] & 0x3F);
i += 3;
}
else if ((b & 0xF8) == 0xF0 && i + 3 < utf8_len)
{
cp = ((b & 0x07) << 18) | ((utf8[i+1] & 0x3F) << 12) |
((utf8[i+2] & 0x3F) << 6) | (utf8[i+3] & 0x3F);
i += 4;
}
else
{
i++;
continue;
}
if (cp < 128)
out[o++] = (char)cp;
else if (cp <= 0xFFFF)
out[o++] = UNICODE_BMP_NORMALIZER[cp];
else
out[o++] = '?';
}
out[o] = '\0';
return o;
}
/* symbol font lookup: returns char code (0x20-0x7E) or -1 if not in font */
static int symbol_font_lookup(uint32_t cp)
{
/* box drawing: U+2500-U+257F */
if (cp >= 0x2500 && cp <= 0x257F)
{
switch (cp)
{
case 0x2500: return 0x9B; /* ─ (not 0x20 — space char doesn't render) */
case 0x2501: return 0x4C; /* ━ */
case 0x2502: return 0x21; /* │ */
case 0x2503: return 0x4D; /* ┃ */
case 0x2504: return 0x4F; /* ┄ */
case 0x250C: return 0x22; /* ┌ */
case 0x2510: return 0x23; /* ┐ */
case 0x2514: return 0x24; /* └ */
case 0x2518: return 0x25; /* ┘ */
case 0x251C: return 0x26; /* ├ */
case 0x2524: return 0x27; /* ┤ */
case 0x252C: return 0x28; /* ┬ */
case 0x2534: return 0x29; /* ┴ */
case 0x253C: return 0x2A; /* ┼ */
case 0x254B: return 0x4E; /* ╋ */
case 0x2550: return 0x2B; /* ═ */
case 0x2551: return 0x2C; /* ║ */
case 0x2552: return 0x36; /* ╒ */
case 0x2553: return 0x37; /* ╓ */
case 0x2554: return 0x2D; /* ╔ */
case 0x2555: return 0x38; /* ╕ */
case 0x2556: return 0x39; /* ╖ */
case 0x2557: return 0x2E; /* ╗ */
case 0x2558: return 0x3A; /* ╘ */
case 0x2559: return 0x3B; /* ╙ */
case 0x255A: return 0x2F; /* ╚ */
case 0x255B: return 0x3C; /* ╛ */
case 0x255C: return 0x3D; /* ╜ */
case 0x255D: return 0x30; /* ╝ */
case 0x255E: return 0x3E; /* ╞ */
case 0x255F: return 0x3F; /* ╟ */
case 0x2560: return 0x31; /* ╠ */
case 0x2561: return 0x40; /* ╡ */
case 0x2562: return 0x41; /* ╢ */
case 0x2563: return 0x32; /* ╣ */
case 0x2564: return 0x42; /* ╤ */
case 0x2565: return 0x43; /* ╥ */
case 0x2566: return 0x33; /* ╦ */
case 0x2567: return 0x44; /* ╧ */
case 0x2568: return 0x45; /* ╨ */
case 0x2569: return 0x34; /* ╩ */
case 0x256A: return 0x46; /* ╪ */
case 0x256B: return 0x47; /* ╫ */
case 0x256C: return 0x35; /* ╬ */
case 0x2574: return 0x48; /* ╴ */
case 0x2575: return 0x49; /* ╵ */
case 0x2576: return 0x4A; /* ╶ */
case 0x2577: return 0x4B; /* ╷ */
/* rounded corners -> same as regular corners */
case 0x256D: return 0x22; /* ╭ -> ┌ */
case 0x256E: return 0x23; /* ╮ -> ┐ */
case 0x256F: return 0x25; /* ╯ -> ┘ */
case 0x2570: return 0x24; /* ╰ -> └ */
/* dashed horizontal variants -> triple dash */
case 0x2505: return 0x4F; /* ┅ -> ┄ */
case 0x2508: return 0x4F; /* ┈ -> ┄ */
case 0x2509: return 0x4F; /* ┉ -> ┄ */
case 0x254C: return 0x4F; /* ╌ -> ┄ */
case 0x254D: return 0x4F; /* ╍ -> ┄ */
/* dashed vertical variants -> vertical line */
case 0x2506: return 0x21; /* ┆ -> │ */
case 0x2507: return 0x4D; /* ┇ -> ┃ */
case 0x250A: return 0x21; /* ┊ -> │ */
case 0x250B: return 0x4D; /* ┋ -> ┃ */
case 0x254E: return 0x21; /* ╎ -> │ */
case 0x254F: return 0x4D; /* ╏ -> ┃ */
/* heavy half lines */
case 0x2578: return 0x48; /* ╸ -> ╴ */
case 0x2579: return 0x49; /* ╹ -> ╵ */
case 0x257A: return 0x4A; /* ╺ -> ╶ */
case 0x257B: return 0x4B; /* ╻ -> ╷ */
case 0x257C: return 0x9B; /* ╼ -> ─ */
case 0x257D: return 0x21; /* ╽ -> │ */
case 0x257E: return 0x9B; /* ╾ -> ─ */
case 0x257F: return 0x21; /* ╿ -> │ */
/* bold corners -> regular corners */
case 0x2512: return 0x23; /* ┒ -> ┐ */
case 0x2513: return 0x23; /* ┓ -> ┐ */
case 0x2516: return 0x24; /* ┖ -> └ */
case 0x2517: return 0x24; /* ┗ -> └ */
case 0x251A: return 0x25; /* ┚ -> ┘ */
case 0x251B: return 0x25; /* ┛ -> ┘ */
case 0x250E: return 0x22; /* ┎ -> ┌ */
case 0x250F: return 0x22; /* ┏ -> ┌ */
case 0x2511: return 0x23; /* ┑ -> ┐ */
default: return -1;
}
}
/* block elements: U+2580-U+259F */
if (cp >= 0x2580 && cp <= 0x259F)
{
if (cp <= 0x258F) return 0x50 + (cp - 0x2580);
if (cp == 0x2590) return 0x60;
if (cp >= 0x2591 && cp <= 0x2595)
{
switch (cp)
{
case 0x2591: return 0x61;
case 0x2592: return 0x62;
case 0x2593: return 0x63;
case 0x2594: return 0x64;
case 0x2595: return 0x65;
}
}
if (cp >= 0x2596 && cp <= 0x259F)
return 0x90 + (cp - 0x2596);
return -1;
}
/* geometric shapes */
switch (cp)
{
case 0x25A0: return 0x66;
case 0x25A1: return 0x67;
case 0x25AA: return 0x68;
case 0x25AB: return 0x69;
case 0x25B2: return 0x6A;
case 0x25B6: return 0x6B;
case 0x25BC: return 0x6C;
case 0x25C0: return 0x6D;
case 0x25CB: return 0x6E;
case 0x25CF: return 0x6F;
}
/* card suits, smileys, music notes, etc */
switch (cp)
{
case 0x2660: return 0x70;
case 0x2663: return 0x71;
case 0x2665: return 0x72;
case 0x2666: return 0x73;
case 0x263A: return 0x74;
case 0x263B: return 0x75;
case 0x266A: return 0x76;
case 0x266B: return 0x77;
case 0x2713: return 0x78;
case 0x2717: return 0x79;
case 0x2190: return 0x7A;
case 0x2191: return 0x7B;
case 0x2192: return 0x7C;
case 0x2193: return 0x7D;
case 0x2194: return 0x7E;
}
/* small triangle variants -> use same glyph as full-size */
switch (cp)
{
case 0x25B4: return 0x6A; /* ▴ small up triangle -> ▲ */
case 0x25B8: return 0x6B; /* ▸ small right triangle -> ▶ */
case 0x25BE: return 0x6C; /* ▾ small down triangle -> ▼ */
case 0x25C2: return 0x6D; /* ◂ small left triangle -> ◀ */
}
/* Claude CLI: prompt marker + search icon */
if (cp == 0x23F5) return 0x7F; /* ⏵ */
if (cp == 0x276F) return 0x9A; /* ❯ pointer / prompt marker */
if (cp == 0x2315) return 0x8F; /* ⌕ telephone recorder / search icon */
/* Claude CLI: spinner dingbats */
switch (cp)
{
case 0x2217: return 0x80; /* ∗ */
case 0x2722: return 0x81; /* ✢ */
case 0x2733: return 0x82; /* ✳ */
case 0x2736: return 0x82; /* ✶ six-pointed star (Claude spinner) */
case 0x273B: return 0x83; /* ✻ */
case 0x273D: return 0x84; /* ✽ */
}
/* Claude CLI: braille spinner */
switch (cp)
{
case 0x280B: return 0x85; /* ⠋ */
case 0x2819: return 0x86; /* ⠙ */
case 0x2839: return 0x87; /* ⠹ */
case 0x2838: return 0x88; /* ⠸ */
case 0x283C: return 0x89; /* ⠼ */
case 0x2834: return 0x8A; /* ⠴ */
case 0x2826: return 0x8B; /* ⠦ */
case 0x2827: return 0x8C; /* ⠧ */
case 0x2807: return 0x8D; /* ⠇ */
case 0x280F: return 0x8E; /* ⠏ */
}
/* middle dot / separator */
switch (cp)
{
case 0x00B7: return 0xAD; /* · middle dot */
case 0x22C5: return 0xAD; /* ⋅ dot operator */
case 0x2027: return 0xAD; /* ‧ hyphenation point */
case 0x2024: return 0xAD; /* ․ one dot leader */
case 0x2219: return 0xAD; /* ∙ bullet operator */
}
/* figures/UI chars: diamonds, checks, boxes, stars, etc. */
switch (cp)
{
case 0x25C6: return 0x9C; /* ◆ black diamond (lozenge) */
case 0x25C7: return 0x9D; /* ◇ white diamond */
case 0x2714: return 0x9E; /* ✔ heavy check mark (tick) */
case 0x2718: return 0x9F; /* ✘ heavy ballot X (cross) */
case 0x25FB: return 0xA0; /* ◻ white medium square */
case 0x25FC: return 0xA1; /* ◼ black medium square */
case 0x25C9: return 0xA2; /* ◉ fisheye (radioOn) */
case 0x25EF: return 0xA3; /* ◯ large circle (radioOff) */
case 0x2610: return 0xA4; /* ☐ ballot box (checkboxOff) */
case 0x2612: return 0xA5; /* ☒ ballot box with X (checkboxOn) */
case 0x2605: return 0xA6; /* ★ black star */
case 0x2630: return 0xA7; /* ☰ trigram/hamburger */
case 0x25B3: return 0xA8; /* △ white up triangle */
case 0x26A0: return 0xA9; /* ⚠ warning sign */
case 0x25CE: return 0xAA; /* ◎ bullseye */
case 0x25CC: return 0xAB; /* ◌ dotted circle */
case 0x2139: return 0xAC; /* ℹ info */
}
return -1;
}
char key_to_vterm[256] = { VTERM_KEY_NONE };
int font_ascent = 0;
void setup_key_translation(void)
{
// TODO: figure out how to translate the rest of these
key_to_vterm[kEnterCharCode] = VTERM_KEY_ENTER;
key_to_vterm[kTabCharCode] = VTERM_KEY_TAB;
key_to_vterm[kBackspaceCharCode] = VTERM_KEY_BACKSPACE;
key_to_vterm[kEscapeCharCode] = VTERM_KEY_ESCAPE;
key_to_vterm[kUpArrowCharCode] = VTERM_KEY_UP;
key_to_vterm[kDownArrowCharCode] = VTERM_KEY_DOWN;
key_to_vterm[kLeftArrowCharCode] = VTERM_KEY_LEFT;
key_to_vterm[kRightArrowCharCode] = VTERM_KEY_RIGHT;
//key_to_vterm[0] = VTERM_KEY_INS;
key_to_vterm[kDeleteCharCode] = VTERM_KEY_DEL;
key_to_vterm[kHomeCharCode] = VTERM_KEY_HOME;
key_to_vterm[kEndCharCode] = VTERM_KEY_END;
key_to_vterm[kPageUpCharCode] = VTERM_KEY_PAGEUP;
key_to_vterm[kPageDownCharCode] = VTERM_KEY_PAGEDOWN;
}
inline Rect cell_rect(struct window_context* wc, int x, int y, Rect bounds)
{
short top_offset = (wc->num_sessions > 1) ? TAB_BAR_HEIGHT + 2 : 2;
Rect r = { (short) (bounds.top + y * con.cell_height + top_offset), (short) (bounds.left + x * con.cell_width + 2),
(short) (bounds.top + (y+1) * con.cell_height + top_offset), (short) (bounds.left + (x+1) * con.cell_width + 2) };
return r;
}
static void print_string_v(VTerm* vt, const char* c)
{
vterm_input_write(vt, c, strlen(c));
}
void print_string(const char* c)
{
print_string_v(ACTIVE_S.vterm, c);
}
inline void draw_char(struct window_context* wc, int x, int y, Rect* r, char c)
{
short top_offset = (wc->num_sessions > 1) ? TAB_BAR_HEIGHT : 0;
MoveTo(r->left + 2 + x * con.cell_width, r->top + 2 + font_ascent + (y * con.cell_height) + top_offset);
DrawChar(c);
}
void toggle_cursor(struct window_context* wc)
{
WC_S(wc).cursor_state = !WC_S(wc).cursor_state;
mark_dirty(&WC_S(wc), WC_S(wc).cursor_y, WC_S(wc).cursor_y + 1);
{
Rect cursor = cell_rect(wc, WC_S(wc).cursor_x, WC_S(wc).cursor_y, wc->win->portRect);
InvalRect(&cursor);
}
wc->needs_redraw = 1;
}
void check_cursor(struct window_context* wc)
{
long unsigned int now = TickCount();
if ((now - WC_S(wc).last_cursor_blink) > GetCaretTime())
{
toggle_cursor(wc);
WC_S(wc).last_cursor_blink = now;
}
}
void point_to_cell(struct window_context* wc, Point p, int* x, int* y)
{
short top_offset = (wc->num_sessions > 1) ? TAB_BAR_HEIGHT : 0;
*x = p.h / con.cell_width;
*y = (p.v - top_offset) / con.cell_height;
if (*x > wc->size_x) *x = wc->size_x;
if (*y > wc->size_y) *y = wc->size_y;
if (*y < 0) *y = 0;
}
void clear_selection(struct window_context* wc)
{
WC_S(wc).select_start_x = -1;
WC_S(wc).select_start_y = -1;
WC_S(wc).select_end_x = -1;
WC_S(wc).select_end_y = -1;
}
void damage_selection(struct window_context* wc)
{
int min_row = MIN(WC_S(wc).select_start_y, WC_S(wc).select_end_y);
int max_row = MAX(WC_S(wc).select_start_y, WC_S(wc).select_end_y);
Rect topleft = cell_rect(wc, 0, min_row, (wc->win->portRect));
Rect bottomright = cell_rect(wc, wc->size_x, max_row, (wc->win->portRect));
mark_dirty(&WC_S(wc), min_row, max_row + 1);
UnionRect(&topleft, &bottomright, &topleft);
InvalRect(&topleft);
wc->needs_redraw = 1;
}
void update_selection_end(struct window_context* wc)
{
static int last_mouse_cell_x = -1;
static int last_mouse_cell_y = -1;
int new_mouse_cell_x;
int new_mouse_cell_y;
Point new_mouse;
GetMouse(&new_mouse);
point_to_cell(wc, new_mouse, &new_mouse_cell_x, &new_mouse_cell_y);
// only damage the selection if the mouse has moved outside of the last cell
if (last_mouse_cell_x != new_mouse_cell_x || last_mouse_cell_y != new_mouse_cell_y)
{
// damage the old selection
damage_selection(wc);
WC_S(wc).select_end_x = new_mouse_cell_x;
WC_S(wc).select_end_y = new_mouse_cell_y;
last_mouse_cell_x = new_mouse_cell_x;
last_mouse_cell_y = new_mouse_cell_y;
// damage the new selection
damage_selection(wc);
}
}
inline void prev(int size_x, int* x, int* y)
{
if (*x == 0)
{
*x = size_x - 1;
*y = (*y) - 1;
}
else
{
*x = (*x) - 1;
}
}
inline void next(int size_x, int* x, int* y)
{
if (*x == size_x -1)
{
*x = 0;
*y = (*y) + 1;
}
else
{
*x = (*x) + 1;
}
}
void select_word(struct window_context* wc)
{
Point mouse;
GetMouse(&mouse);
VTermPos pos;
point_to_cell(wc, mouse, &pos.col, &pos.row);
pos.col++;
char c;
VTermScreenCell vtsc = {0};
// scan backwards from mouse click point
while (!(pos.col == 0 && pos.row == 0))
{
prev(wc->size_x, &pos.col, &pos.row);
int ok = vterm_screen_get_cell(WC_S(wc).vts, pos, &vtsc);
// TODO FIXME not really unicode safe
c = (char)vtsc.chars[0];
if (c == '\0' || c == ' ')
{
next(wc->size_x, &pos.col, &pos.row);
break;
}
}
WC_S(wc).select_start_x = pos.col;
WC_S(wc).select_start_y = pos.row;
// scan forwards from mouse click point
point_to_cell(wc, mouse, &pos.col, &pos.row);
pos.col--;
while (!(pos.col == wc->size_x - 1 && pos.row == wc->size_y -1))
{
next(wc->size_x, &pos.col, &pos.row);
int ok = vterm_screen_get_cell(WC_S(wc).vts, pos, &vtsc);
// TODO FIXME not really unicode safe
c = (char)vtsc.chars[0];
if (c == '\0' || c == ' ')
{
break;
}
}
WC_S(wc).select_end_x = pos.col;
WC_S(wc).select_end_y = pos.row;
damage_selection(wc);
}
// returns 1 if click was in tab bar, 0 otherwise
int tab_bar_click(struct window_context* wc, Point p)
{
if (wc->num_sessions <= 1) return 0;
if (p.v >= TAB_BAR_HEIGHT) return 0;
/* figure out which tab was clicked (exclude scrollbar area) */
int tab_width = (wc->win->portRect.right - wc->win->portRect.left - 16) / wc->num_sessions;
int clicked_tab = p.h / tab_width;
if (clicked_tab >= wc->num_sessions) clicked_tab = wc->num_sessions - 1;
// map to actual session index via window's session_ids
if (clicked_tab < wc->num_sessions)
{
int sid = wc->session_ids[clicked_tab];
// check if click is in close button area
int tab_left = clicked_tab * tab_width;
int close_left = tab_left + tab_width - 15;
if (wc->num_sessions > 1 && p.h >= close_left && p.h <= close_left + 11
&& p.v >= 4 && p.v <= 15)
{
close_session(sid);
}
else if (sid != wc->session_ids[wc->active_session_idx])
{
switch_session(wc, sid);
}
return 1;
}
return 1;
}
// p is in window local coordinates
void mouse_click(struct window_context* wc, Point p, int click)
{
static Point last_click;
static unsigned last_click_time = 0;
// check if click is in tab bar
if (click && tab_bar_click(wc, p)) return;
WC_S(wc).mouse_state = click;
if (WC_S(wc).mouse_mode == CLICK_SEND)
{
short top_offset = (wc->num_sessions > 1) ? TAB_BAR_HEIGHT : 0;
int row = (p.v - top_offset) / con.cell_height;
int col = p.h / con.cell_width;
if (row < 0) row = 0;
vterm_mouse_move(WC_S(wc).vterm, row, col, VTERM_MOD_NONE);
vterm_mouse_button(WC_S(wc).vterm, 1, click, VTERM_MOD_NONE);
}
else if (WC_S(wc).mouse_mode == CLICK_SELECT)
{
if (click)
{
// damage the old selection so it gets wiped from the screen
damage_selection(wc);
last_click = p;
// if it's a double click
if (TickCount() - last_click_time < GetDblTime())
{
select_word(wc);
}
else
{
point_to_cell(wc, p, &WC_S(wc).select_start_x, &WC_S(wc).select_start_y);
point_to_cell(wc, p, &WC_S(wc).select_end_x, &WC_S(wc).select_end_y);
update_selection_end(wc);
}
last_click_time = TickCount();
}
else
{
int a, b, c, d;
point_to_cell(wc, last_click, &a, &b);
point_to_cell(wc, p, &c, &d);
}
}
}
size_t get_selection(struct window_context* wc, char** selection)
{
if (WC_S(wc).select_start_x == -1 || WC_S(wc).select_start_y == -1)
{
*selection = NULL;
return 0;
}
int a = WC_S(wc).select_start_x + WC_S(wc).select_start_y * wc->size_x;
int b = WC_S(wc).select_end_x + WC_S(wc).select_end_y * wc->size_x;
ssize_t len = MAX(a,b) - MIN(a,b) + 1;
char* output = malloc(len + 1);
if (output == NULL) { *selection = NULL; return 0; }
int start_row = MIN(WC_S(wc).select_start_y, WC_S(wc).select_end_y);
int start_col = MIN(WC_S(wc).select_start_x, WC_S(wc).select_end_x);
VTermPos pos = {.row = start_row, .col = start_col-1};
VTermScreenCell vtsc = {0};
for(int i = 0; i < len; i++)
{
next(wc->size_x, &pos.col, &pos.row);
int ok = vterm_screen_get_cell(WC_S(wc).vts, pos, &vtsc);
// TODO FIXME not really unicode safe
output[i] = (char)vtsc.chars[0];
}
output[len] = '\0';
*selection = output;
return len;
}
/* draw_resize_corner is now handled by DrawGrowIcon in draw_screen(),
no clip hack needed with a real scrollbar present */
inline void draw_resize_corner(struct window_context* wc)
{
/* no-op: grow icon + scrollbar drawn via DrawControls/DrawGrowIcon in draw_screen */
}
void draw_tab_bar(struct window_context* wc)
{
if (wc->num_sessions <= 1) return;
short save_font = qd.thePort->txFont;
short save_font_size = qd.thePort->txSize;
short save_font_face = qd.thePort->txFace;
RGBColor save_rgb_fg, save_rgb_bg;
GetForeColor(&save_rgb_fg);
GetBackColor(&save_rgb_bg);
RGBColor rgb_white = {0xFFFF, 0xFFFF, 0xFFFF};
RGBColor rgb_black = {0, 0, 0};
RGBColor rgb_gray = {0x9999, 0x9999, 0x9999};
RGBColor rgb_dimtext = {0x5555, 0x5555, 0x5555};
RGBColor rgb_shadow = {0x5555, 0x5555, 0x5555};
RGBColor rgb_hilite = {0xDDDD, 0xDDDD, 0xDDDD};
TextFont(kFontIDGeneva);
TextSize(9);
TextFace(normal);
Rect portRect = wc->win->portRect;
int total_width = portRect.right - portRect.left - 16; /* exclude scrollbar */
int tab_width = total_width / wc->num_sessions;
int active_sid = wc->session_ids[wc->active_session_idx];
int tab_idx;
for (tab_idx = 0; tab_idx < wc->num_sessions; tab_idx++)
{
int sid = wc->session_ids[tab_idx];
Rect tab_rect;
tab_rect.left = portRect.left + tab_idx * tab_width;
tab_rect.right = (tab_idx == wc->num_sessions - 1) ? (portRect.left + total_width) : tab_rect.left + tab_width;
tab_rect.top = portRect.top;
tab_rect.bottom = portRect.top + TAB_BAR_HEIGHT;
if (sid == active_sid)
{
/* active tab: white background */
RGBBackColor(&rgb_white);
RGBForeColor(&rgb_black);
EraseRect(&tab_rect);
MoveTo(tab_rect.left, tab_rect.top);
LineTo(tab_rect.left, tab_rect.bottom - 1);
LineTo(tab_rect.right - 1, tab_rect.bottom - 1);
LineTo(tab_rect.right - 1, tab_rect.top);
}
else
{
/* inactive tab: darker gray with sunken inset border */
RGBBackColor(&rgb_gray);
EraseRect(&tab_rect);
/* sunken border: dark on top-left, light on bottom-right */
RGBForeColor(&rgb_shadow);
MoveTo(tab_rect.left, tab_rect.bottom - 1);
LineTo(tab_rect.left, tab_rect.top);
LineTo(tab_rect.right - 1, tab_rect.top);
RGBForeColor(&rgb_hilite);
MoveTo(tab_rect.right - 1, tab_rect.top + 1);
LineTo(tab_rect.right - 1, tab_rect.bottom - 1);
LineTo(tab_rect.left + 1, tab_rect.bottom - 1);
}
/* draw tab label */
RGBForeColor(sid == active_sid ? &rgb_black : &rgb_dimtext);
{
char* label = sessions[sid].tab_label;
int label_len = strlen(label);
int max_chars = (tab_width - 20) / CharWidth('M');
if (label_len > max_chars) label_len = max_chars;
MoveTo(tab_rect.left + 4, tab_rect.top + 14);
DrawText(label, 0, label_len);
}
/* draw close button (X) */
if (wc->num_sessions > 1)
{
short cx1 = tab_rect.right - 15;
short cy1 = tab_rect.top + 4;
short cx2 = cx1 + 11;
short cy2 = cy1 + 11;
RGBForeColor(sid == active_sid ? &rgb_black : &rgb_dimtext);
MoveTo(cx1, cy1);
LineTo(cx2, cy2);
MoveTo(cx2, cy1);
LineTo(cx1, cy2);
}
}
/* separator line between tab bar and terminal area */
RGBForeColor(&rgb_black);
MoveTo(portRect.left, portRect.top + TAB_BAR_HEIGHT);
LineTo(portRect.right, portRect.top + TAB_BAR_HEIGHT);
TextFont(save_font);
TextSize(save_font_size);
TextFace(save_font_face);
RGBForeColor(&save_rgb_fg);
RGBBackColor(&save_rgb_bg);
}
/* Get a cell accounting for scroll offset.
* display_row is the row on screen (0..size_y-1).
* If scrolled back, top rows come from the scrollback buffer,
* remaining rows from the live vterm screen. */
static int get_cell_scrolled(struct window_context* wc, int display_row, int col, VTermScreenCell* cell)
{
int sid = wc->session_ids[wc->active_session_idx];
struct session* s = &sessions[sid];
if (s->scroll_offset > 0 && display_row < s->scroll_offset)
{
/* this row comes from scrollback */
int sb_idx;
if (s->scrollback == NULL) return 0;
sb_idx = s->sb_head - s->scroll_offset + display_row;
if (sb_idx < 0) sb_idx += SCROLLBACK_LINES;