summaryrefslogtreecommitdiffstats
path: root/src/input/inputEventHandler.h
blob: 11e2f4e29b9d71cc911d2aa3f33b54f09d4d5abc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
 # 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 <QtGlobal>
#include <QtDebug>
#include <QList>
#include <QString>
#include <QCoreApplication>
#include <src/input/inputEvent.h>
#include "detail/policyChain.h"
#include "detail/systemTraits.h"

/////////////////////////////////////////////////////////////////////////
// InputEventContext:
//  For handling access control, this specifies who sent the input event.
//  This only makes sense in the privileged input handler chain.
/////////////////////////////////////////////////////////////////////////
struct InputEventContext
{
	InputEventContext()
	{
		hasBeenDenied = false;
	}

	virtual pid_t senderPid() const = 0;
	virtual uid_t senderUid() const = 0;
	virtual gid_t senderGid() const = 0;

	// Support the generation of meaningful log messages:
	mutable bool hasBeenDenied;
};

namespace input_policy
{

/////////////////////////////////////////////////////////////////////////
// Policy:
//  Modifies the behaviour of an input handler class.
//
//  There are several kinds of policy:
//   - Security Policy (When to allow a certain action)
//   - System Requirements (When to enable a certain handler)
//   - Applicability (When to consider a certain handler)
//
//  Policies are tied together using the detail::PolicyChain class.
/////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////
// Security Policy:
//  At the moment there are two security policies:
//  1. If the user is on a local seat, allow. If the user is privileged,
//     allow. Else deny.
//  2. Allow everybody.
//  Additional security policies can be added by following the example
//  set by AllowLocalOrPrivileged
/////////////////////////////////////////////////////////////////////////
template<typename PolicyImpl>
BEGIN_POLICY_CLASS(Security)
{
	bool allow(InputEventContext const* context)
	{
		return PolicyImpl::allow(context);
	}
}
END_POLICY_CLASS

struct AllowLocalOrPrivileged
{
	static bool allow(InputEventContext const*);
};

struct AllowEverybody
{
	static bool allow(InputEventContext const*);
};

typedef Security<AllowEverybody> Unprivileged;

/////////////////////////////////////////////////////////////////////////
// System Requirements:
//  At the moment, this is trivial, as PVS only runs on Linux. But,
//  as porting efforts are already underway, we include the necessary
//  machinery anyway.
/////////////////////////////////////////////////////////////////////////
template<IMPLICIT_TYPE_LIST_PARAMS(Trait)>
BEGIN_POLICY_CLASS(Require)
{
	static const bool areSystemRequirementsFulfilled =
			(NextPolicy::areSystemRequirementsFulfilled
				&& !NextPolicy::areSystemRequirementsVacuouslyFulfilled)
			|| detail::Matches<AllOf<IMPLICIT_TYPE_LIST_ARGS(Trait)>, detail::SystemTraits>::value;
	static const bool areSystemRequirementsVacuouslyFulfilled = false;
}
END_POLICY_CLASS

/////////////////////////////////////////////////////////////////////////
// System Requirements:
//  At the moment, this is trivial, as PVS only runs on Linux. But,
//  as porting efforts are already underway, we include the necessary
//  machinery anyway.
/////////////////////////////////////////////////////////////////////////
enum {
	HANDLER_CODE_DONT_CARE = 0xffff,
	HANDLER_VALUE_DONT_CARE = 0xffffffff
};

struct T;

template<unsigned short EventType,
		unsigned short EventCode = HANDLER_CODE_DONT_CARE,
		unsigned int EventValue = HANDLER_VALUE_DONT_CARE>
BEGIN_POLICY_CLASS(Match)
{
	bool isApplicable(InputEvent const& evt)
	{
		if(evt.type() != EventType)
			return NextPolicy::isApplicable(evt);
		if(EventCode != HANDLER_CODE_DONT_CARE && evt.code() != EventCode)
			return NextPolicy::isApplicable(evt);
		if(EventValue != HANDLER_VALUE_DONT_CARE && evt.value() != EventValue)
			return NextPolicy::isApplicable(evt);
		return true;
	}
};
END_POLICY_CLASS

namespace detail
{

/////////////////////////////////////////////////////////////////////////
// Base case: If no policies are given:
/////////////////////////////////////////////////////////////////////////
struct InputEventHandlerPolicyBase
{
	// The default security policy applies
	typedef AllowLocalOrPrivileged DefaultSecurityPolicyImpl;

	bool allow(InputEventContext const* context)
	{
		return DefaultSecurityPolicyImpl::allow(context);
	}

	// A handler that does not specify requirements works
	// everywhere
	static const bool areSystemRequirementsFulfilled = true;

