-
Notifications
You must be signed in to change notification settings - Fork 1
/
sim_video.c
2207 lines (1902 loc) · 74.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"
#include "scp.h"
t_bool vid_active = FALSE;
int32 vid_mouse_xrel;
int32 vid_mouse_yrel;
int32 vid_cursor_x;
int32 vid_cursor_y;
t_bool vid_mouse_b1 = FALSE;
t_bool vid_mouse_b2 = FALSE;
t_bool vid_mouse_b3 = FALSE;
t_stat vid_show (FILE* st, DEVICE *dptr, UNIT* uptr, int32 val, char* desc)
{
return vid_show_video (st, uptr, val, desc);
}
#if defined(USE_SIM_VIDEO) && defined(HAVE_LIBSDL)
char vid_release_key[64] = "Ctrl-Right-Shift";
#include <SDL.h>
#include <SDL_thread.h>
#if defined(HAVE_LIBPNG)
/* From: https://github.com/driedfruit/SDL_SavePNG */
/*
* Save an SDL_Surface as a PNG file.
*
* Returns 0 success or -1 on failure, the error message is then retrievable
* via SDL_GetError().
*/
#define SDL_SavePNG(surface, file) \
SDL_SavePNG_RW(surface, SDL_RWFromFile(file, "wb"), 1)
/*
* SDL_SavePNG -- libpng-based SDL_Surface writer.
*
* This code is free software, available under zlib/libpng license.
* http://www.libpng.org/pub/png/src/libpng-LICENSE.txt
*/
#include <SDL.h>
#include <png.h>
#define SUCCESS 0
#define ERROR -1
#define USE_ROW_POINTERS
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
#define rmask 0xFF000000
#define gmask 0x00FF0000
#define bmask 0x0000FF00
#define amask 0x000000FF
#else
#define rmask 0x000000FF
#define gmask 0x0000FF00
#define bmask 0x00FF0000
#define amask 0xFF000000
#endif
/* libpng callbacks */
static void png_error_SDL(png_structp ctx, png_const_charp str)
{
SDL_SetError("libpng: %s\n", str);
}
static void png_write_SDL(png_structp png_ptr, png_bytep data, png_size_t length)
{
SDL_RWops *rw = (SDL_RWops*)png_get_io_ptr(png_ptr);
SDL_RWwrite(rw, data, sizeof(png_byte), length);
}
static SDL_Surface *SDL_PNGFormatAlpha(SDL_Surface *src)
{
SDL_Surface *surf;
SDL_Rect rect = { 0 };
/* NO-OP for images < 32bpp and 32bpp images that already have Alpha channel */
if (src->format->BitsPerPixel <= 24 || src->format->Amask) {
src->refcount++;
return src;
}
/* Convert 32bpp alpha-less image to 24bpp alpha-less image */
rect.w = src->w;
rect.h = src->h;
surf = SDL_CreateRGBSurface(src->flags, src->w, src->h, 24,
src->format->Rmask, src->format->Gmask, src->format->Bmask, 0);
SDL_LowerBlit(src, &rect, surf, &rect);
return surf;
}
static int SDL_SavePNG_RW(SDL_Surface *surface, SDL_RWops *dst, int freedst)
{
png_structp png_ptr;
png_infop info_ptr;
png_colorp pal_ptr;
SDL_Palette *pal;
int i, colortype;
#ifdef USE_ROW_POINTERS
png_bytep *row_pointers;
#endif
/* Initialize and do basic error checking */
if (!dst)
{
SDL_SetError("Argument 2 to SDL_SavePNG_RW can't be NULL, expecting SDL_RWops*\n");
return (ERROR);
}
if (!surface)
{
SDL_SetError("Argument 1 to SDL_SavePNG_RW can't be NULL, expecting SDL_Surface*\n");
if (freedst) SDL_RWclose(dst);
return (ERROR);
}
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, png_error_SDL, NULL); /* err_ptr, err_fn, warn_fn */
if (!png_ptr)
{
SDL_SetError("Unable to png_create_write_struct on %s\n", PNG_LIBPNG_VER_STRING);
if (freedst) SDL_RWclose(dst);
return (ERROR);
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
SDL_SetError("Unable to png_create_info_struct\n");
png_destroy_write_struct(&png_ptr, NULL);
if (freedst) SDL_RWclose(dst);
return (ERROR);
}
if (setjmp(png_jmpbuf(png_ptr))) /* All other errors, see also "png_error_SDL" */
{
png_destroy_write_struct(&png_ptr, &info_ptr);
if (freedst) SDL_RWclose(dst);
return (ERROR);
}
/* Setup our RWops writer */
png_set_write_fn(png_ptr, dst, png_write_SDL, NULL); /* w_ptr, write_fn, flush_fn */
/* Prepare chunks */
colortype = PNG_COLOR_MASK_COLOR;
if (surface->format->BytesPerPixel > 0
&& surface->format->BytesPerPixel <= 8
&& (pal = surface->format->palette))
{
colortype |= PNG_COLOR_MASK_PALETTE;
pal_ptr = (png_colorp)malloc(pal->ncolors * sizeof(png_color));
for (i = 0; i < pal->ncolors; i++) {
pal_ptr[i].red = pal->colors[i].r;
pal_ptr[i].green = pal->colors[i].g;
pal_ptr[i].blue = pal->colors[i].b;
}
png_set_PLTE(png_ptr, info_ptr, pal_ptr, pal->ncolors);
free(pal_ptr);
}
else if (surface->format->BytesPerPixel > 3 || surface->format->Amask)
colortype |= PNG_COLOR_MASK_ALPHA;
png_set_IHDR(png_ptr, info_ptr, surface->w, surface->h, 8, colortype,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
// png_set_packing(png_ptr);
/* Allow BGR surfaces */
if (surface->format->Rmask == bmask
&& surface->format->Gmask == gmask
&& surface->format->Bmask == rmask)
png_set_bgr(png_ptr);
/* Write everything */
png_write_info(png_ptr, info_ptr);
#ifdef USE_ROW_POINTERS
row_pointers = (png_bytep*) malloc(sizeof(png_bytep)*surface->h);
for (i = 0; i < surface->h; i++)
row_pointers[i] = (png_bytep)(Uint8*)surface->pixels + i * surface->pitch;
png_write_image(png_ptr, row_pointers);
free(row_pointers);
#else
for (i = 0; i < surface->h; i++)
png_write_row(png_ptr, (png_bytep)(Uint8*)surface->pixels + i * surface->pitch);
#endif
png_write_end(png_ptr, info_ptr);
/* Done */
png_destroy_write_struct(&png_ptr, &info_ptr);
if (freedst) SDL_RWclose(dst);
return (SUCCESS);
}
#endif /* defined(HAVE_LIBPNG) */
/*
Some platforms (OS X), require that ALL input event processing be
performed by the main thread of the process.
To satisfy this requirement, we leverage the SDL_MAIN functionality
which does:
#defines main SDL_main
and we define the main() entry point here. Locally, we run the
application's SDL_main in a separate thread, and while that thread
is running, the main thread performs event handling and dispatch.
*/
#define EVENT_REDRAW 1 /* redraw event for SDL */
#define EVENT_CLOSE 2 /* close event for SDL */
#define EVENT_CURSOR 3 /* new cursor for SDL */
#define EVENT_WARP 4 /* warp mouse position for SDL */
#define EVENT_DRAW 5 /* draw/blit region for SDL */
#define EVENT_SHOW 6 /* show SDL capabilities */
#define EVENT_OPEN 7 /* vid_open request */
#define EVENT_EXIT 8 /* program exit */
#define EVENT_SCREENSHOT 9 /* show SDL capabilities */
#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);
int vid_video_events (void);
void vid_show_video_event (void);
void vid_screenshot_event (void);
/*
libSDL and libSDL2 have significantly different APIs.
The consequence is that this code has significant #ifdef sections.
The current structure is to implement the API differences in each
routine that has a difference. This allows the decision and flow
logic to exist once and thus to allow logic changes to be implemented
in one place.
*/
t_bool vid_mouse_captured;
int32 vid_flags; /* Open Flags */
int32 vid_width;
int32 vid_height;
t_bool vid_ready;
#if SDL_MAJOR_VERSION == 1
/*
Some platforms that use X11 display technology have libSDL
environments which need to call XInitThreads when libSDL is used
in multi-threaded programs. This routine attempts to locate
the X11 shareable library and if it is found loads it and calls
the XInitThreads routine to meet this requirement.
*/
#ifdef HAVE_DLOPEN
#include <dlfcn.h>
#endif
static void _XInitThreads (void)
{
#ifdef HAVE_DLOPEN
static void *hLib = 0; /* handle to Library */
#define __STR_QUOTE(tok) #tok
#define __STR(tok) __STR_QUOTE(tok)
static const char* lib_name = "libX11." __STR(HAVE_DLOPEN);
typedef int (*_func)();
_func _func_ptr;
if (!hLib)
hLib = dlopen(lib_name, RTLD_NOW);
if (hLib)
_func_ptr = (_func)((size_t)dlsym(hLib, "XInitThreads"));
if (_func_ptr)
_func_ptr();
#endif
}
t_bool vid_key_state[SDLK_LAST];
SDL_Surface *vid_image; /* video buffer */
SDL_Surface *vid_window; /* window handle */
#else
t_bool vid_key_state[SDL_NUM_SCANCODES];
SDL_Texture *vid_texture; /* video buffer in GPU */
SDL_Renderer *vid_renderer;
SDL_Window *vid_window; /* window handle */
uint32 vid_windowID;
#endif
SDL_Thread *vid_thread_handle = NULL; /* event thread handle */
SDL_Cursor *vid_cursor = NULL; /* current cursor */
t_bool vid_cursor_visible = FALSE; /* cursor visibility state */
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;
#if defined (SDL_MAIN_AVAILABLE)
#if defined (main)
#undef main
#endif
static int main_argc;
static char **main_argv;
static SDL_Thread *vid_main_thread_handle;
int main_thread (void *arg)
{
SDL_Event user_event;
int stat;
stat = SDL_main (main_argc, main_argv);
user_event.type = SDL_USEREVENT;
user_event.user.code = EVENT_EXIT;
user_event.user.data1 = NULL;
user_event.user.data2 = NULL;
while (SDL_PushEvent (&user_event) < 0)
sim_os_ms_sleep (10);
return stat;
}
int main (int argc, char *argv[])
{
SDL_Event event;
int status;
main_argc = argc;
main_argv = argv;
#if SDL_MAJOR_VERSION == 1
_XInitThreads();
SDL_Init (SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE);
vid_main_thread_handle = SDL_CreateThread (main_thread , NULL);
#else
SDL_SetHint (SDL_HINT_RENDER_DRIVER, "software");
SDL_Init (SDL_INIT_VIDEO);
vid_main_thread_handle = SDL_CreateThread (main_thread , "simh-main", NULL);
#endif
while (1) {
int status = SDL_WaitEvent (&event);
if (status == 1) {
if (event.type == SDL_USEREVENT) {
if (event.user.code == EVENT_EXIT)
break;
if (event.user.code == EVENT_OPEN)
vid_video_events ();
else {
if (event.user.code == EVENT_SHOW)
vid_show_video_event ();
else {
if (event.user.code == EVENT_SCREENSHOT)
vid_screenshot_event ();
else {
sim_printf ("main(): Unexpected User event: %d\n", event.user.code);
break;
}
}
}
}
else {
// sim_printf ("main(): Ignoring unexpected event: %d\n", event.type);
}
}
else {
if (status < 0)
sim_printf ("main() - ` error: %s\n", SDL_GetError());
}
}
SDL_WaitThread (vid_main_thread_handle, &status);
SDL_Quit ();
return status;
}
static t_stat vid_create_window ()
{
int wait_count = 0;
SDL_Event user_event;
vid_ready = FALSE;
user_event.type = SDL_USEREVENT;
user_event.user.code = EVENT_OPEN;
user_event.user.data1 = NULL;
user_event.user.data2 = NULL;
SDL_PushEvent (&user_event);
while ((!vid_ready) && (++wait_count < 20))
sim_os_ms_sleep (100);
if (!vid_ready) {
vid_close ();
return SCPE_OPENERR;
}
return SCPE_OK;
}
#else
static int vid_create_window ()
{
int wait_count = 0;
#if SDL_MAJOR_VERSION == 1
vid_thread_handle = SDL_CreateThread (vid_thread, NULL);
#else
vid_thread_handle = SDL_CreateThread (vid_thread, "vid-thread", NULL);
#endif
if (vid_thread_handle == NULL) {
vid_close ();
return SCPE_OPENERR;
}
while ((!vid_ready) && (++wait_count < 20))
sim_os_ms_sleep (100);
if (!vid_ready) {
vid_close ();
return SCPE_OPENERR;
}
return SCPE_OK;
}
#endif
t_stat vid_open (DEVICE *dptr, uint32 width, uint32 height, int flags)
{
if (!vid_active) {
int wait_count = 0;
t_stat stat;
vid_flags = flags;
vid_active = TRUE;
vid_width = width;
vid_height = height;
vid_mouse_captured = FALSE;
vid_cursor_visible = (vid_flags & SIM_VID_INPUTCAPTURED);
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;
stat = vid_create_window ();
if (stat != SCPE_OK)
return stat;
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)
{
if (vid_active) {
SDL_Event user_event;
int status;
vid_active = FALSE;
if (vid_ready) {
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;
while (SDL_PushEvent (&user_event) < 0)
sim_os_ms_sleep (10);
if (vid_thread_handle) {
SDL_WaitThread (vid_thread_handle, &status);
vid_thread_handle = NULL;
}
vid_dev = NULL;
}
while (vid_ready)
sim_os_ms_sleep (10);
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)
{
t_stat stat = SCPE_EOF;
SIM_MOUSE_EVENT *nev;
if (SDL_SemTryWait (vid_mouse_events.sem) == 0) {
if (vid_mouse_events.count > 0) {
stat = SCPE_OK;
*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;
nev = &vid_mouse_events.events[vid_mouse_events.head];
if ((vid_mouse_events.count > 0) &&
(0 == (ev->x_rel + nev->x_rel)) &&
(0 == (ev->y_rel + nev->y_rel)) &&
(ev->b1_state == nev->b1_state) &&
(ev->b2_state == nev->b2_state) &&
(ev->b3_state == nev->b3_state)) {
if ((++vid_mouse_events.head) == MAX_EVENTS)
vid_mouse_events.head = 0;
vid_mouse_events.count--;
stat = SCPE_EOF;
sim_debug (SIM_VID_DBG_MOUSE, vid_dev, "vid_poll_mouse: ignoring bouncing events\n");
}
}
if (SDL_SemPost (vid_mouse_events.sem))
sim_printf ("%s: vid_poll_mouse(): SDL_SemPost error: %s\n", sim_dname(vid_dev), SDL_GetError());
}
return stat;
}
void vid_draw (int32 x, int32 y, int32 w, int32 h, uint32 *buf)
{
#if SDL_MAJOR_VERSION == 1
int32 i;
uint32* pixels;
sim_debug (SIM_VID_DBG_VIDEO, vid_dev, "vid_draw(%d, %d, %d, %d)\n", x, y, w, h);
pixels = (uint32 *)vid_image->pixels;
for (i = 0; i < h; i++)
memcpy (pixels + ((i + y) * vid_width) + x, buf + w*i, w*sizeof(*pixels));
#else
SDL_Event user_event;
SDL_Rect *vid_dst;
uint32 *vid_data;
sim_debug (SIM_VID_DBG_VIDEO, vid_dev, "vid_draw(%d, %d, %d, %d)\n", x, y, w, h);
vid_dst = (SDL_Rect *)malloc (sizeof(*vid_dst));
vid_dst->x = x;
vid_dst->y = y;
vid_dst->w = w;
vid_dst->h = h;
vid_data = (uint32 *)malloc (w*h*sizeof(*buf));
memcpy (vid_data, buf, w*h*sizeof(*buf));
user_event.type = SDL_USEREVENT;
user_event.user.code = EVENT_DRAW;
user_event.user.data1 = (void *)vid_dst;
user_event.user.data2 = (void *)vid_data;
if (SDL_PushEvent (&user_event) < 0) {
sim_printf ("%s: vid_draw() SDL_PushEvent error: %s\n", sim_dname(vid_dev), SDL_GetError());
free (vid_dst);
free (vid_data);
}
#endif
}
t_stat vid_set_cursor (t_bool visible, uint32 width, uint32 height, uint8 *data, uint8 *mask)
{
SDL_Cursor *cursor = SDL_CreateCursor (data, mask, width, height, 0, 0);
SDL_Event user_event;
sim_debug (SIM_VID_DBG_CURSOR, vid_dev, "vid_set_cursor(%s, %d, %d) Setting New Cursor\n", visible ? "visible" : "invisible", width, height);
if (sim_deb) {
uint32 i, j;
for (i=0; i<height; i++) {
sim_debug (SIM_VID_DBG_CURSOR, vid_dev, "Cursor: ");
for (j=0; j<width; j++) {
int byte = (j + i*width) >> 3;
int bit = 7 - ((j + i*width) & 0x7);
static char mode[] = "TWIB";
sim_debug (SIM_VID_DBG_CURSOR, vid_dev, "%c", mode[(((data[byte]>>bit)&1)<<1)|(mask[byte]>>bit)&1]);
}
sim_debug (SIM_VID_DBG_CURSOR, vid_dev, "\n");
}
}
user_event.type = SDL_USEREVENT;
user_event.user.code = EVENT_CURSOR;
user_event.user.data1 = cursor;
user_event.user.data2 = (void *)((size_t)visible);
if (SDL_PushEvent (&user_event) < 0) {
sim_printf ("%s: vid_set_cursor() SDL_PushEvent error: %s\n", sim_dname(vid_dev), SDL_GetError());
SDL_FreeCursor (cursor);
}
return SCPE_OK;
}
void vid_set_cursor_position (int32 x, int32 y)
{
int32 x_delta = vid_cursor_x - x;
int32 y_delta = vid_cursor_y - y;
if ((x_delta) || (y_delta)) {
sim_debug (SIM_VID_DBG_CURSOR, vid_dev, "vid_set_cursor_position(%d, %d) - Cursor position changed\n", x, y);
/* Any queued mouse motion events need to have their relative
positions adjusted since they were queued based on different info. */
if (SDL_SemWait (vid_mouse_events.sem) == 0) {
int32 i;
SIM_MOUSE_EVENT *ev;
for (i=0; i<vid_mouse_events.count; i++) {
ev = &vid_mouse_events.events[(vid_mouse_events.head + i)%MAX_EVENTS];
sim_debug (SIM_VID_DBG_CURSOR, vid_dev, "Pending Mouse Motion Event Adjusted from: (%d, %d) to (%d, %d)\n", ev->x_rel, ev->y_rel, ev->x_rel + x_delta, ev->y_rel + y_delta);
vid_mouse_xrel -= ev->x_rel; /* remove previously accumulated relative position */
vid_mouse_yrel -= ev->y_rel;
ev->x_rel += x_delta;
ev->y_rel += y_delta;
vid_mouse_xrel += ev->x_rel; /* update cumulative x & y rel */
vid_mouse_yrel += ev->y_rel;
}
if (SDL_SemPost (vid_mouse_events.sem))
sim_printf ("%s: vid_set_cursor_position(): SDL_SemPost error: %s\n", sim_dname(vid_dev), SDL_GetError());
}
else {
sim_printf ("%s: vid_set_cursor_position(): SDL_SemWait error: %s\n", sim_dname(vid_dev), SDL_GetError());
}
vid_cursor_x = x;
vid_cursor_y = y;
if (vid_cursor_visible) {
SDL_Event user_event;
user_event.type = SDL_USEREVENT;
user_event.user.code = EVENT_WARP;
user_event.user.data1 = NULL;
user_event.user.data2 = NULL;
if (SDL_PushEvent (&user_event) < 0)
sim_printf ("%s: vid_set_cursor_position() SDL_PushEvent error: %s\n", sim_dname(vid_dev), SDL_GetError());
sim_debug (SIM_VID_DBG_CURSOR, vid_dev, "vid_set_cursor_position() - Warp Queued\n");
}
else {
sim_debug (SIM_VID_DBG_CURSOR, vid_dev, "vid_set_cursor_position() - Warp Skipped\n");
}
}
}
void vid_refresh (void)
{
SDL_Event user_event;
sim_debug (SIM_VID_DBG_VIDEO, vid_dev, "vid_refresh() - Queueing Refresh Event\n");
user_event.type = SDL_USEREVENT;
user_event.user.code = EVENT_REDRAW;
user_event.user.data1 = NULL;
user_event.user.data2 = NULL;
if (SDL_PushEvent (&user_event) < 0)
sim_printf ("%s: vid_refresh() SDL_PushEvent error: %s\n", sim_dname(vid_dev), SDL_GetError());
}
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;
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;
#if SDL_MAJOR_VERSION == 1
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;
#else
case SDLK_KP_0:
return SIM_KEY_KP_INSERT;
case SDLK_KP_1:
return SIM_KEY_KP_END;
case SDLK_KP_2:
return SIM_KEY_KP_DOWN;
case SDLK_KP_3:
return SIM_KEY_KP_PAGE_DOWN;
case SDLK_KP_4:
return SIM_KEY_KP_LEFT;
case SDLK_KP_5:
return SIM_KEY_KP_5;
case SDLK_KP_6:
return SIM_KEY_KP_RIGHT;
case SDLK_KP_7:
return SIM_KEY_KP_HOME;
case SDLK_KP_8:
return SIM_KEY_KP_UP;
case SDLK_KP_9:
return SIM_KEY_KP_PAGE_UP;
#endif
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: