You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

603 lines
16KB

  1. require 'cairo'
  2. netuphist = {}
  3. netdownhist = {}
  4. ramhist = {}
  5. swaphist = {}
  6. cpuhist = {}
  7. cur = 1
  8. -- SETTINGS
  9. hostname = conky_parse("$nodename")
  10. if hostname == "tablet" then
  11. screenWidth = 1920
  12. screenHeight = 1080
  13. nbCPU = 8
  14. FSs = {"/", "/var", "/home"}
  15. ladapter = "eth0"
  16. wadapter = "wlp58s0"
  17. ntop = 10
  18. hwmon = 7
  19. sensor = 1
  20. elseif hostname == "hnv-laptop" then
  21. screenWidth = 1920
  22. screenHeight = 1080
  23. nbCPU = 8
  24. FSs = {"/", "/var", "/home"}
  25. ladapter = "eth0"
  26. wadapter = "wlp58s0"
  27. ntop = 10
  28. hwmon = 7
  29. sensor = 1
  30. elseif hostname == "home" then
  31. screenWidth = 1920
  32. screenHeight = 1080
  33. nbCPU = 12
  34. FSs = {"/", "/home", "/data", "/archives"}
  35. ladapter = "eno1"
  36. wadapter = "wlan0"
  37. ntop = 5
  38. hwmon = 2
  39. sensor = 2
  40. elseif hostname == "hegp-laptop" then
  41. screenWidth = 1920
  42. screenHeight = 1080
  43. nbCPU = 8
  44. FSs = {"/", "/var", "/home"}
  45. ladapter = "eth0"
  46. wadapter = "wlo1"
  47. ntop = 10
  48. hwmon = 7
  49. sensor = 2
  50. elseif hostname == "hegp" then
  51. screenWidth = 3840
  52. screenHeight = 1600
  53. nbCPU = 48
  54. FSs = {"/", "/var", "/home", "/data"}
  55. ladapter = "enp0s31f6"
  56. wadapter = "wlan0"
  57. ntop = 10
  58. hwmon = 2
  59. sensor = 2
  60. end
  61. -- SETTINGS
  62. function conky_init()
  63. local cr, cs = nil
  64. if conky_window == nil then return end
  65. if cs == nil then -- or cairo_xlib_surface_get_width(cs) ~= conky_window.width or cairo_xlib_surface_get_height(cs) ~= conky_window.height then
  66. if cs then cairo_surface_destroy(cs) end
  67. cs = cairo_xlib_surface_create(conky_window.display, conky_window.drawable, conky_window.visual, screenWidth, screenHeight)
  68. end
  69. if cr then cairo_destroy(cr) end
  70. cr = cairo_create(cs)
  71. local items = 30 + nbCPU + #FSs + ntop
  72. if hostname == "hegp" then
  73. defaultSize = screenHeight / items - 1
  74. else
  75. defaultSize = screenHeight / items
  76. end
  77. height = defaultSize + 1
  78. small = defaultSize * .8
  79. blue = {.4,.6,.8,.5}
  80. cur = cur % 100 + 1
  81. local ypos = 60
  82. local xpos = 25
  83. local margin = 50
  84. ypos = general(cr, xpos, ypos) + margin
  85. ypos = fs (cr, xpos, ypos) + margin
  86. ypos = ram (cr, xpos, ypos) + margin
  87. ypos = cpu (cr, xpos, ypos) + margin
  88. ypos = top (cr, xpos, ypos, ntop) + margin
  89. ypos = network(cr, xpos, ypos) + margin
  90. if hostname == "home" then
  91. clock(cr, screenWidth / 2, screenHeight / 2 - 170)
  92. weather(cr, screenWidth / 2 - 11.5*.6*15, screenHeight / 2 - 60)
  93. else
  94. clock(cr, screenWidth - 260, 170)
  95. weather(cr, screenWidth - 420, 290)
  96. calendar(cr, screenWidth - 475, 440)
  97. agenda(cr, screenWidth - 480, 620)
  98. end
  99. cairo_destroy(cr)
  100. cairo_surface_destroy(cs)
  101. end
  102. function clock(cr, x, y)
  103. emboss(cr, x, y - 100, conky_parse("${time %H}"), 1, 200)
  104. emboss(cr, x, y - 50, conky_parse(":${time %M}"), 0, 150)
  105. end
  106. function calendar(cr, x, y)
  107. size = 18
  108. yt = 0
  109. for line in io.lines("/tmp/cal.txt") do
  110. yt = yt + size
  111. xt = 0
  112. for substr in string.gmatch(line, "[^]+") do
  113. if substr:match("%[7m") then
  114. color = {.45, .65, .9, 1}
  115. else
  116. color = nil
  117. end
  118. substr = substr:gsub("%[.?7m", "")
  119. emboss(cr, x + xt, y + yt, substr, 0, size, nil, color, "Fira Code")
  120. xt = xt + .6*size * utf8.len(substr)
  121. end
  122. end
  123. end
  124. function agenda(cr, x, y)
  125. size = 14
  126. yt = 0
  127. for line in io.lines("/tmp/agenda_export.txt") do
  128. yt = yt + size
  129. xt = 0
  130. for substr in string.gmatch(line, "[^]+") do
  131. color = tonumber(substr:match("%[38;5;(%d+)m"))
  132. if color ~= nil then
  133. if color <= 231 then
  134. color = color - 16
  135. b = color % 36 % 6
  136. color = color - b
  137. g = color % 36 / 6
  138. color = color - g * 6
  139. r = color / 36
  140. color = {r * .2, g * .2, b * .2, .5}
  141. else
  142. a = (color - 232) / 24
  143. color = {a, a, a, .5}
  144. end
  145. end
  146. substr = substr:gsub("%[[^m]+m", "")
  147. emboss(cr, x + xt, y + yt, substr, 0, size, nil, color, "Fira Code")
  148. xt = xt + .6*size * utf8.len(substr)
  149. end
  150. end
  151. end
  152. function weather(cr, x, y)
  153. size = 20
  154. yt = 0
  155. for line in io.lines("/tmp/meteo") do
  156. yt = yt + size
  157. xt = 0
  158. for substr in string.gmatch(line, "[^]+") do
  159. color = tonumber(substr:match("%[38;5;(%d+)m"))
  160. if color ~= nil then
  161. if color <= 231 then
  162. color = color - 16
  163. b = color % 36 % 6
  164. color = color - b
  165. g = color % 36 / 6
  166. color = color - g * 6
  167. r = color / 36
  168. color = {r * .2, g * .2, b * .2, .5}
  169. else
  170. a = (color - 232) / 24
  171. color = {a, a, a, .5}
  172. end
  173. end
  174. substr = substr:gsub("%[[^m]+m", "")
  175. emboss(cr, x + xt, y + yt, substr, 0, size, nil, color, "Fira Code")
  176. xt = xt + .6*size * utf8.len(substr)
  177. end
  178. end
  179. end
  180. function general(cr, x, y)
  181. cadre(cr, x, y, 450, 3 * height, 10)
  182. emboss(cr , x , y, "Kernel")
  183. y = emboss(cr, x + 450, y, conky_parse("$kernel") , 1)
  184. emboss(cr , x , y, "Uptime")
  185. y = emboss(cr, x + 450, y, conky_parse("$uptime") , 1)
  186. emboss(cr , x , y, "Load")
  187. y = emboss(cr, x + 450, y, conky_parse("$loadavg"), 1)
  188. return y
  189. end
  190. function ram(cr, x, y)
  191. if hostname == "home" or hostname == "hegp" then
  192. cadre(cr, x, y, 450, 2 * height, 10)
  193. else
  194. cadre(cr, x, y, 450, 10 + 4 * height, 10)
  195. if #swaphist == 0 then
  196. local i
  197. for i = 1,100 do
  198. swaphist[i] =.001
  199. end
  200. end
  201. swaphist[cur] = tonumber(conky_parse("$swapperc"))
  202. end
  203. if #ramhist == 0 then
  204. local i
  205. for i = 1,100 do
  206. ramhist[i] =.001
  207. end
  208. end
  209. ramhist[cur] = tonumber(conky_parse("$memperc"))
  210. emboss(cr, x , y, "Ram")
  211. emboss(cr, x + 180, y, conky_parse("$mem/") , 1)
  212. y = emboss(cr, x + 260, y, conky_parse("$memmax"), 1)
  213. bar(cr, x + 5, y, 255, conky_parse("$memperc"), blue)
  214. graph(cr, x + 280, y, 170, ramhist, blue, 100)
  215. if hostname ~= "home" and hostname ~= "hegp" then
  216. y = y + height + 10
  217. emboss(cr, x , y, "Swap")
  218. emboss(cr, x + 180, y, conky_parse("$swap/") , 1)
  219. y = emboss(cr, x + 260, y, conky_parse("$swapmax"), 1)
  220. bar(cr, x + 5, y, 255, conky_parse("$swapperc"), blue)
  221. graph(cr, x + 280, y, 170, swaphist, blue, 100)
  222. end
  223. y = y + height
  224. return y
  225. end
  226. function cpu(cr, x, y)
  227. cadre(cr, x, y, 450, 10 + (1 + nbCPU) * height, 10)
  228. if #cpuhist == 0 then
  229. local i, j
  230. for i = 1,nbCPU do
  231. cpuhist[i] = {}
  232. for j = 1,100 do
  233. cpuhist[i][j] = .001
  234. end
  235. end
  236. end
  237. emboss(cr , x , y, "Cpu")
  238. emboss(cr , x + 260, y, conky_parse("$freq_g GHz") , 1)
  239. y = emboss(cr, x + 450, y, conky_parse("Temp ${hwmon "..hwmon.." temp "..sensor.."}°C"), 1) + 10
  240. local cpu
  241. for cpu=1,nbCPU do
  242. bar(cr, x + 5, y, 255, conky_parse("${cpu cpu" .. cpu .. "}"), blue)
  243. cpuhist[cpu][cur] = tonumber(conky_parse("${cpu cpu" .. cpu .. "}"))
  244. graph(cr, x + 280, y, 170, cpuhist[cpu], blue, 100)
  245. y = y + height
  246. end
  247. return y
  248. end
  249. function network(cr, x, y)
  250. cadre(cr, x, y, 450, 4 * height + 10, 10)
  251. local upspd = conky_parse("${upspeedf " .. wadapter .. "}") + conky_parse("${upspeedf " .. ladapter .. "}")
  252. local downspd = conky_parse("${downspeedf " .. wadapter .. "}") + conky_parse("${downspeedf " .. ladapter .. "}")
  253. local upspdunit = "KiB/s"
  254. local downspdunit = "KiB/s"
  255. if upspd > 1024 then
  256. upspd = upspd / 1024
  257. upspdunit = "MiB/s"
  258. end
  259. if downspd > 1024 then
  260. downspd = downspd / 1024
  261. downspdunit = "MiB/s"
  262. end
  263. local ethup = string.match(conky_parse("${totalup " .. ladapter .. "}"),"[%d.]+")
  264. local ethupunit = string.match(conky_parse("${totalup " .. ladapter .. "}"),"%a")
  265. if ethupunit == "K" then
  266. ethup = ethup * 1024
  267. elseif ethupunit == "M" then
  268. ethup = ethup * 1024 * 1024
  269. elseif ethupunit == "G" then
  270. ethup = ethup * 1024 * 1024 * 1024
  271. end
  272. local ethdown = string.match(conky_parse("${totaldown " .. ladapter .. "}"),"[%d.]+")
  273. local ethdownunit = string.match(conky_parse("${totaldown " .. ladapter .. "}"),"%a")
  274. if ethdownunit == "K" then
  275. ethdown = ethdown * 1024
  276. elseif ethdownunit == "M" then
  277. ethdown = ethdown * 1024 * 1024
  278. elseif ethdownunit == "G" then
  279. ethdown = ethdown * 1024 * 1024 * 1024
  280. end
  281. local wlanup = string.match(conky_parse("${totalup " .. wadapter .. "}"),"[%d.]+")
  282. local wlanupunit = string.match(conky_parse("${totalup " .. wadapter .. "}"),"%a")
  283. if wlanupunit == "K" then
  284. wlanup = wlanup * 1024
  285. elseif wlanupunit == "M" then
  286. wlanup = wlanup * 1024 * 1024
  287. elseif wlanupunit == "G" then
  288. wlanup = wlanup * 1024 * 1024 * 1024
  289. end
  290. local wlandown = string.match(conky_parse("${totaldown " .. wadapter .. "}"),"[%d.]+")
  291. local wlandownunit = string.match(conky_parse("${totaldown " .. wadapter .. "}"),"%a")
  292. if wlandownunit == "K" then
  293. wlandown = wlandown * 1024
  294. elseif wlandownunit == "M" then
  295. wlandown = wlandown * 1024 * 1024
  296. elseif wlandownunit == "G" then
  297. wlandown = wlandown * 1024 * 1024 * 1024
  298. end
  299. local totalup = ethup + wlanup
  300. local upunit = "B"
  301. if totalup > (1024 * 1024 * 1024) then
  302. totalup = totalup / (1024 * 1024 * 1024)
  303. upunit = "GiB"
  304. elseif totalup > (1024 * 1024) then
  305. totalup = totalup / (1024 * 1024)
  306. upunit = "MiB"
  307. elseif totalup > 1024 then
  308. totalup = totalup / 1024
  309. upunit = "KiB"
  310. end
  311. local totaldown = ethdown + wlandown
  312. local downunit = "B"
  313. if totaldown > (1024 * 1024 * 1024) then
  314. totaldown = totaldown / (1024 * 1024 * 1024)
  315. downunit = "GiB"
  316. elseif totaldown > (1024 * 1024) then
  317. totaldown = totaldown / (1024 * 1024)
  318. downunit = "MiB"
  319. elseif totaldown > 1024 then
  320. totaldown = totaldown / 1024
  321. downunit = "KiB"
  322. end
  323. if #netuphist == 0 then
  324. local i
  325. for i = 1,100 do
  326. netuphist[i] = .001
  327. end
  328. end
  329. if #netdownhist == 0 then
  330. local i
  331. for i = 1,100 do
  332. netdownhist[i] = .001
  333. end
  334. end
  335. netuphist[cur] = tonumber(conky_parse("${upspeedf " .. wadapter .. "}") + conky_parse("${upspeedf " .. ladapter .. "}"))
  336. netdownhist[cur] = tonumber(conky_parse("${downspeedf " .. wadapter .. "}") + conky_parse("${downspeedf " .. ladapter .. "}"))
  337. maxspeed = max({max(netuphist), max(netdownhist)})
  338. if maxspeed == 0 then
  339. maxspeed = 1
  340. end
  341. emboss(cr, x , y, "Up")
  342. emboss(cr, x + 260, y, string.format("%.1f",upspd) .. upspdunit , 1)
  343. graph(cr, x + 280, y, 170, netuphist, blue, maxspeed)
  344. y = y + height
  345. emboss(cr, x , y, "Down")
  346. emboss(cr, x + 260, y, string.format("%.1f",downspd) .. downspdunit, 1)
  347. graph(cr, x + 280, y, 170, netdownhist, blue, maxspeed, 1)
  348. y = y + height + 10
  349. emboss(cr , x , y, "Total up")
  350. y = emboss(cr, x+260, y, string.format("%.2f",totalup) .. upunit , 1)
  351. emboss(cr , x , y, "Total down")
  352. y = emboss(cr, x+260, y, string.format("%.2f",totaldown) .. downunit , 1)
  353. return y
  354. end
  355. function fs(cr, x, y)
  356. cadre(cr, x, y, 450, height * #FSs, 10)
  357. for row=1,#FSs do
  358. emboss(cr, x , y , FSs[row])
  359. emboss(cr, x + 180, y , conky_parse("${fs_used " .. FSs[row] .. "}/"), 1)
  360. emboss(cr, x + 260, y , conky_parse("${fs_size " .. FSs[row] .. "}") , 1)
  361. bar(cr, x + 280, y, 170, conky_parse("${fs_used_perc " .. FSs[row] .. "}"), blue)
  362. y = y + height
  363. end
  364. return y
  365. end
  366. function top(cr, x, y, nrows)
  367. cadre(cr, x, y, 450, height + 10 + nrows * small, 10)
  368. emboss(cr , x , y, "Name")
  369. emboss(cr , x + 220, y, "Cpu", 1)
  370. emboss(cr , x + 230, y, "Name")
  371. y = emboss(cr, x + 450, y, "Ram", 1) + 10
  372. local row
  373. for row=1,nrows do
  374. emboss(cr , x , y, conky_parse("${top name " .. row .. "}") , 0, small)
  375. emboss(cr , x + 220, y, conky_parse("${top cpu " .. row .. "}") , 1, small)
  376. emboss(cr , x + 230, y, conky_parse("${top_mem name " .. row .. "}"), 0, small)
  377. y = emboss(cr, x + 450, y, conky_parse("${top_mem mem " .. row .. "}") , 1, small)
  378. end
  379. return y
  380. end
  381. function cadre(cr, x, y, w, h, r, pop, col)
  382. if pop == nil then pop = 1 end
  383. local pi = 3.141592
  384. if pop < 0 then y = y + pop end
  385. cairo_set_operator(cr, CAIRO_OPERATOR_XOR)
  386. cairo_move_to(cr, x, y - r)
  387. cairo_arc(cr, x + w, y , r, 1.5 * pi, 0 * pi)
  388. cairo_arc(cr, x + w, y + h, r, 0 * pi, .5 * pi)
  389. cairo_arc(cr, x , y + h, r, .5 * pi, 1 * pi)
  390. cairo_arc(cr, x , y , r, 1 * pi, 1.5 * pi)
  391. cairo_set_source_rgb(cr, 1, 1, 1)
  392. cairo_fill(cr)
  393. y = y - pop
  394. cairo_move_to(cr, x, y - r)
  395. cairo_arc(cr, x + w, y , r,1.5 * pi, 0 * pi)
  396. cairo_arc(cr, x + w, y + h, r,0 * pi, .5 * pi)
  397. cairo_arc(cr, x , y + h, r,.5 * pi, 1 * pi)
  398. cairo_arc(cr, x , y , r,1 * pi, 1.5 * pi)
  399. cairo_set_source_rgb(cr, 0, 0, 0)
  400. cairo_fill(cr)
  401. cairo_set_operator(cr, CAIRO_OPERATOR_OVER)
  402. cairo_move_to(cr, x, y - r)
  403. cairo_arc(cr, x + w, y , r, 1.5 * pi, 0 * pi)
  404. cairo_arc(cr, x + w, y + h, r, 0 * pi, .5 * pi)
  405. cairo_arc(cr, x , y + h, r, .5 * pi, 1 * pi)
  406. cairo_arc(cr, x , y , r, 1 * pi, 1.5 * pi)
  407. if pop > 0 then cairo_set_source_rgba(cr, 0, 0, 0, .1) else cairo_set_source_rgba(cr, 1, 1, 1, .1) end
  408. cairo_fill_preserve(cr)
  409. if col ~= nil then cairo_set_source_rgba(cr, col[1], col[2], col[3], col[4]) else cairo_set_source_rgba(cr, 0, 0, 0, 0)end
  410. cairo_fill(cr)
  411. end
  412. function bar(cr, x, y, width, percent, color)
  413. local barh = height / 3
  414. emboss(cr, x + width, y + 3, percent .. "%", 1, small)
  415. width = width - 45
  416. cadre(cr, x, y + barh + 4, width , barh - 4, 4)
  417. cadre(cr, x, y + barh + 4, width * percent / 100, barh - 4, 2, -1, color)
  418. end
  419. function emboss(cr, x, y, text, right, size, pop, col, font)
  420. if pop == nil then pop = 1 end
  421. if size == nil then size = defaultSize end
  422. if right == nil then right = 0 end
  423. if font == nil then font = "Impact" end
  424. y = y + size
  425. cairo_select_font_face (cr, font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
  426. cairo_set_font_size (cr, size);
  427. if right == 1 then
  428. extent = cairo_text_extents_t:create()
  429. tolua.takeownership(extent)
  430. cairo_text_extents(cr, text, extent)
  431. x = x - extent.x_advance
  432. -- cairo_text_extents_t:destroy(extent)
  433. end
  434. if pop < 0 then y = y + pop end
  435. cairo_set_operator(cr, CAIRO_OPERATOR_XOR)
  436. cairo_move_to(cr, x, y)
  437. cairo_text_path(cr, text)
  438. cairo_set_source_rgb(cr, 1, 1, 1)
  439. cairo_fill(cr)
  440. cairo_move_to(cr,x,y - pop)
  441. cairo_text_path(cr, text)
  442. cairo_set_source_rgb(cr, 0, 0, 0)
  443. cairo_fill(cr)
  444. cairo_set_operator(cr, CAIRO_OPERATOR_OVER)
  445. cairo_move_to(cr, x, y)
  446. cairo_text_path(cr, text)
  447. if pop > 0 then cairo_set_source_rgba(cr, 0, 0, 0, .1) else cairo_set_source_rgba(cr, 1, 1, 1, .1) end
  448. cairo_fill_preserve(cr)
  449. if col ~= nil then cairo_set_source_rgba(cr, col[1], col[2], col[3], col[4]) else cairo_set_source_rgba(cr, 0, 0, 0, 0) end
  450. cairo_fill(cr)
  451. return y
  452. end
  453. function graph(cr, x, y, width, hist, color, maximum, reverse)
  454. if maximum == nil then maximum = max(hist) end
  455. if reverse == nil then reverse = 0 end
  456. x = x - 2
  457. width = width + 2
  458. local scalev = (height - 2) / maximum
  459. if reverse == 1 then
  460. scalev = -scalev
  461. y = y + 1
  462. else
  463. y = y + height - 1
  464. end
  465. local scaleh = width / 100
  466. cairo_move_to(cr, x + scaleh, y)
  467. local i
  468. for i = cur+1,100 do
  469. cairo_line_to(cr, x + (i - cur) * scaleh, y - hist[i] * scalev)
  470. end
  471. for i = 1,cur do
  472. cairo_line_to(cr, x + ((100 - cur) + i) * scaleh, y - hist[i] * scalev)
  473. end
  474. cairo_line_to(cr, x + 100 * scaleh, y)
  475. cairo_close_path(cr)
  476. cairo_set_source_rgb(cr, 1, 1, 1)
  477. cairo_fill(cr)
  478. y = y - 2
  479. cairo_move_to(cr, x + scaleh, y)
  480. for i = cur+1,100 do
  481. cairo_line_to(cr, x + (i - cur) * scaleh, y - hist[i] * scalev)
  482. end
  483. for i = 1,cur do
  484. cairo_line_to(cr, x + ((100 - cur) + i) * scaleh, y - hist[i] * scalev)
  485. end
  486. cairo_line_to(cr, x + 100 * scaleh, y)
  487. cairo_close_path(cr)
  488. cairo_set_source_rgb(cr, 0, 0, 0)
  489. cairo_fill(cr)
  490. y = y + 1
  491. cairo_move_to(cr, x + scaleh, y)
  492. for i = cur+1,100 do
  493. cairo_line_to(cr, x + (i - cur) * scaleh, y - hist[i] * scalev)
  494. end
  495. for i = 1,cur do
  496. cairo_line_to(cr, x + ((100 - cur) + i) * scaleh, y - hist[i] * scalev)
  497. end
  498. cairo_line_to(cr, x + 100 * scaleh, y)
  499. cairo_close_path(cr)
  500. cairo_set_source_rgba(cr, color[1], color[2], color[3], 1)
  501. cairo_fill(cr)
  502. end
  503. function max(hist)
  504. local i
  505. local max = 0
  506. for i=1,#hist do
  507. if hist[i] > max then max = hist[i] end
  508. end
  509. return max
  510. end