From 324e428d35ee4e86ed64a4cf0a786c1c866e7736 Mon Sep 17 00:00:00 2001 From: RMT Date: Mon, 28 Sep 2020 20:10:16 +0800 Subject: [PATCH] add battery widget and module --- theme/assets/icons/battery_charging.svg | 1 + theme/assets/icons/battery_empty.svg | 1 + theme/assets/icons/battery_error.svg | 1 + theme/assets/icons/battery_full.svg | 1 + theme/assets/icons/battery_ok.svg | 1 + theme/assets/icons/battery_step_four.svg | 1 + theme/assets/icons/battery_step_one.svg | 1 + theme/assets/icons/battery_step_three.svg | 1 + theme/assets/icons/battery_step_two.svg | 1 + theme/assets/icons/battery_unknown.svg | 1 + theme/assets/icons/init.lua | 19 ++++ theme/fly/init.lua | 4 + theme/fly/layout/topbar.lua | 6 ++ theme/fly/module/battery_manager.lua | 105 ++++++++++++++++++++++ theme/fly/signal.lua | 3 + theme/fly/widget/battery.lua | 46 ++++++++++ utils/constants.lua | 11 +++ 17 files changed, 204 insertions(+) create mode 100644 theme/assets/icons/battery_charging.svg create mode 100644 theme/assets/icons/battery_empty.svg create mode 100644 theme/assets/icons/battery_error.svg create mode 100644 theme/assets/icons/battery_full.svg create mode 100644 theme/assets/icons/battery_ok.svg create mode 100644 theme/assets/icons/battery_step_four.svg create mode 100644 theme/assets/icons/battery_step_one.svg create mode 100644 theme/assets/icons/battery_step_three.svg create mode 100644 theme/assets/icons/battery_step_two.svg create mode 100644 theme/assets/icons/battery_unknown.svg create mode 100644 theme/fly/module/battery_manager.lua diff --git a/theme/assets/icons/battery_charging.svg b/theme/assets/icons/battery_charging.svg new file mode 100644 index 0000000..940d860 --- /dev/null +++ b/theme/assets/icons/battery_charging.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/theme/assets/icons/battery_empty.svg b/theme/assets/icons/battery_empty.svg new file mode 100644 index 0000000..b1cad8e --- /dev/null +++ b/theme/assets/icons/battery_empty.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/theme/assets/icons/battery_error.svg b/theme/assets/icons/battery_error.svg new file mode 100644 index 0000000..85838c2 --- /dev/null +++ b/theme/assets/icons/battery_error.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/theme/assets/icons/battery_full.svg b/theme/assets/icons/battery_full.svg new file mode 100644 index 0000000..e80d355 --- /dev/null +++ b/theme/assets/icons/battery_full.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/theme/assets/icons/battery_ok.svg b/theme/assets/icons/battery_ok.svg new file mode 100644 index 0000000..e8f87e2 --- /dev/null +++ b/theme/assets/icons/battery_ok.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/theme/assets/icons/battery_step_four.svg b/theme/assets/icons/battery_step_four.svg new file mode 100644 index 0000000..a204e30 --- /dev/null +++ b/theme/assets/icons/battery_step_four.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/theme/assets/icons/battery_step_one.svg b/theme/assets/icons/battery_step_one.svg new file mode 100644 index 0000000..77e12ab --- /dev/null +++ b/theme/assets/icons/battery_step_one.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/theme/assets/icons/battery_step_three.svg b/theme/assets/icons/battery_step_three.svg new file mode 100644 index 0000000..7f73a88 --- /dev/null +++ b/theme/assets/icons/battery_step_three.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/theme/assets/icons/battery_step_two.svg b/theme/assets/icons/battery_step_two.svg new file mode 100644 index 0000000..9fa5f90 --- /dev/null +++ b/theme/assets/icons/battery_step_two.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/theme/assets/icons/battery_unknown.svg b/theme/assets/icons/battery_unknown.svg new file mode 100644 index 0000000..3078ea5 --- /dev/null +++ b/theme/assets/icons/battery_unknown.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/theme/assets/icons/init.lua b/theme/assets/icons/init.lua index 1d2f8eb..c30eef3 100644 --- a/theme/assets/icons/init.lua +++ b/theme/assets/icons/init.lua @@ -4,10 +4,17 @@ local base_dir = gears.filesystem.get_configuration_dir() .. "theme/" .. "assets return { fly = { + -- icons for startup widget startup = base_dir .. "startup.svg", + + -- icons for control center widget control_center = base_dir .. "control_center.svg", + + -- icons for systray widget tray_toggle = base_dir .. "traytoggle.svg", tray_toggle_clicked = base_dir .. "traytoggle_clicked.svg", + + -- icons for networking widget networking_disconnected = base_dir .. "networking_disconnected.svg", networking_wireless_connecting = base_dir .. "networking_wireless_connecting.svg", networking_wireless_connected_no_internet = base_dir .. "networking_wireless_connected_no_internet.svg", @@ -15,5 +22,17 @@ return { networking_wired_connecting = base_dir .. "networking_wired_connecting.svg", networking_wired_connected_no_internet = base_dir .. "networking_wired_connected_no_internet.svg", networking_wired_connected = base_dir .. "networking_wired_connected.svg", + + -- icons for battery widget + battery_charging = base_dir .. "battery_charging.svg", + battery_empty = base_dir .. "battery_empty.svg", + battery_error = base_dir .. "battery_error.svg", + battery_ok = base_dir .. "battery_ok.svg", + battery_full = base_dir .. "battery_full.svg", + battery_step_four = base_dir .. "battery_step_four.svg", + battery_step_one = base_dir .. "battery_step_one.svg", + battery_step_three = base_dir .. "battery_step_three.svg", + battery_step_two = base_dir .. "battery_step_two.svg", + battery_unknown = base_dir .. "battery_unknown.svg" } } \ No newline at end of file diff --git a/theme/fly/init.lua b/theme/fly/init.lua index b1b48d9..6b7913f 100644 --- a/theme/fly/init.lua +++ b/theme/fly/init.lua @@ -27,6 +27,7 @@ local rule = require("theme.fly.module.rule") local geoinfo = require("theme.fly.module.geoinfo") local network_manager = require("theme.fly.module.network_manager") local wallpaper = require("theme.fly.module.24hours_wallpaper") +local battery_manager = require("theme.fly.module.battery_manager") beautiful.init(gears.filesystem.get_configuration_dir() .. "theme/fly/theme.lua") @@ -109,6 +110,9 @@ function fly:init(config) autostart:init({ apps = config.autostart }) + -- config battery manager + battery_manager:init({}) + -- initialize network manager network_manager:init({}) diff --git a/theme/fly/layout/topbar.lua b/theme/fly/layout/topbar.lua index c76dd2a..f17b1af 100644 --- a/theme/fly/layout/topbar.lua +++ b/theme/fly/layout/topbar.lua @@ -17,6 +17,7 @@ local systray = require("theme.fly.widget.systray") local layoutbox = require("theme.fly.widget.layoutbox") local hotkeys_popup = require("theme.fly.widget.hotkeys_popup") local networking = require("theme.fly.widget.networking") +local battery = require("theme.fly.widget.battery") -- import configurations local icons = require("theme.assets.icons") @@ -45,6 +46,7 @@ function topbar:init(config) layoutbox:init({ height = self.height }) hotkeys_popup:init({}) networking:init({ width = self.height * 0.7, height = self.height * 0.7 }) + battery:init({ width = self.height * 0.5 * (57 / 32), height = self.height * 0.5 }) local topbar_bg_clickable = "#cdd1d3cc" common.clickable(startup.widget, beautiful.startup_bg, beautiful.startup_bg .. "cc") @@ -67,6 +69,10 @@ function topbar:init(config) systray.widget, widget = wibox.container.place }, + wibox.widget { + battery.widget, + widget = wibox.container.place + }, wibox.widget { networking.widget, widget = wibox.container.place diff --git a/theme/fly/module/battery_manager.lua b/theme/fly/module/battery_manager.lua new file mode 100644 index 0000000..cceebe1 --- /dev/null +++ b/theme/fly/module/battery_manager.lua @@ -0,0 +1,105 @@ +local DBus = require("utils.dbus_wrapper.bus") +local Call = require("utils.dbus_wrapper.call") +local sig = require("theme.fly.signal") +local lgi = require("lgi") +local Gio = lgi.Gio +local GLib = lgi.GLib +local constants = require("utils.constants") + +local battery_manager = {} +local bus + +local function get_display_device() + local call = Call.new() + call.name = "org.freedesktop.UPower" + call.path = "/org/freedesktop/UPower" + call.interface = "org.freedesktop.UPower" + call.member = "GetDisplayDevice" + call.timeout = -1 + call.args = nil + local f, body = call(bus) + + if f then + return body[1] + else + return nil + end +end + +local function parse_device(path, display) + local call = Call.new() + call.name = "org.freedesktop.UPower" + call.path = path + call.interface = "org.freedesktop.DBus.Properties" + call.member = "GetAll" + call.timeout = -1 + call.args = GLib.Variant("(s)", { "org.freedesktop.UPower.Device" }) + local f, body = call(bus) + + local data = body.value[1] + local type = constants.BATTERY_DEVICE_OTHER + if display then + type = constants.BATTERY_DEVICE_DISPLAY + end + + if data["Type"] ~= constants.BATTERY_DEVICE_TYPE_LINE_POWER then + awesome.emit_signal(sig.battery_manager.update_device, { + type = type, + path = path, + device_type = data["Type"], + state = data["State"], + percentage = data["Percentage"], + model = data["Model"] + }) + end +end + +local function get_devices() + local call = Call.new() + call.name = "org.freedesktop.UPower" + call.path = "/org/freedesktop/UPower" + call.interface = "org.freedesktop.UPower" + call.member = "EnumerateDevices" + call.timeout = -1 + call.args = nil + local f, body = call(bus) + + local res = {} + if f then + for i = 1, #body[1] do + table.insert(res, body[1][i]) + end + end + return res +end + +local function device_added(path) + print("device: ", path) +end + +local function device_removed(path) end + +function battery_manager:init(config) + Gio.Async.call(function() + bus = DBus.new(Gio.BusType.SYSTEM) + + -- get display device + local display_device = get_display_device() + + parse_device(display_device, true) + + -- enumerate device + local devices = get_devices() + for i = 1, #devices do + parse_device(devices[i]) + end + + bus:signal_subscribe("org.freedesktop.UPower", "org.freedesktop.UPower", "DeviceAdded", + nil, nil, Gio.DBusSignalFlags.NONE, device_added) + + bus:signal_subscribe("org.freedesktop.UPower", "org.freedesktop.UPower", "DeviceRemoved", + nil, nil, Gio.DBusSignalFlags.NONE, device_removed) + end)() +end + +return battery_manager \ No newline at end of file diff --git a/theme/fly/signal.lua b/theme/fly/signal.lua index 2f97481..aa86fde 100644 --- a/theme/fly/signal.lua +++ b/theme/fly/signal.lua @@ -21,5 +21,8 @@ return { }, taglist = { update_icon = "taglist::update_icon" + }, + battery_manager = { + update_device = "battery::update__device" } } \ No newline at end of file diff --git a/theme/fly/widget/battery.lua b/theme/fly/widget/battery.lua index 2ed0b7b..28bf72c 100644 --- a/theme/fly/widget/battery.lua +++ b/theme/fly/widget/battery.lua @@ -1,7 +1,53 @@ +local icons = require("theme.assets.icons") +local wibox = require("wibox") +local awful = require("awful") +local sig = require("theme.fly.signal") +local constants = require("utils.constants") + local battery = {} function battery:init(config) + self.widget = wibox.widget { + image = icons.fly.battery_unknown, + resize = true, + forced_height = config.height, + forced_width = config.width, + widget = wibox.widget.imagebox + } + + self.panel = awful.popup { + widget = { + { + }, + widget = wibox.container.margin + } + } + awesome.connect_signal(sig.battery_manager.update_device, function(data) + if data.type == constants.BATTERY_DEVICE_DISPLAY then + if data.state == constants.BATTERY_DEVICE_STATE_UNKNOWN then + battery.widget:set_image(icons.fly.battery_unknown) + elseif data.state == constants.BATTERY_DEVICE_STATE_CHARGING then + battery.widet:set_image(icons.fly.battery_charging) + elseif data.state == constants.BATTERY_DEVICE_STATE_EMPTY then + battery.widget:set_image(icons.fly.battery_empty) + elseif data.state == constants.BATTERY_DEVICE_STATE_FULL_CHARGED then + battery.widget:set_image(icons.fly.battery_full) + else + local percentage = data.percentage + if percentage > 0 and percentage < 25 then + battery.widget:set_image(icons.fly.battery_step_one) + elseif percentage >= 25 and percentage < 50 then + battery.widget:set_image(icons.fly.battery_step_two) + elseif percentage >= 50 and percentage < 75 then + battery.widget:set_image(icons.fly.battery_step_three) + elseif percentage >= 75 and percentage < 100 then + battery.widget:set_image(icons.fly.battery_step_four) + end + end + else + end + end) end return battery \ No newline at end of file diff --git a/utils/constants.lua b/utils/constants.lua index 262a46a..9ee40dc 100644 --- a/utils/constants.lua +++ b/utils/constants.lua @@ -16,4 +16,15 @@ return { NM_ACTIVE_CONNECTION_STATE_ACTIVATED = 2, NM_ACTIVE_CONNECTION_STATE_DEACTIVATING = 3, NM_ACTIVE_CONNECTION_STATE_DEACTIVATED = 4, + BATTERY_DEVICE_DISPLAY = 0, + BATTERY_DEVICE_OTHER = 1, + BATTERY_DEVICE_STATE_UNKNOWN = 0, + BATTERY_DEVICE_STATE_CHARGING = 1, + BATTERY_DEVICE_STATE_DISCHARGING = 2, + BATTERY_DEVICE_STATE_EMPTY = 3, + BATTERY_DEVICE_STATE_FULL_CHARGED = 4, + BATTERY_DEVICE_STATE_PENDING_CHARGE = 5, + BATTERY_DEVICE_STATE_PENDING_DISCHARGING = 6, + BATTERY_DEVICE_TYPE_LINE_POWER = 1, + BATTERY_DEVICE_TYPE_BATTERY = 2 } \ No newline at end of file