-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsim_video.c
1379 lines (1113 loc) · 40.3 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
Copyright (c) 2018, Ian Schofield
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
01-Jan-2018 ISS Major changes to the structure of the video system. Functionality unchanged.
This module is now used by all simulators requiring display services.
SDL (Simple Direct Media Layer) version 2.x is now the default as this
is supported by many operating systems.
*/
#include "sim_defs.h"
#include "sim_video.h"
#include "scp.h"
#if defined(HAVE_LIBPNG)
#include <png.h>
#endif
// Global video system flgs/variables
uint32 vid_mono_palette[2]; /* Monochrome Color Map */
double *colmap; /* Parameter used by Refresh and vid_setpixel in sim_video.c */
int32 pxval; /* Also referenced in display.c */
int nostore=0; /* Enables storage display. Always defined. */
char vid_release_key[64] = "Ctrl-Right-Shift";
// Only include SDL code if specified. See end of this module for stubbed code.
#if defined(USE_SIM_VIDEO) && defined(HAVE_LIBSDL)
#define CUR_SIZE 5
#define EVENT_EXIT 8 /* program exit */
#define EVENT_BEEP 10 /* audio beep */
static int iwd,iht,told,tnew,tvl;
static const int MAXFONTPATH = 500;
static int init_x = 0; // Initial window position and size
static int init_y = 0;
static unsigned int init_w = 0;
static unsigned int init_h = 0;
static int32 lcurr_x = 0;
static int32 lcurr_y = 0;
static char vid_title[128];
static int screen_num,xloc,yloc;
static uint32 lstst=0,lstcd=0;
static unsigned char *pixels;
static int pitch,surlen;
SIM_MOUSE_EVENT *xmev = 0,*xhev = 0;
SIM_KEY_EVENT *xkev = 0;
static enum vid_stat{STOPPED,WINDOW_OK,RUNNING,CLOSING,CLOSED}vid_init;
static int init_flags;
unsigned int pixelval; // 24 bit RGB value
struct display *dp;
static int Refresh(void *info);
static int MLoop();
static void vid_close_window(void);
void vid_beep (void);
static void vid_beep_setup (int duration_ms, int tone_frequency);
static void vid_beep_cleanup (void);
static void vid_beep_event (void);
static SDL_Window *window = 0; // Declare some pointers
static SDL_Surface *surface = 0;
static SDL_Cursor *cursor = 0;
int32 vid_flags; /* Open Flags */
t_stat vid_set_release_key (FILE* st, UNIT* uptr, int32 val, CONST void* desc)
{
return SCPE_NOFNC;
}
t_stat vid_show_release_key (FILE* st, UNIT* uptr, int32 val, CONST void* desc)
{
if (vid_flags & SIM_VID_INPUTCAPTURED)
fprintf (st, "ReleaseKey=%s", vid_release_key);
else
fprintf (st, "Not captured");
return SCPE_OK;
}
t_stat vid_show_video (FILE* st, UNIT* uptr, int32 val, CONST void* desc)
{
int i;
fprintf (st, "Video support using SDL: %s\n", vid_version());
#if defined (SDL_MAIN_AVAILABLE)
fprintf (st, " SDL Events being processed on the main process thread\n");
#endif
if (window) {
fprintf (st, " Currently Active Video Window: (%d by %d pixels)\n", init_w, init_h);
fprintf (st, " ");
vid_show_release_key (st, uptr, val, desc);
fprintf (st, "\n");
}
for (i = 0; i < SDL_GetNumVideoDisplays(); ++i) {
SDL_DisplayMode display;
if (SDL_GetCurrentDisplayMode(i, &display)) {
fprintf (st, "Could not get display mode for video display #%d: %s", i, SDL_GetError());
}
else {
fprintf (st, " Display %s(#%d): current display mode is %dx%dpx @ %dhz. \n", SDL_GetDisplayName(i), i, display.w, display.h, display.refresh_rate);
}
}
fprintf (st, " Available SDL Renderers:\n");
for (i = 0; i < SDL_GetNumRenderDrivers(); ++i) {
SDL_RendererInfo info;
if (SDL_GetRenderDriverInfo (i, &info)) {
fprintf (st, "Could not get render driver info for driver #%d: %s", i, SDL_GetError());
}
else {
uint32 j, k;
static struct {uint32 format; const char *name;} PixelFormats[] = {
{SDL_PIXELFORMAT_INDEX1LSB, "Index1LSB"},
{SDL_PIXELFORMAT_INDEX1MSB, "Index1MSB"},
{SDL_PIXELFORMAT_INDEX4LSB, "Index4LSB"},
{SDL_PIXELFORMAT_INDEX4MSB, "Index4MSB"},
{SDL_PIXELFORMAT_INDEX8, "Index8"},
{SDL_PIXELFORMAT_RGB332, "RGB332"},
{SDL_PIXELFORMAT_RGB444, "RGB444"},
{SDL_PIXELFORMAT_RGB555, "RGB555"},
{SDL_PIXELFORMAT_BGR555, "BGR555"},
{SDL_PIXELFORMAT_ARGB4444, "ARGB4444"},
{SDL_PIXELFORMAT_RGBA4444, "RGBA4444"},
{SDL_PIXELFORMAT_ABGR4444, "ABGR4444"},
{SDL_PIXELFORMAT_BGRA4444, "BGRA4444"},
{SDL_PIXELFORMAT_ARGB1555, "ARGB1555"},
{SDL_PIXELFORMAT_RGBA5551, "RGBA5551"},
{SDL_PIXELFORMAT_ABGR1555, "ABGR1555"},
{SDL_PIXELFORMAT_BGRA5551, "BGRA5551"},
{SDL_PIXELFORMAT_RGB565, "RGB565"},
{SDL_PIXELFORMAT_BGR565, "BGR565"},
{SDL_PIXELFORMAT_RGB24, "RGB24"},
{SDL_PIXELFORMAT_BGR24, "BGR24"},
{SDL_PIXELFORMAT_RGB888, "RGB888"},
{SDL_PIXELFORMAT_RGBX8888, "RGBX8888"},
{SDL_PIXELFORMAT_BGR888, "BGR888"},
{SDL_PIXELFORMAT_BGRX8888, "BGRX8888"},
{SDL_PIXELFORMAT_ARGB8888, "ARGB8888"},
{SDL_PIXELFORMAT_RGBA8888, "RGBA8888"},
{SDL_PIXELFORMAT_ABGR8888, "ABGR8888"},
{SDL_PIXELFORMAT_BGRA8888, "BGRA8888"},
{SDL_PIXELFORMAT_ARGB2101010, "ARGB2101010"},
{SDL_PIXELFORMAT_YV12, "YV12"},
{SDL_PIXELFORMAT_IYUV, "IYUV"},
{SDL_PIXELFORMAT_YUY2, "YUY2"},
{SDL_PIXELFORMAT_UYVY, "UYVY"},
{SDL_PIXELFORMAT_YVYU, "YVYU"},
{SDL_PIXELFORMAT_UNKNOWN, "Unknown"}};
fprintf (st, " Render #%d - %s\n", i, info.name);
fprintf (st, " Flags: 0x%X - ", info.flags);
if (info.flags & SDL_RENDERER_SOFTWARE)
fprintf (st, "Software|");
if (info.flags & SDL_RENDERER_ACCELERATED)
fprintf (st, "Accelerated|");
if (info.flags & SDL_RENDERER_PRESENTVSYNC)
fprintf (st, "PresentVSync|");
if (info.flags & SDL_RENDERER_TARGETTEXTURE)
fprintf (st, "TargetTexture|");
fprintf (st, "\n");
if ((info.max_texture_height != 0) || (info.max_texture_width != 0))
fprintf (st, " Max Texture: %d by %d\n", info.max_texture_height, info.max_texture_width);
fprintf (st, " Pixel Formats:\n");
for (j=0; j<info.num_texture_formats; j++) {
for (k=0; 1; k++) {
if (PixelFormats[k].format == info.texture_formats[j]) {
fprintf (st, " %s\n", PixelFormats[k].name);
break;
}
if (PixelFormats[k].format == SDL_PIXELFORMAT_UNKNOWN) {
fprintf (st, " %s - 0x%X\n", PixelFormats[k].name, info.texture_formats[j]);
break;
}
}
}
}
}
if (window)
fprintf (st, " Currently Active Renderer: System default\n");
if (1) {
static const char *hints[] = {
#if defined (SDL_HINT_FRAMEBUFFER_ACCELERATION)
SDL_HINT_FRAMEBUFFER_ACCELERATION ,
#endif
#if defined (SDL_HINT_RENDER_DRIVER)
SDL_HINT_RENDER_DRIVER ,
#endif
#if defined (SDL_HINT_RENDER_OPENGL_SHADERS)
SDL_HINT_RENDER_OPENGL_SHADERS ,
#endif
#if defined (SDL_HINT_RENDER_DIRECT3D_THREADSAFE)
SDL_HINT_RENDER_DIRECT3D_THREADSAFE ,
#endif
#if defined (SDL_HINT_RENDER_DIRECT3D11_DEBUG)
SDL_HINT_RENDER_DIRECT3D11_DEBUG ,
#endif
#if defined (SDL_HINT_RENDER_SCALE_QUALITY)
SDL_HINT_RENDER_SCALE_QUALITY ,
#endif
#if defined (SDL_HINT_RENDER_VSYNC)
SDL_HINT_RENDER_VSYNC ,
#endif
#if defined (SDL_HINT_VIDEO_ALLOW_SCREENSAVER)
SDL_HINT_VIDEO_ALLOW_SCREENSAVER ,
#endif
#if defined (SDL_HINT_VIDEO_X11_XVIDMODE)
SDL_HINT_VIDEO_X11_XVIDMODE ,
#endif
#if defined (SDL_HINT_VIDEO_X11_XINERAMA)
SDL_HINT_VIDEO_X11_XINERAMA ,
#endif
#if defined (SDL_HINT_VIDEO_X11_XRANDR)
SDL_HINT_VIDEO_X11_XRANDR ,
#endif
#if defined (SDL_HINT_GRAB_KEYBOARD)
SDL_HINT_GRAB_KEYBOARD ,
#endif
#if defined (SDL_HINT_MOUSE_RELATIVE_MODE_WARP)
SDL_HINT_MOUSE_RELATIVE_MODE_WARP ,
#endif
#if defined (SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS)
SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS ,
#endif
#if defined (SDL_HINT_IDLE_TIMER_DISABLED)
SDL_HINT_IDLE_TIMER_DISABLED ,
#endif
#if defined (SDL_HINT_ORIENTATIONS)
SDL_HINT_ORIENTATIONS ,
#endif
#if defined (SDL_HINT_ACCELEROMETER_AS_JOYSTICK)
SDL_HINT_ACCELEROMETER_AS_JOYSTICK ,
#endif
#if defined (SDL_HINT_XINPUT_ENABLED)
SDL_HINT_XINPUT_ENABLED ,
#endif
#if defined (SDL_HINT_GAMECONTROLLERCONFIG)
SDL_HINT_GAMECONTROLLERCONFIG ,
#endif
#if defined (SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS)
SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS ,
#endif
#if defined (SDL_HINT_ALLOW_TOPMOST)
SDL_HINT_ALLOW_TOPMOST ,
#endif
#if defined (SDL_HINT_TIMER_RESOLUTION)
SDL_HINT_TIMER_RESOLUTION ,
#endif
#if defined (SDL_HINT_VIDEO_HIGHDPI_DISABLED)
SDL_HINT_VIDEO_HIGHDPI_DISABLED ,
#endif
#if defined (SDL_HINT_MAC_CTRL_CLICK_EMULATE_RIGHT_CLICK)
SDL_HINT_MAC_CTRL_CLICK_EMULATE_RIGHT_CLICK ,
#endif
#if defined (SDL_HINT_VIDEO_WIN_D3DCOMPILER)
SDL_HINT_VIDEO_WIN_D3DCOMPILER ,
#endif
#if defined (SDL_HINT_VIDEO_WINDOW_SHARE_PIXEL_FORMAT)
SDL_HINT_VIDEO_WINDOW_SHARE_PIXEL_FORMAT ,
#endif
#if defined (SDL_HINT_WINRT_PRIVACY_POLICY_URL)
SDL_HINT_WINRT_PRIVACY_POLICY_URL ,
#endif
#if defined (SDL_HINT_WINRT_PRIVACY_POLICY_LABEL)
SDL_HINT_WINRT_PRIVACY_POLICY_LABEL ,
#endif
#if defined (SDL_HINT_WINRT_HANDLE_BACK_BUTTON)
SDL_HINT_WINRT_HANDLE_BACK_BUTTON ,
#endif
#if defined (SDL_HINT_VIDEO_MAC_FULLSCREEN_SPACES)
SDL_HINT_VIDEO_MAC_FULLSCREEN_SPACES,
#endif
NULL};
fprintf (st, " Currently Active SDL Hints:\n");
for (i=0; hints[i]; i++) {
if (SDL_GetHint (hints[i]))
fprintf (st, " %s = %s\n", hints[i], SDL_GetHint (hints[i]));
}
}
return SCPE_OK;
}
t_stat vid_show (FILE* st, DEVICE *dptr, UNIT* uptr, int32 val, CONST char* desc)
{
return vid_show_video (st, uptr, val, desc);
}
/* This section ensures that the window creation and message loop run on the main thread.
This is specifically required by OSX die to the requirement of the Cocoa graphics system.
The method applied here is derived from the SDL specification.
*/
#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)
{
int stat;
stat = SDL_main (main_argc, main_argv); // Actually calls the main(...) function in scp.c
vid_init = CLOSING; // Will close MLoop
return stat;
}
// THis is the entry point called if the graphicsc system is enable.
int main (int argc, char *argv[])
{
int status;
main_argc = argc;
main_argv = argv;
vid_main_thread_handle = SDL_CreateThread (main_thread , "simh-main", NULL);
sim_os_set_thread_priority (PRIORITY_ABOVE_NORMAL);
while (!MLoop())
SDL_Delay(1); // Transiently de-schedule this thread.
SDL_WaitThread (vid_main_thread_handle, &status);
vid_close();
SDL_Quit();
return status;
}
t_stat vid_close (void){
// Close and destroy the window if open
if (vid_init != RUNNING)
return 0;
if (cursor)
SDL_FreeCursor(cursor);
vid_beep_cleanup();
vid_init = CLOSED;
while (vid_init != STOPPED)
SDL_Delay(100); // Wait for MLoop to complete window close.
// Clean up
cursor = 0;
nostore = 0;
surface = 0;
if (xmev) {
free(xmev);
free(xhev);
free(xkev);
}
return 0;
}
/* Create a window that will report various events on the main thread ... required for OSX. */
/* For the QVSS display, set nostore such that the window will merely be refreshed */
/* Also, if QVSS, pixel data arrives from vax_vc.c already formatted. Using vid_draw instead. */
/* In the event that this function is called with a NULL title, storage mode (no fade) is assumed */
/* vid_init states as follows:
STOPPED. Initial state, no window, no message loop.
WINDOW_OK. Set by vid_open. MLoop will then call vid_create_window on main thread. -> RUNNING
RUNNING. Running state. Window open, message loop active.
CLOSING. Set by exit from main simh thread ... inititate shutdown. Exit from MLoop in main.
CLOSED. Set by vid_close. MLoop will close the window and move to state STOPPED.
At present init_flags may only contain SIM_VID_INPUTCATUED or SIM_OWN_CURSOR.
Comment: Some if the above could have been achieved using the SDL messaging system.
*/
t_stat vid_open (DEVICE *dptr, const char *title, uint32 width, uint32 height, int flags)
{
init_w = width;
init_h = height;
init_flags = flags;
if ((strlen(sim_name) + 7 + (dptr ? strlen (dptr->name) : 0) + (title ? strlen (title) : 0)) < sizeof (vid_title))
sprintf (vid_title, "%s%s%s%s%s", sim_name, dptr ? " - " : "", dptr ? dptr->name : "", title ? " - " : "", title ? title : "");
else
sprintf (vid_title, "%s", sim_name);
if (init_flags & SIM_VID_INPUTCAPTURED)
strcat(vid_title, " Captured input mode: ReleaseKey is Right-Ctrl+Right-Shift");
if (!title)
nostore++;
vid_mono_palette[0] = sim_end ? 0xFF000000 : 0x000000FF; /* Black */
vid_mono_palette[1] = 0xFFFFFFFF; /* White */
vid_beep_setup(100, 660);
xmev=(struct mouse_event *)calloc(1,sizeof(SIM_MOUSE_EVENT));
xhev=(struct mouse_event *)calloc(1,sizeof(SIM_MOUSE_EVENT));
xkev=(struct key_event *)calloc(1,sizeof(SIM_KEY_EVENT));
vid_init = WINDOW_OK; // Flag such that MLoop can call vid_create_window
return 0;
}
/* This function creates the actual window and is called from MLoop on the main thread.
The expcted pixel format is 4x8 bytes. Any other format will result in an error exit.
This may seem rather limiting. However, modern hardware generally provides this format.
In the event that errors are reported, this function wil be updated.
NB. This function creates a renderer which is not used. The display system directly
manipulates the pixels in the surface buffer. This buffer is then written to the screen in Refresh().
*/
t_stat vid_create_window(void)
{
// Create an application window with the following settings:
SDL_Init (SDL_INIT_VIDEO);
window = SDL_CreateWindow(
vid_title, // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
init_w, // width, in pixels
init_h, // height, in pixels
SDL_WINDOW_SHOWN // flags
);
// Check that the window was successfully created
if (window == NULL) {
// In the case that the window could not be made...
printf("Could not create SDL window: %s\n", SDL_GetError());
exit(-1);
}
surface = SDL_GetWindowSurface(window);
/* Check the bitdepth of the surface */
if(surface->format->BitsPerPixel != 32){
fprintf(stderr, "Not an 32-bit SDL surface.\n");
exit(-1);
}
/* Check the byes count of the surface */
if(surface->format->BytesPerPixel != 4){
fprintf(stderr, "Invalid pixel format.\n");
exit(-1);
}
pixels = (unsigned char *)surface->pixels;
surlen = (init_h * surface->pitch);
SDL_UpdateWindowSurface(window);
SDL_CreateThread(Refresh,"Refresh",(void *)NULL);
if (!(init_flags & SIM_VID_INPUTCAPTURED))
if (!(init_flags & SIM_OWN_CURSOR))
SDL_ShowCursor(SDL_DISABLE); /* Make host OS cursor invisible in non-capture mode or */
/* for all other system that use their own cursor (see sim_ws.c:vid_open) */
vid_init = RUNNING; /* Init OK continue to next state */
return SCPE_OK;
}
#if defined(HAVE_LIBPNG)
/*
This function coverts the default ARGB surface pixel format to ABGR (RGBA) which is the
only 32 bit format that is managed by libpng. Not very tidy!
Also, set A to 0xff.
*/
t_stat write_png_file(char *filename) {
int i;
Uint8 *p,*buffer,tm;
png_bytep *row_pointers;
Uint8 *pixels;
FILE *fp = fopen(filename, "wb");
png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
png_infop info = png_create_info_struct(png);
png_init_io(png, fp);
buffer = (Uint8 *)malloc(surlen);
pixels = (Uint8 *)surface->pixels;
memcpy(buffer, pixels, surlen);
for (i=0,p=buffer;i<surlen/4;i++,p+=4) {
tm = p[0];
p[0] = p[2];
p[2] = tm;
p[3] = 0xff;
}
row_pointers = (png_bytep *)malloc(sizeof(png_bytep)*init_h);
for (i = 0; i < surface->h; i++)
row_pointers[i] = (png_bytep)(Uint8 *)buffer + i * surface->pitch;
// Output is 8bit depth, RGBA format.
png_set_IHDR(
png,
info,
init_w, init_h,
8,
PNG_COLOR_TYPE_RGBA,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT
);
png_write_info(png, info);
png_write_image(png, row_pointers);
png_write_end(png, NULL);
free(row_pointers);
fclose(fp);
return 0;
}
#else
t_stat write_png_file(char *filename) {
printf("PNG library not available\n");
return 1; /* Always return an error */
}
#endif
t_stat vid_screenshot (const char *filename)
{
int stat;
char *fullname = NULL;
if (!surface) {
sim_printf ("No video display is active\n");
return SCPE_UDIS | SCPE_NOMESSAGE;
}
fullname = (char *)malloc (strlen(filename) + 5);
if (!filename)
return SCPE_MEM;
if (1) {
#if defined(HAVE_LIBPNG)
if (!match_ext (filename, "bmp")) {
sprintf (fullname, "%s%s", filename, match_ext (filename, "png") ? "" : ".png");
stat = write_png_file(fullname);
}
else {
sprintf (fullname, "%s", filename);
stat = SDL_SaveBMP(surface, fullname);
}
#else
sprintf (fullname, "%s%s", filename, match_ext (filename, "bmp") ? "" : ".bmp");
stat = SDL_SaveBMP(surface, fullname);
#endif /* defined(HAVE_LIBPNG) */
}
if (stat) {
sim_printf ("Error saving screenshot to %s: %s\n", fullname, SDL_GetError());
free (fullname);
return SCPE_IOERR | SCPE_NOMESSAGE;
}
else {
if (!sim_quiet)
sim_printf ("Screenshot saved to %s\n", fullname);
free (fullname);
return SCPE_OK;
}
}
void vid_set_cursor_position (int32 x, int32 y) {
lcurr_x = x;
lcurr_y = y;
}
t_stat vid_set_cursor (t_bool visible, uint32 width, uint32 height, uint8 *data, uint8 *mask, uint32 hot_x, uint32 hot_y)
{
if (!cursor) {
while (vid_init != RUNNING) /* Wait for Window to be created */
SDL_Delay(10);
cursor = SDL_CreateCursor (data, mask, width, height, hot_x, hot_y);
SDL_SetCursor(cursor);
SDL_ShowCursor(SDL_ENABLE); /* Make new cursor visible */
return SCPE_OK;
}
return SCPE_NOFNC;
}
t_stat vid_erase_win()
{
memset(surface->pixels, 0, init_w * init_h * 4);
SDL_UpdateWindowSurface(window); /* Write black surface to the host system window */
return 0;
}
/*
vid_draw. Similar to bitblt (Win.GDI) simply copies a pre-formatted buffer into the current surface.
The surface is then copied to the screen every 20 mS. in Refresh() as below.
*/
void vid_draw (int32 x, int32 y, int32 w, int32 h, uint32 *buf)
{
int32 i;
uint32* pixels;
if (vid_init != RUNNING)
return;
pixels = (uint32 *)surface->pixels;
for (i = 0; i < h; i++)
memcpy (pixels + ((i + y) * init_w) + x, buf + w*i, w*sizeof(*pixels));
return;
}
/*
The function is central to all display system. Firstly, the current surface (effectively a back buffer)
is copied to the screen surface by SDL_UpdateWindowSurface. This function (hopefully) calls a GPU
bitblt process and with modern hardware is extremely fast (1 mS.).
The next section su responsible for gradually fading the image in the window. This is achieved using an RGB time
constant sequence in colmap such that the phosphor fade constant for each color can be different. This loop is also very fast
typically 10-15 mS. on recent X86 family processors clocked at >1.5 GHz. The final part of the loop ensure that the
entire cycle takes almost 20 mS. with due allowance for scheduling etc. In this case, the fade constants can be preset and
no matter what is on screen, the fading will be constant.
As the fade constants are set by color, some interesting effects can be obtained. See display.c for examples where the pixels
can start of as blue/white and then have a yellow fade effect. Eg P40 used in the VS60 system.
Howvwer, for some displays eg QVSS, a fade is not required so this function is disabled by nostore. In this case, the system
emeulates a standard RGB bitmap display in framebuffer mode.
The nostore function can also be used for a storage scope display eg the Tektronix 611 XY display. (See pdp8_vc8.c).
*/
static int Refresh(void *info) {
int i,j;
unsigned char *p;
double *d;
while (window && vid_init != STOPPED) {
told=sim_os_msec();
if (vid_init == RUNNING) { // If halted ... freeze display and, display valid
SDL_UpdateWindowSurface(window); // Write the surface to the host system window
if (!nostore && sim_is_running) // Only decay the pixels in store mode and if the simulator is running
for (i = 0,p=(unsigned char *)pixels;i < surlen/4; i++, p++)
for (j = 0,d = colmap;j < 3;j++, p++, d++)
if (*p)
*p = (unsigned char)(*p * (*d) - 1); // Decay none zero pixels only
}
tnew = sim_os_msec();
tvl = 20 - tnew + told; // Calculate delay required for a constant update time of 20mSec.
if (tvl < 0) // System not fast enough so just continue with no delay.
tvl = 0;
SDL_Delay(tvl);
}
return 0; // The window has been closed by vid_close ... exit thread
}
/* The Mouse system.
The following function is central to the mouse implementation for the QVSS (VCB01) Q-bus card.
The intention of this function is to provide seamless enrty and exit of the mouse pointer
from within the host window system. The first 6 lines of code relate to window initialisation.
This could equally have been achived using messages but a state machine is more convenient.
The next 6 lines constitute a system that generates the required relative motion data so that the
QVSS mouse location generated by the OS ruunning on the simh client eg VMS is matched to the
location of the (actaully invisible) cursor managed by the host OS. The is achived by calculating
the X and Y pointing errors and generating a sequence of relative movements until the location of
the client cursor matches that of the host. The conditional expression reduces the 'gain' of this
servo loop for relative errors > 8. This is required because the client OS response to a given
relative move is non-linear due to a degree of mouse acceleration implemented by the client. THis
non linear response is implemented in both DEC Windows under VMS and Ultrix.
The overall efect of this process is that when the host cursor enter the QVSS window, the client
OS is sent a number of move messages to move the client cursor to the same location as the
now invisible host cursor.
This interface is also used by all systems with a light pen (VT11/VT60/PDP1/TX-0).
Similarly, the keyboard data is made available to all systems (QVSS only at present).
*/
static void vid_close_window(void)
{
stop_cpu = 1; // Stop simulation
vid_init = STOPPED; // Reset vid_init state
SDL_DestroyWindow(window); // Close this window and renderer.
window = 0; // Invalidate handle
}
static int MLoop() {
SDL_Event event;
int rel_x,rel_y;
switch (vid_init) {
case STOPPED:
return 0; // Wait until window has been initialised
case WINDOW_OK:
vid_create_window(); // Create window and begin receiving events
break;
case RUNNING: // No action
break;
case CLOSING:
return -1; // Exit message loop and start shutdown
case CLOSED:
vid_close_window(); // Close window continue receiving events until SDL_Quit
break;
}
rel_x = xmev->x_pos - lcurr_x;
rel_y = xmev->y_pos - lcurr_y;
rel_x = (abs(rel_x) > 9)?rel_x/2:rel_x;
rel_y = (abs(rel_y) > 9)?rel_y/2:rel_y;
xmev->x_rel = rel_x;
xmev->y_rel = rel_y;
if (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
return 0;
case SDL_MOUSEMOTION:
xmev->x_pos = event.motion.x-iwd;
xmev->y_pos = event.motion.y-iht;
break;
case SDL_MOUSEBUTTONDOWN:
xmev->b1_state = (event.button.button==SDL_BUTTON_LEFT);
xmev->b2_state = (event.button.button==SDL_BUTTON_RIGHT);
xmev->b3_state = (event.button.button==SDL_BUTTON_MIDDLE);
xmev->x_pos = event.button.x-iwd;
xmev->y_pos = event.button.y-iht;
break;
case SDL_MOUSEBUTTONUP:
if (event.button.button == SDL_BUTTON_LEFT) xmev->b1_state = 0;
if (event.button.button == SDL_BUTTON_RIGHT) xmev->b2_state = 0;
if (event.button.button == SDL_BUTTON_MIDDLE) xmev->b3_state = 0;
xmev->x_pos = event.button.x - iwd;
xmev->y_pos = event.button.y - iht;
break;
case SDL_KEYDOWN:
xkev->key = event.key.keysym.sym;
xkev->state = SIM_KEYPRESS_DOWN;
xkev->mod = event.key.keysym.mod;
break;
case SDL_KEYUP:
xkev->key = event.key.keysym.sym;
xkev->state = SIM_KEYPRESS_UP;
xkev->mod = event.key.keysym.mod;
break;
case SDL_WINDOWEVENT:
if (event.window.event == SDL_WINDOWEVENT_CLOSE)
stop_cpu = 1; // Stop simulation
break;
case SDL_USEREVENT:
if (event.window.event == EVENT_BEEP)
vid_beep_event();
}
}
return 0;
}
/* Only return SCPE_OK if there is a change in the mouse or keyboard data */
int vid_poll_mouse (SIM_MOUSE_EVENT *mev)
{
if (!xmev)
return SCPE_EOF;
memcpy(mev,xmev,sizeof(SIM_MOUSE_EVENT));
if ( !memcmp(xhev,xmev,sizeof(SIM_MOUSE_EVENT)) && !(mev->x_rel | xmev->y_rel) )
return SCPE_EOF; /* Only report changes */
memcpy(xhev,xmev,sizeof(SIM_MOUSE_EVENT)); /* Keep local copy */
return SCPE_OK;
}
t_stat vid_poll_kb (SIM_KEY_EVENT *ev) {
if (!xkev)
return SCPE_EOF;
if (xkev->state == lstst && lstcd == xkev->key)
return SCPE_EOF; /* Single events only */
memcpy(ev,xkev,sizeof(SIM_KEY_EVENT));
lstst = xkev->state;
lstcd = xkev->key;
return SCPE_OK;
}
/*
This function draws a pixel on the current surface if the graphics system is in state 2.
The surface is written to the screen (blit) in Refresh() above. ALL display systems use this
function. At present, level and color are ignored.
*/
t_stat vid_setpixel(int ix,int iy,int level,int color) {
Uint32 *p;
if (vid_init == RUNNING) {
p=(Uint32 *)(pixels + (iy * surface->pitch) + (ix * sizeof(Uint32)));
*p = pxval;
}
return SCPE_OK;
}
t_stat vid_lock_cursor()
{
SDL_ShowCursor(SDL_DISABLE); /* Make host OS cursor invisible in non-capture/default mode */
SDL_SetWindowGrab(window, SDL_TRUE); /* Lock mouse to this window */
#ifdef __APPLE__
SDL_SetRelativeMouseMode(SDL_TRUE);
#endif
return SCPE_OK;
}
t_stat vid_unlock_cursor()
{
if (SDL_GetWindowGrab(window) == SDL_TRUE) {
SDL_SetWindowGrab(window, SDL_FALSE); /* Unlock mouse for this window */
SDL_SetRelativeMouseMode(SDL_FALSE);
SDL_ShowCursor(SDL_ENABLE); /* Make host OS cursor visible in non-capture/default mode */
SDL_WarpMouseInWindow(window, xmev->x_pos + 1, xmev->y_pos ); /* This is required to redraw the host cursor */
return 1;
}
return 0;
}
const char *vid_version(void)
{
static char SDLVersion[80];
SDL_version compiled, running;
SDL_GetVersion(&running);
SDL_VERSION(&compiled);
if ((compiled.major == running.major) &&
(compiled.minor == running.minor) &&
(compiled.patch == running.patch))
sprintf(SDLVersion, "SDL Version %d.%d.%d",
compiled.major, compiled.minor, compiled.patch);
else
sprintf(SDLVersion, "SDL Version (Compiled: %d.%d.%d, Runtime: %d.%d.%d)",
compiled.major, compiled.minor, compiled.patch,
running.major, running.minor, running.patch);
return (const char *)SDLVersion;
}
/* Original beep code from Matt Burke's code */
#include <SDL_audio.h>
#include <math.h>
const int AMPLITUDE = 20000;
const int SAMPLE_FREQUENCY = 11025;
static int16 *vid_beep_data;
static int vid_beep_offset;
static int vid_beep_duration;
static int vid_beep_samples;
static void vid_audio_callback(void *ctx, Uint8 *stream, int length)
{
int16 *data = (int16 *)stream;
int i, sum, remnant = ((vid_beep_samples - vid_beep_offset) * sizeof (*vid_beep_data));
if (length > remnant) {
memset (stream + remnant, 0, length - remnant);
length = remnant;
if (remnant == 0) {
SDL_PauseAudio(1);
return;
}
}
memcpy (stream, &vid_beep_data[vid_beep_offset], length);
for (i=sum=0; i<length; i++)
sum += stream[i];
vid_beep_offset += length / sizeof(*vid_beep_data);
}
static void vid_beep_setup (int duration_ms, int tone_frequency)
{
if (!vid_beep_data) {
int i;
SDL_AudioSpec desiredSpec;
memset (&desiredSpec, 0, sizeof(desiredSpec));
desiredSpec.freq = SAMPLE_FREQUENCY;
desiredSpec.format = AUDIO_S16SYS;
desiredSpec.channels = 1;
desiredSpec.samples = 2048;
desiredSpec.callback = vid_audio_callback;
SDL_OpenAudio(&desiredSpec, NULL);
vid_beep_samples = (int)((SAMPLE_FREQUENCY * duration_ms) / 1000.0);
vid_beep_duration = duration_ms;
vid_beep_data = (int16 *)malloc (sizeof(*vid_beep_data) * vid_beep_samples);
for (i=0; i<vid_beep_samples; i++)
vid_beep_data[i] = (int16)(AMPLITUDE * sin(((double)(i * M_PI * tone_frequency)) / SAMPLE_FREQUENCY));
}
}
static void vid_beep_cleanup (void)
{
SDL_CloseAudio();
free (vid_beep_data);
vid_beep_data = NULL;
}
void vid_beep_event (void)
{
vid_beep_offset = 0; /* reset to beginning of sample set */
SDL_PauseAudio (0); /* Play sound */
}
void vid_beep (void)
{
SDL_Event user_event;
user_event.type = SDL_USEREVENT;
user_event.user.code = EVENT_BEEP;
user_event.user.data1 = NULL;
user_event.user.data2 = NULL;
#if defined (SDL_MAIN_AVAILABLE)
while (SDL_PushEvent (&user_event) < 0)
sim_os_ms_sleep (10);
#else
vid_beep_event ();
#endif
// SDL_Delay (vid_beep_duration + 100);/* Wait for sound to finnish */
}
/*
Translate SDL Keyboard codes to SIMH internal codes for use by any simulator
requiring keyboard data from a window.
*/
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: