summaryrefslogtreecommitdiffstatshomepage
path: root/src/osd/modules/input/input_win32.cpp
blob: 921c902ad31cc39880c5f448bfb53bc3f87c57e3 (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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
// license:BSD-3-Clause
// copyright-holders:Aaron Giles, Brad Hughes
//============================================================
//
//  input_win32.cpp - Win32 input implementation
//
//============================================================

#include "input_module.h"

#if defined(OSD_WINDOWS)

#include "input_windows.h"

#include "input_wincommon.h"

// osd/windows
#include "window.h"

// emu
#include "inpttype.h"

#include "strconv.h"

// standard windows headers
#include <tchar.h>


namespace osd {

namespace {

//============================================================
//  win32_keyboard_device
//============================================================

// This device is purely event driven so the implementation is in the module
class win32_keyboard_device : public event_based_device<KeyPressEventArgs>
{
public:
	win32_keyboard_device(std::string &&name, std::string &&id, input_module &module) :
		event_based_device(std::move(name), std::move(id), module),
		m_keyboard({{0}})
	{
	}

	virtual void reset() override
	{
		event_based_device::reset();
		memset(&m_keyboard, 0, sizeof(m_keyboard));
	}

	virtual void configure(input_device &device) override
	{
		keyboard_trans_table const &table = keyboard_trans_table::instance();

		for (int keynum = 0; keynum < MAX_KEYS; keynum++)
		{
			input_item_id itemid = table.map_di_scancode_to_itemid(keynum);
			TCHAR keyname[100];

			// generate the name
			// FIXME: GetKeyNameText gives bogus names for media keys and various other things
			// in many cases it ignores the "extended" bit and returns the key name corresponding to the scan code alone
			if (GetKeyNameText(((keynum & 0x7f) << 16) | ((keynum & 0x80) << 17), keyname, std::size(keyname)) == 0)
				_sntprintf(keyname, std::size(keyname), TEXT("Scan%03d"), keynum);
			std::string name = text::from_tstring(keyname);

			// add the item to the device
			device.add_item(
					name,
					util::string_format("SCAN%03d", keynum),
					itemid,
					generic_button_get_state<std::uint8_t>,
					&m_keyboard.state[keynum]);
		}
	}

protected:
	virtual void process_event(KeyPressEventArgs const &args) override
	{
		m_keyboard.state[args.scancode] = args.event_id == INPUT_EVENT_KEYDOWN ? 0x80 : 0x00;
	}

private:
	keyboard_state m_keyboard;
};


//============================================================
//  keyboard_input_win32 - win32 keyboard input module
//============================================================

class keyboard_input_win32 : public wininput_module<win32_keyboard_device>
{
public:
	keyboard_input_win32() : wininput_module<win32_keyboard_device>(OSD_KEYBOARDINPUT_PROVIDER, "win32")
	{
	}

	virtual void input_init(running_machine &machine) override
	{
		wininput_module<win32_keyboard_device>::input_init(machine);

		// Add a single win32 keyboard device that we'll monitor using Win32
		create_device<win32_keyboard_device>(DEVICE_CLASS_KEYBOARD, "Win32 Keyboard 1", "Win32 Keyboard 1");
	}

	virtual bool handle_input_event(input_event eventid, void const *eventdata) override
	{
		switch (eventid)
		{
		case INPUT_EVENT_KEYDOWN:
		case INPUT_EVENT_KEYUP:
			devicelist().for_each_device(
					[args = reinterpret_cast<KeyPressEventArgs const *>(eventdata)] (auto &device)
					{
						device.queue_events(args, 1);
					});
			return false; // we still want text input events to be generated

		default:
			return false;
		}
	}
};


//============================================================
//  win32_mouse_device
//============================================================

class win32_mouse_device : public event_based_device<MouseUpdateEventArgs>
{
public:
	win32_mouse_device(std::string &&name, std::string &&id, input_module &module) :
		event_based_device(std::move(name), std::move(id), module),
		m_mouse({0}),
		m_win32_mouse({{0}}),
		m_vscroll(0),
		m_hscroll(0)
	{
	}

	virtual void poll(bool relative_reset) override
	{
		event_based_device::poll(relative_reset);

		if (!relative_reset)
			return;

		CURSORINFO cursor_info = { 0 };
		cursor_info.cbSize = sizeof(CURSORINFO);
		GetCursorInfo(&cursor_info);

		// We only take over the mouse if the cursor isn't showing
		// This should happen anyway in mouse mode
		if (!(cursor_info.flags & CURSOR_SHOWING))
		{
			// We measure the position change from the previously set center position
			m_mouse.lX = (cursor_info.ptScreenPos.x - m_win32_mouse.last_point.x) * input_device::RELATIVE_PER_PIXEL;
			m_mouse.lY = (cursor_info.ptScreenPos.y - m_win32_mouse.last_point.y) * input_device::RELATIVE_PER_PIXEL;

			RECT window_pos = {0};
			GetWindowRect(
					dynamic_cast<win_window_info &>(*osd_common_t::window_list().front()).platform_window(),
					&window_pos);

			// We reset the cursor position to the middle of the window each frame
			m_win32_mouse.last_point.x = window_pos.left + (window_pos.right - window_pos.left) / 2;
			m_win32_mouse.last_point.y = window_pos.top + (window_pos.bottom - window_pos.top) / 2;

			SetCursorPos(m_win32_mouse.last_point.x, m_win32_mouse.last_point.y);
		}

		// update scroll axes
		m_mouse.lV = std::exchange(m_vscroll, 0) * input_device::RELATIVE_PER_PIXEL;
		m_mouse.lH = std::exchange(m_hscroll, 0) * input_device::RELATIVE_PER_PIXEL;
	}

	virtual void configure(input_device &device) override
	{
		// populate the axes
		device.add_item(
				"X",
				std::string_view(),
				ITEM_ID_XAXIS,
				generic_axis_get_state<LONG>,
				&m_mouse.lX);
		device.add_item(
				"Y",
				std::string_view(),
				ITEM_ID_YAXIS,
				generic_axis_get_state<LONG>,
				&m_mouse.lY);
		device.add_item(
				"Scroll V",
				std::string_view(),
				ITEM_ID_ZAXIS,
				generic_axis_get_state<LONG>,
				&m_mouse.lV);
		device.add_item(
				"Scroll H",
				std::string_view(),
				ITEM_ID_RZAXIS,
				generic_axis_get_state<LONG>,
				&m_mouse.lH);

		// populate the buttons
		for (int butnum = 0; butnum < 5; butnum++)
		{
			device.add_item(
					default_button_name(butnum),
					std::string_view(),
					input_item_id(ITEM_ID_BUTTON1 + butnum),
					generic_button_get_state<BYTE>,
					&m_mouse.rgbButtons[butnum]);
		}
	}

	virtual void reset() override
	{
		event_based_device::reset();
		memset(&m_mouse, 0, sizeof(m_mouse));
		memset(&m_win32_mouse, 0, sizeof(m_win32_mouse));
		m_vscroll = m_hscroll = 0;
	}

protected:
	virtual void process_event(MouseUpdateEventArgs const &args) override
	{
		// set the button state
		assert(!(args.pressed & args.released));
		for (unsigned i = 0; 5 > i; ++i)
		{
			if (BIT(args.pressed, i))
				m_mouse.rgbButtons[i] = 0x80;
			else if (BIT(args.released, i))
				m_mouse.rgbButtons[i] = 0x00;
		}

		// accumulate scroll delta
		m_vscroll += args.vdelta;
		m_hscroll += args.hdelta;
	}

private:
	struct win32_mouse_state
	{
		POINT last_point;
	};

	mouse_state         m_mouse;
	win32_mouse_state   m_win32_mouse;
	long                m_vscroll, m_hscroll;
};


//============================================================
//  mouse_input_win32 - win32 mouse input module
//============================================================

class mouse_input_win32 : public wininput_module<win32_mouse_device>
{
public:
	mouse_input_win32() : wininput_module<win32_mouse_device>(OSD_MOUSEINPUT_PROVIDER, "win32")
	{
	}

	virtual void input_init(running_machine &machine) override
	{
		wininput_module<win32_mouse_device>::input_init(machine);

		if (!options()->mouse())
			return;

		// allocate a device
		create_device<win32_mouse_device>(DEVICE_CLASS_MOUSE, "Win32 Mouse 1", "Win32 Mouse 1");
	}

	virtual bool handle_input_event(input_event eventid, void const *eventdata) override
	{
		if (manager().class_enabled(DEVICE_CLASS_MOUSE))
		{
			if ((eventid == INPUT_EVENT_MOUSE_BUTTON) || (eventid == INPUT_EVENT_MOUSE_WHEEL))
			{
				auto const *const args = reinterpret_cast<MouseUpdateEventArgs const *>(eventdata);
				devicelist().for_each_device(
						[args] (auto &device) { device.queue_events(args, 1); });
				return true;
			}
		}

		return false;
	}
};


//============================================================
//  win32_lightgun_device_base
//============================================================

class win32_lightgun_device_base : public event_based_device<MouseUpdateEventArgs>
{
public:
	virtual void reset() override
	{
		event_based_device::reset();
		memset(&m_mouse, 0, sizeof(m_mouse));
	}

protected:
	win32_lightgun_device_base(
			std::string &&name,
			std::string &&id,
			input_module &module) :
		event_based_device(std::move(name), std::move(id), module),
		m_mouse({ 0 })
	{
	}

	void do_configure(input_device &device, unsigned buttons)
	{
		// populate the axes
		for (int axisnum = 0; axisnum < 2; axisnum++)
		{
			device.add_item(
					default_axis_name[axisnum],
					std::string_view(),
					input_item_id(ITEM_ID_XAXIS + axisnum),
					generic_axis_get_state<LONG>,
					&m_mouse.lX + axisnum);
		}

		// populate the buttons
		for (int butnum = 0; butnum < buttons; butnum++)
		{
			device.add_item(
					default_button_name(butnum),
					std::string_view(),
					input_item_id(ITEM_ID_BUTTON1 + butnum),
					generic_button_get_state<BYTE>,
					&m_mouse.rgbButtons[butnum]);
		}
	}

	mouse_state m_mouse;
};



//============================================================
//  win32_lightgun_device
//============================================================

class win32_lightgun_device : public win32_lightgun_device_base
{
public:
	win32_lightgun_device(
			std::string &&name,
			std::string &&id,
			input_module &module) :
		win32_lightgun_device_base(std::move(name), std::move(id), module),
		m_vscroll(0),
		m_hscroll(0)
	{
	}

	virtual void poll(bool relative_reset) override
	{
		event_based_device::poll(relative_reset);

		int32_t xpos = 0, ypos = 0;

		// get the cursor position and transform into final results
		POINT mousepos;
		GetCursorPos(&mousepos);
		if (!osd_common_t::window_list().empty())
		{
			// get the position relative to the window
			HWND const hwnd = dynamic_cast<win_window_info &>(*osd_common_t::window_list().front()).platform_window();
			RECT client_rect;
			GetClientRect(hwnd, &client_rect);
			ScreenToClient(hwnd, &mousepos);

			// convert to absolute coordinates
			xpos = normalize_absolute_axis(mousepos.x, client_rect.left, client_rect.right);
			ypos = normalize_absolute_axis(mousepos.y, client_rect.top, client_rect.bottom);
		}

		// update the X/Y positions
		m_mouse.lX = xpos;
		m_mouse.lY = ypos;

		// update the scroll axes if appropriate
		if (relative_reset)
		{
			m_mouse.lV = std::exchange(m_vscroll, 0) * input_device::RELATIVE_PER_PIXEL;
			m_mouse.lH = std::exchange(m_hscroll, 0) * input_device::RELATIVE_PER_PIXEL;
		}
	}

	virtual void configure(input_device &device) override
	{
		do_configure(device, 5);

		// add scroll axes
		device.add_item(
				"Scroll V",
				std::string_view(),
				ITEM_ID_ADD_RELATIVE1,
				generic_axis_get_state<LONG>,
				&m_mouse.lV);
		device.add_item(
				"Scroll H",
				std::string_view(),
				ITEM_ID_ADD_RELATIVE2,
				generic_axis_get_state<LONG>,
				&m_mouse.lH);
	}

	virtual void reset() override
	{
		win32_lightgun_device_base::reset();
		m_vscroll = m_hscroll = 0;
	}

protected:
	virtual void process_event(MouseUpdateEventArgs const &args) override
	{
		// In non-shared axis mode, just update the button state
		assert(!(args.pressed & args.released));
		for (unsigned i = 0; 5 > i; ++i)
		{
			if (BIT(args.pressed, i))
				m_mouse.rgbButtons[i] = 0x80;
			else if (BIT(args.released, i))
				m_mouse.rgbButtons[i] = 0x00;
		}

		// accumulate scroll delta
		m_vscroll += args.vdelta;
		m_hscroll += args.hdelta;
	}

private:
	long                m_vscroll, m_hscroll;
};


//============================================================
//  win32_dual_lightgun_device
//============================================================

class win32_dual_lightgun_device : public win32_lightgun_device_base
{
public:
	win32_dual_lightgun_device(
			std::string &&name,
			std::string &&id,
			input_module &module,
			int index) :
		win32_lightgun_device_base(std::move(name), std::move(id), module),
		m_gun_index(index)
	{
	}

	virtual void configure(input_device &device) override
	{
		do_configure(device, 2);
	}

protected:
	virtual void process_event(MouseUpdateEventArgs const &args) override
	{
		// We only handle the first four buttons in shared axis mode
		assert(!(args.pressed & args.released));
		for (unsigned i = 0; 2 > i; ++i)
		{
			// Adjust the button if we're the second lightgun
			unsigned const bit = i + ((1 == m_gun_index) ? 2 : 0);
			if (BIT(args.pressed, bit))
			{
				m_mouse.rgbButtons[i] = 0x80;

				// get the position relative to the window
				HWND const hwnd = dynamic_cast<win_window_info &>(*osd_common_t::window_list().front()).platform_window();
				RECT client_rect;
				GetClientRect(hwnd, &client_rect);

				POINT mousepos;
				mousepos.x = args.xpos;
				mousepos.y = args.ypos;
				ScreenToClient(hwnd, &mousepos);

				// convert to absolute coordinates
				m_mouse.lX = normalize_absolute_axis(mousepos.x, client_rect.left, client_rect.right);
				m_mouse.lY = normalize_absolute_axis(mousepos.y, client_rect.top, client_rect.bottom);
			}
			else if (BIT(args.released, bit))
			{
				m_mouse.rgbButtons[i] = 0x00;
			}
		}
	}

private:
	int const m_gun_index;
};


//============================================================
//  lightgun_input_win32 - win32 lightgun input module
//============================================================

class lightgun_input_win32 : public wininput_module<win32_lightgun_device_base>
{
public:
	lightgun_input_win32() : wininput_module<win32_lightgun_device_base>(OSD_LIGHTGUNINPUT_PROVIDER, "win32")
	{
	}

	virtual void input_init(running_machine &machine) override
	{
		wininput_module<win32_lightgun_device_base>::input_init(machine);

		bool const shared_axis_mode = dynamic_cast<windows_options const &>(*options()).dual_lightgun();
		int const max_guns = shared_axis_mode ? 2 : 1;

		// allocate the lightgun devices
		for (int gunnum = 0; gunnum < max_guns; gunnum++)
		{
			static const char *const gun_names[] = { "Win32 Gun 1", "Win32 Gun 2" };

			// allocate a device
			if (shared_axis_mode)
				create_device<win32_dual_lightgun_device>(DEVICE_CLASS_LIGHTGUN, gun_names[gunnum], gun_names[gunnum], gunnum);
			else
				create_device<win32_lightgun_device>(DEVICE_CLASS_LIGHTGUN, gun_names[gunnum], gun_names[gunnum]);
		}
	}

	virtual bool handle_input_event(input_event eventid, void const *eventdata) override
	{
		if (manager().class_enabled(DEVICE_CLASS_LIGHTGUN))
		{
			if ((eventid == INPUT_EVENT_MOUSE_BUTTON) || (eventid == INPUT_EVENT_MOUSE_WHEEL))
			{
				auto const *const args = reinterpret_cast<MouseUpdateEventArgs const *>(eventdata);
				devicelist().for_each_device(
						[args] (auto &device) { device.queue_events(args, 1); });
				return true;
			}
		}

		return false;
	}
};

} // anonymous namespace

} // namespace osd

#else // defined(OSD_WINDOWS)

namespace osd {

namespace {

MODULE_NOT_SUPPORTED(keyboard_input_win32, OSD_KEYBOARDINPUT_PROVIDER, "win32")
MODULE_NOT_SUPPORTED(mouse_input_win32, OSD_MOUSEINPUT_PROVIDER, "win32")
MODULE_NOT_SUPPORTED(lightgun_input_win32, OSD_LIGHTGUNINPUT_PROVIDER, "win32")

} // anonymous namespace

} // namespace osd

#endif // defined(OSD_WINDOWS)

MODULE_DEFINITION(KEYBOARDINPUT_WIN32, osd::keyboard_input_win32)
MODULE_DEFINITION(MOUSEINPUT_WIN32, osd::mouse_input_win32)
MODULE_DEFINITION(LIGHTGUNINPUT_WIN32, osd::lightgun_input_win32)