ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/MacOSX/Launcher/VMSettingsController.mm
Revision: 1.2
Committed: 2009-08-02T20:24:38Z (15 years, 2 months ago) by asvitkine
Branch: MAIN
Changes since 1.1: +12 -1 lines
Log Message:
New Virtual Machine button working

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