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