]> git.madduck.net Git - etc/awesome.git/blobdiff - widget/bat.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Merge pull request #409 from seregaxvm/gap
[etc/awesome.git] / widget / bat.lua
index 8e04569128b6b6131de342f1f805fa14f4ae7fb6..b65494811286e4a5e8087ef413e16e698e506ba7 100644 (file)
@@ -1,20 +1,17 @@
 --[[
 
         Licensed under GNU General Public License v2
 --[[
 
         Licensed under GNU General Public License v2
-         * (c) 2013,      Luke Bonham
+         * (c) 2013,      Luca CPZ
          * (c) 2010-2012, Peter Hofmann
 
 --]]
 
          * (c) 2010-2012, Peter Hofmann
 
 --]]
 
-local first_line = require("lain.helpers").first_line
-local newtimer   = require("lain.helpers").newtimer
+local helpers    = require("lain.helpers")
+local fs         = require("gears.filesystem")
 local naughty    = require("naughty")
 local wibox      = require("wibox")
 local naughty    = require("naughty")
 local wibox      = require("wibox")
-local math       = { abs    = math.abs,
-                     floor  = math.floor,
-                     log10  = math.log10,
-                     min    = math.min }
-local string     = { format = string.format }
+local math       = math
+local string     = string
 local ipairs     = ipairs
 local tonumber   = tonumber
 
 local ipairs     = ipairs
 local tonumber   = tonumber
 
@@ -22,15 +19,35 @@ local tonumber   = tonumber
 -- lain.widget.bat
 
 local function factory(args)
 -- lain.widget.bat
 
 local function factory(args)
+    local pspath = args.pspath or "/sys/class/power_supply/"
+
+    if not fs.is_dir(pspath) then
+        naughty.notify { text = "lain.widget.bat: invalid power supply path", timeout = 0 }
+        return
+    end
+
     local bat       = { widget = wibox.widget.textbox() }
     local args      = args or {}
     local timeout   = args.timeout or 30
     local bat       = { widget = wibox.widget.textbox() }
     local args      = args or {}
     local timeout   = args.timeout or 30
-    local batteries = args.batteries or (args.battery and {args.battery}) or {"BAT0"}
-    local ac        = args.ac or "AC0"
     local notify    = args.notify or "on"
     local n_perc    = args.n_perc or { 5, 15 }
     local notify    = args.notify or "on"
     local n_perc    = args.n_perc or { 5, 15 }
+    local batteries = args.batteries or (args.battery and {args.battery}) or {}
+    local ac        = args.ac or "AC0"
     local settings  = args.settings or function() end
 
     local settings  = args.settings or function() end
 
