1 |
|
/* |
2 |
|
* prefs.cpp - Preferences handling |
3 |
|
* |
4 |
< |
* Basilisk II (C) 1997-2000 Christian Bauer |
4 |
> |
* Basilisk II (C) 1997-2002 Christian Bauer |
5 |
|
* |
6 |
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
* it under the terms of the GNU General Public License as published by |
28 |
|
#include "prefs.h" |
29 |
|
|
30 |
|
|
31 |
< |
// Common preferences items (those which exist on all platforms) |
32 |
< |
// Except for "disk", "floppy", "cdrom", "scsiX", "screen", "rom" and "ether", |
33 |
< |
// these are guaranteed to be in the prefs; "disk", "floppy" and "cdrom" can |
34 |
< |
// occur multiple times |
35 |
< |
prefs_desc common_prefs_items[] = { |
36 |
< |
{"disk", TYPE_STRING, true}, // Device/file names of Mac volumes (disk.cpp) |
37 |
< |
{"floppy", TYPE_STRING, true}, // Device/file names of Mac floppy drives (sony.cpp) |
38 |
< |
{"cdrom", TYPE_STRING, true}, // Device/file names of Mac CD-ROM drives (cdrom.cpp) |
39 |
< |
{"extfs", TYPE_STRING, false}, // Root path of ExtFS (extfs.cpp) |
40 |
< |
{"scsi0", TYPE_STRING, false}, // SCSI targets for Mac SCSI ID 0..6 (scsi_*.cpp) |
41 |
< |
{"scsi1", TYPE_STRING, false}, |
42 |
< |
{"scsi2", TYPE_STRING, false}, |
43 |
< |
{"scsi3", TYPE_STRING, false}, |
44 |
< |
{"scsi4", TYPE_STRING, false}, |
45 |
< |
{"scsi5", TYPE_STRING, false}, |
46 |
< |
{"scsi6", TYPE_STRING, false}, |
47 |
< |
{"screen", TYPE_STRING, false}, // Video mode (video.cpp) |
48 |
< |
{"seriala", TYPE_STRING, false}, // Device name of Mac serial port A (serial_*.cpp) |
49 |
< |
{"serialb", TYPE_STRING, false}, // Device name of Mac serial port B (serial_*.cpp) |
50 |
< |
{"ether", TYPE_STRING, false}, // Device name of Mac ethernet adapter (ether_*.cpp) |
51 |
< |
{"rom", TYPE_STRING, false}, // Path of ROM file (main_*.cpp) |
52 |
< |
{"bootdrive", TYPE_INT16, false}, // Boot drive number (main.cpp) |
53 |
< |
{"bootdriver", TYPE_INT16, false}, // Boot driver number (main.cpp) |
54 |
< |
{"ramsize", TYPE_INT32, false}, // Size of Mac RAM in bytes (main_*.cpp) |
55 |
< |
{"frameskip", TYPE_INT32, false}, // Number of frames to skip in refreshed video modes (video_*.cpp) |
56 |
< |
{"modelid", TYPE_INT32, false}, // Mac Model ID (Gestalt Model ID minus 6) (rom_patches.cpp) |
57 |
< |
{"cpu", TYPE_INT32, false}, // CPU type (0 = 68000, 1 = 68010 etc.) (main.cpp) |
58 |
< |
{"fpu", TYPE_BOOLEAN, false}, // Enable FPU emulation (main.cpp) |
59 |
< |
{"nocdrom", TYPE_BOOLEAN, false}, // Don't install CD-ROM driver (cdrom.cpp/rom_patches.cpp) |
60 |
< |
{"nosound", TYPE_BOOLEAN, false}, // Don't enable sound output (audio_*.cpp) |
61 |
< |
{"nogui", TYPE_BOOLEAN, false}, // Disable GUI (main_*.cpp) |
62 |
< |
{NULL, TYPE_END, false} // End of list |
63 |
< |
}; |
64 |
< |
|
65 |
< |
|
66 |
< |
// Prefs item are stored in a linked list of these nodes |
31 |
> |
// Prefs items are stored in a linked list of these nodes |
32 |
|
struct prefs_node { |
33 |
|
prefs_node *next; |
34 |
|
const char *name; |
37 |
|
}; |
38 |
|
|
39 |
|
// List of prefs nodes |
40 |
< |
static prefs_node *the_prefs; |
40 |
> |
static prefs_node *the_prefs = NULL; |
41 |
> |
|
42 |
> |
// Prototypes |
43 |
> |
static const prefs_desc *find_prefs_desc(const char *name); |
44 |
|
|
45 |
|
|
46 |
|
/* |
47 |
|
* Initialize preferences |
48 |
|
*/ |
49 |
|
|
50 |
< |
void PrefsInit(void) |
50 |
> |
void PrefsInit(int &argc, char **&argv) |
51 |
|
{ |
84 |
– |
// Start with empty list |
85 |
– |
the_prefs = NULL; |
86 |
– |
|
52 |
|
// Set defaults |
53 |
< |
SysAddSerialPrefs(); |
89 |
< |
PrefsAddInt16("bootdriver", 0); |
90 |
< |
PrefsAddInt16("bootdrive", 0); |
91 |
< |
PrefsAddInt32("ramsize", 8 * 1024 * 1024); |
92 |
< |
PrefsAddInt32("frameskip", 6); |
93 |
< |
PrefsAddInt32("modelid", 5); // Mac IIci |
94 |
< |
PrefsAddInt32("cpu", 3); // 68030 |
95 |
< |
PrefsAddBool("fpu", false); |
96 |
< |
PrefsAddBool("nocdrom", false); |
97 |
< |
PrefsAddBool("nosound", false); |
98 |
< |
PrefsAddBool("nogui", false); |
53 |
> |
AddPrefsDefaults(); |
54 |
|
AddPlatformPrefsDefaults(); |
55 |
|
|
56 |
|
// Load preferences from settings file |
57 |
|
LoadPrefs(); |
58 |
+ |
|
59 |
+ |
// Override prefs with command line options |
60 |
+ |
for (int i=1; i<argc; i++) { |
61 |
+ |
|
62 |
+ |
// Options are of the form '--keyword' |
63 |
+ |
const char *option = argv[i]; |
64 |
+ |
if (strlen(option) < 3 || option[0] != '-' || option[1] != '-') |
65 |
+ |
continue; |
66 |
+ |
const char *keyword = option + 2; |
67 |
+ |
|
68 |
+ |
// Find descriptor for keyword |
69 |
+ |
const prefs_desc *d = find_prefs_desc(keyword); |
70 |
+ |
if (d == NULL) |
71 |
+ |
continue; |
72 |
+ |
argv[i] = NULL; |
73 |
+ |
|
74 |
+ |
// Get value |
75 |
+ |
i++; |
76 |
+ |
if (i >= argc) { |
77 |
+ |
fprintf(stderr, "Option '%s' must be followed by a value\n", option); |
78 |
+ |
continue; |
79 |
+ |
} |
80 |
+ |
const char *value = argv[i]; |
81 |
+ |
argv[i] = NULL; |
82 |
+ |
|
83 |
+ |
// Add/replace prefs item |
84 |
+ |
switch (d->type) { |
85 |
+ |
case TYPE_STRING: |
86 |
+ |
if (d->multiple) |
87 |
+ |
PrefsAddString(keyword, value); |
88 |
+ |
else |
89 |
+ |
PrefsReplaceString(keyword, value); |
90 |
+ |
break; |
91 |
+ |
|
92 |
+ |
case TYPE_BOOLEAN: { |
93 |
+ |
if (!strcmp(value, "true") || !strcmp(value, "on") || !strcmp(value, "yes")) |
94 |
+ |
PrefsReplaceBool(keyword, true); |
95 |
+ |
else if (!strcmp(value, "false") || !strcmp(value, "off") || !strcmp(value, "no")) |
96 |
+ |
PrefsReplaceBool(keyword, false); |
97 |
+ |
else |
98 |
+ |
fprintf(stderr, "Value for option '%s' must be 'true' or 'false'\n", option); |
99 |
+ |
break; |
100 |
+ |
} |
101 |
+ |
|
102 |
+ |
case TYPE_INT32: |
103 |
+ |
PrefsReplaceInt32(keyword, atoi(value)); |
104 |
+ |
break; |
105 |
+ |
|
106 |
+ |
default: |
107 |
+ |
break; |
108 |
+ |
} |
109 |
+ |
} |
110 |
+ |
|
111 |
+ |
// Remove processed arguments |
112 |
+ |
for (int i=1; i<argc; i++) { |
113 |
+ |
int k; |
114 |
+ |
for (k=i; k<argc; k++) |
115 |
+ |
if (argv[k] != NULL) |
116 |
+ |
break; |
117 |
+ |
if (k > i) { |
118 |
+ |
k -= i; |
119 |
+ |
for (int j=i+k; j<argc; j++) |
120 |
+ |
argv[j-k] = argv[j]; |
121 |
+ |
argc -= k; |
122 |
+ |
} |
123 |
+ |
} |
124 |
|
} |
125 |
|
|
126 |
|
|
139 |
|
delete p; |
140 |
|
p = next; |
141 |
|
} |
142 |
+ |
the_prefs = NULL; |
143 |
+ |
} |
144 |
+ |
|
145 |
+ |
|
146 |
+ |
/* |
147 |
+ |
* Print preferences options help |
148 |
+ |
*/ |
149 |
+ |
|
150 |
+ |
static void print_options(const prefs_desc *list) |
151 |
+ |
{ |
152 |
+ |
while (list->type != TYPE_END) { |
153 |
+ |
if (list->help) { |
154 |
+ |
const char *typestr, *defstr; |
155 |
+ |
char numstr[32]; |
156 |
+ |
switch (list->type) { |
157 |
+ |
case TYPE_STRING: |
158 |
+ |
typestr = "STRING"; |
159 |
+ |
defstr = PrefsFindString(list->name); |
160 |
+ |
if (defstr == NULL) |
161 |
+ |
defstr = "none"; |
162 |
+ |
break; |
163 |
+ |
case TYPE_BOOLEAN: |
164 |
+ |
typestr = "BOOL"; |
165 |
+ |
if (PrefsFindBool(list->name)) |
166 |
+ |
defstr = "true"; |
167 |
+ |
else |
168 |
+ |
defstr = "false"; |
169 |
+ |
break; |
170 |
+ |
case TYPE_INT32: |
171 |
+ |
typestr = "NUMBER"; |
172 |
+ |
sprintf(numstr, "%d", PrefsFindInt32(list->name)); |
173 |
+ |
defstr = numstr; |
174 |
+ |
break; |
175 |
+ |
default: |
176 |
+ |
typestr = "<unknown>"; |
177 |
+ |
defstr = "none"; |
178 |
+ |
break; |
179 |
+ |
} |
180 |
+ |
printf(" --%s %s\n %s [default=%s]\n", list->name, typestr, list->help, defstr); |
181 |
+ |
} |
182 |
+ |
list++; |
183 |
+ |
} |
184 |
+ |
} |
185 |
+ |
|
186 |
+ |
void PrefsPrintUsage(void) |
187 |
+ |
{ |
188 |
+ |
printf("\nGeneral options:\n"); |
189 |
+ |
print_options(common_prefs_items); |
190 |
+ |
printf("\nPlatform-specific options:\n"); |
191 |
+ |
print_options(platform_prefs_items); |
192 |
+ |
printf("\nBoolean options are specified as '--OPTION true|on|yes' or\n'--OPTION false|off|no'.\n"); |
193 |
|
} |
194 |
|
|
195 |
|
|
207 |
|
return NULL; |
208 |
|
} |
209 |
|
|
210 |
+ |
static const prefs_desc *find_prefs_desc(const char *name) |
211 |
+ |
{ |
212 |
+ |
const prefs_desc *d = find_prefs_desc(name, common_prefs_items); |
213 |
+ |
if (d == NULL) |
214 |
+ |
d = find_prefs_desc(name, platform_prefs_items); |
215 |
+ |
return d; |
216 |
+ |
} |
217 |
+ |
|
218 |
|
|
219 |
|
/* |
220 |
|
* Set prefs items |
250 |
|
add_data(name, TYPE_BOOLEAN, &b, sizeof(bool)); |
251 |
|
} |
252 |
|
|
173 |
– |
void PrefsAddInt16(const char *name, int16 val) |
174 |
– |
{ |
175 |
– |
add_data(name, TYPE_INT16, &val, sizeof(int16)); |
176 |
– |
} |
177 |
– |
|
253 |
|
void PrefsAddInt32(const char *name, int32 val) |
254 |
|
{ |
255 |
|
add_data(name, TYPE_INT32, &val, sizeof(int32)); |
295 |
|
add_data(name, TYPE_BOOLEAN, &b, sizeof(bool)); |
296 |
|
} |
297 |
|
|
223 |
– |
void PrefsReplaceInt16(const char *name, int16 val) |
224 |
– |
{ |
225 |
– |
prefs_node *p = find_node(name, TYPE_INT16); |
226 |
– |
if (p) |
227 |
– |
*(int16 *)(p->data) = val; |
228 |
– |
else |
229 |
– |
add_data(name, TYPE_INT16, &val, sizeof(int16)); |
230 |
– |
} |
231 |
– |
|
298 |
|
void PrefsReplaceInt32(const char *name, int32 val) |
299 |
|
{ |
300 |
|
prefs_node *p = find_node(name, TYPE_INT32); |
327 |
|
return false; |
328 |
|
} |
329 |
|
|
264 |
– |
int16 PrefsFindInt16(const char *name) |
265 |
– |
{ |
266 |
– |
prefs_node *p = find_node(name, TYPE_INT16, 0); |
267 |
– |
if (p) |
268 |
– |
return *(int16 *)(p->data); |
269 |
– |
else |
270 |
– |
return 0; |
271 |
– |
} |
272 |
– |
|
330 |
|
int32 PrefsFindInt32(const char *name) |
331 |
|
{ |
332 |
|
prefs_node *p = find_node(name, TYPE_INT32, 0); |
394 |
|
char *value = p; |
395 |
|
int32 i = atol(value); |
396 |
|
|
397 |
< |
// Look for keyword first in common item list, then in platform specific list |
398 |
< |
const prefs_desc *desc = find_prefs_desc(keyword, common_prefs_items); |
342 |
< |
if (desc == NULL) |
343 |
< |
desc = find_prefs_desc(keyword, platform_prefs_items); |
397 |
> |
// Look for keyword first in prefs item list |
398 |
> |
const prefs_desc *desc = find_prefs_desc(keyword); |
399 |
|
if (desc == NULL) { |
400 |
|
printf("WARNING: Unknown preferences keyword '%s'\n", keyword); |
401 |
|
continue; |
412 |
|
case TYPE_BOOLEAN: |
413 |
|
PrefsReplaceBool(keyword, !strcmp(value, "true")); |
414 |
|
break; |
360 |
– |
case TYPE_INT16: |
361 |
– |
PrefsReplaceInt16(keyword, i); |
362 |
– |
break; |
415 |
|
case TYPE_INT32: |
416 |
|
PrefsReplaceInt32(keyword, i); |
417 |
|
break; |
440 |
|
case TYPE_BOOLEAN: |
441 |
|
fprintf(f, "%s %s\n", list->name, PrefsFindBool(list->name) ? "true" : "false"); |
442 |
|
break; |
391 |
– |
case TYPE_INT16: |
392 |
– |
fprintf(f, "%s %d\n", list->name, PrefsFindInt16(list->name)); |
393 |
– |
break; |
443 |
|
case TYPE_INT32: |
444 |
< |
fprintf(f, "%s %ld\n", list->name, PrefsFindInt32(list->name)); |
444 |
> |
fprintf(f, "%s %d\n", list->name, PrefsFindInt32(list->name)); |
445 |
|
break; |
446 |
|
default: |
447 |
|
break; |