7 #include "sdlv.h" // Dispatching video-related events
16 unsigned short x2, y2;
21 #define kCornerButtonWidth (0.375)
22 #define kCornerButtonHeight (0.0833333333334)
23 #define kBigButtonWidth (0.125)
24 #define kBigButtonHeight (0.2777777777778)
26 static TouchButton touchbuttons[] = {
27 #define TB(actions, x, y, w, h) \
28 {actions, 0, 0, 0, 0, x, y, w, h}
29 #define P(x) SNES_##x##_MASK
30 TB(P(TL), 0.0, 0.0, kCornerButtonWidth, kCornerButtonHeight),
31 TB(P(TR), 0.625, 0.0, kCornerButtonWidth, kCornerButtonHeight),
32 TB(P(LEFT) | P(UP), 0.0, kCornerButtonHeight, kBigButtonWidth, kBigButtonHeight),
33 TB(P(UP), kBigButtonWidth, kCornerButtonHeight, kBigButtonWidth, kBigButtonHeight),
34 TB(P(RIGHT) | P(UP), 2.0 * kBigButtonWidth, kCornerButtonHeight, kBigButtonWidth, kBigButtonHeight),
35 TB(P(LEFT), 0.0, kCornerButtonHeight + kBigButtonHeight, kBigButtonWidth, kBigButtonHeight),
36 TB(P(RIGHT), 2.0 * kBigButtonWidth, kCornerButtonHeight + kBigButtonHeight, kBigButtonWidth, kBigButtonHeight),
37 TB(P(LEFT) | P(DOWN), 0, 1.0 - (kCornerButtonHeight + kBigButtonHeight), kBigButtonWidth, kBigButtonHeight),
38 TB(P(DOWN), kBigButtonWidth, 1.0 - (kCornerButtonHeight + kBigButtonHeight), kBigButtonWidth, kBigButtonHeight),
39 TB(P(RIGHT) | P(DOWN), 2.0 * kBigButtonWidth, 1.0 - (kCornerButtonHeight + kBigButtonHeight), kBigButtonWidth, kBigButtonHeight),
40 TB(P(SELECT), 0.0, 1.0 - kCornerButtonHeight, kCornerButtonWidth, kCornerButtonHeight),
41 TB(P(X), 1.0 - 2.0 * kBigButtonWidth, kCornerButtonHeight, kBigButtonWidth, kBigButtonHeight),
42 TB(P(Y), 1.0 - 3.0 * kBigButtonWidth, kCornerButtonHeight + kBigButtonHeight, kBigButtonWidth, kBigButtonHeight),
43 TB(P(A), 1.0 - kBigButtonWidth, kCornerButtonHeight + kBigButtonHeight, kBigButtonWidth, kBigButtonHeight),
44 TB(P(B), 1.0 - 2.0 * kBigButtonWidth, 1.0 - (kCornerButtonHeight + kBigButtonHeight), kBigButtonWidth, kBigButtonHeight),
45 TB(P(START), 1.0 - kCornerButtonWidth, 1.0 - kCornerButtonHeight, kCornerButtonWidth, kCornerButtonHeight),
50 static TouchButton* current = 0;
52 static uint32 joypads[2];
56 bool enabled, pressed;
59 static TouchButton* getButtonFor(unsigned int x, unsigned int y) {
62 for (i = 0; i < sizeof(touchbuttons)/sizeof(TouchButton); i++) {
63 if (x >= touchbuttons[i].x && x < touchbuttons[i].x2 &&
64 y >= touchbuttons[i].y && y < touchbuttons[i].y2) {
66 return &touchbuttons[i];
73 static inline void unpress(TouchButton* b) {
74 joypads[Config.touchscreenInput - 1] &= ~b->mask;
76 static inline void press(TouchButton* b) {
77 joypads[Config.touchscreenInput - 1] |= b->mask;
80 static void processMouse(unsigned int x, unsigned int y, int pressed = 0)
83 /* no fullscreen escape button, we have to simulate one! */
84 /* TODO: don't hardcode sizes */
85 if (Config.fullscreen && x > (800 - 100) && y < 50 && pressed > 0) {
86 S9xDoAction(kActionQuit);
89 if (Config.touchscreenInput) {
98 // Button down, or mouse motion.
99 TouchButton* b = getButtonFor(x, y);
100 if (current && b && current != b) {
101 // Moving from button to button
105 } else if (current && !b) {
109 } else if (!current && b) {
115 } else if (mouse.enabled) {
119 if (mouse.x < GUI.RenderX) mouse.x = 0;
121 mouse.x -= GUI.RenderX;
122 if (mouse.x > GUI.RenderW) mouse.x = GUI.RenderW;
125 if (mouse.y < GUI.RenderY) mouse.y = 0;
127 mouse.y -= GUI.RenderY;
128 if (mouse.y > GUI.RenderH) mouse.y = GUI.RenderH;
131 // mouse.{x,y} are system coordinates.
132 // Scale them to emulated screen coordinates.
133 mouse.x = static_cast<unsigned int>(mouse.x / GUI.ScaleX);
134 mouse.y = static_cast<unsigned int>(mouse.y / GUI.ScaleY);
137 mouse.pressed = true;
138 else if (pressed < 0)
139 mouse.pressed = false;
143 static void processEvent(const SDL_Event& event)
145 if (videoEventFilter(event)) return;
150 if (Config.action[event.key.keysym.scancode])
151 S9xDoAction(Config.action[event.key.keysym.scancode]);
152 joypads[0] |= Config.joypad1Mapping[event.key.keysym.scancode];
153 joypads[1] |= Config.joypad2Mapping[event.key.keysym.scancode];
156 joypads[0] &= ~Config.joypad1Mapping[event.key.keysym.scancode];
157 joypads[1] &= ~Config.joypad2Mapping[event.key.keysym.scancode];
159 case SDL_MOUSEBUTTONUP:
160 case SDL_MOUSEBUTTONDOWN:
161 processMouse(event.button.x, event.button.y,
162 (event.button.state == SDL_PRESSED) ? 1 : - 1);
164 case SDL_MOUSEMOTION:
165 processMouse(event.motion.x, event.motion.y);
168 Config.quitting = true;
173 /** This function is called to return a bit-wise mask of the state of one of the
174 five emulated SNES controllers.
176 @return 0 if you're not supporting controllers past a certain number or
177 return the mask representing the current state of the controller number
178 passed as a parameter or'ed with 0x80000000.
181 uint32 S9xReadJoypad (int which)
183 if (which < 0 || which >= 2) {
184 // More joypads that we currently handle (could happen if bad conf)
188 return joypads[which];
191 /** Get the current position of the host pointing device, usually a mouse,
192 used to emulated the SNES mouse.
194 @param buttons The buttons return value is a bit-wise mask of the two SNES
195 mouse buttons, bit 0 for button 1 (left) and bit 1 for button 2 (right).
197 bool8 S9xReadMousePosition(int which1, int& x, int& y, uint32& buttons)
199 if (which1 != 0) return FALSE;
203 buttons = mouse.pressed ? 1 : 0;
208 bool8 S9xReadSuperScopePosition(int& x, int& y, uint32& buttons)
212 buttons = mouse.pressed ? 8 : 0;
217 /** Get and process system/input events.
218 @param block true to block, false to poll until the queue is empty.
220 void S9xProcessEvents(bool block)
225 // Wheter blocking or non blocking, poll zeemotes now.
230 SDL_WaitEvent(&event);
233 while(SDL_PollEvent(&event)) {
239 void S9xInitInputDevices()
243 mouse.enabled = false;
244 mouse.pressed = false;
250 if (Config.joypad1Enabled) {
251 joypads[0] = 0x80000000UL;
253 if (Config.joypad2Enabled) {
254 joypads[1] = 0x80000000UL;
257 // Pretty print some information
259 if (Config.joypad1Enabled) {
260 printf("Player 1 (joypad)");
261 if (Config.joypad2Enabled) {
262 printf("+ player 2 (joypad)");
264 } else if (Config.joypad2Enabled) {
265 printf("Player 2 (joypad)");
271 // TODO Non-awful mouse & superscope support
273 S9xInputScreenChanged();
276 void S9xDeinitInputDevices()
283 mouse.enabled = false;
284 mouse.pressed = false;
287 void S9xInputScreenChanged()
290 const unsigned int w = GUI.Width, h = GUI.Height;
291 for (i = 0; i < sizeof(touchbuttons)/sizeof(TouchButton); i++) {
292 touchbuttons[i].x = (unsigned int)(touchbuttons[i].fx * w);
293 touchbuttons[i].y = (unsigned int)(touchbuttons[i].fy * h);
294 touchbuttons[i].x2 = (unsigned int)(touchbuttons[i].x + touchbuttons[i].fw * w);
295 touchbuttons[i].y2 = (unsigned int)(touchbuttons[i].y + touchbuttons[i].fh * h);
299 template <typename T>
300 static void drawControls(T * buffer, const int pitch)
304 const T black = static_cast<T>(0xFFFFFFFFU);
307 for (i = 0; i < sizeof(touchbuttons)/sizeof(TouchButton); i++) {
308 temp = buffer + touchbuttons[i].y * pitch + touchbuttons[i].x;
309 for (x = touchbuttons[i].x; x < touchbuttons[i].x2; x++) {
313 temp = buffer + touchbuttons[i].y2 * pitch + touchbuttons[i].x;
314 for (x = touchbuttons[i].x; x < touchbuttons[i].x2; x++) {
318 temp = buffer + touchbuttons[i].y * pitch + touchbuttons[i].x;
319 for (y = touchbuttons[i].y; y < touchbuttons[i].y2; y++) {
323 temp = buffer + touchbuttons[i].y * pitch + touchbuttons[i].x2;
324 for (y = touchbuttons[i].y; y < touchbuttons[i].y2; y++) {
331 void S9xInputScreenDraw(int pixelSize, void * buffer, int pitch)
336 drawControls(reinterpret_cast<uint8*>(buffer), pitch);
339 drawControls(reinterpret_cast<uint16*>(buffer), pitch / 2);