From 94103c0c025f04e75d5e163d9f0bdde27bb0c848 Mon Sep 17 00:00:00 2001 From: Julian Murgia Date: Sat, 23 Jul 2016 13:15:55 +0200 Subject: Add API to access battery power state Done: - X11, server (tested) - Windows (developed, would be nice to retest) - OSX (not tested) Prepared (not developed): - Android (code is here, but may not compile) - iphone - winrt - bb10 - haiku - javascript --- platform/windows/SCsub | 1 + platform/windows/os_windows.cpp | 14 +++++ platform/windows/os_windows.h | 7 +++ platform/windows/power_windows.cpp | 108 +++++++++++++++++++++++++++++++++++++ platform/windows/power_windows.h | 57 ++++++++++++++++++++ 5 files changed, 187 insertions(+) create mode 100644 platform/windows/power_windows.cpp create mode 100644 platform/windows/power_windows.h (limited to 'platform/windows') diff --git a/platform/windows/SCsub b/platform/windows/SCsub index ae8c07384f..befbe00183 100644 --- a/platform/windows/SCsub +++ b/platform/windows/SCsub @@ -12,6 +12,7 @@ common_win = [ "packet_peer_udp_winsock.cpp", "stream_peer_winsock.cpp", "joypad.cpp", + "power_windows.cpp", ] restarget = "godot_res" + env["OBJSUFFIX"] diff --git a/platform/windows/os_windows.cpp b/platform/windows/os_windows.cpp index 0ef964522b..8a347e5f32 100644 --- a/platform/windows/os_windows.cpp +++ b/platform/windows/os_windows.cpp @@ -1134,6 +1134,8 @@ void OS_Windows::initialize(const VideoMode& p_desired,int p_video_driver,int p_ input = memnew( InputDefault ); joypad = memnew (JoypadWindows(input, &hWnd)); + power_manager = memnew( PowerWindows ); + AudioDriverManager::get_driver(p_audio_driver)->set_singleton(); if (AudioDriverManager::get_driver(p_audio_driver)->init()!=OK) { @@ -2393,6 +2395,18 @@ bool OS_Windows::is_vsync_enabled() const{ return true; } +PowerState OS_Windows::get_power_state() { + return power_manager->get_power_state(); +} + +int OS_Windows::get_power_seconds_left() { + return power_manager->get_power_seconds_left(); +} + +int OS_Windows::get_power_percent_left() { + return power_manager->get_power_percent_left(); +} + bool OS_Windows::check_feature_support(const String& p_feature) { return VisualServer::get_singleton()->has_os_feature(p_feature); diff --git a/platform/windows/os_windows.h b/platform/windows/os_windows.h index 73a2d5f451..c8cacac7a2 100644 --- a/platform/windows/os_windows.h +++ b/platform/windows/os_windows.h @@ -31,6 +31,7 @@ #include "os/input.h" #include "os/os.h" +#include "power_windows.h" #include "context_gl_win.h" #include "servers/visual_server.h" #include "servers/visual/rasterizer.h" @@ -125,6 +126,8 @@ class OS_Windows : public OS { InputDefault *input; JoypadWindows *joypad; + PowerWindows *power_manager; + #ifdef RTAUDIO_ENABLED AudioDriverRtAudio driver_rtaudio; #endif @@ -285,6 +288,10 @@ public: virtual void set_use_vsync(bool p_enable); virtual bool is_vsync_enabled() const; + virtual PowerState get_power_state(); + virtual int get_power_seconds_left(); + virtual int get_power_percent_left(); + virtual bool check_feature_support(const String& p_feature); OS_Windows(HINSTANCE _hInstance); diff --git a/platform/windows/power_windows.cpp b/platform/windows/power_windows.cpp new file mode 100644 index 0000000000..a19472ab8f --- /dev/null +++ b/platform/windows/power_windows.cpp @@ -0,0 +1,108 @@ +/*************************************************************************/ +/* power_windows.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* http://www.godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "power_windows.h" + +// CODE CHUNK IMPORTED FROM SDL 2.0 + +bool PowerWindows::GetPowerInfo_Windows() +{ + SYSTEM_POWER_STATUS status; + bool need_details = FALSE; + + /* This API should exist back to Win95. */ + if (!GetSystemPowerStatus(&status)) + { + /* !!! FIXME: push GetLastError() into GetError() */ + power_state = POWERSTATE_UNKNOWN; + } else if (status.BatteryFlag == 0xFF) { /* unknown state */ + power_state = POWERSTATE_UNKNOWN; + } else if (status.BatteryFlag & (1 << 7)) { /* no battery */ + power_state = POWERSTATE_NO_BATTERY; + } else if (status.BatteryFlag & (1 << 3)) { /* charging */ + power_state = POWERSTATE_CHARGING; + need_details = TRUE; + } else if (status.ACLineStatus == 1) { + power_state = POWERSTATE_CHARGED; /* on AC, not charging. */ + need_details = TRUE; + } else { + power_state = POWERSTATE_ON_BATTERY; /* not on AC. */ + need_details = TRUE; + } + + percent_left = -1; + nsecs_left = -1; + if (need_details) { + const int pct = (int) status.BatteryLifePercent; + const int secs = (int) status.BatteryLifeTime; + + if (pct != 255) { /* 255 == unknown */ + percent_left = (pct > 100) ? 100 : pct; /* clamp between 0%, 100% */ + } + if (secs != 0xFFFFFFFF) { /* ((DWORD)-1) == unknown */ + nsecs_left = secs; + } + } + + return TRUE; /* always the definitive answer on Windows. */ +} + +PowerState PowerWindows::get_power_state() { + if (GetPowerInfo_Windows()) { + return power_state; + } + else { + return POWERSTATE_UNKNOWN; + } +} + +int PowerWindows::get_power_seconds_left() { + if (GetPowerInfo_Windows()) { + return nsecs_left; + } + else { + return -1; + } +} + +int PowerWindows::get_power_percent_left() { + if (GetPowerInfo_Windows()) { + return percent_left; + } + else { + return -1; + } +} + +PowerWindows::PowerWindows() : nsecs_left(-1), percent_left(-1), power_state(POWERSTATE_UNKNOWN) { + +} + +PowerWindows::~PowerWindows() { +} \ No newline at end of file diff --git a/platform/windows/power_windows.h b/platform/windows/power_windows.h new file mode 100644 index 0000000000..a3a0b9568f --- /dev/null +++ b/platform/windows/power_windows.h @@ -0,0 +1,57 @@ +/*************************************************************************/ +/* power_windows.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* http://www.godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef PLATFORM_WINDOWS_POWER_WINDOWS_H_ +#define PLATFORM_WINDOWS_POWER_WINDOWS_H_ + +#include "os/dir_access.h" +#include "os/file_access.h" +#include "os/power.h" + +#include + +class PowerWindows { + +private: + int nsecs_left; + int percent_left; + PowerState power_state; + + bool GetPowerInfo_Windows(); + +public: + PowerWindows(); + virtual ~PowerWindows(); + + PowerState get_power_state(); + int get_power_seconds_left(); + int get_power_percent_left(); +}; + +#endif /* PLATFORM_WINDOWS_POWER_WINDOWS_H_ */ \ No newline at end of file -- cgit v1.2.3