<commit_before>local ffi = require("ffi")
local bit = require("bit")
local C = ffi.C

local android = require("android")
local dummy = require("ffi/linux_input_h")

local input = {
-- to trigger refreshes for certain Android framework events:
    device = nil,
}

function input.open()
end

local inputQueue = {}

local ev_time = ffi.new("struct timeval")
local function genEmuEvent(evtype, code, value)
    C.gettimeofday(ev_time, nil)
    local ev = {
        type = tonumber(evtype),
        code = tonumber(code),
        value = tonumber(value),
        time = { sec = tonumber(ev_time.tv_sec), usec = tonumber(ev_time.tv_usec) }
    }
    table.insert(inputQueue, ev)
end

local function genTouchDownEvent(event, id)
    local x = android.lib.AMotionEvent_getX(event, id)
    local y = android.lib.AMotionEvent_getY(event, id)
    genEmuEvent(C.EV_ABS, C.ABS_MT_SLOT, id)
    genEmuEvent(C.EV_ABS, C.ABS_MT_TRACKING_ID, id)
    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)
end

local function genTouchUpEvent(event, id)
    local x = android.lib.AMotionEvent_getX(event, id)
    local y = android.lib.AMotionEvent_getY(event, id)
    genEmuEvent(C.EV_ABS, C.ABS_MT_SLOT, id)
    genEmuEvent(C.EV_ABS, C.ABS_MT_TRACKING_ID, -1)
    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)
end

local function genTouchMoveEvent(event, id, index)
    local x = android.lib.AMotionEvent_getX(event, index)
    local y = android.lib.AMotionEvent_getY(event, index)
    genEmuEvent(C.EV_ABS, C.ABS_MT_SLOT, id)
    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)
end

local is_in_touch = false
local function motionEventHandler(motion_event)
    if android.isTouchscreenIgnored() then
        return
    end
    local action = android.lib.AMotionEvent_getAction(motion_event)
    local pointer_count = android.lib.AMotionEvent_getPointerCount(motion_event)
    local pointer_index = bit.rshift(
            bit.band(action, C.AMOTION_EVENT_ACTION_POINTER_INDEX_MASK),
            C.AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT)
    local id = android.lib.AMotionEvent_getPointerId(motion_event, pointer_index)
    local flags = bit.band(action, C.AMOTION_EVENT_ACTION_MASK)
    if flags == C.AMOTION_EVENT_ACTION_DOWN then
        is_in_touch = true
        genTouchDownEvent(motion_event, id)
    elseif flags == C.AMOTION_EVENT_ACTION_POINTER_DOWN then
        is_in_touch = true
        genTouchDownEvent(motion_event, id)
    elseif flags == C.AMOTION_EVENT_ACTION_UP then
        is_in_touch = false
        genTouchUpEvent(motion_event, id)
    elseif flags == C.AMOTION_EVENT_ACTION_POINTER_UP then
        is_in_touch = false
        genTouchUpEvent(motion_event, id)
    elseif flags == C.AMOTION_EVENT_ACTION_MOVE then
        if is_in_touch then
            for index = 0, pointer_count - 1 do
                id = android.lib.AMotionEvent_getPointerId(motion_event, index)
                genTouchMoveEvent(motion_event, id, index)
            end
        end
    end
end

local function keyEventHandler(key_event)
    local code = android.lib.AKeyEvent_getKeyCode(key_event)
    local action = android.lib.AKeyEvent_getAction(key_event)
    if code == C.AKEYCODE_VOLUME_UP
    or code == C.AKEYCODE_VOLUME_DOWN then
        if android.getVolumeKeysIgnored() then
            return 0 -- event not consumed
        end
    elseif code == C.AKEYCODE_MEDIA_PLAY_PAUSE
    or code == C.AKEYCODE_MEDIA_STOP
    or code == C.AKEYCODE_MEDIA_NEXT
    or code == C.AKEYCODE_MEDIA_PREVIOUS
    or code == C.AKEYCODE_MEDIA_REWIND
    or code == C.AKEYCODE_MEDIA_FAST_FORWARD then
        return 0 -- event not consumed
    elseif code == C.AKEYCODE_MUTE
    or code == C.AKEYCODE_VOLUME_MUTE then
        if android.getVolumeKeysIgnored() then
            return 0
        else
            return 1
        end
    end

    if action == C.AKEY_EVENT_ACTION_DOWN then
        genEmuEvent(C.EV_KEY, code, 1)
    elseif action == C.AKEY_EVENT_ACTION_UP then
        genEmuEvent(C.EV_KEY, code, 0)
    end
    return 1 -- event consumed
end

local function commandHandler(code, value)
    genEmuEvent(C.EV_MSC, code, value)
end

function input.waitForEvent(usecs)
    local timeout = math.ceil(usecs and usecs/1000 or -1)
    while true do
        -- check for queued events
        if #inputQueue > 0 then
            -- return oldest FIFO element
            return table.remove(inputQueue, 1)
        end
        local events = ffi.new("int[1]")<commit_msg>android: ignore back button events<commit_after>local ffi = require("ffi")
local bit = require("bit")
local C = ffi.C

local android = require("android")
local dummy = require("ffi/linux_input_h")

