Support SDL stylus input (#15344)

This commit is contained in:
Tong authored and GitHub committed 2026-07-11 08:55:29 +02:00
1 parent cf9c59a4a8
commit 533c1612d1
5 files changed
+253 -15

No files matched your search

+105 -15
View File
@@ -27,6 +27,8 @@ local KEY_RELEASE = 0
-- Based on ABS_MT_TOOL_TYPE values on Elan panels
local TOOL_TYPE_FINGER = 0
local TOOL_TYPE_PEN = 1
local TOOL_TYPE_ERASER = 2
local TOOL_TYPE_HIGHLIGHTER = 3
-- For debug logging of ev.type
local linux_evdev_type_map = {
@@ -455,6 +457,66 @@ function Input:gestureAdjustHook(ges)
-- do nothing by default
end
--[[--
Register a callback for stylus/pen events.
Called before gesture detection with fully processed slot data.
The callback receives the Input object and a slot table: {slot, id, x, y, tool, timev}.
Return true from the callback to dominate the event and remove it from gesture detection.
]]
function Input:registerStylusCallback(callback)
self.stylus_callback = callback
logger.info("Input: stylus callback registered, pen_slot =", self.pen_slot)
end
function Input:unregisterStylusCallback()
self.stylus_callback = nil
logger.info("Input: stylus callback unregistered")
end
--[[--
Filter stylus events from MTSlots and route them to the stylus callback.
Called before gesture detection. Modifies self.MTSlots in place.
--]]
function Input:routeStylusEvents()
if not self.stylus_callback or not self.MTSlots or #self.MTSlots == 0 then
return
end
local dominated_indices = {}
for i, slot in ipairs(self.MTSlots) do
local is_stylus = slot.tool == TOOL_TYPE_PEN
or slot.tool == TOOL_TYPE_ERASER
or slot.tool == TOOL_TYPE_HIGHLIGHTER
or (self.pen_slot and slot.slot == self.pen_slot)
if is_stylus then
if slot.tool == TOOL_TYPE_PEN then
if self.stylus_eraser_active then
slot.tool = TOOL_TYPE_ERASER
elseif self.stylus_highlighter_active then
slot.tool = TOOL_TYPE_HIGHLIGHTER
end
end
logger.dbg("Input:routeStylusEvents: stylus detected in slot", slot.slot,
"tool=", slot.tool, "id=", slot.id, "x=", slot.x, "y=", slot.y,
"pen_slot=", self.pen_slot, "eraser=", self.stylus_eraser_active,
"highlighter=", self.stylus_highlighter_active)
local dominated = self.stylus_callback(self, slot)
if dominated then
table.insert(dominated_indices, i)
logger.dbg("Input:routeStylusEvents: slot", slot.slot,
"dominated, will remove from gesture detection")
end
end
end
for i = #dominated_indices, 1, -1 do
table.remove(self.MTSlots, dominated_indices[i])
end
end
--- Catalog of predefined hooks.
-- These are *not* usable directly as hooks, they're just building blocks (c.f., Kobo)
function Input:adjustABS_SwitchXY(ev)
@@ -675,22 +737,40 @@ function Input:handleKeyBoardEv(ev)
return
end
elseif self.wacom_protocol then
if ev.code == C.BTN_TOOL_PEN then
-- Switch to the dedicated pen slot, and make sure it's active, as this can come in a dedicated input frame
self:setupSlotData(self.pen_slot)
if ev.value == 1 then
self:setCurrentMtSlot("tool", TOOL_TYPE_PEN)
else
self:setCurrentMtSlot("tool", TOOL_TYPE_FINGER)
-- Switch back to our main finger slot
self.cur_slot = self.main_finger_slot
end
end
return
elseif ev.code == C.BTN_TOUCH then
-- BTN_TOUCH is bracketed by BTN_TOOL_PEN, so we can limit this to pens, to avoid stomping on panel slots.
if self:getCurrentMtSlotData("tool") == TOOL_TYPE_PEN then
local is_sdl = self.device:isSDL()
-- On Kobo-style stylus devices, barrel/tool buttons can double as
-- eraser/highlighter tool selectors. SDL/Linux pen buttons are handled as
-- plain buttons instead, so standard side buttons don't rewrite the tool.
local stylus_buttons_select_tool = not is_sdl
if stylus_buttons_select_tool then
if ev.code == C.BTN_STYLUS then
self.stylus_eraser_active = ev.value == 1
elseif ev.code == C.BTN_STYLUS2 then
self.stylus_highlighter_active = ev.value == 1
end
end
local stylus_tool_protocol = self.wacom_protocol or is_sdl
if stylus_tool_protocol and (ev.code == C.BTN_TOOL_PEN or ev.code == C.BTN_TOOL_RUBBER) then
-- Switch to the dedicated pen slot, and make sure it's active, as this can come in a dedicated input frame.
self:setupSlotData(self.pen_slot)
if ev.value == 1 then
self:setCurrentMtSlot("tool", ev.code == C.BTN_TOOL_RUBBER and TOOL_TYPE_ERASER or TOOL_TYPE_PEN)
else
self:setCurrentMtSlot("tool", TOOL_TYPE_FINGER)
-- Switch back to our main finger slot.
self.cur_slot = self.main_finger_slot
end
return
elseif self.wacom_protocol then
if ev.code == C.BTN_TOUCH then
-- BTN_TOUCH is bracketed by BTN_TOOL_PEN/BTN_TOOL_RUBBER, so we can limit this to styluses, to avoid stomping on panel slots.
local tool = self:getCurrentMtSlotData("tool")
if tool == TOOL_TYPE_PEN or tool == TOOL_TYPE_ERASER then
-- Make sure the pen slot is active, as this can come in a dedicated input frame
-- (i.e., we need it to be referenced by self.MTSlots for the lift to be picked up in the EV_SYN:SYN_REPORT handler).
-- (Conversely, getCurrentMtSlotData pokes at the *persistent* slot data in self.ev_slots,
@@ -966,6 +1046,7 @@ function Input:handleTouchEv(ev)
for _, MTSlot in ipairs(self.MTSlots) do
self:setMtSlot(MTSlot.slot, "timev", time.timeval(ev.time))
end
self:routeStylusEvents()
-- feed ev in all slots to state machine
local touch_gestures = self.gesture_detector:feedEvent(self.MTSlots)
self:newFrame()
@@ -1007,6 +1088,7 @@ function Input:handleMixedTouchEv(ev)
for _, MTSlot in ipairs(self.MTSlots) do
self:setMtSlot(MTSlot.slot, "timev", time.timeval(ev.time))
end
self:routeStylusEvents()
-- feed ev in all slots to state machine
local touch_gestures = self.gesture_detector:feedEvent(self.MTSlots)
self:newFrame()
@@ -1072,6 +1154,7 @@ function Input:handleTouchEvSnow(ev)
for _, MTSlot in ipairs(self.MTSlots) do
self:setMtSlot(MTSlot.slot, "timev", time.timeval(ev.time))
end
self:routeStylusEvents()
-- feed ev in all slots to state machine
local touch_gestures = self.gesture_detector:feedEvent(self.MTSlots)
self:newFrame()
@@ -1132,6 +1215,7 @@ function Input:handleTouchEvPhoenix(ev)
for _, MTSlot in ipairs(self.MTSlots) do
self:setMtSlot(MTSlot.slot, "timev", time.timeval(ev.time))
end
self:routeStylusEvents()
-- feed ev in all slots to state machine
local touch_gestures = self.gesture_detector:feedEvent(self.MTSlots)
self:newFrame()
@@ -1181,6 +1265,7 @@ function Input:handleTouchEvLegacy(ev)
self:setMtSlot(MTSlot.slot, "timev", time.timeval(ev.time))
end
self:routeStylusEvents()
-- feed ev in all slots to state machine
local touch_gestures = self.gesture_detector:feedEvent(self.MTSlots)
self:newFrame()
@@ -1707,4 +1792,9 @@ function Input:inhibitInputUntil(set_or_seconds)
self:inhibitInput(true)
end
Input.TOOL_TYPE_FINGER = TOOL_TYPE_FINGER
Input.TOOL_TYPE_PEN = TOOL_TYPE_PEN
Input.TOOL_TYPE_ERASER = TOOL_TYPE_ERASER
Input.TOOL_TYPE_HIGHLIGHTER = TOOL_TYPE_HIGHLIGHTER
return Input
+1
View File
@@ -39,6 +39,7 @@ function Key:getSequence()
end
end
table.insert(seq, self.key)
return seq
end
--[[
+4
View File
@@ -84,4 +84,8 @@ return {
[1073741894] = "PrintScreen",
[1073741895] = "ScrollLock",
-- Synthetic Linux pen button codes emitted by ffi/SDL3.lua for SDL3 pen events.
[331] = "Stylus",
[332] = "Stylus2",
}
+125
View File
@@ -130,4 +130,129 @@ Event: time 1510346969.076908, -------------- SYN_REPORT ------------
end)
end)
describe("stylus callback routing", function()
local function withInputState(overrides, callback)
local keys = {}
local old_state = {}
for key, value in pairs(overrides) do
table.insert(keys, key)
old_state[key] = Input[key]
Input[key] = value
end
local ok, err = pcall(callback)
for _, key in ipairs(keys) do
Input[key] = old_state[key]
end
if not ok then
error(err)
end
end
it("routes stylus slots before gesture detection and removes dominated slots", function()
local old_callback = Input.stylus_callback
local old_slots = Input.MTSlots
local calls = {}
Input.MTSlots = {
{ slot = Input.pen_slot, id = 7, x = 10, y = 20, tool = Input.TOOL_TYPE_PEN },
{ slot = Input.main_finger_slot, id = 8, x = 30, y = 40, tool = Input.TOOL_TYPE_FINGER },
}
Input:registerStylusCallback(function(_, slot)
table.insert(calls, slot)
return true
end)
Input:routeStylusEvents()
assert.is_equal(1, #calls)
assert.is_equal(Input.pen_slot, calls[1].slot)
assert.is_equal(1, #Input.MTSlots)
assert.is_equal(Input.main_finger_slot, Input.MTSlots[1].slot)
Input.stylus_callback = old_callback
Input.MTSlots = old_slots
end)
it("does not turn SDL pen side buttons into eraser tool selectors", function()
local old_device = Input.device
local old_event_map = Input.event_map
local old_eraser_active = Input.stylus_eraser_active
Input.device = { isSDL = function() return true end }
Input.event_map = {}
Input.stylus_eraser_active = nil
Input:handleKeyBoardEv({
type = C.EV_KEY,
code = C.BTN_STYLUS,
value = 1,
})
assert.is_nil(Input.stylus_eraser_active)
Input.device = old_device
Input.event_map = old_event_map
Input.stylus_eraser_active = old_eraser_active
end)
it("ignores pen tool key events without a stylus tool protocol", function()
withInputState({
active_slots = {},
cur_slot = Input.main_finger_slot,
device = { isSDL = function() return false end },
event_map = {},
ev_slots = {
[Input.main_finger_slot] = { slot = Input.main_finger_slot },
},
MTSlots = {},
wacom_protocol = false,
}, function()
Input:handleKeyBoardEv({
type = C.EV_KEY,
code = C.BTN_TOOL_PEN,
value = 1,
})
assert.is_nil(Input.ev_slots[Input.pen_slot])
assert.is_equal(Input.main_finger_slot, Input.cur_slot)
end)
end)
it("accepts pen tool key events from SDL", function()
withInputState({
active_slots = {},
cur_slot = Input.main_finger_slot,
device = { isSDL = function() return true end },
event_map = {},
ev_slots = {
[Input.main_finger_slot] = { slot = Input.main_finger_slot },
},
MTSlots = {},
wacom_protocol = false,
}, function()
Input:handleKeyBoardEv({
type = C.EV_KEY,
code = C.BTN_TOOL_RUBBER,
value = 1,
})
assert.is_equal(Input.pen_slot, Input.cur_slot)
assert.is_equal(Input.TOOL_TYPE_ERASER, Input.ev_slots[Input.pen_slot].tool)
Input:handleKeyBoardEv({
type = C.EV_KEY,
code = C.BTN_TOOL_RUBBER,
value = 0,
})
assert.is_equal(Input.main_finger_slot, Input.cur_slot)
assert.is_equal(Input.TOOL_TYPE_FINGER, Input.ev_slots[Input.pen_slot].tool)
end)
end)
end)
end)
+18
View File
@@ -0,0 +1,18 @@
describe("Key module", function()
local Key
setup(function()
require("commonrequire")
Key = require("device/key")
end)
it("returns the key sequence for string conversion", function()
local key = Key:new("Highlighter", {
Ctrl = false,
Shift = true,
})
assert.are.same({ "Shift", "Highlighter" }, key:getSequence())
assert.equals("Shift-Highlighter", tostring(key))
end)
end)