Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDL: add multitouch support #1599

Merged
merged 3 commits into from
Apr 16, 2023
Merged

SDL: add multitouch support #1599

merged 3 commits into from
Apr 16, 2023

Conversation

Frenzie
Copy link
Member

@Frenzie Frenzie commented Apr 15, 2023

Partially inspired by the Android implementation.

After I added it, I suddenly realized we could've had fake multitouch on desktop for years. There's a right button on the mouse, after all. A possible alternative would be to simply fake right mouse button as being two fingers at once, as opposed to having to truly press left and right at the same time.


This change is Reviewable

Partially inspired by the Android implementation.

After I added it, I suddenly realized we could've had fake multitouch on desktop for years. There's a right button on the mouse, after all.
A possible alternative would be to simply fake right mouse button as being to fingers at once, as opposed to having to truly press left and right at the same time.
@Frenzie Frenzie requested a review from NiLuJe April 15, 2023 20:23
@Frenzie
Copy link
Member Author

Frenzie commented Apr 15, 2023

@ptrm Btw, could you check if removing these lines works as expected in combination with the check for SDL_TOUCH_MOUSEID? It might require a "new" version of SDL in case you still happen to be running one from, say, five years ago.

-- if we got an event, examine it here and generate events for koreader
if ffi.os == "OSX" and (event.type == SDL.SDL_FINGERMOTION or
event.type == SDL.SDL_FINGERDOWN or
event.type == SDL.SDL_FINGERUP) then
-- noop for trackpad finger inputs which interfere with emulated mouse inputs
do end -- luacheck: ignore 541

@NiLuJe
Copy link
Member

NiLuJe commented Apr 15, 2023

Untested, but nothing jumps out at a quick glance ;).

@Frenzie
Copy link
Member Author

Frenzie commented Apr 15, 2023

Rest assured I'd have noticed if anything didn't work. ;-)

@ptrm Have you been able to try the Apple touchpad? I noticed you left a thumbs up :-P

@Frenzie
Copy link
Member Author

Frenzie commented Apr 16, 2023

I tested macOS 13.3.1 myself. It seems to be fine.

@Frenzie Frenzie merged commit ca16fb8 into koreader:master Apr 16, 2023
@Frenzie Frenzie deleted the sdl-touch branch April 16, 2023 09:16
Frenzie added a commit to Frenzie/koreader that referenced this pull request Apr 16, 2023
Includes:

* Fix build on Mac OS X 13.3+ (missing ifr_netmask) (koreader/koreader-base#1596)
* Add C declarations needed for zmq http server (koreader/koreader-base#1595)
* SDL: add multitouch support (koreader/koreader-base#1599)
Frenzie added a commit to koreader/koreader that referenced this pull request Apr 16, 2023
Includes:

* Fix build on Mac OS X 13.3+ (missing ifr_netmask) (koreader/koreader-base#1596)
* Add C declarations needed for zmq http server (koreader/koreader-base#1595)
* SDL: add multitouch support (koreader/koreader-base#1599)
local x = is_finger and event.tfinger.x * S.w or event.button.x
local y = is_finger and event.tfinger.y * S.h or event.button.y
setPointerDownState(slot, true)
genTouchDownEvent(event, slot, x, y)
Copy link
Member

@NiLuJe NiLuJe Apr 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be called for !is_finger, while it will access event.tfinger.fingerId, I'm not sure what it points to in the SDL magic union in those cases ;).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, a minor oversight for the mouse, though it doesn't really matter in practice.

Copy link
Member Author

@Frenzie Frenzie Apr 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The most obvious would probably be:

diff --git a/ffi/SDL2_0.lua b/ffi/SDL2_0.lua
index 4823dcac..ee5a767d 100644
--- a/ffi/SDL2_0.lua
+++ b/ffi/SDL2_0.lua
@@ -235,8 +235,9 @@ local function getFingerSlot(event)
 end
 
 local function genTouchDownEvent(event, slot, x, y)
+    local is_finger = event.type == SDL.SDL_FINGERDOWN
     genEmuEvent(C.EV_ABS, C.ABS_MT_SLOT, slot)
-    genEmuEvent(C.EV_ABS, C.ABS_MT_TRACKING_ID, tonumber(event.tfinger.fingerId))
+    genEmuEvent(C.EV_ABS, C.ABS_MT_TRACKING_ID, is_finger and tonumber(event.tfinger.fingerId) or slot)
     genEmuEvent(C.EV_ABS, C.ABS_MT_POSITION_X, x)
     genEmuEvent(C.EV_ABS, C.ABS_MT_POSITION_Y, y)
     genEmuEvent(C.EV_SYN, C.SYN_REPORT, 0)

I prefer tracking id not to be constantly 0 and 1 'cause if nothing else it's clearer to read. 😅

@ptrm
Copy link
Contributor

ptrm commented Apr 17, 2023

Rest assured I'd have noticed if anything didn't work. ;-)

@ptrm Have you been able to try the Apple touchpad? I noticed you left a thumbs up :-P

Sorry, that was meant as a quick sign of queueing it ;) will try it today, I don't have a touchpad on my mac mini though, so it'll be just mouse.

@Frenzie
Copy link
Member Author

Frenzie commented Apr 17, 2023

Additional tests are welcome, but I did test the touchpad on a Macbook Air M1 already. It didn't seem to generate finger events.

Looking again that used to be the behavior at some point in the past (and clearly it was back ca. 2014 when that code was written), but it looks like more recent SDL has made it configurable:

libsdl-org/SDL@bdc7f95

Going back a little further, it was four years ago that the touchpad behavior was changed for Mac, see libsdl-org/SDL@e841b06 and a little further back libsdl-org/SDL@d9a2eff

So I think we might want to set SDL_HINT_TRACKPAD_IS_TOUCH_ONLY for best results in SDL 2.23+ (?) but the rest of the code should work fine either way regardless on any SDL from at least the past four years. And if there are any lingering issues with synthesized touch or mouse events they'll probably occur across platforms.

Frenzie added a commit to Frenzie/koreader-base that referenced this pull request Apr 17, 2023
Frenzie added a commit to Frenzie/koreader-base that referenced this pull request Apr 17, 2023
…rted trackpads

macOS only for now, in SDL itself.

Follow-up to <koreader#1599>.
Frenzie added a commit to koreader/koreader that referenced this pull request Dec 7, 2023
Frenzie added a commit to koreader/koreader that referenced this pull request Dec 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants