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
|
// license:BSD-3-Clause
// copyright-holders:Brad Hughes
//============================================================
//
// input_uwp.cpp - UWP input implementation
//
//============================================================
#include "input_module.h"
#include "modules/osdmodule.h"
#if defined(OSD_UWP)
#include <agile.h>
#include <ppltasks.h>
#include <collection.h>
#undef min
#undef max
#undef interface
// MAME headers
#include "emu.h"
#include "uiinput.h"
// MAMEOS headers
#include "winmain.h"
#include "input_common.h"
#include "input_windows.h"
using namespace concurrency;
using namespace Windows::UI::Core;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
// This device is purely event driven so the implementation is in the module
class uwp_keyboard_device : public event_based_device<KeyPressEventArgs>
{
public:
keyboard_state keyboard;
uwp_keyboard_device(running_machine& machine, const char *name, const char *id, input_module &module)
: event_based_device(machine, name, id, DEVICE_CLASS_KEYBOARD, module),
keyboard({{0}})
{
}
void reset() override
{
memset(&keyboard, 0, sizeof(keyboard));
}
protected:
void process_event(KeyPressEventArgs &args) override
{
keyboard.state[args.vkey] = args.event_id == INPUT_EVENT_KEYDOWN ? 0x80 : 0x00;
}
};
private ref class key_processor sealed
{
private:
Platform::Agile<CoreWindow^> m_window;
input_device_list &m_devicelist;
running_machine &m_machine;
internal:
key_processor(CoreWindow^ window, input_device_list &devicelist, running_machine &machine)
: m_window(window),
m_devicelist(devicelist),
m_machine(machine)
{
m_window->KeyDown += ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>(this, &key_processor::OnKeyDown);
m_window->KeyUp += ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>(this, &key_processor::OnKeyUp);
m_window->CharacterReceived += ref new TypedEventHandler<CoreWindow^, CharacterReceivedEventArgs^>(this, &key_processor::OnCharacterReceived);
}
void OnKeyDown(CoreWindow^ win, KeyEventArgs^ args)
{
auto &table = keyboard_trans_table::instance();
//if (args->VirtualKey < Windows::System::VirtualKey::Space)
// m_machine.ui_input().push_char_event(osd_common_t::s_window_list.front()->target(), static_cast<char32_t>(args->VirtualKey));
KeyPressEventArgs tmp;
tmp.event_id = INPUT_EVENT_KEYDOWN;
tmp.vkey = uint8_t(args->VirtualKey);
m_devicelist.for_each_device([&tmp](device_info *device)
{
static_cast<uwp_keyboard_device*>(device)->queue_events(&tmp, 1);
});
}
void OnKeyUp(CoreWindow^ win, KeyEventArgs^ args)
{
KeyPressEventArgs tmp;
tmp.event_id = INPUT_EVENT_KEYUP;
tmp.vkey = uint8_t(args->VirtualKey);
m_devicelist.for_each_device([&tmp](device_info *device)
{
static_cast<uwp_keyboard_device*>(device)->queue_events(&tmp, 1);
});
}
void OnCharacterReceived(CoreWindow ^sender, CharacterReceivedEventArgs ^args)
{
m_machine.ui_input().push_char_event(osd_common_t::s_window_list.front()->target(), args->KeyCode);
}
};
//============================================================
// uwp_keyboard_module
//============================================================
class uwp_keyboard_module : public wininput_module
{
private:
key_processor^ m_key_processor;
public:
uwp_keyboard_module()
: wininput_module(OSD_KEYBOARDINPUT_PROVIDER, "uwp"),
m_key_processor(nullptr)
{
}
virtual void input_init(running_machine &machine) override
{
auto first_window = std::static_pointer_cast<uwp_window_info>(osd_common_t::s_window_list.front());
m_key_processor = ref new key_processor(first_window->uwp_window(), *devicelist(), machine);
// Add a single UWP keyboard device
uwp_keyboard_device *devinfo = devicelist()->create_device<uwp_keyboard_device>(machine, "UWP Keyboard 1", "UWP Keyboard 1", *this);
keyboard_trans_table &table = keyboard_trans_table::instance();
// populate it
for (int keynum = 0; keynum < MAX_KEYS; keynum++)
{
input_item_id itemid = table.map_vkey_to_itemid(Windows::System::VirtualKey(keynum));
char name[20];
// generate/fetch the name
_snprintf(name, ARRAY_LENGTH(name), "Scan%03d", keynum);
// add the item to the device
devinfo->device()->add_item(name, itemid, generic_button_get_state<std::uint8_t>, &devinfo->keyboard.state[keynum]);
}
}
void exit() override
{
m_key_processor = nullptr;
}
};
#else
MODULE_NOT_SUPPORTED(uwp_keyboard_module, OSD_KEYBOARDINPUT_PROVIDER, "uwp")
#endif
MODULE_DEFINITION(KEYBOARDINPUT_UWP, uwp_keyboard_module)
|