-
Notifications
You must be signed in to change notification settings - Fork 20
/
sim_video.c
2945 lines (2537 loc) · 98.2 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
*/
#if defined(HAVE_LIBPNG) && defined(USE_SIM_VIDEO) && defined(HAVE_LIBSDL)
#include <png.h>
#endif
#include "sim_video.h"
#include "scp.h"
int vid_active = 0;
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;
static VID_QUIT_CALLBACK vid_quit_callback = NULL;
static VID_GAMEPAD_CALLBACK motion_callback[10];
static VID_GAMEPAD_CALLBACK button_callback[10];
static int vid_gamepad_inited = 0;
t_stat vid_register_quit_callback (VID_QUIT_CALLBACK callback)
{
vid_quit_callback = callback;
return SCPE_OK;
}
static t_stat register_callback (void **array, int n, void *callback)
{
int i, j = -1;
if (!vid_gamepad_inited) {
return SCPE_NOATT;
}
for (i = 0; i < n; i++) {
if (array[i] == callback)
return SCPE_ALATT;
if (array[i] == NULL)
j = i;
}
if (j != -1) {
array[j] = callback;
return SCPE_OK;
}
return SCPE_NXM;
}
t_stat vid_register_gamepad_motion_callback (VID_GAMEPAD_CALLBACK callback)
{
int n = sizeof (motion_callback) / sizeof (callback);
return register_callback ((void **)motion_callback, n, (void *)callback);
}
t_stat vid_register_gamepad_button_callback (VID_GAMEPAD_CALLBACK callback)
{
int n = sizeof (button_callback) / sizeof (callback);
return register_callback ((void **)button_callback, n, (void *)callback);
}
t_stat vid_show (FILE* st, DEVICE *dptr, UNIT* uptr, int32 val, CONST char* desc)
{
return vid_show_video (st, uptr, val, desc);
}
#if defined(USE_SIM_VIDEO) && defined(HAVE_LIBSDL)
static const char *vid_dname (DEVICE *dev)
{
return dev ? sim_dname(dev) : "Video Device";
}
static int vid_gamepad_ok = 0; /* Or else just joysticks. */
char vid_release_key[64] = "Ctrl-Right-Shift";
#include <SDL.h>
#include <SDL_thread.h>
static const char *key_names[] =
{"F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12",
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z",
"BACKQUOTE", "MINUS", "EQUALS", "LEFT_BRACKET", "RIGHT_BRACKET",
"SEMICOLON", "SINGLE_QUOTE", "BACKSLASH", "LEFT_BACKSLASH", "COMMA",
"PERIOD", "SLASH", "PRINT", "SCRL_LOCK", "PAUSE", "ESC", "BACKSPACE",
"TAB", "ENTER", "SPACE", "INSERT", "DELETE", "HOME", "END", "PAGE_UP",
"PAGE_DOWN", "UP", "DOWN", "LEFT", "RIGHT", "CAPS_LOCK", "NUM_LOCK",
"ALT_L", "ALT_R", "CTRL_L", "CTRL_R", "SHIFT_L", "SHIFT_R",
"WIN_L", "WIN_R", "MENU", "KP_ADD", "KP_SUBTRACT", "KP_END", "KP_DOWN",
"KP_PAGE_DOWN", "KP_LEFT", "KP_RIGHT", "KP_HOME", "KP_UP", "KP_PAGE_UP",
"KP_INSERT", "KP_DELETE", "KP_5", "KP_ENTER", "KP_MULTIPLY", "KP_DIVIDE"
};
const char *vid_key_name (uint32 key)
{
static char tmp_key_name[40];
if (key < sizeof(key_names)/sizeof(key_names[0]))
sprintf (tmp_key_name, "SIM_KEY_%s", key_names[key]);
else
sprintf (tmp_key_name, "UNKNOWN KEY: %d", key);
return tmp_key_name;
}
#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 <zlib.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 /* produce screenshot of video window */
#define EVENT_BEEP 10 /* audio beep */
#define EVENT_FULLSCREEN 11 /* fullscreen */
#define EVENT_SIZE 12 /* set window size */
#define EVENT_LOGICAL 13 /* set window logical size */
#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 (VID_DISPLAY *vptr);
void vid_show_video_event (void);
void vid_screenshot_event (void);
void vid_beep_event (void);
static void vid_beep_setup (int duration_ms, int tone_frequency);
static void vid_beep_cleanup (void);
static void vid_controllers_setup (DEVICE *dptr);
static void vid_controllers_cleanup (void);
struct VID_DISPLAY {
t_bool vid_active_window;
t_bool vid_mouse_captured;
int32 vid_flags; /* Open Flags */
int32 vid_width;
int32 vid_height;
t_bool vid_ready;
char vid_title[128];
SDL_Texture *vid_texture; /* video buffer in GPU */
SDL_Renderer *vid_renderer;
SDL_Window *vid_window; /* window handle */
SDL_PixelFormat *vid_format;
uint32 vid_windowID;
SDL_mutex *vid_draw_mutex; /* window update mutex */
SDL_Cursor *vid_cursor; /* current cursor */
t_bool vid_cursor_visible; /* cursor visibility state */
DEVICE *vid_dev;
t_bool vid_key_state[SDL_NUM_SCANCODES];
VID_DISPLAY *next;
t_bool vid_blending;
SDL_Rect *vid_dst_last;
SDL_Rect vid_rect;
uint32 *vid_data_last;
};
SDL_Thread *vid_thread_handle = NULL; /* event thread handle */
static VID_DISPLAY vid_first;
KEY_EVENT_QUEUE vid_key_events; /* keyboard events */
MOUSE_EVENT_QUEUE vid_mouse_events; /* mouse events */
static VID_DISPLAY *vid_get_event_window (SDL_Event *ev, Uint32 windowID)
{
static Uint32 lastID = 0xffffffff;
static VID_DISPLAY *last_display = NULL;
VID_DISPLAY *vptr;
SDL_KeyboardEvent *kev;
SDL_MouseButtonEvent *bev;
SDL_MouseMotionEvent *mev;
SDL_WindowEvent *wev;
SDL_UserEvent *uev;
SDL_version ver;
if (windowID == lastID)
return last_display;
for (vptr = &vid_first; vptr != NULL; vptr = vptr->next) {
if (windowID == vptr->vid_windowID) {
lastID = windowID;
return last_display = vptr;
}
}
switch (ev->type) {
case SDL_KEYDOWN:
case SDL_KEYUP:
kev = (SDL_KeyboardEvent *)ev;
sim_messagef (SCPE_OK, "Unrecognized key event.\n");
sim_messagef (SCPE_OK, " type = %u\n", kev->type);
sim_messagef (SCPE_OK, " timestamp = %u\n", kev->timestamp);
sim_messagef (SCPE_OK, " windowID = %u\n", kev->windowID);
sim_messagef (SCPE_OK, " state = %u\n", kev->state);
sim_messagef (SCPE_OK, " repeat = %u\n", kev->repeat);
sim_messagef (SCPE_OK, " scancode = %d\n", kev->keysym.scancode);
sim_messagef (SCPE_OK, " sym = %d\n", kev->keysym.sym);
sim_messagef (SCPE_OK, " mod = %u\n", kev->keysym.mod);
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
bev = (SDL_MouseButtonEvent *)ev;
sim_messagef (SCPE_OK, "Unrecognized mouse button event.\n");
sim_messagef (SCPE_OK, " type = %u\n", bev->type);
sim_messagef (SCPE_OK, " timestamp = %u\n", bev->timestamp);
sim_messagef (SCPE_OK, " windowID = %u\n", bev->windowID);
sim_messagef (SCPE_OK, " which = %u\n", bev->which);
sim_messagef (SCPE_OK, " button = %u\n", bev->button);
sim_messagef (SCPE_OK, " state = %u\n", bev->state);
sim_messagef (SCPE_OK, " clicks = %u\n", bev->clicks);
sim_messagef (SCPE_OK, " x = %d\n", bev->x);
sim_messagef (SCPE_OK, " y = %d\n", bev->y);
break;
case SDL_MOUSEMOTION:
mev = (SDL_MouseMotionEvent *)ev;
sim_messagef (SCPE_OK, "Unrecognized mouse motion event.\n");
sim_messagef (SCPE_OK, " type = %u\n", mev->type);
sim_messagef (SCPE_OK, " timestamp = %u\n", mev->timestamp);
sim_messagef (SCPE_OK, " windowID = %u\n", mev->windowID);
sim_messagef (SCPE_OK, " which = %u\n", mev->which);
sim_messagef (SCPE_OK, " state = %u\n", mev->state);
sim_messagef (SCPE_OK, " x = %d\n", mev->x);
sim_messagef (SCPE_OK, " y = %d\n", mev->y);
sim_messagef (SCPE_OK, " xrel = %d\n", mev->xrel);
sim_messagef (SCPE_OK, " yrel = %d\n", mev->yrel);
break;
case SDL_WINDOWEVENT:
wev = (SDL_WindowEvent *)ev;
sim_messagef (SCPE_OK, "Unrecognized window event.\n");
sim_messagef (SCPE_OK, " type = %u\n", wev->type);
sim_messagef (SCPE_OK, " timestamp = %u\n", wev->timestamp);
sim_messagef (SCPE_OK, " windowID = %u\n", wev->windowID);
sim_messagef (SCPE_OK, " event = %u\n", wev->event);
sim_messagef (SCPE_OK, " data1 = %d\n", wev->data1);
sim_messagef (SCPE_OK, " data2 = %d\n", wev->data2);
break;
case SDL_USEREVENT:
uev = (SDL_UserEvent *)ev;
sim_messagef (SCPE_OK, "Unrecognized user event.\n");
sim_messagef (SCPE_OK, " type = %u\n", uev->type);
sim_messagef (SCPE_OK, " timestamp = %u\n", uev->timestamp);
sim_messagef (SCPE_OK, " windowID = %u\n", uev->windowID);
sim_messagef (SCPE_OK, " code = %d\n", uev->code);
sim_messagef (SCPE_OK, " data1 = %p\n", uev->data1);
sim_messagef (SCPE_OK, " data2 = %p\n", uev->data2);
break;
default:
sim_messagef (SCPE_OK, "Unrecognized event type %u\n", ev->type);
break;
}
SDL_GetVersion(&ver);
sim_messagef (SCPE_OK,
"\nSIMH has encountered a bug in SDL2. An upgrade to SDL2\n"
"version 2.0.14 should fix this problem. You are running "
"version %d.%d.%d.\n", ver.major, ver.minor, ver.patch);
return NULL;
}
#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;
SDL_SetHint (SDL_HINT_RENDER_DRIVER, "software");
#if defined (SDL_HINT_VIDEO_ALLOW_SCREENSAVER)
/* If this hint is defined, the default is to disable the screen saver.
We want to leave the screen saver enabled. */
SDL_SetHint (SDL_HINT_VIDEO_ALLOW_SCREENSAVER, "1");
#endif
status = SDL_Init (SDL_INIT_EVENTS);
if (status) {
fprintf (stderr, "SDL Video subsystem can't initialize: %s\n", SDL_GetError ());
exit (1);
}
vid_main_thread_handle = SDL_CreateThread (main_thread , "simh-main", NULL);
if (vid_main_thread_handle == NULL) {
fprintf (stderr, "SDL_CreateThread failed: %s\n", SDL_GetError ());
exit (1);
}
vid_beep_setup (400, 660);
memset (&event, 0, sizeof (event));
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) {
SDL_Init (SDL_INIT_VIDEO);
vid_video_events ((VID_DISPLAY *)event.user.data1);
}
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);
vid_beep_cleanup ();
SDL_Quit ();
return status;
}
static t_stat vid_create_window (VID_DISPLAY *vptr)
{
int wait_count = 0;
SDL_Event user_event;
vptr->vid_ready = FALSE;
user_event.type = SDL_USEREVENT;
user_event.user.code = EVENT_OPEN;
user_event.user.data1 = vptr;
user_event.user.data2 = NULL;
SDL_PushEvent (&user_event);
while ((!vptr->vid_ready) && (++wait_count < 20))
sim_os_ms_sleep (100);
if (!vptr->vid_ready) {
vid_close ();
return SCPE_OPENERR;
}
return SCPE_OK;
}
#else
static int vid_create_window (VID_DISPLAY *vptr)
{
int wait_count = 0;
if (vid_thread_handle == NULL)
vid_thread_handle = SDL_CreateThread (vid_thread, "vid-thread", vptr);
else {
SDL_Event user_event;
vptr->vid_ready = FALSE;
user_event.type = SDL_USEREVENT;
user_event.user.code = EVENT_OPEN;
user_event.user.data1 = vptr;
user_event.user.data2 = NULL;
SDL_PushEvent (&user_event);
}
if (vid_thread_handle == NULL) {
vid_close ();
return SCPE_OPENERR;
}
while ((!vptr->vid_ready) && (++wait_count < 20))
sim_os_ms_sleep (100);
if (!vptr->vid_ready) {
vid_close ();
return SCPE_OPENERR;
}
return SCPE_OK;
}
#endif
static void vid_controllers_setup (DEVICE *dev)
{
SDL_Joystick *y;
SDL_version ver;
int i, n;
if (vid_gamepad_inited++)
return;
/* Check that the SDL_GameControllerFromInstanceID function is
available at run time. */
SDL_GetVersion(&ver);
vid_gamepad_ok = (ver.major > 2 ||
(ver.major == 2 && (ver.minor > 0 || ver.patch >= 4)));
sim_debug (SIM_VID_DBG_VIDEO|SIM_VID_DBG_JOYSTICK, dev, "SDL %d.%d.%d %s support game controllers.\n", ver.major, ver.minor, ver.patch, vid_gamepad_ok ? "DOES" : "DOES NOT");
if (vid_gamepad_ok)
SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER);
else
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
if (SDL_JoystickEventState (SDL_ENABLE) < 0) {
if (vid_gamepad_ok)
SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER);
else
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
sim_printf ("%s: vid_controllers_setup(): SDL_JoystickEventState error: %s\n", vid_dname(dev), SDL_GetError());
return;
}
if (vid_gamepad_ok && SDL_GameControllerEventState (SDL_ENABLE) < 0) {
if (vid_gamepad_ok)
SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER);
else
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
sim_printf ("%s: vid_controllers_setup(): SDL_GameControllerEventState error: %s\n", vid_dname(dev), SDL_GetError());
return;
}
n = SDL_NumJoysticks();
sim_debug (SIM_VID_DBG_VIDEO|SIM_VID_DBG_JOYSTICK, dev, "Game controllers found: %d\n", n);
for (i = 0; i < n; i++) {
if (vid_gamepad_ok && SDL_IsGameController (i)) {
SDL_GameController *x = SDL_GameControllerOpen (i);
if (x != NULL) {
sim_debug (SIM_VID_DBG_VIDEO|SIM_VID_DBG_JOYSTICK, dev,
"Game controller: %s\n", SDL_GameControllerNameForIndex(i));
}
}
else {
y = SDL_JoystickOpen (i);
if (y != NULL) {
sim_debug (SIM_VID_DBG_VIDEO|SIM_VID_DBG_JOYSTICK, dev,
"%s\n", SDL_JoystickNameForIndex(i));
sim_debug (SIM_VID_DBG_VIDEO|SIM_VID_DBG_JOYSTICK, dev,
"Number of axes: %d, buttons: %d\n",
SDL_JoystickNumAxes(y),
SDL_JoystickNumButtons(y));
}
}
}
}
static void vid_controllers_cleanup (void)
{
if (0 == (--vid_gamepad_inited)) {
memset (motion_callback, 0, sizeof motion_callback);
memset (button_callback, 0, sizeof button_callback);
if (vid_gamepad_ok)
SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER);
else
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
}
}
static t_stat vid_init_window (VID_DISPLAY *vptr, DEVICE *dptr, const char *title, uint32 width, uint32 height, int flags)
{
t_stat stat;
if ((strlen(sim_name) + 7 + (dptr ? strlen (dptr->name) : 0) + (title ? strlen (title) : 0)) < sizeof (vptr->vid_title))
sprintf (vptr->vid_title, "%s%s%s%s%s", sim_name, dptr ? " - " : "", dptr ? dptr->name : "", title ? " - " : "", title ? title : "");
else
sprintf (vptr->vid_title, "%s", sim_name);
vptr->vid_flags = flags;
vptr->vid_active_window = TRUE;
vptr->vid_width = width;
vptr->vid_height = height;
vptr->vid_mouse_captured = FALSE;
vptr->vid_cursor_visible = (vptr->vid_flags & SIM_VID_INPUTCAPTURED);
vptr->vid_blending = FALSE;
vptr->vid_ready = FALSE;
if (!vid_active) {
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);
}
vptr->vid_dev = dptr;
memset (motion_callback, 0, sizeof motion_callback);
memset (button_callback, 0, sizeof button_callback);
stat = vid_create_window (vptr);
if (stat != SCPE_OK)
return stat;
sim_debug (SIM_VID_DBG_VIDEO|SIM_VID_DBG_KEY|SIM_VID_DBG_MOUSE, vptr->vid_dev, "vid_open() - Success\n");
return SCPE_OK;
}
t_stat vid_open_window (VID_DISPLAY **vptr, DEVICE *dptr, const char *title, uint32 width, uint32 height, int flags)
{
t_stat r;
*vptr = (VID_DISPLAY *)malloc (sizeof (VID_DISPLAY));
if (*vptr == NULL)
return SCPE_NXM;
(*vptr)->next = vid_first.next;
vid_first.next = *vptr;
r = vid_init_window (*vptr, dptr, title, width, height, flags);
if (r != SCPE_OK) {
vid_first.next = (*vptr)->next;
free (*vptr);
*vptr = NULL;
return r;
}
return SCPE_OK;
}
t_stat vid_open (DEVICE *dptr, const char *title, uint32 width, uint32 height, int flags)
{
if (!vid_first.vid_active_window)
return vid_init_window (&vid_first, dptr, title, width, height, flags);
return SCPE_OK;
}
t_stat vid_close_window (VID_DISPLAY *vptr)
{
SDL_Event user_event;
int status;
if (vptr->vid_ready) {
sim_debug (SIM_VID_DBG_VIDEO|SIM_VID_DBG_KEY|SIM_VID_DBG_MOUSE, vptr->vid_dev, "vid_close()\n");
user_event.type = SDL_USEREVENT;
user_event.user.windowID = vptr->vid_windowID;
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);
vptr->vid_dev = NULL;
}
if (vid_thread_handle && vid_active <= 1) {
SDL_WaitThread (vid_thread_handle, &status);
vid_thread_handle = NULL;
}
while (vptr->vid_ready)
sim_os_ms_sleep (10);
vptr->vid_active_window = FALSE;
if (!vid_active && vid_mouse_events.sem) {
SDL_DestroySemaphore(vid_mouse_events.sem);
vid_mouse_events.sem = NULL;
}
if (!vid_active && vid_key_events.sem) {
SDL_DestroySemaphore(vid_key_events.sem);
vid_key_events.sem = NULL;
}
return SCPE_OK;
}
t_stat vid_close (void)
{
if (vid_first.vid_active_window)
return vid_close_window (&vid_first);
return SCPE_OK;
}
t_stat vid_close_all (void)
{
VID_DISPLAY *vptr;
vid_close ();
for (vptr = vid_first.next; vptr != NULL; vptr = vptr->next)
vid_close_window (vptr);
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, ev->dev, "vid_poll_mouse: ignoring bouncing events\n");
}
}
if (SDL_SemPost (vid_mouse_events.sem))
sim_printf ("vid_poll_mouse(): SDL_SemPost error: %s\n", SDL_GetError());
}
return stat;
}
uint32 vid_map_rgb_window (VID_DISPLAY *vptr, uint8 r, uint8 g, uint8 b)
{
return SDL_MapRGB (vptr->vid_format, r, g, b);
}
uint32 vid_map_rgb (uint8 r, uint8 g, uint8 b)
{
return vid_map_rgb_window (&vid_first, r, g, b);
}
uint32 vid_map_rgba_window (VID_DISPLAY *vptr, uint8 r, uint8 g, uint8 b, uint8 a)
{
return SDL_MapRGBA (vptr->vid_format, r, g, b, a);
}
void vid_draw_window (VID_DISPLAY *vptr, int32 x, int32 y, int32 w, int32 h, uint32 *buf)
{
SDL_Event user_event;
SDL_Rect *vid_dst, *last;
uint32 *vid_data;
sim_debug (SIM_VID_DBG_VIDEO, vptr->vid_dev, "vid_draw(%d, %d, %d, %d)\n", x, y, w, h);
SDL_LockMutex (vptr->vid_draw_mutex); /* Synchronize to check region dimensions */
last = vptr->vid_dst_last;
if (last && /* As yet unprocessed draw rectangle? */
(last->x == x) && (last->y == y) && /* AND identical position? */
(last->w == w) && (last->h == h)) { /* AND identical dimensions? */
memcpy (vptr->vid_data_last, buf, w*h*sizeof(*buf));/* Replace region contents */
SDL_UnlockMutex (vptr->vid_draw_mutex); /* Done */
return;
}
SDL_UnlockMutex (vptr->vid_draw_mutex);
vid_dst = (SDL_Rect *)malloc (sizeof(*vid_dst));
if (!vid_dst) {
sim_printf ("%s: vid_draw() memory allocation error\n", vid_dname(vptr->vid_dev));
return;
}
vid_dst->x = x;
vid_dst->y = y;
vid_dst->w = w;
vid_dst->h = h;
vid_data = (uint32 *)malloc (w*h*sizeof(*buf));
if (!vid_data) {
sim_printf ("%s: vid_draw() memory allocation error\n", vid_dname(vptr->vid_dev));
free (vid_dst);
return;
}
memcpy (vid_data, buf, w*h*sizeof(*buf));
user_event.type = SDL_USEREVENT;
user_event.user.windowID = vptr->vid_windowID;
user_event.user.code = EVENT_DRAW;
user_event.user.data1 = (void *)vid_dst;
user_event.user.data2 = (void *)vid_data;
SDL_LockMutex (vptr->vid_draw_mutex); /* protect vid_dst_last & vid_data_last */
vptr->vid_dst_last = vid_dst;
vptr->vid_data_last = vid_data;
SDL_UnlockMutex (vptr->vid_draw_mutex); /* done protection */
if (SDL_PushEvent (&user_event) < 0) {
sim_printf ("%s: vid_draw() SDL_PushEvent error: %s\n", vid_dname(vptr->vid_dev), SDL_GetError());
free (vid_dst);
free (vid_data);
}
}
void vid_draw (int32 x, int32 y, int32 w, int32 h, uint32 *buf)
{
vid_draw_window (&vid_first, x, y, w, h, buf);
}
t_stat vid_set_cursor_window (VID_DISPLAY *vptr, t_bool visible, uint32 width, uint32 height, uint8 *data, uint8 *mask, uint32 hot_x, uint32 hot_y)
{
SDL_Cursor *cursor = SDL_CreateCursor (data, mask, width, height, hot_x, hot_y);
SDL_Event user_event;
sim_debug (SIM_VID_DBG_CURSOR, vptr->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, vptr->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, vptr->vid_dev, "%c", mode[(((data[byte]>>bit)&1)<<1)|((mask[byte]>>bit)&1)]);
}
sim_debug (SIM_VID_DBG_CURSOR, vptr->vid_dev, "\n");
}
}
user_event.type = SDL_USEREVENT;
user_event.user.windowID = vptr->vid_windowID;
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", vid_dname(vptr->vid_dev), SDL_GetError());
SDL_FreeCursor (cursor);
}
return SCPE_OK;
}
t_stat vid_set_cursor (t_bool visible, uint32 width, uint32 height, uint8 *data, uint8 *mask, uint32 hot_x, uint32 hot_y)
{
return vid_set_cursor_window (&vid_first, visible, width, height, data, mask, hot_x, hot_y);
}
void vid_set_cursor_position_window (VID_DISPLAY *vptr, int32 x, int32 y)
{
int32 x_delta = vid_cursor_x - x;
int32 y_delta = vid_cursor_y - y;
if (vptr->vid_flags & SIM_VID_INPUTCAPTURED)
return;
if ((x_delta) || (y_delta)) {