SDL  2.0
SDL_syspower.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2018 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 /* uses BeOS euc.jp apm driver. */
24 /* !!! FIXME: does this thing even work on Haiku? */
25 
26 #ifndef SDL_POWER_DISABLED
27 #if SDL_POWER_HAIKU
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <ctype.h>
34 #include <drivers/Drivers.h>
35 
36 /* These values are from apm.h ... */
37 #define APM_DEVICE_PATH "/dev/misc/apm"
38 #define APM_FUNC_OFFSET 0x5300
39 #define APM_FUNC_GET_POWER_STATUS 10
40 #define APM_DEVICE_ALL 1
41 #define APM_BIOS_CALL (B_DEVICE_OP_CODES_END + 3)
42 
43 #include "SDL_power.h"
44 
46 SDL_GetPowerInfo_Haiku(SDL_PowerState * state, int *seconds, int *percent)
47 {
48  const int fd = open("/dev/misc/apm", O_RDONLY);
49  SDL_bool need_details = SDL_FALSE;
50  uint16 regs[6];
51  uint8 ac_status;
52  uint8 battery_status;
53  uint8 battery_flags;
54  uint8 battery_life;
55  uint32 battery_time;
56  int rc;
57 
58  if (fd == -1) {
59  return SDL_FALSE; /* maybe some other method will work? */
60  }
61 
62  memset(regs, '\0', sizeof(regs));
63  regs[0] = APM_FUNC_OFFSET + APM_FUNC_GET_POWER_STATUS;
64  regs[1] = APM_DEVICE_ALL;
65  rc = ioctl(fd, APM_BIOS_CALL, regs);
66  close(fd);
67 
68  if (rc < 0) {
69  return SDL_FALSE;
70  }
71 
72  ac_status = regs[1] >> 8;
73  battery_status = regs[1] & 0xFF;
74  battery_flags = regs[2] >> 8;
75  battery_life = regs[2] & 0xFF;
76  battery_time = (uint32) regs[3];
77 
78  /* in theory, _something_ should be set in battery_flags, right? */
79  if (battery_flags == 0x00) { /* older APM BIOS? Less fields. */
80  battery_time = 0xFFFF;
81  if (battery_status == 0xFF) {
82  battery_flags = 0xFF;
83  } else {
84  battery_flags = (1 << battery_status);
85  }
86  }
87 
88  if ((battery_time != 0xFFFF) && (battery_time & (1 << 15))) {
89  /* time is in minutes, not seconds */
90  battery_time = (battery_time & 0x7FFF) * 60;
91  }
92 
93  if (battery_flags == 0xFF) { /* unknown state */
94  *state = SDL_POWERSTATE_UNKNOWN;
95  } else if (battery_flags & (1 << 7)) { /* no battery */
97  } else if (battery_flags & (1 << 3)) { /* charging */
98  *state = SDL_POWERSTATE_CHARGING;
99  need_details = SDL_TRUE;
100  } else if (ac_status == 1) {
101  *state = SDL_POWERSTATE_CHARGED; /* on AC, not charging. */
102  need_details = SDL_TRUE;
103  } else {
104  *state = SDL_POWERSTATE_ON_BATTERY; /* not on AC. */
105  need_details = SDL_TRUE;
106  }
107 
108  *percent = -1;
109  *seconds = -1;
110  if (need_details) {
111  const int pct = (int) battery_life;
112  const int secs = (int) battery_time;
113 
114  if (pct != 255) { /* 255 == unknown */
115  *percent = (pct > 100) ? 100 : pct; /* clamp between 0%, 100% */
116  }
117  if (secs != 0xFFFF) { /* 0xFFFF == unknown */
118  *seconds = secs;
119  }
120  }
121 
122  return SDL_TRUE; /* the definitive answer if APM driver replied. */
123 }
124 
125 #endif /* SDL_POWER_HAIKU */
126 #endif /* SDL_POWER_DISABLED */
127 
128 /* vi: set ts=4 sw=4 expandtab: */
struct xkb_state * state
#define memset
Definition: SDL_malloc.c:619
SDL_bool SDL_GetPowerInfo_Haiku(SDL_PowerState *, int *, int *)
SDL_bool
Definition: SDL_stdinc.h:139
SDL_PowerState
The basic state for the system&#39;s power supply.
Definition: SDL_power.h:42
GLuint64 GLenum GLint fd
Definition: gl2ext.h:1508