ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/MacOSX/Launcher/VMSettingsController.mm
Revision: 1.9
Committed: 2010-12-28T02:50:55Z (13 years, 10 months ago) by asvitkine
Branch: MAIN
Changes since 1.8: +3 -1 lines
Log Message:
- Add a prefix header which is used in place of SheepShaver's config.h to
allow correct compilation of the Universal Binary.
- Update project to work with Xcode on Snow Leopard.
- Add missing #include <unistd.h>

File Contents

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