Implement getting battery status on Windows

This commit is contained in:
Jyrki Vesterinen 2018-08-09 19:22:40 +03:00
parent ca7f7a2e93
commit 547e91755e
8 changed files with 104 additions and 3 deletions

View file

@ -741,6 +741,13 @@
<ClCompile Include="..\..\src\countdown_clock.cpp" />
<ClCompile Include="..\..\src\cursor.cpp" />
<ClCompile Include="..\..\src\deprecation.cpp" />
<ClCompile Include="..\..\src\desktop\battery_info.cpp">
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(IntDir)Desktop\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='ReleaseDEBUG|Win32'">$(IntDir)Desktop\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(IntDir)Desktop\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Test_Debug|Win32'">$(IntDir)Desktop\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Test_Release|Win32'">$(IntDir)Desktop\</ObjectFileName>
</ClCompile>
<ClCompile Include="..\..\src\desktop\clipboard.cpp">
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(IntDir)Desktop\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='ReleaseDEBUG|Win32'">$(IntDir)Desktop\</ObjectFileName>
@ -776,6 +783,13 @@
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Test_Debug|Win32'">$(IntDir)Desktop\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Test_Release|Win32'">$(IntDir)Desktop\</ObjectFileName>
</ClCompile>
<ClCompile Include="..\..\src\desktop\windows_battery_info.cpp">
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(IntDir)Desktop\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='ReleaseDEBUG|Win32'">$(IntDir)Desktop\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(IntDir)Desktop\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Test_Debug|Win32'">$(IntDir)Desktop\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Test_Release|Win32'">$(IntDir)Desktop\</ObjectFileName>
</ClCompile>
<ClCompile Include="..\..\src\desktop\windows_tray_notification.cpp">
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(IntDir)Desktop\</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='ReleaseDEBUG|Win32'">$(IntDir)Desktop\</ObjectFileName>
@ -3574,11 +3588,13 @@
<ClInclude Include="..\..\src\countdown_clock.hpp" />
<ClInclude Include="..\..\src\cursor.hpp" />
<ClInclude Include="..\..\src\deprecation.hpp" />
<ClInclude Include="..\..\src\desktop\battery_info.hpp" />
<ClInclude Include="..\..\src\desktop\clipboard.hpp" />
<ClInclude Include="..\..\src\desktop\notifications.hpp" />
<ClInclude Include="..\..\src\desktop\open.hpp" />
<ClInclude Include="..\..\src\desktop\paths.hpp" />
<ClInclude Include="..\..\src\desktop\version.hpp" />
<ClInclude Include="..\..\src\desktop\windows_battery_info.hpp" />
<ClInclude Include="..\..\src\desktop\windows_tray_notification.hpp" />
<ClInclude Include="..\..\src\display.hpp" />
<ClInclude Include="..\..\src\display_chat_manager.hpp" />

View file

@ -1554,6 +1554,12 @@
<ClCompile Include="..\..\src\preferences\lobby.cpp">
<Filter>Preferences</Filter>
</ClCompile>
<ClCompile Include="..\..\src\desktop\battery_info.cpp">
<Filter>Desktop</Filter>
</ClCompile>
<ClCompile Include="..\..\src\desktop\windows_battery_info.cpp">
<Filter>Desktop</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\addon\client.hpp">
@ -3030,6 +3036,12 @@
<ClInclude Include="..\..\src\preferences\lobby.hpp">
<Filter>Preferences</Filter>
</ClInclude>
<ClInclude Include="..\..\src\desktop\battery_info.hpp">
<Filter>Desktop</Filter>
</ClInclude>
<ClInclude Include="..\..\src\desktop\windows_battery_info.hpp">
<Filter>Desktop</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<CustomBuild Include="..\..\src\tests\test_sdl_utils.hpp">

View file

@ -256,11 +256,12 @@ endif(ENABLE_GAME OR ENABLE_TESTS)
GetSources("libwesnoth" libwesnoth-game_STAT_SRC)
# On windows only, this file needs to be linked, as its header is included #ifdef win32.
# On windows only, these files need to be linked, as their headers are included #ifdef win32.
if(WIN32)
set(libwesnoth-game_STAT_SRC
${libwesnoth-game_STAT_SRC}
desktop/windows_tray_notification.cpp
desktop/windows_battery_info.cpp
)
endif(WIN32)

View file

@ -87,6 +87,7 @@ wesnoth_client_sources = GetSources("wesnoth")
if env["PLATFORM"] == "win32":
wesnoth_client_sources.append("desktop/windows_tray_notification.cpp")
wesnoth_client_sources.append("desktop/windows_battery_info.cpp")
if env["PLATFORM"] == 'darwin':
wesnoth_client_sources.append("desktop/apple_battery_info.mm")

View file

@ -14,6 +14,10 @@
#include "battery_info.hpp"
#ifdef _WIN32
#include "desktop/windows_battery_info.hpp"
#endif
#ifdef __APPLE__
#include "apple_battery_info.hpp"
#endif
@ -24,7 +28,9 @@ namespace battery_info {
bool does_device_have_battery()
{
#if defined(__APPLE__)
#if defined(_WIN32)
return windows_battery_info::does_device_have_battery();
#elif defined(__APPLE__)
return apple_battery_info::does_device_have_battery();
#else
return false;
@ -33,7 +39,9 @@ bool does_device_have_battery()
double get_battery_percentage()
{
#if defined(__APPLE__)
#if defined(_WIN32)
return windows_battery_info::get_battery_percentage();
#elif defined(__APPLE__)
return apple_battery_info::get_battery_percentage();
#else
return -1;

View file

@ -18,7 +18,9 @@ namespace desktop {
namespace battery_info {
/// @return true if the device has a battery, false otherwise.
bool does_device_have_battery();
/// @return battery charge as a number between 0 and 100.
double get_battery_percentage();
} // end namespace battery_info

View file

@ -0,0 +1,39 @@
/*
Copyright (C) 2018 by Jyrki Vesterinen <sandgtx@gmail.com>
Part of the Battle for Wesnoth Project https://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "desktop/windows_battery_info.hpp"
#include <Windows.h>
bool windows_battery_info::does_device_have_battery()
{
SYSTEM_POWER_STATUS power_status;
BOOL success = GetSystemPowerStatus(&power_status);
if(success) {
return !(power_status.BatteryFlag & 128);
} else {
return false;
}
}
double windows_battery_info::get_battery_percentage()
{
SYSTEM_POWER_STATUS power_status;
BOOL success = GetSystemPowerStatus(&power_status);
if(success) {
return power_status.BatteryLifePercent;
} else {
return 0.0;
}
}

View file

@ -0,0 +1,22 @@
/*
Copyright (C) 2018 by Jyrki Vesterinen <sandgtx@gmail.com>
Part of the Battle for Wesnoth Project https://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#pragma once
class windows_battery_info
{
public:
static bool does_device_have_battery();
static double get_battery_percentage();
};