2 * Acess2 Kernel - Keyboard Character Mappings
3 * - By John Hodge (thePowersGang)
6 * - Core keyboard multiplexer
8 * TODO: Make the key transation code more general (for non-US layouts)
9 * TODO: Support multiple virtual keyboards
12 #define VERSION VER2(1,0)
16 #include <Input/Keyboard/include/keyboard.h>
17 #include "keymap_int.h"
18 #include "layout_kbdus.h"
20 #define USE_KERNEL_MAGIC 1
24 extern void Threads_ToggleTrace(int TID);
25 extern void Threads_Dump(void);
26 extern void Heap_Stats(void);
30 int Keyboard_Install(char **Arguments);
31 void Keyboard_Cleanup(void);
33 tKeymap *Keyboard_LoadMap(const char *Name);
34 void Keyboard_FreeMap(tKeymap *Keymap);
36 int Keyboard_IOCtl(tVFS_Node *Node, int ID, void *Data);
38 tKeyboard *Keyboard_CreateInstance(int MaxSym, const char *Name);
39 void Keyboard_RemoveInstance(tKeyboard *Instance);
40 void Keyboard_HandleKey(tKeyboard *Source, Uint32 HIDKeySym);
43 MODULE_DEFINE(0, VERSION, Keyboard, Keyboard_Install, Keyboard_Cleanup, NULL);
44 tVFS_NodeType gKB_NodeType = {
45 .IOCtl = Keyboard_IOCtl
47 tDevFS_Driver gKB_DevInfo = {
49 { .Type = &gKB_NodeType }
52 int giKB_MagicAddress = 0;
53 int giKB_MagicAddressPos = 0;
57 int Keyboard_Install(char **Arguments)
59 DevFS_AddDevice( &gKB_DevInfo );
63 void Keyboard_Cleanup(void)
65 // TODO: Do I need this?
68 tKeymap *Keyboard_LoadMap(const char *Name)
73 void Keyboard_FreeMap(tKeymap *Keymap)
77 // --- VFS Interface ---
78 static const char *csaIOCTL_NAMES[] = {DRV_IOCTLNAMES, DRV_KEYBAORD_IOCTLNAMES, NULL};
79 int Keyboard_IOCtl(tVFS_Node *Node, int Id, void *Data)
83 BASE_IOCTLS(DRV_TYPE_KEYBOARD, "Keyboard", 0x100, csaIOCTL_NAMES);
85 case KB_IOCTL_SETCALLBACK:
86 if( Threads_GetUID() != 0 ) return -1;
87 if( MM_IsUser( (tVAddr)Data ) ) return -1;
88 if( Node->ImplInt ) return 0; // Can only be set once
89 Node->ImplInt = (Uint)Data;
95 // --- Device Interface ---
96 tKeyboard *Keyboard_CreateInstance(int MaxSym, const char *Name)
99 int sym_bitmap_size = (MaxSym + 7)/8;
100 int string_size = strlen(Name) + 1;
102 ret = malloc( sizeof(tKeyboard) + sym_bitmap_size + string_size );
107 memset(ret, 0, sizeof(tKeyboard) + sym_bitmap_size );
109 ret->Name = (char*)ret + sizeof(tKeyboard) + sym_bitmap_size;
110 memcpy(ret->Name, Name, string_size);
111 // Set node and default keymap
112 ret->Node = &gKB_DevInfo.RootNode;
113 ret->Keymap = &gKeymap_KBDUS;
118 void Keyboard_RemoveInstance(tKeyboard *Instance)
121 Log_Error("Keyboard", "TODO: Implement Keyboard_RemoveInstance");
124 void Keyboard_HandleKey(tKeyboard *Source, Uint32 HIDKeySym)
131 bPressed = !(HIDKeySym & (1 << 31));
132 HIDKeySym &= 0x7FFFFFFF;
136 Uint8 mask = 1 << (HIDKeySym&7);
137 int ofs = HIDKeySym / 8;
138 int oldstate = !!(Source->KeyStates[ofs] & mask);
140 // Get the state of all other devices attached
142 for( tKeyboard *kb = Source->Node->ImplPtr; kb; kb = kb->Next )
144 if( kb == Source ) continue ;
145 if( kb->MaxKeysym <= HIDKeySym ) continue ;
146 otherstate = otherstate || (kb->KeyStates[ofs] & mask);
151 Source->KeyStates[ ofs ] |= mask;
153 Source->KeyStates[ ofs ] &= ~mask;
155 // Get the action (Press, Refire or Release)
158 if( !oldstate && !otherstate )
159 flag = KEY_ACTION_PRESS; // Down
161 flag = KEY_ACTION_REFIRE; // Refire
166 flag = KEY_ACTION_RELEASE; // Up
168 flag = -1 ; // Do nothing
172 // Translate \a State into layer
173 // TODO: Support non-trivial layouts
174 layer = !!(Source->State & 3);
177 if( flag == KEY_ACTION_RELEASE )
181 // Translate the keysym into a character
182 if( layer >= Source->Keymap->nLayers )
184 else if( HIDKeySym >= Source->Keymap->Layers[layer]->nSyms )
187 trans = Source->Keymap->Layers[layer]->Sym[HIDKeySym];
188 // - No translation in \a layer, fall back to layer=0
189 if(!trans && HIDKeySym < Source->Keymap->Layers[0]->nSyms)
190 trans = Source->Keymap->Layers[0]->Sym[HIDKeySym];
195 tKeybardCallback Callback = (void*)Source->Node->ImplInt;
196 Callback( HIDKeySym | KEY_ACTION_RAWSYM );
197 Callback( flag | trans );
200 // TODO: Translate this into agnostic code
203 case KEYSYM_LEFTSHIFT:
204 if(bPressed) Source->State |= 1;
205 else Source->State &= ~1;
207 case KEYSYM_RIGHTSHIFT:
208 if(bPressed) Source->State |= 2;
209 else Source->State &= ~2;
213 // --- Check for Kernel Magic Combos
215 if(Source->KeyStates[KEYSYM_LEFTCTRL/8] & (1 << (KEYSYM_LEFTCTRL&7))
216 && Source->KeyStates[KEYSYM_LEFTALT/8] & (1 << (KEYSYM_LEFTALT &7)) )
221 case '0': val = 0; goto _av; case '1': val = 1; goto _av;
222 case '2': val = 2; goto _av; case '3': val = 3; goto _av;
223 case '4': val = 4; goto _av; case '5': val = 5; goto _av;
224 case '6': val = 6; goto _av; case '7': val = 7; goto _av;
225 case '8': val = 8; goto _av; case '9': val = 9; goto _av;
226 case 'a': val = 10; goto _av; case 'b': val = 11; goto _av;
227 case 'c': val = 12; goto _av; case 'd': val = 13; goto _av;
228 case 'e': val = 14; goto _av; case 'f': val = 15; goto _av;
230 if(giKB_MagicAddressPos == BITS/4) break;
231 giKB_MagicAddress |= (Uint)val << giKB_MagicAddressPos;
232 giKB_MagicAddressPos ++;
235 // Instruction Tracing
237 Log("Toggle instruction tracing on %i\n", giKB_MagicAddress);
238 Threads_ToggleTrace( giKB_MagicAddress );
239 giKB_MagicAddress = 0; giKB_MagicAddressPos = 0;
243 case 'p': Threads_Dump(); return;
245 case 'h': Heap_Stats(); return;