The Pedigree Project 0.1
hosted/Keyboard.cc
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#include "Keyboard.h"
21#include "pedigree/kernel/machine/Device.h"
22#include "pedigree/kernel/machine/HidInputManager.h"
23#include "pedigree/kernel/machine/KeymapManager.h"
24#include "pedigree/kernel/machine/Machine.h"
25
26#if DEBUGGER
27#include "pedigree/kernel/debugger/Debugger.h"
28
29#if TRACK_PAGE_ALLOCATIONS
30#include "pedigree/kernel/debugger/commands/AllocationCommand.h"
31#endif
32#endif
33
34#if DEBUGGER
35#include "pedigree/kernel/debugger/commands/SlamCommand.h"
36#endif
37
38#include "pedigree/kernel/core/SlamAllocator.h"
39
40namespace __pedigree_hosted {};
41using namespace __pedigree_hosted;
42
43#include <fcntl.h>
44#include <termios.h>
45#include <unistd.h>
46
47HostedKeyboard::HostedKeyboard() : m_bDebugState(false) {
48 // Eat any pending input.
49 blocking(false);
50 char buf[64] = {0};
51 while (read(0, buf, 64) == 64)
52 ;
53 blocking(true);
54}
55
56HostedKeyboard::~HostedKeyboard() {}
57
59 // Don't echo characters.
60 struct termios t;
61 tcgetattr(0, &t);
62 t.c_lflag &= ~(ICANON | ECHO);
63 tcsetattr(0, TCSAFLUSH, &t);
64}
65
67 if (!getDebugState())
68 return 0;
69
70 blocking(true);
71
72 char buf[2] = {0};
73 ssize_t n = read(0, buf, 1);
74 if (n != 1)
75 return 0;
76 return buf[0];
77}
78
80 if (!getDebugState())
81 return 0;
82
83 blocking(false);
84
85 char buf[2] = {0};
86 ssize_t n = read(0, buf, 1);
87 if (n != 1)
88 return 0;
89 return buf[0];
90}
91
92void HostedKeyboard::setDebugState(bool enableDebugState) {
93 m_bDebugState = enableDebugState;
94}
95
96bool HostedKeyboard::getDebugState() {
97 return m_bDebugState;
98}
99
101 return 0;
102}
103
105
106void HostedKeyboard::blocking(bool enable) {
107 int flags = fcntl(0, F_GETFL);
108 if (flags < 0) {
109 return;
110 }
111 if (enable)
112 flags &= ~O_NONBLOCK;
113 else
114 flags |= O_NONBLOCK;
115 fcntl(0, F_SETFL, flags);
116}
virtual char getChar()
bool m_bDebugState
True if we're in debug state.
virtual char getLedState()
virtual void setLedState(char state)
virtual void setDebugState(bool enableDebugState)
virtual void initialise()
virtual char getCharNonBlock()