1 /**************************************************************************/
2 /*                                                                        */
3 /*             'Hu-Go! Compact disk Definition' handling code             */
4 /*                                                                        */
5 /* This is 'copyleft'. Use and abuse it as you want.                      */
6 /*                                                                        */
7 /**************************************************************************/
8 
9 #include "iniconfig.h"
10 #include "utils.h"
11 #include "hcd.h"
12 
13 UInt32 HCD_first_track;
14 
15 UInt32 HCD_last_track;
16 
17 char HCD_cover_filename[256] = "";
18 
19 FILE *HCD_iso_FILE = 0;
20 
21 #ifdef ALLEGRO
22 PACKFILE *HCD_packed_iso_FILE = 0;
23 #endif
24 
25 UInt32 HCD_current_subtitle = 0;
26 
27 UInt32 HCD_frame_at_beginning_of_track = 0;
28 
29 UChar HCD_current_played_track = 0;
30 
31 int
fill_HCD_info(char * name)32 fill_HCD_info (char *name)
33 {
34 
35 #if defined(ALLEGRO) || defined(SDL_mixer)
36 
37   int current_track;
38 
39 
40 
41   char *MP3_path, *ISO_path;
42 
43 
44 
45   char MP3_path_tmp[256], ISO_path_tmp[256], cw_dir[256];
46 
47 
48 
49   set_config_file (name);
50 
51 
52   init_config();
53   HCD_first_track = get_config_int ("main", "first_track", 1);
54 
55 
56 
57   HCD_last_track = get_config_int ("main", "last_track", 22);
58 
59 
60 
61   current_track = get_config_int ("main", "minimum_bios", -1);
62 
63 
64 
65   MP3_path = get_config_string ("main", "MP3_path", "");
66 
67 
68   if (!strcmp (MP3_path, ""))
69 
70     {
71 
72 #ifdef ALLEGRO
73       fix_filename_path (MP3_path_tmp, name, 256);
74 #else
75 		memset(MP3_path_tmp,0,256);
76 		memset(cw_dir,0,256);
77 		strcpy (MP3_path_tmp, name);
78 		//Log("MP3_path_tmp:%s#\n",MP3_path_tmp);
79 		//if filepath to .hcd is relative...?
80 		// getcwd +name = gives the full path the hcd
81 		//1. MP3_path_tmp==name          => current dir + MP3_path_tmp=.
82 		//2. MP3_path begins with '.'    =>
83 #endif
84 //      {
85 // because dirname is GNU C only ?
86 	int i;
87 
88 	for (i = 0; MP3_path_tmp[i]; i++)
89 
90 	  if (MP3_path_tmp[i] == '\\')
91 
92 	    MP3_path_tmp[i] = '/';
93 
94 
95 
96 //      }
97 
98 
99 
100       if (strrchr (MP3_path_tmp, '/'))
101 
102 	*(strrchr (MP3_path_tmp, '/') + 1) = 0;
103 
104 
105 
106 	if ((strcmp(MP3_path_tmp,name)==0)||(strncmp (MP3_path_tmp, ".",1)==0))
107 		{
108 		if (strcmp(MP3_path_tmp,name)==0)
109 #if defined(MSDOS)||defined(WIN32)||defined(DJGPP)
110 						 strcpy(MP3_path_tmp,"\\");
111 #else
112 						 strcpy(MP3_path_tmp,"/");
113 #endif
114 		getcwd(cw_dir,256);
115 		//Log("%s\n", cw_dir);
116 		strncat(cw_dir,MP3_path_tmp,256);
117 		MP3_path = cw_dir;
118 		//Log("%s\n", MP3_path);
119 		}else{
120 		MP3_path = MP3_path_tmp;
121 		}
122 
123     }else{
124 		strcpy (MP3_path_tmp, MP3_path);
125 		MP3_path = MP3_path_tmp;
126 	}
127 
128 
129 
130 
131   ISO_path = get_config_string ("main", "ISO_path", "");
132 
133 
134 
135 
136 
137   if (!strcmp (ISO_path, ""))
138 
139     {
140 
141 
142 #ifdef ALLEGRO
143       fix_filename_path (ISO_path_tmp, name, 256);
144 #else
145 		memset(ISO_path_tmp,0,256);
146 		memset(cw_dir,0,256);
147 		strcpy(ISO_path_tmp, name);
148 #endif
149 
150 
151 //      {
152 
153 	int i;
154 
155 	for (i = 0; ISO_path_tmp[i]; i++)
156 
157 	  if (ISO_path_tmp[i] == '\\')
158 
159 	    ISO_path_tmp[i] = '/';
160 
161 
162 
163 //      }
164 
165 
166 
167       if (strrchr (ISO_path_tmp, '/'))
168 
169 	*(strrchr (ISO_path_tmp, '/') + 1) = 0;
170 
171 
172 	if ((strcmp(ISO_path_tmp,name)==0)||(strncmp (ISO_path_tmp, ".",1)==0))
173 		{
174 		if (strcmp(ISO_path_tmp,name)==0)
175 #if defined(MSDOS)||defined(WIN32)||defined(DJGPP)
176 						 strcpy(ISO_path_tmp,"\\");
177 #else
178 						 strcpy(ISO_path_tmp,"/");
179 #endif
180 		getcwd(cw_dir,256);
181 		//Log("%s\n", cw_dir);
182 		strncat(cw_dir,ISO_path_tmp,256);
183 		ISO_path = cw_dir;
184 		//Log("%s\n", ISO_path);
185 		}else{
186 		ISO_path = ISO_path_tmp;
187 		}
188 
189 
190 
191 
192     }else{
193 		strcpy(ISO_path_tmp, ISO_path);
194 		ISO_path = ISO_path_tmp;
195 	}
196 
197 
198 
199   if (current_track != -1)
200 
201     minimum_bios_hooking = current_track;
202 
203 
204 
205   for (current_track = HCD_first_track;
206 
207        current_track <= HCD_last_track; current_track++)
208 
209     {
210 
211       char *section_name = (char *) alloca (100);
212 
213       char *tmp_buf = (char *) alloca (100);
214 
215 
216 
217       sprintf (section_name, "track%d", current_track);
218 
219 
220 
221       //----- Init patch section -----
222 
223 
224 
225       CD_track[current_track].patch_number = 0;
226 
227 
228 
229       CD_track[current_track].patch = NULL;
230 
231 
232 
233       //----- find beginning ---------
234 
235 
236 
237       strcpy (tmp_buf,
238 
239 	      strupr (get_config_string (section_name, "begin", "")));
240 
241 
242 
243       if (strcasestr (tmp_buf, "MSF"))
244 
245 	{
246 
247 
248 
249 	  int min = (tmp_buf[4] - '0') * 10 + (tmp_buf[5] - '0');
250 
251 	  int sec = (tmp_buf[7] - '0') * 10 + (tmp_buf[8] - '0');
252 
253 	  int fra = (tmp_buf[10] - '0') * 10 + (tmp_buf[11] - '0');
254 
255 
256 
257 	  CD_track[current_track].beg_lsn = fra + 75 * (sec + 60 * min);
258 
259 
260 
261 	  CD_track[current_track].beg_lsn -= 150;
262 
263 
264 
265 	}
266 
267       else if (strcasestr (tmp_buf, "LSN"))
268 
269 	{
270 
271 
272 
273 	  CD_track[current_track].beg_lsn = atoi (&tmp_buf[4]) - 150;
274 
275 
276 
277 	}
278 
279       else if (strcasestr (tmp_buf, "LBA"))
280 
281 	{
282 
283 
284 
285 	  CD_track[current_track].beg_lsn = atoi (&tmp_buf[4]);
286 
287 
288 
289 	}
290 
291       else
292 
293 	CD_track[current_track].beg_lsn = 0;
294 
295 
296 
297       //----- determine type ---------
298 
299 
300 
301       strcpy (tmp_buf,
302 
303 	      strupr (get_config_string (section_name, "type", "AUDIO")));
304 
305 
306 
307       if (strcmp (tmp_buf, "CODE") == 0)
308 
309 	{
310 
311 
312 
313 	  //----- track is code ---------
314 
315 
316 
317 	  CD_track[current_track].type = 4;	// Code track
318 
319 
320 
321 	  //----- emulated track will use a regular file ---------
322 
323 
324 
325 	  CD_track[current_track].source_type = HCD_SOURCE_REGULAR_FILE;
326 
327 
328 
329 	  //----- search filename of ISO ---------
330 
331 
332 
333 	  strcpy (tmp_buf, ISO_path);
334 
335 
336 
337 	  strcat (tmp_buf, get_config_string (section_name, "filename", ""));
338 
339 
340 
341 	  strcpy (CD_track[current_track].filename, tmp_buf);
342 
343 
344 #ifdef ALLEGRO
345 	  if (!exists (tmp_buf))
346 	  {
347 
348 	      Log ("Missing ISO file : %s\nUsed to emulate track %d.",
349 
350 		   tmp_buf, current_track);
351 
352 	      return 0;
353 
354 	    }
355 #elif defined (SDL_mixer) && defined (LINUX)
356 		struct stat rom_file_buf;
357 
358 	  if (stat(tmp_buf, &rom_file_buf) != 0)
359 	  {
360 				Log("Error : %X when stating %s\n", errno, tmp_buf);
361 		  return 0;
362 	  }
363 #endif
364 
365 
366 
367 	  //----- determine file length of track ---------
368 
369 
370 
371 	  CD_track[current_track].length = file_size (tmp_buf) / 2048;
372 
373 
374 
375 	  //----- search for patch -----------------------
376 
377 
378 
379 	  CD_track[current_track].patch_number =
380 
381 	    get_config_int (section_name, "patch_number", 0);
382 
383 
384 
385 	  if (CD_track[current_track].patch_number)
386 
387 	    {
388 
389 	      UInt32 i;
390 
391 	      char tmp_str[256];
392 
393 
394 
395 	      CD_track[current_track].patch =
396 
397 		(PatchEntry *) malloc (sizeof (PatchEntry)
398 
399 				       *
400 
401 				       CD_track[current_track].patch_number);
402 
403 
404 
405 	      for (i = 0; i < CD_track[current_track].patch_number; i++)
406 
407 		{
408 
409 
410 
411 		  sprintf (tmp_str, "patch%d", i);
412 
413 
414 
415 		  strcpy (tmp_str, get_config_string (section_name,
416 
417 						      tmp_str,
418 
419 						      "0XFFFFFFFF,0XFF"));
420 
421 
422 
423 		  sscanf (tmp_str, "%X,%X",
424 
425 			  &CD_track[current_track].patch[i].offset,
426 
427 			  &CD_track[current_track].patch[i].new_val);
428 
429 
430 
431 		  Log
432 
433 		    ("Patch #%d makes bytes at offset 0x%X replaced by 0x%X\n",
434 
435 		     i, CD_track[current_track].patch[i].offset,
436 
437 		     CD_track[current_track].patch[i].new_val);
438 
439 
440 
441 
442 
443 		}
444 
445 	    }
446 
447 
448 
449 	}
450 
451       else if (strcmp (tmp_buf, "CD") == 0)
452 
453 	{
454 
455 
456 
457 	  int min, sec, fra, control;
458 
459 	  int min_end, sec_end, fra_end, control_end;
460 
461 
462 
463 	  //------- track is extracted from CD ----------------
464 
465 
466 
467 	  CD_track[current_track].source_type = HCD_SOURCE_CD_TRACK;
468 
469 
470 
471 	  //------- initializing the drive --------------------
472 
473 
474 
475 	  strcpy (tmp_buf, get_config_string (section_name, "drive", "0"));
476 
477 
478 
479 	  osd_cd_init (tmp_buf);
480 
481 
482 
483 	  //------- looking for the index of the track to use for emulation ----
484 
485 
486 
487 	  CD_track[current_track].filename[0] =
488 
489 	    get_config_int (section_name, "track", current_track);
490 
491 
492 
493 	  //------- looking for the control byte and deducing type ------
494 
495 
496 
497 	  osd_cd_track_info (CD_track[current_track].filename[0],
498 
499 			     &min, &sec, &fra, &control);
500 
501 
502 
503 	  /* TODO : add support if track is in last position */
504 
505 
506 
507 	  osd_cd_track_info (CD_track[current_track].filename[0] + 1,
508 
509 			     &min_end, &sec_end, &fra_end, &control_end);
510 
511 
512 
513 	  CD_track[current_track].length = Time2Frame (min_end,
514 
515 						       sec_end,
516 
517 						       fra_end)
518 
519 	    - Time2Frame (min, sec, fra);
520 
521 
522 
523 	  CD_track[current_track].type = (control & 4);
524 
525 
526 
527 	}
528 
529       else
530 
531 	{
532 
533 
534 
535 	  //----- track is audio ---------
536 
537 
538 
539 	  CD_track[current_track].type = 0;	// Audio track
540 
541 
542 
543 	  //----- emulated track will use a regular file ---------
544 
545 
546 
547 	  CD_track[current_track].source_type = HCD_SOURCE_REGULAR_FILE;
548 
549 
550 
551 	  //----- search filename ---------
552 
553 
554 
555 	  strcpy (tmp_buf, MP3_path);
556 
557 
558 
559 	  strcat (tmp_buf, get_config_string (section_name, "filename", ""));
560 
561 
562 
563 	  strcpy (CD_track[current_track].filename, tmp_buf);
564 
565 
566 
567 	  //----- search for subtitles -----------------------
568 
569 
570 
571 	  CD_track[current_track].subtitle_synchro_type =
572 
573 	    get_config_int (section_name, "synchro_mode", 0);
574 
575 
576 
577 	  CD_track[current_track].subtitle_number =
578 
579 	    get_config_int (section_name, "subtitle_number", 0);
580 
581 
582 
583 	  if (CD_track[current_track].subtitle_number)
584 
585 	    {
586 
587 	      UInt32 i;
588 
589 	      char tmp_str[256];
590 
591 
592 
593 	      CD_track[current_track].subtitle =
594 
595 		(SubtitleEntry *) malloc (sizeof (SubtitleEntry)
596 
597 					  *
598 
599 					  CD_track
600 
601 					  [current_track].subtitle_number);
602 
603 
604 
605 	      for (i = 0; i < CD_track[current_track].subtitle_number; i++)
606 
607 		{
608 
609 
610 
611 		  sprintf (tmp_str, "subtitle%d", i);
612 
613 
614 
615 		  strcpy (tmp_str, get_config_string (section_name,
616 
617 						      tmp_str, "0,0,"));
618 
619 
620 
621 		  sscanf (tmp_str, "%d,%d",
622 
623 			  &CD_track[current_track].subtitle[i].StartTime,
624 
625 			  &CD_track[current_track].subtitle[i].Duration);
626 
627 
628 
629 		  if (strrchr (tmp_str, ','))
630 
631 		    {
632 
633 		      memset (CD_track[current_track].subtitle[i].data, 0,
634 
635 			      32);
636 
637 		      strncpy (CD_track[current_track].subtitle[i].data,
638 
639 			       strrchr (tmp_str, ',') + 1, 31);
640 
641 		    }
642 
643 
644 
645 		  Log
646 
647 		    ("Subtitle #%d begins at %d, lasts %d 60th of sec and is %s\n",
648 
649 		     i, CD_track[current_track].subtitle[i].StartTime,
650 
651 		     CD_track[current_track].subtitle[i].Duration,
652 
653 		     CD_track[current_track].subtitle[i].data);
654 
655 
656 
657 		}
658 
659 	    }
660 
661 
662 
663 
664 
665 	  //----- use Tormod's work to guess MP3 length ---------
666 
667 
668 	  if (strcasestr(CD_track[current_track].filename, ".mp3")) {
669 	  CD_track[current_track].length =
670 
671 	    (int) (MP3_length (CD_track[current_track].filename) * 75.0);
672 
673 
674 
675 	  Log ("After MP3 length guessing, size of %s is %d 75th of second\n",
676 
677 	       CD_track[current_track].filename,
678 
679 	       CD_track[current_track].length);
680 	}
681 
682 	  #ifdef OGG_SUPPORT
683 	  //----- littletux: use ogginfo hack to guess OGG length ---------
684 	  if (strcasestr(CD_track[current_track].filename, ".ogg")) {
685 		CD_track[current_track].length = (int) (OGG_length(CD_track[current_track].filename));
686 	    Log ("After OGG length guessing, size of %s is %d 75th of second\n", CD_track[current_track].filename, CD_track[current_track].length);
687   	  }
688 	  #endif
689 
690 	  if (CD_track[current_track].length == 0)
691 
692 	    CD_track[current_track].length = 30 * 75;	// 30 sec track
693 
694 
695 
696 	}
697 
698 
699 
700       //----- if begin hasn't been specified ---------
701 
702 
703 
704       if (!CD_track[current_track].beg_lsn)
705 
706 	{
707 
708 	  if (current_track == 1)
709 
710 	    CD_track[current_track].beg_lsn = 0;
711 
712 	  else
713 
714 	    CD_track[current_track].beg_lsn =
715 
716 	      CD_track[current_track - 1].beg_lsn +
717 
718 	      CD_track[current_track - 1].length;
719 
720 	}
721 
722 
723 
724       //----- convert beginning in PCE msf format ---------
725 
726 
727 
728       {
729 
730 	int min, sec, fra;
731 
732 
733 
734 	Frame2Time (CD_track[current_track].beg_lsn + 150, &min, &sec, &fra);
735 
736 
737 
738 	CD_track[current_track].beg_min = binbcd[min];
739 
740 	CD_track[current_track].beg_sec = binbcd[sec];
741 
742 	CD_track[current_track].beg_fra = binbcd[fra];
743 
744       }
745 
746     }
747 
748 
749 
750 
751 #endif
752 
753 //restores right file for hugo config
754 set_config_file_back();
755 
756   return 1;
757 
758 
759 
760 }
761 
762 
763 
764 void
765 
HCD_pause_playing()766 HCD_pause_playing ()
767 
768 {
769 
770 
771 
772   if (MP3_playing)
773 
774     MP3_playing = 0;
775 
776 
777 
778 };
779 
780 void
HCD_play_track(UChar track,char repeat)781 HCD_play_track (UChar track, char repeat)
782 {
783 
784   switch (CD_track[track].subtitle_synchro_type)
785     {
786     case 0:			// frame synchronisation
787       HCD_frame_at_beginning_of_track = (UInt32)frame;
788       break;
789     case 1:			// timer synchronisation
790       HCD_frame_at_beginning_of_track = timer_60;
791       break;
792     }
793 
794   HCD_current_played_track = (UChar)track;
795   HCD_current_subtitle = 0;
796 
797   if (CD_track[track].source_type == HCD_SOURCE_CD_TRACK)
798     {
799      osd_cd_play_audio_track((UChar)(CD_track[track].filename[0]));
800      }
801 
802 #warning "reenable mp3 with sdl"
803 #if (defined(LINUX) || defined(MSDOS)) && defined(ALLEGRO)
804 
805   else
806 
807   if ((CD_track[track].source_type == HCD_SOURCE_REGULAR_FILE) &&
808 
809      (strcasestr (CD_track[track].filename, ".MP3")))
810 
811     {				// must play MP3
812 
813       load_amp (CD_track[track].filename, repeat);
814 
815       MP3_playing = 1;
816 
817     }
818 
819 #elif defined (SDL_mixer)
820 
821   else if ((CD_track[track].source_type == HCD_SOURCE_REGULAR_FILE)
822 			&& ((strcasestr (CD_track[track].filename, ".mp3"))
823 			||(strcasestr (CD_track[track].filename, ".ogg"))
824 			||(strcasestr (CD_track[track].filename, ".wav"))))
825     {
826 	  Mix_PlayMusic(sdlmixmusic[track],repeat);
827       MP3_playing = 1;
828     }
829 
830 #endif
831 
832 };
833 
834 void
HCD_play_sectors(int begin_sect,int sect_len,char repeat)835 HCD_play_sectors (int begin_sect, int sect_len, char repeat)
836 {
837   int result;
838 
839   for (result = nb_max_track; result; result--)
840     {
841       if (((UInt32)begin_sect >= CD_track[result].beg_lsn) &&
842 	  		((unsigned)begin_sect <= CD_track[result].beg_lsn + CD_track[result].length))
843 			break;
844     }
845 
846   if (CD_track[result].source_type == HCD_SOURCE_CD_TRACK)
847     {
848      UInt32 min_from, sec_from, fra_from;
849      UInt32 min_to, sec_to, fra_to;
850      UInt32 min_real, sec_real, fra_real, dummy;
851      begin_sect -= CD_track[result].beg_lsn;
852 
853      /* begin_sect is now relative to the begin of the track to play */
854      Frame2Time((unsigned)begin_sect,
855                 (int*)&min_from,
856                 (int*)&sec_from,
857                 (int*)&fra_from);
858 
859      sect_len += begin_sect;
860 
861      /* sect_len is now also relative to the begin of the track to play */
862      Frame2Time((unsigned)sect_len,
863                 (int*)&min_to,
864                 (int*)&sec_to,
865                 (int*)&fra_to);
866 
867      osd_cd_track_info((UChar)(CD_track[result].filename[0]),
868                        (int*)&min_real,
869                        (int*)&sec_real,
870                        (int*)&fra_real,
871                        (int*)&dummy);
872 
873      min_from += min_real;
874      sec_from += sec_real;
875      fra_from += fra_real;
876 
877      min_to += min_real;
878      sec_to += sec_real;
879      fra_to += fra_real;
880 
881 
882 
883      if (fra_to > 75)
884 
885        {
886 
887         fra_to -= 75;
888 
889         sec_to ++;
890 
891         }
892 
893 
894 
895      if (fra_from > 75)
896 
897        {
898 
899         fra_from -= 75;
900 
901         sec_from ++;
902 
903         }
904 
905 
906 
907      if (sec_to > 60)
908 
909        {
910 
911         sec_to -= 60;
912 
913         min_to ++;
914 
915         }
916 
917 
918 
919      if (sec_from > 60)
920 
921        {
922 
923         sec_from -= 60;
924 
925         min_from ++;
926 
927         }
928 
929 
930 
931       osd_cd_play_audio_range(min_from,
932 
933                               sec_from,
934 
935                               fra_from,
936 
937                               min_to,
938 
939                               sec_to,
940 
941                               fra_to);
942 
943 
944 
945      }
946 
947 #warning "reenable mp3 with sdl"
948 #if (defined(LINUX) || defined(MSDOS)) && defined(ALLEGRO)
949 
950   else
951 
952   if ((CD_track[result].source_type == HCD_SOURCE_REGULAR_FILE) &&
953 
954       (strcasestr (CD_track[result].filename, ".MP3")))
955 
956     {				// must play MP3
957 
958 
959 
960       if (-150 < begin_sect - CD_track[result].beg_lsn < 150)
961 
962 	load_amp (CD_track[result].filename, repeat);
963 
964       else			/* can't yet easily repeat "inside" a track */
965 
966 	load_amp (CD_track[result].filename, FALSE);
967 
968 
969 
970       if (amp_pollsize)
971 
972 	seek_amp_abs (amp_samprat / amp_pollsize *
973 
974 		      (begin_sect - CD_track[result].beg_lsn) / 75);
975 
976 
977 
978       HCD_frame_at_beginning_of_track =
979 
980 	frame - (begin_sect - CD_track[result].beg_lsn) / 75.0 * 60.0;
981 
982       /* try to estimate the number of cycle that should have elapsed since
983 
984          the beginning of the track */
985 
986 
987 
988       HCD_current_played_track = result;
989 
990       HCD_current_subtitle = 0;
991 
992 
993 
994       MP3_playing = 1;
995 
996 
997 
998 
999 
1000     }
1001 
1002 #elif SDL_mixer
1003 
1004   else if ((CD_track[result].source_type == HCD_SOURCE_REGULAR_FILE)
1005 				&& (strcasestr (CD_track[result].filename, ".mp3")
1006 				||strcasestr (CD_track[result].filename, ".ogg"))) {
1007 
1008       if (-150 < begin_sect - CD_track[result].beg_lsn < 150)
1009 		Mix_PlayMusic(sdlmixmusic[result], repeat);
1010       else			/* can't yet easily repeat "inside" a track */
1011 		Mix_PlayMusic(sdlmixmusic[result], FALSE);
1012 
1013 	  Mix_RewindMusic();
1014 	  if (Mix_SetMusicPosition((begin_sect - CD_track[result].beg_lsn) / 75)==-1){
1015 	    printf("Mix_SetMusicPosition(): %s\n", Mix_GetError());
1016 	  }
1017 
1018       HCD_frame_at_beginning_of_track = frame - (begin_sect - CD_track[result].beg_lsn) / 75.0 * 60.0;
1019 
1020       /* try to estimate the number of cycle that should have elapsed since
1021          the beginning of the track */
1022       HCD_current_played_track = result;
1023       HCD_current_subtitle = 0;
1024       MP3_playing = 1;
1025 
1026   }
1027 #endif
1028   else if (strcasestr (CD_track[result].filename, ".WAV"))
1029 
1030     {
1031 
1032 #ifdef MSDOS
1033 
1034 
1035 
1036       static SAMPLE *wav_sample;
1037 
1038 
1039 
1040       wav_sample = load_sample (CD_track[result].filename);
1041 
1042 
1043 
1044       play_sample (wav_sample, 255, 128, 1000, FALSE);
1045 
1046 
1047 
1048 #endif
1049 
1050 
1051 
1052     }
1053 
1054 
1055 
1056 };
1057 
1058 void
HCD_shutdown()1059 HCD_shutdown ()
1060 {
1061   int current_track;
1062 
1063   for (current_track = (int)HCD_first_track;
1064        current_track <= (int)HCD_last_track; current_track++)
1065     {
1066       free (CD_track[current_track].patch);
1067       free (CD_track[current_track].subtitle);
1068     }
1069 
1070 };
1071 
1072 void
HCD_handle_subtitle()1073 HCD_handle_subtitle ()
1074 {
1075 
1076   if (HCD_current_subtitle >=
1077       CD_track[HCD_current_played_track].subtitle_number)
1078     return;
1079 
1080   switch (CD_track[HCD_current_played_track].subtitle_synchro_type)
1081     {
1082     case 0:
1083       if (frame - HCD_frame_at_beginning_of_track >=
1084 	  		(int)(CD_track[HCD_current_played_track].
1085 	  		subtitle[HCD_current_subtitle].StartTime))
1086 			{
1087 				osd_gfx_set_message (
1088 					CD_track[HCD_current_played_track].subtitle[HCD_current_subtitle].data
1089 				);
1090 				message_delay =
1091 					CD_track[HCD_current_played_track].subtitle[HCD_current_subtitle].Duration;
1092 				HCD_current_subtitle++;
1093 			}
1094       break;
1095 
1096     case 1:
1097 
1098       if (timer_60 - HCD_frame_at_beginning_of_track >=
1099 
1100 	  CD_track[HCD_current_played_track].
1101 
1102 	  subtitle[HCD_current_subtitle].StartTime)
1103 
1104 	{
1105 
1106 	  osd_gfx_set_message (CD_track[HCD_current_played_track].subtitle
1107 
1108 			       [HCD_current_subtitle].data);
1109 
1110 	  message_delay =
1111 
1112 	    CD_track[HCD_current_played_track].
1113 
1114 	    subtitle[HCD_current_subtitle].Duration;
1115 
1116 	  HCD_current_subtitle++;
1117 
1118 	}
1119 
1120       break;
1121     }
1122 
1123 }
1124 
1125 void
read_sector_HCD(unsigned char * p,UInt32 dum)1126 read_sector_HCD (unsigned char *p, UInt32 dum)
1127 {
1128   int result;
1129 
1130   for (result = (int)HCD_last_track; result > 0x01; result--)
1131     {
1132       if ((pce_cd_sectoraddy >= CD_track[result].beg_lsn) &&
1133 	  (pce_cd_sectoraddy <= CD_track[result].beg_lsn +
1134 	   CD_track[result].length))
1135 	break;
1136     }
1137 
1138   if (CD_track[result].source_type == HCD_SOURCE_REGULAR_FILE)
1139     HCD_iso_read_sector(p, dum, (UInt32)result);
1140   else
1141     if (CD_track[result].source_type == HCD_SOURCE_CD_TRACK)
1142       HCD_cd_read_sector(p, dum, (UInt32)result);
1143 
1144 }
1145 
1146 void
HCD_iso_read_sector(unsigned char * p,UInt32 dum,UInt32 result)1147 HCD_iso_read_sector(unsigned char *p, UInt32 dum, UInt32 result)
1148 {
1149 
1150   static int current_position = 0;
1151   static int current_file = 0;
1152   static int current_type = 0;
1153 
1154 #ifndef FINAL_RELEASE
1155   fprintf (stderr, "Loading sector nø%d.\n", pce_cd_sectoraddy);
1156   Log ("Loading sector nø%d.\n", pce_cd_sectoraddy);
1157   Log ("HCD : seek at %d\n",
1158        (pce_cd_sectoraddy - CD_track[result].beg_lsn) * 2048);
1159   Log ("Track nø%d begin at %d\n", result, CD_track[result].beg_lsn);
1160   Log ("Track nø2 begin at %d\n", CD_track[2].beg_lsn);
1161 #endif
1162 
1163 label_are_bad:
1164 
1165   if (result == current_file)
1166     {
1167       if (current_type == 1)	// non compacted
1168 	{
1169 	  fseek (HCD_iso_FILE, (long)((pce_cd_sectoraddy - CD_track[result].beg_lsn)
1170 		 * 2048), SEEK_SET);
1171 
1172 	  if (fread (p, 2048, 1, HCD_iso_FILE) != 2048)
1173 			Log("Error when reading sector %d in hcd", pce_cd_sectoraddy);
1174 
1175 	  current_position = ftell (HCD_iso_FILE);	// not useful
1176 
1177 	}
1178 
1179 
1180 #ifdef ALLEGRO
1181 
1182       else if (current_type == 2)	//compacted
1183 
1184 	{
1185 
1186 	  int dummy = (pce_cd_sectoraddy - CD_track[result].beg_lsn) * 2048;
1187 
1188 
1189 
1190 	  if (current_position > dummy)
1191 
1192 	    {
1193 
1194 	      pack_fclose (HCD_packed_iso_FILE);
1195 
1196 
1197 
1198 	      HCD_packed_iso_FILE =
1199 
1200 		pack_fopen (CD_track[result].filename, F_READ_PACKED);
1201 
1202 
1203 
1204 	      pack_fseek (HCD_packed_iso_FILE, dummy);
1205 
1206 
1207 
1208 	      current_position = dummy;
1209 
1210 	    }
1211 
1212 	  else if (current_position < dummy)
1213 
1214 	    {
1215 
1216 	      pack_fseek (HCD_packed_iso_FILE, dummy - current_position);
1217 
1218 	      current_position = dummy;
1219 
1220 	    }
1221 
1222 
1223 
1224 	  pack_fread (p, 2048, HCD_packed_iso_FILE);
1225 
1226 
1227 
1228 	  current_position += 2048;
1229 
1230 
1231 
1232 	}
1233 
1234 
1235 #endif
1236 
1237       else
1238 
1239 	{
1240 
1241 	  Log ("Open mode in HCD read function incorrect\n");
1242 
1243 	  exit (-2);
1244 
1245 	}
1246 
1247     }				// file well opened
1248 
1249   else
1250 
1251     {				// must open file
1252 
1253       if (strcasestr (CD_track[result].filename, ".ISO"))
1254 
1255 	{
1256 
1257 	  HCD_iso_FILE = fopen (CD_track[result].filename, "rb");
1258 
1259 
1260 
1261 #ifndef FINAL_RELEASE
1262 
1263 	  Log ("File tried to open\n");
1264 
1265 #endif
1266 
1267 
1268 
1269 	  if (!HCD_iso_FILE)
1270 
1271 	    {
1272 
1273 
1274 
1275 	      Log ("ISO file not found : %s\nUsed for track %d\n",
1276 
1277 		   CD_track[result].filename, result);
1278 
1279 
1280 
1281 	      exit (-3);
1282 
1283 
1284 
1285 	    }
1286 
1287 
1288 
1289 	  current_file = result;
1290 
1291 	  current_type = 1;
1292 
1293 	  current_position = 0;
1294 
1295 	}
1296 
1297 #ifdef ALLEGRO
1298       else if (strcasestr (CD_track[result].filename, ".ISQ"))
1299 
1300 	{
1301 
1302 	  HCD_packed_iso_FILE =
1303 
1304 	    pack_fopen (CD_track[result].filename, F_READ_PACKED);
1305 
1306 
1307 
1308 	  if (!HCD_packed_iso_FILE)
1309 
1310 	    {
1311 
1312 
1313 
1314 	      Log ("ISQ file not found : %s\nUsed for track %d\n",
1315 
1316 		   CD_track[result].filename, result);
1317 
1318 
1319 
1320 	      exit (-3);
1321 
1322 
1323 
1324 	    }
1325 
1326 
1327 
1328 
1329 
1330 	  current_file = result;
1331 
1332 	  current_type = 2;
1333 
1334 	  current_position = 0;
1335 
1336 	}
1337 
1338 #endif
1339       goto label_are_bad;
1340     }
1341 
1342   {
1343     int dummy;
1344     for (dummy = 0; dummy < (int)(CD_track[result].patch_number); dummy++)
1345       {
1346 	if ((CD_track[result].patch[dummy].offset >> 11) ==
1347 	    pce_cd_sectoraddy - CD_track[result].beg_lsn)
1348 	  p[CD_track[result].patch[dummy].offset & 2047] =
1349 	    CD_track[result].patch[dummy].new_val;
1350 
1351       }
1352 
1353   }
1354 
1355  }
1356 
1357 void
HCD_cd_read_sector(unsigned char * p,UInt32 dum,UInt32 result)1358 HCD_cd_read_sector(unsigned char *p, UInt32 dum, UInt32 result)
1359 {
1360  UInt32 min, sec, fra, control;
1361 
1362  osd_cd_track_info((UChar)CD_track[result].filename[0],
1363                    (int*)&min,
1364                    (int*)&sec,
1365                    (int*)&fra,
1366                    (int*)&control);
1367 
1368 
1369 
1370  osd_cd_read(p, dum - CD_track[result].beg_lsn + Time2HSG((int)min, (int)sec, (int)fra));
1371 
1372 
1373 
1374 }
1375