summaryrefslogtreecommitdiff
path: root/shell/input/sdl/input.c
blob: ad7088101edd290a3f1525c0ca4c5da166782434 (plain)
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
#include <SDL/SDL.h>
#include <stdint.h>
#include <stdio.h>
#include "main.h"
#include "snes9x.h"
#include "soundux.h"
#include "memmap.h"
#include "apu.h"
#include "cheats.h"
#include "display.h"
#include "gfx.h"
#include "cpuexec.h"
#include "srtc.h"
#include "sa1.h"
#include "scaler.h"

#include "menu.h"
#include "config.h"
#include "buttons.h"

uint8_t *keystate;
uint8_t exit_snes = 0;
extern uint32_t emulator_state;

static uint32_t joypad = 0;

uint32_t S9xReadJoypad(int32_t port)
{
	// Only 1P is supported
	if (port != 0) return 0;

	SDL_Event event;
	static const uint32_t snes_lut[] =
	{
		SNES_B_MASK,
		SNES_Y_MASK,
		SNES_SELECT_MASK,
		SNES_START_MASK,
		SNES_UP_MASK,
		SNES_DOWN_MASK,
		SNES_LEFT_MASK,
		SNES_RIGHT_MASK,
		SNES_A_MASK,
		SNES_X_MASK,
		SNES_TL_MASK,
		SNES_TR_MASK
	};

	while (SDL_PollEvent(&event))
	{
		switch (event.type)
		{
			case SDL_KEYDOWN: {
				SDLKey key = event.key.keysym.sym;
				
				if (key==BTN_MENU) 
					emulator_state = 1;
				
				if (key==option.config_buttons[0][0])
					joypad |= SNES_UP_MASK;
				if (key==option.config_buttons[0][1])
					joypad |= SNES_RIGHT_MASK;
				if (key==option.config_buttons[0][2])
					joypad |= SNES_DOWN_MASK;
				if (key==option.config_buttons[0][3])
					joypad |= SNES_LEFT_MASK;
				
				if (key==option.config_buttons[0][4])
					joypad |= SNES_A_MASK;
				if (key==option.config_buttons[0][5])
					joypad |= SNES_B_MASK;
				if (key==option.config_buttons[0][6])
					joypad |= SNES_X_MASK;
				if (key==option.config_buttons[0][7])
					joypad |= SNES_Y_MASK;
				if (key==option.config_buttons[0][8])
					joypad |= SNES_TL_MASK;
				if (key==option.config_buttons[0][9])
					joypad |= SNES_TR_MASK;
				
				if (key==option.config_buttons[0][10])
					joypad |= SNES_START_MASK;
				if (key==option.config_buttons[0][11])
					joypad |= SNES_SELECT_MASK;
			} break;
				
			
			case SDL_KEYUP: {
				SDLKey key = event.key.keysym.sym;
				
				if (key==option.config_buttons[0][0])
					joypad &= ~SNES_UP_MASK;
				if (key==option.config_buttons[0][1])
					joypad &= ~SNES_RIGHT_MASK;
				if (key==option.config_buttons[0][2])
					joypad &= ~SNES_DOWN_MASK;
				if (key==option.config_buttons[0][3])
					joypad &= ~SNES_LEFT_MASK;
				
				if (key==option.config_buttons[0][4])
					joypad &= ~SNES_A_MASK;
				if (key==option.config_buttons[0][5])
					joypad &= ~SNES_B_MASK;
				if (key==option.config_buttons[0][6])
					joypad &= ~SNES_X_MASK;
				if (key==option.config_buttons[0][7])
					joypad &= ~SNES_Y_MASK;
				if (key==option.config_buttons[0][8])
					joypad &= ~SNES_TL_MASK;
				if (key==option.config_buttons[0][9])
					joypad &= ~SNES_TR_MASK;
				
				if (key==option.config_buttons[0][10])
					joypad &= ~SNES_START_MASK;
				if (key==option.config_buttons[0][11])
					joypad &= ~SNES_SELECT_MASK;
			} break;
		}
	}

	return joypad;
}