waybar/src/factory.cpp

68 lines
2.1 KiB
C++
Raw Normal View History

2018-08-09 12:05:48 +02:00
#include "factory.hpp"
2018-12-01 00:10:41 +01:00
waybar::Factory::Factory(const Bar& bar, const Json::Value& config)
2018-08-20 14:50:45 +02:00
: bar_(bar), config_(config)
2018-08-09 12:05:48 +02:00
{}
2018-08-20 14:50:45 +02:00
waybar::IModule* waybar::Factory::makeModule(const std::string &name) const
2018-08-09 12:05:48 +02:00
{
2018-08-13 14:05:13 +02:00
try {
2018-12-18 17:30:54 +01:00
auto hash_pos = name.find("#");
auto ref = name.substr(0, hash_pos);
auto id = hash_pos != std::string::npos ? name.substr(hash_pos + 1) : "";
2018-10-25 17:30:26 +02:00
if (ref == "battery") {
2018-12-18 17:30:54 +01:00
return new waybar::modules::Battery(id, config_[name]);
2018-08-16 14:29:41 +02:00
}
2018-08-20 17:20:02 +02:00
#ifdef HAVE_SWAY
2018-10-30 13:39:30 +01:00
if (ref == "sway/mode") {
2018-12-18 17:30:54 +01:00
return new waybar::modules::sway::Mode(id, bar_, config_[name]);
2018-10-30 13:39:30 +01:00
}
2018-10-25 17:30:26 +02:00
if (ref == "sway/workspaces") {
2018-12-18 17:30:54 +01:00
return new waybar::modules::sway::Workspaces(id, bar_, config_[name]);
2018-08-16 14:29:41 +02:00
}
2018-10-25 17:30:26 +02:00
if (ref == "sway/window") {
2018-12-18 17:30:54 +01:00
return new waybar::modules::sway::Window(id, bar_, config_[name]);
2018-08-16 14:29:41 +02:00
}
2018-09-10 11:25:53 +02:00
#endif
2018-10-25 17:30:26 +02:00
if (ref == "memory") {
2018-12-18 17:30:54 +01:00
return new waybar::modules::Memory(id, config_[name]);
2018-08-16 14:29:41 +02:00
}
2018-10-25 17:30:26 +02:00
if (ref == "cpu") {
2018-12-18 17:30:54 +01:00
return new waybar::modules::Cpu(id, config_[name]);
2018-08-16 14:29:41 +02:00
}
2018-10-25 17:30:26 +02:00
if (ref == "clock") {
2018-12-18 17:30:54 +01:00
return new waybar::modules::Clock(id, config_[name]);
2018-08-16 14:29:41 +02:00
}
2018-10-25 12:24:39 +02:00
#ifdef HAVE_DBUSMENU
2018-10-25 17:30:26 +02:00
if (ref == "tray") {
2018-12-18 17:30:54 +01:00
return new waybar::modules::SNI::Tray(id, config_[name]);
}
2018-10-25 11:47:03 +02:00
#endif
2018-08-20 17:20:02 +02:00
#ifdef HAVE_LIBNL
2018-10-25 17:30:26 +02:00
if (ref == "network") {
2018-12-18 17:30:54 +01:00
return new waybar::modules::Network(id, config_[name]);
2018-08-16 14:29:41 +02:00
}
2018-08-20 17:20:02 +02:00
#endif
#ifdef HAVE_LIBUDEV
if (ref == "backlight") {
return new waybar::modules::Backlight(id, config_[name]);
}
#endif
2018-08-20 17:20:02 +02:00
#ifdef HAVE_LIBPULSE
2018-10-25 17:30:26 +02:00
if (ref == "pulseaudio") {
2018-12-18 17:30:54 +01:00
return new waybar::modules::Pulseaudio(id, config_[name]);
2018-08-16 14:29:41 +02:00
}
2018-08-20 17:20:02 +02:00
#endif
2018-10-25 17:30:26 +02:00
if (ref.compare(0, 7, "custom/") == 0 && ref.size() > 7) {
return new waybar::modules::Custom(ref.substr(7), config_[name]);
2018-08-16 14:29:41 +02:00
}
2018-08-13 14:05:13 +02:00
} catch (const std::exception& e) {
auto err = fmt::format("Disabling module \"{}\", {}", name, e.what());
2018-08-19 13:39:57 +02:00
throw std::runtime_error(err);
2018-08-13 14:05:13 +02:00
} catch (...) {
auto err = fmt::format("Disabling module \"{}\", Unknown reason", name);
2018-08-19 13:39:57 +02:00
throw std::runtime_error(err);
2018-08-13 14:05:13 +02:00
}
2018-08-19 13:39:57 +02:00
throw std::runtime_error("Unknown module: " + name);
2018-08-09 12:05:48 +02:00
}