local upower = require('lgi').require('UPowerGlib') local gtable = require 'gears.table' local wbase = require 'wibox.widget.base' local setmetatable = setmetatable -- luacheck: ignore setmetatable local screen = screen -- luacheck: ignore screen local battery_widget = {} local mt = {} --- Helper to get the path of all connected power devices. -- @treturn table The list of all power devices path. function battery_widget.list_devices() local ret = {} local devices = upower.Client():get_devices() for _,d in ipairs(devices) do table.insert(ret, d:get_object_path()) end return ret end --- Helper function to get a device instance from its path. -- @tparam string path The path of the device to get. -- @treturn UPowerGlib.Device|nil The device if it was found, `nil` otherwise. function battery_widget.get_device(path) local devices = upower.Client():get_devices() for _,d in ipairs(devices) do if d:get_object_path() == path then return d end end return nil end --- Helper function to easily get the default BAT0 device path without. -- @treturn string The BAT0 device path. function battery_widget.get_BAT0_device_path() local bat0_path = '/org/freedesktop/UPower/devices/battery_BAT0' return bat0_path end --- Helper function to convert seconds into a human readable clock string. -- -- This translates the given seconds parameter into a human readable string -- following the notation `HH:MM` (where HH is the number of hours and MM the -- number of minutes). -- @tparam number seconds The umber of seconds to translate. -- @treturn string The human readable generated clock string. function battery_widget.to_clock(seconds) if seconds <= 0 then return '00:00'; else local hours = string.format('%02.f', math.floor(seconds/3600)); local mins = string.format('%02.f', math.floor(seconds/60 - hours*60)); return hours .. ':' .. mins end end --- Gives the default widget to use if user didn't specify one. -- The default widget used is an `empty_widget` instance. -- @treturn widget The default widget to use. local function default_template () return wbase.empty_widget() end --- battery_widget constructor. -- -- This function creates a new `battery_widget` instance. This widget watches -- the `display_device` status and report. -- @tparam table args The arguments table. -- @tparam[opt=1] screen|number args.screen the widget's screen. -- @tparam[opt] widget args.widget_template The widget template to use to -- create the widget instance. -- @tparam[opt] function args.create_callback User defined callback for the -- widget initialization. -- @tparam[opt] string args.device_path Path of the device to monitor. -- @tparam[opt=false] boolean args.use_display_device Should the widget monitor -- the _display device_? -- @treturn battery_widget The battery_widget instance build. function battery_widget.new (args) args = gtable.crush({ widget_template = default_template(), create_callback = nil, device_path = '', use_display_device = false }, args or {}) args.screen = screen[args.screen or 1] local widget = wbase.make_widget_from_value(args.widget_template) local device = args.use_display_device and upower.Client():get_display_device() or battery_widget.get_device(args.device_path) if type(args.create_callback) == 'function' then args.create_callback(widget, device) end -- Attach signals: device.on_notify = function (d) widget:emit_signal('upower::update', d) end return widget end function mt.__call(self, ...) return battery_widget.new(...) end return setmetatable(battery_widget, mt)