-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim_video.c
1687 lines (1333 loc) · 46.4 KB
/
sim_video.c
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
/* sim_video.c: Bitmap video output
Copyright (c) 2011-2013, Matt Burke
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of the author shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from the author.
08-Nov-2013 MB Added globals for current mouse status
11-Jun-2013 MB First version
*/
#include "sim_video.h"
t_bool vid_active = FALSE;
int32 vid_mouse_xrel = 0;
int32 vid_mouse_yrel = 0;
t_bool vid_mouse_b1 = FALSE;
t_bool vid_mouse_b2 = FALSE;
t_bool vid_mouse_b3 = FALSE;
#if HAVE_LIBSDL
#include <SDL.h>
#include <SDL_thread.h>
extern int32 sim_is_running;
#define EVENT_REDRAW 1 /* redraw event for SDL */
#define EVENT_CLOSE 2 /* close event for SDL */
#define MAX_EVENTS 20 /* max events in queue */
typedef struct {
SIM_KEY_EVENT events[MAX_EVENTS];
SDL_sem *sem;
int32 head;
int32 tail;
int32 count;
} KEY_EVENT_QUEUE;
typedef struct {
SIM_MOUSE_EVENT events[MAX_EVENTS];
SDL_sem *sem;
int32 head;
int32 tail;
int32 count;
} MOUSE_EVENT_QUEUE;
int vid_thread (void* arg);
/*
Currently there are two separate video implementations which exist
due to the fact that libSDL and libSDL2 provide vastly different APIs.
libSDL2 is the distinctly better choice going forward, and, given the
background thread event digestion, is the only one which will work on
OSX.
We will abandon libSDL (Version 1) completely when the libSDL2 developer
components are readily packaged for most Linux distributions.
*/
#if SDL_MAJOR_VERSION == 1
t_bool vid_key_state[SDLK_LAST];
t_bool vid_mouse_captured;
int32 vid_width;
int32 vid_height;
SDL_Surface *vid_image; /* video buffer */
SDL_Surface *vid_window; /* window handle */
SDL_Thread *vid_thread_handle; /* event thread handle */
uint32 vid_mono_palette[2];
KEY_EVENT_QUEUE vid_key_events; /* keyboard events */
MOUSE_EVENT_QUEUE vid_mouse_events; /* mouse events */
DEVICE *vid_dev;
t_stat vid_open (DEVICE *dptr, uint32 width, uint32 height)
{
if (!vid_active) {
vid_active = TRUE;
vid_width = width;
vid_height = height;
vid_mouse_captured = FALSE;
vid_mouse_xrel = 0;
vid_mouse_yrel = 0;
vid_key_events.head = 0;
vid_key_events.tail = 0;
vid_key_events.count = 0;
vid_key_events.sem = SDL_CreateSemaphore (1);
vid_mouse_events.head = 0;
vid_mouse_events.tail = 0;
vid_mouse_events.count = 0;
vid_mouse_events.sem = SDL_CreateSemaphore (1);
vid_dev = dptr;
vid_thread_handle = SDL_CreateThread (vid_thread, NULL);
if (vid_thread_handle == NULL) {
vid_close ();
return SCPE_OPENERR;
}
sim_debug (SIM_VID_DBG_VIDEO|SIM_VID_DBG_KEY|SIM_VID_DBG_MOUSE, vid_dev, "vid_open() - Success\n");
}
return SCPE_OK;
}
t_stat vid_close (void)
{
SDL_Event user_event;
int status;
if (vid_active) {
vid_active = FALSE;
if (vid_thread_handle) {
sim_debug (SIM_VID_DBG_VIDEO|SIM_VID_DBG_KEY|SIM_VID_DBG_MOUSE, vid_dev, "vid_close()\n");
user_event.type = SDL_USEREVENT;
user_event.user.code = EVENT_CLOSE;
user_event.user.data1 = NULL;
user_event.user.data2 = NULL;
SDL_PushEvent (&user_event);
SDL_WaitThread (vid_thread_handle, &status);
vid_thread_handle = NULL;
vid_dev = NULL;
}
if (vid_mouse_events.sem) {
SDL_DestroySemaphore(vid_mouse_events.sem);
vid_mouse_events.sem = NULL;
}
if (vid_key_events.sem) {
SDL_DestroySemaphore(vid_key_events.sem);
vid_key_events.sem = NULL;
}
}
return SCPE_OK;
}
t_stat vid_poll_kb (SIM_KEY_EVENT *ev)
{
if (SDL_SemTryWait (vid_key_events.sem) == 0) { /* get lock */
if (vid_key_events.count > 0) { /* events in queue? */
*ev = vid_key_events.events[vid_key_events.head++];
vid_key_events.count--;
if (vid_key_events.head == MAX_EVENTS)
vid_key_events.head = 0;
SDL_SemPost (vid_key_events.sem);
return SCPE_OK;
}
SDL_SemPost (vid_key_events.sem);
}
return SCPE_EOF;
}
t_stat vid_poll_mouse (SIM_MOUSE_EVENT *ev)
{
if (SDL_SemTryWait (vid_mouse_events.sem) == 0) {
if (vid_mouse_events.count > 0) {
*ev = vid_mouse_events.events[vid_mouse_events.head++];
vid_mouse_events.count--;
if (vid_mouse_events.head == MAX_EVENTS)
vid_mouse_events.head = 0;
SDL_SemPost (vid_mouse_events.sem);
return SCPE_OK;
}
SDL_SemPost (vid_mouse_events.sem);
}
return SCPE_EOF;
}
void vid_draw (int32 x, int32 y, int32 w, int32 h, uint32 *buf)
{
int32 i;
uint32* pixels;
pixels = (uint32 *)vid_image->pixels;
for (i = y; i < (y + h); i++)
memcpy (pixels + (i * vid_width) + x, buf, (size_t)w*sizeof(*pixels));
}
void vid_refresh (void)
{
SDL_Event user_event;
user_event.type = SDL_USEREVENT;
user_event.user.code = EVENT_REDRAW;
user_event.user.data1 = NULL;
user_event.user.data2 = NULL;
SDL_PushEvent (&user_event);
}
int vid_map_key (int key)
{
switch (key) {
case SDLK_BACKSPACE:
return SIM_KEY_BACKSPACE;
case SDLK_TAB:
return SIM_KEY_TAB;
case SDLK_RETURN:
return SIM_KEY_ENTER;
case SDLK_PAUSE:
return SIM_KEY_PAUSE;
case SDLK_ESCAPE:
return SIM_KEY_ESC;
case SDLK_SPACE:
return SIM_KEY_SPACE;
case SDLK_QUOTE:
return SIM_KEY_SINGLE_QUOTE;
case SDLK_COMMA:
return SIM_KEY_COMMA;
case SDLK_MINUS:
return SIM_KEY_MINUS;
case SDLK_PERIOD:
return SIM_KEY_PERIOD;
case SDLK_SLASH:
return SIM_KEY_SLASH;
case SDLK_0:
return SIM_KEY_0;
case SDLK_1:
return SIM_KEY_1;
case SDLK_2:
return SIM_KEY_2;
case SDLK_3:
return SIM_KEY_3;
case SDLK_4:
return SIM_KEY_4;
case SDLK_5:
return SIM_KEY_5;
case SDLK_6:
return SIM_KEY_6;
case SDLK_7:
return SIM_KEY_7;
case SDLK_8:
return SIM_KEY_8;
case SDLK_9:
return SIM_KEY_9;
case SDLK_SEMICOLON:
return SIM_KEY_SEMICOLON;
case SDLK_EQUALS:
return SIM_KEY_EQUALS;
case SDLK_LEFTBRACKET:
return SIM_KEY_LEFT_BRACKET;
case SDLK_BACKSLASH:
return SIM_KEY_BACKSLASH;
case SDLK_RIGHTBRACKET:
return SIM_KEY_RIGHT_BRACKET;
case SDLK_BACKQUOTE:
return SIM_KEY_BACKQUOTE;
case SDLK_a:
return SIM_KEY_A;
case SDLK_b:
return SIM_KEY_B;
case SDLK_c:
return SIM_KEY_C;
case SDLK_d:
return SIM_KEY_D;
case SDLK_e:
return SIM_KEY_E;
case SDLK_f:
return SIM_KEY_F;
case SDLK_g:
return SIM_KEY_G;
case SDLK_h:
return SIM_KEY_H;
case SDLK_i:
return SIM_KEY_I;
case SDLK_j:
return SIM_KEY_J;
case SDLK_k:
return SIM_KEY_K;
case SDLK_l:
return SIM_KEY_L;
case SDLK_m:
return SIM_KEY_M;
case SDLK_n:
return SIM_KEY_N;
case SDLK_o:
return SIM_KEY_O;
case SDLK_p:
return SIM_KEY_P;
case SDLK_q:
return SIM_KEY_Q;
case SDLK_r:
return SIM_KEY_R;
case SDLK_s:
return SIM_KEY_S;
case SDLK_t:
return SIM_KEY_T;
case SDLK_u:
return SIM_KEY_U;
case SDLK_v:
return SIM_KEY_V;
case SDLK_w:
return SIM_KEY_W;
case SDLK_x:
return SIM_KEY_X;
case SDLK_y:
return SIM_KEY_Y;
case SDLK_z:
return SIM_KEY_Z;
case SDLK_DELETE:
return SIM_KEY_DELETE;
case SDLK_KP0:
return SIM_KEY_KP_INSERT;
case SDLK_KP1:
return SIM_KEY_KP_END;
case SDLK_KP2:
return SIM_KEY_KP_DOWN;
case SDLK_KP3:
return SIM_KEY_KP_PAGE_DOWN;
case SDLK_KP4:
return SIM_KEY_KP_LEFT;
case SDLK_KP5:
return SIM_KEY_KP_5;
case SDLK_KP6:
return SIM_KEY_KP_RIGHT;
case SDLK_KP7:
return SIM_KEY_KP_HOME;
case SDLK_KP8:
return SIM_KEY_KP_UP;
case SDLK_KP9:
return SIM_KEY_KP_PAGE_UP;
case SDLK_KP_PERIOD:
return SIM_KEY_KP_DELETE;
case SDLK_KP_DIVIDE:
return SIM_KEY_KP_DIVIDE;
case SDLK_KP_MULTIPLY:
return SIM_KEY_KP_MULTIPLY;
case SDLK_KP_MINUS:
return SIM_KEY_KP_SUBTRACT;
case SDLK_KP_PLUS:
return SIM_KEY_KP_ADD;
case SDLK_KP_ENTER:
return SIM_KEY_KP_ENTER;
case SDLK_UP:
return SIM_KEY_UP;
case SDLK_DOWN:
return SIM_KEY_DOWN;
case SDLK_RIGHT:
return SIM_KEY_RIGHT;
case SDLK_LEFT:
return SIM_KEY_LEFT;
case SDLK_INSERT:
return SIM_KEY_INSERT;
case SDLK_HOME:
return SIM_KEY_HOME;
case SDLK_END:
return SIM_KEY_END;
case SDLK_PAGEUP:
return SIM_KEY_PAGE_UP;
case SDLK_PAGEDOWN:
return SIM_KEY_PAGE_DOWN;
case SDLK_F1:
return SIM_KEY_F1;
case SDLK_F2:
return SIM_KEY_F2;
case SDLK_F3:
return SIM_KEY_F3;
case SDLK_F4:
return SIM_KEY_F4;
case SDLK_F5:
return SIM_KEY_F5;
case SDLK_F6:
return SIM_KEY_F6;
case SDLK_F7:
return SIM_KEY_F7;
case SDLK_F8:
return SIM_KEY_F8;
case SDLK_F9:
return SIM_KEY_F9;
case SDLK_F10:
return SIM_KEY_F10;
case SDLK_F11:
return SIM_KEY_F11;
case SDLK_F12:
return SIM_KEY_F12;
case SDLK_NUMLOCK:
return SIM_KEY_NUM_LOCK;
case SDLK_CAPSLOCK:
return SIM_KEY_CAPS_LOCK;
case SDLK_SCROLLOCK:
return SIM_KEY_SCRL_LOCK;
case SDLK_RSHIFT:
return SIM_KEY_SHIFT_R;
case SDLK_LSHIFT:
return SIM_KEY_SHIFT_L;
case SDLK_RCTRL:
return SIM_KEY_CTRL_R;
case SDLK_LCTRL:
return SIM_KEY_CTRL_L;
case SDLK_RALT:
return SIM_KEY_ALT_R;
case SDLK_LALT:
return SIM_KEY_ALT_L;
case SDLK_RMETA:
return SIM_KEY_ALT_R;
case SDLK_LMETA:
return SIM_KEY_WIN_L;
case SDLK_LSUPER:
return SIM_KEY_WIN_L;
case SDLK_RSUPER:
return SIM_KEY_WIN_R;
case SDLK_PRINT:
return SIM_KEY_PRINT;
case SDLK_BREAK:
return SIM_KEY_PAUSE;
case SDLK_MENU:
return SIM_KEY_MENU;
default:
return SIM_KEY_UNKNOWN;
}
}
void vid_key (SDL_KeyboardEvent *event)
{
SIM_KEY_EVENT ev;
if (vid_mouse_captured) {
static Uint8 *KeyStates = NULL;
static int numkeys;
if (!KeyStates)
KeyStates = SDL_GetKeyState(&numkeys);
if ((event->state == SDL_PRESSED) && KeyStates[SDLK_RSHIFT] && (KeyStates[SDLK_LCTRL] || KeyStates[SDLK_RCTRL])) {
sim_debug (SIM_VID_DBG_KEY, vid_dev, "vid_key() - Cursor Release\n");
SDL_WM_GrabInput (SDL_GRAB_OFF); /* relese cursor */
SDL_ShowCursor (SDL_ENABLE); /* show cursor */
vid_mouse_captured = FALSE;
return;
}
}
if (!sim_is_running)
return;
if (SDL_SemWait (vid_key_events.sem) == 0) {
if (vid_key_events.count < MAX_EVENTS) {
sim_debug (SIM_VID_DBG_KEY, vid_dev, "Keyboard Event: State: %d, Keysym: %d\n", event->state, event->keysym);
if (event->state == SDL_PRESSED) {
if (!vid_key_state[event->keysym.sym]) { /* Key was not down before */
vid_key_state[event->keysym.sym] = TRUE;
ev.key = vid_map_key (event->keysym.sym);
ev.state = SIM_KEYPRESS_DOWN;
}
else {
ev.key = vid_map_key (event->keysym.sym);
ev.state = SIM_KEYPRESS_REPEAT;
}
}
else {
vid_key_state[event->keysym.sym] = FALSE;
ev.key = vid_map_key (event->keysym.sym);
ev.state = SIM_KEYPRESS_UP;
}
vid_key_events.events[vid_key_events.tail++] = ev;
vid_key_events.count++;
if (vid_key_events.tail == MAX_EVENTS)
vid_key_events.tail = 0;
}
else {
sim_debug (SIM_VID_DBG_KEY, vid_dev, "Keyboard Event DISCARDED: State: %d, Keysym: %d\n", event->state, event->keysym);
}
SDL_SemPost (vid_key_events.sem);
}
}
void vid_mouse_move (SDL_MouseMotionEvent *event)
{
SDL_Event dummy_event;
int32 cx;
int32 cy;
SIM_MOUSE_EVENT ev;
if (!vid_mouse_captured)
return;
if ((event->x == 0) ||
(event->y == 0) ||
(event->x == (vid_width - 1)) ||
(event->y == (vid_height - 1))) { /* reached edge of window? */
cx = vid_width / 2;
cy = vid_height / 2;
SDL_WarpMouse (cx, cy); /* back to centre */
SDL_PumpEvents ();
while (SDL_PeepEvents (&dummy_event, 1, SDL_GETEVENT, SDL_MOUSEMOTIONMASK)) {};
}
if (!sim_is_running)
return;
vid_mouse_xrel += event->xrel; /* update cumulative x rel */
vid_mouse_yrel -= event->yrel; /* update cumulative y rel */
vid_mouse_b1 = (event->state & SDL_BUTTON(SDL_BUTTON_LEFT)) ? TRUE : FALSE;
vid_mouse_b2 = (event->state & SDL_BUTTON(SDL_BUTTON_MIDDLE)) ? TRUE : FALSE;
vid_mouse_b3 = (event->state & SDL_BUTTON(SDL_BUTTON_RIGHT)) ? TRUE : FALSE;
if (SDL_SemWait (vid_mouse_events.sem) == 0) {
sim_debug (SIM_VID_DBG_MOUSE, vid_dev, "Mouse Move Event: (%d,%d)\n", event->xrel, event->yrel);
if (vid_mouse_events.count < MAX_EVENTS) {
ev.x_rel = event->xrel;
ev.y_rel = (-event->yrel);
ev.b1_state = vid_mouse_b1;
ev.b2_state = vid_mouse_b2;
ev.b3_state = vid_mouse_b3;
vid_mouse_events.events[vid_mouse_events.tail++] = ev;
vid_mouse_events.count++;
if (vid_mouse_events.tail == MAX_EVENTS)
vid_mouse_events.tail = 0;
}
SDL_SemPost (vid_mouse_events.sem);
}
}
void vid_mouse_button (SDL_MouseButtonEvent *event)
{
SDL_Event dummy_event;
int32 cx;
int32 cy;
SIM_MOUSE_EVENT ev;
t_bool state;
if (!vid_mouse_captured) {
if ((event->state == SDL_PRESSED) &&
(event->button == SDL_BUTTON_LEFT)) { /* left click and cursor not captured? */
sim_debug (SIM_VID_DBG_KEY, vid_dev, "vid_mouse_button() - Cursor Captured\n");
SDL_WM_GrabInput (SDL_GRAB_ON); /* lock cursor to window */
SDL_ShowCursor (SDL_DISABLE); /* hide cursor */
cx = vid_width / 2;
cy = vid_height / 2;
SDL_WarpMouse (cx, cy); /* move cursor to centre of window */
SDL_PumpEvents ();
while (SDL_PeepEvents (&dummy_event, 1, SDL_GETEVENT, SDL_MOUSEMOTIONMASK)) {};
vid_mouse_captured = TRUE;
}
return;
}
if (!sim_is_running)
return;
state = (event->state == SDL_PRESSED) ? TRUE : FALSE;
switch (event->button) {
case SDL_BUTTON_LEFT:
vid_mouse_b1 = state;
break;
case SDL_BUTTON_MIDDLE:
vid_mouse_b2 = state;
break;
case SDL_BUTTON_RIGHT:
vid_mouse_b3 = state;
break;
}
if (SDL_SemWait (vid_mouse_events.sem) == 0) {
sim_debug (SIM_VID_DBG_MOUSE, vid_dev, "Mouse Button Event: State: %d, Button: %d, (%d,%d)\n", event->state, event->button, event->x, event->y);
if (vid_mouse_events.count < MAX_EVENTS) {
ev.x_rel = 0;
ev.y_rel = 0;
ev.b1_state = vid_mouse_b1;
ev.b2_state = vid_mouse_b2;
ev.b3_state = vid_mouse_b3;
vid_mouse_events.events[vid_mouse_events.tail++] = ev;
vid_mouse_events.count++;
if (vid_mouse_events.tail == MAX_EVENTS)
vid_mouse_events.tail = 0;
}
SDL_SemPost (vid_mouse_events.sem);
}
}
void vid_update (void)
{
SDL_Rect vid_dst;
vid_dst.x = 0;
vid_dst.y = 0;
vid_dst.w = vid_width;
vid_dst.h = vid_height;
sim_debug (SIM_VID_DBG_VIDEO, vid_dev, "Video Update Event: \n");
SDL_BlitSurface (vid_image, NULL, vid_window, &vid_dst);
SDL_UpdateRects (vid_window, 1, &vid_dst);
}
int vid_thread (void* arg)
{
SDL_Event event;
static char *eventtypes[] = {
"NOEVENT", /**< Unused (do not remove) */
"ACTIVEEVENT", /**< Application loses/gains visibility */
"KEYDOWN", /**< Keys pressed */
"KEYUP", /**< Keys released */
"MOUSEMOTION", /**< Mouse moved */
"MOUSEBUTTONDOWN", /**< Mouse button pressed */
"MOUSEBUTTONUP", /**< Mouse button released */
"JOYAXISMOTION", /**< Joystick axis motion */
"JOYBALLMOTION", /**< Joystick trackball motion */
"JOYHATMOTION", /**< Joystick hat position change */
"JOYBUTTONDOWN", /**< Joystick button pressed */
"JOYBUTTONUP", /**< Joystick button released */
"QUIT", /**< User-requested quit */
"SYSWMEVENT", /**< System specific event */
"EVENT_RESERVEDA", /**< Reserved for future use.. */
"EVENT_RESERVEDB", /**< Reserved for future use.. */
"VIDEORESIZE", /**< User resized video mode */
"VIDEOEXPOSE", /**< Screen needs to be redrawn */
"EVENT_RESERVED2", /**< Reserved for future use.. */
"EVENT_RESERVED3", /**< Reserved for future use.. */
"EVENT_RESERVED4", /**< Reserved for future use.. */
"EVENT_RESERVED5", /**< Reserved for future use.. */
"EVENT_RESERVED6", /**< Reserved for future use.. */
"EVENT_RESERVED7", /**< Reserved for future use.. */
"USEREVENT", /** Events SDL_USEREVENT(24) through SDL_MAXEVENTS-1(31) are for your use */
"",
"",
"",
"",
"",
"",
""
};
sim_debug (SIM_VID_DBG_VIDEO|SIM_VID_DBG_KEY|SIM_VID_DBG_MOUSE, vid_dev, "vid_thread() - Starting\n");
SDL_Init (SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE);
vid_window = SDL_SetVideoMode (vid_width, vid_height, 8, 0);
SDL_EnableKeyRepeat (SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
if (sim_end)
vid_image = SDL_CreateRGBSurface (SDL_SWSURFACE, vid_width, vid_height, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
else
vid_image = SDL_CreateRGBSurface (SDL_SWSURFACE, vid_width, vid_height, 32, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
vid_mono_palette[0] = sim_end ? 0xFF000000 : 0x000000FF;
vid_mono_palette[1] = 0xFFFFFFFF;
SDL_WM_SetCaption (&sim_name[0], &sim_name[0]);
memset (&vid_key_state, 0, sizeof(vid_key_state));
sim_debug (SIM_VID_DBG_VIDEO|SIM_VID_DBG_KEY|SIM_VID_DBG_MOUSE, vid_dev, "vid_thread() - Started\n");
while (vid_active) {
if (SDL_WaitEvent (&event)) {
switch (event.type) {
case SDL_KEYDOWN:
case SDL_KEYUP:
vid_key ((SDL_KeyboardEvent*)&event);
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
vid_mouse_button ((SDL_MouseButtonEvent*)&event);
break;
case SDL_MOUSEMOTION:
vid_mouse_move ((SDL_MouseMotionEvent*)&event);
break;
case SDL_USEREVENT:
if (event.user.code == EVENT_REDRAW)
vid_update ();
break;
default:
sim_debug (SIM_VID_DBG_VIDEO|SIM_VID_DBG_KEY|SIM_VID_DBG_MOUSE, vid_dev, "vid_thread() - Ignored Event: Type: %s(%d)\n", eventtypes[event.type], event.type);
break;
}
}
}
SDL_Quit ();
sim_debug (SIM_VID_DBG_VIDEO|SIM_VID_DBG_KEY|SIM_VID_DBG_MOUSE, vid_dev, "vid_thread() - Exiting\n");
return 0;
}
#else /* libSDL2 implementation */
t_bool vid_key_state[SDL_NUM_SCANCODES];
t_bool vid_mouse_captured;
int32 vid_width;
int32 vid_height;
SDL_Texture *vid_texture; /* video buffer in GPU */
SDL_Window *vid_window; /* window handle */
SDL_Renderer *vid_renderer;
SDL_Thread *vid_thread_handle; /* event thread handle */
uint32 vid_mono_palette[2]; /* Monochrome Color Map */
SDL_Color vid_colors[256];
KEY_EVENT_QUEUE vid_key_events; /* keyboard events */
MOUSE_EVENT_QUEUE vid_mouse_events; /* mouse events */
DEVICE *vid_dev;
t_stat vid_open (DEVICE *dptr, uint32 width, uint32 height)
{
if (!vid_active) {
int wait_count = 0;
vid_active = TRUE;
vid_width = width;
vid_height = height;
vid_mouse_captured = FALSE;
vid_mouse_xrel = 0;
vid_mouse_yrel = 0;
vid_key_events.head = 0;
vid_key_events.tail = 0;
vid_key_events.count = 0;
vid_key_events.sem = SDL_CreateSemaphore (1);
vid_mouse_events.head = 0;
vid_mouse_events.tail = 0;
vid_mouse_events.count = 0;
vid_mouse_events.sem = SDL_CreateSemaphore (1);
vid_dev = dptr;
vid_thread_handle = SDL_CreateThread (vid_thread, "vid-thread", NULL);
if (vid_thread_handle == NULL) {
vid_close ();
return SCPE_OPENERR;
}
while ((!vid_texture) && (++wait_count < 20))
sim_os_ms_sleep (100);
if (!vid_texture) {
vid_close ();
return SCPE_OPENERR;
}
sim_debug (SIM_VID_DBG_VIDEO|SIM_VID_DBG_KEY|SIM_VID_DBG_MOUSE, vid_dev, "vid_open() - Success\n");
}
return SCPE_OK;
}
t_stat vid_close (void)
{
SDL_Event user_event;
int status;
if (vid_active) {
vid_active = FALSE;
if (vid_thread_handle) {
sim_debug (SIM_VID_DBG_VIDEO|SIM_VID_DBG_KEY|SIM_VID_DBG_MOUSE, vid_dev, "vid_close()\n");
user_event.type = SDL_USEREVENT;
user_event.user.code = EVENT_CLOSE;
user_event.user.data1 = NULL;
user_event.user.data2 = NULL;
SDL_PushEvent (&user_event);
SDL_WaitThread (vid_thread_handle, &status);
vid_thread_handle = NULL;
vid_dev = NULL;
}
if (vid_mouse_events.sem) {
SDL_DestroySemaphore(vid_mouse_events.sem);
vid_mouse_events.sem = NULL;
}
if (vid_key_events.sem) {
SDL_DestroySemaphore(vid_key_events.sem);
vid_key_events.sem = NULL;
}
}
return SCPE_OK;
}
t_stat vid_poll_kb (SIM_KEY_EVENT *ev)
{
if (SDL_SemTryWait (vid_key_events.sem) == 0) { /* get lock */
if (vid_key_events.count > 0) { /* events in queue? */
*ev = vid_key_events.events[vid_key_events.head++];
vid_key_events.count--;
if (vid_key_events.head == MAX_EVENTS)
vid_key_events.head = 0;
SDL_SemPost (vid_key_events.sem);
return SCPE_OK;
}
SDL_SemPost (vid_key_events.sem);
}
return SCPE_EOF;
}
t_stat vid_poll_mouse (SIM_MOUSE_EVENT *ev)
{
if (SDL_SemTryWait (vid_mouse_events.sem) == 0) {
if (vid_mouse_events.count > 0) {
*ev = vid_mouse_events.events[vid_mouse_events.head++];
vid_mouse_events.count--;
if (vid_mouse_events.head == MAX_EVENTS)
vid_mouse_events.head = 0;
SDL_SemPost (vid_mouse_events.sem);
return SCPE_OK;
}
SDL_SemPost (vid_mouse_events.sem);
}
return SCPE_EOF;
}
void vid_draw (int32 x, int32 y, int32 w, int32 h, uint32 *buf)
{
int32 i;
uint32 *pixels;
int pitch;
SDL_LockTexture(vid_texture, NULL, (void *)&pixels, &pitch);
for (i = y; i < (y + h); i++)
memcpy (pixels + (i * vid_width) + x, buf, (size_t)w*sizeof(*pixels));
SDL_UnlockTexture(vid_texture);
}
void vid_refresh (void)
{
SDL_Event user_event;
user_event.type = SDL_USEREVENT;
user_event.user.code = EVENT_REDRAW;
user_event.user.data1 = NULL;
user_event.user.data2 = NULL;
SDL_PushEvent (&user_event);
}
int vid_map_key (int key)
{
switch (key) {
case SDLK_BACKSPACE:
return SIM_KEY_BACKSPACE;
case SDLK_TAB:
return SIM_KEY_TAB;
case SDLK_RETURN:
return SIM_KEY_ENTER;
case SDLK_ESCAPE:
return SIM_KEY_ESC;
case SDLK_SPACE:
return SIM_KEY_SPACE;
case SDLK_QUOTE:
return SIM_KEY_SINGLE_QUOTE;
case SDLK_COMMA:
return SIM_KEY_COMMA;
case SDLK_MINUS:
return SIM_KEY_MINUS;
case SDLK_PERIOD:
return SIM_KEY_PERIOD;
case SDLK_SLASH:
return SIM_KEY_SLASH;
case SDLK_0:
return SIM_KEY_0;
case SDLK_1:
return SIM_KEY_1;
case SDLK_2:
return SIM_KEY_2;
case SDLK_3:
return SIM_KEY_3;
case SDLK_4:
return SIM_KEY_4;
case SDLK_5:
return SIM_KEY_5;
case SDLK_6:
return SIM_KEY_6;
case SDLK_7:
return SIM_KEY_7;
case SDLK_8:
return SIM_KEY_8;