VirtualC64 v5.0 beta
Commodore 64 Emulator
Loading...
Searching...
No Matches
Keyboard.h
1// -----------------------------------------------------------------------------
2// This file is part of VirtualC64
3//
4// Copyright (C) Dirk W. Hoffmann. www.dirkwhoffmann.de
5// This FILE is dual-licensed. You are free to choose between:
6//
7// - The GNU General Public License v3 (or any later version)
8// - The Mozilla Public License v2
9//
10// SPDX-License-Identifier: GPL-3.0-or-later OR MPL-2.0
11// -----------------------------------------------------------------------------
12
13#pragma once
14
15#include "SubComponent.h"
16#include "CmdQueue.h"
17#include "C64Types.h"
18#include "C64Key.h"
19#include "Buffer.h"
20
21namespace vc64 {
22
23class Keyboard final : public SubComponent {
24
25 Descriptions descriptions = {{
26
27 .name = "Keyboard",
28 .description = "Keyboard"
29 }};
30
31 // The keyboard matrix (indexed by row or by column)
32 u8 kbMatrixRow[8] = { };
33 u8 kbMatrixCol[8] = { };
34
35 // The number of pressed keys in a certain row or column
36 u8 kbMatrixRowCnt[8] = { };
37 u8 kbMatrixColCnt[8] = { };
38
39 // Indicates if the shift lock is currently pressed
40 bool shiftLock = false;
41
42 // Delayed keyboard commands (auto-typing)
43 util::SortedRingBuffer<Cmd, 1024> pending;
44
45
46 //
47 // Methods
48 //
49
50public:
51
52 Keyboard(C64 &ref) : SubComponent(ref) { }
53
54 Keyboard& operator= (const Keyboard& other) {
55
56 CLONE_ARRAY(kbMatrixRow)
57 CLONE_ARRAY(kbMatrixCol)
58 CLONE_ARRAY(kbMatrixRowCnt)
59 CLONE_ARRAY(kbMatrixColCnt)
60 CLONE(shiftLock)
61 CLONE(pending)
62
63 return *this;
64 }
65
66
67 //
68 // Methods from Serializable
69 //
70
71public:
72
73 template <class T>
74 void serialize(T& worker)
75 {
76 worker
77
78 << kbMatrixRow
79 << kbMatrixCol
80 << kbMatrixRowCnt
81 << kbMatrixColCnt
82 << shiftLock;
83
84 } SERIALIZERS(serialize);
85
86
87 //
88 // Methods from CoreComponent
89 //
90
91public:
92
93 const Descriptions &getDescriptions() const override { return descriptions; }
94
95private:
96
97 void _dump(Category category, std::ostream& os) const override;
98 void _reset(bool hard) override;
99
100
101 //
102 // Accessing the keyboard matrix
103 //
104
105public:
106
107 // Reads a column or row from the keyboard matrix
108 u8 getColumnValues(u8 rowMask) const;
109 u8 getRowValues(u8 columnMask) const;
110 u8 getRowValues(u8 columnMask, u8 thresholdMask) const;
111
112 // Checks whether a certain key is pressed
113 bool isPressed(C64Key key) const;
114
115 // Presses or releases a key
116 void press(C64Key key);
117 void release(C64Key key);
118 void toggle(C64Key key) { isPressed(key) ? release(key) : press(key); }
119
120 // Clears the keyboard matrix
121 void releaseAll();
122
123
124 //
125 // Auto typing
126 //
127
128public:
129
130 // Auto-types a string
131 void autoType(const string &text);
132
133 // Discards all pending key events and clears the keyboard matrix
134 void abortAutoTyping();
135
136
137 //
138 // Processing commands and events
139 //
140
141public:
142
143 // Processes a command from the command queue
144 void processCommand(const Cmd &cmd);
145
146 // Processes the next auto-type event
147 void processKeyEvent(EventID id);
148};
149
150}
VirtualC64 project namespace.
Definition CmdQueue.cpp:16