My configuration files for Debian/Ubuntu applications
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

148 行
4.7 KiB

  1. --[[
  2. Licensed under GNU General Public License v2
  3. * (c) 2018, Luca CPZ
  4. * (c) 2013, Conor Heine
  5. --]]
  6. local helpers = require("lain.helpers")
  7. local focused = require("awful.screen").focused
  8. local naughty = require("naughty")
  9. local wibox = require("wibox")
  10. local string = string
  11. local type = type
  12. -- ThinkPad battery infos and widget creator
  13. -- http://www.thinkwiki.org/wiki/Tp_smapi
  14. -- lain.widget.contrib.tp_smapi
  15. local function factory(apipath)
  16. local tp_smapi = {
  17. path = apipath or "/sys/devices/platform/smapi"
  18. }
  19. function tp_smapi.get(batid, feature)
  20. return helpers.first_line(string.format("%s/%s/%s", tp_smapi.path, batid or "BAT0", feature or ""))
  21. end
  22. function tp_smapi.installed(batid)
  23. return tp_smapi.get(batid, "installed") == "1"
  24. end
  25. function tp_smapi.status(batid)
  26. return tp_smapi.get(batid, "state")
  27. end
  28. function tp_smapi.percentage(batid)
  29. return tp_smapi.get(batid, "remaining_percent")
  30. end
  31. -- either running or charging time
  32. function tp_smapi.time(batid)
  33. local status = tp_smapi.status(batid)
  34. local mins_left = tp_smapi.get(batid, string.match(string.lower(status), "discharging") and "remaining_running_time" or "remaining_charging_time")
  35. if not string.find(mins_left, "^%d+") then return "N/A" end
  36. return string.format("%02d:%02d", math.floor(mins_left / 60), mins_left % 60) -- HH:mm
  37. end
  38. function tp_smapi.hide()
  39. if not tp_smapi.notification then return end
  40. naughty.destroy(tp_smapi.notification)
  41. tp_smapi.notification = nil
  42. end
  43. function tp_smapi.show(batid, seconds, scr)
  44. if not tp_smapi.installed(batid) then return end
  45. local mfgr = tp_smapi.get(batid, "manufacturer") or "no_mfgr"
  46. local model = tp_smapi.get(batid, "model") or "no_model"
  47. local chem = tp_smapi.get(batid, "chemistry") or "no_chem"
  48. local status = tp_smapi.get(batid, "state")
  49. local time = tp_smapi.time(batid)
  50. local msg
  51. if status and status ~= "idle" then
  52. msg = string.format("[%s] %s %s", status, time ~= "N/A" and time or "unknown remaining time",
  53. string.lower(status):gsub(" ", ""):gsub("\n", "") == "charging" and " until charged" or " remaining")
  54. else
  55. msg = "On AC power"
  56. end
  57. tp_smapi.hide()
  58. tp_smapi.notification = naughty.notify {
  59. title = string.format("%s: %s %s (%s)", batid, mfgr, model, chem),
  60. text = msg,
  61. timeout = type(seconds) == "number" and seconds or 0,
  62. screen = scr or focused()
  63. }
  64. end
  65. function tp_smapi.create_widget(args)
  66. args = args or {}
  67. local pspath = args.pspath or "/sys/class/power_supply/"
  68. local batteries = args.batteries or (args.battery and {args.battery}) or {}
  69. local timeout = args.timeout or 30
  70. local settings = args.settings or function() end
  71. if #batteries == 0 then
  72. helpers.line_callback("ls -1 " .. pspath, function(line)
  73. local bstr = string.match(line, "BAT%w+")
  74. if bstr then batteries[#batteries + 1] = bstr end
  75. end)
  76. end
  77. local all_batteries_installed = true
  78. for _, battery in ipairs(batteries) do
  79. if not tp_smapi.installed(battery) then
  80. naughty.notify {
  81. preset = naughty.config.critical,
  82. title = "tp_smapi: error while creating widget",
  83. text = string.format("battery %s is not installed", battery)
  84. }
  85. all_batteries_installed = false
  86. break
  87. end
  88. end
  89. if not all_batteries_installed then return end
  90. tpbat = {
  91. batteries = batteries,
  92. widget = args.widget or wibox.widget.textbox()
  93. }
  94. function tpbat.update()
  95. tpbat_now = {
  96. n_status = {},
  97. n_perc = {},
  98. n_time = {},
  99. status = "N/A"
  100. }
  101. for i = 1, #batteries do
  102. tpbat_now.n_status[i] = tp_smapi.status(batteries[i]) or "N/A"
  103. tpbat_now.n_perc[i] = tp_smapi.percentage(batteries[i])
  104. tpbat_now.n_time[i] = tp_smapi.time(batteries[i]) or "N/A"
  105. if not tpbat_now.n_status[i]:lower():match("full") then
  106. tpbat_now.status = tpbat_now.n_status[i]
  107. end
  108. end
  109. widget = tpbat.widget -- backwards compatibility
  110. settings()
  111. end
  112. helpers.newtimer("thinkpad-batteries", timeout, tpbat.update)
  113. return tpbat
  114. end
  115. return tp_smapi
  116. end
  117. return factory