From: Ken Brown <kbrown@cornell.edu>
To: Emacs <emacs-devel@gnu.org>
Subject: Adding battery support on Cygwin
Date: Fri, 12 Jan 2018 21:33:33 -0500 [thread overview]
Message-ID: <302136d9-0b2e-222e-502d-2db8f7de5992@cornell.edu> (raw)
[-- Attachment #1: Type: text/plain, Size: 459 bytes --]
The Cygwin-w32 currently supports battery status via the function
w32fns.c:Fw32_battery_status. The X11 and nox builds don't have this
support, and Cygwin lacks the facilities used on unix-like systems to
provide it (/proc/apm, etc.). But it turns out to be easy to compile
and use Fw32_battery_status on all Cygwin builds, simply by pulling it
out of w32fns.c into a new file.
The attached patch does this. OK to push? If so, to which branch?
Ken
[-- Attachment #2: 0001-Add-battery-support-to-all-Cygwin-builds.patch --]
[-- Type: text/plain, Size: 13751 bytes --]
From 0ede3dbda135e32fa2d00172cdecdd08026e9be0 Mon Sep 17 00:00:00 2001
From: Ken Brown <kbrown@cornell.edu>
Date: Fri, 12 Jan 2018 08:51:16 -0500
Subject: [PATCH] Add battery support to all Cygwin builds
It already exists in the Cygwin-w32 build.
* src/w32fns.c (Fw32_battery_status): Move to a new file,
src/w32battery.c.
* src/w32battery.c (syms_of_w32battery): New function.
* src/w32battery.h: New file, containing prototype of
syms_of_w32battery.
* src/emacs.c (main) [HAVE_NTGUI or CYGWIN]: #include w32battery.h
and call syms_of_w32battery.
* src/Makefile.in (SOME_MACHINE_OBJECTS): Add w32battery.o.
* configure.ac (W32_OBJ) [HAVE_W32 or CYGWIN]: Add w32battery.o.
(W32_LIBS) [CYGWIN]: Set equal to "-lkernel32" in non-w32 build.
---
configure.ac | 8 +++-
src/Makefile.in | 9 ++--
src/emacs.c | 10 +++-
src/w32battery.c | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
src/w32battery.h | 25 ++++++++++
src/w32fns.c | 110 -------------------------------------------
6 files changed, 187 insertions(+), 116 deletions(-)
create mode 100644 src/w32battery.c
create mode 100644 src/w32battery.h
diff --git a/configure.ac b/configure.ac
index 0fddfeab77..ca425716ae 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2076,7 +2076,7 @@ AC_DEFUN
AC_CHECK_TOOL(WINDRES, [windres],
[AC_MSG_ERROR([No resource compiler found.])])
W32_OBJ="w32fns.o w32menu.o w32reg.o w32font.o w32term.o"
- W32_OBJ="$W32_OBJ w32xfns.o w32select.o w32uniscribe.o"
+ W32_OBJ="$W32_OBJ w32xfns.o w32select.o w32uniscribe.o w32battery.o"
EMACSRES="emacs.res"
case "$canonical" in
x86_64-*-*) EMACS_MANIFEST="emacs-x64.manifest" ;;
@@ -2111,6 +2111,12 @@ AC_DEFUN
XARGS_LIMIT="-s 10000"
fi
fi
+
+if test "${HAVE_W32}" = "no" && test "${opsys}" = "cygwin"; then
+ W32_LIBS="$W32_LIBS -lkernel32"
+ W32_OBJ="$W32_OBJ w32battery.o"
+fi
+
AC_SUBST(W32_OBJ)
AC_SUBST(W32_LIBS)
AC_SUBST(EMACSRES)
diff --git a/src/Makefile.in b/src/Makefile.in
index 15ca1667d6..10cfde72c6 100644
--- a/src/Makefile.in
+++ b/src/Makefile.in
@@ -277,11 +277,12 @@ NS_OBJC_OBJ=
## Used only for GNUstep.
GNU_OBJC_CFLAGS=$(patsubst -specs=%-hardened-cc1,,@GNU_OBJC_CFLAGS@)
## w32fns.o w32menu.c w32reg.o fringe.o fontset.o w32font.o w32term.o
-## w32xfns.o w32select.o image.o w32uniscribe.o if HAVE_W32, else
-## empty.
+## w32xfns.o w32select.o image.o w32uniscribe.o w32battery.o if HAVE_W32,
+## w32battery.o if CYGWIN but not HAVE_W32, else empty.
W32_OBJ=@W32_OBJ@
## -lkernel32 -luser32 -lusp10 -lgdi32 -lole32 -lcomdlg32 -lcomctl32
-## --lwinspool if HAVE_W32, else empty.
+## -lwinspool if HAVE_W32,
+## -lkernel32 if CYGWIN but not HAVE_W32, else empty.
W32_LIBS=@W32_LIBS@
## emacs.res if HAVE_W32
@@ -408,7 +409,7 @@ SOME_MACHINE_OBJECTS =
xterm.o xfns.o xmenu.o xselect.o xrdb.o xsmfns.o fringe.o image.o \
fontset.o dbusbind.o cygw32.o \
nsterm.o nsfns.o nsmenu.o nsselect.o nsimage.o nsfont.o macfont.o \
- w32.o w32console.o w32fns.o w32heap.o w32inevt.o w32notify.o \
+ w32.o w32battery.o w32console.o w32fns.o w32heap.o w32inevt.o w32notify.o \
w32menu.o w32proc.o w32reg.o w32select.o w32term.o w32xfns.o \
w16select.o widget.o xfont.o ftfont.o xftfont.o ftxfont.o gtkutil.o \
xsettings.o xgselect.o termcap.o
diff --git a/src/emacs.c b/src/emacs.c
index 017c62308c..ef42923554 100644
--- a/src/emacs.c
+++ b/src/emacs.c
@@ -48,6 +48,10 @@ along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
#include "w32common.h"
#endif
+#if defined HAVE_NTGUI || defined CYGWIN
+#include "w32battery.h"
+#endif
+
#if defined CYGWIN
#include "cygw32.h"
#endif
@@ -1556,13 +1560,17 @@ Using an Emacs configured with --with-x-toolkit=lucid does not have this problem
syms_of_menu ();
-#ifdef HAVE_NTGUI
+#if defined HAVE_NTGUI
syms_of_w32term ();
syms_of_w32fns ();
syms_of_w32menu ();
syms_of_fontset ();
#endif /* HAVE_NTGUI */
+#if defined HAVE_NTGUI || defined CYGWIN
+ syms_of_w32battery ();
+#endif
+
#if defined WINDOWSNT || defined HAVE_NTGUI
syms_of_w32select ();
#endif
diff --git a/src/w32battery.c b/src/w32battery.c
new file mode 100644
index 0000000000..2a6c22bd3a
--- /dev/null
+++ b/src/w32battery.c
@@ -0,0 +1,141 @@
+/* Battery status function for the Microsoft Windows API.
+
+Copyright (C) 2018 Free Software Foundation, Inc.
+
+This file is part of GNU Emacs.
+
+GNU Emacs is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or (at
+your option) any later version.
+
+GNU Emacs is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
+
+#include <config.h>
+
+#include <stdio.h>
+
+#include "lisp.h"
+#include "w32battery.h"
+#include "w32common.h"
+
+DEFUN ("w32-battery-status", Fw32_battery_status, Sw32_battery_status, 0, 0, 0,
+ doc: /* Get power status information from Windows system.
+
+The following %-sequences are provided:
+%L AC line status (verbose)
+%B Battery status (verbose)
+%b Battery status, empty means high, `-' means low,
+ `!' means critical, and `+' means charging
+%p Battery load percentage
+%s Remaining time (to charge or discharge) in seconds
+%m Remaining time (to charge or discharge) in minutes
+%h Remaining time (to charge or discharge) in hours
+%t Remaining time (to charge or discharge) in the form `h:min' */)
+ (void)
+{
+ Lisp_Object status = Qnil;
+
+ SYSTEM_POWER_STATUS system_status;
+ if (GetSystemPowerStatus (&system_status))
+ {
+ Lisp_Object line_status, battery_status, battery_status_symbol;
+ Lisp_Object load_percentage, seconds, minutes, hours, remain;
+
+ long seconds_left = (long) system_status.BatteryLifeTime;
+
+ if (system_status.ACLineStatus == 0)
+ line_status = build_string ("off-line");
+ else if (system_status.ACLineStatus == 1)
+ line_status = build_string ("on-line");
+ else
+ line_status = build_string ("N/A");
+
+ if (system_status.BatteryFlag & 128)
+ {
+ battery_status = build_string ("N/A");
+ battery_status_symbol = empty_unibyte_string;
+ }
+ else if (system_status.BatteryFlag & 8)
+ {
+ battery_status = build_string ("charging");
+ battery_status_symbol = build_string ("+");
+ if (system_status.BatteryFullLifeTime != -1L)
+ seconds_left = system_status.BatteryFullLifeTime - seconds_left;
+ }
+ else if (system_status.BatteryFlag & 4)
+ {
+ battery_status = build_string ("critical");
+ battery_status_symbol = build_string ("!");
+ }
+ else if (system_status.BatteryFlag & 2)
+ {
+ battery_status = build_string ("low");
+ battery_status_symbol = build_string ("-");
+ }
+ else if (system_status.BatteryFlag & 1)
+ {
+ battery_status = build_string ("high");
+ battery_status_symbol = empty_unibyte_string;
+ }
+ else
+ {
+ battery_status = build_string ("medium");
+ battery_status_symbol = empty_unibyte_string;
+ }
+
+ if (system_status.BatteryLifePercent > 100)
+ load_percentage = build_string ("N/A");
+ else
+ {
+ char buffer[16];
+ snprintf (buffer, 16, "%d", system_status.BatteryLifePercent);
+ load_percentage = build_string (buffer);
+ }
+
+ if (seconds_left < 0)
+ seconds = minutes = hours = remain = build_string ("N/A");
+ else
+ {
+ long m;
+ double h;
+ char buffer[16];
+ snprintf (buffer, 16, "%ld", seconds_left);
+ seconds = build_string (buffer);
+
+ m = seconds_left / 60;
+ snprintf (buffer, 16, "%ld", m);
+ minutes = build_string (buffer);
+
+ h = seconds_left / 3600.0;
+ snprintf (buffer, 16, "%3.1f", h);
+ hours = build_string (buffer);
+
+ snprintf (buffer, 16, "%ld:%02ld", m / 60, m % 60);
+ remain = build_string (buffer);
+ }
+
+ status = listn (CONSTYPE_HEAP, 8,
+ Fcons (make_number ('L'), line_status),
+ Fcons (make_number ('B'), battery_status),
+ Fcons (make_number ('b'), battery_status_symbol),
+ Fcons (make_number ('p'), load_percentage),
+ Fcons (make_number ('s'), seconds),
+ Fcons (make_number ('m'), minutes),
+ Fcons (make_number ('h'), hours),
+ Fcons (make_number ('t'), remain));
+ }
+ return status;
+}
+
+void
+syms_of_w32battery (void)
+{
+ defsubr (&Sw32_battery_status);
+}
diff --git a/src/w32battery.h b/src/w32battery.h
new file mode 100644
index 0000000000..796c6be72e
--- /dev/null
+++ b/src/w32battery.h
@@ -0,0 +1,25 @@
+/* Battery status function for the Microsoft Windows API.
+
+Copyright (C) 2018 Free Software Foundation, Inc.
+
+This file is part of GNU Emacs.
+
+GNU Emacs is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or (at
+your option) any later version.
+
+GNU Emacs is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
+
+#ifndef EMACS_W32BATTERY_H
+#define EMACS_W32BATTERY_H
+
+extern void syms_of_w32battery (void);
+
+#endif
diff --git a/src/w32fns.c b/src/w32fns.c
index e50b7d5c3c..c561f84863 100644
--- a/src/w32fns.c
+++ b/src/w32fns.c
@@ -9213,115 +9213,6 @@ The coordinates X and Y are interpreted in pixels relative to a position
return Qnil;
}
-DEFUN ("w32-battery-status", Fw32_battery_status, Sw32_battery_status, 0, 0, 0,
- doc: /* Get power status information from Windows system.
-
-The following %-sequences are provided:
-%L AC line status (verbose)
-%B Battery status (verbose)
-%b Battery status, empty means high, `-' means low,
- `!' means critical, and `+' means charging
-%p Battery load percentage
-%s Remaining time (to charge or discharge) in seconds
-%m Remaining time (to charge or discharge) in minutes
-%h Remaining time (to charge or discharge) in hours
-%t Remaining time (to charge or discharge) in the form `h:min' */)
- (void)
-{
- Lisp_Object status = Qnil;
-
- SYSTEM_POWER_STATUS system_status;
- if (GetSystemPowerStatus (&system_status))
- {
- Lisp_Object line_status, battery_status, battery_status_symbol;
- Lisp_Object load_percentage, seconds, minutes, hours, remain;
-
- long seconds_left = (long) system_status.BatteryLifeTime;
-
- if (system_status.ACLineStatus == 0)
- line_status = build_string ("off-line");
- else if (system_status.ACLineStatus == 1)
- line_status = build_string ("on-line");
- else
- line_status = build_string ("N/A");
-
- if (system_status.BatteryFlag & 128)
- {
- battery_status = build_string ("N/A");
- battery_status_symbol = empty_unibyte_string;
- }
- else if (system_status.BatteryFlag & 8)
- {
- battery_status = build_string ("charging");
- battery_status_symbol = build_string ("+");
- if (system_status.BatteryFullLifeTime != -1L)
- seconds_left = system_status.BatteryFullLifeTime - seconds_left;
- }
- else if (system_status.BatteryFlag & 4)
- {
- battery_status = build_string ("critical");
- battery_status_symbol = build_string ("!");
- }
- else if (system_status.BatteryFlag & 2)
- {
- battery_status = build_string ("low");
- battery_status_symbol = build_string ("-");
- }
- else if (system_status.BatteryFlag & 1)
- {
- battery_status = build_string ("high");
- battery_status_symbol = empty_unibyte_string;
- }
- else
- {
- battery_status = build_string ("medium");
- battery_status_symbol = empty_unibyte_string;
- }
-
- if (system_status.BatteryLifePercent > 100)
- load_percentage = build_string ("N/A");
- else
- {
- char buffer[16];
- snprintf (buffer, 16, "%d", system_status.BatteryLifePercent);
- load_percentage = build_string (buffer);
- }
-
- if (seconds_left < 0)
- seconds = minutes = hours = remain = build_string ("N/A");
- else
- {
- long m;
- double h;
- char buffer[16];
- snprintf (buffer, 16, "%ld", seconds_left);
- seconds = build_string (buffer);
-
- m = seconds_left / 60;
- snprintf (buffer, 16, "%ld", m);
- minutes = build_string (buffer);
-
- h = seconds_left / 3600.0;
- snprintf (buffer, 16, "%3.1f", h);
- hours = build_string (buffer);
-
- snprintf (buffer, 16, "%ld:%02ld", m / 60, m % 60);
- remain = build_string (buffer);
- }
-
- status = listn (CONSTYPE_HEAP, 8,
- Fcons (make_number ('L'), line_status),
- Fcons (make_number ('B'), battery_status),
- Fcons (make_number ('b'), battery_status_symbol),
- Fcons (make_number ('p'), load_percentage),
- Fcons (make_number ('s'), seconds),
- Fcons (make_number ('m'), minutes),
- Fcons (make_number ('h'), hours),
- Fcons (make_number ('t'), remain));
- }
- return status;
-}
-
\f
#ifdef WINDOWSNT
typedef BOOL (WINAPI *GetDiskFreeSpaceExW_Proc)
@@ -10774,7 +10665,6 @@ tip frame. */);
defsubr (&Sw32_reconstruct_hot_key);
defsubr (&Sw32_toggle_lock_key);
defsubr (&Sw32_window_exists_p);
- defsubr (&Sw32_battery_status);
defsubr (&Sw32__menu_bar_in_use);
#if defined WINDOWSNT && !defined HAVE_DBUS
defsubr (&Sw32_notification_notify);
--
2.15.1
next reply other threads:[~2018-01-13 2:33 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-01-13 2:33 Ken Brown [this message]
2018-01-13 8:11 ` Adding battery support on Cygwin Eli Zaretskii
2018-01-13 18:19 ` Ken Brown
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=302136d9-0b2e-222e-502d-2db8f7de5992@cornell.edu \
--to=kbrown@cornell.edu \
--cc=emacs-devel@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.