Modules - Implementing mouse multiplexer
[tpg/acess2.git] / KernelLand / Modules / Input / Keyboard / main.c
1 /*
2  * Acess2 Kernel - Keyboard Driver
3  * - By John Hodge (thePowersGang)
4  *
5  * main.c
6  * - Keyboard driver core
7  *
8  * TODO: Make the key transation code more general (for non-US layouts)
9  * TODO: Support multiple virtual keyboards
10  */
11 #define DEBUG   0
12 #define VERSION VER2(1,0)
13 #include <acess.h>
14 #include <modules.h>
15 #include <fs_devfs.h>
16 #include <Input/Keyboard/include/keyboard.h>
17 #include "keymap_int.h"
18 #include "layout_kbdus.h"
19
20 #define USE_KERNEL_MAGIC        1
21
22 // === IMPORTS ===
23 #if USE_KERNEL_MAGIC
24 extern void     Threads_ToggleTrace(int TID);
25 extern void     Threads_Dump(void);
26 extern void     Heap_Stats(void);
27 #endif
28
29 // === PROTOTYPES ===
30  int    Keyboard_Install(char **Arguments);
31 void    Keyboard_Cleanup(void);
32 // - Internal
33 tKeymap *Keyboard_LoadMap(const char *Name);
34 void    Keyboard_FreeMap(tKeymap *Keymap);
35 // - "User" side (Actually VT)
36  int    Keyboard_IOCtl(tVFS_Node *Node, int ID, void *Data);
37 // - Device Side
38 tKeyboard *Keyboard_CreateInstance(int MaxSym, const char *Name);
39 void    Keyboard_RemoveInstance(tKeyboard *Instance);
40 void    Keyboard_HandleKey(tKeyboard *Source, Uint32 HIDKeySym);
41
42 // === GLOBALS ===
43 MODULE_DEFINE(0, VERSION, Keyboard, Keyboard_Install, Keyboard_Cleanup, NULL);
44 tVFS_NodeType   gKB_NodeType = {
45         .IOCtl = Keyboard_IOCtl
46 };
47 tDevFS_Driver   gKB_DevInfo = {
48         NULL, "Keyboard",
49         { .Type = &gKB_NodeType }
50 };
51 #if USE_KERNEL_MAGIC
52  int    giKB_MagicAddress = 0;
53  int    giKB_MagicAddressPos = 0;
54 #endif
55
56 // === CODE ===
57 /**
58  * \brief Initialise the keyboard driver
59  */
60 int Keyboard_Install(char **Arguments)
61 {
62         /// - Register with DevFS
63         DevFS_AddDevice( &gKB_DevInfo );
64         return 0;
65 }
66
67 /**
68  * \brief Pre-unload cleanup function
69  */
70 void Keyboard_Cleanup(void)
71 {
72         // TODO: Do I need this?
73 }
74
75 // --- Map Management ---
76 /**
77  * \brief Load an arbitary keyboard map
78  * \param Name  Keymap name (e.g. "en-us")
79  * \return Keymap pointer
80  */
81 tKeymap *Keyboard_int_LoadMap(const char *Name)
82 {
83         Log_Warning("Keyboard", "TOD: Impliment Keyboard_int_LoadMap");
84         return NULL;
85 }
86
87 /**
88  * \brief Unload a keyboard map
89  * \param Keymap        Keymap to unload/free
90  */
91 void Keyboard_int_FreeMap(tKeymap *Keymap)
92 {
93 }
94
95 // --- VFS Interface ---
96 static const char *csaIOCTL_NAMES[] = {DRV_IOCTLNAMES, DRV_KEYBAORD_IOCTLNAMES, NULL};
97 /**
98  * \brief Keyboard IOCtl
99  */
100 int Keyboard_IOCtl(tVFS_Node *Node, int Id, void *Data)
101 {
102         switch(Id)
103         {
104         BASE_IOCTLS(DRV_TYPE_KEYBOARD, "Keyboard", 0x100, csaIOCTL_NAMES);
105         
106         case KB_IOCTL_SETCALLBACK:
107                 if( Threads_GetUID() != 0 )     return -1;
108                 if( MM_IsUser( (tVAddr)Data ) ) return -1;
109                 if( Node->ImplInt )     return 0;       // Can only be set once
110                 Node->ImplInt = (Uint)Data;
111                 return 1;
112         }
113         return -1;
114 }
115
116 // --- Device Interface ---
117 /*
118  * Create a new keyboard device instance
119  * TODO: Allow linking to other VFS nodes
120  * See Input/Keyboard/include/keyboard.h
121  */
122 tKeyboard *Keyboard_CreateInstance(int MaxSym, const char *Name)
123 {
124         tKeyboard       *ret;
125          int    sym_bitmap_size = (MaxSym + 7)/8;
126          int    string_size = strlen(Name) + 1;
127
128         ret = malloc( sizeof(tKeyboard) + sym_bitmap_size + string_size );
129         if( !ret ) {
130                 return NULL;
131         }
132         // Clear
133         memset(ret, 0, sizeof(tKeyboard) + sym_bitmap_size );
134         // Set name
135         ret->Name = (char*)ret + sizeof(tKeyboard) + sym_bitmap_size;
136         memcpy(ret->Name, Name, string_size);
137         // Set node and default keymap
138         ret->Node = &gKB_DevInfo.RootNode;
139         ret->Keymap = &gKeymap_KBDUS;
140
141         return ret;
142 }
143
144 /*
145  * Destroy a keyboard instance
146  * - See Input/Keyboard/include/keyboard.h
147  */
148 void Keyboard_RemoveInstance(tKeyboard *Instance)
149 {
150         // TODO: Implement
151         Log_Error("Keyboard", "TODO: Implement Keyboard_RemoveInstance");
152 }
153
154 /*
155  * Handle a key press/release event
156  * - See Input/Keyboard/include/keyboard.h
157  */
158 void Keyboard_HandleKey(tKeyboard *Source, Uint32 HIDKeySym)
159 {
160          int    bPressed;
161         Uint32  trans;
162         Uint32  flag;
163         Uint8   layer;
164         
165         bPressed = !(HIDKeySym & (1 << 31));
166         HIDKeySym &= 0x7FFFFFFF;
167
168         // - Determine the action (Press, Release or Refire)
169         {
170                 Uint8   mask = 1 << (HIDKeySym&7);
171                  int    ofs = HIDKeySym / 8;
172                  int    oldstate = !!(Source->KeyStates[ofs] & mask);
173                 
174                 // Get the state of all other devices attached
175                  int    otherstate = 0;
176                 for( tKeyboard *kb = Source->Node->ImplPtr; kb; kb = kb->Next )
177                 {
178                         if( kb == Source )      continue ;
179                         if( kb->MaxKeysym <= HIDKeySym )        continue ;
180                         otherstate = otherstate || (kb->KeyStates[ofs] & mask);
181                 }
182                 
183                 // Update key state
184                 if( bPressed )
185                         Source->KeyStates[ ofs ] |= mask;
186                 else
187                         Source->KeyStates[ ofs ] &= ~mask;
188                 
189                 // Get the final flag
190                 if( bPressed )
191                 {
192                         if( !oldstate && !otherstate )
193                                 flag = KEY_ACTION_PRESS; // Down
194                         else
195                                 flag = KEY_ACTION_REFIRE; // Refire
196                 }
197                 else
198                 {
199                         if( !otherstate )
200                                 flag = KEY_ACTION_RELEASE; // Up
201                         else
202                                 flag = -1; // Do nothing
203                 }
204         }
205
206         // Translate \a State into layer
207         // TODO: Support non-trivial layouts
208         layer = !!(Source->State & 3);
209         
210         // Do translation
211         if( flag == KEY_ACTION_RELEASE )
212                 trans = 0;
213         else {  
214                 
215                 // Translate the keysym into a character
216                 if( layer >= Source->Keymap->nLayers )
217                         trans = 0;
218                 else if( HIDKeySym >= Source->Keymap->Layers[layer]->nSyms )
219                         trans = 0;
220                 else
221                         trans = Source->Keymap->Layers[layer]->Sym[HIDKeySym];
222                 // - No translation in \a layer, fall back to layer=0
223                 if(!trans && HIDKeySym < Source->Keymap->Layers[0]->nSyms)
224                         trans = Source->Keymap->Layers[0]->Sym[HIDKeySym];
225         }
226
227         // Call callback (only if the action is valid)
228         if( flag != -1 )
229         {
230                 tKeybardCallback Callback = (void*)Source->Node->ImplInt;
231                 Callback( HIDKeySym | KEY_ACTION_RAWSYM );
232                 Callback( flag | trans );
233         }
234
235         // TODO: Translate this into agnostic code
236         switch( HIDKeySym )
237         {
238         case KEYSYM_LEFTSHIFT:
239                 if(bPressed) Source->State |= 1;
240                 else Source->State &= ~1;
241                 break;
242         case KEYSYM_RIGHTSHIFT:
243                 if(bPressed) Source->State |= 2;
244                 else Source->State &= ~2;
245                 break;
246         }
247
248         // --- Check for Kernel Magic Combos
249         #if USE_KERNEL_MAGIC
250         if(bPressed
251         && Source->KeyStates[KEYSYM_LEFTCTRL/8] & (1 << (KEYSYM_LEFTCTRL&7))
252         && Source->KeyStates[KEYSYM_LEFTALT/8]  & (1 << (KEYSYM_LEFTALT &7)) )
253         {
254                  int    val;
255                 switch(trans)
256                 {
257                 case '0': val = 0;  goto _av;   case '1': val = 1;  goto _av;
258                 case '2': val = 2;  goto _av;   case '3': val = 3;  goto _av;
259                 case '4': val = 4;  goto _av;   case '5': val = 5;  goto _av;
260                 case '6': val = 6;  goto _av;   case '7': val = 7;  goto _av;
261                 case '8': val = 8;  goto _av;   case '9': val = 9;  goto _av;
262                 case 'a': val = 10; goto _av;   case 'b': val = 11; goto _av;
263                 case 'c': val = 12; goto _av;   case 'd': val = 13; goto _av;
264                 case 'e': val = 14; goto _av;   case 'f': val = 15; goto _av;
265                 _av:
266                         if(giKB_MagicAddressPos == BITS/4)      break;
267                         giKB_MagicAddress |= (Uint)val << giKB_MagicAddressPos;
268                         giKB_MagicAddressPos ++;
269                         break;
270                 
271                 // Instruction Tracing
272                 case 't':
273                         Log("Toggle instruction tracing on %i\n", giKB_MagicAddress);
274                         Threads_ToggleTrace( giKB_MagicAddress );
275                         giKB_MagicAddress = 0;  giKB_MagicAddressPos = 0;
276                         return;
277                 
278                 // Thread List Dump
279                 case 'p':       Threads_Dump(); return;
280                 // Heap Statistics
281                 case 'h':       Heap_Stats();   return;
282                 // Dump Structure
283                 case 's':       return;
284                 }
285         }
286         #endif
287
288 }
289

UCC git Repository :: git.ucc.asn.au