root/src/w32cygwinx.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. ATTRIBUTE_FORMAT_PRINTF
  2. DEFUN
  3. syms_of_w32cygwinx

     1 /* Common functions for the Microsoft Windows and Cygwin builds.
     2 
     3 Copyright (C) 2018-2023 Free Software Foundation, Inc.
     4 
     5 This file is part of GNU Emacs.
     6 
     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.
    11 
    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.
    16 
    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/>.  */
    19 
    20 #include <config.h>
    21 
    22 #include <stdio.h>
    23 
    24 #include "lisp.h"
    25 #include "w32common.h"
    26 
    27 static Lisp_Object ATTRIBUTE_FORMAT_PRINTF (1, 2)
    28 format_string (char const *format, ...)
    29 {
    30   va_list args;
    31   va_start (args, format);
    32   Lisp_Object str = vformat_string (format, args);
    33   va_end (args);
    34   return str;
    35 }
    36 
    37 DEFUN ("w32-battery-status", Fw32_battery_status, Sw32_battery_status, 0, 0, 0,
    38        doc: /* Get power status information from Windows system.
    39 
    40 The following %-sequences are provided:
    41 %L AC line status (verbose)
    42 %B Battery status (verbose)
    43 %b Battery status, empty means high, `-' means low,
    44    `!' means critical, and `+' means charging
    45 %p Battery load percentage
    46 %s Remaining time (to charge or discharge) in seconds
    47 %m Remaining time (to charge or discharge) in minutes
    48 %h Remaining time (to charge or discharge) in hours
    49 %t Remaining time (to charge or discharge) in the form `h:min'  */)
    50   (void)
    51 {
    52   Lisp_Object status = Qnil;
    53 
    54   SYSTEM_POWER_STATUS system_status;
    55   if (GetSystemPowerStatus (&system_status))
    56     {
    57       Lisp_Object line_status, battery_status, battery_status_symbol;
    58       Lisp_Object load_percentage, seconds, minutes, hours, remain;
    59 
    60       long seconds_left = (long) system_status.BatteryLifeTime;
    61 
    62       if (system_status.ACLineStatus == 0)
    63         line_status = build_string ("off-line");
    64       else if (system_status.ACLineStatus == 1)
    65         line_status = build_string ("on-line");
    66       else
    67         line_status = build_string ("N/A");
    68 
    69       if (system_status.BatteryFlag & 128)
    70         {
    71           battery_status = build_string ("N/A");
    72           battery_status_symbol = empty_unibyte_string;
    73         }
    74       else if (system_status.BatteryFlag & 8)
    75         {
    76           battery_status = build_string ("charging");
    77           battery_status_symbol = build_string ("+");
    78           if (system_status.BatteryFullLifeTime != -1L)
    79             seconds_left = system_status.BatteryFullLifeTime - seconds_left;
    80         }
    81       else if (system_status.BatteryFlag & 4)
    82         {
    83           battery_status = build_string ("critical");
    84           battery_status_symbol = build_string ("!");
    85         }
    86       else if (system_status.BatteryFlag & 2)
    87         {
    88           battery_status = build_string ("low");
    89           battery_status_symbol = build_string ("-");
    90         }
    91       else if (system_status.BatteryFlag & 1)
    92         {
    93           battery_status = build_string ("high");
    94           battery_status_symbol = empty_unibyte_string;
    95         }
    96       else
    97         {
    98           battery_status = build_string ("medium");
    99           battery_status_symbol = empty_unibyte_string;
   100         }
   101 
   102       if (system_status.BatteryLifePercent > 100)
   103         load_percentage = build_string ("N/A");
   104       else
   105         load_percentage = format_string ("%d", system_status.BatteryLifePercent);
   106 
   107       if (seconds_left < 0)
   108         seconds = minutes = hours = remain = build_string ("N/A");
   109       else
   110         {
   111           long m = seconds_left / 60;
   112           seconds = format_string ("%ld", seconds_left);
   113           minutes = format_string ("%ld", m);
   114           hours = format_string ("%3.1f", seconds_left / 3600.0);
   115           remain = format_string ("%ld:%02ld", m / 60, m % 60);
   116         }
   117 
   118       status =  list (Fcons (make_fixnum ('L'), line_status),
   119                       Fcons (make_fixnum ('B'), battery_status),
   120                       Fcons (make_fixnum ('b'), battery_status_symbol),
   121                       Fcons (make_fixnum ('p'), load_percentage),
   122                       Fcons (make_fixnum ('s'), seconds),
   123                       Fcons (make_fixnum ('m'), minutes),
   124                       Fcons (make_fixnum ('h'), hours),
   125                       Fcons (make_fixnum ('t'), remain));
   126     }
   127   return status;
   128 }
   129 
   130 void
   131 syms_of_w32cygwinx (void)
   132 {
   133   defsubr (&Sw32_battery_status);
   134 }

/* [<][>][^][v][top][bottom][index][help] */