diff --git a/libinput/events.cpp b/libinput/events.cpp index 17efc64253..3f79062f5f 100644 --- a/libinput/events.cpp +++ b/libinput/events.cpp @@ -67,7 +67,7 @@ Event *Event::create(libinput_event *event) 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) { } @@ -76,6 +76,14 @@ 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) { diff --git a/libinput/events.h b/libinput/events.h index 28d68a6b55..c16fae2dd6 100644 --- a/libinput/events.h +++ b/libinput/events.h @@ -37,9 +37,7 @@ public: virtual ~Event(); libinput_event_type type() const; - Device *device() const { - return m_device; - } + Device *device() const; libinput_device *nativeDevice() const; operator libinput_event*() { @@ -57,7 +55,7 @@ protected: private: libinput_event *m_event; libinput_event_type m_type; - Device *m_device; + mutable Device *m_device; }; class KeyEvent : public Event