From 57a0cf1d4b3863b6ca66c5b0e3c7aa53d95dc64b Mon Sep 17 00:00:00 2001 From: Guillaume Meunier Date: Mon, 3 Feb 2025 20:02:10 +0100 Subject: [PATCH] Display the last connection time in dashboard/wivrnctl --- dashboard/qml/HeadsetsPage.qml | 31 ++++++++++-- dashboard/wivrn_server.cpp | 9 +++- dashboard/wivrn_server.h | 22 ++++++++ dbus/io.github.wivrn.Server.xml | 6 +-- locale/fr/wivrn-dashboard.po | 80 ++++++++++++++++++++---------- server/driver/configuration.cpp | 75 ++++++++++++++++++++++++---- server/driver/configuration.h | 3 ++ server/driver/wivrn_connection.cpp | 2 + server/main.cpp | 10 ++-- tools/wivrnctl/main.cpp | 71 ++++++++++++++++++++++---- 10 files changed, 252 insertions(+), 57 deletions(-) diff --git a/dashboard/qml/HeadsetsPage.qml b/dashboard/qml/HeadsetsPage.qml index d7fa412d..6a71e708 100644 --- a/dashboard/qml/HeadsetsPage.qml +++ b/dashboard/qml/HeadsetsPage.qml @@ -1,5 +1,5 @@ pragma ComponentBehavior: Bound - +import QtQml import QtQuick import QtQuick.Layouts import QtQuick.Controls as Controls @@ -13,6 +13,28 @@ Kirigami.ScrollablePage { // flickable.interactive: false + property date now: new Date() + + Timer { + interval: 1000 + running: true + repeat: true + onTriggered: headsets.now = new Date(); + } + + function fuzzy_relative_date(then) { + var time_difference = (headsets.now.getTime() - then.getTime()) / 1000; + + if (time_difference < 60) + return i18np("Last connection: %1 second ago", "Last connection: %1 seconds ago", Math.round(time_difference)); + if (time_difference < 3600) + return i18np("Last connection: %1 minute ago", "Last connection: %1 minutes ago", Math.round(time_difference / 60)); + if (time_difference < 86400) + return i18np("Last connection: %1 hour ago", "Last connection: %1 hours ago", Math.round(time_difference / 3600)); + + return i18np("Last connection: %1 day ago", "Last connection: %1 days ago", Math.round(time_difference / 86400)); + } + Kirigami.PromptDialog { id: rename_headset title: i18n("Rename headset") @@ -32,7 +54,7 @@ Kirigami.ScrollablePage { Kirigami.Action { text: i18n("Cancel") icon.name: "dialog-cancel" - onTriggered: rename_headset.close(); + onTriggered: rename_headset.close() } ] ColumnLayout { @@ -62,6 +84,8 @@ Kirigami.ScrollablePage { id: headset required property string name required property string publicKey + required property bool hasLastConnection + required property date lastConnection contentItem: GridLayout { id: delegate_layout @@ -83,8 +107,7 @@ Kirigami.ScrollablePage { Layout.column: 0 Layout.row: 1 wrapMode: Text.WordWrap - // text: "Last connection: unknown" - text: "" + text: headset.hasLastConnection ? headsets.fuzzy_relative_date(headset.lastConnection) : "" } Controls.Button { diff --git a/dashboard/wivrn_server.cpp b/dashboard/wivrn_server.cpp index be041f7d..d2a666f6 100644 --- a/dashboard/wivrn_server.cpp +++ b/dashboard/wivrn_server.cpp @@ -348,10 +348,15 @@ void wivrn_server::on_server_properties_changed(const QString & interface_name, keys.beginStructure(); QString public_key; QString name; - keys >> name >> public_key; + qlonglong last_connection_timestamp; + keys >> name >> public_key >> last_connection_timestamp; keys.endStructure(); - m_knownKeys.push_back(headset{name, public_key}); + headset h{name, public_key}; + if (last_connection_timestamp) + h.setLastConnection(QDateTime::fromSecsSinceEpoch(last_connection_timestamp)); + + m_knownKeys.push_back(h); } keys.endArray(); diff --git a/dashboard/wivrn_server.h b/dashboard/wivrn_server.h index 7f01a7ba..57833697 100644 --- a/dashboard/wivrn_server.h +++ b/dashboard/wivrn_server.h @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -39,9 +40,12 @@ class headset QML_ELEMENT Q_PROPERTY(QString name READ name WRITE setName) Q_PROPERTY(QString publicKey READ publicKey WRITE setPublicKey) + Q_PROPERTY(bool hasLastConnection READ hasLastConnection) + Q_PROPERTY(QDateTime lastConnection READ lastConnection WRITE setLastConnection) QString m_name; QString m_publicKey; + QDateTime m_lastConnection; public: headset() = default; @@ -49,6 +53,9 @@ public: headset(headset &&) = default; headset & operator=(const headset &) = default; headset & operator=(headset &&) = default; + headset(QString name, QString publicKey, QDateTime lastConnection) : + m_name(name), m_publicKey(publicKey), m_lastConnection(lastConnection) {} + headset(QString name, QString publicKey) : m_name(name), m_publicKey(publicKey) {} @@ -62,6 +69,16 @@ public: return m_publicKey; } + QDateTime lastConnection() const + { + return m_lastConnection; + } + + bool hasLastConnection() const + { + return !m_lastConnection.isNull(); + } + void setName(QString value) { m_name = value; @@ -71,6 +88,11 @@ public: { m_publicKey = value; } + + void setLastConnection(QDateTime value) + { + m_lastConnection = value; + } }; class wivrn_server : public QObject diff --git a/dbus/io.github.wivrn.Server.xml b/dbus/io.github.wivrn.Server.xml index 10a09911..886746d1 100644 --- a/dbus/io.github.wivrn.Server.xml +++ b/dbus/io.github.wivrn.Server.xml @@ -67,9 +67,9 @@ - - - + + + diff --git a/locale/fr/wivrn-dashboard.po b/locale/fr/wivrn-dashboard.po index d4d0a42f..08bec3c7 100644 --- a/locale/fr/wivrn-dashboard.po +++ b/locale/fr/wivrn-dashboard.po @@ -2,55 +2,56 @@ msgid "" msgstr "" "Project-Id-Version: WiVRn-dashboard\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2025-02-02 23:45+0100\n" +"POT-Creation-Date: 2025-02-03 20:03+0100\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: Guillaume Meunier \n" "Language: French\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=(n > 1);\n" -#: dashboard/apk_installer.cpp:175 +#: dashboard/apk_installer.cpp:167 #, kde-format msgid "Downloading APK: %1 MB / %2 MB" msgstr "Téléchargement de l'APK : %1 MB / %2 MB" -#: dashboard/apk_installer.cpp:177 +#: dashboard/apk_installer.cpp:169 #, kde-format msgid "Downloading APK: %1 MB" msgstr "Téléchargement de l'APK : %1 MB" -#: dashboard/apk_installer.cpp:186 +#: dashboard/apk_installer.cpp:178 #, kde-format msgid "Download cancelled" msgstr "Téléchargement annulé" -#: dashboard/apk_installer.cpp:202 +#: dashboard/apk_installer.cpp:194 #, kde-format msgid "Cannot download APK: %1" msgstr "Impossible de télécharger l'APK : %1" -#: dashboard/apk_installer.cpp:225 +#: dashboard/apk_installer.cpp:217 #, kde-format msgid "Installing APK" msgstr "Installation de l'APK" -#: dashboard/apk_installer.cpp:229 +#: dashboard/apk_installer.cpp:221 #, kde-format msgid "adb failed to start: %1" msgstr "adb n'a pas pu démarrer : %1" -#: dashboard/apk_installer.cpp:239 +#: dashboard/apk_installer.cpp:231 #, kde-format msgid "adb exited abnormally: %1" msgstr "adb a quitté de façon anormale : %1" -#: dashboard/apk_installer.cpp:247 +#: dashboard/apk_installer.cpp:239 #, kde-format msgid "The 'adb install' command failed: exit code %1" msgstr "La commande 'adb install' a échoué : code de retour %1" -#: dashboard/apk_installer.cpp:252 +#: dashboard/apk_installer.cpp:244 #, kde-format msgid "Installation successful" msgstr "Installation réussie" @@ -93,11 +94,12 @@ msgid "" "Follow the instructions in the documentation to build your own " "client." -msgstr "Suivez les instructions dans la documentation pour construire votre " -"propre client." +msgstr "" +"Suivez les instructions dans la documentation pour " +"construire votre propre client." -#: dashboard/qml/ApkInstallPage.qml:65 dashboard/qml/HeadsetsPage.qml:160 +#: dashboard/qml/ApkInstallPage.qml:65 dashboard/qml/HeadsetsPage.qml:183 #: dashboard/qml/HeadsetStatsPage.qml:36 #, kde-format msgctxt "go back to the home page" @@ -129,61 +131,89 @@ msgstr "Installer WiVRn" msgid "Headsets" msgstr "Casques" -#: dashboard/qml/HeadsetsPage.qml:18 +#: dashboard/qml/HeadsetsPage.qml:29 +#, kde-format +msgid "Last connection: %1 second ago" +msgid_plural "Last connection: %1 seconds ago" +msgstr[0] "Dernière connexion : il a y %1 seconde" +msgstr[1] "Dernière connexion : il a y %1 secondes" + +#: dashboard/qml/HeadsetsPage.qml:31 +#, kde-format +msgid "Last connection: %1 minute ago" +msgid_plural "Last connection: %1 minutes ago" +msgstr[0] "Dernière connexion : il a y %1 minute" +msgstr[1] "Dernière connexion : il a y %1 minutes" + +#: dashboard/qml/HeadsetsPage.qml:33 +#, kde-format +msgid "Last connection: %1 hour ago" +msgid_plural "Last connection: %1 hours ago" +msgstr[0] "Dernière connexion : il a y %1 heure" +msgstr[1] "Dernière connexion : il a y %1 heures" + +#: dashboard/qml/HeadsetsPage.qml:35 +#, kde-format +msgid "Last connection: %1 day ago" +msgid_plural "Last connection: %1 days ago" +msgstr[0] "Dernière connexion : il a y %1 jour" +msgstr[1] "Dernière connexion : il a y %1 jours" + +#: dashboard/qml/HeadsetsPage.qml:40 #, kde-format msgid "Rename headset" msgstr "Renommer le casque" -#: dashboard/qml/HeadsetsPage.qml:26 dashboard/qml/HeadsetsPage.qml:93 +#: dashboard/qml/HeadsetsPage.qml:48 dashboard/qml/HeadsetsPage.qml:116 #, kde-format msgid "Rename" msgstr "Renommer" -#: dashboard/qml/HeadsetsPage.qml:33 dashboard/qml/WizardPage.qml:38 +#: dashboard/qml/HeadsetsPage.qml:55 dashboard/qml/WizardPage.qml:38 #: dashboard/qml/WizardPage.qml:88 dashboard/qml/WizardPage.qml:263 #: dashboard/qml/WizardPage.qml:312 dashboard/qml/WizardPage.qml:396 #, kde-format msgid "Cancel" msgstr "Annuler" -#: dashboard/qml/HeadsetsPage.qml:42 +#: dashboard/qml/HeadsetsPage.qml:64 #, kde-format msgid "Headset name…" msgstr "Nom du casque…" -#: dashboard/qml/HeadsetsPage.qml:105 +#: dashboard/qml/HeadsetsPage.qml:128 #, kde-format msgid "Remove" msgstr "Enlever" -#: dashboard/qml/HeadsetsPage.qml:120 +#: dashboard/qml/HeadsetsPage.qml:143 #, kde-format msgid "Pairing enabled, PIN: %1" msgstr "Appairage activé, PIN : %1 " -#: dashboard/qml/HeadsetsPage.qml:125 +#: dashboard/qml/HeadsetsPage.qml:148 #, kde-format msgid "Disable pairing" msgstr "Désactiver l'appairage" -#: dashboard/qml/HeadsetsPage.qml:143 +#: dashboard/qml/HeadsetsPage.qml:166 #, kde-format msgid "Start the WiVRn app on your headset and use the PIN: %1" msgstr "Démarrez l'appli WiVRn sur votre casque et utilisez le PIN : %1" -#: dashboard/qml/HeadsetsPage.qml:143 +#: dashboard/qml/HeadsetsPage.qml:166 #, kde-format msgid "Enable pairing to allow your headset to connect to this computer" msgstr "" "Activez l'appairage pour autoriser votre casque à se connecter à cet " "ordinateur" -#: dashboard/qml/HeadsetsPage.qml:148 +#: dashboard/qml/HeadsetsPage.qml:171 #, kde-format msgid "Enable pairing" msgstr "Activer l'appairage" -#: dashboard/qml/HeadsetsPage.qml:168 +#: dashboard/qml/HeadsetsPage.qml:191 #, kde-format msgid "Pair a new headset" msgstr "Appairer un nouveau casque" diff --git a/server/driver/configuration.cpp b/server/driver/configuration.cpp index ce9d3fde..5fe1287d 100644 --- a/server/driver/configuration.cpp +++ b/server/driver/configuration.cpp @@ -20,6 +20,8 @@ #define JSON_DISABLE_ENUM_SERIALIZATION 1 #include +#include +#include #include #include #include @@ -204,6 +206,36 @@ std::string server_cookie() } } +std::string to_iso8601(std::chrono::system_clock::time_point timestamp) +{ + // Truncate to an integer number of seconds to be parsable by strptime + auto t = std::chrono::time_point_cast(timestamp); + +#if __cpp_lib_chrono >= 201907L + return std::format("{:%FT%H:%M:%S%z}", std::chrono::zoned_time{std::chrono::current_zone(), t}); +#else + return std::format("{:%FT%H:%M:%S%z}", t); +#endif +} + +template +std::string to_iso8601(std::chrono::zoned_time timestamp) +{ + return std::format("{:%FT%H:%M:%S%z}", timestamp); +} + +std::optional from_iso8601(const std::string & timestamp) +{ + tm t; + if (strptime(timestamp.c_str(), "%FT%H:%M:%S%z", &t) == nullptr) + return std::nullopt; + + // Convert from local time to UTC + t.tm_sec += t.tm_gmtoff; + + return std::chrono::system_clock::from_time_t(mktime(&t)); +} + std::vector known_keys() { if (not std::filesystem::exists(known_keys_file)) @@ -217,7 +249,15 @@ std::vector known_keys() for (const auto & key: json) { - keys.emplace_back(key["key"], key["name"]); + headset_key k{ + .public_key = key["key"], + .name = key["name"], + }; + + if (key.contains("last_connection")) + k.last_connection = from_iso8601(key["last_connection"]); + + keys.push_back(k); } return keys; @@ -235,10 +275,13 @@ static void save_keys(const std::vector & keys) for (const auto & key: keys) { - nlohmann::json key_json; - key_json["key"] = key.public_key; - key_json["name"] = key.name; - json.push_back(key_json); + nlohmann::json json_key; + json_key["key"] = key.public_key; + json_key["name"] = key.name; + if (key.last_connection) + json_key["last_connection"] = to_iso8601(*key.last_connection); + + json.push_back(json_key); } std::string json_str = json.dump(); @@ -260,6 +303,7 @@ void add_known_key(headset_key key) { std::vector keys = known_keys(); + key.last_connection = std::chrono::system_clock::now(); if (key.name == "") key.name = "Unknown headset"; @@ -286,13 +330,24 @@ void remove_known_key(const std::string & key) void rename_known_key(headset_key key) { std::vector keys = known_keys(); - for (headset_key & k: keys) - { - if (k.public_key == key.public_key) - k.name = key.name; - } + + auto key_iter = std::ranges::find(keys, key.public_key, &headset_key::public_key); + if (key_iter != keys.end()) + key_iter->name = key.name; save_keys(keys); } +void update_last_connection_timestamp(const std::string & key) +{ + std::vector keys = known_keys(); + + auto key_iter = std::ranges::find(keys, key, &headset_key::public_key); + if (key_iter != keys.end()) + { + key_iter->last_connection = std::chrono::system_clock::now(); + save_keys(keys); + } +} + } // namespace wivrn diff --git a/server/driver/configuration.h b/server/driver/configuration.h index 5a7ededf..8da552ea 100644 --- a/server/driver/configuration.h +++ b/server/driver/configuration.h @@ -20,6 +20,7 @@ #pragma once #include +#include #include #include #include @@ -72,11 +73,13 @@ struct headset_key { std::string public_key; std::string name; + std::optional last_connection; }; std::vector known_keys(); void add_known_key(headset_key key); void remove_known_key(const std::string & key); void rename_known_key(headset_key key); +void update_last_connection_timestamp(const std::string & key); } // namespace wivrn diff --git a/server/driver/wivrn_connection.cpp b/server/driver/wivrn_connection.cpp index 268c66ad..d04be0b3 100644 --- a/server/driver/wivrn_connection.cpp +++ b/server/driver/wivrn_connection.cpp @@ -275,6 +275,8 @@ void wivrn::wivrn_connection::init(std::stop_token stop_token, std::function tick) diff --git a/server/main.cpp b/server/main.cpp index da57cc2a..f0e0b624 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -735,12 +735,16 @@ void on_json_configuration(WivrnServer * server, const GParamSpec * pspec, gpoin void expose_known_keys_on_dbus() { - GVariantBuilder * builder = g_variant_builder_new(G_VARIANT_TYPE("a(ss)")); + GVariantBuilder * builder = g_variant_builder_new(G_VARIANT_TYPE("a(ssx)")); for (const auto & i: known_keys()) { - g_variant_builder_add(builder, "(ss)", i.name.c_str(), i.public_key.c_str()); + g_variant_builder_add(builder, + "(ssx)", + i.name.c_str(), + i.public_key.c_str(), + i.last_connection ? std::chrono::duration_cast((*i.last_connection).time_since_epoch()).count() : 0); } - GVariant * value = g_variant_new("a(ss)", builder); + GVariant * value = g_variant_new("a(ssx)", builder); g_variant_builder_unref(builder); wivrn_server_set_known_keys(dbus_server, value); } diff --git a/tools/wivrnctl/main.cpp b/tools/wivrnctl/main.cpp index 959781eb..296ad213 100644 --- a/tools/wivrnctl/main.cpp +++ b/tools/wivrnctl/main.cpp @@ -17,6 +17,7 @@ */ #include +#include #include #include #include @@ -34,6 +35,13 @@ struct deleter } }; +struct headset +{ + std::string name; + std::string public_key; + std::optional last_connection; +}; + using sd_bus_ptr = std::unique_ptr; using sd_bus_message_ptr = std::unique_ptr; @@ -135,28 +143,34 @@ void print_table(const std::array> & } } -std::vector> get_keys(const sd_bus_ptr & bus) +std::vector get_keys(const sd_bus_ptr & bus) { auto msg = get_property(bus, "KnownKeys", - "a(ss)"); + "a(ssx)"); - int ret = sd_bus_message_enter_container(msg.get(), 'a', "(ss)"); + int ret = sd_bus_message_enter_container(msg.get(), 'a', "(ssx)"); if (ret < 0) throw std::system_error(-ret, std::system_category(), "Failed to get paired headsets"); - std::vector> values; + std::vector values; while (true) { char * name; char * key; - int ret = sd_bus_message_read(msg.get(), "(ss)", &name, &key); + int64_t timestamp; + int ret = sd_bus_message_read(msg.get(), "(ssx)", &name, &key, ×tamp); if (ret == 0) break; if (ret < 0) throw std::system_error(-ret, std::system_category(), "Failed to get paired headset details"); - values.emplace_back(name, key); + std::optional last_connection; + + if (timestamp) + last_connection = std::chrono::system_clock::from_time_t(timestamp); + + values.emplace_back(name, key, last_connection); } return values; @@ -194,7 +208,37 @@ void unpair(size_t headset_id) if (headset_id < 1 or headset_id > values.size()) throw std::runtime_error(std::format("Invalid headset number: {}", headset_id)); - call_method(bus, "RevokeKey", "s", values.at(headset_id - 1).second.c_str()); + call_method(bus, "RevokeKey", "s", values.at(headset_id - 1).public_key.c_str()); +} + +template +auto member(T U::* x) +{ + return std::views::transform([x](const auto & y) { return y.*x; }); +} + +std::string relative_timestamp(std::optional t) +{ + static const auto now = std::chrono::system_clock::now(); + + if (!t) + return "Unknown"; + + auto how_long_ago = std::chrono::duration_cast(now - *t).count(); + + if (how_long_ago < 0) + return std::format("{}", *t); + + if (how_long_ago < 2 * 60) + return std::format("{} seconds ago", how_long_ago); + + if (how_long_ago < 2 * 3600) + return std::format("{:.0f} minutes ago", how_long_ago / 60.); + + if (how_long_ago < 2 * 86400) + return std::format("{:.0f} hours ago", how_long_ago / 3600.); + + return std::format("{:.0f} days ago", how_long_ago / 86400.); } void list_paired(bool show_keys) @@ -208,9 +252,16 @@ void list_paired(bool show_keys) else { if (show_keys) - print_table({"", "Headset name", "Public key"}, std::views::zip(std::views::iota(1), values | std::views::elements<0>, values | std::views::elements<1>)); + print_table({"", "Headset name", "Last connection", "Public key"}, + std::views::zip(std::views::iota(1), + values | member(&headset::name), + values | std::views::transform([](const headset & h) { return relative_timestamp(h.last_connection); }), + values | member(&headset::public_key))); else - print_table({"", "Headset name"}, std::views::zip(std::views::iota(1), values | std::views::elements<0>)); + print_table({"", "Headset name", "Last connection"}, + std::views::zip(std::views::iota(1), + values | member(&headset::name), + values | std::views::transform([](const headset & h) { return relative_timestamp(h.last_connection); }))); } } @@ -222,7 +273,7 @@ void rename(size_t headset_id, const std::string & headset_name) if (headset_id < 1 or headset_id > values.size()) throw std::runtime_error(std::format("Invalid headset number: {}", headset_id)); - call_method(bus, "RenameKey", "ss", values.at(headset_id - 1).second.c_str(), headset_name.c_str()); + call_method(bus, "RenameKey", "ss", values.at(headset_id - 1).public_key.c_str(), headset_name.c_str()); } void stop_server()