| 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 mouse driver |
| 10 |
* |
| 11 |
*/ |
| 12 |
|
| 13 |
#include <string.h> |
| 14 |
#include <SDL.h> |
| 15 |
|
| 16 |
#include "maths.h" |
| 17 |
#include "timer.h" |
| 18 |
#include "event.h" |
| 19 |
#include "window.h" |
| 20 |
#include "mouse.h" |
| 21 |
#include "dxxerror.h" |
| 22 |
#include "args.h" |
| 23 |
#include "gr.h" |
| 24 |
|
| 25 |
#include "d_underlying_value.h" |
| 26 |
|
| 27 |
namespace dcx { |
| 28 |
|
| 29 |
namespace { |
| 30 |
|
| 31 |
struct flushable_mouseinfo |
| 32 |
{ |
| 33 |
int delta_x, delta_y, delta_z; |
| 34 |
int z; |
| 35 |
}; |
| 36 |
|
| 37 |
struct mouseinfo : flushable_mouseinfo |
| 38 |
{ |
| 39 |
int x,y; |
| 40 |
int cursor_enabled; |
| 41 |
fix64 cursor_time; |
| 42 |
enumerated_array<fix64, MOUSE_MAX_BUTTONS, mbtn> time_lastpressed; |
| 43 |
}; |
| 44 |
|
| 45 |
static mouseinfo Mouse; |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
d_event_mousebutton::d_event_mousebutton(const event_type etype, const mbtn b) : |
| 50 |
d_event{etype}, button{b} |
| 51 |
{ |
| 52 |
} |
| 53 |
|
| 54 |
void mouse_init(void) |
| 55 |
{ |
| 56 |
Mouse = {}; |
| 57 |
} |
| 58 |
|
| 59 |
void mouse_close(void) |
| 60 |
{ |
| 61 |
SDL_ShowCursor(SDL_ENABLE); |
| 62 |
} |
| 63 |
|
| 64 |
namespace { |
| 65 |
|
| 66 |
static window_event_result maybe_send_z_move(const mbtn button) |
| 67 |
{ |
| 68 |
short dz; |
| 69 |
if (button == mbtn::z_up) |
| 70 |
{ |
| 71 |
Mouse.delta_z += Z_SENSITIVITY; |
| 72 |
Mouse.z += Z_SENSITIVITY; |
| 73 |
dz = Z_SENSITIVITY; |
| 74 |
} |
| 75 |
else if (button == mbtn::z_down) |
| 76 |
{ |
| 77 |
Mouse.delta_z -= Z_SENSITIVITY; |
| 78 |
Mouse.z -= Z_SENSITIVITY; |
| 79 |
dz = -1*Z_SENSITIVITY; |
| 80 |
} |
| 81 |
else |
| 82 |
return window_event_result::ignored; |
| 83 |
return event_send(d_event_mouse_moved{event_type::mouse_moved, 0, 0, dz}); |
| 84 |
} |
| 85 |
|
| 86 |
static window_event_result send_singleclick(const bool pressed, const mbtn button) |
| 87 |
{ |
| 88 |
const d_event_mousebutton event{pressed ? event_type::mouse_button_down : event_type::mouse_button_up, button}; |
| 89 |
con_printf(CON_DEBUG, "Sending event EVENT_MOUSE_BUTTON_%s, button %d, coords %d,%d,%d", |
| 90 |
pressed ? "DOWN" : "UP", underlying_value(event.button), Mouse.x, Mouse.y, Mouse.z); |
| 91 |
return event_send(event); |
| 92 |
} |
| 93 |
|
| 94 |
static window_event_result maybe_send_doubleclick(const fix64 now, const mbtn button) |
| 95 |
{ |
| 96 |
auto &when = Mouse.time_lastpressed[button]; |
| 97 |
const auto then{when}; |
| 98 |
when = now; |
| 99 |
if (now > then + F1_0/5) |
| 100 |
return window_event_result::ignored; |
| 101 |
const d_event_mousebutton event{event_type::mouse_double_clicked, button}; |
| 102 |
con_printf(CON_DEBUG, "Sending event event_type::mouse_double_clicked, button %d, coords %d,%d", underlying_value(button), Mouse.x, Mouse.y); |
| 103 |
return event_send(event); |
| 104 |
} |
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
window_event_result mouse_button_handler(const SDL_MouseButtonEvent *const mbe) |
| 109 |
{ |
| 110 |
window_event_result highest_result(window_event_result::ignored); |
| 111 |
|
| 112 |
if (unlikely(CGameArg.CtlNoMouse)) |
| 113 |
return window_event_result::ignored; |
| 114 |
// to bad, SDL buttons use a different mapping as descent expects, |
| 115 |
// this is at least true and tested for the first three buttons |
| 116 |
static constexpr std::array<mbtn, 17> button_remap{{ |
| 117 |
mbtn::left, |
| 118 |
mbtn::middle, |
| 119 |
mbtn::right, |
| 120 |
mbtn::z_up, |
| 121 |
mbtn::z_down, |
| 122 |
mbtn::pitch_backward, |
| 123 |
mbtn::pitch_forward, |
| 124 |
mbtn::bank_left, |
| 125 |
mbtn::bank_right, |
| 126 |
mbtn::head_left, |
| 127 |
mbtn::head_right, |
| 128 |
mbtn::_11, |
| 129 |
mbtn::_12, |
| 130 |
mbtn::_13, |
| 131 |
mbtn::_14, |
| 132 |
mbtn::_15, |
| 133 |
mbtn::_16, |
| 134 |
}}; |
| 135 |
const unsigned button_idx = mbe->button - 1; // -1 since SDL seems to start counting at 1 |
| 136 |
if (unlikely(button_idx >= button_remap.size())) |
| 137 |
return window_event_result::ignored; |
| 138 |
|
| 139 |
const auto now = timer_query(); |
| 140 |
const auto button = button_remap[button_idx]; |
| 141 |
const auto mbe_state = mbe->state; |
| 142 |
Mouse.cursor_time = now; |
| 143 |
|
| 144 |
const auto pressed = mbe_state != SDL_RELEASED; |
| 145 |
if (pressed) { |
| 146 |
highest_result = maybe_send_z_move(button); |
| 147 |
} |
| 148 |
highest_result = std::max(send_singleclick(pressed, button), highest_result); |
| 149 |
//Double-click support |
| 150 |
if (pressed) |
| 151 |
{ |
| 152 |
highest_result = std::max(maybe_send_doubleclick(now, button), highest_result); |
| 153 |
} |
| 154 |
|
| 155 |
return highest_result; |
| 156 |
} |
| 157 |
|
| 158 |
window_event_result mouse_motion_handler(const SDL_MouseMotionEvent *const mme) |
| 159 |
{ |
| 160 |
Mouse.cursor_time = timer_query(); |
| 161 |
Mouse.x += mme->xrel; |
| 162 |
Mouse.y += mme->yrel; |
| 163 |
|
| 164 |
// z handled in mouse_button_handler |
| 165 |
const d_event_mouse_moved event{event_type::mouse_moved, mme->xrel, mme->yrel, 0}; |
| 166 |
|
| 167 |
//con_printf(CON_DEBUG, "Sending event event_type::mouse_moved, relative motion %d,%d,%d", |
| 168 |
// event.dx, event.dy, event.dz); |
| 169 |
return event_send(event); |
| 170 |
} |
| 171 |
|
| 172 |
void mouse_flush() // clears all mice events... |
| 173 |
{ |
| 174 |
// event_poll(); |
| 175 |
static_cast<flushable_mouseinfo &>(Mouse) = {}; |
| 176 |
SDL_GetMouseState(&Mouse.x, &Mouse.y); // necessary because polling only gives us the delta. |
| 177 |
} |
| 178 |
|
| 179 |
//======================================================================== |
| 180 |
std::tuple<int, int, int> mouse_get_pos() |
| 181 |
{ |
| 182 |
//event_poll(); // Have to assume this is called in event_process, because event_poll can cause a window to close (depending on what the user does) |
| 183 |
return { |
| 184 |
Mouse.x, |
| 185 |
Mouse.y, |
| 186 |
Mouse.z, |
| 187 |
}; |
| 188 |
} |
| 189 |
|
| 190 |
window_event_result mouse_in_window(window *wind) |
| 191 |
{ |
| 192 |
auto &canv = wind->w_canv; |
| 193 |
return (static_cast<unsigned>(Mouse.x) - canv.cv_bitmap.bm_x <= canv.cv_bitmap.bm_w) && |
| 194 |
(static_cast<unsigned>(Mouse.y) - canv.cv_bitmap.bm_y <= canv.cv_bitmap.bm_h) ? window_event_result::handled : window_event_result::ignored; |
| 195 |
} |
| 196 |
|
| 197 |
#if 0 |
| 198 |
std::tuple<int, int, int> mouse_get_delta() |
| 199 |
{ |
| 200 |
const int dz{Mouse.delta_z}; |
| 201 |
int dx{}, dy{}; |
| 202 |
Mouse.delta_x = 0; |
| 203 |
Mouse.delta_y = 0; |
| 204 |
Mouse.delta_z = 0; |
| 205 |
SDL_GetRelativeMouseState(&dx, &dy); |
| 206 |
return { |
| 207 |
dx, |
| 208 |
dy, |
| 209 |
dz, |
| 210 |
}; |
| 211 |
} |
| 212 |
#endif |
| 213 |
|
| 214 |
namespace { |
| 215 |
|
| 216 |
template <bool noactivate> |
| 217 |
static void mouse_change_cursor() |
| 218 |
{ |
| 219 |
Mouse.cursor_enabled = (!noactivate && !CGameArg.CtlNoMouse && !CGameArg.CtlNoCursor); |
| 220 |
if (!Mouse.cursor_enabled) |
| 221 |
SDL_ShowCursor(SDL_DISABLE); |
| 222 |
} |
| 223 |
|
| 224 |
} |
| 225 |
|
| 226 |
void mouse_enable_cursor() |
| 227 |
{ |
| 228 |
mouse_change_cursor<false>(); |
| 229 |
} |
| 230 |
|
| 231 |
void mouse_disable_cursor() |
| 232 |
{ |
| 233 |
mouse_change_cursor<true>(); |
| 234 |
} |
| 235 |
|
| 236 |
// If we want to display/hide cursor, do so if not already and also hide it automatically after some time. |
| 237 |
void mouse_cursor_autohide() |
| 238 |
{ |
| 239 |
static fix64 hidden_time = 0; |
| 240 |
|
| 241 |
const auto is_showing = SDL_ShowCursor(SDL_QUERY); |
| 242 |
int result; |
| 243 |
if (Mouse.cursor_enabled) |
| 244 |
{ |
| 245 |
const auto now = timer_query(); |
| 246 |
const auto cursor_time = Mouse.cursor_time; |
| 247 |
const auto recent_cursor_time = cursor_time + (F1_0*2) >= now; |
| 248 |
if (is_showing) |
| 249 |
{ |
| 250 |
if (recent_cursor_time) |
| 251 |
return; |
| 252 |
hidden_time = now; |
| 253 |
result = SDL_DISABLE; |
| 254 |
} |
| 255 |
else |
| 256 |
{ |
| 257 |
if (!(recent_cursor_time && hidden_time + (F1_0/2) < now)) |
| 258 |
return; |
| 259 |
result = SDL_ENABLE; |
| 260 |
} |
| 261 |
} |
| 262 |
else |
| 263 |
{ |
| 264 |
if (!is_showing) |
| 265 |
return; |
| 266 |
result = SDL_DISABLE; |
| 267 |
} |
| 268 |
SDL_ShowCursor(result); |
| 269 |
} |
| 270 |
|
| 271 |
} |