cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

SDL_syspower.c (3977B)


      1/*
      2  Simple DirectMedia Layer
      3  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
      4
      5  This software is provided 'as-is', without any express or implied
      6  warranty.  In no event will the authors be held liable for any damages
      7  arising from the use of this software.
      8
      9  Permission is granted to anyone to use this software for any purpose,
     10  including commercial applications, and to alter it and redistribute it
     11  freely, subject to the following restrictions:
     12
     13  1. The origin of this software must not be misrepresented; you must not
     14     claim that you wrote the original software. If you use this software
     15     in a product, an acknowledgment in the product documentation would be
     16     appreciated but is not required.
     17  2. Altered source versions must be plainly marked as such, and must not be
     18     misrepresented as being the original software.
     19  3. This notice may not be removed or altered from any source distribution.
     20*/
     21#include "../../SDL_internal.h"
     22
     23/* !!! FIXME: does this thing even work on Haiku? */
     24#ifndef SDL_POWER_DISABLED
     25#if SDL_POWER_HAIKU
     26
     27#include <stdio.h>
     28#include <stdlib.h>
     29#include <unistd.h>
     30#include <fcntl.h>
     31#include <ctype.h>
     32#include <drivers/Drivers.h>
     33
     34/* These values are from apm.h ... */
     35#define APM_DEVICE_PATH "/dev/misc/apm"
     36#define APM_FUNC_OFFSET 0x5300
     37#define APM_FUNC_GET_POWER_STATUS 10
     38#define APM_DEVICE_ALL 1
     39#define APM_BIOS_CALL (B_DEVICE_OP_CODES_END + 3)
     40
     41#include "SDL_power.h"
     42
     43SDL_bool
     44SDL_GetPowerInfo_Haiku(SDL_PowerState * state, int *seconds, int *percent)
     45{
     46    const int fd = open("/dev/misc/apm", O_RDONLY);
     47    SDL_bool need_details = SDL_FALSE;
     48    uint16 regs[6];
     49    uint8 ac_status;
     50    uint8 battery_status;
     51    uint8 battery_flags;
     52    uint8 battery_life;
     53    uint32 battery_time;
     54    int rc;
     55
     56    if (fd == -1) {
     57        return SDL_FALSE;       /* maybe some other method will work? */
     58    }
     59
     60    memset(regs, '\0', sizeof(regs));
     61    regs[0] = APM_FUNC_OFFSET + APM_FUNC_GET_POWER_STATUS;
     62    regs[1] = APM_DEVICE_ALL;
     63    rc = ioctl(fd, APM_BIOS_CALL, regs);
     64    close(fd);
     65
     66    if (rc < 0) {
     67        return SDL_FALSE;
     68    }
     69
     70    ac_status = regs[1] >> 8;
     71    battery_status = regs[1] & 0xFF;
     72    battery_flags = regs[2] >> 8;
     73    battery_life = regs[2] & 0xFF;
     74    battery_time = (uint32) regs[3];
     75
     76    /* in theory, _something_ should be set in battery_flags, right? */
     77    if (battery_flags == 0x00) {        /* older APM BIOS? Less fields. */
     78        battery_time = 0xFFFF;
     79        if (battery_status == 0xFF) {
     80            battery_flags = 0xFF;
     81        } else {
     82            battery_flags = (1 << battery_status);
     83        }
     84    }
     85
     86    if ((battery_time != 0xFFFF) && (battery_time & (1 << 15))) {
     87        /* time is in minutes, not seconds */
     88        battery_time = (battery_time & 0x7FFF) * 60;
     89    }
     90
     91    if (battery_flags == 0xFF) {        /* unknown state */
     92        *state = SDL_POWERSTATE_UNKNOWN;
     93    } else if (battery_flags & (1 << 7)) {      /* no battery */
     94        *state = SDL_POWERSTATE_NO_BATTERY;
     95    } else if (battery_flags & (1 << 3)) {      /* charging */
     96        *state = SDL_POWERSTATE_CHARGING;
     97        need_details = SDL_TRUE;
     98    } else if (ac_status == 1) {
     99        *state = SDL_POWERSTATE_CHARGED;        /* on AC, not charging. */
    100        need_details = SDL_TRUE;
    101    } else {
    102        *state = SDL_POWERSTATE_ON_BATTERY;     /* not on AC. */
    103        need_details = SDL_TRUE;
    104    }
    105
    106    *percent = -1;
    107    *seconds = -1;
    108    if (need_details) {
    109        const int pct = (int) battery_life;
    110        const int secs = (int) battery_time;
    111
    112        if (pct != 255) {       /* 255 == unknown */
    113            *percent = (pct > 100) ? 100 : pct; /* clamp between 0%, 100% */
    114        }
    115        if (secs != 0xFFFF) {   /* 0xFFFF == unknown */
    116            *seconds = secs;
    117        }
    118    }
    119
    120    return SDL_TRUE;            /* the definitive answer if APM driver replied. */
    121}
    122
    123#endif /* SDL_POWER_HAIKU */
    124#endif /* SDL_POWER_DISABLED */
    125
    126/* vi: set ts=4 sw=4 expandtab: */