<commit_before>-- Input handling library. This is meant to simplify the handling of keyboard
-- and joystick events.
-- Note that it doesn't really support joystick axes except as d-pads - that is
-- to say, pushing an axis all the way will register as (say) "up" and anything
-- else is "center".
input = {}

-- event bindings
local bindings = {}

-- timers used for key-repeat settings
local timers = {}

local function start_timer(name)
    if timers[name] and not timers[name].t then
        timers[name].t = timers[name].delay
    end
end

local function stop_timer(name)
    if timers[name] then
        timers[name].t = nil
    end
end

-- Dispatch an event with the given names and arguments.
-- Returns true if an event handler existed, false otherwise
local function event(name, ...)
    if name:sub(1,1) == "!" then
        stop_timer(name:sub(2))
    else
        start_timer(name)
    end
    if bindings[name] then
        bindings[name](...)
        return true
    end
    return false
end

function input.bind(from, to, up)
    if type(to) == "string" then
        bindings[from] = function(...)
            return event(to, ...)
        end
        bindings["!"..from] = function(...)
            return event(up or "!"..to, ...)
        end
    else
        bindings[from] = to
        bindings["!" .. from] = up
    end
end

function input.unbind(from, rest, ...)
    bindings[from] = nil
    if rest then
        return input.unbind(rest, ...)
    end
end

function input.unbindall()
    bindings = {}
end

-- enable key repeat for the given event
-- delay the amount of time to wait until it starts repeating, period is how
-- frequently to emit the repeated events.
-- eg: love.set_repeat("key_a", 1.0, 0.5)
function input.setRepeat(event, delay, period)
    if delay then
        local t
        timers[event] = { delay = delay, period = period }
    else
        timers[event] = nil
        timers["!"..event] = nil
    end
end

-- love2d callback function for keyboard events. Pressing 'a' will trigger event
-- key_a() or, if that doesn't exist, key_any('a')
function love.keypressed(name, num)
    return event("key_"..name)
    or event("key_any", name)
end

function love.keyreleased(name, num)
    return event("!key_"..name)
    or event("!key_any")
end

-- same as above, but for joysticks. Joystick events are parameterized by both
-- stick and button; button 3 on stick 0 shows up as joy_0_button_3.
function love.joystickpressed(joystick, button)
    local name = "joy_"..joystick.."_button_"..button
    
    start_timer(name)
    return event(name, joystick, "button", button)
    or event("joy_" .. joystick .. "_button_any", joystick, "button", button)
    or event("joy_any_button_" .. button, joystick, "button", button)
    or event("joy_any_button_any", joystick, "button", button)
    or event("joy_any", joystick, "button", button, joystick, "button", button)
end

function love.joystickreleased(joystick, button)
    local name = "joy_"..joystick.."_button_"..button
    
    stop_timer(name)
end

-- joystick axis handling. At initialization, read the state of every axis on<commit_msg>Clean up joystick button handling<commit_after>-- Input handling library. This is meant to simplify the handling of keyboard
-- and joystick events.
-- Note that it doesn't really support joystick axes except as d-pads - that is
-- to say, pushing an axis all the way will register as (say) "up" and anything
-- else is "center".
input = {}

-- event bindings
local bindings = {}

-- timers used for key-repeat settings
local timers = {}

local function start_timer(name)
    if timers[name] and not timers[name].t then
        timers[name].t = timers[name].delay
    end
end

local function stop_timer(name)
    if timers[name] then
        timers[name].t = nil
    end
end

-- Dispatch an event with the given names and arguments.
-- Returns true if an event handler existed, false otherwise
local function event(name, ...)
    if name:sub(1,1) == "!" then
        stop_timer(name:sub(2))
    else
        start_timer(name)
    end
    if bindings[name] then
        bindings[name](...)
        return true
    end
    return false
end

function input.bind(from, to, up)
    if type(to) == "string" then
        bindings[from] = function(...)
            return event(to, ...)
        end
        bindings["!"..from] = function(...)
            return event(up or "!"..to, ...)
        end
    else
        bindings[from] = to
        bindings["!" .. from] = up
    end
end

function input.unbind(from, rest, ...)
    bindings[from] = nil
    if rest then
        return input.unbind(rest, ...)
    end
end

function input.unbindall()
    bindings = {}
end

-- enable key repeat for the given event
-- delay the amount of time to wait until it starts repeating, period is how
-- frequently to emit the repeated events.
-- eg: love.set_repeat("key_a", 1.0, 0.5)
function input.setRepeat(event, delay, period)
    if delay then
        local t
        timers[event] = { delay = delay, period = period }
    else
        timers[event] = nil
        timers["!"..event] = nil
    end
end

-- love2d callback function for keyboard events. Pressing 'a' will trigger event
-- key_a() or, if that doesn't exist, key_any('a')
function love.keypressed(name, num)
    return event("key_"..name)
    or event("key_any", name)
end

function love.keyreleased(name, num)
    return event("!key_"..name)
    or event("!key_any")
end

-- same as above, but for joysticks. Joystick events are parameterized by both
-- stick and button; button 3 on stick 0 shows up as joy_0_button_3.
function love.joystickpressed(j, b)
    return event("joy_"..j.."_button_"..b, j, "button", b)
    or event("joy_"..j.."_button_any", j, "button", b)
    or event("joy_any_button_"..b, j, "button", b)
    or event("joy_any_button_any", j, "button", b)
    or event("joy_any", j, "button", b)
end

function love.joystickreleased(joystick, button)
    return event("!joy_"..j.."_button_"..b, j, "button", b)
    or event("!joy_"..j.."_button_any", j, "button", b)
    or event("!joy_any_button_"..b, j, "button", b)
    or event("!joy_any_button_any", j, "button", b)
    or event("!joy_any", j, "button", b)
end

-- joystick axis handling. At initialization, read the state of every axis on
{'fraction_non_alphanumeric': 0.12670906200317966, 'fraction_numerical': 0.0038155802861685214, 'mean_word_length': 2.8058076225045374, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1000758', 'n_tokens_mistral': 2034, 'n_tokens_neox': 1942, 'n_words': 682}