ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/MacOSX/Launcher/VMSettingsController.mm
Revision: 1.6
Committed: 2009-08-18T03:42:11Z (15 years, 3 months ago) by asvitkine
Branch: MAIN
Changes since 1.5: +10 -0 lines
Log Message:
Launcher: Use default settings from NIB for new VM.

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     NSWindow *window = [self window];
181     [NSApp runModalForWindow:window];
182     }
183    
184 asvitkine 1.1 static NSString *makeRelativeIfNecessary(NSString *path)
185     {
186 asvitkine 1.4 char cwd[1024], filename[1024];
187     int cwdlen;
188     strlcpy(filename, [path fileSystemRepresentation], sizeof(filename));
189     getcwd(cwd, sizeof(cwd));
190     cwdlen = strlen(cwd);
191     if (!strncmp(cwd, filename, cwdlen)) {
192     if (cwdlen >= 0 && cwd[cwdlen-1] != '/')
193     cwdlen++;
194     return [NSString stringWithCString: filename + cwdlen];
195     }
196     return path;
197 asvitkine 1.1 }
198    
199     - (IBAction) addDisk: (id) sender
200     {
201     NSOpenPanel *open = [NSOpenPanel openPanel];
202     [open setCanChooseDirectories:NO];
203     [open setAllowsMultipleSelection:NO];
204 asvitkine 1.3 [open setTreatsFilePackagesAsDirectories:YES];
205     [open beginSheetForDirectory: [[NSFileManager defaultManager] currentDirectoryPath]
206 asvitkine 1.1 file: @"Unknown"
207     modalForWindow: [self window]
208     modalDelegate: self
209     didEndSelector: @selector(_addDiskEnd: returnCode: contextInfo:)
210     contextInfo: nil];
211     }
212    
213     - (void) _addDiskEnd: (NSOpenPanel *) open returnCode: (int) theReturnCode contextInfo: (void *) theContextInfo
214     {
215     if (theReturnCode == NSOKButton) {
216 asvitkine 1.4 [diskArray addObject: makeRelativeIfNecessary([open filename])];
217 asvitkine 1.1 [disks reloadData];
218     }
219     }
220    
221     - (IBAction) removeDisk: (id) sender
222     {
223     int selectedRow = [disks selectedRow];
224     if (selectedRow >= 0) {
225     [diskArray removeObjectAtIndex: selectedRow];
226     [disks reloadData];
227     }
228     }
229    
230     - (IBAction) createDisk: (id) sender
231     {
232     NSSavePanel *save = [NSSavePanel savePanel];
233     [save setAccessoryView: diskSaveSize];
234 asvitkine 1.3 [save setTreatsFilePackagesAsDirectories:YES];
235     [save beginSheetForDirectory: [[NSFileManager defaultManager] currentDirectoryPath]
236 asvitkine 1.1 file: @"New.dsk"
237     modalForWindow: [self window]
238     modalDelegate: self
239     didEndSelector: @selector(_createDiskEnd: returnCode: contextInfo:)
240     contextInfo: nil];
241     }
242    
243     - (void) _createDiskEnd: (NSSavePanel *) save returnCode: (int) theReturnCode contextInfo: (void *) theContextInfo
244     {
245     if (theReturnCode == NSOKButton) {
246     int size = [diskSaveSizeField intValue];
247     if (size >= 0 && size <= 10000) {
248     char cmd[1024];
249     snprintf(cmd, sizeof(cmd), "dd if=/dev/zero \"of=%s\" bs=1024k count=%d", [[save filename] UTF8String], [diskSaveSizeField intValue]);
250     int ret = system(cmd);
251     if (ret == 0) {
252 asvitkine 1.4 [diskArray addObject: makeRelativeIfNecessary([save filename])];
253 asvitkine 1.1 [disks reloadData];
254     }
255     }
256     }
257     [(NSData *)theContextInfo release];
258     }
259    
260     - (IBAction) useRawKeyCodesClicked: (id) sender
261     {
262     [rawKeyCodes setEnabled:[useRawKeyCodes intValue]];
263 asvitkine 1.4 [browseRawKeyCodesButton setEnabled:[useRawKeyCodes intValue]];
264 asvitkine 1.1 }
265    
266     - (IBAction) browseForROMFileClicked: (id) sender
267     {
268     NSOpenPanel *open = [NSOpenPanel openPanel];
269     [open setCanChooseDirectories:NO];
270     [open setAllowsMultipleSelection:NO];
271 asvitkine 1.3 [open setTreatsFilePackagesAsDirectories:YES];
272 asvitkine 1.1 [open beginSheetForDirectory: @""
273     file: [romFile stringValue]
274     modalForWindow: [self window]
275     modalDelegate: self
276     didEndSelector: @selector(_browseForROMFileEnd: returnCode: contextInfo:)
277     contextInfo: nil];
278     }
279    
280     - (void) _browseForROMFileEnd: (NSOpenPanel *) open returnCode: (int) theReturnCode contextInfo: (void *) theContextInfo
281     {
282     if (theReturnCode == NSOKButton) {
283 asvitkine 1.4 [romFile setStringValue: makeRelativeIfNecessary([open filename])];
284     }
285 asvitkine 1.1 }
286    
287     - (IBAction) browseForUnixRootClicked: (id) sender
288     {
289     NSOpenPanel *open = [NSOpenPanel openPanel];
290     [open setCanChooseDirectories:YES];
291     [open setCanChooseFiles:NO];
292     [open setAllowsMultipleSelection:NO];
293     [open beginSheetForDirectory: @""
294     file: [unixRoot stringValue]
295     modalForWindow: [self window]
296     modalDelegate: self
297     didEndSelector: @selector(_browseForUnixRootEnd: returnCode: contextInfo:)
298     contextInfo: nil];
299     }
300    
301     - (void) _browseForUnixRootEnd: (NSOpenPanel *) open returnCode: (int) theReturnCode contextInfo: (void *) theContextInfo
302     {
303     if (theReturnCode == NSOKButton) {
304 asvitkine 1.4 [unixRoot setStringValue: makeRelativeIfNecessary([open filename])];
305     }
306     }
307    
308     - (IBAction) browseForKeyCodesFileClicked: (id) sender
309     {
310     NSOpenPanel *open = [NSOpenPanel openPanel];
311     [open setCanChooseDirectories:NO];
312     [open setAllowsMultipleSelection:NO];
313     [open setTreatsFilePackagesAsDirectories:YES];
314     [open beginSheetForDirectory: @""
315     file: [unixRoot stringValue]
316     modalForWindow: [self window]
317     modalDelegate: self
318     didEndSelector: @selector(_browseForKeyCodesFileEnd: returnCode: contextInfo:)
319     contextInfo: nil];
320     }
321    
322     - (void) _browseForKeyCodesFileEnd: (NSOpenPanel *) open returnCode: (int) theReturnCode contextInfo: (void *) theContextInfo
323     {
324     if (theReturnCode == NSOKButton) {
325     [rawKeyCodes setStringValue: makeRelativeIfNecessary([open filename])];
326 asvitkine 1.1 }
327     }
328    
329     - (void) cancelEdit: (id) sender
330     {
331 asvitkine 1.5 #ifdef STANDALONE_PREFS
332 asvitkine 1.1 PrefsExit();
333 asvitkine 1.5 #endif
334 asvitkine 1.4 [[self window] close];
335     [NSApp stopModal];
336     cancelWasClicked = YES;
337 asvitkine 1.1 }
338    
339     - (void) saveChanges: (id) sender
340     {
341     while (PrefsFindString("disk"))
342     PrefsRemoveItem("disk");
343    
344     for (int i = 0; i < [diskArray count]; i++) {
345     PrefsAddString("disk", [[diskArray objectAtIndex:i] UTF8String]);
346     }
347     PrefsReplaceInt32("bootdriver", ([bootFrom indexOfSelectedItem] == 1 ? CDROMRefNum : 0));
348     PrefsReplaceString("rom", [[romFile stringValue] UTF8String]);
349     PrefsReplaceString("extfs", [[unixRoot stringValue] UTF8String]);
350     PrefsReplaceBool("nocdrom", [disableCdrom intValue]);
351     PrefsReplaceInt32("ramsize", [ramSize intValue] << 20);
352    
353     char pref[256];
354     snprintf(pref, sizeof(pref), "%s/%d/%d", [videoType indexOfSelectedItem] == 0 ? "win" : "dga", [width intValue], [height intValue]);
355     PrefsReplaceString("screen", pref);
356    
357     int rate = 8;
358     switch ([refreshRate indexOfSelectedItem]) {
359     case 0: rate = 12; break;
360     case 1: rate = 8; break;
361     case 2: rate = 6; break;
362     case 3: rate = 4; break;
363     case 4: rate = 2; break;
364     case 5: rate = 1; break;
365     case 6: rate = 0; break;
366     }
367     PrefsReplaceInt32("frameskip", rate);
368     PrefsReplaceBool("gfxaccel", [qdAccel intValue]);
369    
370     PrefsReplaceBool("nosound", [disableSound intValue]);
371     PrefsReplaceString("dsp", [[outDevice stringValue] UTF8String]);
372     PrefsReplaceString("mixer", [[mixDevice stringValue] UTF8String]);
373    
374     PrefsReplaceBool("keycodes", [useRawKeyCodes intValue]);
375     PrefsReplaceString("keycodefile", [[rawKeyCodes stringValue] UTF8String]);
376    
377     PrefsReplaceInt32("mousewheelmode", [mouseWheel indexOfSelectedItem]);
378     PrefsReplaceInt32("mousewheellines", [scrollLines intValue]);
379    
380     PrefsReplaceBool("ignoresegv", [ignoreIllegalMemoryAccesses intValue]);
381     PrefsReplaceBool("ignoreillegal", [ignoreIllegalInstructions intValue]);
382     PrefsReplaceBool("idlewait", [dontUseCPUWhenIdle intValue]);
383     PrefsReplaceBool("jit", [enableJIT intValue]);
384     PrefsReplaceBool("jit68k", [enable68kDREmulator intValue]);
385    
386     PrefsReplaceString("seriala", [[modemPort stringValue] UTF8String]);
387     PrefsReplaceString("serialb", [[printerPort stringValue] UTF8String]);
388     PrefsReplaceString("ether", [[ethernetInterface stringValue] UTF8String]);
389     SavePrefs();
390 asvitkine 1.5 #ifdef STANDALONE_PREFS
391 asvitkine 1.1 PrefsExit();
392 asvitkine 1.5 #endif
393 asvitkine 1.1
394 asvitkine 1.4 [[self window] close];
395     [NSApp stopModal];
396     cancelWasClicked = NO;
397 asvitkine 1.2 }
398    
399     - (BOOL) cancelWasClicked
400     {
401     return cancelWasClicked;
402 asvitkine 1.1 }
403    
404     - (void) dealloc
405     {
406     [super dealloc];
407     }
408    
409     @end
410