iPXE
keymap.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2022 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * 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, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <string.h>
27 #include <ctype.h>
28 #include <ipxe/keys.h>
29 #include <ipxe/keymap.h>
30 
31 /** @file
32  *
33  * Keyboard mappings
34  *
35  */
36 
37 /** ASCII character mask */
38 #define ASCII_MASK 0x7f
39 
40 /** Control character mask */
41 #define CTRL_MASK 0x1f
42 
43 /** Upper case character mask */
44 #define UPPER_MASK 0x5f
45 
46 /** Case toggle bit */
47 #define CASE_TOGGLE ( ASCII_MASK & ~UPPER_MASK )
48 
49 /** Default keyboard mapping */
50 static TABLE_START ( keymap_start, KEYMAP );
51 
52 /** Current keyboard mapping */
53 static struct keymap *keymap_current = keymap_start;
54 
55 /**
56  * Remap a key
57  *
58  * @v character Character read from console
59  * @ret mapped Mapped character
60  */
61 unsigned int key_remap ( unsigned int character ) {
62  struct keymap *keymap = keymap_current;
63  unsigned int mapped = ( character & KEYMAP_MASK );
64  struct keymap_key *key;
65 
66  /* Invert case before remapping if applicable */
67  if ( ( character & KEYMAP_CAPSLOCK_UNDO ) && isalpha ( mapped ) )
68  mapped ^= CASE_TOGGLE;
69 
70  /* Select remapping table */
71  key = ( ( character & KEYMAP_ALTGR ) ? keymap->altgr : keymap->basic );
72 
73  /* Remap via table */
74  for ( ; key->from ; key++ ) {
75  if ( mapped == key->from ) {
76  mapped = key->to;
77  break;
78  }
79  }
80 
81  /* Handle Ctrl-<key> and CapsLock */
82  if ( isalpha ( mapped ) ) {
83  if ( character & KEYMAP_CTRL ) {
84  mapped &= CTRL_MASK;
85  } else if ( character & KEYMAP_CAPSLOCK ) {
86  mapped ^= CASE_TOGGLE;
87  }
88  }
89 
90  /* Clear flags */
91  mapped &= ASCII_MASK;
92 
93  DBGC2 ( &keymap_current, "KEYMAP mapped %04x => %02x\n",
94  character, mapped );
95  return mapped;
96 }
97 
98 /**
99  * Find keyboard map by name
100  *
101  * @v name Keyboard map name
102  * @ret keymap Keyboard map, or NULL if not found
103  */
104 struct keymap * keymap_find ( const char *name ) {
105  struct keymap *keymap;
106 
107  /* Find matching keyboard map */
109  if ( strcmp ( keymap->name, name ) == 0 )
110  return keymap;
111  }
112 
113  return NULL;
114 }
115 
116 /**
117  * Set keyboard map
118  *
119  * @v keymap Keyboard map, or NULL to use default
120  */
121 void keymap_set ( struct keymap *keymap ) {
122 
123  /* Use default keymap if none specified */
124  if ( ! keymap )
125  keymap = keymap_start;
126 
127  /* Set new keyboard map */
128  if ( keymap != keymap_current )
129  DBGC ( &keymap_current, "KEYMAP using \"%s\"\n", keymap->name );
131 }
#define KEYMAP
Keyboard mapping table.
Definition: keymap.h:40
const char * name
Definition: ath9k_hw.c:1984
#define KEYMAP_CAPSLOCK
CapsLock key flag.
Definition: keymap.h:58
Keyboard mappings.
struct keymap_key * basic
Basic remapping table (zero-terminated)
Definition: keymap.h:34
#define KEYMAP_MASK
Mappable character mask.
Definition: keymap.h:49
static struct keymap * keymap_current
Current keyboard mapping.
Definition: keymap.c:53
#define KEYMAP_CTRL
Ctrl key flag.
Definition: keymap.h:55
A remapped key.
Definition: keymap.h:22
#define DBGC(...)
Definition: compiler.h:505
static int isalpha(int character)
Check if character is alphabetic.
Definition: ctype.h:75
Character types.
struct keymap_key * altgr
AltGr remapping table (zero-terminated)
Definition: keymap.h:36
#define CASE_TOGGLE
Case toggle bit.
Definition: keymap.c:47
#define KEYMAP_ALTGR
AltGr key flag.
Definition: keymap.h:73
#define CTRL_MASK
Control character mask.
Definition: keymap.c:41
unsigned int key_remap(unsigned int character)
Remap a key.
Definition: keymap.c:61
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition: tables.h:385
const char * name
Name.
Definition: keymap.h:32
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
#define DBGC2(...)
Definition: compiler.h:522
void keymap_set(struct keymap *keymap)
Set keyboard map.
Definition: keymap.c:121
int strcmp(const char *first, const char *second)
Compare strings.
Definition: string.c:173
Key definitions.
#define ASCII_MASK
ASCII character mask.
Definition: keymap.c:38
struct keymap * keymap_find(const char *name)
Find keyboard map by name.
Definition: keymap.c:104
static TABLE_START(keymap_start, KEYMAP)
Default keyboard mapping.
#define KEYMAP_CAPSLOCK_UNDO
Undo CapsLock key flag.
Definition: keymap.h:67
A keyboard mapping.
Definition: keymap.h:30
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
String functions.
union @382 key
Sense key.
Definition: crypto.h:284