-
Notifications
You must be signed in to change notification settings - Fork 41
/
wireless.lua
240 lines (206 loc) · 7.54 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
local wibox = require("wibox")
local awful = require("awful")
local beautiful = require("beautiful")
local naughty = require("naughty")
local gears = require("gears")
local cairo = require("lgi").cairo
local module_path = (...):match ("(.+/)[^/]+$") or ""
local theme = beautiful.get()
function dbg(message)
naughty.notify({ preset = naughty.config.presets.normal,
title = "debug",
text = message })
end
local function draw_signal(level)
-- draw 32x32 for simplicity, imagebox will resize it using loseless transform
local img = cairo.ImageSurface.create(cairo.Format.ARGB32, 32, 32)
local cr = cairo.Context(img)
cr:set_source(gears.color(theme.fg_normal))
if level > 75 then
cr:arc( 32/2, 32/2, 32/2, 145*math.pi/180, 395*math.pi/180)
cr:arc_negative(32/2, 32/2, 32/2-3, 395*math.pi/180, 145*math.pi/180)
end
if level > 50 then
cr:arc( 32/2, 32/2, 24/2, 145*math.pi/180, 395*math.pi/180)
cr:arc_negative(32/2, 32/2, 24/2-3, 395*math.pi/180, 145*math.pi/180)
end
if level > 25 then
cr:arc( 32/2, 32/2, 16/2, 145*math.pi/180, 395*math.pi/180)
cr:arc_negative(32/2, 32/2, 16/2-3, 395*math.pi/180, 145*math.pi/180)
end
cr:rectangle(32/2-1, 32/2-1, 2, 32/2-2)
cr:fill()
if level == 0 then
cr:set_source(gears.color("#cf5050"))
gears.shape.transform(gears.shape.cross)
:rotate(45*math.pi/180)
:translate(12, -10)(cr, 10, 10, 3)
end
cr:close_path()
cr:fill()
return img
end
function net_stats(card,which)
local prefix = {
[0] = "",
[1] = "K",
[2] = "M",
[3] = "G",
[4] = "T"
}
local function readAll(file)
local f = assert(io.open(file, "rb"))
local content = f:read()
f:close()
return content
end
local function round(num, numDecimalPlaces)
local mult = 10^(numDecimalPlaces or 0)
return math.floor(num * mult + 0.5) / mult
end
if (which == "d") then
f = readAll("/sys/class/net/" .. card .. "/statistics/rx_bytes")
else if (which == "u") then
f = readAll("/sys/class/net/" .. card .. "/statistics/tx_bytes")
end
end
local count = 0
local stat = tonumber(f)
while (stat > 1024) do
stat = (stat / 1024)
count = count + 1
end
result = (round(stat,2) .." "..prefix[count].."B")
return result
end
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 popup_metrics = args.popup_metrics or false
local net_icon = wibox.widget.imagebox(draw_signal(0))
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 )
if signal_level == nil then
connected = false
net_text:set_text(" N/A ")
net_icon:set_image(draw_signal(0))
else
connected = true
net_text:set_text(string.format("%"..indent.."d%%", signal_level))
net_icon:set_image(draw_signal(signal_level))
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
metrics_down = ""
metrics_up = ""
if popup_metrics then
local tdown = net_stats(interface,"d")
local tup = net_stats(interface,"u")
metrics_down = "├DOWN:\t\t"..tdown.."\n"
metrics_up = "├UP:\t\t"..tup.."\n"
end
msg =
"<span font_desc=\""..font.."\">"..
"┌["..interface.."]\n"..
"├ESSID:\t\t"..essid.."\n"..
"├IP:\t\t"..inet.."\n"..
"├BSSID\t\t"..mac.."\n"..
""..metrics_down..
""..metrics_up..
""..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})