local input = {
-- to trigger refreshes for certain Android framework events:
    device = nil,
}

function input.open()
end

local inputQueue = {}

local ev_time = ffi.new("struct timeval")
local function genEmuEvent(evtype, code, value)
    C.gettimeofday(ev_time, nil)
    local ev = {
        type = tonumber(evtype),
        code = tonumber(code),
        value = tonumber(value),
        time = { sec = tonumber(ev_time.tv_sec), usec = tonumber(ev_time.tv_usec) }
    }
    table.insert(inputQueue, ev)
end

local function genTouchDownEvent(event, id)
    local x = android.lib.AMotionEvent_getX(event, id)
    local y = android.lib.AMotionEvent_getY(event, id)
    genEmuEvent(C.EV_ABS, C.ABS_MT_SLOT, id)
    genEmuEvent(C.EV_ABS, C.ABS_MT_TRACKING_ID, id)
    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)
end

local function genTouchUpEvent(event, id)
    local x = android.lib.AMotionEvent_getX(event, id)
    local y = android.lib.AMotionEvent_getY(event, id)
    genEmuEvent(C.EV_ABS, C.ABS_MT_SLOT, id)
    genEmuEvent(C.EV_ABS, C.ABS_MT_TRACKING_ID, -1)
    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)
end

local function genTouchMoveEvent(event, id, index)
    local x = android.lib.AMotionEvent_getX(event, index)
    local y = android.lib.AMotionEvent_getY(event, index)
    genEmuEvent(C.EV_ABS, C.ABS_MT_SLOT, id)
    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)
end

local is_in_touch = false
local function motionEventHandler(motion_event)
    if android.isTouchscreenIgnored() then
        return
    end
    local action = android.lib.AMotionEvent_getAction(motion_event)
    local pointer_count = android.lib.AMotionEvent_getPointerCount(motion_event)
    local pointer_index = bit.rshift(
            bit.band(action, C.AMOTION_EVENT_ACTION_POINTER_INDEX_MASK),
            C.AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT)
    local id = android.lib.AMotionEvent_getPointerId(motion_event, pointer_index)
    local flags = bit.band(action, C.AMOTION_EVENT_ACTION_MASK)
    if flags == C.AMOTION_EVENT_ACTION_DOWN then
        is_in_touch = true
        genTouchDownEvent(motion_event, id)
    elseif flags == C.AMOTION_EVENT_ACTION_POINTER_DOWN then
        is_in_touch = true
        genTouchDownEvent(motion_event, id)
    elseif flags == C.AMOTION_EVENT_ACTION_UP then
        is_in_touch = false
        genTouchUpEvent(motion_event, id)
    elseif flags == C.AMOTION_EVENT_ACTION_POINTER_UP then
        is_in_touch = false
        genTouchUpEvent(motion_event, id)
    elseif flags == C.AMOTION_EVENT_ACTION_MOVE then
        if is_in_touch then
            for index = 0, pointer_count - 1 do
                id = android.lib.AMotionEvent_getPointerId(motion_event, index)
                genTouchMoveEvent(motion_event, id, index)
            end
        end
    end
end

local function keyEventHandler(key_event)
    local code = android.lib.AKeyEvent_getKeyCode(key_event)
    local action = android.lib.AKeyEvent_getAction(key_event)
    if code == C.AKEYCODE_VOLUME_UP
    or code == C.AKEYCODE_VOLUME_DOWN then
        if android.getVolumeKeysIgnored() then
            return 0 -- event not consumed
        end
    elseif code == C.AKEYCODE_MEDIA_PLAY_PAUSE
    or code == C.AKEYCODE_MEDIA_STOP
    or code == C.AKEYCODE_MEDIA_NEXT
    or code == C.AKEYCODE_MEDIA_PREVIOUS
    or code == C.AKEYCODE_MEDIA_REWIND
    or code == C.AKEYCODE_MEDIA_FAST_FORWARD then
        return 0 -- event not consumed
    elseif code == C.AKEYCODE_MUTE
    or code == C.AKEYCODE_VOLUME_MUTE then
        if android.getVolumeKeysIgnored() then
            return 0
        else
            return 1
        end
    end
    if code == C.AKEYCODE_BACK and android.isBackButtonIgnored() then
        return 1
    end
    if action == C.AKEY_EVENT_ACTION_DOWN then
        genEmuEvent(C.EV_KEY, code, 1)
    elseif action == C.AKEY_EVENT_ACTION_UP then
        genEmuEvent(C.EV_KEY, code, 0)
    end
    return 1 -- event consumed
end

local function commandHandler(code, value)
    genEmuEvent(C.EV_MSC, code, value)
end

function input.waitForEvent(usecs)
    local timeout = math.ceil(usecs and usecs/1000 or -1)
    while true do
        -- check for queued events
        if #inputQueue > 0 then
            -- return oldest FIFO element
            return table.remove(inputQueue, 1)
        end
        local events = ffi.new("int[1]")
{'fraction_non_alphanumeric': 0.08527780684315162, 'fraction_numerical': 0.004499319870252171, 'mean_word_length': 3.1665213600697473, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5485066', 'n_tokens_mistral': 3471, 'n_tokens_neox': 3428, 'n_words': 716}