Track button state for each mouse input source separately This way we'll get button down and up events for each mouseID individually. Fixes https://github.com/libsdl-org/SDL/issues/4518
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
diff --git a/src/events/SDL_mouse.c b/src/events/SDL_mouse.c
index 45fb092..9d2ea9a 100644
--- a/src/events/SDL_mouse.c
+++ b/src/events/SDL_mouse.c
@@ -177,32 +177,24 @@ SDL_GetMouse(void)
return &SDL_mouse;
}
-SDL_Window *
-SDL_GetMouseFocus(void)
+static Uint32 GetButtonState(SDL_Mouse *mouse)
{
- SDL_Mouse *mouse = SDL_GetMouse();
+ int i;
+ Uint32 buttonstate = 0;
- return mouse->focus;
+ for (i = 0; i < mouse->num_sources; ++i) {
+ buttonstate |= mouse->sources[i].buttonstate;
+ }
+ return buttonstate;
}
-#if 0
-void
-SDL_ResetMouse(void)
+SDL_Window *
+SDL_GetMouseFocus(void)
{
SDL_Mouse *mouse = SDL_GetMouse();
- Uint8 i;
-#ifdef DEBUG_MOUSE
- printf("Resetting mouse\n");
-#endif
- for (i = 1; i <= sizeof(mouse->buttonstate)*8; ++i) {
- if (mouse->buttonstate & SDL_BUTTON(i)) {
- SDL_SendMouseButton(mouse->focus, mouse->mouseID, SDL_RELEASED, i);
- }
- }
- SDL_assert(mouse->buttonstate == 0);
+ return mouse->focus;
}
-#endif
void
SDL_SetMouseFocus(SDL_Window * window)
@@ -299,7 +291,7 @@ SDL_SendMouseMotion(SDL_Window * window, SDL_MouseID mouseID, int relative, int
{
if (window && !relative) {
SDL_Mouse *mouse = SDL_GetMouse();
- if (!SDL_UpdateMouseFocus(window, x, y, mouse->buttonstate, (mouseID == SDL_TOUCH_MOUSEID) ? SDL_FALSE : SDL_TRUE)) {
+ if (!SDL_UpdateMouseFocus(window, x, y, GetButtonState(mouse), (mouseID == SDL_TOUCH_MOUSEID) ? SDL_FALSE : SDL_TRUE)) {
return 0;
}
}
@@ -399,7 +391,7 @@ SDL_PrivateSendMouseMotion(SDL_Window * window, SDL_MouseID mouseID, int relativ
}
/* Ignore relative motion positioning the first touch */
- if (mouseID == SDL_TOUCH_MOUSEID && !mouse->buttonstate) {
+ if (mouseID == SDL_TOUCH_MOUSEID && !GetButtonState(mouse)) {
xrel = 0;
yrel = 0;
}
@@ -473,7 +465,7 @@ SDL_PrivateSendMouseMotion(SDL_Window * window, SDL_MouseID mouseID, int relativ
event.motion.which = mouseID;
/* Set us pending (or clear during a normal mouse movement event) as having triggered */
mouse->was_touch_mouse_events = (mouseID == SDL_TOUCH_MOUSEID)? SDL_TRUE : SDL_FALSE;
- event.motion.state = mouse->buttonstate;
+ event.motion.state = GetButtonState(mouse);
event.motion.x = mouse->x;
event.motion.y = mouse->y;
event.motion.xrel = xrel;
@@ -491,6 +483,30 @@ SDL_PrivateSendMouseMotion(SDL_Window * window, SDL_MouseID mouseID, int relativ
return posted;
}
+static SDL_MouseInputSource *GetMouseInputSource(SDL_Mouse *mouse, SDL_MouseID mouseID)
+{
+ SDL_MouseInputSource *source, *sources;
+ int i;
+
+ for (i = 0; i < mouse->num_sources; ++i) {
+ source = &mouse->sources[i];
+ if (source->mouseID == mouseID) {
+ return source;
+ }
+ }
+
+ sources = (SDL_MouseInputSource *)SDL_realloc(mouse->sources, (mouse->num_sources + 1)*sizeof(*mouse->sources));
+ if (sources) {
+ mouse->sources = sources;
+ ++mouse->num_sources;
+ source = &sources[mouse->num_sources - 1];
+ source->mouseID = mouseID;
+ source->buttonstate = 0;
+ return source;
+ }
+ return NULL;
+}
+
static SDL_MouseClickState *GetMouseClickState(SDL_Mouse *mouse, Uint8 button)
{
if (button >= mouse->num_clickstates) {
@@ -515,7 +531,14 @@ SDL_PrivateSendMouseButton(SDL_Window * window, SDL_MouseID mouseID, Uint8 state
SDL_Mouse *mouse = SDL_GetMouse();
int posted;
Uint32 type;
- Uint32 buttonstate = mouse->buttonstate;
+ Uint32 buttonstate;
+ SDL_MouseInputSource *source;
+
+ source = GetMouseInputSource(mouse, mouseID);
+ if (!source) {
+ return 0;
+ }
+ buttonstate = source->buttonstate;
/* SDL_HINT_MOUSE_TOUCH_EVENTS: controlling whether mouse events should generate synthetic touch events */
if (mouse->mouse_touch_events) {
@@ -560,11 +583,11 @@ SDL_PrivateSendMouseButton(SDL_Window * window, SDL_MouseID mouseID, Uint8 state
SDL_UpdateMouseFocus(window, mouse->x, mouse->y, buttonstate, SDL_TRUE);
}
- if (buttonstate == mouse->buttonstate) {
+ if (buttonstate == source->buttonstate) {
/* Ignore this event, no state change */
return 0;
}
- mouse->buttonstate = buttonstate;
+ source->buttonstate = buttonstate;
if (clicks < 0) {
SDL_MouseClickState *clickstate = GetMouseClickState(mouse, button);
@@ -722,10 +745,17 @@ SDL_MouseQuit(void)
mouse->def_cursor = NULL;
}
+ if (mouse->sources) {
+ SDL_free(mouse->sources);
+ mouse->sources = NULL;
+ }
+ mouse->num_sources = 0;
+
if (mouse->clickstate) {
SDL_free(mouse->clickstate);
mouse->clickstate = NULL;
}
+ mouse->num_clickstates = 0;
SDL_DelHintCallback(SDL_HINT_MOUSE_NORMAL_SPEED_SCALE,
SDL_MouseNormalSpeedScaleChanged, mouse);
@@ -745,7 +775,7 @@ SDL_GetMouseState(int *x, int *y)
if (y) {
*y = mouse->y;
}
- return mouse->buttonstate;
+ return GetButtonState(mouse);
}
Uint32
@@ -761,7 +791,7 @@ SDL_GetRelativeMouseState(int *x, int *y)
}
mouse->xdelta = 0;
mouse->ydelta = 0;
- return mouse->buttonstate;
+ return GetButtonState(mouse);
}
Uint32
diff --git a/src/events/SDL_mouse_c.h b/src/events/SDL_mouse_c.h
index 9c4c628..0915c37 100644
--- a/src/events/SDL_mouse_c.h
+++ b/src/events/SDL_mouse_c.h
@@ -35,6 +35,12 @@ struct SDL_Cursor
typedef struct
{
+ SDL_MouseID mouseID;
+ Uint32 buttonstate;
+} SDL_MouseInputSource;
+
+typedef struct
+{
int last_x, last_y;
Uint32 last_timestamp;
Uint8 click_count;
@@ -82,7 +88,6 @@ typedef struct
int last_x, last_y; /* the last reported x and y coordinates */
float accumulated_wheel_x;
float accumulated_wheel_y;
- Uint32 buttonstate;
SDL_bool has_position;
SDL_bool relative_mode;
SDL_bool relative_mode_warp;
@@ -96,6 +101,10 @@ typedef struct
SDL_bool mouse_touch_events;
SDL_bool was_touch_mouse_events; /* Was a touch-mouse event pending? */
+ /* Data for input source state */
+ int num_sources;
+ SDL_MouseInputSource *sources;
+
/* Data for double-click tracking */
int num_clickstates;
SDL_MouseClickState *clickstate;