forked from Tronix286/DOSMID
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdosmid.c
2126 lines (1986 loc) · 68.6 KB
/
dosmid.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
/*
* DOSMID - a low-requirement MIDI and MUS player for DOS
*
* Copyright (C) 2014-2022, Mateusz Viste
* All rights reserved.
* Copyright 2015-2024 Rivoreo
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "defines.h"
#ifdef MSDOS
#include <dos.h> /* REGS */
#else
#include <sys/param.h>
#if defined __FreeBSD__ && !defined __FreeBSD_kernel__
#define __FreeBSD_kernel__
#elif defined __LINUX__ && !defined __linux__
#define __linux__
#endif
#ifdef HAVE_PORT_IO
#include "unixpio.h"
#endif
#include <sys/select.h>
#include <sys/stat.h>
#include <unistd.h>
#include <locale.h>
#ifdef WCHAR
#include <langinfo.h>
#endif
#include <time.h>
#include <fcntl.h>
#include <errno.h>
#include <curses.h> /* KEY_* */
#endif
#if defined CMSLPT || defined OPLLPT
#include "lpt.h"
#endif
#include <stdio.h> /* printf() */
#include <limits.h> /* ULONG_MAX */
#include <stdlib.h> /* rand() */
#include <string.h> /* memset(), strcpy(), strncat(), memcpy() */
#if !defined MSDOS || (defined __WATCOMC__ && __WATCOMC__ >= 1240)
#define HAVE_STRCASECMP
#include <strings.h>
#endif
#include "bitfield.h"
#include "fio.h"
#ifdef MSDOS
#include "gus.h"
#endif
#include "mem.h"
#include "midi.h"
#include "mus.h"
#include "outdev.h"
#include "rs232.h"
#include "syx.h"
#include "timer.h"
#include "ui.h"
#include "version.h"
#include <assert.h>
#if defined MSDOS && !defined MK_FP
#define MK_FP(S,O) (void __far *)(((unsigned long int)(S) << 16) + (unsigned long int)(O))
#endif
#if !defined MSDOS && !defined CLOCK_MONOTONIC_FAST
#define CLOCK_MONOTONIC_FAST CLOCK_MONOTONIC
#endif
#define MAXTRACKS 64
#define EVENTSCACHESIZE 64 /* *must* be a power of 2 !!! */
#define EVENTSCACHEMASK 63 /* used by the circular events buffer */
#define PRESET_GM 0 /* default */
#define PRESET_GS 1
#define PRESET_XG 2
#define PRESET_NONE 3
/* define a work buffer that will be used instead of malloc() calls whenever a temporary buffer is required */
unsigned char wbuff[8192];
enum playaction {
ACTION_NONE = 0,
ACTION_NEXT = 1,
ACTION_PREV = 2,
ACTION_ERR_SOFT = 3,
ACTION_ERR_HARD = 4,
ACTION_EXIT = 64
};
struct clioptions {
int memmode; /* type of memory to use: MEM_XMS or MEM_MALLOC */
#ifndef MSDOS
int devfd;
char *devname;
#endif
#ifdef HAVE_PORT_IO
unsigned short devport;
unsigned short port_mpu;
unsigned short port_awe;
unsigned short port_sb;
#endif
enum outdev_type device;
int devicesubtype;
char *devtypename;/* the human name of the out device (MPU, AWE..) */
char *midifile; /* MIDI filename to play */
char *syxrst; /* syx file to use for MIDI resets */
int delay; /* additional delay to apply before playing a file */
char *playlist; /* the playlist to read files from */
char *sbnk; /* optional sound bank to use (IBK file or so) */
#ifdef DBGFILE
FILE *logfile; /* an open debug log file */
#endif
int ui_init_flags;
int dev_clear_flags;
unsigned char onlpt;
unsigned char volume;
/* 'flags' */
unsigned char xmsdelay;
char nockdev;
#ifdef MSDOS
unsigned char nopowersave;
#endif
unsigned char dontstop;
unsigned char random; /* randomize playlist order */
unsigned char gmgspreset; /* PRESET_GM, PRESET_GS, PRESET_XG, PRESET_NONE */
};
#ifdef MSDOS
/* fetch directory where the program resides, and return its length. result
* string is never longer than 128 (incl. the null terminator), and it is
* always terminated with a backslash separator, unless it is an empty string */
static int exepath(char *result) {
char far *psp, far *env;
unsigned int envseg, pspseg, x, i;
int lastsep;
union REGS regs;
/* get the PSP segment */
regs.h.ah = 0x62;
int86(0x21, ®s, ®s),
pspseg = regs.x.bx;
/* compute a far pointer that points to the top of PSP */
psp = MK_FP(pspseg, 0);
/* fetch the segment address of the environment */
envseg = psp[0x2D];
envseg <<= 8;
envseg |= psp[0x2C];
/* compute the env pointer */
env = MK_FP(envseg, 0);
/* skip all environment variables */
x = 0;
for (;;) {
x++;
if (env[x] == 0) { /* end of variable */
x++;
if (env[x] == 0) break; /* end of list */
}
}
x++;
/* read the WORD that indicates string that follow */
if (env[x] < 1) {
result[0] = 0;
return(0);
}
x += 2;
/* else copy the EXEPATH to our return variable, and truncate after last '\' */
lastsep = -1;
for (i = 0;; i++) {
result[i] = env[x++];
if (result[i] == '\\') lastsep = i;
if (result[i] == 0) break; /* end of string */
if (i >= 126) break; /* this DOS string should never go beyond 127 chars! */
}
result[lastsep + 1] = 0;
return(lastsep + 1);
}
#if 0
static void dos_puts(char *s) {
/* DOS 1+ - WRITE STRING TO STANDARD OUTPUT
AH = 09h
DS:DX -> '$'-terminated string */
unsigned short segm, offs;
segm = FP_SEG(s);
offs = FP_OFF(s);
__asm {
mov ah, 9
push ds
mov cx, segm
push cx
pop ds
mov dx, offs
int 21h
pop ds /* restore DS */
/* print out a CR/LF using INT21h AH=2 */
mov ah, 2
mov dl, 0dh
int 21h
mov ah, 2
mov dl, 0ah
int 21h
}
}
#endif
#endif
/* returns a pseudo-random number, based on the DOS system timer */
static unsigned long int clock_rnd(void) {
#ifdef MSDOS
unsigned long res;
union REGS regs;
regs.h.ah = 0;
int86(0x1A, ®s, ®s);
res = regs.x.cx; /* number of clock ticks since midnight (high word) */
res <<= 16;
res |= regs.x.dx; /* number of clock ticks since midnight (low word) */
return(res);
#else
struct timespec ts;
if(clock_gettime(CLOCK_MONOTONIC_FAST, &ts) < 0) return time(NULL);
return ts.tv_sec ^ ts.tv_nsec;
#endif
}
/* copies the base name of a file (ie without directory path) into a string */
static int filename2basename(const char *fromname, char *tobasename, char *todirname, int maxlen) {
int x, x2, firstchar = 0;
/* find the first character of the base name */
for (x = 0; fromname[x] != 0; x++) {
switch (fromname[x]) {
case '/':
#ifdef MSDOS
case '\\':
case ':':
#endif
firstchar = x + 1;
break;
}
}
/* copy basename to tobasename */
if (tobasename != NULL) {
x2 = 0;
for (x = firstchar; fromname[x] != 0; x++) {
if ((fromname[x] == 0) || (x2+1 >= maxlen)) break;
tobasename[x2++] = fromname[x];
}
tobasename[x2] = 0;
}
/* copy dirname to todirname */
if (todirname != NULL) {
x2 = 0;
for (x = 0; x < firstchar; x++) {
if ((fromname[x] == 0) || (x2+1 >= maxlen)) break;
todirname[x2++] = fromname[x];
}
todirname[x2] = 0;
}
return firstchar;
}
#ifdef MSDOS
/* switch a string to upper case */
static void ucasestr(char *s) {
for (; *s != 0; s++) if ((*s >= 'a') && (*s <= 'z')) *s -= 32;
}
#endif
/* returns the lower-case version of c char, if applicable */
static int lcase(char c) {
if ((c >= 'A') && (c <= 'Z')) return(c + 32);
return(c);
}
#ifndef HAVE_STRCASECMP
/* a case-insensitive version of strcmp() */
static int strcasecmp(const char *s1, const char *s2) {
for (;;) {
if (lcase(*s1) != lcase(*s2)) return(1);
if (*s1 == 0) return(0);
s1++;
s2++;
}
}
#endif
/* case-insensitively check whether 'str' starts with 'start' */
static int stringstartswith(const char *str, const char *start) {
while (*start != 0) {
if (lcase(*start) != lcase(*str)) return(0);
str++;
start++;
}
return(1);
}
static int hexchar2int(char c) {
if ((c >= '0') && (c <= '9')) return(c - '0');
if ((c >= 'a') && (c <= 'f')) return(10 + c - 'a');
if ((c >= 'A') && (c <= 'F')) return(10 + c - 'A');
return(-1);
}
/* converts a hex string to unsigned int. stops at first null terminator or
* space. returns zero on error. */
static unsigned int hexstr2uint(const char *hexstr) {
unsigned int v = 0;
while (*hexstr && *hexstr != ' ') {
int c;
c = hexchar2int(*hexstr);
if (c < 0) return(0);
v <<= 4;
v |= c;
hexstr++;
}
return(v);
}
static char *devtoname(enum outdev_type device, int devicesubtype) {
switch (device) {
case DEV_NONE: return("NONE");
case DEV_MPU401: return("MPU");
#ifdef SBAWE
case DEV_AWE: return("AWE");
#endif
#ifdef OPL
case DEV_OPL: return("OPL");
case DEV_OPL2: return("OPL2");
case DEV_OPL3: return("OPL3");
#endif
case DEV_RS232:
#ifdef MSDOS
if (devicesubtype == 1) return("COM1");
if (devicesubtype == 2) return("COM2");
if (devicesubtype == 3) return("COM3");
if (devicesubtype == 4) return("COM4");
#endif
return("COM");
case DEV_SBMIDI: return("SB");
#ifdef MSDOS
case DEV_GUS: return("GUS");
#endif
#ifdef CMS
case DEV_CMS: return("CMS");
#endif
default: return("UNK");
}
}
/* analyzes a 16 bytes file header and guess the file format */
static enum fileformat header2fileformat(unsigned char *hdr) {
/* Classic MIDI */
if ((hdr[0] == 'M') && (hdr[1] == 'T') && (hdr[2] == 'h') && (hdr[3] == 'd')) {
return(FORMAT_MIDI);
}
/* RMID inside a RIFF container */
if ((hdr[0] == 'R') && (hdr[1] == 'I') && (hdr[2] == 'F') && (hdr[3] == 'F')
&& (hdr[8] == 'R') && (hdr[9] == 'M') && (hdr[10] == 'I') && (hdr[11] == 'D')) {
return(FORMAT_RMID);
}
/* MUS (as used in Doom, from Id Software) */
if ((hdr[0] == 'M') && (hdr[1] == 'U') && (hdr[2] == 'S') && (hdr[3] == 0x1A)) {
return(FORMAT_MUS);
}
/* else I don't know */
return(FORMAT_UNKNOWN);
}
#ifndef MSDOS
static void close_device(struct clioptions *config) {
if(config->devfd == -1) return;
close(config->devfd);
config->devfd = -1;
free(config->devname);
config->devname = NULL;
}
static void open_device(struct clioptions *config, const char *name, int is_serial) {
assert(config->devfd == -1);
if(*name != '/') {
size_t len = strlen(name) + 1;
char path[5 + len];
memcpy(path, "/dev/", 5);
memcpy(path + 5, name, len);
config->devfd = open(path, O_WRONLY | O_NOCTTY);
}
if(config->devfd == -1) {
config->devfd = open(name, O_WRONLY | O_NOCTTY);
if(config->devfd == -1) return;
}
struct stat st;
errno = ENOTTY;
if(fstat(config->devfd, &st) < 0 || (!S_ISCHR(st.st_mode) && ((!S_ISFIFO(st.st_mode) && !S_ISSOCK(st.st_mode)) || !is_serial)) || (is_serial && !isatty(config->devfd))) {
close_device(config);
return;
}
config->devname = strdup(strncmp(name, "/dev/", 5) == 0 ? name + 5 : name);
}
#endif
#if defined CMSLPT || defined OPLLPT
static int try_parse_lpt_name(struct clioptions *config, const char *s) {
config->onlpt = 0;
#ifdef HAVE_PORT_IO
if(stringstartswith(s, "lpt")) {
char unit = s[3];
if(unit < '1' || unit > '4' || s[4]) return 0;
return config->devicesubtype = config->onlpt = unit - '0';
}
#endif
#ifndef MSDOS
#if defined __FreeBSD_kernel__ || defined __linux__
const char *devname = s;
if(stringstartswith(devname, "/dev/")) devname += 5;
#ifdef __FreeBSD_kernel__
if(stringstartswith(devname, "ppi")) {
#else
if(stringstartswith(devname, "parport")) {
#endif
open_device(config, s, 0);
if(config->devfd != -1) {
if(claim_lpt(config->devfd) < 0) {
int e = errno;
close_device(config);
errno = e;
return 0;
}
config->devicesubtype = config->onlpt = 255;
}
return config->onlpt;
}
#endif
#endif
return -1;
}
#ifdef HAVE_PORT_IO
static uint16_t get_lpt_port(unsigned int i) {
#ifdef MSDOS
return *(uint16_t __far *)MK_FP(0x40, 6 + 2*i);
#else
#ifdef __linux__
// TODO: Try /proc/parport
#endif
// Fallback to hardcoded ports
switch(i) {
case 1:
return 0x378;
case 2:
return 0x278;
case 3:
return 0x3bc;
default:
return 0;
}
#endif
}
#endif /* HAVE_PORT_IO */
#endif
/* loads the file's extension into ext (limited to limit characters) */
static void getfileext(char *ext, char *filename, int limit) {
int x;
char *extptr = NULL;
ext[0] = 0;
/* find the last dot first */
while (*filename != 0) {
if (*filename == '.') {
extptr = filename + 1;
}
filename++;
}
if (extptr == NULL) return;
/* copy the extension to ext, up to limit bytes */
limit--; /* make room for the null char */
for (x = 0; extptr[x] != 0; x++) {
if (x >= limit) break;
/* make sure the extension is all-lowercase */
if ((extptr[x] >= 'A') && (extptr[x] <= 'Z')) {
ext[x] = extptr[x] + 32;
} else {
ext[x] = extptr[x];
}
}
ext[x] = 0; /* terminate the ext string */
}
static inline int is_option(const char *s) {
#ifdef MSDOS
return *s == '/' || *s == '-';
#else
switch(*s) {
const char *p;
case '/':
p = s + 1;
while(*p) {
if(*p == '/') return 0;
if(*p == '=') return 1;
p++;
}
return 1;
case '-':
return 1;
default:
return 0;
}
#endif
}
#define REQUEST_HELP ((char *)-1)
#define REQUEST_VERSION ((char *)-2)
/* interpret a single config argument, returns NULL on succes, or a pointer to
* an error string otherwise */
static char *feedarg(char *arg, struct clioptions *params, int option_allowed, int file_allowed) {
if(option_allowed && is_option(arg)) {
#ifndef MSDOS
static char errmsg[128];
#endif
char *o = arg + 1;
if (stringstartswith(o, "preset=")) {
o += 7;
if(strcasecmp(o, "gm") == 0) params->gmgspreset = PRESET_GM;
else if(strcasecmp(o, "gs") == 0) params->gmgspreset = PRESET_GS;
else if(strcasecmp(o, "xg") == 0) params->gmgspreset = PRESET_XG;
else if(strcasecmp(o, "none") == 0) params->gmgspreset = PRESET_NONE;
else return "Invalid preset setting";
#ifdef HAVE_PORT_IO
} else if (strcasecmp(o, "mpu") == 0) {
#ifndef MSDOS
close_device(params);
#endif
params->device = DEV_MPU401;
params->devport = params->port_mpu;
/* if MPU port not found in BLASTER, use the default 0x330 */
if (params->devport == 0) params->devport = 0x330;
} else if (stringstartswith(o, "mpu=")) {
#ifndef MSDOS
close_device(params);
#endif
params->device = DEV_MPU401;
params->devport = hexstr2uint(o + 4);
if (params->devport < 1) return("Invalid MPU port provided. Example: /mpu=330");
#ifdef SBAWE
} else if (strcasecmp(o, "awe") == 0) {
params->device = DEV_AWE;
params->devport = params->port_awe;
/* if AWE port not found in BLASTER, use the default 0x620 */
if (params->devport == 0) params->devport = 0x620;
} else if (stringstartswith(o, "awe=")) {
params->device = DEV_AWE;
params->devport = hexstr2uint(o + 4);
if (params->devport < 1) return("Invalid AWE port provided. Example: /awe=620");
#endif
#ifdef MSDOS
} else if (strcasecmp(o, "gus") == 0) {
params->device = DEV_GUS;
params->devport = gus_find();
if (params->devport < 1) return("GUS error: No ULTRAMID driver found");
#endif
#endif /* HAVE_PORT_IO */
#ifdef OPL
#ifdef HAVE_PORT_IO
} else if (strcasecmp(o, "opl") == 0) {
#ifndef MSDOS
close_device(params);
#endif
params->device = DEV_OPL;
params->devport = 0x388;
} else if (stringstartswith(o, "opl=")) {
#ifndef MSDOS
close_device(params);
#endif
params->device = DEV_OPL;
params->devport = hexstr2uint(o + 4);
if (params->devport < 1) return("Invalid OPL port provided. Example: /opl=388");
#endif /* HAVE_PORT_IO */
} else if (stringstartswith(o, "opl") && (o[3] == '2' || o[3] == '3') && (!o[4] || o[4] == '=')) {
#ifndef MSDOS
close_device(params);
#endif
params->device = o[3] == '3' ? DEV_OPL3 : DEV_OPL2;
if(o[4]) {
#ifdef OPLLPT
#ifndef MSDOS
errno = 0;
#endif
if(try_parse_lpt_name(params, o + 5) >= 0) {
if(!params->onlpt) {
#ifndef MSDOS
int e = errno;
if(e) {
snprintf(errmsg, sizeof errmsg, "Failed to open '%s', %s", o + 5, strerror(e));
return errmsg;
}
#endif
return "Invalid LPT unit name provided. Example: /opl3=lpt2";
}
} else
#endif
{
#ifdef HAVE_PORT_IO
params->devport = hexstr2uint(o + 5);
if (params->devport < 1) {
return params->device == DEV_OPL3 ?
"Invalid OPL3 port provided. Example: /opl3=388" :
"Invalid OPL2 port provided. Example: /opl2=388";
}
#else
return "Invalid device name provided.";
#endif
}
params->nockdev = 1;
} else {
#ifdef HAVE_PORT_IO
params->devport = 0x388;
#else
return "Device name must be specified";
#endif
}
#endif /* OPL */
#ifdef CMS
} else if (strcasecmp(o, "cms") == 0) {
#ifdef HAVE_PORT_IO
#ifndef MSDOS
close_device(params);
#endif
params->device = DEV_CMS;
params->devport = 0x220;
#else
return "Device name must be specified";
#endif /* HAVE_PORT_IO */
} else if (stringstartswith(o, "cms=")) {
#ifndef MSDOS
close_device(params);
#endif
params->device = DEV_CMS;
#ifdef CMSLPT
if(try_parse_lpt_name(params, o + 4) >= 0) {
if(!params->onlpt) {
#ifndef MSDOS
int e = errno;
if(e) {
snprintf(errmsg, sizeof errmsg, "Failed to open '%s', %s", o + 4, strerror(e));
return errmsg;
}
#endif
return "Invalid LPT unit name provided. Example: /cms=lpt2";
}
} else
#endif
{
#ifdef HAVE_PORT_IO
params->devport = hexstr2uint(o + 4);
if (params->devport < 1) return("Invalid CMS port provided. Example: /cms=220");
#else
return "Invalid device name provided.";
#endif
}
#endif
} else if (stringstartswith(o, "sbnk=")) {
if (params->sbnk != NULL) free(params->sbnk); /* drop last sbnk if already present, so a CLI sbnk would take precedence over a config-file sbnk */
params->sbnk = strdup(o + 5);
} else if (stringstartswith(o, "com=")) {
#ifndef MSDOS
close_device(params);
#endif
params->device = DEV_RS232;
#ifdef HAVE_PORT_IO
params->devport = hexstr2uint(o + 4);
if (params->devport < 10) {
#endif
#ifdef MSDOS
return("Invalid COM port provided. Example: /com=3f8");
#else
open_device(params, o + 4, 1);
if(params->devfd == -1) {
int e = errno;
if(e == ENOENT) {
return "Invalid COM device provided. Example: -com="
#ifdef __linux__
"ttyS1";
#else
"cuau1";
#endif
}
snprintf(errmsg, sizeof errmsg, "Failed to open device '%s', %s", o + 4, strerror(e));
return errmsg;
}
#endif
#ifdef HAVE_PORT_IO
}
#endif
#ifdef MSDOS
} else if (stringstartswith(o, "com")) { /* must be compared AFTER "com=" */
params->device = DEV_RS232;
params->devicesubtype = o[3] - '0';
if ((params->devicesubtype < 1) || (params->devicesubtype > 4)) return("Invalid COM port provided. Example: /com1");
params->devport = rs232_getport(params->devicesubtype);
if (params->devport < 1) return("Failed to autodetect the I/O address of this COM port. Try using the /com=XXX option.");
#endif
#ifdef HAVE_PORT_IO
} else if (strcasecmp(o, "sbmidi") == 0) {
#ifndef MSDOS
close_device(params);
#endif
params->device = DEV_SBMIDI;
params->devport = params->port_sb;
/* if SB port not found in BLASTER, use the default 0x220 */
if (params->devport == 0) params->devport = 0x220;
} else if (stringstartswith(o, "sbmidi=")) {
#ifndef MSDOS
close_device(params);
#endif
params->device = DEV_SBMIDI;
params->devport = hexstr2uint(o + 7);
if (params->devport < 1) return("Invalid SBMIDI port provided. Example: /sbmidi=220");
#endif /* HAVE_PORT_IO */
#ifdef DBGFILE
} else if (stringstartswith(o, "log=")) {
if (params->logfile) fclose(params->logfile);
params->logfile = fopen(o + 4, "wb");
if (params->logfile == NULL) return("Failed to open the debug log file.");
#endif
} else if (stringstartswith(o, "syx=")) {
params->syxrst = strdup(o + 4);
} else if (stringstartswith(o, "delay=")) {
params->delay = atoi(o + 6);
if ((params->delay < 1) || (params->delay > 9000)) {
return("Invalid delay value: must be in the range 1..9000");
}
#ifdef MSDOS
} else if (strcasecmp(o, "fullcpu") == 0) {
params->nopowersave = 1;
#endif
} else if (strcasecmp(o, "dontstop") == 0) {
params->dontstop = 1;
} else if (strcasecmp(o, "random") == 0) {
params->random = 1;
#ifdef MSDOS
} else if (strcasecmp(o, "noxms") == 0) {
params->memmode = MEM_MALLOC;
} else if (strcasecmp(o, "xmsdelay") == 0) {
params->xmsdelay = 1;
#endif
} else if (strcasecmp(o, "nosound") == 0) {
#ifndef MSDOS
close_device(params);
#endif
params->device = DEV_NONE;
#ifdef HAVE_PORT_IO
params->devport = 0;
#endif
} else if (stringstartswith(o, "volume=")) {
int v = atoi(o + 7);
if(v < 1) return "Invalid volume setting.";
if(v > 100) v = 100;
params->volume = v;
} else if (strcasecmp(o, "nocolor") == 0) {
params->ui_init_flags |= UI_NOCOLOR;
} else if(stringstartswith(o, "quirk=")) {
char *comma;
o += 6;
do {
comma = strchr(o, ',');
if(comma) *comma++ = 0;
if(strcmp(o, "norstctrl") == 0) params->dev_clear_flags |= DOSMID_DEV_NORSTCTRL;
else return "Unrecognized quirk name.";
} while(comma && *(o = comma));
} else if (strcmp(o, "?") == 0 || strcasecmp(o, "h") == 0 || strcasecmp(o, "help") == 0) {
return REQUEST_HELP;
} else if (strcasecmp(o, "version") == 0) {
return REQUEST_VERSION;
} else {
return("Unknown option.");
}
} else if (file_allowed && !params->midifile && !params->playlist) {
char ext[4];
getfileext(ext, arg, 4);
if (strcasecmp(ext, "m3u") == 0) {
params->playlist = arg;
} else {
params->midifile = arg;
}
}
return(NULL);
}
/* trims any white-space and line feeds occuring at the right of the string */
static void rtrim(char *s) {
char *lastchar = s;
while (*s != 0) {
switch (*s) {
case ' ':
case '\t':
case '\r':
case '\n':
s++;
break;
default:
lastchar = ++s;
}
}
*lastchar = 0;
}
static char *loadconfigfile(struct clioptions *params) {
char buff[128 + 12]; /* 128 for exepath plus 8+3 for the config file */
int len;
char *errmsg = NULL;
struct fiofile f;
#ifdef MSDOS
/* prepare config file's full path */
len = exepath(buff);
if (len < 1) return(NULL);
/* append the config file itself */
sprintf(buff + len, "dosmid.cfg");
/* open file */
if (fio_open(buff, FIO_OPEN_RD, &f) != 0) return(NULL);
#else
const char *home = getenv("HOME");
if(!home) return NULL;
len = strlen(home);
char cfg_path[len + sizeof "/.dosmid.cfg"];
memcpy(cfg_path, home, len);
strcpy(cfg_path + len, "/.dosmid.cfg");
if(fio_open(cfg_path, FIO_OPEN_RD, &f) < 0) return NULL;
#endif
do {
/* read line & trim */
len = fio_getline(&f, buff, sizeof(buff));
if (len < 0) break; /* stop on EOF */
if (*buff == '#') continue; /* skip comments */
rtrim(buff);
if (*buff == 0) continue; /* skip empty lines */
/* push arg to feedarg() (files not allowed because filename not allocated in persistent memory) */
errmsg = feedarg(buff, params, 1, 0);
} while(!errmsg);
/* close file */
fio_close(&f);
return(errmsg);
}
/* parse command line params and fills the params struct accordingly. returns
NULL on sucess, or a pointer to an error string otherwise. */
static char *parseargv(int argc, char **argv, struct clioptions *params) {
int end_of_options = 0;
int i;
/* if no params at all, don't waste time */
if (argc == 0) return("");
/* now read params */
for (i = 1; i < argc; i++) {
char *r;
if(!end_of_options && strcmp(argv[i], "--") == 0) {
end_of_options = 1;
continue;
}
r = feedarg(argv[i], params, !end_of_options, 1);
if (r != NULL) return(r);
}
/* check if at least a MIDI filename have been provided */
if ((params->midifile == NULL) && (params->playlist == NULL)) {
return("You have to provide the path to a MIDI file or a playlist to play.");
}
/* all good */
return(NULL);
}
/* computes the time elapsed since the song started (in secs). Returns 0 if
* elapsed time didn't changed since last time, non-zero otherwise */
static int compute_elapsed_time(unsigned long starttime, unsigned long *elapsed) {
unsigned long curtime, res;
timer_read(&curtime);
if (curtime < starttime) { /* wraparound detected */
res = (ULONG_MAX - starttime) + curtime;
} else {
res = curtime - starttime;
}
res /= 1000000lu; /* microseconds to seconds */
if (res == *elapsed) return(0);
*elapsed = res;
return(1);
}
/* check the event cache for a given event. to reset the cache, issue a single
* call with trackpos < 0. */
static struct midi_event *getnexteventfromcache(struct midi_event *eventscache, long int trackpos, int xmsdelay) {
static unsigned int itemsincache = 0;
static unsigned int curcachepos = 0;
struct midi_event *res = NULL;
long nextevent;
/* if trackpos < 0 then this is only about flushing cache */
if (trackpos < 0) {
memset(eventscache, 0, sizeof(*eventscache));
itemsincache = 0;
curcachepos = 0;
return(NULL);
}
/* if we have available cache */
if (itemsincache > 0) {
curcachepos++;
curcachepos &= EVENTSCACHEMASK;
itemsincache--;
res = &eventscache[curcachepos];
/* if we have some free time, refill the cache proactively */
if (res->deltatime > 0) {
int nextslot, pullres;
/* sleep 2ms after a MIDI OUT write, and before accessing XMS.
This is especially important for SoundBlaster "AWE" cards with the
AWEUTIL TSR midi emulation enabled, without this AWEUTIL crashes. */
if (xmsdelay != 0) udelay(2000);
nextslot = curcachepos + itemsincache;
nextevent = eventscache[nextslot & EVENTSCACHEMASK].next;
while ((itemsincache < EVENTSCACHESIZE - 1) && (nextevent >= 0)) {
nextslot++;
nextslot &= EVENTSCACHEMASK;
pullres = mem_pull(nextevent, &eventscache[nextslot], sizeof(struct midi_event));
if (pullres != 0) {
/* printf("pullevent() ERROR: %u (eventid = %ld)\n", pullres, trackpos); */
return(NULL);
}
nextevent = eventscache[nextslot].next;
itemsincache++;
}
}
} else { /* need to refill the cache NOW */
int refillcount, pullres;
/* sleep 2ms after a MIDI OUT write, and before accessing XMS.
this is especially important for SoundBlaster "AWE" cards with the
AWEUTIL TSR midi emulation enabled, without this AWEUTIL crashes. */
if (xmsdelay != 0) udelay(2000);
nextevent = trackpos;
curcachepos = 0;
for (refillcount = 0; refillcount < EVENTSCACHESIZE; refillcount++) {
pullres = mem_pull(nextevent, &eventscache[refillcount], sizeof(struct midi_event));
if (pullres != 0) {
/* printf("pullevent() ERROR: %u (eventid = %ld)\n", pullres, trackpos); */
return(NULL);
}
nextevent = eventscache[refillcount].next;
itemsincache++;
if (nextevent < 0) break;
}
itemsincache--;
res = eventscache;
}
return(res);
}
/* reads the BLASTER variable for best guessing of current hardware and port.
* If nothing found, fallbacks to MPU and 0x330 */
static void preload_outdev(struct clioptions *params) {
#ifdef HAVE_PORT_IO
char *blaster;
params->port_mpu = 0;
params->port_awe = 0;
params->port_sb = 0;
/* check if a blaster variable is present */
blaster = getenv("BLASTER");
/* if so, read it looking for 'P' and 'E' parameters */
if (blaster != NULL) {
char *blasterptr[16];
int blastercount = 0;
/* read the variable in a first pass to collect all starting points */