diff --git a/libinput/events.h b/libinput/events.h --- a/libinput/events.h +++ b/libinput/events.h @@ -42,9 +42,7 @@ virtual ~Event(); libinput_event_type type() const; - Device *device() const { - return m_device; - } + Device *device() const; libinput_device *nativeDevice() const; operator libinput_event*() { @@ -62,7 +60,7 @@ private: libinput_event *m_event; libinput_event_type m_type; - Device *m_device; + mutable Device *m_device; }; class KeyEvent : public Event diff --git a/libinput/events.cpp b/libinput/events.cpp --- a/libinput/events.cpp +++ b/libinput/events.cpp @@ -69,15 +69,23 @@ Event::Event(libinput_event *event, libinput_event_type type) : m_event(event) , m_type(type) - , m_device(Device::getDevice(libinput_event_get_device(m_event))) + , m_device(nullptr) { } Event::~Event() { libinput_event_destroy(m_event); } +Device *Event::device() const +{ + if (!m_device) { + m_device = Device::getDevice(libinput_event_get_device(m_event)); + } + return m_device; +} + libinput_device *Event::nativeDevice() const { if (m_device) {