"stashing" stuff in a stupid way for 1.2.5
[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
8 struct TouchButton {
9         unsigned short mask;
10         unsigned short x, y;
11         unsigned short x2, y2;
12         double fx, fy;
13         double fw, fh;
14 };
15
16 #define TOUCH_BUTTON_INITIALIZER(name, x, y, w, h) \
17         {SNES_##name##_MASK, 0, 0, 0, 0, x, y, w, h}
18
19 #define kCornerButtonWidth      (0.375)
20 #define kCornerButtonHeight     (0.0833333333334)
21 #define kBigButtonWidth         (0.125)
22 #define kBigButtonHeight        (0.2777777777778)
23
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),
37 };
38
39 static TouchButton* current = 0;
40
41 static uint32 joypads[2];
42 static struct {
43         unsigned x;
44         unsigned y;
45         bool enabled, pressed;
46 } mouse;
47
48 static TouchButton* getButtonFor(unsigned int x, unsigned int y) {
49         unsigned int i;
50
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) {
54
55                         return &touchbuttons[i];
56                 }
57         }
58
59         return 0;
60 }
61
62 static inline void unpress(TouchButton* b) {
63         joypads[0] &= ~b->mask;
64 }
65 static inline void press(TouchButton* b) {
66         joypads[0] |= b->mask;
67 }
68
69 static void processMouse(unsigned int x, unsigned int y, int pressed = 0)
70 {
71 #if CONF_EXIT_BUTTON
72         /* no fullscreen escape button, we have to simulate one! */
73         /* TODO: don't hardcode sizes */
74         if (Config.fullscreen && x > (800 - 100) && y < 50 && pressed > 0) {
75                 S9xDoAction(kActionQuit);
76         }
77 #endif
78         if (Config.touchscreenInput) {
79                 if (pressed < 0) {
80                         // Button up.
81                         if (current) {
82                                 // Leaving button
83                                 unpress(current);
84                                 current = 0;
85                         }
86                 } else {
87                         // Button down, or mouse motion.
88                         TouchButton* b = getButtonFor(x, y);
89                         if (current && b && current != b) {
90                                 // Moving from button to button
91                                 unpress(current);
92                                 current = b;
93                                 press(current);
94                         } else if (current && !b) {
95                                 // Leaving button
96                                 unpress(current);
97                                 current = 0;
98                         } else if (!current && b) {
99                                 // Entering button
100                                 current = b;
101                                 press(current);
102                         }
103                 }
104         } else if (mouse.enabled) {
105                 mouse.x = x;
106                 mouse.y = y;
107
108                 if (mouse.x < GUI.RenderX) mouse.x = 0;
109                 else {
110                         mouse.x -= GUI.RenderX;
111                         if (mouse.x > GUI.RenderW) mouse.x = GUI.RenderW;
112                 }
113
114                 if (mouse.y < GUI.RenderY) mouse.y = 0;
115                 else {
116                         mouse.y -= GUI.RenderY;
117                         if (mouse.y > GUI.RenderH) mouse.y = GUI.RenderH;
118                 }
119
120                 // Take care of scaling
121                 mouse.x /= GUI.ScaleX;
122                 mouse.y /= GUI.ScaleY;
123
124                 if (pressed > 0)
125                         mouse.pressed = true;
126                 else if (pressed < 0)
127                         mouse.pressed = false;
128         }
129 }
130
131 static void processEvent(const SDL_Event& event)
132 {
133         switch (event.type) 
134         {
135         case SDL_KEYDOWN:
136                 if (Config.action[event.key.keysym.scancode]) 
137                         S9xDoAction(Config.action[event.key.keysym.scancode]);
138                 joypads[0] |= Config.joypad1Mapping[event.key.keysym.scancode];
139                 break;
140         case SDL_KEYUP:
141                 joypads[0] &= ~Config.joypad1Mapping[event.key.keysym.scancode];
142                 break;
143         case SDL_MOUSEBUTTONUP:
144         case SDL_MOUSEBUTTONDOWN:
145                 processMouse(event.button.x, event.button.y,
146                                 (event.button.state == SDL_PRESSED) ? 1 : - 1);
147                 break;
148         case SDL_MOUSEMOTION:
149                 processMouse(event.motion.x, event.motion.y);
150                 break;
151         case SDL_ACTIVEEVENT:
152                 if (event.active.state & SDL_APPINPUTFOCUS) {
153                         S9xVideoOutputFocus(event.active.gain);
154                 }
155                 break;
156         case SDL_QUIT:
157                 Config.quitting = true;
158                 break;
159         }
160 }
161
162 uint32 S9xReadJoypad (int which)
163 {
164         if (which < 0 || which > 2) {
165                 return 0;
166         }
167
168         return joypads[which];
169 }
170
171 bool8 S9xReadMousePosition(int which1, int& x, int& y, uint32& buttons)
172 {
173         if (which1 != 0) return FALSE;
174
175         x = mouse.x;
176         y = mouse.y;
177         buttons = mouse.pressed ? 1 : 0;
178
179         return TRUE;
180 }
181
182 bool8 S9xReadSuperScopePosition(int& x, int& y, uint32& buttons)
183 {
184         x = mouse.x;
185         y = mouse.y;
186         buttons = mouse.pressed ? 8 : 0;
187
188         return TRUE;
189 }
190
191 void S9xProcessEvents(bool8_32 block)
192 {
193         SDL_Event event;
194
195         if (block) {
196                 SDL_WaitEvent(&event);
197                 processEvent(event);
198         } else {
199                 while(SDL_PollEvent(&event)) {
200                         processEvent(event);
201                 }
202         }
203 }
204
205 void S9xInitInputDevices()
206 {
207         joypads[0] = 0;
208         joypads[1] = 0;
209         mouse.enabled = false;
210         mouse.pressed = false;
211
212         switch (Settings.ControllerOption) {
213                 case SNES_JOYPAD:
214                         joypads[0] = 0x80000000UL;
215                         printf("Input: 1 joypad, keyboard only\n");
216                         break;
217                 case SNES_MOUSE:
218                         joypads[0] = 0x80000000UL;
219                         mouse.enabled = true;
220                         printf("Input: 1 joypad + mouse\n");
221                         break;
222                 case SNES_MOUSE_SWAPPED:
223                         printf("Input: mouse\n");
224                         mouse.enabled = true;
225                         break;
226                 case SNES_SUPERSCOPE:
227                         joypads[0] = 0x80000000UL;
228                         mouse.enabled = true;
229                         printf("Input: 1 joypad + superscope\n");
230                         break;
231                 default:
232                         printf("Input: unknown\n");
233                         break;
234         }
235
236         S9xInputScreenChanged();
237 }
238
239 void S9xDeinitInputDevices()
240 {
241
242 }
243
244 void S9xInputScreenChanged()
245 {
246         unsigned int i = 0;
247         const unsigned int w = GUI.Width, h = GUI.Height;
248         for (i = 0; i < sizeof(touchbuttons)/sizeof(TouchButton); i++) {
249                 touchbuttons[i].x = (unsigned int)(touchbuttons[i].fx * w);
250                 touchbuttons[i].y = (unsigned int)(touchbuttons[i].fy * h);
251                 touchbuttons[i].x2 = (unsigned int)(touchbuttons[i].x + touchbuttons[i].fw * w);
252                 touchbuttons[i].y2 = (unsigned int)(touchbuttons[i].y + touchbuttons[i].fh * h);
253         }
254 }
255
256 template <typename T>
257 static void drawControls(T * buffer, const int pitch)
258 {
259         unsigned int i = 0;
260         int x, y;
261         const T black = static_cast<T>(0xFFFFFFFFU);
262         T* temp;
263
264         for (i = 0; i < sizeof(touchbuttons)/sizeof(TouchButton); i++) {
265                 temp = buffer + touchbuttons[i].y * pitch + touchbuttons[i].x;
266                 for (x = touchbuttons[i].x; x < touchbuttons[i].x2; x++) {
267                         *temp = black;
268                         temp++;
269                 }
270                 temp = buffer + touchbuttons[i].y2 * pitch + touchbuttons[i].x;
271                 for (x = touchbuttons[i].x; x < touchbuttons[i].x2; x++) {
272                         *temp = black;
273                         temp++;
274                 }
275                 temp = buffer + touchbuttons[i].y * pitch + touchbuttons[i].x;
276                 for (y = touchbuttons[i].y; y < touchbuttons[i].y2; y++) {
277                         *temp = black;
278                         temp+=pitch;
279                 }
280                 temp = buffer + touchbuttons[i].y * pitch + touchbuttons[i].x2;
281                 for (y = touchbuttons[i].y; y < touchbuttons[i].y2; y++) {
282                         *temp = black;
283                         temp+=pitch;
284                 }
285         }
286 }
287
288 void S9xInputScreenDraw(int pixelSize, void * buffer, int pitch)
289 {
290         switch (pixelSize)
291         {
292                 case 1:
293                         drawControls(reinterpret_cast<uint8*>(buffer), pitch);
294                         break;
295                 case 2:
296                         drawControls(reinterpret_cast<uint16*>(buffer), pitch / 2);
297                         break;
298         }
299 }
300