summaryrefslogtreecommitdiffstats
blob: 44d9849f64a10dbd75cf72f3dc1bafb353ca76bf (plain)
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
/*
 * Copyright (c) 2012 Yoann Blein
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

#include <Swiften/Parser/PayloadParsers/InputEventParser.h>

#include <boost/optional.hpp>
#include <boost/lexical_cast.hpp>

namespace Swift {

void InputEventParser::handleStartElement(const std::string& element, const std::string&, const AttributeMap& attributes) {
	if (level == 0) {
		// <inputevt> tag
		InputEventPayload::ref payload = getPayloadInternal();
		payload->setAction(actionFromString(attributes.getAttributeValue("action").get_value_or("")));
	} else if (level == 1) {
		// <remote-control> tag
	} else if (level == 2) {
		InputEventPayload::Event event(eventTypeFromString(element));
		switch (event.type) {
			case InputEventPayload::Event::MouseMove:
				try {
					event.realArg1 = boost::lexical_cast<float>(attributes.getAttributeValue("x").get_value_or("0"));
					event.realArg2 = boost::lexical_cast<float>(attributes.getAttributeValue("y").get_value_or("0"));
				} catch (boost::bad_lexical_cast &) {}
				break;
			case InputEventPayload::Event::MouseWheel:
			case InputEventPayload::Event::MousePress:
			case InputEventPayload::Event::MouseRelease:
			case InputEventPayload::Event::KeyType:
			case InputEventPayload::Event::KeyPress:
			case InputEventPayload::Event::KeyRelease:
				try {
					event.integerArg = boost::lexical_cast<int>(attributes.getEntries().front().getValue());
				} catch (boost::bad_lexical_cast &) {}
			default:
				break;
		}
	}
	++level;
}

void InputEventParser::handleEndElement(const std::string&, const std::string&) {
	--level;
}

void InputEventParser::handleCharacterData(const std::string&){

}

InputEventPayload::Action InputEventParser::actionFromString(const std::string& action)
{
	if (action == "notify")
		return InputEventPayload::Notify;

	return InputEventPayload::Unknown;
}

InputEventPayload::Event::EventType InputEventParser::eventTypeFromString(const std::string& eventType) {
	if (eventType == "mouse-move")
		return InputEventPayload::Event::MouseMove;
	else if (eventType == "mouse-wheel")
		return InputEventPayload::Event::MouseWheel;
	else if (eventType == "mouse-press")
		return InputEventPayload::Event::MousePress;
	else if (eventType == "mouse-release")
		return InputEventPayload::Event::MouseRelease;
	else if (eventType == "key-type")
		return InputEventPayload::Event::KeyType;
	else if (eventType == "key-press")
		return InputEventPayload::Event::KeyPress;
	else if (eventType == "key-release")
		return InputEventPayload::Event::KeyRelease;

	return InputEventPayload::Event::Unknown;
}

}