ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/MacOSX/Launcher/VMSettingsController.mm
Revision: 1.4
Committed: 2009-08-06T18:12:24Z (15 years, 1 month ago) by asvitkine
Branch: MAIN
Changes since 1.3: +56 -33 lines
Log Message:
more improvements to Launcher

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