From 923eb46350b1102749eb05cd2120c96cc6a715d0 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Fri, 18 Oct 2024 21:49:17 -0500 Subject: initial commit --- widgets/audio.lua | 21 +++++++++ widgets/battery.lua | 24 +++++++++++ widgets/button.lua | 33 ++++++++++++++ widgets/classiclayouts.lua | 64 ++++++++++++++++++++++++++++ widgets/cpu.lua | 38 +++++++++++++++++ widgets/pavolctld.lua | 104 +++++++++++++++++++++++++++++++++++++++++++++ widgets/ram.lua | 34 +++++++++++++++ widgets/temperature.lua | 26 ++++++++++++ 8 files changed, 344 insertions(+) create mode 100644 widgets/audio.lua create mode 100644 widgets/battery.lua create mode 100644 widgets/button.lua create mode 100644 widgets/classiclayouts.lua create mode 100644 widgets/cpu.lua create mode 100644 widgets/pavolctld.lua create mode 100644 widgets/ram.lua create mode 100644 widgets/temperature.lua (limited to 'widgets') diff --git a/widgets/audio.lua b/widgets/audio.lua new file mode 100644 index 0000000..002c556 --- /dev/null +++ b/widgets/audio.lua @@ -0,0 +1,21 @@ +local awful = require("awful") +local wibox = require("wibox") +local gears = require("gears") + +local audio_dropdown = awful.popup { + widget = { + margins = 10, + widget = wibox.container.margin, + { + layout = wibox.layout.fixed.horizontal, + wibox.widget.textbox "AUDIO", + }, + }, + ontop = true, + placement = awful.placement.centered, + border_width = 1, + border_color = "#ff0000", + visible = true, +} + +return audio_dropdown diff --git a/widgets/battery.lua b/widgets/battery.lua new file mode 100644 index 0000000..f6e6860 --- /dev/null +++ b/widgets/battery.lua @@ -0,0 +1,24 @@ +local awful = require("awful") +local wibox = require("wibox") +local naughty = require("naughty") + +local widget = wibox.widget.textbox() + +-- update widget on lowbat output +local lowbat_pid = awful.spawn.with_line_callback("lowbat", { + stdout = function(stdout) + widget:set_text(stdout) + end, + stderr = function(stderr) + naughty.notify({ + preset = naughty.config.presets.critical, + title = "lowbat error", + text = stderr + }) + end, +}) + +-- kill current lowbat on refresh/exit +awesome.connect_signal("exit", function() awful.spawn("kill " .. lowbat_pid) end) + +return widget diff --git a/widgets/button.lua b/widgets/button.lua new file mode 100644 index 0000000..cd1da1e --- /dev/null +++ b/widgets/button.lua @@ -0,0 +1,33 @@ +local awful = require("awful") +local beautiful = require("beautiful") +local menubar = require("menubar") + +local hotkeys_popup = require("awful.hotkeys_popup") +-- Enable hotkeys help widget for VIM and other apps +-- when client with a matching name is opened: +require("awful.hotkeys_popup.keys") + +-- Menu +-- Create a launcher widget and a main menu +myawesomemenu = { + { "hotkeys", function() hotkeys_popup.show_help(nil, awful.screen.focused()) end }, + { "restart", awesome.restart }, + { "quit", function() awesome.quit() end }, +} + +mymainmenu = awful.menu({ + items = { + { "awesome", myawesomemenu, beautiful.awesome_icon }, + { "open terminal", terminal } + } +}) + +mylauncher = awful.widget.launcher({ + image = beautiful.awesome_icon, + menu = mymainmenu +}) + +-- Menubar configuration +menubar.utils.terminal = terminal -- Set the terminal for applications that require it + +return mylauncher diff --git a/widgets/classiclayouts.lua b/widgets/classiclayouts.lua new file mode 100644 index 0000000..3b4fd64 --- /dev/null +++ b/widgets/classiclayouts.lua @@ -0,0 +1,64 @@ +local wibox = require("wibox") +local layout = require("awful.layout") + +local screen_widgets = {} -- need one widget per screen + +local layout_icons = { + cornernw = "TT=", + cornerne = "=TT", + cornersw = "LL=", + cornerse = "=LL", + fairh = "#_#", + fairv = "##H", + max = "[M]", + floating = "><>", + magnifier = "=O=", + fullscreen = "[*]", + spiral = "[]@", + dwindle = "[]\\", + tile = "[]=", + tiletop = "LLL", + tilebottom = "TTT", + tileleft = "=[]", +} + +function update(screen_index) + local s = screen[screen_index or 1] + local w = screen_widgets[s] + local l = layout.get(s) + + -- create widget if not existing + if w == nil then + screen_widgets[s] = { widget = wibox.widget.textbox(), layout = l } + w = screen_widgets[s] + --w._layoutbox_tooltip = tooltip {objects = {w}, delay_show = 1} + + -- skip if no change + elseif w.layout == l then + return + end + + -- set widget + local name = layout.getname(l) + --w._layoutbox_tooltip:set_text(name) + w.widget:set_text(layout_icons[name]) -- TODO handle nil + return w.widget +end + +function update_screens() + for s, w in pairs(boxes) do + if s.valid then + update(s) + end + end +end + +function update_current_tag(t) + update(t.screen) +end + +tag.connect_signal("property::selected", update_current_tag) +tag.connect_signal("property::layout", update_current_tag) +tag.connect_signal("property::screen", update_screens) + +return update diff --git a/widgets/cpu.lua b/widgets/cpu.lua new file mode 100644 index 0000000..d92bce8 --- /dev/null +++ b/widgets/cpu.lua @@ -0,0 +1,38 @@ +local wibox = require("wibox") +local widgets = require("util.widgets") + +-- this is directly adapted from slstatus's cpu.c module +local cpu_time = { 0,0,0,0,0,0,0 } -- user, nice, system, idle, iowait, irq, softirq + +function linux_cpu_usage(widget) + -- read stat + local statf = io.open("/proc/stat") + local stat_iter = statf:read():gmatch("%d+") + statf:close() + + -- calc + local sum = 0 + local a, b = cpu_time, {} -- set cpu_time as a for smaller code + table.move(a, 1, 7, 1, b) -- copy first 7 to b + + for i = 1, 7 do + a[i] = stat_iter() + sum = sum + b[i] - a[i] + end + + if sum == 0 then + return + end + + local usage = ((b[1] + b[2] + b[3] + b[6] + b[7]) - + (a[1] + a[2] + a[3] + a[6] + a[7])) * 100 / sum + + widget:set_text(math.floor(usage)) +end + +-- return correct widget for os +if osname == "Linux" then + return widgets.watchfn(linux_cpu_usage, 5) +end + +return wibox.widget.textbox("unsupported os") diff --git a/widgets/pavolctld.lua b/widgets/pavolctld.lua new file mode 100644 index 0000000..f59b27f --- /dev/null +++ b/widgets/pavolctld.lua @@ -0,0 +1,104 @@ +local awful = require("awful") +local wibox = require("wibox") +local naughty = require("naughty") + +-- Gio is used to handle the subprocess instead of awful.spawn. +-- Gio is more flexible and allows writing to stdin. +-- also, awful.spawn.with_line_callback does not play nicely with pavolctld, as +-- it seems to feed the stdout back into its stdin and kill performance. +local lgi = require("lgi") +local Gio = lgi.Gio + +-- return table +local widget = { + textbox = wibox.widget.textbox() +} + +-- start subprocess +local p = Gio.Subprocess.new({ "pavolctld" }, Gio.SubprocessFlags.STDIN_PIPE + Gio.SubprocessFlags.STDOUT_PIPE) + +local stdout = p:get_stdout_pipe() +local stdin = p:get_stdin_pipe() + +-- state vars +local sinks = { + default = nil, + command = nil, -- sink being modified by commands + sinks = {}, +} + +function sinks.get(i) + if sinks.sinks[i] == nil then + sinks.sinks[i] = { + vol = 0, + db = 0.0, + mute = 0, + name = "", + desc = "", + } + end + return sinks.sinks[i] +end + +-- parse output +function parse_csv(csv) + return (csv .. ","):gmatch("(.-),") +end + +awful.spawn.read_lines(stdout, function(s) + local cmd = s:sub(1, 1) -- first char of output + -- volume change + if cmd == 'v' then + local v = parse_csv(s:sub(2)) + local i = tonumber(v()) + local sink = sinks.get(i) + sink.vol = tonumber(v()) + sink.db = tonumber(v()) + sink.mute = tonumber(v()) + + if sink == sinks.default then + widget.textbox:set_text(sink.vol) + end + -- sink description change + elseif cmd == 's' then + local v = parse_csv(s:sub(2)) + local i = tonumber(v()) + local sink = sinks.get(i) + sink.name = v() + sink.desc = v() + -- default sink change + elseif cmd == 'f' then + local f = tonumber(s:sub(2)) + sinks.default = sinks.get(f) + widget.textbox:set_text(sinks.default.vol) + -- set command sink to default sink for now TODO change later + pavolctld_cmd("s") + -- sink removed + elseif cmd == 'x' then + local x = tonumber(s:sub(2)) + table.remove(sinks.sinks, x) + else + naughty.notify({ + preset = naughty.config.presets.critical, + title = "pavolctld error", + text = s + }) + end +end) + +-- pavolctld takes commands in using stdin +function pavolctld_cmd(cmd) + local _, err = stdin:write_all(cmd .. "\n", nil) + + if err then return nil end + return true +end + +function widget.volume_inc(vol) return pavolctld_cmd("v+" .. vol) end +function widget.volume_dec(vol) return pavolctld_cmd("v-" .. vol) end +function widget.volume_set(vol) return pavolctld_cmd("v" .. vol) end +function widget.mute_set(muted) return pavolctld_cmd("m" .. muted and 1 or 0) end +function widget.mute_toggle() return pavolctld_cmd("m") end +function widget.default_sink_set(i) return pavolctld_cmd("f" .. i) end + +return widget diff --git a/widgets/ram.lua b/widgets/ram.lua new file mode 100644 index 0000000..44ab314 --- /dev/null +++ b/widgets/ram.lua @@ -0,0 +1,34 @@ +local wibox = require("wibox") +local widgets = require("util.widgets") + + +function linux_ram_usage(widget) + -- read meminfo + local meminfof = io.open("/proc/meminfo") + + local total = meminfof:read():match("%d+") + local free = meminfof:read():match("%d+") + + meminfof:read() -- memavailable not used + + local buffers = meminfof:read():match("%d+") + local cached = meminfof:read():match("%d+") + + meminfof:close() + + -- calc + if total == nil then + return + end + + local used = (total - free - buffers - cached) * 100 / total + + widget:set_text(math.floor(used)) +end + +-- return correct widget for os +if osname == "Linux" then + return widgets.watchfn(linux_ram_usage, 5) +end + +return wibox.widget.textbox("unsupported os") diff --git a/widgets/temperature.lua b/widgets/temperature.lua new file mode 100644 index 0000000..a30e1ed --- /dev/null +++ b/widgets/temperature.lua @@ -0,0 +1,26 @@ +local wibox = require("wibox") +local widgets = require("util.widgets") + + +local linux = { file = nil } +function linux.temperature(widget) + -- read meminfo + local tempf = io.open(linux.file) + if tempf then + local temp = tempf:read() / 1000 + tempf:close() + widget:set_text(math.floor(temp)) + else + widget:set_text("err") + end +end + +-- return correct widget for os +if osname == "Linux" then + return function(file) + linux.file = file + return widgets.watchfn(linux.temperature, 5) + end +end + +return wibox.widget.textbox("unsupported os") -- cgit v1.2.3