]> git.madduck.net Git - etc/awesome.git/blob - widgets/net.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:

#103: revert to original fs implementation
[etc/awesome.git] / widgets / net.lua
1
2 --[[
3                                                   
4      Licensed under GNU General Public License v2 
5       * (c) 2013,      Luke Bonham                
6       * (c) 2010-2012, Peter Hofmann              
7                                                   
8 --]]
9
10 local helpers      = require("lain.helpers")
11
12 local notify_fg    = require("beautiful").fg_focus
13 local naughty      = require("naughty")
14 local wibox        = require("wibox")
15
16 local io           = { popen  = io.popen }
17 local tostring     = tostring
18 local string       = { format = string.format,
19                        gsub   = string.gsub,
20                        match  = string.match }
21
22 local setmetatable = setmetatable
23
24 -- Network infos
25 -- lain.widgets.net
26 local net = {
27     last_t = 0,
28     last_r = 0
29 }
30
31 function net.get_device()
32     f = io.popen("ip link show | cut -d' ' -f2,9")
33     ws = f:read("*a")
34     f:close()
35     ws = ws:match("%w+: UP") or ws:match("ppp%w+: UNKNOWN")
36     if ws ~= nil then
37         return ws:match("(%w+):")
38     else
39         return "network off"
40     end
41 end
42
43 local function worker(args)
44     local args = args or {}
45     local timeout = args.timeout or 2
46     local units = args.units or 1024 --kb
47     local notify = args.notify or "on"
48     local screen = args.screen or 1
49     local settings = args.settings or function() end
50
51     iface = args.iface or net.get_device()
52
53     net.widget = wibox.widget.textbox('')
54
55     helpers.set_map(iface, true)
56
57     function update()
58         net_now = {}
59
60         if iface == "" or string.match(iface, "network off")
61         then
62             iface = net.get_device()
63         end
64
65         net_now.carrier = helpers.first_line('/sys/class/net/' .. iface ..
66                                            '/carrier') or "0"
67         net_now.state = helpers.first_line('/sys/class/net/' .. iface ..
68                                            '/operstate') or "down"
69         local now_t = helpers.first_line('/sys/class/net/' .. iface ..
70                                            '/statistics/tx_bytes') or 0
71         local now_r = helpers.first_line('/sys/class/net/' .. iface ..
72                                            '/statistics/rx_bytes') or 0
73
74         net_now.sent = tostring((now_t - net.last_t) / timeout / units)
75         net_now.sent = string.gsub(string.format('%.1f', net_now.sent), ",", ".")
76
77         net_now.received = tostring((now_r - net.last_r) / timeout / units)
78         net_now.received = string.gsub(string.format('%.1f', net_now.received), ",", ".")
79
80         widget = net.widget
81         settings()
82
83         net.last_t = now_t
84         net.last_r = now_r
85
86         if net_now.carrier ~= "1" and notify == "on"
87         then
88             if helpers.get_map(iface)
89             then
90                 naughty.notify({
91                     title    = iface,
92                     text     = "no carrier",
93                     timeout  = 7,
94                     position = "top_left",
95                     icon     = helpers.icons_dir .. "no_net.png",
96                     fg       = notify_fg or "#FFFFFF",
97                     screen   = screen
98                 })
99                 helpers.set_map(iface, false)
100             end
101         else
102             helpers.set_map(iface, true)
103         end
104     end
105
106     helpers.newtimer(iface, timeout, update)
107     return net.widget
108 end
109
110 return setmetatable(net, { __call = function(_, ...) return worker(...) end })