File: controls.cpp

package info (click to toggle)
snes9x 1.39-7
  • links: PTS
  • area: non-free
  • in suites: woody
  • size: 3,380 kB
  • ctags: 10,759
  • sloc: cpp: 46,954; asm: 30,662; ansic: 10,178; makefile: 324
file content (459 lines) | stat: -rw-r--r-- 14,341 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
 * Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
 *
 * This file (c) Copyright 2001 Brad Jorsch (anomie@users.sourceforge.net)
 *
 * This file is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * As a specific exemption, this code may be distributed with and compiled with
 * that of the Super Nintendo emulator snes9x. The resulting binaries may be
 * also be redistributed as long as the corresponding source is available for
 * noncommercial use.
 * 
 * This file is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.
 * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "snes9x.h"
#include "actions.h"
#include "controls.h"
#include "messages.h"
#include "reader.h"
#include "memmap.h"
#include "display.h"

#define S9x_SF_XXXX  (S9x_F_SNES9x | S9x_XXXX)

static char *functions[]={"UNKNOWN", "EmuTurbo", "ToggleEmuTurbo", "Exit",
			  "Reset", "FullscreenOn", "FullscreenOff",
			  "FullscreenToggle", "ToggleHDMA", "ToggleBG0",
			  "ToggleBG1", "ToggleBG2", "ToggleBG3",
			  "ToggleSprites", "SwapJoypads", "NextController",
			  "BGLayeringHack", "Mode7Interpolate", "Transparency",
			  "IncFrameRate", "DecFrameRate", "IncFrameTime",
			  "DecFrameTime", "ClipWindows", "InterpolateSound",
			  "SynchronizeSound", "Pause", "SuperscopePause",
			  "ToggleSuperscopeTurbo", "DumpControls", "SaveSPC",
			  "LoadFreezeFile", "SaveFreezeFile", "QuickLoad000",
			  "QuickLoad001", "QuickLoad002", "QuickLoad003",
			  "QuickLoad004", "QuickLoad005", "QuickLoad006",
			  "QuickLoad007", "QuickLoad008", "QuickLoad009",
			  "QuickLoad010", "QuickSave000", "QuickSave001",
			  "QuickSave002", "QuickSave003", "QuickSave004",
			  "QuickSave005", "QuickSave006", "QuickSave007",
			  "QuickSave008", "QuickSave009", "QuickSave010",
			  "SoundChannel0", "SoundChannel1", "SoundChannel2",
			  "SoundChannel3", "SoundChannel4", "SoundChannel5",
			  "SoundChannel6", "SoundChannel7", "SoundChannelsOn",
                          "IncTurboSpeed", "DecTurboSpeed", "DumpKeymap",
                          "OpenGLCube", "Screenshot", "LoadOopsFile",
                          "LowpassSound", "ReverseStereoSound", "NoEchoSound",
                          "Debugger", "ToggleRangeTimeOver" };

S9xKey *keys[256];

static S9xKey do_nothing={0, 0, S9x_SF_XXXX};


/* Key management */
void S9xInitKeys(void){
    int i;

    for(i=0; i<256; i++){
	keys[i]=&do_nothing;
    }
}

uint16 S9xGetKey(uint8 keycode, uint8 state){
    int idx;
    for(idx=0; (state&keys[keycode][idx].mask)!=keys[keycode][idx].mods; idx++);
    return keys[keycode][idx].action;
}

void S9xAddKey(uint8 keycode, uint8 mask, uint8 mods, uint16 action){
    int idx, i;
    
    if((mods&mask) != mods){
        char *k=S9xKeyToString(keycode);
        char *modp=S9xModifierToString(mods);
        char *maskp=S9xModifierToString(mask);
        char *x=S9xModifierToString(mods&~mask);
        printf("'%s/%s-%s' can never match because %s is masked.\n", modp,
               maskp, k, x);
        free(x);
        free(maskp);
        free(modp);
        free(k);
        return;
    }

    for(i=idx=0; idx<256 && i==0; idx++)
        if((idx&mask)==mods && S9xGetKey(keycode, idx)==S9x_SF_XXXX) i=1;
    if(i==0){
        char *k=S9xKeyToString(keycode);
        char *modp=S9xModifierToString(mods);
        char *maskp=S9xModifierToString(mask);
        printf("'%s/%s-%s' shadowed by earlier definitions\n", modp, maskp, k);
        free(maskp);
        free(modp);
        free(k);
        return;
    }

    if(keys[keycode]==&do_nothing){
	keys[keycode]=(S9xKey *)malloc(2*sizeof(S9xKey));
        assert(keys[keycode]!=NULL);
	keys[keycode][1]=do_nothing;
	idx=0;
    } else {
        for(idx=0; keys[keycode][idx].action!=S9x_SF_XXXX; idx++);
	S9xKey *tmp=(S9xKey *)malloc((idx+2)*sizeof(S9xKey));
        assert(tmp!=NULL);
	for(i=0; i<idx; i++){
	    tmp[i]=keys[keycode][i];
	}
	tmp[idx+1]=do_nothing;
	free(keys[keycode]);
	keys[keycode]=tmp;
    }

    keys[keycode][idx].mask=mask;
    keys[keycode][idx].mods=mods;
    keys[keycode][idx].action=action;
}


