diff --git a/internal/platform/implementation/linux/device_info.cc b/internal/platform/implementation/linux/device_info.cc index 80fa2c30..6c398448 100644 --- a/internal/platform/implementation/linux/device_info.cc +++ b/internal/platform/implementation/linux/device_info.cc @@ -1,10 +1,13 @@ +#include #include #include #include #include #include + #include +#include #include "internal/platform/implementation/device_info.h" #include "internal/platform/implementation/linux/device_info.h" @@ -18,6 +21,10 @@ const char *HOSTNAME_DEST = "org.freedesktop.hostname1"; const char *HOSTNAME_PATH = "/org/freedesktop/hostname1"; const char *HOSTNAME_INTERFACE = "org.freedesktop.hostname1"; +const char *LOGIN_DEST = "org.freedesktop.login1"; +const char *LOGIN_PATH = "/org/freedesktop/login1/session/_"; +const char *LOGIN_INTERFACE = "org.freedesktop.login1.Session"; + std::optional DeviceInfo::GetOsDeviceName() const { sd_bus *bus = nullptr; if (sd_bus_default_system(&bus) < 0) { @@ -117,5 +124,88 @@ std::optional DeviceInfo::GetProfileUserName() const { char *name = strtok(pwd->pw_gecos, ","); return std::string(name); } + +std::optional DeviceInfo::GetDownloadPath() const { + char *dir = getenv("XDG_DOWNLOAD_DIR"); + if (dir == NULL) { + std::filesystem::path home_path(std::string(getenv("HOME"))); + return home_path / "Desktop"; + } + return std::filesystem::path(std::string(dir)); +} + +std::optional DeviceInfo::GetLocalAppDataPath() const { + char *dir = getenv("XDG_STATE_HOME"); + if (dir == NULL) { + return std::filesystem::path("/tmp"); + } + return std::filesystem::path(std::string(dir)) / "com.github.google.nearby"; +} + +std::optional DeviceInfo::GetTemporaryPath() const { + char *dir = getenv("XDG_CACHE_HOME"); + if (dir == NULL) { + return std::filesystem::path("/tmp"); + } + return std::filesystem::path(std::string(dir)) / "com.github.google.nearby"; +} + +std::optional DeviceInfo::GetLogPath() const { + char *dir = getenv("XDG_STATE_HOME"); + if (dir == NULL) { + return std::filesystem::path("/tmp"); + } + return std::filesystem::path(std::string(dir)) / "com.github.google.nearby" / + "logs"; +} + +std::optional DeviceInfo::GetCrashDumpPath() const { + char *dir = getenv("XDG_STATE_HOME"); + if (dir == NULL) { + return std::filesystem::path("/tmp"); + } + return std::filesystem::path(std::string(dir)) / "com.github.google.nearby" / + "crash"; +} + +bool DeviceInfo::IsScreenLocked() const { + char *session = nullptr; + if (sd_pid_get_session(getpid(), &session) < 0) { + NEARBY_LOGS(ERROR) << __func__ + << ": Error getting session for current user"; + return false; + } + + sd_bus *bus; + if (sd_bus_default_system(&bus) < 0) { + NEARBY_LOGS(ERROR) << __func__ << ": Error connecting to systemd bus."; + free(session); + return false; + } + + std::string session_path(LOGIN_PATH); + session_path += session; + + free(session); + + sd_bus_error err = SD_BUS_ERROR_NULL; + bool locked; + + if (sd_bus_get_property_trivial(bus, LOGIN_DEST, session_path.c_str(), + LOGIN_INTERFACE, "LockedHint", &err, 'b', + &locked) < 0) { + + NEARBY_LOGS(ERROR) + << __func__ + << ": Error getting LockedState from org.freedesktop.login1: " + << err.message; + locked = false; + } + + sd_bus_error_free(&err); + sd_bus_unref(bus); + + return locked; +} } // namespace linux } // namespace nearby diff --git a/internal/platform/implementation/linux/device_info.h b/internal/platform/implementation/linux/device_info.h index 43cef077..3d001486 100644 --- a/internal/platform/implementation/linux/device_info.h +++ b/internal/platform/implementation/linux/device_info.h @@ -32,16 +32,20 @@ public: std::optional GetDownloadPath() const override; std::optional GetLocalAppDataPath() const override; - std::optional GetCommonAppDataPath() const override; + std::optional GetCommonAppDataPath() const override { + return std::nullopt; + }; std::optional GetTemporaryPath() const override; std::optional GetLogPath() const override; std::optional GetCrashDumpPath() const override; bool IsScreenLocked() const override; + // TODO: Implement listening to logind for changes to LockedState. void RegisterScreenLockedListener( absl::string_view listener_name, - std::function callback) override; - void UnregisterScreenLockedListener(absl::string_view listener_name) override; + std::function callback) override{}; + void + UnregisterScreenLockedListener(absl::string_view listener_name) override{}; sd_bus *system_bus; };