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 FILE_SECBOOT ( PERMITTED );
26 
27 #include <string.h>
28 #include <ctype.h>
29 #include <ipxe/keys.h>
30 #include <ipxe/keymap.h>
31 
32 /** @file
33  *
34  * Keyboard mappings
35  *
36  */
37 
38 /** ASCII character mask */
39 #define ASCII_MASK 0x7f
40 
41 /** Control character mask */
42 #define CTRL_MASK 0x1f
43 
44 /** Upper case character mask */
45 #define UPPER_MASK 0x5f
46 
47 /** Case toggle bit */
48 #define CASE_TOGGLE ( ASCII_MASK & ~UPPER_MASK )
49 
50 /** Default keyboard mapping */
51 static TABLE_START ( keymap_start, KEYMAP );
52 
53 /** Current keyboard mapping */
54 static struct keymap *keymap_current = keymap_start;
55 
56 /**
57  * Remap a key
58  *
59  * @v character Character read from console
60  * @ret mapped Mapped character
61  */
62 unsigned int key_remap ( unsigned int character ) {
63  struct keymap *keymap = keymap_current;
64  unsigned int mapped = ( character & KEYMAP_MASK );
65  struct keymap_key *key;
66 
67  /* Invert case before remapping if applicable */
68  if ( ( character & KEYMAP_CAPSLOCK_UNDO ) && isalpha ( mapped ) )
69  mapped ^= CASE_TOGGLE;
70 
71  /* Select remapping table */
72  key = ( ( character & KEYMAP_ALTGR ) ? keymap->altgr : keymap->basic );
73 
74  /* Remap via table */
75  for ( ; key->from ; key++ ) {
76  if ( mapped == key->from ) {
77  mapped = key->to;
78  break;
79  }
80  }
81 
82  /* Handle Ctrl-<key> and CapsLock */
83  if ( isalpha ( mapped ) ) {
84  if ( character & KEYMAP_CTRL ) {
85  mapped &= CTRL_MASK;
86  } else if ( character & KEYMAP_CAPSLOCK ) {
87  mapped ^= CASE_TOGGLE;
88  }
89  }
90 
91  /* Clear flags */
92  mapped &= ASCII_MASK;
93 
94  DBGC2 ( &keymap_current, "KEYMAP mapped %04x => %02x\n",
95  character, mapped );
96  return mapped;
97 }
98 
99 /**
100  * Find keyboard map by name
101  *
102  * @v name Keyboard map name
103  * @ret keymap Keyboard map, or NULL if not found
104  */
105 struct keymap * keymap_find ( const char *name ) {
106  struct keymap *keymap;
107 
108  /* Find matching keyboard map */
110  if ( strcmp ( keymap->name, name ) == 0 )
111  return keymap;
112  }
113 
114  return NULL;
115 }
116 
117 /**
118  * Set keyboard map
119  *
120  * @v keymap Keyboard map, or NULL to use default
121  */
122 void keymap_set ( struct keymap *keymap ) {
123 
124  /* Use default keymap if none specified */
125  if ( ! keymap )
126  keymap = keymap_start;
127 
128  /* Set new keyboard map */
129  if ( keymap != keymap_current )
130  DBGC ( &keymap_current, "KEYMAP using \"%s\"\n", keymap->name );
132 }
#define KEYMAP
Keyboard mapping table.
Definition: keymap.h:41
const char * name
Definition: ath9k_hw.c:1986
#define KEYMAP_CAPSLOCK
CapsLock key flag.
Definition: keymap.h:59
Keyboard mappings.
struct keymap_key * basic
Basic remapping table (zero-terminated)
Definition: keymap.h:35
#define KEYMAP_MASK
Mappable character mask.
Definition: keymap.h:50
static struct keymap * keymap_current
Current keyboard mapping.
Definition: keymap.c:54
#define KEYMAP_CTRL
Ctrl key flag.
Definition: keymap.h:56
A remapped key.
Definition: keymap.h:23
#define DBGC(...)
Definition: compiler.h:505
static int isalpha(int character)
Check if character is alphabetic.
Definition: ctype.h:76
Character types.
struct keymap_key * altgr
AltGr remapping table (zero-terminated)
Definition: keymap.h:37
#define CASE_TOGGLE
Case toggle bit.
Definition: keymap.c:48
#define KEYMAP_ALTGR
AltGr key flag.
Definition: keymap.h:74
#define CTRL_MASK
Control character mask.
Definition: keymap.c:42
unsigned int key_remap(unsigned int character)
Remap a key.
Definition: keymap.c:62
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition: tables.h:386
const char * name
Name.
Definition: keymap.h:33
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:122
int strcmp(const char *first, const char *second)
Compare strings.
Definition: string.c:174
Key definitions.
#define ASCII_MASK
ASCII character mask.
Definition: keymap.c:39
struct keymap * keymap_find(const char *name)
Find keyboard map by name.
Definition: keymap.c:105
static TABLE_START(keymap_start, KEYMAP)
Default keyboard mapping.
#define KEYMAP_CAPSLOCK_UNDO
Undo CapsLock key flag.
Definition: keymap.h:68
A keyboard mapping.
Definition: keymap.h:31
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
String functions.
FILE_SECBOOT(PERMITTED)
union @391 key
Sense key.
Definition: scsi.h:18