wifi.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. --[[
  2. Awesome WM Wifi Widget
  3. Distopico Vegan <distopico [at] riseup [dot] net>
  4. Licensed under GPL3
  5. Original from: https://github.com/mrzapp/awesomerc
  6. --]]
  7. local wibox = require("wibox")
  8. local awful = require("awful")
  9. local gears = require("gears")
  10. local naughty = require("naughty")
  11. local helpers = require("widgets/helpers")
  12. local config = awful.util.getdir("config")
  13. local iwconfig = "iwconfig"
  14. local widget = {}
  15. local popup = nil
  16. local adapter = "wlan0"
  17. local iconpath = ""
  18. local binpaths = {'/usr/bin/', '/usr/sbin/', '/sbin/', '/bin/'}
  19. local networktext = "--"
  20. -- {{{ Define sub-widgets
  21. widget.text = wibox.widget.textbox()
  22. widget._icon = wibox.widget.imagebox()
  23. -- {{{ Define interactive behavior
  24. widget._icon:buttons(gears.table.join(
  25. awful.button({ }, 1, function ()
  26. awful.spawn("urxvt -e /usr/bin/nmtui", {
  27. floating = true,
  28. tag = mouse.screen.selected_tag,
  29. placement = awful.placement.centered,
  30. })
  31. end)
  32. ))
  33. -- }}}
  34. -- {{{ Check adapter method
  35. function widget:check()
  36. if not helpers:test(iwconfig) then
  37. for key, path in pairs(binpaths) do
  38. local cmd = path..iwconfig
  39. if helpers:test(cmd) then
  40. iwconfig = cmd
  41. break
  42. end
  43. end
  44. end
  45. -- Test adapter
  46. self.haswifi = helpers:test(iwconfig .. " " .. adapter)
  47. -- Try another adapter name
  48. if not self.haswifi then
  49. local netdata = helpers:run("ip addr")
  50. local interface = string.match(netdata, "%d: (w[^:]+)")
  51. if interface ~= nil then
  52. adapter = interface
  53. self.haswifi = helpers:test(iwconfig .. " " .. adapter)
  54. end
  55. end
  56. end
  57. -- }}}
  58. -- {{{ Update method
  59. function widget:update()
  60. local quality = 0
  61. local connected = ""
  62. local rate = ""
  63. spacer = " "
  64. if not self.haswifi then
  65. -- Check adapter
  66. self:check()
  67. end
  68. -- definitely has not
  69. if not self.haswifi then
  70. return
  71. end
  72. if not helpers:test("nmcli") then
  73. local wifi = helpers:run(iwconfig .. " " .. adapter)
  74. local wifiMin, wifiMax = string.match(wifi, "(%d?%d)/(%d?%d)")
  75. connected = string.match(wifi, "ESSID:\"(.*)\"")
  76. wifiMin = tonumber(wifiMin) or 0
  77. wifiMax = tonumber(wifiMax) or 70
  78. quality = math.floor(wifiMin / wifiMax * 100)
  79. else
  80. local wifi = helpers:run("nmcli -g IN-USE,SSID,RATE,SIGNAL,BARS,SECURITY device wifi")
  81. local data = string.match(wifi, "*:(.*)")
  82. local values = {}
  83. local i = 0;
  84. if data ~= nil then
  85. for val in string.gmatch(data, "([^:]+)") do
  86. values[i] = val
  87. i = i + 1
  88. end
  89. networktext = values[2]
  90. if tonumber(values[2]) ~= nil then
  91. connected = values[0]
  92. quality = math.floor(values[2] or 0)
  93. rate = " | " .. values[1] .. " | " .. values[3]
  94. end
  95. end
  96. end
  97. networktext = quality .. "%"
  98. if quality <= 0 then
  99. networktext = " no connected" .. " | " .. adapter
  100. elseif connected then
  101. networktext = networktext .. " | " .. connected .. rate
  102. end
  103. self.text:set_markup(networktext)
  104. iconpath = config.."/theme/icons/status/network-wireless-signal"
  105. if quality <= 0 then
  106. iconpath = iconpath .. "-none"
  107. elseif quality < 25 then
  108. iconpath = iconpath .. "-weak"
  109. elseif quality < 50 then
  110. iconpath = iconpath .. "-ok"
  111. elseif quality < 75 then
  112. iconpath = iconpath .. "-good"
  113. else
  114. iconpath = iconpath .. "-excellent"
  115. end
  116. iconpath = iconpath .. "-symbolic.svg"
  117. self._icon:set_image(iconpath)
  118. self.icon = helpers:set_draw_method(self._icon)
  119. end
  120. function widget:show()
  121. popup = naughty.notify({ icon = iconpath,
  122. icon_size = 16,
  123. text = networktext,
  124. timeout = 0, hover_timeout = 0.5,
  125. screen = mouse.screen,
  126. ignore_suspend = true
  127. })
  128. end
  129. function widget:hide()
  130. if popup ~= nil then
  131. naughty.destroy(popup)
  132. popup = nil
  133. end
  134. end
  135. -- }}}
  136. -- {{{ Listen if signal was found
  137. helpers:listen(widget, 30)
  138. widget._icon:connect_signal("mouse::enter", function() widget:show() end)
  139. widget._icon:connect_signal("mouse::leave", function() widget:hide() end)
  140. -- }}}
  141. return widget;