ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/MacOSX/Launcher/VMSettingsController.mm
Revision: 1.1
Committed: 2009-08-02T18:34:57Z (15 years, 2 months ago) by asvitkine
Branch: MAIN
Log Message:
initial import of SheepShaver Launcher project for Mac OS X (WIP)

File Contents

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