Skip to content

Commit

Permalink
WIP: implement battery status querying on GNU/Linux
Browse files Browse the repository at this point in the history
This code is supposed to work in theory, but in practice (at least on my
PC) it fails with this error message:

The name org.freedesktop.UPower was not provided by any .service files

I'll need to investigate later on why it's happening.
  • Loading branch information
jyrkive committed Oct 30, 2018
1 parent 34d93d4 commit 291a02f
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Expand Up @@ -279,7 +279,7 @@ endif(APPLE)
if(LIBDBUS_FOUND)
set(libwesnoth-game_STAT_SRC
${libwesnoth-game_STAT_SRC}
desktop/dbus_notification.cpp
desktop/dbus_features.cpp
)
endif(LIBDBUS_FOUND)

Expand Down
2 changes: 1 addition & 1 deletion src/SConscript
Expand Up @@ -95,7 +95,7 @@ if env["PLATFORM"] == 'darwin':
wesnoth_client_sources.append("desktop/apple_version.mm")

if env["notifications"]:
wesnoth_client_sources.append("desktop/dbus_notification.cpp")
wesnoth_client_sources.append("desktop/dbus_features.cpp")

libwesnoth_client = client_env.Library("wesnoth-client", wesnoth_client_sources)

Expand Down
8 changes: 8 additions & 0 deletions src/desktop/battery_info.cpp
Expand Up @@ -22,6 +22,10 @@
#include "apple_battery_info.hpp"
#endif

#ifdef HAVE_LIBDBUS
#include "desktop/dbus_features.hpp"
#endif

namespace desktop {
namespace battery_info {

Expand All @@ -31,6 +35,8 @@ bool does_device_have_battery()
return windows_battery_info::does_device_have_battery();
#elif defined(__APPLE__)
return desktop::battery_info::apple::does_device_have_battery();
#elif defined(HAVE_LIBDBUS)
return dbus::does_device_have_battery();
#else
return false;
#endif
Expand All @@ -42,6 +48,8 @@ double get_battery_percentage()
return windows_battery_info::get_battery_percentage();
#elif defined(__APPLE__)
return desktop::battery_info::apple::get_battery_percentage();
#elif defined(HAVE_LIBDBUS)
return dbus::get_battery_percentage();
#else
return -1;
#endif
Expand Down
Expand Up @@ -12,14 +12,14 @@
See the COPYING file for more details.
*/

#include "desktop/dbus_notification.hpp"
#include "desktop/dbus_features.hpp"

#include "filesystem.hpp"
#include "game_config.hpp"
#include "log.hpp"

#ifndef HAVE_LIBDBUS
#error "The HAVE_LIBDBUS symbol is not defined, you do not have lib dbus available, you should not be trying to compile dbus_notification.cpp"
#error "The HAVE_LIBDBUS symbol is not defined, you do not have lib dbus available, you should not be trying to compile dbus_features.cpp"
#endif

#include <dbus/dbus.h>
Expand All @@ -28,6 +28,8 @@
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/member.hpp>
#include <cstdlib>
#include <functional>
#include <memory>
#include <string>

#pragma GCC diagnostic ignored "-Wold-style-cast"
Expand Down Expand Up @@ -196,6 +198,45 @@ uint32_t send_dbus_notification(DBusConnection *connection, uint32_t replaces_id
return id;
}

template<typename T>
T get_power_source_property(const std::string &name, int type, T fallback)
{
DBusConnection *connection = get_dbus_connection();
if (!connection) return fallback;

std::unique_ptr<DBusMessage, std::function<void(DBusMessage*)>> msg(dbus_message_new_method_call(
"org.freedesktop.UPower",
"/org/freedesktop/UPower/devices/DisplayDevice",
"org.freedesktop.DBus.Properties",
"Get"),
dbus_message_unref);

const char *interface_name = "org.freedesktop.UPower.Device";
const char *property_name = name.data();
dbus_message_append_args(msg.get(),
DBUS_TYPE_STRING, &interface_name,
DBUS_TYPE_STRING, &property_name,
DBUS_TYPE_INVALID);

DBusError err;
dbus_error_init(&err);

std::unique_ptr<DBusMessage, std::function<void(DBusMessage*)>> ret(dbus_connection_send_with_reply_and_block(
connection, msg.get(), 1000, &err), dbus_message_unref);
if (ret == nullptr) {
DBG_DU << "Failed to query power source properties: " << err.message << '\n';
dbus_error_free(&err);
return fallback;
}

T value;
dbus_message_get_args(ret.get(), nullptr,
type, &value,
DBUS_TYPE_INVALID);

return value;
}

} // end anonymous namespace

namespace dbus {
Expand Down Expand Up @@ -243,4 +284,14 @@ void send_notification(const std::string & owner, const std::string & message, b
}
}

bool does_device_have_battery()
{
return get_power_source_property<uint32_t>("Type", DBUS_TYPE_UINT32, 0) == 2;
}

double get_battery_percentage()
{
return get_power_source_property<double>("Percentage", DBUS_TYPE_DOUBLE, 0.0);
}

} // end namespace dbus
Expand Up @@ -18,4 +18,6 @@

namespace dbus {
void send_notification(const std::string& owner, const std::string& message, bool with_history);
bool does_device_have_battery();
double get_battery_percentage();
}
2 changes: 1 addition & 1 deletion src/desktop/notifications.cpp
Expand Up @@ -20,7 +20,7 @@
#include "video.hpp" //CVideo::get_singleton().window_state()

#ifdef HAVE_LIBDBUS
#include "desktop/dbus_notification.hpp"
#include "desktop/dbus_features.hpp"
#endif

#ifdef __APPLE__
Expand Down

0 comments on commit 291a02f

Please sign in to comment.