summaryrefslogtreecommitdiffstatshomepage
path: root/src/frontend/mame/iptseqpoll.cpp
blob: 7808256c19032cad10d1c39af749f3ff6cfeb165 (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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
// license:BSD-3-Clause
// copyright-holders:Vas Crabb, Aaron Giles

#include "emu.h"
#include "iptseqpoll.h"

#include "inputdev.h"

#include <cassert>
#include <algorithm>


input_code_poller::input_code_poller(input_manager &manager) noexcept :
	m_manager(manager),
	m_axis_memory(),
	m_switch_memory()
{
}


input_code_poller::~input_code_poller()
{
}


void input_code_poller::reset()
{
	// iterate over device classes and devices
	m_axis_memory.clear();
	m_switch_memory.clear();
	for (input_device_class classno = DEVICE_CLASS_FIRST_VALID; DEVICE_CLASS_LAST_VALID >= classno; ++classno)
	{
		input_class &devclass(m_manager.device_class(classno));
		if (devclass.enabled())
		{
			for (int devnum = 0; devclass.maxindex() >= devnum; ++devnum)
			{
				// fetch the device; ignore if nullptr
				input_device *const device(devclass.device(devnum));
				if (device)
				{
					// iterate over items within each device
					for (input_item_id itemid = ITEM_ID_FIRST_VALID; device->maxitem() >= itemid; ++itemid)
					{
						// for any non-switch items, set memory to the current value
						input_device_item *const item(device->item(itemid));
						if (item && (item->itemclass() != ITEM_CLASS_SWITCH))
							m_axis_memory.emplace_back(item, m_manager.code_value(item->code()));
					}
				}
			}
		}
	}
	std::sort(m_axis_memory.begin(), m_axis_memory.end());
}


bool input_code_poller::code_pressed_once(input_code code, bool moved)
{
	// look for the code in the memory
	bool const pressed(m_manager.code_pressed(code));
	auto const found(std::lower_bound(m_switch_memory.begin(), m_switch_memory.end(), code));
	if ((m_switch_memory.end() != found) && (*found == code))
	{
		// if no longer pressed, clear entry
		if (!pressed)
			m_switch_memory.erase(found);

		// always return false
		return false;
	}

	// if we get here, we were not previously pressed; if still not pressed, return false
	if (!pressed || !moved)
		return false;

	// otherwise, add the code to the memory and return true
	m_switch_memory.emplace(found, code);
	return true;
}



axis_code_poller::axis_code_poller(input_manager &manager) noexcept :
	input_code_poller(manager),
	m_axis_active()
{
}


void axis_code_poller::reset()
{
	input_code_poller::reset();
	m_axis_active.clear();
	m_axis_active.resize(m_axis_memory.size(), false);
}


input_code axis_code_poller::poll()
{
	// iterate over the axis items we found
	for (std::size_t i = 0; m_axis_memory.size() > i; ++i)
	{
		auto &memory = m_axis_memory[i];
		input_code code = memory.first->code();
		if (!memory.first->check_axis(code.item_modifier(), memory.second))
		{
			m_axis_active[i] = false;
		}
		else if (!m_axis_active[i])
		{
			if (code.item_class() == ITEM_CLASS_ABSOLUTE)
			{
				m_axis_active[i] = true;
			}
			else
			{
				// can only cycle modifiers on a relative item with append
				m_axis_memory.erase(m_axis_memory.begin() + i);
				m_axis_active.erase(m_axis_active.begin() + i);
			}
			if (!m_manager.device_class(memory.first->device().devclass()).multi())
				code.set_device_index(0);
			return code;
		}
	}

	// iterate over device classes and devices, skipping disabled classes
	for (input_device_class classno = DEVICE_CLASS_FIRST_VALID; DEVICE_CLASS_LAST_VALID >= classno; ++classno)
	{
		input_class &devclass(m_manager.device_class(classno));
		if (!devclass.enabled())
			continue;

		for (int devnum = 0; devclass.maxindex() >= devnum; ++devnum)
		{
			// fetch the device; ignore if nullptr
			input_device *const device(devclass.device(devnum));
			if (!device)
				continue;

			// iterate over items within each device
			for (input_item_id itemid = ITEM_ID_FIRST_VALID; device->maxitem() >= itemid; ++itemid)
			{
				input_device_item *const item(device->item(itemid));
				if (!item)
					continue;

				input_code code = item->code();
				if (item->itemclass() == ITEM_CLASS_SWITCH)
				{
					// item is natively a switch, poll it
					if (code_pressed_once(code, true))
						return code;
					else
						continue;
				}
			}
		}
	}

	// if nothing, return an invalid code
	return INPUT_CODE_INVALID;
}



switch_code_poller::switch_code_poller(input_manager &manager) noexcept :
	input_code_poller(manager)
{
}


input_code switch_code_poller::poll()
{
	// iterate over device classes and devices, skipping disabled classes
	for (input_device_class classno = DEVICE_CLASS_FIRST_VALID; DEVICE_CLASS_LAST_VALID >= classno; ++classno)
	{
		input_class &devclass(m_manager.device_class(classno));
		if (!devclass.enabled())
			continue;

		for (int devnum = 0; devclass.maxindex() >= devnum; ++devnum)
		{
			// fetch the device; ignore if nullptr
			input_device *const device(devclass.device(devnum));
			if (!device)
				continue;

			// iterate over items within each device
			for (input_item_id itemid = ITEM_ID_FIRST_VALID; device->maxitem() >= itemid; ++itemid)
			{
				input_device_item *const item(device->item(itemid));
				if (!item)
					continue;

				input_code code = item->code();
				if (item->itemclass() == ITEM_CLASS_SWITCH)
				{
					// item is natively a switch, poll it
					if (code_pressed_once(code, true))
						return code;
					else
						continue;
				}

				auto const memory(std::lower_bound(
							m_axis_memory.begin(),
							m_axis_memory.end(),
							item,
							[] (auto const &x, auto const &y) { return x.first < y; }));
				if ((m_axis_memory.end() == memory) || (item != memory->first))
					continue;

				// poll axes digitally
				bool const moved(item->check_axis(code.item_modifier(), memory->second));
				code.set_item_class(ITEM_CLASS_SWITCH);
				if ((classno == DEVICE_CLASS_JOYSTICK) && (code.item_id() == ITEM_ID_XAXIS))
				{
					// joystick X axis - check with left/right modifiers
					code.set_item_modifier(ITEM_MODIFIER_LEFT);
					if (code_pressed_once(code, moved))
						return code;
					code.set_item_modifier(ITEM_MODIFIER_RIGHT);
					if (code_pressed_once(code, moved))
						return code;
				}
				else if ((classno == DEVICE_CLASS_JOYSTICK) && (code.item_id() == ITEM_ID_YAXIS))
				{
					// if this is a joystick Y axis, check with up/down modifiers
					code.set_item_modifier(ITEM_MODIFIER_UP);
					if (code_pressed_once(code, moved))
						return code;
					code.set_item_modifier(ITEM_MODIFIER_DOWN);
					if (code_pressed_once(code, moved))
						return code;
				}
				else
				{
					// any other axis, check with pos/neg modifiers
					code.set_item_modifier(ITEM_MODIFIER_POS);
					if (code_pressed_once(code, moved))
						return code;
					code.set_item_modifier(ITEM_MODIFIER_NEG);
					if (code_pressed_once(code, moved))
						return code;
				}
			}
		}
	}

	// if nothing, return an invalid code
	return INPUT_CODE_INVALID;
}



keyboard_code_poller::keyboard_code_poller(input_manager &manager) noexcept :
	input_code_poller(manager)
{
}


input_code keyboard_code_poller::poll()
{
	// iterate over devices in keyboard class
	input_class &devclass = m_manager.device_class(DEVICE_CLASS_KEYBOARD);
	for (int devnum = 0; devclass.maxindex() >= devnum; ++devnum)
	{
		// fetch the device; ignore if nullptr
		input_device *const device(devclass.device(devnum));
		if (device)
		{
			// iterate over items within each device
			for (input_item_id itemid = ITEM_ID_FIRST_VALID; itemid <= device->maxitem(); ++itemid)
			{
				// iterate over items within each device
				for (input_item_id itemid = ITEM_ID_FIRST_VALID; device->maxitem() >= itemid; ++itemid)
				{
					input_device_item *const item = device->item(itemid);
					if (item && (item->itemclass() == ITEM_CLASS_SWITCH))
					{
						input_code const code = item->code();
						if (code_pressed_once(code, true))
							return code;
					}
				}
			}
		}
	}

	// if nothing, return an invalid code
	return INPUT_CODE_INVALID;
}



input_sequence_poller::input_sequence_poller() noexcept :
	m_sequence(),
	m_last_ticks(0),
	m_modified(false)
{
}


input_sequence_poller::~input_sequence_poller()
{
}


void input_sequence_poller::start()
{
	// start with an empty sequence
	m_sequence.reset();

	// reset the recording count and the clock
	m_last_ticks = 0;
	m_modified = false;
	do_start();
}


void input_sequence_poller::start(input_seq const &startseq)
{
	// grab the starting sequence to append to, and append an OR if it isn't empty
	m_sequence = startseq;
	if (input_seq::end_code != m_sequence[0])
		m_sequence += input_seq::or_code;

	// reset the recording count and the clock
	m_last_ticks = 0;
	m_modified = false;
	do_start();
}


bool input_sequence_poller::poll()
{
	// if we got a new code to append it, append it and reset the timer
	input_code const newcode = do_poll();
	osd_ticks_t const newticks = osd_ticks();
	if (INPUT_CODE_INVALID != newcode)
	{
		m_sequence += newcode;
		m_last_ticks = newticks;
		m_modified = true;
	}

	// if we've recorded at least one item and one second has passed, we're done
	if (m_last_ticks && ((m_last_ticks + osd_ticks_per_second()) < newticks))
		return true;

	// return false to indicate we are still polling
	return false;
}



axis_sequence_poller::axis_sequence_poller(input_manager &manager) noexcept :
	input_sequence_poller(),
	m_code_poller(manager)
{
}


void axis_sequence_poller::do_start()
{
	// wait for any inputs that are already active to be released
	m_code_poller.reset();
	for (input_code dummycode = KEYCODE_ENTER; INPUT_CODE_INVALID != dummycode; )
		dummycode = m_code_poller.poll();
}


input_code axis_sequence_poller::do_poll()
{
	// absolute/relative case: see if we have an analog change of sufficient amount
	int const curlen = m_sequence.length();
	input_code lastcode = m_sequence[curlen - 1];
	bool const has_or = input_seq::or_code == lastcode;
	if (has_or)
		lastcode = m_sequence[curlen - 2];
	input_code newcode = m_code_poller.poll();

	// if not empty, see if it's the same control again to cycle modifiers
	if ((INPUT_CODE_INVALID != newcode) && curlen)
	{
		input_item_class const newclass = newcode.item_class();
		input_code last_nomodifier = lastcode;
		last_nomodifier.set_item_modifier(ITEM_MODIFIER_NONE);
		if (newcode == last_nomodifier)
		{
			if (ITEM_CLASS_ABSOLUTE == newclass)
			{
				// increment the modifier, wrapping back to none
				switch (lastcode.item_modifier())
				{
				case ITEM_MODIFIER_NONE:
					newcode.set_item_modifier(ITEM_MODIFIER_POS);
					break;
				case ITEM_MODIFIER_POS:
					newcode.set_item_modifier(ITEM_MODIFIER_NEG);
					break;
				case ITEM_MODIFIER_NEG:
					newcode.set_item_modifier(ITEM_MODIFIER_REVERSE);
					break;
				default:
				case ITEM_MODIFIER_REVERSE:
					newcode.set_item_modifier(ITEM_MODIFIER_NONE);
					break;
				}

				// back up over the previous code so we can re-append
				if (has_or)
					m_sequence.backspace();
				m_sequence.backspace();
			}
			else if (ITEM_CLASS_RELATIVE == newclass)
			{
				// increment the modifier, wrapping back to none
				switch (lastcode.item_modifier())
				{
				case ITEM_MODIFIER_NONE:
					newcode.set_item_modifier(ITEM_MODIFIER_REVERSE);
					break;
				default:
				case ITEM_MODIFIER_REVERSE:
					newcode.set_item_modifier(ITEM_MODIFIER_NONE);
					break;
				}

				// back up over the previous code so we can re-append
				if (has_or)
					m_sequence.backspace();
				m_sequence.backspace();
			}
			else if (!has_or && (ITEM_CLASS_SWITCH == newclass))
			{
				// back up over the existing code
				m_sequence.backspace();

				// if there was a NOT preceding it, delete it as well, otherwise append a fresh one
				if (m_sequence[curlen - 2] == input_seq::not_code)
					m_sequence.backspace();
				else
					m_sequence += input_seq::not_code;
			}
		}
		else if (!has_or && (ITEM_CLASS_SWITCH == newclass))
		{
			// ignore switches following axes
			if (ITEM_CLASS_SWITCH != lastcode.item_class())
			{
				// hack to stop it timing out so user can cancel
				m_sequence.backspace();
				newcode = lastcode;
			}
		}
	}

	// hack to stop it timing out before assigning an axis
	if (ITEM_CLASS_SWITCH == newcode.item_class())
	{
		m_sequence += newcode;
		set_modified();
		return INPUT_CODE_INVALID;
	}
	else
	{
		return newcode;
	}
}



switch_sequence_poller::switch_sequence_poller(input_manager &manager) noexcept :
	input_sequence_poller(),
	m_code_poller(manager)
{
}


void switch_sequence_poller::do_start()
{
	// wait for any inputs that are already active to be released
	m_code_poller.reset();
	for (input_code dummycode = KEYCODE_ENTER; INPUT_CODE_INVALID != dummycode; )
		dummycode = m_code_poller.poll();
}


input_code switch_sequence_poller::do_poll()
{
	// switch case: see if we have a new code to process
	int const curlen = m_sequence.length();
	input_code lastcode = m_sequence[curlen - 1];
	input_code newcode = m_code_poller.poll();
	if (INPUT_CODE_INVALID != newcode)
	{
		// if code is duplicate, toggle the NOT state on the code
		if (curlen && (newcode == lastcode))
		{
			// back up over the existing code
			m_sequence.backspace();

			// if there was a NOT preceding it, delete it as well, otherwise append a fresh one
			if (m_sequence[curlen - 2] == input_seq::not_code)
				m_sequence.backspace();
			else
				m_sequence += input_seq::not_code;
		}
	}
	return newcode;
}