Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/yabause/src/carbon/settings.c
2 views
1
/* Copyright 2006 Guillaume Duhamel
2
Copyright 2006 Anders Montonen
3
Copyright 2010 Alex Marshall
4
5
This file is part of Yabause.
6
7
Yabause is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2 of the License, or
10
(at your option) any later version.
11
12
Yabause is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
GNU General Public License for more details.
16
17
You should have received a copy of the GNU General Public License
18
along with Yabause; if not, write to the Free Software
19
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
*/
21
22
#include <unistd.h>
23
#include <Carbon/Carbon.h>
24
#include <ApplicationServices/ApplicationServices.h>
25
#include "settings.h"
26
27
#define TAB_ID 128
28
#define TAB_SIGNATURE 'tabs'
29
int tabList[] = {129, 130, 131, 132};
30
31
int loadtype = 0;
32
ControlRef oldTab;
33
34
int mystrnlen(char* in, int maxlen)
35
{
36
int len;
37
for(len = 0; (*in != 0) && (len < maxlen); len++, in++);
38
return len;
39
}
40
41
unsigned int mytoi(char* in)
42
{
43
unsigned int out = 0;
44
int length = 0;
45
int i;
46
int format = 0; /* Decimal */
47
if((in[0] == '0') && (in[1] == 'x')) {
48
in += 2;
49
format = 1; /* Hexadecimal */
50
}else if(in[0] == '$') {
51
in += 1;
52
format = 1; /* Hexadecimal */
53
}else if((in[0] == 'H') && (in[1] == '\'')) {
54
in += 2;
55
format = 1; /* Hexadecimal */
56
}
57
length = mystrnlen(in, 11);
58
for(i = 0; i < length; i++) {
59
switch(format) {
60
case 0: /* Decimal */
61
out *= 10;
62
if((in[i] >= '0') && (in[i] <= '9'))
63
out += in[i] - '0';
64
break;
65
case 1: /* Hexadecimal */
66
out <<= 4;
67
if((in[i] >= '0') && (in[i] <= '9'))
68
out += in[i] - '0';
69
if((in[i] >= 'A') && (in[i] <= 'F'))
70
out += (in[i] - 'A') + 0xA;
71
if((in[i] >= 'a') && (in[i] <= 'f'))
72
out += (in[i] - 'a') + 0xA;
73
break;
74
}
75
}
76
return out;
77
}
78
79
void SelectItemOfTabControl(ControlRef tabControl)
80
{
81
ControlRef userPane;
82
ControlID controlID;
83
84
GetControlID(tabControl, &controlID);
85
if (controlID.id != TAB_ID) return;
86
87
controlID.signature = TAB_SIGNATURE;
88
89
controlID.id = tabList[GetControlValue(tabControl) - 1];
90
GetControlByID(GetControlOwner(tabControl), &controlID, &userPane);
91
92
DisableControl(oldTab);
93
SetControlVisibility(oldTab, false, false);
94
EnableControl(userPane);
95
SetControlVisibility(userPane, true, true);
96
oldTab = userPane;
97
98
Draw1Control(tabControl);
99
}
100
101
pascal OSStatus TabEventHandler(EventHandlerCallRef inHandlerRef, EventRef inEvent, void *inUserData)
102
{
103
ControlRef control;
104
105
GetEventParameter(inEvent, kEventParamDirectObject, typeControlRef,
106
NULL, sizeof(ControlRef), NULL, &control );
107
108
SelectItemOfTabControl(control);
109
110
return eventNotHandledErr;
111
}
112
113
void InstallTabHandler(WindowRef window)
114
{
115
EventTypeSpec controlSpec = { kEventClassControl, kEventControlHit };
116
ControlRef tabControl;
117
ControlID controlID;
118
int i;
119
120
controlID.signature = TAB_SIGNATURE;
121
122
for(i = 0;i < 4;i++) {
123
controlID.id = tabList[i];
124
GetControlByID(window, &controlID, &tabControl);
125
DisableControl(tabControl);
126
SetControlVisibility(tabControl, false, false);
127
}
128
129
controlID.id = TAB_ID;
130
GetControlByID(window, &controlID, &tabControl);
131
132
InstallControlEventHandler(tabControl,
133
NewEventHandlerUPP( TabEventHandler ),
134
1, &controlSpec, 0, NULL);
135
136
SetControl32BitValue(tabControl, 1);
137
138
SelectItemOfTabControl(tabControl);
139
}
140
141
CFStringRef get_settings(WindowRef window, int i) {
142
ControlID id;
143
ControlRef control;
144
CFStringRef s;
145
146
id.signature = 'conf';
147
id.id = i;
148
GetControlByID(window, &id, &control);
149
GetControlData(control, kControlEditTextPart,
150
kControlEditTextCFStringTag, sizeof(CFStringRef), &s, NULL);
151
152
return s;
153
}
154
155
CFStringRef get_settings_c(WindowRef window, int i) {
156
ControlID id;
157
ControlRef control;
158
CFStringRef s;
159
160
id.signature = 'conf';
161
id.id = i;
162
GetControlByID(window, &id, &control);
163
s = CFStringCreateWithFormat(kCFAllocatorDefault, NULL,
164
CFSTR("%d"), GetControl32BitValue(control));
165
166
return s;
167
}
168
169
void set_settings(WindowRef window, int i, CFStringRef s) {
170
ControlID id;
171
ControlRef control;
172
173
if (s) {
174
id.signature = 'conf';
175
id.id = i;
176
GetControlByID(window, &id, &control);
177
SetControlData(control, kControlEditTextPart,
178
kControlEditTextCFStringTag, sizeof(CFStringRef), &s);
179
}
180
}
181
182
void set_settings_c(WindowRef window, int i, CFStringRef s) {
183
ControlID id;
184
ControlRef control;
185
186
if (s) {
187
id.signature = 'conf';
188
id.id = i;
189
GetControlByID(window, &id, &control);
190
SetControl32BitValue(control, CFStringGetIntValue(s));
191
}
192
}
193
194
void save_settings(WindowRef window) {
195
PerPad_struct * pad;
196
int i;
197
CFStringRef s;
198
199
CFPreferencesSetAppValue(CFSTR("BiosPath"), get_settings(window, 1),
200
kCFPreferencesCurrentApplication);
201
CFPreferencesSetAppValue(CFSTR("CDROMDrive"), get_settings(window, 2),
202
kCFPreferencesCurrentApplication);
203
CFPreferencesSetAppValue(CFSTR("CDROMCore"), get_settings_c(window, 3),
204
kCFPreferencesCurrentApplication);
205
CFPreferencesSetAppValue(CFSTR("Region"), get_settings_c(window, 4),
206
kCFPreferencesCurrentApplication);
207
CFPreferencesSetAppValue(CFSTR("VideoCore"), get_settings_c(window, 5),
208
kCFPreferencesCurrentApplication);
209
CFPreferencesSetAppValue(CFSTR("SoundCore"), get_settings_c(window, 6),
210
kCFPreferencesCurrentApplication);
211
CFPreferencesSetAppValue(CFSTR("CartPath"), get_settings(window, 7),
212
kCFPreferencesCurrentApplication);
213
CFPreferencesSetAppValue(CFSTR("CartType"), get_settings_c(window, 8),
214
kCFPreferencesCurrentApplication);
215
CFPreferencesSetAppValue(CFSTR("BackupRamPath"),
216
get_settings(window, 9), kCFPreferencesCurrentApplication);
217
CFPreferencesSetAppValue(CFSTR("MpegRomPath"),
218
get_settings(window, 10), kCFPreferencesCurrentApplication);
219
CFPreferencesSetAppValue(CFSTR("AutoFrameSkip"),
220
get_settings_c(window, 11), kCFPreferencesCurrentApplication);
221
222
PerPortReset();
223
pad = PerPadAdd(&PORTDATA1);
224
225
i = 0;
226
while(PerPadNames[i]) {
227
s = get_settings(window, 31 + i);
228
CFPreferencesSetAppValue(
229
CFStringCreateWithCString(0, PerPadNames[i], 0),
230
s, kCFPreferencesCurrentApplication);
231
PerSetKey(CFStringGetIntValue(s), i, pad);
232
i++;
233
}
234
235
CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication);
236
}
237
238
void load_settings(WindowRef window) {
239
int i;
240
241
set_settings(window, 1, CFPreferencesCopyAppValue(CFSTR("BiosPath"),
242
kCFPreferencesCurrentApplication));
243
set_settings(window, 2, CFPreferencesCopyAppValue(CFSTR("CDROMDrive"),
244
kCFPreferencesCurrentApplication));
245
set_settings_c(window, 3, CFPreferencesCopyAppValue(CFSTR("CDROMCore"),
246
kCFPreferencesCurrentApplication));
247
set_settings_c(window, 4, CFPreferencesCopyAppValue(CFSTR("Region"),
248
kCFPreferencesCurrentApplication));
249
set_settings_c(window, 5, CFPreferencesCopyAppValue(CFSTR("VideoCore"),
250
kCFPreferencesCurrentApplication));
251
set_settings_c(window, 6, CFPreferencesCopyAppValue(CFSTR("SoundCore"),
252
kCFPreferencesCurrentApplication));
253
set_settings(window, 7, CFPreferencesCopyAppValue(CFSTR("CartPath"),
254
kCFPreferencesCurrentApplication));
255
set_settings_c(window, 8, CFPreferencesCopyAppValue(CFSTR("CartType"),
256
kCFPreferencesCurrentApplication));
257
set_settings(window, 9,
258
CFPreferencesCopyAppValue(CFSTR("BackupRamPath"),
259
kCFPreferencesCurrentApplication));
260
set_settings(window, 10, CFPreferencesCopyAppValue(CFSTR("MpegRomPath"),
261
kCFPreferencesCurrentApplication));
262
set_settings_c(window, 11, CFPreferencesCopyAppValue(CFSTR("AutoFrameSkip"),
263
kCFPreferencesCurrentApplication));
264
265
i = 0;
266
while(PerPadNames[i]) {
267
set_settings(window, 31 + i, CFPreferencesCopyAppValue(
268
CFStringCreateWithCString(0, PerPadNames[i], 0),
269
kCFPreferencesCurrentApplication));
270
i++;
271
}
272
}
273
274
int load_file_core(char* file, char* addr, int type)
275
{
276
unsigned int adr;
277
int ret = -1;
278
if(addr == NULL)
279
adr = 0;
280
else
281
adr = mytoi(addr);
282
switch(type) {
283
case 0:
284
ret = MappedMemoryLoad(file, adr);
285
break;
286
case 1:
287
MappedMemoryLoadExec(file, adr);
288
ret = 0;
289
break;
290
}
291
return ret;
292
}
293
294
void load_file(WindowRef window, int type) {
295
char addrbuf[12];
296
char filebuf[256];
297
int ret = -1;
298
CFStringGetCString(get_settings(window, 1), filebuf, 256, kCFStringEncodingUTF8);
299
CFStringGetCString(get_settings(window, 2), addrbuf, 12, kCFStringEncodingUTF8);
300
ret = load_file_core(filebuf, addrbuf, type);
301
(void)(ret); /* We need to do something about bad return values... */
302
}
303
304
OSStatus SettingsWindowEventHandler (EventHandlerCallRef myHandler, EventRef theEvent, void* userData)
305
{
306
OSStatus result = eventNotHandledErr;
307
308
switch (GetEventKind (theEvent))
309
{
310
case kEventWindowClose:
311
{
312
WindowRef window;
313
GetEventParameter(theEvent, kEventParamDirectObject, typeWindowRef,
314
0, sizeof(typeWindowRef), 0, &window);
315
316
save_settings(window);
317
318
DisposeWindow(window);
319
}
320
result = noErr;
321
break;
322
323
}
324
325
return (result);
326
}
327
328
OSStatus BrowseHandler(EventHandlerCallRef h, EventRef event, void* data) {
329
NavDialogRef dialog;
330
NavDialogCreationOptions options;
331
332
NavGetDefaultDialogCreationOptions(&options);
333
NavCreateChooseFileDialog(&options, NULL, NULL, NULL, NULL,
334
NULL, &dialog);
335
NavDialogRun(dialog);
336
337
if (NavDialogGetUserAction(dialog) == kNavUserActionChoose) {
338
NavReplyRecord reply;
339
FSRef fileAsFSRef;
340
CFURLRef fileAsCFURLRef = NULL;
341
CFStringRef s;
342
343
NavDialogGetReply(dialog, &reply);
344
345
AEGetNthPtr(&(reply.selection), 1, typeFSRef,
346
NULL, NULL, &fileAsFSRef, sizeof(FSRef), NULL);
347
348
NavDisposeReply(&reply);
349
NavDialogDispose(dialog);
350
351
fileAsCFURLRef = CFURLCreateFromFSRef(NULL, &fileAsFSRef);
352
s = CFURLCopyFileSystemPath(fileAsCFURLRef, kCFURLPOSIXPathStyle);
353
354
CFShow(s);
355
356
SetControlData(data, kControlEditTextPart,
357
kControlEditTextCFStringTag, sizeof(CFStringRef), &s);
358
Draw1Control(data);
359
}
360
361
return noErr;
362
}
363
364
OSStatus KeyConfigHandler(EventHandlerCallRef h, EventRef event, void* data) {
365
UInt32 key;
366
CFStringRef s;
367
GetEventParameter(event, kEventParamKeyCode,
368
typeUInt32, NULL, sizeof(UInt32), NULL, &key);
369
s = CFStringCreateWithFormat(NULL, NULL, CFSTR("%d"), key);
370
SetControlData(data, kControlEditTextPart,
371
kControlEditTextCFStringTag, sizeof(CFStringRef), &s);
372
Draw1Control(data);
373
374
return noErr;
375
}
376
377
void InstallBrowseHandler(WindowRef myWindow, const SInt32 ControllerId,
378
const SInt32 ControlledId)
379
{
380
EventTypeSpec flist[] = {
381
{ kEventClassControl, kEventControlHit }
382
};
383
ControlID Id;
384
ControlRef Controller, Controlled;
385
386
Id.signature = 'conf';
387
Id.id = ControllerId;
388
GetControlByID(myWindow, &Id, &Controller);
389
Id.id = ControlledId;
390
GetControlByID(myWindow, &Id, &Controlled);
391
InstallControlEventHandler(Controller, NewEventHandlerUPP(BrowseHandler),
392
GetEventTypeCount(flist), flist, Controlled, NULL);
393
}
394
395
WindowRef CreateSettingsWindow() {
396
397
WindowRef myWindow;
398
IBNibRef nib;
399
400
EventTypeSpec eventList[] = {
401
{ kEventClassWindow, kEventWindowClose }
402
};
403
404
CreateNibReference(CFSTR("preferences"), &nib);
405
CreateWindowFromNib(nib, CFSTR("Dialog"), &myWindow);
406
407
load_settings(myWindow);
408
409
InstallTabHandler(myWindow);
410
411
{
412
int i;
413
ControlRef control, controlled;
414
ControlID id;
415
EventTypeSpec elist[] = {
416
{ kEventClassKeyboard, kEventRawKeyDown },
417
{ kEventClassKeyboard, kEventRawKeyUp }
418
};
419
420
id.signature = 'conf';
421
i = 0;
422
while(PerPadNames[i]) {
423
id.id = 31 + i;
424
GetControlByID(myWindow, &id, &control);
425
426
InstallControlEventHandler(control, NewEventHandlerUPP(KeyConfigHandler),
427
GetEventTypeCount(elist), elist, control, NULL);
428
i++;
429
}
430
431
InstallBrowseHandler(myWindow, 50, 1); /* BIOS */
432
InstallBrowseHandler(myWindow, 51, 2); /* CDROM */
433
InstallBrowseHandler(myWindow, 52, 7); /* Cartridge ROM */
434
InstallBrowseHandler(myWindow, 53, 9); /* Memory */
435
InstallBrowseHandler(myWindow, 54, 10); /* MPEG ROM */
436
}
437
438
ShowWindow(myWindow);
439
440
InstallWindowEventHandler(myWindow,
441
NewEventHandlerUPP (SettingsWindowEventHandler),
442
GetEventTypeCount(eventList),
443
eventList, myWindow, NULL);
444
445
return myWindow;
446
}
447
448
OSStatus LoadWindowEventHandler (EventHandlerCallRef myHandler, EventRef theEvent, void* userData)
449
{
450
OSStatus result = eventNotHandledErr;
451
switch (GetEventKind (theEvent))
452
{
453
case kEventWindowClose:
454
{
455
WindowRef window;
456
GetEventParameter(theEvent, kEventParamDirectObject, typeWindowRef,
457
0, sizeof(typeWindowRef), 0, &window);
458
459
load_file(window, loadtype);
460
461
DisposeWindow(window);
462
}
463
result = noErr;
464
break;
465
}
466
return (result);
467
}
468
469
WindowRef CreateLoadWindow(int type) {
470
WindowRef myWindow;
471
IBNibRef nib;
472
int* hack;
473
EventTypeSpec eventList[] = {
474
{ kEventClassWindow, kEventWindowClose }
475
};
476
477
CreateNibReference(CFSTR("load_dialog"), &nib);
478
CreateWindowFromNib(nib, CFSTR("Dialog"), &myWindow);
479
480
InstallTabHandler(myWindow);
481
hack = malloc(sizeof(int));
482
loadtype = type;
483
InstallBrowseHandler(myWindow, 50, 1); /* File */
484
ShowWindow(myWindow);
485
486
InstallWindowEventHandler(myWindow,
487
NewEventHandlerUPP (LoadWindowEventHandler),
488
GetEventTypeCount(eventList),
489
eventList, myWindow, NULL);
490
return myWindow;
491
}
492
493