+ int *iData = Data;
+ tVTerm *term = Node->ImplPtr;
+ ENTER("pNode iId pData", Node, Id, Data);
+
+ if(Id >= DRV_IOCTL_LOOKUP) {
+ if( Threads_GetUID() != 0 ) return -1;
+ }
+
+ switch(Id)
+ {
+ case DRV_IOCTL_TYPE:
+ LEAVE('i', DRV_TYPE_TERMINAL);
+ return DRV_TYPE_TERMINAL;
+ case DRV_IOCTL_IDENT:
+ memcpy(Data, "VT\0\0", 4);
+ LEAVE('i', 0);
+ return 0;
+ case DRV_IOCTL_VERSION:
+ LEAVE('x', VERSION);
+ return VERSION;
+ case DRV_IOCTL_LOOKUP:
+ LEAVE('i', 0);
+ return 0;
+
+ // Get/Set the mode (and apply any changes)
+ case TERM_IOCTL_MODETYPE:
+ if(Data != NULL)
+ {
+ if(term->Mode != *iData)
+ VT_int_ChangeMode(term, *iData);
+
+ // Update the screen dimensions
+ if(giVT_CurrentTerminal == Node->Inode)
+ VT_SetTerminal( giVT_CurrentTerminal );
+ }
+ LEAVE('i', term->Mode);
+ return term->Mode;
+
+ // Get/set the terminal width
+ case TERM_IOCTL_WIDTH:
+ if(Data != NULL) term->Width = *iData;
+ Log("VT_Terminal_IOCtl - RETURN term->Width = %i", term->Width);
+ LEAVE('i', term->Width);
+ return term->Width;
+
+ // Get/set the terminal height
+ case TERM_IOCTL_HEIGHT:
+ if(Data != NULL) term->Height = *iData;
+ Log("VT_Terminal_IOCtl - RETURN term->Height = %i", term->Height);
+ LEAVE('i', term->Height);
+ return term->Height;
+
+ case TERM_IOCTL_FORCESHOW:
+ VT_SetTerminal( Node->Inode );
+ LEAVE('i', 1);
+ return 1;
+ }
+ LEAVE('i', -1);
+ return -1;
+}
+
+/**
+ * \fn void VT_SetTerminal(int ID)
+ * \brief Set the current terminal
+ */
+void VT_SetTerminal(int ID)
+{
+ tVideo_IOCtl_Mode mode = {0};
+ int modeNum;
+
+ // Create the video mode
+ mode.width = gVT_Terminals[ ID ].Width;
+ mode.height = gVT_Terminals[ ID ].Height;
+ // - Text Mode
+ if(gVT_Terminals[ ID ].Mode == TERM_MODE_TEXT) {
+ mode.bpp = 12;
+ mode.flags = VIDEO_FLAG_TEXT;
+ }
+ // - Framebuffer or 3D
+ else {
+ mode.bpp = 32;
+ mode.flags = 0;
+ }
+
+ // Set video mode
+ VFS_IOCtl( giVT_OutputDevHandle, VIDEO_IOCTL_FINDMODE, &mode );
+ modeNum = mode.id;
+ gVT_Terminals[ ID ].RealWidth = mode.width;
+ gVT_Terminals[ ID ].RealHeight = mode.height;
+ VFS_IOCtl( giVT_OutputDevHandle, VIDEO_IOCTL_GETSETMODE, &modeNum );
+
+ // Update current terminal ID
+ Log("Changed terminal from %i to %i", giVT_CurrentTerminal, ID);
+ giVT_CurrentTerminal = ID;
+
+ // Update the screen
+ VT_int_UpdateScreen( &gVT_Terminals[ ID ], 1 );
+}
+
+/**
+ * \fn void VT_KBCallBack(Uint32 Codepoint)
+ * \brief Called on keyboard interrupt
+ * \param Codepoint Pseudo-UTF32 character
+ *
+ * Handles a key press and sends the key code to the user's buffer.
+ * If the code creates a kernel-magic sequence, it is not passed to the
+ * user and is handled in-kernel.
+ */
+void VT_KBCallBack(Uint32 Codepoint)
+{
+ tVTerm *term = &gVT_Terminals[giVT_CurrentTerminal];
+
+ // How the hell did we get a Codepoint of zero?
+ if(Codepoint == 0) return;
+
+ // Key Up
+ if( Codepoint & 0x80000000 )
+ {
+ Codepoint &= 0x7FFFFFFF;
+ switch(Codepoint)
+ {
+ #if !USE_CTRL_ALT
+ case KEY_RSHIFT: gbVT_CtrlDown = 0; break;
+ case KEY_LSHIFT: gbVT_AltDown = 0; break;
+ #else
+ case KEY_LALT:
+ case KEY_RALT:
+ gbVT_AltDown = 0;
+ break;
+ case KEY_LCTRL:
+ case KEY_RCTRL:
+ gbVT_CtrlDown = 0;
+ break;
+ #endif
+ }
+ return;
+ }
+
+ switch(Codepoint)
+ {
+ #if !USE_CTRL_ALT
+ case KEY_RSHIFT: gbVT_CtrlDown = 1; break;
+ case KEY_LSHIFT: gbVT_AltDown = 1; break;
+ #else
+ case KEY_LALT:
+ case KEY_RALT:
+ gbVT_AltDown = 1;
+ break;
+ case KEY_LCTRL:
+ case KEY_RCTRL:
+ gbVT_CtrlDown = 1;
+ break;
+ #endif
+
+ default:
+ #if USE_CTRL_ALT
+ if(!gbVT_AltDown || !gbVT_CtrlDown)
+ break;
+ #endif
+ switch(Codepoint)
+ {
+ case KEY_F1: VT_SetTerminal(0); return;
+ case KEY_F2: VT_SetTerminal(1); return;
+ case KEY_F3: VT_SetTerminal(2); return;
+ case KEY_F4: VT_SetTerminal(3); return;
+ case KEY_F5: VT_SetTerminal(4); return;
+ case KEY_F6: VT_SetTerminal(5); return;
+ case KEY_F7: VT_SetTerminal(6); return;
+ case KEY_F8: VT_SetTerminal(7); return;
+ case KEY_F9: VT_SetTerminal(8); return;
+ case KEY_F10: VT_SetTerminal(9); return;
+ case KEY_F11: VT_SetTerminal(10); return;
+ case KEY_F12: VT_SetTerminal(11); return;
+ case KEY_PGUP:
+ return;
+ case KEY_PGDOWN:
+ return;
+ }
+ }
+
+ // Encode key
+ if(term->Mode == TERM_MODE_TEXT)
+ {
+ Uint8 buf[6] = {0};
+ int len = 0;
+
+ // Ignore Modifer Keys
+ if(Codepoint > KEY_MODIFIERS) return;
+
+ // Get UTF-8/ANSI Encoding
+ switch(Codepoint)
+ {
+ case KEY_LEFT:
+ buf[0] = '\x1B'; buf[1] = '['; buf[2] = 'D';
+ len = 3;
+ break;
+ case KEY_RIGHT:
+ buf[0] = '\x1B'; buf[1] = '['; buf[2] = 'C';
+ len = 3;
+ break;
+ case KEY_UP:
+ buf[0] = '\x1B'; buf[1] = '['; buf[2] = 'A';
+ len = 3;
+ break;
+ case KEY_DOWN:
+ buf[0] = '\x1B'; buf[1] = '['; buf[2] = 'B';
+ len = 3;
+ break;
+
+ case KEY_PGUP:
+ buf[0] = '\x1B'; buf[1] = '['; buf[2] = '5'; // Some overline also
+ //len = 4; // Commented out until I'm sure
+ break;
+ case KEY_PGDOWN:
+ len = 0;
+ break;
+
+ // Attempt to encode in UTF-8
+ default:
+ len = WriteUTF8( buf, Codepoint );
+ if(len == 0) {
+ Warning("Codepoint (%x) is unrepresentable in UTF-8", Codepoint);
+ }
+ break;
+ }
+
+ if(len == 0) {
+ // Unprintable / Don't Pass
+ return;
+ }
+
+ // Write
+ if( MAX_INPUT_CHARS8 - term->InputWrite >= len )
+ memcpy( &term->InputBuffer[term->InputWrite], buf, len );
+ else {
+ memcpy( &term->InputBuffer[term->InputWrite], buf, MAX_INPUT_CHARS8 - term->InputWrite );
+ memcpy( &term->InputBuffer[0], buf, len - (MAX_INPUT_CHARS8 - term->InputWrite) );
+ }
+ // Roll the buffer over
+ term->InputWrite += len;
+ term->InputWrite %= MAX_INPUT_CHARS8;
+ if( (term->InputWrite - term->InputRead + MAX_INPUT_CHARS8)%MAX_INPUT_CHARS8 < len ) {
+ term->InputRead = term->InputWrite + 1;
+ term->InputRead %= MAX_INPUT_CHARS8;
+ }
+
+ }
+ else
+ {
+ // Encode the raw UTF-32 Key
+ ((Uint32*)term->InputBuffer)[ term->InputWrite ] = Codepoint;
+ term->InputWrite ++;
+ term->InputWrite %= MAX_INPUT_CHARS32;
+ if(term->InputRead == term->InputWrite) {
+ term->InputRead ++;
+ term->InputRead %= MAX_INPUT_CHARS32;
+ }
+ }