mode-line-thermal.lisp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. ;;; mode-line-thermal.lisp --- Thermal zones info for the mode line
  2. ;; Copyright © 2007 Anonymous Coward, Jonathan Moore Liles
  3. ;; Copyright © 2019 Alex Kost <alezost@gmail.com>
  4. ;; This program is free software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;;
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This file originates from
  18. ;; <https://github.com/stumpwm/stumpwm-contrib/blob/master/modeline/cpu>.
  19. ;; Documentation on "/sys/class/thermal/*" can be found at:
  20. ;; <https://github.com/torvalds/linux/blob/master/Documentation/thermal/sysfs-api.txt>
  21. ;;; Code:
  22. (defpackage #:al/stumpwm-thermal
  23. (:use :common-lisp
  24. :stumpwm)
  25. (:export #:all-thermal-zones
  26. #:thermal-zones-mode-line-string))
  27. (in-package #:al/stumpwm-thermal)
  28. (defun all-thermal-zones ()
  29. "Return a list of files of all thermal zones."
  30. (remove nil
  31. (mapcar (lambda (dir)
  32. (when (ppcre:scan "thermal_zone" (namestring dir))
  33. (let ((file (make-pathname
  34. :directory (pathname-directory dir)
  35. :name "temp")))
  36. (and (al/file-readable? file)
  37. file))))
  38. (list-directory #P"/sys/class/thermal/"))))
  39. (defun thermal-zone-temperature (zone)
  40. "Return temperature of a thermal ZONE file."
  41. (round (/ (al/read-sys-file zone t)
  42. 1000)))
  43. (defun thermal-zones-mode-line-string (&rest zones)
  44. "Return a string with thermal ZONES info suitable for the mode-line."
  45. (if (null zones)
  46. ""
  47. (concat
  48. "^[^b^7*"
  49. (if (cdr zones) ; not a single zone
  50. (apply #'concat
  51. "°C:"
  52. (mapcar (lambda (zone)
  53. (format nil " ~D"
  54. (thermal-zone-temperature zone)))
  55. zones))
  56. (format nil "~D°C"
  57. (thermal-zone-temperature (car zones))))
  58. "^]")))
  59. ;;; mode-line-thermal.lisp ends here