diff --git a/gui/CMakeLists.txt b/gui/CMakeLists.txt index 9a72982..6343a48 100644 --- a/gui/CMakeLists.txt +++ b/gui/CMakeLists.txt @@ -36,7 +36,9 @@ add_executable(chiaki include/avopenglframeuploader.h src/avopenglframeuploader.cpp include/servericonwidget.h - src/servericonwidget.cpp) + src/servericonwidget.cpp + include/settings.h + src/settings.cpp) target_include_directories(chiaki PRIVATE include) target_link_libraries(chiaki chiaki-lib chiaki-cli-lib) diff --git a/gui/include/discoverymanager.h b/gui/include/discoverymanager.h index a1e69d5..c99e0c9 100644 --- a/gui/include/discoverymanager.h +++ b/gui/include/discoverymanager.h @@ -43,6 +43,7 @@ class DiscoveryManager : public QObject private: ChiakiLog log; ChiakiDiscoveryService service; + bool service_active; QList hosts; private slots: @@ -52,6 +53,8 @@ class DiscoveryManager : public QObject explicit DiscoveryManager(QObject *parent = nullptr); ~DiscoveryManager(); + void SetActive(bool active); + const QList GetHosts() const { return hosts; } signals: diff --git a/gui/include/mainwindow.h b/gui/include/mainwindow.h index ffc0e5c..a78388f 100644 --- a/gui/include/mainwindow.h +++ b/gui/include/mainwindow.h @@ -24,6 +24,7 @@ class DynamicGridWidget; class ServerItemWidget; +class Settings; struct DisplayServer { @@ -36,6 +37,10 @@ class MainWindow : public QMainWindow Q_OBJECT private: + Settings *settings; + + QAction *discover_action; + DynamicGridWidget *grid_widget; QList server_item_widgets; @@ -47,14 +52,14 @@ class MainWindow : public QMainWindow void ServerItemWidgetSelected(); void ServerItemWidgetTriggered(); - void RunDiscovery(); + void UpdateDiscoveryEnabled(); void ShowSettings(); void UpdateDisplayServers(); void UpdateServerWidgets(); public: - explicit MainWindow(QWidget *parent = nullptr); + explicit MainWindow(Settings *settings, QWidget *parent = nullptr); ~MainWindow() override; }; diff --git a/gui/include/settings.h b/gui/include/settings.h new file mode 100644 index 0000000..bc68fb1 --- /dev/null +++ b/gui/include/settings.h @@ -0,0 +1,35 @@ +/* + * This file is part of Chiaki. + * + * Chiaki is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Chiaki is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Chiaki. If not, see . + */ + +#ifndef CHIAKI_SETTINGS_H +#define CHIAKI_SETTINGS_H + +#include + +class Settings +{ + private: + QSettings settings; + + public: + Settings(); + + bool GetDiscoveryEnabled() { return settings.value("settings/auto_discovery", true).toBool(); } + void SetDiscoveryEnabled(bool enabled) { settings.setValue("settings/auto_discovery", enabled); } +}; + +#endif // CHIAKI_SETTINGS_H diff --git a/gui/src/discoverymanager.cpp b/gui/src/discoverymanager.cpp index 05f478a..0e5b289 100644 --- a/gui/src/discoverymanager.cpp +++ b/gui/src/discoverymanager.cpp @@ -31,28 +31,51 @@ DiscoveryManager::DiscoveryManager(QObject *parent) : QObject(parent) { chiaki_log_init(&log, CHIAKI_LOG_ALL & ~CHIAKI_LOG_VERBOSE, chiaki_log_cb_print, nullptr); - ChiakiDiscoveryServiceOptions options; - options.ping_ms = PING_MS; - options.hosts_max = HOSTS_MAX; - options.host_drop_pings = DROP_PINGS; - options.cb = DiscoveryServiceHostsCallback; - options.cb_user = this; - - sockaddr_in addr = {}; - addr.sin_family = AF_INET; - addr.sin_port = htons(CHIAKI_DISCOVERY_PORT); - addr.sin_addr.s_addr = 0xffffffff; // 255.255.255.255 - options.send_addr = reinterpret_cast(&addr); - options.send_addr_size = sizeof(addr); - - ChiakiErrorCode err = chiaki_discovery_service_init(&service, &options, &log); - if(err != CHIAKI_ERR_SUCCESS) - throw std::exception(); + service_active = false; } DiscoveryManager::~DiscoveryManager() { - chiaki_discovery_service_fini(&service); + if(service_active) + chiaki_discovery_service_fini(&service); +} + +void DiscoveryManager::SetActive(bool active) +{ + if(service_active == active) + return; + service_active = active; + + if(active) + { + ChiakiDiscoveryServiceOptions options; + options.ping_ms = PING_MS; + options.hosts_max = HOSTS_MAX; + options.host_drop_pings = DROP_PINGS; + options.cb = DiscoveryServiceHostsCallback; + options.cb_user = this; + + sockaddr_in addr = {}; + addr.sin_family = AF_INET; + addr.sin_port = htons(CHIAKI_DISCOVERY_PORT); + addr.sin_addr.s_addr = 0xffffffff; // 255.255.255.255 + options.send_addr = reinterpret_cast(&addr); + options.send_addr_size = sizeof(addr); + + ChiakiErrorCode err = chiaki_discovery_service_init(&service, &options, &log); + if(err != CHIAKI_ERR_SUCCESS) + { + CHIAKI_LOGE(&log, "DiscoveryManager failed to init Discovery Service"); + return; + } + } + else + { + chiaki_discovery_service_fini(&service); + hosts = {}; + emit HostsUpdated(); + } + } void DiscoveryManager::DiscoveryServiceHosts(QList hosts) diff --git a/gui/src/main.cpp b/gui/src/main.cpp index 0064b96..d658a37 100644 --- a/gui/src/main.cpp +++ b/gui/src/main.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include @@ -28,13 +29,16 @@ static const QMap cli_commands = { }; int RunStream(QApplication &app, const StreamSessionConnectInfo &connect_info); -int RunMain(QApplication &app); +int RunMain(QApplication &app, Settings *settings); int main(int argc, char *argv[]) { qRegisterMetaType(); qRegisterMetaType(); + QApplication::setOrganizationName("Chiaki"); + QApplication::setApplicationName("Chiaki"); + ChiakiErrorCode err = chiaki_lib_init(); if(err != CHIAKI_ERR_SUCCESS) { @@ -43,7 +47,8 @@ int main(int argc, char *argv[]) } QApplication app(argc, argv); - QApplication::setApplicationName("Chiaki"); + + Settings settings; QCommandLineParser parser; parser.setOptionsAfterPositionalArgumentsMode(QCommandLineParser::ParseAsPositionalArguments); @@ -76,7 +81,7 @@ int main(int argc, char *argv[]) QStringList args = parser.positionalArguments(); if(args.length() == 0) - return RunMain(app); + return RunMain(app, &settings); if(args[0] == "stream") { @@ -129,9 +134,9 @@ int main(int argc, char *argv[]) } } -int RunMain(QApplication &app) +int RunMain(QApplication &app, Settings *settings) { - MainWindow main_window; + MainWindow main_window(settings); main_window.show(); return app.exec(); } diff --git a/gui/src/mainwindow.cpp b/gui/src/mainwindow.cpp index f1cde41..2dc8b6d 100644 --- a/gui/src/mainwindow.cpp +++ b/gui/src/mainwindow.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -25,7 +26,9 @@ #include #include -MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) +MainWindow::MainWindow(Settings *settings, QWidget *parent) + : QMainWindow(parent), + settings(settings) { auto main_widget = new QWidget(this); auto layout = new QVBoxLayout(); @@ -37,9 +40,11 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) tool_bar->setMovable(false); addToolBar(tool_bar); - auto discover_action = new QAction(tr("Discover"), this); + discover_action = new QAction(tr("Automatically Search for Consoles"), this); + discover_action->setCheckable(true); + discover_action->setChecked(settings->GetDiscoveryEnabled()); tool_bar->addAction(discover_action); - connect(discover_action, &QAction::triggered, this, &MainWindow::RunDiscovery); + connect(discover_action, &QAction::triggered, this, &MainWindow::UpdateDiscoveryEnabled); auto tool_bar_spacer = new QWidget(); tool_bar_spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Ignored); @@ -69,6 +74,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) connect(&discovery_manager, &DiscoveryManager::HostsUpdated, this, &MainWindow::UpdateDisplayServers); UpdateDisplayServers(); + UpdateDiscoveryEnabled(); } MainWindow::~MainWindow() @@ -98,9 +104,11 @@ void MainWindow::ServerItemWidgetTriggered() // TODO: connect } -void MainWindow::RunDiscovery() +void MainWindow::UpdateDiscoveryEnabled() { - qDebug() << "TODO: RunDiscovery()"; + bool enabled = discover_action->isChecked(); + settings->SetDiscoveryEnabled(enabled); + discovery_manager.SetActive(enabled); } void MainWindow::ShowSettings() diff --git a/gui/src/settings.cpp b/gui/src/settings.cpp new file mode 100644 index 0000000..527abb5 --- /dev/null +++ b/gui/src/settings.cpp @@ -0,0 +1,22 @@ +/* + * This file is part of Chiaki. + * + * Chiaki is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Chiaki is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Chiaki. If not, see . + */ + +#include + +Settings::Settings() +{ +}