diff --git a/CMakeLists.txt b/CMakeLists.txt index 2a93ff3e..18d4d49d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -173,6 +173,8 @@ if (WIVRN_BUILD_SERVER) find_package(CLI11 REQUIRED) pkg_check_modules(glib2 REQUIRED IMPORTED_TARGET glib-2.0 gio-2.0 gio-unix-2.0) + + pkg_check_modules(libnotify REQUIRED IMPORTED_TARGET libnotify) endif() if (WIVRN_BUILD_DASHBOARD) diff --git a/dashboard/main_window.cpp b/dashboard/main_window.cpp index 625766e1..ed834796 100644 --- a/dashboard/main_window.cpp +++ b/dashboard/main_window.cpp @@ -640,22 +640,12 @@ void main_window::on_server_start_timeout() void main_window::on_action_settings() { - assert(not settings_window); - - settings_window = new settings(server_interface); - connect(settings_window, &QDialog::finished, this, [&]() { - settings_window->deleteLater(); - settings_window = nullptr; - }); - settings_window->exec(); + settings{server_interface}.exec(); } void main_window::on_action_wizard() { - assert(not wizard_window); - - wizard_window = new wizard; - wizard_window->exec(); + wizard{}.exec(); } void main_window::on_action_usb(const std::string & serial) diff --git a/dashboard/main_window.h b/dashboard/main_window.h index 00f1ed2c..45eeb158 100644 --- a/dashboard/main_window.h +++ b/dashboard/main_window.h @@ -49,9 +49,6 @@ class main_window : public QMainWindow Ui::MainWindow * ui = nullptr; wivrn_server * server_interface = nullptr; - settings * settings_window = nullptr; - wizard * wizard_window = nullptr; - QIcon icon{":/images/wivrn.png"}; QSystemTrayIcon systray{icon}; diff --git a/dashboard/wivrn_server.cpp b/dashboard/wivrn_server.cpp index bd48c167..732ec869 100644 --- a/dashboard/wivrn_server.cpp +++ b/dashboard/wivrn_server.cpp @@ -107,6 +107,38 @@ void wivrn_server::on_server_properties_changed(const QString & interface_name, pinChanged(m_pin = changed_properties["Pin"].toString()); } + if (changed_properties.contains("KnownKeys")) + { + m_knownKeys.clear(); + + const auto keys = qvariant_cast(changed_properties["KnownKeys"]); + keys.beginArray(); + + while (not keys.atEnd()) + { + keys.beginStructure(); + QString public_key; + QString name; + keys >> name >> public_key; + keys.endStructure(); + + m_knownKeys.push_back(HeadsetKey{public_key, name}); + } + keys.endArray(); + + knownKeysChanged(m_knownKeys); + } + + if (changed_properties.contains("EnrollEnabled")) + { + enrollEnabledChanged(m_isEnrollEnabled = changed_properties["EnrollEnabled"].toBool()); + } + + if (changed_properties.contains("EncryptionEnabled")) + { + encryptionEnabledChanged(m_isEncryptionEnabled = changed_properties["EncryptionEnabled"].toBool()); + } + if (changed_properties.contains("RecommendedEyeSize")) { const auto arg = qvariant_cast(changed_properties["RecommendedEyeSize"]); @@ -210,6 +242,26 @@ void wivrn_server::setJsonConfiguration(QString new_configuration) server_interface->setJsonConfiguration(m_jsonConfiguration = new_configuration); } +void wivrn_server::revoke_key(QString public_key) +{ + server_interface->RevokeKey(public_key); +} + +void wivrn_server::rename_key(QString public_key, QString name) +{ + server_interface->RenameKey(public_key, name); +} + +QString wivrn_server::enroll_headset(int timeout_secs) +{ + return server_interface->EnrollHeadset(timeout_secs).value(); +} + +void wivrn_server::disable_enroll_headset() +{ + server_interface->DisableEnrollHeadset(); +} + QString wivrn_server::hostname() { static auto _hostname = []() -> QString { diff --git a/dashboard/wivrn_server.h b/dashboard/wivrn_server.h index ca9d1f1d..7b637ce8 100644 --- a/dashboard/wivrn_server.h +++ b/dashboard/wivrn_server.h @@ -38,6 +38,13 @@ class wivrn_server : public QObject QDBusPendingCallWatcher * get_all_properties_call_watcher = nullptr; public: + struct HeadsetKey + { + QString public_key; + QString name; + }; + using HeadsetKeys = std::vector; + wivrn_server(QObject * parent = nullptr); wivrn_server(const wivrn_server &) = delete; wivrn_server & operator=(const wivrn_server &) = delete; @@ -47,7 +54,16 @@ public: Q_PROPERTY(bool serverRunning READ isServerRunning NOTIFY serverRunningChanged) Q_PROPERTY(bool headsetConnected READ isHeadsetConnected NOTIFY headsetConnectedChanged) Q_PROPERTY(QString jsonConfiguration READ jsonConfiguration WRITE setJsonConfiguration) + + // Authentication Q_PROPERTY(QString pin READ pin NOTIFY pinChanged) + Q_PROPERTY(HeadsetKeys knownKeys READ knownKeys NOTIFY knownKeysChanged) + Q_PROPERTY(bool enrollEnabled READ isEnrollEnabled NOTIFY enrollEnabledChanged) + Q_PROPERTY(bool encryptionEnabled READ isEncryptionEnabled NOTIFY encryptionEnabledChanged) + void revoke_key(QString public_key); + void rename_key(QString public_key, QString name); + QString enroll_headset(int timeout_secs = 120); + void disable_enroll_headset(); // Headset information, valid only if HeadsetConnected is true Q_PROPERTY(QSize recommendedEyeSize READ recommendedEyeSize NOTIFY recommendedEyeSizeChanged) @@ -89,6 +105,21 @@ public: return m_pin; } + HeadsetKeys knownKeys() const + { + return m_knownKeys; + } + + bool isEnrollEnabled() const + { + return m_isEnrollEnabled; + } + + bool isEncryptionEnabled() const + { + return m_isEncryptionEnabled; + } + QSize recommendedEyeSize() const { return m_recommendedEyeSize; @@ -169,7 +200,11 @@ private: bool m_serverRunning{}; bool m_headsetConnected{}; QString m_jsonConfiguration{}; + QString m_pin{}; + HeadsetKeys m_knownKeys; + bool m_isEnrollEnabled{}; + bool m_isEncryptionEnabled{}; QSize m_recommendedEyeSize{}; std::vector m_availableRefreshRates{}; @@ -189,6 +224,9 @@ Q_SIGNALS: void serverRunningChanged(bool); void headsetConnectedChanged(bool); void pinChanged(QString); + void knownKeysChanged(HeadsetKeys); + void enrollEnabledChanged(bool); + void encryptionEnabledChanged(bool); void recommendedEyeSizeChanged(QSize); void availableRefreshRatesChanged(const std::vector &); diff --git a/dashboard/wizard.cpp b/dashboard/wizard.cpp index ad8269d5..59974837 100644 --- a/dashboard/wizard.cpp +++ b/dashboard/wizard.cpp @@ -125,10 +125,8 @@ wizard::wizard() : }); ui->label_steam_command->setText(server->steamCommand()); - connect(server, &wivrn_server::pinChanged, this, [this](QString value) { - ui->label_pin->setText(value); - }); - ui->label_pin->setText(server->pin()); + connect(server, &wivrn_server::pinChanged, this, &wizard::on_pin_changed); + on_pin_changed(server->pin()); setPixmap(WatermarkPixmap, QPixmap(":/images/wivrn.svg")); for (const headset & i: headsets_info) @@ -404,6 +402,10 @@ void wizard::on_page_changed(int id) button(WizardButton::CustomButton1)->setText(tr("Skip")); setButtonLayout({QWizard::Stretch, QWizard::BackButton, QWizard::NextButton, QWizard::CustomButton1, QWizard::CancelButton}); on_headset_connected_changed(server->isHeadsetConnected()); + + if (not server->isEnrollEnabled()) + server->enroll_headset(-1); + return; case wizard_page::start_game: @@ -456,6 +458,8 @@ void wizard::retranslate() wizard::~wizard() { + server->disable_enroll_headset(); + delete ui; ui = nullptr; } @@ -693,4 +697,20 @@ void wizard::on_headset_connected_changed(bool connected) } } +void wizard::on_pin_changed(QString pin) +{ + if (pin == "") + { + ui->label_pin_1->setVisible(false); + ui->label_pin_2->setVisible(false); + } + else + { + ui->label_pin_1->setVisible(true); + ui->label_pin_2->setVisible(true); + } + + ui->label_pin_2->setText(pin); +} + #include "moc_wizard.cpp" diff --git a/dashboard/wizard.h b/dashboard/wizard.h index f2994f68..f515edb3 100644 --- a/dashboard/wizard.h +++ b/dashboard/wizard.h @@ -100,4 +100,6 @@ public: void update_welcome_page(); void on_headset_connected_changed(bool); void on_custom_button_clicked(int which); + + void on_pin_changed(QString pin); }; diff --git a/dashboard/wizard.ui b/dashboard/wizard.ui index 139de087..2b7734b9 100644 --- a/dashboard/wizard.ui +++ b/dashboard/wizard.ui @@ -441,14 +441,14 @@ - + Use the following PIN to connect your headset: - + 14 diff --git a/dbus/io.github.wivrn.Server.xml b/dbus/io.github.wivrn.Server.xml index 32d381e3..40f688c2 100644 --- a/dbus/io.github.wivrn.Server.xml +++ b/dbus/io.github.wivrn.Server.xml @@ -36,8 +36,10 @@ + + diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index 19fe9519..4cb12554 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -174,6 +174,7 @@ target_include_directories(wivrn-server-dbus PUBLIC ${CMAKE_CURRENT_BINARY_DIR}) add_dependencies(wivrn-server-dbus wivrn-server-dbus-codegen) target_link_libraries(wivrn-server-dbus PUBLIC PkgConfig::glib2) target_link_libraries(wivrn-server PUBLIC wivrn-server-dbus) +target_link_libraries(wivrn-server PUBLIC PkgConfig::libnotify) include(CompileGLSL) diff --git a/server/main.cpp b/server/main.cpp index 52fc7023..a5d130f3 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -29,10 +29,11 @@ #include #include -#include +#include // IWYU pragma: keep #include #include #include +#include #include #include #include @@ -45,6 +46,7 @@ #include #include #include +#include #include #include @@ -209,6 +211,7 @@ guint listener_watch; wivrn_connection::encryption_state enc_state = wivrn_connection::encryption_state::enabled; guint enroll_timeout; std::string pin; +NotifyNotification * pin_notification; WivrnServer * dbus_server; @@ -530,6 +533,14 @@ gboolean control_received(gint fd, GIOCondition condition, gpointer user_data) void set_encryption_state(wivrn_connection::encryption_state new_enc_state) { + // TODO translate the notifications + if (pin_notification) + { + notify_notification_close(pin_notification, nullptr); + g_object_unref(G_OBJECT(pin_notification)); + pin_notification = nullptr; + } + switch (new_enc_state) { case wivrn_connection::encryption_state::disabled: @@ -537,6 +548,9 @@ void set_encryption_state(wivrn_connection::encryption_state new_enc_state) std::cerr << "Encryption is disabled" << std::endl; wivrn_server_set_enroll_enabled(dbus_server, false); wivrn_server_set_encryption_enabled(dbus_server, false); + + notify_notification_set_timeout(pin_notification, NOTIFY_EXPIRES_NEVER); + notify_notification_show(pin_notification, nullptr); break; case wivrn_connection::encryption_state::enabled: @@ -558,7 +572,11 @@ void set_encryption_state(wivrn_connection::encryption_state new_enc_state) wivrn_server_set_enroll_enabled(dbus_server, true); wivrn_server_set_encryption_enabled(dbus_server, true); - // TODO desktop notification + // Desktop notification + pin_notification = notify_notification_new("PIN", pin.c_str(), "dialog-password"); + notify_notification_set_timeout(pin_notification, NOTIFY_EXPIRES_NEVER); + // TODO: notify_notification_set_image_from_pixbuf + notify_notification_show(pin_notification, nullptr); break; } @@ -628,15 +646,29 @@ gboolean on_handle_enroll_headset(WivrnServer * skeleton, GDBusMethodInvocation if (enroll_timeout) g_source_remove(enroll_timeout); - enroll_timeout = g_timeout_add(120'000, [](void *) { - enroll_timeout = 0; - set_encryption_state(wivrn_connection::encryption_state::enabled); - return G_SOURCE_REMOVE; }, nullptr); + int timeout_secs; + GVariant * args = g_dbus_method_invocation_get_parameters(invocation); + g_variant_get_child(args, 0, "i", &timeout_secs); + + if (timeout_secs > 0) + { + enroll_timeout = g_timeout_add(timeout_secs * 1000, [](void *) { + enroll_timeout = 0; + set_encryption_state(wivrn_connection::encryption_state::enabled); + return G_SOURCE_REMOVE; }, nullptr); + } g_dbus_method_invocation_return_value(invocation, g_variant_new("(s)", pin.c_str())); return G_SOURCE_CONTINUE; } +gboolean on_handle_disable_enroll_headset(WivrnServer * skeleton, GDBusMethodInvocation * invocation, gpointer user_data) +{ + set_encryption_state(wivrn_connection::encryption_state::enabled); + g_dbus_method_invocation_return_value(invocation, nullptr); + return G_SOURCE_CONTINUE; +} + void on_json_configuration(WivrnServer * server, const GParamSpec * pspec, gpointer data) { const char * json = wivrn_server_get_json_configuration(server); @@ -746,11 +778,18 @@ void on_name_acquired(GDBusConnection * connection, const gchar * name, gpointer NULL); if (enc_state != wivrn_connection::encryption_state::disabled) + { g_signal_connect(dbus_server, "handle-enroll-headset", G_CALLBACK(on_handle_enroll_headset), NULL); + g_signal_connect(dbus_server, + "handle-disable-enroll-headset", + G_CALLBACK(on_handle_disable_enroll_headset), + NULL); + } + wivrn_server_set_steam_command(dbus_server, steam_command().c_str()); on_headset_info_packet({}); @@ -844,10 +883,14 @@ int inner_main(int argc, char * argv[], bool show_instructions) nullptr, nullptr); + // Initialize libnotify + notify_init("WiVRn"); + // Main loop g_main_loop_run(main_loop); // Cleanup + notify_uninit(); runtime_setter.reset(); stop_publishing(); stop_listening();