5a3e7bff8e6c04ec3639e0a91ef6cea4d9661853
[drnoksnes] / platform / sdli.cpp
1 #include <SDL.h>
2 #include <math.h>
3
4 #include "platform.h"
5 #include "snes9x.h"
6 #include "display.h"
7 #include "sdlv.h" // Dispatching video-related events
8
9 #if CONF_ZEEMOTE
10 #include "zeemote.h"
11 #endif
12
13 struct TouchButton {
14         unsigned short mask;
15         unsigned short x, y;
16         unsigned short x2, y2;
17         double fx, fy;
18         double fw, fh;
19 };
20
21 #define kCornerButtonWidth      (0.375)
22 #define kCornerButtonHeight     (0.0833333333334)
23 #define kBigButtonWidth         (0.125)
24 #define kBigButtonHeight        (0.2777777777778)
25
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),
46 #undef P
47 #undef TB
48 };
49
50 static TouchButton* current = 0;
51
52 static uint32 joypads[2];
53 static struct {
54         unsigned x;
55         unsigned y;
56         bool enabled, pressed;
57 } mouse;
58
59 static TouchButton* getButtonFor(unsigned int x, unsigned int y) {
60         unsigned int i;
61
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) {
65
66                         return &touchbuttons[i];
67                 }
68         }
69
70         return 0;
71 }
72
73 static inline void unpress(TouchButton* b) {
74         joypads[Config.touchscreenInput - 1] &= ~b->mask;
75 }
76 static inline void press(TouchButton* b) {
77         joypads[Config.touchscreenInput - 1] |= b->mask;
78 }
79
80 static void processMouse(unsigned int x, unsigned int y, int pressed = 0)
81 {
82 #if CONF_EXIT_BUTTON
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);
87         }
88 #endif
89         if (Config.touchscreenInput) {
90                 if (pressed < 0) {
91                         // Button up.
92                         if (current) {
93                                 // Leaving button
94                                 unpress(current);
95                                 current = 0;
96                         }
97                 } else {
98                         // Button down, or mouse motion.
99                         TouchButton* b = getButtonFor(x, y);
100                         if (current && b && current != b) {
101                                 // Moving from button to button
102                                 unpress(current);
103                                 current = b;
104                                 press(current);
105                         } else if (current && !b) {
106                                 // Leaving button
107                                 unpress(current);
108                                 current = 0;
109                         } else if (!current && b) {
110                                 // Entering button
111                                 current = b;
112                                 press(current);
113                         }
114                 }
115         } else if (mouse.enabled) {
116                 mouse.x = x;
117                 mouse.y = y;
118
119                 if (mouse.x < GUI.RenderX) mouse.x = 0;
120                 else {
121                         mouse.x -= GUI.RenderX;
122                         if (mouse.x > GUI.RenderW) mouse.x = GUI.RenderW;
123                 }
124
125                 if (mouse.y < GUI.RenderY) mouse.y = 0;
126                 else {
127                         mouse.y -= GUI.RenderY;
128                         if (mouse.y > GUI.RenderH) mouse.y = GUI.RenderH;
129                 }
130
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);
135
136                 if (pressed > 0)
137                         mouse.pressed = true;
138                 else if (pressed < 0)
139                         mouse.pressed = false;
140         }
141 }
142
143 static void processEvent(const SDL_Event& event)
144 {
145         if (videoEventFilter(event)) return;
146
147         switch (event.type) 
148         {
149                 case SDL_KEYDOWN:
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];
154                         break;
155                 case SDL_KEYUP:
156                         joypads[0] &= ~Config.joypad1Mapping[event.key.keysym.scancode];
157                         joypads[1] &= ~Config.joypad2Mapping[event.key.keysym.scancode];
158                         break;
159                 case SDL_MOUSEBUTTONUP:
160                 case SDL_MOUSEBUTTONDOWN:
161                         processMouse(event.button.x, event.button.y,
162                                         (event.button.state == SDL_PRESSED) ? 1 : - 1);
163                         break;
164                 case SDL_MOUSEMOTION:
165                         processMouse(event.motion.x, event.motion.y);
166                         break;
167                 case SDL_QUIT:
168                         Config.quitting = true;
169                         break;
170         }
171 }
172
173 /** This function is called to return a bit-wise mask of the state of one of the
174         five emulated SNES controllers.
175
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.
179 */
180
181 uint32 S9xReadJoypad (int which)
182 {
183         if (which < 0 || which >= 2) {
184                 // More joypads that we currently handle (could happen if bad conf)
185                 return 0;
186         }
187
188         return joypads[which];
189 }
190
191 /** Get the current position of the host pointing device, usually a mouse,
192         used to emulated the SNES mouse.
193
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).
196 */
197 bool8 S9xReadMousePosition(int which1, int& x, int& y, uint32& buttons)
198 {
199         if (which1 != 0) return FALSE;
200
201         x = mouse.x;
202         y = mouse.y;
203         buttons = mouse.pressed ? 1 : 0;
204
205         return TRUE;
206 }
207
208 bool8 S9xReadSuperScopePosition(int& x, int& y, uint32& buttons)
209 {
210         x = mouse.x;
211         y = mouse.y;
212         buttons = mouse.pressed ? 8 : 0;
213
214         return TRUE;
215 }
216
217 /** Get and process system/input events.
218         @param block true to block, false to poll until the queue is empty.
219 */
220 void S9xProcessEvents(bool block)
221 {
222         SDL_Event event;
223
224 #if CONF_ZEEMOTE
225         // Wheter blocking or non blocking, poll zeemotes now.
226         ZeeRead(joypads);
227 #endif
228
229         if (block) {
230                 SDL_WaitEvent(&event);
231                 processEvent(event);
232         } else {
233                 while(SDL_PollEvent(&event)) {
234                         processEvent(event);
235                 }
236         }
237 }
238
239 void S9xInitInputDevices()
240 {
241         joypads[0] = 0;
242         joypads[1] = 0;
243         mouse.enabled = false;
244         mouse.pressed = false;
245
246 #if CONF_ZEEMOTE
247         ZeeInit();
248 #endif
249
250         if (Config.joypad1Enabled) {
251                 joypads[0] = 0x80000000UL;
252         }
253         if (Config.joypad2Enabled) {
254                 joypads[1] = 0x80000000UL;
255         }
256
257         // Pretty print some information
258         printf("Input: ");
259         if (Config.joypad1Enabled) {
260                 printf("Player 1 (joypad)");
261                 if (Config.joypad2Enabled) {
262                         printf("+ player 2 (joypad)");
263                 }
264         } else if (Config.joypad2Enabled) {
265                 printf("Player 2 (joypad)");
266         } else {
267                 printf("Nothing");
268         }
269         printf("\n");
270
271         // TODO Non-awful mouse & superscope support
272
273         S9xInputScreenChanged();
274 }
275
276 void S9xDeinitInputDevices()
277 {
278 #if CONF_ZEEMOTE
279         ZeeQuit();
280 #endif
281         joypads[0] = 0;
282         joypads[1] = 0;
283         mouse.enabled = false;
284         mouse.pressed = false;
285 }
286
287 void S9xInputScreenChanged()
288 {
289         unsigned int i = 0;
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);
296         }
297 }
298
299 template <typename T>
300 static void drawControls(T * buffer, const int pitch)
301 {
302         unsigned int i = 0;
303         int x, y;
304         const T black = static_cast<T>(0xFFFFFFFFU);
305         T* temp;
306
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++) {
310                         *temp = black;
311                         temp++;
312                 }
313                 temp = buffer + touchbuttons[i].y2 * pitch + touchbuttons[i].x;
314                 for (x = touchbuttons[i].x; x < touchbuttons[i].x2; x++) {
315                         *temp = black;
316                         temp++;
317                 }
318                 temp = buffer + touchbuttons[i].y * pitch + touchbuttons[i].x;
319                 for (y = touchbuttons[i].y; y < touchbuttons[i].y2; y++) {
320                         *temp = black;
321                         temp+=pitch;
322                 }
323                 temp = buffer + touchbuttons[i].y * pitch + touchbuttons[i].x2;
324                 for (y = touchbuttons[i].y; y < touchbuttons[i].y2; y++) {
325                         *temp = black;
326                         temp+=pitch;
327                 }
328         }
329 }
330
331 void S9xInputScreenDraw(int pixelSize, void * buffer, int pitch)
332 {
333         switch (pixelSize)
334         {
335                 case 1:
336                         drawControls(reinterpret_cast<uint8*>(buffer), pitch);
337                         break;
338                 case 2:
339                         drawControls(reinterpret_cast<uint16*>(buffer), pitch / 2);
340                         break;
341         }
342 }
343