/* Conversion to string */
static char *buttonsToString(char *base, uint16 buttons){
    char *s=(char *)malloc((strlen(base)+46)*sizeof(char));
    assert(s!=NULL);
    strcpy(s, base);
    strcat(s, (buttons & S9x_JoyPadMask)?"2":"1");

    if(buttons&S9x_Up) strcat(s, " Up");
    if(buttons&S9x_Down) strcat(s, " Down"); 
    if(buttons&S9x_Left) strcat(s, " Left"); 
    if(buttons&S9x_Right) strcat(s, " Right"); 
    if(buttons&S9x_A) strcat(s, " A"); 
    if(buttons&S9x_B) strcat(s, " B"); 
    if(buttons&S9x_X) strcat(s, " X"); 
    if(buttons&S9x_Y) strcat(s, " Y"); 
    if(buttons&S9x_L) strcat(s, " L"); 
    if(buttons&S9x_R) strcat(s, " R"); 
    if(buttons&S9x_Start) strcat(s, " Start"); 
    if(buttons&S9x_Select) strcat(s, " Select"); 
    return s;
}

char *S9xActionToString(uint16 action){
    if(action==(S9x_F_SNES9x | S9x_DoNothing)) return strdup("DoNothing");
    if(action&S9x_ButtonMask==0) return strdup(functions[0]);

    uint16 func=action&S9x_FunctionMask;
    uint16 jfunc=action&S9x_JoyFuncMask;
    
    if(jfunc==S9x_F_Joypad) return buttonsToString("Joypad", action);
    if(jfunc==S9x_F_Turbo) return buttonsToString("Turbo", action);
    if(jfunc==S9x_F_Sticky) return buttonsToString("Sticky", action);
    if(jfunc==S9x_F_StickyTurbo) return buttonsToString("StickyTurbo", action);
    if(jfunc==S9x_F_ToggleTurbo) return buttonsToString("ToggleTurbo", action);
    if(jfunc==S9x_F_ToggleSticky) return buttonsToString("ToggleSticky", action);
    if(func==S9x_F_SNES9x){
	uint16 f=action&S9x_SubFuncMask;
	if(f>S9x_LastFunction) f=0;
	return strdup(functions[f]);
    }
    return strdup(functions[0]);
}


/* Conversion from string */
static uint16 parseButtons(char *s){
    uint16 buttons=0;
    char *c, *d=s;
    char tmp;

    while(1){
	c=d;
	for(; *c!=0 && (isspace(*c) || *c=='+'); c++);
	if(*c==0) return buttons;
	for(d=c; *d!=0 && !isspace(*d) && *d!='+'; d++);
	tmp=*d;
	*d=0;
	if(!strcmp(c,"Up")) buttons|=S9x_Up;
	if(!strcmp(c,"Down")) buttons|=S9x_Down;
	if(!strcmp(c,"Left")) buttons|=S9x_Left;
	if(!strcmp(c,"Right")) buttons|=S9x_Right;
	if(!strcmp(c,"A")) buttons|=S9x_A;
	if(!strcmp(c,"B")) buttons|=S9x_B;
	if(!strcmp(c,"X")) buttons|=S9x_X;
	if(!strcmp(c,"Y")) buttons|=S9x_Y;
	if(!strcmp(c,"L")) buttons|=S9x_L;
	if(!strcmp(c,"R")) buttons|=S9x_R;
	if(!strcmp(c,"Start")) buttons|=S9x_Start;
	if(!strcmp(c,"Select")) buttons|=S9x_Select;
	*d=tmp;
    }
    return buttons; /* Will never get here... */
}

