ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/prefs.cpp
Revision: 1.2
Committed: 1999-10-19T17:41:18Z (25 years ago) by cebix
Branch: MAIN
CVS Tags: snapshot-21101999
Changes since 1.1: +1 -0 lines
Log Message:
- added external file system
- moved most init/deinit code to InitAll()/ExitAll() in main.cpp

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * prefs.cpp - Preferences handling
3     *
4     * Basilisk II (C) 1997-1999 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
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19     */
20    
21     #include <string.h>
22     #include <stdlib.h>
23     #include <stdio.h>
24     #include <ctype.h>
25    
26     #include "sysdeps.h"
27     #include "sys.h"
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 cebix 1.2 {"extfs", TYPE_STRING, false}, // Root path of ExtFS (extfs.cpp)
40 cebix 1.1 {"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     {"fpu", TYPE_BOOLEAN, false}, // Enable FPU emulation (main_*.cpp)
58     {"nocdrom", TYPE_BOOLEAN, false}, // Don't install CD-ROM driver (cdrom.cpp/rom_patches.cpp)
59     {"nosound", TYPE_BOOLEAN, false}, // Don't enable sound output (audio_*.cpp)
60     {"nogui", TYPE_BOOLEAN, false}, // Disable GUI (main_*.cpp)
61     {NULL, TYPE_END, false} // End of list
62     };
63    
64    
65     // Prefs item are stored in a linked list of these nodes
66     struct prefs_node {
67     prefs_node *next;
68     const char *name;
69     prefs_type type;
70     void *data;
71     };
72    
73     // List of prefs nodes
74     static prefs_node *the_prefs;
75    
76    
77     /*
78     * Initialize preferences
79     */
80    
81     void PrefsInit(void)
82     {
83     // Start with empty list
84     the_prefs = NULL;
85    
86     // Set defaults
87     SysAddSerialPrefs();
88     PrefsAddInt16("bootdriver", 0);
89     PrefsAddInt16("bootdrive", 0);
90     PrefsAddInt32("ramsize", 8 * 1024 * 1024);
91     PrefsAddInt32("frameskip", 6);
92     PrefsAddInt32("modelid", 5); // Mac IIci
93     PrefsAddBool("fpu", false);
94     PrefsAddBool("nocdrom", false);
95     PrefsAddBool("nosound", false);
96     PrefsAddBool("nogui", false);
97     AddPlatformPrefsDefaults();
98    
99     // Load preferences from settings file
100     LoadPrefs();
101     }
102    
103    
104     /*
105     * Deinitialize preferences
106     */
107    
108     void PrefsExit(void)
109     {
110     // Free prefs list
111     prefs_node *p = the_prefs, *next;
112     while (p) {
113     next = p->next;
114     free((void *)p->name);
115     free(p->data);
116     delete p;
117     p = next;
118     }
119     }
120    
121    
122     /*
123     * Find preferences descriptor by keyword
124     */
125    
126     static const prefs_desc *find_prefs_desc(const char *name, const prefs_desc *list)
127     {
128     while (list->type != TYPE_ANY) {
129     if (strcmp(list->name, name) == 0)
130     return list;
131     list++;
132     }
133     return NULL;
134     }
135    
136    
137     /*
138     * Set prefs items
139     */
140    
141     static void add_data(const char *name, prefs_type type, void *data, int size)
142     {
143     void *d = malloc(size);
144     if (d == NULL)
145     return;
146     memcpy(d, data, size);
147     prefs_node *p = new prefs_node;
148     p->next = 0;
149     p->name = strdup(name);
150     p->type = type;
151     p->data = d;
152     if (the_prefs) {
153     prefs_node *prev = the_prefs;
154     while (prev->next)
155     prev = prev->next;
156     prev->next = p;
157     } else
158     the_prefs = p;
159     }
160    
161     void PrefsAddString(const char *name, const char *s)
162     {
163     add_data(name, TYPE_STRING, (void *)s, strlen(s) + 1);
164     }
165    
166     void PrefsAddBool(const char *name, bool b)
167     {
168     add_data(name, TYPE_BOOLEAN, &b, sizeof(bool));
169     }
170    
171     void PrefsAddInt16(const char *name, int16 val)
172     {
173     add_data(name, TYPE_INT16, &val, sizeof(int16));
174     }
175    
176     void PrefsAddInt32(const char *name, int32 val)
177     {
178     add_data(name, TYPE_INT32, &val, sizeof(int32));
179     }
180    
181    
182     /*
183     * Replace prefs items
184     */
185    
186     static prefs_node *find_node(const char *name, prefs_type type, int index = 0)
187     {
188     prefs_node *p = the_prefs;
189     int i = 0;
190     while (p) {
191     if ((type == TYPE_ANY || p->type == type) && !strcmp(p->name, name)) {
192     if (i == index)
193     return p;
194     else
195     i++;
196     }
197     p = p->next;
198     }
199     return NULL;
200     }
201    
202     void PrefsReplaceString(const char *name, const char *s, int index)
203     {
204     prefs_node *p = find_node(name, TYPE_STRING, index);
205     if (p) {
206     free(p->data);
207     p->data = strdup(s);
208     } else
209     add_data(name, TYPE_STRING, (void *)s, strlen(s) + 1);
210     }
211    
212     void PrefsReplaceBool(const char *name, bool b)
213     {
214     prefs_node *p = find_node(name, TYPE_BOOLEAN);
215     if (p)
216     *(bool *)(p->data) = b;
217     else
218     add_data(name, TYPE_BOOLEAN, &b, sizeof(bool));
219     }
220    
221     void PrefsReplaceInt16(const char *name, int16 val)
222     {
223     prefs_node *p = find_node(name, TYPE_INT16);
224     if (p)
225     *(int16 *)(p->data) = val;
226     else
227     add_data(name, TYPE_INT16, &val, sizeof(int16));
228     }
229    
230     void PrefsReplaceInt32(const char *name, int32 val)
231     {
232     prefs_node *p = find_node(name, TYPE_INT32);
233     if (p)
234     *(int32 *)(p->data) = val;
235     else
236     add_data(name, TYPE_INT32, &val, sizeof(int32));
237     }
238    
239    
240     /*
241     * Get prefs items
242     */
243    
244     const char *PrefsFindString(const char *name, int index)
245     {
246     prefs_node *p = find_node(name, TYPE_STRING, index);
247     if (p)
248     return (char *)(p->data);
249     else
250     return NULL;
251     }
252    
253     bool PrefsFindBool(const char *name)
254     {
255     prefs_node *p = find_node(name, TYPE_BOOLEAN, 0);
256     if (p)
257     return *(bool *)(p->data);
258     else
259     return false;
260     }
261    
262     int16 PrefsFindInt16(const char *name)
263     {
264     prefs_node *p = find_node(name, TYPE_INT16, 0);
265     if (p)
266     return *(int16 *)(p->data);
267     else
268     return 0;
269     }
270    
271     int32 PrefsFindInt32(const char *name)
272     {
273     prefs_node *p = find_node(name, TYPE_INT32, 0);
274     if (p)
275     return *(int32 *)(p->data);
276     else
277     return 0;
278     }
279    
280    
281     /*
282     * Remove prefs items
283     */
284    
285     void PrefsRemoveItem(const char *name, int index)
286     {
287     prefs_node *p = find_node(name, TYPE_ANY, index);
288     if (p) {
289     free((void *)p->name);
290     free(p->data);
291     prefs_node *q = the_prefs;
292     if (q == p) {
293     the_prefs = NULL;
294     delete p;
295     return;
296     }
297     while (q) {
298     if (q->next == p) {
299     q->next = p->next;
300     delete p;
301     return;
302     }
303     q = q->next;
304     }
305     }
306     }
307    
308    
309     /*
310     * Load prefs from stream (utility function for LoadPrefs() implementation)
311     */
312    
313     void LoadPrefsFromStream(FILE *f)
314     {
315     char line[256];
316     while(fgets(line, 255, f)) {
317     // Read line
318     int len = strlen(line);
319     if (len == 0)
320     continue;
321     line[len-1] = 0;
322    
323     // Comments begin with "#" or ";"
324     if (line[0] == '#' || line[0] == ';')
325     continue;
326    
327     // Terminate string after keyword
328     char *p = line;
329     while (!isspace(*p)) p++;
330     *p++ = 0;
331    
332     // Skip whitespace until value
333     while (isspace(*p)) p++;
334     if (*p == 0)
335     continue;
336     char *keyword = line;
337     char *value = p;
338     int32 i = atol(value);
339    
340     // Look for keyword first in common item list, then in platform specific list
341     const prefs_desc *desc = find_prefs_desc(keyword, common_prefs_items);
342     if (desc == NULL)
343     desc = find_prefs_desc(keyword, platform_prefs_items);
344     if (desc == NULL) {
345     printf("WARNING: Unknown preferences keyword '%s'\n", keyword);
346     continue;
347     }
348    
349     // Add item to prefs
350     switch (desc->type) {
351     case TYPE_STRING:
352     if (desc->multiple)
353     PrefsAddString(keyword, value);
354     else
355     PrefsReplaceString(keyword, value);
356     break;
357     case TYPE_BOOLEAN:
358     PrefsReplaceBool(keyword, !strcmp(value, "true"));
359     break;
360     case TYPE_INT16:
361     PrefsReplaceInt16(keyword, i);
362     break;
363     case TYPE_INT32:
364     PrefsReplaceInt32(keyword, i);
365     break;
366     default:
367     break;
368     }
369     }
370     }
371    
372    
373     /*
374     * Save settings to stream (utility function for SavePrefs() implementation)
375     */
376    
377     static void write_prefs(FILE *f, const prefs_desc *list)
378     {
379     while (list->type != TYPE_ANY) {
380     switch (list->type) {
381     case TYPE_STRING: {
382     int index = 0;
383     const char *str;
384     while ((str = PrefsFindString(list->name, index++)) != NULL)
385     fprintf(f, "%s %s\n", list->name, str);
386     break;
387     }
388     case TYPE_BOOLEAN:
389     fprintf(f, "%s %s\n", list->name, PrefsFindBool(list->name) ? "true" : "false");
390     break;
391     case TYPE_INT16:
392     fprintf(f, "%s %d\n", list->name, PrefsFindInt16(list->name));
393     break;
394     case TYPE_INT32:
395     fprintf(f, "%s %ld\n", list->name, PrefsFindInt32(list->name));
396     break;
397     default:
398     break;
399     }
400     list++;
401     }
402     }
403    
404     void SavePrefsToStream(FILE *f)
405     {
406     write_prefs(f, common_prefs_items);
407     write_prefs(f, platform_prefs_items);
408     }