battery.el 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. ;;; battery.el --- display battery status information -*- coding: iso-8859-1 -*-
  2. ;; Copyright (C) 1997-1998, 2000-2012 Free Software Foundation, Inc.
  3. ;; Author: Ralph Schleicher <rs@nunatak.allgaeu.org>
  4. ;; Keywords: hardware
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; There is at present support for GNU/Linux, OS X and Windows. This
  18. ;; library supports both the `/proc/apm' file format of Linux version
  19. ;; 1.3.58 or newer and the `/proc/acpi/' directory structure of Linux
  20. ;; 2.4.20 and 2.6. Darwin (OS X) is supported by using the `pmset'
  21. ;; program. Windows is supported by the GetSystemPowerStatus API call.
  22. ;;; Code:
  23. (require 'timer)
  24. (eval-when-compile (require 'cl))
  25. (defgroup battery nil
  26. "Display battery status information."
  27. :prefix "battery-"
  28. :group 'hardware)
  29. (defcustom battery-status-function
  30. (cond ((and (eq system-type 'gnu/linux)
  31. (file-readable-p "/proc/apm"))
  32. 'battery-linux-proc-apm)
  33. ((and (eq system-type 'gnu/linux)
  34. (file-directory-p "/proc/acpi/battery"))
  35. 'battery-linux-proc-acpi)
  36. ((and (eq system-type 'gnu/linux)
  37. (file-directory-p "/sys/class/power_supply/")
  38. (directory-files "/sys/class/power_supply/" nil "BAT[0-9]$"))
  39. 'battery-linux-sysfs)
  40. ((and (eq system-type 'gnu/linux)
  41. (file-directory-p "/sys/class/power_supply/yeeloong-bat/")
  42. (directory-files "/sys/class/power_supply/yeeloong-bat/" nil "charge_"))
  43. 'battery-yeeloong-sysfs)
  44. ((and (eq system-type 'darwin)
  45. (condition-case nil
  46. (with-temp-buffer
  47. (and (eq (call-process "pmset" nil t nil "-g" "ps") 0)
  48. (> (buffer-size) 0)))
  49. (error nil)))
  50. 'battery-pmset)
  51. ((eq system-type 'windows-nt)
  52. 'w32-battery-status))
  53. "Function for getting battery status information.
  54. The function has to return an alist of conversion definitions.
  55. Its cons cells are of the form
  56. (CONVERSION . REPLACEMENT-TEXT)
  57. CONVERSION is the character code of a \"conversion specification\"
  58. introduced by a `%' character in a control string."
  59. :type '(choice (const nil) function)
  60. :group 'battery)
  61. (defcustom battery-echo-area-format
  62. (cond ((eq battery-status-function 'battery-linux-proc-acpi)
  63. "Power %L, battery %B at %r (%p%% load, remaining time %t)")
  64. ((eq battery-status-function 'battery-linux-sysfs)
  65. "Power %L, battery %B (%p%% load, remaining time %t)")
  66. ((eq battery-status-function 'battery-pmset)
  67. "%L power, battery %B (%p%% load, remaining time %t)")
  68. ((eq battery-status-function 'battery-yeeloong-sysfs)
  69. "%L power, battery %B (%p%% load, remaining time %t)")
  70. (battery-status-function
  71. "Power %L, battery %B (%p%% load, remaining time %t)"))
  72. "Control string formatting the string to display in the echo area.
  73. Ordinary characters in the control string are printed as-is, while
  74. conversion specifications introduced by a `%' character in the control
  75. string are substituted as defined by the current value of the variable
  76. `battery-status-function'. Here are the ones generally available:
  77. %c Current capacity (mAh or mWh)
  78. %r Current rate of charge or discharge
  79. %B Battery status (verbose)
  80. %b Battery status: empty means high, `-' means low,
  81. `!' means critical, and `+' means charging
  82. %d Temperature (in degrees Celsius)
  83. %L AC line status (verbose)
  84. %p Battery load percentage
  85. %m Remaining time (to charge or discharge) in minutes
  86. %h Remaining time (to charge or discharge) in hours
  87. %t Remaining time (to charge or discharge) in the form `h:min'"
  88. :type '(choice string (const nil))
  89. :group 'battery)
  90. (defvar battery-mode-line-string nil
  91. "String to display in the mode line.")
  92. ;;;###autoload (put 'battery-mode-line-string 'risky-local-variable t)
  93. (defcustom battery-mode-line-limit 100
  94. "Percentage of full battery load below which display battery status"
  95. :version "24.1"
  96. :type 'integer
  97. :group 'battery)
  98. (defcustom battery-mode-line-format
  99. (cond ((eq battery-status-function 'battery-linux-proc-acpi)
  100. "[%b%p%%,%d°C]")
  101. (battery-status-function
  102. "[%b%p%%]"))
  103. "Control string formatting the string to display in the mode line.
  104. Ordinary characters in the control string are printed as-is, while
  105. conversion specifications introduced by a `%' character in the control
  106. string are substituted as defined by the current value of the variable
  107. `battery-status-function'. Here are the ones generally available:
  108. %c Current capacity (mAh or mWh)
  109. %r Current rate of charge or discharge
  110. %B Battery status (verbose)
  111. %b Battery status: empty means high, `-' means low,
  112. `!' means critical, and `+' means charging
  113. %d Temperature (in degrees Celsius)
  114. %L AC line status (verbose)
  115. %p Battery load percentage
  116. %m Remaining time (to charge or discharge) in minutes
  117. %h Remaining time (to charge or discharge) in hours
  118. %t Remaining time (to charge or discharge) in the form `h:min'"
  119. :type '(choice string (const nil))
  120. :group 'battery)
  121. (defcustom battery-update-interval 60
  122. "Seconds after which the battery status will be updated."
  123. :type 'integer
  124. :group 'battery)
  125. (defcustom battery-load-low 25
  126. "Upper bound of low battery load percentage.
  127. A battery load percentage below this number is considered low."
  128. :type 'integer
  129. :group 'battery)
  130. (defcustom battery-load-critical 10
  131. "Upper bound of critical battery load percentage.
  132. A battery load percentage below this number is considered critical."
  133. :type 'integer
  134. :group 'battery)
  135. (defvar battery-update-timer nil
  136. "Interval timer object.")
  137. ;;;###autoload
  138. (defun battery ()
  139. "Display battery status information in the echo area.
  140. The text being displayed in the echo area is controlled by the variables
  141. `battery-echo-area-format' and `battery-status-function'."
  142. (interactive)
  143. (message "%s" (if (and battery-echo-area-format battery-status-function)
  144. (battery-format battery-echo-area-format
  145. (funcall battery-status-function))
  146. "Battery status not available")))
  147. ;;;###autoload
  148. (define-minor-mode display-battery-mode
  149. "Toggle battery status display in mode line (Display Battery mode).
  150. With a prefix argument ARG, enable Display Battery mode if ARG is
  151. positive, and disable it otherwise. If called from Lisp, enable
  152. the mode if ARG is omitted or nil.
  153. The text displayed in the mode line is controlled by
  154. `battery-mode-line-format' and `battery-status-function'.
  155. The mode line is be updated every `battery-update-interval'
  156. seconds."
  157. :global t :group 'battery
  158. (setq battery-mode-line-string "")
  159. (or global-mode-string (setq global-mode-string '("")))
  160. (and battery-update-timer (cancel-timer battery-update-timer))
  161. (if (and battery-status-function battery-mode-line-format)
  162. (if (not display-battery-mode)
  163. (setq global-mode-string
  164. (delq 'battery-mode-line-string global-mode-string))
  165. (add-to-list 'global-mode-string 'battery-mode-line-string t)
  166. (setq battery-update-timer (run-at-time nil battery-update-interval
  167. 'battery-update-handler))
  168. (battery-update))
  169. (message "Battery status not available")
  170. (setq display-battery-mode nil)))
  171. (defun battery-update-handler ()
  172. (battery-update)
  173. (sit-for 0))
  174. (defun battery-update ()
  175. "Update battery status information in the mode line."
  176. (let ((data (and battery-status-function (funcall battery-status-function))))
  177. (setq battery-mode-line-string
  178. (propertize (if (and battery-mode-line-format
  179. (<= (car (read-from-string (cdr (assq ?p data))))
  180. battery-mode-line-limit))
  181. (battery-format
  182. battery-mode-line-format
  183. data)
  184. "")
  185. 'face
  186. (and (<= (car (read-from-string (cdr (assq ?p data))))
  187. battery-load-critical)
  188. 'error)
  189. 'help-echo "Battery status information")))
  190. (force-mode-line-update))
  191. ;;; `/proc/apm' interface for Linux.
  192. (defconst battery-linux-proc-apm-regexp
  193. (concat "^\\([^ ]+\\)" ; Driver version.
  194. " \\([^ ]+\\)" ; APM BIOS version.
  195. " 0x\\([0-9a-f]+\\)" ; APM BIOS flags.
  196. " 0x\\([0-9a-f]+\\)" ; AC line status.
  197. " 0x\\([0-9a-f]+\\)" ; Battery status.
  198. " 0x\\([0-9a-f]+\\)" ; Battery flags.
  199. " \\(-?[0-9]+\\)%" ; Load percentage.
  200. " \\(-?[0-9]+\\)" ; Remaining time.
  201. " \\(.*\\)" ; Time unit.
  202. "$")
  203. "Regular expression matching contents of `/proc/apm'.")
  204. (defun battery-linux-proc-apm ()
  205. "Get APM status information from Linux (the kernel).
  206. This function works only with the new `/proc/apm' format introduced
  207. in Linux version 1.3.58.
  208. The following %-sequences are provided:
  209. %v Linux driver version
  210. %V APM BIOS version
  211. %I APM BIOS status (verbose)
  212. %L AC line status (verbose)
  213. %B Battery status (verbose)
  214. %b Battery status, empty means high, `-' means low,
  215. `!' means critical, and `+' means charging
  216. %p Battery load percentage
  217. %s Remaining time (to charge or discharge) in seconds
  218. %m Remaining time (to charge or discharge) in minutes
  219. %h Remaining time (to charge or discharge) in hours
  220. %t Remaining time (to charge or discharge) in the form `h:min'"
  221. (let (driver-version bios-version bios-interface line-status
  222. battery-status battery-status-symbol load-percentage
  223. seconds minutes hours remaining-time tem)
  224. (with-temp-buffer
  225. (ignore-errors (insert-file-contents "/proc/apm"))
  226. (when (re-search-forward battery-linux-proc-apm-regexp)
  227. (setq driver-version (match-string 1))
  228. (setq bios-version (match-string 2))
  229. (setq tem (string-to-number (match-string 3) 16))
  230. (if (not (logand tem 2))
  231. (setq bios-interface "not supported")
  232. (setq bios-interface "enabled")
  233. (cond ((logand tem 16) (setq bios-interface "disabled"))
  234. ((logand tem 32) (setq bios-interface "disengaged")))
  235. (setq tem (string-to-number (match-string 4) 16))
  236. (cond ((= tem 0) (setq line-status "off-line"))
  237. ((= tem 1) (setq line-status "on-line"))
  238. ((= tem 2) (setq line-status "on backup")))
  239. (setq tem (string-to-number (match-string 6) 16))
  240. (if (= tem 255)
  241. (setq battery-status "N/A")
  242. (setq tem (string-to-number (match-string 5) 16))
  243. (cond ((= tem 0) (setq battery-status "high"
  244. battery-status-symbol ""))
  245. ((= tem 1) (setq battery-status "low"
  246. battery-status-symbol "-"))
  247. ((= tem 2) (setq battery-status "critical"
  248. battery-status-symbol "!"))
  249. ((= tem 3) (setq battery-status "charging"
  250. battery-status-symbol "+")))
  251. (setq load-percentage (match-string 7))
  252. (setq seconds (string-to-number (match-string 8)))
  253. (and (string-equal (match-string 9) "min")
  254. (setq seconds (* 60 seconds)))
  255. (setq minutes (/ seconds 60)
  256. hours (/ seconds 3600))
  257. (setq remaining-time
  258. (format "%d:%02d" hours (- minutes (* 60 hours))))))))
  259. (list (cons ?v (or driver-version "N/A"))
  260. (cons ?V (or bios-version "N/A"))
  261. (cons ?I (or bios-interface "N/A"))
  262. (cons ?L (or line-status "N/A"))
  263. (cons ?B (or battery-status "N/A"))
  264. (cons ?b (or battery-status-symbol ""))
  265. (cons ?p (or load-percentage "N/A"))
  266. (cons ?s (or (and seconds (number-to-string seconds)) "N/A"))
  267. (cons ?m (or (and minutes (number-to-string minutes)) "N/A"))
  268. (cons ?h (or (and hours (number-to-string hours)) "N/A"))
  269. (cons ?t (or remaining-time "N/A")))))
  270. ;;; `/proc/acpi/' interface for Linux.
  271. (defun battery-linux-proc-acpi ()
  272. "Get ACPI status information from Linux (the kernel).
  273. This function works only with the `/proc/acpi/' format introduced
  274. in Linux version 2.4.20 and 2.6.0.
  275. The following %-sequences are provided:
  276. %c Current capacity (mAh)
  277. %r Current rate
  278. %B Battery status (verbose)
  279. %b Battery status, empty means high, `-' means low,
  280. `!' means critical, and `+' means charging
  281. %d Temperature (in degrees Celsius)
  282. %L AC line status (verbose)
  283. %p Battery load percentage
  284. %m Remaining time (to charge or discharge) in minutes
  285. %h Remaining time (to charge or discharge) in hours
  286. %t Remaining time (to charge or discharge) in the form `h:min'"
  287. (let ((design-capacity 0)
  288. (last-full-capacity 0)
  289. full-capacity
  290. (warn 0)
  291. (low 0)
  292. capacity rate rate-type charging-state minutes hours)
  293. ;; ACPI provides information about each battery present in the system in
  294. ;; a separate subdirectory. We are going to merge the available
  295. ;; information together since displaying for a variable amount of
  296. ;; batteries seems overkill for format-strings.
  297. (with-temp-buffer
  298. (dolist (dir (ignore-errors (directory-files "/proc/acpi/battery/"
  299. t "\\`[^.]")))
  300. (erase-buffer)
  301. (ignore-errors (insert-file-contents (expand-file-name "state" dir)))
  302. (when (re-search-forward "present: +yes$" nil t)
  303. (and (re-search-forward "charging state: +\\(.*\\)$" nil t)
  304. (member charging-state '("unknown" "charged" nil))
  305. ;; On most multi-battery systems, most of the time only one
  306. ;; battery is "charging"/"discharging", the others are
  307. ;; "unknown".
  308. (setq charging-state (match-string 1)))
  309. (when (re-search-forward "present rate: +\\([0-9]+\\) \\(m[AW]\\)$"
  310. nil t)
  311. (setq rate (+ (or rate 0) (string-to-number (match-string 1)))
  312. rate-type (or (and rate-type
  313. (if (string= rate-type (match-string 2))
  314. rate-type
  315. (error
  316. "Inconsistent rate types (%s vs. %s)"
  317. rate-type (match-string 2))))
  318. (match-string 2))))
  319. (when (re-search-forward "remaining capacity: +\\([0-9]+\\) m[AW]h$"
  320. nil t)
  321. (setq capacity
  322. (+ (or capacity 0) (string-to-number (match-string 1))))))
  323. (goto-char (point-max))
  324. (ignore-errors (insert-file-contents (expand-file-name "info" dir)))
  325. (when (re-search-forward "present: +yes$" nil t)
  326. (when (re-search-forward "design capacity: +\\([0-9]+\\) m[AW]h$"
  327. nil t)
  328. (incf design-capacity (string-to-number (match-string 1))))
  329. (when (re-search-forward "last full capacity: +\\([0-9]+\\) m[AW]h$"
  330. nil t)
  331. (incf last-full-capacity (string-to-number (match-string 1))))
  332. (when (re-search-forward
  333. "design capacity warning: +\\([0-9]+\\) m[AW]h$" nil t)
  334. (incf warn (string-to-number (match-string 1))))
  335. (when (re-search-forward "design capacity low: +\\([0-9]+\\) m[AW]h$"
  336. nil t)
  337. (incf low (string-to-number (match-string 1)))))))
  338. (setq full-capacity (if (> last-full-capacity 0)
  339. last-full-capacity design-capacity))
  340. (and capacity rate
  341. (setq minutes (if (zerop rate) 0
  342. (floor (* (/ (float (if (string= charging-state
  343. "charging")
  344. (- full-capacity capacity)
  345. capacity))
  346. rate)
  347. 60)))
  348. hours (/ minutes 60)))
  349. (list (cons ?c (or (and capacity (number-to-string capacity)) "N/A"))
  350. (cons ?L (or (battery-search-for-one-match-in-files
  351. (mapcar (lambda (e) (concat e "/state"))
  352. (ignore-errors
  353. (directory-files "/proc/acpi/ac_adapter/"
  354. t "\\`[^.]")))
  355. "state: +\\(.*\\)$" 1)
  356. "N/A"))
  357. (cons ?d (or (battery-search-for-one-match-in-files
  358. (mapcar (lambda (e) (concat e "/temperature"))
  359. (ignore-errors
  360. (directory-files "/proc/acpi/thermal_zone/"
  361. t "\\`[^.]")))
  362. "temperature: +\\([0-9]+\\) C$" 1)
  363. "N/A"))
  364. (cons ?r (or (and rate (concat (number-to-string rate) " "
  365. rate-type)) "N/A"))
  366. (cons ?B (or charging-state "N/A"))
  367. (cons ?b (or (and (string= charging-state "charging") "+")
  368. (and capacity (< capacity low) "!")
  369. (and capacity (< capacity warn) "-")
  370. ""))
  371. (cons ?h (or (and hours (number-to-string hours)) "N/A"))
  372. (cons ?m (or (and minutes (number-to-string minutes)) "N/A"))
  373. (cons ?t (or (and minutes
  374. (format "%d:%02d" hours (- minutes (* 60 hours))))
  375. "N/A"))
  376. (cons ?p (or (and full-capacity capacity
  377. (> full-capacity 0)
  378. (number-to-string
  379. (floor (/ capacity
  380. (/ (float full-capacity) 100)))))
  381. "N/A")))))
  382. ;;; `/sys/class/power_supply/BATN' interface for Linux.
  383. (defun battery-linux-sysfs ()
  384. "Get ACPI status information from Linux kernel.
  385. This function works only with the new `/sys/class/power_supply/'
  386. format introduced in Linux version 2.4.25.
  387. The following %-sequences are provided:
  388. %c Current capacity (mAh or mWh)
  389. %r Current rate
  390. %B Battery status (verbose)
  391. %d Temperature (in degrees Celsius)
  392. %p Battery load percentage
  393. %L AC line status (verbose)
  394. %m Remaining time (to charge or discharge) in minutes
  395. %h Remaining time (to charge or discharge) in hours
  396. %t Remaining time (to charge or discharge) in the form `h:min'"
  397. (let (charging-state rate temperature hours
  398. (charge-full 0.0)
  399. (charge-now 0.0)
  400. (energy-full 0.0)
  401. (energy-now 0.0))
  402. ;; SysFS provides information about each battery present in the
  403. ;; system in a separate subdirectory. We are going to merge the
  404. ;; available information together.
  405. (with-temp-buffer
  406. (dolist (dir (ignore-errors
  407. (directory-files
  408. "/sys/class/power_supply/" t "BAT[0-9]$")))
  409. (erase-buffer)
  410. (ignore-errors (insert-file-contents
  411. (expand-file-name "uevent" dir)))
  412. (when (re-search-forward "POWER_SUPPLY_PRESENT=1$" nil t)
  413. (goto-char (point-min))
  414. (and (re-search-forward "POWER_SUPPLY_STATUS=\\(.*\\)$" nil t)
  415. (member charging-state '("Unknown" "Full" nil))
  416. (setq charging-state (match-string 1)))
  417. (when (re-search-forward
  418. "POWER_SUPPLY_\\(CURRENT\\|POWER\\)_NOW=\\([0-9]*\\)$"
  419. nil t)
  420. (setq rate (float (string-to-number (match-string 2)))))
  421. (when (re-search-forward "POWER_SUPPLY_TEMP=\\([0-9]*\\)$" nil t)
  422. (setq temperature (match-string 1)))
  423. (let (full-string now-string)
  424. ;; Sysfs may list either charge (mAh) or energy (mWh).
  425. ;; Keep track of both, and choose which to report later.
  426. (cond ((and (re-search-forward
  427. "POWER_SUPPLY_CHARGE_FULL=\\([0-9]*\\)$" nil t)
  428. (setq full-string (match-string 1))
  429. (re-search-forward
  430. "POWER_SUPPLY_CHARGE_NOW=\\([0-9]*\\)$" nil t)
  431. (setq now-string (match-string 1)))
  432. (setq charge-full (+ charge-full
  433. (string-to-number full-string))
  434. charge-now (+ charge-now
  435. (string-to-number now-string))))
  436. ((and (re-search-forward
  437. "POWER_SUPPLY_ENERGY_FULL=\\([0-9]*\\)$" nil t)
  438. (setq full-string (match-string 1))
  439. (re-search-forward
  440. "POWER_SUPPLY_ENERGY_NOW=\\([0-9]*\\)$" nil t)
  441. (setq now-string (match-string 1)))
  442. (setq energy-full (+ energy-full
  443. (string-to-number full-string))
  444. energy-now (+ energy-now
  445. (string-to-number now-string))))))
  446. (goto-char (point-min))
  447. (when (and energy-now rate (not (zerop rate))
  448. (re-search-forward
  449. "POWER_SUPPLY_VOLTAGE_NOW=\\([0-9]*\\)$" nil t))
  450. (let ((remaining (if (string= charging-state "Discharging")
  451. energy-now
  452. (- energy-full energy-now))))
  453. (setq hours (/ (/ (* remaining (string-to-number
  454. (match-string 1)))
  455. rate)
  456. 10000000.0)))))))
  457. (list (cons ?c (cond ((or (> charge-full 0) (> charge-now 0))
  458. (number-to-string charge-now))
  459. ((or (> energy-full 0) (> energy-now 0))
  460. (number-to-string energy-now))
  461. (t "N/A")))
  462. (cons ?r (if rate (format "%.1f" (/ rate 1000000.0)) "N/A"))
  463. (cons ?m (if hours (format "%d" (* hours 60)) "N/A"))
  464. (cons ?h (if hours (format "%d" hours) "N/A"))
  465. (cons ?t (if hours
  466. (format "%d:%02d" hours (* (- hours (floor hours)) 60))
  467. "N/A"))
  468. (cons ?d (or temperature "N/A"))
  469. (cons ?B (or charging-state "N/A"))
  470. (cons ?p (cond ((and (> charge-full 0) (> charge-now 0))
  471. (format "%.1f"
  472. (/ (* 100 charge-now) charge-full)))
  473. ((> energy-full 0)
  474. (format "%.1f"
  475. (/ (* 100 energy-now) energy-full)))
  476. (t "N/A")))
  477. (cons ?L (if (file-readable-p "/sys/class/power_supply/AC/online")
  478. (if (battery-search-for-one-match-in-files
  479. (list "/sys/class/power_supply/AC/online"
  480. "/sys/class/power_supply/ACAD/online")
  481. "1" 0)
  482. "AC"
  483. "BAT")
  484. "N/A")))))
  485. (defun battery-yeeloong-sysfs ()
  486. "Get ACPI status information from Linux (the kernel).
  487. This function works only on the Lemote Yeeloong.
  488. The following %-sequences are provided:
  489. %c Current capacity (mAh)
  490. %r Current rate
  491. %B Battery status (verbose)
  492. %b Battery status, empty means high, `-' means low,
  493. `!' means critical, and `+' means charging
  494. %L AC line status (verbose)
  495. %p Battery load percentage
  496. %m Remaining time (to charge or discharge) in minutes
  497. %h Remaining time (to charge or discharge) in hours
  498. %t Remaining time (to charge or discharge) in the form `h:min'"
  499. (let (capacity
  500. capacity-level
  501. status
  502. ac-online
  503. hours
  504. current-now
  505. charge-full
  506. charge-now)
  507. (with-temp-buffer
  508. (ignore-errors
  509. (insert-file-contents "/sys/class/power_supply/yeeloong-bat/uevent")
  510. (goto-char 1)
  511. (search-forward "POWER_SUPPLY_CHARGE_NOW=")
  512. (setq charge-now (read (current-buffer)))
  513. (goto-char 1)
  514. (search-forward "POWER_SUPPLY_CHARGE_FULL=")
  515. (setq charge-full (read (current-buffer)))
  516. (goto-char 1)
  517. (search-forward "POWER_SUPPLY_CURRENT_NOW=")
  518. (setq current-now (read (current-buffer)))
  519. (goto-char 1)
  520. (search-forward "POWER_SUPPLY_CAPACITY_LEVEL=")
  521. (setq capacity-level (buffer-substring (point) (line-end-position)))
  522. (goto-char 1)
  523. (search-forward "POWER_SUPPLY_STATUS=")
  524. (setq status (buffer-substring (point) (line-end-position))))
  525. (erase-buffer)
  526. (ignore-errors
  527. (insert-file-contents
  528. "/sys/class/power_supply/yeeloong-ac/online")
  529. (goto-char 1)
  530. (setq ac-online (read (current-buffer)))
  531. (erase-buffer)))
  532. (setq capacity (round (/ (* charge-now 100.0) charge-full)))
  533. (when (and current-now (not (= current-now 0)))
  534. (if (< current-now 0)
  535. ;; Charging
  536. (setq hours (/ (- charge-now charge-full) (+ 0.0 current-now)))
  537. ;; Discharging
  538. (setq hours (/ charge-now (+ 0.0 current-now)))))
  539. (list (cons ?c (if charge-now
  540. (number-to-string charge-now)
  541. "N/A"))
  542. (cons ?r current-now)
  543. (cons ?B (cond ((equal capacity-level "Full") "full")
  544. ((equal status "Charging") "charging")
  545. ((equal capacity-level "Low") "low")
  546. ((equal capacity-level "Critical") "critical")
  547. (t "high")))
  548. (cons ?b (cond ((equal capacity-level "Full") " ")
  549. ((equal status "Charging") "+")
  550. ((equal capacity-level "Low") "-")
  551. ((equal capacity-level "Critical") "!")
  552. (t " ")))
  553. (cons ?h (if hours (number-to-string hours) "N/A"))
  554. (cons ?m (if hours (number-to-string (* 60 hours)) "N/A"))
  555. (cons ?t (if hours
  556. (format "%d:%d"
  557. (/ (round (* 60 hours)) 60)
  558. (% (round (* 60 hours)) 60))
  559. "N/A"))
  560. (cons ?p (if capacity (number-to-string capacity) "N/A"))
  561. (cons ?L (if (eq ac-online 1) "AC" "BAT")))))
  562. ;;; `pmset' interface for Darwin (OS X).
  563. (defun battery-pmset ()
  564. "Get battery status information using `pmset'.
  565. The following %-sequences are provided:
  566. %L Power source (verbose)
  567. %B Battery status (verbose)
  568. %b Battery status, empty means high, `-' means low,
  569. `!' means critical, and `+' means charging
  570. %p Battery load percentage
  571. %h Remaining time in hours
  572. %m Remaining time in minutes
  573. %t Remaining time in the form `h:min'"
  574. (let (power-source load-percentage battery-status battery-status-symbol
  575. remaining-time hours minutes)
  576. (with-temp-buffer
  577. (ignore-errors (call-process "pmset" nil t nil "-g" "ps"))
  578. (goto-char (point-min))
  579. (when (re-search-forward "Currentl?y drawing from '\\(AC\\|Battery\\) Power'" nil t)
  580. (setq power-source (match-string 1))
  581. (when (re-search-forward "^ -InternalBattery-0[ \t]+" nil t)
  582. (when (looking-at "\\([0-9]\\{1,3\\}\\)%")
  583. (setq load-percentage (match-string 1))
  584. (goto-char (match-end 0))
  585. (cond ((looking-at "; charging")
  586. (setq battery-status "charging"
  587. battery-status-symbol "+"))
  588. ((< (string-to-number load-percentage) battery-load-low)
  589. (setq battery-status "low"
  590. battery-status-symbol "-"))
  591. ((< (string-to-number load-percentage) battery-load-critical)
  592. (setq battery-status "critical"
  593. battery-status-symbol "!"))
  594. (t
  595. (setq battery-status "high"
  596. battery-status-symbol "")))
  597. (when (re-search-forward "\\(\\([0-9]+\\):\\([0-9]+\\)\\) remaining" nil t)
  598. (setq remaining-time (match-string 1))
  599. (let ((h (string-to-number (match-string 2)))
  600. (m (string-to-number (match-string 3))))
  601. (setq hours (number-to-string (+ h (if (< m 30) 0 1)))
  602. minutes (number-to-string (+ (* h 60) m)))))))))
  603. (list (cons ?L (or power-source "N/A"))
  604. (cons ?p (or load-percentage "N/A"))
  605. (cons ?B (or battery-status "N/A"))
  606. (cons ?b (or battery-status-symbol ""))
  607. (cons ?h (or hours "N/A"))
  608. (cons ?m (or minutes "N/A"))
  609. (cons ?t (or remaining-time "N/A")))))
  610. ;;; Private functions.
  611. (defun battery-format (format alist)
  612. "Substitute %-sequences in FORMAT."
  613. (replace-regexp-in-string
  614. "%."
  615. (lambda (str)
  616. (let ((char (aref str 1)))
  617. (if (eq char ?%) "%"
  618. (or (cdr (assoc char alist)) ""))))
  619. format t t))
  620. (defun battery-search-for-one-match-in-files (files regexp match-num)
  621. "Search REGEXP in the content of the files listed in FILES.
  622. If a match occurred, return the parenthesized expression numbered by
  623. MATCH-NUM in the match. Otherwise, return nil."
  624. (with-temp-buffer
  625. (catch 'found
  626. (dolist (file files)
  627. (and (ignore-errors (insert-file-contents file nil nil nil 'replace))
  628. (re-search-forward regexp nil t)
  629. (throw 'found (match-string match-num)))))))
  630. (provide 'battery)
  631. ;;; battery.el ends here