uint16 S9xStringToAction(char *s){
    uint16 action=S9x_F_SNES9x;
    
    if(!strcmp(s, "DoNothing")) return S9x_F_SNES9x | S9x_DoNothing;
    if(!strncmp(s, "Joypad1 ", 8)) action=S9x_F_Joypad;
    if(!strncmp(s, "Joypad2 ", 8)) action=S9x_F_Joypad | S9x_JoyPadMask;
    if(!strncmp(s, "Turbo1 ", 7)) action=S9x_F_Turbo;
    if(!strncmp(s, "Turbo2 ", 7)) action=S9x_F_Turbo | S9x_JoyPadMask;
    if(!strncmp(s, "Sticky1 ", 8)) action=S9x_F_Sticky;
    if(!strncmp(s, "Sticky2 ", 8)) action=S9x_F_Sticky | S9x_JoyPadMask;
    if(!strncmp(s, "StickyTurbo1 ", 13)) action=S9x_F_StickyTurbo;
    if(!strncmp(s, "StickyTurbo2 ", 13)) action=S9x_F_StickyTurbo | S9x_JoyPadMask;
    if(!strncmp(s, "ToggleTurbo1 ", 13)) action=S9x_F_ToggleTurbo;
    if(!strncmp(s, "ToggleTurbo2 ", 13)) action=S9x_F_ToggleTurbo | S9x_JoyPadMask;
    if(!strncmp(s, "ToggleSticky1 ", 14)) action=S9x_F_ToggleSticky;
    if(!strncmp(s, "ToggleSticky2 ", 14)) action=S9x_F_ToggleSticky | S9x_JoyPadMask;

    if(action!=S9x_F_SNES9x) return action|parseButtons(s);

    for(int i=1; i<=S9x_LastFunction; i++){
	if(!strcasecmp(s, functions[i])) return S9x_F_SNES9x|i;
    }
    return S9x_SF_XXXX;
}


/* Loading the file */

/* Return values:
 *  0 = good
 *  1 = comment
 *  2 = couldn't find char
 *  3 = either left or right would be empty
 */
static bool8 split_string(int line, char *s, char chr, char **left, char **right){
    char *c, *d;

    for(c=s; *c!=0 && isspace(*c); c++);
    if(*c==0 || *c=='#') return 1;
    *left=c;
    c=strchr(*left, chr);
    if(c==NULL){ 
	if(line>0) printf("Line %d: no '%c'\n", line, chr);
	return 2;
    }
    if(c==*left){
	if(line>0) printf("Line %d: nothing before '%c'\n", line, chr);
	return 3;
    }
    for(d=c-1; d>=*left && isspace(*d); d--);
    *(d+1)=0;
    for(c++; *c!=0 && isspace(*c); c++);
    if(*c==0){
	if(line>0) printf("Line %d: nothing after '%c'\n", line, chr);
	return 3;
    }
    *right=c;
    for(; *c!=0; c++);
    for(c--; isspace(*c); c--);
    *(c+1)=0;

    return 0;
}

static bool8 ReadControlSpec(Reader *r){
    int line=0;
    int ret;
    int flag=0;
    char buf[1024];
    char *maskp, *modp, *keyp, *actionp;
    int key;
    uint8 mask, mod;
    uint16 action;

    while(r->gets(buf, 1024)!=NULL){
	maskp=modp=keyp=actionp=NULL;
	line++;
	if(split_string(line, buf, '=', &keyp, &actionp)) continue;
	ret=split_string(0, keyp, '-', &modp, &keyp);
	if(ret==3){
            printf("Line %d: misplaced '-'\n", line);
            continue;
        }
	if(ret==0){
	    if(split_string(0, modp, '/', &modp, &maskp)==3){
                printf("Line %d: misplaced '/'\n", line);
                continue;
            }
	}

	key=S9xStringToKey(keyp);
	if(key<0 || key>255){
	    printf("Line %d: Bad key specifier\n", line);
	    continue;
	}
	action=S9xStringToAction(actionp);
	if(action==S9x_SF_XXXX){
	    printf("Line %d: Bad action specifier\n", line);
	    continue;
	}
	mask=255;
	mod=0;
	if(maskp!=NULL) mask=S9xStringToModifier(maskp);
	if(modp!=NULL) mod=S9xStringToModifier(modp);

	S9xAddKey(key, mask, mod, action);
	flag=1;
    }
    return flag;
}

/* Checks for a control file in the following locations.
 *  romdir/romname.ctl
 *  The first .ctl inside the zip file (if rom_filename is a zip file)
 *  freezedir/romname.ctl
 *  romdir/snes9x.ctl
 *  freezedir/snes9x.ctl
 *  /usr/share/snes9x/snes9x.ctl
 */
