/* # Copyright (c) 2009 - OpenSLX Project, Computer Center University of Freiburg # # This program is free software distributed under the GPL version 2. # See http://openslx.org/COPYING # # If you have any feedback please consult http://openslx.org/feedback and # send your suggestions, praise, or complaints to feedback@openslx.org # # General information about OpenSLX can be found at http://openslx.org/ # -------------------------------------------------------------------------- # inputEventHandler.h: # - Common definitions for input event handlers # -------------------------------------------------------------------------- */ #ifndef INPUTEVENTHANDLER_H_ #define INPUTEVENTHANDLER_H_ #include #include #include #include #include #define HANDLER_TYPE_DONT_CARE 0xffff #define HANDLER_CODE_DONT_CARE 0xffff #define HANDLER_VALUE_DONT_CARE 0xffffffff class InputEventContext { public: virtual pid_t getSenderPid() const = 0; virtual uid_t getSenderUid() const = 0; virtual gid_t getSenderGid() const = 0; }; struct SpecialInputEventDescription { SpecialInputEventDescription(QString const& d, quint16 t, quint16 c, quint32 v = 0) : descriptionString(d), evtType(t), evtCode(c), evtValue(v) { } QString descriptionString; quint16 evtType; quint16 evtCode; quint32 evtValue; InputEvent toEvent() const { return InputEvent(evtType, evtCode, evtValue); } }; template class DefaultInputEventHandler { public: virtual bool matches(InputEvent const& evt, InputEventContext const*) { if(Type != HANDLER_TYPE_DONT_CARE) { if(evt.type() != Type) return false; } if(Code != HANDLER_CODE_DONT_CARE) { if(evt.code() != Code) return false; } if(Value != HANDLER_VALUE_DONT_CARE) { if(evt.value() != Value) return false; } return true; } virtual void initialize() { } virtual void handle(InputEvent const& evt, InputEventContext const*) = 0; static void describeInto(QList& description) { } }; namespace policy { enum SecurityFlags { SEC_PHYSICAL_SEAT = 1, SEC_PRIVILEGED_USER = 2 }; bool allowPhysicalSeat(InputEvent const& evt, InputEventContext const* ctx); bool allowPrivilegedUser(InputEvent const& evt, InputEventContext const* ctx); template struct Security { bool allow(InputEvent const& evt, InputEventContext const* ctx) { if((flags & SEC_PHYSICAL_SEAT) && !allowPhysicalSeat(evt, ctx)) return false; if((flags & SEC_PRIVILEGED_USER) && !allowPrivilegedUser(evt, ctx)) return false; return true; } }; struct UnixLike; struct Linux; struct Windows; #if defined(__linux) typedef boost::mpl::vector2::type Systems; #elif defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__) || defined(__WINDOWS__) typedef boost::mpl::vector1::type Systems; #else # error "Porting is needed!" #endif struct SystemEnabled; struct SystemDisabled; template struct RequireSystem { typedef typename boost::mpl::contains::type enabled_type; static const bool enabled = enabled_type::value; }; struct RequireNoSystem { typedef boost::mpl::bool_::type enabled_type; static const bool enabled = enabled_type::value; }; } template class HandlerHelper { public: bool handle(InputEvent const& evt, InputEventContext const* context = 0) { if(!securityPolicy.allow(evt, context)) { return true; } if(delegate.matches(evt, context)) { delegate.handle(evt, context); return true; } else { return false; } } void initialize() { delegate.initialize(); } private: Delegate delegate; SecurityPolicy securityPolicy; }; template class HandlerHelper { public: bool handle(InputEvent const& evt, InputEventContext const* context = 0) { return false; } void initialize() { } }; template > struct Handler : public HandlerHelper { }; template struct InputEventHandlerChainHelper { private: typedef typename boost::mpl::next::type next_iterator_type; typedef InputEventHandlerChainHelper next_in_chain; typedef typename boost::mpl::deref::type handler_type; handler_type _handler; next_in_chain _next; public: void handle(InputEvent const& evt, InputEventContext const* context = 0) { if(!_handler.handle(evt, context)) { _next.handle(evt, context); } } void initialize() { _handler.initialize(); _next.initialize(); } } }; template struct InputEventHandlerChainHelper { void handle(InputEvent const&, InputEventContext const* context = 0) { // do nothing } void initialize() { // do nothing } }; template struct InputEventHandlerChain : public InputEventHandlerChainHelper::type, typename boost::mpl::end::type> { }; #endif /* INPUTEVENTHANDLER_H_ */