Repository navigation
Expand file tree
/
Copy pathvideo_sdl.cpp
More file actions
2738 lines (2377 loc) · 69.3 KB
/
Copy pathvideo_sdl.cpp
File metadata and controls
2738 lines (2377 loc) · 69.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
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
/*
* video_sdl.cpp - Video/graphics emulation, SDL specific stuff
*
* Basilisk II (C) 1997-2008 Christian Bauer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* NOTES:
* The Ctrl key works like a qualifier for special actions:
* Ctrl-Tab = suspend DGA mode (TODO)
* Ctrl-Esc = emergency quit
* Ctrl-F1 = mount floppy
* Ctrl-F5 = grab mouse (in windowed mode)
*
* FIXMEs and TODOs:
* - Windows requires an extra mouse event to update the actual cursor image?
* - Ctr-Tab for suspend/resume but how? SDL does not support that for non-Linux
* - Ctrl-Fn doesn't generate SDL_KEYDOWN events (SDL bug?)
* - Mouse acceleration, there is no API in SDL yet for that
* - Force relative mode in Grab mode even if SDL provides absolute coordinates?
* - Gamma tables support is likely to be broken here
* - Events processing is bound to the general emulation thread as SDL requires
* to PumpEvents() within the same thread as the one that called SetVideoMode().
* Besides, there can't seem to be a way to call SetVideoMode() from a child thread.
* - Backport hw cursor acceleration to Basilisk II?
* - Factor out code
*/
#include "sysdeps.h"
#include <SDL.h>
#include <SDL_mutex.h>
#include <SDL_thread.h>
#include <errno.h>
#include <vector>
#ifdef WIN32
#include <malloc.h> /* alloca() */
#endif
#include "cpu_emulation.h"
#include "main.h"
#include "adb.h"
#include "macos_util.h"
#include "prefs.h"
#include "user_strings.h"
#include "video.h"
#include "video_defs.h"
#include "video_blit.h"
#include "vm_alloc.h"
#define DEBUG 1
#include "debug.h"
#define REUSE_VIDEO_BUFFER 1
#define BROWSER_VIDEO 1
#ifdef EMSCRIPTEN
#include <emscripten.h>
#endif
// Supported video modes
using std::vector;
static vector<VIDEO_MODE> VideoModes;
// Display types
#ifdef SHEEPSHAVER
enum {
DISPLAY_WINDOW = DIS_WINDOW, // windowed display
DISPLAY_SCREEN = DIS_SCREEN // fullscreen display
};
extern int display_type; // See enum above
#else
enum {
DISPLAY_WINDOW, // windowed display
DISPLAY_SCREEN // fullscreen display
};
static int display_type = DISPLAY_WINDOW; // See enum above
#endif
// Constants
#ifdef WIN32
const char KEYCODE_FILE_NAME[] = "BasiliskII_keycodes";
#else
const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes";
#endif
// Global variables
static int32 frame_skip; // Prefs items
static int16 mouse_wheel_mode;
static int16 mouse_wheel_lines;
static uint8 *the_buffer = NULL; // Mac frame buffer (where MacOS draws into)
static uint8 *the_buffer_copy = NULL; // Copy of Mac frame buffer (for refreshed modes)
static uint32 the_buffer_size; // Size of allocated the_buffer
static bool redraw_thread_active = false; // Flag: Redraw thread installed
#ifndef USE_CPU_EMUL_SERVICES
static volatile bool redraw_thread_cancel; // Flag: Cancel Redraw thread
static SDL_Thread *redraw_thread = NULL; // Redraw thread
#ifdef SHEEPSHAVER
static volatile bool thread_stop_req = false;
static volatile bool thread_stop_ack = false; // Acknowledge for thread_stop_req
#endif
#endif
#ifdef ENABLE_VOSF
static bool use_vosf = false; // Flag: VOSF enabled
#else
static const bool use_vosf = false; // VOSF not possible
#endif
static bool ctrl_down = false; // Flag: Ctrl key pressed
static bool caps_on = false; // Flag: Caps Lock on
static bool quit_full_screen = false; // Flag: DGA close requested from redraw thread
static bool emerg_quit = false; // Flag: Ctrl-Esc pressed, emergency quit requested from MacOS thread
static bool emul_suspended = false; // Flag: Emulator suspended
static bool classic_mode = false; // Flag: Classic Mac video mode
static bool use_keycodes = false; // Flag: Use keycodes rather than keysyms
static int keycode_table[256]; // X keycode -> Mac keycode translation table
// SDL variables
static int screen_depth; // Depth of current screen
static SDL_Cursor *sdl_cursor; // Copy of Mac cursor
static SDL_Color sdl_palette[256]; // Color palette to be used as CLUT and gamma table
static bool sdl_palette_changed = false; // Flag: Palette changed, redraw thread must set new colors
static const int sdl_eventmask = SDL_MOUSEEVENTMASK | SDL_KEYEVENTMASK | SDL_VIDEOEXPOSEMASK | SDL_QUITMASK | SDL_ACTIVEEVENTMASK;
// Mutex to protect SDL events
static SDL_mutex *sdl_events_lock = NULL;
#define LOCK_EVENTS SDL_LockMutex(sdl_events_lock)
#define UNLOCK_EVENTS SDL_UnlockMutex(sdl_events_lock)
// Mutex to protect palette
static SDL_mutex *sdl_palette_lock = NULL;
#define LOCK_PALETTE SDL_LockMutex(sdl_palette_lock)
#define UNLOCK_PALETTE SDL_UnlockMutex(sdl_palette_lock)
// Mutex to protect frame buffer
static SDL_mutex *frame_buffer_lock = NULL;
#define LOCK_FRAME_BUFFER SDL_LockMutex(frame_buffer_lock)
#define UNLOCK_FRAME_BUFFER SDL_UnlockMutex(frame_buffer_lock)
// Video refresh function
static void VideoRefreshInit(void);
static void (*video_refresh)(void);
// Prototypes
static int redraw_func(void *arg);
// From sys_unix.cpp
extern void SysMountFirstFloppy(void);
/*
* SDL surface locking glue
*/
#ifdef ENABLE_VOSF
#define SDL_VIDEO_LOCK_VOSF_SURFACE(SURFACE) do { \
if ((SURFACE)->flags & (SDL_HWSURFACE | SDL_FULLSCREEN)) \
the_host_buffer = (uint8 *)(SURFACE)->pixels; \
} while (0)
#else
#define SDL_VIDEO_LOCK_VOSF_SURFACE(SURFACE)
#endif
#define SDL_VIDEO_LOCK_SURFACE(SURFACE) do { \
if (SDL_MUSTLOCK(SURFACE)) { \
SDL_LockSurface(SURFACE); \
SDL_VIDEO_LOCK_VOSF_SURFACE(SURFACE); \
} \
} while (0)
#define SDL_VIDEO_UNLOCK_SURFACE(SURFACE) do { \
if (SDL_MUSTLOCK(SURFACE)) \
SDL_UnlockSurface(SURFACE); \
} while (0)
/*
* Framebuffer allocation routines
*/
static void *vm_acquire_framebuffer(uint32 size)
{
// always try to reallocate framebuffer at the same address
static void *fb = VM_MAP_FAILED;
if (fb != VM_MAP_FAILED) {
if (vm_acquire_fixed(fb, size) < 0) {
#ifndef SHEEPSHAVER
printf("FATAL: Could not reallocate framebuffer at previous address\n");
#endif
fb = VM_MAP_FAILED;
}
}
if (fb == VM_MAP_FAILED)
fb = vm_acquire(size, VM_MAP_DEFAULT | VM_MAP_32BIT);
return fb;
}
static inline void vm_release_framebuffer(void *fb, uint32 size)
{
vm_release(fb, size);
}
/*
* Windows message handler
*/
#ifdef WIN32
#include <dbt.h>
static WNDPROC sdl_window_proc = NULL; // Window proc used by SDL
extern void SysMediaArrived(void);
extern void SysMediaRemoved(void);
extern HWND GetMainWindowHandle(void);
static LRESULT CALLBACK windows_message_handler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg) {
case WM_DEVICECHANGE:
if (wParam == DBT_DEVICEREMOVECOMPLETE) {
DEV_BROADCAST_HDR *p = (DEV_BROADCAST_HDR *)lParam;
if (p->dbch_devicetype == DBT_DEVTYP_VOLUME)
SysMediaRemoved();
}
else if (wParam == DBT_DEVICEARRIVAL) {
DEV_BROADCAST_HDR *p = (DEV_BROADCAST_HDR *)lParam;
if (p->dbch_devicetype == DBT_DEVTYP_VOLUME)
SysMediaArrived();
}
return 0;
default:
if (sdl_window_proc)
return CallWindowProc(sdl_window_proc, hwnd, msg, wParam, lParam);
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
#endif
/*
* SheepShaver glue
*/
#ifdef SHEEPSHAVER
// Color depth modes type
typedef int video_depth;
// 1, 2, 4 and 8 bit depths use a color palette
static inline bool IsDirectMode(VIDEO_MODE const & mode)
{
return IsDirectMode(mode.viAppleMode);
}
// Abstract base class representing one (possibly virtual) monitor
// ("monitor" = rectangular display with a contiguous frame buffer)
class monitor_desc {
public:
monitor_desc(const vector<VIDEO_MODE> &available_modes, video_depth default_depth, uint32 default_id) {}
virtual ~monitor_desc() {}
// Get current Mac frame buffer base address
uint32 get_mac_frame_base(void) const {return screen_base;}
// Set Mac frame buffer base address (called from switch_to_mode())
void set_mac_frame_base(uint32 base) {screen_base = base;}
// Get current video mode
const VIDEO_MODE &get_current_mode(void) const {return VModes[cur_mode];}
// Called by the video driver to switch the video mode on this display
// (must call set_mac_frame_base())
virtual void switch_to_current_mode(void) = 0;
// Called by the video driver to set the color palette (in indexed modes)
// or the gamma table (in direct modes)
virtual void set_palette(uint8 *pal, int num) = 0;
};
// Vector of pointers to available monitor descriptions, filled by VideoInit()
static vector<monitor_desc *> VideoMonitors;
// Find Apple mode matching best specified dimensions
static int find_apple_resolution(int xsize, int ysize)
{
if (xsize == 640 && ysize == 480)
return APPLE_640x480;
if (xsize == 800 && ysize == 600)
return APPLE_800x600;
if (xsize == 1024 && ysize == 768)
return APPLE_1024x768;
if (xsize == 1152 && ysize == 768)
return APPLE_1152x768;
if (xsize == 1152 && ysize == 900)
return APPLE_1152x900;
if (xsize == 1280 && ysize == 1024)
return APPLE_1280x1024;
if (xsize == 1600 && ysize == 1200)
return APPLE_1600x1200;
return APPLE_CUSTOM;
}
// Display error alert
static void ErrorAlert(int error)
{
ErrorAlert(GetString(error));
}
// Display warning alert
static void WarningAlert(int warning)
{
WarningAlert(GetString(warning));
}
#endif
/*
* monitor_desc subclass for SDL display
*/
class SDL_monitor_desc : public monitor_desc {
public:
SDL_monitor_desc(const vector<VIDEO_MODE> &available_modes, video_depth default_depth, uint32 default_id) : monitor_desc(available_modes, default_depth, default_id) {}
~SDL_monitor_desc() {}
virtual void switch_to_current_mode(void);
virtual void set_palette(uint8 *pal, int num);
bool video_open(void);
void video_close(void);
};
/*
* Utility functions
*/
// Find palette size for given color depth
static int palette_size(int mode)
{
switch (mode) {
case VIDEO_DEPTH_1BIT: return 2;
case VIDEO_DEPTH_2BIT: return 4;
case VIDEO_DEPTH_4BIT: return 16;
case VIDEO_DEPTH_8BIT: return 256;
case VIDEO_DEPTH_16BIT: return 32;
case VIDEO_DEPTH_32BIT: return 256;
default: return 0;
}
}
// Return bytes per pixel for requested depth
static inline int bytes_per_pixel(int depth)
{
int bpp;
switch (depth) {
case 8:
bpp = 1;
break;
case 15: case 16:
bpp = 2;
break;
case 24: case 32:
bpp = 4;
break;
default:
abort();
}
return bpp;
}
// Map video_mode depth ID to numerical depth value
static int mac_depth_of_video_depth(int video_depth)
{
int depth = -1;
switch (video_depth) {
case VIDEO_DEPTH_1BIT:
depth = 1;
break;
case VIDEO_DEPTH_2BIT:
depth = 2;
break;
case VIDEO_DEPTH_4BIT:
depth = 4;
break;
case VIDEO_DEPTH_8BIT:
depth = 8;
break;
case VIDEO_DEPTH_16BIT:
depth = 16;
break;
case VIDEO_DEPTH_32BIT:
depth = 32;
break;
default:
abort();
}
return depth;
}
// Map video_mode depth ID to SDL screen depth
static int sdl_depth_of_video_depth(int video_depth)
{
return (video_depth <= VIDEO_DEPTH_8BIT) ? 8 : mac_depth_of_video_depth(video_depth);
}
// Get screen dimensions
static void sdl_display_dimensions(int &width, int &height)
{
static int max_width, max_height;
if (max_width == 0 && max_height == 0) {
max_width = 640 ; max_height = 480;
SDL_Rect **modes = SDL_ListModes(NULL, SDL_FULLSCREEN | SDL_HWSURFACE);
if (modes && modes != (SDL_Rect **)-1) {
// It turns out that on some implementations, and contrary to the documentation,
// the returned list is not sorted from largest to smallest (e.g. Windows)
for (int i = 0; modes[i] != NULL; i++) {
const int w = modes[i]->w;
const int h = modes[i]->h;
if (w > max_width && h > max_height) {
max_width = w;
max_height = h;
}
}
}
}
width = max_width;
height = max_height;
}
static inline int sdl_display_width(void)
{
#ifdef EMSCRIPTEN
return 800;
#endif
int width, height;
sdl_display_dimensions(width, height);
return width;
}
static inline int sdl_display_height(void)
{
#ifdef EMSCRIPTEN
return 600;
#endif
int width, height;
sdl_display_dimensions(width, height);
return height;
}
// Check wether specified mode is available
static bool has_mode(int type, int width, int height, int depth)
{
#ifdef SHEEPSHAVER
// Filter out Classic resolutions
if (width == 512 && height == 384)
return false;
#endif
// Filter out out-of-bounds resolutions
if (width > sdl_display_width() || height > sdl_display_height())
return false;
// Rely on SDL capabilities
return SDL_VideoModeOK(width, height,
sdl_depth_of_video_depth(depth),
SDL_HWSURFACE | (type == DISPLAY_SCREEN ? SDL_FULLSCREEN : 0));
}
// Add mode to list of supported modes
static void add_mode(int type, int width, int height, int resolution_id, int bytes_per_row, int depth)
{
// Filter out unsupported modes
if (!has_mode(type, width, height, depth))
return;
// Fill in VideoMode entry
VIDEO_MODE mode;
#ifdef SHEEPSHAVER
resolution_id = find_apple_resolution(width, height);
mode.viType = type;
#endif
VIDEO_MODE_X = width;
VIDEO_MODE_Y = height;
VIDEO_MODE_RESOLUTION = resolution_id;
VIDEO_MODE_ROW_BYTES = bytes_per_row;
VIDEO_MODE_DEPTH = (video_depth)depth;
VideoModes.push_back(mode);
}
// Set Mac frame layout and base address (uses the_buffer/MacFrameBaseMac)
static void set_mac_frame_buffer(SDL_monitor_desc &monitor, int depth, bool native_byte_order)
{
#if !REAL_ADDRESSING && !DIRECT_ADDRESSING
int layout = FLAYOUT_DIRECT;
if (depth == VIDEO_DEPTH_16BIT)
layout = (screen_depth == 15) ? FLAYOUT_HOST_555 : FLAYOUT_HOST_565;
else if (depth == VIDEO_DEPTH_32BIT)
layout = (screen_depth == 24) ? FLAYOUT_HOST_888 : FLAYOUT_DIRECT;
if (native_byte_order)
MacFrameLayout = layout;
else
MacFrameLayout = FLAYOUT_DIRECT;
monitor.set_mac_frame_base(MacFrameBaseMac);
// Set variables used by UAE memory banking
const VIDEO_MODE &mode = monitor.get_current_mode();
MacFrameBaseHost = the_buffer;
MacFrameSize = VIDEO_MODE_ROW_BYTES * VIDEO_MODE_Y;
InitFrameBufferMapping();
#else
monitor.set_mac_frame_base(Host2MacAddr(the_buffer));
#endif
D(bug("monitor.mac_frame_base = %08x\n", monitor.get_mac_frame_base()));
}
// Set window name and class
static void set_window_name(int name)
{
const SDL_VideoInfo *vi = SDL_GetVideoInfo();
if (vi && vi->wm_available) {
const char *str = GetString(name);
SDL_WM_SetCaption(str, str);
}
}
// Set mouse grab mode
static SDL_GrabMode set_grab_mode(SDL_GrabMode mode)
{
const SDL_VideoInfo *vi = SDL_GetVideoInfo();
return (vi && vi->wm_available ? SDL_WM_GrabInput(mode) : SDL_GRAB_OFF);
}
// Migrate preferences items (XXX to be handled in MigratePrefs())
static void migrate_screen_prefs(void)
{
#ifdef SHEEPSHAVER
// Look-up priorities are: "screen", "screenmodes", "windowmodes".
if (PrefsFindString("screen"))
return;
uint32 window_modes = PrefsFindInt32("windowmodes");
uint32 screen_modes = PrefsFindInt32("screenmodes");
int width = 0, height = 0;
if (screen_modes) {
static const struct {
int id;
int width;
int height;
}
modes[] = {
{ 1, 640, 480 },
{ 2, 800, 600 },
{ 4, 1024, 768 },
{ 64, 1152, 768 },
{ 8, 1152, 900 },
{ 16, 1280, 1024 },
{ 32, 1600, 1200 },
{ 0, }
};
for (int i = 0; modes[i].id != 0; i++) {
if (screen_modes & modes[i].id) {
if (width < modes[i].width && height < modes[i].height) {
width = modes[i].width;
height = modes[i].height;
}
}
}
} else {
if (window_modes & 1)
width = 640, height = 480;
if (window_modes & 2)
width = 800, height = 600;
}
if (width && height) {
char str[32];
sprintf(str, "%s/%d/%d", screen_modes ? "dga" : "win", width, height);
PrefsReplaceString("screen", str);
}
#endif
}
// Map RGB color to pixel value (this only works in TrueColor/DirectColor visuals)
static inline uint32 map_rgb(uint8 red, uint8 green, uint8 blue, bool fix_byte_order = false)
{
uint32 val = (red&0xff)|(green&0xff)<<8|(blue&0xff)<<16|0xff000000;
return val;
// uint32 val = ((red >> 8) << 0) | ((green >> 8) << 0) | ((blue >> 8) << 0);
if (fix_byte_order) {
// We have to fix byte order in the ExpandMap[]
// NOTE: this is only an optimization since Screen_blitter_init()
// could be arranged to choose an NBO or OBO (with
// byteswapping) Blit_Expand_X_To_Y() function
switch (32) {
case 15: case 16:
val = do_byteswap_16(val);
break;
case 24: case 32:
val = do_byteswap_32(val);
break;
}
}
return val;
}
__attribute__((noinline))
void free_browser_pixels(uint8 *browser_pixels) {
free(browser_pixels);
}
/*
* Display "driver" classes
*/
class driver_base {
public:
driver_base(SDL_monitor_desc &m);
virtual ~driver_base();
virtual void update_palette(void);
virtual void suspend(void) {}
virtual void resume(void) {}
virtual void toggle_mouse_grab(void) {}
virtual void mouse_moved(int x, int y) { ADBMouseMoved(x, y); }
void disable_mouse_accel(void);
void restore_mouse_accel(void);
virtual void grab_mouse(void) {}
virtual void ungrab_mouse(void) {}
public:
SDL_monitor_desc &monitor; // Associated video monitor
const VIDEO_MODE &mode; // Video mode handled by the driver
bool init_ok; // Initialization succeeded (we can't use exceptions because of -fomit-frame-pointer)
SDL_Surface *s; // The surface we draw into
// uint8 *browser_pixels;
};
class driver_window;
#ifdef ENABLE_VOSF
static void update_display_window_vosf(driver_window *drv);
#endif
static void update_display_static(driver_base *drv);
class driver_window : public driver_base {
#ifdef ENABLE_VOSF
friend void update_display_window_vosf(driver_window *drv);
#endif
friend void update_display_static(driver_base *drv);
public:
driver_window(SDL_monitor_desc &monitor);
~driver_window();
void toggle_mouse_grab(void);
void mouse_moved(int x, int y);
void grab_mouse(void);
void ungrab_mouse(void);
private:
bool mouse_grabbed; // Flag: mouse pointer grabbed, using relative mouse mode
int mouse_last_x, mouse_last_y; // Last mouse position (for relative mode)
};
class driver_fullscreen : public driver_base {
public:
driver_fullscreen(SDL_monitor_desc &monitor);
~driver_fullscreen();
};
static driver_base *drv = NULL; // Pointer to currently used driver object
#ifdef ENABLE_VOSF
# include "video_vosf.h"
#endif
driver_base::driver_base(SDL_monitor_desc &m)
: monitor(m), mode(m.get_current_mode()), init_ok(false), s(NULL)
{
the_buffer = NULL;
the_buffer_copy = NULL;
// browser_pixels = NULL;
}
driver_base::~driver_base()
{
ungrab_mouse();
restore_mouse_accel();
if (s)
SDL_FreeSurface(s);
if (REUSE_VIDEO_BUFFER) {
// printf("~driver_base checking browser_pixels %p != %p \n", (void *)browser_pixels, (void *)NULL);
// assert(browser_pixels != NULL);
// printf("~driver_base freeing %p\n", (void *)browser_pixels);
// #ifdef EMSCRIPTEN
// EM_ASM_({
// Module.debugPointer($0);
// }, browser_pixels);
// #endif
// free_browser_pixels(browser_pixels);
// browser_pixels = NULL;
// assert(browser_pixels == NULL);
// printf("~driver_base freed browser_pixels\n");
}
// the_buffer shall always be mapped through vm_acquire_framebuffer()
if (the_buffer != VM_MAP_FAILED) {
D(bug(" releasing the_buffer at %p (%d bytes)\n", the_buffer, the_buffer_size));
vm_release_framebuffer(the_buffer, the_buffer_size);
the_buffer = NULL;
}
// Free frame buffer(s)
if (!use_vosf) {
if (the_buffer_copy) {
free(the_buffer_copy);
the_buffer_copy = NULL;
}
}
#ifdef ENABLE_VOSF
else {
if (the_host_buffer) {
D(bug(" freeing the_host_buffer at %p\n", the_host_buffer));
free(the_host_buffer);
the_host_buffer = NULL;
}
if (the_buffer_copy) {
D(bug(" freeing the_buffer_copy at %p\n", the_buffer_copy));
free(the_buffer_copy);
the_buffer_copy = NULL;
}
// Deinitialize VOSF
video_vosf_exit();
}
#endif
}
// Palette has changed
void driver_base::update_palette(void)
{
const VIDEO_MODE &mode = monitor.get_current_mode();
#ifndef EMSCRIPTEN
if ((int)VIDEO_MODE_DEPTH <= VIDEO_DEPTH_8BIT)
SDL_SetPalette(s, SDL_PHYSPAL, sdl_palette, 0, 256);
#endif
}
// Disable mouse acceleration
void driver_base::disable_mouse_accel(void)
{
}
// Restore mouse acceleration to original value
void driver_base::restore_mouse_accel(void)
{
}
static uint8 *browser_pixels = NULL;
uint8 *alloc_browser_pixels(int32 size_to_copy) {
if (browser_pixels) {
return browser_pixels;
}
return (uint8 *)malloc(size_to_copy * sizeof(uint8));
}
/*
* Windowed display driver
*/
static bool SDL_display_opened = false;
// Open display
driver_window::driver_window(SDL_monitor_desc &m)
: driver_base(m), mouse_grabbed(false)
{
printf("driver_window::driver_window\n");
int width = VIDEO_MODE_X, height = VIDEO_MODE_Y;
int aligned_height = (height + 15) & ~15;
// Set absolute mouse mode
ADBSetRelMouseMode(mouse_grabbed);
// This is ugly:
// If we're switching resolutions (ie, not setting it for the first time),
// there's a bug in SDL where the SDL_Surface created will not be properly
// setup. The solution is to SDL_QuitSubSystem(SDL_INIT_VIDEO) before calling
// SDL_SetVideoMode for the second time (SDL_SetVideoMode will call SDL_Init()
// and all will be well). Without this, the video becomes corrupted (at least
// on Mac OS X), after the resolution switch.
if (SDL_display_opened)
SDL_QuitSubSystem(SDL_INIT_VIDEO);
// Create surface
int depth = sdl_depth_of_video_depth(VIDEO_MODE_DEPTH);
#ifdef EMSCRIPTEN
depth = 32; // always 32 is easier
#else
if ((s = SDL_SetVideoMode(width, height, depth, SDL_HWSURFACE)) == NULL)
return;
#endif
if (REUSE_VIDEO_BUFFER) {
const int bytes_per_pixel = VIDEO_MODE_ROW_BYTES / VIDEO_MODE_X;
uint32 size_to_copy = VIDEO_MODE_X * bytes_per_pixel * VIDEO_MODE_Y;
// printf("driver_window checking browser_pixels %p != %p \n", (void *)browser_pixels, (void *)NULL);
// assert(browser_pixels == NULL);
browser_pixels = alloc_browser_pixels(size_to_copy);
// assert(browser_pixels);
// printf("driver_window allocated %p\n", (void *)browser_pixels);
#ifdef EMSCRIPTEN
EM_ASM_({
Module.debugPointer($0);
}, browser_pixels);
#endif
}
SDL_display_opened = true;
#ifdef ENABLE_VOSF
printf("using vosf\n");
use_vosf = true;
// Allocate memory for frame buffer (SIZE is extended to page-boundary)
the_host_buffer = (uint8 *)s->pixels;
the_buffer_size = page_extend((aligned_height + 2) * s->pitch);
the_buffer = (uint8 *)vm_acquire_framebuffer(the_buffer_size);
the_buffer_copy = (uint8 *)malloc(the_buffer_size);
D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
// Check whether we can initialize the VOSF subsystem and it's profitable
if (!video_vosf_init(m)) {
WarningAlert(STR_VOSF_INIT_ERR);
use_vosf = false;
}
else if (!video_vosf_profitable()) {
video_vosf_exit();
printf("VOSF acceleration is not profitable on this platform, disabling it\n");
use_vosf = false;
}
if (!use_vosf) {
free(the_buffer_copy);
vm_release(the_buffer, the_buffer_size);
the_host_buffer = NULL;
}
#endif
if (!use_vosf) {
printf("allocating the_buffer, the_buffer_copy\n");
// Allocate memory for frame buffer
the_buffer_size = (aligned_height + 2) * s->pitch;
the_buffer_copy = (uint8 *)calloc(1, the_buffer_size);
#ifdef BROWSER_VIDEO
the_buffer = (uint8 *)malloc(the_buffer_size);
#else
the_buffer = (uint8 *)vm_acquire_framebuffer(the_buffer_size);
#endif
D(bug("the_buffer = %p, the_buffer_copy = %p\n", the_buffer, the_buffer_copy));
}
#ifdef SHEEPSHAVER
// Create cursor
if ((sdl_cursor = SDL_CreateCursor(MacCursor + 4, MacCursor + 36, 16, 16, 0, 0)) != NULL) {
SDL_SetCursor(sdl_cursor);
}
#else
// Hide cursor
SDL_ShowCursor(0);
#endif
// Set window name/class
#ifndef EMSCRIPTEN
set_window_name(STR_WINDOW_TITLE);
#endif
// Init blitting routines
SDL_PixelFormat *f = s->format;
VisualFormat visualFormat;
visualFormat.depth = depth;
visualFormat.Rmask = f->Rmask;
visualFormat.Gmask = f->Gmask;
visualFormat.Bmask = f->Bmask;
Screen_blitter_init(visualFormat, true, mac_depth_of_video_depth(VIDEO_MODE_DEPTH));
// Load gray ramp to 8->16/32 expand map
if (!IsDirectMode(mode)) {
for (int i=0; i<256; i++) {
#ifdef EMSCRIPTEN
ExpandMap[i] = map_rgb(i, i, i, true);
#else
ExpandMap[i] = SDL_MapRGB(f, i, i, i);
#endif
}
}
// Set frame buffer base
set_mac_frame_buffer(monitor, VIDEO_MODE_DEPTH, true);
// Everything went well
init_ok = true;
}
// Close display
driver_window::~driver_window()
{
#ifdef ENABLE_VOSF
if (use_vosf)
the_host_buffer = NULL; // don't free() in driver_base dtor
#endif
#ifndef EMSCRIPTEN
if (s)
SDL_FreeSurface(s);
#endif
}
// Toggle mouse grab
void driver_window::toggle_mouse_grab(void)
{
if (mouse_grabbed)
ungrab_mouse();
else
grab_mouse();
}
// Grab mouse, switch to relative mouse mode
void driver_window::grab_mouse(void)
{
if (!mouse_grabbed) {
SDL_GrabMode new_mode = set_grab_mode(SDL_GRAB_ON);
if (new_mode == SDL_GRAB_ON) {
set_window_name(STR_WINDOW_TITLE_GRABBED);
disable_mouse_accel();
mouse_grabbed = true;
}
}
}
// Ungrab mouse, switch to absolute mouse mode
void driver_window::ungrab_mouse(void)
{
if (mouse_grabbed) {
SDL_GrabMode new_mode = set_grab_mode(SDL_GRAB_OFF);
if (new_mode == SDL_GRAB_OFF) {
set_window_name(STR_WINDOW_TITLE);
restore_mouse_accel();
mouse_grabbed = false;
}
}
}
// Mouse moved
void driver_window::mouse_moved(int x, int y)
{
mouse_last_x = x; mouse_last_y = y;
ADBMouseMoved(x, y);
}
/*
* Full-screen display driver
*/
// Open display
driver_fullscreen::driver_fullscreen(SDL_monitor_desc &m)
: driver_base(m)
{
printf("driver_fullscreen::driver_fullscreen\n");