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