Compare commits
10
Commits
d9ac353a0d
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
420dff0fc5 | ||
|
|
db0aa8c0aa | ||
|
|
0549437776 | ||
|
|
55a76be186 | ||
|
|
d1b81c4d3e | ||
|
|
ea9ab4b2cb | ||
|
|
e1217305a5 | ||
|
|
a90d1c2f6c | ||
|
|
6be70a7d93 | ||
|
|
ff2d0afd9b |
+12
-5
@@ -47,6 +47,7 @@ target_sources(bar_lib
|
|||||||
src/connection/httpConnection.cpp
|
src/connection/httpConnection.cpp
|
||||||
src/connection/dbus/notification.cpp
|
src/connection/dbus/notification.cpp
|
||||||
src/connection/dbus/mpris.cpp
|
src/connection/dbus/mpris.cpp
|
||||||
|
src/connection/dbus/bluetooth.cpp
|
||||||
src/connection/dbus/tray.cpp
|
src/connection/dbus/tray.cpp
|
||||||
|
|
||||||
src/widgets/clock.cpp
|
src/widgets/clock.cpp
|
||||||
@@ -57,15 +58,21 @@ target_sources(bar_lib
|
|||||||
src/widgets/notification/spotifyNotification.cpp
|
src/widgets/notification/spotifyNotification.cpp
|
||||||
src/widgets/controlCenter/controlCenter.cpp
|
src/widgets/controlCenter/controlCenter.cpp
|
||||||
src/widgets/controlCenter/mediaWidget.cpp
|
src/widgets/controlCenter/mediaWidget.cpp
|
||||||
|
src/widgets/controlCenter/timer.cpp
|
||||||
src/widgets/volumeWidget.cpp
|
src/widgets/volumeWidget.cpp
|
||||||
|
src/widgets/mouseBattery.cpp
|
||||||
src/widgets/weather.cpp
|
src/widgets/weather.cpp
|
||||||
src/widgets/webWidget.cpp
|
src/widgets/webWidget.cpp
|
||||||
src/widgets/tray.cpp
|
src/widgets/tray.cpp
|
||||||
|
src/widgets/controlCenter/bluetoothSettings.cpp
|
||||||
|
src/widgets/controlCenter/settings.cpp
|
||||||
|
src/widgets/controlCenter/bluetoothSettingsRow.cpp
|
||||||
|
|
||||||
src/services/hyprland.cpp
|
src/services/hyprland.cpp
|
||||||
src/services/notificationController.cpp
|
src/services/notificationController.cpp
|
||||||
src/services/textureCache.cpp
|
src/services/textureCache.cpp
|
||||||
|
src/services/hidppBattery.cpp
|
||||||
|
src/services/mouseBatteryService.cpp
|
||||||
|
|
||||||
src/components/popover.cpp
|
src/components/popover.cpp
|
||||||
src/components/workspaceIndicator.cpp
|
src/components/workspaceIndicator.cpp
|
||||||
@@ -102,8 +109,8 @@ include(Catch)
|
|||||||
catch_discover_tests(bar_tests)
|
catch_discover_tests(bar_tests)
|
||||||
|
|
||||||
# Copy all CSS files in resources/ into build and user config directories
|
# Copy all CSS files in resources/ into build and user config directories
|
||||||
file(GLOB RES_CSS_FILES CONFIGURE_DEPENDS
|
file(GLOB RES_FILES CONFIGURE_DEPENDS
|
||||||
"${CMAKE_CURRENT_SOURCE_DIR}/resources/*.css"
|
"${CMAKE_CURRENT_SOURCE_DIR}/resources/*"
|
||||||
)
|
)
|
||||||
set(RES_BUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}/resources")
|
set(RES_BUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}/resources")
|
||||||
set(RES_USER_DIR "$ENV{HOME}/.config/bar")
|
set(RES_USER_DIR "$ENV{HOME}/.config/bar")
|
||||||
@@ -111,7 +118,7 @@ set(RES_USER_DIR "$ENV{HOME}/.config/bar")
|
|||||||
set(RES_DST_FILES "")
|
set(RES_DST_FILES "")
|
||||||
set(USER_DST_FILES "")
|
set(USER_DST_FILES "")
|
||||||
|
|
||||||
foreach(RES_FILE IN LISTS RES_CSS_FILES)
|
foreach(RES_FILE IN LISTS RES_FILES)
|
||||||
get_filename_component(RES_NAME "${RES_FILE}" NAME)
|
get_filename_component(RES_NAME "${RES_FILE}" NAME)
|
||||||
set(RES_DST "${RES_BUILD_DIR}/${RES_NAME}")
|
set(RES_DST "${RES_BUILD_DIR}/${RES_NAME}")
|
||||||
set(USER_DST "${RES_USER_DIR}/${RES_NAME}")
|
set(USER_DST "${RES_USER_DIR}/${RES_NAME}")
|
||||||
|
|||||||
+16
-7
@@ -1,29 +1,38 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <vector>
|
#include <memory>
|
||||||
|
|
||||||
#include "bar/bar.hpp"
|
#include "bar/bar.hpp"
|
||||||
|
#include "connection/dbus/bluetooth.hpp"
|
||||||
#include "connection/dbus/notification.hpp"
|
#include "connection/dbus/notification.hpp"
|
||||||
#include "connection/dbus/tray.hpp"
|
#include "connection/dbus/tray.hpp"
|
||||||
#include "services/hyprland.hpp"
|
#include "services/hyprland.hpp"
|
||||||
|
#include "services/notificationController.hpp"
|
||||||
|
|
||||||
|
#include "gdkmm/monitor.h"
|
||||||
#include "glibmm/refptr.h"
|
#include "glibmm/refptr.h"
|
||||||
#include "gtkmm/application.h"
|
#include "gtkmm/application.h"
|
||||||
|
|
||||||
class MprisController;
|
|
||||||
|
|
||||||
class App {
|
class App {
|
||||||
public:
|
public:
|
||||||
|
struct BarMonitorPair {
|
||||||
|
std::shared_ptr<Bar> bar;
|
||||||
|
std::shared_ptr<Gdk::Monitor> window;
|
||||||
|
};
|
||||||
|
|
||||||
App();
|
App();
|
||||||
|
|
||||||
int run();
|
int run();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Glib::RefPtr<Gtk::Application> app;
|
Glib::RefPtr<Gtk::Application> app;
|
||||||
std::vector<std::shared_ptr<Bar>> bars;
|
std::map<std::string, BarMonitorPair> bars;
|
||||||
std::shared_ptr<NotificationService> notificationService = nullptr;
|
|
||||||
std::shared_ptr<MprisController> mprisController = nullptr;
|
std::shared_ptr<NotificationService> notificationService = NotificationService::getInstance();
|
||||||
HyprlandService *hyprlandService = nullptr;
|
std::shared_ptr<NotificationController> notificationController = NotificationController::getInstance();
|
||||||
|
std::shared_ptr<BluetoothController> bluetoothController = BluetoothController::getInstance();
|
||||||
|
std::shared_ptr<MprisController> mprisController = MprisController::getInstance();
|
||||||
|
std::shared_ptr<HyprlandService> hyprlandService = HyprlandService::getInstance();
|
||||||
|
|
||||||
TrayService *trayService = TrayService::getInstance();
|
TrayService *trayService = TrayService::getInstance();
|
||||||
void setupServices();
|
void setupServices();
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
#include "widgets/clock.hpp"
|
#include "widgets/clock.hpp"
|
||||||
#include "widgets/controlCenter/controlCenter.hpp"
|
#include "widgets/controlCenter/controlCenter.hpp"
|
||||||
#include "widgets/date.hpp"
|
#include "widgets/date.hpp"
|
||||||
|
#include "widgets/mouseBattery.hpp"
|
||||||
#include "widgets/tray.hpp"
|
#include "widgets/tray.hpp"
|
||||||
#include "widgets/volumeWidget.hpp"
|
#include "widgets/volumeWidget.hpp"
|
||||||
#include "widgets/webWidget.hpp"
|
#include "widgets/webWidget.hpp"
|
||||||
@@ -28,6 +29,7 @@ class Bar : public Gtk::Window {
|
|||||||
|
|
||||||
Clock clock;
|
Clock clock;
|
||||||
Date date;
|
Date date;
|
||||||
|
MouseBatteryWidget mouseBattery;
|
||||||
WebWidget homeAssistant{Icon::HOME_ASSISTANT, "Home Assistant", "https://home.rivercry.com"};
|
WebWidget homeAssistant{Icon::HOME_ASSISTANT, "Home Assistant", "https://home.rivercry.com"};
|
||||||
ControlCenter controlCenter{Icon::MENU, "Control Center"};
|
ControlCenter controlCenter{Icon::MENU, "Control Center"};
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,6 @@
|
|||||||
|
|
||||||
class IconButton : public TextButton {
|
class IconButton : public TextButton {
|
||||||
public:
|
public:
|
||||||
IconButton(Icon::Type icon, std::string fontFamilyCss = "materia-icons");
|
IconButton(Icon::Type icon, std::string fontFamilyCss = "material-icons");
|
||||||
void setIcon(Icon::Type icon);
|
void setIcon(Icon::Type icon);
|
||||||
};
|
};
|
||||||
@@ -0,0 +1,105 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <iomanip>
|
||||||
|
#include <iostream>
|
||||||
|
#include <memory>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#include "components/button/iconButton.hpp"
|
||||||
|
#include "services/timerService.hpp"
|
||||||
|
|
||||||
|
#include "gtkmm/box.h"
|
||||||
|
#include "gtkmm/label.h"
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
std::string format_duration(const std::string &digits) {
|
||||||
|
std::string d = digits;
|
||||||
|
if (d.size() > 6) {
|
||||||
|
d = d.substr(d.size() - 6);
|
||||||
|
}
|
||||||
|
if (d.empty()) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string padded = std::string(6 - d.size(), '0') + d;
|
||||||
|
|
||||||
|
int h = std::stoi(padded.substr(0, 2));
|
||||||
|
int m = std::stoi(padded.substr(2, 2));
|
||||||
|
int s = std::stoi(padded.substr(4, 2));
|
||||||
|
|
||||||
|
std::ostringstream out;
|
||||||
|
if (h > 0) {
|
||||||
|
out << h << "h " << std::setw(2) << std::setfill('0') << m << "m "
|
||||||
|
<< std::setw(2) << std::setfill('0') << s << "s";
|
||||||
|
} else if (m > 0) {
|
||||||
|
out << m << "m " << std::setw(2) << std::setfill('0') << s << "s";
|
||||||
|
} else {
|
||||||
|
out << s << "s";
|
||||||
|
}
|
||||||
|
|
||||||
|
return out.str();
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
class Timer : public Gtk::Box {
|
||||||
|
public:
|
||||||
|
Timer(const std::string &duration, uint64_t timerId) {
|
||||||
|
uint32_t totalTime = std::stoul(duration);
|
||||||
|
u_int64_t seconds = totalTime % 100;
|
||||||
|
u_int64_t minutes = (totalTime / 100) % 100;
|
||||||
|
u_int64_t hours = totalTime / 10000;
|
||||||
|
|
||||||
|
u_int64_t totalSeconds = hours * 3600 + minutes * 60 + seconds + 1; // +1 to account for the first tick happening after 1 second
|
||||||
|
|
||||||
|
this->timeLeft = totalSeconds;
|
||||||
|
this->timerLabel = std::make_shared<Gtk::Label>(format_duration(duration));
|
||||||
|
timerLabel->add_css_class("control-center-timer-label");
|
||||||
|
|
||||||
|
auto timerBox = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL, 5);
|
||||||
|
timerBox->append(*timerLabel);
|
||||||
|
|
||||||
|
auto cancelButton = Gtk::make_managed<IconButton>(Icon::Type::TOKEN);
|
||||||
|
cancelButton->set_tooltip_text("Cancel Timer");
|
||||||
|
cancelButton->signal_clicked().connect([this, timerId]() {
|
||||||
|
this->timerService->removeTimer(timerId);
|
||||||
|
});
|
||||||
|
timerBox->append(*cancelButton);
|
||||||
|
this->append(*timerBox);
|
||||||
|
}
|
||||||
|
|
||||||
|
void activateTimer() {
|
||||||
|
// std::cout << "Timer activated" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void updateTimeLeft(uint64_t timeValue) {
|
||||||
|
uint64_t s = timeValue % 100;
|
||||||
|
uint64_t m = (timeValue / 100) % 100;
|
||||||
|
uint64_t h = timeValue / 10000;
|
||||||
|
uint64_t totalSeconds = h * 3600 + m * 60 + s;
|
||||||
|
this->timeLeft = totalSeconds;
|
||||||
|
|
||||||
|
std::ostringstream out;
|
||||||
|
if (h > 0) {
|
||||||
|
out << h << "h " << std::setw(2) << std::setfill('0') << m << "m "
|
||||||
|
<< std::setw(2) << std::setfill('0') << s << "s";
|
||||||
|
} else if (m > 0) {
|
||||||
|
out << m << "m " << std::setw(2) << std::setfill('0') << s << "s";
|
||||||
|
} else {
|
||||||
|
out << s << "s";
|
||||||
|
}
|
||||||
|
|
||||||
|
this->timerLabel->set_text(out.str());
|
||||||
|
}
|
||||||
|
|
||||||
|
void tickDown() {
|
||||||
|
if (timeLeft > 0) {
|
||||||
|
this->updateTimeLeft(this->timeLeft - 1);
|
||||||
|
} else {
|
||||||
|
timeLeft = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::shared_ptr<TimerService> timerService = TimerService::getInstance();
|
||||||
|
uint64_t timeLeft;
|
||||||
|
std::shared_ptr<Gtk::Label> timerLabel;
|
||||||
|
};
|
||||||
@@ -22,6 +22,25 @@ class Icon {
|
|||||||
CONTENT_COPY,
|
CONTENT_COPY,
|
||||||
|
|
||||||
TOKEN,
|
TOKEN,
|
||||||
|
|
||||||
|
SETTINGS,
|
||||||
|
POWER_SETTINGS_NEW,
|
||||||
|
BLUETOOTH,
|
||||||
|
BLUETOOTH_CONNECTED,
|
||||||
|
BLUETOOTH_SEARCHING,
|
||||||
|
LINK,
|
||||||
|
LINK_OFF,
|
||||||
|
|
||||||
|
VERIFIED,
|
||||||
|
VERIFIED_OFF,
|
||||||
|
|
||||||
|
DONE_ALL,
|
||||||
|
REMOVE_DONE,
|
||||||
|
|
||||||
|
BATTERY_FULL,
|
||||||
|
BATTERY_ALERT,
|
||||||
|
BATTERY_CHARGING_FULL,
|
||||||
|
BATTERY_UNKNOWN,
|
||||||
};
|
};
|
||||||
|
|
||||||
static const std::string toString(Type type) {
|
static const std::string toString(Type type) {
|
||||||
@@ -46,5 +65,24 @@ class Icon {
|
|||||||
{CONTENT_COPY, "\ue14d"},
|
{CONTENT_COPY, "\ue14d"},
|
||||||
|
|
||||||
{TOKEN, "\uea25"},
|
{TOKEN, "\uea25"},
|
||||||
|
|
||||||
|
{SETTINGS, "\ue8b8"},
|
||||||
|
{POWER_SETTINGS_NEW, "\ue8ac"},
|
||||||
|
{BLUETOOTH, "\ue1a7"},
|
||||||
|
{BLUETOOTH_CONNECTED, "\ue1a8"},
|
||||||
|
{BLUETOOTH_SEARCHING, "\ue1aa"},
|
||||||
|
{LINK, "\ue157"},
|
||||||
|
{LINK_OFF, "\ue16f"},
|
||||||
|
|
||||||
|
{VERIFIED, "\uef76"},
|
||||||
|
{VERIFIED_OFF, "\uf30e"},
|
||||||
|
|
||||||
|
{DONE_ALL, "\ue877"},
|
||||||
|
{REMOVE_DONE, "\ue9d3"},
|
||||||
|
|
||||||
|
{BATTERY_FULL, "\ue1a5"},
|
||||||
|
{BATTERY_ALERT, "\ue19c"},
|
||||||
|
{BATTERY_CHARGING_FULL, "\ue1a3"},
|
||||||
|
{BATTERY_UNKNOWN, "\ue1a6"},
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@@ -0,0 +1,129 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
|
#include "services/textureCache.hpp"
|
||||||
|
#include "widgets/weather.hpp"
|
||||||
|
|
||||||
|
#include "gtkmm/box.h"
|
||||||
|
#include "gtkmm/enums.h"
|
||||||
|
#include "gtkmm/image.h"
|
||||||
|
|
||||||
|
std::string formatTemperature(double temp) {
|
||||||
|
return std::to_string(static_cast<int>(std::round(temp))) + "°C";
|
||||||
|
}
|
||||||
|
|
||||||
|
class WeatherInfo : public Gtk::Box {
|
||||||
|
private:
|
||||||
|
Gtk::Image getImageFromWeatherCode(int weather_code, bool is_daytime = true) {
|
||||||
|
Gtk::Image pic;
|
||||||
|
|
||||||
|
|
||||||
|
auto file = std::ifstream("resources/weatherCodes.json");
|
||||||
|
if (!file.is_open()) {
|
||||||
|
auto homeDir = std::getenv("HOME");
|
||||||
|
std::cout << "Failed to open local weatherCodes.json, trying " << std::string(homeDir) + "/.config/bar/weatherCodes.json" << std::endl;
|
||||||
|
file = std::ifstream(std::string(homeDir) + "/.config/bar/weatherCodes.json");
|
||||||
|
|
||||||
|
if (!file.is_open()) {
|
||||||
|
std::cerr << "Failed to open weatherCodes.json" << std::endl;
|
||||||
|
return pic;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
auto json = nlohmann::json::parse(file);
|
||||||
|
file.close();
|
||||||
|
|
||||||
|
std::string image_path = json[std::to_string(weather_code)][is_daytime ? "day" : "night"]["image"].get<std::string>();
|
||||||
|
|
||||||
|
auto texture = TextureCacheService::getInstance()->getTexture(image_path);
|
||||||
|
|
||||||
|
if (texture) {
|
||||||
|
pic.set(texture);
|
||||||
|
}
|
||||||
|
|
||||||
|
pic.set_pixel_size(64);
|
||||||
|
|
||||||
|
return pic;
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
WeatherInfo(WeatherWidget::CurrentWeather current_weather) {
|
||||||
|
set_orientation(Gtk::Orientation::HORIZONTAL);
|
||||||
|
this->set_valign(Gtk::Align::CENTER);
|
||||||
|
this->set_halign(Gtk::Align::FILL);
|
||||||
|
|
||||||
|
auto image = getImageFromWeatherCode(current_weather.weather_code);
|
||||||
|
auto temp = Gtk::Label();
|
||||||
|
temp.set_markup("<span weight='bold'>" + formatTemperature(current_weather.temperature) + "</span>" + "<span color='gray' size='small'> (" + formatTemperature(current_weather.apparent_temperature) + ")</span>");
|
||||||
|
temp.set_halign(Gtk::Align::START);
|
||||||
|
|
||||||
|
|
||||||
|
auto temp_box = Gtk::Box(Gtk::Orientation::VERTICAL);
|
||||||
|
temp_box.set_valign(Gtk::Align::CENTER);
|
||||||
|
temp_box.set_halign(Gtk::Align::START);
|
||||||
|
temp_box.append(temp);
|
||||||
|
|
||||||
|
|
||||||
|
append(image);
|
||||||
|
append(temp_box);
|
||||||
|
}
|
||||||
|
|
||||||
|
WeatherInfo(WeatherWidget::HourlyWeather hourly_weather) {
|
||||||
|
set_orientation(Gtk::Orientation::HORIZONTAL);
|
||||||
|
this->set_valign(Gtk::Align::CENTER);
|
||||||
|
add_css_class("weather-info");
|
||||||
|
|
||||||
|
auto image = getImageFromWeatherCode(hourly_weather.weather_code);
|
||||||
|
|
||||||
|
auto temp = Gtk::Label();
|
||||||
|
temp.set_markup("<span weight='bold'>" + formatTemperature(hourly_weather.temperature_2m) + "</span>" + "<span color='gray' size='small'> (" + formatTemperature(hourly_weather.apparent_temperature) + ")</span>");
|
||||||
|
temp.set_halign(Gtk::Align::START);
|
||||||
|
|
||||||
|
|
||||||
|
Gtk::Box temp_box(Gtk::Orientation::VERTICAL);
|
||||||
|
temp_box.set_halign(Gtk::Align::START);
|
||||||
|
temp_box.set_valign(Gtk::Align::CENTER);
|
||||||
|
temp_box.append(temp);
|
||||||
|
|
||||||
|
auto timeLabel = Gtk::Label();
|
||||||
|
timeLabel.set_markup("<span size='small'>" + hourly_weather.time + "</span>");
|
||||||
|
timeLabel.set_halign(Gtk::Align::START);
|
||||||
|
temp_box.append(timeLabel);
|
||||||
|
|
||||||
|
append(image);
|
||||||
|
append(temp_box);
|
||||||
|
}
|
||||||
|
|
||||||
|
WeatherInfo(WeatherWidget::DailyWeather daily_weather) {
|
||||||
|
set_orientation(Gtk::Orientation::HORIZONTAL);
|
||||||
|
this->set_valign(Gtk::Align::CENTER);
|
||||||
|
add_css_class("weather-info");
|
||||||
|
|
||||||
|
auto image = getImageFromWeatherCode(daily_weather.weather_code);
|
||||||
|
|
||||||
|
auto max_label = Gtk::Label();
|
||||||
|
max_label.set_markup("<span weight='bold'>H: " + formatTemperature(daily_weather.temperature_2m_max) + "</span>" + "<span color='gray' size='small'> (" + formatTemperature(daily_weather.apparent_temperature_max) + ")</span>");
|
||||||
|
max_label.set_halign(Gtk::Align::START);
|
||||||
|
|
||||||
|
auto min_label = Gtk::Label();
|
||||||
|
min_label.set_markup("<span size='small'>L: " + formatTemperature(daily_weather.temperature_2m_min) + "</span>" + "<span color='gray' size='small'> (" + formatTemperature(daily_weather.apparent_temperature_min) + ")</span>");
|
||||||
|
min_label.set_halign(Gtk::Align::START);
|
||||||
|
|
||||||
|
Gtk::Box temp_box(Gtk::Orientation::VERTICAL);
|
||||||
|
temp_box.set_halign(Gtk::Align::START);
|
||||||
|
temp_box.set_valign(Gtk::Align::CENTER);
|
||||||
|
temp_box.append(max_label);
|
||||||
|
temp_box.append(min_label);
|
||||||
|
|
||||||
|
auto timeLabel = Gtk::Label();
|
||||||
|
timeLabel.set_markup("<span size='small'>" + daily_weather.time + "</span>");
|
||||||
|
timeLabel.set_halign(Gtk::Align::START);
|
||||||
|
temp_box.append(timeLabel);
|
||||||
|
|
||||||
|
append(image);
|
||||||
|
append(temp_box);
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,128 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <giomm.h>
|
||||||
|
#include <map>
|
||||||
|
#include <memory>
|
||||||
|
#include <sigc++/sigc++.h>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "connection/dbus/dbus.hpp"
|
||||||
|
|
||||||
|
struct BluetoothDevice {
|
||||||
|
std::string object_path;
|
||||||
|
std::string address;
|
||||||
|
std::string name;
|
||||||
|
std::string icon;
|
||||||
|
bool paired = false;
|
||||||
|
bool connected = false;
|
||||||
|
bool trusted = false;
|
||||||
|
int16_t rssi = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
class BluetoothController : public DbusConnection {
|
||||||
|
using PropertiesMap = std::map<Glib::ustring, Glib::VariantBase>;
|
||||||
|
|
||||||
|
public:
|
||||||
|
static std::shared_ptr<BluetoothController> getInstance() {
|
||||||
|
if (!instance) {
|
||||||
|
instance = std::shared_ptr<BluetoothController>(new BluetoothController());
|
||||||
|
}
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Adapter control
|
||||||
|
void setPowered(bool powered);
|
||||||
|
bool isPowered() const;
|
||||||
|
|
||||||
|
// Discovery
|
||||||
|
void startDiscovery();
|
||||||
|
void stopDiscovery();
|
||||||
|
bool isDiscovering() const;
|
||||||
|
|
||||||
|
// Device actions (identified by object_path)
|
||||||
|
void pairDevice(const std::string &object_path);
|
||||||
|
void unpairDevice(const std::string &object_path);
|
||||||
|
void connectDevice(const std::string &object_path);
|
||||||
|
void disconnectDevice(const std::string &object_path);
|
||||||
|
void trustDevice(const std::string &object_path, bool trusted);
|
||||||
|
|
||||||
|
// Queries
|
||||||
|
std::vector<BluetoothDevice> getDevices() const;
|
||||||
|
std::vector<BluetoothDevice> getPairedDevices() const;
|
||||||
|
|
||||||
|
// Signals
|
||||||
|
sigc::signal<void(bool)> &signalPoweredChanged();
|
||||||
|
sigc::signal<void(bool)> &signalDiscoveringChanged();
|
||||||
|
sigc::signal<void(const BluetoothDevice &)> &signalDeviceAdded();
|
||||||
|
sigc::signal<void(const std::string &)> &signalDeviceRemoved();
|
||||||
|
sigc::signal<void(const BluetoothDevice &)> &signalDeviceChanged();
|
||||||
|
|
||||||
|
private:
|
||||||
|
BluetoothController();
|
||||||
|
|
||||||
|
inline static std::shared_ptr<BluetoothController> instance = nullptr;
|
||||||
|
|
||||||
|
bool m_powered = false;
|
||||||
|
bool m_discovering = false;
|
||||||
|
|
||||||
|
std::string m_adapter_path;
|
||||||
|
Glib::RefPtr<Gio::DBus::Proxy> m_adapter_proxy;
|
||||||
|
Glib::RefPtr<Gio::DBus::Proxy> m_object_manager_proxy;
|
||||||
|
|
||||||
|
std::map<std::string, BluetoothDevice> m_devices;
|
||||||
|
std::map<std::string, Glib::RefPtr<Gio::DBus::Proxy>> m_device_proxies;
|
||||||
|
|
||||||
|
sigc::signal<void(bool)> m_powered_signal;
|
||||||
|
sigc::signal<void(bool)> m_discovering_signal;
|
||||||
|
sigc::signal<void(const BluetoothDevice &)> m_device_added_signal;
|
||||||
|
sigc::signal<void(const std::string &)> m_device_removed_signal;
|
||||||
|
sigc::signal<void(const BluetoothDevice &)> m_device_changed_signal;
|
||||||
|
|
||||||
|
// Bus setup
|
||||||
|
void onBusConnected(const Glib::RefPtr<Gio::AsyncResult> &result);
|
||||||
|
void enumerateObjects();
|
||||||
|
void parseInterfaces(const std::string &object_path, const Glib::VariantBase &interfaces_var);
|
||||||
|
|
||||||
|
// ObjectManager signals
|
||||||
|
void onObjectManagerSignal(const Glib::ustring &sender_name,
|
||||||
|
const Glib::ustring &signal_name,
|
||||||
|
const Glib::VariantContainerBase ¶meters);
|
||||||
|
|
||||||
|
// Agent
|
||||||
|
guint m_agent_id = 0;
|
||||||
|
void registerAgent();
|
||||||
|
void on_agent_method_call(const Glib::RefPtr<Gio::DBus::Connection> &connection,
|
||||||
|
const Glib::ustring &sender,
|
||||||
|
const Glib::ustring &object_path,
|
||||||
|
const Glib::ustring &interface_name,
|
||||||
|
const Glib::ustring &method_name,
|
||||||
|
const Glib::VariantContainerBase ¶meters,
|
||||||
|
const Glib::RefPtr<Gio::DBus::MethodInvocation> &invocation);
|
||||||
|
|
||||||
|
// Adapter
|
||||||
|
void setupAdapter(const std::string &path, const PropertiesMap &properties);
|
||||||
|
void onAdapterPropertiesChanged(const Gio::DBus::Proxy::MapChangedProperties &changed,
|
||||||
|
const std::vector<Glib::ustring> &invalidated);
|
||||||
|
|
||||||
|
// Devices
|
||||||
|
void addDevice(const std::string &path, const PropertiesMap &properties);
|
||||||
|
void removeDevice(const std::string &path);
|
||||||
|
void onDevicePropertiesChanged(const std::string &object_path,
|
||||||
|
const Gio::DBus::Proxy::MapChangedProperties &changed,
|
||||||
|
const std::vector<Glib::ustring> &invalidated);
|
||||||
|
|
||||||
|
// Helpers
|
||||||
|
static BluetoothDevice parseDeviceProperties(const std::string &path, const PropertiesMap &properties);
|
||||||
|
void setDbusProperty(const std::string &object_path,
|
||||||
|
const std::string &interface,
|
||||||
|
const std::string &property,
|
||||||
|
const Glib::VariantBase &value);
|
||||||
|
|
||||||
|
// HID Authorization handler
|
||||||
|
void authorizeHIDDevice(const std::string &object_path);
|
||||||
|
|
||||||
|
Glib::RefPtr<Gio::DBus::NodeInfo> m_node_info;
|
||||||
|
std::shared_ptr<Gio::DBus::InterfaceVTable> m_interface_vtable;
|
||||||
|
};
|
||||||
@@ -14,6 +14,10 @@ class DbusConnection {
|
|||||||
Gio::DBus::Connection::get(Gio::DBus::BusType::SESSION, callback);
|
Gio::DBus::Connection::get(Gio::DBus::BusType::SESSION, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void connect_system_async(const sigc::slot<void(const Glib::RefPtr<Gio::AsyncResult> &)> &callback) {
|
||||||
|
Gio::DBus::Connection::get(Gio::DBus::BusType::SYSTEM, callback);
|
||||||
|
}
|
||||||
|
|
||||||
static void ensure_gio_init() {
|
static void ensure_gio_init() {
|
||||||
try {
|
try {
|
||||||
Gio::init();
|
Gio::init();
|
||||||
|
|||||||
@@ -37,11 +37,15 @@ struct NotifyMessage {
|
|||||||
std::string body;
|
std::string body;
|
||||||
std::vector<std::string> actions;
|
std::vector<std::string> actions;
|
||||||
NotificationUrgency urgency = NORMAL;
|
NotificationUrgency urgency = NORMAL;
|
||||||
int32_t expire_timeout;
|
int32_t expire_timeout = -1;
|
||||||
// Callback to invoke when an action is triggered
|
// Callback to invoke when an action is triggered
|
||||||
std::function<void(const std::string &action_id)> on_action;
|
std::function<void(const std::string &action_id)> on_action;
|
||||||
// Guard to prevent multiple action invocations across mirrors
|
// Guard to prevent multiple action invocations across mirrors
|
||||||
std::shared_ptr<bool> actionInvoked;
|
std::shared_ptr<bool> actionInvoked = std::make_shared<bool>(false);
|
||||||
// image data (if any) from dbus
|
// image data (if any) from dbus
|
||||||
std::optional<Glib::RefPtr<Gdk::Pixbuf>> imageData;
|
std::optional<Glib::RefPtr<Gdk::Pixbuf>> imageData;
|
||||||
|
|
||||||
|
bool has_input = false;
|
||||||
|
std::string input_placeholder;
|
||||||
|
std::function<void(const std::string &)> on_input;
|
||||||
};
|
};
|
||||||
@@ -17,6 +17,7 @@ class MprisController : public DbusConnection {
|
|||||||
std::string artwork_url;
|
std::string artwork_url;
|
||||||
int64_t length_ms;
|
int64_t length_ms;
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class PlaybackStatus {
|
enum class PlaybackStatus {
|
||||||
Playing,
|
Playing,
|
||||||
Paused,
|
Paused,
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include <giomm.h>
|
#include <giomm.h>
|
||||||
#include <gtkmm.h>
|
#include <gtkmm.h>
|
||||||
#include <sigc++/sigc++.h>
|
#include <sigc++/sigc++.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
#include "connection/dbus/dbus.hpp"
|
#include "connection/dbus/dbus.hpp"
|
||||||
|
|
||||||
@@ -43,7 +44,17 @@ const Glib::ustring introspection_xml = R"(
|
|||||||
|
|
||||||
class NotificationService : public DbusConnection {
|
class NotificationService : public DbusConnection {
|
||||||
public:
|
public:
|
||||||
NotificationService() : notificationIdCounter(1) {
|
static std::shared_ptr<NotificationService> getInstance() {
|
||||||
|
if (NotificationService::instance == nullptr) {
|
||||||
|
NotificationService::instance = std::shared_ptr<NotificationService>(new NotificationService());
|
||||||
|
}
|
||||||
|
return NotificationService::instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
void onBusAcquired(const Glib::RefPtr<Gio::DBus::Connection> &connection, const Glib::ustring &name);
|
||||||
|
|
||||||
|
private:
|
||||||
|
NotificationService() {
|
||||||
Gio::DBus::own_name(
|
Gio::DBus::own_name(
|
||||||
Gio::DBus::BusType::SESSION,
|
Gio::DBus::BusType::SESSION,
|
||||||
"org.freedesktop.Notifications",
|
"org.freedesktop.Notifications",
|
||||||
@@ -53,10 +64,9 @@ class NotificationService : public DbusConnection {
|
|||||||
Gio::DBus::BusNameOwnerFlags::REPLACE);
|
Gio::DBus::BusNameOwnerFlags::REPLACE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void onBusAcquired(const Glib::RefPtr<Gio::DBus::Connection> &connection, const Glib::ustring &name);
|
static inline std::shared_ptr<NotificationService> instance = nullptr;
|
||||||
|
|
||||||
private:
|
guint notificationIdCounter = 0;
|
||||||
guint notificationIdCounter;
|
|
||||||
const Gio::DBus::InterfaceVTable &getMessageInterfaceVTable();
|
const Gio::DBus::InterfaceVTable &getMessageInterfaceVTable();
|
||||||
void on_method_call(const Glib::RefPtr<Gio::DBus::Connection> &connection,
|
void on_method_call(const Glib::RefPtr<Gio::DBus::Connection> &connection,
|
||||||
const Glib::ustring &sender,
|
const Glib::ustring &sender,
|
||||||
|
|||||||
+108
-43
@@ -1,52 +1,16 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <array>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <cstring>
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
|
#include <span>
|
||||||
|
#include <spdlog/spdlog.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
#include <sys/un.h>
|
||||||
#include "helpers/command.hpp"
|
#include <unistd.h>
|
||||||
|
|
||||||
class HyprctlHelper {
|
|
||||||
public:
|
|
||||||
static nlohmann::json getMonitorData() {
|
|
||||||
std::string result = CommandHelper::exec("hyprctl -j monitors");
|
|
||||||
assert(!result.empty() && "Failed to get monitor data from hyprctl");
|
|
||||||
|
|
||||||
auto json = nlohmann::json::parse(result);
|
|
||||||
|
|
||||||
assert(json.is_array());
|
|
||||||
|
|
||||||
return json;
|
|
||||||
}
|
|
||||||
|
|
||||||
static nlohmann::json getWorkspaceData() {
|
|
||||||
std::string result = CommandHelper::exec("hyprctl -j workspaces");
|
|
||||||
assert(!result.empty() && "Failed to get workspace data from hyprctl");
|
|
||||||
|
|
||||||
auto json = nlohmann::json::parse(result);
|
|
||||||
|
|
||||||
assert(json.is_array());
|
|
||||||
|
|
||||||
return json;
|
|
||||||
}
|
|
||||||
|
|
||||||
static nlohmann::json getClientData() {
|
|
||||||
std::string result = CommandHelper::exec("hyprctl -j clients");
|
|
||||||
assert(!result.empty() && "Failed to get client data from hyprctl");
|
|
||||||
|
|
||||||
auto json = nlohmann::json::parse(result);
|
|
||||||
|
|
||||||
assert(json.is_array());
|
|
||||||
|
|
||||||
return json;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void dispatchWorkspace(int workspaceNumber) {
|
|
||||||
std::string out = "hyprctl dispatch workspace " + std::to_string(workspaceNumber);
|
|
||||||
CommandHelper::execNoOutput(out.c_str());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class HyprSocketHelper {
|
class HyprSocketHelper {
|
||||||
public:
|
public:
|
||||||
@@ -66,4 +30,105 @@ class HyprSocketHelper {
|
|||||||
|
|
||||||
return std::string();
|
return std::string();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static std::string getHyprlandCommandSocketPath() {
|
||||||
|
const char *hyprlandInstanceSignature = std::getenv("HYPRLAND_INSTANCE_SIGNATURE");
|
||||||
|
const char *xdgRuntimeDir = std::getenv("XDG_RUNTIME_DIR");
|
||||||
|
|
||||||
|
if (!xdgRuntimeDir || !hyprlandInstanceSignature) {
|
||||||
|
return std::string();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string basePath = std::string(xdgRuntimeDir) + "/hypr/" + std::string(hyprlandInstanceSignature) + "/";
|
||||||
|
std::string sock1 = basePath + ".socket.sock";
|
||||||
|
if (access(sock1.c_str(), F_OK) == 0) {
|
||||||
|
return sock1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::string();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class HyprctlHelper {
|
||||||
|
public:
|
||||||
|
static std::string sendCommand(const std::string &command) {
|
||||||
|
std::string socketPath = HyprSocketHelper::getHyprlandCommandSocketPath();
|
||||||
|
assert(!socketPath.empty() && "Failed to get Hyprland command socket path");
|
||||||
|
|
||||||
|
int socketFd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||||
|
assert(socketFd != -1 && "Failed to create Hyprland command socket");
|
||||||
|
|
||||||
|
struct sockaddr_un addr{};
|
||||||
|
std::memset(&addr, 0, sizeof(addr));
|
||||||
|
addr.sun_family = AF_UNIX;
|
||||||
|
|
||||||
|
auto sunPathSpan = std::span<char, sizeof(addr.sun_path)>(addr.sun_path);
|
||||||
|
std::ranges::fill(sunPathSpan, '\0');
|
||||||
|
constexpr size_t kMaxPathLength = sizeof(addr.sun_path) - 1;
|
||||||
|
const size_t copyLength = std::min(socketPath.size(), kMaxPathLength);
|
||||||
|
std::copy_n(socketPath.begin(), copyLength, sunPathSpan.begin());
|
||||||
|
|
||||||
|
auto *sockaddrPtr = static_cast<struct sockaddr *>(static_cast<void *>(&addr));
|
||||||
|
int result = connect(socketFd, sockaddrPtr, sizeof(addr));
|
||||||
|
assert(result != -1 && "Failed to connect to Hyprland command socket");
|
||||||
|
|
||||||
|
ssize_t bytesSent = send(socketFd, command.c_str(), command.size(), 0);
|
||||||
|
assert(bytesSent != -1 && "Failed to send command to Hyprland socket");
|
||||||
|
|
||||||
|
shutdown(socketFd, SHUT_WR);
|
||||||
|
|
||||||
|
std::string response;
|
||||||
|
constexpr size_t kBufferSize = 4096;
|
||||||
|
std::array<char, kBufferSize> buffer{};
|
||||||
|
while (true) {
|
||||||
|
ssize_t bytesRead = recv(socketFd, buffer.data(), buffer.size(), 0);
|
||||||
|
if (bytesRead <= 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
response.append(buffer.data(), static_cast<size_t>(bytesRead));
|
||||||
|
}
|
||||||
|
|
||||||
|
close(socketFd);
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
static nlohmann::json getMonitorData() {
|
||||||
|
std::string result = sendCommand("j/monitors");
|
||||||
|
assert(!result.empty() && "Failed to get monitor data from Hyprland socket");
|
||||||
|
|
||||||
|
auto json = nlohmann::json::parse(result);
|
||||||
|
|
||||||
|
assert(json.is_array());
|
||||||
|
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
static nlohmann::json getWorkspaceData() {
|
||||||
|
std::string result = sendCommand("j/workspaces");
|
||||||
|
assert(!result.empty() && "Failed to get workspace data from Hyprland socket");
|
||||||
|
|
||||||
|
auto json = nlohmann::json::parse(result);
|
||||||
|
|
||||||
|
assert(json.is_array());
|
||||||
|
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
static nlohmann::json getClientData() {
|
||||||
|
std::string result = sendCommand("j/clients");
|
||||||
|
assert(!result.empty() && "Failed to get client data from Hyprland socket");
|
||||||
|
|
||||||
|
auto json = nlohmann::json::parse(result);
|
||||||
|
|
||||||
|
assert(json.is_array());
|
||||||
|
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void dispatchWorkspace(int workspaceNumber) {
|
||||||
|
std::string command = "dispatch hl.dsp.focus({ workspace = \"" + std::to_string(workspaceNumber) + "\" })";
|
||||||
|
sendCommand(command);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
@@ -41,6 +41,12 @@ class StringHelper {
|
|||||||
if (input.length() <= maxSize) {
|
if (input.length() <= maxSize) {
|
||||||
return input;
|
return input;
|
||||||
}
|
}
|
||||||
return input.substr(0, maxSize) + "...";
|
|
||||||
|
size_t len = maxSize;
|
||||||
|
while (len > 0 && (static_cast<unsigned char>(input[len]) & 0xC0) == 0x80) {
|
||||||
|
len--;
|
||||||
|
}
|
||||||
|
|
||||||
|
return input.substr(0, len) + "...";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <gio/gio.h>
|
|
||||||
#include <gtk/gtk.h>
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include "sigc++/signal.h"
|
|
||||||
|
|
||||||
class BluetoothService {
|
|
||||||
inline static BluetoothService *instance = nullptr;
|
|
||||||
|
|
||||||
public:
|
|
||||||
sigc::signal<void(bool)> powerStateChangedSignal;
|
|
||||||
sigc::signal<void(bool)> isDiscoveringChangedSignal;
|
|
||||||
|
|
||||||
bool getPowerState();
|
|
||||||
bool getIsDiscovering();
|
|
||||||
|
|
||||||
void togglePowerState();
|
|
||||||
void toggleIsDiscovering();
|
|
||||||
|
|
||||||
static BluetoothService *getInstance() {
|
|
||||||
if (BluetoothService::instance == nullptr) {
|
|
||||||
|
|
||||||
BluetoothService::instance = new BluetoothService();
|
|
||||||
}
|
|
||||||
return BluetoothService::instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
BluetoothService();
|
|
||||||
|
|
||||||
GDBusProxy *adapter_proxy = nullptr;
|
|
||||||
|
|
||||||
std::vector<std::string> getDeviceObjectPaths();
|
|
||||||
bool powerState = false;
|
|
||||||
bool isDiscovering = false;
|
|
||||||
|
|
||||||
void onPropertyChanged(GDBusProxy *proxy,
|
|
||||||
GVariant *changed_properties,
|
|
||||||
const gchar *const *invalidated_properties,
|
|
||||||
gpointer user_data);
|
|
||||||
|
|
||||||
static void onPropertyChangedStatic(GDBusProxy *proxy,
|
|
||||||
GVariant *changed_properties,
|
|
||||||
const gchar *const *invalidated_properties,
|
|
||||||
gpointer user_data);
|
|
||||||
};
|
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <optional>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace hidpp {
|
||||||
|
|
||||||
|
/// Battery state of a wireless Logitech device.
|
||||||
|
struct BatteryStatus {
|
||||||
|
enum class Level : uint8_t { UNKNOWN,
|
||||||
|
CRITICAL,
|
||||||
|
LOW,
|
||||||
|
GOOD,
|
||||||
|
FULL };
|
||||||
|
|
||||||
|
bool connected = false;
|
||||||
|
int percentage = -1; // 0..100, or -1 when the device only reports a level
|
||||||
|
Level level = Level::UNKNOWN;
|
||||||
|
bool charging = false;
|
||||||
|
bool externalPower = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Reads the battery level of a Logitech HID++ 2.0 device.
|
||||||
|
///
|
||||||
|
/// Prefers the power_supply entry that hid-logitech-hidpp exports, and falls
|
||||||
|
/// back to speaking HID++ 2.0 over /dev/hidraw* when that driver is not bound
|
||||||
|
/// (the receiver then sits on hid-generic and exports no battery at all).
|
||||||
|
///
|
||||||
|
/// read() blocks on wireless round-trips that can take seconds when the device
|
||||||
|
/// is asleep, so it must be called off the GTK main thread.
|
||||||
|
class BatteryReader {
|
||||||
|
public:
|
||||||
|
explicit BatteryReader(std::string deviceName);
|
||||||
|
~BatteryReader();
|
||||||
|
|
||||||
|
BatteryReader(const BatteryReader &) = delete;
|
||||||
|
BatteryReader &operator=(const BatteryReader &) = delete;
|
||||||
|
|
||||||
|
BatteryStatus read();
|
||||||
|
|
||||||
|
private:
|
||||||
|
/// A resolved HID++ endpoint: which hidraw node, which device slot on it,
|
||||||
|
/// and the feature index the battery lives behind.
|
||||||
|
struct Endpoint {
|
||||||
|
int fd = -1;
|
||||||
|
uint8_t deviceIndex = 0;
|
||||||
|
uint8_t featureIdx = 0;
|
||||||
|
uint16_t featureId = 0; // kFeatureUnifiedBattery or kFeatureBatteryStatus
|
||||||
|
};
|
||||||
|
|
||||||
|
std::string deviceName;
|
||||||
|
Endpoint endpoint{};
|
||||||
|
|
||||||
|
std::optional<BatteryStatus> readFromSysfs() const;
|
||||||
|
std::optional<BatteryStatus> readFromHidpp();
|
||||||
|
|
||||||
|
/// Finds the hidraw node and device slot our device answers on. Expensive:
|
||||||
|
/// only called on first use and after the cached endpoint stops replying.
|
||||||
|
bool resolveEndpoint();
|
||||||
|
void closeEndpoint();
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace hidpp
|
||||||
@@ -17,7 +17,7 @@
|
|||||||
#define NUM_WORKSPACES 7
|
#define NUM_WORKSPACES 7
|
||||||
|
|
||||||
class HyprlandService {
|
class HyprlandService {
|
||||||
inline static HyprlandService *instance = nullptr;
|
static inline std::shared_ptr<HyprlandService> instance;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
struct Client {
|
struct Client {
|
||||||
@@ -49,9 +49,9 @@ class HyprlandService {
|
|||||||
std::shared_ptr<Bar> bar;
|
std::shared_ptr<Bar> bar;
|
||||||
};
|
};
|
||||||
|
|
||||||
static HyprlandService *getInstance() {
|
static std::shared_ptr<HyprlandService> getInstance() {
|
||||||
if (!instance) {
|
if (!instance) {
|
||||||
instance = new HyprlandService();
|
instance = std::shared_ptr<HyprlandService>(new HyprlandService());
|
||||||
}
|
}
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
@@ -60,7 +60,12 @@ class HyprlandService {
|
|||||||
|
|
||||||
void addBar(std::shared_ptr<Bar> bar, std::string monitorName);
|
void addBar(std::shared_ptr<Bar> bar, std::string monitorName);
|
||||||
|
|
||||||
|
sigc::signal<void(std::string)> &signal_monitor_added() { return m_signal_monitor_added; }
|
||||||
|
sigc::signal<void(std::string)> &signal_monitor_removed() { return m_signal_monitor_removed; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
HyprlandService();
|
||||||
|
|
||||||
enum SocketEventType {
|
enum SocketEventType {
|
||||||
WORKSPACE_CHANGED,
|
WORKSPACE_CHANGED,
|
||||||
|
|
||||||
@@ -72,6 +77,7 @@ class HyprlandService {
|
|||||||
|
|
||||||
FOCUSED_MONITOR,
|
FOCUSED_MONITOR,
|
||||||
MONITOR_REMOVED,
|
MONITOR_REMOVED,
|
||||||
|
MONITOR_ADDED,
|
||||||
};
|
};
|
||||||
|
|
||||||
std::map<std::string, SocketEventType> socketEventTypeMap = {
|
std::map<std::string, SocketEventType> socketEventTypeMap = {
|
||||||
@@ -83,7 +89,9 @@ class HyprlandService {
|
|||||||
{"urgent", URGENT},
|
{"urgent", URGENT},
|
||||||
{"focusedmon", FOCUSED_MONITOR},
|
{"focusedmon", FOCUSED_MONITOR},
|
||||||
{"monitorremoved", MONITOR_REMOVED},
|
{"monitorremoved", MONITOR_REMOVED},
|
||||||
|
{"monitoradded", MONITOR_ADDED},
|
||||||
};
|
};
|
||||||
|
|
||||||
void onWorkspaceChanged(int workspaceId);
|
void onWorkspaceChanged(int workspaceId);
|
||||||
void onFocusedMonitorChanged(std::string monitorData);
|
void onFocusedMonitorChanged(std::string monitorData);
|
||||||
void onOpenWindow(std::string windowData);
|
void onOpenWindow(std::string windowData);
|
||||||
@@ -92,16 +100,15 @@ class HyprlandService {
|
|||||||
void onUrgent(std::string windowAddress);
|
void onUrgent(std::string windowAddress);
|
||||||
void onActiveWindowChanged(std::string windowAddress);
|
void onActiveWindowChanged(std::string windowAddress);
|
||||||
void onMonitorRemoved(std::string monitorName);
|
void onMonitorRemoved(std::string monitorName);
|
||||||
// void onMonitorAdded(std::string monitorName);
|
void onMonitorAdded(std::string monitorName);
|
||||||
|
|
||||||
HyprlandService();
|
|
||||||
std::map<std::string, std::shared_ptr<Monitor>> monitors;
|
std::map<std::string, std::shared_ptr<Monitor>> monitors;
|
||||||
std::map<int, std::shared_ptr<Workspace>> workspaces;
|
std::map<int, std::shared_ptr<Workspace>> workspaces;
|
||||||
std::map<std::string, std::shared_ptr<Client>> clients;
|
std::map<std::string, std::shared_ptr<Client>> clients;
|
||||||
|
|
||||||
/// maybe refactor into reusable class
|
/// maybe refactor into reusable class
|
||||||
std::string socketBuffer;
|
std::string socketBuffer;
|
||||||
int socketFd;
|
int socketFd = -1;
|
||||||
///
|
///
|
||||||
|
|
||||||
void bindHyprlandSocket();
|
void bindHyprlandSocket();
|
||||||
@@ -112,4 +119,7 @@ class HyprlandService {
|
|||||||
void refreshIndicator(std::shared_ptr<Workspace> workspace);
|
void refreshIndicator(std::shared_ptr<Workspace> workspace);
|
||||||
|
|
||||||
void handleSocketMessage(SocketHelper::SocketMessage message);
|
void handleSocketMessage(SocketHelper::SocketMessage message);
|
||||||
|
|
||||||
|
sigc::signal<void(std::string)> m_signal_monitor_added;
|
||||||
|
sigc::signal<void(std::string)> m_signal_monitor_removed;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -0,0 +1,57 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <condition_variable>
|
||||||
|
#include <glibmm/dispatcher.h>
|
||||||
|
#include <memory>
|
||||||
|
#include <mutex>
|
||||||
|
#include <sigc++/sigc++.h>
|
||||||
|
#include <string>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
#include "services/hidppBattery.hpp"
|
||||||
|
|
||||||
|
/// Polls the mouse battery once for the whole application.
|
||||||
|
///
|
||||||
|
/// The bar builds one window per monitor, so the widget cannot own the polling
|
||||||
|
/// itself without every monitor opening its own HID++ session against the same
|
||||||
|
/// receiver and competing for it. This service keeps a single worker thread and
|
||||||
|
/// fans the result out to every widget through updated().
|
||||||
|
class MouseBatteryService {
|
||||||
|
public:
|
||||||
|
static constexpr const char *kDeviceName = "MX Ergo S";
|
||||||
|
|
||||||
|
static std::shared_ptr<MouseBatteryService> getInstance();
|
||||||
|
|
||||||
|
~MouseBatteryService();
|
||||||
|
|
||||||
|
MouseBatteryService(const MouseBatteryService &) = delete;
|
||||||
|
MouseBatteryService &operator=(const MouseBatteryService &) = delete;
|
||||||
|
|
||||||
|
/// Last reading. Safe to call from the main thread.
|
||||||
|
hidpp::BatteryStatus getStatus() const;
|
||||||
|
|
||||||
|
/// Emitted on the main thread whenever a new reading lands.
|
||||||
|
sigc::signal<void()> &updated() { return updatedSignal; }
|
||||||
|
|
||||||
|
/// Stops the worker thread; called from the application's shutdown handler
|
||||||
|
/// so it cannot outlive the main loop the dispatcher posts to.
|
||||||
|
void stop();
|
||||||
|
|
||||||
|
private:
|
||||||
|
explicit MouseBatteryService(std::string deviceName);
|
||||||
|
|
||||||
|
void poll();
|
||||||
|
|
||||||
|
hidpp::BatteryReader reader;
|
||||||
|
|
||||||
|
Glib::Dispatcher dispatcher;
|
||||||
|
sigc::signal<void()> updatedSignal;
|
||||||
|
|
||||||
|
std::thread worker;
|
||||||
|
mutable std::mutex mutex;
|
||||||
|
std::condition_variable wakeup;
|
||||||
|
bool running = true;
|
||||||
|
hidpp::BatteryStatus status{};
|
||||||
|
|
||||||
|
inline static std::shared_ptr<MouseBatteryService> instance = nullptr;
|
||||||
|
};
|
||||||
@@ -12,11 +12,11 @@
|
|||||||
#include "gdkmm/monitor.h"
|
#include "gdkmm/monitor.h"
|
||||||
|
|
||||||
class NotificationController {
|
class NotificationController {
|
||||||
static std::shared_ptr<NotificationController> instance;
|
inline static std::shared_ptr<NotificationController> instance = nullptr;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static std::shared_ptr<NotificationController> getInstance() {
|
static std::shared_ptr<NotificationController> getInstance() {
|
||||||
if (!NotificationController::instance) {
|
if (NotificationController::instance == nullptr) {
|
||||||
NotificationController::instance = std::shared_ptr<NotificationController>(new NotificationController());
|
NotificationController::instance = std::shared_ptr<NotificationController>(new NotificationController());
|
||||||
}
|
}
|
||||||
return NotificationController::instance;
|
return NotificationController::instance;
|
||||||
@@ -26,12 +26,15 @@ class NotificationController {
|
|||||||
void showNotificationOnAllMonitors(NotifyMessage notify);
|
void showNotificationOnAllMonitors(NotifyMessage notify);
|
||||||
void showCopyNotification(NotifyMessage notify);
|
void showCopyNotification(NotifyMessage notify);
|
||||||
|
|
||||||
|
void addMonitor(std::shared_ptr<Gdk::Monitor> monitor);
|
||||||
|
void removeMonitor(std::shared_ptr<Gdk::Monitor> monitor);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint64_t globalNotificationId = 1;
|
uint64_t globalNotificationId = 1;
|
||||||
std::map<uint64_t, std::vector<std::shared_ptr<BaseNotification>>> activeNotifications;
|
std::map<uint64_t, std::vector<std::shared_ptr<BaseNotification>>> activeNotifications;
|
||||||
std::map<uint64_t, int> hoverCounts;
|
std::map<uint64_t, int> hoverCounts;
|
||||||
NotificationController();
|
NotificationController();
|
||||||
std::vector<std::shared_ptr<Gdk::Monitor>> activeMonitors;
|
std::map<std::string, std::shared_ptr<Gdk::Monitor>> activeMonitors;
|
||||||
void updateHoverState(uint64_t notificationId, bool isHovered);
|
void updateHoverState(uint64_t notificationId, bool isHovered);
|
||||||
void closeNotification(uint64_t notificationId);
|
void closeNotification(uint64_t notificationId);
|
||||||
};
|
};
|
||||||
@@ -12,9 +12,11 @@ class TextureCacheService {
|
|||||||
|
|
||||||
Glib::RefPtr<Gdk::Texture> getTexture(const std::string &url);
|
Glib::RefPtr<Gdk::Texture> getTexture(const std::string &url);
|
||||||
void pruneCache();
|
void pruneCache();
|
||||||
|
void clear();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TextureCacheService() = default;
|
TextureCacheService();
|
||||||
|
~TextureCacheService();
|
||||||
|
|
||||||
std::unordered_map<std::string, Glib::RefPtr<Gdk::Texture>> cache;
|
std::unordered_map<std::string, Glib::RefPtr<Gdk::Texture>> cache;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -0,0 +1,129 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
#include <memory>
|
||||||
|
#include <spdlog/spdlog.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#include "glibmm/main.h"
|
||||||
|
#include "gtkmm/mediafile.h"
|
||||||
|
#include "gtkmm/mediastream.h"
|
||||||
|
#include "sigc++/signal.h"
|
||||||
|
|
||||||
|
class TimerService {
|
||||||
|
struct TimerData {
|
||||||
|
uint64_t duration;
|
||||||
|
uint64_t timerId;
|
||||||
|
};
|
||||||
|
|
||||||
|
public:
|
||||||
|
static std::shared_ptr<TimerService> getInstance() {
|
||||||
|
if (!instance) {
|
||||||
|
instance = std::shared_ptr<TimerService>(new TimerService());
|
||||||
|
|
||||||
|
// add a timer to tick every second
|
||||||
|
Glib::signal_timeout().connect([]() {
|
||||||
|
instance->tick();
|
||||||
|
return true; // continue calling every second
|
||||||
|
},
|
||||||
|
1000);
|
||||||
|
}
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
void addTimer(uint64_t duration) {
|
||||||
|
this->timerAddedSignal.emit(std::to_string(duration), timerIdCounter);
|
||||||
|
|
||||||
|
this->activeTimers[timerIdCounter] = TimerData{.duration = duration, .timerId = timerIdCounter};
|
||||||
|
|
||||||
|
timerIdCounter++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void expireTimer(uint64_t timerId) {
|
||||||
|
this->timerExpiredSignal.emit(timerId);
|
||||||
|
|
||||||
|
if (ringingTimerCounter == 0) {
|
||||||
|
mediaFile->set_volume(1.0);
|
||||||
|
mediaFile->set_loop(true);
|
||||||
|
mediaFile->play();
|
||||||
|
spdlog::info("Playing timer sound");
|
||||||
|
}
|
||||||
|
|
||||||
|
ringingTimerCounter++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void removeTimer(uint64_t timerId) {
|
||||||
|
ringingTimerCounter--;
|
||||||
|
this->activeTimers.erase(timerId);
|
||||||
|
this->timerCancelledSignal.emit(timerId);
|
||||||
|
|
||||||
|
if (ringingTimerCounter <= 0) {
|
||||||
|
ringingTimerCounter = 0;
|
||||||
|
mediaFile->pause();
|
||||||
|
spdlog::info("Paused timer sound");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sigc::signal<void(const std::string &, u_int64_t)> &getSignalTimerSet() {
|
||||||
|
return this->timerAddedSignal;
|
||||||
|
}
|
||||||
|
|
||||||
|
sigc::signal<void(u_int64_t)> &getSignalTimerExpired() {
|
||||||
|
return this->timerExpiredSignal;
|
||||||
|
}
|
||||||
|
|
||||||
|
sigc::signal<void(u_int64_t)> &getSignalTimerCancelled() {
|
||||||
|
return this->timerCancelledSignal;
|
||||||
|
}
|
||||||
|
|
||||||
|
sigc::signal<void()> tickSignal;
|
||||||
|
|
||||||
|
private:
|
||||||
|
TimerService() {
|
||||||
|
this->mediaFile = Gtk::MediaFile::create();
|
||||||
|
if (this->mediaFile) {
|
||||||
|
this->mediaFile->set_filename("/usr/share/sounds/freedesktop/stereo/alarm-clock-elapsed.oga");
|
||||||
|
|
||||||
|
// Connect to signals to debug playback issues
|
||||||
|
this->mediaFile->property_error().signal_changed().connect([]() {
|
||||||
|
if (auto err = mediaFile->get_error()) {
|
||||||
|
spdlog::error("Timer sound error: {}", err.what());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
this->mediaFile->property_prepared().signal_changed().connect([]() {
|
||||||
|
if (mediaFile->is_prepared()) {
|
||||||
|
spdlog::info("Timer sound ready");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
spdlog::error("Failed to create media file!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline static Glib::RefPtr<Gtk::MediaFile> mediaFile;
|
||||||
|
|
||||||
|
static inline uint64_t timerIdCounter = 0;
|
||||||
|
static inline u_int64_t ringingTimerCounter = 0;
|
||||||
|
sigc::signal<void(const std::string &, u_int64_t)> timerAddedSignal;
|
||||||
|
sigc::signal<void(u_int64_t)> timerCancelledSignal;
|
||||||
|
sigc::signal<void(u_int64_t)> timerExpiredSignal;
|
||||||
|
inline static std::shared_ptr<TimerService> instance = nullptr;
|
||||||
|
|
||||||
|
std::map<uint64_t, TimerData> activeTimers;
|
||||||
|
|
||||||
|
void tick() {
|
||||||
|
for (auto &[id, timer] : activeTimers) {
|
||||||
|
|
||||||
|
if (timer.duration > 0) {
|
||||||
|
timer.duration--;
|
||||||
|
} else if (timer.duration == 0) {
|
||||||
|
spdlog::info("Timer {} expired", timer.timerId);
|
||||||
|
expireTimer(timer.timerId);
|
||||||
|
timer.duration = -1; // Mark as expired to prevent multiple expirations
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tickSignal.emit();
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <spdlog/spdlog.h>
|
||||||
|
|
||||||
|
#include "components/button/iconButton.hpp"
|
||||||
|
#include "connection/dbus/bluetooth.hpp"
|
||||||
|
|
||||||
|
#include "gtkmm/box.h"
|
||||||
|
#include "gtkmm/scrolledwindow.h"
|
||||||
|
|
||||||
|
#include "widgets/controlCenter/bluetoothSettingsRow.hpp"
|
||||||
|
|
||||||
|
class BluetoothSettings : public Gtk::Box {
|
||||||
|
public:
|
||||||
|
BluetoothSettings();
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::shared_ptr<BluetoothController> bluetoothController = BluetoothController::getInstance();
|
||||||
|
std::map<std::string, BluetoothDevice> activeBluetoothDevices;
|
||||||
|
std::map<std::string, std::shared_ptr<BluetoothSettingsRow>> deviceRows;
|
||||||
|
|
||||||
|
std::shared_ptr<IconButton> powerButton = std::make_shared<IconButton>(Icon::POWER_SETTINGS_NEW);
|
||||||
|
std::shared_ptr<IconButton> scanButton = std::make_shared<IconButton>(Icon::BLUETOOTH_SEARCHING);
|
||||||
|
|
||||||
|
Gtk::Box buttonRow;
|
||||||
|
Gtk::Box connectedDevicesBox;
|
||||||
|
Gtk::ScrolledWindow connectedDevicesScroll;
|
||||||
|
Gtk::Box availableDevicesBox;
|
||||||
|
Gtk::ScrolledWindow availableDevicesScroll;
|
||||||
|
|
||||||
|
bool bluetoothIsPowered = false;
|
||||||
|
bool bluetoothIsScanning = false;
|
||||||
|
|
||||||
|
void addBluetoothDevice(const BluetoothDevice &device);
|
||||||
|
void removeBluetoothDevice(const std::string &object_path);
|
||||||
|
void updateBluetoothDevice(const BluetoothDevice &device);
|
||||||
|
|
||||||
|
void setBluetoothPowered(bool powered);
|
||||||
|
void setScanning(bool scanning);
|
||||||
|
};
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <spdlog/spdlog.h>
|
||||||
|
#include "components/button/iconButton.hpp"
|
||||||
|
#include "connection/dbus/bluetooth.hpp"
|
||||||
|
#include "gtkmm/box.h"
|
||||||
|
#include "gtkmm/button.h"
|
||||||
|
#include "gtkmm/image.h"
|
||||||
|
#include "gtkmm/label.h"
|
||||||
|
|
||||||
|
class BluetoothSettingsRow : public Gtk::Box {
|
||||||
|
public:
|
||||||
|
BluetoothSettingsRow(const BluetoothDevice &device);
|
||||||
|
void updateDevice(const BluetoothDevice &device);
|
||||||
|
|
||||||
|
private:
|
||||||
|
BluetoothDevice device;
|
||||||
|
Gtk::Image icon;
|
||||||
|
Gtk::Label nameLabel;
|
||||||
|
Gtk::Label addressLabel;
|
||||||
|
IconButton pairButton;
|
||||||
|
IconButton connectButton;
|
||||||
|
IconButton trustButton;
|
||||||
|
};
|
||||||
@@ -6,6 +6,8 @@
|
|||||||
#include "components/button/tabButton.hpp"
|
#include "components/button/tabButton.hpp"
|
||||||
#include "components/popover.hpp"
|
#include "components/popover.hpp"
|
||||||
#include "widgets/controlCenter/mediaWidget.hpp"
|
#include "widgets/controlCenter/mediaWidget.hpp"
|
||||||
|
#include "widgets/controlCenter/settings.hpp"
|
||||||
|
#include "widgets/controlCenter/timer.hpp"
|
||||||
#include "widgets/weather.hpp"
|
#include "widgets/weather.hpp"
|
||||||
|
|
||||||
#include "gtkmm/box.h"
|
#include "gtkmm/box.h"
|
||||||
@@ -26,9 +28,12 @@ class ControlCenter : public Popover {
|
|||||||
std::unique_ptr<TabButton> mediaTabButton;
|
std::unique_ptr<TabButton> mediaTabButton;
|
||||||
std::unique_ptr<TabButton> infoTabButton;
|
std::unique_ptr<TabButton> infoTabButton;
|
||||||
std::unique_ptr<TabButton> timerButton;
|
std::unique_ptr<TabButton> timerButton;
|
||||||
|
std::unique_ptr<TabButton> settingsTabButton;
|
||||||
|
|
||||||
std::unique_ptr<WeatherWidget> weatherWidget;
|
std::unique_ptr<WeatherWidget> weatherWidget;
|
||||||
std::unique_ptr<MediaWidget> mediaControlWidget;
|
std::unique_ptr<MediaWidget> mediaControlWidget;
|
||||||
|
std::unique_ptr<TimerWidget> timerWidget;
|
||||||
|
std::unique_ptr<SettingsWidget> settingsWidget;
|
||||||
|
|
||||||
void addPlayerWidget(const std::string &bus_name);
|
void addPlayerWidget(const std::string &bus_name);
|
||||||
void removePlayerWidget(const std::string &bus_name);
|
void removePlayerWidget(const std::string &bus_name);
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "components/mediaPlayer.hpp"
|
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include "components/mediaPlayer.hpp"
|
||||||
#include "connection/dbus/mpris.hpp"
|
#include "connection/dbus/mpris.hpp"
|
||||||
|
|
||||||
#include "gtkmm/box.h"
|
#include "gtkmm/box.h"
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
#include "connection/dbus/bluetooth.hpp"
|
||||||
|
#include "widgets/controlCenter/bluetoothSettings.hpp"
|
||||||
|
|
||||||
|
#include "gtkmm/box.h"
|
||||||
|
|
||||||
|
class SettingsWidget : public Gtk::Box {
|
||||||
|
public:
|
||||||
|
SettingsWidget();
|
||||||
|
|
||||||
|
private:
|
||||||
|
BluetoothSettings bluetoothSettings;
|
||||||
|
};
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <sigc++/connection.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#include "components/timer.hpp"
|
||||||
|
|
||||||
|
#include "gtkmm/box.h"
|
||||||
|
|
||||||
|
class TimerWidget : public Gtk::Box {
|
||||||
|
public:
|
||||||
|
TimerWidget();
|
||||||
|
~TimerWidget();
|
||||||
|
void addTimer(const std::string &duration, uint64_t timerId);
|
||||||
|
void removeTimer(uint64_t timerId);
|
||||||
|
void activateTimer(uint64_t timerId);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::shared_ptr<TimerService> timerService = TimerService::getInstance();
|
||||||
|
bool updatingText = false;
|
||||||
|
std::string rawDigits;
|
||||||
|
|
||||||
|
std::map<uint64_t, std::unique_ptr<Timer>> activeTimers;
|
||||||
|
sigc::connection timerSetConnection;
|
||||||
|
sigc::connection timerCancelledConnection;
|
||||||
|
sigc::connection timerExpiredConnection;
|
||||||
|
sigc::connection tickConnection;
|
||||||
|
};
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <gtkmm/box.h>
|
||||||
|
#include <gtkmm/label.h>
|
||||||
|
#include <memory>
|
||||||
|
#include <sigc++/sigc++.h>
|
||||||
|
|
||||||
|
#include "services/mouseBatteryService.hpp"
|
||||||
|
|
||||||
|
/// Shows the battery level of the wireless mouse tracked by MouseBatteryService.
|
||||||
|
class MouseBatteryWidget : public Gtk::Box {
|
||||||
|
public:
|
||||||
|
MouseBatteryWidget();
|
||||||
|
~MouseBatteryWidget() override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Gtk::Label icon;
|
||||||
|
Gtk::Label label;
|
||||||
|
|
||||||
|
std::shared_ptr<MouseBatteryService> service;
|
||||||
|
sigc::connection updatedConn;
|
||||||
|
|
||||||
|
void refresh();
|
||||||
|
};
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <memory>
|
||||||
#include <sigc++/connection.h>
|
#include <sigc++/connection.h>
|
||||||
|
|
||||||
#include "gdkmm/monitor.h"
|
#include "gdkmm/monitor.h"
|
||||||
@@ -9,7 +10,7 @@
|
|||||||
|
|
||||||
#define DEFAULT_NOTIFICATION_TIMEOUT 6700
|
#define DEFAULT_NOTIFICATION_TIMEOUT 6700
|
||||||
|
|
||||||
class BaseNotification : public Gtk::Window {
|
class BaseNotification : public Gtk::Window, public std::enable_shared_from_this<BaseNotification> {
|
||||||
public:
|
public:
|
||||||
BaseNotification(uint64_t notificationId, std::shared_ptr<Gdk::Monitor> monitor);
|
BaseNotification(uint64_t notificationId, std::shared_ptr<Gdk::Monitor> monitor);
|
||||||
|
|
||||||
@@ -17,14 +18,29 @@ class BaseNotification : public Gtk::Window {
|
|||||||
void resumeAutoClose();
|
void resumeAutoClose();
|
||||||
void startAutoClose(int timeoutMs);
|
void startAutoClose(int timeoutMs);
|
||||||
|
|
||||||
sigc::signal<void(uint64_t)> signal_close;
|
|
||||||
sigc::signal<void(bool)> signal_hover_changed;
|
|
||||||
|
|
||||||
uint64_t getNotificationId() const {
|
uint64_t getNotificationId() const {
|
||||||
return this->notificationId;
|
return this->notificationId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sigc::signal<void(uint64_t)> getSignalClose() {
|
||||||
|
return this->signalClose;
|
||||||
|
}
|
||||||
|
|
||||||
|
sigc::signal<void(bool)> getSignalHoverChanged() {
|
||||||
|
return this->signalHoverChanged;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
sigc::signal<void(uint64_t)> signalClose;
|
||||||
|
sigc::signal<void(bool)> signalHoverChanged;
|
||||||
|
|
||||||
|
uint64_t notificationId;
|
||||||
|
|
||||||
|
bool autoClosePaused = false;
|
||||||
|
int autoCloseRemainingMs = 0;
|
||||||
|
std::chrono::steady_clock::time_point autoCloseDeadline;
|
||||||
|
sigc::connection autoCloseConnection;
|
||||||
|
|
||||||
void ensure_notification_css_loaded();
|
void ensure_notification_css_loaded();
|
||||||
|
|
||||||
void start_auto_close_timeout(int timeoutMs);
|
void start_auto_close_timeout(int timeoutMs);
|
||||||
@@ -32,10 +48,15 @@ class BaseNotification : public Gtk::Window {
|
|||||||
void resume_auto_close();
|
void resume_auto_close();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
uint64_t notificationId;
|
bool getAutoClosePaused() const {
|
||||||
|
return this->autoClosePaused;
|
||||||
|
}
|
||||||
|
|
||||||
bool autoClosePaused = false;
|
int getAutoCloseRemainingMs() const {
|
||||||
int autoCloseRemainingMs = 0;
|
return this->autoCloseRemainingMs;
|
||||||
std::chrono::steady_clock::time_point autoCloseDeadline;
|
}
|
||||||
sigc::connection autoCloseConnection;
|
|
||||||
|
std::chrono::steady_clock::time_point getAutoCloseDeadline() const {
|
||||||
|
return this->autoCloseDeadline;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
@@ -1,20 +1,58 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <glibmm/dispatcher.h>
|
||||||
#include <gtkmm/box.h>
|
#include <gtkmm/box.h>
|
||||||
#include <gtkmm/label.h>
|
#include <gtkmm/label.h>
|
||||||
#include <sigc++/sigc++.h>
|
#include <sigc++/sigc++.h>
|
||||||
|
|
||||||
|
#include "gtkmm/scrolledwindow.h"
|
||||||
|
|
||||||
class WeatherWidget : public Gtk::Box {
|
class WeatherWidget : public Gtk::Box {
|
||||||
public:
|
public:
|
||||||
|
struct CurrentWeather {
|
||||||
|
double temperature;
|
||||||
|
double apparent_temperature;
|
||||||
|
double precipitation;
|
||||||
|
int weather_code;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct DailyWeather {
|
||||||
|
double temperature_2m_max;
|
||||||
|
double temperature_2m_min;
|
||||||
|
double apparent_temperature_min;
|
||||||
|
double apparent_temperature_max;
|
||||||
|
double precipitation_sum;
|
||||||
|
int weather_code;
|
||||||
|
std::string time;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct HourlyWeather {
|
||||||
|
double temperature_2m;
|
||||||
|
double apparent_temperature;
|
||||||
|
double precipitation_probability;
|
||||||
|
double precipitation;
|
||||||
|
int weather_code;
|
||||||
|
std::string time;
|
||||||
|
};
|
||||||
|
|
||||||
WeatherWidget();
|
WeatherWidget();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Gtk::Label titleLabel;
|
std::array<DailyWeather, 7> daily_weather{};
|
||||||
Gtk::Label currentLabel;
|
std::array<HourlyWeather, 24> hourly_weather{};
|
||||||
Gtk::Label todayLabel;
|
CurrentWeather current_weather{};
|
||||||
|
|
||||||
|
Gtk::ScrolledWindow currentScroll;
|
||||||
|
Gtk::ScrolledWindow hourlyScroll;
|
||||||
|
Gtk::ScrolledWindow dailyScroll;
|
||||||
|
|
||||||
|
Glib::Dispatcher m_dispatcher;
|
||||||
|
|
||||||
bool onRefreshTick();
|
bool onRefreshTick();
|
||||||
void fetchWeather();
|
void fetchWeather();
|
||||||
void applyWeatherText(const std::string ¤t_text,
|
|
||||||
const std::string &today_text);
|
void refreshCurrentWeather();
|
||||||
|
void refreshHourlyWeather();
|
||||||
|
void refreshDailyWeather();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
#include "app.hpp"
|
#include "app.hpp"
|
||||||
|
|
||||||
int main()
|
int main() {
|
||||||
{
|
|
||||||
App app;
|
App app;
|
||||||
|
|
||||||
return app.run();
|
return app.run();
|
||||||
|
|||||||
+133
-18
@@ -1,4 +1,29 @@
|
|||||||
|
/* Custom Scrollbar Styling */
|
||||||
/** biome-ignore-all lint/correctness/noUnknownTypeSelector: gtk css has more valid identifiers */
|
/** biome-ignore-all lint/correctness/noUnknownTypeSelector: gtk css has more valid identifiers */
|
||||||
|
|
||||||
|
scrollbar, scrollbar * {
|
||||||
|
min-width: 8px;
|
||||||
|
background: transparent;
|
||||||
|
}
|
||||||
|
|
||||||
|
scrollbar slider {
|
||||||
|
background: rgba(255, 255, 255, 0.25);
|
||||||
|
border-radius: 6px;
|
||||||
|
min-width: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
scrollbar slider:hover, scrollbar slider:active {
|
||||||
|
background: rgba(255, 255, 255, 0.45);
|
||||||
|
}
|
||||||
|
|
||||||
|
scrollbar trough {
|
||||||
|
background: rgba(255, 255, 255, 0.08);
|
||||||
|
border-radius: 6px;
|
||||||
|
}
|
||||||
|
|
||||||
|
scrollbar button {
|
||||||
|
background: transparent;
|
||||||
|
}
|
||||||
* {
|
* {
|
||||||
all: unset;
|
all: unset;
|
||||||
}
|
}
|
||||||
@@ -16,28 +41,39 @@ window {
|
|||||||
color: #ffffff;
|
color: #ffffff;
|
||||||
font-size: 14px;
|
font-size: 14px;
|
||||||
font-family: var(--text-font);
|
font-family: var(--text-font);
|
||||||
padding: 2px 6px;
|
padding: 3px;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
.text-area {
|
||||||
|
font-family: var(--text-font-mono);
|
||||||
|
background-color: rgba(25, 25, 25, 0.8);
|
||||||
|
border-radius: 8px;
|
||||||
|
padding: 4px 8px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.material-icons {
|
.material-icons {
|
||||||
font-family: var(--icon-font-material);
|
font-family: var(--icon-font-material);
|
||||||
}
|
}
|
||||||
|
|
||||||
.icon-button {
|
.power-button-on {
|
||||||
font-size: 20px;
|
/* bright green */
|
||||||
|
background-color: rgba(0, 255, 0, 0.2);
|
||||||
}
|
}
|
||||||
|
|
||||||
.tab-icon {
|
.power-button-off {
|
||||||
font-size: 20px;
|
/* bright red */
|
||||||
|
background-color: rgba(255, 0, 0, 0.2);
|
||||||
|
color: #ffffff;
|
||||||
}
|
}
|
||||||
|
|
||||||
.control-center-tab-row {
|
.control-center-tab-row {
|
||||||
background-color: rgba(50, 50, 50, 0.8);
|
background-color: rgba(255, 255, 255, 0.1);
|
||||||
border-radius: 4px;
|
border-radius: 4px;
|
||||||
padding: 2px 4px;
|
|
||||||
margin-bottom: 4px;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
.active-button {
|
.active-button {
|
||||||
background-color: #ffffff;
|
background-color: #ffffff;
|
||||||
color: #1e1e1e;
|
color: #1e1e1e;
|
||||||
@@ -46,20 +82,20 @@ window {
|
|||||||
}
|
}
|
||||||
|
|
||||||
popover {
|
popover {
|
||||||
margin-top: 4px;
|
font-family: var(--text-font);
|
||||||
font-family: var(--text-font);
|
border-radius: 12px;
|
||||||
border-radius: 4px;
|
|
||||||
|
|
||||||
background: rgba(25, 25, 25, 0.8);
|
|
||||||
|
background: rgba(25, 25, 25, 0.9);
|
||||||
box-shadow: 0 4px 30px rgba(0, 0, 0, 0.1);
|
box-shadow: 0 4px 30px rgba(0, 0, 0, 0.1);
|
||||||
border: 1px solid rgba(57, 57, 57, 0.71);
|
border: 1px solid rgba(57, 57, 57, 0.71);
|
||||||
}
|
}
|
||||||
|
|
||||||
.control-center-popover {
|
.control-center-popover {
|
||||||
|
margin-top: 6px;
|
||||||
background-color: rgba(35, 35, 35, 0.95);
|
background-color: rgba(35, 35, 35, 0.95);
|
||||||
padding: 12px;
|
padding: 12px;
|
||||||
padding-top : 6px;
|
padding-top : 6px;
|
||||||
border-radius: 4px;
|
|
||||||
box-shadow: 0 4px 30px rgba(0, 0, 0, 0.3);
|
box-shadow: 0 4px 30px rgba(0, 0, 0, 0.3);
|
||||||
border: 1px solid rgba(57, 57, 57, 0.8);
|
border: 1px solid rgba(57, 57, 57, 0.8);
|
||||||
}
|
}
|
||||||
@@ -67,7 +103,6 @@ popover {
|
|||||||
.control-center-player-container {
|
.control-center-player-container {
|
||||||
border-radius: 4px;
|
border-radius: 4px;
|
||||||
background: rgba(35, 35, 35, 0.95);
|
background: rgba(35, 35, 35, 0.95);
|
||||||
margin-bottom: 6px;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -137,7 +172,6 @@ tooltip {
|
|||||||
#spacer {
|
#spacer {
|
||||||
font-weight: 900;
|
font-weight: 900;
|
||||||
padding: 0 5px;
|
padding: 0 5px;
|
||||||
text-shadow: 0 0 5px #ffffffaa;
|
|
||||||
border-radius: 4px;
|
border-radius: 4px;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -147,10 +181,10 @@ tooltip {
|
|||||||
}
|
}
|
||||||
|
|
||||||
.workspace-pill {
|
.workspace-pill {
|
||||||
padding: 2px 5px;
|
padding: 2px 4px;
|
||||||
margin-right: 6px;
|
|
||||||
border-radius: 4px;
|
border-radius: 4px;
|
||||||
text-shadow: 0 0 2px #646464;
|
margin: 0 2px;
|
||||||
|
color: #cccccc;
|
||||||
transition:
|
transition:
|
||||||
background-color 0.2s,
|
background-color 0.2s,
|
||||||
color 0.2s,
|
color 0.2s,
|
||||||
@@ -201,6 +235,17 @@ tooltip {
|
|||||||
margin-top: 4px;
|
margin-top: 4px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
.icon-button {
|
||||||
|
font-weight: 700;
|
||||||
|
font-size: 18px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tab-icon {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@keyframes workspace-updown {
|
@keyframes workspace-updown {
|
||||||
0% {
|
0% {
|
||||||
transform: translateY(4px);
|
transform: translateY(4px);
|
||||||
@@ -228,3 +273,73 @@ tooltip {
|
|||||||
opacity: 1;
|
opacity: 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.bluetooth-settings {
|
||||||
|
font-family: var(--text-font);
|
||||||
|
font-size: 14px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.bluetooth-device-address {
|
||||||
|
font-size: 10px;
|
||||||
|
color: #cccccce7;
|
||||||
|
}
|
||||||
|
|
||||||
|
.bluetooth-settings-row {
|
||||||
|
transition: background-color 0.2s;
|
||||||
|
}
|
||||||
|
|
||||||
|
.active-devices {
|
||||||
|
background-color: rgba(0, 255, 0, 0.1);
|
||||||
|
border-radius: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.mouse-battery {
|
||||||
|
padding: 0 6px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.mouse-battery-good {
|
||||||
|
color: #ffffff;
|
||||||
|
}
|
||||||
|
|
||||||
|
.mouse-battery-low {
|
||||||
|
color: #ffb454;
|
||||||
|
}
|
||||||
|
|
||||||
|
.mouse-battery-critical {
|
||||||
|
color: #ff5555;
|
||||||
|
}
|
||||||
|
|
||||||
|
.mouse-battery-charging {
|
||||||
|
color: #5aff8f;
|
||||||
|
}
|
||||||
|
|
||||||
|
.mouse-battery-disconnected {
|
||||||
|
color: #ffffff66;
|
||||||
|
}
|
||||||
|
|
||||||
|
.available-devices {
|
||||||
|
background-color: rgba(255, 255, 255, 0.1);
|
||||||
|
border-radius: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.current-weather-scroll {
|
||||||
|
background-color: rgba(255, 255, 255, 0.1);
|
||||||
|
|
||||||
|
border-radius: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.hourly-weather-scroll {
|
||||||
|
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||||
|
border-radius: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.daily-weather-scroll {
|
||||||
|
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||||
|
border-radius: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.weather-info {
|
||||||
|
padding: 4px 8px;
|
||||||
|
border-radius: 4px;
|
||||||
|
background-color: rgba(255, 255, 255, 0.1);
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,282 @@
|
|||||||
|
{
|
||||||
|
"0":{
|
||||||
|
"day":{
|
||||||
|
"description":"Sunny",
|
||||||
|
"image":"http://openweathermap.org/img/wn/01d@2x.png"
|
||||||
|
},
|
||||||
|
"night":{
|
||||||
|
"description":"Clear",
|
||||||
|
"image":"http://openweathermap.org/img/wn/01n@2x.png"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"1":{
|
||||||
|
"day":{
|
||||||
|
"description":"Mainly Sunny",
|
||||||
|
"image":"http://openweathermap.org/img/wn/01d@2x.png"
|
||||||
|
},
|
||||||
|
"night":{
|
||||||
|
"description":"Mainly Clear",
|
||||||
|
"image":"http://openweathermap.org/img/wn/01n@2x.png"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"2":{
|
||||||
|
"day":{
|
||||||
|
"description":"Partly Cloudy",
|
||||||
|
"image":"http://openweathermap.org/img/wn/02d@2x.png"
|
||||||
|
},
|
||||||
|
"night":{
|
||||||
|
"description":"Partly Cloudy",
|
||||||
|
"image":"http://openweathermap.org/img/wn/02n@2x.png"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"3":{
|
||||||
|
"day":{
|
||||||
|
"description":"Cloudy",
|
||||||
|
"image":"http://openweathermap.org/img/wn/03d@2x.png"
|
||||||
|
},
|
||||||
|
"night":{
|
||||||
|
"description":"Cloudy",
|
||||||
|
"image":"http://openweathermap.org/img/wn/03n@2x.png"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"45":{
|
||||||
|
"day":{
|
||||||
|
"description":"Foggy",
|
||||||
|
"image":"http://openweathermap.org/img/wn/50d@2x.png"
|
||||||
|
},
|
||||||
|
"night":{
|
||||||
|
"description":"Foggy",
|
||||||
|
"image":"http://openweathermap.org/img/wn/50n@2x.png"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"48":{
|
||||||
|
"day":{
|
||||||
|
"description":"Rime Fog",
|
||||||
|
"image":"http://openweathermap.org/img/wn/50d@2x.png"
|
||||||
|
},
|
||||||
|
"night":{
|
||||||
|
"description":"Rime Fog",
|
||||||
|
"image":"http://openweathermap.org/img/wn/50n@2x.png"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"51":{
|
||||||
|
"day":{
|
||||||
|
"description":"Light Drizzle",
|
||||||
|
"image":"http://openweathermap.org/img/wn/09d@2x.png"
|
||||||
|
},
|
||||||
|
"night":{
|
||||||
|
"description":"Light Drizzle",
|
||||||
|
"image":"http://openweathermap.org/img/wn/09n@2x.png"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"53":{
|
||||||
|
"day":{
|
||||||
|
"description":"Drizzle",
|
||||||
|
"image":"http://openweathermap.org/img/wn/09d@2x.png"
|
||||||
|
},
|
||||||
|
"night":{
|
||||||
|
"description":"Drizzle",
|
||||||
|
"image":"http://openweathermap.org/img/wn/09n@2x.png"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"55":{
|
||||||
|
"day":{
|
||||||
|
"description":"Heavy Drizzle",
|
||||||
|
"image":"http://openweathermap.org/img/wn/09d@2x.png"
|
||||||
|
},
|
||||||
|
"night":{
|
||||||
|
"description":"Heavy Drizzle",
|
||||||
|
"image":"http://openweathermap.org/img/wn/09n@2x.png"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"56":{
|
||||||
|
"day":{
|
||||||
|
"description":"Light Freezing Drizzle",
|
||||||
|
"image":"http://openweathermap.org/img/wn/09d@2x.png"
|
||||||
|
},
|
||||||
|
"night":{
|
||||||
|
"description":"Light Freezing Drizzle",
|
||||||
|
"image":"http://openweathermap.org/img/wn/09n@2x.png"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"57":{
|
||||||
|
"day":{
|
||||||
|
"description":"Freezing Drizzle",
|
||||||
|
"image":"http://openweathermap.org/img/wn/09d@2x.png"
|
||||||
|
},
|
||||||
|
"night":{
|
||||||
|
"description":"Freezing Drizzle",
|
||||||
|
"image":"http://openweathermap.org/img/wn/09n@2x.png"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"61":{
|
||||||
|
"day":{
|
||||||
|
"description":"Light Rain",
|
||||||
|
"image":"http://openweathermap.org/img/wn/10d@2x.png"
|
||||||
|
},
|
||||||
|
"night":{
|
||||||
|
"description":"Light Rain",
|
||||||
|
"image":"http://openweathermap.org/img/wn/10n@2x.png"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"63":{
|
||||||
|
"day":{
|
||||||
|
"description":"Rain",
|
||||||
|
"image":"http://openweathermap.org/img/wn/10d@2x.png"
|
||||||
|
},
|
||||||
|
"night":{
|
||||||
|
"description":"Rain",
|
||||||
|
"image":"http://openweathermap.org/img/wn/10n@2x.png"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"65":{
|
||||||
|
"day":{
|
||||||
|
"description":"Heavy Rain",
|
||||||
|
"image":"http://openweathermap.org/img/wn/10d@2x.png"
|
||||||
|
},
|
||||||
|
"night":{
|
||||||
|
"description":"Heavy Rain",
|
||||||
|
"image":"http://openweathermap.org/img/wn/10n@2x.png"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"66":{
|
||||||
|
"day":{
|
||||||
|
"description":"Light Freezing Rain",
|
||||||
|
"image":"http://openweathermap.org/img/wn/10d@2x.png"
|
||||||
|
},
|
||||||
|
"night":{
|
||||||
|
"description":"Light Freezing Rain",
|
||||||
|
"image":"http://openweathermap.org/img/wn/10n@2x.png"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"67":{
|
||||||
|
"day":{
|
||||||
|
"description":"Freezing Rain",
|
||||||
|
"image":"http://openweathermap.org/img/wn/10d@2x.png"
|
||||||
|
},
|
||||||
|
"night":{
|
||||||
|
"description":"Freezing Rain",
|
||||||
|
"image":"http://openweathermap.org/img/wn/10n@2x.png"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"71":{
|
||||||
|
"day":{
|
||||||
|
"description":"Light Snow",
|
||||||
|
"image":"http://openweathermap.org/img/wn/13d@2x.png"
|
||||||
|
},
|
||||||
|
"night":{
|
||||||
|
"description":"Light Snow",
|
||||||
|
"image":"http://openweathermap.org/img/wn/13n@2x.png"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"73":{
|
||||||
|
"day":{
|
||||||
|
"description":"Snow",
|
||||||
|
"image":"http://openweathermap.org/img/wn/13d@2x.png"
|
||||||
|
},
|
||||||
|
"night":{
|
||||||
|
"description":"Snow",
|
||||||
|
"image":"http://openweathermap.org/img/wn/13n@2x.png"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"75":{
|
||||||
|
"day":{
|
||||||
|
"description":"Heavy Snow",
|
||||||
|
"image":"http://openweathermap.org/img/wn/13d@2x.png"
|
||||||
|
},
|
||||||
|
"night":{
|
||||||
|
"description":"Heavy Snow",
|
||||||
|
"image":"http://openweathermap.org/img/wn/13n@2x.png"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"77":{
|
||||||
|
"day":{
|
||||||
|
"description":"Snow Grains",
|
||||||
|
"image":"http://openweathermap.org/img/wn/13d@2x.png"
|
||||||
|
},
|
||||||
|
"night":{
|
||||||
|
"description":"Snow Grains",
|
||||||
|
"image":"http://openweathermap.org/img/wn/13n@2x.png"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"80":{
|
||||||
|
"day":{
|
||||||
|
"description":"Light Showers",
|
||||||
|
"image":"http://openweathermap.org/img/wn/09d@2x.png"
|
||||||
|
},
|
||||||
|
"night":{
|
||||||
|
"description":"Light Showers",
|
||||||
|
"image":"http://openweathermap.org/img/wn/09n@2x.png"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"81":{
|
||||||
|
"day":{
|
||||||
|
"description":"Showers",
|
||||||
|
"image":"http://openweathermap.org/img/wn/09d@2x.png"
|
||||||
|
},
|
||||||
|
"night":{
|
||||||
|
"description":"Showers",
|
||||||
|
"image":"http://openweathermap.org/img/wn/09n@2x.png"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"82":{
|
||||||
|
"day":{
|
||||||
|
"description":"Heavy Showers",
|
||||||
|
"image":"http://openweathermap.org/img/wn/09d@2x.png"
|
||||||
|
},
|
||||||
|
"night":{
|
||||||
|
"description":"Heavy Showers",
|
||||||
|
"image":"http://openweathermap.org/img/wn/09n@2x.png"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"85":{
|
||||||
|
"day":{
|
||||||
|
"description":"Light Snow Showers",
|
||||||
|
"image":"http://openweathermap.org/img/wn/13d@2x.png"
|
||||||
|
},
|
||||||
|
"night":{
|
||||||
|
"description":"Light Snow Showers",
|
||||||
|
"image":"http://openweathermap.org/img/wn/13n@2x.png"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"86":{
|
||||||
|
"day":{
|
||||||
|
"description":"Snow Showers",
|
||||||
|
"image":"http://openweathermap.org/img/wn/13d@2x.png"
|
||||||
|
},
|
||||||
|
"night":{
|
||||||
|
"description":"Snow Showers",
|
||||||
|
"image":"http://openweathermap.org/img/wn/13n@2x.png"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"95":{
|
||||||
|
"day":{
|
||||||
|
"description":"Thunderstorm",
|
||||||
|
"image":"http://openweathermap.org/img/wn/11d@2x.png"
|
||||||
|
},
|
||||||
|
"night":{
|
||||||
|
"description":"Thunderstorm",
|
||||||
|
"image":"http://openweathermap.org/img/wn/11n@2x.png"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"96":{
|
||||||
|
"day":{
|
||||||
|
"description":"Light Thunderstorms With Hail",
|
||||||
|
"image":"http://openweathermap.org/img/wn/11d@2x.png"
|
||||||
|
},
|
||||||
|
"night":{
|
||||||
|
"description":"Light Thunderstorms With Hail",
|
||||||
|
"image":"http://openweathermap.org/img/wn/11n@2x.png"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"99":{
|
||||||
|
"day":{
|
||||||
|
"description":"Thunderstorm With Hail",
|
||||||
|
"image":"http://openweathermap.org/img/wn/11d@2x.png"
|
||||||
|
},
|
||||||
|
"night":{
|
||||||
|
"description":"Thunderstorm With Hail",
|
||||||
|
"image":"http://openweathermap.org/img/wn/11n@2x.png"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+81
-10
@@ -1,26 +1,90 @@
|
|||||||
#include "app.hpp"
|
#include "app.hpp"
|
||||||
|
|
||||||
|
#include <glibmm/main.h>
|
||||||
#include <sigc++/sigc++.h>
|
#include <sigc++/sigc++.h>
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include "connection/dbus/mpris.hpp"
|
#include "connection/dbus/mpris.hpp"
|
||||||
#include "connection/dbus/notification.hpp"
|
#include "connection/dbus/notification.hpp"
|
||||||
|
#include "services/mouseBatteryService.hpp"
|
||||||
#include "services/notificationController.hpp"
|
#include "services/notificationController.hpp"
|
||||||
#include "services/textureCache.hpp"
|
#include "services/textureCache.hpp"
|
||||||
|
|
||||||
App::App() {
|
App::App() : app(Gtk::Application::create("org.example.mybar")) {
|
||||||
this->app = Gtk::Application::create("org.example.mybar");
|
|
||||||
this->setupServices();
|
this->setupServices();
|
||||||
this->hyprlandService = HyprlandService::getInstance();
|
|
||||||
this->notificationService = std::make_shared<NotificationService>();
|
|
||||||
this->mprisController = MprisController::getInstance();
|
|
||||||
|
|
||||||
auto notificationController = NotificationController::getInstance();
|
|
||||||
this->mprisController->signal_mpris_updated().connect(
|
this->mprisController->signal_mpris_updated().connect(
|
||||||
[notificationController](const MprisPlayer2Message &msg) {
|
[this](const MprisPlayer2Message &msg) {
|
||||||
notificationController->showSpotifyNotification(msg);
|
this->notificationController->showSpotifyNotification(msg);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this->hyprlandService->signal_monitor_added().connect([this](std::string monitorName) {
|
||||||
|
spdlog::info("[App] Monitor added: {}", monitorName);
|
||||||
|
auto tryCreateBar = [this](const std::string &name) -> bool {
|
||||||
|
if (bars.find(name) != bars.end()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto display = Gdk::Display::get_default();
|
||||||
|
if (!display) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto monitors = display->get_monitors();
|
||||||
|
for (guint i = 0; i < monitors->get_n_items(); ++i) {
|
||||||
|
auto monitor = std::dynamic_pointer_cast<Gdk::Monitor>(
|
||||||
|
monitors->get_object(i));
|
||||||
|
|
||||||
|
if (monitor && monitor->get_connector() == name) {
|
||||||
|
auto bar = std::make_shared<Bar>(monitor->gobj());
|
||||||
|
|
||||||
|
bar->set_application(app);
|
||||||
|
bar->show();
|
||||||
|
|
||||||
|
std::string connectorName = monitor->get_connector();
|
||||||
|
bar->addLeftWidget(hyprlandService->getWorkspaceIndicatorsForMonitor(connectorName));
|
||||||
|
hyprlandService->addBar(bar, connectorName);
|
||||||
|
|
||||||
|
BarMonitorPair barMonitorPair{.bar = bar, .window = monitor};
|
||||||
|
bars[connectorName] = barMonitorPair;
|
||||||
|
if (this->notificationController) {
|
||||||
|
this->notificationController->addMonitor(monitor);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (tryCreateBar(monitorName)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto attempts = std::make_shared<int>(0);
|
||||||
|
Glib::signal_timeout().connect(
|
||||||
|
[monitorName, attempts, tryCreateBar]() -> bool {
|
||||||
|
if (*attempts >= 10) {
|
||||||
|
spdlog::warn("[App] Monitor {} not ready after retries", monitorName);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
(*attempts)++;
|
||||||
|
return !tryCreateBar(monitorName);
|
||||||
|
},
|
||||||
|
200);
|
||||||
|
});
|
||||||
|
|
||||||
|
this->hyprlandService->signal_monitor_removed().connect([this](std::string monitorName) {
|
||||||
|
spdlog::info("[App] Monitor removed: {}", monitorName);
|
||||||
|
auto bar = bars[monitorName].bar;
|
||||||
|
|
||||||
|
if (bar) {
|
||||||
|
bar->close();
|
||||||
|
}
|
||||||
|
|
||||||
|
bars.erase(monitorName);
|
||||||
|
});
|
||||||
|
|
||||||
app->signal_activate().connect([&]() {
|
app->signal_activate().connect([&]() {
|
||||||
auto display = Gdk::Display::get_default();
|
auto display = Gdk::Display::get_default();
|
||||||
auto monitors = display->get_monitors();
|
auto monitors = display->get_monitors();
|
||||||
@@ -39,12 +103,19 @@ App::App() {
|
|||||||
bar->addLeftWidget(hyprlandService->getWorkspaceIndicatorsForMonitor(monitorName));
|
bar->addLeftWidget(hyprlandService->getWorkspaceIndicatorsForMonitor(monitorName));
|
||||||
hyprlandService->addBar(bar, monitorName);
|
hyprlandService->addBar(bar, monitorName);
|
||||||
|
|
||||||
bars.push_back(bar);
|
BarMonitorPair barMonitorPair{.bar = bar, .window = monitor};
|
||||||
|
|
||||||
|
bars[monitorName] = barMonitorPair;
|
||||||
|
if (this->notificationController) {
|
||||||
|
this->notificationController->addMonitor(monitor);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
app->signal_shutdown().connect([&]() {
|
app->signal_shutdown().connect([&]() {
|
||||||
|
TextureCacheService::getInstance()->clear();
|
||||||
|
MouseBatteryService::getInstance()->stop();
|
||||||
this->trayService->stop();
|
this->trayService->stop();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-2
@@ -6,6 +6,7 @@
|
|||||||
#include <gtkmm/window.h>
|
#include <gtkmm/window.h>
|
||||||
|
|
||||||
#include "components/button/textButton.hpp"
|
#include "components/button/textButton.hpp"
|
||||||
|
#include "connection/dbus/bluetooth.hpp"
|
||||||
#include "helpers/system.hpp"
|
#include "helpers/system.hpp"
|
||||||
#include "widgets/date.hpp"
|
#include "widgets/date.hpp"
|
||||||
#include "widgets/spacer.hpp"
|
#include "widgets/spacer.hpp"
|
||||||
@@ -13,10 +14,12 @@
|
|||||||
|
|
||||||
#include "glibmm/main.h"
|
#include "glibmm/main.h"
|
||||||
#include "gtk/gtk.h"
|
#include "gtk/gtk.h"
|
||||||
#include "sigc++/functors/mem_fun.h"
|
|
||||||
|
|
||||||
Bar::Bar(GdkMonitor *monitor) {
|
Bar::Bar(GdkMonitor *monitor) {
|
||||||
set_name("bar-window");
|
set_name("bar-window");
|
||||||
|
add_css_class("bar");
|
||||||
|
|
||||||
|
auto bluetooh = BluetoothController::getInstance();
|
||||||
|
|
||||||
gtk_layer_init_for_window(this->gobj());
|
gtk_layer_init_for_window(this->gobj());
|
||||||
|
|
||||||
@@ -32,6 +35,7 @@ Bar::Bar(GdkMonitor *monitor) {
|
|||||||
gtk_layer_auto_exclusive_zone_enable(this->gobj());
|
gtk_layer_auto_exclusive_zone_enable(this->gobj());
|
||||||
|
|
||||||
set_child(main_box);
|
set_child(main_box);
|
||||||
|
set_valign(Gtk::Align::CENTER);
|
||||||
|
|
||||||
this->volumeWidget = std::make_shared<VolumeWidget>();
|
this->volumeWidget = std::make_shared<VolumeWidget>();
|
||||||
this->trayWidget = std::make_shared<TrayWidget>();
|
this->trayWidget = std::make_shared<TrayWidget>();
|
||||||
@@ -43,7 +47,7 @@ Bar::Bar(GdkMonitor *monitor) {
|
|||||||
date.onUpdate();
|
date.onUpdate();
|
||||||
|
|
||||||
Glib::signal_timeout().connect(sigc::mem_fun(clock, &Clock::onUpdate), 1000);
|
Glib::signal_timeout().connect(sigc::mem_fun(clock, &Clock::onUpdate), 1000);
|
||||||
Glib::signal_timeout().connect(sigc::mem_fun(date, &Date::onUpdate), 1000);
|
Glib::signal_timeout().connect(sigc::mem_fun(date, &Date::onUpdate), 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Bar::setup_ui() {
|
void Bar::setup_ui() {
|
||||||
@@ -71,6 +75,7 @@ void Bar::setup_center_box() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Bar::setup_right_box() {
|
void Bar::setup_right_box() {
|
||||||
|
right_box.append(this->mouseBattery);
|
||||||
right_box.append(*this->trayWidget);
|
right_box.append(*this->trayWidget);
|
||||||
right_box.append(this->homeAssistant);
|
right_box.append(this->homeAssistant);
|
||||||
right_box.append(this->controlCenter);
|
right_box.append(this->controlCenter);
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
#include "components/button/iconButton.hpp"
|
#include "components/button/iconButton.hpp"
|
||||||
|
|
||||||
IconButton::IconButton(Icon::Type icon, std::string fontFamilyCss) : TextButton(Icon::toString(icon)) {
|
IconButton::IconButton(Icon::Type icon, std::string fontFamilyCss) : TextButton(Icon::toString(icon)) {
|
||||||
|
this->get_style_context()->add_class("icon-button");
|
||||||
this->get_style_context()->add_class(fontFamilyCss);
|
this->get_style_context()->add_class(fontFamilyCss);
|
||||||
this->add_css_class("icon-button");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void IconButton::setIcon(Icon::Type icon) {
|
void IconButton::setIcon(Icon::Type icon) {
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
#include "components/mediaPlayer.hpp"
|
|
||||||
|
|
||||||
#include "components/button/iconButton.hpp"
|
#include "components/button/iconButton.hpp"
|
||||||
|
#include "components/mediaPlayer.hpp"
|
||||||
#include "helpers/string.hpp"
|
#include "helpers/string.hpp"
|
||||||
#include "services/textureCache.hpp"
|
#include "services/textureCache.hpp"
|
||||||
|
|
||||||
|
|||||||
@@ -7,14 +7,17 @@ Popover::Popover(Icon::Type icon, std::string name) : IconButton(icon) {
|
|||||||
|
|
||||||
set_name(name);
|
set_name(name);
|
||||||
|
|
||||||
this->add_css_class("material-icons");
|
|
||||||
|
|
||||||
popover = std::make_unique<Gtk::Popover>();
|
popover = std::make_unique<Gtk::Popover>();
|
||||||
popover->set_parent(*this);
|
popover->set_parent(*this);
|
||||||
popover->set_autohide(true);
|
popover->set_autohide(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
Popover::~Popover() = default;
|
Popover::~Popover() {
|
||||||
|
if (popover) {
|
||||||
|
popover->popdown();
|
||||||
|
popover->unparent();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
void Popover::on_toggle_window() {
|
void Popover::on_toggle_window() {
|
||||||
if (popover->get_visible()) {
|
if (popover->get_visible()) {
|
||||||
@@ -22,4 +25,4 @@ void Popover::on_toggle_window() {
|
|||||||
} else {
|
} else {
|
||||||
popover->popup();
|
popover->popup();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,7 +6,6 @@
|
|||||||
|
|
||||||
WorkspaceIndicator::WorkspaceIndicator(int id, std::string label, sigc::slot<void(int)> onClick)
|
WorkspaceIndicator::WorkspaceIndicator(int id, std::string label, sigc::slot<void(int)> onClick)
|
||||||
: Gtk::Box(Gtk::Orientation::HORIZONTAL) {
|
: Gtk::Box(Gtk::Orientation::HORIZONTAL) {
|
||||||
|
|
||||||
overlay = std::make_shared<Gtk::Overlay>();
|
overlay = std::make_shared<Gtk::Overlay>();
|
||||||
auto numLabel = Gtk::make_managed<Gtk::Label>(label);
|
auto numLabel = Gtk::make_managed<Gtk::Label>(label);
|
||||||
auto pillContainer = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL);
|
auto pillContainer = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL);
|
||||||
|
|||||||
@@ -0,0 +1,768 @@
|
|||||||
|
#include "connection/dbus/bluetooth.hpp"
|
||||||
|
#include "services/notificationController.hpp"
|
||||||
|
|
||||||
|
#include <spdlog/spdlog.h>
|
||||||
|
#include <iomanip>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
const std::string agent_introspection_xml = R"(
|
||||||
|
<node>
|
||||||
|
<interface name="org.bluez.Agent1">
|
||||||
|
<method name="Release"/>
|
||||||
|
<method name="RequestPinCode">
|
||||||
|
<arg type="o" name="device" direction="in"/>
|
||||||
|
<arg type="s" name="pincode" direction="out"/>
|
||||||
|
</method>
|
||||||
|
<method name="DisplayPinCode">
|
||||||
|
<arg type="o" name="device" direction="in"/>
|
||||||
|
<arg type="s" name="pincode" direction="in"/>
|
||||||
|
</method>
|
||||||
|
<method name="RequestPasskey">
|
||||||
|
<arg type="o" name="device" direction="in"/>
|
||||||
|
<arg type="u" name="passkey" direction="out"/>
|
||||||
|
</method>
|
||||||
|
<method name="DisplayPasskey">
|
||||||
|
<arg type="o" name="device" direction="in"/>
|
||||||
|
<arg type="u" name="passkey" direction="in"/>
|
||||||
|
<arg type="q" name="entered" direction="in"/>
|
||||||
|
</method>
|
||||||
|
<method name="RequestConfirmation">
|
||||||
|
<arg type="o" name="device" direction="in"/>
|
||||||
|
<arg type="u" name="passkey" direction="in"/>
|
||||||
|
</method>
|
||||||
|
<method name="RequestAuthorization">
|
||||||
|
<arg type="o" name="device" direction="in"/>
|
||||||
|
</method>
|
||||||
|
<method name="AuthorizeService">
|
||||||
|
<arg type="o" name="device" direction="in"/>
|
||||||
|
<arg type="s" name="uuid" direction="in"/>
|
||||||
|
</method>
|
||||||
|
<method name="Cancel"/>
|
||||||
|
</interface>
|
||||||
|
</node>
|
||||||
|
)";
|
||||||
|
|
||||||
|
BluetoothController::BluetoothController() {
|
||||||
|
connect_system_async(sigc::mem_fun(*this, &BluetoothController::onBusConnected));
|
||||||
|
}
|
||||||
|
|
||||||
|
sigc::signal<void(bool)> &BluetoothController::signalPoweredChanged() { return m_powered_signal; }
|
||||||
|
sigc::signal<void(bool)> &BluetoothController::signalDiscoveringChanged() { return m_discovering_signal; }
|
||||||
|
sigc::signal<void(const BluetoothDevice &)> &BluetoothController::signalDeviceAdded() { return m_device_added_signal; }
|
||||||
|
sigc::signal<void(const std::string &)> &BluetoothController::signalDeviceRemoved() { return m_device_removed_signal; }
|
||||||
|
sigc::signal<void(const BluetoothDevice &)> &BluetoothController::signalDeviceChanged() { return m_device_changed_signal; }
|
||||||
|
|
||||||
|
bool BluetoothController::isPowered() const { return m_powered; }
|
||||||
|
bool BluetoothController::isDiscovering() const { return m_discovering; }
|
||||||
|
|
||||||
|
std::vector<BluetoothDevice> BluetoothController::getDevices() const {
|
||||||
|
std::vector<BluetoothDevice> result;
|
||||||
|
result.reserve(m_devices.size());
|
||||||
|
for (const auto &[_, device] : m_devices) {
|
||||||
|
result.push_back(device);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<BluetoothDevice> BluetoothController::getPairedDevices() const {
|
||||||
|
std::vector<BluetoothDevice> result;
|
||||||
|
for (const auto &[_, device] : m_devices) {
|
||||||
|
if (device.paired) {
|
||||||
|
result.push_back(device);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void BluetoothController::setPowered(bool powered) {
|
||||||
|
spdlog::info("Bluetooth: setting powered to {}", powered);
|
||||||
|
setDbusProperty(m_adapter_path, "org.bluez.Adapter1", "Powered",
|
||||||
|
Glib::Variant<bool>::create(powered));
|
||||||
|
}
|
||||||
|
|
||||||
|
void BluetoothController::startDiscovery() {
|
||||||
|
spdlog::info("Bluetooth: starting discovery");
|
||||||
|
if (m_adapter_proxy) {
|
||||||
|
m_adapter_proxy->call("StartDiscovery");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void BluetoothController::stopDiscovery() {
|
||||||
|
spdlog::info("Bluetooth: stopping discovery");
|
||||||
|
if (m_adapter_proxy) {
|
||||||
|
m_adapter_proxy->call("StopDiscovery");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void BluetoothController::pairDevice(const std::string &object_path) {
|
||||||
|
spdlog::info("Bluetooth: pairing device {}", object_path);
|
||||||
|
auto it = m_device_proxies.find(object_path);
|
||||||
|
if (it != m_device_proxies.end() && it->second) {
|
||||||
|
it->second->call("Pair");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void BluetoothController::unpairDevice(const std::string &object_path) {
|
||||||
|
spdlog::info("Bluetooth: unpairing device {}", object_path);
|
||||||
|
if (!m_adapter_proxy)
|
||||||
|
return;
|
||||||
|
|
||||||
|
auto params = Glib::VariantContainerBase::create_tuple(
|
||||||
|
Glib::Variant<Glib::DBusObjectPathString>::create(object_path));
|
||||||
|
m_adapter_proxy->call("RemoveDevice", params);
|
||||||
|
}
|
||||||
|
|
||||||
|
void BluetoothController::connectDevice(const std::string &object_path) {
|
||||||
|
spdlog::info("Bluetooth: connecting device {}", object_path);
|
||||||
|
auto it = m_device_proxies.find(object_path);
|
||||||
|
if (it != m_device_proxies.end() && it->second) {
|
||||||
|
it->second->call("Connect");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void BluetoothController::disconnectDevice(const std::string &object_path) {
|
||||||
|
spdlog::info("Bluetooth: disconnecting device {}", object_path);
|
||||||
|
auto it = m_device_proxies.find(object_path);
|
||||||
|
if (it != m_device_proxies.end() && it->second) {
|
||||||
|
it->second->call("Disconnect");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void BluetoothController::trustDevice(const std::string &object_path, bool trusted) {
|
||||||
|
spdlog::info("Bluetooth: setting trusted={} for device {}", trusted, object_path);
|
||||||
|
setDbusProperty(object_path, "org.bluez.Device1", "Trusted",
|
||||||
|
Glib::Variant<bool>::create(trusted));
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Bus connection
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
void BluetoothController::onBusConnected(const Glib::RefPtr<Gio::AsyncResult> &result) {
|
||||||
|
if (!result) {
|
||||||
|
spdlog::error("Bluetooth: null async result");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
connection = Gio::DBus::Connection::get_finish(result);
|
||||||
|
|
||||||
|
m_object_manager_proxy = Gio::DBus::Proxy::create_sync(
|
||||||
|
connection,
|
||||||
|
"org.bluez",
|
||||||
|
"/",
|
||||||
|
"org.freedesktop.DBus.ObjectManager");
|
||||||
|
|
||||||
|
if (m_object_manager_proxy) {
|
||||||
|
m_object_manager_proxy->signal_signal().connect(
|
||||||
|
sigc::mem_fun(*this, &BluetoothController::onObjectManagerSignal));
|
||||||
|
|
||||||
|
enumerateObjects();
|
||||||
|
}
|
||||||
|
|
||||||
|
registerAgent();
|
||||||
|
} catch (const Glib::Error &ex) {
|
||||||
|
spdlog::error("Bluetooth DBus Connection Error: {}", ex.what());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Enumerate existing BlueZ objects
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
void BluetoothController::enumerateObjects() {
|
||||||
|
if (!m_object_manager_proxy)
|
||||||
|
return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
auto result = m_object_manager_proxy->call_sync("GetManagedObjects");
|
||||||
|
auto objects = Glib::VariantBase::cast_dynamic<Glib::VariantContainerBase>(
|
||||||
|
result.get_child(0));
|
||||||
|
|
||||||
|
for (gsize i = 0; i < objects.get_n_children(); i++) {
|
||||||
|
auto entry = Glib::VariantBase::cast_dynamic<Glib::VariantContainerBase>(
|
||||||
|
objects.get_child(i));
|
||||||
|
|
||||||
|
auto path = static_cast<std::string>(
|
||||||
|
Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::DBusObjectPathString>>(
|
||||||
|
entry.get_child(0))
|
||||||
|
.get());
|
||||||
|
|
||||||
|
parseInterfaces(path, entry.get_child(1));
|
||||||
|
}
|
||||||
|
} catch (const Glib::Error &ex) {
|
||||||
|
spdlog::error("Bluetooth: Error enumerating objects: {}", ex.what());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Parse an interfaces-and-properties dict a{sa{sv}}
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
void BluetoothController::parseInterfaces(const std::string &object_path,
|
||||||
|
const Glib::VariantBase &interfaces_var) {
|
||||||
|
auto ifaces = Glib::VariantBase::cast_dynamic<Glib::VariantContainerBase>(interfaces_var);
|
||||||
|
|
||||||
|
for (gsize i = 0; i < ifaces.get_n_children(); i++) {
|
||||||
|
auto entry = Glib::VariantBase::cast_dynamic<Glib::VariantContainerBase>(
|
||||||
|
ifaces.get_child(i));
|
||||||
|
|
||||||
|
auto iface_name = static_cast<std::string>(
|
||||||
|
Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(
|
||||||
|
entry.get_child(0))
|
||||||
|
.get());
|
||||||
|
|
||||||
|
if (iface_name != "org.bluez.Device1" && iface_name != "org.bluez.Adapter1") {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto props = Glib::VariantBase::cast_dynamic<Glib::Variant<PropertiesMap>>(
|
||||||
|
entry.get_child(1))
|
||||||
|
.get();
|
||||||
|
|
||||||
|
if (iface_name == "org.bluez.Device1") {
|
||||||
|
addDevice(object_path, props);
|
||||||
|
} else {
|
||||||
|
setupAdapter(object_path, props);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// ObjectManager signal handler (InterfacesAdded / InterfacesRemoved)
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
void BluetoothController::onObjectManagerSignal(
|
||||||
|
const Glib::ustring &,
|
||||||
|
const Glib::ustring &signal_name,
|
||||||
|
const Glib::VariantContainerBase ¶meters) {
|
||||||
|
|
||||||
|
if (signal_name == "InterfacesAdded") {
|
||||||
|
// signature: (oa{sa{sv}})
|
||||||
|
auto path = static_cast<std::string>(
|
||||||
|
Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::DBusObjectPathString>>(
|
||||||
|
parameters.get_child(0))
|
||||||
|
.get());
|
||||||
|
|
||||||
|
parseInterfaces(path, parameters.get_child(1));
|
||||||
|
|
||||||
|
} else if (signal_name == "InterfacesRemoved") {
|
||||||
|
// signature: (oas)
|
||||||
|
auto path = static_cast<std::string>(
|
||||||
|
Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::DBusObjectPathString>>(
|
||||||
|
parameters.get_child(0))
|
||||||
|
.get());
|
||||||
|
|
||||||
|
auto ifaces = Glib::VariantBase::cast_dynamic<
|
||||||
|
Glib::Variant<std::vector<Glib::ustring>>>(parameters.get_child(1))
|
||||||
|
.get();
|
||||||
|
|
||||||
|
for (const auto &iface : ifaces) {
|
||||||
|
if (iface == "org.bluez.Device1") {
|
||||||
|
removeDevice(path);
|
||||||
|
} else if (iface == "org.bluez.Adapter1" && path == m_adapter_path) {
|
||||||
|
m_adapter_proxy.reset();
|
||||||
|
m_adapter_path.clear();
|
||||||
|
m_powered = false;
|
||||||
|
m_discovering = false;
|
||||||
|
m_powered_signal.emit(false);
|
||||||
|
m_discovering_signal.emit(false);
|
||||||
|
spdlog::info("Bluetooth adapter removed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Adapter
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
void BluetoothController::setupAdapter(const std::string &path,
|
||||||
|
const PropertiesMap &properties) {
|
||||||
|
m_adapter_path = path;
|
||||||
|
spdlog::info("Bluetooth adapter found: {}", path);
|
||||||
|
|
||||||
|
auto it = properties.find("Powered");
|
||||||
|
if (it != properties.end() && it->second.is_of_type(Glib::VariantType("b"))) {
|
||||||
|
m_powered = Glib::VariantBase::cast_dynamic<Glib::Variant<bool>>(it->second).get();
|
||||||
|
m_powered_signal.emit(m_powered);
|
||||||
|
}
|
||||||
|
|
||||||
|
it = properties.find("Discovering");
|
||||||
|
if (it != properties.end() && it->second.is_of_type(Glib::VariantType("b"))) {
|
||||||
|
m_discovering = Glib::VariantBase::cast_dynamic<Glib::Variant<bool>>(it->second).get();
|
||||||
|
m_discovering_signal.emit(m_discovering);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
m_adapter_proxy = Gio::DBus::Proxy::create_sync(
|
||||||
|
connection, "org.bluez", path, "org.bluez.Adapter1");
|
||||||
|
|
||||||
|
if (m_adapter_proxy) {
|
||||||
|
m_adapter_proxy->signal_properties_changed().connect(
|
||||||
|
sigc::mem_fun(*this, &BluetoothController::onAdapterPropertiesChanged));
|
||||||
|
}
|
||||||
|
} catch (const Glib::Error &ex) {
|
||||||
|
spdlog::error("Bluetooth: Error creating adapter proxy: {}", ex.what());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void BluetoothController::onAdapterPropertiesChanged(
|
||||||
|
const Gio::DBus::Proxy::MapChangedProperties &changed,
|
||||||
|
const std::vector<Glib::ustring> &) {
|
||||||
|
|
||||||
|
auto it = changed.find("Powered");
|
||||||
|
if (it != changed.end() && it->second.is_of_type(Glib::VariantType("b"))) {
|
||||||
|
m_powered = Glib::VariantBase::cast_dynamic<Glib::Variant<bool>>(it->second).get();
|
||||||
|
spdlog::info("Bluetooth powered: {}", m_powered);
|
||||||
|
m_powered_signal.emit(m_powered);
|
||||||
|
}
|
||||||
|
|
||||||
|
it = changed.find("Discovering");
|
||||||
|
if (it != changed.end() && it->second.is_of_type(Glib::VariantType("b"))) {
|
||||||
|
m_discovering = Glib::VariantBase::cast_dynamic<Glib::Variant<bool>>(it->second).get();
|
||||||
|
spdlog::info("Bluetooth discovering: {}", m_discovering);
|
||||||
|
m_discovering_signal.emit(m_discovering);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Device management
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
void BluetoothController::addDevice(const std::string &path,
|
||||||
|
const PropertiesMap &properties) {
|
||||||
|
auto device = parseDeviceProperties(path, properties);
|
||||||
|
m_devices[path] = device;
|
||||||
|
|
||||||
|
spdlog::info("Bluetooth device added: {} ({}) {}", device.name, device.address, device.icon);
|
||||||
|
|
||||||
|
try {
|
||||||
|
auto proxy = Gio::DBus::Proxy::create_sync(
|
||||||
|
connection, "org.bluez", path, "org.bluez.Device1");
|
||||||
|
|
||||||
|
if (proxy) {
|
||||||
|
proxy->signal_properties_changed().connect(
|
||||||
|
[this, path](const Gio::DBus::Proxy::MapChangedProperties &changed,
|
||||||
|
const std::vector<Glib::ustring> &invalidated) {
|
||||||
|
onDevicePropertiesChanged(path, changed, invalidated);
|
||||||
|
});
|
||||||
|
m_device_proxies[path] = proxy;
|
||||||
|
}
|
||||||
|
} catch (const Glib::Error &ex) {
|
||||||
|
spdlog::error("Bluetooth: Error creating device proxy for {}: {}", path, ex.what());
|
||||||
|
}
|
||||||
|
|
||||||
|
m_device_added_signal.emit(device);
|
||||||
|
}
|
||||||
|
|
||||||
|
void BluetoothController::removeDevice(const std::string &path) {
|
||||||
|
spdlog::info("Bluetooth device removed: {}", path);
|
||||||
|
m_devices.erase(path);
|
||||||
|
m_device_proxies.erase(path);
|
||||||
|
m_device_removed_signal.emit(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
void BluetoothController::onDevicePropertiesChanged(
|
||||||
|
const std::string &object_path,
|
||||||
|
const Gio::DBus::Proxy::MapChangedProperties &changed,
|
||||||
|
const std::vector<Glib::ustring> &) {
|
||||||
|
|
||||||
|
auto it = m_devices.find(object_path);
|
||||||
|
if (it == m_devices.end())
|
||||||
|
return;
|
||||||
|
|
||||||
|
auto &device = it->second;
|
||||||
|
|
||||||
|
for (const auto &[key, value] : changed) {
|
||||||
|
if (key == "Name" && value.is_of_type(Glib::VariantType("s"))) {
|
||||||
|
device.name = static_cast<std::string>(
|
||||||
|
Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(value).get());
|
||||||
|
} else if (key == "Alias" && value.is_of_type(Glib::VariantType("s"))) {
|
||||||
|
// Only use Alias as fallback if Name is still the MAC address
|
||||||
|
auto alias = static_cast<std::string>(
|
||||||
|
Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(value).get());
|
||||||
|
if (device.name == device.address) {
|
||||||
|
device.name = alias;
|
||||||
|
}
|
||||||
|
} else if (key == "Paired" && value.is_of_type(Glib::VariantType("b"))) {
|
||||||
|
device.paired = Glib::VariantBase::cast_dynamic<Glib::Variant<bool>>(value).get();
|
||||||
|
} else if (key == "Connected" && value.is_of_type(Glib::VariantType("b"))) {
|
||||||
|
device.connected = Glib::VariantBase::cast_dynamic<Glib::Variant<bool>>(value).get();
|
||||||
|
} else if (key == "Trusted" && value.is_of_type(Glib::VariantType("b"))) {
|
||||||
|
device.trusted = Glib::VariantBase::cast_dynamic<Glib::Variant<bool>>(value).get();
|
||||||
|
} else if (key == "RSSI" && value.is_of_type(Glib::VariantType("n"))) {
|
||||||
|
device.rssi = Glib::VariantBase::cast_dynamic<Glib::Variant<gint16>>(value).get();
|
||||||
|
} else if (key == "Icon" && value.is_of_type(Glib::VariantType("s"))) {
|
||||||
|
device.icon = static_cast<std::string>(
|
||||||
|
Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(value).get());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
m_device_changed_signal.emit(device);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Property parsing helper
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
BluetoothDevice BluetoothController::parseDeviceProperties(
|
||||||
|
const std::string &path,
|
||||||
|
const PropertiesMap &properties) {
|
||||||
|
|
||||||
|
BluetoothDevice device;
|
||||||
|
device.object_path = path;
|
||||||
|
|
||||||
|
auto getString = [&](const Glib::ustring &key) -> std::string {
|
||||||
|
auto it = properties.find(key);
|
||||||
|
if (it != properties.end() && it->second.is_of_type(Glib::VariantType("s"))) {
|
||||||
|
return static_cast<std::string>(
|
||||||
|
Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(it->second).get());
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
};
|
||||||
|
|
||||||
|
auto getBool = [&](const Glib::ustring &key) -> bool {
|
||||||
|
auto it = properties.find(key);
|
||||||
|
if (it != properties.end() && it->second.is_of_type(Glib::VariantType("b"))) {
|
||||||
|
return Glib::VariantBase::cast_dynamic<Glib::Variant<bool>>(it->second).get();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
device.address = getString("Address");
|
||||||
|
device.name = getString("Name");
|
||||||
|
if (device.name.empty())
|
||||||
|
device.name = getString("Alias");
|
||||||
|
if (device.name.empty())
|
||||||
|
device.name = device.address;
|
||||||
|
device.icon = getString("Icon");
|
||||||
|
device.paired = getBool("Paired");
|
||||||
|
device.connected = getBool("Connected");
|
||||||
|
device.trusted = getBool("Trusted");
|
||||||
|
|
||||||
|
auto rssi_it = properties.find("RSSI");
|
||||||
|
if (rssi_it != properties.end() && rssi_it->second.is_of_type(Glib::VariantType("n"))) {
|
||||||
|
device.rssi = Glib::VariantBase::cast_dynamic<Glib::Variant<gint16>>(rssi_it->second).get();
|
||||||
|
}
|
||||||
|
|
||||||
|
return device;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// D-Bus property setter (calls org.freedesktop.DBus.Properties.Set)
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
void BluetoothController::setDbusProperty(const std::string &object_path,
|
||||||
|
const std::string &interface,
|
||||||
|
const std::string &property,
|
||||||
|
const Glib::VariantBase &value) {
|
||||||
|
if (!connection || object_path.empty())
|
||||||
|
return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
auto props_proxy = Gio::DBus::Proxy::create_sync(
|
||||||
|
connection, "org.bluez", object_path, "org.freedesktop.DBus.Properties");
|
||||||
|
|
||||||
|
auto wrapped = Glib::Variant<Glib::VariantBase>::create(value);
|
||||||
|
auto params = Glib::VariantContainerBase::create_tuple({Glib::Variant<Glib::ustring>::create(interface),
|
||||||
|
Glib::Variant<Glib::ustring>::create(property),
|
||||||
|
wrapped});
|
||||||
|
|
||||||
|
props_proxy->call_sync("Set", params);
|
||||||
|
} catch (const Glib::Error &ex) {
|
||||||
|
spdlog::error("Error setting {}.{}: {}", interface, property, ex.what());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Agent
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
void BluetoothController::registerAgent() {
|
||||||
|
if (!connection)
|
||||||
|
return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
m_node_info = Gio::DBus::NodeInfo::create_for_xml(agent_introspection_xml);
|
||||||
|
auto interface_info = m_node_info->lookup_interface("org.bluez.Agent1");
|
||||||
|
|
||||||
|
m_interface_vtable = std::make_shared<Gio::DBus::InterfaceVTable>(
|
||||||
|
sigc::mem_fun(*this, &BluetoothController::on_agent_method_call));
|
||||||
|
|
||||||
|
m_agent_id = connection->register_object("/org/bluez/bar_agent", interface_info, *m_interface_vtable);
|
||||||
|
|
||||||
|
auto agent_manager = Gio::DBus::Proxy::create_sync(
|
||||||
|
connection, "org.bluez", "/org/bluez", "org.bluez.AgentManager1");
|
||||||
|
|
||||||
|
if (agent_manager) {
|
||||||
|
agent_manager->call_sync(
|
||||||
|
"RegisterAgent",
|
||||||
|
Glib::VariantContainerBase::create_tuple(
|
||||||
|
{Glib::Variant<Glib::DBusObjectPathString>::create("/org/bluez/bar_agent"),
|
||||||
|
Glib::Variant<Glib::ustring>::create("DisplayYesNo")}));
|
||||||
|
|
||||||
|
agent_manager->call_sync(
|
||||||
|
"RequestDefaultAgent",
|
||||||
|
Glib::VariantContainerBase::create_tuple(
|
||||||
|
{Glib::Variant<Glib::DBusObjectPathString>::create("/org/bluez/bar_agent")}));
|
||||||
|
|
||||||
|
spdlog::info("Bluetooth Agent registered successfully");
|
||||||
|
}
|
||||||
|
} catch (const Glib::Error &ex) {
|
||||||
|
spdlog::error("Bluetooth: Failed to register agent: {}", ex.what());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void BluetoothController::on_agent_method_call(
|
||||||
|
const Glib::RefPtr<Gio::DBus::Connection> &,
|
||||||
|
const Glib::ustring &,
|
||||||
|
const Glib::ustring &,
|
||||||
|
const Glib::ustring &,
|
||||||
|
const Glib::ustring &method_name,
|
||||||
|
const Glib::VariantContainerBase ¶meters,
|
||||||
|
const Glib::RefPtr<Gio::DBus::MethodInvocation> &invocation) {
|
||||||
|
|
||||||
|
spdlog::info("Bluetooth Agent method called: {}", method_name.c_str());
|
||||||
|
|
||||||
|
if (method_name == "RequestConfirmation") {
|
||||||
|
// Signature: (ou)
|
||||||
|
std::string device_path = static_cast<std::string>(
|
||||||
|
Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::DBusObjectPathString>>(
|
||||||
|
parameters.get_child(0)).get());
|
||||||
|
|
||||||
|
guint32 passkey = Glib::VariantBase::cast_dynamic<Glib::Variant<guint32>>(
|
||||||
|
parameters.get_child(1)).get();
|
||||||
|
|
||||||
|
std::string device_name = device_path;
|
||||||
|
if (m_devices.count(device_path)) {
|
||||||
|
device_name = m_devices[device_path].name;
|
||||||
|
if (device_name.empty())
|
||||||
|
device_name = m_devices[device_path].address;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ostringstream ss;
|
||||||
|
ss << std::setw(6) << std::setfill('0') << passkey;
|
||||||
|
std::string passkey_str = ss.str();
|
||||||
|
|
||||||
|
NotifyMessage msg;
|
||||||
|
msg.summary = "Bluetooth Pairing Request";
|
||||||
|
msg.body = "Device '" + device_name + "' wants to pair.\nPasskey: " + passkey_str;
|
||||||
|
msg.app_name = "Bar";
|
||||||
|
msg.actions = {"Confirm", "Confirm", "Cancel", "Cancel"};
|
||||||
|
msg.urgency = NotificationUrgency::CRITICAL;
|
||||||
|
msg.expire_timeout = -1; // No timeout, requires user interaction
|
||||||
|
|
||||||
|
// Capture invocation to respond later
|
||||||
|
// Note: invocation is a RefPtr, so copying it increases ref count
|
||||||
|
msg.on_action = [invocation](const std::string &action) {
|
||||||
|
if (action == "Confirm") {
|
||||||
|
invocation->return_value(Glib::VariantContainerBase());
|
||||||
|
} else {
|
||||||
|
invocation->return_dbus_error("org.bluez.Error.Rejected", "Rejected by user");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
NotificationController::getInstance()->showNotificationOnAllMonitors(msg);
|
||||||
|
|
||||||
|
} else if (method_name == "RequestAuthorization") {
|
||||||
|
// Signature: (o)
|
||||||
|
std::string device_path = static_cast<std::string>(
|
||||||
|
Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::DBusObjectPathString>>(
|
||||||
|
parameters.get_child(0)).get());
|
||||||
|
|
||||||
|
std::string device_name = device_path;
|
||||||
|
if (m_devices.count(device_path)) {
|
||||||
|
device_name = m_devices[device_path].name;
|
||||||
|
if (device_name.empty())
|
||||||
|
device_name = m_devices[device_path].address;
|
||||||
|
}
|
||||||
|
|
||||||
|
NotifyMessage msg;
|
||||||
|
msg.summary = "Bluetooth Authorization Request";
|
||||||
|
msg.body = "Device '" + device_name + "' requests authorization to connect.";
|
||||||
|
msg.app_name = "Bar";
|
||||||
|
msg.actions = {"Allow", "Allow", "Deny", "Deny"};
|
||||||
|
msg.urgency = NotificationUrgency::LOW;
|
||||||
|
msg.expire_timeout = -1;
|
||||||
|
|
||||||
|
msg.on_action = [invocation](const std::string &action) {
|
||||||
|
if (action == "Allow") {
|
||||||
|
invocation->return_value(Glib::VariantContainerBase());
|
||||||
|
} else {
|
||||||
|
invocation->return_dbus_error("org.bluez.Error.Rejected", "Rejected by user");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
NotificationController::getInstance()->showNotificationOnAllMonitors(msg);
|
||||||
|
|
||||||
|
} else if (method_name == "AuthorizeService") {
|
||||||
|
// Signature: (os)
|
||||||
|
std::string device_path = static_cast<std::string>(
|
||||||
|
Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::DBusObjectPathString>>(
|
||||||
|
parameters.get_child(0)).get());
|
||||||
|
std::string uuid = static_cast<std::string>(
|
||||||
|
Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(
|
||||||
|
parameters.get_child(1)).get());
|
||||||
|
|
||||||
|
std::string device_name = device_path;
|
||||||
|
if (m_devices.count(device_path)) {
|
||||||
|
device_name = m_devices[device_path].name;
|
||||||
|
if (device_name.empty())
|
||||||
|
device_name = m_devices[device_path].address;
|
||||||
|
}
|
||||||
|
|
||||||
|
NotifyMessage msg;
|
||||||
|
msg.summary = "Bluetooth Service Authorization";
|
||||||
|
msg.body = "Device '" + device_name + "' wants to access service: " + uuid;
|
||||||
|
msg.app_name = "Bar";
|
||||||
|
msg.actions = {"Allow", "Allow", "Deny", "Deny"};
|
||||||
|
msg.urgency = NotificationUrgency::CRITICAL;
|
||||||
|
msg.expire_timeout = -1;
|
||||||
|
|
||||||
|
msg.on_action = [invocation](const std::string &action) {
|
||||||
|
if (action == "Allow") {
|
||||||
|
invocation->return_value(Glib::VariantContainerBase());
|
||||||
|
} else {
|
||||||
|
invocation->return_dbus_error("org.bluez.Error.Rejected", "Rejected by user");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
NotificationController::getInstance()->showNotificationOnAllMonitors(msg);
|
||||||
|
|
||||||
|
} else if (method_name == "DisplayPinCode") {
|
||||||
|
// Signature: (os)
|
||||||
|
std::string device_path = static_cast<std::string>(
|
||||||
|
Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::DBusObjectPathString>>(
|
||||||
|
parameters.get_child(0)).get());
|
||||||
|
std::string pincode = static_cast<std::string>(
|
||||||
|
Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(
|
||||||
|
parameters.get_child(1)).get());
|
||||||
|
|
||||||
|
std::string device_name = device_path;
|
||||||
|
if (m_devices.count(device_path)) {
|
||||||
|
device_name = m_devices[device_path].name;
|
||||||
|
if (device_name.empty())
|
||||||
|
device_name = m_devices[device_path].address;
|
||||||
|
}
|
||||||
|
|
||||||
|
NotifyMessage msg;
|
||||||
|
msg.summary = "Bluetooth Pin Code";
|
||||||
|
msg.body = "Pin code for device '" + device_name + "': " + pincode;
|
||||||
|
msg.app_name = "Bar";
|
||||||
|
msg.actions = {"Dismiss"};
|
||||||
|
msg.urgency = NotificationUrgency::NORMAL;
|
||||||
|
msg.expire_timeout = 0;
|
||||||
|
|
||||||
|
NotificationController::getInstance()->showNotificationOnAllMonitors(msg);
|
||||||
|
invocation->return_value(Glib::VariantContainerBase());
|
||||||
|
|
||||||
|
} else if (method_name == "DisplayPasskey") {
|
||||||
|
// Signature: (ouq)
|
||||||
|
std::string device_path = static_cast<std::string>(
|
||||||
|
Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::DBusObjectPathString>>(
|
||||||
|
parameters.get_child(0)).get());
|
||||||
|
guint32 passkey = Glib::VariantBase::cast_dynamic<Glib::Variant<guint32>>(
|
||||||
|
parameters.get_child(1)).get();
|
||||||
|
guint16 entered = Glib::VariantBase::cast_dynamic<Glib::Variant<guint16>>(
|
||||||
|
parameters.get_child(2)).get();
|
||||||
|
|
||||||
|
// Only show on first display (entered == 0) to avoid spam
|
||||||
|
if (entered == 0) {
|
||||||
|
std::string device_name = device_path;
|
||||||
|
if (m_devices.count(device_path)) {
|
||||||
|
device_name = m_devices[device_path].name;
|
||||||
|
if (device_name.empty())
|
||||||
|
device_name = m_devices[device_path].address;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ostringstream ss;
|
||||||
|
ss << std::setw(6) << std::setfill('0') << passkey;
|
||||||
|
std::string passkey_str = ss.str();
|
||||||
|
|
||||||
|
NotifyMessage msg;
|
||||||
|
msg.summary = "Bluetooth Passkey";
|
||||||
|
msg.body = "Type this passkey on '" + device_name + "': " + passkey_str;
|
||||||
|
msg.app_name = "Bar";
|
||||||
|
msg.actions = {"Dismiss"};
|
||||||
|
msg.urgency = NotificationUrgency::CRITICAL;
|
||||||
|
msg.expire_timeout = -1;
|
||||||
|
|
||||||
|
NotificationController::getInstance()->showNotificationOnAllMonitors(msg);
|
||||||
|
}
|
||||||
|
invocation->return_value(Glib::VariantContainerBase());
|
||||||
|
|
||||||
|
} else if (method_name == "RequestPinCode") {
|
||||||
|
// Signature: (o) -> s
|
||||||
|
std::string device_path = static_cast<std::string>(
|
||||||
|
Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::DBusObjectPathString>>(
|
||||||
|
parameters.get_child(0)).get());
|
||||||
|
|
||||||
|
std::string device_name = device_path;
|
||||||
|
if (m_devices.count(device_path)) {
|
||||||
|
device_name = m_devices[device_path].name;
|
||||||
|
if (device_name.empty())
|
||||||
|
device_name = m_devices[device_path].address;
|
||||||
|
}
|
||||||
|
|
||||||
|
NotifyMessage msg;
|
||||||
|
msg.summary = "Bluetooth PIN Request";
|
||||||
|
msg.body = "Enter PIN Code for '" + device_name + "'";
|
||||||
|
msg.app_name = "Bar";
|
||||||
|
msg.urgency = NotificationUrgency::CRITICAL;
|
||||||
|
msg.expire_timeout = -1;
|
||||||
|
msg.has_input = true;
|
||||||
|
msg.input_placeholder = "PIN Code";
|
||||||
|
|
||||||
|
msg.on_input = [invocation](const std::string &input) {
|
||||||
|
invocation->return_value(
|
||||||
|
Glib::VariantContainerBase::create_tuple(
|
||||||
|
{Glib::Variant<Glib::ustring>::create(input)}));
|
||||||
|
};
|
||||||
|
|
||||||
|
NotificationController::getInstance()->showNotificationOnAllMonitors(msg);
|
||||||
|
|
||||||
|
} else if (method_name == "RequestPasskey") {
|
||||||
|
// Signature: (o) -> u
|
||||||
|
std::string device_path = static_cast<std::string>(
|
||||||
|
Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::DBusObjectPathString>>(
|
||||||
|
parameters.get_child(0)).get());
|
||||||
|
|
||||||
|
std::string device_name = device_path;
|
||||||
|
if (m_devices.count(device_path)) {
|
||||||
|
device_name = m_devices[device_path].name;
|
||||||
|
if (device_name.empty())
|
||||||
|
device_name = m_devices[device_path].address;
|
||||||
|
}
|
||||||
|
|
||||||
|
NotifyMessage msg;
|
||||||
|
msg.summary = "Bluetooth Passkey Request";
|
||||||
|
msg.body = "Enter Passkey for '" + device_name + "'";
|
||||||
|
msg.app_name = "Bar";
|
||||||
|
msg.urgency = NotificationUrgency::CRITICAL;
|
||||||
|
msg.expire_timeout = -1;
|
||||||
|
msg.has_input = true;
|
||||||
|
msg.input_placeholder = "Passkey (0-999999)";
|
||||||
|
|
||||||
|
msg.on_input = [invocation](const std::string &input) {
|
||||||
|
try {
|
||||||
|
uint32_t passkey = std::stoul(input);
|
||||||
|
invocation->return_value(
|
||||||
|
Glib::VariantContainerBase::create_tuple(
|
||||||
|
{Glib::Variant<guint32>::create(passkey)}));
|
||||||
|
} catch (...) {
|
||||||
|
invocation->return_dbus_error("org.bluez.Error.Failed", "Invalid passkey format");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
NotificationController::getInstance()->showNotificationOnAllMonitors(msg);
|
||||||
|
|
||||||
|
} else if (method_name == "Cancel" || method_name == "Release") {
|
||||||
|
// Just return
|
||||||
|
invocation->return_value(Glib::VariantContainerBase());
|
||||||
|
} else {
|
||||||
|
// Not implemented
|
||||||
|
invocation->return_dbus_error("org.bluez.Error.Rejected", "Not implemented");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -10,7 +10,10 @@
|
|||||||
#include "giomm/dbusproxy.h"
|
#include "giomm/dbusproxy.h"
|
||||||
|
|
||||||
std::shared_ptr<MprisController> MprisController::getInstance() {
|
std::shared_ptr<MprisController> MprisController::getInstance() {
|
||||||
static std::shared_ptr<MprisController> instance = std::shared_ptr<MprisController>(new MprisController());
|
static std::shared_ptr<MprisController> instance;
|
||||||
|
if (!instance) {
|
||||||
|
instance = std::shared_ptr<MprisController>(new MprisController());
|
||||||
|
}
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -18,7 +21,7 @@ std::shared_ptr<MprisController> MprisController::createForPlayer(const std::str
|
|||||||
return std::shared_ptr<MprisController>(new MprisController(bus_name));
|
return std::shared_ptr<MprisController>(new MprisController(bus_name));
|
||||||
}
|
}
|
||||||
|
|
||||||
MprisController::MprisController() {
|
MprisController::MprisController() : m_player_bus_name() {
|
||||||
connect_session_async(sigc::mem_fun(*this, &MprisController::on_bus_connected));
|
connect_session_async(sigc::mem_fun(*this, &MprisController::on_bus_connected));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,6 +66,33 @@ void MprisController::on_bus_connected(const Glib::RefPtr<Gio::AsyncResult> &res
|
|||||||
try {
|
try {
|
||||||
connection = Gio::DBus::Connection::get_finish(result);
|
connection = Gio::DBus::Connection::get_finish(result);
|
||||||
|
|
||||||
|
// Per-player instances only need to create their own player proxy,
|
||||||
|
// not monitor all players on the bus.
|
||||||
|
if (!m_player_bus_name.empty()) {
|
||||||
|
if (!m_proxy) {
|
||||||
|
try {
|
||||||
|
m_proxy = Gio::DBus::Proxy::create_sync(
|
||||||
|
connection,
|
||||||
|
m_player_bus_name,
|
||||||
|
"/org/mpris/MediaPlayer2",
|
||||||
|
"org.mpris.MediaPlayer2.Player");
|
||||||
|
|
||||||
|
if (m_proxy) {
|
||||||
|
m_proxy->signal_properties_changed().connect(
|
||||||
|
sigc::mem_fun(*this, &MprisController::on_properties_changed));
|
||||||
|
signalNotification();
|
||||||
|
emit_cached_playback_status();
|
||||||
|
emit_cached_position();
|
||||||
|
emit_cached_can_seek();
|
||||||
|
}
|
||||||
|
} catch (const Glib::Error &ex) {
|
||||||
|
spdlog::error("DBus Connection Error for player {}: {}", m_player_bus_name, ex.what());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Singleton instance: monitor all MPRIS players on the bus.
|
||||||
if (!m_dbus_proxy) {
|
if (!m_dbus_proxy) {
|
||||||
m_dbus_proxy = Gio::DBus::Proxy::create_sync(
|
m_dbus_proxy = Gio::DBus::Proxy::create_sync(
|
||||||
connection,
|
connection,
|
||||||
@@ -77,7 +107,7 @@ void MprisController::on_bus_connected(const Glib::RefPtr<Gio::AsyncResult> &res
|
|||||||
try {
|
try {
|
||||||
auto list_names_result = m_dbus_proxy->call_sync("ListNames");
|
auto list_names_result = m_dbus_proxy->call_sync("ListNames");
|
||||||
auto names_variant = Glib::VariantBase::cast_dynamic<
|
auto names_variant = Glib::VariantBase::cast_dynamic<
|
||||||
Glib::Variant<std::vector<Glib::ustring>>>(list_names_result.get_child(0));
|
Glib::Variant<std::vector<Glib::ustring>>>(list_names_result.get_child(0));
|
||||||
|
|
||||||
for (const auto &name : names_variant.get()) {
|
for (const auto &name : names_variant.get()) {
|
||||||
const std::string bus_name = name;
|
const std::string bus_name = name;
|
||||||
@@ -319,9 +349,11 @@ void MprisController::on_properties_changed(const Gio::DBus::Proxy::MapChangedPr
|
|||||||
|
|
||||||
if (changed_properties.find("PlaybackStatus") != changed_properties.end()) {
|
if (changed_properties.find("PlaybackStatus") != changed_properties.end()) {
|
||||||
auto status_var = changed_properties.at("PlaybackStatus");
|
auto status_var = changed_properties.at("PlaybackStatus");
|
||||||
|
|
||||||
if (status_var.is_of_type(Glib::VariantType("s"))) {
|
if (status_var.is_of_type(Glib::VariantType("s"))) {
|
||||||
auto status = Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(status_var).get();
|
auto status = Glib::VariantBase::cast_dynamic<Glib::Variant<Glib::ustring>>(status_var).get();
|
||||||
spdlog::info("Playback Status changed to: {}", status.raw());
|
spdlog::debug("Playback Status changed to: {}", status.raw());
|
||||||
|
|
||||||
auto parsedStatusIt = playbackStatusMap.find(static_cast<std::string>(status));
|
auto parsedStatusIt = playbackStatusMap.find(static_cast<std::string>(status));
|
||||||
|
|
||||||
if (parsedStatusIt != playbackStatusMap.end()) {
|
if (parsedStatusIt != playbackStatusMap.end()) {
|
||||||
@@ -337,7 +369,7 @@ void MprisController::on_properties_changed(const Gio::DBus::Proxy::MapChangedPr
|
|||||||
auto position_var = changed_properties.at("Position");
|
auto position_var = changed_properties.at("Position");
|
||||||
if (position_var.is_of_type(Glib::VariantType("x"))) {
|
if (position_var.is_of_type(Glib::VariantType("x"))) {
|
||||||
auto position = Glib::VariantBase::cast_dynamic<Glib::Variant<gint64>>(position_var).get();
|
auto position = Glib::VariantBase::cast_dynamic<Glib::Variant<gint64>>(position_var).get();
|
||||||
spdlog::info("Position changed to: {}", position);
|
spdlog::debug("Position changed to: {}", position);
|
||||||
playbackPositionChangedSignal.emit(static_cast<int64_t>(position));
|
playbackPositionChangedSignal.emit(static_cast<int64_t>(position));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,519 @@
|
|||||||
|
#include "services/hidppBattery.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <array>
|
||||||
|
#include <cerrno>
|
||||||
|
#include <chrono>
|
||||||
|
#include <cstring>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <fstream>
|
||||||
|
#include <poll.h>
|
||||||
|
#include <spdlog/spdlog.h>
|
||||||
|
#include <string_view>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace hidpp {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
// HID++ transport: a short report carries 3 payload bytes, a long one 16.
|
||||||
|
constexpr uint8_t kReportShort = 0x10;
|
||||||
|
constexpr uint8_t kReportLong = 0x11;
|
||||||
|
constexpr size_t kShortLen = 7;
|
||||||
|
constexpr size_t kLongLen = 20;
|
||||||
|
constexpr size_t kHeaderLen = 4;
|
||||||
|
|
||||||
|
// Tags our requests so replies meant for other clients (Solaar, libratbag)
|
||||||
|
// can be told apart from ours on the shared receiver node.
|
||||||
|
constexpr uint8_t kSwId = 0x0A;
|
||||||
|
|
||||||
|
constexpr uint8_t kRootFeature = 0x00;
|
||||||
|
constexpr uint8_t kRootGetFeature = 0x00;
|
||||||
|
constexpr uint8_t kRootGetProtocolVersion = 0x01;
|
||||||
|
|
||||||
|
constexpr uint16_t kFeatureDeviceName = 0x0005;
|
||||||
|
constexpr uint16_t kFeatureBatteryStatus = 0x1000;
|
||||||
|
constexpr uint16_t kFeatureUnifiedBattery = 0x1004;
|
||||||
|
|
||||||
|
// UNIFIED_BATTERY function 0x00 is get_capabilities; the reading lives behind
|
||||||
|
// 0x01. BATTERY_STATUS puts its reading behind 0x00 instead.
|
||||||
|
constexpr uint8_t kUnifiedBatteryGetStatus = 0x01;
|
||||||
|
constexpr uint8_t kBatteryStatusGetLevel = 0x00;
|
||||||
|
|
||||||
|
// Feature index 0x8F marks a HID++ 1.0 error, 0xFF a HID++ 2.0 one.
|
||||||
|
constexpr uint8_t kErrorHidpp10 = 0x8F;
|
||||||
|
constexpr uint8_t kErrorHidpp20 = 0xFF;
|
||||||
|
|
||||||
|
// 0xFF is the device itself on a node created by hid-logitech-dj; 1..6 are the
|
||||||
|
// pairing slots when we are talking to a receiver directly.
|
||||||
|
constexpr std::array<uint8_t, 7> kProbeIndices = {0xFF, 1, 2, 3, 4, 5, 6};
|
||||||
|
|
||||||
|
constexpr uint32_t kLogitechVendorId = 0x046D;
|
||||||
|
|
||||||
|
// A sleeping device takes seconds to answer the request that wakes it, but
|
||||||
|
// replies in milliseconds once awake. Scanning therefore costs one long timeout
|
||||||
|
// per empty pairing slot, which is why the resolved endpoint is cached.
|
||||||
|
constexpr int kWakeTimeoutMs = 2500;
|
||||||
|
constexpr int kTimeoutMs = 3000;
|
||||||
|
constexpr int kProbeTimeoutMs = 600;
|
||||||
|
|
||||||
|
std::string readSysfsString(const std::filesystem::path &path) {
|
||||||
|
std::ifstream file(path);
|
||||||
|
if (!file.is_open()) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string value;
|
||||||
|
std::getline(file, value);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Drops notifications the receiver queued up (mouse movement, connection
|
||||||
|
/// events) so they cannot be mistaken for the reply we are about to wait for.
|
||||||
|
void drain(int fd) {
|
||||||
|
std::array<uint8_t, 64> scratch{};
|
||||||
|
while (::read(fd, scratch.data(), scratch.size()) > 0) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sends one HID++ request and waits for the reply carrying our software id.
|
||||||
|
/// Returns the payload after the 4-byte header, or nullopt on error/timeout.
|
||||||
|
std::optional<std::vector<uint8_t>> request(int fd, uint8_t deviceIndex, uint8_t featureIdx,
|
||||||
|
uint8_t function, const std::vector<uint8_t> ¶ms = {},
|
||||||
|
uint8_t reportId = kReportShort, int timeoutMs = kTimeoutMs) {
|
||||||
|
const size_t reportLen = (reportId == kReportLong) ? kLongLen : kShortLen;
|
||||||
|
if (params.size() > reportLen - kHeaderLen) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
const uint8_t tag = static_cast<uint8_t>((function << 4) | kSwId);
|
||||||
|
|
||||||
|
std::vector<uint8_t> out(reportLen, 0);
|
||||||
|
out[0] = reportId;
|
||||||
|
out[1] = deviceIndex;
|
||||||
|
out[2] = featureIdx;
|
||||||
|
out[3] = tag;
|
||||||
|
std::copy(params.begin(), params.end(), out.begin() + kHeaderLen);
|
||||||
|
|
||||||
|
drain(fd);
|
||||||
|
|
||||||
|
// Transient EPIPE happens when the receiver is busy serving another HID++
|
||||||
|
// client (logiod, Solaar); the next poll retries.
|
||||||
|
if (::write(fd, out.data(), out.size()) < 0) {
|
||||||
|
spdlog::debug("[hidpp] write failed: {}", std::strerror(errno));
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeoutMs);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
const auto now = std::chrono::steady_clock::now();
|
||||||
|
if (now >= deadline) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto remaining =
|
||||||
|
std::chrono::duration_cast<std::chrono::milliseconds>(deadline - now).count();
|
||||||
|
|
||||||
|
pollfd pfd{.fd = fd, .events = POLLIN, .revents = 0};
|
||||||
|
const int ready = ::poll(&pfd, 1, static_cast<int>(remaining));
|
||||||
|
if (ready <= 0) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::array<uint8_t, 64> in{};
|
||||||
|
const ssize_t n = ::read(fd, in.data(), in.size());
|
||||||
|
if (n < static_cast<ssize_t>(kHeaderLen)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Someone else's device, or an unsolicited notification.
|
||||||
|
if (in[1] != deviceIndex) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((in[2] == kErrorHidpp20 || in[2] == kErrorHidpp10) && in[3] == tag) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (in[2] == featureIdx && in[3] == tag) {
|
||||||
|
return std::vector<uint8_t>(in.begin() + kHeaderLen, in.begin() + n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Resolves a feature id to the index this particular device assigned it.
|
||||||
|
/// Index 0 means the device does not implement the feature.
|
||||||
|
std::optional<uint8_t> getFeatureIndex(int fd, uint8_t deviceIndex, uint16_t featureId, int timeoutMs) {
|
||||||
|
const std::vector<uint8_t> params = {static_cast<uint8_t>(featureId >> 8),
|
||||||
|
static_cast<uint8_t>(featureId & 0xFF), 0x00};
|
||||||
|
|
||||||
|
const auto reply =
|
||||||
|
request(fd, deviceIndex, kRootFeature, kRootGetFeature, params, kReportShort, timeoutMs);
|
||||||
|
if (!reply || reply->empty() || (*reply)[0] == 0) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (*reply)[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string getDeviceName(int fd, uint8_t deviceIndex) {
|
||||||
|
const auto nameFeature = getFeatureIndex(fd, deviceIndex, kFeatureDeviceName, kProbeTimeoutMs);
|
||||||
|
if (!nameFeature) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto lengthReply = request(fd, deviceIndex, *nameFeature, 0x00, {}, kReportShort, kProbeTimeoutMs);
|
||||||
|
if (!lengthReply || lengthReply->empty()) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
const size_t length = (*lengthReply)[0];
|
||||||
|
std::string name;
|
||||||
|
|
||||||
|
while (name.size() < length) {
|
||||||
|
const auto chunk = request(fd, deviceIndex, *nameFeature, 0x01,
|
||||||
|
{static_cast<uint8_t>(name.size())}, kReportLong, kProbeTimeoutMs);
|
||||||
|
if (!chunk || chunk->empty()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
name.append(reinterpret_cast<const char *>(chunk->data()), chunk->size());
|
||||||
|
}
|
||||||
|
|
||||||
|
name.resize(std::min(name.size(), length));
|
||||||
|
|
||||||
|
// The device pads the last chunk with NULs.
|
||||||
|
name.erase(std::find(name.begin(), name.end(), '\0'), name.end());
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
BatteryStatus::Level levelFromMask(uint8_t mask) {
|
||||||
|
if (mask & 0x01)
|
||||||
|
return BatteryStatus::Level::CRITICAL;
|
||||||
|
if (mask & 0x02)
|
||||||
|
return BatteryStatus::Level::LOW;
|
||||||
|
if (mask & 0x04)
|
||||||
|
return BatteryStatus::Level::GOOD;
|
||||||
|
if (mask & 0x08)
|
||||||
|
return BatteryStatus::Level::FULL;
|
||||||
|
|
||||||
|
return BatteryStatus::Level::UNKNOWN;
|
||||||
|
}
|
||||||
|
|
||||||
|
BatteryStatus::Level levelFromPercentage(int percentage) {
|
||||||
|
if (percentage < 0)
|
||||||
|
return BatteryStatus::Level::UNKNOWN;
|
||||||
|
if (percentage <= 10)
|
||||||
|
return BatteryStatus::Level::CRITICAL;
|
||||||
|
if (percentage <= 25)
|
||||||
|
return BatteryStatus::Level::LOW;
|
||||||
|
if (percentage < 95)
|
||||||
|
return BatteryStatus::Level::GOOD;
|
||||||
|
|
||||||
|
return BatteryStatus::Level::FULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// True when the hidraw node sits under a Logitech USB device.
|
||||||
|
bool isLogitechNode(const std::string &nodeName) {
|
||||||
|
std::ifstream file(std::filesystem::path("/sys/class/hidraw") / nodeName / "device/uevent");
|
||||||
|
if (!file.is_open()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Looking for HID_ID=<bus>:<vendor>:<product>, all zero-padded hex. It is
|
||||||
|
// not the first line of uevent, so scan for it.
|
||||||
|
std::string line;
|
||||||
|
while (std::getline(file, line)) {
|
||||||
|
constexpr std::string_view kPrefix = "HID_ID=";
|
||||||
|
if (!line.starts_with(kPrefix)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto busEnd = line.find(':', kPrefix.size());
|
||||||
|
if (busEnd == std::string::npos) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return std::stoul(line.substr(busEnd + 1, 8), nullptr, 16) == kLogitechVendorId;
|
||||||
|
} catch (const std::exception &) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// True when the node's report descriptor declares the HID++ collection, i.e.
|
||||||
|
/// report 0x10 or 0x11 under the vendor-defined usage page 0xFF00.
|
||||||
|
///
|
||||||
|
/// A receiver exposes several interfaces and only one of them speaks HID++;
|
||||||
|
/// checking the descriptor avoids firing writes at the plain keyboard and mouse
|
||||||
|
/// interfaces, which reject them and can leave the endpoint briefly stalled.
|
||||||
|
bool nodeSpeaksHidpp(const std::string &nodeName) {
|
||||||
|
std::ifstream file(std::filesystem::path("/sys/class/hidraw") / nodeName / "device/report_descriptor",
|
||||||
|
std::ios::binary);
|
||||||
|
if (!file.is_open()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::vector<uint8_t> descriptor((std::istreambuf_iterator<char>(file)),
|
||||||
|
std::istreambuf_iterator<char>());
|
||||||
|
|
||||||
|
constexpr uint8_t kItemLong = 0xFE;
|
||||||
|
constexpr uint8_t kTypeGlobal = 1;
|
||||||
|
constexpr uint8_t kTagUsagePage = 0x0;
|
||||||
|
constexpr uint8_t kTagReportId = 0x8;
|
||||||
|
constexpr uint32_t kVendorPage = 0xFF00;
|
||||||
|
|
||||||
|
uint32_t usagePage = 0;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < descriptor.size();) {
|
||||||
|
const uint8_t prefix = descriptor[i];
|
||||||
|
|
||||||
|
if (prefix == kItemLong) {
|
||||||
|
if (i + 1 >= descriptor.size()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
i += 3 + descriptor[i + 1];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t size = prefix & 0x03;
|
||||||
|
if (size == 3) {
|
||||||
|
size = 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
const uint8_t type = (prefix >> 2) & 0x03;
|
||||||
|
const uint8_t tag = (prefix >> 4) & 0x0F;
|
||||||
|
|
||||||
|
if (i + 1 + size > descriptor.size()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t value = 0;
|
||||||
|
for (size_t b = 0; b < size; b++) {
|
||||||
|
value |= static_cast<uint32_t>(descriptor[i + 1 + b]) << (8 * b);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type == kTypeGlobal && tag == kTagUsagePage) {
|
||||||
|
usagePage = value;
|
||||||
|
} else if (type == kTypeGlobal && tag == kTagReportId && usagePage == kVendorPage &&
|
||||||
|
(value == kReportShort || value == kReportLong)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
i += 1 + size;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
BatteryReader::BatteryReader(std::string deviceName) : deviceName(std::move(deviceName)) {}
|
||||||
|
|
||||||
|
BatteryReader::~BatteryReader() {
|
||||||
|
closeEndpoint();
|
||||||
|
}
|
||||||
|
|
||||||
|
void BatteryReader::closeEndpoint() {
|
||||||
|
if (endpoint.fd >= 0) {
|
||||||
|
::close(endpoint.fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
endpoint = Endpoint{};
|
||||||
|
}
|
||||||
|
|
||||||
|
BatteryStatus BatteryReader::read() {
|
||||||
|
// The kernel already polls the device for us when hid-logitech-hidpp is
|
||||||
|
// bound, which costs no HID traffic and never blocks.
|
||||||
|
if (const auto fromSysfs = readFromSysfs()) {
|
||||||
|
return *fromSysfs;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (const auto fromHidpp = readFromHidpp()) {
|
||||||
|
return *fromHidpp;
|
||||||
|
}
|
||||||
|
|
||||||
|
return BatteryStatus{};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<BatteryStatus> BatteryReader::readFromSysfs() const {
|
||||||
|
const std::filesystem::path root = "/sys/class/power_supply";
|
||||||
|
|
||||||
|
std::error_code ec;
|
||||||
|
if (!std::filesystem::exists(root, ec)) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const auto &entry : std::filesystem::directory_iterator(root, ec)) {
|
||||||
|
if (readSysfsString(entry.path() / "model_name") != deviceName) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
BatteryStatus status{};
|
||||||
|
status.connected = true;
|
||||||
|
|
||||||
|
const std::string capacity = readSysfsString(entry.path() / "capacity");
|
||||||
|
if (!capacity.empty()) {
|
||||||
|
try {
|
||||||
|
status.percentage = std::stoi(capacity);
|
||||||
|
} catch (const std::exception &) {
|
||||||
|
status.percentage = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string state = readSysfsString(entry.path() / "status");
|
||||||
|
status.charging = (state == "Charging");
|
||||||
|
status.externalPower = status.charging || state == "Full";
|
||||||
|
|
||||||
|
if (status.percentage >= 0) {
|
||||||
|
status.level = levelFromPercentage(status.percentage);
|
||||||
|
} else {
|
||||||
|
const std::string capacityLevel = readSysfsString(entry.path() / "capacity_level");
|
||||||
|
if (capacityLevel == "Critical")
|
||||||
|
status.level = BatteryStatus::Level::CRITICAL;
|
||||||
|
else if (capacityLevel == "Low")
|
||||||
|
status.level = BatteryStatus::Level::LOW;
|
||||||
|
else if (capacityLevel == "Normal" || capacityLevel == "High")
|
||||||
|
status.level = BatteryStatus::Level::GOOD;
|
||||||
|
else if (capacityLevel == "Full")
|
||||||
|
status.level = BatteryStatus::Level::FULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool BatteryReader::resolveEndpoint() {
|
||||||
|
closeEndpoint();
|
||||||
|
|
||||||
|
const std::filesystem::path root = "/sys/class/hidraw";
|
||||||
|
|
||||||
|
std::error_code ec;
|
||||||
|
if (!std::filesystem::exists(root, ec)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const auto &entry : std::filesystem::directory_iterator(root, ec)) {
|
||||||
|
const std::string nodeName = entry.path().filename().string();
|
||||||
|
if (!isLogitechNode(nodeName) || !nodeSpeaksHidpp(nodeName)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string devicePath = "/dev/" + nodeName;
|
||||||
|
|
||||||
|
const int fd = ::open(devicePath.c_str(), O_RDWR | O_NONBLOCK | O_CLOEXEC);
|
||||||
|
if (fd < 0) {
|
||||||
|
spdlog::debug("[hidpp] cannot open {}: {}", devicePath, std::strerror(errno));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const uint8_t deviceIndex : kProbeIndices) {
|
||||||
|
// Only an occupied pairing slot answers the root ping, and a
|
||||||
|
// sleeping device needs the long timeout to answer it at all.
|
||||||
|
const auto pong = request(fd, deviceIndex, kRootFeature, kRootGetProtocolVersion,
|
||||||
|
{0x00, 0x00, 0xAA}, kReportShort, kWakeTimeoutMs);
|
||||||
|
if (!pong) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (getDeviceName(fd, deviceIndex) != deviceName) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const uint16_t featureId : {kFeatureUnifiedBattery, kFeatureBatteryStatus}) {
|
||||||
|
const auto featureIdx = getFeatureIndex(fd, deviceIndex, featureId, kProbeTimeoutMs);
|
||||||
|
if (!featureIdx) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
endpoint = Endpoint{
|
||||||
|
.fd = fd,
|
||||||
|
.deviceIndex = deviceIndex,
|
||||||
|
.featureIdx = *featureIdx,
|
||||||
|
.featureId = featureId,
|
||||||
|
};
|
||||||
|
|
||||||
|
spdlog::info("[hidpp] '{}' on {} slot {} via feature 0x{:04x}", deviceName,
|
||||||
|
devicePath, deviceIndex, featureId);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
::close(fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
spdlog::debug("[hidpp] '{}' not found on any hidraw node", deviceName);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<BatteryStatus> BatteryReader::readFromHidpp() {
|
||||||
|
if (endpoint.fd < 0 && !resolveEndpoint()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto readFunction = [this] {
|
||||||
|
return endpoint.featureId == kFeatureUnifiedBattery ? kUnifiedBatteryGetStatus
|
||||||
|
: kBatteryStatusGetLevel;
|
||||||
|
};
|
||||||
|
|
||||||
|
auto reply = request(endpoint.fd, endpoint.deviceIndex, endpoint.featureIdx, readFunction());
|
||||||
|
|
||||||
|
// The device may have roamed to another slot or node since we cached it.
|
||||||
|
if (!reply) {
|
||||||
|
if (!resolveEndpoint()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
reply = request(endpoint.fd, endpoint.deviceIndex, endpoint.featureIdx, readFunction());
|
||||||
|
if (!reply) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
BatteryStatus status{};
|
||||||
|
status.connected = true;
|
||||||
|
|
||||||
|
if (endpoint.featureId == kFeatureUnifiedBattery) {
|
||||||
|
// get_status -> state_of_charge, battery_level mask, charging state, external power
|
||||||
|
if (reply->size() < 4) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
status.charging = (*reply)[2] != 0;
|
||||||
|
status.externalPower = (*reply)[3] != 0;
|
||||||
|
|
||||||
|
// The coarse level mask disagrees with the percentage on this hardware
|
||||||
|
// (an MX Ergo S calls 65% "full"), so trust the percentage when the
|
||||||
|
// device reports one and fall back to the mask when it does not.
|
||||||
|
if ((*reply)[0] > 0) {
|
||||||
|
status.percentage = (*reply)[0];
|
||||||
|
status.level = levelFromPercentage(status.percentage);
|
||||||
|
} else {
|
||||||
|
status.percentage = -1;
|
||||||
|
status.level = levelFromMask((*reply)[1]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// BATTERY_STATUS -> discharge level, next level, charging state
|
||||||
|
if (reply->size() < 3) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
status.percentage = (*reply)[0];
|
||||||
|
status.level = levelFromPercentage(status.percentage);
|
||||||
|
|
||||||
|
const uint8_t chargingState = (*reply)[2];
|
||||||
|
status.charging = (chargingState == 0x01 || chargingState == 0x02);
|
||||||
|
status.externalPower = (chargingState != 0x00);
|
||||||
|
}
|
||||||
|
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace hidpp
|
||||||
+222
-66
@@ -1,8 +1,10 @@
|
|||||||
#include "services/hyprland.hpp"
|
#include "services/hyprland.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <glib-unix.h>
|
#include <glib-unix.h>
|
||||||
#include <glib.h>
|
#include <glib.h>
|
||||||
|
#include <iostream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -17,6 +19,9 @@
|
|||||||
#include "spdlog/spdlog.h"
|
#include "spdlog/spdlog.h"
|
||||||
|
|
||||||
HyprlandService::HyprlandService() {
|
HyprlandService::HyprlandService() {
|
||||||
|
if (instance) {
|
||||||
|
throw std::runtime_error("HyprlandService instance already exists");
|
||||||
|
}
|
||||||
init();
|
init();
|
||||||
this->bindHyprlandSocket();
|
this->bindHyprlandSocket();
|
||||||
}
|
}
|
||||||
@@ -62,7 +67,11 @@ void HyprlandService::init() {
|
|||||||
clientPtr->title = client["title"].get<std::string>();
|
clientPtr->title = client["title"].get<std::string>();
|
||||||
this->clients[clientPtr->address] = clientPtr;
|
this->clients[clientPtr->address] = clientPtr;
|
||||||
|
|
||||||
auto workspacePtr = workspaces[clientPtr->workspaceId];
|
auto workspaceIt = workspaces.find(clientPtr->workspaceId);
|
||||||
|
if (workspaceIt == workspaces.end()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
auto workspacePtr = workspaceIt->second;
|
||||||
workspacePtr->state->clients[clientPtr->address] = clientPtr;
|
workspacePtr->state->clients[clientPtr->address] = clientPtr;
|
||||||
|
|
||||||
if (client.contains("urgent") && client["urgent"].get<bool>()) {
|
if (client.contains("urgent") && client["urgent"].get<bool>()) {
|
||||||
@@ -73,7 +82,11 @@ void HyprlandService::init() {
|
|||||||
auto workspaceDataJson = HyprctlHelper::getWorkspaceData();
|
auto workspaceDataJson = HyprctlHelper::getWorkspaceData();
|
||||||
|
|
||||||
for (const auto &workspace : workspaceDataJson) {
|
for (const auto &workspace : workspaceDataJson) {
|
||||||
auto workspacePtr = workspaces[workspace["id"].get<int>()];
|
auto workspaceIt = workspaces.find(workspace["id"].get<int>());
|
||||||
|
if (workspaceIt == workspaces.end()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
auto workspacePtr = workspaceIt->second;
|
||||||
auto state = workspacePtr->state;
|
auto state = workspacePtr->state;
|
||||||
|
|
||||||
state->id = workspace["id"].get<int>();
|
state->id = workspace["id"].get<int>();
|
||||||
@@ -84,6 +97,9 @@ void HyprlandService::init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void HyprlandService::bindHyprlandSocket() {
|
void HyprlandService::bindHyprlandSocket() {
|
||||||
|
if (socketFd != -1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
std::string socketPath = HyprSocketHelper::getHyprlandSocketPath();
|
std::string socketPath = HyprSocketHelper::getHyprlandSocketPath();
|
||||||
|
|
||||||
socketFd = socket(AF_UNIX, SOCK_STREAM, 0);
|
socketFd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||||
@@ -92,7 +108,7 @@ void HyprlandService::bindHyprlandSocket() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct sockaddr_un addr;
|
struct sockaddr_un addr{};
|
||||||
memset(&addr, 0, sizeof(addr));
|
memset(&addr, 0, sizeof(addr));
|
||||||
addr.sun_family = AF_UNIX;
|
addr.sun_family = AF_UNIX;
|
||||||
std::strncpy(addr.sun_path, socketPath.c_str(), sizeof(addr.sun_path) - 1);
|
std::strncpy(addr.sun_path, socketPath.c_str(), sizeof(addr.sun_path) - 1);
|
||||||
@@ -106,36 +122,49 @@ void HyprlandService::bindHyprlandSocket() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto socket_conditions = static_cast<GIOCondition>(G_IO_IN | G_IO_HUP | G_IO_ERR); // NOLINT(clang-analyzer-optin.core.EnumCastOutOfRange)
|
auto socket_conditions = static_cast<GIOCondition>(G_IO_IN | G_IO_HUP | G_IO_ERR); // NOLINT(clang-analyzer-optin.core.EnumCastOutOfRange)
|
||||||
GSource *source = g_unix_fd_source_new(socketFd, socket_conditions);
|
g_unix_fd_add(
|
||||||
|
socketFd,
|
||||||
|
socket_conditions,
|
||||||
|
[](gint fd, GIOCondition condition, gpointer user_data) -> gboolean {
|
||||||
|
auto *self = static_cast<HyprlandService *>(user_data);
|
||||||
|
if (condition & (G_IO_HUP | G_IO_ERR)) {
|
||||||
|
spdlog::error("[Hyprland] Socket read error or connection closed");
|
||||||
|
close(fd);
|
||||||
|
self->socketFd = -1;
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
auto onSocketEvent = [](gint fd, GIOCondition, gpointer user_data) -> gboolean {
|
auto messages = SocketHelper::parseSocketMessage(fd, ">>");
|
||||||
HyprlandService *self = static_cast<HyprlandService *>(user_data);
|
for (const auto &message : messages) {
|
||||||
auto messages = SocketHelper::parseSocketMessage(fd, ">>");
|
self->handleSocketMessage(message);
|
||||||
|
}
|
||||||
for (const auto &message : messages) {
|
return TRUE;
|
||||||
self->handleSocketMessage(message);
|
},
|
||||||
}
|
this);
|
||||||
|
|
||||||
return G_SOURCE_CONTINUE;
|
|
||||||
};
|
|
||||||
|
|
||||||
g_source_set_callback(source, reinterpret_cast<GSourceFunc>(reinterpret_cast<void *>(+onSocketEvent)), this, nullptr);
|
|
||||||
g_source_attach(source, g_main_context_default());
|
|
||||||
g_source_unref(source);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void HyprlandService::onWorkspaceChanged(int workspaceId) {
|
void HyprlandService::onWorkspaceChanged(int workspaceId) {
|
||||||
auto newActive = workspaces[workspaceId];
|
auto newActiveIt = workspaces.find(workspaceId);
|
||||||
|
if (newActiveIt == workspaces.end()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
auto newActive = newActiveIt->second;
|
||||||
auto state = newActive->state;
|
auto state = newActive->state;
|
||||||
auto monitorPtr = monitors[state->monitorName];
|
auto monitorIt = monitors.find(state->monitorName);
|
||||||
|
if (monitorIt == monitors.end()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
auto monitorPtr = monitorIt->second;
|
||||||
|
|
||||||
int oldActiveWorkspaceId = monitorPtr->activeWorkspaceId;
|
int oldActiveWorkspaceId = monitorPtr->activeWorkspaceId;
|
||||||
auto oldActive = workspaces[oldActiveWorkspaceId];
|
|
||||||
|
|
||||||
monitorPtr->activeWorkspaceId = workspaceId;
|
monitorPtr->activeWorkspaceId = workspaceId;
|
||||||
|
|
||||||
refreshIndicator(newActive);
|
refreshIndicator(newActive);
|
||||||
refreshIndicator(oldActive);
|
|
||||||
|
auto oldActiveIt = workspaces.find(oldActiveWorkspaceId);
|
||||||
|
if (oldActiveIt != workspaces.end()) {
|
||||||
|
refreshIndicator(oldActiveIt->second);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void HyprlandService::onFocusedMonitorChanged(std::string monitorData) {
|
void HyprlandService::onFocusedMonitorChanged(std::string monitorData) {
|
||||||
@@ -160,20 +189,6 @@ void HyprlandService::onFocusedMonitorChanged(std::string monitorData) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void HyprlandService::onMonitorRemoved(std::string monitorName) {
|
|
||||||
auto monitorPtr = this->monitors[monitorName];
|
|
||||||
|
|
||||||
for (const auto &[wsId, wsPtr] : monitorPtr->monitorWorkspaces) {
|
|
||||||
this->workspaces.erase(wsId);
|
|
||||||
}
|
|
||||||
|
|
||||||
monitorPtr->monitorWorkspaces.clear();
|
|
||||||
monitorPtr->bar->close();
|
|
||||||
monitorPtr->bar = nullptr;
|
|
||||||
|
|
||||||
this->monitors.erase(monitorName);
|
|
||||||
}
|
|
||||||
|
|
||||||
void HyprlandService::onMoveWindow(std::string windowData) {
|
void HyprlandService::onMoveWindow(std::string windowData) {
|
||||||
auto parts = StringHelper::split(windowData, ',');
|
auto parts = StringHelper::split(windowData, ',');
|
||||||
std::string addr = "0x" + parts[0];
|
std::string addr = "0x" + parts[0];
|
||||||
@@ -187,36 +202,46 @@ void HyprlandService::onMoveWindow(std::string windowData) {
|
|||||||
auto clientPtr = this->clients[addr];
|
auto clientPtr = this->clients[addr];
|
||||||
int oldWorkspaceId = clientPtr->workspaceId;
|
int oldWorkspaceId = clientPtr->workspaceId;
|
||||||
|
|
||||||
auto oldWorkspacePtr = workspaces[oldWorkspaceId];
|
auto oldWorkspaceIt = workspaces.find(oldWorkspaceId);
|
||||||
oldWorkspacePtr->state->clients.erase(addr);
|
bool wasUrgent = false;
|
||||||
bool wasUrgent = oldWorkspacePtr->state->urgentClients.erase(addr) > 0;
|
if (oldWorkspaceIt != workspaces.end()) {
|
||||||
refreshIndicator(oldWorkspacePtr);
|
oldWorkspaceIt->second->state->clients.erase(addr);
|
||||||
|
wasUrgent = oldWorkspaceIt->second->state->urgentClients.erase(addr) > 0;
|
||||||
|
refreshIndicator(oldWorkspaceIt->second);
|
||||||
|
}
|
||||||
|
|
||||||
clientPtr->workspaceId = newWorkspaceId;
|
clientPtr->workspaceId = newWorkspaceId;
|
||||||
|
|
||||||
auto newWorkspacePtr = workspaces[newWorkspaceId];
|
auto newWorkspaceIt = workspaces.find(newWorkspaceId);
|
||||||
newWorkspacePtr->state->clients[addr] = clientPtr;
|
if (newWorkspaceIt != workspaces.end()) {
|
||||||
if (wasUrgent) {
|
newWorkspaceIt->second->state->clients[addr] = clientPtr;
|
||||||
newWorkspacePtr->state->urgentClients.insert(addr);
|
if (wasUrgent) {
|
||||||
|
newWorkspaceIt->second->state->urgentClients.insert(addr);
|
||||||
|
}
|
||||||
|
refreshIndicator(newWorkspaceIt->second);
|
||||||
}
|
}
|
||||||
refreshIndicator(newWorkspacePtr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void HyprlandService::onOpenWindow(std::string windowData) {
|
void HyprlandService::onOpenWindow(std::string windowData) {
|
||||||
auto parts = StringHelper::split(windowData, ',');
|
auto parts = StringHelper::split(windowData, ',');
|
||||||
std::string addr = "0x" + parts[0];
|
std::string addr = "0x" + parts[0];
|
||||||
int workspaceId = std::stoi(parts[1]);
|
int workspaceId = std::stoi(parts[1]);
|
||||||
std::string title = parts[2];
|
std::string title = "";
|
||||||
|
|
||||||
|
if (parts.size() > 2) {
|
||||||
|
std::string title = parts[2];
|
||||||
|
}
|
||||||
|
|
||||||
auto clientPtr = std::make_shared<Client>();
|
auto clientPtr = std::make_shared<Client>();
|
||||||
clientPtr->address = addr;
|
clientPtr->address = addr;
|
||||||
clientPtr->workspaceId = workspaceId;
|
clientPtr->workspaceId = workspaceId;
|
||||||
clientPtr->title = title;
|
clientPtr->title = title;
|
||||||
this->clients[clientPtr->address] = clientPtr;
|
this->clients[clientPtr->address] = clientPtr;
|
||||||
auto workspacePtr = workspaces[clientPtr->workspaceId];
|
auto workspaceIt = workspaces.find(clientPtr->workspaceId);
|
||||||
workspacePtr->state->clients[clientPtr->address] = clientPtr;
|
if (workspaceIt != workspaces.end()) {
|
||||||
|
workspaceIt->second->state->clients[clientPtr->address] = clientPtr;
|
||||||
refreshIndicator(workspacePtr);
|
refreshIndicator(workspaceIt->second);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void HyprlandService::onCloseWindow(std::string windowData) {
|
void HyprlandService::onCloseWindow(std::string windowData) {
|
||||||
@@ -229,12 +254,15 @@ void HyprlandService::onCloseWindow(std::string windowData) {
|
|||||||
|
|
||||||
auto clientPtr = this->clients[addr];
|
auto clientPtr = this->clients[addr];
|
||||||
int workspaceId = clientPtr->workspaceId;
|
int workspaceId = clientPtr->workspaceId;
|
||||||
|
|
||||||
auto workspacePtr = workspaces[workspaceId];
|
|
||||||
workspacePtr->state->clients.erase(addr);
|
|
||||||
this->clients.erase(addr);
|
this->clients.erase(addr);
|
||||||
|
|
||||||
refreshIndicator(workspacePtr);
|
auto workspaceIt = workspaces.find(workspaceId);
|
||||||
|
if (workspaceIt == workspaces.end()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
workspaceIt->second->state->clients.erase(addr);
|
||||||
|
|
||||||
|
refreshIndicator(workspaceIt->second);
|
||||||
}
|
}
|
||||||
|
|
||||||
void HyprlandService::handleSocketMessage(SocketHelper::SocketMessage message) {
|
void HyprlandService::handleSocketMessage(SocketHelper::SocketMessage message) {
|
||||||
@@ -270,6 +298,10 @@ void HyprlandService::handleSocketMessage(SocketHelper::SocketMessage message) {
|
|||||||
this->onActiveWindowChanged(eventData);
|
this->onActiveWindowChanged(eventData);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case MONITOR_ADDED: {
|
||||||
|
this->onMonitorAdded(eventData);
|
||||||
|
break;
|
||||||
|
}
|
||||||
case MONITOR_REMOVED: {
|
case MONITOR_REMOVED: {
|
||||||
this->onMonitorRemoved(eventData);
|
this->onMonitorRemoved(eventData);
|
||||||
break;
|
break;
|
||||||
@@ -280,6 +312,112 @@ void HyprlandService::handleSocketMessage(SocketHelper::SocketMessage message) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void HyprlandService::onMonitorAdded(std::string monitorName) {
|
||||||
|
spdlog::info("[Hyprland] Monitor added: {}", monitorName);
|
||||||
|
auto monitorDataJson = HyprctlHelper::getMonitorData();
|
||||||
|
auto monitorIt = std::ranges::find_if(monitorDataJson,
|
||||||
|
[&monitorName](const auto &item) {
|
||||||
|
return item.contains("name") && item["name"] == monitorName;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (monitorIt == monitorDataJson.end()) {
|
||||||
|
spdlog::warn("[Hyprland] Monitor data not found for {}", monitorName);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto monitorPtr = std::make_shared<Monitor>();
|
||||||
|
monitorPtr->id = (*monitorIt)["id"];
|
||||||
|
monitorPtr->name = monitorName;
|
||||||
|
monitorPtr->activeWorkspaceId = (*monitorIt)["activeWorkspace"]["id"];
|
||||||
|
monitorPtr->focused = (*monitorIt)["focused"];
|
||||||
|
this->monitors[monitorName] = monitorPtr;
|
||||||
|
|
||||||
|
auto onClick = sigc::mem_fun(*this, &HyprlandService::switchToWorkspace);
|
||||||
|
for (int i = 1; i <= NUM_WORKSPACES; i++) {
|
||||||
|
auto state = std::make_shared<WorkspaceData>();
|
||||||
|
int workspaceId = i + (NUM_WORKSPACES * monitorPtr->id);
|
||||||
|
|
||||||
|
state->id = workspaceId;
|
||||||
|
state->monitorName = monitorName;
|
||||||
|
auto view = std::make_shared<WorkspaceIndicator>(workspaceId, std::to_string(i), onClick);
|
||||||
|
auto workSpace = std::make_shared<Workspace>();
|
||||||
|
workSpace->state = state;
|
||||||
|
workSpace->view = view;
|
||||||
|
|
||||||
|
monitorPtr->monitorWorkspaces[workspaceId] = workSpace;
|
||||||
|
this->workspaces[workspaceId] = workSpace;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto workspaceDataJson = HyprctlHelper::getWorkspaceData();
|
||||||
|
for (const auto &workspace : workspaceDataJson) {
|
||||||
|
if (!workspace.contains("monitor") || workspace["monitor"].get<std::string>() != monitorName) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
auto workspaceIt = workspaces.find(workspace["id"].get<int>());
|
||||||
|
if (workspaceIt == workspaces.end()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
auto workspacePtr = workspaceIt->second;
|
||||||
|
auto state = workspacePtr->state;
|
||||||
|
|
||||||
|
state->id = workspace["id"].get<int>();
|
||||||
|
state->monitorName = workspace["monitor"].get<std::string>();
|
||||||
|
|
||||||
|
refreshIndicator(workspacePtr);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto clientsDataJson = HyprctlHelper::getClientData();
|
||||||
|
for (const auto &client : clientsDataJson) {
|
||||||
|
auto address = client["address"].get<std::string>();
|
||||||
|
if (this->clients.find(address) != this->clients.end()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
int workspaceId = client["workspace"]["id"].get<int>();
|
||||||
|
auto workspaceIt = workspaces.find(workspaceId);
|
||||||
|
if (workspaceIt == workspaces.end()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (workspaceIt->second->state->monitorName != monitorName) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto clientPtr = std::make_shared<Client>();
|
||||||
|
clientPtr->address = address;
|
||||||
|
clientPtr->workspaceId = workspaceId;
|
||||||
|
clientPtr->title = client["title"].get<std::string>();
|
||||||
|
this->clients[clientPtr->address] = clientPtr;
|
||||||
|
|
||||||
|
workspaceIt->second->state->clients[clientPtr->address] = clientPtr;
|
||||||
|
if (client.contains("urgent") && client["urgent"].get<bool>()) {
|
||||||
|
workspaceIt->second->state->urgentClients.insert(clientPtr->address);
|
||||||
|
}
|
||||||
|
refreshIndicator(workspaceIt->second);
|
||||||
|
}
|
||||||
|
|
||||||
|
m_signal_monitor_added.emit(monitorName);
|
||||||
|
}
|
||||||
|
|
||||||
|
void HyprlandService::onMonitorRemoved(std::string monitorName) {
|
||||||
|
spdlog::info("[Hyprland] Monitor removed: {}", monitorName);
|
||||||
|
auto monitorIt = monitors.find(monitorName);
|
||||||
|
if (monitorIt == monitors.end()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto monitorPtr = monitorIt->second;
|
||||||
|
for (const auto &[workspaceId, workspacePtr] : monitorPtr->monitorWorkspaces) {
|
||||||
|
for (const auto &[clientAddr, _] : workspacePtr->state->clients) {
|
||||||
|
clients.erase(clientAddr);
|
||||||
|
}
|
||||||
|
workspaces.erase(workspaceId);
|
||||||
|
}
|
||||||
|
|
||||||
|
this->monitors.erase(monitorIt);
|
||||||
|
|
||||||
|
m_signal_monitor_removed.emit(monitorName);
|
||||||
|
}
|
||||||
|
|
||||||
void HyprlandService::onUrgent(std::string windowAddress) {
|
void HyprlandService::onUrgent(std::string windowAddress) {
|
||||||
std::string addr = "0x" + windowAddress;
|
std::string addr = "0x" + windowAddress;
|
||||||
if (this->clients.find(addr) == this->clients.end()) {
|
if (this->clients.find(addr) == this->clients.end()) {
|
||||||
@@ -289,10 +427,13 @@ void HyprlandService::onUrgent(std::string windowAddress) {
|
|||||||
auto clientPtr = this->clients[addr];
|
auto clientPtr = this->clients[addr];
|
||||||
int workspaceId = clientPtr->workspaceId;
|
int workspaceId = clientPtr->workspaceId;
|
||||||
|
|
||||||
auto workspacePtr = workspaces[workspaceId];
|
auto workspaceIt = workspaces.find(workspaceId);
|
||||||
workspacePtr->state->urgentClients.insert(addr);
|
if (workspaceIt == workspaces.end()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
workspaceIt->second->state->urgentClients.insert(addr);
|
||||||
|
|
||||||
refreshIndicator(workspacePtr);
|
refreshIndicator(workspaceIt->second);
|
||||||
}
|
}
|
||||||
|
|
||||||
void HyprlandService::onActiveWindowChanged(std::string windowAddress) {
|
void HyprlandService::onActiveWindowChanged(std::string windowAddress) {
|
||||||
@@ -304,15 +445,22 @@ void HyprlandService::onActiveWindowChanged(std::string windowAddress) {
|
|||||||
auto clientPtr = this->clients[addr];
|
auto clientPtr = this->clients[addr];
|
||||||
int workspaceId = clientPtr->workspaceId;
|
int workspaceId = clientPtr->workspaceId;
|
||||||
|
|
||||||
auto workspacePtr = workspaces[workspaceId];
|
auto workspaceIt = workspaces.find(workspaceId);
|
||||||
workspacePtr->state->urgentClients.erase(addr);
|
if (workspaceIt == workspaces.end()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
workspaceIt->second->state->urgentClients.erase(addr);
|
||||||
|
|
||||||
refreshIndicator(workspacePtr);
|
refreshIndicator(workspaceIt->second);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<Gtk::Box> HyprlandService::getWorkspaceIndicatorsForMonitor(std::string monitorName) {
|
std::shared_ptr<Gtk::Box> HyprlandService::getWorkspaceIndicatorsForMonitor(std::string monitorName) {
|
||||||
auto box = std::make_shared<Gtk::Box>(Gtk::Orientation::HORIZONTAL);
|
auto box = std::make_shared<Gtk::Box>(Gtk::Orientation::HORIZONTAL);
|
||||||
auto monitor = monitors[monitorName];
|
auto monitorIt = monitors.find(monitorName);
|
||||||
|
if (monitorIt == monitors.end()) {
|
||||||
|
return box;
|
||||||
|
}
|
||||||
|
auto monitor = monitorIt->second;
|
||||||
|
|
||||||
for (const auto &[wsId, wsPair] : monitor->monitorWorkspaces) {
|
for (const auto &[wsId, wsPair] : monitor->monitorWorkspaces) {
|
||||||
box->append((Gtk::Box &)*wsPair->view);
|
box->append((Gtk::Box &)*wsPair->view);
|
||||||
@@ -328,8 +476,12 @@ void HyprlandService::switchToWorkspace(int workspaceId) {
|
|||||||
void HyprlandService::refreshIndicator(std::shared_ptr<Workspace> workspace) {
|
void HyprlandService::refreshIndicator(std::shared_ptr<Workspace> workspace) {
|
||||||
auto view = workspace->view;
|
auto view = workspace->view;
|
||||||
|
|
||||||
auto state = workspace->state;
|
auto state = workspace->state;
|
||||||
auto monitorsPtr = monitors[state->monitorName];
|
auto monitorIt = monitors.find(state->monitorName);
|
||||||
|
if (monitorIt == monitors.end()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
auto monitorsPtr = monitorIt->second;
|
||||||
|
|
||||||
bool isUrgent = !state->urgentClients.empty();
|
bool isUrgent = !state->urgentClients.empty();
|
||||||
bool isEmpty = state->clients.empty();
|
bool isEmpty = state->clients.empty();
|
||||||
@@ -350,5 +502,9 @@ void HyprlandService::refreshIndicator(std::shared_ptr<Workspace> workspace) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void HyprlandService::addBar(std::shared_ptr<Bar> bar, std::string monitorName) {
|
void HyprlandService::addBar(std::shared_ptr<Bar> bar, std::string monitorName) {
|
||||||
this->monitors[monitorName]->bar = bar;
|
auto monitorIt = monitors.find(monitorName);
|
||||||
|
if (monitorIt == monitors.end()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
monitorIt->second->bar = bar;
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
#include "services/mouseBatteryService.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
// The battery moves by single digits over hours; polling harder only keeps the
|
||||||
|
// device's radio awake and drains it faster.
|
||||||
|
constexpr auto kPollInterval = std::chrono::minutes(5);
|
||||||
|
|
||||||
|
// Retry sooner while the mouse is off or out of range so it shows up promptly
|
||||||
|
// once it comes back, but back off rather than rescanning every hidraw node
|
||||||
|
// forever when there is simply no such device attached.
|
||||||
|
constexpr auto kRetryInterval = std::chrono::seconds(30);
|
||||||
|
constexpr auto kMaxRetryInterval = std::chrono::minutes(10);
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
std::shared_ptr<MouseBatteryService> MouseBatteryService::getInstance() {
|
||||||
|
if (!instance) {
|
||||||
|
instance = std::shared_ptr<MouseBatteryService>(new MouseBatteryService(kDeviceName));
|
||||||
|
}
|
||||||
|
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
MouseBatteryService::MouseBatteryService(std::string deviceName) : reader(std::move(deviceName)) {
|
||||||
|
dispatcher.connect([this]() { updatedSignal.emit(); });
|
||||||
|
|
||||||
|
worker = std::thread(&MouseBatteryService::poll, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
MouseBatteryService::~MouseBatteryService() {
|
||||||
|
stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
void MouseBatteryService::stop() {
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(mutex);
|
||||||
|
if (!running) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
running = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
wakeup.notify_all();
|
||||||
|
|
||||||
|
if (worker.joinable()) {
|
||||||
|
worker.join();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
hidpp::BatteryStatus MouseBatteryService::getStatus() const {
|
||||||
|
std::lock_guard<std::mutex> lock(mutex);
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MouseBatteryService::poll() {
|
||||||
|
auto retryInterval = std::chrono::seconds(kRetryInterval);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
const hidpp::BatteryStatus latest = reader.read();
|
||||||
|
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(mutex);
|
||||||
|
status = latest;
|
||||||
|
}
|
||||||
|
|
||||||
|
dispatcher.emit();
|
||||||
|
|
||||||
|
std::chrono::seconds interval;
|
||||||
|
if (latest.connected) {
|
||||||
|
interval = std::chrono::seconds(kPollInterval);
|
||||||
|
retryInterval = std::chrono::seconds(kRetryInterval);
|
||||||
|
} else {
|
||||||
|
interval = retryInterval;
|
||||||
|
retryInterval = std::min(retryInterval * 2, std::chrono::seconds(kMaxRetryInterval));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_lock<std::mutex> lock(mutex);
|
||||||
|
wakeup.wait_for(lock, interval, [this] { return !running; });
|
||||||
|
|
||||||
|
if (!running) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,8 +12,6 @@
|
|||||||
#include "gdkmm/display.h"
|
#include "gdkmm/display.h"
|
||||||
#include "sigc++/adaptors/bind.h"
|
#include "sigc++/adaptors/bind.h"
|
||||||
|
|
||||||
std::shared_ptr<NotificationController> NotificationController::instance = nullptr;
|
|
||||||
|
|
||||||
NotificationController::NotificationController() {
|
NotificationController::NotificationController() {
|
||||||
if (NotificationController::instance) {
|
if (NotificationController::instance) {
|
||||||
throw std::runtime_error("use getInstance()!");
|
throw std::runtime_error("use getInstance()!");
|
||||||
@@ -32,8 +30,8 @@ NotificationController::NotificationController() {
|
|||||||
for (guint i = 0; i < monitors->get_n_items(); ++i) {
|
for (guint i = 0; i < monitors->get_n_items(); ++i) {
|
||||||
auto monitor = std::dynamic_pointer_cast<Gdk::Monitor>(
|
auto monitor = std::dynamic_pointer_cast<Gdk::Monitor>(
|
||||||
monitors->get_object(i));
|
monitors->get_object(i));
|
||||||
|
auto name = monitor->get_connector();
|
||||||
this->activeMonitors.push_back(monitor);
|
this->activeMonitors[name] = monitor;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,7 +39,7 @@ void NotificationController::showNotificationOnAllMonitors(NotifyMessage notify)
|
|||||||
uint64_t id = this->globalNotificationId++;
|
uint64_t id = this->globalNotificationId++;
|
||||||
std::vector<std::shared_ptr<BaseNotification>> notifications;
|
std::vector<std::shared_ptr<BaseNotification>> notifications;
|
||||||
|
|
||||||
for (const auto &monitor : this->activeMonitors) {
|
for (const auto &[name, monitor] : this->activeMonitors) {
|
||||||
auto notification = std::make_shared<NotificationWindow>(id, monitor, notify);
|
auto notification = std::make_shared<NotificationWindow>(id, monitor, notify);
|
||||||
notifications.push_back(notification);
|
notifications.push_back(notification);
|
||||||
|
|
||||||
@@ -52,11 +50,11 @@ void NotificationController::showNotificationOnAllMonitors(NotifyMessage notify)
|
|||||||
timeout = DEFAULT_NOTIFICATION_TIMEOUT;
|
timeout = DEFAULT_NOTIFICATION_TIMEOUT;
|
||||||
}
|
}
|
||||||
|
|
||||||
notification->signal_close.connect([this, id = notification->getNotificationId()](int) {
|
notification->getSignalClose().connect([this, id = notification->getNotificationId()](int) {
|
||||||
closeNotification(id);
|
closeNotification(id);
|
||||||
});
|
});
|
||||||
|
|
||||||
notification->signal_hover_changed.connect([this, id = notification->getNotificationId()](bool hovered) {
|
notification->getSignalHoverChanged().connect([this, id = notification->getNotificationId()](bool hovered) {
|
||||||
updateHoverState(id, hovered);
|
updateHoverState(id, hovered);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -74,17 +72,17 @@ void NotificationController::showCopyNotification(NotifyMessage notify) {
|
|||||||
std::vector<std::shared_ptr<BaseNotification>> notifications;
|
std::vector<std::shared_ptr<BaseNotification>> notifications;
|
||||||
uint64_t id = this->globalNotificationId++;
|
uint64_t id = this->globalNotificationId++;
|
||||||
|
|
||||||
for (const auto &monitor : this->activeMonitors) {
|
for (const auto &[name, monitor] : this->activeMonitors) {
|
||||||
auto notification = std::make_shared<CopyNotification>(id, monitor, notify);
|
auto notification = std::make_shared<CopyNotification>(id, monitor, notify);
|
||||||
|
|
||||||
notification->show();
|
notification->show();
|
||||||
notifications.push_back(notification);
|
notifications.push_back(notification);
|
||||||
|
|
||||||
notification->signal_close.connect([this, id = notification->getNotificationId()](int) {
|
notification->getSignalClose().connect([this, id = notification->getNotificationId()](int) {
|
||||||
closeNotification(id);
|
closeNotification(id);
|
||||||
});
|
});
|
||||||
|
|
||||||
notification->signal_hover_changed.connect([this, id = notification->getNotificationId()](bool hovered) {
|
notification->getSignalHoverChanged().connect([this, id = notification->getNotificationId()](bool hovered) {
|
||||||
updateHoverState(id, hovered);
|
updateHoverState(id, hovered);
|
||||||
});
|
});
|
||||||
notification->startAutoClose(DEFAULT_NOTIFICATION_TIMEOUT);
|
notification->startAutoClose(DEFAULT_NOTIFICATION_TIMEOUT);
|
||||||
@@ -97,17 +95,17 @@ void NotificationController::showSpotifyNotification(MprisPlayer2Message mpris)
|
|||||||
std::vector<std::shared_ptr<BaseNotification>> notifications;
|
std::vector<std::shared_ptr<BaseNotification>> notifications;
|
||||||
uint64_t id = this->globalNotificationId++;
|
uint64_t id = this->globalNotificationId++;
|
||||||
|
|
||||||
for (const auto &monitor : this->activeMonitors) {
|
for (const auto &[name, monitor] : this->activeMonitors) {
|
||||||
auto notification = std::make_shared<SpotifyNotification>(id, monitor, mpris);
|
auto notification = std::make_shared<SpotifyNotification>(id, monitor, mpris);
|
||||||
|
|
||||||
notification->show();
|
notification->show();
|
||||||
notifications.push_back(notification);
|
notifications.push_back(notification);
|
||||||
|
|
||||||
notification->signal_close.connect([this, id = notification->getNotificationId()](int) {
|
notification->getSignalClose().connect([this, id = notification->getNotificationId()](int) {
|
||||||
closeNotification(id);
|
closeNotification(id);
|
||||||
});
|
});
|
||||||
|
|
||||||
notification->signal_hover_changed.connect([this, id = notification->getNotificationId()](bool hovered) {
|
notification->getSignalHoverChanged().connect([this, id = notification->getNotificationId()](bool hovered) {
|
||||||
updateHoverState(id, hovered);
|
updateHoverState(id, hovered);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -153,4 +151,14 @@ void NotificationController::closeNotification(uint64_t notificationId) {
|
|||||||
|
|
||||||
this->activeNotifications.erase(notificationId);
|
this->activeNotifications.erase(notificationId);
|
||||||
this->hoverCounts.erase(notificationId);
|
this->hoverCounts.erase(notificationId);
|
||||||
|
}
|
||||||
|
|
||||||
|
void NotificationController::addMonitor(std::shared_ptr<Gdk::Monitor> monitor) {
|
||||||
|
auto name = monitor->get_connector();
|
||||||
|
this->activeMonitors[name] = monitor;
|
||||||
|
}
|
||||||
|
|
||||||
|
void NotificationController::removeMonitor(std::shared_ptr<Gdk::Monitor> monitor) {
|
||||||
|
auto name = monitor->get_connector();
|
||||||
|
this->activeMonitors.erase(name);
|
||||||
}
|
}
|
||||||
@@ -132,6 +132,15 @@ TextureCacheService *TextureCacheService::getInstance() {
|
|||||||
return &instance;
|
return &instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TextureCacheService::TextureCacheService() {
|
||||||
|
curl_global_init(CURL_GLOBAL_DEFAULT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TextureCacheService::~TextureCacheService() {
|
||||||
|
clear();
|
||||||
|
curl_global_cleanup();
|
||||||
|
}
|
||||||
|
|
||||||
Glib::RefPtr<Gdk::Texture> TextureCacheService::getTexture(const std::string &url) {
|
Glib::RefPtr<Gdk::Texture> TextureCacheService::getTexture(const std::string &url) {
|
||||||
if (url.empty()) {
|
if (url.empty()) {
|
||||||
return {};
|
return {};
|
||||||
@@ -220,3 +229,7 @@ void TextureCacheService::pruneCache() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TextureCacheService::clear() {
|
||||||
|
cache.clear();
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,145 @@
|
|||||||
|
#include "widgets/controlCenter/bluetoothSettings.hpp"
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
BluetoothSettings::BluetoothSettings() : bluetoothIsPowered(this->bluetoothController->isPowered()), bluetoothIsScanning(this->bluetoothController->isDiscovering()) {
|
||||||
|
set_orientation(Gtk::Orientation::VERTICAL);
|
||||||
|
set_spacing(12);
|
||||||
|
|
||||||
|
add_css_class("bluetooth-settings");
|
||||||
|
|
||||||
|
powerButton->add_css_class("power-button");
|
||||||
|
|
||||||
|
setBluetoothPowered(bluetoothIsPowered);
|
||||||
|
|
||||||
|
powerButton->set_tooltip_text(bluetoothIsPowered ? "Turn Bluetooth Off" : "Turn Bluetooth On");
|
||||||
|
powerButton->signal_clicked().connect([this]() {
|
||||||
|
bluetoothIsPowered = !bluetoothIsPowered;
|
||||||
|
this->bluetoothController->setPowered(bluetoothIsPowered);
|
||||||
|
});
|
||||||
|
|
||||||
|
this->buttonRow.append(*powerButton);
|
||||||
|
|
||||||
|
scanButton->add_css_class("power-button");
|
||||||
|
setScanning(bluetoothIsScanning);
|
||||||
|
scanButton->set_tooltip_text(bluetoothIsScanning ? "Stop Scanning" : "Start Scanning");
|
||||||
|
scanButton->signal_clicked().connect([this]() {
|
||||||
|
bluetoothIsScanning = !bluetoothIsScanning;
|
||||||
|
if (bluetoothIsScanning) {
|
||||||
|
this->bluetoothController->startDiscovery();
|
||||||
|
} else {
|
||||||
|
this->bluetoothController->stopDiscovery();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
this->buttonRow.append(*scanButton);
|
||||||
|
|
||||||
|
this->buttonRow.set_spacing(12);
|
||||||
|
append(this->buttonRow);
|
||||||
|
|
||||||
|
|
||||||
|
connectedDevicesBox.add_css_class("active-devices");
|
||||||
|
connectedDevicesBox.set_orientation(Gtk::Orientation::VERTICAL);
|
||||||
|
connectedDevicesScroll.set_child(connectedDevicesBox);
|
||||||
|
connectedDevicesScroll.set_policy(Gtk::PolicyType::NEVER, Gtk::PolicyType::AUTOMATIC);
|
||||||
|
connectedDevicesScroll.set_propagate_natural_height(true);
|
||||||
|
connectedDevicesScroll.set_max_content_height(200);
|
||||||
|
|
||||||
|
append(connectedDevicesScroll);
|
||||||
|
|
||||||
|
|
||||||
|
availableDevicesBox.add_css_class("available-devices");
|
||||||
|
availableDevicesBox.set_orientation(Gtk::Orientation::VERTICAL);
|
||||||
|
availableDevicesScroll.set_child(availableDevicesBox);
|
||||||
|
availableDevicesScroll.set_policy(Gtk::PolicyType::NEVER, Gtk::PolicyType::AUTOMATIC);
|
||||||
|
availableDevicesScroll.set_propagate_natural_height(true);
|
||||||
|
availableDevicesScroll.set_max_content_height(200);
|
||||||
|
append(availableDevicesScroll);
|
||||||
|
|
||||||
|
this->bluetoothController->signalPoweredChanged().connect([this](bool powered) {
|
||||||
|
bluetoothIsPowered = powered;
|
||||||
|
setBluetoothPowered(powered);
|
||||||
|
});
|
||||||
|
|
||||||
|
this->bluetoothController->signalDiscoveringChanged().connect([this](bool scanning) {
|
||||||
|
bluetoothIsScanning = scanning;
|
||||||
|
setScanning(scanning);
|
||||||
|
});
|
||||||
|
|
||||||
|
auto devices = this->bluetoothController->getDevices();
|
||||||
|
|
||||||
|
for (const auto &device : devices) {
|
||||||
|
auto row = std::make_shared<BluetoothSettingsRow>(device);
|
||||||
|
deviceRows[device.object_path] = row;
|
||||||
|
if (device.connected) {
|
||||||
|
connectedDevicesBox.append(*row);
|
||||||
|
} else {
|
||||||
|
availableDevicesBox.append(*row);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this->bluetoothController->signalDeviceAdded().connect([this](const BluetoothDevice &device) {
|
||||||
|
auto row = std::make_shared<BluetoothSettingsRow>(device);
|
||||||
|
deviceRows[device.object_path] = row;
|
||||||
|
if (device.connected) {
|
||||||
|
connectedDevicesBox.append(*row);
|
||||||
|
} else {
|
||||||
|
availableDevicesBox.append(*row);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
this->bluetoothController->signalDeviceRemoved().connect([this](const std::string &object_path) {
|
||||||
|
auto it = deviceRows.find(object_path);
|
||||||
|
if (it != deviceRows.end()) {
|
||||||
|
auto parent = it->second->get_parent();
|
||||||
|
if (parent) {
|
||||||
|
dynamic_cast<Gtk::Box *>(parent)->remove(*it->second);
|
||||||
|
}
|
||||||
|
deviceRows.erase(it);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
this->bluetoothController->signalDeviceChanged().connect([this](const BluetoothDevice &device) {
|
||||||
|
auto it = deviceRows.find(device.object_path);
|
||||||
|
if (it != deviceRows.end()) {
|
||||||
|
it->second->updateDevice(device);
|
||||||
|
|
||||||
|
// Move between boxes if connection status changed
|
||||||
|
auto parent = it->second->get_parent();
|
||||||
|
Gtk::Box *targetBox = device.connected ? &connectedDevicesBox : &availableDevicesBox;
|
||||||
|
if (parent != targetBox) {
|
||||||
|
if (parent) {
|
||||||
|
dynamic_cast<Gtk::Box *>(parent)->remove(*it->second);
|
||||||
|
}
|
||||||
|
targetBox->append(*it->second);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void BluetoothSettings::setBluetoothPowered(bool powered) {
|
||||||
|
this->bluetoothIsPowered = powered;
|
||||||
|
|
||||||
|
if (powered) {
|
||||||
|
powerButton->remove_css_class("power-button-off");
|
||||||
|
powerButton->add_css_class("power-button-on");
|
||||||
|
powerButton->set_tooltip_text("Turn Bluetooth Off");
|
||||||
|
} else {
|
||||||
|
powerButton->remove_css_class("power-button-on");
|
||||||
|
powerButton->add_css_class("power-button-off");
|
||||||
|
powerButton->set_tooltip_text("Turn Bluetooth On");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void BluetoothSettings::setScanning(bool scanning) {
|
||||||
|
this->bluetoothIsScanning = scanning;
|
||||||
|
|
||||||
|
if (scanning) {
|
||||||
|
scanButton->add_css_class("power-button-on");
|
||||||
|
scanButton->remove_css_class("power-button-off");
|
||||||
|
scanButton->set_tooltip_text("Stop Scanning");
|
||||||
|
} else {
|
||||||
|
scanButton->remove_css_class("power-button-on");
|
||||||
|
scanButton->add_css_class("power-button-off");
|
||||||
|
scanButton->set_tooltip_text("Start Scanning");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
#include "widgets/controlCenter/bluetoothSettingsRow.hpp"
|
||||||
|
|
||||||
|
#include <spdlog/spdlog.h>
|
||||||
|
|
||||||
|
#include "components/button/iconButton.hpp"
|
||||||
|
#include "connection/dbus/bluetooth.hpp"
|
||||||
|
#include "helpers/string.hpp"
|
||||||
|
#include "services/notificationController.hpp"
|
||||||
|
|
||||||
|
BluetoothSettingsRow::BluetoothSettingsRow(const BluetoothDevice &device)
|
||||||
|
: device(device),
|
||||||
|
pairButton(device.paired ? Icon::BLUETOOTH_CONNECTED : Icon::BLUETOOTH),
|
||||||
|
connectButton(device.connected ? Icon::LINK : Icon::LINK_OFF),
|
||||||
|
trustButton(device.trusted ? Icon::DONE_ALL : Icon::REMOVE_DONE) {
|
||||||
|
set_orientation(Gtk::Orientation::HORIZONTAL);
|
||||||
|
set_spacing(10);
|
||||||
|
|
||||||
|
add_css_class("bluetooth-settings-row");
|
||||||
|
set_hexpand(true);
|
||||||
|
set_halign(Gtk::Align::FILL);
|
||||||
|
|
||||||
|
if (!device.icon.empty()) {
|
||||||
|
this->icon.set_from_icon_name(device.icon);
|
||||||
|
this->icon.set_pixel_size(24);
|
||||||
|
append(this->icon);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto deviceInfoBox = Gtk::Box(Gtk::Orientation::VERTICAL);
|
||||||
|
deviceInfoBox.set_spacing(2);
|
||||||
|
append(deviceInfoBox);
|
||||||
|
|
||||||
|
nameLabel.set_ellipsize(Pango::EllipsizeMode::END);
|
||||||
|
nameLabel.set_text(device.name.empty() ? "Unknown Device" : device.name );
|
||||||
|
nameLabel.set_halign(Gtk::Align::START);
|
||||||
|
nameLabel.set_valign(Gtk::Align::CENTER);
|
||||||
|
deviceInfoBox.append(nameLabel);
|
||||||
|
|
||||||
|
addressLabel.set_text(device.address);
|
||||||
|
addressLabel.set_halign(Gtk::Align::START);
|
||||||
|
addressLabel.set_valign(Gtk::Align::CENTER);
|
||||||
|
addressLabel.add_css_class("bluetooth-device-address");
|
||||||
|
deviceInfoBox.append(addressLabel);
|
||||||
|
deviceInfoBox.set_size_request(140, -1);
|
||||||
|
|
||||||
|
auto buttonBox = Gtk::Box(Gtk::Orientation::HORIZONTAL);
|
||||||
|
buttonBox.set_hexpand(true);
|
||||||
|
buttonBox.set_halign(Gtk::Align::END);
|
||||||
|
append(buttonBox);
|
||||||
|
|
||||||
|
pairButton.signal_clicked().connect([this]() {
|
||||||
|
if (this->device.paired) {
|
||||||
|
BluetoothController::getInstance()->unpairDevice(this->device.object_path);
|
||||||
|
} else {
|
||||||
|
BluetoothController::getInstance()->pairDevice(this->device.object_path);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
buttonBox.append(pairButton);
|
||||||
|
|
||||||
|
connectButton.signal_clicked().connect([this]() {
|
||||||
|
if (this->device.connected) {
|
||||||
|
BluetoothController::getInstance()->disconnectDevice(this->device.object_path);
|
||||||
|
} else {
|
||||||
|
BluetoothController::getInstance()->connectDevice(this->device.object_path);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
buttonBox.append(connectButton);
|
||||||
|
|
||||||
|
trustButton.signal_clicked().connect([this]() {
|
||||||
|
BluetoothController::getInstance()->trustDevice(this->device.object_path, !this->device.trusted);
|
||||||
|
});
|
||||||
|
buttonBox.append(trustButton);
|
||||||
|
}
|
||||||
|
|
||||||
|
void BluetoothSettingsRow::updateDevice(const BluetoothDevice &device) {
|
||||||
|
this->device = device;
|
||||||
|
|
||||||
|
if (!device.icon.empty()) {
|
||||||
|
this->icon.set_from_icon_name(device.icon);
|
||||||
|
}
|
||||||
|
|
||||||
|
spdlog::info("Updating device {}: paired={}, connected={}, trusted={}", device.name, device.paired, device.connected, device.trusted);
|
||||||
|
|
||||||
|
nameLabel.set_text(device.name.empty() ? "Unknown Device" : device.name);
|
||||||
|
addressLabel.set_text(device.address);
|
||||||
|
pairButton.setIcon(device.paired ? Icon::BLUETOOTH_CONNECTED : Icon::BLUETOOTH);
|
||||||
|
connectButton.setIcon(device.connected ? Icon::LINK : Icon::LINK_OFF);
|
||||||
|
trustButton.setIcon(device.trusted ? Icon::DONE_ALL : Icon::REMOVE_DONE);
|
||||||
|
}
|
||||||
@@ -21,16 +21,17 @@ ControlCenter::ControlCenter(Icon::Type icon, std::string name)
|
|||||||
|
|
||||||
this->tabRow.set_orientation(Gtk::Orientation::HORIZONTAL);
|
this->tabRow.set_orientation(Gtk::Orientation::HORIZONTAL);
|
||||||
this->tabRow.set_spacing(4);
|
this->tabRow.set_spacing(4);
|
||||||
this->tabRow.set_margin_bottom(4);
|
|
||||||
this->tabRow.add_css_class("control-center-tab-row");
|
this->tabRow.add_css_class("control-center-tab-row");
|
||||||
|
|
||||||
this->mediaTabButton = std::make_unique<TabButton>(Icon::PLAY_CIRCLE);
|
this->mediaTabButton = std::make_unique<TabButton>(Icon::PLAY_CIRCLE);
|
||||||
this->infoTabButton = std::make_unique<TabButton>(Icon::EMPTY_DASHBOARD);
|
this->infoTabButton = std::make_unique<TabButton>(Icon::EMPTY_DASHBOARD);
|
||||||
this->timerButton = std::make_unique<TabButton>(Icon::TOKEN);
|
this->timerButton = std::make_unique<TabButton>(Icon::TOKEN);
|
||||||
|
this->settingsTabButton = std::make_unique<TabButton>(Icon::SETTINGS);
|
||||||
|
|
||||||
this->tabRow.append(*this->mediaTabButton);
|
this->tabRow.append(*this->mediaTabButton);
|
||||||
this->tabRow.append(*this->infoTabButton);
|
this->tabRow.append(*this->infoTabButton);
|
||||||
this->tabRow.append(*this->timerButton);
|
this->tabRow.append(*this->timerButton);
|
||||||
|
this->tabRow.append(*this->settingsTabButton);
|
||||||
|
|
||||||
this->container.append(this->tabRow);
|
this->container.append(this->tabRow);
|
||||||
|
|
||||||
@@ -39,15 +40,17 @@ ControlCenter::ControlCenter(Icon::Type icon, std::string name)
|
|||||||
this->contentStack.set_transition_type(Gtk::StackTransitionType::CROSSFADE);
|
this->contentStack.set_transition_type(Gtk::StackTransitionType::CROSSFADE);
|
||||||
|
|
||||||
this->mediaControlWidget = std::make_unique<MediaWidget>();
|
this->mediaControlWidget = std::make_unique<MediaWidget>();
|
||||||
|
this->weatherWidget = std::make_unique<WeatherWidget>();
|
||||||
this->weatherWidget = std::make_unique<WeatherWidget>();
|
this->timerWidget = std::make_unique<TimerWidget>();
|
||||||
|
this->settingsWidget = std::make_unique<SettingsWidget>();
|
||||||
|
|
||||||
this->contentStack.add(*this->mediaControlWidget, "controls", "Controls");
|
this->contentStack.add(*this->mediaControlWidget, "controls", "Controls");
|
||||||
this->contentStack.add(*this->weatherWidget, "info", "Info");
|
this->contentStack.add(*this->weatherWidget, "info", "Info");
|
||||||
this->contentStack.add(*Gtk::make_managed<Gtk::Label>("Timer"), "timer", "Timer");
|
this->contentStack.add(*this->timerWidget, "timer", "Timer");
|
||||||
|
this->contentStack.add(*this->settingsWidget, "settings", "Settings");
|
||||||
|
|
||||||
this->contentStack.set_visible_child("controls");
|
this->contentStack.set_visible_child("controls");
|
||||||
this->setActiveTab("info");
|
this->setActiveTab("controls");
|
||||||
|
|
||||||
this->container.append(this->contentStack);
|
this->container.append(this->contentStack);
|
||||||
|
|
||||||
@@ -63,6 +66,9 @@ ControlCenter::ControlCenter(Icon::Type icon, std::string name)
|
|||||||
this->setActiveTab("timer");
|
this->setActiveTab("timer");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this->settingsTabButton->signal_clicked().connect([this]() {
|
||||||
|
this->setActiveTab("settings");
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void ControlCenter::setActiveTab(const std::string &tab_name) {
|
void ControlCenter::setActiveTab(const std::string &tab_name) {
|
||||||
@@ -71,13 +77,15 @@ void ControlCenter::setActiveTab(const std::string &tab_name) {
|
|||||||
this->mediaTabButton->setActive(false);
|
this->mediaTabButton->setActive(false);
|
||||||
this->infoTabButton->setActive(false);
|
this->infoTabButton->setActive(false);
|
||||||
this->timerButton->setActive(false);
|
this->timerButton->setActive(false);
|
||||||
|
this->settingsTabButton->setActive(false);
|
||||||
|
|
||||||
if (tab_name == "controls") {
|
if (tab_name == "controls") {
|
||||||
this->mediaTabButton->setActive(true);
|
this->mediaTabButton->setActive(true);
|
||||||
} else if (tab_name == "info") {
|
} else if (tab_name == "info") {
|
||||||
this->infoTabButton->setActive(true);
|
this->infoTabButton->setActive(true);
|
||||||
} else if (tab_name == "timer") {
|
} else if (tab_name == "timer") {
|
||||||
this->timerButton->setActive(true);
|
this->timerButton->setActive(true);
|
||||||
|
} else if (tab_name == "settings") {
|
||||||
|
this->settingsTabButton->setActive(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ void MediaWidget::addPlayerWidget(const std::string &bus_name) {
|
|||||||
|
|
||||||
auto controller = MprisController::createForPlayer(bus_name);
|
auto controller = MprisController::createForPlayer(bus_name);
|
||||||
auto widget = std::make_unique<MediaPlayer>(controller);
|
auto widget = std::make_unique<MediaPlayer>(controller);
|
||||||
|
|
||||||
this->mediaWidgets.emplace(bus_name, std::move(widget));
|
this->mediaWidgets.emplace(bus_name, std::move(widget));
|
||||||
this->container.append(*this->mediaWidgets[bus_name]);
|
this->container.append(*this->mediaWidgets[bus_name]);
|
||||||
}
|
}
|
||||||
@@ -44,7 +44,7 @@ void MediaWidget::removePlayerWidget(const std::string &bus_name) {
|
|||||||
if (it == this->mediaWidgets.end()) {
|
if (it == this->mediaWidgets.end()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->container.remove(*it->second);
|
this->container.remove(*it->second);
|
||||||
this->mediaWidgets.erase(it);
|
this->mediaWidgets.erase(it);
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
#include "widgets/controlCenter/settings.hpp"
|
||||||
|
|
||||||
|
SettingsWidget::SettingsWidget() {
|
||||||
|
set_orientation(Gtk::Orientation::VERTICAL);
|
||||||
|
set_spacing(12);
|
||||||
|
|
||||||
|
this->append(this->bluetoothSettings);
|
||||||
|
}
|
||||||
@@ -0,0 +1,143 @@
|
|||||||
|
#include "widgets/controlCenter/timer.hpp"
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <gdk/gdkkeysyms.h>
|
||||||
|
#include <memory>
|
||||||
|
#include <spdlog/spdlog.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#include "gtkmm/entry.h"
|
||||||
|
#include "gtkmm/eventcontrollerkey.h"
|
||||||
|
#include "gtkmm/label.h"
|
||||||
|
|
||||||
|
TimerWidget::TimerWidget() {
|
||||||
|
set_orientation(Gtk::Orientation::VERTICAL);
|
||||||
|
set_spacing(6);
|
||||||
|
|
||||||
|
this->timerSetConnection = this->timerService->getSignalTimerSet().connect([this](const std::string &duration, uint64_t timerId) {
|
||||||
|
this->addTimer(duration, timerId);
|
||||||
|
});
|
||||||
|
|
||||||
|
this->timerCancelledConnection = this->timerService->getSignalTimerCancelled().connect([this](uint64_t timerId) {
|
||||||
|
this->removeTimer(timerId);
|
||||||
|
});
|
||||||
|
|
||||||
|
this->timerExpiredConnection = this->timerService->getSignalTimerExpired().connect([this](uint64_t timerId) {
|
||||||
|
this->activateTimer(timerId);
|
||||||
|
});
|
||||||
|
|
||||||
|
this->tickConnection = this->timerService->tickSignal.connect([this]() {
|
||||||
|
for (auto &[id, timer] : activeTimers) {
|
||||||
|
timer->tickDown();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
auto label = Gtk::make_managed<Gtk::Label>("Set Timer");
|
||||||
|
label->add_css_class("control-center-timer-label");
|
||||||
|
|
||||||
|
auto entry = Gtk::make_managed<Gtk::Entry>();
|
||||||
|
entry->set_placeholder_text("0s");
|
||||||
|
entry->set_valign(Gtk::Align::CENTER);
|
||||||
|
entry->set_alignment(0.5);
|
||||||
|
entry->add_css_class("text-area");
|
||||||
|
entry->set_editable(false);
|
||||||
|
entry->set_focusable(true);
|
||||||
|
entry->set_position(-1);
|
||||||
|
|
||||||
|
set_focusable(false);
|
||||||
|
|
||||||
|
auto keyController = Gtk::EventControllerKey::create();
|
||||||
|
keyController->set_propagation_phase(Gtk::PropagationPhase::CAPTURE);
|
||||||
|
keyController->signal_key_pressed().connect([this, entry](guint keyval, guint, Gdk::ModifierType) -> bool {
|
||||||
|
if (updatingText) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (keyval >= GDK_KEY_0 && keyval <= GDK_KEY_9) {
|
||||||
|
rawDigits.push_back(static_cast<char>('0' + (keyval - GDK_KEY_0)));
|
||||||
|
} else if (keyval >= GDK_KEY_KP_0 && keyval <= GDK_KEY_KP_9) {
|
||||||
|
rawDigits.push_back(static_cast<char>('0' + (keyval - GDK_KEY_KP_0)));
|
||||||
|
} else if (keyval == GDK_KEY_BackSpace || keyval == GDK_KEY_Delete ||
|
||||||
|
keyval == GDK_KEY_KP_Delete) {
|
||||||
|
if (!rawDigits.empty()) {
|
||||||
|
rawDigits.pop_back();
|
||||||
|
}
|
||||||
|
} else if (keyval == GDK_KEY_Return || keyval == GDK_KEY_KP_Enter ||
|
||||||
|
keyval == GDK_KEY_Tab || keyval == GDK_KEY_ISO_Left_Tab) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rawDigits.size() > 6) {
|
||||||
|
rawDigits.erase(0, rawDigits.size() - 6);
|
||||||
|
}
|
||||||
|
|
||||||
|
updatingText = true;
|
||||||
|
entry->set_text(format_duration(rawDigits));
|
||||||
|
entry->set_position(-1);
|
||||||
|
updatingText = false;
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
false);
|
||||||
|
entry->add_controller(keyController);
|
||||||
|
|
||||||
|
entry->signal_activate().connect([this, entry]() {
|
||||||
|
if (rawDigits.empty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t rawValue = std::stoull(rawDigits);
|
||||||
|
uint64_t seconds = (rawValue / 10000) * 3600 + ((rawValue / 100) % 100) * 60 + (rawValue % 100);
|
||||||
|
|
||||||
|
spdlog::info("Timer set for {} seconds", seconds);
|
||||||
|
this->timerService->addTimer(seconds);
|
||||||
|
rawDigits.clear();
|
||||||
|
this->updatingText = true;
|
||||||
|
entry->set_text("");
|
||||||
|
entry->set_position(-1);
|
||||||
|
this->updatingText = false;
|
||||||
|
},
|
||||||
|
false);
|
||||||
|
|
||||||
|
append(*label);
|
||||||
|
append(*entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
TimerWidget::~TimerWidget() {
|
||||||
|
if (this->timerSetConnection.connected()) {
|
||||||
|
this->timerSetConnection.disconnect();
|
||||||
|
}
|
||||||
|
if (this->timerCancelledConnection.connected()) {
|
||||||
|
this->timerCancelledConnection.disconnect();
|
||||||
|
}
|
||||||
|
if (this->timerExpiredConnection.connected()) {
|
||||||
|
this->timerExpiredConnection.disconnect();
|
||||||
|
}
|
||||||
|
if (this->tickConnection.connected()) {
|
||||||
|
this->tickConnection.disconnect();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
void TimerWidget::addTimer(const std::string &duration, uint64_t timerId) {
|
||||||
|
std::unique_ptr<Timer> timer = std::make_unique<Timer>(duration, timerId);
|
||||||
|
|
||||||
|
append(*timer);
|
||||||
|
|
||||||
|
this->activeTimers[timerId] = std::move(timer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void TimerWidget::removeTimer(uint64_t timerId) {
|
||||||
|
auto it = this->activeTimers.find(timerId);
|
||||||
|
if (it != this->activeTimers.end()) {
|
||||||
|
this->remove(*it->second);
|
||||||
|
this->activeTimers.erase(it);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void TimerWidget::activateTimer(uint64_t timerId) {
|
||||||
|
auto it = this->activeTimers.find(timerId);
|
||||||
|
if (it != this->activeTimers.end()) {
|
||||||
|
it->second->activateTimer();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,98 @@
|
|||||||
|
#include "widgets/mouseBattery.hpp"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "components/types/icon.hpp"
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
const char *cssClassFor(const hidpp::BatteryStatus &status) {
|
||||||
|
if (!status.connected)
|
||||||
|
return "mouse-battery-disconnected";
|
||||||
|
if (status.charging)
|
||||||
|
return "mouse-battery-charging";
|
||||||
|
|
||||||
|
switch (status.level) {
|
||||||
|
case hidpp::BatteryStatus::Level::CRITICAL:
|
||||||
|
return "mouse-battery-critical";
|
||||||
|
case hidpp::BatteryStatus::Level::LOW:
|
||||||
|
return "mouse-battery-low";
|
||||||
|
default:
|
||||||
|
return "mouse-battery-good";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Icon::Type iconFor(const hidpp::BatteryStatus &status) {
|
||||||
|
if (!status.connected)
|
||||||
|
return Icon::BATTERY_UNKNOWN;
|
||||||
|
if (status.charging)
|
||||||
|
return Icon::BATTERY_CHARGING_FULL;
|
||||||
|
if (status.level == hidpp::BatteryStatus::Level::CRITICAL)
|
||||||
|
return Icon::BATTERY_ALERT;
|
||||||
|
|
||||||
|
return Icon::BATTERY_FULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string labelFor(const hidpp::BatteryStatus &status) {
|
||||||
|
if (!status.connected)
|
||||||
|
return "--";
|
||||||
|
if (status.percentage >= 0)
|
||||||
|
return std::to_string(status.percentage) + "%";
|
||||||
|
|
||||||
|
switch (status.level) {
|
||||||
|
case hidpp::BatteryStatus::Level::CRITICAL:
|
||||||
|
return "crit";
|
||||||
|
case hidpp::BatteryStatus::Level::LOW:
|
||||||
|
return "low";
|
||||||
|
case hidpp::BatteryStatus::Level::GOOD:
|
||||||
|
return "ok";
|
||||||
|
case hidpp::BatteryStatus::Level::FULL:
|
||||||
|
return "full";
|
||||||
|
default:
|
||||||
|
return "?";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
MouseBatteryWidget::MouseBatteryWidget() : Gtk::Box(Gtk::Orientation::HORIZONTAL) {
|
||||||
|
set_valign(Gtk::Align::CENTER);
|
||||||
|
set_halign(Gtk::Align::CENTER);
|
||||||
|
set_spacing(4);
|
||||||
|
add_css_class("mouse-battery");
|
||||||
|
|
||||||
|
icon.add_css_class("material-icons");
|
||||||
|
icon.set_valign(Gtk::Align::CENTER);
|
||||||
|
|
||||||
|
label.set_valign(Gtk::Align::CENTER);
|
||||||
|
|
||||||
|
append(icon);
|
||||||
|
append(label);
|
||||||
|
|
||||||
|
this->service = MouseBatteryService::getInstance();
|
||||||
|
|
||||||
|
this->updatedConn =
|
||||||
|
this->service->updated().connect(sigc::mem_fun(*this, &MouseBatteryWidget::refresh));
|
||||||
|
|
||||||
|
refresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
MouseBatteryWidget::~MouseBatteryWidget() {
|
||||||
|
if (this->updatedConn.connected()) {
|
||||||
|
this->updatedConn.disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void MouseBatteryWidget::refresh() {
|
||||||
|
const hidpp::BatteryStatus current = this->service->getStatus();
|
||||||
|
|
||||||
|
icon.set_text(Icon::toString(iconFor(current)));
|
||||||
|
label.set_text(labelFor(current));
|
||||||
|
|
||||||
|
for (const char *name : {"mouse-battery-good", "mouse-battery-low", "mouse-battery-critical",
|
||||||
|
"mouse-battery-charging", "mouse-battery-disconnected"}) {
|
||||||
|
remove_css_class(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
add_css_class(cssClassFor(current));
|
||||||
|
}
|
||||||
@@ -41,17 +41,17 @@ BaseNotification::BaseNotification(uint64_t notificationId, std::shared_ptr<Gdk:
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this->signal_close.emit(this->notificationId);
|
this->getSignalClose().emit(this->notificationId);
|
||||||
});
|
});
|
||||||
add_controller(window_click);
|
add_controller(window_click);
|
||||||
|
|
||||||
auto window_motion = Gtk::EventControllerMotion::create();
|
auto window_motion = Gtk::EventControllerMotion::create();
|
||||||
window_motion->signal_enter().connect([this](double, double) {
|
window_motion->signal_enter().connect([this](double, double) {
|
||||||
signal_hover_changed.emit(true);
|
getSignalHoverChanged().emit(true);
|
||||||
pause_auto_close();
|
pause_auto_close();
|
||||||
});
|
});
|
||||||
window_motion->signal_leave().connect([this]() {
|
window_motion->signal_leave().connect([this]() {
|
||||||
signal_hover_changed.emit(false);
|
getSignalHoverChanged().emit(false);
|
||||||
resume_auto_close();
|
resume_auto_close();
|
||||||
});
|
});
|
||||||
add_controller(window_motion);
|
add_controller(window_motion);
|
||||||
@@ -81,8 +81,8 @@ void BaseNotification::start_auto_close_timeout(int timeoutMs) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
autoCloseDeadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeoutMs);
|
autoCloseDeadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeoutMs);
|
||||||
autoCloseConnection = Glib::signal_timeout().connect([this]() {
|
autoCloseConnection = Glib::signal_timeout().connect([self = shared_from_this()]() {
|
||||||
this->signal_close.emit(this->notificationId);
|
self->getSignalClose().emit(self->getNotificationId());
|
||||||
return false; // Don't repeat
|
return false; // Don't repeat
|
||||||
},
|
},
|
||||||
timeoutMs);
|
timeoutMs);
|
||||||
@@ -110,7 +110,7 @@ void BaseNotification::resume_auto_close() {
|
|||||||
|
|
||||||
autoClosePaused = false;
|
autoClosePaused = false;
|
||||||
if (autoCloseRemainingMs <= 0) {
|
if (autoCloseRemainingMs <= 0) {
|
||||||
this->signal_close.emit(this->notificationId);
|
this->getSignalClose().emit(this->notificationId);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -79,7 +79,6 @@ void CopyNotification::createImageNotification(NotifyMessage notify) {
|
|||||||
|
|
||||||
auto contentBox = Gtk::make_managed<Gtk::Box>();
|
auto contentBox = Gtk::make_managed<Gtk::Box>();
|
||||||
contentBox->set_orientation(Gtk::Orientation::VERTICAL);
|
contentBox->set_orientation(Gtk::Orientation::VERTICAL);
|
||||||
contentBox->set_margin(0);
|
|
||||||
auto imageWidget = Gtk::make_managed<Gtk::Image>(this->copiedImage);
|
auto imageWidget = Gtk::make_managed<Gtk::Image>(this->copiedImage);
|
||||||
contentBox->append(*imageWidget);
|
contentBox->append(*imageWidget);
|
||||||
imageWidget->set_pixel_size(300);
|
imageWidget->set_pixel_size(300);
|
||||||
@@ -92,7 +91,7 @@ void CopyNotification::createImageNotification(NotifyMessage notify) {
|
|||||||
copyToClipboard(this->copiedImage);
|
copyToClipboard(this->copiedImage);
|
||||||
spdlog::info("Copied image to clipboard");
|
spdlog::info("Copied image to clipboard");
|
||||||
|
|
||||||
this->signal_close.emit(this->notificationId);
|
this->getSignalClose().emit(this->getNotificationId());
|
||||||
});
|
});
|
||||||
saveToClipboardButton->set_tooltip_text("Copy to Clipboard");
|
saveToClipboardButton->set_tooltip_text("Copy to Clipboard");
|
||||||
saveToClipboardButton->add_css_class("notification-button");
|
saveToClipboardButton->add_css_class("notification-button");
|
||||||
@@ -100,7 +99,6 @@ void CopyNotification::createImageNotification(NotifyMessage notify) {
|
|||||||
|
|
||||||
auto saveToFileButton = Gtk::make_managed<IconButton>(Icon::SAVE);
|
auto saveToFileButton = Gtk::make_managed<IconButton>(Icon::SAVE);
|
||||||
saveToFileButton->signal_clicked().connect([this]() {
|
saveToFileButton->signal_clicked().connect([this]() {
|
||||||
// xdg-pic/screenshot // use env
|
|
||||||
auto xdgPicturesDir = Glib::get_user_special_dir(Glib::UserDirectory::PICTURES);
|
auto xdgPicturesDir = Glib::get_user_special_dir(Glib::UserDirectory::PICTURES);
|
||||||
auto dateStamp = Glib::DateTime::create_now_local().format("%Y%m%d_%H%M%S");
|
auto dateStamp = Glib::DateTime::create_now_local().format("%Y%m%d_%H%M%S");
|
||||||
auto filepath = xdgPicturesDir + "/screenshot";
|
auto filepath = xdgPicturesDir + "/screenshot";
|
||||||
@@ -108,7 +106,7 @@ void CopyNotification::createImageNotification(NotifyMessage notify) {
|
|||||||
saveImageToFile(this->copiedImage, filepath, filename);
|
saveImageToFile(this->copiedImage, filepath, filename);
|
||||||
spdlog::info("Saved image to {}", filepath.c_str());
|
spdlog::info("Saved image to {}", filepath.c_str());
|
||||||
|
|
||||||
this->signal_close.emit(this->notificationId);
|
this->getSignalClose().emit(this->getNotificationId());
|
||||||
});
|
});
|
||||||
saveToFileButton->set_tooltip_text("Save to File");
|
saveToFileButton->set_tooltip_text("Save to File");
|
||||||
saveToFileButton->add_css_class("notification-button");
|
saveToFileButton->add_css_class("notification-button");
|
||||||
@@ -134,7 +132,7 @@ void CopyNotification::createTextNotification(NotifyMessage notify) {
|
|||||||
auto copyToClipboardButton = Gtk::make_managed<IconButton>(Icon::CONTENT_COPY);
|
auto copyToClipboardButton = Gtk::make_managed<IconButton>(Icon::CONTENT_COPY);
|
||||||
copyToClipboardButton->signal_clicked().connect([this]() {
|
copyToClipboardButton->signal_clicked().connect([this]() {
|
||||||
copyToClipboard(this->copiedText);
|
copyToClipboard(this->copiedText);
|
||||||
this->signal_close.emit(this->notificationId);
|
this->getSignalClose().emit(this->getNotificationId());
|
||||||
});
|
});
|
||||||
copyToClipboardButton->set_tooltip_text("Copy to Clipboard");
|
copyToClipboardButton->set_tooltip_text("Copy to Clipboard");
|
||||||
copyToClipboardButton->add_css_class("notification-icon-button");
|
copyToClipboardButton->add_css_class("notification-icon-button");
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
#include "helpers/string.hpp"
|
#include "helpers/string.hpp"
|
||||||
|
|
||||||
#include "gtkmm/box.h"
|
#include "gtkmm/box.h"
|
||||||
|
#include "gtkmm/entry.h"
|
||||||
#include "gtkmm/image.h"
|
#include "gtkmm/image.h"
|
||||||
#include "gtkmm/label.h"
|
#include "gtkmm/label.h"
|
||||||
|
|
||||||
@@ -44,26 +45,49 @@ NotificationWindow::NotificationWindow(uint64_t notificationId, std::shared_ptr<
|
|||||||
header_box->append(*img);
|
header_box->append(*img);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto app_label = Gtk::make_managed<Gtk::Label>(StringHelper::trimToSize(notify.app_name, 24));
|
auto app_label = Gtk::make_managed<Gtk::Label>(notify.app_name);
|
||||||
app_label->set_halign(Gtk::Align::START);
|
app_label->set_halign(Gtk::Align::START);
|
||||||
|
app_label->set_ellipsize(Pango::EllipsizeMode::END);
|
||||||
|
app_label->set_max_width_chars(24);
|
||||||
app_label->set_wrap(false);
|
app_label->set_wrap(false);
|
||||||
app_label->add_css_class("notification-app-name");
|
app_label->add_css_class("notification-app-name");
|
||||||
header_box->append(*app_label);
|
header_box->append(*app_label);
|
||||||
vbox->append(*header_box);
|
vbox->append(*header_box);
|
||||||
|
|
||||||
// Summary label
|
// Summary label
|
||||||
auto summary_label = Gtk::make_managed<Gtk::Label>("<b>" + StringHelper::trimToSize(notify.summary, 20) + "</b>");
|
auto summary_label = Gtk::make_managed<Gtk::Label>("<b>" + notify.summary + "</b>");
|
||||||
summary_label->set_use_markup(true);
|
summary_label->set_use_markup(true);
|
||||||
summary_label->set_halign(Gtk::Align::START);
|
summary_label->set_halign(Gtk::Align::START);
|
||||||
|
summary_label->set_ellipsize(Pango::EllipsizeMode::END);
|
||||||
|
summary_label->set_lines(1);
|
||||||
summary_label->set_wrap(true);
|
summary_label->set_wrap(true);
|
||||||
vbox->append(*summary_label);
|
vbox->append(*summary_label);
|
||||||
|
|
||||||
auto body_label = Gtk::make_managed<Gtk::Label>(StringHelper::trimToSize(notify.body, 100));
|
auto body_label = Gtk::make_managed<Gtk::Label>(notify.body);
|
||||||
body_label->set_use_markup(true);
|
body_label->set_use_markup(true);
|
||||||
body_label->set_halign(Gtk::Align::START);
|
body_label->set_halign(Gtk::Align::START);
|
||||||
|
body_label->set_ellipsize(Pango::EllipsizeMode::END);
|
||||||
|
body_label->set_lines(3);
|
||||||
body_label->set_wrap(true);
|
body_label->set_wrap(true);
|
||||||
vbox->append(*body_label);
|
vbox->append(*body_label);
|
||||||
|
|
||||||
|
if (notify.has_input) {
|
||||||
|
auto entry = Gtk::make_managed<Gtk::Entry>();
|
||||||
|
entry->set_placeholder_text(notify.input_placeholder);
|
||||||
|
entry->set_halign(Gtk::Align::FILL);
|
||||||
|
entry->set_hexpand(true);
|
||||||
|
entry->add_css_class("notification-entry");
|
||||||
|
vbox->append(*entry);
|
||||||
|
entry->grab_focus_without_selecting();
|
||||||
|
|
||||||
|
entry->signal_activate().connect([this, entry, cb = notify.on_input]() {
|
||||||
|
if (cb) {
|
||||||
|
cb(entry->get_text());
|
||||||
|
this->getSignalClose().emit(this->getNotificationId());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// If actions exist, add buttons
|
// If actions exist, add buttons
|
||||||
if (!notify.actions.empty()) {
|
if (!notify.actions.empty()) {
|
||||||
auto actions_box = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL, 6);
|
auto actions_box = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL, 6);
|
||||||
@@ -91,7 +115,7 @@ NotificationWindow::NotificationWindow(uint64_t notificationId, std::shared_ptr<
|
|||||||
if (cb && guard && !*guard) {
|
if (cb && guard && !*guard) {
|
||||||
*guard = true;
|
*guard = true;
|
||||||
cb(action_id);
|
cb(action_id);
|
||||||
this->signal_close.emit(this->notificationId);
|
this->getSignalClose().emit(this->getNotificationId());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
actions_box->append(*btn);
|
actions_box->append(*btn);
|
||||||
|
|||||||
@@ -36,10 +36,12 @@ SpotifyNotification::SpotifyNotification(uint64_t notificationId, std::shared_pt
|
|||||||
|
|
||||||
auto artistLabel = Gtk::make_managed<Gtk::Label>();
|
auto artistLabel = Gtk::make_managed<Gtk::Label>();
|
||||||
if (!mpris.artist.empty()) {
|
if (!mpris.artist.empty()) {
|
||||||
artistLabel->set_text(StringHelper::trimToSize(mpris.artist[0], 30));
|
artistLabel->set_text(mpris.artist[0]);
|
||||||
} else {
|
} else {
|
||||||
artistLabel->set_text("Unknown Artist");
|
artistLabel->set_text("Unknown Artist");
|
||||||
}
|
}
|
||||||
|
artistLabel->set_ellipsize(Pango::EllipsizeMode::END);
|
||||||
|
artistLabel->set_max_width_chars(30);
|
||||||
artistLabel->set_hexpand(true);
|
artistLabel->set_hexpand(true);
|
||||||
artistLabel->set_halign(Gtk::Align::CENTER);
|
artistLabel->set_halign(Gtk::Align::CENTER);
|
||||||
|
|
||||||
@@ -66,7 +68,7 @@ std::unique_ptr<Gtk::CenterBox> SpotifyNotification::createButtonBox(MprisPlayer
|
|||||||
backButton->signal_clicked().connect([this, mpris]() {
|
backButton->signal_clicked().connect([this, mpris]() {
|
||||||
if (mpris.previous) {
|
if (mpris.previous) {
|
||||||
mpris.previous();
|
mpris.previous();
|
||||||
this->signal_close.emit(this->notificationId);
|
this->getSignalClose().emit(this->getNotificationId());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -93,7 +95,7 @@ std::unique_ptr<Gtk::CenterBox> SpotifyNotification::createButtonBox(MprisPlayer
|
|||||||
nextButton->signal_clicked().connect([this, mpris]() {
|
nextButton->signal_clicked().connect([this, mpris]() {
|
||||||
if (mpris.next) {
|
if (mpris.next) {
|
||||||
mpris.next();
|
mpris.next();
|
||||||
this->signal_close.emit(this->notificationId);
|
this->getSignalClose().emit(this->getNotificationId());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
buttonBox->set_start_widget(*backButton);
|
buttonBox->set_start_widget(*backButton);
|
||||||
|
|||||||
@@ -266,6 +266,7 @@ void TrayIconWidget::update(const TrayService::Item &item) {
|
|||||||
picture.set_visible(true);
|
picture.set_visible(true);
|
||||||
image.set_visible(false);
|
image.set_visible(false);
|
||||||
} else if (!item.iconName.empty()) {
|
} else if (!item.iconName.empty()) {
|
||||||
|
spdlog::info("App {} requesting icon name: {}", id, item.iconName); // Add this
|
||||||
image.set_from_icon_name(item.iconName);
|
image.set_from_icon_name(item.iconName);
|
||||||
image.set_pixel_size(20);
|
image.set_pixel_size(20);
|
||||||
image.set_visible(true);
|
image.set_visible(true);
|
||||||
|
|||||||
+121
-53
@@ -1,16 +1,21 @@
|
|||||||
#include "widgets/weather.hpp"
|
#include "widgets/weather.hpp"
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
#include <curl/curl.h>
|
#include <curl/curl.h>
|
||||||
#include <glibmm/main.h>
|
#include <glibmm/main.h>
|
||||||
#include <ios>
|
#include <iomanip>
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
#include <sstream>
|
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "components/weather.hpp"
|
||||||
|
|
||||||
|
#include "gtkmm/object.h"
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
constexpr const char *kWeatherUrl =
|
constexpr const char *kWeatherUrl =
|
||||||
"https://api.open-meteo.com/v1/forecast?latitude=49.0094&longitude=8.4044&daily=temperature_2m_max,temperature_2m_min,weather_code&hourly=temperature_2m,rain¤t=temperature_2m&timezone=Europe%2FBerlin";
|
"https://api.open-meteo.com/v1/forecast?latitude=49.0094&longitude=8.4044&daily=temperature_2m_max,temperature_2m_min,apparent_temperature_min,apparent_temperature_max,precipitation_sum,weather_code&hourly=temperature_2m,apparent_temperature,weather_code,precipitation_probability,precipitation¤t=temperature_2m,apparent_temperature,weather_code,precipitation&timezone=Europe%2FBerlin&past_days=0&forecast_days=7";
|
||||||
|
|
||||||
size_t write_to_buffer(void *contents, size_t size, size_t nmemb, void *userp) {
|
size_t write_to_buffer(void *contents, size_t size, size_t nmemb, void *userp) {
|
||||||
size_t total = size * nmemb;
|
size_t total = size * nmemb;
|
||||||
@@ -19,36 +24,41 @@ size_t write_to_buffer(void *contents, size_t size, size_t nmemb, void *userp) {
|
|||||||
return total;
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string formatTemp(double value) {
|
|
||||||
std::ostringstream ss;
|
|
||||||
ss.setf(std::ios::fixed);
|
|
||||||
ss.precision(1);
|
|
||||||
ss << value << "°C";
|
|
||||||
return ss.str();
|
|
||||||
}
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
WeatherWidget::WeatherWidget()
|
WeatherWidget::WeatherWidget() : Gtk::Box(Gtk::Orientation::VERTICAL) {
|
||||||
: Gtk::Box(Gtk::Orientation::VERTICAL) {
|
set_spacing(10);
|
||||||
this->set_orientation(Gtk::Orientation::VERTICAL);
|
|
||||||
this->set_spacing(6);
|
|
||||||
this->set_margin_top(4);
|
|
||||||
this->set_margin_bottom(4);
|
|
||||||
this->set_margin_start(4);
|
|
||||||
this->set_margin_end(4);
|
|
||||||
|
|
||||||
this->titleLabel.set_text("Weather");
|
currentScroll.set_policy(Gtk::PolicyType::ALWAYS, Gtk::PolicyType::NEVER);
|
||||||
this->currentLabel.set_text("Now: --");
|
currentScroll.set_propagate_natural_height(true);
|
||||||
this->todayLabel.set_text("Today: -- / --");
|
currentScroll.add_css_class("current-weather-scroll");
|
||||||
|
currentScroll.set_valign(Gtk::Align::START);
|
||||||
|
currentScroll.set_halign(Gtk::Align::FILL);
|
||||||
|
|
||||||
this->append(this->titleLabel);
|
hourlyScroll.set_policy(Gtk::PolicyType::ALWAYS, Gtk::PolicyType::NEVER);
|
||||||
this->append(this->currentLabel);
|
hourlyScroll.set_propagate_natural_height(true);
|
||||||
this->append(this->todayLabel);
|
hourlyScroll.add_css_class("hourly-weather-scroll");
|
||||||
|
hourlyScroll.set_valign(Gtk::Align::START);
|
||||||
|
hourlyScroll.set_halign(Gtk::Align::FILL);
|
||||||
|
|
||||||
this->fetchWeather();
|
dailyScroll.set_policy(Gtk::PolicyType::ALWAYS, Gtk::PolicyType::NEVER);
|
||||||
Glib::signal_timeout().connect_seconds(
|
dailyScroll.set_propagate_natural_height(true);
|
||||||
sigc::mem_fun(*this, &WeatherWidget::onRefreshTick),
|
dailyScroll.add_css_class("daily-weather-scroll");
|
||||||
3600);
|
dailyScroll.set_valign(Gtk::Align::START);
|
||||||
|
dailyScroll.set_halign(Gtk::Align::FILL);
|
||||||
|
|
||||||
|
this->append(currentScroll);
|
||||||
|
this->append(hourlyScroll);
|
||||||
|
this->append(dailyScroll);
|
||||||
|
|
||||||
|
m_dispatcher.connect([this]() {
|
||||||
|
this->refreshCurrentWeather();
|
||||||
|
this->refreshHourlyWeather();
|
||||||
|
this->refreshDailyWeather();
|
||||||
|
});
|
||||||
|
|
||||||
|
Glib::signal_timeout().connect(sigc::mem_fun(*this, &WeatherWidget::onRefreshTick), 15 * 60 * 1000);
|
||||||
|
this->onRefreshTick();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WeatherWidget::onRefreshTick() {
|
bool WeatherWidget::onRefreshTick() {
|
||||||
@@ -56,15 +66,45 @@ bool WeatherWidget::onRefreshTick() {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WeatherWidget::refreshCurrentWeather() {
|
||||||
|
auto current_box = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL);
|
||||||
|
current_box->set_halign(Gtk::Align::CENTER);
|
||||||
|
current_box->set_valign(Gtk::Align::CENTER);
|
||||||
|
this->currentScroll.set_child(*current_box);
|
||||||
|
|
||||||
|
auto &cw = this->current_weather;
|
||||||
|
auto weatherPill = Gtk::make_managed<WeatherInfo>(cw);
|
||||||
|
current_box->append(*weatherPill);
|
||||||
|
}
|
||||||
|
|
||||||
|
void WeatherWidget::refreshHourlyWeather() {
|
||||||
|
auto hourly_box = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL);
|
||||||
|
this->hourlyScroll.set_child(*hourly_box);
|
||||||
|
hourly_box->set_spacing(5);
|
||||||
|
|
||||||
|
for (auto &hw : this->hourly_weather) {
|
||||||
|
auto weatherPill = Gtk::make_managed<WeatherInfo>(hw);
|
||||||
|
hourly_box->append(*weatherPill);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void WeatherWidget::refreshDailyWeather() {
|
||||||
|
auto daily_box = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL);
|
||||||
|
this->dailyScroll.set_child(*daily_box);
|
||||||
|
daily_box->set_spacing(5);
|
||||||
|
|
||||||
|
for (auto &dw : this->daily_weather) {
|
||||||
|
auto weatherPill = Gtk::make_managed<WeatherInfo>(dw);
|
||||||
|
daily_box->append(*weatherPill);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void WeatherWidget::fetchWeather() {
|
void WeatherWidget::fetchWeather() {
|
||||||
std::thread([this]() {
|
std::thread([this]() {
|
||||||
std::string buffer;
|
std::string buffer;
|
||||||
|
|
||||||
CURL *curl = curl_easy_init();
|
CURL *curl = curl_easy_init();
|
||||||
if (!curl) {
|
if (!curl) {
|
||||||
Glib::signal_idle().connect_once([this]() {
|
|
||||||
this->applyWeatherText("Now: --", "Today: -- / --");
|
|
||||||
});
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,41 +112,69 @@ void WeatherWidget::fetchWeather() {
|
|||||||
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_to_buffer);
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_to_buffer);
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
|
||||||
curl_easy_setopt(curl, CURLOPT_USERAGENT, "bar/1.0");
|
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3");
|
||||||
|
|
||||||
auto res = curl_easy_perform(curl);
|
auto res = curl_easy_perform(curl);
|
||||||
curl_easy_cleanup(curl);
|
curl_easy_cleanup(curl);
|
||||||
|
|
||||||
if (res != CURLE_OK || buffer.empty()) {
|
if (res != CURLE_OK || buffer.empty()) {
|
||||||
Glib::signal_idle().connect_once([this]() {
|
|
||||||
this->applyWeatherText("Now: --", "Today: -- / --");
|
|
||||||
});
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
auto json = nlohmann::json::parse(buffer);
|
auto json = nlohmann::json::parse(buffer);
|
||||||
double current = json.at("current").at("temperature_2m").get<double>();
|
|
||||||
double minTemp = json.at("daily").at("temperature_2m_min").at(0).get<double>();
|
|
||||||
double maxTemp = json.at("daily").at("temperature_2m_max").at(0).get<double>();
|
|
||||||
|
|
||||||
std::string currentText = "Now: " + formatTemp(current);
|
auto current = json["current"];
|
||||||
std::string todayText = "Today: " + formatTemp(minTemp) + " / " + formatTemp(maxTemp);
|
|
||||||
|
this->current_weather.temperature = current["temperature_2m"].get<double>();
|
||||||
|
this->current_weather.apparent_temperature = current["apparent_temperature"].get<double>();
|
||||||
|
this->current_weather.weather_code = current["weather_code"].get<int>();
|
||||||
|
this->current_weather.precipitation = current["precipitation"].get<double>();
|
||||||
|
|
||||||
|
auto daily = json["daily"];
|
||||||
|
|
||||||
|
for (size_t i = 0; i < daily["time"].size(); i++) {
|
||||||
|
DailyWeather dw{};
|
||||||
|
dw.temperature_2m_max = daily["temperature_2m_max"][i].get<double>();
|
||||||
|
dw.temperature_2m_min = daily["temperature_2m_min"][i].get<double>();
|
||||||
|
dw.apparent_temperature_min = daily["apparent_temperature_min"][i].get<double>();
|
||||||
|
dw.apparent_temperature_max = daily["apparent_temperature_max"][i].get<double>();
|
||||||
|
dw.precipitation_sum = daily["precipitation_sum"][i].get<double>();
|
||||||
|
dw.weather_code = daily["weather_code"][i].get<int>();
|
||||||
|
auto time_str = daily["time"][i].get<std::string>();
|
||||||
|
std::tm tm = {};
|
||||||
|
std::istringstream ss(time_str);
|
||||||
|
ss >> std::get_time(&tm, "%Y-%m-%d");
|
||||||
|
|
||||||
|
std::mktime(&tm);
|
||||||
|
char buffer[10];
|
||||||
|
std::strftime(buffer, sizeof(buffer), "%a", &tm);
|
||||||
|
dw.time = buffer;
|
||||||
|
|
||||||
|
this->daily_weather[i] = dw;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto hourly = json["hourly"];
|
||||||
|
auto current_time = std::time(nullptr);
|
||||||
|
auto currentHour = std::localtime(¤t_time)->tm_hour;
|
||||||
|
|
||||||
|
for (int i = currentHour; i < currentHour + 24; i++) {
|
||||||
|
HourlyWeather hw{};
|
||||||
|
hw.temperature_2m = hourly["temperature_2m"][i].get<double>();
|
||||||
|
hw.apparent_temperature = hourly["apparent_temperature"][i].get<double>();
|
||||||
|
hw.weather_code = hourly["weather_code"][i].get<int>();
|
||||||
|
hw.precipitation_probability = hourly["precipitation_probability"][i].get<double>();
|
||||||
|
hw.precipitation = hourly["precipitation"][i].get<double>();
|
||||||
|
hw.time = hourly["time"][i].get<std::string>().substr(11, 5);
|
||||||
|
|
||||||
|
this->hourly_weather[i - currentHour] = hw;
|
||||||
|
}
|
||||||
|
|
||||||
Glib::signal_idle().connect_once([this, currentText, todayText]() {
|
|
||||||
this->applyWeatherText(currentText, todayText);
|
|
||||||
});
|
|
||||||
} catch (const std::exception &ex) {
|
} catch (const std::exception &ex) {
|
||||||
spdlog::error("Weather parse error: {}", ex.what());
|
spdlog::error("Weather parse error: {}", ex.what());
|
||||||
Glib::signal_idle().connect_once([this]() {
|
|
||||||
this->applyWeatherText("Now: --", "Today: -- / --");
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m_dispatcher.emit();
|
||||||
}).detach();
|
}).detach();
|
||||||
}
|
}
|
||||||
|
|
||||||
void WeatherWidget::applyWeatherText(const std::string ¤t_text,
|
|
||||||
const std::string &today_text) {
|
|
||||||
this->currentLabel.set_text(current_text);
|
|
||||||
this->todayLabel.set_text(today_text);
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user