diff options
Diffstat (limited to '.config/awesome/services/common.lua')
-rw-r--r-- | .config/awesome/services/common.lua | 87 |
1 files changed, 37 insertions, 50 deletions
diff --git a/.config/awesome/services/common.lua b/.config/awesome/services/common.lua index b23a064..ad1f998 100644 --- a/.config/awesome/services/common.lua +++ b/.config/awesome/services/common.lua @@ -1,52 +1,61 @@ local gfs = require "gears.filesystem" local gtimer = require "gears.timer" local phosphor = require "assets.phosphor" -local qfs = require "quarrel.fs" local qjson = require "quarrel.json" local qmath = require "quarrel.math" local qnative = require "quarrel.native" local qstore = require "quarrel.store" local qvars = require "quarrel.vars" +local UPower = require("lgi").UPowerGlib -local q = require "quarrel" - -local function register(name, service, icon) +--- Register a service +---@param name string +---@param service Service +local function register(name, service) gtimer { - timeout = 0.5, + timeout = 1, call_now = true, autostart = true, callback = function() - local service_result = table.pack(service()) + local service_result = table.pack(service[1]()) awesome.emit_signal("services::" .. name, table.unpack(service_result)) - awesome.emit_signal("services::" .. name .. "::icon", icon(table.unpack(service_result))) - end + awesome.emit_signal("services::" .. name .. "::icon", service[2](table.unpack(service_result))) + end, } end +--- Read a file and automatically close it +---@param file FileHandle +---@param format ReadMode? +---@return string | number local function read(file, format) local content = file:read(format or "a") - file:seek("set") + file:rewind() return content end -- create file in case it's not there yet -if not gfs.file_readable("/tmp/wp_audio_status") then +if not gfs.file_readable "/tmp/wp_audio_status" then assert(io.open("/tmp/wp_audio_status", "w")):write("{}"):close() end -qstore.audio_file = assert(io.open("/tmp/wp_audio_status", "r")) -qstore.battery_capacity_file = assert(io.open("/sys/class/power_supply/BAT0/capacity", "r")) -qstore.battery_status_file = assert(io.open("/sys/class/power_supply/BAT0/status", "r")) -qstore.brightness_file = assert(io.open("/sys/class/backlight/amdgpu_bl0/actual_brightness", "r")) -qstore.wifi_file = assert(io.open("/proc/net/wireless", "r")) + +qstore.audio_file = qnative.util.open_file "/tmp/wp_audio_status" +qstore.brightness_file = qnative.util.open_file "/sys/class/backlight/amdgpu_bl1/actual_brightness" +qstore.wifi_file = qnative.util.open_file "/proc/net/wireless" -- follows the format `service = { provider, icon_provider }` +---@class Service +---@field [1] fun(): ... +---@field [2] fun(...): path: string, color: string + +---@type table<string, Service> local services = { audio = { -- volume, muted function() local audio_status = qjson.decode(read(qstore.audio_file)) local default_sink = audio_status["G435 Wireless Gaming Headset Analog Stereo"] - + if not default_sink then return nil, false end @@ -63,29 +72,11 @@ local services = { { 25, "none" }, { 50, "low" }, { 75, "high" }, - { 100 } + { 100 }, }) return phosphor["speaker_simple_" .. icon_data .. "_fill"], qvars.colors.fg - end - }, - battery = { - -- capacity, status - function() - return read(qstore.battery_capacity_file, "n"), read(qstore.battery_status_file, "l") end, - function(capacity, status) - local icon_data = status == "Charging" and { "charging", "green" } or qmath.step_value(capacity, { - { 0, { "empty", "red" } }, - { 20, { "low", "red" } }, - { 40, { "medium", "yellow" } }, - { 60, { "high", "green" } }, - { 80, { "full", "green" } }, - { 100 } - }) - - return phosphor["battery_vertical_" .. icon_data[1] .. "_fill"], qvars.colors[icon_data[2]] - end }, brightness = { -- brightness @@ -99,29 +90,25 @@ local services = { { 102, "sun_horizon" }, { 153, "sun_dim" }, { 204, "sun" }, - { 255 } + { 255 }, }) return phosphor[icon_data .. "_fill"], qvars.colors.fg - end + end, }, wifi = { -- essid, strength, connected function() - local lines = {} - - for line in qstore.wifi_file:lines() do - table.insert(lines, line) - end - qstore.wifi_file:seek("set") + local lines = qstore.wifi_file:lines() + qstore.wifi_file:rewind() - if not lines[3] then + if not lines[3] then return nil, 0, false end - local strength = lines[3]:match("^%s*(.*)"):match("^.-%s+.-%s+.-(%S+)%.") + local strength = lines[3]:match("^%s*(.*)"):match "^.-%s+.-%s+.-(%S+)%." - return qnative.get_essid(), math.floor(tonumber(strength) * 10 / 7), true + return qnative.net.get_essid(), math.floor(tonumber(strength) * 10 / 7), true end, function(_, strength, connected) if not connected then @@ -133,14 +120,14 @@ local services = { { 25, { "low", "yellow" } }, { 50, { "medium", "yellow" } }, { 75, { "high", "green" } }, - { 100 } + { 100 }, }) return phosphor["wifi_" .. icon_data[1] .. "_fill"], qvars.colors[icon_data[2]] - end - } + end, + }, } for name, service in pairs(services) do - register(name, service[1], service[2]) + register(name, service) end |