11 unsigned short x2, y2;
16 #define TOUCH_BUTTON_INITIALIZER(name, x, y, w, h) \
17 {SNES_##name##_MASK, 0, 0, 0, 0, x, y, w, h}
19 #define kCornerButtonWidth (0.375)
20 #define kCornerButtonHeight (0.0833333333334)
21 #define kBigButtonWidth (0.125)
22 #define kBigButtonHeight (0.2777777777778)
24 static TouchButton touchbuttons[] = {
25 TOUCH_BUTTON_INITIALIZER(TL, 0.0, 0.0, kCornerButtonWidth, kCornerButtonHeight),
26 TOUCH_BUTTON_INITIALIZER(TR, 0.625, 0.0, kCornerButtonWidth, kCornerButtonHeight),
27 TOUCH_BUTTON_INITIALIZER(UP, kBigButtonWidth, kCornerButtonHeight, kBigButtonWidth, kBigButtonHeight),
28 TOUCH_BUTTON_INITIALIZER(LEFT, 0.0, kCornerButtonHeight + kBigButtonHeight, kBigButtonWidth, kBigButtonHeight),
29 TOUCH_BUTTON_INITIALIZER(RIGHT, 2.0 * kBigButtonWidth, kCornerButtonHeight + kBigButtonHeight, kBigButtonWidth, kBigButtonHeight),
30 TOUCH_BUTTON_INITIALIZER(DOWN, kBigButtonWidth, 1.0 - (kCornerButtonHeight + kBigButtonHeight), kBigButtonWidth, kBigButtonHeight),
31 TOUCH_BUTTON_INITIALIZER(SELECT, 0.0, 1.0 - kCornerButtonHeight, kCornerButtonWidth, kCornerButtonHeight),
32 TOUCH_BUTTON_INITIALIZER(X, 1.0 - 2.0 * kBigButtonWidth, kCornerButtonHeight, kBigButtonWidth, kBigButtonHeight),
33 TOUCH_BUTTON_INITIALIZER(Y, 1.0 - 3.0 * kBigButtonWidth, kCornerButtonHeight + kBigButtonHeight, kBigButtonWidth, kBigButtonHeight),
34 TOUCH_BUTTON_INITIALIZER(A, 1.0 - kBigButtonWidth, kCornerButtonHeight + kBigButtonHeight, kBigButtonWidth, kBigButtonHeight),
35 TOUCH_BUTTON_INITIALIZER(B, 1.0 - 2.0 * kBigButtonWidth, 1.0 - (kCornerButtonHeight + kBigButtonHeight), kBigButtonWidth, kBigButtonHeight),
36 TOUCH_BUTTON_INITIALIZER(START, 1.0 - kCornerButtonWidth, 1.0 - kCornerButtonHeight, kCornerButtonWidth, kCornerButtonHeight),
39 static TouchButton* current = 0;
41 static uint32 joypads[2];
45 bool enabled, pressed;
48 static TouchButton* getButtonFor(unsigned int x, unsigned int y) {
51 for (i = 0; i < sizeof(touchbuttons)/sizeof(TouchButton); i++) {
52 if (x >= touchbuttons[i].x && x < touchbuttons[i].x2 &&
53 y >= touchbuttons[i].y && y < touchbuttons[i].y2) {
55 return &touchbuttons[i];
62 static inline void unpress(TouchButton* b) {
63 joypads[0] &= ~b->mask;
65 static inline void press(TouchButton* b) {
66 joypads[0] |= b->mask;
69 static void processMouse(unsigned int x, unsigned int y, int pressed = 0)
71 if (Config.touchscreenInput) {
80 // Button down, or mouse motion.
81 TouchButton* b = getButtonFor(x, y);
82 if (current && b && current != b) {
83 // Moving from button to button
87 } else if (current && !b) {
91 } else if (!current && b) {
97 } else if (mouse.enabled) {
101 if (mouse.x < GUI.RenderX) mouse.x = 0;
103 mouse.x -= GUI.RenderX;
104 if (mouse.x > GUI.RenderW) mouse.x = GUI.RenderW;
107 if (mouse.y < GUI.RenderY) mouse.y = 0;
109 mouse.y -= GUI.RenderY;
110 if (mouse.y > GUI.RenderH) mouse.y = GUI.RenderH;
113 // Take care of scaling
114 mouse.x /= GUI.Scale;
115 mouse.y /= GUI.Scale;
118 mouse.pressed = true;
119 else if (pressed < 0)
120 mouse.pressed = false;
124 static void processEvent(const SDL_Event& event)
129 if (Config.action[event.key.keysym.scancode])
130 S9xDoAction(Config.action[event.key.keysym.scancode]);
131 joypads[0] |= Config.joypad1Mapping[event.key.keysym.scancode];
134 joypads[0] &= ~Config.joypad1Mapping[event.key.keysym.scancode];
136 case SDL_MOUSEBUTTONUP:
137 case SDL_MOUSEBUTTONDOWN:
138 processMouse(event.button.x, event.button.y,
139 (event.button.state == SDL_PRESSED) ? 1 : - 1);
141 case SDL_MOUSEMOTION:
142 processMouse(event.motion.x, event.motion.y);
145 Config.quitting = true;
150 uint32 S9xReadJoypad (int which)
152 if (which < 0 || which > 2) {
156 return joypads[which];
159 bool8 S9xReadMousePosition(int which1, int& x, int& y, uint32& buttons)
161 if (which1 != 0) return FALSE;
165 buttons = mouse.pressed ? 1 : 0;
170 bool8 S9xReadSuperScopePosition(int& x, int& y, uint32& buttons)
174 buttons = mouse.pressed ? 8 : 0;
179 void S9xProcessEvents(bool8_32 block)
184 SDL_WaitEvent(&event);
187 while(SDL_PollEvent(&event)) {
193 void S9xInitInputDevices()
198 switch (Settings.ControllerOption) {
200 joypads[0] = 0x80000000UL;
201 printf("Input: 1 joypad, keyboard only\n");
204 joypads[0] = 0x80000000UL;
205 mouse.enabled = true;
206 printf("Input: 1 joypad + mouse\n");
208 case SNES_MOUSE_SWAPPED:
209 printf("Input: mouse\n");
210 mouse.enabled = true;
212 case SNES_SUPERSCOPE:
213 joypads[0] = 0x80000000UL;
214 mouse.enabled = true;
215 printf("Input: 1 joypad + superscope\n");
218 printf("Input: unknown\n");
222 S9xInputScreenChanged();
225 void S9xDeinitInputDevices()
230 void S9xInputScreenChanged()
233 const unsigned int w = GUI.Width, h = GUI.Height;
234 for (i = 0; i < sizeof(touchbuttons)/sizeof(TouchButton); i++) {
235 touchbuttons[i].x = (unsigned int)(touchbuttons[i].fx * w);
236 touchbuttons[i].y = (unsigned int)(touchbuttons[i].fy * h);
237 touchbuttons[i].x2 = (unsigned int)(touchbuttons[i].x + touchbuttons[i].fw * w);
238 touchbuttons[i].y2 = (unsigned int)(touchbuttons[i].y + touchbuttons[i].fh * h);
242 template <typename T>
243 static void drawControls(T * buffer, const int pitch)
247 const T black = static_cast<T>(0xFFFFFFFFU);
250 for (i = 0; i < sizeof(touchbuttons)/sizeof(TouchButton); i++) {
251 temp = buffer + touchbuttons[i].y * pitch + touchbuttons[i].x;
252 for (x = touchbuttons[i].x; x < touchbuttons[i].x2; x++) {
256 temp = buffer + touchbuttons[i].y2 * pitch + touchbuttons[i].x;
257 for (x = touchbuttons[i].x; x < touchbuttons[i].x2; x++) {
261 temp = buffer + touchbuttons[i].y * pitch + touchbuttons[i].x;
262 for (y = touchbuttons[i].y; y < touchbuttons[i].y2; y++) {
266 temp = buffer + touchbuttons[i].y * pitch + touchbuttons[i].x2;
267 for (y = touchbuttons[i].y; y < touchbuttons[i].y2; y++) {
274 void S9xInputScreenDraw(int pixelSize, void * buffer, int pitch)
279 drawControls(reinterpret_cast<uint8*>(buffer), pitch);
282 drawControls(reinterpret_cast<uint16*>(buffer), pitch / 2);