1 /* Common functions for the Microsoft Windows and Cygwin builds.
3 Copyright (C) 2018 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or (at
10 your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
25 #include "w32common.h"
27 DEFUN ("w32-battery-status", Fw32_battery_status
, Sw32_battery_status
, 0, 0, 0,
28 doc
: /* Get power status information from Windows system.
30 The following %-sequences are provided:
31 %L AC line status (verbose)
32 %B Battery status (verbose)
33 %b Battery status, empty means high, `-' means low,
34 `!' means critical, and `+' means charging
35 %p Battery load percentage
36 %s Remaining time (to charge or discharge) in seconds
37 %m Remaining time (to charge or discharge) in minutes
38 %h Remaining time (to charge or discharge) in hours
39 %t Remaining time (to charge or discharge) in the form `h:min' */)
42 Lisp_Object status
= Qnil
;
44 SYSTEM_POWER_STATUS system_status
;
45 if (GetSystemPowerStatus (&system_status
))
47 Lisp_Object line_status
, battery_status
, battery_status_symbol
;
48 Lisp_Object load_percentage
, seconds
, minutes
, hours
, remain
;
50 long seconds_left
= (long) system_status
.BatteryLifeTime
;
52 if (system_status
.ACLineStatus
== 0)
53 line_status
= build_string ("off-line");
54 else if (system_status
.ACLineStatus
== 1)
55 line_status
= build_string ("on-line");
57 line_status
= build_string ("N/A");
59 if (system_status
.BatteryFlag
& 128)
61 battery_status
= build_string ("N/A");
62 battery_status_symbol
= empty_unibyte_string
;
64 else if (system_status
.BatteryFlag
& 8)
66 battery_status
= build_string ("charging");
67 battery_status_symbol
= build_string ("+");
68 if (system_status
.BatteryFullLifeTime
!= -1L)
69 seconds_left
= system_status
.BatteryFullLifeTime
- seconds_left
;
71 else if (system_status
.BatteryFlag
& 4)
73 battery_status
= build_string ("critical");
74 battery_status_symbol
= build_string ("!");
76 else if (system_status
.BatteryFlag
& 2)
78 battery_status
= build_string ("low");
79 battery_status_symbol
= build_string ("-");
81 else if (system_status
.BatteryFlag
& 1)
83 battery_status
= build_string ("high");
84 battery_status_symbol
= empty_unibyte_string
;
88 battery_status
= build_string ("medium");
89 battery_status_symbol
= empty_unibyte_string
;
92 if (system_status
.BatteryLifePercent
> 100)
93 load_percentage
= build_string ("N/A");
97 snprintf (buffer
, 16, "%d", system_status
.BatteryLifePercent
);
98 load_percentage
= build_string (buffer
);
101 if (seconds_left
< 0)
102 seconds
= minutes
= hours
= remain
= build_string ("N/A");
108 snprintf (buffer
, 16, "%ld", seconds_left
);
109 seconds
= build_string (buffer
);
111 m
= seconds_left
/ 60;
112 snprintf (buffer
, 16, "%ld", m
);
113 minutes
= build_string (buffer
);
115 h
= seconds_left
/ 3600.0;
116 snprintf (buffer
, 16, "%3.1f", h
);
117 hours
= build_string (buffer
);
119 snprintf (buffer
, 16, "%ld:%02ld", m
/ 60, m
% 60);
120 remain
= build_string (buffer
);
123 status
= listn (CONSTYPE_HEAP
, 8,
124 Fcons (make_number ('L'), line_status
),
125 Fcons (make_number ('B'), battery_status
),
126 Fcons (make_number ('b'), battery_status_symbol
),
127 Fcons (make_number ('p'), load_percentage
),
128 Fcons (make_number ('s'), seconds
),
129 Fcons (make_number ('m'), minutes
),
130 Fcons (make_number ('h'), hours
),
131 Fcons (make_number ('t'), remain
));
137 syms_of_w32cygwinx (void)
139 defsubr (&Sw32_battery_status
);