[Krafton Jungle] PintOS 2.0.0
크래프톤 정글 PintOS
 
Loading...
Searching...
No Matches
kbd.c File Reference
#include "devices/kbd.h"
#include <ctype.h>
#include <debug.h>
#include <stdio.h>
#include <string.h>
#include "devices/input.h"
#include "threads/interrupt.h"
#include "threads/io.h"
Include dependency graph for kbd.c:

Classes

struct  keymap
 

Macros

#define DATA_REG   0x60
 

Functions

void kbd_init (void)
 
void kbd_print_stats (void)
 
static bool map_key (const struct keymap[], unsigned scancode, uint8_t *)
 
static void keyboard_interrupt (struct intr_frame *args UNUSED)
 

Variables

static bool left_shift
 
static bool right_shift
 
static bool left_alt
 
static bool right_alt
 
static bool left_ctrl
 
static bool right_ctrl
 
static bool caps_lock
 
static int64_t key_cnt
 
static intr_handler_func keyboard_interrupt
 
static const struct keymap invariant_keymap []
 
static const struct keymap unshifted_keymap []
 
static const struct keymap shifted_keymap []
 

Macro Definition Documentation

◆ DATA_REG

#define DATA_REG   0x60

Function Documentation

◆ kbd_init()

void kbd_init ( void  )
30 {
31 intr_register_ext (0x21, keyboard_interrupt, "8042 Keyboard");
32}
void intr_register_ext(uint8_t vec, intr_handler_func *, const char *name)
Definition: interrupt.c:228
static intr_handler_func keyboard_interrupt
Definition: kbd.c:26
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kbd_print_stats()

