ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/prefs.cpp
(Generate patch)

Comparing BasiliskII/src/prefs.cpp (file contents):
Revision 1.1.1.1 by cebix, 1999-10-03T14:16:25Z vs.
Revision 1.18 by asvitkine, 2009-07-23T19:19:14Z

# Line 1 | Line 1
1   /*
2   *  prefs.cpp - Preferences handling
3   *
4 < *  Basilisk II (C) 1997-1999 Christian Bauer
4 > *  Basilisk II (C) 1997-2008 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
# Line 28 | Line 28
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 <        {"scsi0", TYPE_STRING, false},          // SCSI targets for Mac SCSI ID 0..6 (scsi_*.cpp)
40 <        {"scsi1", TYPE_STRING, false},
41 <        {"scsi2", TYPE_STRING, false},
42 <        {"scsi3", TYPE_STRING, false},
43 <        {"scsi4", TYPE_STRING, false},
44 <        {"scsi5", TYPE_STRING, false},
45 <        {"scsi6", TYPE_STRING, false},
46 <        {"screen", TYPE_STRING, false},         // Video mode (video.cpp)
47 <        {"seriala", TYPE_STRING, false},        // Device name of Mac serial port A (serial_*.cpp)
48 <        {"serialb", TYPE_STRING, false},        // Device name of Mac serial port B (serial_*.cpp)
49 <        {"ether", TYPE_STRING, false},          // Device name of Mac ethernet adapter (ether_*.cpp)
50 <        {"rom", TYPE_STRING, false},            // Path of ROM file (main_*.cpp)
51 <        {"bootdrive", TYPE_INT16, false},       // Boot drive number (main_*.cpp)
52 <        {"bootdriver", TYPE_INT16, false},      // Boot driver number (main_*.cpp)
53 <        {"ramsize", TYPE_INT32, false},         // Size of Mac RAM in bytes (main_*.cpp)
54 <        {"frameskip", TYPE_INT32, false},       // Number of frames to skip in refreshed video modes (video_*.cpp)
55 <        {"modelid", TYPE_INT32, false},         // Mac Model ID (Gestalt Model ID minus 6) (rom_patches.cpp)
56 <        {"fpu", TYPE_BOOLEAN, false},           // Enable FPU emulation (main_*.cpp)
57 <        {"nocdrom", TYPE_BOOLEAN, false},       // Don't install CD-ROM driver (cdrom.cpp/rom_patches.cpp)
58 <        {"nosound", TYPE_BOOLEAN, false},       // Don't enable sound output (audio_*.cpp)
59 <        {"nogui", TYPE_BOOLEAN, false},         // Disable GUI (main_*.cpp)
60 <        {NULL, TYPE_END, false} // End of list
61 < };
62 <
63 <
64 < // 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;
# Line 70 | Line 37 | struct prefs_node {
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(const char *vmdir, int &argc, char **&argv)
51   {
82        // Start with empty list
83        the_prefs = NULL;
84
52          // Set defaults
53 <        SysAddSerialPrefs();
87 <        PrefsAddInt16("bootdriver", 0);
88 <        PrefsAddInt16("bootdrive", 0);
89 <        PrefsAddInt32("ramsize", 8 * 1024 * 1024);
90 <        PrefsAddInt32("frameskip", 6);
91 <        PrefsAddInt32("modelid", 5);    // Mac IIci
92 <        PrefsAddBool("fpu", false);
93 <        PrefsAddBool("nocdrom", false);
94 <        PrefsAddBool("nosound", false);
95 <        PrefsAddBool("nogui", false);
53 >        AddPrefsDefaults();
54          AddPlatformPrefsDefaults();
55  
56          // Load preferences from settings file
57 <        LoadPrefs();
57 >        LoadPrefs(vmdir);
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 (!option || 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 > #ifdef SHEEPSHAVER
126 >        // System specific initialization
127 >        prefs_init();
128 > #endif
129   }
130  
131  
# Line 106 | Line 135 | void PrefsInit(void)
135  
136   void PrefsExit(void)
137   {
138 + #ifdef SHEEPSHAVER
139 +        // System specific deinitialization
140 +        prefs_exit();
141 + #endif
142 +
143          // Free prefs list
144          prefs_node *p = the_prefs, *next;
145          while (p) {
# Line 115 | Line 149 | void PrefsExit(void)
149                  delete p;
150                  p = next;
151          }
152 +        the_prefs = NULL;
153 + }
154 +
155 +
156 + /*
157 + *  Print preferences options help
158 + */
159 +
160 + static void print_options(const prefs_desc *list)
161 + {
162 +        while (list->type != TYPE_END) {
163 +                if (list->help) {
164 +                        const char *typestr, *defstr;
165 +                        char numstr[32];
166 +                        switch (list->type) {
167 +                                case TYPE_STRING:
168 +                                        typestr = "STRING";
169 +                                        defstr = PrefsFindString(list->name);
170 +                                        if (defstr == NULL)
171 +                                                defstr = "none";
172 +                                        break;
173 +                                case TYPE_BOOLEAN:
174 +                                        typestr = "BOOL";
175 +                                        if (PrefsFindBool(list->name))
176 +                                                defstr = "true";
177 +                                        else
178 +                                                defstr = "false";
179 +                                        break;
180 +                                case TYPE_INT32:
181 +                                        typestr = "NUMBER";
182 +                                        sprintf(numstr, "%d", PrefsFindInt32(list->name));
183 +                                        defstr = numstr;
184 +                                        break;
185 +                                default:
186 +                                        typestr = "<unknown>";
187 +                                        defstr = "none";
188 +                                        break;
189 +                        }
190 +                        printf("  --%s %s\n    %s [default=%s]\n", list->name, typestr, list->help, defstr);
191 +                }
192 +                list++;
193 +        }
194 + }
195 +
196 + void PrefsPrintUsage(void)
197 + {
198 +        printf("\nGeneral options:\n");
199 +        print_options(common_prefs_items);
200 +        printf("\nPlatform-specific options:\n");
201 +        print_options(platform_prefs_items);
202 +        printf("\nBoolean options are specified as '--OPTION true|on|yes' or\n'--OPTION false|off|no'.\n");
203   }
204  
205  
# Line 132 | Line 217 | static const prefs_desc *find_prefs_desc
217          return NULL;
218   }
219  
220 + static const prefs_desc *find_prefs_desc(const char *name)
221 + {
222 +        const prefs_desc *d = find_prefs_desc(name, common_prefs_items);
223 +        if (d == NULL)
224 +                d = find_prefs_desc(name, platform_prefs_items);
225 +        return d;
226 + }
227 +
228  
229   /*
230   *  Set prefs items
# Line 167 | Line 260 | void PrefsAddBool(const char *name, bool
260          add_data(name, TYPE_BOOLEAN, &b, sizeof(bool));
261   }
262  
170 void PrefsAddInt16(const char *name, int16 val)
171 {
172        add_data(name, TYPE_INT16, &val, sizeof(int16));
173 }
174
263   void PrefsAddInt32(const char *name, int32 val)
264   {
265          add_data(name, TYPE_INT32, &val, sizeof(int32));
# Line 217 | Line 305 | void PrefsReplaceBool(const char *name,
305                  add_data(name, TYPE_BOOLEAN, &b, sizeof(bool));
306   }
307  
220 void PrefsReplaceInt16(const char *name, int16 val)
221 {
222        prefs_node *p = find_node(name, TYPE_INT16);
223        if (p)
224                *(int16 *)(p->data) = val;
225        else
226                add_data(name, TYPE_INT16, &val, sizeof(int16));
227 }
228
308   void PrefsReplaceInt32(const char *name, int32 val)
309   {
310          prefs_node *p = find_node(name, TYPE_INT32);
# Line 258 | Line 337 | bool PrefsFindBool(const char *name)
337                  return false;
338   }
339  
261 int16 PrefsFindInt16(const char *name)
262 {
263        prefs_node *p = find_node(name, TYPE_INT16, 0);
264        if (p)
265                return *(int16 *)(p->data);
266        else
267                return 0;
268 }
269
340   int32 PrefsFindInt32(const char *name)
341   {
342          prefs_node *p = find_node(name, TYPE_INT32, 0);
# Line 330 | Line 400 | void LoadPrefsFromStream(FILE *f)
400  
401                  // Skip whitespace until value
402                  while (isspace(*p)) p++;
333                if (*p == 0)
334                        continue;
403                  char *keyword = line;
404                  char *value = p;
405                  int32 i = atol(value);
406  
407 <                // Look for keyword first in common item list, then in platform specific list
408 <                const prefs_desc *desc = find_prefs_desc(keyword, common_prefs_items);
341 <                if (desc == NULL)
342 <                        desc = find_prefs_desc(keyword, platform_prefs_items);
407 >                // Look for keyword first in prefs item list
408 >                const prefs_desc *desc = find_prefs_desc(keyword);
409                  if (desc == NULL) {
410                          printf("WARNING: Unknown preferences keyword '%s'\n", keyword);
411                          continue;
# Line 356 | Line 422 | void LoadPrefsFromStream(FILE *f)
422                          case TYPE_BOOLEAN:
423                                  PrefsReplaceBool(keyword, !strcmp(value, "true"));
424                                  break;
359                        case TYPE_INT16:
360                                PrefsReplaceInt16(keyword, i);
361                                break;
425                          case TYPE_INT32:
426                                  PrefsReplaceInt32(keyword, i);
427                                  break;
# Line 387 | Line 450 | static void write_prefs(FILE *f, const p
450                          case TYPE_BOOLEAN:
451                                  fprintf(f, "%s %s\n", list->name, PrefsFindBool(list->name) ? "true" : "false");
452                                  break;
390                        case TYPE_INT16:
391                                fprintf(f, "%s %d\n", list->name, PrefsFindInt16(list->name));
392                                break;
453                          case TYPE_INT32:
454 <                                fprintf(f, "%s %ld\n", list->name, PrefsFindInt32(list->name));
454 >                                fprintf(f, "%s %d\n", list->name, PrefsFindInt32(list->name));
455                                  break;
456                          default:
457                                  break;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines