forked from pltanton/net_widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwireless.lua
155 lines (135 loc) · 5 KB
/
wireless.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
local wibox = require("wibox")
local awful = require("awful")
local beautiful = require("beautiful")
local naughty = require("naughty")
local gears = require("gears")
local module_path = (...):match ("(.+/)[^/]+$") or ""
local wireless = {}
local function worker(args)
local args = args or {}
widgets_table = {}
local connected = false
-- Settings
local ICON_DIR = awful.util.getdir("config").."/"..module_path.."/net_widgets/icons/"
local interface = args.interface or "wlan0"
local timeout = args.timeout or 5
local font = args.font or beautiful.font
local popup_signal = args.popup_signal or false
local popup_position = args.popup_position or naughty.config.defaults.position
local onclick = args.onclick
local widget = args.widget == nil and wibox.layout.fixed.horizontal() or args.widget == false and nil or args.widget
local indent = args.indent or 3
local net_icon = wibox.widget.imagebox()
net_icon:set_image(ICON_DIR.."wireless_na.png")
local net_text = wibox.widget.textbox()
net_text.font = font
net_text:set_text(" N/A ")
local signal_level = 0
local function net_update()
awful.spawn.easy_async("awk 'NR==3 {printf \"%3.0f\" ,($3/70)*100}' /proc/net/wireless", function(stdout, stderr, reason, exit_code)
signal_level = tonumber( stdout )
end)
if signal_level == nil then
connected = false
net_text:set_text(" N/A ")
net_icon:set_image(ICON_DIR.."wireless_na.png")
else
connected = true
net_text:set_text(string.format("%"..indent.."d%%", signal_level))
if signal_level < 25 then
net_icon:set_image(ICON_DIR.."wireless_0.png")
elseif signal_level < 50 then
net_icon:set_image(ICON_DIR.."wireless_1.png")
elseif signal_level < 75 then
net_icon:set_image(ICON_DIR.."wireless_2.png")
else
net_icon:set_image(ICON_DIR.."wireless_3.png")
end
end
end
net_update()
local timer = gears.timer.start_new( timeout, function () net_update()
return true end )
widgets_table["imagebox"] = net_icon
widgets_table["textbox"] = net_text
if widget then
widget:add(net_icon)
-- Hide the text when we want to popup the signal instead
if not popup_signal then
widget:add(net_text)
end
wireless:attach(widget,{onclick = onclick})
end
local function text_grabber()
local msg = ""
if connected then
local mac = "N/A"
local essid = "N/A"
local bitrate = "N/A"
local inet = "N/A"
-- Use iw/ip
f = io.popen("iw dev "..interface.." link")
for line in f:lines() do
-- Connected to 00:01:8e:11:45:ac (on wlp1s0)
mac = string.match(line, "Connected to ([0-f:]+)") or mac
-- SSID: 00018E1145AC
essid = string.match(line, "SSID: (.+)") or essid
-- tx bitrate: 36.0 MBit/s
bitrate = string.match(line, "tx bitrate: (.+/s)") or bitrate
end
f:close()
f = io.popen("ip addr show "..interface)
for line in f:lines() do
inet = string.match(line, "inet (%d+%.%d+%.%d+%.%d+)") or inet
end
f:close()
signal = ""
if popup_signal then
signal = "├Strength\t"..signal_level.."\n"
end
msg =
"<span font_desc=\""..font.."\">"..
"┌["..interface.."]\n"..
"├ESSID:\t\t"..essid.."\n"..
"├IP:\t\t"..inet.."\n"..
"├BSSID\t\t"..mac.."\n"..
""..signal..
"└Bit rate:\t"..bitrate.."</span>"
else
msg = "Wireless network is disconnected"
end
return msg
end
local notification = nil
function wireless:hide()
if notification ~= nil then
naughty.destroy(notification)
notification = nil
end
end
function wireless:show(t_out)
wireless:hide()
notification = naughty.notify({
preset = fs_notification_preset,
text = text_grabber(),
timeout = t_out,
screen = mouse.screen,
position = popup_position
})
end
return widget or widgets_table
end
function wireless:attach(widget, args)
local args = args or {}
local onclick = args.onclick
-- Bind onclick event function
if onclick then
widget:buttons(awful.util.table.join(
awful.button({}, 1, function() awful.util.spawn(onclick) end)
))
end
widget:connect_signal('mouse::enter', function () wireless:show(0) end)
widget:connect_signal('mouse::leave', function () wireless:hide() end)
return widget
end
return setmetatable(wireless, {__call = function(_,...) return worker(...) end})