MonkOS  v0.1
A simple 64-bit operating system (x86_64)
keyboard.h
Go to the documentation of this file.
1 //============================================================================
2 /// @file keyboard.h
3 /// @brief Keyboard input routines.
4 //
5 // Copyright 2016 Brett Vickers.
6 // Use of this source code is governed by a BSD-style license
7 // that can be found in the MonkOS LICENSE file.
8 //============================================================================
9 
10 #pragma once
11 
12 #include <core.h>
13 
14 //----------------------------------------------------------------------------
15 // Constants
16 //----------------------------------------------------------------------------
17 
18 // Keyboard break codes
19 #define KEYBRK_DOWN 0 ///< Key-down break code
20 #define KEYBRK_UP 1 ///< Key-up break code
21 
22 // Meta-key bit masks
23 #define META_SHIFT (1 << 0) ///< Set while the shift key is pressed.
24 #define META_CTRL (1 << 1) ///< Set while the ctrl key is pressed.
25 #define META_ALT (1 << 2) ///< Set while the alt key is pressed.
26 #define META_ESCAPED (1 << 3) ///< Set if key's scan code is escaped.
27 #define META_CAPSLOCK (1 << 4) ///< Set while caps lock is on.
28 #define META_NUMLOCK (1 << 5) ///< Set while num lock is on.
29 #define META_SCRLOCK (1 << 6) ///< Set while scroll lock is on.
30 
31 //----------------------------------------------------------------------------
32 // @enum keycode_t
33 /// @brief Key code values representing individual keys on the keyboard.
34 /// @details Key codes corresponding to printable characters are not
35 /// listed here, but they are equal in value to their lowercase
36 /// ASCII representations (e.g., KEY_A = 'a', KEY_1 = '1', etc.).
37 //----------------------------------------------------------------------------
38 enum keycode
39 {
40  KEY_BACKSPACE = 0x08,
41  KEY_TAB = 0x09,
42  KEY_ENTER = 0x0d,
43  KEY_ESCAPE = 0x1b,
44  KEY_CTRL = 0x81,
45  KEY_SHIFT = 0x82,
46  KEY_ALT = 0x83,
47  KEY_PRTSCR = 0x90,
48  KEY_CAPSLOCK = 0x91,
49  KEY_NUMLOCK = 0x92,
50  KEY_SCRLOCK = 0x93,
51  KEY_INSERT = 0xa0,
52  KEY_END = 0xa1,
53  KEY_DOWN = 0xa2,
54  KEY_PGDN = 0xa3,
55  KEY_LEFT = 0xa4,
56  KEY_CENTER = 0xa5, ///< Keypad Center
57  KEY_RIGHT = 0xa6,
58  KEY_HOME = 0xa7,
59  KEY_UP = 0xa8,
60  KEY_PGUP = 0xa9,
61  KEY_DEL = 0xaa,
62  KEY_MINUS = 0xab, ///< Keypad Minus
63  KEY_PLUS = 0xac, ///< Keypad Plus
64  KEY_F1 = 0xb0,
65  KEY_F2 = 0xb1,
66  KEY_F3 = 0xb2,
67  KEY_F4 = 0xb3,
68  KEY_F5 = 0xb4,
69  KEY_F6 = 0xb5,
70  KEY_F7 = 0xb6,
71  KEY_F8 = 0xb7,
72  KEY_F9 = 0xb8,
73  KEY_F10 = 0xb9,
74  KEY_F11 = 0xba,
75  KEY_F12 = 0xbb,
76  KEY_SCANESC = 0xfe, ///< Escaped scan code
77  KEY_INVALID = 0xff, ///< Invalid scan code
78 };
79 
80 typedef enum keycode keycode_t;
81 
82 //----------------------------------------------------------------------------
83 // @struct key_t
84 /// @brief A record representing the state of the keyboard at the time
85 /// a key was presssed or unpressed.
86 //----------------------------------------------------------------------------
87 struct key
88 {
89  uint8_t brk; ///< KEYBRK_UP or KEYBRK_DOWN
90  uint8_t meta; ///< Metakey mask when key was generated.
91  uint8_t code; ///< Keycode value (type = keycode_t).
92  uint8_t ch; ///< Character value, if valid.
93 };
94 
95 typedef struct key key_t;
96 
97 //----------------------------------------------------------------------------
98 // @struct keylayout_t
99 /// @brief A map of keyboard scan codes to key codes.
100 //----------------------------------------------------------------------------
101 struct keylayout
102 {
103  uint8_t shifted[128]; ///< when shift key is down.
104  uint8_t unshifted[128]; ///< when shift key is up.
105 };
106 
107 typedef struct keylayout keylayout_t;
108 
109 //----------------------------------------------------------------------------
110 // @function kb_init
111 /// @brief Initialize the keyboard so that it can provide input to the
112 /// kernel.
113 /// @details kb_init installs the default US English PS/2 keyboard layout.
114 //----------------------------------------------------------------------------
115 void
116 kb_init();
117 
118 //----------------------------------------------------------------------------
119 // @function kb_setlayout
120 /// @brief Install a new keyboard layout.
121 /// @param[in] layout The keyboard layout to install.
122 //----------------------------------------------------------------------------
123 void
124 kb_setlayout(keylayout_t *layout);
125 
126 //----------------------------------------------------------------------------
127 // @function kb_getchar
128 /// @brief Return the next available character from the keyboard's
129 /// input buffer.
130 /// @returns The ascii value of the next character in the input buffer,
131 /// or 0 if there are no characters available.
132 //----------------------------------------------------------------------------
133 char
134 kb_getchar();
135 
136 //----------------------------------------------------------------------------
137 // @function kb_getkey
138 /// @brief Return the available next key from the keyboard's input
139 /// buffer.
140 /// @param[out] key The key record of the next key in the buffer.
141 /// @returns true if there is a key in the buffer, false otherwise.
142 //----------------------------------------------------------------------------
143 bool
144 kb_getkey(key_t *key);
145 
146 //----------------------------------------------------------------------------
147 // @function kb_meta
148 /// @brief Return the current meta-key bit mask.
149 /// @returns The meta-key bitmask.
150 //----------------------------------------------------------------------------
151 uint8_t
152 kb_meta();
void kb_init()
Initialize the keyboard so that it can provide input to the kernel.
Definition: keyboard.c:268
bool kb_getkey(key_t *key)
Return the available next key from the keyboard&#39;s input buffer.
Definition: keyboard.c:318
uint8_t ch
Character value, if valid.
Definition: keyboard.h:92
Core include file.
A map of keyboard scan codes to key codes.
Definition: keyboard.h:101
Keypad Minus.
Definition: keyboard.h:62
void kb_setlayout(keylayout_t *layout)
Install a new keyboard layout.
Definition: keyboard.c:288
Keypad Plus.
Definition: keyboard.h:63
uint8_t code
Keycode value (type = keycode_t).
Definition: keyboard.h:91
Invalid scan code.
Definition: keyboard.h:77
uint8_t brk
KEYBRK_UP or KEYBRK_DOWN.
Definition: keyboard.h:89
char kb_getchar()
Return the next available character from the keyboard&#39;s input buffer.
Definition: keyboard.c:294
Escaped scan code.
Definition: keyboard.h:76
enum keycode keycode_t
Definition: keyboard.h:80
uint8_t meta
Metakey mask when key was generated.
Definition: keyboard.h:90
Keypad Center.
Definition: keyboard.h:56
keycode
Key code values representing individual keys on the keyboard.
Definition: keyboard.h:38
A record representing the state of the keyboard at the time a key was presssed or unpressed...
Definition: keyboard.h:87
uint8_t kb_meta()
Return the current meta-key bit mask.
Definition: keyboard.c:340