ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/MacOSX/Launcher/VMSettingsController.mm
Revision: 1.3
Committed: 2009-08-06T06:42:43Z (15 years, 3 months ago) by asvitkine
Branch: MAIN
Changes since 1.2: +5 -2 lines
Log Message:
allow looking inside bundles and default disk location to inside bundle

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