stmm-input-doc  0.17.0
Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | List of all members
stmi::Event Class Referenceabstract

Base event class. More...

Inheritance diagram for stmi::Event:
Inheritance graph
[legend]

Classes

class  Class
 The representation of a registered event class. More...
 
class  RegisterClass
 Template used to register event classes. More...
 

Public Types

enum  AS_KEY_INPUT_TYPE { AS_KEY_PRESS = 1, AS_KEY_RELEASE = 2, AS_KEY_RELEASE_CANCEL = 3 }
 Key simulation type. More...
 

Public Member Functions

virtual ~Event () noexcept=default
 
int64_t getTimeUsec () const noexcept
 Returns the time this event originated. More...
 
virtual shared_ptr< CapabilitygetCapability () const noexcept=0
 Returns the capability that generated this event. More...
 
int32_t getCapabilityId () const noexcept
 Returns the id of the capability that generated this event. More...
 
const shared_ptr< Accessor > & getAccessor () const noexcept
 Returns the accessor that helped generate this event. More...
 
virtual bool getAsKey (HARDWARE_KEY &eKey, AS_KEY_INPUT_TYPE &eType, bool &bMoreThanOne) const noexcept
 Tells whether the event can simulate hardware keys. More...
 
virtual std::vector< std::pair< HARDWARE_KEY, AS_KEY_INPUT_TYPE > > getAsKeys () const noexcept
 All the keys this event simulates. More...
 
const ClassgetEventClass () const noexcept
 Get the registered class of the event instance. More...
 

Static Public Member Functions

static bool isEventClassIdRegistered (const std::string &sEventClassId) noexcept
 Tells whether the given event class id is registered. More...
 
static Class getEventClassIdClass (const std::string &sEventClassId) noexcept
 Gets the event class with given class id. More...
 

Protected Member Functions

 Event (const Class &oClass, int64_t nTimeUsec, int32_t nCapabilityId, const shared_ptr< Accessor > &refAccessor) noexcept
 Constructor to be called from subclasses. More...
 
 Event (const Class &oClass, int64_t nTimeUsec, int32_t nCapabilityId) noexcept
 Constructor to be called from subclasses (with empty accessor). More...
 
void setTimeUsec (int64_t nTimeUsec) noexcept
 Set the event time. More...
 
void setCapabilityId (int32_t nCapabilityId) noexcept
 Set the capability id. More...
 
void setAccessor (const shared_ptr< Accessor > &refAccessor) noexcept
 Set the accessor. More...
 

Static Protected Member Functions

static bool isEventTypeRegistered (const std::type_info &oEventType) noexcept
 Tells whether an event type was registered. More...
 
static const std::type_info & getEventClassIdType (const std::string &sEventClassId, bool &bRegistered) noexcept
 Gets the type of the registered event subclass associated with class id. More...
 
static std::string getEventTypeClassId (const std::type_info &oType) noexcept
 Gets the class id of the given type. More...
 

Detailed Description

Base event class.

Usage: if an event listener's callback (stmi::EventListener) is called by the device manager, you get the actual registered subclass of the event by statically casting as follows:

void handleEvent(const shared_ptr<stmi::Event>& refEvent)
{
  const stmi::Event::Class& oC = refEvent->getEventClass();
  auto& oT = oC.getTypeInfo();
  if (oT == typeid(stmi::KeyEvent)) {
    handleKeyEvent(static_cast<stmi::KeyEvent*>(refEvent.get()));
  } else if (oT == typeid(stmi::PointerEvent)) {
    handlePointerEvent(static_cast<stmi::PointerEvent*>(refEvent.get()));
  } // and so on
}

with specific registered class implementations:

void handleKeyEvent(stmi::KeyEvent* pEvent) { ... }
void handlePointerEvent(stmi::PointerEvent* pEvent) { ... }
// and so on

Member Enumeration Documentation

◆ AS_KEY_INPUT_TYPE

Key simulation type.

Enumerator
AS_KEY_PRESS 
AS_KEY_RELEASE 
AS_KEY_RELEASE_CANCEL 

Constructor & Destructor Documentation

◆ ~Event()

virtual stmi::Event::~Event ( )
virtualdefaultnoexcept

◆ Event() [1/2]

stmi::Event::Event ( const Class oClass,
int64_t  nTimeUsec,
int32_t  nCapabilityId,
const shared_ptr< Accessor > &  refAccessor 
)
protectednoexcept

Constructor to be called from subclasses.

The registered class has to be the actual class of the instance being constructed or a superclass of it.

Parameters
oClassThe registered class of the event.
nTimeUsecTime from epoch in microseconds.
nCapabilityIdThe id of the capability that generated this event. Must be >= 0.
refAccessorThe accessor used to generate the event. Can be null.

◆ Event() [2/2]

stmi::Event::Event ( const Class oClass,
int64_t  nTimeUsec,
int32_t  nCapabilityId 
)
inlineprotectednoexcept

Constructor to be called from subclasses (with empty accessor).

The registered class has to be the actual class of the instance being constructed or a superclass of it.

Parameters
oClassThe registered class of the event.
nTimeUsecTime from epoch in microseconds.
nCapabilityIdThe id of the capability that generated this event. Must be >= 0.

Member Function Documentation

◆ getAccessor()

const shared_ptr<Accessor>& stmi::Event::getAccessor ( ) const
inlinenoexcept

Returns the accessor that helped generate this event.

Returns
The accessor. Can be null.

◆ getAsKey()

