Started migrating to module architecture

master
Mark 2022-07-16 14:13:03 -07:00
parent d68efc5713
commit 840f084db8
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
16 changed files with 369 additions and 289 deletions

View File

@ -72,15 +72,5 @@ return {
--awful.spawn("flameshot gui -p \"" .. conf.screenshot_dir .. "\"", false)
awful.spawn("flameshot gui", false)
end
},
mpc = {
watch = function(command, timeout, callback, widget)
awful.widget.watch("mpc --host " .. conf.mpd_host .. " " .. command, timeout, callback, widget)
end,
command = function(command, callback)
awful.spawn.easy_async("mpc --host " .. conf.mpd_host .. " " .. command, callback)
end
}
}

View File

@ -1,10 +1,8 @@
return {
keys = gears.table.join(
require("binds.system.backlight"),
require("binds.system.mpd"),
require("binds.system.volume"),
require("binds.system.system"),
require("binds.system.input"),
require("binds.window.client"),
require("binds.window.layout"),

View File

@ -1,15 +1,11 @@
local desktop = {
Tagger = require("desktop.tagger"),
popup = {
language = require("desktop.popups.language")
},
widgets = {
tasklist = require("desktop.widgets.tasklist"),
textclock = require("desktop.widgets.textclock"),
layoutbox = require("desktop.widgets.layoutbox"),
keymap = require("desktop.widgets.keymap"),
keymap = modules.ibus.widgets.ibus,
volume = require("desktop.widgets.volume"),
tagindicator = require("desktop.widgets.tagindicator"),
launcher = require("desktop.widgets.launcher"),
@ -56,7 +52,7 @@ if conf.battery_enabled then
desktop.widgets.battery = require("desktop.widgets.battery")
end
if conf.mpc_enabled then
desktop.widgets.mpc = require("desktop.widgets.mpc")
desktop.widgets.mpc = modules.mpc.widgets.mpc
end

View File

@ -1,41 +0,0 @@
local keymap = {}
keymap.widget = wibox.widget {
{
{ -- Right spacer
widget = wibox.widget.separator,
color = beautiful.color.transparent,
forced_width = beautiful.dpi(3)
},
wibox.widget {
wrapper.ibus.ibus_indicator_text,
wrapper.ibus.xkb_indicator_text,
forced_num_cols = 1,
forced_num_rows = 2,
homogeneous = true,
expand = true,
layout = wibox.layout.grid
},
{ -- Left spacer
widget = wibox.widget.separator,
color = beautiful.color.transparent,
forced_width = beautiful.dpi(3)
},
layout = wibox.layout.align.horizontal,
},
layout = wibox.container.background,
}
-- Change background when mouse is over widget
keymap.widget:connect_signal("mouse::enter", function(result)
keymap.widget.bg = beautiful.color.bar.hover_bg
end)
keymap.widget:connect_signal("mouse::leave", function(result)
keymap.widget.bg = beautiful.color.transparent
end)
return keymap.widget

12
modules/ibus/init.lua Normal file
View File

@ -0,0 +1,12 @@
local util = require("modules.ibus.util")
return {
widgets = {
ibus = require("modules.ibus.widget").widget
},
keybinds = require("modules.ibus.keybinds"),
init = function()
util.set(1)
end
}

View File

@ -1,25 +1,42 @@
local util = req_rel(..., "util")
local popup = req_rel(..., "popup")
local widget = req_rel(..., "widget")
return gears.table.join(
awful.key( {"Mod4"}, "space",
function()
desktop.popup.language.next()
util.next(function()
popup.update()
widget.update()
end)
end,
{
description = "Change input language",
group = "System"
}
),
awful.key( {"Mod4", "Shift", "Control"}, "9",
function()
desktop.popup.language.set(2)
util.set(2, function()
popup.update()
widget.update()
end)
end,
{
description = "Set input language to RU",
group = "System"
}
),
awful.key( {"Mod4", "Shift", "Control"}, "0",
function()
desktop.popup.language.set(1)
util.set(1, function()
popup.update()
widget.update()
end)
end,
{
description = "Set input language to EN",

View File

@ -1,10 +1,8 @@
local language = {}
local popup = {}
local util = req_rel(..., "util")
language.language_list = conf.ibus_language_list
language.widget = wibox.widget {
popup.widget = wibox.widget {
homogeneous = false,
vertical_homogeneous = true,
horizontal_homogeneous = false,
@ -16,7 +14,7 @@ language.widget = wibox.widget {
}
for k, l in pairs(language.language_list) do
for k, l in pairs(conf.ibus_language_list) do
l["widget_checkbox"] = wibox.widget {
checked = false,
@ -44,52 +42,33 @@ for k, l in pairs(language.language_list) do
l["widget_text"].bg = "#00000000"
language.widget:add_widget_at(l["widget_text"], k, 1, 1, 1)
--language.widget:add_widget_at(l["widget_checkbox"], k, 1, 1, 1)
--language.widget:add_widget_at(l["widget_text"], k, 2, 0, 1)
popup.widget:add_widget_at(l["widget_text"], k, 1, 1, 1)
--popup.widget:add_widget_at(l["widget_checkbox"], k, 1, 1, 1)
--popup.widget:add_widget_at(l["widget_text"], k, 2, 0, 1)
end
language.next = function()
if (language.popup.visible) then
wrapper.ibus.next(function()
language.update_checks()
end)
else
language.update_checks()
end
language.show_popup()
end
language.set = function(idx)
wrapper.ibus.set(idx, function()
language.update_checks()
end)
language.show_popup()
end
language.update_checks = function()
for _, l in pairs(language.language_list) do
if (wrapper.ibus.current_engine == l["ibus_engine"]) then
popup.update = function()
-- Update checkmarks
for _, l in pairs(conf.ibus_language_list) do
if (util.current_engine == l["ibus_engine"]) then
l["widget_text"].bg = "#FF0000"
else
l["widget_text"].bg = "#00000000"
end
l["widget_checkbox"].checked = (wrapper.ibus.current_engine == l["ibus_engine"])
l["widget_checkbox"].checked = (util.current_engine == l["ibus_engine"])
end
-- Show popup
popup.popup.screen = awful.screen.focused()
popup.popup.visible = true
popup.popup_timer:again()
end
language.show_popup = function()
language.popup.screen = awful.screen.focused()
language.popup.visible = true
language.popup_timer:again()
end
language.popup = awful.popup {
popup.popup = awful.popup {
widget = {
{
language.widget,
popup.widget,
margins = 10,
widget = wibox.container.margin
},
@ -108,22 +87,25 @@ language.popup = awful.popup {
shape = gears.shape.rectangle,
placement = function(d)
return awful.placement.bottom_right(d, {
return awful.placement.bottom_right(
d,
{
honor_workarea = true
})
}
)
end,
}
language.popup_timer = gears.timer {
popup.popup_timer = gears.timer {
timeout = 1,
autostart = false,
call_now = false,
single_shot = true,
callback = function()
language.popup.visible = false
popup.popup.visible = false
end
}
return language
return popup

94
modules/ibus/util.lua Normal file
View File

@ -0,0 +1,94 @@
local util = {}
util.current_engine = ""
util.current_engine_index = nil
util.current_xkbmap = ""
-- Update current_engine and current_engine index,
-- call callback() when done.
util.get = function(callback)
-- Get current ibus engine
awful.spawn.easy_async(
"ibus engine",
function(stdout, stderr, exitreason, exitcode)
util.current_engine = string.gsub(stdout, "\n", "")
util.current_engine_index = nil
-- Find the current engine's index in conf.ibus_language_list.
-- If it is not there, util.current_engine_index will be nil.
for k, v in pairs(conf.ibus_language_list) do
if (v["ibus_engine"] == util.current_engine) then
util.current_engine_index = k
end
end
-- Get current xkbmap
awful.spawn.easy_async(
"setxkbmap -query",
function(stdout, stderr, exitreason, exitcode)
util.current_xkbmap = string.match(stdout, "layout:%s+(%w+)")
-- When everything is done, run callback if it is given.
if callback then
callback()
end
end
)
end
)
end
-- Set engine to language_index.
-- calls util.get(callback) when done.
util.set = function(language_index, callback)
-- engine is an index of the language list above
local engine = conf.ibus_language_list[language_index]["ibus_engine"]
-- Get required engine, if one is given
local requires_engine
for k, v in pairs(conf.ibus_language_list) do
if (v["ibus_engine"] == engine) then
requires_engine = v["requires_engine"]
end
end
-- If a required xkb engine is given, but it is not active, switch to it before switching to the target
if (requires_engine ~= util.current_engine) and (requires_engine ~= nil) then
awful.spawn.easy_async(
"ibus engine " .. requires_engine,
function(stdout, stderr, exitreason, exitcode)
awful.spawn.easy_async(
"ibus engine " .. engine,
function(stdout, stderr, exitreason, exitcode)
util.get(callback)
end
)
end
)
else
awful.spawn.easy_async(
"ibus engine " .. engine,
function(stdout, stderr, exitreason, exitcode)
util.get(callback)
end
)
end
end
-- Calls util.set(callback) with next language in list.
util.next = function(callback)
if (util.current_engine_index == nil) or (util.current_engine_index == #conf.ibus_language_list) then
util.current_engine_index = 1
else
util.current_engine_index = util.current_engine_index + 1
end
util.set(util.current_engine_index, callback)
end
return util

72
modules/ibus/widget.lua Executable file
View File

@ -0,0 +1,72 @@
local util = req_rel(..., "util")
local widget = {}
widget.ibus_indicator_text = wibox.widget.textbox("??")
widget.ibus_indicator_text.valign = "center"
widget.ibus_indicator_text.align = "center"
widget.ibus_indicator_text.font = "Hack NF 14"
widget.xkb_indicator_text = wibox.widget.textbox("??")
widget.xkb_indicator_text.valign = "center"
widget.xkb_indicator_text.align = "center"
widget.xkb_indicator_text.font = "Hack NF 10"
widget.widget = wibox.widget {
{
{ -- Right spacer
widget = wibox.widget.separator,
color = beautiful.color.transparent,
forced_width = beautiful.dpi(3)
},
wibox.widget {
widget.ibus_indicator_text,
widget.xkb_indicator_text,
forced_num_cols = 1,
forced_num_rows = 2,
homogeneous = true,
expand = true,
layout = wibox.layout.grid
},
{ -- Left spacer
widget = wibox.widget.separator,
color = beautiful.color.transparent,
forced_width = beautiful.dpi(3)
},
layout = wibox.layout.align.horizontal,
},
layout = wibox.container.background,
}
-- Change background when mouse is over widget
widget.widget:connect_signal("mouse::enter", function(result)
widget.widget.bg = beautiful.color.bar.hover_bg
end)
widget.widget:connect_signal("mouse::leave", function(result)
widget.widget.bg = beautiful.color.transparent
end)
widget.update = function()
widget.ibus_indicator_text.markup = conf.ibus_language_list[util.current_engine_index]["indicator_code"]
widget.xkb_indicator_text.markup = util.current_xkbmap
end
-- Update every 2 seconds, just in case.
widget.update_timer = gears.timer {
timeout = 2,
call_now = false,
autostart = true,
single_shot = false,
callback = function ()
util.get(widget.update)
end
}
return widget

6
modules/mpc/init.lua Normal file
View File

@ -0,0 +1,6 @@
return {
widgets = {
mpc = require("modules.mpc.widget").widget
},
keybinds = require("modules.mpc.keybinds")
}

View File

@ -1,51 +1,53 @@
local mpc = req_rel(..., "util")
return gears.table.join(
awful.key( {}, "XF86AudioPrev",
function ()
bin.mpc.command("prev")
mpc.mpc_command("prev")
end,
{
description = "Previous track",
group = "MPD"
group = "MPC"
}
),
awful.key( {}, "XF86AudioPlay",
function ()
bin.mpc.command("toggle")
mpc.mpc_command("toggle")
end,
{
description = "Play/Pause",
group = "MPD"
group = "MPC"
}
),
awful.key( {}, "XF86AudioNext",
function ()
bin.mpc.command("next")
mpc.mpc_command("next")
end,
{
description = "Next track",
group = "MPD"
group = "MPC"
}
),
awful.key( {"Mod4"}, "XF86AudioRaiseVolume",
function ()
bin.mpc.command("vol +5")
mpc.mpc_command("vol +5")
end,
{
description = "Volume up",
group = "MPD"
group = "MPC"
}
),
awful.key( {"Mod4"}, "XF86AudioLowerVolume",
function ()
bin.mpc.command("vol -5")
mpc.mpc_command("vol -5")
end,
{
description = "Volume down",
group = "MPD"
group = "MPC"
}
)
)

19
modules/mpc/util.lua Normal file
View File

@ -0,0 +1,19 @@
local util = {}
util.mpc_command = function(command, callback)
awful.spawn.easy_async(
"mpc --host " .. conf.mpd_host .. " " .. command,
callback
)
end
util.mpc_watch = function(command, timeout, callback, widget)
awful.widget.watch(
"mpc --host " .. conf.mpd_host .. " " .. command,
timeout,
callback,
widget
)
end
return util

View File

@ -1,3 +1,4 @@
local mpc = req_rel(..., "util")
local mpc_widget = {}
mpc_widget.title = wibox.widget.textbox("MPD is not")
@ -64,7 +65,6 @@ mpc_widget.right = wibox.widget {
layout = wibox.container.background,
}
mpc_widget.widget = wibox.widget {
{
{ -- Right spacer
@ -87,7 +87,7 @@ mpc_widget.widget = wibox.widget {
}
mpc_widget.widget:connect_signal("mouse::enter", function(result)
mpc_widget.widget:connect_signal("mouse::enter",function(result)
mpc_widget.widget.bg = beautiful.color.bar.hover_bg
end)
@ -99,7 +99,7 @@ end)
mpc_widget.widget:connect_signal("button::press",
function(_, _, _, button, mods)
if (button == 3) or (button == 1)then
bin.mpc.command("toggle")
mpc.mpc_command("toggle")
end
mpc_widget.readupdate()
end
@ -109,9 +109,9 @@ mpc_widget.widget:connect_signal("button::press",
mpc_widget.right:connect_signal("button::press",
function(_, _, _, button, mods)
if (button == 4) then -- Scroll up
bin.mpc.command("prev")
mpc.mpc_command("prev")
elseif (button == 5) then -- Scroll down
bin.mpc.command("next")
mpc.mpc_command("next")
end
mpc_widget.readupdate()
@ -122,31 +122,33 @@ mpc_widget.right:connect_signal("button::press",
mpc_widget.left:connect_signal("button::press",
function(_, _, _, button, mods)
if (button == 4) then -- Scroll up
bin.mpc.command("volume +5")
mpc.mpc_command("volume +5")
elseif (button == 5) then -- Scroll down
bin.mpc.command("volume -5")
mpc.mpc_command("volume -5")
end
mpc_widget.readupdate()
end
)
mpc_widget.update = function(stdout)
mpc_widget.readupdate = function()
-- Update title and artist
mpc.mpc_command("current --format \"title=%title% artist=%artist%\"",
function(stdout, stderr, exitreason, exitcode)
if (stdout == "") then
return
end
mpc_widget.title.markup = string.match(stdout, "title=(.*)artist=")
mpc_widget.artist.markup = string.match(stdout, "artist=(.*)")
end
)
end
mpc_widget.update_status = function(stdout)
play = string.match(stdout, "(%[playing)") or false
pause = string.match(stdout, "(%[paused)") or false
stop = not (play or pause)
-- Update status
mpc.mpc_command("status",
function(stdout, stderr, exitreason, exitcode)
local play = string.match(stdout, "(%[playing)") or false
local pause = string.match(stdout, "(%[paused)") or false
local stop = not (play or pause)
if (play) then
mpc_widget.icon_play.image = beautiful.icons.music.blue.playing
@ -156,38 +158,41 @@ mpc_widget.update_status = function(stdout)
mpc_widget.icon_play.image = beautiful.icons.music.blue.stopped
end
volume = string.match(stdout, "volume: (%d?%d?%d)%%")
local volume = string.match(stdout, "volume: (%d?%d?%d)%%")
if (volume) then
mpc_widget.volume.markup = volume .. "%"
else
mpc_widget.volume.markup = ""
end
end
mpc_widget.readupdate = function()
bin.mpc.command("current --format \"title=%title% artist=%artist%\"",
function(stdout, stderr, exitreason, exitcode)
mpc_widget.update(stdout)
end
)
bin.mpc.command("status",
function(stdout, stderr, exitreason, exitcode)
mpc_widget.update_status(stdout)
end
)
end
-- The time here is super short because the --wait option is used in mpc.
-- It will not return a response until changes are seen.
bin.mpc.watch("current --wait", 0.01, function(_, stdout) mpc_widget.readupdate() end, mpc_widget.widget)
bin.mpc.watch("current", 1, function(_, stdout) mpc_widget.readupdate() end, mpc_widget.widget)
-- Short delay here, the --wait option makes mpc block until
-- a change is detected.
mpc.mpc_watch(
"current --wait",
0.01,
function(_, stdout)
mpc_widget.readupdate()
end,
mpc_widget.widget
)
mpc.mpc_watch(
"current",
1,
function(_, stdout)
mpc_widget.readupdate()
end,
mpc_widget.widget
)
-- Make sure you do an initial read, though, otherwise the widget will
-- Make sure you do an initial read, otherwise the widget will
-- not update until a change occurs.
mpc_widget.readupdate()
return mpc_widget.widget
return mpc_widget

63
rc.lua
View File

@ -1,6 +1,4 @@
-- Load libaries.
-- We only need to load these once.
-- These are global variables, all scripts can access them
-- AwesomeWM core
gears = require("gears")
awful = require("awful")
naughty = require("naughty")
@ -11,31 +9,73 @@ configuration_dir = gears.filesystem.get_configuration_dir()
require("awful.autofocus")
-- A "relative require" hack. Does the same thing as `require,`
-- But takes a relative path.
-- Called with req_rel(..., "module")
function req_rel(ddd, modname)
local parent_folder = (ddd):match("(.-)[^%.]+$")
return require(parent_folder .. modname)
end
-- Quick debug function
debug_message = function(msg)
naughty.notify({title = "Debug message:", text = tostring(msg)})
end
-- These must be loaded in order.
-- Make sure you've created conf.lua!
conf = require("conf")
conf.sound_dir = configuration_dir .. "theme/resources/sounds/"
conf.icon_dir = configuration_dir .. "theme/resources/icons/"
layoutmanager = require("desktop.layoutmanager")
layoutmanager.layouts = conf.layouts
bin = require("bin")
wrapper = require("wrapper")
wrapper.ibus.set(1)
beautiful.init(require("theme"))
-- Load key bindings
local binds = require("binds")
local keys = binds.keys
local buttons = binds.buttons
-- Load modules
modules = {}
for k, v in ipairs({
"mpc",
"ibus"
}) do
modules[v] = require("modules." .. v)
-- If module defines keybinds, load keybinds.
if (modules[v]["keybinds"] ~= nil) then
keys = gears.table.join(
keys,
modules[v]["keybinds"]
)
end
end
-- Run all module init methods
for _, mod in ipairs(modules) do
if (mod["init"] ~= nil) then
mod["init"]()
end
end
-- Finalize keys
root.keys(keys)
root.buttons(buttons)
desktop = require("desktop")
-- Autostart
for i, v in ipairs(conf.startup_commands) do
awful.spawn(v)
@ -55,15 +95,6 @@ terminal = conf.terminal
menubar.utils.terminal = terminal
------------------
-- Load Modules --
------------------
-- Load key bindings
local binds = require("binds")
root.keys(binds.keys)
root.buttons(binds.buttons)
-- Load client methods
awful.rules.rules = require("clients.rules")
dofile(configuration_dir .. "clients/signals.lua")

View File

@ -1,102 +0,0 @@
local ibus = {}
ibus.current_engine = ""
ibus.current_engine_index = nil
ibus.ibus_indicator_text = wibox.widget.textbox("??")
ibus.ibus_indicator_text.valign = "center"
ibus.ibus_indicator_text.align = "center"
ibus.ibus_indicator_text.font = "Hack NF 14"
ibus.xkb_indicator_text = wibox.widget.textbox("??")
ibus.xkb_indicator_text.valign = "center"
ibus.xkb_indicator_text.align = "center"
ibus.xkb_indicator_text.font = "Hack NF 10"
ibus.get = function(callback)
awful.spawn.easy_async("ibus engine", function(stdout, stderr, exitreason, exitcode)
-- Get current ibus engine, remove newlines from output.
ibus.current_engine = string.gsub(stdout, "\n", "")
ibus.current_engine_index = nil
-- Find the current engine's index in conf.ibus_language_list.
-- If it is not there, ibus.current_engine_index will be nil.
for k, v in pairs(conf.ibus_language_list) do
if (v["ibus_engine"] == ibus.current_engine) then
ibus.current_engine_index = k
end
end
if callback then
callback()
end
end)
end
ibus.update_indicator = function()
-- Update indicators
awful.spawn.easy_async("setxkbmap -query" , function(stdout, stderr, exitreason, exitcode)
ibus.ibus_indicator_text.markup = conf.ibus_language_list[ibus.current_engine_index]["indicator_code"]
ibus.xkb_indicator_text.markup = string.match(stdout, "layout:%s+(%w+)")
end)
end
ibus.set = function(language_index, callback)
-- engine is an index of the language list above
engine = conf.ibus_language_list[language_index]["ibus_engine"]
-- Get required engine, if one is given
local requires_engine
for k, v in pairs(conf.ibus_language_list) do
if (v["ibus_engine"] == engine) then
requires_engine = v["requires_engine"]
end
end
-- If a required xkb engine is given, but it is not active, switch to it before switching to the target
if (requires_engine ~= ibus.current_engine) and (requires_engine ~= nil) then
awful.spawn.easy_async("ibus engine " .. requires_engine, function(stdout, stderr, exitreason, exitcode)
awful.spawn.easy_async("ibus engine " .. engine, function(stdout, stderr, exitreason, exitcode)
ibus.update_indicator()
ibus.get(callback)
end)
end)
else
awful.spawn.easy_async("ibus engine " .. engine, function(stdout, stderr, exitreason, exitcode)
ibus.update_indicator()
ibus.get(callback)
end)
end
end
ibus.next = function(callback)
if (ibus.current_engine_index == nil) or (ibus.current_engine_index == #conf.ibus_language_list) then
ibus.current_engine_index = 1
else
ibus.current_engine_index = ibus.current_engine_index + 1
end
ibus.set(ibus.current_engine_index, callback)
end
-- Update the language indicator
ibus.update_timer = gears.timer {
timeout = 2,
call_now = false,
autostart = true,
single_shot = false,
callback = function()
ibus.get(
function()
ibus.update_indicator()
end
)
end
}
return ibus

View File

@ -2,6 +2,5 @@ return {
backlight = require("wrapper.backlight"),
volume = require("wrapper.volume"),
sound = require("wrapper.sound"),
ibus = require("wrapper.ibus"),
mdadm = require("wrapper.mdadm")
}