Skip to content

Commit

Permalink
GTK4: Migration gamemode
Browse files Browse the repository at this point in the history
Signed-off-by: Viktar Lukashonak <myxabeer@gmail.com>
  • Loading branch information
LukashonakV committed Mar 24, 2024
1 parent 7e61e5f commit d3e32d1
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 87 deletions.
12 changes: 6 additions & 6 deletions include/bar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,11 @@ class Bar {
void handleSignal(int);

struct waybar_output *output;
Json::Value config;
struct wl_surface *surface;
bool visible = true;
Gtk::Window window;
Glib::RefPtr<Gdk::Surface> gdk_surface_;
Gtk::Orientation orientation = Gtk::Orientation::HORIZONTAL;
Gtk::PositionType position = Gtk::PositionType::TOP;

int x_global;
int y_global;

#ifdef HAVE_SWAY
std::string bar_id;
#endif
Expand All @@ -93,6 +87,12 @@ class Bar {
void configureGlobalOffset(int width, int height);
void onOutputGeometryChanged();

Glib::RefPtr<Gdk::Surface> gdk_surface_;
struct wl_surface *surface;
int x_global;
int y_global;
Json::Value config;

/* Copy initial set of modes to allow customization */
bar_mode_map configured_modes = PRESET_MODES;
std::string last_mode_{MODE_DEFAULT};
Expand Down
20 changes: 7 additions & 13 deletions include/modules/gamemode.hpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
#pragma once

#include <iostream>
#include <map>
#include <string>

#include "ALabel.hpp"
#include "giomm/dbusconnection.h"
#include "giomm/dbusproxy.h"
#include "glibconfig.h"
#include "gtkmm/box.h"
#include "gtkmm/image.h"
#include "gtkmm/label.h"
#include "gtkmm/overlay.h"
#include <giomm/dbusproxy.h>
#include <gtkmm/box.h>
#include <gtkmm/image.h>
#include <gtkmm/icontheme.h>

namespace waybar::modules {

class Gamemode : public AModule {
class Gamemode final : public ALabel {
public:
Gamemode(const std::string &, const Json::Value &);
virtual ~Gamemode();
Expand All @@ -39,7 +32,7 @@ class Gamemode : public AModule {
const Glib::VariantContainerBase &arguments);

void getData();
bool handleToggle(GdkEventButton *const &) override;
void handleToggle(int n_press, double dx, double dy) override;

// Config
std::string format = DEFAULT_FORMAT;
Expand Down Expand Up @@ -70,6 +63,7 @@ class Gamemode : public AModule {
guint login1_id;
Glib::RefPtr<Gio::DBus::Proxy> gamemode_proxy;
Glib::RefPtr<Gio::DBus::Connection> system_connection;
Glib::RefPtr<Gtk::IconTheme> gtkTheme_;
bool gamemodeRunning;
guint gamemodeWatcher_id;
};
Expand Down
17 changes: 8 additions & 9 deletions include/modules/inhibitor.hpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
#pragma once

#include <gio/gio.h>

#include <memory>

#include "ALabel.hpp"
#include "bar.hpp"

namespace waybar::modules {

class Inhibitor : public ALabel {
class Inhibitor final : public ALabel {
public:
Inhibitor(const std::string&, const waybar::Bar&, const Json::Value&);
Inhibitor(const std::string&, const Json::Value&);
virtual ~Inhibitor();
auto update() -> void override;
auto doAction(const std::string& name) -> void override;
auto activated() -> bool;

private:
auto handleToggle(::GdkEventButton* const& e) -> bool override;

const std::unique_ptr<::GDBusConnection, void (*)(::GDBusConnection*)> dbus_;
const std::string inhibitors_;
int handle_ = -1;
// Module actions
void toggle();
// Module Action Map
static inline std::map<const std::string, void (waybar::modules::Inhibitor::*const)()> actionMap_{
{"toggle", &waybar::modules::Inhibitor::toggle}};
};

} // namespace waybar::modules
1 change: 1 addition & 0 deletions include/util/gtk_icon.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include <gtkmm/icontheme.h>
#include <gdkmm/pixbuf.h>

#include <mutex>
#include <string>
Expand Down
12 changes: 12 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,18 @@ add_project_arguments('-DHAVE_DWL', language: 'cpp')
src_files += files('src/modules/dwl/tags.cpp')
man_files += files('man/waybar-dwl-tags.5.scd')

if not get_option('logind').disabled()
add_project_arguments('-DHAVE_GAMEMODE', '-DHAVE_LOGIND_INHIBITOR', language: 'cpp')
src_files += files(
'src/modules/gamemode.cpp',
'src/modules/inhibitor.cpp',
)
man_files += files(
'man/waybar-gamemode.5.scd',
'man/waybar-inhibitor.5.scd',
)
endif

subdir('protocol')

executable(
Expand Down
4 changes: 2 additions & 2 deletions src/factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
#include "modules/temperature.hpp"
#include "modules/user.hpp"

waybar::Factory::Factory(const Bar& bar, const Json::Value& config) : bar_(bar), config_(config) {}
waybar::Factory::Factory(const Bar& bar, const Json::Value& config) : bar_{bar}, config_{config} {}

waybar::AModule* waybar::Factory::makeModule(const std::string& name,
const std::string& pos) const {
Expand All @@ -121,12 +121,12 @@ waybar::AModule* waybar::Factory::makeModule(const std::string& name,
return new waybar::modules::Battery(id, config_[name]);
}
#endif
/* gtk4 todo
#ifdef HAVE_GAMEMODE
if (ref == "gamemode") {
return new waybar::modules::Gamemode(id, config_[name]);
}
#endif
/* gtk4 todo
#ifdef HAVE_UPOWER
if (ref == "upower") {
return new waybar::modules::upower::UPower(id, config_[name]);
Expand Down
54 changes: 19 additions & 35 deletions src/modules/gamemode.cpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,20 @@
#include "modules/gamemode.hpp"

#include <fmt/core.h>
#include <spdlog/spdlog.h>

#include <cstdio>
#include <cstring>
#include <string>

#include "AModule.hpp"
#include "giomm/dbusconnection.h"
#include "giomm/dbusinterface.h"
#include "giomm/dbusproxy.h"
#include "giomm/dbuswatchname.h"
#include "glibmm/error.h"
#include "glibmm/ustring.h"
#include "glibmm/variant.h"
#include "glibmm/varianttype.h"
#include "gtkmm/label.h"
#include "gtkmm/tooltip.h"
#include "util/gtk_icon.hpp"
#include <giomm/dbuswatchname.h>

namespace waybar::modules {
Gamemode::Gamemode(const std::string& id, const Json::Value& config)
: AModule(config, "gamemode", id), box_(Gtk::ORIENTATION_HORIZONTAL, 0), icon_(), label_() {
box_.pack_start(icon_);
box_.pack_start(label_);
: ALabel(config, "gamemode", id, "{}"), box_(Gtk::Orientation::HORIZONTAL, 0), icon_(), label_() {
box_.prepend(icon_);
box_.prepend(label_);
box_.set_name(name_);
event_box_.add(box_);

// Get current theme
gtkTheme_ = Gtk::IconTheme::get_for_display(Gtk::Widget::get_display());

// Tooltip
if (config_["tooltip"].isBool()) {
Expand Down Expand Up @@ -82,12 +70,12 @@ Gamemode::Gamemode(const std::string& id, const Json::Value& config)
}

gamemodeWatcher_id = Gio::DBus::watch_name(
Gio::DBus::BUS_TYPE_SESSION, dbus_name, sigc::mem_fun(*this, &Gamemode::appear),
Gio::DBus::BusType::SESSION, dbus_name, sigc::mem_fun(*this, &Gamemode::appear),
sigc::mem_fun(*this, &Gamemode::disappear),
Gio::DBus::BusNameWatcherFlags::BUS_NAME_WATCHER_FLAGS_AUTO_START);
Gio::DBus::BusNameWatcherFlags::AUTO_START);

// Connect to gamemode
gamemode_proxy = Gio::DBus::Proxy::create_for_bus_sync(Gio::DBus::BusType::BUS_TYPE_SESSION,
gamemode_proxy = Gio::DBus::Proxy::create_for_bus_sync(Gio::DBus::BusType::SESSION,
dbus_name, dbus_obj_path, dbus_interface);
if (!gamemode_proxy) {
throw std::runtime_error("Unable to connect to gamemode DBus!...");
Expand All @@ -96,16 +84,14 @@ Gamemode::Gamemode(const std::string& id, const Json::Value& config)
}

// Connect to Login1 PrepareForSleep signal
system_connection = Gio::DBus::Connection::get_sync(Gio::DBus::BusType::BUS_TYPE_SYSTEM);
system_connection = Gio::DBus::Connection::get_sync(Gio::DBus::BusType::SYSTEM);
if (!system_connection) {
throw std::runtime_error("Unable to connect to the SYSTEM Bus!...");
} else {
login1_id = system_connection->signal_subscribe(
sigc::mem_fun(*this, &Gamemode::prepareForSleep_cb), "org.freedesktop.login1",
"org.freedesktop.login1.Manager", "PrepareForSleep", "/org/freedesktop/login1");
}

event_box_.signal_button_press_event().connect(sigc::mem_fun(*this, &Gamemode::handleToggle));
}

Gamemode::~Gamemode() {
Expand Down Expand Up @@ -137,7 +123,7 @@ void Gamemode::getData() {
}
}
} catch (Glib::Error& e) {
spdlog::error("Gamemode Error {}", e.what().c_str());
spdlog::error("Gamemode Error {}", e.what());
}
}
gameCount = 0;
Expand Down Expand Up @@ -172,32 +158,31 @@ void Gamemode::prepareForSleep_cb(const Glib::RefPtr<Gio::DBus::Connection>& con
void Gamemode::appear(const Glib::RefPtr<Gio::DBus::Connection>& connection,
const Glib::ustring& name, const Glib::ustring& name_owner) {
gamemodeRunning = true;
event_box_.set_visible(true);
Gtk::Label::set_visible(true);
getData();
dp.emit();
}
// When the gamemode name disappears
void Gamemode::disappear(const Glib::RefPtr<Gio::DBus::Connection>& connection,
const Glib::ustring& name) {
gamemodeRunning = false;
event_box_.set_visible(false);
Gtk::Label::set_visible(false);
}

bool Gamemode::handleToggle(GdkEventButton* const& event) {
void Gamemode::handleToggle(int n_press, double dx, double dy) {
showAltText = !showAltText;
dp.emit();
return true;
}

auto Gamemode::update() -> void {
// Don't update widget if the Gamemode service isn't running
if (!gamemodeRunning || (gameCount <= 0 && hideNotRunning)) {
event_box_.set_visible(false);
Gtk::Label::set_visible(false);
return;
}

// Show the module
if (!event_box_.get_visible()) event_box_.set_visible(true);
if (!Gtk::Label::get_visible()) Gtk::Label::set_visible(true);

// CSS status class
const std::string status = gamemodeRunning && gameCount > 0 ? "running" : "";
Expand All @@ -224,10 +209,9 @@ auto Gamemode::update() -> void {
label_.set_markup(str);

if (useIcon) {
if (!DefaultGtkIconThemeWrapper::has_icon(iconName)) {
if (!gtkTheme_->has_icon(iconName))
iconName = DEFAULT_ICON_NAME;
}
icon_.set_from_icon_name(iconName, Gtk::ICON_SIZE_INVALID);
icon_.set_from_icon_name(iconName);
}

// Call parent update
Expand Down
42 changes: 20 additions & 22 deletions src/modules/inhibitor.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "modules/inhibitor.hpp"

#include <gio/gio.h>
#include <gio/gunixfdlist.h>
#include <spdlog/spdlog.h>

namespace {
Expand Down Expand Up @@ -97,12 +95,10 @@ auto getInhibitors(const Json::Value& config) -> std::string {

namespace waybar::modules {

Inhibitor::Inhibitor(const std::string& id, const Bar& bar, const Json::Value& config)
Inhibitor::Inhibitor(const std::string& id, const Json::Value& config)
: ALabel(config, "inhibitor", id, "{status}", true),
dbus_(::dbus()),
inhibitors_(::getInhibitors(config)) {
event_box_.add_events(Gdk::BUTTON_PRESS_MASK);
event_box_.signal_button_press_event().connect(sigc::mem_fun(*this, &Inhibitor::handleToggle));
dp.emit();
}

Expand All @@ -117,32 +113,34 @@ auto Inhibitor::activated() -> bool { return handle_ != -1; }
auto Inhibitor::update() -> void {
std::string status_text = activated() ? "activated" : "deactivated";

label_.get_style_context()->remove_class(activated() ? "deactivated" : "activated");
label_.set_markup(fmt::format(fmt::runtime(format_), fmt::arg("status", status_text),
Gtk::Label::get_style_context()->remove_class(activated() ? "deactivated" : "activated");
Gtk::Label::set_markup(fmt::format(fmt::runtime(format_), fmt::arg("status", status_text),
fmt::arg("icon", getIcon(0, status_text))));
label_.get_style_context()->add_class(status_text);
Gtk::Label::get_style_context()->add_class(status_text);

if (tooltipEnabled()) {
label_.set_tooltip_text(status_text);
Gtk::Label::set_tooltip_text(status_text);
}

return ALabel::update();
}

auto Inhibitor::handleToggle(GdkEventButton* const& e) -> bool {
if (e->button == 1) {
if (activated()) {
::close(handle_);
handle_ = -1;
} else {
handle_ = ::getLocks(dbus_, inhibitors_);
if (handle_ == -1) {
spdlog::error("cannot get inhibitor locks");
}
}
}
auto Inhibitor::doAction(const std::string& name) -> void {
if (actionMap_[name]) {
(this->*actionMap_[name])();
} else
spdlog::info("Inhibitor. Unsupported action \"{0}\"", name);
}

return ALabel::handleToggle(e);
void Inhibitor::toggle() {
if (activated()) {
::close(handle_);
handle_ = -1;
} else {
handle_ = ::getLocks(dbus_, inhibitors_);
if (handle_ == -1)
spdlog::error("cannot get inhibitor locks");
}
}

} // namespace waybar::modules

0 comments on commit d3e32d1

Please sign in to comment.