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

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