FreeNOS
Keyboard.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009 Niek Linnenbank
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <FreeNOS/User.h>
19 #include <Macros.h>
20 #include "Keyboard.h"
21 
26 const char Keyboard::keymap[0x3a][2] =
27 {
28  /*00*/{0x0, 0x0}, {0x0, 0x0}, {'1', '!'}, {'2', '@'},
29  /*04*/{'3', '#'}, {'4', '$'}, {'5', '%'}, {'6', '^'},
30  /*08*/{'7', '&'}, {'8', '*'}, {'9', '('}, {'0', ')'},
31  /*0c*/{'-', '_'}, {'=', '+'}, {'\b','\b'},{'\t','\t'},
32  /*10*/{'q', 'Q'}, {'w', 'W'}, {'e', 'E'}, {'r', 'R'},
33  /*14*/{'t', 'T'}, {'y', 'Y'}, {'u', 'U'}, {'i', 'I'},
34  /*18*/{'o', 'O'}, {'p', 'P'}, {'[', '{'}, {']', '}'},
35  /*1c*/{'\n','\n'},{0x0, 0x0}, {'a', 'A'}, {'s', 'S'},
36  /*20*/{'d', 'D'}, {'f', 'F'}, {'g', 'G'}, {'h', 'H'},
37  /*24*/{'j', 'J'}, {'k', 'K'}, {'l', 'L'}, {';', ':'},
38  /*28*/{'\'','\"'},{'`', '~'}, {0x0, 0x0}, {'\\','|'},
39  /*2c*/{'z', 'Z'}, {'x', 'X'}, {'c', 'C'}, {'v', 'V'},
40  /*30*/{'b', 'B'}, {'n', 'N'}, {'m', 'M'}, {',', '<'},
41  /*34*/{'.', '>'}, {'/', '?'}, {0x0, 0x0}, {'*', '*'},
42  /*38*/{0x0, 0x0}, {' ', ' '}
43 };
44 
45 Keyboard::Keyboard(const u32 inode)
47  , shiftState(ZERO)
48 {
49  m_identifier << "keyboard0";
50 }
51 
53 {
54  return FileSystem::Success;
55 }
56 
58 {
59  pending = true;
60  return FileSystem::Success;
61 }
62 
64  Size & size,
65  const Size offset)
66 {
67  Size bytes = 0;
68 
69  // Do we have any new key events?
70  if (pending)
71  {
72  pending = false;
73 
74  // Read byte from the keyboard.
75  u8 keycode = m_io.inb(PS2_PORT);
76 
77  // Update shift state
78  if (keycode == 0x2a || keycode == 0xaa)
79  {
80  shiftState ^= 1;
81  }
82  // Don't do anything on release
83  else if (!(keycode & PS2_RELEASE) &&
84  (keymap[keycode & 0x7f][shiftState]))
85  {
86  // Write to buffer
87  buffer.write((void *) &keymap[keycode & 0x7f][shiftState], 1);
88  bytes = 1;
89  }
90  // Re-enable interrupt
92  }
93 
94  if (bytes > 0)
95  {
96  size = bytes;
97  return FileSystem::Success;
98  }
99  else
100  {
101  return FileSystem::RetryAgain;
102  }
103 }
Keyboard::read
virtual FileSystem::Result read(IOBuffer &buffer, Size &size, const Size offset)
Read a character from the keyboard.
Definition: Keyboard.cpp:63
Macros.h
PS2_RELEASE
#define PS2_RELEASE
Bit is set in the scancode, if a key is released.
Definition: Keyboard.h:41
EnableIRQ
@ EnableIRQ
Definition: ProcessCtl.h:44
Keyboard::m_io
Arch::IO m_io
Port I/O object.
Definition: Keyboard.h:105
Device
Abstract device class interface.
Definition: Device.h:35
Keyboard::pending
bool pending
Do we have a byte ready?
Definition: Keyboard.h:102
FileSystem::Success
@ Success
Definition: FileSystem.h:54
ProcessCtl
API::Result ProcessCtl(const ProcessID proc, const ProcessOperation op, const Address addr=0, const Address output=0)
Prototype for user applications.
Definition: ProcessCtl.h:93
Keyboard::interrupt
virtual FileSystem::Result interrupt(const Size vector)
Executed when a key state has changed.
Definition: Keyboard.cpp:57
Keyboard::shiftState
u8 shiftState
State of the shift key.
Definition: Keyboard.h:99
SELF
#define SELF
Definition: ProcessID.h:35
Keyboard.h
IOBuffer
Abstract Input/Output buffer.
Definition: IOBuffer.h:37
u32
unsigned int u32
Unsigned 32-bit number.
Definition: Types.h:53
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
PS2_IRQ
#define PS2_IRQ
Interrupt number of the i8042 controller.
Definition: Keyboard.h:38
Device::m_identifier
String m_identifier
Unique identifier for this Device.
Definition: Device.h:79
FileSystem::CharacterDeviceFile
@ CharacterDeviceFile
Definition: FileSystem.h:75
Keyboard::keymap
static const char keymap[0x3a][2]
Keyboard map table.
Definition: Keyboard.h:92
FileSystem::Result
Result
Result code for filesystem Actions.
Definition: FileSystem.h:52
u8
unsigned char u8
Unsigned 8-bit number.
Definition: Types.h:59
FileSystem::RetryAgain
@ RetryAgain
Definition: FileSystem.h:57
Keyboard::initialize
virtual FileSystem::Result initialize()
Initialize the device.
Definition: Keyboard.cpp:52
FileSystem
Definition: FileSystem.h:31
Keyboard::Keyboard
Keyboard(const u32 inode)
Constructor.
Definition: Keyboard.cpp:45
IOBuffer::write
FileSystem::Result write(const void *buffer, const Size size, const Size offset=ZERO)
Write bytes to the I/O buffer.
Definition: IOBuffer.cpp:180
ZERO
#define ZERO
Zero value.
Definition: Macros.h:43
PS2_PORT
#define PS2_PORT
PS2 Keyboard input port.
Definition: Keyboard.h:35