-
Notifications
You must be signed in to change notification settings - Fork 48
/
Controller.cpp
2350 lines (1877 loc) · 66.5 KB
/
Controller.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
/*
Changes:
01/04/29 (TOM): Product version handling changed, removed the m_iProductVersion member variable,
fixed the version check in .run() and added the version property to the interface
01/07/26 (SJE): Added MessageBox confirmation if game is flagged not working!
Added Warning message for game flagged with NO SOUND!
01/08/12 (SJE): Added Warning message for game flagged with IMPERFECT SOUND
01/09/26 (SJE): Added Check to see if Invalid CRC games can be run.
03/09/22 (SJE): Added IMPERFECT GRAPHICS FLAG and reworked code to allow more than 1 message to display together
PinMame runs on another thread than the controller without blocking synchronization primitives. Therefore when getters are called,
the return value is the last known state. For lazy updated values like physic outputs, getters also trigger an update that will be
serviced asynchronously. The theory of operation is that these getters are to be called repeatedly to update/get the actual value.
*/
// Controller.cpp : Implementation of Controller and DLL registration.
#include "StdAfx.h"
#include "VPinMAME.h"
#include "VPinMAMEAboutDlg.h"
#include "VPinMAMEConfig.h"
#include "Controller.h"
#include "ControllerDisclaimerDlg.h"
#include "ControllerGames.h"
#include "ControllerRegkeys.h"
extern "C" {
#include "driver.h"
#include "core.h"
#include "vpintf.h"
#include "mame.h"
#include "video.h"
extern HWND win_video_window;
extern int g_fPause;
extern struct RunningMachine *Machine;
extern struct mame_display *current_display_ptr;
extern UINT8 g_raw_dmdbuffer[DMD_MAXY*DMD_MAXX];
extern UINT32 g_raw_colordmdbuffer[DMD_MAXY*DMD_MAXX];
extern UINT32 g_raw_dmdx;
extern UINT32 g_raw_dmdy;
extern UINT8 g_needs_DMD_update;
extern int g_cpu_affinity_mask;
extern char g_fShowWinDMD;
extern char g_szGameName[256];
// from ticker.c
extern void uSleep(const UINT64 u);
// from snd_alt.h
#ifdef VPINMAME_ALTSOUND
extern void alt_sound_pause(BOOL pause);
#endif
}
#include "Alias.h"
extern int fAllowWriteAccess;
extern int deprecated_synclevel;
void CreateEventWindow(CController* pController);
void DestroyEventWindow(CController* pController);
extern "C" {
extern void set_lowest_possible_win_timer_resolution();
extern void restore_win_timer_resolution();
}
/////////////////////////////////////////////////////////////////////////////
//
/***********************/
/*COM ERROR INTERFACE */
/***********************/
STDMETHODIMP CController::InterfaceSupportsErrorInfo(REFIID riid)
{
static const IID* arr[] =
{
&IID_IController,
};
for (int i=0;i<sizeof(arr)/sizeof(arr[0]);i++)
{
if (InlineIsEqualGUID(*arr[i],riid))
return S_OK;
}
return S_FALSE;
}
/***************************************
* Get product version from the resource
***************************************/
void CController::GetProductVersion(int *nVersionNo0, int *nVersionNo1, int *nVersionNo2, int *nVersionNo3)
{
DWORD dwVerHandle;
DWORD dwInfoSize;
HANDLE hVersionInfo;
LPVOID lpEntryInfo;
UINT wVarSize;
VS_FIXEDFILEINFO *pFixedFileInfo;
char szFilename[MAX_PATH];
GetModuleFileName(_Module.m_hInst, szFilename, sizeof szFilename);
if ( !(dwInfoSize = GetFileVersionInfoSize((LPSTR) szFilename, &dwVerHandle)) ) {
return;
}
hVersionInfo = GlobalAlloc(GPTR, dwInfoSize);
if ( !hVersionInfo )
return;
lpEntryInfo = GlobalLock(hVersionInfo);
if ( !lpEntryInfo ) {
GlobalFree(hVersionInfo);
return;
}
if ( !GetFileVersionInfo((LPSTR) szFilename, dwVerHandle, dwInfoSize, lpEntryInfo) ) {
GlobalUnlock(hVersionInfo);
GlobalFree(hVersionInfo);
return;
}
if ( !VerQueryValue(lpEntryInfo, "\\", (void**) &pFixedFileInfo, &wVarSize) ) {
GlobalUnlock(hVersionInfo);
GlobalFree(hVersionInfo);
return;
}
if ( nVersionNo0 )
*nVersionNo0 = (int) HIWORD(pFixedFileInfo->dwProductVersionMS);
if ( nVersionNo1 )
*nVersionNo1 = (int) LOWORD(pFixedFileInfo->dwProductVersionMS);
if ( nVersionNo2 )
*nVersionNo2 = (int) HIWORD(pFixedFileInfo->dwProductVersionLS);
if ( nVersionNo3 )
*nVersionNo3 = (int) LOWORD(pFixedFileInfo->dwProductVersionLS);
GlobalUnlock(hVersionInfo);
GlobalFree(hVersionInfo);
}
/*************************************************
* IController: Class construction and destruction
*************************************************/
CController::CController() {
set_lowest_possible_win_timer_resolution();
cli_frontend_init();
lstrcpy(m_szSplashInfoLine, "");
m_hThreadRun = INVALID_HANDLE_VALUE;
m_hEmuIsRunning = CreateEvent(NULL, TRUE, FALSE, NULL);
m_hEventWnd = 0;
m_szROM[0] = '\0';
m_nGameNo = -1;
LoadGlobalSettings();
// get a pointer to out ControllerSettings object
HRESULT hr = CComObject<CControllerSettings>::CreateInstance(&m_pControllerSettings);
if ( SUCCEEDED(hr) )
m_pControllerSettings->AddRef();
hr = CComObject<CGames>::CreateInstance(&m_pGames);
if ( SUCCEEDED(hr) )
m_pGames->AddRef();
// get a pointer to the settings object for the "default" game
CComVariant szROM(m_szROM);
m_pGames->get_Item(&szROM, &m_pGame);
m_pGame->get_Settings(&m_pGameSettings);
// these value are not stored to the registry
m_fHandleKeyboard = FALSE;
m_iHandleMechanics = 0xFF;
m_fDisplayLocked = FALSE;
char szRegKey[256];
lstrcpy(szRegKey, REG_BASEKEY);
fAllowWriteAccess = ReadRegistry(szRegKey, REG_DWALLOWWRITEACCESS, REG_DWALLOWWRITEACCESSDEF);
vp_init();
// Never started before?
// Check this by asking if the default registry key is existing
if ( GameUsedTheFirstTime("") ) {
ShowAboutDialog(0);
MessageBox(GetActiveWindow(),"Welcome to Visual PinMAME! \n Please setup the default game options by clicking \"OK\"!","Notice!",MB_OK | MB_ICONINFORMATION);
m_pControllerSettings->ShowSettingsDlg(0);
m_pGameSettings->ShowSettingsDlg(0);
}
}
CController::~CController() {
Stop();
CloseHandle(m_hEmuIsRunning);
restore_win_timer_resolution();
m_pGame->Release();
m_pGameSettings->Release();
m_pGames->Release();
m_pControllerSettings->Release();
cli_frontend_exit();
}
/*****************************************
* IController.Run method: start emulation
*****************************************/
STDMETHODIMP CController::Run(/*[in]*/ LONG_PTR hParentWnd, /*[in,defaultvalue(100)]*/ int nMinVersion)
{
/*Make sure GameName Specified!*/
if (m_szROM[0] == '\0')
return Error(TEXT("Game name not specified!"));
int nVersionNo0, nVersionNo1;
GetProductVersion(&nVersionNo0, &nVersionNo1, NULL, NULL);
if ( (nVersionNo0*100+nVersionNo1)<nMinVersion )
return Error(TEXT("You need a newer version of Visual PinMame to run this emulation!"));
if ( m_hThreadRun!=INVALID_HANDLE_VALUE ) {
if ( WaitForSingleObject(m_hThreadRun, 0)==WAIT_TIMEOUT )
return Error(TEXT("Emulation already running!"));
else {
CloseHandle(m_hThreadRun);
m_hThreadRun = INVALID_HANDLE_VALUE;
}
}
char szTemp[256];
if (m_nGameNo<0) {
sprintf(szTemp, "Machine '%s' not found! Invalid game name, or game name not set!", m_szROM);
return Error(TEXT(szTemp));
}
// set the parent window
m_hParentWnd = (HWND) hParentWnd;
// if we don't have gotten a window handle try to find the VP player window
if ( !hParentWnd || !IsWindow(m_hParentWnd) ) {
m_hParentWnd = FindWindow("VPPlayer", "Visual Pinball Player");
if ( !IsWindow(m_hParentWnd) )
m_hParentWnd = (HWND) 0;
}
VARIANT vValue;
VariantInit(&vValue);
m_pGameSettings->get_Value(CComBSTR("sound"), &vValue);
int iCheckVal = IDOK;
m_pGame->ShowInfoDlg(0x8000|CHECKOPTIONS_SHOWRESULTSIFFAIL|((vValue.boolVal==VARIANT_TRUE)?0x0000:CHECKOPTIONS_IGNORESOUNDROMS), (LONG_PTR) m_hParentWnd, &iCheckVal);
if ( iCheckVal==IDCANCEL ) {
sprintf(szTemp, "Game ROMs for '%s' (%s) invalid!", m_szROM, drivers[m_nGameNo]->description);
return Error(TEXT(szTemp));
}
VariantInit(&vValue);
m_pGameSettings->get_Value(CComBSTR("cabinet_mode"), &vValue);
bool cabinetMode = (vValue.boolVal==VARIANT_TRUE);
if ( GameWasNeverStarted(m_szROM) ) {
int fFirstTime = GameUsedTheFirstTime(m_szROM);
if(!cabinetMode) {
CComBSTR sDescription;
m_pGame->get_Description(&sDescription);
char szDescription[256];
WideCharToMultiByte(CP_ACP, 0, sDescription, -1, szDescription, sizeof szDescription, NULL, NULL);
if ( !ShowDisclaimer(m_hParentWnd, szDescription) )
return Error(TEXT("User is not legally entitled to play this game!"));
}
SetGameWasStarted(m_szROM);
if(!cabinetMode) {
if ( fFirstTime ) {
char szTemp[256];
sprintf(szTemp,"This is the first time you use: %s\nPlease specify options for this game by clicking \"OK\"!",drivers[m_nGameNo]->description);
MessageBox(GetActiveWindow(),szTemp,"Notice!",MB_OK | MB_ICONINFORMATION);
m_pGameSettings->ShowSettingsDlg(0);
}
}
}
#ifndef DEBUG
if(!cabinetMode) {
// See if game is flagged as GAME_NOT_WORKING and ask user if they wish to continue!!
if ( drivers[m_nGameNo]->flags&GAME_NOT_WORKING ) {
if(MessageBox(GetActiveWindow(),"This game DOES NOT WORK properly!\n Running it may cause VPinMAME to crash or lock up \n\n Are you sure you would like to continue?","Notice!",MB_YESNO | MB_ICONWARNING) == IDNO)
return 1;
}
// See if the game is flagged as GAME_NOCRC so that the CRC *must* be correct
if ((iCheckVal!=IDOK) && (drivers[m_nGameNo]->flags & GAME_NOCRC)) {
MessageBox(GetActiveWindow(),"This game can only run with the EXACT romset required!","Notice!",MB_OK | MB_ICONINFORMATION);
sprintf(szTemp, "CRC Errors! Game '%s' (%s) cannot be run!", m_szROM, drivers[m_nGameNo]->description);
return Error(TEXT(szTemp));
}
//Any game messages to display (messages that allow game to continue)
if ( drivers[m_nGameNo]->flags )
{
sprintf(szTemp,"");
// See if game is flagged as GAME_NO_SOUND and show user a message!
if ( drivers[m_nGameNo]->flags&GAME_NO_SOUND )
sprintf(szTemp,"%sPlease be aware that this game does not have sound emulated yet!\n\n",szTemp);
// See if game is flagged as GAME_IMPERFECT_SOUND and show user a message!
if ( drivers[m_nGameNo]->flags&GAME_IMPERFECT_SOUND )
sprintf(szTemp,"%sPlease be aware that this game's sound emulation is not working 100%% properly!\n\n",szTemp);
// See if game is flagged as GAME_IMPERFECT_GRAPHICS and show user a message!
if ( drivers[m_nGameNo]->flags&GAME_IMPERFECT_GRAPHICS )
sprintf(szTemp,"%sPlease be aware that this game's graphics are not working 100%% properly!\n\n",szTemp);
if(strlen(szTemp)>0)
MessageBox(GetActiveWindow(),szTemp,"Notice!",MB_OK | MB_ICONINFORMATION);
}
}
#endif
ResetEvent(m_hEmuIsRunning);
CreateEventWindow(this);
DWORD dwThreadID;
m_hThreadRun = /*_beginthreadex*/CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE) RunController,
(LPVOID) this,
0, &dwThreadID);
if ( !dwThreadID ) {
DestroyEventWindow(this);
return Error(TEXT("Unable to start thread!"));
}
if (g_cpu_affinity_mask > 0)
SetThreadAffinityMask(m_hThreadRun, g_cpu_affinity_mask);
// ok, let's wait for either the machine is set up or the thread terminates for some reason
HANDLE StartHandles[2] = {m_hEmuIsRunning, m_hThreadRun};
if ( WaitForMultipleObjects(2, StartHandles, FALSE, INFINITE)==WAIT_OBJECT_0) {
// fine, here we go
return S_OK;
}
CloseHandle(m_hThreadRun);
m_hThreadRun = INVALID_HANDLE_VALUE;
DestroyEventWindow(this);
sprintf(szTemp, "Machine '%s' (%s) terminated before initialized, check the rom path or rom file!", m_szROM, drivers[m_nGameNo]->description);
return Error(TEXT(szTemp));
}
/*********************************************
* IController.Stop() method: stop emulation
*********************************************/
STDMETHODIMP CController::Stop()
{
if ( m_hThreadRun==INVALID_HANDLE_VALUE )
return S_OK;
PostMessage(win_video_window, WM_CLOSE, 0, 0);
WaitForSingleObject(m_hThreadRun,INFINITE);
CloseHandle(m_hThreadRun);
m_hThreadRun = INVALID_HANDLE_VALUE;
DestroyEventWindow(this);
return S_OK;
}
/****************************************************************
* IController.Lamp property (read only): get the state of a lamp
****************************************************************/
STDMETHODIMP CController::get_Lamp(int nLamp, VARIANT_BOOL *pVal)
{
if (!pVal) return S_FALSE;
if (WaitForSingleObject(m_hEmuIsRunning, 0) == WAIT_TIMEOUT)
*pVal= false;
else {
core_request_pwm_output_update();
*pVal = vp_getLamp(nLamp)?VARIANT_TRUE:VARIANT_FALSE;
}
return S_OK;
}
/************************************************************************
* IController.Solenoid property (read only): get the state of a solenoid
************************************************************************/
STDMETHODIMP CController::get_Solenoid(int nSolenoid, VARIANT_BOOL *pVal)
{
if (!pVal) return S_FALSE;
if (WaitForSingleObject(m_hEmuIsRunning, 0) == WAIT_TIMEOUT)
*pVal= false;
else {
core_request_pwm_output_update();
*pVal = vp_getSolenoid(nSolenoid)?VARIANT_TRUE:VARIANT_FALSE;
}
return S_OK;
}
/************************************************************************
* IController.Switch property: get/set the state of a switch
************************************************************************/
STDMETHODIMP CController::get_Switch(int nSwitchNo, VARIANT_BOOL *pVal) {
if (pVal)
{
if (WaitForSingleObject(m_hEmuIsRunning, 0) == WAIT_TIMEOUT)
*pVal = false;
else
*pVal = vp_getSwitch(nSwitchNo)?VARIANT_TRUE:VARIANT_FALSE;
}
return S_OK;
}
STDMETHODIMP CController::put_Switch(int nSwitchNo, VARIANT_BOOL newVal)
{
if (WaitForSingleObject(m_hEmuIsRunning, 0) == WAIT_TIMEOUT)
return S_OK;
else
vp_putSwitch(nSwitchNo, newVal);
return S_OK;
}
/*************************************************
* IController.Pause property: Pause the emulation
*************************************************/
STDMETHODIMP CController::get_Pause(VARIANT_BOOL *pVal)
{
*pVal = (WaitForSingleObject(m_hEmuIsRunning, 0)==WAIT_OBJECT_0) ? (g_fPause?VARIANT_TRUE:VARIANT_FALSE):VARIANT_FALSE;
return S_OK;
}
STDMETHODIMP CController::put_Pause(VARIANT_BOOL newVal)
{
if (WaitForSingleObject(m_hEmuIsRunning, 0) == WAIT_TIMEOUT)
return S_OK;
g_fPause = newVal;
#ifdef VPINMAME_ALTSOUND
alt_sound_pause(g_fPause);
#endif
return S_OK;
}
/***************************************************************************************
* IController.WPCNumbering property (read-only): returns if WPCNumbering is used or not
***************************************************************************************/
STDMETHODIMP CController::get_WPCNumbering(/*[out, retval]*/ VARIANT_BOOL *pVal)
{
if (!pVal) return S_FALSE;
if (WaitForSingleObject(m_hEmuIsRunning, 0) == WAIT_TIMEOUT)
*pVal= 0;
else
*pVal = vp_getWPCNumbering()?VARIANT_TRUE:VARIANT_FALSE;
return S_OK;
}
/*********************************************************************
* IController.Lamps property (read-only): gets the state of all lamps
*********************************************************************/
STDMETHODIMP CController::get_Lamps(VARIANT *pVal)
{
if ( !pVal )
return S_FALSE;
core_request_pwm_output_update();
SAFEARRAY *psa = SafeArrayCreateVector(VT_VARIANT, 0, 89);
VARIANT* pData;
SafeArrayAccessData(psa, (void**)&pData);
const bool timeout = (WaitForSingleObject(m_hEmuIsRunning, 0) == WAIT_TIMEOUT);
for (int i = timeout ? 11 : 0; i < 89; ++i)
{
pData[i].vt = VT_BOOL;
pData[i].boolVal = timeout ? VARIANT_FALSE : (vp_getLamp(i) ? VARIANT_TRUE : VARIANT_FALSE);
}
SafeArrayUnaccessData(psa);
pVal->vt = VT_ARRAY|VT_VARIANT;
pVal->parray = psa;
return S_OK;
}
/************************************************************************
* IController.RawDmdWidth property (read-only): get the width of the DMD
************************************************************************/
STDMETHODIMP CController::get_RawDmdWidth(int *pVal)
{
*pVal = g_raw_dmdx;
return S_OK;
}
/**************************************************************************
* IController.RawDmdHeight property (read-only): get the height of the DMD
**************************************************************************/
STDMETHODIMP CController::get_RawDmdHeight(int *pVal)
{
*pVal = g_raw_dmdy;
return S_OK;
}
/**************************************************************************
* IController.NVRAM (read-only): Copy whole NVRAM to a self allocated array
***************************************************************************/
STDMETHODIMP CController::get_NVRAM(VARIANT *pVal)
{
if (Machine && Machine->drv && Machine->drv->nvram_handler && pVal)
{
// setup a ram file manually (MAME has no mechanism so far)
mame_file* nvram_file = (mame_file*)malloc(sizeof(mame_file));
memset(nvram_file, 0, sizeof(mame_file));
nvram_file->type = RAM_FILE;
// call nvram handler to write to file
(*Machine->drv->nvram_handler)(nvram_file, 1);
if (nvram_file->offset == 0)
{
mame_fclose(nvram_file);
return S_FALSE;
}
SAFEARRAY *psa = SafeArrayCreateVector(VT_VARIANT, 0, (ULONG)nvram_file->offset);
VARIANT* pData;
SafeArrayAccessData(psa, (void**)&pData);
for (int ofs = 0; ofs < (int)nvram_file->offset; ++ofs)
{
pData[ofs].vt = VT_UI1;
pData[ofs].cVal = nvram_file->data[ofs];
}
SafeArrayUnaccessData(psa);
pVal->vt = VT_ARRAY | VT_VARIANT;
pVal->parray = psa;
mame_fclose(nvram_file);
return S_OK;
}
else
return S_FALSE;
}
static UINT8 oldNVRAM[CORE_MAXNVRAM];
static char* oldNVRAMname = 0;
static vp_tChgNVRAMs chgNVRAMs; // stack overflow when put into get_ChangedNVRAM??
/***************************************************************
* IController.ChangedNVRAM property: returns a list of the
* numbers of NVRAM locations, which state has changed since the last call
* number is in the first, state in the second part, previous state in the third
***************************************************************/
STDMETHODIMP CController::get_ChangedNVRAM(VARIANT *pVal)
{
if (!pVal) return S_FALSE;
if (WaitForSingleObject(m_hEmuIsRunning, 0) == WAIT_TIMEOUT)
{
pVal->vt = 0; return S_OK;
}
if (!(Machine && Machine->drv && Machine->drv->nvram_handler))
return S_FALSE;
// setup a ram file manually (MAME has no mechanism so far)
mame_file* nvram_file = (mame_file*)malloc(sizeof(mame_file));
memset(nvram_file, 0, sizeof(mame_file));
nvram_file->type = RAM_FILE;
// call nvram handler to write to file
(*Machine->drv->nvram_handler)(nvram_file, 1);
if (nvram_file->offset == 0)
{
mame_fclose(nvram_file);
return S_FALSE;
}
/*-- Count changes --*/
size_t uCount;
if (oldNVRAMname == 0 || strstr(Machine->gamedrv->name, oldNVRAMname) == 0) // detect initial VPM start or game change
{
uCount = min((size_t)nvram_file->offset, CORE_MAXNVRAM);
for (size_t i = 0; i < uCount; ++i)
{
chgNVRAMs[i].nvramNo = i;
chgNVRAMs[i].oldStat = 0; //!!
chgNVRAMs[i].currStat = nvram_file->data[i];
}
memcpy(oldNVRAM, nvram_file->data, uCount);
if (oldNVRAMname)
free(oldNVRAMname);
oldNVRAMname = (char*)malloc(strlen(Machine->gamedrv->name) + 1);
strcpy(oldNVRAMname, Machine->gamedrv->name);
mame_fclose(nvram_file);
pVal->vt = 0; return S_OK; //!! for now, as too many changes initially!?!
}
else
{
uCount = 0;
size_t uCountMax = min((size_t)nvram_file->offset, CORE_MAXNVRAM);
for (size_t i = 0; i < uCountMax; ++i)
{
if (oldNVRAM[i] != nvram_file->data[i])
{
chgNVRAMs[uCount].nvramNo = i;
chgNVRAMs[uCount].oldStat = oldNVRAM[i];
chgNVRAMs[uCount].currStat = nvram_file->data[i];
uCount++;
oldNVRAM[i] = nvram_file->data[i];
}
}
}
mame_fclose(nvram_file);
if (uCount == 0)
{
pVal->vt = 0; return S_OK;
}
/*-- Create array --*/
SAFEARRAYBOUND Bounds[] = { { (ULONG)uCount, 0 }, { 3, 0 } };
SAFEARRAY *psa = SafeArrayCreate(VT_VARIANT, 2, Bounds);
long ix[2];
VARIANT varValue;
varValue.vt = VT_I4;
/*-- add changed locations to array --*/
for (ix[0] = 0; ix[0] < (long)uCount; ix[0]++) {
ix[1] = 0;
varValue.lVal = chgNVRAMs[ix[0]].nvramNo;
SafeArrayPutElement(psa, ix, &varValue);
ix[1] = 1; // NVRAM value
varValue.lVal = chgNVRAMs[ix[0]].currStat;
SafeArrayPutElement(psa, ix, &varValue);
ix[1] = 2; // Old NVRAM value
varValue.lVal = chgNVRAMs[ix[0]].oldStat;
SafeArrayPutElement(psa, ix, &varValue);
}
pVal->vt = VT_ARRAY | VT_VARIANT;
pVal->parray = psa;
return S_OK;
}
/************************************************************************************************
* IController.RawDmdPixels (read-only): Copy whole DMD to a self allocated array (values 0..100)
************************************************************************************************/
STDMETHODIMP CController::get_RawDmdPixels(VARIANT *pVal)
{
if(Machine && g_needs_DMD_update && (int)g_raw_dmdx > 0 && (int)g_raw_dmdy > 0 && pVal)
{
SAFEARRAY *psa = SafeArrayCreateVector(VT_VARIANT, 0, g_raw_dmdx*g_raw_dmdy);
VARIANT* pData;
SafeArrayAccessData(psa, (void**)&pData);
const UINT32 end = g_raw_dmdx*g_raw_dmdy;
for (UINT32 i = 0; i < end; ++i)
{
pData[i].vt = VT_UI1;
pData[i].cVal = g_raw_dmdbuffer[i];
}
SafeArrayUnaccessData(psa);
pVal->vt = VT_ARRAY|VT_VARIANT;
pVal->parray = psa;
g_needs_DMD_update = 0;
return S_OK;
}
else
return S_FALSE;
}
/******************************************************************************************************
* IController.RawDmdColoredPixels (read-only): Copy whole DMD to a self allocated array (RGB(A) values)
*******************************************************************************************************/
STDMETHODIMP CController::get_RawDmdColoredPixels(VARIANT *pVal)
{
if(Machine && g_needs_DMD_update && (int)g_raw_dmdx > 0 && (int)g_raw_dmdy > 0 && pVal)
{
SAFEARRAY *psa = SafeArrayCreateVector(VT_VARIANT, 0, g_raw_dmdx*g_raw_dmdy);
VARIANT* pData;
SafeArrayAccessData(psa, (void**)&pData);
const UINT32 end = g_raw_dmdx*g_raw_dmdy;
for (UINT32 i = 0; i < end; ++i)
{
pData[i].vt = VT_UI4;
pData[i].uintVal = g_raw_colordmdbuffer[i];
}
SafeArrayUnaccessData(psa);
pVal->vt = VT_ARRAY|VT_VARIANT;
pVal->parray = psa;
g_needs_DMD_update = 0;
return S_OK;
}
else
return S_FALSE;
}
/************************************************************************
* IController.DmdWidth property (read-only): get the width of DMD bitmap
************************************************************************/
STDMETHODIMP CController::get_DmdWidth(int *pVal)
{
*pVal = current_display_ptr ? current_display_ptr->game_visible_area.max_x-current_display_ptr->game_visible_area.min_x+1 : 0;
return S_OK;
}
/**************************************************************************
* IController.DmdHeight property (read-only): get the height of DMD bitmap
**************************************************************************/
STDMETHODIMP CController::get_DmdHeight(int *pVal)
{
*pVal = current_display_ptr ? current_display_ptr->game_visible_area.max_y-current_display_ptr->game_visible_area.min_y+1 : 0;
return S_OK;
}
/*************************************************************************
* IController.DmdPixel (read-only): read a given pixel of the DMD (slow!)
*************************************************************************/
STDMETHODIMP CController::get_DmdPixel(int x, int y, int *pVal)
{
if(Machine && Machine->scrbitmap)
*pVal = Machine->scrbitmap->read(Machine->scrbitmap,x,y);
else
*pVal = 0;
return S_OK;
}
/*******************************************************************************
* updateDmdPixels (read-only): Copy whole Dmd Bitmap to a user allocated array
*******************************************************************************/
STDMETHODIMP CController::get_updateDmdPixels(int **buf, int width, int height, int *pVal)
{
if(!buf)
{
*pVal = 0;
return S_FALSE;
}
mame_bitmap * btm = current_display_ptr ? current_display_ptr->game_bitmap : 0;
if(Machine && current_display_ptr && btm)
{
if(width != (current_display_ptr->game_visible_area.max_x-current_display_ptr->game_visible_area.min_x+1) ||
height != (current_display_ptr->game_visible_area.max_y-current_display_ptr->game_visible_area.min_y+1) )
{
*pVal = 0;
return S_OK;
}
float *dst = reinterpret_cast<float*>(buf);
if (btm->depth == 8)
{
for(int j=current_display_ptr->game_visible_area.max_y;j>=current_display_ptr->game_visible_area.min_y;j--)
{
UINT8 *src = (UINT8*)btm->line[j] + current_display_ptr->game_visible_area.min_x;
for(int i=current_display_ptr->game_visible_area.min_x;i<=current_display_ptr->game_visible_area.max_x;i++)
{
UINT8 r,g,b;
palette_get_color((*src++),&r,&g,&b);
*(dst++) = (float)r*(float)(1.0/255.0);
*(dst++) = (float)g*(float)(1.0/255.0);
*(dst++) = (float)b*(float)(1.0/255.0);
*(dst++) = 1.0f;
}
}
}
else if(btm->depth == 15 || btm->depth == 16)
{
for(int j=current_display_ptr->game_visible_area.max_y;j>=current_display_ptr->game_visible_area.min_y;j--)
{
UINT16 *src = (UINT16*)btm->line[j] + current_display_ptr->game_visible_area.min_x;
for(int i=current_display_ptr->game_visible_area.min_x;i<=current_display_ptr->game_visible_area.max_x;i++)
{
UINT8 r,g,b;
palette_get_color((*src++),&r,&g,&b);
*(dst++) = (float)r*(float)(1.0/255.0);
*(dst++) = (float)g*(float)(1.0/255.0);
*(dst++) = (float)b*(float)(1.0/255.0);
*(dst++) = 1.0f;
}
}
}
else
{
for(int j=current_display_ptr->game_visible_area.max_y;j>=current_display_ptr->game_visible_area.min_y;j--)
{
UINT32 *src = (UINT32*)btm->line[j] + current_display_ptr->game_visible_area.min_x;
for(int i=current_display_ptr->game_visible_area.min_x;i<=current_display_ptr->game_visible_area.max_x;i++)
{
UINT8 r,g,b;
palette_get_color((*src++),&r,&g,&b);
*(dst++) = (float)r*(float)(1.0/255.0);
*(dst++) = (float)g*(float)(1.0/255.0);
*(dst++) = (float)b*(float)(1.0/255.0);
*(dst++) = 1.0f;
}
}
}
//*pVal = Machine->scrbitmap->read(Machine->scrbitmap,x,y);
*pVal = 1;
}
else
*pVal = 0;
return S_OK;
}
/*****************************************************************************************
* ChangedLampsState (read-only): Copy whole Changed Lamps array to a user allocated array
*****************************************************************************************/
STDMETHODIMP CController::get_ChangedLampsState(int **buf, int *pVal)
{
if(!buf)
{
*pVal = 0;
return S_FALSE;
}
if (!pVal) return S_FALSE;
if (WaitForSingleObject(m_hEmuIsRunning, 0) == WAIT_TIMEOUT)
{ *pVal = 0; return S_OK; }
/*-- Request an update that will be processed asynchronously --*/
core_request_pwm_output_update();
/*-- Count changes --*/
vp_tChgLamps chgLamps;
int uCount = vp_getChangedLamps(chgLamps);
if (uCount == 0)
{ *pVal = 0; return S_OK; }
/*-- add changed lamps to array --*/
int *dst = reinterpret_cast<int*>(buf);
for (int i = 0; i < uCount; i++)
{
*(dst++) = chgLamps[i].lampNo;
*(dst++) = chgLamps[i].currStat;
}
*pVal = uCount;
return S_OK;
}
/**************************************************************************
* LampsState (read-only): Copy whole Lamps array to a user allocated array
**************************************************************************/
STDMETHODIMP CController::get_LampsState(int **buf, int *pVal)
{
if(!buf)
{
*pVal = 0;
return S_FALSE;
}
if (!pVal) return S_FALSE;
/*-- Request an update that will be processed asynchronously --*/
core_request_pwm_output_update();
/*-- list lamps states to array --*/
int *dst = reinterpret_cast<int*>(buf);
if ( WaitForSingleObject(m_hEmuIsRunning, 0)==WAIT_TIMEOUT ) {
for (int ix=0; ix<89; ix++)
*(dst++) = 0;
}
else {
for (int ix=0; ix<89; ix++)
*(dst++) = vp_getLamp(ix) ? 1:0;
}
*pVal = 89;
return S_OK;
}
/*************************************************************************************************
* ChangedSolenoidsState (read-only): Copy whole Changed Solenoids array to a user allocated array
*************************************************************************************************/
STDMETHODIMP CController::get_ChangedSolenoidsState(int **buf, int *pVal)
{
if(!buf)
{
*pVal = 0;
return S_FALSE;
}
if (!pVal) return S_FALSE;
if (WaitForSingleObject(m_hEmuIsRunning, 0) == WAIT_TIMEOUT)
{ *pVal = 0; return S_OK; }
/*-- Request an update that will be processed asynchronously --*/
core_request_pwm_output_update();
/*-- Count changes --*/
vp_tChgSols chgSol;
int uCount = vp_getChangedSolenoids(chgSol);
if (uCount == 0)
{ *pVal = 0; return S_OK; }
/*-- add changed solenoids to array --*/
int *dst = reinterpret_cast<int*>(buf);
for (int i = 0; i < uCount; i++)
{
*(dst++) = chgSol[i].solNo;
*(dst++) = chgSol[i].currStat;
}
*pVal = uCount;
return S_OK;
}
/**********************************************************************************
* SolenoidsState (read-only): Copy whole Solenoids array to a user allocated array
**********************************************************************************/
STDMETHODIMP CController::get_SolenoidsState(int **buf, int *pVal)
{
if(!buf)
{
*pVal = 0;
return S_FALSE;
}
if (!pVal) return S_FALSE;
/*-- Request an update that will be processed asynchronously --*/
core_request_pwm_output_update();
/*-- list lamps states to array --*/
int *dst = reinterpret_cast<int*>(buf);
if ( WaitForSingleObject(m_hEmuIsRunning, 0)==WAIT_TIMEOUT ) {
for (int ix=0; ix<65; ix++)
*(dst++) = 0;
}
else {
for (int ix=0; ix<65; ix++)
*(dst++) = vp_getSolenoid(ix);
}
*pVal = 65;
return S_OK;
}
/*************************************************************************************