battery.el 23 KB

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