bool8 S9xCheckForControlSpec(const char *rom_filename){
    char dir[_MAX_DIR+1];
    char drive[_MAX_DRIVE+1];
    char name [_MAX_FNAME+1];
    char ext [_MAX_EXT+1];
    char fname [_MAX_PATH+1];
    STREAM control_file = NULL;
    bool8 ret=0;

    S9xInitKeys();
    
    _splitpath(rom_filename, drive, dir, name, ext);
    _makepath(fname, drive, dir, name, "ctl");

    if((control_file=OPEN_STREAM(fname, "r"))!=NULL){
	printf("Reading control specification from %s\n", fname);
	ret=ReadControlSpec(new fReader(control_file));
	CLOSE_STREAM(control_file);
	if(ret) return 1;
	else printf("Read failed!\n");
    }

#ifdef UNZIP_SUPPORT
    if(strcasecmp(ext, "zip")==0){
	unzFile file=unzOpen(rom_filename);
	if(file!=NULL){
	    int port=unzGoToFirstFile(file);
	    unz_file_info info;
	    while(port==UNZ_OK){
		char name[132];
		unzGetCurrentFileInfo(file, &info, name,128, NULL,0, NULL,0);
		int len=strlen(name);
		if(len>=4 && strcasecmp(name+len-4, ".ctl")==0 &&
		   unzOpenCurrentFile(file) == UNZ_OK){
		    printf("Reading control specification from %s in %s\n", name, rom_filename);
		    ret=ReadControlSpec(new unzReader(file));
		    if(ret) break;
		    else printf("Read failed!\n");
		}
		port = unzGoToNextFile(file);
	    }
	    assert(unzClose(file)==UNZ_OK);
	    if(ret) return 1;
	}
    }
#endif

    if((control_file=OPEN_STREAM(S9xGetFilename(".ctl"), "r"))!=NULL){
	printf("Reading control specification from %s\n", S9xGetFilename(".ctl"));
	ret=ReadControlSpec(new fReader(control_file));
	CLOSE_STREAM(control_file);
	if(ret) return 1;
	else printf("Read failed!\n");
    }


    /* Try the global control filename "snes9x.ctl" */
    _makepath(fname, drive, dir, "snes9x", "ctl");

    if((control_file=OPEN_STREAM(fname, "r"))!=NULL){
	printf("Reading control specification from %s\n", fname);
	ret=ReadControlSpec(new fReader(control_file));
	CLOSE_STREAM(control_file);
	if(ret) return 1;
	else printf("Read failed!\n");
    }

    snprintf(fname, _MAX_PATH+1, "%s%ssnes9x.ctl", S9xGetSnapshotDirectory(), SLASH_STR);

    if((control_file=OPEN_STREAM(fname, "r"))!=NULL){
	printf("Reading control specification from %s\n", fname);
	ret=ReadControlSpec(new fReader(control_file));
	CLOSE_STREAM(control_file);
        if(ret) return 1;
        else printf("Read failed!\n");
    }
    
    if((control_file=OPEN_STREAM("/usr/share/snes9x/snes9x.ctl", "r"))!=NULL){
	printf("Reading control specification from /usr/share/snes9x/snes9x.ctl\n");
	ret=ReadControlSpec(new fReader(control_file));
	CLOSE_STREAM(control_file);
        if(ret) return 1;
        else printf("Read failed!\n");
    }

    printf("WARNING: No control specification file read!\n");
    return ret;
}

bool8 S9xDumpControls(char *filename){
    int keycode;
    int i;
    char *c=NULL;
    FILE *fp;

    if((fp=fopen(filename, "w"))==NULL) return 0;    
    
    for(keycode=0; keycode<256; keycode++){
	for(i=0; keys[keycode][i].action!=S9x_SF_XXXX; i++){
	    if(c==NULL) c=S9xKeyToString(keycode);
            assert(c!=NULL);
	    char *act=S9xActionToString(keys[keycode][i].action);
            assert(act!=NULL);
	    if(keys[keycode][i].mask!=255){
		char *modp=S9xModifierToString(keys[keycode][i].mods);
		char *maskp=S9xModifierToString(keys[keycode][i].mask);
                assert(modp!=NULL);
                assert(maskp!=NULL);
		fprintf(fp, "%s/%s-%s = %s\n", modp, maskp, c, act);
		free(modp);
		free(maskp);
	    } else if(keys[keycode][i].mods!=0){
		char *modp=S9xModifierToString(keys[keycode][i].mods);
                assert(modp!=NULL);
		fprintf(fp, "%s-%s = %s\n", modp, c, act);
		free(modp);
	    } else {
		fprintf(fp, "%s = %s\n", c, act);
	    }
	    free(act);
	}
	if(c!=NULL) free(c);
	c=NULL;
    }
    fclose(fp);
    return 1;
}