virtual bool stmi::Event::getAsKey ( HARDWARE_KEY eKey,
AS_KEY_INPUT_TYPE eType,
bool &  bMoreThanOne 
) const
inlinevirtualnoexcept

Tells whether the event can simulate hardware keys.

If it can the parameters nKey and eType will contain the first of the simulated keys, bMoreThanOne whether there are more. If it's the case (bMoreThanOne == true) then getAsKeys() should be called, which returns all the simulated keys (the first included). If the function returns false the parameters are left unchanged. The default implementation returns false.

Ex.: a subclass JoystickHatEvent might return, for transition from HAT_CENTER to HAT_RIGHTUP, the first key (nKey=KEY_RIGHT, eType=AS_KEY_PRESS) and bMoreThanOne=true while getAsKeys() returns all keys {(KEY_RIGHT,AS_KEY_PRESS), (KEY_UP,AS_KEY_PRESS)}.

Parameters
eKeyThe first simulated hardware key (or unchanged if no keys).
eTypeThe type of the first simulated key (or unchanged if no keys).
bMoreThanOneIf more than one key is simulated (or unchanged if no keys).
Returns
Whether keys are simulated.

Reimplemented in stmi::JoystickButtonEvent, stmi::PointerScrollEvent, stmi::JoystickHatEvent, stmi::PointerEvent, and stmi::KeyEvent.

◆ getAsKeys()

virtual std::vector< std::pair<HARDWARE_KEY, AS_KEY_INPUT_TYPE> > stmi::Event::getAsKeys ( ) const
inlinevirtualnoexcept

All the keys this event simulates.

The default implementation calls getAsKey() and returns an empty set if no keys are simulated, otherwise a set containing the first (nKey, eType) pair. Therefore subclass implementations have to override this function only if more than one key is simulated.

Ex.: a subclass PointerScrollEvent may return {(KEY_SCROLLUP,AS_KEY_PRESS), (KEY_SCROLLUP,AS_KEY_RELEASE)}, if mouse's wheel was scrolled up.

Returns
Vector of (HARDWARE_KEY,AS_KEY_INPUT_TYPE) pairs that simulate the event. Empty if no key simulated.

Reimplemented in stmi::PointerScrollEvent, and stmi::JoystickHatEvent.

◆ getCapability()

virtual shared_ptr<Capability> stmi::Event::getCapability ( ) const
pure virtualnoexcept

Returns the capability that generated this event.

The Event subclass implementation keeps only a weak_ptr on the capability to avoid reference cycles.

Returns
The capability or null if the capability was deleted.

Implemented in stmi::JoystickAxisEvent, stmi::JoystickButtonEvent, stmi::PointerScrollEvent, stmi::JoystickHatEvent, stmi::PointerEvent, stmi::DeviceMgmtEvent, stmi::TouchEvent, and stmi::KeyEvent.

◆ getCapabilityId()

int32_t stmi::Event::getCapabilityId ( ) const
inlinenoexcept

Returns the id of the capability that generated this event.

The unique id isn't changed when the capability is removed and getCapability() returns null.

Returns
The capability id.

◆ getEventClass()

const Class& stmi::Event::getEventClass ( ) const
inlinenoexcept

Get the registered class of the event instance.

Returns
The registered class.

◆ getEventClassIdClass()

static Class stmi::Event::getEventClassIdClass ( const std::string &  sEventClassId)
staticnoexcept

Gets the event class with given class id.

Parameters
sEventClassIdThe registered class id.
Returns
The class. Might be empty if sEventClassId is not registered.

◆ getEventClassIdType()

static const std::type_info& stmi::Event::getEventClassIdType ( const std::string &  sEventClassId,
bool &  bRegistered 
)
staticprotectednoexcept

Gets the type of the registered event subclass associated with class id.

Parameters
sEventClassIdThe type string.
bRegisteredWhether the return value is valid.
Returns
The registered type. If bRegistered is false the result is undefined.

◆ getEventTypeClassId()

static std::string stmi::Event::getEventTypeClassId ( const std::type_info &  oType)
staticprotectednoexcept

Gets the class id of the given type.

Parameters
oTypeThe type to be tested.
Returns
The class id or an empty string if the type is not registered.

◆ getTimeUsec()

int64_t stmi::Event::getTimeUsec ( ) const
inlinenoexcept

Returns the time this event originated.

Returns
The time is in microseconds from epoch or -1 if unknown.

◆ isEventClassIdRegistered()

static bool stmi::Event::isEventClassIdRegistered ( const std::string &  sEventClassId)
staticnoexcept

Tells whether the given event class id is registered.

Parameters
sEventClassIdThe class id.
Returns
Whether registered.

◆ isEventTypeRegistered()

static bool stmi::Event::isEventTypeRegistered ( const std::type_info &  oEventType)
staticprotectednoexcept

Tells whether an event type was registered.

Parameters
oEventTypeThe c++ typeid of a class.
Returns
Whether the class was registered.

◆ setAccessor()

void stmi::Event::setAccessor ( const shared_ptr< Accessor > &  refAccessor)
inlineprotectednoexcept

Set the accessor.

Parameters
refAccessorCan be null.

◆ setCapabilityId()

void stmi::Event::setCapabilityId ( int32_t  nCapabilityId)
inlineprotectednoexcept

Set the capability id.

Parameters
nCapabilityIdThe id of the capability that generated this event. Must be >= 0.

◆ setTimeUsec()

void stmi::Event::setTimeUsec ( int64_t  nTimeUsec)
inlineprotectednoexcept

Set the event time.

Parameters
nTimeUsecTime from epoch in microseconds.