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 #ifdef DEBUGGER
27 #include "pedigree/kernel/debugger/Debugger.h"
28 
29 #ifdef TRACK_PAGE_ALLOCATIONS
30 #include "pedigree/kernel/debugger/commands/AllocationCommand.h"
31 #endif
32 #endif
33 
34 #ifdef DEBUGGER
35 #include "pedigree/kernel/debugger/commands/SlamCommand.h"
36 #endif
37 
38 #include "pedigree/kernel/core/SlamAllocator.h"
39 
40 namespace __pedigree_hosted
41 {
42 };
43 using namespace __pedigree_hosted;
44 
45 #include <fcntl.h>
46 #include <termios.h>
47 #include <unistd.h>
48 
49 HostedKeyboard::HostedKeyboard() : m_bDebugState(false)
50 {
51  // Eat any pending input.
52  blocking(false);
53  char buf[64] = {0};
54  while (read(0, buf, 64) == 64)
55  ;
56  blocking(true);
57 }
58 
59 HostedKeyboard::~HostedKeyboard()
60 {
61 }
62 
64 {
65  // Don't echo characters.
66  struct termios t;
67  tcgetattr(0, &t);
68  t.c_lflag &= ~(ICANON | ECHO);
69  tcsetattr(0, TCSAFLUSH, &t);
70 }
71 
73 {
74  if (!getDebugState())
75  return 0;
76 
77  blocking(true);
78 
79  char buf[2] = {0};
80  ssize_t n = read(0, buf, 1);
81  if (n != 1)
82  return 0;
83  return buf[0];
84 }
85 
87 {
88  if (!getDebugState())
89  return 0;
90 
91  blocking(false);
92 
93  char buf[2] = {0};
94  ssize_t n = read(0, buf, 1);
95  if (n != 1)
96  return 0;
97  return buf[0];
98 }
99 
100 void HostedKeyboard::setDebugState(bool enableDebugState)
101 {
102  m_bDebugState = enableDebugState;
103 }
104 
105 bool HostedKeyboard::getDebugState()
106 {
107  return m_bDebugState;
108 }
109 
111 {
112  return 0;
113 }
114 
116 {
117 }
118 
119 void HostedKeyboard::blocking(bool enable)
120 {
121  int flags = fcntl(0, F_GETFL);
122  if (flags < 0)
123  {
124  return;
125  }
126  if (enable)
127  flags &= ~O_NONBLOCK;
128  else
129  flags |= O_NONBLOCK;
130  fcntl(0, F_SETFL, flags);
131 }
virtual void initialise()
virtual char getCharNonBlock()
virtual void setDebugState(bool enableDebugState)
virtual char getChar()
virtual void setLedState(char state)
virtual char getLedState()