| 1 | /* |
| 2 | * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>. |
| 3 | * It is copyright by its individual contributors, as recorded in the |
| 4 | * project's Git history. See COPYING.txt at the top level for license |
| 5 | * terms and a link to the Git history. |
| 6 | */ |
| 7 | /* |
| 8 | * |
| 9 | * SDL Event related stuff |
| 10 | * |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <ranges> |
| 15 | #include <SDL.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include "event.h" |
| 19 | #include "key.h" |
| 20 | #include "mouse.h" |
| 21 | #include "window.h" |
| 22 | #include "timer.h" |
| 23 | #include "cmd.h" |
| 24 | #include "config.h" |
| 25 | #include "inferno.h" |
| 26 | |
| 27 | #include "joy.h" |
| 28 | #include "args.h" |
| 29 | #include "partial_range.h" |
| 30 | |
| 31 | namespace dcx { |
| 32 | |
| 33 | namespace { |
| 34 | |
| 35 | struct event_poll_state |
| 36 | { |
| 37 | uint8_t clean_uniframe{1}; |
| 38 | const window *const front_window = window_get_front(); |
| 39 | window_event_result highest_result = window_event_result::ignored; |
| 40 | void process_event_batch(std::ranges::subrange<const SDL_Event *>); |
| 41 | }; |
| 42 | |
| 43 | } |
| 44 | |
| 45 | #if SDL_MAJOR_VERSION == 2 |
| 46 | extern SDL_Window *g_pRebirthSDLMainWindow; |
| 47 | |
| 48 | static void windowevent_handler(const SDL_WindowEvent &windowevent) |
| 49 | { |
| 50 | switch (windowevent.event) |
| 51 | { |
| 52 | case SDL_WINDOWEVENT_SIZE_CHANGED: |
| 53 | { |
| 54 | const d_window_size_event e{windowevent.data1, windowevent.data2}; |
| 55 | event_send(e); |
| 56 | break; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | #endif |
| 61 | |
| 62 | namespace { |
| 63 | |
| 64 | static void event_notify_begin_loop() |
| 65 | { |
| 66 | const d_event_begin_loop event; |
| 67 | event_send(event); |
| 68 | } |
| 69 | |
| 70 | static void event_notify_end_loop() |
| 71 | { |
| 72 | event_send(d_event_end_loop{}); |
| 73 | } |
| 74 | |
| 75 | } |
| 76 | |
| 77 | window_event_result event_poll() |
| 78 | { |
| 79 | event_poll_state state; |
| 80 | event_notify_begin_loop(); |
| 81 | |
| 82 | for (;;) |
| 83 | { |
| 84 | // If the front window changes, exit this loop, otherwise unintended behavior can occur |
| 85 | // like pressing 'Return' really fast at 'Difficulty Level' causing multiple games to be started |
| 86 | if (state.front_window != window_get_front()) |
| 87 | break; |
| 88 | std::array<SDL_Event, 128> events; |
| 89 | |
| 90 | SDL_PumpEvents(); |
| 91 | #if SDL_MAJOR_VERSION == 1 |
| 92 | const auto peep = SDL_PeepEvents(events.data(), events.size(), SDL_GETEVENT, SDL_ALLEVENTS); |
| 93 | #elif SDL_MAJOR_VERSION == 2 |
| 94 | const auto peep = SDL_PeepEvents(events.data(), events.size(), SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT); |
| 95 | #endif |
| 96 | if (peep <= 0) |
| 97 | break; |
| 98 | state.process_event_batch(unchecked_partial_range(events, static_cast<unsigned>(peep))); |
| 99 | if (state.highest_result == window_event_result::deleted) |
| 100 | break; |
| 101 | } |
| 102 | // Send the idle event if there were no other events (or they were ignored) |
| 103 | if (state.highest_result == window_event_result::ignored) |
| 104 | { |
| 105 | const d_event ievent{event_type::idle}; |
| 106 | state.highest_result = std::max(event_send(ievent), state.highest_result); |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | #if DXX_USE_EDITOR |
| 111 | event_reset_idle_seconds(); |
| 112 | #endif |
| 113 | } |
| 114 | mouse_cursor_autohide(); |
| 115 | event_notify_end_loop(); |
| 116 | return state.highest_result; |
| 117 | } |
| 118 | |
| 119 | void event_poll_state::process_event_batch(const std::ranges::subrange<const SDL_Event *> events) |
| 120 | { |
| 121 | for (auto &&event : events) |
| 122 | { |
| 123 | window_event_result result; |
| 124 | switch(event.type) { |
| 125 | #if SDL_MAJOR_VERSION == 2 |
| 126 | case SDL_WINDOWEVENT: |
| 127 | windowevent_handler(event.window); |
| 128 | continue; |
| 129 | #endif |
| 130 | case SDL_KEYDOWN: |
| 131 | case SDL_KEYUP: |
| 132 | if (clean_uniframe) |
| 133 | { |
| 134 | clean_uniframe=0; |
| 135 | unicode_frame_buffer = {}; |
| 136 | } |
| 137 | result = key_handler(&event.key); |
| 138 | break; |
| 139 | case SDL_MOUSEBUTTONDOWN: |
| 140 | case SDL_MOUSEBUTTONUP: |
| 141 | if (CGameArg.CtlNoMouse) |
| 142 | continue; |
| 143 | result = mouse_button_handler(&event.button); |
| 144 | break; |
| 145 | case SDL_MOUSEMOTION: |
| 146 | if (CGameArg.CtlNoMouse) |
| 147 | continue; |
| 148 | result = mouse_motion_handler(&event.motion); |
| 149 | break; |
| 150 | case SDL_JOYBUTTONDOWN: |
| 151 | case SDL_JOYBUTTONUP: |
| 152 | if (CGameArg.CtlNoJoystick) |
| 153 | continue; |
| 154 | result = joy_button_handler(&event.jbutton); |
| 155 | break; |
| 156 | case SDL_JOYAXISMOTION: |
| 157 | if (CGameArg.CtlNoJoystick) |
| 158 | continue; |
| 159 | #if DXX_MAX_BUTTONS_PER_JOYSTICK || DXX_MAX_HATS_PER_JOYSTICK |
| 160 | highest_result = std::max(joy_axisbutton_handler(&event.jaxis), highest_result); |
| 161 | #endif |
| 162 | result = joy_axis_handler(&event.jaxis); |
| 163 | break; |
| 164 | case SDL_JOYHATMOTION: |
| 165 | if (CGameArg.CtlNoJoystick) |
| 166 | continue; |
| 167 | result = joy_hat_handler(&event.jhat); |
| 168 | break; |
| 169 | case SDL_JOYBALLMOTION: |
| 170 | continue; |
| 171 | case SDL_QUIT: { |
| 172 | result = call_default_handler(d_event{event_type::quit}); |
| 173 | break; |
| 174 | } |
| 175 | default: |
| 176 | continue; |
| 177 | } |
| 178 | highest_result = std::max(result, highest_result); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | void event_flush() |
| 183 | { |
| 184 | std::array<SDL_Event, 128> events; |
| 185 | for (;;) |
| 186 | { |
| 187 | SDL_PumpEvents(); |
| 188 | #if SDL_MAJOR_VERSION == 1 |
| 189 | const auto peep = SDL_PeepEvents(events.data(), events.size(), SDL_GETEVENT, SDL_ALLEVENTS); |
| 190 | #elif SDL_MAJOR_VERSION == 2 |
| 191 | const auto peep = SDL_PeepEvents(events.data(), events.size(), SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT); |
| 192 | #endif |
| 193 | if (peep != events.size()) |
| 194 | break; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | window_event_result call_default_handler(const d_event &event) |
| 199 | { |
| 200 | return standard_handler(event); |
| 201 | } |
| 202 | |
| 203 | window_event_result event_send(const d_event &event) |
| 204 | { |
| 205 | window *wind; |
| 206 | window_event_result handled = window_event_result::ignored; |
| 207 | |
| 208 | for (wind = window_get_front(); wind && handled == window_event_result::ignored; wind = window_get_prev(*wind)) |
| 209 | if (wind->is_visible()) |
| 210 | { |
| 211 | handled = wind->send_event(event); |
| 212 | |
| 213 | if (handled == window_event_result::deleted) // break away if necessary: window_send_event() could have closed wind by now |
| 214 | break; |
| 215 | if (wind->is_modal()) |
| 216 | break; |
| 217 | } |
| 218 | |
| 219 | if (handled == window_event_result::ignored) |
| 220 | return call_default_handler(event); |
| 221 | |
| 222 | return handled; |
| 223 | } |
| 224 | |
| 225 | // Process the first event in queue, sending to the appropriate handler |
| 226 | // This is the new object-oriented system |
| 227 | // Uses the old system for now, but this may change |
| 228 | window_event_result event_process(void) |
| 229 | { |
| 230 | window *wind = window_get_front(); |
| 231 | window_event_result highest_result; |
| 232 | |
| 233 | timer_update(); |
| 234 | |
| 235 | highest_result = event_poll(); // send input events first |
| 236 | |
| 237 | cmd_queue_process(); |
| 238 | |
| 239 | // Doing this prevents problems when a draw event can create a newmenu, |
| 240 | // such as some network menus when they report a problem |
| 241 | // Also checking for window_event_result::deleted in case a window was created |
| 242 | // with the same pointer value as the deleted one |
| 243 | if ((highest_result == window_event_result::deleted) || (window_get_front() != wind)) |
| 244 | return highest_result; |
| 245 | |
| 246 | const d_event event{event_type::window_draw}; // then draw all visible windows |
| 247 | for (wind = window_get_first(); wind != nullptr;) |
| 248 | { |
| 249 | if (wind->is_visible()) |
| 250 | { |
| 251 | auto prev = window_get_prev(*wind); |
| 252 | auto result = wind->send_event(event); |
| 253 | highest_result = std::max(result, highest_result); |
| 254 | if (result == window_event_result::deleted) |
| 255 | { |
| 256 | if (!prev) |
| 257 | { |
| 258 | wind = window_get_first(); |
| 259 | continue; |
| 260 | } |
| 261 | wind = prev; // take the previous window and get the next one from that (if prev isn't nullptr) |
| 262 | } |
| 263 | } |
| 264 | wind = window_get_next(*wind); |
| 265 | } |
| 266 | |
| 267 | gr_flip(); |
| 268 | |
| 269 | return highest_result; |
| 270 | } |
| 271 | |
| 272 | namespace { |
| 273 | |
| 274 | template <bool activate_focus> |
| 275 | static void event_change_focus() |
| 276 | { |
| 277 | const auto enable_grab = activate_focus && CGameCfg.Grabinput && likely(!CGameArg.DbgForbidConsoleGrab); |
| 278 | #if SDL_MAJOR_VERSION == 1 |
| 279 | SDL_WM_GrabInput(enable_grab ? SDL_GRAB_ON : SDL_GRAB_OFF); |
| 280 | #elif SDL_MAJOR_VERSION == 2 |
| 281 | SDL_SetWindowGrab(g_pRebirthSDLMainWindow, enable_grab ? SDL_TRUE : SDL_FALSE); |
| 282 | SDL_SetRelativeMouseMode(enable_grab ? SDL_TRUE : SDL_FALSE); |
| 283 | #endif |
| 284 | if (activate_focus) |
| 285 | mouse_disable_cursor(); |
| 286 | else |
| 287 | mouse_enable_cursor(); |
| 288 | } |
| 289 | |
| 290 | } |
| 291 | |
| 292 | void event_enable_focus() |
| 293 | { |
| 294 | event_change_focus<true>(); |
| 295 | } |
| 296 | |
| 297 | void event_disable_focus() |
| 298 | { |
| 299 | event_change_focus<false>(); |
| 300 | } |
| 301 | |
| 302 | #if DXX_USE_EDITOR |
| 303 | static fix64 last_event = 0; |
| 304 | |
| 305 | void event_reset_idle_seconds() |
| 306 | { |
| 307 | last_event = timer_query(); |
| 308 | } |
| 309 | |
| 310 | fix event_get_idle_seconds() |
| 311 | { |
| 312 | return (timer_query() - last_event)/F1_0; |
| 313 | } |
| 314 | #endif |
| 315 | |
| 316 | } |