+    function bat.get_batteries()
+        helpers.line_callback("ls -1 " .. pspath, function(line)
+            local bstr =  string.match(line, "BAT%w+")
+            if bstr then
+                batteries[#batteries + 1] = bstr
+            else
+                ac = string.match(line, "A%w+") or "AC0"
+            end
+        end)
+    end
+
+    if #batteries == 0 then bat.get_batteries() end
+
     bat_notification_critical_preset = {
         title   = "Battery exhausted",
         text    = "Shutdown imminent",
     bat_notification_critical_preset = {
         title   = "Battery exhausted",
         text    = "Shutdown imminent",
@@ -47,6 +64,14 @@ local function factory(args)
         bg      = "#CDCDCD"
     }
 
         bg      = "#CDCDCD"
     }
 
+    bat_notification_charged_preset = {
+        title   = "Battery full",
+        text    = "You can unplug the cable",
+        timeout = 15,
+        fg      = "#202020",
+        bg      = "#CDCDCD"
+    }
+
     bat_now = {
         status    = "N/A",
         ac_status = "N/A",
     bat_now = {
         status    = "N/A",
         ac_status = "N/A",
@@ -62,6 +87,9 @@ local function factory(args)
         bat_now.n_perc[i] = 0
     end
 
         bat_now.n_perc[i] = 0
     end
 
+    -- used to notify full charge only once before discharging
+    local fullnotification = false
+
     function bat.update()
         local sum_rate_current = 0
         local sum_rate_voltage = 0
     function bat.update()
         local sum_rate_current = 0
         local sum_rate_voltage = 0
@@ -69,30 +97,29 @@ local function factory(args)
         local sum_rate_energy  = 0
         local sum_energy_now   = 0
         local sum_energy_full  = 0
         local sum_rate_energy  = 0
         local sum_energy_now   = 0
         local sum_energy_full  = 0
-        local pspath           = "/sys/class/power_supply/"
 
         for i, battery in ipairs(batteries) do
             local bstr    = pspath .. battery
 
         for i, battery in ipairs(batteries) do
             local bstr    = pspath .. battery
-            local present = first_line(bstr .. "/present")
+            local present = helpers.first_line(bstr .. "/present")
 
             if tonumber(present) == 1 then
                 -- current_now(I)[uA], voltage_now(U)[uV], power_now(P)[uW]
 
             if tonumber(present) == 1 then
                 -- current_now(I)[uA], voltage_now(U)[uV], power_now(P)[uW]
-                local rate_current = tonumber(first_line(bstr .. "/current_now"))
-                local rate_voltage = tonumber(first_line(bstr .. "/voltage_now"))
-                local rate_power   = tonumber(first_line(bstr .. "/power_now"))
+                local rate_current = tonumber(helpers.first_line(bstr .. "/current_now"))
+                local rate_voltage = tonumber(helpers.first_line(bstr .. "/voltage_now"))
+                local rate_power   = tonumber(helpers.first_line(bstr .. "/power_now"))
 
                 -- energy_now(P)[uWh], charge_now(I)[uAh]
 
                 -- energy_now(P)[uWh], charge_now(I)[uAh]
-                local energy_now        = tonumber(first_line(bstr .. "/energy_now") or
-                                          first_line(bstr .. "/charge_now"))
+                local energy_now = tonumber(helpers.first_line(bstr .. "/energy_now") or
+                                   helpers.first_line(bstr .. "/charge_now"))
 
                 -- energy_full(P)[uWh], charge_full(I)[uAh]
 
                 -- energy_full(P)[uWh], charge_full(I)[uAh]
-                local energy_full       = tonumber(first_line(bstr .. "/energy_full") or
-                                          first_line(bstr .. "/charge_full"))
+                local energy_full = tonumber(helpers.first_line(bstr .. "/energy_full") or
+                                    helpers.first_line(bstr .. "/charge_full"))
 
 
-                local energy_percentage = tonumber(first_line(bstr .. "/capacity")) or
+                local energy_percentage = tonumber(helpers.first_line(bstr .. "/capacity")) or
                                           math.floor((energy_now / energy_full) * 100)
 
                                           math.floor((energy_now / energy_full) * 100)
 
-                bat_now.n_status[i] = first_line(bstr .. "/status") or "N/A"
+                bat_now.n_status[i] = helpers.first_line(bstr .. "/status") or "N/A"
                 bat_now.n_perc[i]   = energy_percentage or bat_now.n_perc[i]
 
                 sum_rate_current = sum_rate_current + (rate_current or 0)
                 bat_now.n_perc[i]   = energy_percentage or bat_now.n_perc[i]
 
                 sum_rate_current = sum_rate_current + (rate_current or 0)
@@ -114,7 +141,7 @@ local function factory(args)
                 bat_now.status = status
             end
         end
                 bat_now.status = status
             end
         end
-        bat_now.ac_status = tonumber(first_line(string.format("%s%s/online", pspath, ac))) or "N/A"
+        bat_now.ac_status = tonumber(helpers.first_line(string.format("%s%s/online", pspath, ac))) or "N/A"
 
         if bat_now.status ~= "N/A" then
             if bat_now.status ~= "Full" and sum_rate_power == 0 and bat_now.ac_status == 1 then
 
         if bat_now.status ~= "N/A" then
             if bat_now.status ~= "Full" and sum_rate_power == 0 and bat_now.ac_status == 1 then
@@ -156,23 +183,32 @@ local function factory(args)
         widget = bat.widget
         settings()
 
         widget = bat.widget
         settings()
 
-        -- notifications for critical and low levels
-        if notify == "on" and bat_now.status == "Discharging" then
-            if tonumber(bat_now.perc) <= n_perc[1] then
-                bat.id = naughty.notify({
-                    preset = bat_notification_critical_preset,
-                    replaces_id = bat.id
-                }).id
-            elseif tonumber(bat_now.perc) <= n_perc[2] then
+        -- notifications for critical, low, and full levels
+        if notify == "on" then
+            if bat_now.status == "Discharging" then
+                if tonumber(bat_now.perc) <= n_perc[1] then
+                    bat.id = naughty.notify({
+                        preset = bat_notification_critical_preset,
+                        replaces_id = bat.id
+                    }).id
+                elseif tonumber(bat_now.perc) <= n_perc[2] then
+                    bat.id = naughty.notify({
+                        preset = bat_notification_low_preset,
+                        replaces_id = bat.id
+                    }).id
+                end
+                fullnotification = false
+            elseif bat_now.status == "Full" and not fullnotification then
                 bat.id = naughty.notify({
                 bat.id = naughty.notify({
-                    preset = bat_notification_low_preset,
+                    preset = bat_notification_charged_preset,
                     replaces_id = bat.id
                 }).id
                     replaces_id = bat.id
                 }).id
+                fullnotification = true
             end
         end
     end
 
             end
         end
     end
 
-    newtimer("batteries", timeout, bat.update)
+    helpers.newtimer("batteries", timeout, bat.update)
 
     return bat
 end
 
     return bat
 end