	// We need this to implement proper logical OR
	static const bool areSystemRequirementsVacuouslyFulfilled = true;

	// Generate an error when no match policy is given.
	bool isApplicable(InputEvent const&)
	{
		return false;
	}

	// If any policy implementation needs an initialization hook:
	// Don't forget to call NextPolicy::initialize() in your
	// implementation!
	void initialize()
	{
	}
};

}
}

/////////////////////////////////////////////////////////////////////////
// We want a nice non-generic base so we can make a list of polymorphic
// handlers.
//
// The actual handler class need to provide doHandle and can override
// allow and isApplicable.
/////////////////////////////////////////////////////////////////////////
class InputEventHandlerBase
{
public:
	enum HandlerStatus
	{
		HANDLER_MATCHED,
		HANDLER_NOT_ALLOWED,
		HANDLER_NOT_APPLICABLE
	};

	virtual void initialize() = 0;
	HandlerStatus handle(InputEvent const& evt, InputEventContext const* context = 0);

protected:
	virtual bool allow(InputEvent const& event, InputEventContext const* context = 0) = 0;
	virtual bool isApplicable(InputEvent const& event, InputEventContext const* context = 0) = 0;
	virtual void doHandle(InputEvent const& event, InputEventContext const* context = 0) = 0;
};

/////////////////////////////////////////////////////////////////////////
// Now that the machinery is in place, we can finally define what it
// is like to be an input event handler:
/////////////////////////////////////////////////////////////////////////
template<POLICY_PARAMS>
class InputEventHandler : public InputEventHandlerBase
{
protected:
	// instantiate our policy:
	typedef USE_POLICY(input_policy::detail::InputEventHandlerPolicyBase) policy_type;
	policy_type policy;

public:
	void initialize()
	{
		policy.initialize();
	}

	// Export this so the handler chain can decide whether to include this handler
	static const bool areSystemRequirementsFulfilled = policy_type::areSystemRequirementsFulfilled;

protected:

	typedef InputEventHandler super;

	// allow and isApplicable are actually provided by Policy. If the
	// handler class wishes to override any of them, the policy implementation
	// can be called by means of super.
	bool allow(InputEvent const&, InputEventContext const* context = 0)
	{
		return policy.allow(context);
	}

	bool isApplicable(InputEvent const& event, InputEventContext const* = 0)
	{
		return policy.isApplicable(event);
	}
};

/////////////////////////////////////////////////////////////////////////
// And we can chain input handlers together:
/////////////////////////////////////////////////////////////////////////
class InputEventHandlerChain
{
private:
	QList<InputEventHandlerBase*> handlers;

	/////////////////////////////////////////////////////////////////////////
	// We need to statically dispatch on a static member of HandlerType.
	// Unfortunately, we cannot specialize member functions of a template.
	// So, we need to make this a class with a static non-template member.
	/////////////////////////////////////////////////////////////////////////
	template<bool Compatible, typename HandlerType>
	struct ConditionallyAppend
	{
		/////////////////////////////////////////////////////////////////////////
		// This method will never be instantiated for handlers that are
		// not Compatible, thus generating no reference to HandlerType
		// and permitting compilation to proceed without
		// tedious nested preprocessor conditionals.
		/////////////////////////////////////////////////////////////////////////
		static void doIt(InputEventHandlerChain* chain)
		{
			chain->handlers.append(new HandlerType);
		}
	};

	template<typename HandlerType>
	struct ConditionallyAppend<false, HandlerType>
	{
		static void doIt(InputEventHandlerChain*)
		{
		}
	};

public:
	// Add an event handler to the chain. The argument is only for
	// compilers which cannot handle template member functions
	// correctly.
	template<typename HandlerType>
	InputEventHandlerChain& add(HandlerType const* = 0)
	{
		ConditionallyAppend<HandlerType::areSystemRequirementsFulfilled, HandlerType>::doIt(this);
		return *this;
	}

	void initialize()
	{
		QListIterator<InputEventHandlerBase*> i(handlers);
		while(i.hasNext())
		{
			i.next()->initialize();
		}
	}

	void handle(InputEvent const& event, InputEventContext const* context = 0)
	{
		QListIterator<InputEventHandlerBase*> i(handlers);
		while(i.hasNext())
		{
			switch(i.next()->handle(event, context))
			{
			case InputEventHandlerBase::HANDLER_MATCHED:
				break;
			case InputEventHandlerBase::HANDLER_NOT_ALLOWED:
				context->hasBeenDenied = true;
			case InputEventHandlerBase::HANDLER_NOT_APPLICABLE:
				continue;
			}
		}
	}
};

#endif /* INPUTEVENTHANDLER_H_ */