Skip to content
This repository has been archived by the owner on Dec 25, 2023. It is now read-only.

Commit

Permalink
add battery widget and module
Browse files Browse the repository at this point in the history
  • Loading branch information
RMTT committed Sep 28, 2020
1 parent bb4d81f commit 324e428
Show file tree
Hide file tree
Showing 17 changed files with 204 additions and 0 deletions.
1 change: 1 addition & 0 deletions theme/assets/icons/battery_charging.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions theme/assets/icons/battery_empty.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions theme/assets/icons/battery_error.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions theme/assets/icons/battery_full.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions theme/assets/icons/battery_ok.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions theme/assets/icons/battery_step_four.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions theme/assets/icons/battery_step_one.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions theme/assets/icons/battery_step_three.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions theme/assets/icons/battery_step_two.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions theme/assets/icons/battery_unknown.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions theme/assets/icons/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,35 @@ 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",
networking_wireless_connected = base_dir .. "networking_wireless_connected.svg",
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"
}
}
4 changes: 4 additions & 0 deletions theme/fly/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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({})

Expand Down
6 changes: 6 additions & 0 deletions theme/fly/layout/topbar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand All @@ -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
Expand Down
105 changes: 105 additions & 0 deletions theme/fly/module/battery_manager.lua
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions theme/fly/signal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@ return {
},
taglist = {
update_icon = "taglist::update_icon"
},
battery_manager = {
update_device = "battery::update__device"
}
}
46 changes: 46 additions & 0 deletions theme/fly/widget/battery.lua
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions utils/constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 324e428

Please sign in to comment.