Skip to content

Commit b946ef2

Browse files
committed
Changing to class structure --Semi rebase from tfc_tui_play
1 parent cbdf84d commit b946ef2

File tree

4 files changed

+189
-140
lines changed

4 files changed

+189
-140
lines changed

exes/ttui/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
add_executable(ttui src/ttui.cpp)
1+
add_executable(ttui src/ttui.cpp
2+
src/dbus_screen_manager.hpp
3+
src/dbus_screen_manager.cpp)
24

35
find_package(Boost REQUIRED COMPONENTS program_options)
46
find_package(mp-units CONFIG REQUIRED)

exes/ttui/src/dbus_screen_manager.cpp

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#include "dbus_screen_manager.hpp"
2+
#include <boost/asio.hpp>
3+
#include <iostream>
4+
#include <sdbusplus/asio/connection.hpp>
5+
#include <sdbusplus/bus/match.hpp>
6+
#include <tfc/dbus/sd_bus.hpp>
7+
8+
DBusScreenManager::DBusScreenManager() : screen(ftxui::ScreenInteractive::TerminalOutput()) {
9+
}
10+
11+
void DBusScreenManager::Run() {
12+
SetupComponents();
13+
SetupDBusConnection();
14+
ftxui::Loop loop(&screen, renderer);
15+
while (!loop.HasQuitted()) {
16+
loop.RunOnce();
17+
ctx.run_for(std::chrono::milliseconds(10));
18+
}
19+
}
20+
21+
void DBusScreenManager::SetupComponents() {
22+
right_menuz = right_menu();
23+
left_menuz = left_menu();
24+
container = ftxui::Container::Horizontal({left_menuz, right_menuz});
25+
renderer = Renderer(container, [&] {
26+
return ftxui::vbox({
27+
// -------- Top panel --------------
28+
ftxui::hbox({
29+
// -------- Left Menu --------------
30+
ftxui::vbox({
31+
ftxui::hcenter(ftxui::bold(ftxui::text("Percentage by 10%"))),
32+
ftxui::separator(),
33+
left_menuz->Render(),
34+
}),
35+
ftxui::separator(),
36+
// -------- Right Menu --------------
37+
ftxui::vbox({
38+
ftxui::hcenter(ftxui::bold(ftxui::text("Percentage by 1%"))),
39+
ftxui::separator(),
40+
right_menuz->Render(),
41+
}) | ftxui::flex,
42+
ftxui::separator(),
43+
}),
44+
}) |
45+
ftxui::border | ftxui::flex;
46+
});
47+
}
48+
49+
void DBusScreenManager::SetupDBusConnection() {
50+
auto connection = std::make_shared<sdbusplus::asio::connection>(ctx, tfc::dbus::sd_bus_open_system_mon());
51+
auto match = std::make_unique<sdbusplus::bus::match::match>(
52+
*connection,
53+
"",
54+
std::bind(&DBusScreenManager::match_callback, this, std::placeholders::_1)
55+
);
56+
auto mc = connection->new_method_call("org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus.Monitoring",
57+
"BecomeMonitor");
58+
mc.append<std::vector<std::string>, uint32_t>({ "path=/com/skaginn3x/Signals" }, 0);
59+
auto reply = connection->call(mc);
60+
if (reply.is_method_error()) {
61+
if (reply.get_error() != nullptr)
62+
throw std::runtime_error(reply.get_error()->message);
63+
throw std::runtime_error("Unknown error");
64+
}
65+
66+
}
67+
68+
void DBusScreenManager::match_callback(sdbusplus::message_t& msg) {
69+
if (msg.get_member() && std::string(msg.get_member()) == "PropertiesChanged") {
70+
std::tuple<std::string, std::vector<std::pair<std::string, std::variant<bool, uint64_t>>>, std::vector<std::string>>
71+
container{};
72+
if (sdbusplus::utility::read_into_tuple(container, msg)) {
73+
std::string const& interface = std::get<0>(container);
74+
auto position = std::find(noticed_interfaces.begin(), noticed_interfaces.end(), interface);
75+
auto value = std::get<1>(container)[0].second;
76+
if (position == noticed_interfaces.end()) {
77+
noticed_interfaces.emplace_back(interface);
78+
values.emplace_back(value);
79+
right_menuz->Render();
80+
} else {
81+
auto index = position - noticed_interfaces.begin();
82+
if (values[index] != value) {
83+
if (index == left_menu_selected) {
84+
values[index] = value;
85+
86+
std::string current_value = std::visit(VariantToString(), values[index]);
87+
entries = { current_value };
88+
right_menuz->Render();
89+
screen.Post(ftxui::Event::Custom);
90+
}
91+
}
92+
}
93+
// std::visit([&interface](auto& value) { std::cout << interface << ": " << value << std::endl; },
94+
// std::get<1>(container)[0].second);
95+
}
96+
}
97+
}
98+
99+
ftxui::Component DBusScreenManager::left_menu() {
100+
auto option = ftxui::MenuOption::Vertical();
101+
102+
option.on_enter = [this] {
103+
std::string current_value = std::visit(VariantToString(), values[left_menu_selected]);
104+
entries = { current_value };
105+
right_menuz->TakeFocus();
106+
screen.Post(ftxui::Event::Custom);
107+
};
108+
109+
return ftxui::Menu(&noticed_interfaces, &left_menu_selected, option);
110+
}
111+
112+
ftxui::Component DBusScreenManager::right_menu() {
113+
auto option = ftxui::MenuOption::Vertical();
114+
option.on_enter = screen.ExitLoopClosure();
115+
return ftxui::Menu(&entries, &right_menu_selected, option);
116+
}

