-
Notifications
You must be signed in to change notification settings - Fork 8
/
libretro_os.cpp
executable file
·1365 lines (1166 loc) · 41.2 KB
/
libretro_os.cpp
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
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#define FORBIDDEN_SYMBOL_ALLOW_ALL
#include <unistd.h>
#include <sys/time.h>
#include <list>
#include <retro_miscellaneous.h>
#include <retro_inline.h>
#include "surface.libretro.h"
#include "backends/base-backend.h"
#include "common/events.h"
#include "common/config-manager.h"
#include "audio/mixer_intern.h"
#if defined(_WIN32)
#include "backends/fs/windows/windows-fs-factory.h"
#define FS_SYSTEM_FACTORY WindowsFilesystemFactory
#else
#include "libretro-fs-factory.h"
#define FS_SYSTEM_FACTORY LibRetroFilesystemFactory
#endif
#include "backends/timer/default/default-timer.h"
#include "graphics/colormasks.h"
#include "graphics/palette.h"
#include "backends/saves/default/default-saves.h"
#if defined(_WIN32)
#include <direct.h>
#ifdef _XBOX
#include <xtl.h>
#else
#include <windows.h>
#endif
#elif defined(__CELLOS_LV2__)
#include <sys/sys_time.h>
#elif (defined(GEKKO) && !defined(WIIU))
#include <ogc/lwp_watchdog.h>
#else
#include <time.h>
#endif
#include "libretro.h"
#include "retro_emu_thread.h"
extern retro_log_printf_t log_cb;
#include "common/mutex.h"
/**
* Dummy mutex implementation
*/
class LibretroMutexInternal final : public Common::MutexInternal {
public:
LibretroMutexInternal() {};
~LibretroMutexInternal() override {};
bool lock() override { return 0; }
bool unlock() override { return 0; };
};
Common::MutexInternal *createLibretroMutexInternal() {
return new LibretroMutexInternal();
}
struct RetroPalette
{
unsigned char _colors[256 * 3];
RetroPalette()
{
memset(_colors, 0, sizeof(_colors));
}
void set(const byte *colors, uint start, uint num)
{
memcpy(_colors + start * 3, colors, num * 3);
}
void get(byte* colors, uint start, uint num) const
{
memcpy(colors, _colors + start * 3, num * 3);
}
unsigned char *getColor(uint aIndex) const
{
return (unsigned char*)&_colors[aIndex * 3];
}
};
static INLINE void blit_uint8_uint16_fast(Graphics::Surface& aOut, const Graphics::Surface& aIn, const RetroPalette& aColors)
{
for(int i = 0; i < aIn.h; i ++)
{
if(i >= aOut.h)
continue;
uint8_t * const in = (uint8_t*)aIn.pixels + (i * aIn.w);
uint16_t* const out = (uint16_t*)aOut.pixels + (i * aOut.w);
for(int j = 0; j < aIn.w; j ++)
{
if (j >= aOut.w)
continue;
uint8 r, g, b;
const uint8_t val = in[j];
//if(val != 0xFFFFFFFF)
{
if(aIn.format.bytesPerPixel == 1)
{
unsigned char *col = aColors.getColor(val);
r = *col++;
g = *col++;
b = *col++;
}
else
aIn.format.colorToRGB(in[j], r, g, b);
out[j] = aOut.format.RGBToColor(r, g, b);
}
}
}
}
static INLINE void blit_uint32_uint16(Graphics::Surface& aOut, const Graphics::Surface& aIn, const RetroPalette& aColors)
{
for(int i = 0; i < aIn.h; i ++)
{
if(i >= aOut.h)
continue;
uint32_t* const in = (uint32_t*)aIn.pixels + (i * aIn.w);
uint16_t* const out = (uint16_t*)aOut.pixels + (i * aOut.w);
for(int j = 0; j < aIn.w; j ++)
{
if(j >= aOut.w)
continue;
uint8 r, g, b;
//const uint32_t val = in[j];
//if(val != 0xFFFFFFFF)
{
aIn.format.colorToRGB(in[j], r, g, b);
out[j] = aOut.format.RGBToColor(r, g, b);
}
}
}
}
static INLINE void blit_uint16_uint16(Graphics::Surface& aOut, const Graphics::Surface& aIn, const RetroPalette& aColors)
{
for(int i = 0; i < aIn.h; i ++)
{
if(i >= aOut.h)
continue;
uint16_t* const in = (uint16_t*)aIn.pixels + (i * aIn.w);
uint16_t* const out = (uint16_t*)aOut.pixels + (i * aOut.w);
for(int j = 0; j < aIn.w; j ++)
{
if(j >= aOut.w)
continue;
uint8 r, g, b;
//const uint16_t val = in[j];
//if(val != 0xFFFFFFFF)
{
aIn.format.colorToRGB(in[j], r, g, b);
out[j] = aOut.format.RGBToColor(r, g, b);
}
}
}
}
static void blit_uint8_uint16(Graphics::Surface& aOut, const Graphics::Surface& aIn, int aX, int aY, const RetroPalette& aColors, uint32 aKeyColor)
{
for(int i = 0; i < aIn.h; i ++)
{
if((i + aY) < 0 || (i + aY) >= aOut.h)
continue;
uint8_t* const in = (uint8_t*)aIn.pixels + (i * aIn.w);
uint16_t* const out = (uint16_t*)aOut.pixels + ((i + aY) * aOut.w);
for(int j = 0; j < aIn.w; j ++)
{
if((j + aX) < 0 || (j + aX) >= aOut.w)
continue;
uint8 r, g, b;
const uint8_t val = in[j];
if(val != aKeyColor)
{
unsigned char *col = aColors.getColor(val);
r = *col++;
g = *col++;
b = *col++;
out[j + aX] = aOut.format.RGBToColor(r, g, b);
}
}
}
}
static void blit_uint16_uint16(Graphics::Surface& aOut, const Graphics::Surface& aIn, int aX, int aY, const RetroPalette& aColors, uint32 aKeyColor)
{
for(int i = 0; i < aIn.h; i ++)
{
if((i + aY) < 0 || (i + aY) >= aOut.h)
continue;
uint16_t* const in = (uint16_t*)aIn.pixels + (i * aIn.w);
uint16_t* const out = (uint16_t*)aOut.pixels + ((i + aY) * aOut.w);
for(int j = 0; j < aIn.w; j ++)
{
if((j + aX) < 0 || (j + aX) >= aOut.w)
continue;
uint8 r, g, b;
const uint16_t val = in[j];
if(val != aKeyColor)
{
aIn.format.colorToRGB(in[j], r, g, b);
out[j + aX] = aOut.format.RGBToColor(r, g, b);
}
}
}
}
static void blit_uint32_uint16(Graphics::Surface& aOut, const Graphics::Surface& aIn, int aX, int aY, const RetroPalette& aColors, uint32 aKeyColor)
{
for(int i = 0; i < aIn.h; i ++)
{
if((i + aY) < 0 || (i + aY) >= aOut.h)
continue;
uint32_t* const in = (uint32_t*)aIn.pixels + (i * aIn.w);
uint16_t* const out = (uint16_t*)aOut.pixels + ((i + aY) * aOut.w);
for(int j = 0; j < aIn.w; j ++)
{
if((j + aX) < 0 || (j + aX) >= aOut.w)
continue;
uint8 in_a, in_r, in_g, in_b;
uint8 out_r, out_g, out_b;
uint32_t blend_r, blend_g, blend_b;
const uint32_t val = in[j];
if(val != aKeyColor)
{
aIn.format.colorToARGB(in[j], in_a, in_r, in_g, in_b);
if(in_a)
{
aOut.format.colorToRGB(out[j + aX], out_r, out_g, out_b);
blend_r = ((in_r * in_a) + (out_r * (255 - in_a))) / 255;
blend_g = ((in_g * in_a) + (out_g * (255 - in_a))) / 255;
blend_b = ((in_b * in_a) + (out_b * (255 - in_a))) / 255;
out[j + aX] = aOut.format.RGBToColor(blend_r, blend_g, blend_b);
}
}
}
}
}
static INLINE void copyRectToSurface(uint8_t *pixels, int out_pitch, const uint8_t *src, int pitch, int x, int y, int w, int h, int out_bpp)
{
uint8_t *dst = pixels + y * out_pitch + x * out_bpp;
do
{
memcpy(dst, src, w * out_bpp);
src += pitch;
dst += out_pitch;
}while(--h);
}
static Common::String s_systemDir;
static Common::String s_saveDir;
#ifdef FRONTEND_SUPPORTS_RGB565
#define SURF_BPP 2
#define SURF_RBITS 2
#define SURF_GBITS 5
#define SURF_BBITS 6
#define SURF_ABITS 5
#define SURF_ALOSS (8-SURF_ABITS)
#define SURF_RLOSS (8-SURF_RBITS)
#define SURF_GLOSS (8-SURF_GBITS)
#define SURF_BLOSS (8-SURF_BBITS)
#define SURF_RSHIFT 0
#define SURF_GSHIFT 11
#define SURF_BSHIFT 5
#define SURF_ASHIFT 0
#else
#define SURF_BPP 2
#define SURF_RBITS 5
#define SURF_GBITS 5
#define SURF_BBITS 5
#define SURF_ABITS 1
#define SURF_ALOSS (8-SURF_ABITS)
#define SURF_RLOSS (8-SURF_RBITS)
#define SURF_GLOSS (8-SURF_GBITS)
#define SURF_BLOSS (8-SURF_BBITS)
#define SURF_RSHIFT 10
#define SURF_GSHIFT 5
#define SURF_BSHIFT 0
#define SURF_ASHIFT 15
#endif
std::list<Common::Event> _events;
class OSystem_RETRO : public EventsBaseBackend, public PaletteManager {
public:
Graphics::Surface _screen;
Graphics::Surface _gameScreen;
RetroPalette _gamePalette;
Graphics::Surface _overlay;
bool _overlayVisible;
bool _overlayInGUI;
Graphics::Surface _mouseImage;
RetroPalette _mousePalette;
bool _mousePaletteEnabled;
bool _mouseVisible;
int _mouseX;
int _mouseY;
float _mouseXAcc;
float _mouseYAcc;
int _mouseHotspotX;
int _mouseHotspotY;
int _mouseKeyColor;
bool _mouseDontScale;
bool _mouseButtons[2];
bool _joypadmouseButtons[2];
bool _joypadkeyboardButtons[8];
unsigned _joypadnumpadLast;
bool _joypadnumpadActive;
bool _ptrmouseButton;
uint32 _startTime;
uint32 _threadExitTime;
bool _speed_hack_enabled;
Audio::MixerImpl* _mixer;
OSystem_RETRO(bool aEnableSpeedHack) :
_mousePaletteEnabled(false), _mouseVisible(false),
_mouseX(0), _mouseY(0), _mouseXAcc(0.0), _mouseYAcc(0.0), _mouseHotspotX(0), _mouseHotspotY(0),
_mouseKeyColor(0), _mouseDontScale(false),
_joypadnumpadLast(8), _joypadnumpadActive(false),
_mixer(0), _startTime(0), _threadExitTime(10),
_speed_hack_enabled(aEnableSpeedHack)
{
_fsFactory = new FS_SYSTEM_FACTORY();
memset(_mouseButtons, 0, sizeof(_mouseButtons));
memset(_joypadmouseButtons, 0, sizeof(_joypadmouseButtons));
memset(_joypadkeyboardButtons, 0, sizeof(_joypadkeyboardButtons));
_startTime = getMillis();
if(s_systemDir.empty())
s_systemDir = ".";
if(s_saveDir.empty())
s_saveDir = ".";
}
virtual ~OSystem_RETRO()
{
_gameScreen.free();
_overlay.free();
_mouseImage.free();
_screen.free();
delete _mixer;
}
virtual void initBackend()
{
_savefileManager = new DefaultSaveFileManager(s_saveDir);
#ifdef FRONTEND_SUPPORTS_RGB565
_overlay.create(RES_W_OVERLAY, RES_H_OVERLAY, Graphics::PixelFormat(2, 5, 6, 5, 0, 11, 5, 0, 0));
#else
_overlay.create(RES_W_OVERLAY, RES_H_OVERLAY, Graphics::PixelFormat(2, 5, 5, 5, 1, 10, 5, 0, 15));
#endif
_mixer = new Audio::MixerImpl(48000);
_timerManager = new DefaultTimerManager();
_mixer->setReady(true);
EventsBaseBackend::initBackend();
}
virtual void engineInit(){
Common::String engineId = ConfMan.get("engineid");
if ( engineId.equalsIgnoreCase("scumm") && ConfMan.getBool("original_gui") ){
ConfMan.setBool("original_gui",false);
log_cb(RETRO_LOG_INFO, "\"original_gui\" setting forced to false\n");
}
}
virtual bool hasFeature(Feature f)
{
return (f == OSystem::kFeatureCursorPalette);
}
virtual void setFeatureState(Feature f, bool enable)
{
if (f == kFeatureCursorPalette)
_mousePaletteEnabled = enable;
}
virtual bool getFeatureState(Feature f)
{
return (f == kFeatureCursorPalette) ? _mousePaletteEnabled : false;
}
virtual const GraphicsMode *getSupportedGraphicsModes() const
{
static const OSystem::GraphicsMode s_noGraphicsModes[] = { {0, 0, 0} };
return s_noGraphicsModes;
}
virtual int getDefaultGraphicsMode() const
{
return 0;
}
virtual bool isOverlayVisible() const
{
return false;
}
virtual bool setGraphicsMode(int mode)
{
return true;
}
virtual int getGraphicsMode() const
{
return 0;
}
virtual void initSize(uint width, uint height, const Graphics::PixelFormat *format)
{
_gameScreen.create(width, height, format ? *format : Graphics::PixelFormat::createFormatCLUT8());
}
virtual int16 getHeight()
{
return _gameScreen.h;
}
virtual int16 getWidth()
{
return _gameScreen.w;
}
virtual Graphics::PixelFormat getScreenFormat() const
{
return _gameScreen.format;
}
virtual Common::List<Graphics::PixelFormat> getSupportedFormats() const
{
Common::List<Graphics::PixelFormat> result;
/* RGBA8888 */
result.push_back(Graphics::PixelFormat(4, 8, 8, 8, 8, 24, 16, 8, 0));
#ifdef FRONTEND_SUPPORTS_RGB565
/* RGB565 - overlay */
result.push_back(Graphics::PixelFormat(2, 5, 6, 5, 0, 11, 5, 0, 0));
#endif
/* RGB555 - fmtowns */
result.push_back(Graphics::PixelFormat(2, 5, 5, 5, 1, 10, 5, 0, 15));
/* Palette - most games */
result.push_back(Graphics::PixelFormat::createFormatCLUT8());
return result;
}
virtual PaletteManager *getPaletteManager() { return this; }
protected:
// PaletteManager API
virtual void setPalette(const byte *colors, uint start, uint num)
{
_gamePalette.set(colors, start, num);
}
virtual void grabPalette(byte *colors, uint start, uint num) const
{
_gamePalette.get(colors, start, num);
}
public:
virtual void copyRectToScreen(const void *buf, int pitch, int x, int y, int w, int h)
{
const uint8_t *src = (const uint8_t*)buf;
uint8_t *pix = (uint8_t*)_gameScreen.pixels;
copyRectToSurface(pix, _gameScreen.pitch, src, pitch, x, y, w, h, _gameScreen.format.bytesPerPixel);
}
virtual void updateScreen()
{
const Graphics::Surface& srcSurface = (_overlayInGUI) ? _overlay : _gameScreen;
if(srcSurface.w && srcSurface.h)
{
switch(srcSurface.format.bytesPerPixel)
{
case 1:
case 3:
blit_uint8_uint16_fast(_screen, srcSurface, _gamePalette);
break;
case 2:
blit_uint16_uint16(_screen, srcSurface, _gamePalette);
break;
case 4:
blit_uint32_uint16(_screen, srcSurface, _gamePalette);
break;
}
}
// Draw Mouse
if(_mouseVisible && _mouseImage.w && _mouseImage.h)
{
const int x = _mouseX - _mouseHotspotX;
const int y = _mouseY - _mouseHotspotY;
switch(_mouseImage.format.bytesPerPixel)
{
case 1:
case 3:
blit_uint8_uint16(_screen, _mouseImage, x, y, _mousePaletteEnabled ? _mousePalette : _gamePalette, _mouseKeyColor);
break;
case 2:
blit_uint16_uint16(_screen, _mouseImage, x, y, _mousePaletteEnabled ? _mousePalette : _gamePalette, _mouseKeyColor);
break;
case 4:
blit_uint32_uint16(_screen, _mouseImage, x, y, _mousePaletteEnabled ? _mousePalette : _gamePalette, _mouseKeyColor);
break;
}
}
}
virtual Graphics::Surface *lockScreen()
{
return &_gameScreen;
}
virtual void unlockScreen()
{
/* EMPTY */
}
virtual void setShakePos(int shakeXOffset, int shakeYOffset)
{
// TODO
}
virtual void showOverlay(bool inGUI)
{
_overlayVisible = true;
_overlayInGUI = inGUI;
}
virtual void hideOverlay()
{
_overlayVisible = false;
_overlayInGUI = false;
}
virtual void clearOverlay()
{
_overlay.fillRect(Common::Rect(_overlay.w, _overlay.h), 0);
}
virtual void grabOverlay(Graphics::Surface &surface)
{
const unsigned char *src = (unsigned char*)_overlay.pixels;
unsigned char *dst = (byte *)surface.getPixels();;
unsigned i = RES_H_OVERLAY;
do{
memcpy(dst, src, RES_W_OVERLAY << 1);
dst += surface.pitch;
src += RES_W_OVERLAY << 1;
}while(--i);
}
virtual void copyRectToOverlay(const void *buf, int pitch, int x, int y, int w, int h)
{
const uint8_t *src = (const uint8_t*)buf;
uint8_t *pix = (uint8_t*)_overlay.pixels;
copyRectToSurface(pix, _overlay.pitch, src, pitch, x, y, w, h, _overlay.format.bytesPerPixel);
}
virtual int16 getOverlayHeight()
{
return _overlay.h;
}
virtual int16 getOverlayWidth()
{
return _overlay.w;
}
virtual Graphics::PixelFormat getOverlayFormat() const
{
return _overlay.format;
}
virtual bool showMouse(bool visible)
{
const bool wasVisible = _mouseVisible;
_mouseVisible = visible;
return wasVisible;
}
virtual void warpMouse(int x, int y)
{
_mouseX = x;
_mouseY = y;
}
virtual void setMouseCursor(const void *buf, uint w, uint h, int hotspotX, int hotspotY, uint32 keycolor = 255, bool dontScale = false, const Graphics::PixelFormat *format = NULL)
{
const Graphics::PixelFormat mformat = format ? *format : Graphics::PixelFormat::createFormatCLUT8();
if(_mouseImage.w != w || _mouseImage.h != h || _mouseImage.format != mformat)
{
_mouseImage.create(w, h, mformat);
}
memcpy(_mouseImage.pixels, buf, h * _mouseImage.pitch);
_mouseHotspotX = hotspotX;
_mouseHotspotY = hotspotY;
_mouseKeyColor = keycolor;
_mouseDontScale = dontScale;
}
virtual void setCursorPalette(const byte *colors, uint start, uint num)
{
_mousePalette.set(colors, start, num);
_mousePaletteEnabled = true;
}
void retroCheckThread(uint32 offset = 0)
{
if(_threadExitTime <= (getMillis() + offset))
{
#if defined(USE_LIBCO)
extern void retro_leave_thread();
retro_leave_thread();
#else
retro_switch_thread();
#endif
_threadExitTime = getMillis() + 10;
}
}
virtual bool pollEvent(Common::Event &event)
{
retroCheckThread();
((DefaultTimerManager*)_timerManager)->handler();
if(!_events.empty())
{
event = _events.front();
_events.pop_front();
return true;
}
return false;
}
virtual uint32 getMillis(bool skipRecord = false)
{
#if (defined(GEKKO) && !defined(WIIU))
return (ticks_to_microsecs(gettime()) / 1000.0) - _startTime;
#elif defined(WIIU)
return ((cpu_features_get_time_usec())/1000) - _startTime;
#elif defined(__CELLOS_LV2__)
return (sys_time_get_system_time() / 1000.0) - _startTime;
#else
struct timeval t;
gettimeofday(&t, 0);
return ((t.tv_sec * 1000) + (t.tv_usec / 1000)) - _startTime;
#endif
}
virtual void delayMillis(uint msecs)
{
// Implement 'non-blocking' sleep...
uint32 start_time = getMillis();
if (_speed_hack_enabled)
{
// Use janky inaccurate method...
uint32 elapsed_time = 0;
uint32 time_remaining = msecs;
while(time_remaining > 0)
{
// If delay would take us past the next
// thread exit time, exit the thread immediately
// (i.e. start burning delay time in the main RetroArch
// thread as soon as possible...)
retroCheckThread(time_remaining);
// Check how much delay time remains...
elapsed_time = getMillis() - start_time;
if (time_remaining > elapsed_time)
{
time_remaining = time_remaining - elapsed_time;
usleep(1000);
}
else
{
time_remaining = 0;
}
// Have to handle the timer manager here, since some engines
// (e.g. dreamweb) sit in a delayMillis() loop waiting for a
// timer callback...
((DefaultTimerManager*)_timerManager)->handler();
}
}
else
{
// Use accurate method...
while(getMillis() < start_time + msecs)
{
usleep(1000);
retroCheckThread();
// Have to handle the timer manager here, since some engines
// (e.g. dreamweb) sit in a delayMillis() loop waiting for a
// timer callback...
((DefaultTimerManager*)_timerManager)->handler();
}
}
}
virtual Common::MutexInternal *createMutex(void)
{
return createLibretroMutexInternal();
}
virtual void quit()
{
// TODO:
}
virtual void addSysArchivesToSearchSet(Common::SearchSet &s, int priority = 0)
{
// TODO: NOTHING?
}
virtual void getTimeAndDate(TimeDate &t, bool skipRecord) const
{
time_t curTime = time(NULL);
#define YEAR0 1900
#define EPOCH_YR 1970
#define SECS_DAY (24L * 60L * 60L)
#define LEAPYEAR(year) (!((year) % 4) && (((year) % 100) || !((year) % 400)))
#define YEARSIZE(year) (LEAPYEAR(year) ? 366 : 365)
const int _ytab[2][12] = {
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
int year = EPOCH_YR;
unsigned long dayclock = (unsigned long)curTime % SECS_DAY;
unsigned long dayno = (unsigned long)curTime / SECS_DAY;
t.tm_sec = dayclock % 60;
t.tm_min = (dayclock % 3600) / 60;
t.tm_hour = dayclock / 3600;
t.tm_wday = (dayno + 4) % 7; /* day 0 was a thursday */
while (dayno >= YEARSIZE(year)) {
dayno -= YEARSIZE(year);
year++;
}
t.tm_year = year - YEAR0;
t.tm_mon = 0;
while (dayno >= _ytab[LEAPYEAR(year)][t.tm_mon]) {
dayno -= _ytab[LEAPYEAR(year)][t.tm_mon];
t.tm_mon++;
}
t.tm_mday = dayno + 1;
}
virtual Audio::Mixer *getMixer()
{
return _mixer;
}
virtual Common::String getDefaultConfigFileName()
{
return s_systemDir + "/scummvm.ini";
}
virtual void logMessage(LogMessageType::Type type, const char *message)
{
if (log_cb)
log_cb(RETRO_LOG_INFO, "%s\n", message);
}
//
const Graphics::Surface& getScreen()
{
const Graphics::Surface& srcSurface = (_overlayInGUI) ? _overlay : _gameScreen;
if(srcSurface.w != _screen.w || srcSurface.h != _screen.h)
{
#ifdef FRONTEND_SUPPORTS_RGB565
_screen.create(srcSurface.w, srcSurface.h, Graphics::PixelFormat(2, 5, 6, 5, 0, 11, 5, 0, 0));
#else
_screen.create(srcSurface.w, srcSurface.h, Graphics::PixelFormat(2, 5, 5, 5, 1, 10, 5, 0, 15));
#endif
}
return _screen;
}
#define ANALOG_RANGE 0x8000
#define BASE_CURSOR_SPEED 4
#define PI 3.141592653589793238
void processMouse(retro_input_state_t aCallback, int device, float gampad_cursor_speed, bool analog_response_is_quadratic, int analog_deadzone, float mouse_speed)
{
int16_t joy_x, joy_y, joy_rx, joy_ry, x, y;
float analog_amplitude_x, analog_amplitude_y;
int mouse_acc_int;
bool do_joystick, do_mouse, down;
float adjusted_cursor_speed = (float)BASE_CURSOR_SPEED * gampad_cursor_speed;
int dpad_cursor_offset;
double rs_radius, rs_angle;
unsigned numpad_index;
static const uint32_t retroButtons[2] = {RETRO_DEVICE_ID_MOUSE_LEFT, RETRO_DEVICE_ID_MOUSE_RIGHT};
static const Common::EventType eventID[2][2] =
{
{Common::EVENT_LBUTTONDOWN, Common::EVENT_LBUTTONUP},
{Common::EVENT_RBUTTONDOWN, Common::EVENT_RBUTTONUP}
};
static const unsigned gampad_key_map[8][3] = {
{ RETRO_DEVICE_ID_JOYPAD_X, (unsigned)Common::KEYCODE_ESCAPE, (unsigned)Common::ASCII_ESCAPE }, // Esc
{ RETRO_DEVICE_ID_JOYPAD_Y, (unsigned)Common::KEYCODE_PERIOD, 46 }, // .
{ RETRO_DEVICE_ID_JOYPAD_L, (unsigned)Common::KEYCODE_RETURN, (unsigned)Common::ASCII_RETURN }, // Enter
{ RETRO_DEVICE_ID_JOYPAD_R, (unsigned)Common::KEYCODE_KP5, 53 }, // Numpad 5
{ RETRO_DEVICE_ID_JOYPAD_L2, (unsigned)Common::KEYCODE_BACKSPACE, (unsigned)Common::ASCII_BACKSPACE }, // Backspace
{ RETRO_DEVICE_ID_JOYPAD_L3, (unsigned)Common::KEYCODE_F10, (unsigned)Common::ASCII_F10 }, // F10
{ RETRO_DEVICE_ID_JOYPAD_R3, (unsigned)Common::KEYCODE_KP0, 48 }, // Numpad 0
{ RETRO_DEVICE_ID_JOYPAD_SELECT, (unsigned)Common::KEYCODE_F1, (unsigned)Common::ASCII_F1 }, // F1
};
// Right stick circular wrap around: 1 -> 2 -> 3 -> 6 -> 9 -> 8 -> 7 -> 4
static const unsigned gampad_numpad_map[8][2] = {
{ (unsigned)Common::KEYCODE_KP1, 49 },
{ (unsigned)Common::KEYCODE_KP2, 50 },
{ (unsigned)Common::KEYCODE_KP3, 51 },
{ (unsigned)Common::KEYCODE_KP6, 54 },
{ (unsigned)Common::KEYCODE_KP9, 57 },
{ (unsigned)Common::KEYCODE_KP8, 56 },
{ (unsigned)Common::KEYCODE_KP7, 55 },
{ (unsigned)Common::KEYCODE_KP4, 52 },
};
// Reduce gamepad cursor speed, if required
if (device == RETRO_DEVICE_JOYPAD &&
aCallback(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2))
{
adjusted_cursor_speed = adjusted_cursor_speed * (1.0f / 3.0f);
}
down = false;
do_joystick = false;
x = aCallback(0, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_X);
y = aCallback(0, RETRO_DEVICE_MOUSE, 0, RETRO_DEVICE_ID_MOUSE_Y);
joy_x = aCallback(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_X);
joy_y = aCallback(0, RETRO_DEVICE_ANALOG, RETRO_DEVICE_INDEX_ANALOG_LEFT, RETRO_DEVICE_ID_ANALOG_Y);
// Left Analog X Axis
if (joy_x > analog_deadzone || joy_x < -analog_deadzone)
{
if (joy_x > analog_deadzone)
{
// Reset accumulator when changing direction
_mouseXAcc = (_mouseXAcc < 0.0) ? 0.0 : _mouseXAcc;
joy_x = joy_x - analog_deadzone;
}
if (joy_x < -analog_deadzone)
{
// Reset accumulator when changing direction
_mouseXAcc = (_mouseXAcc > 0.0) ? 0.0 : _mouseXAcc;
joy_x = joy_x + analog_deadzone;
}
// Update accumulator
analog_amplitude_x = (float)joy_x / (float)(ANALOG_RANGE - analog_deadzone);
if (analog_response_is_quadratic)
{
if (analog_amplitude_x < 0.0)
analog_amplitude_x = -(analog_amplitude_x * analog_amplitude_x);
else
analog_amplitude_x = analog_amplitude_x * analog_amplitude_x;
}
//printf("analog_amplitude_x: %f\n", analog_amplitude_x);
_mouseXAcc += analog_amplitude_x * adjusted_cursor_speed;
// Get integer part of accumulator
mouse_acc_int = (int)_mouseXAcc;
if (mouse_acc_int != 0)
{
// Set mouse position
_mouseX += mouse_acc_int;
_mouseX = (_mouseX < 0) ? 0 : _mouseX;
_mouseX = (_mouseX >= _screen.w) ? _screen.w : _mouseX;
do_joystick = true;
// Update accumulator
_mouseXAcc -= (float)mouse_acc_int;
}
}
// Left Analog Y Axis
if (joy_y > analog_deadzone || joy_y < -analog_deadzone)
{
if (joy_y > analog_deadzone)
{
// Reset accumulator when changing direction
_mouseYAcc = (_mouseYAcc < 0.0) ? 0.0 : _mouseYAcc;
joy_y = joy_y - analog_deadzone;
}
if (joy_y < -analog_deadzone)
{
// Reset accumulator when changing direction
_mouseYAcc = (_mouseYAcc > 0.0) ? 0.0 : _mouseYAcc;
joy_y = joy_y + analog_deadzone;
}
// Update accumulator
analog_amplitude_y = (float)joy_y / (float)(ANALOG_RANGE - analog_deadzone);
if (analog_response_is_quadratic)
{
if (analog_amplitude_y < 0.0)
analog_amplitude_y = -(analog_amplitude_y * analog_amplitude_y);
else
analog_amplitude_y = analog_amplitude_y * analog_amplitude_y;
}
//printf("analog_amplitude_y: %f\n", analog_amplitude_y);
_mouseYAcc += analog_amplitude_y * adjusted_cursor_speed;
// Get integer part of accumulator
mouse_acc_int = (int)_mouseYAcc;
if (mouse_acc_int != 0)
{
// Set mouse position
_mouseY += mouse_acc_int;