| 1 | /* |
| 2 | * OpenTyrian: A modern cross-platform port of Tyrian |
| 3 | * Copyright (C) 2007-2009 The OpenTyrian Development Team |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version 2 |
| 8 | * of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | #include "keyboard.h"cxx . . $ `<file>/src/keyboard.h`/ |
| 20 | |
| 21 | #include "joystick.h"cxx . . $ `<file>/src/joystick.h`/ |
| 22 | #include "mouse.h"cxx . . $ `<file>/src/mouse.h`/ |
| 23 | #include "network.h"cxx . . $ `<file>/src/network.h`/ |
| 24 | #include "opentyr.h"cxx . . $ `<file>/src/opentyr.h`/ |
| 25 | #include "video.h"cxx . . $ `<file>/src/video.h`/ |
| 26 | #include "video_scale.h"cxx . . $ `<file>/src/video_scale.h`/ |
| 27 | |
| 28 | #include "SDL.h" |
| 29 | |
| 30 | #include <stdio.h> |
| 31 | |
| 32 | JE_booleancxx . . $ JE_boolean# ESCPressedcxx . . $ ESCPressed.; |
| 33 | |
| 34 | JE_booleancxx . . $ JE_boolean# newkeycxx . . $ newkey., newmousecxx . . $ newmouse., keydowncxx . . $ keydown., mousedowncxx . . $ mousedown.; |
| 35 | SDL_Scancodecxx . . $ SDL_Scancode# lastkey_scancxx . . $ lastkey_scan.; |
| 36 | SDL_Keymodcxx . . $ SDL_Keymod# lastkey_modcxx . . $ lastkey_mod.; |
| 37 | Uint8cxx . . $ Uint8# lastmouse_butcxx . . $ lastmouse_but.; |
| 38 | Sint32cxx . . $ Sint32# lastmouse_xcxx . . $ lastmouse_x., lastmouse_ycxx . . $ lastmouse_y.; |
| 39 | JE_booleancxx . . $ JE_boolean# mouse_pressedcxx . . $ mouse_pressed.[3] = {falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!, falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!, falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!}; |
| 40 | Sint32cxx . . $ Sint32# mouse_xcxx . . $ mouse_x., mouse_ycxx . . $ mouse_y.; |
| 41 | |
| 42 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! windowHasFocuscxx . . $ windowHasFocus.; |
| 43 | |
| 44 | Uint8cxx . . $ Uint8# keysactivecxx . . $ keysactive.[SDL_NUM_SCANCODEScxx . . $ SDL_NUM_SCANCODES.]; |
| 45 | |
| 46 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! new_textcxx . . $ new_text.; |
| 47 | char last_textcxx . . $ last_text.[SDL_TEXTINPUTEVENT_TEXT_SIZEcxx . . $ `<external>/12778062723415653681/SDL_events.h:268:9`!]; |
| 48 | |
| 49 | static boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! mouseRelativeEnabledcxx . . $ mouseRelativeEnabled.; |
| 50 | |
| 51 | // Relative mouse position in window coordinates. |
| 52 | static Sint32cxx . . $ Sint32# mouseWindowXRelativecxx . . $ mouseWindowXRelative.; |
| 53 | static Sint32cxx . . $ Sint32# mouseWindowYRelativecxx . . $ mouseWindowYRelative.; |
| 54 | |
| 55 | void flush_events_buffercxx . . $ flush_events_buffer(49f6e7a06ebc5aa8).(void) |
| 56 | { |
| 57 | SDL_Eventcxx . . $ SDL_Event# ev; |
| 58 | |
| 59 | while (SDL_PollEventcxx . . $ SDL_PollEvent(c228975526fb3e93).(&ev)); |
| 60 | } |
| 61 | |
| 62 | void wait_inputcxx . . $ wait_input(9e113dd3a0d25c95).(JE_booleancxx . . $ JE_boolean# keyboard, JE_booleancxx . . $ JE_boolean# mouse, JE_booleancxx . . $ JE_boolean# joystick) |
| 63 | { |
| 64 | service_SDL_eventscxx . . $ service_SDL_events(b45ddcbc8423892c).(falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!); |
| 65 | while (!((keyboard && keydowncxx . . $ keydown.) || (mouse && mousedowncxx . . $ mousedown.) || (joystick && joydowncxx . . $ joydown.))) |
| 66 | { |
| 67 | SDL_Delaycxx . . $ SDL_Delay(cb2f890c2fdad230).(SDL_POLL_INTERVALcxx . . $ `src/keyboard.h:28:9`!); |
| 68 | push_joysticks_as_keyboardcxx . . $ push_joysticks_as_keyboard(49f6e7a06ebc5aa8).(); |
| 69 | service_SDL_eventscxx . . $ service_SDL_events(b45ddcbc8423892c).(falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!); |
| 70 | |
| 71 | #ifdef WITH_NETWORKcxx . . $ `<built-in>:413:9`! |
| 72 | if (isNetworkGamecxx . . $ isNetworkGame.) |
| 73 | network_checkcxx . . $ network_check(b126dc7c1de90089).(); |
| 74 | #endif |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | void wait_noinputcxx . . $ wait_noinput(9e113dd3a0d25c95).(JE_booleancxx . . $ JE_boolean# keyboard, JE_booleancxx . . $ JE_boolean# mouse, JE_booleancxx . . $ JE_boolean# joystick) |
| 79 | { |
| 80 | service_SDL_eventscxx . . $ service_SDL_events(b45ddcbc8423892c).(falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!); |
| 81 | while ((keyboard && keydowncxx . . $ keydown.) || (mouse && mousedowncxx . . $ mousedown.) || (joystick && joydowncxx . . $ joydown.)) |
| 82 | { |
| 83 | SDL_Delaycxx . . $ SDL_Delay(cb2f890c2fdad230).(SDL_POLL_INTERVALcxx . . $ `src/keyboard.h:28:9`!); |
| 84 | poll_joystickscxx . . $ poll_joysticks(49f6e7a06ebc5aa8).(); |
| 85 | service_SDL_eventscxx . . $ service_SDL_events(b45ddcbc8423892c).(falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!); |
| 86 | |
| 87 | #ifdef WITH_NETWORKcxx . . $ `<built-in>:413:9`! |
| 88 | if (isNetworkGamecxx . . $ isNetworkGame.) |
| 89 | network_checkcxx . . $ network_check(b126dc7c1de90089).(); |
| 90 | #endif |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void init_keyboardcxx . . $ init_keyboard(49f6e7a06ebc5aa8).(void) |
| 95 | { |
| 96 | //SDL_EnableKeyRepeat(500, 60); TODO Find if SDL2 has an equivalent. |
| 97 | |
| 98 | newkeycxx . . $ newkey. = newmousecxx . . $ newmouse. = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 99 | keydowncxx . . $ keydown. = mousedowncxx . . $ mousedown. = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 100 | |
| 101 | SDL_ShowCursorcxx . . $ SDL_ShowCursor(7864480464b09eea).(SDL_FALSEcxx . . $ SDL_FALSE.); |
| 102 | |
| 103 | #if SDL_VERSION_ATLEASTcxx . . $ `<external>/11196385350576350796/SDL_version.h:121:9`!(2, 26, 0) |
| 104 | SDL_SetHintcxx . . $ SDL_SetHint(f203032a27c3eb90).(SDL_HINT_MOUSE_RELATIVE_SYSTEM_SCALEcxx . . $ `<external>/10713713676398969957/SDL_hints.h:1660:9`!, "1"); |
| 105 | #endif |
| 106 | } |
| 107 | |
| 108 | void mouseSetRelativecxx . . $ mouseSetRelative(b45ddcbc8423892c).(boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! enable) |
| 109 | { |
| 110 | SDL_SetRelativeMouseModecxx . . $ SDL_SetRelativeMouseMode(29c014e38114527e).(enable && windowHasFocuscxx . . $ windowHasFocus.); |
| 111 | |
| 112 | mouseRelativeEnabledcxx . . $ mouseRelativeEnabled. = enable; |
| 113 | |
| 114 | mouseWindowXRelativecxx . . $ mouseWindowXRelative. = 0; |
| 115 | mouseWindowYRelativecxx . . $ mouseWindowYRelative. = 0; |
| 116 | } |
| 117 | |
| 118 | JE_wordcxx . . $ JE_word# JE_mousePositioncxx . . $ JE_mousePosition(2aa6fbd8d75b91c).(JE_wordcxx . . $ JE_word# *mouseX, JE_wordcxx . . $ JE_word# *mouseY) |
| 119 | { |
| 120 | service_SDL_eventscxx . . $ service_SDL_events(b45ddcbc8423892c).(falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!); |
| 121 | *mouseX = mouse_xcxx . . $ mouse_x.; |
| 122 | *mouseY = mouse_ycxx . . $ mouse_y.; |
| 123 | return mousedowncxx . . $ mousedown. ? lastmouse_butcxx . . $ lastmouse_but. : 0; |
| 124 | } |
| 125 | |
| 126 | void mouseGetRelativePositioncxx . . $ mouseGetRelativePosition(ce8f7825f8908cfa).(Sint32cxx . . $ Sint32# *const out_x, Sint32cxx . . $ Sint32# *const out_y) |
| 127 | { |
| 128 | service_SDL_eventscxx . . $ service_SDL_events(b45ddcbc8423892c).(falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!); |
| 129 | |
| 130 | scaleWindowDistanceToScreencxx . . $ scaleWindowDistanceToScreen(ce8f7825f8908cfa).(&mouseWindowXRelativecxx . . $ mouseWindowXRelative., &mouseWindowYRelativecxx . . $ mouseWindowYRelative.); |
| 131 | *out_x = mouseWindowXRelativecxx . . $ mouseWindowXRelative.; |
| 132 | *out_y = mouseWindowYRelativecxx . . $ mouseWindowYRelative.; |
| 133 | |
| 134 | mouseWindowXRelativecxx . . $ mouseWindowXRelative. = 0; |
| 135 | mouseWindowYRelativecxx . . $ mouseWindowYRelative. = 0; |
| 136 | } |
| 137 | |
| 138 | void service_SDL_eventscxx . . $ service_SDL_events(b45ddcbc8423892c).(JE_booleancxx . . $ JE_boolean# clear_new) |
| 139 | { |
| 140 | SDL_Eventcxx . . $ SDL_Event# ev; |
| 141 | |
| 142 | if (clear_new) |
| 143 | { |
| 144 | newkeycxx . . $ newkey. = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 145 | newmousecxx . . $ newmouse. = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 146 | new_textcxx . . $ new_text. = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 147 | } |
| 148 | |
| 149 | while (SDL_PollEventcxx . . $ SDL_PollEvent(c228975526fb3e93).(&ev)) |
| 150 | { |
| 151 | switch (ev.typecxx . . $ SDL_Event#type.) |
| 152 | { |
| 153 | case SDL_WINDOWEVENTcxx . . $ SDL_WINDOWEVENT.: |
| 154 | switch (ev.windowcxx . . $ SDL_Event#window..eventcxx . . $ SDL_WindowEvent#event.) |
| 155 | { |
| 156 | case SDL_WINDOWEVENT_FOCUS_LOSTcxx . . $ SDL_WINDOWEVENT_FOCUS_LOST.: |
| 157 | windowHasFocuscxx . . $ windowHasFocus. = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 158 | |
| 159 | mouseSetRelativecxx . . $ mouseSetRelative(b45ddcbc8423892c).(mouseRelativeEnabledcxx . . $ mouseRelativeEnabled.); |
| 160 | break; |
| 161 | |
| 162 | case SDL_WINDOWEVENT_FOCUS_GAINEDcxx . . $ SDL_WINDOWEVENT_FOCUS_GAINED.: |
| 163 | windowHasFocuscxx . . $ windowHasFocus. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 164 | |
| 165 | mouseSetRelativecxx . . $ mouseSetRelative(b45ddcbc8423892c).(mouseRelativeEnabledcxx . . $ mouseRelativeEnabled.); |
| 166 | break; |
| 167 | |
| 168 | case SDL_WINDOWEVENT_RESIZEDcxx . . $ SDL_WINDOWEVENT_RESIZED.: |
| 169 | video_on_win_resizecxx . . $ video_on_win_resize(49f6e7a06ebc5aa8).(); |
| 170 | break; |
| 171 | } |
| 172 | break; |
| 173 | |
| 174 | case SDL_KEYDOWNcxx . . $ SDL_KEYDOWN.: |
| 175 | /* <alt><enter> toggle fullscreen */ |
| 176 | if (ev.keycxx . . $ SDL_Event#key..keysymcxx . . $ SDL_KeyboardEvent#keysym..modcxx . . $ SDL_Keysym#mod. & KMOD_ALTcxx . . $ KMOD_ALT. && ev.keycxx . . $ SDL_Event#key..keysymcxx . . $ SDL_KeyboardEvent#keysym..scancodecxx . . $ SDL_Keysym#scancode. == SDL_SCANCODE_RETURNcxx . . $ SDL_SCANCODE_RETURN.) |
| 177 | { |
| 178 | toggle_fullscreencxx . . $ toggle_fullscreen(49f6e7a06ebc5aa8).(); |
| 179 | break; |
| 180 | } |
| 181 | |
| 182 | keysactivecxx . . $ keysactive.[ev.keycxx . . $ SDL_Event#key..keysymcxx . . $ SDL_KeyboardEvent#keysym..scancodecxx . . $ SDL_Keysym#scancode.] = 1; |
| 183 | |
| 184 | newkeycxx . . $ newkey. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 185 | lastkey_scancxx . . $ lastkey_scan. = ev.keycxx . . $ SDL_Event#key..keysymcxx . . $ SDL_KeyboardEvent#keysym..scancodecxx . . $ SDL_Keysym#scancode.; |
| 186 | lastkey_modcxx . . $ lastkey_mod. = ev.keycxx . . $ SDL_Event#key..keysymcxx . . $ SDL_KeyboardEvent#keysym..modcxx . . $ SDL_Keysym#mod.; |
| 187 | keydowncxx . . $ keydown. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 188 | |
| 189 | mouseInactivecxx . . $ mouseInactive. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 190 | return; |
| 191 | |
| 192 | case SDL_KEYUPcxx . . $ SDL_KEYUP.: |
| 193 | keysactivecxx . . $ keysactive.[ev.keycxx . . $ SDL_Event#key..keysymcxx . . $ SDL_KeyboardEvent#keysym..scancodecxx . . $ SDL_Keysym#scancode.] = 0; |
| 194 | keydowncxx . . $ keydown. = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 195 | return; |
| 196 | |
| 197 | case SDL_MOUSEMOTIONcxx . . $ SDL_MOUSEMOTION.: |
| 198 | mouse_xcxx . . $ mouse_x. = ev.motioncxx . . $ SDL_Event#motion..xcxx . . $ SDL_MouseMotionEvent#x.; |
| 199 | mouse_ycxx . . $ mouse_y. = ev.motioncxx . . $ SDL_Event#motion..ycxx . . $ SDL_MouseMotionEvent#y.; |
| 200 | mapWindowPointToScreencxx . . $ mapWindowPointToScreen(ce8f7825f8908cfa).(&mouse_xcxx . . $ mouse_x., &mouse_ycxx . . $ mouse_y.); |
| 201 | |
| 202 | if (mouseRelativeEnabledcxx . . $ mouseRelativeEnabled. && windowHasFocuscxx . . $ windowHasFocus.) |
| 203 | { |
| 204 | mouseWindowXRelativecxx . . $ mouseWindowXRelative. += ev.motioncxx . . $ SDL_Event#motion..xrelcxx . . $ SDL_MouseMotionEvent#xrel.; |
| 205 | mouseWindowYRelativecxx . . $ mouseWindowYRelative. += ev.motioncxx . . $ SDL_Event#motion..yrelcxx . . $ SDL_MouseMotionEvent#yrel.; |
| 206 | } |
| 207 | |
| 208 | // Show system mouse pointer if outside screen. |
| 209 | SDL_ShowCursorcxx . . $ SDL_ShowCursor(7864480464b09eea).(mouse_xcxx . . $ mouse_x. < 0 || mouse_xcxx . . $ mouse_x. >= vga_widthcxx . . $ `src/video.h:26:9`! || |
| 210 | mouse_ycxx . . $ mouse_y. < 0 || mouse_ycxx . . $ mouse_y. >= vga_heightcxx . . $ `src/video.h:27:9`! ? SDL_TRUEcxx . . $ SDL_TRUE. : SDL_FALSEcxx . . $ SDL_FALSE.); |
| 211 | |
| 212 | if (ev.motioncxx . . $ SDL_Event#motion..xrelcxx . . $ SDL_MouseMotionEvent#xrel. != 0 || ev.motioncxx . . $ SDL_Event#motion..yrelcxx . . $ SDL_MouseMotionEvent#yrel. != 0) |
| 213 | mouseInactivecxx . . $ mouseInactive. = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 214 | break; |
| 215 | |
| 216 | case SDL_MOUSEBUTTONDOWNcxx . . $ SDL_MOUSEBUTTONDOWN.: |
| 217 | mouseInactivecxx . . $ mouseInactive. = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 218 | |
| 219 | // fall through |
| 220 | case SDL_MOUSEBUTTONUPcxx . . $ SDL_MOUSEBUTTONUP.: |
| 221 | mapWindowPointToScreencxx . . $ mapWindowPointToScreen(ce8f7825f8908cfa).(&ev.buttoncxx . . $ SDL_Event#button..xcxx . . $ SDL_MouseButtonEvent#x., &ev.buttoncxx . . $ SDL_Event#button..ycxx . . $ SDL_MouseButtonEvent#y.); |
| 222 | if (ev.typecxx . . $ SDL_Event#type. == SDL_MOUSEBUTTONDOWNcxx . . $ SDL_MOUSEBUTTONDOWN.) |
| 223 | { |
| 224 | newmousecxx . . $ newmouse. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 225 | lastmouse_butcxx . . $ lastmouse_but. = ev.buttoncxx . . $ SDL_Event#button..buttoncxx . . $ SDL_MouseButtonEvent#button.; |
| 226 | lastmouse_xcxx . . $ lastmouse_x. = ev.buttoncxx . . $ SDL_Event#button..xcxx . . $ SDL_MouseButtonEvent#x.; |
| 227 | lastmouse_ycxx . . $ lastmouse_y. = ev.buttoncxx . . $ SDL_Event#button..ycxx . . $ SDL_MouseButtonEvent#y.; |
| 228 | mousedowncxx . . $ mousedown. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 229 | } |
| 230 | else |
| 231 | { |
| 232 | mousedowncxx . . $ mousedown. = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 233 | } |
| 234 | switch (ev.buttoncxx . . $ SDL_Event#button..buttoncxx . . $ SDL_MouseButtonEvent#button.) |
| 235 | { |
| 236 | case SDL_BUTTON_LEFTcxx . . $ `<external>/13849749812924280863/SDL_mouse.h:445:9`!: |
| 237 | mouse_pressedcxx . . $ mouse_pressed.[0] = mousedowncxx . . $ mousedown.; break; |
| 238 | case SDL_BUTTON_RIGHTcxx . . $ `<external>/13849749812924280863/SDL_mouse.h:447:9`!: |
| 239 | mouse_pressedcxx . . $ mouse_pressed.[1] = mousedowncxx . . $ mousedown.; break; |
| 240 | case SDL_BUTTON_MIDDLEcxx . . $ `<external>/13849749812924280863/SDL_mouse.h:446:9`!: |
| 241 | mouse_pressedcxx . . $ mouse_pressed.[2] = mousedowncxx . . $ mousedown.; break; |
| 242 | } |
| 243 | break; |
| 244 | |
| 245 | case SDL_TEXTINPUTcxx . . $ SDL_TEXTINPUT.: |
| 246 | SDL_strlcpycxx . . $ SDL_strlcpy(55abe9014c6d0e39).(last_textcxx . . $ last_text., ev.textcxx . . $ SDL_Event#text..textcxx . . $ SDL_TextInputEvent#text., COUNTOFcxx . . $ `src/opentyr.h:27:9`!(last_text)); |
| 247 | new_textcxx . . $ new_text. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 248 | break; |
| 249 | |
| 250 | case SDL_TEXTEDITINGcxx . . $ SDL_TEXTEDITING.: |
| 251 | break; |
| 252 | |
| 253 | case SDL_QUITcxx . . $ SDL_QUIT.: |
| 254 | /* TODO: Call the cleanup code here. */ |
| 255 | exitcxx . . $ exit(3ca292c338df9bb3).(0); |
| 256 | break; |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | void JE_clearKeyboardcxx . . $ JE_clearKeyboard(49f6e7a06ebc5aa8).(void) |
| 262 | { |
| 263 | // /!\ Doesn't seems important. I think. D: |
| 264 | } |