exes/ttui/src/dbus_screen_manager.hpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// DBusScreenManager.hpp
2+
3+
#pragma once
4+
5+
#include "ftxui/component/captured_mouse.hpp" // for ftxui
6+
#include "ftxui/component/component.hpp" // for Menu, Renderer, ScreenInteractive
7+
#include "ftxui/component/component_options.hpp" // for MenuOption
8+
#include "ftxui/component/loop.hpp" // for Loop
9+
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
10+
#include <sdbusplus/asio/connection.hpp>
11+
#include <sdbusplus/bus/match.hpp>
12+
#include <boost/program_options.hpp>
13+
#include <boost/asio.hpp>
14+
#include <vector>
15+
#include <variant>
16+
#include <string>
17+
18+
namespace asio = boost::asio;
19+
namespace po = boost::program_options;
20+
21+
22+
class DBusScreenManager {
23+
public:
24+
DBusScreenManager();
25+
void Run();
26+
27+
private:
28+
ftxui::ScreenInteractive screen;
29+
ftxui::Component left_menuz, right_menuz, container;
30+
ftxui::Component renderer;
31+
std::vector<std::string> noticed_interfaces;
32+
std::vector<std::variant<bool, uint64_t>> values;
33+
std::vector<std::string> entries;
34+
int left_menu_selected = 0;
35+
int right_menu_selected = 0;
36+
asio::io_context ctx{};
37+
38+
void SetupComponents();
39+
void SetupDBusConnection();
40+
void match_callback(sdbusplus::message_t& msg);
41+
ftxui::Component left_menu();
42+
ftxui::Component right_menu();
43+
struct VariantToString {
44+
std::string operator()(bool v) const { return v ? "true" : "false"; }
45+
std::string operator()(int64_t v) const { return std::to_string(v); }
46+
std::string operator()(uint64_t v) const { return std::to_string(v); }
47+
std::string operator()(double v) const { return std::to_string(v); }
48+
std::string operator()(const std::string& v) const { return v; }
49+
std::string operator()(const std::vector<std::string>& v) const {
50+
std::string result;
51+
for (const auto& elem : v) {
52+
if (!result.empty()) {
53+
result += ", "; // Delimiter between elements
54+
}
55+
result += elem;
56+
}
57+
return result;
58+
}
59+
};
60+
};

