ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/MacOSX/Launcher/VMSettingsController.mm
Revision: 1.7
Committed: 2009-08-30T17:56:30Z (15 years, 1 month ago) by asvitkine
Branch: MAIN
Changes since 1.6: +7 -0 lines
Log Message:
better default prefs

File Contents

# User Rev Content
1 asvitkine 1.1 /*
2     * VMSettingsController.mm - Preferences editing in Cocoa on Mac OS X
3     *
4     * Copyright (C) 2006-2009 Alexei Svitkine
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     #import "VMSettingsController.h"
22    
23     #import "sysdeps.h"
24     #import "prefs.h"
25    
26     const int CDROMRefNum = -62; // RefNum of driver
27    
28 asvitkine 1.5 #ifdef STANDALONE_PREFS
29 asvitkine 1.1 void prefs_init()
30     {
31     }
32    
33     void prefs_exit()
34     {
35     }
36 asvitkine 1.5 #endif
37 asvitkine 1.1
38     @implementation VMSettingsController
39    
40     + (id) sharedInstance
41     {
42 asvitkine 1.4 static VMSettingsController *_sharedInstance = nil;
43     if (!_sharedInstance) {
44     _sharedInstance = [[VMSettingsController allocWithZone:[self zone]] init];
45     }
46     return _sharedInstance;
47 asvitkine 1.1 }
48    
49     - (id) init
50     {
51 asvitkine 1.2 self = [super initWithWindowNibName:@"VMSettingsWindow"];
52    
53 asvitkine 1.4 cancelWasClicked = NO;
54 asvitkine 1.2
55 asvitkine 1.4 return self;
56 asvitkine 1.1 }
57    
58     - (int) numberOfRowsInTableView: (NSTableView *) table
59     {
60     return [diskArray count];
61     }
62    
63     - (id) tableView: (NSTableView *) table objectValueForTableColumn: (NSTableColumn *) col row: (int) row
64     {
65     return [diskArray objectAtIndex: row];
66     }
67    
68     static NSString *getStringFromPrefs(const char *key)
69     {
70     const char *value = PrefsFindString(key);
71     if (value == NULL)
72     return @"";
73     return [NSString stringWithCString: value];
74     }
75    
76     - (void) setupGUI
77     {
78     diskArray = [[NSMutableArray alloc] init];
79    
80     const char *dsk;
81     int index = 0;
82     while ((dsk = PrefsFindString("disk", index++)) != NULL)
83     [diskArray addObject: [NSString stringWithCString: dsk ]];
84    
85     [disks setDataSource: self];
86     [disks reloadData];
87    
88     int bootdriver = PrefsFindInt32("bootdriver"), active = 0;
89     switch (bootdriver) {
90     case 0: active = 0; break;
91     case CDROMRefNum: active = 1; break;
92     }
93     [bootFrom selectItemAtIndex: active ];
94    
95     [romFile setStringValue: getStringFromPrefs("rom") ];
96     [unixRoot setStringValue: getStringFromPrefs("extfs") ];
97     [disableCdrom setIntValue: PrefsFindBool("nocdrom") ];
98     [ramSize setIntValue: PrefsFindInt32("ramsize") / (1024*1024) ];
99     [ramSizeStepper setIntValue: PrefsFindInt32("ramsize") / (1024*1024) ];
100    
101     int display_type = 0;
102     int dis_width = 640;
103     int dis_height = 480;
104    
105     const char *str = PrefsFindString("screen");
106     if (str != NULL) {
107     if (sscanf(str, "win/%d/%d", &dis_width, &dis_height) == 2)
108     display_type = 0;
109     else if (sscanf(str, "dga/%d/%d", &dis_width, &dis_height) == 2)
110     display_type = 1;
111     }
112    
113     [videoType selectItemAtIndex: display_type ];
114     [width setIntValue: dis_width ];
115     [height setIntValue: dis_height ];
116    
117     int frameskip = PrefsFindInt32("frameskip");
118     int item = -1;
119     switch (frameskip) {
120     case 12: item = 0; break;
121     case 8: item = 1; break;
122     case 6: item = 2; break;
123     case 4: item = 3; break;
124     case 2: item = 4; break;
125     case 1: item = 5; break;
126     case 0: item = 6; break;
127     }
128     if (item >= 0)
129     [refreshRate selectItemAtIndex: item ];
130    
131     [qdAccel setIntValue: PrefsFindBool("gfxaccel") ];
132    
133     [disableSound setIntValue: PrefsFindBool("nosound") ];
134     [outDevice setStringValue: getStringFromPrefs("dsp") ];
135     [mixDevice setStringValue: getStringFromPrefs("mixer") ];
136    
137     [useRawKeyCodes setIntValue: PrefsFindBool("keycodes") ];
138     [rawKeyCodes setStringValue: getStringFromPrefs("keycodefile") ];
139     [rawKeyCodes setEnabled:[useRawKeyCodes intValue]];
140 asvitkine 1.4 [browseRawKeyCodesButton setEnabled:[useRawKeyCodes intValue]];
141 asvitkine 1.1
142     int wheelmode = PrefsFindInt32("mousewheelmode"), wheel = 0;
143     switch (wheelmode) {
144     case 0: wheel = 0; break;
145     case 1: wheel = 1; break;
146     }
147     [mouseWheel selectItemAtIndex: wheel ];
148    
149     [scrollLines setIntValue: PrefsFindInt32("mousewheellines") ];
150     [scrollLinesStepper setIntValue: PrefsFindInt32("mousewheellines") ];
151    
152     [ignoreIllegalMemoryAccesses setIntValue: PrefsFindBool("ignoresegv") ];
153     [ignoreIllegalInstructions setIntValue: PrefsFindBool("ignoreillegal") ];
154     [dontUseCPUWhenIdle setIntValue: PrefsFindBool("idlewait") ];
155     [enableJIT setIntValue: PrefsFindBool("jit") ];
156     [enable68kDREmulator setIntValue: PrefsFindBool("jit68k") ];
157    
158     [modemPort setStringValue: getStringFromPrefs("seriala") ];
159     [printerPort setStringValue: getStringFromPrefs("serialb") ];
160     [ethernetInterface setStringValue: getStringFromPrefs("ether") ];
161     }
162    
163     - (void) editSettingsFor: (NSString *) vmdir sender: (id) sender
164     {
165 asvitkine 1.4 chdir([vmdir fileSystemRepresentation]);
166 asvitkine 1.1 AddPrefsDefaults();
167     AddPlatformPrefsDefaults();
168     LoadPrefs([vmdir fileSystemRepresentation]);
169 asvitkine 1.4 NSWindow *window = [self window];
170     [self setupGUI];
171     [NSApp runModalForWindow:window];
172 asvitkine 1.1 }
173    
174 asvitkine 1.6 - (void) editSettingsForNewVM: (NSString *) vmdir sender: (id) sender
175     {
176     chdir([vmdir fileSystemRepresentation]);
177     AddPrefsDefaults();
178     AddPlatformPrefsDefaults();
179     LoadPrefs([vmdir fileSystemRepresentation]);
180 asvitkine 1.7 PrefsReplaceString("screen", "win/800/600");
181     PrefsReplaceString("extfs", "");
182     PrefsReplaceString("ether", "slirp");
183     PrefsReplaceInt32("ramsize", 64 << 20);
184     PrefsReplaceInt32("frameskip", 2);
185     PrefsReplaceBool("jit", true);
186 asvitkine 1.6 NSWindow *window = [self window];
187 asvitkine 1.7 [self setupGUI];
188 asvitkine 1.6 [NSApp runModalForWindow:window];
189     }
190    
191 asvitkine 1.1 static NSString *makeRelativeIfNecessary(NSString *path)
192     {
193 asvitkine 1.4 char cwd[1024], filename[1024];
194     int cwdlen;
195     strlcpy(filename, [path fileSystemRepresentation], sizeof(filename));
196     getcwd(cwd, sizeof(cwd));
197     cwdlen = strlen(cwd);
198     if (!strncmp(cwd, filename, cwdlen)) {
199     if (cwdlen >= 0 && cwd[cwdlen-1] != '/')
200     cwdlen++;
201     return [NSString stringWithCString: filename + cwdlen];
202     }
203     return path;
204 asvitkine 1.1 }
205    
206     - (IBAction) addDisk: (id) sender
207     {
208     NSOpenPanel *open = [NSOpenPanel openPanel];
209     [open setCanChooseDirectories:NO];
210     [open setAllowsMultipleSelection:NO];
211 asvitkine 1.3 [open setTreatsFilePackagesAsDirectories:YES];
212     [open beginSheetForDirectory: [[NSFileManager defaultManager] currentDirectoryPath]
213 asvitkine 1.1 file: @"Unknown"
214     modalForWindow: [self window]
215     modalDelegate: self
216     didEndSelector: @selector(_addDiskEnd: returnCode: contextInfo:)
217     contextInfo: nil];
218     }
219    
220     - (void) _addDiskEnd: (NSOpenPanel *) open returnCode: (int) theReturnCode contextInfo: (void *) theContextInfo
221     {
222     if (theReturnCode == NSOKButton) {
223 asvitkine 1.4 [diskArray addObject: makeRelativeIfNecessary([open filename])];
224 asvitkine 1.1 [disks reloadData];
225     }
226     }
227    
228     - (IBAction) removeDisk: (id) sender
229     {
230     int selectedRow = [disks selectedRow];
231     if (selectedRow >= 0) {
232     [diskArray removeObjectAtIndex: selectedRow];
233     [disks reloadData];
234     }
235     }
236    
237     - (IBAction) createDisk: (id) sender
238     {
239     NSSavePanel *save = [NSSavePanel savePanel];
240     [save setAccessoryView: diskSaveSize];
241 asvitkine 1.3 [save setTreatsFilePackagesAsDirectories:YES];
242     [save beginSheetForDirectory: [[NSFileManager defaultManager] currentDirectoryPath]
243 asvitkine 1.1 file: @"New.dsk"
244     modalForWindow: [self window]
245     modalDelegate: self
246     didEndSelector: @selector(_createDiskEnd: returnCode: contextInfo:)
247     contextInfo: nil];
248     }
249    
250     - (void) _createDiskEnd: (NSSavePanel *) save returnCode: (int) theReturnCode contextInfo: (void *) theContextInfo
251     {
252     if (theReturnCode == NSOKButton) {
253     int size = [diskSaveSizeField intValue];
254     if (size >= 0 && size <= 10000) {
255     char cmd[1024];
256     snprintf(cmd, sizeof(cmd), "dd if=/dev/zero \"of=%s\" bs=1024k count=%d", [[save filename] UTF8String], [diskSaveSizeField intValue]);
257     int ret = system(cmd);
258     if (ret == 0) {
259 asvitkine 1.4 [diskArray addObject: makeRelativeIfNecessary([save filename])];
260 asvitkine 1.1 [disks reloadData];
261     }
262     }
263     }
264     [(NSData *)theContextInfo release];
265     }
266    
267     - (IBAction) useRawKeyCodesClicked: (id) sender
268     {
269     [rawKeyCodes setEnabled:[useRawKeyCodes intValue]];
270 asvitkine 1.4 [browseRawKeyCodesButton setEnabled:[useRawKeyCodes intValue]];
271 asvitkine 1.1 }
272    
273     - (IBAction) browseForROMFileClicked: (id) sender
274     {
275     NSOpenPanel *open = [NSOpenPanel openPanel];
276     [open setCanChooseDirectories:NO];
277     [open setAllowsMultipleSelection:NO];
278 asvitkine 1.3 [open setTreatsFilePackagesAsDirectories:YES];
279 asvitkine 1.1 [open beginSheetForDirectory: @""
280     file: [romFile stringValue]
281     modalForWindow: [self window]
282     modalDelegate: self
283     didEndSelector: @selector(_browseForROMFileEnd: returnCode: contextInfo:)
284     contextInfo: nil];
285     }
286    
287     - (void) _browseForROMFileEnd: (NSOpenPanel *) open returnCode: (int) theReturnCode contextInfo: (void *) theContextInfo
288     {
289     if (theReturnCode == NSOKButton) {
290 asvitkine 1.4 [romFile setStringValue: makeRelativeIfNecessary([open filename])];
291     }
292 asvitkine 1.1 }
293    
294     - (IBAction) browseForUnixRootClicked: (id) sender
295     {
296     NSOpenPanel *open = [NSOpenPanel openPanel];
297     [open setCanChooseDirectories:YES];
298     [open setCanChooseFiles:NO];
299     [open setAllowsMultipleSelection:NO];
300     [open beginSheetForDirectory: @""
301     file: [unixRoot stringValue]
302     modalForWindow: [self window]
303     modalDelegate: self
304     didEndSelector: @selector(_browseForUnixRootEnd: returnCode: contextInfo:)
305     contextInfo: nil];
306     }
307    
308     - (void) _browseForUnixRootEnd: (NSOpenPanel *) open returnCode: (int) theReturnCode contextInfo: (void *) theContextInfo
309     {
310     if (theReturnCode == NSOKButton) {
311 asvitkine 1.4 [unixRoot setStringValue: makeRelativeIfNecessary([open filename])];
312     }
313     }
314    
315     - (IBAction) browseForKeyCodesFileClicked: (id) sender
316     {
317     NSOpenPanel *open = [NSOpenPanel openPanel];
318     [open setCanChooseDirectories:NO];
319     [open setAllowsMultipleSelection:NO];
320     [open setTreatsFilePackagesAsDirectories:YES];
321     [open beginSheetForDirectory: @""
322     file: [unixRoot stringValue]
323     modalForWindow: [self window]
324     modalDelegate: self
325     didEndSelector: @selector(_browseForKeyCodesFileEnd: returnCode: contextInfo:)
326     contextInfo: nil];
327     }
328    
329     - (void) _browseForKeyCodesFileEnd: (NSOpenPanel *) open returnCode: (int) theReturnCode contextInfo: (void *) theContextInfo
330     {
331     if (theReturnCode == NSOKButton) {
332     [rawKeyCodes setStringValue: makeRelativeIfNecessary([open filename])];
333 asvitkine 1.1 }
334     }
335    
336     - (void) cancelEdit: (id) sender
337     {
338 asvitkine 1.5 #ifdef STANDALONE_PREFS
339 asvitkine 1.1 PrefsExit();
340 asvitkine 1.5 #endif
341 asvitkine 1.4 [[self window] close];
342     [NSApp stopModal];
343     cancelWasClicked = YES;
344 asvitkine 1.1 }
345    
346     - (void) saveChanges: (id) sender
347     {
348     while (PrefsFindString("disk"))
349     PrefsRemoveItem("disk");
350    
351     for (int i = 0; i < [diskArray count]; i++) {
352     PrefsAddString("disk", [[diskArray objectAtIndex:i] UTF8String]);
353     }
354     PrefsReplaceInt32("bootdriver", ([bootFrom indexOfSelectedItem] == 1 ? CDROMRefNum : 0));
355     PrefsReplaceString("rom", [[romFile stringValue] UTF8String]);
356     PrefsReplaceString("extfs", [[unixRoot stringValue] UTF8String]);
357     PrefsReplaceBool("nocdrom", [disableCdrom intValue]);
358     PrefsReplaceInt32("ramsize", [ramSize intValue] << 20);
359    
360     char pref[256];
361     snprintf(pref, sizeof(pref), "%s/%d/%d", [videoType indexOfSelectedItem] == 0 ? "win" : "dga", [width intValue], [height intValue]);
362     PrefsReplaceString("screen", pref);
363    
364     int rate = 8;
365     switch ([refreshRate indexOfSelectedItem]) {
366     case 0: rate = 12; break;
367     case 1: rate = 8; break;
368     case 2: rate = 6; break;
369     case 3: rate = 4; break;
370     case 4: rate = 2; break;
371     case 5: rate = 1; break;
372     case 6: rate = 0; break;
373     }
374     PrefsReplaceInt32("frameskip", rate);
375     PrefsReplaceBool("gfxaccel", [qdAccel intValue]);
376    
377     PrefsReplaceBool("nosound", [disableSound intValue]);
378     PrefsReplaceString("dsp", [[outDevice stringValue] UTF8String]);
379     PrefsReplaceString("mixer", [[mixDevice stringValue] UTF8String]);
380    
381     PrefsReplaceBool("keycodes", [useRawKeyCodes intValue]);
382     PrefsReplaceString("keycodefile", [[rawKeyCodes stringValue] UTF8String]);
383    
384     PrefsReplaceInt32("mousewheelmode", [mouseWheel indexOfSelectedItem]);
385     PrefsReplaceInt32("mousewheellines", [scrollLines intValue]);
386    
387     PrefsReplaceBool("ignoresegv", [ignoreIllegalMemoryAccesses intValue]);
388     PrefsReplaceBool("ignoreillegal", [ignoreIllegalInstructions intValue]);
389     PrefsReplaceBool("idlewait", [dontUseCPUWhenIdle intValue]);
390     PrefsReplaceBool("jit", [enableJIT intValue]);
391     PrefsReplaceBool("jit68k", [enable68kDREmulator intValue]);
392    
393     PrefsReplaceString("seriala", [[modemPort stringValue] UTF8String]);
394     PrefsReplaceString("serialb", [[printerPort stringValue] UTF8String]);
395     PrefsReplaceString("ether", [[ethernetInterface stringValue] UTF8String]);
396     SavePrefs();
397 asvitkine 1.5 #ifdef STANDALONE_PREFS
398 asvitkine 1.1 PrefsExit();
399 asvitkine 1.5 #endif
400 asvitkine 1.1
401 asvitkine 1.4 [[self window] close];
402     [NSApp stopModal];
403     cancelWasClicked = NO;
404 asvitkine 1.2 }
405    
406     - (BOOL) cancelWasClicked
407     {
408     return cancelWasClicked;
409 asvitkine 1.1 }
410    
411     - (void) dealloc
412     {
413     [super dealloc];
414     }
415    
416     @end
417