summaryrefslogtreecommitdiffstatshomepage
path: root/src/osd/interface/inputseq.cpp
blob: da1f2b98b26b117fc2856b9abc8940b6f0fe5cfc (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
// license:BSD-3-Clause
// copyright-holders:Aaron Giles
/***************************************************************************

    inputseq.cpp

    A combination of hosts inputs that can be assigned to a control.

***************************************************************************/

#include "inputseq.h"


namespace osd {

//**************************************************************************
//  CONSTANTS
//**************************************************************************

// additional expanded input codes for sequences
constexpr input_code input_seq::end_code;
constexpr input_code input_seq::default_code;
constexpr input_code input_seq::not_code;
constexpr input_code input_seq::or_code;

// constant sequences
const input_seq input_seq::empty_seq;



//**************************************************************************
//  INPUT SEQ
//**************************************************************************

//-------------------------------------------------
//  operator+= - append a code to the end of an
//  input sequence
//-------------------------------------------------

input_seq &input_seq::operator+=(input_code code) noexcept
{
	// if not enough room, return false
	const int curlength = length();
	if (curlength < m_code.size())
	{
		m_code[curlength] = code;
		if ((curlength + 1) < m_code.size())
			m_code[curlength + 1] = end_code;
	}
	return *this;
}


//-------------------------------------------------
//  operator|= - append a code to a sequence; if
//  the sequence is non-empty, insert an OR
//  before the new code
//-------------------------------------------------

input_seq &input_seq::operator|=(input_code code) noexcept
{
	// overwrite end/default with the new code
	if (m_code[0] == default_code)
	{
		m_code[0] = code;
		m_code[1] = end_code;
	}
	else
	{
		// otherwise, append an OR token and then the new code
		const int curlength = length();
		if ((curlength + 1) < m_code.size())
		{
			m_code[curlength] = or_code;
			m_code[curlength + 1] = code;
			if ((curlength + 2) < m_code.size())
				m_code[curlength + 2] = end_code;
		}
	}
	return *this;
}


//-------------------------------------------------
//  length - return the length of the sequence
//-------------------------------------------------

int input_seq::length() const noexcept
{
	// find the end token; error if none found
	for (int seqnum = 0; seqnum < m_code.size(); seqnum++)
		if (m_code[seqnum] == end_code)
			return seqnum;
	return m_code.size();
}


//-------------------------------------------------
//  is_valid - return true if a given sequence is
//  valid
//-------------------------------------------------

bool input_seq::is_valid() const noexcept
{
	// "default" can only be of length 1
	if (m_code[0] == default_code)
		return m_code[1] == end_code;

	// scan the sequence for valid codes
	input_item_class lastclass = ITEM_CLASS_INVALID;
	input_code lastcode = INPUT_CODE_INVALID;
	decltype(m_code) positive_codes;
	decltype(m_code) negative_codes;
	auto positive_codes_end = positive_codes.begin();
	auto negative_codes_end = negative_codes.begin();
	for (input_code code : m_code)
	{
		// invalid codes are never permitted
		if (code == INPUT_CODE_INVALID)
			return false;

		// if we hit an OR or the end, validate the previous chunk
		if (code == or_code || code == end_code)
		{
			// must be at least one positive code
			if (positive_codes.begin() == positive_codes_end)
				return false;

			// last code must not have been an internal code
			if (lastcode.internal())
				return false;

			// if this is the end, we're ok
			if (code == end_code)
				return true;

			// reset the state for the next chunk
			positive_codes_end = positive_codes.begin();
			negative_codes_end = negative_codes.begin();
			lastclass = ITEM_CLASS_INVALID;
		}
		else if (code == not_code)
		{
			// if we hit a NOT, make sure we don't have a double
			if (lastcode == not_code)
				return false;
		}
		else
		{
			// track positive codes, and don't allow positive and negative for the same code
			if (lastcode != not_code)
			{
				*positive_codes_end++ = code;
				if (std::find(negative_codes.begin(), negative_codes_end, code) != negative_codes_end)
					return false;
			}
			else
			{
				*negative_codes_end++ = code;
				if (std::find(positive_codes.begin(), positive_codes_end, code) != positive_codes_end)
					return false;
			}

			// non-switch items can't have a NOT
			input_item_class itemclass = code.item_class();
			if (itemclass != ITEM_CLASS_SWITCH && lastcode == not_code)
				return false;

			// absolute/relative items must all be the same class
			if ((lastclass == ITEM_CLASS_ABSOLUTE && itemclass != ITEM_CLASS_ABSOLUTE) ||
				(lastclass == ITEM_CLASS_RELATIVE && itemclass != ITEM_CLASS_RELATIVE))
				return false;
		}

		// remember the last code
		lastcode = code;
	}

	// if we got here, we were missing an END token; fail
	return false;
}


//-------------------------------------------------
//  backspace - "backspace" over the last entry in
//  a sequence
//-------------------------------------------------

void input_seq::backspace() noexcept
{
	// if we have at least one entry, remove it
	const int curlength = length();
	if (curlength > 0)
		m_code[curlength - 1] = end_code;
}


//-------------------------------------------------
//  replace - replace all instances of oldcode
//  with newcode in a sequence
//-------------------------------------------------

void input_seq::replace(input_code oldcode, input_code newcode) noexcept
{
	for (input_code &elem : m_code)
		if (elem == oldcode)
			elem = newcode;
}

} // namespace osd