exes/ttui/src/ttui.cpp

Lines changed: 10 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,8 @@
1-
#include "ftxui/component/captured_mouse.hpp" // for ftxui
2-
#include "ftxui/component/component.hpp" // for Menu
3-
#include "ftxui/component/component_options.hpp" // for MenuOption
4-
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
5-
6-
#include <memory>
7-
#include <string>
8-
#include <string_view>
9-
#include <variant>
10-
11-
#include <fmt/core.h>
12-
#include <mp-units/format.h>
13-
14-
#include <boost/asio.hpp>
15-
#include <boost/program_options.hpp>
16-
#include <ftxui/component/component.hpp>
171
#include <iostream>
18-
19-
#include <sdbusplus/asio/connection.hpp>
20-
#include <sdbusplus/asio/property.hpp>
21-
#include <sdbusplus/bus/match.hpp>
22-
23-
#include <tfc/dbus/match_rules.hpp>
24-
#include <tfc/dbus/sd_bus.hpp>
25-
26-
#include <tfc/ipc.hpp>
272
#include <tfc/progbase.hpp>
3+
#include "dbus_screen_manager.hpp"
284

29-
namespace asio = boost::asio;
30-
namespace po = boost::program_options;
31-
32-
using variant_t = std::variant<bool, int64_t, uint64_t, double, std::string, std::vector<std::string>>;
33-
34-
auto left_menu(const std::vector<std::string>* entries, int* selected, const ftxui::Component& right_menu)
35-
-> ftxui::Component {
36-
auto option = ftxui::MenuOption::Vertical();
37-
38-
option.on_enter = [selected, right_menu] { right_menu->TakeFocus(); };
39-
40-
return ftxui::Menu(entries, selected, option);
41-
};
42-
43-
auto right_menu(const std::vector<std::string>* entries, int* selected) -> ftxui::Component {
44-
const auto option = ftxui::MenuOption::Vertical();
45-
// fill remaining horizontal space
46-
return ftxui::Menu(entries, selected, option);
47-
};
48-
49-
// GET DEMM DBUS DATA
50-
auto match_callback(sdbusplus::message_t& msg) -> void {
51-
// std::cout << msg.get_member() << std::endl;
52-
// std::cout << msg.get_signature() << std::endl;
53-
// std::cout << msg.get_sender() << std::endl;
54-
// std::cout << msg.get_interface() << std::endl;
55-
// std::cout << msg.get_path() << std::endl;
56-
if (msg.get_member() && std::string(msg.get_member()) == "PropertiesChanged") {
57-
std::tuple<std::string, std::vector<std::pair<std::string, std::variant<bool, uint64_t>>>, std::vector<std::string>>
58-
container{};
59-
if (sdbusplus::utility::read_into_tuple(container, msg)) {
60-
std::string const& interface = std::get<0>(container);
61-
std::visit([&interface](auto& value) { std::cout << interface << ": " << value << std::endl; },
62-
std::get<1>(container)[0].second);
63-
}
64-
}
65-
}
66-
67-
auto main(int argc, char** argv) -> int {
5+
int main(int argc, char** argv) {
686
auto description{ tfc::base::default_description() };
697

708
std::string signal{};
@@ -79,79 +17,12 @@ auto main(int argc, char** argv) -> int {
7917
"list-signals", po::bool_switch(&list_signals), "List all available IPC signals")(
8018
"list-slots", po::bool_switch(&list_slots), "List all available IPC slots");
8119
tfc::base::init(argc, argv, description);
82-
83-
asio::io_context ctx{};
84-
std::shared_ptr<sdbusplus::asio::connection> connection =
85-
std::make_shared<sdbusplus::asio::connection>(ctx, tfc::dbus::sd_bus_open_system_mon());
86-
// Attach callback
87-
auto match = std::make_unique<sdbusplus::bus::match::match>(*connection, "", match_callback);
88-
// Become monitor method
89-
auto mc = connection->new_method_call("org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus.Monitoring",
90-
"BecomeMonitor");
91-
mc.append<std::vector<std::string>, uint32_t>({ "path=/com/skaginn3x/Signals" }, 0);
92-
auto reply = connection->call(mc);
93-
if (reply.is_method_error()) {
94-
if (reply.get_error() != nullptr)
95-
throw std::runtime_error(reply.get_error()->message);
96-
throw std::runtime_error("Unknown error");
20+
try {
21+
DBusScreenManager sm;
22+
sm.Run();
23+
} catch (const std::exception& e) {
24+
std::cerr << "Error: " << e.what() << std::endl;
25+
return 1;
9726
}
98-
// auto ucontainer = connection->new_method_call("org.freedesktop.DBus", "/org/freedesktop/DBus",
99-
// "org.freedesktop.DBus.Monitoring",
100-
// "BecomeMonitor");
101-
// ucontainer.append<std::tuple<std::string, std::vector<std::pair<std::string, std::variant<bool>>>,
102-
// std::vector<std::string>>>(
103-
// {"test",std::vector<std::pair<std::string, std::variant<bool>>>{ std::pair<std::string, std::variant<bool>>{ "test",
104-
// true } }, std::vector<std::string>{ "test" }}
105-
// );
106-
// try{
107-
// connection->call(ucontainer);
108-
// }catch (sdbusplus::exception::SdBusError& e){
109-
// std::cout << ucontainer.get_signature() << " IS THIS THE REAL LIFE" << std::endl;
110-
// }
111-
ctx.run();
112-
auto screen = ftxui::ScreenInteractive::TerminalOutput();
113-
114-
std::vector<std::string> left_menu_entries = {
115-
"0%", "10%", "20%", "30%", "40%", "50%", "60%", "70%", "80%", "90%",
116-
};
117-
std::vector<std::string> right_menu_entries = {
118-
"0%", "1%", "2%", "3%", "4%", "5%", "6%", "7%", "8%", "9%", "10%",
119-
};
120-
121-
auto menu_option = ftxui::MenuOption();
122-
menu_option.on_enter = screen.ExitLoopClosure();
123-
124-
int left_menu_selected = 0;
125-
int right_menu_selected = 0;
126-
127-
std::vector<std::string> services = { "hi" };
128-
ftxui::Component right_menuz = right_menu(&right_menu_entries, &right_menu_selected);
129-
ftxui::Component left_menuz = left_menu(&services, &left_menu_selected, right_menuz);
130-
131-
ftxui::Component container = ftxui::Container::Horizontal({ left_menuz, right_menuz });
132-
133-
auto renderer = Renderer(container, [&] {
134-
return ftxui::vbox({
135-
// -------- Top panel --------------
136-
ftxui::hbox({
137-
// -------- Left Menu --------------
138-
ftxui::vbox({
139-
ftxui::hcenter(ftxui::bold(ftxui::text("Percentage by 10%"))),
140-
ftxui::separator(),
141-
left_menuz->Render(),
142-
}),
143-
ftxui::separator(),
144-
// -------- Right Menu --------------
145-
ftxui::vbox({
146-
ftxui::hcenter(ftxui::bold(ftxui::text("Percentage by 1%"))),
147-
ftxui::separator(),
148-
right_menuz->Render(),
149-
}) | ftxui::flex,
150-
ftxui::separator(),
151-
}),
152-
}) |
153-
ftxui::border | ftxui::flex;
154-
});
155-
156-
screen.Loop(renderer);
157-
};
27+
return 0;
28+
}

0 commit comments

Comments
 (0)