dotfiles/awesome/keys.lua

699 lines
20 KiB
Lua
Raw Normal View History

2022-07-19 19:45:11 +02:00
local awful = require("awful")
local gears = require("gears")
local naughty = require("naughty")
local beautiful = require("beautiful")
2023-06-05 16:40:17 +02:00
local volume_widget = require("components.widgets.volume-widget.volume")
2022-07-19 19:45:11 +02:00
local dpi = beautiful.xresources.apply_dpi
2022-07-27 01:43:14 +02:00
2022-07-19 19:45:11 +02:00
local modkey = "Mod4"
-- define module table
local keys = {}
2023-05-26 15:05:03 +02:00
-- AwesomeWM Vim Tmux Navigator
local function get_first_nonempty_tag()
local screen = awful.screen.focused()
local tags = screen.tags
for _, t in ipairs(tags) do
2023-07-17 16:52:50 +02:00
if #t:clients() == 0 then
return t
end
end
end
2023-05-26 15:05:03 +02:00
local focus_bydirection = function(direction)
awful.client.focus.global_bydirection(direction)
if client.focus then
-- focus on the client
client.focus:raise()
end
-- BUG: focus across screens is wonky when there are no clients on the destination screen
-- https://github.com/awesomeWM/awesome/issues/3638
-- Workaround: manually unfocus client after moving focus to an empty screen
local is_empty_destination = #awful.screen.focused().clients < 1
if is_empty_destination then
-- manually unfocus the current focused client
client.focus = nil
end
end
require("awesomewm-vim-tmux-navigator") {
2023-09-06 16:28:37 +02:00
up = {"Up", "k"},
down = {"Down", "j"},
left = {"Left", "h"},
right = {"Right", "l"},
mod = "Mod4",
mod_keysym = "Super_L",
experimental = true,
focus = focus_bydirection
2023-05-26 15:05:03 +02:00
}
2022-07-19 19:45:11 +02:00
-- ===================================================================
-- Movement Functions (Called by some keybinds)
-- ===================================================================
local function get_screen(s)
return s and screen[s]
end
2022-07-19 19:45:11 +02:00
-- Move given client to given direction
local function move_client(sel, dir)
sel = sel or awful.client.focus
if sel then
-- move focus
awful.client.focus.global_bydirection(dir, sel)
local c = client.focus
-- swapping inside a screen
if get_screen(sel.screen) == get_screen(c.screen) and sel ~= c then
c:swap(sel)
client.focus = sel
sel:raise()
2023-09-06 16:28:37 +02:00
-- swapping to an empty screen
elseif sel == c then
sel:move_to_screen(awful.screen.focused())
-- swapping to a nonempty screen
elseif get_screen(sel.screen) ~= get_screen(c.screen) and sel ~= c then
sel:move_to_screen(c.screen)
client.focus = sel
sel:raise()
end
end
2022-07-19 19:45:11 +02:00
end
-- Resize client in given direction
local floating_resize_amount = dpi(20)
local tiling_resize_factor = 0.01
2022-07-19 19:45:11 +02:00
local function resize_client(c, direction)
2022-07-23 19:31:38 +02:00
if awful.layout.get(mouse.screen) == awful.layout.suit.floating or (c and c.floating) then
if direction == "up" then
c:relative_move(0, 0, 0, -floating_resize_amount)
elseif direction == "down" then
c:relative_move(0, 0, 0, floating_resize_amount)
elseif direction == "left" then
c:relative_move(0, 0, -floating_resize_amount, 0)
elseif direction == "right" then
c:relative_move(0, 0, floating_resize_amount, 0)
end
else
if direction == "up" then
awful.client.incwfact(-tiling_resize_factor)
elseif direction == "down" then
awful.client.incwfact(tiling_resize_factor)
elseif direction == "left" then
awful.tag.incmwfact(-tiling_resize_factor)
elseif direction == "right" then
awful.tag.incmwfact(tiling_resize_factor)
end
end
2022-07-19 19:45:11 +02:00
end
-- ===================================================================
-- Mouse bindings
-- ===================================================================
-- Mouse buttons on the desktop
keys.desktopbuttons = gears.table.join(
2022-07-23 19:31:38 +02:00
-- left click on desktop to hide notification
awful.button({}, 1,
function ()
naughty.destroy_all_notifications()
end
),
awful.button({}, 7, function() volume_widget:inc(5) end),
awful.button({}, 6, function() volume_widget:dec(5) end)
2022-07-19 19:45:11 +02:00
)
-- Mouse buttons on the client
keys.clientbuttons = gears.table.join(
2022-07-23 19:31:38 +02:00
-- Raise client
awful.button({}, 1,
function(c)
client.focus = c
c:raise()
end
),
-- Move and Resize Client
awful.button({modkey}, 1, awful.mouse.client.move),
awful.button({modkey}, 3, awful.mouse.client.resize),
awful.button({}, 7, function() volume_widget:inc(5) end),
awful.button({}, 6, function() volume_widget:dec(5) end)
2022-07-19 19:45:11 +02:00
)
-- ===================================================================
-- Desktop Key bindings
-- ===================================================================
keys.globalkeys = gears.table.join(
2022-07-23 19:31:38 +02:00
-- =========================================
-- SPAWN APPLICATION KEY BINDINGS
-- =========================================
-- Spawn terminal
awful.key({modkey}, "Return",
function()
local c = client.focus
if c and c.class == "Alacritty" then
2022-08-06 17:21:13 +02:00
awful.spawn(string.format(os.getenv("XDG_CONFIG_HOME") .. "/awesome/scripts/launch_alacritty.sh %d", c.pid))
else
2023-06-05 16:40:17 +02:00
awful.spawn('bash -c "WINIT_X11_SCALE_FACTOR=1 alacritty"')
end
2022-07-23 19:31:38 +02:00
end,
{description = "open a terminal", group = "launcher"}
),
-- Spawn browser
awful.key({modkey}, "b",
function()
2023-03-23 09:46:18 +01:00
awful.spawn("firefox")
2022-07-23 19:31:38 +02:00
end,
{description = "Open Firefox", group = "launcher"}
),
-- Lock the screen
awful.key({"Control", "Mod1"}, "l",
2023-09-06 16:28:37 +02:00
function()
awful.spawn('bash -c "i3lockr --blur 25"')
end,
{description = "Open lock", group = "launcher"}
),
2023-08-27 15:17:35 +02:00
-- Generate TOTP
awful.key({modkey}, "y",
2022-11-19 15:07:34 +01:00
function()
2023-08-27 15:17:35 +02:00
awful.spawn.with_shell(os.getenv("XDG_CONFIG_HOME") .. "/rofi/rofi-ykman.sh")
2022-11-19 15:07:34 +01:00
end,
2023-08-27 15:17:35 +02:00
{description = "Generate TOTP", group = "launcher"}
2022-11-19 15:07:34 +01:00
),
-- Spawn file manager
awful.key({modkey}, "a",
function()
awful.spawn("nautilus")
end,
{description = "Open Nautilus", group = "launcher"}
),
2022-07-23 19:31:38 +02:00
-- launch rofi
2022-07-27 13:22:39 +02:00
awful.key({modkey}, "XF86Launch5",
function()
2022-08-06 21:02:49 +02:00
awful.spawn(os.getenv("XDG_CONFIG_HOME") .. "/awesome/scripts/toggle_rofi.sh")
2022-07-27 13:22:39 +02:00
end,
{description = "application launcher", group = "launcher"}
),
-- launch rofi
awful.key({modkey}, "d",
2022-07-23 19:31:38 +02:00
function()
2022-08-06 17:21:13 +02:00
awful.spawn(os.getenv("XDG_CONFIG_HOME") .. "/awesome/scripts/toggle_rofi.sh")
2022-07-23 19:31:38 +02:00
end,
{description = "application launcher", group = "launcher"}
),
-- launch clipman
awful.key({modkey}, "c",
function()
awful.spawn.with_shell('CM_LAUNCHER=rofi-script rofi -modi "clipmenu:/usr/bin/clipmenu" -show clipmenu')
2022-07-23 19:31:38 +02:00
end,
{description = "Clipboard manager", group = "launcher"}
),
2022-11-22 16:18:29 +01:00
-- Go to sleep
awful.key({"Control", "Mod2", "Mod4"}, "q",
function()
awful.spawn.with_shell('systemctl suspend')
end,
{description = "Clipboard manager", group = "launcher"}
),
2022-07-23 19:31:38 +02:00
-- Quake
awful.key({ }, "F9", function () awful.screen.focused().quake:toggle() end),
-- =========================================
-- FUNCTION KEYS
-- =========================================
-- Brightness
awful.key({}, "XF86MonBrightnessUp",
function()
awful.spawn("xbacklight -inc 10", false)
end,
{description = "+10%", group = "hotkeys"}
),
awful.key({}, "XF86MonBrightnessDown",
function()
awful.spawn("xbacklight -dec 10", false)
end,
{description = "-10%", group = "hotkeys"}
),
2023-05-08 13:35:23 +02:00
-- Brightness
awful.key({ }, "XF86MonBrightnessDown", function ()
2023-05-21 00:35:23 +02:00
awful.spawn("xbacklight -dec 15") end),
2023-05-08 13:35:23 +02:00
awful.key({ }, "XF86MonBrightnessUp", function ()
2023-05-21 00:35:23 +02:00
awful.spawn("xbacklight -inc 15") end),
2023-05-08 13:35:23 +02:00
2022-07-23 19:31:38 +02:00
-- ALSA volume control
awful.key({}, "XF86AudioRaiseVolume",
2023-09-06 16:28:37 +02:00
function()
volume_widget:inc(5)
end,
{description = "volume up", group = "hotkeys"}
2022-07-23 19:31:38 +02:00
),
awful.key({}, "XF86AudioLowerVolume",
2023-09-06 16:28:37 +02:00
function()
volume_widget:dec(5)
end,
{description = "volume down", group = "hotkeys"}
2022-07-23 19:31:38 +02:00
),
awful.key({}, "XF86AudioMute",
2023-09-06 16:28:37 +02:00
function()
volume_widget:toggle()
end,
{description = "toggle mute", group = "hotkeys"}
2022-07-23 19:31:38 +02:00
),
awful.key({}, "XF86AudioNext",
function()
2022-08-31 15:27:22 +02:00
awful.spawn("playerctl next", false)
2022-07-23 19:31:38 +02:00
end,
{description = "next music", group = "hotkeys"}
),
awful.key({}, "XF86AudioPrev",
function()
2022-08-31 15:27:22 +02:00
awful.spawn("playerctl previous", false)
2022-07-23 19:31:38 +02:00
end,
{description = "previous music", group = "hotkeys"}
),
awful.key({}, "XF86AudioPlay",
function()
2022-08-31 15:27:22 +02:00
awful.spawn("playerctl play-pause", false)
2022-07-23 19:31:38 +02:00
end,
{description = "play/pause music", group = "hotkeys"}
),
2023-05-08 13:35:23 +02:00
awful.key({modkey}, "XF86AudioMute",
function()
awful.spawn("playerctl play-pause", false)
end,
{description = "play/pause music", group = "hotkeys"}
),
awful.key({modkey}, "XF86AudioRaiseVolume",
2023-09-06 16:28:37 +02:00
function()
2023-05-08 13:35:23 +02:00
awful.spawn("playerctl next", false)
2023-09-06 16:28:37 +02:00
end,
{description = "volume up", group = "hotkeys"}
2023-05-08 13:35:23 +02:00
),
awful.key({modkey}, "XF86AudioLowerVolume",
2023-09-06 16:28:37 +02:00
function()
2023-05-08 13:35:23 +02:00
awful.spawn("playerctl previous", false)
2023-09-06 16:28:37 +02:00
end,
{description = "volume down", group = "hotkeys"}
2023-05-08 13:35:23 +02:00
),
2022-07-23 19:31:38 +02:00
-- Screenshot on prtscn using scrot
awful.key({}, "Print",
function()
2023-06-05 16:40:17 +02:00
awful.spawn("scrot -e 'mv $f ~/Pictures/Screenshots/ 2>/dev/null'", false)
2022-07-23 19:31:38 +02:00
end
),
-- =========================================
-- RELOAD / QUIT AWESOME
-- =========================================
-- Reload Awesome
awful.key({modkey, "Shift"}, "r",
awesome.restart,
{description = "reload awesome", group = "awesome"}
),
-- Quit Awesome
awful.key({modkey}, "Escape",
function()
-- emit signal to show the exit screen
2023-05-21 00:35:23 +02:00
awful.spawn("rofi -show power-menu -modi power-menu:" .. os.getenv("XDG_CONFIG_HOME") .. "/awesome/scripts/rofi-power-menu")
2022-07-23 19:31:38 +02:00
end,
{description = "toggle exit screen", group = "hotkeys"}
),
awful.key({}, "XF86PowerOff",
function()
-- emit signal to show the exit screen
2023-05-21 00:35:23 +02:00
awful.spawn("rofi -show power-menu -modi power-menu:~/.config/awesome/scripts/rofi-power-menu")
2022-07-23 19:31:38 +02:00
end,
{description = "toggle exit screen", group = "hotkeys"}
),
2022-08-03 12:51:26 +02:00
awful.key({}, "XF86Calculator",
function()
2023-05-21 00:35:23 +02:00
awful.spawn("rofi -show calc -modi calc -no-show-match -no-sort -calc-command \"echo -n '{result}' | xsel --clipboard\"")
2022-08-03 12:51:26 +02:00
end,
{description = "Launch the calculator", group = "hotkeys"}
),
2022-07-23 19:31:38 +02:00
-- Focus client by index (cycle through clients)
awful.key({modkey}, "Tab",
function()
2023-05-25 17:47:26 +02:00
local screen = awful.screen.focused()
local tags = screen.tags
local current_tag = screen.selected_tag
local used_tags = {}
for _, t in ipairs(tags) do
if t == current_tag or #t:clients() > 0 then
table.insert(used_tags, t)
end
end
local found = false
for _, t in ipairs(used_tags) do
if found then
t:view_only()
return
elseif t == current_tag then
found = true
end
end
used_tags[1]:view_only()
2022-07-23 19:31:38 +02:00
end,
2023-05-25 17:47:26 +02:00
{description = "Switch to next tag", group = "client"}
2022-07-23 19:31:38 +02:00
),
awful.key({modkey, "Shift"}, "Tab",
function()
2023-05-25 17:47:26 +02:00
local tags = root.tags()
local current_tag = awful.screen.focused().selected_tag
local used_tags = {}
for _, t in ipairs(tags) do
if t == current_tag or #t:clients() > 0 then
table.insert(used_tags, t)
end
end
local found = false
for _, t in ipairs(used_tags) do
if found then
awful.screen.focus(t.screen.index)
t:view_only()
return
elseif t == current_tag then
found = true
end
end
used_tags[1]:view_only()
end,
{description = "Switch to next tag", group = "client"}
),
2023-08-25 11:13:46 +02:00
awful.key({"Mod1"}, "Tab", -- Alt-Tab Cycle through clients
function()
local clients = {}
local found = false
local function focus_client(c)
awful.screen.focus(c.screen.index)
c.first_tag:view_only()
client.focus = c
c:raise()
end
for _, t in ipairs(root.tags()) do
for i, c in ipairs(t:clients()) do
if found then
focus_client(c)
return
elseif c == client.focus then
found = true
end
table.insert(clients, c)
end
end
focus_client(clients[1])
end,
{description = "Switch to next tag", group = "client"}
),
2023-05-25 17:47:26 +02:00
awful.key({modkey}, "t",
function()
local first_empty = get_first_nonempty_tag()
if first_empty ~= nil then
first_empty:view_only()
2023-05-25 17:47:26 +02:00
end
end,
{description = "Switch to next tag", group = "client"}
),
awful.key({modkey, "Shift"}, "t",
function()
local first_empty = get_first_nonempty_tag()
2023-05-25 17:47:26 +02:00
if first_empty ~= nil then
client.focus:move_to_tag(first_empty)
2023-05-25 17:47:26 +02:00
first_empty:view_only()
end
2022-07-23 19:31:38 +02:00
end,
2023-05-25 17:47:26 +02:00
{description = "Switch to next tag", group = "client"}
2022-07-23 19:31:38 +02:00
),
-- =========================================
-- SCREEN FOCUSING
-- =========================================
-- Focus screen by index (cycle through screens)
awful.key({modkey}, "s",
function()
2023-05-25 21:25:49 +02:00
awful.screen.focus_relative(-1)
2022-07-23 19:31:38 +02:00
end
),
-- =========================================
-- CLIENT RESIZING
-- =========================================
awful.key({modkey, "Control"}, "Down",
2023-05-20 23:04:33 +02:00
function(_)
2022-07-23 19:31:38 +02:00
resize_client(client.focus, "down")
end
),
awful.key({modkey, "Control"}, "Up",
2023-05-20 23:04:33 +02:00
function(_)
2022-07-23 19:31:38 +02:00
resize_client(client.focus, "up")
end
),
awful.key({modkey, "Control"}, "Left",
2023-05-20 23:04:33 +02:00
function(_)
2022-07-23 19:31:38 +02:00
resize_client(client.focus, "left")
end
),
awful.key({modkey, "Control"}, "Right",
2023-05-20 23:04:33 +02:00
function(_)
2022-07-23 19:31:38 +02:00
resize_client(client.focus, "right")
end
),
awful.key({modkey, "Control"}, "j",
2023-05-20 23:04:33 +02:00
function(_)
2022-07-23 19:31:38 +02:00
resize_client(client.focus, "down")
end
),
awful.key({ modkey, "Control" }, "k",
2023-05-20 23:04:33 +02:00
function(_)
2022-07-23 19:31:38 +02:00
resize_client(client.focus, "up")
end
),
awful.key({modkey, "Control"}, "h",
2023-05-20 23:04:33 +02:00
function(_)
2022-07-23 19:31:38 +02:00
resize_client(client.focus, "left")
end
),
awful.key({modkey, "Control"}, "l",
2023-05-20 23:04:33 +02:00
function(_)
2022-07-23 19:31:38 +02:00
resize_client(client.focus, "right")
end
),
-- =========================================
-- GAP CONTROL
-- =========================================
-- Gap control
-- awful.key({modkey, :Shift"}, "minus",
-- function()
-- awful.tag.incgap(5, nil)
-- end,
-- {description = "increment gaps size for the current tag", group = "gaps"}
-- ),
-- awful.key({modkey}, "minus",
-- function()
-- awful.tag.incgap(-5, nil)
-- end,
-- {description = "decrement gap size for the current tag", group = "gaps"}
-- ),
awful.key({ modkey }, "=",
function ()
awful.tag.incncol( 1, nil, true)
2022-07-23 19:31:38 +02:00
end,
{description = "increase the number of columns", group = "layout"}
2022-07-23 19:31:38 +02:00
),
awful.key({ modkey }, "minus",
function ()
awful.tag.incncol(-1, nil, true)
2022-07-23 19:31:38 +02:00
end,
{description = "decrease the number of columns", group = "layout"}
2022-07-23 19:31:38 +02:00
),
-- =========================================
-- LAYOUT SELECTION
-- =========================================
-- select next layout
awful.key({modkey}, "space",
function()
awful.layout.inc(1)
end,
{description = "select next", group = "layout"}
),
-- select previous layout
awful.key({modkey, "Shift"}, "space",
function()
awful.layout.inc(-1)
end,
{description = "select previous", group = "layout"}
),
-- =========================================
-- CLIENT MINIMIZATION
-- =========================================
-- restore minimized client
awful.key({modkey, "Shift"}, "n",
function()
local c = awful.client.restore()
-- Focus restored client
if c then
client.focus = c
c:raise()
end
end,
{description = "restore minimized", group = "client"}
)
2022-07-19 19:45:11 +02:00
)
-- ===================================================================
-- Client Key bindings
-- ===================================================================
keys.clientkeys = gears.table.join(
2022-07-23 19:31:38 +02:00
-- Move to edge or swap by direction
awful.key({modkey, "Shift"}, "Down",
function(c)
move_client(c, "down")
end
),
awful.key({modkey, "Shift"}, "Up",
function(c)
move_client(c, "up")
end
),
awful.key({modkey, "Shift"}, "Left",
function(c)
move_client(c, "left")
end
),
awful.key({modkey, "Shift"}, "Right",
function(c)
move_client(c, "right")
end
),
awful.key({modkey, "Shift"}, "j",
function(c)
move_client(c, "down")
end
),
awful.key({modkey, "Shift"}, "k",
function(c)
move_client(c, "up")
end
),
awful.key({modkey, "Shift"}, "h",
function(c)
move_client(c, "left")
end
),
awful.key({modkey, "Shift"}, "l",
function(c)
move_client(c, "right")
end
),
-- close client
awful.key({modkey}, "q",
function(c)
c:kill()
2023-05-20 23:04:33 +02:00
for _, i in ipairs(client.get()) do
i.minimized = false
2022-07-31 13:04:34 +02:00
end
2022-07-23 19:31:38 +02:00
end,
{description = "close", group = "client"}
),
-- float client
awful.key({modkey}, "f",
function(c)
2023-09-06 16:28:37 +02:00
awful.client.floating.toggle(c)
2022-07-23 19:31:38 +02:00
end,
{description = "float", group = "client"}
),
-- Minimize
awful.key({modkey}, "n",
function(c)
c.minimized = true
end,
{description = "minimize", group = "client"}
),
-- Maximize
awful.key({modkey}, "m",
function(c)
c.maximized = not c.maximized
c:raise()
end,
{description = "(un)maximize", group = "client"}
)
2022-07-19 19:45:11 +02:00
)
local function register_tag(tag)
2022-07-23 19:31:38 +02:00
keys.globalkeys = gears.table.join(keys.globalkeys,
-- Switch to tag
awful.key({modkey}, "#" .. tag + 9,
2022-07-23 19:31:38 +02:00
function()
local screen = awful.screen.focused()
local t = screen.tags[tag]
if t then
t:view_only()
2022-07-23 19:31:38 +02:00
end
end,
{description = "view tag #"..tag, group = "tag"}
2022-07-23 19:31:38 +02:00
),
-- Move client to tag
awful.key({modkey, "Shift"}, "#" .. tag + 9,
2022-07-23 19:31:38 +02:00
function()
if client.focus then
local t= client.focus.screen.tags[tag]
if t then
client.focus:move_to_tag(t)
t:view_only()
2022-07-23 19:31:38 +02:00
end
end
end,
{description = "move focused client to tag #"..tag, group = "tag"}
2022-07-23 19:31:38 +02:00
)
)
2022-07-19 19:45:11 +02:00
end
-- Bind all key numbers to tags
for i = 1, 10 do
register_tag(i)
end
2022-07-19 19:45:11 +02:00
return keys