Repository navigation
Expand file tree
/
Copy pathDOSMiniFB.c
More file actions
962 lines (825 loc) · 31.6 KB
/
Copy pathDOSMiniFB.c
File metadata and controls
962 lines (825 loc) · 31.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
#include "MiniFB.h&
8000
quot;
#include "MiniFB_enums.h"
#include "MiniFB_internal.h"
#include "WindowData.h"
#include <dpmi.h>
#include <pc.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/movedata.h>
#include <sys/segments.h>
#include <time.h>
#include "vesa.h"
//-------------------------------------
// The 101 key keyboard gave its new keys the scancode of the key they replaced, prefixed
// with E0, so the prefix is the only thing telling a dedicated arrow from a keypad one.
static const uint32_t extended_scancode_to_mfb_key[] = {
[0x1c] = MFB_KB_KEY_KP_ENTER,
[0x1d] = MFB_KB_KEY_RIGHT_CONTROL,
[0x35] = MFB_KB_KEY_KP_DIVIDE,
[0x37] = MFB_KB_KEY_PRINT_SCREEN,
[0x38] = MFB_KB_KEY_RIGHT_ALT,
[0x47] = MFB_KB_KEY_HOME,
[0x48] = MFB_KB_KEY_UP,
[0x49] = MFB_KB_KEY_PAGE_UP,
[0x4b] = MFB_KB_KEY_LEFT,
[0x4d] = MFB_KB_KEY_RIGHT,
[0x4f] = MFB_KB_KEY_END,
[0x50] = MFB_KB_KEY_DOWN,
[0x51] = MFB_KB_KEY_PAGE_DOWN,
[0x52] = MFB_KB_KEY_INSERT,
[0x53] = MFB_KB_KEY_DELETE,
[0x5b] = MFB_KB_KEY_LEFT_SUPER,
[0x5c] = MFB_KB_KEY_RIGHT_SUPER,
[0x5d] = MFB_KB_KEY_MENU,
};
static const uint32_t scancode_to_mfb_key[] = {
MFB_KB_KEY_UNKNOWN,
MFB_KB_KEY_ESCAPE,
MFB_KB_KEY_1,
MFB_KB_KEY_2,
MFB_KB_KEY_3,
MFB_KB_KEY_4,
MFB_KB_KEY_5,
MFB_KB_KEY_6,
MFB_KB_KEY_7,
MFB_KB_KEY_8,
MFB_KB_KEY_9,
MFB_KB_KEY_0,
MFB_KB_KEY_MINUS,
MFB_KB_KEY_EQUAL,
MFB_KB_KEY_BACKSPACE,
MFB_KB_KEY_TAB,
MFB_KB_KEY_Q,
MFB_KB_KEY_W,
MFB_KB_KEY_E,
MFB_KB_KEY_R,
MFB_KB_KEY_T,
MFB_KB_KEY_Y,
MFB_KB_KEY_U,
MFB_KB_KEY_I,
MFB_KB_KEY_O,
MFB_KB_KEY_P,
MFB_KB_KEY_LEFT_BRACKET,
MFB_KB_KEY_RIGHT_BRACKET,
MFB_KB_KEY_ENTER,
MFB_KB_KEY_LEFT_CONTROL,
MFB_KB_KEY_A,
MFB_KB_KEY_S,
MFB_KB_KEY_D,
MFB_KB_KEY_F,
MFB_KB_KEY_G,
MFB_KB_KEY_H,
MFB_KB_KEY_J,
MFB_KB_KEY_K,
MFB_KB_KEY_L,
MFB_KB_KEY_SEMICOLON,
MFB_KB_KEY_APOSTROPHE,
MFB_KB_KEY_GRAVE_ACCENT,
MFB_KB_KEY_LEFT_SHIFT,
MFB_KB_KEY_BACKSLASH,
MFB_KB_KEY_Z,
MFB_KB_KEY_X,
MFB_KB_KEY_C,
MFB_KB_KEY_V,
MFB_KB_KEY_B,
MFB_KB_KEY_N,
MFB_KB_KEY_M,
MFB_KB_KEY_COMMA,
MFB_KB_KEY_PERIOD,
MFB_KB_KEY_SLASH,
MFB_KB_KEY_RIGHT_SHIFT,
MFB_KB_KEY_KP_MULTIPLY, // 0x37 (numpad *); real Print Screen sends E0+0x37
MFB_KB_KEY_LEFT_ALT,
MFB_KB_KEY_SPACE,
MFB_KB_KEY_CAPS_LOCK,
MFB_KB_KEY_F1,
MFB_KB_KEY_F2,
MFB_KB_KEY_F3,
MFB_KB_KEY_F4,
MFB_KB_KEY_F5,
MFB_KB_KEY_F6,
MFB_KB_KEY_F7,
MFB_KB_KEY_F8,
MFB_KB_KEY_F9,
MFB_KB_KEY_F10,
MFB_KB_KEY_NUM_LOCK,
MFB_KB_KEY_SCROLL_LOCK,
MFB_KB_KEY_KP_7, // 0x47
MFB_KB_KEY_KP_8, // 0x48
MFB_KB_KEY_KP_9, // 0x49
MFB_KB_KEY_KP_SUBTRACT, // 0x4A (numpad -)
MFB_KB_KEY_KP_4, // 0x4B
MFB_KB_KEY_KP_5, // 0x4C
MFB_KB_KEY_KP_6, // 0x4D
MFB_KB_KEY_KP_ADD,
MFB_KB_KEY_KP_1, // 0x4F
MFB_KB_KEY_KP_2, // 0x50
MFB_KB_KEY_KP_3, // 0x51
MFB_KB_KEY_KP_0, // 0x52
MFB_KB_KEY_KP_DECIMAL, // 0x53
MFB_KB_KEY_UNKNOWN, // 0x54 (SysRq / Alt+PrScr)
MFB_KB_KEY_UNKNOWN, // 0x55 (undefined)
MFB_KB_KEY_WORLD_2, // 0x56 (ISO extra key between LShift and Z)
MFB_KB_KEY_F11, // 0x57
MFB_KB_KEY_F12, // 0x58
};
//-------------------------------------
static const char scancode_to_ascii[] = {
0, 0, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 0,
0, 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', 0, 0,
'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`', 0, '\\', 'z',
'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 0, 0, 0, ' ',
};
//-------------------------------------
static const char scancode_to_ascii_shift[] = {
0, 0, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', 0,
0, 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', 0, 0,
'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~', 0, '|', 'Z',
'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', 0, 0, 0, ' ',
};
//-------------------------------------
#define RING_BUFFER_SIZE 512
//-------------------------------------
typedef struct ring_buffer {
uint32_t read_index;
uint32_t write_index;
uint8_t buffer[RING_BUFFER_SIZE];
} ring_buffer;
//-------------------------------------
static bool
ring_buffer_push(ring_buffer *buffer, uint8_t value) {
uint32_t next = buffer->write_index + 1;
if (next >= RING_BUFFER_SIZE)
next = 0;
if (next == buffer->read_index)
return false;
buffer->buffer[buffer->write_index] = value;
buffer->write_index = next;
return true;
}
//-------------------------------------
static bool
ring_buffer_pop(ring_buffer *buffer, uint8_t *value) {
if (buffer->write_index == buffer->read_index)
return false;
uint32_t next = buffer->read_index + 1;
if (next >= RING_BUFFER_SIZE)
next = 0;
*value = buffer->buffer[buffer->read_index];
buffer->read_index = next;
return true;
}
//-------------------------------------
typedef struct keyboard_state {
bool initialized;
ring_buffer buffer;
_go32_dpmi_seginfo old_keyboard_handler;
_go32_dpmi_seginfo new_keyboard_handler;
uint8_t last_scancode_was_extended; // 0=no, 1=E0, 2=E1, 3=E2
bool caps_lock;
bool num_lock;
uint8_t pause_bytes_left;
} keyboard_state;
//-------------------------------------
static keyboard_state g_keyboard = {0};
//-------------------------------------
typedef struct SWindowData_DOS {
uint32_t actual_width, actual_height, actual_bpp, bytes_per_scanline;
uint32_t *scale_buffer;
uint8_t *scanline_buffer;
struct mfb_timer *timer;
} SWindowData_DOS;
//-------------------------------------
static SWindowData *g_window = NULL;
static bool g_mouse_wheel_present = false;
static bool g_mouse_present = false;
//-------------------------------------
__attribute__((destructor))
static void
tear_down() {
vesa_dispose();
if (g_keyboard.initialized) {
_go32_dpmi_set_protected_mode_interrupt_vector(
0x9, &g_keyboard.old_keyboard_handler);
_go32_dpmi_free_iret_wrapper(&g_keyboard.new_keyboard_handler);
}
}
//-------------------------------------
// The driver writes only the registers it answers with, and whatever the struct held on the
// way in is read back as if the driver had put it there. That is not academic here: the
// wheel counter of function 0x0b comes back in BX, which nothing else sets, so an
// uninitialised call read the button mask left by the previous frame and turned a held
// button into a wheel notch per frame.
//-------------------------------------
static void
mouse_call(__dpmi_regs *regs, unsigned function) {
memset(regs, 0, sizeof(*regs));
regs->x.ax = (unsigned short) function;
__dpmi_int(0x33, regs);
}
//-------------------------------------
static void
init_mouse(SWindowData *window_data) {
__dpmi_regs regs;
// AX=0: reset driver and check presence; returns AX=0xFFFF if driver present
mouse_call(®s, 0x00);
if (regs.x.ax == 0) {
MFB_LOG(MFB_LOG_WARNING, "No mouse driver detected, mouse support disabled");
return;
}
g_mouse_present = true;
// CuteMouse and the drivers that copied its API answer 'WM' here. Reading the wheel from
// the driver leaves the arrow scancodes to the arrow keys, so nothing has to be traded.
mouse_call(®s, 0x11);
g_mouse_wheel_present = (regs.x.ax == 0x574d) && ((regs.x.cx & 1) != 0);
if (!g_mouse_wheel_present)
MFB_LOG(MFB_LOG_DEBUG, "Mouse driver has no wheel API, scroll events disabled");
// Use the actual VESA resolution for the mouse range, not the user-requested
// window size, so the cursor covers the full screen area.
SWindowData_DOS *window_data_specific = (SWindowData_DOS *) window_data->specific;
uint32_t width = window_data_specific ? window_data_specific->actual_width : window_data->window_width;
uint32_t height = window_data_specific ? window_data_specific->actual_height : window_data->window_height;
memset(®s, 0, sizeof(regs));
regs.x.ax = 7;
regs.x.cx = 0;
regs.x.dx = width - 1;
__dpmi_int(0x33, ®s);
memset(®s, 0, sizeof(regs));
regs.x.ax = 8;
regs.x.cx = 0;
regs.x.dx = height - 1;
__dpmi_int(0x33, ®s);
mouse_call(®s, 0x02);
// There is no window here and the driver range covers the whole screen, so the
// pointer can never be outside it. The event is never emitted, only the state.
window_data->is_mouse_inside = true;
}
//-------------------------------------
static void
keyboard_handler() {
ring_buffer_push(&g_keyboard.buffer, inp(0x60));
outportb(0x20, 0x20);
}
//-------------------------------------
static void
init_keyboard(void) {
if (g_keyboard.initialized)
return;
_go32_dpmi_lock_data(&g_keyboard, sizeof(g_keyboard));
_go32_dpmi_lock_code(keyboard_handler, 4096);
_go32_dpmi_lock_code(ring_buffer_push, 4096);
_go32_dpmi_get_protected_mode_interrupt_vector(
0x9, &g_keyboard.old_keyboard_handler);
g_keyboard.new_keyboard_handler.pm_offset = (int) keyboard_handler;
g_keyboard.new_keyboard_handler.pm_selector = _my_cs();
_go32_dpmi_allocate_iret_wrapper(&g_keyboard.new_keyboard_handler);
_go32_dpmi_set_protected_mode_interrupt_vector(
0x9, &g_keyboard.new_keyboard_handler);
g_keyboard.initialized = true;
}
//-------------------------------------
static void
destroy_window_data(SWindowData *window_data) {
if (window_data == NULL)
return;
release_cpp_stub((struct mfb_window *) window_data);
g_window = NULL;
vesa_dispose();
SWindowData_DOS *window_data_specific = (SWindowData_DOS *) window_data->specific;
if (window_data_specific != NULL) {
mfb_timer_destroy(window_data_specific->timer);
window_data_specific->timer = NULL;
free(window_data_specific->scale_buffer);
free(window_data_specific->scanline_buffer);
memset(window_data_specific, 0, sizeof(SWindowData_DOS));
free(window_data_specific);
}
window_data->specific = NULL;
memset(window_data, 0, sizeof(SWindowData));
free(window_data);
}
//-------------------------------------
struct mfb_window *
mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags) {
(void) title;
uint32_t buffer_stride = 0;
if (!calculate_buffer_layout(width, height, &buffer_stride, NULL)) {
MFB_LOG(MFB_LOG_ERROR, "DOSMiniFB: invalid window size %ux%u", width, height);
return NULL;
}
if (flags != 0u) {
MFB_LOG(MFB_LOG_WARNING, "DOSMiniFB: window flags 0x%x are ignored by the DOS backend", flags);
}
if (g_window) {
MFB_LOG(MFB_LOG_WARNING, "mfb_open_ex called while DOS backend window is already open");
return NULL;
}
uint32_t actual_width, actual_height, actual_bpp, bytes_per_scanline;
if (!vesa_init(width, height, &actual_width, &actual_height, &actual_bpp, &bytes_per_scanline)) {
MFB_LOG(MFB_LOG_ERROR, "Couldn't set VESA mode %ux%u", width, height);
return NULL;
}
SWindowData *window_data;
window_data = malloc(sizeof(SWindowData));
if (window_data == NULL) {
MFB_LOG(MFB_LOG_ERROR, "Cannot allocate window data");
vesa_dispose();
return NULL;
}
memset(window_data, 0, sizeof(SWindowData));
window_data->window_width = width;
window_data->window_height = height;
window_data->buffer_width = width;
window_data->buffer_height = height;
window_data->buffer_stride = buffer_stride;
window_data->dst_offset_x = 0;
window_data->dst_offset_y = 0;
window_data->dst_width = width;
window_data->dst_height = height;
calc_dst_factor(window_data, width, height);
SWindowData_DOS *window_data_specific = malloc(sizeof(SWindowData_DOS));
if (!window_data_specific) {
MFB_LOG(MFB_LOG_ERROR, "Cannot allocate DOS window data");
free(window_data);
vesa_dispose();
return NULL;
}
window_data_specific->timer = mfb_timer_create();
window_data_specific->actual_width = actual_width;
window_data_specific->actual_height = actual_height;
window_data_specific->actual_bpp = actual_bpp;
window_data_specific->bytes_per_scanline = bytes_per_scanline;
window_data_specific->scale_buffer = (uint32_t *) malloc((size_t) actual_width * actual_height * sizeof(uint32_t));
window_data_specific->scanline_buffer =
actual_bpp != 32 || bytes_per_scanline != width << 2
? (uint8_t *) malloc(actual_height * bytes_per_scanline)
: NULL;
if (!window_data_specific->scale_buffer) {
MFB_LOG(MFB_LOG_ERROR, "Cannot allocate DOS scale buffer");
free(window_data_specific);
free(window_data);
vesa_dispose();
return NULL;
}
if ((actual_bpp != 32 || bytes_per_scanline != width << 2) && !window_data_specific->scanline_buffer) {
MFB_LOG(MFB_LOG_ERROR, "Cannot allocate DOS scanline buffer");
free(window_data_specific->scale_buffer);
free(window_data_specific);
free(window_data);
vesa_dispose();
return NULL;
}
window_data->specific = window_data_specific;
mfb_set_keyboard_callback((struct mfb_window *) window_data, keyboard_default);
window_data->is_active = true;
window_data->is_initialized = true;
window_data->is_cursor_visible = false;
g_window = window_data;
init_mouse(window_data);
init_keyboard();
// Sync Caps Lock initial state from BIOS (INT 16h AH=02h: read shift flags, bit 6 = Caps Lock)
{
__dpmi_regs regs;
regs.x.ax = 0x0200;
__dpmi_int(0x16, ®s);
if (regs.h.al & 0x40) {
g_keyboard.caps_lock = true;
window_data->mod_keys |= MFB_KB_MOD_CAPS_LOCK;
}
}
MFB_LOG(MFB_LOG_DEBUG,
"DOS window created (%ux%u, actual %ux%u, bpp=%u, pitch=%u)", width,
height, actual_width, actual_height, actual_bpp, bytes_per_scanline);
return (struct mfb_window *) window_data;
}
//-------------------------------------
bool
mfb_set_viewport(struct mfb_window *window, unsigned offset_x, unsigned offset_y, unsigned width, unsigned height) {
SWindowData *window_data = (SWindowData *) window;
if (!mfb_validate_viewport(window_data, offset_x, offset_y, width, height, "DOSMiniFB")) {
return false;
}
window_data->dst_offset_x = offset_x;
window_data->dst_offset_y = offset_y;
window_data->dst_width = width;
window_data->dst_height = height;
calc_dst_factor(window_data, window_data->window_width, window_data->window_height);
return true;
}
//-------------------------------------
void
mfb_set_title(struct mfb_window *window, const char *title) {
(void) window;
(void) title;
}
//-------------------------------------
static void
update_mouse(SWindowData *window_data) {
if (!g_mouse_present)
return;
__dpmi_regs regs;
mouse_call(®s, 0x03);
// Drivers disagree on where the wheel counter lives. CuteMouse documents BX of function
// 0x0b, DOSBox-X fills only BH of this call, so both are read. Function 0x0b is asked for
// even when BH already answered, because on the drivers that keep a counter it is that
// read which clears it, and without it one notch would repeat on every frame.
if (g_mouse_wheel_present) {
__dpmi_regs motion;
int wheel = (int) (signed char) regs.h.bh;
mouse_call(&motion, 0x0b);
if (wheel == 0) {
wheel = (int) (int16_t) motion.x.bx;
}
if (wheel != 0) {
// Positive is up, as on every other backend. The driver counts down as positive.
float delta_y = (float) -wheel;
window_data->mouse_wheel_x = 0.0f;
window_data->mouse_wheel_y = delta_y;
if (window_data->mouse_wheel_func)
window_data->mouse_wheel_func((struct mfb_window *) window_data,
(mfb_key_mod) window_data->mod_keys,
0.0f,
delta_y);
}
}
int32_t old_x = window_data->mouse_pos_x;
int32_t old_y = window_data->mouse_pos_y;
uint8_t old_left_pressed = window_data->mouse_button_status[MFB_MOUSE_LEFT];
uint8_t old_right_pressed = window_data->mouse_button_status[MFB_MOUSE_RIGHT];
uint8_t old_middle_pressed = window_data->mouse_button_status[MFB_MOUSE_MIDDLE];
uint8_t left_pressed = (regs.x.bx >> 0) & 1;
uint8_t right_pressed = (regs.x.bx >> 1) & 1;
uint8_t middle_pressed = (regs.x.bx >> 2) & 1;
window_data->mouse_button_status[MFB_MOUSE_LEFT] = left_pressed;
window_data->mouse_button_status[MFB_MOUSE_RIGHT] = right_pressed;
window_data->mouse_button_status[MFB_MOUSE_MIDDLE] = middle_pressed;
// The driver range is the VESA resolution, which can be twice the size the application
// asked for, so its coordinates are scaled back into window units. Every other backend
// reports the position in the units of the window.
SWindowData_DOS *window_data_specific = (SWindowData_DOS *) window_data->specific;
int32_t pos_x = regs.x.cx;
int32_t pos_y = regs.x.dx;
if (window_data_specific != NULL &&
window_data_specific->actual_width > 0 && window_data_specific->actual_height > 0) {
pos_x = (int32_t) ((int64_t) pos_x * window_data->window_width / window_data_specific->actual_width);
pos_y = (int32_t) ((int64_t) pos_y * window_data->window_height / window_data_specific->actual_height);
}
window_data->mouse_pos_x = pos_x;
window_data->mouse_pos_y = pos_y;
mfb_key_mod mod = (mfb_key_mod) window_data->mod_keys;
if (old_left_pressed != left_pressed && window_data->mouse_btn_func)
window_data->mouse_btn_func((struct mfb_window *) window_data, MFB_MOUSE_LEFT, mod, left_pressed);
if (old_right_pressed != right_pressed && window_data->mouse_btn_func)
window_data->mouse_btn_func((struct mfb_window *) window_data, MFB_MOUSE_RIGHT, mod, right_pressed);
if (old_middle_pressed != middle_pressed && window_data->mouse_btn_func)
window_data->mouse_btn_func((struct mfb_window *) window_data, MFB_MOUSE_MIDDLE, mod, middle_pressed);
if ((old_x != pos_x || old_y != pos_y) && window_data->mouse_move_func)
window_data->mouse_move_func((struct mfb_window *) window_data, pos_x, pos_y);
}
//-------------------------------------
// Without Num Lock the keypad has moved the cursor since the 83 key keyboard, and a program
// written for DOS expects that. MINIFB_DOS_KEYPAD_POSITIONAL reports the physical keypad key
// instead, which is what every other backend does.
static uint32_t
keypad_without_num_lock(uint32_t key_code) {
switch (key_code) {
case MFB_KB_KEY_KP_7: return MFB_KB_KEY_HOME;
case MFB_KB_KEY_KP_8: return MFB_KB_KEY_UP;
case MFB_KB_KEY_KP_9: return MFB_KB_KEY_PAGE_UP;
case MFB_KB_KEY_KP_4: return MFB_KB_KEY_LEFT;
case MFB_KB_KEY_KP_6: return MFB_KB_KEY_RIGHT;
case MFB_KB_KEY_KP_1: return MFB_KB_KEY_END;
case MFB_KB_KEY_KP_2: return MFB_KB_KEY_DOWN;
case MFB_KB_KEY_KP_3: return MFB_KB_KEY_PAGE_DOWN;
case MFB_KB_KEY_KP_0: return MFB_KB_KEY_INSERT;
case MFB_KB_KEY_KP_DECIMAL: return MFB_KB_KEY_DELETE;
default: return key_code;
}
}
//-------------------------------------
static void
update_keyboard(SWindowData *window_data) {
uint8_t raw_scancode;
while (ring_buffer_pop(&g_keyboard.buffer, &raw_scancode)) {
if (raw_scancode == 0xe0 || raw_scancode == 0xe1 || raw_scancode == 0xe2) {
g_keyboard.last_scancode_was_extended = raw_scancode + 1 - 0xe0;
// Pause is the only key that uses E1, and it spells itself out as E1 1D 45 followed by
// E1 9D C5. Its two payload bytes carry no key of their own.
if (raw_scancode == 0xe1)
g_keyboard.pause_bytes_left = 2;
continue;
}
bool pressed = !(raw_scancode & 0x80);
uint8_t scancode = raw_scancode & 0x7f;
uint32_t key_code;
if (g_keyboard.pause_bytes_left > 0) {
g_keyboard.last_scancode_was_extended = 0;
if (--g_keyboard.pause_bytes_left > 0)
continue;
key_code = MFB_KB_KEY_PAUSE;
}
else if (g_keyboard.last_scancode_was_extended != 0) {
if (scancode >= sizeof(extended_scancode_to_mfb_key) / sizeof(extended_scancode_to_mfb_key[0]))
continue;
key_code = extended_scancode_to_mfb_key[scancode];
if (key_code == 0)
key_code = MFB_KB_KEY_UNKNOWN;
}
else {
if (scancode >= sizeof(scancode_to_mfb_key) / sizeof(scancode_to_mfb_key[0]))
continue;
key_code = scancode_to_mfb_key[scancode];
#if !defined(MINIFB_DOS_KEYPAD_POSITIONAL)
if (!g_keyboard.num_lock)
key_code = keypad_without_num_lock(key_code);
#endif
}
// Some DOS mouse drivers emulate a wheel by injecting extended Up/Down keys. Reading
// those as scroll costs the dedicated arrow keys, which send the very same scancodes
// and would never reach the keyboard callback, so it has to be asked for.
#if defined(MINIFB_DOS_WHEEL_FROM_ARROW_KEYS)
bool is_extended = g_keyboard.last_scancode_was_extended != 0;
if (is_extended && (key_code == MFB_KB_KEY_UP || key_code == MFB_KB_KEY_DOWN)) {
if (pressed) {
float delta_y = (key_code == MFB_KB_KEY_UP) ? 1.0f : -1.0f;
window_data->mouse_wheel_x = 0.0f;
window_data->mouse_wheel_y = delta_y;
if (window_data->mouse_wheel_func) {
window_data->mouse_wheel_func((struct mfb_window *) window_data,
(mfb_key_mod) window_data->mod_keys,
0.0f,
delta_y
);
}
}
g_keyboard.last_scancode_was_extended = 0;
continue;
}
#endif
// An extended key borrows the number of the key it replaced, so this table would give it
// that key's text: Home would type a 7. The keypad slash is the one that prints the same
// either way.
bool prints_text = g_keyboard.last_scancode_was_extended == 0 || scancode == 0x35;
char ascii = 0;
if (prints_text && scancode < sizeof(scancode_to_ascii)) {
char base_ascii = scancode_to_ascii[scancode];
bool is_letter = (base_ascii >= 'a' && base_ascii <= 'z');
// Caps Lock toggles shift only for letter keys; Shift always applies to all keys
bool use_shift = (window_data->mod_keys & MFB_KB_MOD_SHIFT) != 0;
if ((window_data->mod_keys & MFB_KB_MOD_CAPS_LOCK) && is_letter)
use_shift = !use_shift;
ascii = use_shift ? scancode_to_ascii_shift[scancode] : base_ascii;
}
//MFB_LOG(MFB_LOG_TRACE, "scancode=%u key=%s ascii=%u pressed=%u",
// (unsigned) scancode, mfb_get_key_name((mfb_key) key_code),
// (unsigned)(uint8_t) ascii, (unsigned) pressed);
// MFB_KB_KEY_UNKNOWN == -1 -> as uint32_t it becomes 0xFFFFFFFF, which would
// overflow key_status[MFB_MAX_KEYS]. Guard the write with a bounds check.
if (key_code < MFB_MAX_KEYS)
window_data->key_status[key_code] = pressed;
// The locks answer to their own toggle, the held ones come from the key buffer, and
// dropping a bit because one side of a pair came up is what the shared helper avoids.
if (!pressed) {
if (key_code == MFB_KB_KEY_CAPS_LOCK)
g_keyboard.caps_lock = !g_keyboard.caps_lock;
else if (key_code == MFB_KB_KEY_NUM_LOCK)
g_keyboard.num_lock = !g_keyboard.num_lock;
}
uint32_t lock_mods = 0;
if (g_keyboard.caps_lock)
lock_mods |= MFB_KB_MOD_CAPS_LOCK;
if (g_keyboard.num_lock)
lock_mods |= MFB_KB_MOD_NUM_LOCK;
mfb_recalc_mod_keys(window_data, lock_mods);
if (window_data->keyboard_func && key_code != (uint32_t) MFB_KB_KEY_UNKNOWN)
window_data->keyboard_func((struct mfb_window *) window_data,
key_code,
window_data->mod_keys,
pressed);
if (window_data->char_input_func && pressed && ascii != 0)
window_data->char_input_func((struct mfb_window *) window_data, ascii);
// The prefix belongs to the scancode that followed it and to nothing else.
g_keyboard.last_scancode_was_extended = 0;
}
}
//-------------------------------------
mfb_update_state
mfb_update_events(struct mfb_window *window) {
if (!window) {
MFB_LOG(MFB_LOG_DEBUG, "mfb_update_events: invalid window");
return MFB_STATE_INVALID_WINDOW;
}
SWindowData *window_data = (SWindowData *) window;
if (window_data->close) {
destroy_window_data(window_data);
return MFB_STATE_EXIT;
}
window_data->mouse_wheel_x = 0.0f;
window_data->mouse_wheel_y = 0.0f;
update_mouse(window_data);
update_keyboard(window_data);
if (window_data->close) {
destroy_window_data(window_data);
return MFB_STATE_EXIT;
}
return MFB_STATE_OK;
}
//-------------------------------------
//static void
//convert_to_24_bpp(uint32_t *source, uint32_t width, uint32_t height) {}
//-------------------------------------
mfb_update_state
mfb_update_ex(struct mfb_window *window, void *buffer, unsigned width, unsigned height) {
uint32_t buffer_stride = 0;
if (!window) {
MFB_LOG(MFB_LOG_DEBUG, "mfb_update_ex: invalid window");
return MFB_STATE_INVALID_WINDOW;
}
if (!buffer) {
MFB_LOG(MFB_LOG_DEBUG, "mfb_update_ex: invalid buffer");
return MFB_STATE_INVALID_BUFFER;
}
if (!calculate_buffer_layout(width, height, &buffer_stride, NULL)) {
MFB_LOG(MFB_LOG_DEBUG, "mfb_update_ex: invalid buffer size %ux%u", width, height);
return MFB_STATE_INVALID_BUFFER;
}
SWindowData *window_data = (SWindowData *) window;
mfb_update_state state = mfb_update_events(window);
if (state)
return state;
SWindowData_DOS *window_data_specific = window_data->specific;
if (!window_data_specific) {
MFB_LOG(MFB_LOG_DEBUG, "mfb_update_ex: invalid window specific data");
return MFB_STATE_INVALID_WINDOW;
}
window_data->buffer_width = width;
window_data->buffer_height = height;
window_data->buffer_stride = buffer_stride;
uint32_t *scale_buffer = window_data_specific->scale_buffer;
uint8_t *scanline_buffer = window_data_specific->scanline_buffer;
uint32_t a_width = window_data_specific->actual_width;
uint32_t a_height = window_data_specific->actual_height;
uint32_t a_bpp = window_data_specific->actual_bpp;
uint32_t a_bytes_per_scanline = window_data_specific->bytes_per_scanline;
bool has_viewport = window_data->dst_offset_x != 0 || window_data->dst_offset_y != 0 ||
window_data->dst_width != window_data->window_width ||
window_data->dst_height != window_data->window_height;
// Exact match, copy directly. Unlikely to happen on a real device.
// Happens in DOSBox.
if (!has_viewport &&
a_width == width && a_height == height && a_bpp == 32 &&
a_bytes_per_scanline == a_width * sizeof(uint32_t)) {
movedata(_my_ds(),
(unsigned int) buffer,
vesa_get_frame_buffer_selector(),
0,
height * a_bytes_per_scanline
);
return MFB_STATE_OK;
}
// Else we need to transfer to scale, convert to 24-bit, pad the scanlines,
// or all of the above...
uint8_t *frame = NULL;
if (has_viewport) {
memset(scale_buffer, 0, a_width * a_height * sizeof(uint32_t));
uint32_t vp_x = (uint32_t)(((uint64_t) window_data->dst_offset_x * a_width) /
window_data->window_width);
uint32_t vp_y = (uint32_t)(((uint64_t) window_data->dst_offset_y * a_height) /
window_data->window_height);
uint32_t vp_w = (uint32_t)(((uint64_t) window_data->dst_width * a_width) /
window_data->window_width);
uint32_t vp_h = (uint32_t)(((uint64_t) window_data->dst_height * a_height) /
window_data->window_height);
if (vp_w == 0)
vp_w = 1;
if (vp_h == 0)
vp_h = 1;
if (vp_x < a_width && vp_y < a_height) {
if (vp_x + vp_w > a_width)
vp_w = a_width - vp_x;
if (vp_y + vp_h > a_height)
vp_h = a_height - vp_y;
stretch_image(buffer, 0, 0, width, height, width, scale_buffer, vp_x,
vp_y, vp_w, vp_h, a_width);
}
frame = (uint8_t *) scale_buffer;
}
else if (a_width != width || a_height != height) {
stretch_image(buffer, 0, 0, width, height, width,
scale_buffer, 0, 0, a_width, a_height, a_width);
frame = (uint8_t *) scale_buffer;
}
else {
frame = (uint8_t *) buffer;
}
if (a_bpp == 24) {
// Neither pitch nor bpp matched, worst case
uint8_t *source = frame;
uint8_t *dest = scanline_buffer;
uint32_t dest_skip = a_bytes_per_scanline - a_width * 3;
for (uint32_t y = 0, i = 0; y < a_height; y++) {
for (uint32_t x = 0, xe = a_width; x < xe; x++, i += 4, dest += 3) {
dest[0] = source[i];
dest[1] = source[i + 1];
dest[2] = source[i + 2];
}
dest += dest_skip;
}
movedata(_my_ds(),
(unsigned int) scanline_buffer,
vesa_get_frame_buffer_selector(),
0,
a_height * a_bytes_per_scanline
);
}
else {
if (a_bytes_per_scanline != a_width * 4) {
// bpp matched, but pitch didn't. very unlikely to happen...
uint8_t *source = (uint8_t *) frame;
uint8_t *dest = (uint8_t *) scanline_buffer;
uint32_t source_pitch = a_width << 2;
for (uint32_t y = 0; y < a_height; y++, dest += a_bytes_per_scanline, source += source_pitch) {
memcpy(dest, source, source_pitch);
}
movedata(_my_ds(),
(unsigned int) scanline_buffer,
vesa_get_frame_buffer_selector(),
0,
a_height * a_bytes_per_scanline
);
}
else {
// Only stretched
movedata(_my_ds(),
(unsigned int) frame,
vesa_get_frame_buffer_selector(),
0,
a_height * a_width * sizeof(uint32_t)
);
}
}
return MFB_STATE_OK;
}
//-------------------------------------
extern double g_time_for_frame;
//-------------------------------------
bool
mfb_wait_sync(struct mfb_window *window) {
if (!window) {
MFB_LOG(MFB_LOG_DEBUG, "mfb_wait_sync: invalid window");
return false;
}
SWindowData *window_data = (SWindowData *) window;
SWindowData_DOS *window_data_specific = (SWindowData_DOS *) window_data->specific;
mfb_update_state state = mfb_update_events(window);
if (state != MFB_STATE_OK)
return false;
// A target of zero means the caller wants every cycle it can get, which is what this
// backend did before it paced at all. Yielding gives the timeslice back to whatever is
// hosting DOS instead of burning it in a spin.
if (g_time_for_frame <= 0.0 || window_data_specific == NULL || window_data_specific->timer == NULL)
return true;
while (mfb_timer_now(window_data_specific->timer) < g_time_for_frame) {
__dpmi_yield();
state = mfb_update_events(window);
if (state != MFB_STATE_OK)
return false;
}
mfb_timer_compensated_reset(window_data_specific->timer);
return true;
}
//-------------------------------------
void
mfb_get_monitor_scale(struct mfb_window *window, float *scale_x, float *scale_y) {
kUnused(window);
if (scale_x)
*scale_x = 1.0f;
if (scale_y)
*scale_y = 1.0f;
}
//-------------------------------------
extern double g_timer_frequency;
extern double g_timer_resolution;
//-------------------------------------
void
mfb_timer_init(void) {
g_timer_frequency = UCLOCKS_PER_SEC;
g_timer_resolution = 1.0 / g_timer_frequency;
}
//-------------------------------------
uint64_t
mfb_timer_tick(void) {
return uclock();
}
//-------------------------------------
void
mfb_show_cursor(struct mfb_window *window, bool show) {
(void) window;
(void) show;
// Hardware cursor not supported in VESA mode; is_cursor_visible is always false
}