#include <cstring>
#include "base/compiler_specific.h"
#include "ui/events/event.h"
namespace ui {
namespace {
constexpr char kPropertyKeyboardDeviceId[] = "keyboard_device_id";
}
int GetKeyboardDeviceIdProperty(const Event& event) {
if (auto* properties = event.properties()) {
auto it = properties->find(kPropertyKeyboardDeviceId);
if (it != properties->end()) {
int result = 0;
UNSAFE_TODO(std::memcpy(&result, it->second.data(), it->second.size()));
return result;
}
}
DCHECK(event.IsKeyEvent());
return event.source_device_id();
}
void SetKeyboardDeviceIdProperty(Event* event, int device_id) {
std::vector<std::uint8_t> buf;
buf.resize(sizeof(device_id));
UNSAFE_TODO(std::memcpy(buf.data(), &device_id, buf.size()));
auto properties =
event->properties() ? *event->properties() : Event::Properties();
properties.emplace(kPropertyKeyboardDeviceId, std::move(buf));
event->SetProperties(properties);
}
}