mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Initial sketch.
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
licenses(["notice"])
|
||||
|
||||
cc_library(
|
||||
name = "types",
|
||||
hdrs = [
|
||||
"device_info.h",
|
||||
],
|
||||
srcs = [
|
||||
"device_info.cc",
|
||||
],
|
||||
deps = [
|
||||
"//internal/platform/implementation:types",
|
||||
"//internal/platform:logging",
|
||||
"@com_google_absl//absl/strings",
|
||||
"@libsystemd//:lib",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
#include <cstring>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
#include <pwd.h>
|
||||
#include <sys/types.h>
|
||||
#include <systemd/sd-bus.h>
|
||||
|
||||
#include "internal/platform/implementation/device_info.h"
|
||||
#include "internal/platform/implementation/linux/device_info.h"
|
||||
#include "internal/platform/logging.h"
|
||||
|
||||
namespace nearby {
|
||||
|
||||
namespace linux {
|
||||
|
||||
const char *HOSTNAME_DEST = "org.freedesktop.hostname1";
|
||||
const char *HOSTNAME_PATH = "/org/freedesktop/hostname1";
|
||||
const char *HOSTNAME_INTERFACE = "org.freedesktop.hostname1";
|
||||
|
||||
std::optional<std::u16string> DeviceInfo::GetOsDeviceName() const {
|
||||
sd_bus *bus = nullptr;
|
||||
if (sd_bus_default_system(&bus) < 0) {
|
||||
NEARBY_LOGS(ERROR) << __func__ << ": Error connecting to systemd bus.";
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
sd_bus_error err = SD_BUS_ERROR_NULL;
|
||||
|
||||
char *hostname = nullptr;
|
||||
if (sd_bus_get_property_string(bus, HOSTNAME_DEST, HOSTNAME_PATH,
|
||||
HOSTNAME_INTERFACE, "PrettyHostname", &err,
|
||||
&hostname) < 0) {
|
||||
NEARBY_LOGS(ERROR)
|
||||
<< __func__
|
||||
<< ": Error getting PrettyHostname from org.freedesktop.hostname1: "
|
||||
<< err.message;
|
||||
}
|
||||
if (!hostname || hostname[0] == '\0') {
|
||||
int ret = sd_bus_get_property_string(bus, HOSTNAME_DEST, HOSTNAME_PATH,
|
||||
HOSTNAME_INTERFACE, "Hostname", &err,
|
||||
&hostname);
|
||||
if (ret < 0) {
|
||||
NEARBY_LOGS(ERROR)
|
||||
<< __func__
|
||||
<< ": Error getting Hostname from org.freedesktop.hostname1: "
|
||||
<< err.message;
|
||||
sd_bus_error_free(&err);
|
||||
sd_bus_unref(bus);
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
sd_bus_error_free(&err);
|
||||
sd_bus_unref(bus);
|
||||
|
||||
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
|
||||
|
||||
std::u16string name = convert.from_bytes(hostname);
|
||||
free(hostname);
|
||||
return name;
|
||||
}
|
||||
|
||||
api::DeviceInfo::DeviceType DeviceInfo::GetDeviceType() const {
|
||||
sd_bus *bus;
|
||||
if (sd_bus_default_system(&bus) < 0) {
|
||||
NEARBY_LOGS(ERROR) << __func__ << ": Error connecting to systemd bus.";
|
||||
return api::DeviceInfo::DeviceType::kUnknown;
|
||||
}
|
||||
sd_bus_error err = SD_BUS_ERROR_NULL;
|
||||
char *chasis = nullptr;
|
||||
|
||||
if (sd_bus_get_property_string(bus, HOSTNAME_DEST, HOSTNAME_PATH,
|
||||
HOSTNAME_INTERFACE, "Chasis", &err,
|
||||
&chasis) < 0) {
|
||||
NEARBY_LOGS(ERROR)
|
||||
<< __func__ << ": Error getting Chasis from org.freedesktop.hostname1: "
|
||||
<< err.message;
|
||||
sd_bus_error_free(&err);
|
||||
sd_bus_unref(bus);
|
||||
return api::DeviceInfo::DeviceType::kUnknown;
|
||||
}
|
||||
|
||||
sd_bus_unref(bus);
|
||||
|
||||
api::DeviceInfo::DeviceType device = api::DeviceInfo::DeviceType::kUnknown;
|
||||
|
||||
if (strcmp(chasis, "phone") == 0) {
|
||||
device = api::DeviceInfo::DeviceType::kPhone;
|
||||
} else if (strcmp(chasis, "laptop") == 0 || strcmp(chasis, "desktop") == 0) {
|
||||
device = api::DeviceInfo::DeviceType::kLaptop;
|
||||
} else if (strcmp(chasis, "tablet") == 0) {
|
||||
device = api::DeviceInfo::DeviceType::kTablet;
|
||||
} else if (strcmp(chasis, "handset") == 0) {
|
||||
device = api::DeviceInfo::DeviceType::kPhone;
|
||||
}
|
||||
free(chasis);
|
||||
return device;
|
||||
}
|
||||
|
||||
std::optional<std::u16string> DeviceInfo::GetFullName() const {
|
||||
struct passwd *pwd = getpwuid(getuid());
|
||||
if (!pwd) {
|
||||
return std::nullopt;
|
||||
}
|
||||
char *name = strtok(pwd->pw_gecos, ",");
|
||||
|
||||
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
|
||||
return convert.from_bytes(name ? name : pwd->pw_gecos);
|
||||
}
|
||||
|
||||
std::optional<std::string> DeviceInfo::GetProfileUserName() const {
|
||||
struct passwd *pwd = getpwuid(getuid());
|
||||
if (!pwd) {
|
||||
return std::nullopt;
|
||||
}
|
||||
char *name = strtok(pwd->pw_gecos, ",");
|
||||
return std::string(name);
|
||||
}
|
||||
} // namespace linux
|
||||
} // namespace nearby
|
||||
@@ -0,0 +1,51 @@
|
||||
#ifndef PLATFORM_IMPL_LINUX_DEVICE_INFO_H_
|
||||
#define PLATFORM_IMPL_LINUX_DEVICE_INFO_H_
|
||||
|
||||
#include <systemd/sd-bus.h>
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "internal/platform/implementation/device_info.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace linux {
|
||||
|
||||
class DeviceInfo : public api::DeviceInfo {
|
||||
public:
|
||||
~DeviceInfo() override;
|
||||
|
||||
std::optional<std::u16string> GetOsDeviceName() const override;
|
||||
api::DeviceInfo::DeviceType GetDeviceType() const override;
|
||||
api::DeviceInfo::OsType GetOsType() const override {
|
||||
return api::DeviceInfo::OsType::kWindows; // Or ChromeOS?
|
||||
}
|
||||
std::optional<std::u16string> GetFullName() const override;
|
||||
std::optional<std::u16string> GetGivenName() const override {
|
||||
return GetFullName();
|
||||
}
|
||||
std::optional<std::u16string> GetLastName() const override {
|
||||
return GetFullName();
|
||||
}
|
||||
std::optional<std::string> GetProfileUserName() const override;
|
||||
|
||||
std::optional<std::filesystem::path> GetDownloadPath() const override;
|
||||
std::optional<std::filesystem::path> GetLocalAppDataPath() const override;
|
||||
std::optional<std::filesystem::path> GetCommonAppDataPath() const override;
|
||||
std::optional<std::filesystem::path> GetTemporaryPath() const override;
|
||||
std::optional<std::filesystem::path> GetLogPath() const override;
|
||||
std::optional<std::filesystem::path> GetCrashDumpPath() const override;
|
||||
|
||||
bool IsScreenLocked() const override;
|
||||
void RegisterScreenLockedListener(
|
||||
absl::string_view listener_name,
|
||||
std::function<void(api::DeviceInfo::ScreenStatus)> callback) override;
|
||||
void UnregisterScreenLockedListener(absl::string_view listener_name) override;
|
||||
|
||||
sd_bus *system_bus;
|
||||
};
|
||||
} // namespace linux
|
||||
} // namespace nearby
|
||||
|
||||
#endif // PLATFORM_IMPL_LINUX_DEVICE_INFO_H_
|
||||
@@ -0,0 +1,12 @@
|
||||
#include "internal/platform/implementation/platform.h"
|
||||
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
|
||||
namespace nearby {
|
||||
namespace api {
|
||||
namespace {
|
||||
std::string ImplementationPlatform::GetCustomSavePath()
|
||||
}
|
||||
} // namespace api
|
||||
} // namespace nearby
|
||||
Reference in New Issue
Block a user