summaryrefslogtreecommitdiffstatshomepage
path: root/src/frontend/mame/ui/keyboard.cpp
blob: 7efebefd64c4902c214835430167f5c7e4b04cda (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
// license:BSD-3-Clause
// copyright-holders:Vas Crabb
/***************************************************************************

    ui/keyboard.cpp

    Keyboard mode menu.

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

#include "emu.h"
#include "ui/keyboard.h"

#include "natkeyboard.h"


namespace ui {

namespace {

constexpr uintptr_t ITEM_KBMODE         = 0x00000100;
constexpr uintptr_t ITEM_KBDEV_FIRST    = 0x00000200;

} // anonymous namespace


menu_keyboard_mode::menu_keyboard_mode(mame_ui_manager &mui, render_container &container) : menu(mui, container)
{
	set_heading(_("menu-keyboard", "Keyboard Selection"));
}

void menu_keyboard_mode::menu_activated()
{
	// scripts could have changed something behind our back
	reset(reset_options::REMEMBER_POSITION);
}

void menu_keyboard_mode::populate(float &customtop, float &custombottom)
{
	natural_keyboard &natkbd(machine().natkeyboard());

	if (natkbd.can_post())
	{
		bool const natmode(natkbd.in_use());
		item_append(
				_("menu-keyboard", "Keyboard Mode"),
				natmode ? _("menu-keyboard", "Natural") : _("menu-keyboard", "Emulated"),
				natmode ? FLAG_LEFT_ARROW : FLAG_RIGHT_ARROW,
				reinterpret_cast<void *>(ITEM_KBMODE));
		item_append(menu_item_type::SEPARATOR);
	}

	uintptr_t ref(ITEM_KBDEV_FIRST);
	for (size_t i = 0; natkbd.keyboard_count() > i; ++i, ++ref)
	{
		device_t &kbddev(natkbd.keyboard_device(i));
		bool const enabled(natkbd.keyboard_enabled(i));
		item_append(
				util::string_format(
					kbddev.owner() ? _("%1$s [root%2$s]") : _("[root%2$s]"),
					kbddev.type().fullname(),
					kbddev.tag()),
				enabled ? _("Enabled") : _("Disabled"),
				enabled ? FLAG_LEFT_ARROW : FLAG_RIGHT_ARROW,
				reinterpret_cast<void *>(ref));
	}
	item_append(menu_item_type::SEPARATOR);
}

menu_keyboard_mode::~menu_keyboard_mode()
{
}

void menu_keyboard_mode::handle(event const *ev)
{
	if (ev && uintptr_t(ev->itemref))
	{
		natural_keyboard &natkbd(machine().natkeyboard());
		uintptr_t const ref(uintptr_t(ev->itemref));
		bool left(IPT_UI_LEFT == ev->iptkey);
		bool right(IPT_UI_RIGHT == ev->iptkey);
		if (ITEM_KBMODE == ref)
		{
			if (IPT_UI_SELECT == ev->iptkey)
			{
				left = natkbd.in_use();
				right = !left;
			}
			if ((left || right) && (natkbd.in_use() != right))
			{
				natkbd.set_in_use(right);
				ev->item->set_subtext(right ? _("menu-keyboard", "Natural") : _("menu-keyboard", "Emulated"));
				ev->item->set_flags(right ? FLAG_LEFT_ARROW : FLAG_RIGHT_ARROW);
			}
		}
		else if (ITEM_KBDEV_FIRST <= ref)
		{
			auto const kbdno(ref - ITEM_KBDEV_FIRST);
			if (IPT_UI_SELECT == ev->iptkey)
			{
				left = natkbd.keyboard_enabled(kbdno);
				right = !left;
			}
			if ((left || right) && (natkbd.keyboard_enabled(kbdno) != right))
			{
				if (right)
					natkbd.enable_keyboard(kbdno);
				else
					natkbd.disable_keyboard(kbdno);
				ev->item->set_subtext(right ? _("Enabled") : _("Disabled"));
				ev->item->set_flags(right ? FLAG_LEFT_ARROW : FLAG_RIGHT_ARROW);
			}
		}
	}
}

} // namespace ui