home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / showapm.c < prev    next >
C/C++ Source or Header  |  1996-07-22  |  3KB  |  98 lines

  1. #define INCL_DOSDEVIOCTL
  2. #define INCL_DOSFILEMGR
  3. #include <os2.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. typedef struct
  9. {
  10.   USHORT ParmLength;
  11.   USHORT PowerFlags;
  12.   UCHAR  ACStatus;
  13.   UCHAR  BatteryStatus;
  14.   UCHAR  BatteryLife;
  15. } PARMPACK;
  16.  
  17. INT main(VOID)
  18. {
  19.   HFILE    DevHandle;
  20.   ULONG    ulCategory;
  21.   ULONG    ulFunction;
  22.   PARMPACK ParmPack;
  23.   ULONG    ulParmLen;
  24.   UCHAR    uchDataArea;
  25.   ULONG    ulDataLen;
  26.   APIRET   rc;
  27.   ULONG    ulAction;
  28.  
  29.  
  30.   rc = DosOpen("APM$", &DevHandle, &ulAction, 0, FILE_NORMAL, FILE_OPEN, OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYNONE, 0);
  31.   switch(rc)
  32.   {
  33.     case   0: break;
  34.     case 110: printf("You must have the APM device driver installed.\n");
  35.               return 1;
  36.               break;
  37.     default : printf("DosOpen error: rc = %u\n", rc);
  38.               return rc;
  39.   }
  40.  
  41.   ulCategory  = IOCTL_POWER;
  42.   ulFunction  = POWER_GETPOWERSTATUS;
  43.   ulParmLen   = sizeof(ParmPack);
  44.   ulDataLen   = sizeof(uchDataArea);
  45.   ParmPack.ParmLength = 7;
  46.  
  47.   /****************************************************************************
  48.    * I use the ANSI escape sequences because thunking the 16 bit calls is too *
  49.    * much work!                                                               *
  50.    ****************************************************************************/
  51.   printf("");  //clear screen
  52.   printf("s");   //save cursor position
  53.  
  54.   while (TRUE)     //do forever
  55.   {
  56.     rc = DosDevIOCtl(DevHandle, ulCategory, ulFunction, &ParmPack, sizeof(ParmPack), &ulParmLen, &uchDataArea, sizeof(uchDataArea), &ulDataLen);
  57.     if (rc != 0)
  58.     {
  59.       printf("DosDevIOCtl error: return code = %u\n", rc);
  60.       printf("                   Date area   = %c\n", uchDataArea);
  61.       return rc;
  62.     }
  63.  
  64.     if (!ParmPack.PowerFlags)
  65.     {
  66.       printf("Power Management support is currently disabled.\n");
  67.       return 1;
  68.     }
  69.  
  70.     printf("u");     //restore cursor position
  71.  
  72.     printf("Plugged in    : ");
  73.     switch(ParmPack.ACStatus)
  74.     {
  75.       case   0: printf("No     ");  break;
  76.       case   1: printf("Yes    ");  break;
  77.       case 255: printf("Unknown");  break;
  78.     }
  79.     printf("\n");
  80.  
  81.     printf("Battery Status: ");
  82.     switch(ParmPack.BatteryStatus)
  83.     {
  84.       case   0: printf("High    "); break;
  85.       case   1: printf("Low     "); break;
  86.       case   2: printf("Critical"); break;
  87.       case   3: printf("Charging"); break;
  88.       case 255: printf("Unknown "); break;
  89.     }
  90.     printf("\n");
  91.  
  92.     printf("Battery Life  : %d%%  \n", ParmPack.BatteryLife);
  93.     DosSleep(500);
  94.   }
  95.  
  96.   DosClose(DevHandle);
  97. }
  98.