void kbd_print_stats ( void  )
36 {
37 printf ("Keyboard: %lld keys pressed\n", key_cnt);
38}
static int64_t key_cnt
Definition: kbd.c:24
int printf(const char *,...) PRINTF_FORMAT(1
Here is the call graph for this function:
Here is the caller graph for this function:

◆ keyboard_interrupt()

static void keyboard_interrupt ( struct intr_frame *args  UNUSED)
static
88 {
89 /* Status of shift keys. */
90 bool shift = left_shift || right_shift;
91 bool alt = left_alt || right_alt;
92 bool ctrl = left_ctrl || right_ctrl;
93
94 /* Keyboard scancode. */
95 unsigned code;
96
97 /* False if key pressed, true if key released. */
98 bool release;
99
100 /* Character that corresponds to `code'. */
101 uint8_t c;
102
103 /* Read scancode, including second byte if prefix code. */
104 code = inb (DATA_REG);
105 if (code == 0xe0)
106 code = (code << 8) | inb (DATA_REG);
107
108 /* Bit 0x80 distinguishes key press from key release
109 (even if there's a prefix). */
110 release = (code & 0x80) != 0;
111 code &= ~0x80u;
112
113 /* Interpret key. */
114 if (code == 0x3a) {
115 /* Caps Lock. */
116 if (!release)
118 } else if (map_key (invariant_keymap, code, &c)
119 || (!shift && map_key (unshifted_keymap, code, &c))
120 || (shift && map_key (shifted_keymap, code, &c))) {
121 /* Ordinary character. */
122 if (!release) {
123 /* Handle Ctrl, Shift.
124 Note that Ctrl overrides Shift. */
125 if (ctrl && c >= 0x40 && c < 0x60) {
126 /* A is 0x41, Ctrl+A is 0x01, etc. */
127 c -= 0x40;
128 } else if (shift == caps_lock)
129 c = tolower (c);
130
131 /* Handle Alt by setting the high bit.
132 This 0x80 is unrelated to the one used to
133 distinguish key press from key release. */
134 if (alt)
135 c += 0x80;
136
137 /* Append to keyboard buffer. */
138 if (!input_full ()) {
139 key_cnt++;
140 input_putc (c);
141 }
142 }
143 } else {
144 /* Maps a keycode into a shift state variable. */
145 struct shift_key {
146 unsigned scancode;
147 bool *state_var;
148 };
149
150 /* Table of shift keys. */
151 static const struct shift_key shift_keys[] = {
152 { 0x2a, &left_shift},
153 { 0x36, &right_shift},
154 { 0x38, &left_alt},
155 {0xe038, &right_alt},
156 { 0x1d, &left_ctrl},
157 {0xe01d, &right_ctrl},
158 {0, NULL},
159 };
160
161 const struct shift_key *key;
162
163 /* Scan the table. */
164 for (key = shift_keys; key->scancode != 0; key++)
165 if (key->scancode == code) {
166 *key->state_var = !release;
167 break;
168 }
169 }
170}
static int tolower(int c)
Definition: ctype.h:25
bool input_full(void)
Definition: input.c:45
void input_putc(uint8_t key)
Definition: input.c:18
static uint8_t inb(uint16_t port)
Definition: io.h:49
static bool left_ctrl
Definition: kbd.c:17
static bool right_shift
Definition: kbd.c:15
static bool map_key(const struct keymap[], unsigned scancode, uint8_t *)
Definition: kbd.c:177
static const struct keymap unshifted_keymap[]
Definition: kbd.c:65
static bool caps_lock
Definition: kbd.c:21
static bool left_shift
Definition: kbd.c:15
#define DATA_REG
Definition: kbd.c:11
static const struct keymap invariant_keymap[]
Definition: kbd.c:51
static bool right_ctrl
Definition: kbd.c:17
static bool left_alt
Definition: kbd.c:16
static const struct keymap shifted_keymap[]
Definition: kbd.c:76
static bool right_alt
Definition: kbd.c:16
#define NULL
Definition: stddef.h:4
unsigned char uint8_t
Definition: stdint.h:20
Here is the call graph for this function:

◆ map_key()

static bool map_key ( const struct keymap  k[],
unsigned  scancode,
uint8_t c 
)
static
177 {
178 for (; k->first_scancode != 0; k++)
179 if (scancode >= k->first_scancode
180 && scancode < k->first_scancode + strlen (k->chars)) {
181 *c = k->chars[scancode - k->first_scancode];
182 return true;
183 }
184
185 return false;
186}
size_t strlen(const char *)
Definition: string.c:271
const char * chars
Definition: kbd.c:43
uint8_t first_scancode
Definition: kbd.c:42
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ caps_lock

bool caps_lock
static

◆ invariant_keymap

const struct keymap invariant_keymap[]
static
Initial value:
= {
{0x01, "\033"},
{0x0e, "\b"},
{0x0f, "\tQWERTYUIOP"},
{0x1c, "\r"},
{0x1e, "ASDFGHJKL"},
{0x2c, "ZXCVBNM"},
{0x37, "*"},
{0x39, " "},
{0, NULL},
}

◆ key_cnt

int64_t key_cnt
static

◆ keyboard_interrupt

intr_handler_func keyboard_interrupt
static

◆ left_alt

bool left_alt
static

◆ left_ctrl

bool left_ctrl
static

◆ left_shift

bool left_shift
static

◆ right_alt

bool right_alt
static

◆ right_ctrl

bool right_ctrl
static

◆ right_shift

bool right_shift
static

◆ shifted_keymap

const struct keymap shifted_keymap[]
static
Initial value:
= {
{0x02, "!@#$%^&*()_+"},
{0x1a, "{}"},
{0x27, ":\"~"},
{0x2b, "|"},
{0x33, "<>?"},
{0, NULL},
}

◆ unshifted_keymap

const struct keymap unshifted_keymap[]
static
Initial value:
= {
{0x02, "1234567890-="},
{0x1a, "[]"},
{0x27, ";'`"},
{0x2b, "\\"},
{0x33, ",./"},
{0, NULL},
}