2023-08-01 18:06:35 +02:00
-- SPDX-License-Identifier: GPL-3.0-or-later
2023-06-26 00:18:50 +02:00
2023-09-11 05:01:40 +02:00
---@nodoc TODO: add enum, alias, and type capabilities to ldoc_gen
---@enum MouseButton
local buttons = {
--- Left
[ 1 ] = 0x110 ,
--- Right
[ 2 ] = 0x111 ,
--- Middle
[ 3 ] = 0x112 ,
--- Side
[ 4 ] = 0x113 ,
--- Extra
[ 5 ] = 0x114 ,
--- Forward
[ 6 ] = 0x115 ,
--- Back
[ 7 ] = 0x116 ,
left = 0x110 ,
right = 0x111 ,
middle = 0x112 ,
side = 0x113 ,
extra = 0x114 ,
forward = 0x115 ,
back = 0x116 ,
}
2023-09-28 17:17:28 +02:00
---@class XkbConfig
---@field rules string?
---@field model string?
---@field layout string?
---@field variant string?
---@field options string?
2023-09-10 03:52:52 +02:00
---Input management.
---
---This module provides utilities to set keybinds.
2023-07-22 04:02:02 +02:00
---@class InputModule
local input_module = {
2023-09-10 03:52:52 +02:00
--- A table with every key provided by xkbcommon.
2023-06-22 00:36:51 +02:00
keys = require ( " keys " ) ,
2023-09-11 04:54:58 +02:00
--- A table with mouse button codes. You can use indexes (1, 2, and 3 are left, right, and middle)
--- or keyed values (buttons.left, buttons.right, etc.).
2023-09-11 05:01:40 +02:00
buttons = buttons ,
2023-09-29 01:46:08 +02:00
--- Libinput settings.
libinput = require ( " input.libinput " ) ,
2023-06-22 00:36:51 +02:00
}
2023-06-15 19:42:34 +02:00
2023-07-02 17:26:07 +02:00
---Set a keybind. If called with an already existing keybind, it gets replaced.
2023-07-11 18:59:38 +02:00
---
2023-09-10 03:41:21 +02:00
---You must provide three arguments:
---
--- - `modifiers`: An array of `Modifier`s. If you don't want any, provide an empty table.
--- - `key`: The key that will trigger `action`. You can provide three types of key:
--- - Something from the `Keys` table in `input.keys`, which lists every xkbcommon key. The naming pattern is the xkbcommon key without the `KEY_` prefix, unless that would make it start with a number or the reserved lua keyword `function`, in which case the `KEY_` prefix is included.
--- - A single character representing your key. This can be something like "g", "$", "~", "1", and so on.
--- - A string of the key's name. This is the name of the xkbcommon key without the `KEY_` prefix.
--- - `action`: The function that will be run when the keybind is pressed.
---
---It is important to note that `"a"` is different than `"A"`. Similarly, `keys.a` is different than `keys.A`.
---Usually, it's best to use the non-modified key to prevent confusion and unintended behavior.
---
---```lua
---input.keybind({ "Shift" }, "a", function() end) -- This is preferred
---input.keybind({ "Shift" }, "A", function() end) -- over this
---
--- -- And in fact, this keybind won't work at all because it expects no modifiers,
--- -- but you can't get "A" without using `Shift`.
---input.keybind({}, "A", function() end)
---```
---
2023-07-18 22:12:23 +02:00
---### Example
2023-07-11 18:59:38 +02:00
---
---```lua
2023-09-08 03:48:41 +02:00
--- -- Set `Super + Return` to open Alacritty
2023-07-11 18:59:38 +02:00
---input.keybind({ "Super" }, input.keys.Return, function()
--- process.spawn("Alacritty")
---end)
---```
2023-09-10 02:27:05 +02:00
---@param key Keys|string The key for the keybind.
2023-07-02 17:26:07 +02:00
---@param modifiers (Modifier)[] Which modifiers need to be pressed for the keybind to trigger.
2023-07-11 18:59:38 +02:00
---@param action fun() What to do.
2023-07-22 04:02:02 +02:00
function input_module . keybind ( modifiers , key , action )
2023-06-15 19:42:34 +02:00
table.insert ( CallbackTable , action )
2023-09-10 02:27:05 +02:00
local k = { }
if type ( key ) == " string " then
k.String = key
else
k.Int = key
end
2023-06-15 19:42:34 +02:00
SendMsg ( {
SetKeybind = {
modifiers = modifiers ,
2023-09-10 02:27:05 +02:00
key = k ,
2023-06-15 19:42:34 +02:00
callback_id = # CallbackTable ,
} ,
} )
end
2023-09-11 04:54:58 +02:00
---Set a mousebind. If called with an already existing mousebind, it gets replaced.
---
---The mousebind can happen either on button press or release, so you must specify
---which edge you desire.
---
---@param modifiers (Modifier)[] The modifiers that need to be held for the mousebind to trigger.
---@param button MouseButton The button that needs to be pressed or released.
---@param edge "Press"|"Release" Whether or not to trigger `action` on button press or release.
---@param action fun() The function to run.
function input_module . mousebind ( modifiers , button , edge , action )
table.insert ( CallbackTable , action )
SendMsg ( {
SetMousebind = {
modifiers = modifiers ,
button = button ,
edge = edge ,
callback_id = # CallbackTable ,
} ,
} )
end
2023-09-29 01:46:08 +02:00
---Set the xkbconfig for your keyboard.
2023-09-28 17:17:28 +02:00
---
---Fields not present will be set to their default values.
---
2023-09-29 01:46:08 +02:00
---Read `xkeyboard-config(7)` for more information.
---
2023-09-28 17:17:28 +02:00
---### Example
---```lua
---input.set_xkb_config({
--- layout = "us,fr,ge",
--- options = "ctrl:swapcaps,caps:shift"
---})
---```
---
---@param xkb_config XkbConfig
function input_module . set_xkb_config ( xkb_config )
SendMsg ( {
SetXkbConfig = xkb_config ,
} )
end
2023-07-22 04:02:02 +02:00
return input_module