]> 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:

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