ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/MacOSX/Launcher/VMSettingsController.mm
Revision: 1.8
Committed: 2010-07-27T02:57:10Z (14 years, 1 month ago) by asvitkine
Branch: MAIN
Changes since 1.7: +5 -2 lines
Log Message:
Fix compile on Snow Leopard.

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