-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay.c
More file actions
2129 lines (1907 loc) · 59.6 KB
/
Copy pathdisplay.c
File metadata and controls
2129 lines (1907 loc) · 59.6 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
/*
* display.c — compositing, software cursor, VT handling.
* Scanout setup lives in display_fbdev.c (default) or display_drm.c (BGCE_USE_DRM).
*/
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "compositor.h"
#include "server.h"
#include <errno.h>
#include <fcntl.h>
#include <linux/kd.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stb_image_write.h>
extern struct ServerState server;
extern struct config config;
static int vt_fd = -1;
/* Previous KDSKBMODE so we can restore on exit (KDGKBMODE uses long). */
static long vt_saved_kbmode = -1;
/* 1 if we successfully set K_OFF and must undo it even when KDGKBMODE failed. */
static int vt_kb_off_active;
#ifndef K_OFF
/* Disabled keyboard → console (Linux ≥ 2.6.39). */
#define K_OFF 0x04
#endif
#ifndef K_XLATE
#define K_XLATE 0x00
#endif
#if CURSOR_WIDTH != 32 || CURSOR_HEIGHT != 32
#error "software cursor blit assumes CURSOR_WIDTH/HEIGHT == 32"
#endif
/* Software cursor: fixed 32×32 glyph + underlay, row memcpy onto fbdev. */
static uint32_t cur_img[32 * 32];
static uint32_t cur_underlay[32 * 32];
/* Logical hotspot (mouse) position — top-left of the 32×32 tile. */
static int cursor_x;
static int cursor_y;
/*
* Where cur_underlay was captured / the glyph was last drawn. Restoring
* always uses these coords, not cursor_x/y, so a move never leaves a trail
* if paint/restore get out of sync.
*/
static int cur_saved_x;
static int cur_saved_y;
static int cur_underlay_valid; /* 1 = FB has glyph; underlay is live */
/*
* Software cursor is painted only on the compositor (orchestrator) thread.
* Input enqueues COMP_CURSOR jobs; it never blits the glyph itself.
*/
static int cursor_dirty;
static enum BGCECursorType current_cursor = BGCE_CURSOR_DEFAULT;
/* Helper: set a single pixel (ARGB8888) with bounds check */
static inline void cur_px(uint32_t* buf, uint32_t stride_px, uint32_t w, uint32_t h,
int x, int y, uint32_t argb) {
if (x >= 0 && x < (int)w && y >= 0 && y < (int)h)
buf[y * stride_px + x] = argb;
}
/* Draw a filled line (horizontal) */
static void cur_hline(uint32_t* buf, uint32_t stride_px, uint32_t w, uint32_t h,
int x0, int x1, int y, uint32_t argb) {
for (int x = x0; x <= x1; x++)
cur_px(buf, stride_px, w, h, x, y, argb);
}
/* Draw a filled line (vertical) */
static void cur_vline(uint32_t* buf, uint32_t stride_px, uint32_t w, uint32_t h,
int x, int y0, int y1, uint32_t argb) {
for (int y = y0; y <= y1; y++)
cur_px(buf, stride_px, w, h, x, y, argb);
}
/* Outline color and fill color for all cursors */
#define CUR_BLACK 0xFF000000u
#define CUR_WHITE 0xFFFFFFFFu
/* ---------- Individual cursor renderers ---------- */
static void draw_cursor_default(uint32_t* buf, uint32_t sp, uint32_t w, uint32_t h) {
/* Classic arrow pointer, ~20px tall */
/* Outline */
static const int arrow[][2] = {
{0,0},{0,1},{0,2},{0,3},{0,4},{0,5},{0,6},{0,7},{0,8},{0,9},{0,10},{0,11},{0,12},{0,13},{0,14},{0,15},{0,16},
{1,1},{1,2},{1,3},{1,4},{1,5},{1,6},{1,7},{1,8},{1,9},{1,10},{1,11},{1,12},{1,13},{1,14},{1,15},
{2,2},{2,3},{2,4},{2,5},{2,6},{2,7},{2,8},{2,9},{2,10},{2,11},{2,12},{2,13},{2,14},
{3,3},{3,4},{3,5},{3,6},{3,7},{3,8},{3,9},{3,10},{3,11},{3,12},{3,13},
{4,4},{4,5},{4,6},{4,7},{4,8},{4,9},{4,10},{4,11},{4,12},
{5,5},{5,6},{5,7},{5,8},{5,9},{5,10},{5,11},
{6,6},{6,7},{6,8},{6,9},{6,10},
{7,7},{7,8},{7,9},
{8,8},
};
/* White fill (interior) */
for (size_t i = 0; i < sizeof(arrow)/sizeof(arrow[0]); i++)
cur_px(buf, sp, w, h, arrow[i][0], arrow[i][1], CUR_WHITE);
/* Black outline around the arrow */
/* Left edge */
for (int y = 0; y <= 16; y++)
cur_px(buf, sp, w, h, 0, y, CUR_BLACK);
/* Diagonal right edge */
for (int i = 0; i <= 16; i++)
cur_px(buf, sp, w, h, (i+1)/2, i, CUR_BLACK);
/* Bottom horizontal at y=11 */
for (int x = 0; x <= 6; x++)
cur_px(buf, sp, w, h, x, 11, CUR_BLACK);
/* Bottom tip */
cur_px(buf, sp, w, h, 0, 16, CUR_BLACK);
cur_px(buf, sp, w, h, 0, 17, CUR_BLACK);
/* Diagonal from (6,11) -> (3,17) for the notch */
cur_px(buf, sp, w, h, 6, 11, CUR_BLACK);
cur_px(buf, sp, w, h, 5, 12, CUR_BLACK);
cur_px(buf, sp, w, h, 5, 13, CUR_BLACK);
cur_px(buf, sp, w, h, 4, 14, CUR_BLACK);
cur_px(buf, sp, w, h, 4, 15, CUR_BLACK);
cur_px(buf, sp, w, h, 3, 16, CUR_BLACK);
cur_px(buf, sp, w, h, 3, 17, CUR_BLACK);
/* Tail shaft from notch */
cur_px(buf, sp, w, h, 3, 12, CUR_BLACK);
cur_px(buf, sp, w, h, 3, 13, CUR_BLACK);
cur_px(buf, sp, w, h, 2, 14, CUR_BLACK);
cur_px(buf, sp, w, h, 2, 15, CUR_BLACK);
cur_px(buf, sp, w, h, 1, 16, CUR_BLACK);
cur_px(buf, sp, w, h, 1, 17, CUR_BLACK);
/* White fill for shaft */
cur_px(buf, sp, w, h, 4, 12, CUR_WHITE);
cur_px(buf, sp, w, h, 4, 13, CUR_WHITE);
cur_px(buf, sp, w, h, 3, 14, CUR_WHITE);
cur_px(buf, sp, w, h, 3, 15, CUR_WHITE);
cur_px(buf, sp, w, h, 2, 16, CUR_WHITE);
cur_px(buf, sp, w, h, 2, 17, CUR_WHITE);
}
static void draw_cursor_text(uint32_t* buf, uint32_t sp, uint32_t w, uint32_t h) {
/* I-beam cursor, centered at x=7 */
int cx = 7;
/* Top serif */
cur_hline(buf, sp, w, h, cx - 4, cx + 4, 0, CUR_BLACK);
cur_hline(buf, sp, w, h, cx - 3, cx + 3, 1, CUR_BLACK);
/* Vertical bar */
cur_vline(buf, sp, w, h, cx, 2, 17, CUR_BLACK);
cur_vline(buf, sp, w, h, cx - 1, 2, 17, CUR_WHITE);
cur_vline(buf, sp, w, h, cx + 1, 2, 17, CUR_WHITE);
/* Bottom serif */
cur_hline(buf, sp, w, h, cx - 3, cx + 3, 18, CUR_BLACK);
cur_hline(buf, sp, w, h, cx - 4, cx + 4, 19, CUR_BLACK);
}
static void draw_cursor_hand(uint32_t* buf, uint32_t sp, uint32_t w, uint32_t h) {
/* Pointing hand — finger up, simplified */
/* Index finger */
cur_vline(buf, sp, w, h, 6, 0, 7, CUR_BLACK);
cur_vline(buf, sp, w, h, 7, 0, 7, CUR_WHITE);
cur_vline(buf, sp, w, h, 8, 0, 7, CUR_WHITE);
cur_vline(buf, sp, w, h, 9, 0, 7, CUR_BLACK);
/* Other fingers opening at y=8 */
for (int f = 0; f < 4; f++) {
int bx = 3 + f * 3;
cur_vline(buf, sp, w, h, bx, 8, 12, CUR_BLACK);
cur_vline(buf, sp, w, h, bx + 1, 8, 12, CUR_WHITE);
cur_vline(buf, sp, w, h, bx + 2, 8, 12, CUR_BLACK);
}
/* Palm */
for (int y = 13; y <= 18; y++) {
cur_px(buf, sp, w, h, 3, y, CUR_BLACK);
cur_hline(buf, sp, w, h, 4, 13, y, CUR_WHITE);
cur_px(buf, sp, w, h, 14, y, CUR_BLACK);
}
/* Bottom */
cur_hline(buf, sp, w, h, 3, 14, 19, CUR_BLACK);
}
static void draw_cursor_resize_ns(uint32_t* buf, uint32_t sp, uint32_t w, uint32_t h) {
/* Vertical double-headed arrow, centered at (7, 10) */
int cx = 7;
/* Up arrow head */
for (int i = 0; i < 5; i++) {
cur_hline(buf, sp, w, h, cx - i, cx + i, 4 + i, CUR_BLACK);
if (i > 0)
cur_hline(buf, sp, w, h, cx - i + 1, cx + i - 1, 4 + i, CUR_WHITE);
}
cur_hline(buf, sp, w, h, cx - 4, cx + 4, 8, CUR_BLACK);
/* Shaft */
cur_vline(buf, sp, w, h, cx - 1, 9, 12, CUR_BLACK);
cur_vline(buf, sp, w, h, cx, 9, 12, CUR_WHITE);
cur_vline(buf, sp, w, h, cx + 1, 9, 12, CUR_BLACK);
/* Down arrow head */
cur_hline(buf, sp, w, h, cx - 4, cx + 4, 13, CUR_BLACK);
for (int i = 0; i < 5; i++) {
cur_hline(buf, sp, w, h, cx - 4 + i, cx + 4 - i, 13 + i, CUR_BLACK);
if (i > 0 && 4 - i > 0)
cur_hline(buf, sp, w, h, cx - 4 + i + 1, cx + 4 - i - 1, 13 + i, CUR_WHITE);
}
}
static void draw_cursor_resize_ew(uint32_t* buf, uint32_t sp, uint32_t w, uint32_t h) {
/* Horizontal double-headed arrow, centered at (9, 7) */
int cy = 7;
/* Left arrow head */
for (int i = 0; i < 5; i++) {
cur_vline(buf, sp, w, h, 2 + i, cy - i, cy + i, CUR_BLACK);
if (i > 0)
cur_vline(buf, sp, w, h, 2 + i, cy - i + 1, cy + i - 1, CUR_WHITE);
}
cur_vline(buf, sp, w, h, 6, cy - 4, cy + 4, CUR_BLACK);
/* Shaft */
cur_hline(buf, sp, w, h, 7, 11, cy - 1, CUR_BLACK);
cur_hline(buf, sp, w, h, 7, 11, cy, CUR_WHITE);
cur_hline(buf, sp, w, h, 7, 11, cy + 1, CUR_BLACK);
/* Right arrow head */
cur_vline(buf, sp, w, h, 12, cy - 4, cy + 4, CUR_BLACK);
for (int i = 0; i < 5; i++) {
cur_vline(buf, sp, w, h, 12 + i, cy - 4 + i, cy + 4 - i, CUR_BLACK);
if (i > 0 && 4 - i > 0)
cur_vline(buf, sp, w, h, 12 + i, cy - 4 + i + 1, cy + 4 - i - 1, CUR_WHITE);
}
}
static void draw_cursor_resize_nwse(uint32_t* buf, uint32_t sp, uint32_t w, uint32_t h) {
/* Diagonal (NW-SE) double arrow */
/* NW arrow head */
for (int i = 0; i < 6; i++) {
cur_px(buf, sp, w, h, i, 0, CUR_BLACK);
cur_px(buf, sp, w, h, 0, i, CUR_BLACK);
}
for (int i = 1; i < 5; i++) {
cur_px(buf, sp, w, h, i, 1, CUR_WHITE);
cur_px(buf, sp, w, h, 1, i, CUR_WHITE);
}
/* Diagonal shaft */
for (int i = 3; i <= 14; i++) {
cur_px(buf, sp, w, h, i - 1, i, CUR_BLACK);
cur_px(buf, sp, w, h, i, i, CUR_WHITE);
cur_px(buf, sp, w, h, i + 1, i, CUR_BLACK);
}
/* SE arrow head */
for (int i = 0; i < 6; i++) {
cur_px(buf, sp, w, h, 17 - i, 17, CUR_BLACK);
cur_px(buf, sp, w, h, 17, 17 - i, CUR_BLACK);
}
for (int i = 1; i < 5; i++) {
cur_px(buf, sp, w, h, 17 - i, 16, CUR_WHITE);
cur_px(buf, sp, w, h, 16, 17 - i, CUR_WHITE);
}
}
static void draw_cursor_move(uint32_t* buf, uint32_t sp, uint32_t w, uint32_t h) {
/* Four-way arrow, centered at (9, 9) */
int cx = 9, cy = 9;
/* Horizontal bar */
cur_hline(buf, sp, w, h, 2, 16, cy - 1, CUR_BLACK);
cur_hline(buf, sp, w, h, 2, 16, cy, CUR_WHITE);
cur_hline(buf, sp, w, h, 2, 16, cy + 1, CUR_BLACK);
/* Vertical bar */
cur_vline(buf, sp, w, h, cx - 1, 2, 16, CUR_BLACK);
cur_vline(buf, sp, w, h, cx, 2, 16, CUR_WHITE);
cur_vline(buf, sp, w, h, cx + 1, 2, 16, CUR_BLACK);
/* Up arrow */
for (int i = 0; i < 4; i++) {
cur_px(buf, sp, w, h, cx - i, 2 + i, CUR_BLACK);
cur_px(buf, sp, w, h, cx + i, 2 + i, CUR_BLACK);
}
/* Down arrow */
for (int i = 0; i < 4; i++) {
cur_px(buf, sp, w, h, cx - i, 16 - i, CUR_BLACK);
cur_px(buf, sp, w, h, cx + i, 16 - i, CUR_BLACK);
}
/* Left arrow */
for (int i = 0; i < 4; i++) {
cur_px(buf, sp, w, h, 2 + i, cy - i, CUR_BLACK);
cur_px(buf, sp, w, h, 2 + i, cy + i, CUR_BLACK);
}
/* Right arrow */
for (int i = 0; i < 4; i++) {
cur_px(buf, sp, w, h, 16 - i, cy - i, CUR_BLACK);
cur_px(buf, sp, w, h, 16 - i, cy + i, CUR_BLACK);
}
}
/* Render the currently selected cursor into the cursor buffer.
* If a cursor image was loaded from the config, blit it directly;
* otherwise fall back to the built-in procedural shape. */
static void render_cursor(uint32_t* buf, uint32_t stride_px, uint32_t w, uint32_t h) {
memset(buf, 0, h * stride_px * 4); /* clear to fully transparent */
/* Try loaded image first */
if (current_cursor >= 0 && current_cursor < BGCE_CURSOR_COUNT &&
config.cursors.images[current_cursor]) {
uint32_t* src = config.cursors.images[current_cursor];
for (uint32_t y = 0; y < h; y++)
memcpy(buf + y * stride_px, src + y * w, w * sizeof(uint32_t));
return;
}
/* Built-in fallback */
switch (current_cursor) {
case BGCE_CURSOR_TEXT: draw_cursor_text(buf, stride_px, w, h); break;
case BGCE_CURSOR_HAND: draw_cursor_hand(buf, stride_px, w, h); break;
case BGCE_CURSOR_RESIZE_NS: draw_cursor_resize_ns(buf, stride_px, w, h); break;
case BGCE_CURSOR_RESIZE_EW: draw_cursor_resize_ew(buf, stride_px, w, h); break;
case BGCE_CURSOR_RESIZE_NWSE: draw_cursor_resize_nwse(buf, stride_px, w, h); break;
case BGCE_CURSOR_MOVE: draw_cursor_move(buf, stride_px, w, h); break;
default: draw_cursor_default(buf, stride_px, w, h); break;
}
}
static void cursor_restore(void);
static void cursor_paint(void);
static uint32_t display_stride_px(const struct ServerState *srv)
{
if (srv->display_pitch >= srv->display_w * 4)
return srv->display_pitch / 4;
return srv->display_w;
}
/* Branchless src-over; a==0 keeps dst, a==255 keeps src. */
static inline uint32_t blend32(uint32_t s, uint32_t d)
{
uint32_t a = s >> 24;
uint32_t na = 255u - a;
uint32_t sr = (s >> 16) & 255u, sg = (s >> 8) & 255u, sb = s & 255u;
uint32_t dr = (d >> 16) & 255u, dg = (d >> 8) & 255u, db = d & 255u;
return (255u << 24)
| (((sr * a + dr * na) / 255u) << 16)
| (((sg * a + dg * na) / 255u) << 8)
| ((sb * a + db * na) / 255u);
}
/*
* Blend a contiguous 32×32 block in place (1024 px). Unrolled — no row loop.
* sq holds underlay on entry, composited result on exit; src is the glyph.
*/
static void blend_sq32(uint32_t sq[32 * 32], const uint32_t src[32 * 32])
{
#define B(i) sq[i] = blend32(src[i], sq[i])
#define BROW(o) \
B((o)+0); B((o)+1); B((o)+2); B((o)+3); \
B((o)+4); B((o)+5); B((o)+6); B((o)+7); \
B((o)+8); B((o)+9); B((o)+10); B((o)+11); \
B((o)+12); B((o)+13); B((o)+14); B((o)+15); \
B((o)+16); B((o)+17); B((o)+18); B((o)+19); \
B((o)+20); B((o)+21); B((o)+22); B((o)+23); \
B((o)+24); B((o)+25); B((o)+26); B((o)+27); \
B((o)+28); B((o)+29); B((o)+30); B((o)+31)
BROW(0); BROW(32); BROW(64); BROW(96);
BROW(128); BROW(160); BROW(192); BROW(224);
BROW(256); BROW(288); BROW(320); BROW(352);
BROW(384); BROW(416); BROW(448); BROW(480);
BROW(512); BROW(544); BROW(576); BROW(608);
BROW(640); BROW(672); BROW(704); BROW(736);
BROW(768); BROW(800); BROW(832); BROW(864);
BROW(896); BROW(928); BROW(960); BROW(992);
#undef BROW
#undef B
}
/* fb rows are pitch-separated; gather/scatter one 32×32 tile (unrolled memcpy). */
static void fb_gather32(uint32_t sq[32 * 32], uint32_t *fb, uint32_t sp, int x, int y)
{
size_t n = 32 * sizeof(uint32_t);
#define G(r) memcpy(sq + (r) * 32, fb + (y + (r)) * (int)sp + x, n)
G(0); G(1); G(2); G(3); G(4); G(5); G(6); G(7);
G(8); G(9); G(10); G(11); G(12); G(13); G(14); G(15);
G(16); G(17); G(18); G(19); G(20); G(21); G(22); G(23);
G(24); G(25); G(26); G(27); G(28); G(29); G(30); G(31);
#undef G
}
static void fb_scatter32(uint32_t *fb, uint32_t sp, int x, int y,
const uint32_t sq[32 * 32])
{
size_t n = 32 * sizeof(uint32_t);
#define S(r) memcpy(fb + (y + (r)) * (int)sp + x, sq + (r) * 32, n)
S(0); S(1); S(2); S(3); S(4); S(5); S(6); S(7);
S(8); S(9); S(10); S(11); S(12); S(13); S(14); S(15);
S(16); S(17); S(18); S(19); S(20); S(21); S(22); S(23);
S(24); S(25); S(26); S(27); S(28); S(29); S(30); S(31);
#undef S
}
static void clamp_cursor_pos(int *x, int *y)
{
int max_x = (int)server.display_w - 32;
int max_y = (int)server.display_h - 32;
if (max_x < 0) max_x = 0;
if (max_y < 0) max_y = 0;
if (*x < 0) *x = 0;
if (*y < 0) *y = 0;
if (*x > max_x) *x = max_x;
if (*y > max_y) *y = max_y;
}
int display_cursor_init(void)
{
render_cursor(cur_img, 32, 32, 32);
cursor_x = (int)server.display_w / 2;
cursor_y = (int)server.display_h / 2;
clamp_cursor_pos(&cursor_x, &cursor_y);
/* Not visible yet: first scene draw restores (no-op) then paints.
* Painting here would capture a black/empty underlay and leave a
* permanent black square on the first mouse move. */
cur_underlay_valid = 0;
cur_saved_x = cursor_x;
cur_saved_y = cursor_y;
return 0;
}
void display_cursor_fini(void)
{
cursor_restore();
}
/*
* Put back the pixels under the last painted glyph. Uses cur_saved_* so a
* pending move of cursor_x/y cannot restore the wrong square.
*/
static void cursor_restore(void)
{
uint32_t sp;
if (!cur_underlay_valid || !server.framebuffer)
return;
sp = display_stride_px(&server);
fb_scatter32(server.framebuffer, sp, cur_saved_x, cur_saved_y,
cur_underlay);
cur_underlay_valid = 0;
}
/*
* Draw the glyph at cursor_x/y. Always lifts any previous glyph first so
* calling paint twice never bakes a pointer into the underlay.
* Compositor thread only.
*/
static void cursor_paint(void)
{
uint32_t sp;
uint32_t block[32 * 32];
if (!server.framebuffer)
return;
if (cur_underlay_valid)
cursor_restore();
sp = display_stride_px(&server);
fb_gather32(block, server.framebuffer, sp, cursor_x, cursor_y);
memcpy(cur_underlay, block, sizeof(block));
blend_sq32(block, cur_img);
fb_scatter32(server.framebuffer, sp, cursor_x, cursor_y, block);
cur_saved_x = cursor_x;
cur_saved_y = cursor_y;
cur_underlay_valid = 1;
cursor_dirty = 0;
}
/* True if [x0,x1)×[y0,y1) intersects the cursor's 32×32 tile. */
static int cursor_tile_overlaps(int x0, int y0, int x1, int y1)
{
int cx = cur_underlay_valid ? cur_saved_x : cursor_x;
int cy = cur_underlay_valid ? cur_saved_y : cursor_y;
int cx1 = cx + 32;
int cy1 = cy + 32;
return x0 < cx1 && x1 > cx && y0 < cy1 && y1 > cy;
}
static int cursor_lift_for_rect_ret(int x0, int y0, int x1, int y1)
{
if (!cur_underlay_valid)
return 0;
if (!cursor_tile_overlaps(x0, y0, x1, y1))
return 0;
cursor_restore();
return 1;
}
static int cursor_lift_all_ret(void)
{
if (!cur_underlay_valid)
return 0;
cursor_restore();
return 1;
}
/*
* Begin/end scene paint on the compositor thread. Glyph is lifted for the
* duration; restored at end if need_present (or left dirty for a later
* COMP_CURSOR).
*/
static void cursor_fb_begin(void)
{
(void)cursor_lift_all_ret();
}
static void cursor_fb_end(int need_present)
{
if (need_present)
cursor_dirty = 1;
if (server.framebuffer && (cursor_dirty || !cur_underlay_valid))
cursor_paint();
}
int display_cursor_pending(void)
{
return cursor_dirty || !cur_underlay_valid;
}
void display_cursor_present(void)
{
if (!server.framebuffer)
return;
if (cursor_dirty || !cur_underlay_valid)
cursor_paint();
}
void display_cursor_refresh(void)
{
cursor_restore();
cursor_paint();
}
/*
* Move the software cursor and paint it. Must run on the compositor
* thread (via COMP_CURSOR or after a scene job). Input only enqueues.
*/
void set_cursor_pos(struct ServerState *srv, int x, int y)
{
(void)srv;
clamp_cursor_pos(&x, &y);
if (x == cursor_x && y == cursor_y) {
if (cursor_dirty || !cur_underlay_valid)
cursor_paint();
return;
}
cursor_restore();
cursor_x = x;
cursor_y = y;
cursor_paint();
}
void set_cursor_type(enum BGCECursorType type)
{
if (type < 0 || type >= BGCE_CURSOR_COUNT)
type = BGCE_CURSOR_DEFAULT;
if (type == current_cursor && cur_underlay_valid && !cursor_dirty)
return;
current_cursor = type;
/*
* Rebuild glyph only. Caller should enqueue COMP_CURSOR so the
* compositor paints (safe for client threads).
*/
render_cursor(cur_img, 32, 32, 32);
cursor_dirty = 1;
}
int setup_vt_handling(void) {
/*
* KD_GRAPHICS only stops the *console driver drawing* text.
* Keys still go to the VT line discipline unless we also disable
* keyboard mode (K_OFF). Without that, getty/shell/new apps on the
* same tty still see keystrokes while bgce reads /dev/input.
*/
vt_fd = open("/dev/tty", O_RDWR | O_CLOEXEC);
if (vt_fd < 0)
vt_fd = open("/dev/tty0", O_RDWR | O_CLOEXEC);
if (vt_fd < 0) {
fprintf(stderr, "[BGCE] VT: cannot open tty: %s "
"(keystrokes may still go to the console)\n",
strerror(errno));
return -1;
}
vt_kb_off_active = 0;
vt_saved_kbmode = -1;
if (ioctl(vt_fd, KDGKBMODE, &vt_saved_kbmode) < 0) {
fprintf(stderr, "[BGCE] VT: KDGKBMODE failed: %s "
"(will restore K_XLATE on exit)\n",
strerror(errno));
vt_saved_kbmode = -1;
}
if (ioctl(vt_fd, KDSETMODE, KD_GRAPHICS) < 0) {
fprintf(stderr, "[BGCE] VT: KDSETMODE KD_GRAPHICS failed: %s "
"(keystrokes may still go to the console)\n",
strerror(errno));
close(vt_fd);
vt_fd = -1;
vt_saved_kbmode = -1;
return -1;
}
/*
* Silence the VT keyboard: no more cooked input / Ctrl+C / echo for
* processes on this console. bgce keeps using /dev/input/event*.
*/
if (ioctl(vt_fd, KDSKBMODE, (long)K_OFF) < 0) {
fprintf(stderr, "[BGCE] VT: KDSKBMODE K_OFF failed: %s "
"(keys may still reach the tty)\n",
strerror(errno));
} else {
vt_kb_off_active = 1;
printf("[BGCE] VT: KD_GRAPHICS + keyboard off "
"(input via /dev/input only)\n");
}
return 0;
}
/*
* Always put the console back to a usable state. Leaving K_OFF active
* freezes the tty (no keyboard) after quit — that was the exit bug.
*/
void release_vt(void) {
int fd = vt_fd;
long mode;
/* Re-open if needed so we can still recover after a partial teardown. */
if (fd < 0) {
fd = open("/dev/tty", O_RDWR | O_CLOEXEC);
if (fd < 0)
fd = open("/dev/tty0", O_RDWR | O_CLOEXEC);
if (fd < 0)
return;
}
/*
* Prefer the saved mode; never restore K_OFF. If we never saw a
* mode (KDGKBMODE failed), fall back to K_XLATE so the shell works.
* K_XLATE is 0 — do not use ">= 0" alone as a "valid" flag.
*/
if (vt_saved_kbmode >= 0 && vt_saved_kbmode != (long)K_OFF)
mode = vt_saved_kbmode;
else
mode = (long)K_XLATE;
/* Keyboard first so the console can accept input as soon as text is on. */
if (vt_kb_off_active || vt_saved_kbmode >= 0) {
if (ioctl(fd, KDSKBMODE, mode) < 0) {
fprintf(stderr, "[BGCE] VT: restore KDSKBMODE %ld failed: %s; "
"trying K_XLATE\n",
mode, strerror(errno));
if (ioctl(fd, KDSKBMODE, (long)K_XLATE) < 0)
fprintf(stderr, "[BGCE] VT: K_XLATE restore failed: %s\n",
strerror(errno));
}
}
if (ioctl(fd, KDSETMODE, KD_TEXT) < 0)
fprintf(stderr, "[BGCE] VT: KDSETMODE KD_TEXT failed: %s\n",
strerror(errno));
else
printf("[BGCE] VT: restored KD_TEXT + keyboard mode\n");
if (fd == vt_fd)
vt_fd = -1;
close(fd);
vt_saved_kbmode = -1;
vt_kb_off_active = 0;
}
/* ------------------------------------------------------------------
* Viewport / coordinate transforms
* ------------------------------------------------------------------ */
/* Zoom percent helpers (all integer). screen = world * pct / 100 */
static int zoom_pct_of(const struct ServerState *srv)
{
int z = srv && srv->zoom_pct > 0 ? srv->zoom_pct : BGCE_ZOOM_PCT_1X;
if (z < BGCE_ZOOM_PCT_MIN)
z = BGCE_ZOOM_PCT_MIN;
if (z > BGCE_ZOOM_PCT_MAX)
z = BGCE_ZOOM_PCT_MAX;
return z;
}
/*
* pan_x/y are screen-pixel offsets (see server.h):
* sx = (wx * z) / 100 - pan_x
* wx = (sx + pan_x) * 100 / z
*/
void screen_to_world(const struct ServerState *srv, int sx, int sy,
int *wx, int *wy)
{
int z = zoom_pct_of(srv);
if (wx)
*wx = (sx + srv->pan_x) * 100 / z;
if (wy)
*wy = (sy + srv->pan_y) * 100 / z;
}
void world_to_screen(const struct ServerState *srv, int wx, int wy,
int *sx, int *sy)
{
int z = zoom_pct_of(srv);
if (sx)
*sx = (wx * z) / 100 - srv->pan_x;
if (sy)
*sy = (wy * z) / 100 - srv->pan_y;
}
int bgce_zoom_set(struct ServerState *srv, int zoom_pct)
{
int old;
if (!srv)
return 0;
if (zoom_pct < BGCE_ZOOM_PCT_MIN)
zoom_pct = BGCE_ZOOM_PCT_MIN;
if (zoom_pct > BGCE_ZOOM_PCT_MAX)
zoom_pct = BGCE_ZOOM_PCT_MAX;
old = srv->zoom_pct > 0 ? srv->zoom_pct : BGCE_ZOOM_PCT_1X;
if (zoom_pct == old)
return 0;
srv->zoom_pct = zoom_pct;
return 1;
}
int bgce_zoom_step(struct ServerState *srv, int dir)
{
int cur, next;
if (!srv || dir == 0)
return 0;
cur = srv->zoom_pct > 0 ? srv->zoom_pct : BGCE_ZOOM_PCT_1X;
next = cur + (dir > 0 ? BGCE_ZOOM_PCT_STEP : -BGCE_ZOOM_PCT_STEP);
return bgce_zoom_set(srv, next);
}
void clamp_viewport(struct ServerState *srv)
{
int z, max_x, max_y;
if (!srv)
return;
z = zoom_pct_of(srv);
srv->zoom_pct = z;
/* Max pan in screen pixels: world [0, virtual) must cover the view. */
max_x = (int)srv->virtual_w * z / 100 - (int)srv->display_w;
max_y = (int)srv->virtual_h * z / 100 - (int)srv->display_h;
if (max_x < 0)
srv->pan_x = max_x / 2;
else {
if (srv->pan_x < 0)
srv->pan_x = 0;
if (srv->pan_x > max_x)
srv->pan_x = max_x;
}
if (max_y < 0)
srv->pan_y = max_y / 2;
else {
if (srv->pan_y < 0)
srv->pan_y = 0;
if (srv->pan_y > max_y)
srv->pan_y = max_y;
}
}
/*
* Screen-space axis-aligned bounds of a client (exclusive end).
*
* Screen pixel (sx,sy) samples world
* wx = floor((sx + pan_x) * 100 / z)
* (see blit_client_scaled / screen_to_world). The bounds must cover every
* pixel whose sample lands in [x, x+ww) × [y, y+wh):
*
* sx0 = ceil(x * z / 100) - pan_x
* sx1 = ceil((x + ww) * z / 100) - pan_x (exclusive)
*
* Using floor(world_to_screen) for both corners under-covers the trailing
* edge and can leave 1px hairs / soft edges when the window moves slowly
* (especially at zoom ≠ 100%).
*/
static void client_screen_bounds(const struct ServerState *srv,
const struct Client *cli,
int *sx0, int *sy0, int *sx1, int *sy1)
{
uint32_t ww = cli->world_w ? cli->world_w : cli->width;
uint32_t wh = cli->world_h ? cli->world_h : cli->height;
int z = zoom_pct_of(srv);
int x = (int)cli->x;
int y = (int)cli->y;
int w = (int)ww;
int h = (int)wh;
/* ceil(n/100) for n >= 0 is (n + 99) / 100 */
*sx0 = (x * z + 99) / 100 - srv->pan_x;
*sy0 = (y * z + 99) / 100 - srv->pan_y;
*sx1 = ((x + w) * z + 99) / 100 - srv->pan_x;
*sy1 = ((y + h) * z + 99) / 100 - srv->pan_y;
if (*sx1 <= *sx0)
*sx1 = *sx0 + 1;
if (*sy1 <= *sy0)
*sy1 = *sy0 + 1;
}
/*
* Fill a horizontal run of identical pixels (unrolled like blend_sq32).
*/
static inline void fill_u32(uint32_t *p, int n, uint32_t v)
{
int i = 0;
for (; i + 8 <= n; i += 8) {
p[i + 0] = v; p[i + 1] = v; p[i + 2] = v; p[i + 3] = v;
p[i + 4] = v; p[i + 5] = v; p[i + 6] = v; p[i + 7] = v;
}
for (; i < n; i++)
p[i] = v;
}
/* Forward decl — used by wallpaper_fill before its definition. */
static int clip_to_display(const struct ServerState *srv,
int *x0, int *y0, int *x1, int *y1);
/* Positive modulo for world coords that can go slightly negative. */
static int wallpaper_mod(int a, int m)
{
int r;
if (m <= 0)
return 0;
r = a % m;
if (r < 0)
r += m;
return r;
}
/*
* Procedural wallpaper into a screen damage rect. Samples solid color or
* a small source image (tiled / scaled over the virtual desktop). No
* full-world buffer — only the damage rect is written.
*/
void wallpaper_fill_screen_rect(struct ServerState *srv,
int sx0, int sy0, int sx1, int sy1)
{
uint32_t *dst;
uint32_t stride;
int z, pan_x, pan_y;
uint32_t color;
int y, x;
int vw, vh;
if (!srv || !srv->framebuffer)
return;
if (!clip_to_display(srv, &sx0, &sy0, &sx1, &sy1))
return;
dst = (uint32_t *)srv->framebuffer;
stride = display_stride_px(srv);
z = zoom_pct_of(srv);
pan_x = srv->pan_x;
pan_y = srv->pan_y;
color = wallpaper.color;
vw = srv->virtual_w > 0 ? (int)srv->virtual_w : 1;
vh = srv->virtual_h > 0 ? (int)srv->virtual_h : 1;
/* Fast path: solid color (default and image-load fallback). */
if (wallpaper.type != BG_IMAGE || !wallpaper.src ||
wallpaper.src_w == 0 || wallpaper.src_h == 0) {
for (y = sy0; y < sy1; y++)
fill_u32(dst + y * (int)stride + sx0, sx1 - sx0, color);
return;
}
{
const uint32_t *src = wallpaper.src;
int sw = (int)wallpaper.src_w;
int sh = (int)wallpaper.src_h;
if (wallpaper.mode == IMAGE_TILED) {
/*
* Tile in world space: (wx,wy) → src[wy % sh][wx % sw].
* At zoom 100%: wx = sx + pan (same as old full canvas).
*/
if (z == BGCE_ZOOM_PCT_1X) {
for (y = sy0; y < sy1; y++) {
int wy = y + pan_y;
int ty = wallpaper_mod(wy, sh);
const uint32_t *srow = src + ty * sw;
uint32_t *drow = dst + y * (int)stride;
for (x = sx0; x < sx1; x++) {
int wx = x + pan_x;
drow[x] = srow[wallpaper_mod(wx, sw)];
}
}
} else {
for (y = sy0; y < sy1; y++) {
int wy = (y + pan_y) * 100 / z;
int ty = wallpaper_mod(wy, sh);
const uint32_t *srow = src + ty * sw;
uint32_t *drow = dst + y * (int)stride;
for (x = sx0; x < sx1; x++) {
int wx = (x + pan_x) * 100 / z;
drow[x] = srow[wallpaper_mod(wx, sw)];
}
}
}
return;
}
/*
* Scaled: stretch source over the full virtual desktop.
* ix = wx * sw / virtual_w
* iy = wy * sh / virtual_h
*/
if (z == BGCE_ZOOM_PCT_1X) {
for (y = sy0; y < sy1; y++) {
int wy = y + pan_y;
int iy = wy * sh / vh;
uint32_t *drow = dst + y * (int)stride;
const uint32_t *srow;
if (iy < 0)
iy = 0;
if (iy >= sh)
iy = sh - 1;
srow = src + iy * sw;
for (x = sx0; x < sx1; x++) {
int wx = x + pan_x;
int ix = wx * sw / vw;
if (ix < 0)
ix = 0;
if (ix >= sw)
ix = sw - 1;
drow[x] = srow[ix];
}
}
} else {
for (y = sy0; y < sy1; y++) {
int wy = (y + pan_y) * 100 / z;
int iy = wy * sh / vh;
uint32_t *drow = dst + y * (int)stride;
const uint32_t *srow;
if (iy < 0)
iy = 0;
if (iy >= sh)
iy = sh - 1;
srow = src + iy * sw;
for (x = sx0; x < sx1; x++) {
int wx = (x + pan_x) * 100 / z;
int ix = wx * sw / vw;
if (ix < 0)
ix = 0;
if (ix >= sw)
ix = sw - 1;
drow[x] = srow[ix];
}
}
}
}
}
/*
* 1:1 blit when zoom_pct == 100 and buffer size == world size.
* pan is screen-pixel pan: world = screen + pan
*/
static void blit_client_1to1(uint32_t *dst, uint32_t screen_w,
const uint32_t *src, int cw, int ch,
int cli_x, int cli_y,
int ox0, int oy0, int ox1, int oy1,
int pan_x, int pan_y)
{
int ipx = pan_x;
int ipy = pan_y;
int cx0 = cli_x, cy0 = cli_y;
int cx1 = cx0 + cw, cy1 = cy0 + ch;
int ix0 = (ox0 + ipx) > cx0 ? (ox0 + ipx) : cx0;
int iy0 = (oy0 + ipy) > cy0 ? (oy0 + ipy) : cy0;
int ix1 = (ox1 + ipx) < cx1 ? (ox1 + ipx) : cx1;
int iy1 = (oy1 + ipy) < cy1 ? (oy1 + ipy) : cy1;
if (ix0 >= ix1 || iy0 >= iy1)
return;
size_t row_bytes = (size_t)(ix1 - ix0) * sizeof(uint32_t);
for (int wy = iy0; wy < iy1; wy++) {
int sy = wy - ipy;
uint32_t *drow = dst + sy * (int)screen_w + (ix0 - ipx);