7 #include "sdlv.h" // Dispatching video-related events
12 unsigned short x2, y2;
17 #define kCornerButtonWidth (0.375)
18 #define kCornerButtonHeight (0.0833333333334)
19 #define kBigButtonWidth (0.125)
20 #define kBigButtonHeight (0.2777777777778)
22 static TouchButton touchbuttons[] = {
23 #define TB(actions, x, y, w, h) \
24 {actions, 0, 0, 0, 0, x, y, w, h}
25 #define P(x) SNES_##x##_MASK
26 TB(P(TL), 0.0, 0.0, kCornerButtonWidth, kCornerButtonHeight),
27 TB(P(TR), 0.625, 0.0, kCornerButtonWidth, kCornerButtonHeight),
28 TB(P(LEFT) | P(UP), 0.0, kCornerButtonHeight, kBigButtonWidth, kBigButtonHeight),
29 TB(P(UP), kBigButtonWidth, kCornerButtonHeight, kBigButtonWidth, kBigButtonHeight),
30 TB(P(RIGHT) | P(UP), 2.0 * kBigButtonWidth, kCornerButtonHeight, kBigButtonWidth, kBigButtonHeight),
31 TB(P(LEFT), 0.0, kCornerButtonHeight + kBigButtonHeight, kBigButtonWidth, kBigButtonHeight),
32 TB(P(RIGHT), 2.0 * kBigButtonWidth, kCornerButtonHeight + kBigButtonHeight, kBigButtonWidth, kBigButtonHeight),
33 TB(P(LEFT) | P(DOWN), 0, 1.0 - (kCornerButtonHeight + kBigButtonHeight), kBigButtonWidth, kBigButtonHeight),
34 TB(P(DOWN), kBigButtonWidth, 1.0 - (kCornerButtonHeight + kBigButtonHeight), kBigButtonWidth, kBigButtonHeight),
35 TB(P(RIGHT) | P(DOWN), 2.0 * kBigButtonWidth, 1.0 - (kCornerButtonHeight + kBigButtonHeight), kBigButtonWidth, kBigButtonHeight),
36 TB(P(SELECT), 0.0, 1.0 - kCornerButtonHeight, kCornerButtonWidth, kCornerButtonHeight),
37 TB(P(X), 1.0 - 2.0 * kBigButtonWidth, kCornerButtonHeight, kBigButtonWidth, kBigButtonHeight),
38 TB(P(Y), 1.0 - 3.0 * kBigButtonWidth, kCornerButtonHeight + kBigButtonHeight, kBigButtonWidth, kBigButtonHeight),
39 TB(P(A), 1.0 - kBigButtonWidth, kCornerButtonHeight + kBigButtonHeight, kBigButtonWidth, kBigButtonHeight),
40 TB(P(B), 1.0 - 2.0 * kBigButtonWidth, 1.0 - (kCornerButtonHeight + kBigButtonHeight), kBigButtonWidth, kBigButtonHeight),
41 TB(P(START), 1.0 - kCornerButtonWidth, 1.0 - kCornerButtonHeight, kCornerButtonWidth, kCornerButtonHeight),
46 static TouchButton* current = 0;
48 static uint32 joypads[2];
52 bool enabled, pressed;
55 static TouchButton* getButtonFor(unsigned int x, unsigned int y) {
58 for (i = 0; i < sizeof(touchbuttons)/sizeof(TouchButton); i++) {
59 if (x >= touchbuttons[i].x && x < touchbuttons[i].x2 &&
60 y >= touchbuttons[i].y && y < touchbuttons[i].y2) {
62 return &touchbuttons[i];
69 static inline void unpress(TouchButton* b) {
70 joypads[Config.touchscreenInput - 1] &= ~b->mask;
72 static inline void press(TouchButton* b) {
73 joypads[Config.touchscreenInput - 1] |= b->mask;
76 static void processMouse(unsigned int x, unsigned int y, int pressed = 0)
79 /* no fullscreen escape button, we have to simulate one! */
80 /* TODO: don't hardcode sizes */
81 if (Config.fullscreen && x > (800 - 100) && y < 50 && pressed > 0) {
82 S9xDoAction(kActionQuit);
85 if (Config.touchscreenInput) {
94 // Button down, or mouse motion.
95 TouchButton* b = getButtonFor(x, y);
96 if (current && b && current != b) {
97 // Moving from button to button
101 } else if (current && !b) {
105 } else if (!current && b) {
111 } else if (mouse.enabled) {
115 if (mouse.x < GUI.RenderX) mouse.x = 0;
117 mouse.x -= GUI.RenderX;
118 if (mouse.x > GUI.RenderW) mouse.x = GUI.RenderW;
121 if (mouse.y < GUI.RenderY) mouse.y = 0;
123 mouse.y -= GUI.RenderY;
124 if (mouse.y > GUI.RenderH) mouse.y = GUI.RenderH;
127 // mouse.{x,y} are system coordinates.
128 // Scale them to emulated screen coordinates.
129 mouse.x = static_cast<unsigned int>(mouse.x / GUI.ScaleX);
130 mouse.y = static_cast<unsigned int>(mouse.y / GUI.ScaleY);
133 mouse.pressed = true;
134 else if (pressed < 0)
135 mouse.pressed = false;
139 static void processEvent(const SDL_Event& event)
144 if (Config.action[event.key.keysym.scancode])
145 S9xDoAction(Config.action[event.key.keysym.scancode]);
146 joypads[0] |= Config.joypad1Mapping[event.key.keysym.scancode];
147 joypads[1] |= Config.joypad2Mapping[event.key.keysym.scancode];
150 joypads[0] &= ~Config.joypad1Mapping[event.key.keysym.scancode];
151 joypads[1] &= ~Config.joypad2Mapping[event.key.keysym.scancode];
153 case SDL_MOUSEBUTTONUP:
154 case SDL_MOUSEBUTTONDOWN:
155 processMouse(event.button.x, event.button.y,
156 (event.button.state == SDL_PRESSED) ? 1 : - 1);
158 case SDL_MOUSEMOTION:
159 processMouse(event.motion.x, event.motion.y);
162 Config.quitting = true;
164 case SDL_ACTIVEEVENT:
166 processVideoEvent(event);
171 /** This function is called to return a bit-wise mask of the state of one of the
172 five emulated SNES controllers.
174 @return 0 if you're not supporting controllers past a certain number or
175 return the mask representing the current state of the controller number
176 passed as a parameter or'ed with 0x80000000.
179 uint32 S9xReadJoypad (int which)
181 if (which < 0 || which >= 2) {
182 // More joypads that we currently handle (could happen if bad conf)
186 return joypads[which];
189 /** Get the current position of the host pointing device, usually a mouse,
190 used to emulated the SNES mouse.
192 @param buttons The buttons return value is a bit-wise mask of the two SNES
193 mouse buttons, bit 0 for button 1 (left) and bit 1 for button 2 (right).
195 bool8 S9xReadMousePosition(int which1, int& x, int& y, uint32& buttons)
197 if (which1 != 0) return FALSE;
201 buttons = mouse.pressed ? 1 : 0;
206 bool8 S9xReadSuperScopePosition(int& x, int& y, uint32& buttons)
210 buttons = mouse.pressed ? 8 : 0;
215 /** Get and process system/input events.
216 @param block true to block, false to poll until the queue is empty.
218 void S9xProcessEvents(bool block)
223 SDL_WaitEvent(&event);
226 while(SDL_PollEvent(&event)) {
232 void S9xInitInputDevices()
236 mouse.enabled = false;
237 mouse.pressed = false;
239 if (Config.joypad1Enabled) {
240 joypads[0] = 0x80000000UL;
242 if (Config.joypad2Enabled) {
243 joypads[1] = 0x80000000UL;
246 // Pretty print some information
248 if (Config.joypad1Enabled) {
249 printf("Player 1 (joypad)");
250 if (Config.joypad2Enabled) {
251 printf("+ player 2 (joypad)");
253 } else if (Config.joypad2Enabled) {
254 printf("Player 2 (joypad)");
260 // TODO Non-awful mouse support, Superscope
262 S9xInputScreenChanged();
265 void S9xDeinitInputDevices()
269 mouse.enabled = false;
270 mouse.pressed = false;
273 void S9xInputScreenChanged()
276 const unsigned int w = GUI.Width, h = GUI.Height;
277 for (i = 0; i < sizeof(touchbuttons)/sizeof(TouchButton); i++) {
278 touchbuttons[i].x = (unsigned int)(touchbuttons[i].fx * w);
279 touchbuttons[i].y = (unsigned int)(touchbuttons[i].fy * h);
280 touchbuttons[i].x2 = (unsigned int)(touchbuttons[i].x + touchbuttons[i].fw * w);
281 touchbuttons[i].y2 = (unsigned int)(touchbuttons[i].y + touchbuttons[i].fh * h);
285 template <typename T>
286 static void drawControls(T * buffer, const int pitch)
290 const T black = static_cast<T>(0xFFFFFFFFU);
293 for (i = 0; i < sizeof(touchbuttons)/sizeof(TouchButton); i++) {
294 temp = buffer + touchbuttons[i].y * pitch + touchbuttons[i].x;
295 for (x = touchbuttons[i].x; x < touchbuttons[i].x2; x++) {
299 temp = buffer + touchbuttons[i].y2 * pitch + touchbuttons[i].x;
300 for (x = touchbuttons[i].x; x < touchbuttons[i].x2; x++) {
304 temp = buffer + touchbuttons[i].y * pitch + touchbuttons[i].x;
305 for (y = touchbuttons[i].y; y < touchbuttons[i].y2; y++) {
309 temp = buffer + touchbuttons[i].y * pitch + touchbuttons[i].x2;
310 for (y = touchbuttons[i].y; y < touchbuttons[i].y2; y++) {
317 void S9xInputScreenDraw(int pixelSize, void * buffer, int pitch)
322 drawControls(reinterpret_cast<uint8*>(buffer), pitch);
325 drawControls(reinterpret_cast<uint16*>(buffer), pitch / 2);