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)
72 /* no fullscreen escape button, we have to simulate one! */
73 if (Config.fullscreen && x > (800 - 100) && y < 50 && pressed > 0) {
74 S9xDoAction(kActionQuit);
77 if (Config.touchscreenInput) {
86 // Button down, or mouse motion.
87 TouchButton* b = getButtonFor(x, y);
88 if (current && b && current != b) {
89 // Moving from button to button
93 } else if (current && !b) {
97 } else if (!current && b) {
103 } else if (mouse.enabled) {
107 if (mouse.x < GUI.RenderX) mouse.x = 0;
109 mouse.x -= GUI.RenderX;
110 if (mouse.x > GUI.RenderW) mouse.x = GUI.RenderW;
113 if (mouse.y < GUI.RenderY) mouse.y = 0;
115 mouse.y -= GUI.RenderY;
116 if (mouse.y > GUI.RenderH) mouse.y = GUI.RenderH;
119 // Take care of scaling
120 mouse.x /= GUI.ScaleX;
121 mouse.y /= GUI.ScaleY;
124 mouse.pressed = true;
125 else if (pressed < 0)
126 mouse.pressed = false;
130 static void processEvent(const SDL_Event& event)
135 if (Config.action[event.key.keysym.scancode])
136 S9xDoAction(Config.action[event.key.keysym.scancode]);
137 joypads[0] |= Config.joypad1Mapping[event.key.keysym.scancode];
140 joypads[0] &= ~Config.joypad1Mapping[event.key.keysym.scancode];
142 case SDL_MOUSEBUTTONUP:
143 case SDL_MOUSEBUTTONDOWN:
144 processMouse(event.button.x, event.button.y,
145 (event.button.state == SDL_PRESSED) ? 1 : - 1);
147 case SDL_MOUSEMOTION:
148 processMouse(event.motion.x, event.motion.y);
150 case SDL_ACTIVEEVENT:
151 if (event.active.state & SDL_APPINPUTFOCUS) {
152 S9xVideoOutputFocus(event.active.gain);
156 Config.quitting = true;
161 uint32 S9xReadJoypad (int which)
163 if (which < 0 || which > 2) {
167 return joypads[which];
170 bool8 S9xReadMousePosition(int which1, int& x, int& y, uint32& buttons)
172 if (which1 != 0) return FALSE;
176 buttons = mouse.pressed ? 1 : 0;
181 bool8 S9xReadSuperScopePosition(int& x, int& y, uint32& buttons)
185 buttons = mouse.pressed ? 8 : 0;
190 void S9xProcessEvents(bool8_32 block)
195 SDL_WaitEvent(&event);
198 while(SDL_PollEvent(&event)) {
204 void S9xInitInputDevices()
208 mouse.enabled = false;
209 mouse.pressed = false;
211 switch (Settings.ControllerOption) {
213 joypads[0] = 0x80000000UL;
214 printf("Input: 1 joypad, keyboard only\n");
217 joypads[0] = 0x80000000UL;
218 mouse.enabled = true;
219 printf("Input: 1 joypad + mouse\n");
221 case SNES_MOUSE_SWAPPED:
222 printf("Input: mouse\n");
223 mouse.enabled = true;
225 case SNES_SUPERSCOPE:
226 joypads[0] = 0x80000000UL;
227 mouse.enabled = true;
228 printf("Input: 1 joypad + superscope\n");
231 printf("Input: unknown\n");
235 S9xInputScreenChanged();
238 void S9xDeinitInputDevices()
243 void S9xInputScreenChanged()
246 const unsigned int w = GUI.Width, h = GUI.Height;
247 for (i = 0; i < sizeof(touchbuttons)/sizeof(TouchButton); i++) {
248 touchbuttons[i].x = (unsigned int)(touchbuttons[i].fx * w);
249 touchbuttons[i].y = (unsigned int)(touchbuttons[i].fy * h);
250 touchbuttons[i].x2 = (unsigned int)(touchbuttons[i].x + touchbuttons[i].fw * w);
251 touchbuttons[i].y2 = (unsigned int)(touchbuttons[i].y + touchbuttons[i].fh * h);
255 template <typename T>
256 static void drawControls(T * buffer, const int pitch)
260 const T black = static_cast<T>(0xFFFFFFFFU);
263 for (i = 0; i < sizeof(touchbuttons)/sizeof(TouchButton); i++) {
264 temp = buffer + touchbuttons[i].y * pitch + touchbuttons[i].x;
265 for (x = touchbuttons[i].x; x < touchbuttons[i].x2; x++) {
269 temp = buffer + touchbuttons[i].y2 * pitch + touchbuttons[i].x;
270 for (x = touchbuttons[i].x; x < touchbuttons[i].x2; x++) {
274 temp = buffer + touchbuttons[i].y * pitch + touchbuttons[i].x;
275 for (y = touchbuttons[i].y; y < touchbuttons[i].y2; y++) {
279 temp = buffer + touchbuttons[i].y * pitch + touchbuttons[i].x2;
280 for (y = touchbuttons[i].y; y < touchbuttons[i].y2; y++) {
287 void S9xInputScreenDraw(int pixelSize, void * buffer, int pitch)
292 drawControls(reinterpret_cast<uint8*>(buffer), pitch);
295 drawControls(reinterpret_cast<uint16*>(buffer), pitch / 2);