From 94103c0c025f04e75d5e163d9f0bdde27bb0c848 Mon Sep 17 00:00:00 2001 From: Julian Murgia Date: Sat, 23 Jul 2016 13:15:55 +0200 Subject: [PATCH] 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 --- core/bind/core_bind.cpp | 21 + core/bind/core_bind.h | 5 + core/os/os.cpp | 10 + core/os/os.h | 6 + core/os/power.h | 44 ++ platform/android/SCsub | 1 + platform/android/os_android.cpp | 2 + platform/android/os_android.h | 3 + platform/android/power_android.cpp | 238 ++++++++++ platform/android/power_android.h | 82 ++++ platform/bb10/os_bb10.cpp | 14 + platform/bb10/os_bb10.h | 6 + platform/bb10/power_bb10.cpp | 76 +++ platform/bb10/power_bb10.h | 49 ++ platform/haiku/os_haiku.cpp | 2 + platform/haiku/os_haiku.h | 6 + platform/haiku/power_haiku.cpp | 74 +++ platform/haiku/power_haiku.h | 49 ++ platform/iphone/power_iphone.cpp | 71 +++ platform/iphone/power_iphone.h | 50 ++ platform/javascript/os_javascript.cpp | 15 +- platform/javascript/os_javascript.h | 7 + platform/javascript/power_javascript.cpp | 76 +++ platform/javascript/power_javascript.h | 51 ++ platform/osx/SCsub | 1 + platform/osx/detect.py | 1 + platform/osx/os_osx.h | 7 + platform/osx/os_osx.mm | 15 + platform/osx/power_osx.cpp | 233 +++++++++ platform/osx/power_osx.h | 57 +++ platform/server/os_server.cpp | 12 + platform/server/os_server.h | 7 + platform/uwp/os_uwp.cpp | 14 + platform/uwp/os_uwp.h | 8 + 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 +++ platform/x11/SCsub | 1 + platform/x11/os_x11.cpp | 12 + platform/x11/os_x11.h | 7 + platform/x11/power_x11.cpp | 575 +++++++++++++++++++++++ platform/x11/power_x11.h | 68 +++ 44 files changed, 2162 insertions(+), 1 deletion(-) create mode 100644 core/os/power.h create mode 100644 platform/android/power_android.cpp create mode 100644 platform/android/power_android.h create mode 100644 platform/bb10/power_bb10.cpp create mode 100644 platform/bb10/power_bb10.h create mode 100644 platform/haiku/power_haiku.cpp create mode 100644 platform/haiku/power_haiku.h create mode 100644 platform/iphone/power_iphone.cpp create mode 100644 platform/iphone/power_iphone.h create mode 100644 platform/javascript/power_javascript.cpp create mode 100644 platform/javascript/power_javascript.h create mode 100644 platform/osx/power_osx.cpp create mode 100644 platform/osx/power_osx.h create mode 100644 platform/windows/power_windows.cpp create mode 100644 platform/windows/power_windows.h create mode 100644 platform/x11/power_x11.cpp create mode 100644 platform/x11/power_x11.h diff --git a/core/bind/core_bind.cpp b/core/bind/core_bind.cpp index 79cfae0a3a3..e4348cc4063 100644 --- a/core/bind/core_bind.cpp +++ b/core/bind/core_bind.cpp @@ -450,6 +450,17 @@ bool _OS::is_vsync_enabled() const { return OS::get_singleton()->is_vsync_enabled(); } +PowerState _OS::get_power_state() { + return OS::get_singleton()->get_power_state(); +} + +int _OS::get_power_seconds_left() { + return OS::get_singleton()->get_power_seconds_left(); +} + +int _OS::get_power_percent_left() { + return OS::get_singleton()->get_power_percent_left(); +} /* enum Weekday { @@ -1113,6 +1124,10 @@ void _OS::_bind_methods() { ClassDB::bind_method(D_METHOD("set_use_vsync","enable"),&_OS::set_use_vsync); ClassDB::bind_method(D_METHOD("is_vsync_enabled"),&_OS::is_vsync_enabled); + ClassDB::bind_method(D_METHOD("get_power_state"),&_OS::get_power_state); + ClassDB::bind_method(D_METHOD("get_power_seconds_left"),&_OS::get_power_seconds_left); + ClassDB::bind_method(D_METHOD("get_power_percent_left"),&_OS::get_power_percent_left); + BIND_CONSTANT( DAY_SUNDAY ); BIND_CONSTANT( DAY_MONDAY ); BIND_CONSTANT( DAY_TUESDAY ); @@ -1150,6 +1165,12 @@ void _OS::_bind_methods() { BIND_CONSTANT( SYSTEM_DIR_MUSIC ); BIND_CONSTANT( SYSTEM_DIR_PICTURES ); BIND_CONSTANT( SYSTEM_DIR_RINGTONES ); + + BIND_CONSTANT( POWERSTATE_UNKNOWN ); + BIND_CONSTANT( POWERSTATE_ON_BATTERY ); + BIND_CONSTANT( POWERSTATE_NO_BATTERY ); + BIND_CONSTANT( POWERSTATE_CHARGING ); + BIND_CONSTANT( POWERSTATE_CHARGED ); } diff --git a/core/bind/core_bind.h b/core/bind/core_bind.h index 4241912dddc..b6aa8737673 100644 --- a/core/bind/core_bind.h +++ b/core/bind/core_bind.h @@ -35,6 +35,7 @@ #include "os/dir_access.h" #include "os/thread.h" #include "os/semaphore.h" +#include "os/power.h" class _ResourceLoader : public Object { @@ -308,6 +309,10 @@ public: void set_use_vsync(bool p_enable); bool is_vsync_enabled() const; + PowerState get_power_state(); + int get_power_seconds_left(); + int get_power_percent_left(); + static _OS *get_singleton() { return singleton; } _OS(); diff --git a/core/os/os.cpp b/core/os/os.cpp index 912f7f0b0f2..1d670b44662 100644 --- a/core/os/os.cpp +++ b/core/os/os.cpp @@ -513,6 +513,16 @@ bool OS::is_vsync_enabled() const{ } +PowerState OS::get_power_state() { + return POWERSTATE_UNKNOWN; +} +int OS::get_power_seconds_left() { + return -1; +} +int OS::get_power_percent_left() { + return -1; +} + OS::OS() { last_error=NULL; singleton=this; diff --git a/core/os/os.h b/core/os/os.h index e179b82daef..7c8100679a9 100644 --- a/core/os/os.h +++ b/core/os/os.h @@ -34,8 +34,10 @@ #include "vector.h" #include "engine.h" #include "os/main_loop.h" +#include "power.h" #include + /** @author Juan Linietsky */ @@ -402,6 +404,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)=0; diff --git a/core/os/power.h b/core/os/power.h new file mode 100644 index 00000000000..c858d391a12 --- /dev/null +++ b/core/os/power.h @@ -0,0 +1,44 @@ +/*************************************************************************/ +/* power.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 CORE_OS_POWER_H_ +#define CORE_OS_POWER_H_ + + +typedef enum +{ + POWERSTATE_UNKNOWN, /**< cannot determine power status */ + POWERSTATE_ON_BATTERY, /**< Not plugged in, running on the battery */ + POWERSTATE_NO_BATTERY, /**< Plugged in, no battery available */ + POWERSTATE_CHARGING, /**< Plugged in, charging battery */ + POWERSTATE_CHARGED /**< Plugged in, battery charged */ +} PowerState; + + +#endif /* CORE_OS_POWER_H_ */ diff --git a/platform/android/SCsub b/platform/android/SCsub index 146f7c25a31..86f8c40f835 100644 --- a/platform/android/SCsub +++ b/platform/android/SCsub @@ -20,6 +20,7 @@ android_files = [ 'java_glue.cpp', 'cpu-features.c', 'java_class_wrapper.cpp' + 'power_android.cpp' ] # env.Depends('#core/math/vector3.h', 'vector3_psp.h') diff --git a/platform/android/os_android.cpp b/platform/android/os_android.cpp index 2290571735f..a10f27424cb 100644 --- a/platform/android/os_android.cpp +++ b/platform/android/os_android.cpp @@ -159,6 +159,8 @@ void OS_Android::initialize(const VideoMode& p_desired,int p_video_driver,int p_ input = memnew( InputDefault ); input->set_fallback_mapping("Default Android Gamepad"); + + power_manager = memnew( power_android ); } void OS_Android::set_main_loop( MainLoop * p_main_loop ) { diff --git a/platform/android/os_android.h b/platform/android/os_android.h index bf1db57ba58..ead39697440 100644 --- a/platform/android/os_android.h +++ b/platform/android/os_android.h @@ -32,6 +32,7 @@ #include "os/input.h" #include "drivers/unix/os_unix.h" #include "os/main_loop.h" +#include "power_android.h" #include "servers/physics/physics_server_sw.h" #include "servers/audio_server.h" #include "servers/physics_2d/physics_2d_server_sw.h" @@ -142,6 +143,8 @@ private: SetKeepScreenOnFunc set_keep_screen_on_func; AlertFunc alert_func; + power_android *power_manager; + public: // functions used by main to initialize/deintialize the OS diff --git a/platform/android/power_android.cpp b/platform/android/power_android.cpp new file mode 100644 index 00000000000..8cd5cb13974 --- /dev/null +++ b/platform/android/power_android.cpp @@ -0,0 +1,238 @@ +/*************************************************************************/ +/* power_android.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 "core/error_macros.h" + +#include "power_android.h" + +static void LocalReferenceHolder_Cleanup(struct LocalReferenceHolder *refholder) { + if (refholder->m_env) { + JNIEnv* env = refholder->m_env; + (*env)->PopLocalFrame(env, NULL); + --s_active; + } +} + +static struct LocalReferenceHolder LocalReferenceHolder_Setup(const char *func) +{ + struct LocalReferenceHolder refholder; + refholder.m_env = NULL; + refholder.m_func = func; + return refholder; +} + +static bool LocalReferenceHolder_Init(struct LocalReferenceHolder *refholder, JNIEnv *env) +{ + const int capacity = 16; + if ((*env)->PushLocalFrame(env, capacity) < 0) { + return false; + } + ++s_active; + refholder->m_env = env; + return true; +} + + +static SDL_bool LocalReferenceHolder_IsActive(void) +{ + return s_active > 0; +} + +ANativeWindow* Android_JNI_GetNativeWindow(void) +{ + ANativeWindow* anw; + jobject s; + JNIEnv *env = Android_JNI_GetEnv(); + + s = (*env)->CallStaticObjectMethod(env, mActivityClass, midGetNativeSurface); + anw = ANativeWindow_fromSurface(env, s); + (*env)->DeleteLocalRef(env, s); + + return anw; +} + + +/* + * CODE CHUNK IMPORTED FROM SDL 2.0 + * returns 0 on success or -1 on error (others undefined then) + * returns truthy or falsy value in plugged, charged and battery + * returns the value in seconds and percent or -1 if not available + */ +int Android_JNI_GetPowerInfo(int* plugged, int* charged, int* battery, int* seconds, int* percent) +{ + env = Android_JNI_GetEnv(); + refs = LocalReferenceHolder_Setup(__FUNCTION__); + + if (!LocalReferenceHolder_Init(&refs, env)) { + LocalReferenceHolder_Cleanup(&refs); + return -1; + } + mid = (*env)->GetStaticMethodID(env, mActivityClass, "getContext", "()Landroid/content/Context;"); + context = (*env)->CallStaticObjectMethod(env, mActivityClass, mid); + action = (*env)->NewStringUTF(env, "android.intent.action.BATTERY_CHANGED"); + cls = (*env)->FindClass(env, "android/content/IntentFilter"); + mid = (*env)->GetMethodID(env, cls, "", "(Ljava/lang/String;)V"); + filter = (*env)->NewObject(env, cls, mid, action); + (*env)->DeleteLocalRef(env, action); + mid = (*env)->GetMethodID(env, mActivityClass, "registerReceiver", "(Landroid/content/BroadcastReceiver;Landroid/content/IntentFilter;)Landroid/content/Intent;"); + intent = (*env)->CallObjectMethod(env, context, mid, NULL, filter); + (*env)->DeleteLocalRef(env, filter); + cls = (*env)->GetObjectClass(env, intent); + imid = (*env)->GetMethodID(env, cls, "getIntExtra", "(Ljava/lang/String;I)I"); + // Watch out for C89 scoping rules because of the macro +#define GET_INT_EXTRA(var, key) \ + int var; \ + iname = (*env)->NewStringUTF(env, key); \ + var = (*env)->CallIntMethod(env, intent, imid, iname, -1); \ + (*env)->DeleteLocalRef(env, iname); + bmid = (*env)->GetMethodID(env, cls, "getBooleanExtra", "(Ljava/lang/String;Z)Z"); + // Watch out for C89 scoping rules because of the macro +#define GET_BOOL_EXTRA(var, key) \ + int var; \ + bname = (*env)->NewStringUTF(env, key); \ + var = (*env)->CallBooleanMethod(env, intent, bmid, bname, JNI_FALSE); \ + (*env)->DeleteLocalRef(env, bname); + if (plugged) { + // Watch out for C89 scoping rules because of the macro + GET_INT_EXTRA(plug, "plugged") // == BatteryManager.EXTRA_PLUGGED (API 5) + if (plug == -1) { + LocalReferenceHolder_Cleanup(&refs); + return -1; + } + // 1 == BatteryManager.BATTERY_PLUGGED_AC + // 2 == BatteryManager.BATTERY_PLUGGED_USB + *plugged = (0 < plug) ? 1 : 0; + } + if (charged) { + // Watch out for C89 scoping rules because of the macro + GET_INT_EXTRA(status, "status") // == BatteryManager.EXTRA_STATUS (API 5) + if (status == -1) { + LocalReferenceHolder_Cleanup(&refs); + return -1; + } + // 5 == BatteryManager.BATTERY_STATUS_FULL + *charged = (status == 5) ? 1 : 0; + } + if (battery) { + GET_BOOL_EXTRA(present, "present") // == BatteryManager.EXTRA_PRESENT (API 5) + *battery = present ? 1 : 0; + } + if (seconds) { + *seconds = -1; // not possible + } + if (percent) { + int level; + int scale; + // Watch out for C89 scoping rules because of the macro + { + GET_INT_EXTRA(level_temp, "level") // == BatteryManager.EXTRA_LEVEL (API 5) + level = level_temp; + } + // Watch out for C89 scoping rules because of the macro + { + GET_INT_EXTRA(scale_temp, "scale") // == BatteryManager.EXTRA_SCALE (API 5) + scale = scale_temp; + } + if ((level == -1) || (scale == -1)) { + LocalReferenceHolder_Cleanup(&refs); + return -1; + } + *percent = level * 100 / scale; + } + (*env)->DeleteLocalRef(env, intent); + LocalReferenceHolder_Cleanup(&refs); + + return 0; +} + + +bool power_android::GetPowerInfo_Android() { + int battery; + int plugged; + int charged; + + if (Android_JNI_GetPowerInfo(&plugged, &charged, &battery, &this->nsecs_left, &this->percent_left) != -1) { + if (plugged) { + if (charged) { + this->power_state = POWERSTATE_CHARGED; + } else if (battery) { + this->power_state = POWERSTATE_CHARGING; + } else { + this->power_state = POWERSTATE_NO_BATTERY; + this->nsecs_left = -1; + this->percent_left = -1; + } + } else { + this->power_state = POWERSTATE_ON_BATTERY; + } + } else { + this->power_state = POWERSTATE_UNKNOWN; + this->nsecs_left = -1; + this->percent_left = -1; + } + + return true; +} + +PowerState power_android::get_power_state() { + if (GetPowerInfo_Android()) { + return power_state; + } + else { + WARN_PRINT("Power management is not implemented on this platform, defaulting to POWERSTATE_UNKNOWN"); + return POWERSTATE_UNKNOWN; + } +} + +int power_android::get_power_seconds_left() { + if (GetPowerInfo_Android()) { + return nsecs_left; + } + else { + WARN_PRINT("Power management is not implemented on this platform, defaulting to -1"); + return -1; + } +} + +int power_android::get_power_percent_left() { + if (GetPowerInfo_Android()) { + return percent_left; + } + else { + WARN_PRINT("Power management is not implemented on this platform, defaulting to -1"); + return -1; + } +} + +power_android::power_android() : nsecs_left(-1), percent_left(-1), power_state(POWERSTATE_UNKNOWN) { + +} + +power_android::~power_android() { +} diff --git a/platform/android/power_android.h b/platform/android/power_android.h new file mode 100644 index 00000000000..873f06de1d4 --- /dev/null +++ b/platform/android/power_android.h @@ -0,0 +1,82 @@ +/*************************************************************************/ +/* power_android.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_ANDROID_POWER_ANDROID_H_ +#define PLATFORM_ANDROID_POWER_ANDROID_H_ + +#include "os/power.h" +#include + +class power_android { + +struct LocalReferenceHolder +{ + JNIEnv *m_env; + const char *m_func; +}; + +private: + static struct LocalReferenceHolder refs; + static JNIEnv* env; + static jmethodID mid; + static jobject context; + static jstring action; + static jclass cls; + static jobject filter; + static jobject intent; + static jstring iname; + static jmethodID imid; + static jstring bname; + static jmethodID bmid; + + + int nsecs_left; + int percent_left; + PowerState power_state; + + bool GetPowerInfo_Android(); + bool UpdatePowerInfo(); + +public: + + static int s_active; + + + power_android(); + virtual ~power_android(); + static bool LocalReferenceHolder_Init(struct LocalReferenceHolder *refholder, JNIEnv *env); + static struct LocalReferenceHolder LocalReferenceHolder_Setup(const char *func); + static void LocalReferenceHolder_Cleanup(struct LocalReferenceHolder *refholder); + + PowerState get_power_state(); + int get_power_seconds_left(); + int get_power_percent_left(); +}; + +#endif /* PLATFORM_ANDROID_POWER_ANDROID_H_ */ \ No newline at end of file diff --git a/platform/bb10/os_bb10.cpp b/platform/bb10/os_bb10.cpp index 728707628bf..bf7bfb6909f 100644 --- a/platform/bb10/os_bb10.cpp +++ b/platform/bb10/os_bb10.cpp @@ -146,6 +146,8 @@ void OSBB10::initialize(const VideoMode& p_desired,int p_video_driver,int p_audi physics_2d_server->init(); input = memnew( InputDefault ); + + power_manager = memnew( PowerBB10 ); #ifdef PAYMENT_SERVICE_ENABLED payment_service = memnew(PaymentService); @@ -587,6 +589,18 @@ Size2 OSBB10::get_window_size() const { return Vector2(default_videomode.width, default_videomode.height); } +PowerState OSBB10::get_power_state() { + return power_manager->get_power_state(); +} + +int OSBB10::get_power_seconds_left() { + return power_manager->get_power_seconds_left(); +} + +int OSBB10::get_power_percent_left() { + return power_manager->get_power_percent_left(); +} + OSBB10::OSBB10() { main_loop=NULL; diff --git a/platform/bb10/os_bb10.h b/platform/bb10/os_bb10.h index 9cf2091d49c..678b8c9fc2f 100644 --- a/platform/bb10/os_bb10.h +++ b/platform/bb10/os_bb10.h @@ -39,6 +39,7 @@ #include "servers/visual/rasterizer.h" #include "audio_driver_bb10.h" #include "payment_service.h" +#include "power_bb10.h" #include #include @@ -58,6 +59,7 @@ class OSBB10 : public OS_Unix { PhysicsServer *physics_server; Physics2DServer *physics_2d_server; AudioDriverBB10* audio_driver; + PowerBB10 *power_manager; #ifdef PAYMENT_SERVICE_ENABLED PaymentService* payment_service; @@ -142,6 +144,10 @@ public: void run(); + virtual PowerState get_power_state(); + virtual int get_power_seconds_left(); + virtual int get_power_percent_left(); + OSBB10(); ~OSBB10(); diff --git a/platform/bb10/power_bb10.cpp b/platform/bb10/power_bb10.cpp new file mode 100644 index 00000000000..ef4a59d5d08 --- /dev/null +++ b/platform/bb10/power_bb10.cpp @@ -0,0 +1,76 @@ +/*************************************************************************/ +/* power_bb10.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_bb10.h" + +#include "core/error_macros.h" + + +bool PowerBB10::UpdatePowerInfo() { + + return false; +} + +PowerState PowerBB10::get_power_state() { + if (UpdatePowerInfo()) { + return power_state; + } + else { + WARN_PRINT("Power management is not implemented on this platform, defaulting to POWERSTATE_UNKNOWN"); + return POWERSTATE_UNKNOWN; + } +} + +int PowerBB10::get_power_seconds_left() { + if (UpdatePowerInfo()) { + return nsecs_left; + } + else { + WARN_PRINT("Power management is not implemented on this platform, defaulting to -1"); + return -1; + } +} + +int PowerBB10::get_power_percent_left() { + if (UpdatePowerInfo()) { + return percent_left; + } + else { + WARN_PRINT("Power management is not implemented on this platform, defaulting to -1"); + return -1; + } +} + +PowerBB10::PowerBB10() : nsecs_left(-1), percent_left(-1), power_state(POWERSTATE_UNKNOWN) { + +} + +PowerBB10::~PowerBB10() { +} + diff --git a/platform/bb10/power_bb10.h b/platform/bb10/power_bb10.h new file mode 100644 index 00000000000..156dd31df76 --- /dev/null +++ b/platform/bb10/power_bb10.h @@ -0,0 +1,49 @@ +/*************************************************************************/ +/* power_bb10.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_BB10_POWER_BB10_H_ +#define PLATFORM_BB10_POWER_BB10_H_ + +class PowerBB10 { +private: + int nsecs_left; + int percent_left; + PowerState power_state; + + bool UpdatePowerInfo(); +public: + PowerBB10(); + virtual ~PowerBB10(); + + PowerState get_power_state(); + int get_power_seconds_left(); + int get_power_percent_left(); +}; + +#endif /* PLATFORM_BB10_POWER_BB10_H_ */ \ No newline at end of file diff --git a/platform/haiku/os_haiku.cpp b/platform/haiku/os_haiku.cpp index 9f218aeff44..b9b95ddacd5 100644 --- a/platform/haiku/os_haiku.cpp +++ b/platform/haiku/os_haiku.cpp @@ -143,6 +143,8 @@ void OS_Haiku::initialize(const VideoMode& p_desired, int p_video_driver, int p_ if (AudioDriverManager::get_driver(p_audio_driver)->init() != OK) { ERR_PRINT("Initializing audio failed."); } + + power_manager = memnew( PowerHaiku ); } void OS_Haiku::finalize() { diff --git a/platform/haiku/os_haiku.h b/platform/haiku/os_haiku.h index 531c7a56c3f..2c84e95ac3e 100644 --- a/platform/haiku/os_haiku.h +++ b/platform/haiku/os_haiku.h @@ -40,6 +40,7 @@ #include "context_gl_haiku.h" #include "haiku_application.h" #include "haiku_direct_window.h" +#include "power_haiku.h" class OS_Haiku : public OS_Unix { @@ -53,6 +54,7 @@ private: VideoMode current_video_mode; PhysicsServer* physics_server; Physics2DServer* physics_2d_server; + PowerHaiku* power_manager; #ifdef MEDIA_KIT_ENABLED AudioDriverMediaKit driver_media_kit; @@ -114,6 +116,10 @@ public: virtual VideoMode get_video_mode(int p_screen=0) const; virtual void get_fullscreen_mode_list(List *p_list, int p_screen=0) const; virtual String get_executable_path() const; + + virtual PowerState get_power_state(); + virtual int get_power_seconds_left(); + virtual int get_power_percent_left(); }; #endif diff --git a/platform/haiku/power_haiku.cpp b/platform/haiku/power_haiku.cpp new file mode 100644 index 00000000000..ecedc46500b --- /dev/null +++ b/platform/haiku/power_haiku.cpp @@ -0,0 +1,74 @@ +/*************************************************************************/ +/* power_haiku.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 "core/error_macros.h" + +#include "power_haiku.h" + +bool PowerHaiku::UpdatePowerInfo() { + + return false; +} + +PowerState PowerHaiku::get_power_state() { + if (UpdatePowerInfo()) { + return power_state; + } + else { + WARN_PRINT("Power management is not implemented on this platform, defaulting to POWERSTATE_UNKNOWN"); + return POWERSTATE_UNKNOWN; + } +} + +int PowerX11::get_power_seconds_left() { + if (UpdatePowerInfo()) { + return nsecs_left; + } + else { + WARN_PRINT("Power management is not implemented on this platform, defaulting to -1"); + return -1; + } +} + +int PowerX11::get_power_percent_left() { + if (UpdatePowerInfo()) { + return percent_left; + } + else { + WARN_PRINT("Power management is not implemented on this platform, defaulting to -1"); + return -1; + } +} + +PowerHaiku::PowerHaiku() : nsecs_left(-1), percent_left(-1), power_state(POWERSTATE_UNKNOWN) { + +} + +PowerHaiku::~PowerHaiku() { +} \ No newline at end of file diff --git a/platform/haiku/power_haiku.h b/platform/haiku/power_haiku.h new file mode 100644 index 00000000000..0889e5880c8 --- /dev/null +++ b/platform/haiku/power_haiku.h @@ -0,0 +1,49 @@ +/*************************************************************************/ +/* power_haiku.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_HAIKU_POWER_HAIKU_H_ +#define PLATFORM_HAIKU_POWER_HAIKU_H_ + +class PowerHaiku { +private: + int nsecs_left; + int percent_left; + PowerState power_state; + + bool UpdatePowerInfo(); +public: + PowerHaiku(); + virtual ~PowerHaiku(); + + PowerState get_power_state(); + int get_power_seconds_left(); + int get_power_percent_left(); +}; + +#endif /* PLATFORM_HAIKU_POWER_HAIKU_H_ */ \ No newline at end of file diff --git a/platform/iphone/power_iphone.cpp b/platform/iphone/power_iphone.cpp new file mode 100644 index 00000000000..44013794ade --- /dev/null +++ b/platform/iphone/power_iphone.cpp @@ -0,0 +1,71 @@ +/*************************************************************************/ +/* power_iphone.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_iphone.h" + +bool PowerState::UpdatePowerInfo() { + return false; +} + + +PowerState PowerIphone::get_power_state() { + if (UpdatePowerInfo()) { + return power_state; + } + else { + return POWERSTATE_UNKNOWN; + } +} + +int PowerIphone::get_power_seconds_left() { + if (UpdatePowerInfo()) { + return nsecs_left; + } + else { + return -1; + } +} + +int PowerIphone::get_power_percent_left() { + if (UpdatePowerInfo()) { + return percent_left; + } + else { + return -1; + } +} + +PowerIphone::PowerIphone() : nsecs_left(-1), percent_left(-1), power_state(POWERSTATE_UNKNOWN) { + // TODO Auto-generated constructor stub + +} + +PowerIphone::~PowerIphone() { + // TODO Auto-generated destructor stub +} \ No newline at end of file diff --git a/platform/iphone/power_iphone.h b/platform/iphone/power_iphone.h new file mode 100644 index 00000000000..5ab2a6c27fd --- /dev/null +++ b/platform/iphone/power_iphone.h @@ -0,0 +1,50 @@ +/*************************************************************************/ +/* power_iphone.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_IPHONE_POWER_IPHONE_H_ +#define PLATFORM_IPHONE_POWER_IPHONE_H_ + +class PowerIphone { +private: + int nsecs_left; + int percent_left; + PowerState power_state; + + bool UpdatePowerInfo(); + +public: + PowerIphone(); + virtual ~PowerIphone(); + + PowerState get_power_state(); + int get_power_seconds_left(); + int get_power_percent_left(); +}; + +#endif /* PLATFORM_IPHONE_POWER_IPHONE_H_ */ \ No newline at end of file diff --git a/platform/javascript/os_javascript.cpp b/platform/javascript/os_javascript.cpp index 201008b1db6..d570d64acfc 100644 --- a/platform/javascript/os_javascript.cpp +++ b/platform/javascript/os_javascript.cpp @@ -259,6 +259,8 @@ void OS_JavaScript::initialize(const VideoMode& p_desired,int p_video_driver,int physics_2d_server->init(); input = memnew( InputDefault ); + + power_manager = memnew( PowerJavascript ); #define EM_CHECK(ev) if (result!=EMSCRIPTEN_RESULT_SUCCESS)\ ERR_PRINTS("Error while setting " #ev " callback: Code " + itos(result)) @@ -853,8 +855,19 @@ String OS_JavaScript::get_joy_guid(int p_device) const { return input->get_joy_guid_remapped(p_device); } -OS_JavaScript::OS_JavaScript(GFXInitFunc p_gfx_init_func,void*p_gfx_init_ud, GetDataDirFunc p_get_data_dir_func) { +PowerState OS_JavaScript::get_power_state() { + return power_manager->get_power_state(); +} +int OS_JavaScript::get_power_seconds_left() { + return power_manager->get_power_seconds_left(); +} + +int OS_JavaScript::get_power_percent_left() { + return power_manager->get_power_percent_left(); +} + +OS_JavaScript::OS_JavaScript(GFXInitFunc p_gfx_init_func,void*p_gfx_init_ud, GetDataDirFunc p_get_data_dir_func) { gfx_init_func=p_gfx_init_func; gfx_init_ud=p_gfx_init_ud; last_button_mask=0; diff --git a/platform/javascript/os_javascript.h b/platform/javascript/os_javascript.h index 582f128ce8c..5f6051bedbb 100644 --- a/platform/javascript/os_javascript.h +++ b/platform/javascript/os_javascript.h @@ -32,6 +32,7 @@ #include "os/input.h" #include "drivers/unix/os_unix.h" #include "os/main_loop.h" +#include "power_javascript.h" #include "servers/physics/physics_server_sw.h" #include "servers/audio_server.h" #include "servers/physics_2d/physics_2d_server_sw.h" @@ -81,6 +82,8 @@ private: GetDataDirFunc get_data_dir_func; + PowerJavascript *power_manager; + #ifdef JAVASCRIPT_EVAL_ENABLED JavaScript* javascript_eval; #endif @@ -174,6 +177,10 @@ public: virtual bool is_joy_known(int p_device); virtual String get_joy_guid(int p_device) const; bool joy_connection_changed(int p_type, const EmscriptenGamepadEvent *p_event); + + virtual PowerState get_power_state(); + virtual int get_power_seconds_left(); + virtual int get_power_percent_left(); OS_JavaScript(GFXInitFunc p_gfx_init_func,void*p_gfx_init_ud, GetDataDirFunc p_get_data_dir_func); ~OS_JavaScript(); diff --git a/platform/javascript/power_javascript.cpp b/platform/javascript/power_javascript.cpp new file mode 100644 index 00000000000..d535ea4a5a3 --- /dev/null +++ b/platform/javascript/power_javascript.cpp @@ -0,0 +1,76 @@ +/*************************************************************************/ +/* power_javascript.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 "core/error_macros.h" +#include "power_javascript.h" + + +bool PowerJavascript::UpdatePowerInfo() { + // TODO Javascript implementation + return false; +} + +PowerState PowerJavascript::get_power_state() { + if (UpdatePowerInfo()) { + return power_state; + } + else { + WARN_PRINT("Power management is not implemented on this platform, defaulting to POWERSTATE_UNKNOWN"); + return POWERSTATE_UNKNOWN; + } +} + +int PowerJavascript::get_power_seconds_left() { + if (UpdatePowerInfo()) { + return nsecs_left; + } + else { + WARN_PRINT("Power management is not implemented on this platform, defaulting to -1"); + return -1; + } +} + +int PowerJavascript::get_power_percent_left() { + if (UpdatePowerInfo()) { + return percent_left; + } + else { + WARN_PRINT("Power management is not implemented on this platform, defaulting to -1"); + return -1; + } +} + + +PowerJavascript::PowerJavascript() : nsecs_left(-1), percent_left(-1), power_state(POWERSTATE_UNKNOWN) { + +} + +PowerJavascript::~PowerJavascript() { +} \ No newline at end of file diff --git a/platform/javascript/power_javascript.h b/platform/javascript/power_javascript.h new file mode 100644 index 00000000000..e93737f98a1 --- /dev/null +++ b/platform/javascript/power_javascript.h @@ -0,0 +1,51 @@ +/*************************************************************************/ +/* power_javascript.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_JAVASCRIPT_POWER_JAVASCRIPT_H_ +#define PLATFORM_JAVASCRIPT_POWER_JAVASCRIPT_H_ + +class PowerJavascript { +private: + int nsecs_left; + int percent_left; + PowerState power_state; + + bool UpdatePowerInfo(); + +public: + PowerJavascript(); + virtual ~PowerJavascript(); + + PowerState get_power_state(); + int get_power_seconds_left(); + int get_power_percent_left(); +}; + +#endif /* PLATFORM_JAVASCRIPT_POWER_JAVASCRIPT_H_ */ \ No newline at end of file diff --git a/platform/osx/SCsub b/platform/osx/SCsub index 00f23687cf5..1427c2e00d0 100644 --- a/platform/osx/SCsub +++ b/platform/osx/SCsub @@ -10,6 +10,7 @@ files = [ # 'context_gl_osx.cpp', 'dir_access_osx.mm', 'joypad_osx.cpp', + 'power_osx.cpp', ] env.Program('#bin/godot', files) diff --git a/platform/osx/detect.py b/platform/osx/detect.py index ccd86177ab7..b59dfe1afb2 100644 --- a/platform/osx/detect.py +++ b/platform/osx/detect.py @@ -100,3 +100,4 @@ def configure(env): #env.Append( BUILDERS = { 'HLSL9' : env.Builder(action = methods.build_hlsl_dx9_headers, suffix = 'hlsl.h',src_suffix = '.hlsl') } ) env["x86_libtheora_opt_gcc"] = True + diff --git a/platform/osx/os_osx.h b/platform/osx/os_osx.h index d8c35472f23..9941774b403 100644 --- a/platform/osx/os_osx.h +++ b/platform/osx/os_osx.h @@ -32,6 +32,7 @@ #include "os/input.h" #include "joypad_osx.h" +#include "power_osx.h" #include "drivers/unix/os_unix.h" #include "main/input_default.h" #include "servers/visual_server.h" @@ -107,6 +108,8 @@ public: Size2 window_size; int current_screen; Rect2 restore_rect; + + power_osx *power_manager; float _mouse_scale(float p_scale) { if (display_scale>1.0) @@ -200,6 +203,10 @@ public: virtual bool is_window_maximized() const; virtual void request_attention(); virtual String get_joy_guid(int p_device) const; + + virtual PowerState get_power_state(); + virtual int get_power_seconds_left(); + virtual int get_power_percent_left(); void run(); diff --git a/platform/osx/os_osx.mm b/platform/osx/os_osx.mm index 7108d94b5b8..0699978caf9 100644 --- a/platform/osx/os_osx.mm +++ b/platform/osx/os_osx.mm @@ -1119,6 +1119,8 @@ void OS_OSX::initialize(const VideoMode& p_desired,int p_video_driver,int p_audi input = memnew( InputDefault ); joypad_osx = memnew( JoypadOSX ); + power_manager = memnew( power_osx ); + _ensure_data_dir(); NSArray *screenArray = [NSScreen screens]; @@ -1758,10 +1760,23 @@ OS::MouseMode OS_OSX::get_mouse_mode() const { return mouse_mode; } + String OS_OSX::get_joy_guid(int p_device) const { return input->get_joy_guid_remapped(p_device); } +PowerState OS_OSX::get_power_state() { + return power_manager->get_power_state(); +} + +int OS_OSX::get_power_seconds_left() { + return power_manager->get_power_seconds_left(); +} + +int OS_OSX::get_power_percent_left() { + return power_manager->get_power_percent_left(); +} + OS_OSX* OS_OSX::singleton=NULL; OS_OSX::OS_OSX() { diff --git a/platform/osx/power_osx.cpp b/platform/osx/power_osx.cpp new file mode 100644 index 00000000000..0b20a78b929 --- /dev/null +++ b/platform/osx/power_osx.cpp @@ -0,0 +1,233 @@ +/*************************************************************************/ +/* power_osx.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_osx.h" + +#include +#include +#include + +// CODE CHUNK IMPORTED FROM SDL 2.0 + +/* CoreFoundation is so verbose... */ +#define STRMATCH(a,b) (CFStringCompare(a, b, 0) == kCFCompareEqualTo) +#define GETVAL(k,v) \ + CFDictionaryGetValueIfPresent(dict, CFSTR(k), (const void **) v) + +/* Note that AC power sources also include a laptop battery it is charging. */ +void power_osx::checkps(CFDictionaryRef dict, bool * have_ac, bool * have_battery, bool * charging) +{ + CFStringRef strval; /* don't CFRelease() this. */ + CFBooleanRef bval; + CFNumberRef numval; + bool charge = false; + bool choose = false; + bool is_ac = false; + int secs = -1; + int maxpct = -1; + int pct = -1; + + if ((GETVAL(kIOPSIsPresentKey, &bval)) && (bval == kCFBooleanFalse)) { + return; /* nothing to see here. */ + } + + if (!GETVAL(kIOPSPowerSourceStateKey, &strval)) { + return; + } + + if (STRMATCH(strval, CFSTR(kIOPSACPowerValue))) { + is_ac = *have_ac = true; + } else if (!STRMATCH(strval, CFSTR(kIOPSBatteryPowerValue))) { + return; /* not a battery? */ + } + + if ((GETVAL(kIOPSIsChargingKey, &bval)) && (bval == kCFBooleanTrue)) { + charge = true; + } + + if (GETVAL(kIOPSMaxCapacityKey, &numval)) { + SInt32 val = -1; + CFNumberGetValue(numval, kCFNumberSInt32Type, &val); + if (val > 0) { + *have_battery = true; + maxpct = (int) val; + } + } + + if (GETVAL(kIOPSMaxCapacityKey, &numval)) { + SInt32 val = -1; + CFNumberGetValue(numval, kCFNumberSInt32Type, &val); + if (val > 0) { + *have_battery = true; + maxpct = (int) val; + } + } + + if (GETVAL(kIOPSTimeToEmptyKey, &numval)) { + SInt32 val = -1; + CFNumberGetValue(numval, kCFNumberSInt32Type, &val); + + /* Mac OS X reports 0 minutes until empty if you're plugged in. :( */ + if ((val == 0) && (is_ac)) { + val = -1; /* !!! FIXME: calc from timeToFull and capacity? */ + } + + secs = (int) val; + if (secs > 0) { + secs *= 60; /* value is in minutes, so convert to seconds. */ + } + } + + if (GETVAL(kIOPSCurrentCapacityKey, &numval)) { + SInt32 val = -1; + CFNumberGetValue(numval, kCFNumberSInt32Type, &val); + pct = (int) val; + } + + if ((pct > 0) && (maxpct > 0)) { + pct = (int) ((((double) pct) / ((double) maxpct)) * 100.0); + } + + if (pct > 100) { + pct = 100; + } + + /* + * We pick the battery that claims to have the most minutes left. + * (failing a report of minutes, we'll take the highest percent.) + */ + if ((secs < 0) && (nsecs_left < 0)) { + if ((pct < 0) && (percent_left < 0)) { + choose = true; /* at least we know there's a battery. */ + } + if (pct > percent_left) { + choose = true; + } + } else if (secs > nsecs_left) { + choose = true; + } + + if (choose) { + nsecs_left = secs; + percent_left = pct; + *charging = charge; + } +} + +#undef GETVAL +#undef STRMATCH + +// CODE CHUNK IMPORTED FROM SDL 2.0 +bool power_osx::GetPowerInfo_MacOSX() +{ + CFTypeRef blob = IOPSCopyPowerSourcesInfo(); + + nsecs_left = -1; + percent_left = -1; + power_state = POWERSTATE_UNKNOWN; + + if (blob != NULL) { + CFArrayRef list = IOPSCopyPowerSourcesList(blob); + if (list != NULL) { + /* don't CFRelease() the list items, or dictionaries! */ + bool have_ac = false; + bool have_battery = false; + bool charging = false; + const CFIndex total = CFArrayGetCount(list); + CFIndex i; + for (i = 0; i < total; i++) { + CFTypeRef ps = (CFTypeRef) CFArrayGetValueAtIndex(list, i); + CFDictionaryRef dict = IOPSGetPowerSourceDescription(blob, ps); + if (dict != NULL) { + checkps(dict, &have_ac, &have_battery, &charging); + } + } + + if (!have_battery) { + power_state = POWERSTATE_NO_BATTERY; + } else if (charging) { + power_state = POWERSTATE_CHARGING; + } else if (have_ac) { + power_state = POWERSTATE_CHARGED; + } else { + power_state = POWERSTATE_ON_BATTERY; + } + + CFRelease(list); + } + CFRelease(blob); + } + + return true; /* always the definitive answer on Mac OS X. */ +} + + + +bool power_osx::UpdatePowerInfo() { + if (GetPowerInfo_MacOSX()) { + return true; + } + return false; +} + + +PowerState power_osx::get_power_state() { + if (UpdatePowerInfo()) { + return power_state; + } + else { + return POWERSTATE_UNKNOWN; + } +} + +int power_osx::get_power_seconds_left() { + if (UpdatePowerInfo()) { + return nsecs_left; + } + else { + return -1; + } +} + +int power_osx::get_power_percent_left() { + if (UpdatePowerInfo()) { + return percent_left; + } + else { + return -1; + } +} + + +power_osx::power_osx() : nsecs_left(-1), percent_left(-1), power_state(POWERSTATE_UNKNOWN) { + +} + +power_osx::~power_osx() { + +} \ No newline at end of file diff --git a/platform/osx/power_osx.h b/platform/osx/power_osx.h new file mode 100644 index 00000000000..a8ab50d9f20 --- /dev/null +++ b/platform/osx/power_osx.h @@ -0,0 +1,57 @@ +/*************************************************************************/ +/* power_osx.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_OSX_POWER_OSX_H_ +#define PLATFORM_OSX_POWER_OSX_H_ + +#include "dir_access_osx.h" +#include "os/file_access.h" +#include "os/power.h" +#include + +class power_osx { + +private: + int nsecs_left; + int percent_left; + PowerState power_state; + void checkps(CFDictionaryRef dict, bool * have_ac, bool * have_battery, bool * charging); + bool GetPowerInfo_MacOSX(/*PowerState * state, int *seconds, int *percent*/); + bool UpdatePowerInfo(); + +public: + power_osx(); + virtual ~power_osx(); + + PowerState get_power_state(); + int get_power_seconds_left(); + int get_power_percent_left(); +}; + +#endif /* PLATFORM_OSX_POWER_OSX_H_ */ \ No newline at end of file diff --git a/platform/server/os_server.cpp b/platform/server/os_server.cpp index d691ae35bd4..ee80080b4e6 100644 --- a/platform/server/os_server.cpp +++ b/platform/server/os_server.cpp @@ -217,6 +217,18 @@ void OS_Server::set_cursor_shape(CursorShape p_shape) { } +PowerState OS_Server::get_power_state() { + return power_manager->get_power_state(); +} + +int OS_Server::get_power_seconds_left() { + return power_manager->get_power_seconds_left(); +} + +int OS_Server::get_power_percent_left() { + return power_manager->get_power_percent_left(); +} + void OS_Server::run() { force_quit = false; diff --git a/platform/server/os_server.h b/platform/server/os_server.h index 2b6225c48de..a0350767269 100644 --- a/platform/server/os_server.h +++ b/platform/server/os_server.h @@ -38,6 +38,7 @@ #include "servers/audio_server.h" #include "drivers/rtaudio/audio_driver_rtaudio.h" #include "servers/physics_2d/physics_2d_server_sw.h" +#include "../x11/power_x11.h" //bitch #undef CursorShape @@ -65,6 +66,8 @@ class OS_Server : public OS_Unix { bool force_quit; InputDefault *input; + + PowerX11 *power_manager; @@ -105,6 +108,10 @@ public: virtual void move_window_to_foreground(); void run(); + + virtual PowerState get_power_state(); + virtual int get_power_seconds_left(); + virtual int get_power_percent_left(); OS_Server(); }; diff --git a/platform/uwp/os_uwp.cpp b/platform/uwp/os_uwp.cpp index 61b8a1c240f..f2a99fe62c7 100644 --- a/platform/uwp/os_uwp.cpp +++ b/platform/uwp/os_uwp.cpp @@ -292,6 +292,8 @@ void OSUWP::initialize(const VideoMode& p_desired,int p_video_driver,int p_audio ERR_PRINT("Initializing audio failed."); } + power_manager = memnew ( PowerWinRT ); + managed_object->update_clipboard(); Clipboard::ContentChanged += ref new EventHandler(managed_object, &ManagedType::on_clipboard_changed); @@ -919,6 +921,18 @@ String OSUWP::get_data_dir() const { return String(data_folder->Path->Data()).replace("\\", "/"); } +PowerState OSWinrt::get_power_state() { + return power_manager->get_power_state(); +} + +int OSWinrt::get_power_seconds_left() { + return power_manager->get_power_seconds_left(); +} + +int OSWinrt::get_power_percent_left() { + return power_manager->get_power_percent_left(); +} + OSUWP::OSUWP() { diff --git a/platform/uwp/os_uwp.h b/platform/uwp/os_uwp.h index f65aa2d8d92..72cc8a78545 100644 --- a/platform/uwp/os_uwp.h +++ b/platform/uwp/os_uwp.h @@ -42,12 +42,14 @@ #include "core/ustring.h" #include "main/input_default.h" #include "joypad_uwp.h" +#include "power_winrt.h" #include #include #include #include + /** @author Juan Linietsky */ @@ -111,6 +113,8 @@ private: AudioDriverXAudio2 audio_driver; + PowerWinRT *power_manager; + MouseMode mouse_mode; bool alt_mem; bool gr_mem; @@ -260,6 +264,10 @@ public: virtual bool get_swap_ok_cancel() { return true; } void input_event(InputEvent &p_event); + + virtual PowerState get_power_state(); + virtual int get_power_seconds_left(); + virtual int get_power_percent_left(); void queue_key_event(KeyEvent &p_event); diff --git a/platform/windows/SCsub b/platform/windows/SCsub index ae8c07384f0..befbe001838 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 0ef964522b7..8a347e5f32c 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 73a2d5f4510..c8cacac7a2e 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 00000000000..a19472ab8f0 --- /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 00000000000..a3a0b9568f2 --- /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 diff --git a/platform/x11/SCsub b/platform/x11/SCsub index 4ae8ac07f7b..fc9208c5633 100644 --- a/platform/x11/SCsub +++ b/platform/x11/SCsub @@ -8,6 +8,7 @@ common_x11 = [\ "os_x11.cpp",\ "key_mapping_x11.cpp",\ "joypad_linux.cpp",\ + "power_x11.cpp",\ ] env.Program('#bin/godot', ['godot_x11.cpp'] + common_x11) diff --git a/platform/x11/os_x11.cpp b/platform/x11/os_x11.cpp index a4ed08f3309..689fe076f12 100644 --- a/platform/x11/os_x11.cpp +++ b/platform/x11/os_x11.cpp @@ -2007,6 +2007,18 @@ void OS_X11::set_context(int p_context) { } } +PowerState OS_X11::get_power_state() { + return power_manager->get_power_state(); +} + +int OS_X11::get_power_seconds_left() { + return power_manager->get_power_seconds_left(); +} + +int OS_X11::get_power_percent_left() { + return power_manager->get_power_percent_left(); +} + OS_X11::OS_X11() { #ifdef RTAUDIO_ENABLED diff --git a/platform/x11/os_x11.h b/platform/x11/os_x11.h index 3ec358f1032..ce2e2df6e93 100644 --- a/platform/x11/os_x11.h +++ b/platform/x11/os_x11.h @@ -46,6 +46,7 @@ #include "servers/physics_2d/physics_2d_server_wrap_mt.h" #include "main/input_default.h" #include "joypad_linux.h" +#include "power_x11.h" #include #include @@ -164,6 +165,8 @@ class OS_X11 : public OS_Unix { AudioDriverDummy driver_dummy; Atom net_wm_icon; + + PowerX11 *power_manager; int audio_driver_index; unsigned int capture_idle; @@ -260,6 +263,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(); + void run(); OS_X11(); diff --git a/platform/x11/power_x11.cpp b/platform/x11/power_x11.cpp new file mode 100644 index 00000000000..f6966124b6a --- /dev/null +++ b/platform/x11/power_x11.cpp @@ -0,0 +1,575 @@ +/*************************************************************************/ +/* power_x11.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_x11.h" + +#include +#include + +#include +#include +#include +#include + +// CODE CHUNK IMPORTED FROM SDL 2.0 + +static const char* proc_apm_path = "/proc/apm"; +static const char* proc_acpi_battery_path = "/proc/acpi/battery"; +static const char* proc_acpi_ac_adapter_path = "/proc/acpi/ac_adapter"; +static const char* sys_class_power_supply_path = "/sys/class/power_supply"; + +FileAccessRef PowerX11::open_power_file(const char* base, const char* node, const char* key) +{ + String path = String(base) + String("/") + String(node) + String("/") + String(key); + FileAccessRef f = FileAccess::open(path,FileAccess::READ); + return f; +} + + +bool PowerX11::read_power_file(const char* base, const char* node, const char* key, char* buf, size_t buflen) +{ + ssize_t br = 0; + FileAccessRef fd = open_power_file(base, node, key); + if (!fd) { + return false; + } + br = fd->get_buffer(reinterpret_cast(buf), buflen-1); + fd->close(); + if (br < 0) { + return false; + } + buf[br] = '\0'; // null-terminate the string + return true; +} + + +bool PowerX11::make_proc_acpi_key_val(char **_ptr, char **_key, char **_val) +{ + char *ptr = *_ptr; + + while (*ptr == ' ') { + ptr++; /* skip whitespace. */ + } + + if (*ptr == '\0') { + return false; /* EOF. */ + } + + *_key = ptr; + + while ((*ptr != ':') && (*ptr != '\0')) { + ptr++; + } + + if (*ptr == '\0') { + return false; /* (unexpected) EOF. */ + } + + *(ptr++) = '\0'; /* terminate the key. */ + + while ((*ptr == ' ') && (*ptr != '\0')) { + ptr++; /* skip whitespace. */ + } + + if (*ptr == '\0') { + return false; /* (unexpected) EOF. */ + } + + *_val = ptr; + + while ((*ptr != '\n') && (*ptr != '\0')) { + ptr++; + } + + if (*ptr != '\0') { + *(ptr++) = '\0'; /* terminate the value. */ + } + + *_ptr = ptr; /* store for next time. */ + return true; +} + +void +PowerX11::check_proc_acpi_battery(const char * node, bool * have_battery, bool * charging) +{ + const char *base = proc_acpi_battery_path; + char info[1024]; + char state[1024]; + char *ptr = NULL; + char *key = NULL; + char *val = NULL; + bool charge = false; + bool choose = false; + int maximum = -1; + int remaining = -1; + int secs = -1; + int pct = -1; + + if (!read_power_file(base, node, "state", state, sizeof (state))) { + return; + } else { + if (!read_power_file(base, node, "info", info, sizeof (info))) + return; + } + + ptr = &state[0]; + while (make_proc_acpi_key_val(&ptr, &key, &val)) { + if (String(key) == "present") { + if (String(val) == "yes") { + *have_battery = true; + } + } else if (String(key) == "charging state") { + /* !!! FIXME: what exactly _does_ charging/discharging mean? */ + if (String(val) == "charging/discharging") { + charge = true; + } else if (String(val) == "charging") { + charge = true; + } + } else if (String(key) == "remaining capacity") { + char *endptr = NULL; + //const int cvt = (int) strtol(val, &endptr, 10); + String sval = val; + const int cvt = sval.to_int(); + if (*endptr == ' ') { + remaining = cvt; + } + } + } + + ptr = &info[0]; + while (make_proc_acpi_key_val(&ptr, &key, &val)) { + if (String(key) == "design capacity") { + char *endptr = NULL; + String sval = val; + const int cvt = sval.to_int(); + if (*endptr == ' ') { + maximum = cvt; + } + } + } + + if ((maximum >= 0) && (remaining >= 0)) { + pct = (int) ((((float) remaining) / ((float) maximum)) * 100.0f); + if (pct < 0) { + pct = 0; + } else if (pct > 100) { + pct = 100; + } + } + + /* !!! FIXME: calculate (secs). */ + + /* + * We pick the battery that claims to have the most minutes left. + * (failing a report of minutes, we'll take the highest percent.) + */ + if ((secs < 0) && (this->nsecs_left < 0)) { + if ((pct < 0) && (this->percent_left < 0)) { + choose = true; /* at least we know there's a battery. */ + } + if (pct > this->percent_left) { + choose = true; + } + } else if (secs > this->nsecs_left) { + choose = true; + } + + if (choose) { + this->nsecs_left = secs; + this->percent_left = pct; + *charging = charge; + } +} + +void PowerX11::check_proc_acpi_ac_adapter(const char * node, bool * have_ac) +{ + const char *base = proc_acpi_ac_adapter_path; + char state[256]; + char *ptr = NULL; + char *key = NULL; + char *val = NULL; + + if (!read_power_file(base, node, "state", state, sizeof (state))) { + return; + } + + ptr = &state[0]; + while (make_proc_acpi_key_val(&ptr, &key, &val)) { + String skey = key; + if (skey == "state") { + String sval = val; + if (sval == "on-line") { + *have_ac = true; + } + } + } +} + + +bool PowerX11::GetPowerInfo_Linux_proc_acpi() +{ + String node; + DirAccess *dirp = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); + bool have_battery = false; + bool have_ac = false; + bool charging = false; + + this->nsecs_left = -1; + this->percent_left = -1; + this->power_state = POWERSTATE_UNKNOWN; + + + dirp->change_dir(proc_acpi_battery_path); + dirp->list_dir_begin(); + + if (dirp == NULL) { + return false; /* can't use this interface. */ + } else { + node = dirp->get_next(); + while (node != "") { + check_proc_acpi_battery(node.utf8().get_data(), &have_battery, &charging/*, seconds, percent*/); + node = dirp->get_next(); + } + memdelete(dirp); + } + + dirp->change_dir(proc_acpi_ac_adapter_path); + dirp->list_dir_begin(); + if (dirp == NULL) { + return false; /* can't use this interface. */ + } else { + node = dirp->get_next(); + while (node != "") { + check_proc_acpi_ac_adapter(node.utf8().get_data(), &have_ac); + node = dirp->get_next(); + } + memdelete(dirp); + } + + if (!have_battery) { + this->power_state = POWERSTATE_NO_BATTERY; + } else if (charging) { + this->power_state = POWERSTATE_CHARGING; + } else if (have_ac) { + this->power_state = POWERSTATE_CHARGED; + } else { + this->power_state = POWERSTATE_ON_BATTERY; + } + + return true; /* definitive answer. */ +} + + +bool PowerX11::next_string(char **_ptr, char **_str) +{ + char *ptr = *_ptr; + char *str = *_str; + + while (*ptr == ' ') { /* skip any spaces... */ + ptr++; + } + + if (*ptr == '\0') { + return false; + } + + str = ptr; + while ((*ptr != ' ') && (*ptr != '\n') && (*ptr != '\0')) + ptr++; + + if (*ptr != '\0') + *(ptr++) = '\0'; + + *_str = str; + *_ptr = ptr; + return true; +} + +bool PowerX11::int_string(char *str, int *val) +{ + String sval = str; + *val = sval.to_int(); + return (*str != '\0'); +} + +/* http://lxr.linux.no/linux+v2.6.29/drivers/char/apm-emulation.c */ +bool PowerX11::GetPowerInfo_Linux_proc_apm() +{ + bool need_details = false; + int ac_status = 0; + int battery_status = 0; + int battery_flag = 0; + int battery_percent = 0; + int battery_time = 0; + FileAccessRef fd = FileAccess::open(proc_apm_path,FileAccess::READ); + char buf[128]; + char *ptr = &buf[0]; + char *str = NULL; + ssize_t br; + + if (!fd) { + return false; /* can't use this interface. */ + } + + br = fd->get_buffer(reinterpret_cast(buf), sizeof (buf) - 1); + fd->close(); + + if (br < 0) { + return false; + } + + buf[br] = '\0'; /* null-terminate the string. */ + if (!next_string(&ptr, &str)) { /* driver version */ + return false; + } + if (!next_string(&ptr, &str)) { /* BIOS version */ + return false; + } + if (!next_string(&ptr, &str)) { /* APM flags */ + return false; + } + + if (!next_string(&ptr, &str)) { /* AC line status */ + return false; + } else if (!int_string(str, &ac_status)) { + return false; + } + + if (!next_string(&ptr, &str)) { /* battery status */ + return false; + } else if (!int_string(str, &battery_status)) { + return false; + } + if (!next_string(&ptr, &str)) { /* battery flag */ + return false; + } else if (!int_string(str, &battery_flag)) { + return false; + } + if (!next_string(&ptr, &str)) { /* remaining battery life percent */ + return false; + } + String sstr = str; + if (sstr[sstr.length() - 1] == '%') { + sstr[sstr.length() - 1] = '\0'; + } + if (!int_string(str, &battery_percent)) { + return false; + } + + if (!next_string(&ptr, &str)) { /* remaining battery life time */ + return false; + } else if (!int_string(str, &battery_time)) { + return false; + } + + if (!next_string(&ptr, &str)) { /* remaining battery life time units */ + return false; + } else if (String(str) == "min") { + battery_time *= 60; + } + + if (battery_flag == 0xFF) { /* unknown state */ + this->power_state = POWERSTATE_UNKNOWN; + } else if (battery_flag & (1 << 7)) { /* no battery */ + this->power_state = POWERSTATE_NO_BATTERY; + } else if (battery_flag & (1 << 3)) { /* charging */ + this->power_state = POWERSTATE_CHARGING; + need_details = true; + } else if (ac_status == 1) { + this->power_state = POWERSTATE_CHARGED; /* on AC, not charging. */ + need_details = true; + } else { + this->power_state = POWERSTATE_ON_BATTERY; + need_details = true; + } + + this->percent_left = -1; + this->nsecs_left = -1; + if (need_details) { + const int pct = battery_percent; + const int secs = battery_time; + + if (pct >= 0) { /* -1 == unknown */ + this->percent_left = (pct > 100) ? 100 : pct; /* clamp between 0%, 100% */ + } + if (secs >= 0) { /* -1 == unknown */ + this->nsecs_left = secs; + } + } + + return true; +} + +/* !!! FIXME: implement d-bus queries to org.freedesktop.UPower. */ + +bool PowerX11::GetPowerInfo_Linux_sys_class_power_supply(/*PowerState *state, int *seconds, int *percent*/) +{ + const char* base = sys_class_power_supply_path; + String name; + + DirAccess *dirp = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); + dirp->change_dir(base); + dirp->list_dir_begin(); + + if (!dirp) { + return false; + } + + this->power_state = POWERSTATE_NO_BATTERY; /* assume we're just plugged in. */ + this->nsecs_left = -1; + this->percent_left = -1; + + name = dirp->get_next(); + + while (name != "") { + bool choose = false; + char str[64]; + PowerState st; + int secs; + int pct; + + if ((name == ".") || (name == "..")) { + name = dirp->get_next(); + continue; //skip these, of course. + } else { + if (!read_power_file(base, name.utf8().get_data(), "type", str, sizeof (str))) { + name = dirp->get_next(); + continue; // Don't know _what_ we're looking at. Give up on it. + } else { + if (String(str) != "Battery\n") { + name = dirp->get_next(); + continue; // we don't care about UPS and such. + } + } + } + + /* some drivers don't offer this, so if it's not explicitly reported assume it's present. */ + if (read_power_file(base, name.utf8().get_data(), "present", str, sizeof (str)) && (String(str) == "0\n")) { + st = POWERSTATE_NO_BATTERY; + } else if (!read_power_file(base, name.utf8().get_data(), "status", str, sizeof (str))) { + st = POWERSTATE_UNKNOWN; /* uh oh */ + } else if (String(str) == "Charging\n") { + st = POWERSTATE_CHARGING; + } else if (String(str) == "Discharging\n") { + st = POWERSTATE_ON_BATTERY; + } else if ((String(str) == "Full\n") || (String(str) == "Not charging\n")) { + st = POWERSTATE_CHARGED; + } else { + st = POWERSTATE_UNKNOWN; /* uh oh */ + } + + if (!read_power_file(base, name.utf8().get_data(), "capacity", str, sizeof (str))) { + pct = -1; + } else { + pct = String(str).to_int(); + pct = (pct > 100) ? 100 : pct; /* clamp between 0%, 100% */ + } + + if (!read_power_file(base, name.utf8().get_data(), "time_to_empty_now", str, sizeof (str))) { + secs = -1; + } else { + secs = String(str).to_int(); + secs = (secs <= 0) ? -1 : secs; /* 0 == unknown */ + } + + /* + * We pick the battery that claims to have the most minutes left. + * (failing a report of minutes, we'll take the highest percent.) + */ + if ((secs < 0) && (this->nsecs_left < 0)) { + if ((pct < 0) && (this->percent_left < 0)) { + choose = true; /* at least we know there's a battery. */ + } else if (pct > this->percent_left) { + choose = true; + } + } else if (secs > this->nsecs_left) { + choose = true; + } + + if (choose) { + this->nsecs_left = secs; + this->percent_left = pct; + this->power_state = st; + } + + name = dirp->get_next(); + } + + memdelete(dirp); + return true; /* don't look any further*/ +} + + + +bool PowerX11::UpdatePowerInfo() +{ + if (GetPowerInfo_Linux_sys_class_power_supply()) { // try method 1 + return true; + } + if (GetPowerInfo_Linux_proc_acpi()) { // try further + return true; + } + if (GetPowerInfo_Linux_proc_apm()) { // try even further + return true; + } + return false; +} + +PowerX11::PowerX11() : nsecs_left(-1), percent_left(-1), power_state(POWERSTATE_UNKNOWN) { +} + +PowerX11::~PowerX11() { +} + +PowerState PowerX11::get_power_state() { + if (UpdatePowerInfo()) { + return power_state; + } + else { + return POWERSTATE_UNKNOWN; + } +} + +int PowerX11::get_power_seconds_left() { + if (UpdatePowerInfo()) { + return nsecs_left; + } + else { + return -1; + } +} + +int PowerX11::get_power_percent_left() { + if (UpdatePowerInfo()) { + return percent_left; + } + else { + return -1; + } +} \ No newline at end of file diff --git a/platform/x11/power_x11.h b/platform/x11/power_x11.h new file mode 100644 index 00000000000..5d03cac3940 --- /dev/null +++ b/platform/x11/power_x11.h @@ -0,0 +1,68 @@ +/*************************************************************************/ +/* power_x11.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 X11_POWER_H_ +#define X11_POWER_H_ + +#include "os/dir_access.h" +#include "os/file_access.h" +#include "os/power.h" + + +class PowerX11 { + +private: + int nsecs_left; + int percent_left; + PowerState power_state; + + + FileAccessRef open_power_file(const char* base, const char* node, const char* key); + bool read_power_file(const char* base, const char* node, const char* key, char* buf, size_t buflen); + bool make_proc_acpi_key_val(char **_ptr, char **_key, char **_val); + void check_proc_acpi_battery(const char * node, bool * have_battery, bool * charging); + void check_proc_acpi_ac_adapter(const char * node, bool * have_ac); + bool GetPowerInfo_Linux_proc_acpi(); + bool next_string(char **_ptr, char **_str); + bool int_string(char *str, int *val); + bool GetPowerInfo_Linux_proc_apm(); + bool GetPowerInfo_Linux_sys_class_power_supply(); + bool UpdatePowerInfo(); + + +public: + PowerX11(); + virtual ~PowerX11(); + + PowerState get_power_state(); + int get_power_seconds_left(); + int get_power_percent_left(); +}; + +#endif /* X11_POWER_H_ */ \ No newline at end of file