forked from dippy-dipper/ogg-winmm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathogg-winmm.c
1936 lines (1746 loc) · 74.2 KB
/
ogg-winmm.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
/*
* Copyright (c) 2012 Toni Spets <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* Code revised by DD (2020) (v.0.2.0.2) */
// 2022 - 2023 revisions by Y-B.
#include <windows.h>
#include <stdio.h>
#include <ctype.h>
#include <dirent.h>
#include "player.h"
/* MCI Relay declarations: */
MCIERROR WINAPI relay_mciSendCommandA(MCIDEVICEID a0, UINT a1, DWORD a2, DWORD a3);
MCIERROR WINAPI relay_mciSendStringA(LPCSTR a0, LPSTR a1, UINT a2, HWND a3);
int MAGIC_DEVICEID = 48879; /* 48879 = 0xBEEF */
#define MAX_TRACKS 99
MCI_OPEN_PARMS mciOpenParms;
struct track_info
{
char path[MAX_PATH]; /* full path to ogg */
unsigned int length; /* seconds */
unsigned int position; /* seconds */
};
static struct track_info tracks[MAX_TRACKS];
struct play_info
{
int first;
int last;
};
#define dprintf(...) if (fh) { fprintf(fh, __VA_ARGS__); fflush(NULL); }
FILE *fh = NULL;
int FullNotify = 0;
int opened = 0;
int sendStringNotify = 0;
int ACCSeekOFF = 0;
int seek = 0;
int plrpos = 0;
int plrpos2 = -1;
int current = 1;
int paused = 0;
int notify = 0;
int playing = 0;
HANDLE player = NULL;
HANDLE initialize = NULL;
HINSTANCE hModule = 0;
int firstTrack = -1;
int lastTrack = 0;
int numTracks = 1; /* +1 for data track on mixed mode cd's */
char music_path[2048];
int time_format = MCI_FORMAT_MSF;
char alias_s[100] = "cdaudio";
static struct play_info info = { -1, -1 };
int player_main(struct play_info *info)
{
int first = info->first;
int last = info->last -1; /* -1 for plr logic */
if(last<first)last = first; /* manage plr logic */
current = first;
if(current<firstTrack)current = firstTrack;
dprintf("OGG Player logic: %d to %d\r\n", first, last);
while (current <= last && playing)
{
dprintf("Current track: %s\r\n", tracks[current].path);
plr_play(tracks[current].path);
while (1)
{
if(paused || seek){
plr_seek(plrpos);
paused = 0;
seek = 0;
plrpos = 0;
}
if(plr_tell() >= plrpos2 && plrpos2!=-1 && current == last){
plrpos = plr_tell();
plrpos2 = -1;
paused = 1;
playing = 0;
if(notify){
notify = 0;
dprintf(" Sending MCI_NOTIFY_SUCCESSFUL message...\r\n");
SendMessageA((HWND)0xffff, MM_MCINOTIFY, MCI_NOTIFY_SUCCESSFUL, MAGIC_DEVICEID);
}
return 0;
}
if (plr_pump() == 0)
break;
if (!playing)
{
return 0;
}
}
current++;
}
playing = 0;
/* Sending notify successful message:*/
if(notify && !paused)
{
notify = 0;
dprintf(" Sending MCI_NOTIFY_SUCCESSFUL message...\r\n");
SendMessageA((HWND)0xffff, MM_MCINOTIFY, MCI_NOTIFY_SUCCESSFUL, MAGIC_DEVICEID);
/* NOTE: Notify message after successful playback is not working in Vista+.
MCI_STATUS_MODE does not update to show that the track is no longer playing.
Bug or broken design in mcicda.dll (also noted by the Wine team) */
}
return 0;
}
//Initialization thread:
int initialize_main(void)
{
//Read winmm.ini options:
int bLog = GetPrivateProfileInt("winmm", "Log", 0, ".\\winmm.ini");
if(bLog)fh = fopen("winmm.log", "w"); // Renamed to .log
int bMCIDevID = GetPrivateProfileInt("winmm", "MCIDevID", 0, ".\\winmm.ini");
if(bMCIDevID){
mciOpenParms.lpstrDeviceType = "waveaudio";
int MCIERRret = 0;
// Could be changed to MCI_OPEN | MCI_WAIT to ensure the wave device has finished opening before continuing...
if (MCIERRret = mciSendCommand(0, MCI_OPEN, MCI_OPEN_TYPE, (DWORD)(LPVOID) &mciOpenParms)){
// Failed to open wave device.
MAGIC_DEVICEID = 48879;
dprintf("Failed to open wave device! Using 0xBEEF as cdaudio id.\r\n");
}
else{
MAGIC_DEVICEID = mciOpenParms.wDeviceID;
dprintf("Wave device opened succesfully using cdaudio ID %d for emulation.\r\n",MAGIC_DEVICEID);
}
}
int bACCSeekOFF = GetPrivateProfileInt("winmm", "ACCSeekOFF", 0, ".\\winmm.ini");
if(bACCSeekOFF) ACCSeekOFF = 1;
int bFullNotify = GetPrivateProfileInt("winmm", "FullNotify", 0, ".\\winmm.ini");
if(bFullNotify) FullNotify = 1;
//End of read winmm.ini options...
//Do the other stuff:
GetModuleFileName(hModule, music_path, sizeof music_path);
memset(tracks, 0, sizeof tracks);
char *last = strrchr(music_path, '\\');
if (last)
{
*last = '\0';
}
strncat(music_path, "\\MUSIC", sizeof music_path - 1);
dprintf("ogg-winmm music directory is %s\r\n", music_path);
dprintf("ogg-winmm searching tracks...\r\n");
unsigned int position = 0;
for (int i = 1; i < MAX_TRACKS; i++) /* "Changed: int i = 0" to "1" we can skip track00.ogg" */
{
snprintf(tracks[i].path, sizeof tracks[i].path, "%s\\Track%02d.ogg", music_path, i);
tracks[i].length = plr_length(tracks[i].path);
tracks[i].position = position + 2; //2 second pre-gap
if (tracks[i].length < 4)
{
tracks[i].path[0] = '\0';
//position += 4; /* missing tracks are 4 second data tracks for us */
}
else
{
if (firstTrack == -1)
{
firstTrack = i;
}
if(i == numTracks) numTracks -= 1; /* Take into account pure music cd's starting with track01.ogg */
dprintf("Track %02d: %02d:%02d @ %d seconds\r\n", i, tracks[i].length / 60, tracks[i].length % 60, tracks[i].position);
numTracks++;
lastTrack = i;
position += tracks[i].length;
}
}
dprintf("Emulating total of %d CD tracks.\r\n\r\n", numTracks);
return 0;
}
// The less done inside DllMain the better...
// https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-best-practices
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
if (fdwReason == DLL_PROCESS_ATTACH){
hModule = hinstDLL;
//Moved initialization stuff to its own thread to avoid issues...
initialize = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)initialize_main, NULL, 0, NULL);
}
if (fdwReason == DLL_PROCESS_DETACH){
if (fh)
{
fclose(fh);
fh = NULL;
}
}
return TRUE;
}
/* MCI commands */
/* https://docs.microsoft.com/windows/win32/multimedia/multimedia-commands */
MCIERROR WINAPI fake_mciSendCommandA(MCIDEVICEID IDDevice, UINT uMsg, DWORD_PTR fdwCommand, DWORD_PTR dwParam)
{
char cmdbuf[1024];
dprintf("mciSendCommandA(IDDevice=%p, uMsg=%p, fdwCommand=%p, dwParam=%p)\r\n", IDDevice, uMsg, fdwCommand, dwParam);
if (uMsg == MCI_OPEN)
{
LPMCI_OPEN_PARMS parms = (LPVOID)dwParam;
dprintf(" MCI_OPEN\r\n");
if (fdwCommand & MCI_OPEN_ALIAS)
{
dprintf(" MCI_OPEN_ALIAS\r\n");
dprintf(" -> %s\r\n", parms->lpstrAlias);
}
if (fdwCommand & MCI_OPEN_SHAREABLE)
{
dprintf(" MCI_OPEN_SHAREABLE\r\n");
}
if (fdwCommand & MCI_OPEN_TYPE_ID)
{
dprintf(" MCI_OPEN_TYPE_ID\r\n");
if (LOWORD(parms->lpstrDeviceType) == MCI_DEVTYPE_CD_AUDIO)
{
dprintf(" Returning magic device id %d for MCI_DEVTYPE_CD_AUDIO\r\n", MAGIC_DEVICEID);
parms->wDeviceID = MAGIC_DEVICEID;
if (fdwCommand & MCI_NOTIFY)
{
if (FullNotify && !opened){
dprintf(" MCI_NOTIFY\r\n");
dprintf(" Sending MCI_NOTIFY_SUCCESSFUL message...\r\n");
SendMessageA((HWND)0xffff, MM_MCINOTIFY, MCI_NOTIFY_SUCCESSFUL, MAGIC_DEVICEID);
Sleep(50);
}
}
opened = 1;
return 0;
}
else return relay_mciSendCommandA(IDDevice, uMsg, fdwCommand, dwParam); /* Added MCI relay */
}
if (fdwCommand & MCI_OPEN_TYPE && !(fdwCommand & MCI_OPEN_TYPE_ID))
{
dprintf(" MCI_OPEN_TYPE\r\n");
dprintf(" -> %s\r\n", parms->lpstrDeviceType);
/* copy alias to buffer */
char cmpaliasbuf[1024];
strcpy (cmpaliasbuf,parms->lpstrDeviceType);
/* change cmpaliasbuf into lower case */
for (int i = 0; cmpaliasbuf[i]; i++)
{
cmpaliasbuf[i] = tolower(cmpaliasbuf[i]);
}
if (strcmp(cmpaliasbuf, "cdaudio") == 0)
{
dprintf(" Returning magic device id %d for MCI_DEVTYPE_CD_AUDIO\r\n", MAGIC_DEVICEID);
parms->wDeviceID = MAGIC_DEVICEID;
if (fdwCommand & MCI_NOTIFY)
{
if (FullNotify && !opened){
dprintf(" MCI_NOTIFY\r\n");
dprintf(" Sending MCI_NOTIFY_SUCCESSFUL message...\r\n");
SendMessageA((HWND)0xffff, MM_MCINOTIFY, MCI_NOTIFY_SUCCESSFUL, MAGIC_DEVICEID);
Sleep(50);
}
}
opened = 1;
return 0;
}
else return relay_mciSendCommandA(IDDevice, uMsg, fdwCommand, dwParam); /* Added MCI relay */
}
}
if (IDDevice == MAGIC_DEVICEID || IDDevice == 0 || IDDevice == 0xFFFFFFFF)
{
if (fdwCommand & MCI_WAIT)
{
dprintf(" MCI_WAIT\r\n");
}
if (uMsg == MCI_GETDEVCAPS)
{
LPMCI_GETDEVCAPS_PARMS parms = (LPVOID)dwParam;
dprintf(" MCI_GETDEVCAPS\r\n");
parms->dwReturn = 0;
if (fdwCommand & MCI_GETDEVCAPS_ITEM)
{
dprintf(" MCI_GETDEVCAPS_ITEM\r\n");
if (parms->dwItem == MCI_GETDEVCAPS_CAN_PLAY || parms->dwItem == MCI_GETDEVCAPS_CAN_EJECT || parms->dwItem == MCI_GETDEVCAPS_HAS_AUDIO)
{
parms->dwReturn = TRUE;
}
else if (parms->dwItem == MCI_GETDEVCAPS_DEVICE_TYPE)
{
parms->dwReturn = MCI_DEVTYPE_CD_AUDIO;
}
else
{
parms->dwReturn = FALSE;
}
}
if (fdwCommand & MCI_NOTIFY)
{
if (FullNotify && opened){
dprintf(" MCI_NOTIFY\r\n");
notify = 0;
dprintf(" Sending MCI_NOTIFY_SUCCESSFUL message...\r\n");
// Note that MCI_NOTIFY_SUPERSEDED would be sent before MCI_NOTIFY_SUCCESSFUL if track was playing, but this is not emulated.
SendMessageA((HWND)0xffff, MM_MCINOTIFY, MCI_NOTIFY_SUCCESSFUL, MAGIC_DEVICEID);
Sleep(50);
}
}
}
if (uMsg == MCI_SET)
{
LPMCI_SET_PARMS parms = (LPVOID)dwParam;
dprintf(" MCI_SET\r\n");
if (fdwCommand & MCI_SET_TIME_FORMAT)
{
dprintf(" MCI_SET_TIME_FORMAT\r\n");
time_format = parms->dwTimeFormat;
if (parms->dwTimeFormat == MCI_FORMAT_BYTES)
{
dprintf(" MCI_FORMAT_BYTES\r\n");
}
if (parms->dwTimeFormat == MCI_FORMAT_FRAMES)
{
dprintf(" MCI_FORMAT_FRAMES\r\n");
}
if (parms->dwTimeFormat == MCI_FORMAT_HMS)
{
dprintf(" MCI_FORMAT_HMS\r\n");
}
if (parms->dwTimeFormat == MCI_FORMAT_MILLISECONDS)
{
dprintf(" MCI_FORMAT_MILLISECONDS\r\n");
}
if (parms->dwTimeFormat == MCI_FORMAT_MSF)
{
dprintf(" MCI_FORMAT_MSF\r\n");
}
if (parms->dwTimeFormat == MCI_FORMAT_SAMPLES)
{
dprintf(" MCI_FORMAT_SAMPLES\r\n");
}
if (parms->dwTimeFormat == MCI_FORMAT_TMSF)
{
dprintf(" MCI_FORMAT_TMSF\r\n");
}
}
if (fdwCommand & MCI_SET_AUDIO)
{
if (parms->dwAudio == MCI_SET_AUDIO_ALL)
{
dprintf(" MCI_SET_AUDIO_ALL\r\n");
if (fdwCommand & MCI_SET_ON) plr_volume(100);
if (fdwCommand & MCI_SET_OFF) plr_volume(0);
}
if (parms->dwAudio == MCI_SET_AUDIO_LEFT)
{
dprintf(" MCI_SET_AUDIO_LEFT\r\n");
}
if (parms->dwAudio == MCI_SET_AUDIO_RIGHT)
{
dprintf(" MCI_SET_AUDIO_RIGHT\r\n");
}
}
if (fdwCommand & MCI_NOTIFY)
{
if (FullNotify && opened){
dprintf(" MCI_NOTIFY\r\n");
notify = 0;
dprintf(" Sending MCI_NOTIFY_SUCCESSFUL message...\r\n");
// Note that MCI_NOTIFY_SUPERSEDED would be sent before MCI_NOTIFY_SUCCESSFUL if track was playing, but this is not emulated.
SendMessageA((HWND)0xffff, MM_MCINOTIFY, MCI_NOTIFY_SUCCESSFUL, MAGIC_DEVICEID);
Sleep(50);
}
}
}
// MCI_SEEK implementation. Note that seeking stops playback. MCI_PLAY NULL or MCI_PLAY+MCI_TO starts from seeked position...
if (uMsg == MCI_SEEK)
{
if(notify){
notify = 0;
dprintf(" Sending MCI_NOTIFY_ABORTED message...\r\n");
SendMessageA((HWND)0xffff, MM_MCINOTIFY, MCI_NOTIFY_ABORTED, MAGIC_DEVICEID);
}
LPMCI_SEEK_PARMS parms = (LPVOID)dwParam;
dprintf(" MCI_SEEK\r\n");
if (fdwCommand & MCI_SEEK_TO_START)
{
dprintf(" Seek to firstTrack %d\r\n",firstTrack);
current = info.first = firstTrack;
info.last = lastTrack;
plr_stop();
playing = 0;
plrpos = 0;
paused = 0;
}
if (fdwCommand & MCI_SEEK_TO_END)
{
dprintf(" Seek to end of disc\r\n");
// Not very useful as a real disc can not play from this position
plr_stop();
playing = 0;
plrpos = 0;
paused = 0;
}
if (fdwCommand & MCI_TO)
{
dprintf(" dwTo: %d\r\n", parms->dwTo);
if (time_format == MCI_FORMAT_TMSF)
{
current = info.first = MCI_TMSF_TRACK(parms->dwTo);
info.last = lastTrack;
dprintf(" TRACK %d\n", MCI_TMSF_TRACK(parms->dwTo));
dprintf(" MINUTE %d\n", MCI_TMSF_MINUTE(parms->dwTo));
dprintf(" SECOND %d\n", MCI_TMSF_SECOND(parms->dwTo));
dprintf(" FRAME %d\n", MCI_TMSF_FRAME(parms->dwTo));
int msf_min = MCI_TMSF_MINUTE(parms->dwTo) * 60;
int msf_sec = MCI_TMSF_SECOND(parms->dwTo);
if((!ACCSeekOFF && msf_min != 0) || (!ACCSeekOFF && msf_sec != 0)){
seek = 1;
plrpos = msf_min+msf_sec;
dprintf("seek to plrpos %d\n",plrpos);
paused = 1;
}
else{
paused = 0;
plrpos = 0;
}
}
else if (time_format == MCI_FORMAT_MILLISECONDS)
{
int target = (parms->dwTo / 1000)+1; //+1 needed for matching logic
int i = firstTrack;
int match = 0, comp_a = 0, comp_b = 0;
// Find the match in a range:
while(i < numTracks+1){
comp_a = abs(tracks[i].position);
comp_b = abs(tracks[i].position + tracks[i].length);
if((target - comp_a)*(target - comp_b) <= 0){
match=i;
break;
}
i++;
}
current = info.first = match+1;
info.last = lastTrack;
if(!ACCSeekOFF && parms->dwTo / 1000 != tracks[match].position){
seek = 1;
plrpos = (parms->dwTo / 1000) - tracks[match].position;
dprintf("seek to plrpos %d\n",plrpos);
paused = 1;
}
else{
paused = 0;
plrpos = 0;
}
dprintf(" mapped milliseconds to %d\n", info.last);
}
else // MCI_FORMAT_MSF
{
dprintf(" MINUTE %d\n", MCI_MSF_MINUTE(parms->dwTo));
dprintf(" SECOND %d\n", MCI_MSF_SECOND(parms->dwTo));
dprintf(" FRAME %d\n", MCI_MSF_FRAME(parms->dwTo));
// Convert minutes and seconds to milliseconds (ignore frames)
int msf_min = MCI_MSF_MINUTE(parms->dwTo) * 60;
int msf_sec = MCI_MSF_SECOND(parms->dwTo);
int target = msf_min + msf_sec +1; //+1 needed for matching logic
int i = firstTrack;
int match = 0, comp_a = 0, comp_b = 0;
// Find the match in a range:
while(i < numTracks+1){
comp_a = abs(tracks[i].position);
comp_b = abs(tracks[i].position + tracks[i].length);
if((target - comp_a)*(target - comp_b) <= 0){
match=i;
break;
}
i++;
}
current = info.first = match;
info.last = lastTrack;
if(!ACCSeekOFF && msf_min+msf_sec != tracks[match].position){
seek = 1;
plrpos = (msf_min + msf_sec) - tracks[match].position;
dprintf("seek to plrpos %d\n",plrpos);
paused = 1;
}
else{
paused = 0;
plrpos = 0;
}
}
if(playing)plr_stop();
playing = 0;
}
if ((fdwCommand & MCI_NOTIFY) || sendStringNotify)
{
sendStringNotify = 0;
if (FullNotify && opened){
dprintf(" MCI_NOTIFY\r\n");
dprintf(" Sending MCI_NOTIFY_SUCCESSFUL message...\r\n");
SendMessageA((HWND)0xffff, MM_MCINOTIFY, MCI_NOTIFY_SUCCESSFUL, MAGIC_DEVICEID);
Sleep(50);
}
}
}
if (uMsg == MCI_CLOSE)
{
dprintf(" MCI_CLOSE\r\n");
time_format = MCI_FORMAT_MSF;
if (fdwCommand & MCI_NOTIFY)
{
if (FullNotify && opened){
notify = 0;
dprintf(" MCI_NOTIFY\r\n");
dprintf(" Sending MCI_NOTIFY_SUCCESSFUL message...\r\n");
SendMessageA((HWND)0xffff, MM_MCINOTIFY, MCI_NOTIFY_SUCCESSFUL, MAGIC_DEVICEID);
Sleep(50);
}
}
opened = 0;
/* NOTE: MCI_CLOSE does stop the music in Vista+ but the original behaviour did not
it only closed the handle to the opened device. You could still send MCI commands
to a default cdaudio device but if you had used an alias you needed to re-open it.
In addition WinXP had a bug where after MCI_CLOSE the device would be unresponsive. */
}
if (uMsg == MCI_PLAY)
{
if(notify){
notify = 0;
dprintf(" Sending MCI_NOTIFY_ABORTED message...\r\n");
SendMessageA((HWND)0xffff, MM_MCINOTIFY, MCI_NOTIFY_ABORTED, MAGIC_DEVICEID);
}
LPMCI_PLAY_PARMS parms = (LPVOID)dwParam;
int ignore = 0; // To deal with MCI_PLAY NULL while track is playing
if(playing) ignore = 1;
if(!playing)info.last = lastTrack+1; /* default MCI_TO */
if(!playing)info.first = current; // default MCI_FROM
plrpos2 = -1;
dprintf(" MCI_PLAY\r\n");
if ((fdwCommand & MCI_NOTIFY) || sendStringNotify)
{
dprintf(" MCI_NOTIFY\r\n");
notify = 1; /* storing the notify request */
sendStringNotify = 0;
}
if (fdwCommand & MCI_FROM)
{
dprintf(" dwFrom: %d\r\n", parms->dwFrom);
if (time_format == MCI_FORMAT_TMSF)
{
info.first = MCI_TMSF_TRACK(parms->dwFrom);
dprintf(" TRACK %d\n", MCI_TMSF_TRACK(parms->dwFrom));
dprintf(" MINUTE %d\n", MCI_TMSF_MINUTE(parms->dwFrom));
dprintf(" SECOND %d\n", MCI_TMSF_SECOND(parms->dwFrom));
dprintf(" FRAME %d\n", MCI_TMSF_FRAME(parms->dwFrom));
int msf_min = MCI_TMSF_MINUTE(parms->dwFrom) * 60;
int msf_sec = MCI_TMSF_SECOND(parms->dwFrom);
//If minutes or seconds are not zero -> seek
if((!ACCSeekOFF && msf_min != 0) || (!ACCSeekOFF && msf_sec != 0)){
seek = 1;
plrpos = msf_min+msf_sec;
dprintf("seek to plrpos %d\n",plrpos);
}
}
else if (time_format == MCI_FORMAT_MILLISECONDS)
{
info.first = 0;
int target = (parms->dwFrom / 1000)+1; //+1 needed for matching logic
int i = firstTrack;
int match = 0, comp_a = 0, comp_b = 0;
// Find the match in a range:
while(i < numTracks+1){
comp_a = abs(tracks[i].position);
comp_b = abs(tracks[i].position + tracks[i].length);
if((target - comp_a)*(target - comp_b) <= 0){
match=i;
break;
}
i++;
}
info.first = match;
dprintf("match from track: %d\n",match);
//If mci_from does not match track starting position seek to it.
if(!ACCSeekOFF && parms->dwFrom / 1000 != tracks[match].position){
seek = 1;
plrpos = (parms->dwFrom / 1000) - tracks[match].position;
dprintf("seek to plrpos %d\n",plrpos);
}
dprintf(" mapped milliseconds from %d\n", info.first);
}
else // MCI_FORMAT_MSF
{
dprintf(" MINUTE %d\n", MCI_MSF_MINUTE(parms->dwFrom));
dprintf(" SECOND %d\n", MCI_MSF_SECOND(parms->dwFrom));
dprintf(" FRAME %d\n", MCI_MSF_FRAME(parms->dwFrom));
info.first = 0;
int msf_min = MCI_MSF_MINUTE(parms->dwFrom) * 60;
int msf_sec = MCI_MSF_SECOND(parms->dwFrom);
int target = msf_min + msf_sec +1; //+1 needed for matching logic
int i = firstTrack;
int match = 0, comp_a = 0, comp_b = 0;
// Find the match in a range:
while(i < numTracks+1){
comp_a = abs(tracks[i].position);
comp_b = abs(tracks[i].position + tracks[i].length);
if((target - comp_a)*(target - comp_b) <= 0){
match=i;
break;
}
i++;
}
info.first = match;
dprintf("match from track: %d\n",match);
//If mci_from does not match track starting position seek to it.
if(!ACCSeekOFF && msf_min+msf_sec != tracks[match].position){
seek = 1;
plrpos = (msf_min + msf_sec) - tracks[match].position;
dprintf("seek to plrpos %d\n",plrpos);
}
}
if (info.first < firstTrack){
if (info.first == 0)return MCIERR_OUTOFRANGE; //Hunter Hunted tries to play from track 0 and expects a failure as would happen with a real mcicda device.
else info.first = firstTrack;
}
if (info.first > lastTrack)
info.first = lastTrack;
paused = 0;
info.last = lastTrack+1; /* default MCI_TO */
ignore = 0;
}
if (fdwCommand & MCI_TO)
{
dprintf(" dwTo: %d\r\n", parms->dwTo);
if (time_format == MCI_FORMAT_TMSF)
{
info.last = MCI_TMSF_TRACK(parms->dwTo);
dprintf(" TRACK %d\n", MCI_TMSF_TRACK(parms->dwTo));
dprintf(" MINUTE %d\n", MCI_TMSF_MINUTE(parms->dwTo));
dprintf(" SECOND %d\n", MCI_TMSF_SECOND(parms->dwTo));
dprintf(" FRAME %d\n", MCI_TMSF_FRAME(parms->dwTo));
int msf_min = MCI_TMSF_MINUTE(parms->dwTo) * 60;
int msf_sec = MCI_TMSF_SECOND(parms->dwTo);
//If minutes or seconds are not zero add to end pos
if((!ACCSeekOFF && msf_min != 0) || (!ACCSeekOFF && msf_sec != 0)){
if(info.last != info.first)info.last = MCI_TMSF_TRACK(parms->dwTo)+1;
plrpos2 = msf_min+msf_sec;
dprintf("seek to plrpos2 %d\n",plrpos2);
}
}
else if (time_format == MCI_FORMAT_MILLISECONDS)
{
info.last = info.first;
int target = (parms->dwTo / 1000);
int i = firstTrack;
int match = 0, comp_a = 0, comp_b = 0;
// Find the match in a range:
while(i < numTracks+1){
comp_a = abs(tracks[i].position);
comp_b = abs(tracks[i].position + tracks[i].length);
if((target - comp_a)*(target - comp_b) <= 0){
match=i;
break;
}
i++;
}
info.last = match;
if(info.last != info.first)info.last = match+1;
dprintf("match to track: %d\n",match);
//If mci_to does not match track start store the end as plrpos2
if(!ACCSeekOFF && parms->dwTo / 1000 != tracks[match].position){
plrpos2 = (parms->dwTo / 1000) - tracks[match].position;
dprintf("seek to plrpos2 %d\n",plrpos2);
}
dprintf(" mapped milliseconds to %d\n", info.last);
}
else // MCI_FORMAT_MSF
{
dprintf(" MINUTE %d\n", MCI_MSF_MINUTE(parms->dwTo));
dprintf(" SECOND %d\n", MCI_MSF_SECOND(parms->dwTo));
dprintf(" FRAME %d\n", MCI_MSF_FRAME(parms->dwTo));
info.last = info.first;
// Convert minutes and seconds to milliseconds (ignore frames)
int msf_min = MCI_MSF_MINUTE(parms->dwTo) * 60;
int msf_sec = MCI_MSF_SECOND(parms->dwTo);
int target = msf_min + msf_sec;
int i = firstTrack;
int match = 0, comp_a = 0, comp_b = 0;
// Find the match in a range:
while(i < numTracks+1){
comp_a = abs(tracks[i].position);
comp_b = abs(tracks[i].position + tracks[i].length);
if((target - comp_a)*(target - comp_b) <= 0){
match=i;
break;
}
i++;
}
info.last = match;
if(info.last != info.first)info.last = match+1;
dprintf("match to track: %d\n",match);
//If mci_to does not match track start store the end as plrpos2
if(!ACCSeekOFF && msf_min+msf_sec != tracks[match].position){
plrpos2 = (msf_min + msf_sec) - tracks[match].position;
dprintf("end at plrpos2 %d\n",plrpos2);
}
}
if (info.last < info.first)
info.last = info.first;
if (info.last > lastTrack)
info.last = lastTrack;
if(ignore){
seek = 1;
plrpos = plr_tell();
ignore = 0;
}
}
if(!ignore){
if (player)
{
TerminateThread(player, 0);
}
playing = 1;
player = CreateThread(NULL, 100000, (LPTHREAD_START_ROUTINE)player_main, (void *)&info, 0, NULL);
}
}
// MCI_STOP and MCI_PAUSE do the same on win9x
if (uMsg == MCI_PAUSE || uMsg == MCI_STOP)
{
if(uMsg == MCI_STOP)dprintf(" MCI_STOP\r\n");
if(uMsg == MCI_PAUSE)dprintf(" MCI_PAUSE\r\n");
if(playing){
plrpos = plr_tell(); // save current position of ogg player
dprintf("stop/pause plrpos %d\n",plrpos);
paused = 1;
}
playing = 0;
plr_stop();
if(notify){
notify = 0;
dprintf(" Sending MCI_NOTIFY_ABORTED message...\r\n");
SendMessageA((HWND)0xffff, MM_MCINOTIFY, MCI_NOTIFY_ABORTED, MAGIC_DEVICEID);
}
if ((fdwCommand & MCI_NOTIFY) || sendStringNotify)
{
sendStringNotify = 0;
if (FullNotify && opened){
dprintf(" MCI_NOTIFY\r\n");
dprintf(" Sending MCI_NOTIFY_SUCCESSFUL message...\r\n");
SendMessageA((HWND)0xffff, MM_MCINOTIFY, MCI_NOTIFY_SUCCESSFUL, MAGIC_DEVICEID);
Sleep(50);
}
}
}
if (uMsg == MCI_INFO)
{
dprintf(" MCI_INFO\n");
LPMCI_INFO_PARMS parms = (LPVOID)dwParam;
if(fdwCommand & MCI_INFO_PRODUCT)
{
dprintf(" MCI_INFO_PRODUCT\n");
memcpy((LPVOID)(parms->lpstrReturn), (LPVOID)&"CD Audio", 9);
dprintf(" Return: %s\r\n", parms->lpstrReturn);
}
if(fdwCommand & MCI_INFO_MEDIA_IDENTITY)
{
dprintf(" MCI_INFO_MEDIA_IDENTITY\n");
memcpy((LPVOID)(parms->lpstrReturn), (LPVOID)&"12345678", 9);
dprintf(" Return: %s\r\n", parms->lpstrReturn);
}
if (fdwCommand & MCI_NOTIFY)
{
if (FullNotify && opened){
dprintf(" MCI_NOTIFY\r\n");
notify = 0;
dprintf(" Sending MCI_NOTIFY_SUCCESSFUL message...\r\n");
// Note that MCI_NOTIFY_SUPERSEDED would be sent before MCI_NOTIFY_SUCCESSFUL if track was playing, but this is not emulated.
SendMessageA((HWND)0xffff, MM_MCINOTIFY, MCI_NOTIFY_SUCCESSFUL, MAGIC_DEVICEID);
Sleep(50);
}
}
}
/* Handling of MCI_SYSINFO (Heavy Gear, Battlezone2, Interstate 76) */
if (uMsg == MCI_SYSINFO)
{
dprintf(" MCI_SYSINFO\r\n");
LPMCI_SYSINFO_PARMSA parms = (LPVOID)dwParam;
if(fdwCommand & MCI_SYSINFO_QUANTITY)
{
dprintf(" MCI_SYSINFO_QUANTITY\r\n");
memcpy((LPVOID)(parms->lpstrReturn), (LPVOID)&"1", 2); /* quantity = 1 */
//parms->dwRetSize = sizeof(DWORD);
//parms->dwNumber = MAGIC_DEVICEID;
dprintf(" Return: %s\r\n", parms->lpstrReturn);
}
if(fdwCommand & MCI_SYSINFO_NAME || fdwCommand & MCI_SYSINFO_INSTALLNAME)
{
dprintf(" MCI_SYSINFO_NAME\r\n");
memcpy((LPVOID)(parms->lpstrReturn), (LPVOID)&"cdaudio", 8); /* name = cdaudio */
//parms->dwRetSize = sizeof(DWORD);
//parms->dwNumber = MAGIC_DEVICEID;
dprintf(" Return: %s\r\n", parms->lpstrReturn);
}
}
if (uMsg == MCI_STATUS)
{
LPMCI_STATUS_PARMS parms = (LPVOID)dwParam;
dprintf(" MCI_STATUS\r\n");
parms->dwReturn = 0;
if (fdwCommand & MCI_TRACK)
{
dprintf(" MCI_TRACK\r\n");
dprintf(" dwTrack = %d\r\n", parms->dwTrack);
}
if (fdwCommand & MCI_STATUS_ITEM)
{
dprintf(" MCI_STATUS_ITEM\r\n");
if (parms->dwItem == MCI_STATUS_CURRENT_TRACK)
{
dprintf(" MCI_STATUS_CURRENT_TRACK\r\n");
parms->dwReturn = current;
}
if (parms->dwItem == MCI_STATUS_LENGTH)
{
dprintf(" MCI_STATUS_LENGTH\r\n");
/* Get track length */
if(fdwCommand & MCI_TRACK)
{
int seconds = tracks[parms->dwTrack].length;
if (time_format == MCI_FORMAT_MILLISECONDS)
{
parms->dwReturn = seconds * 1000;
}
else
{
parms->dwReturn = MCI_MAKE_MSF(seconds / 60, seconds % 60, 0);
}
}
/* Get full length */
else
{
if (time_format == MCI_FORMAT_MILLISECONDS)
{
parms->dwReturn = (tracks[lastTrack].position + tracks[lastTrack].length) * 1000;
}
else
{
int seconds = 0;
int countTracks = 1;
while(countTracks < numTracks){
seconds += tracks[countTracks].length;
countTracks++;
}
parms->dwReturn = MCI_MAKE_MSF(seconds / 60, seconds % 60, 0);
}
}
}
if (parms->dwItem == MCI_CDA_STATUS_TYPE_TRACK)
{
dprintf(" MCI_CDA_STATUS_TYPE_TRACK\r\n");