X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2FApplications%2Fgui_shell_src%2Fmain.c;h=822f28b8646bd1ff809ae8168771b29974fa7c12;hb=704880994998a15d512860b5bb68d4b7285db510;hp=c7aac852d134598045464c53ef25c080890217ca;hpb=479d0634670b58da044bc58149662adba0ad1d0b;p=tpg%2Facess2.git diff --git a/Usermode/Applications/gui_shell_src/main.c b/Usermode/Applications/gui_shell_src/main.c index c7aac852..822f28b8 100644 --- a/Usermode/Applications/gui_shell_src/main.c +++ b/Usermode/Applications/gui_shell_src/main.c @@ -8,20 +8,26 @@ #include #include #include +#include #include +#include #include "include/display.h" #include "include/vt100.h" +#include +#include +#include +#include // === PROTOTYPES === int main(int argc, char *argv[], const char **envp); int Term_KeyHandler(tHWND Window, int bPress, uint32_t KeySym, uint32_t Translated); int Term_MouseHandler(tHWND Window, int bPress, int Button, int Row, int Col); +void Term_HandleOutput(tTerminal *Term, int Len, const char *Buf); // === GLOBALS === tHWND gMainWindow; tHWND gMenuWindow; - int giChildStdin; - int giChildStdout; + int giPTYHandle; // === CODE === int main(int argc, char *argv[], const char **envp) @@ -29,7 +35,7 @@ int main(int argc, char *argv[], const char **envp) AxWin3_Connect(NULL); // --- Build up window - gMainWindow = AxWin3_RichText_CreateWindow(NULL, 0); + gMainWindow = AxWin3_RichText_CreateWindow(NULL, AXWIN3_RICHTEXT_READONLY); AxWin3_SetWindowTitle(gMainWindow, "Terminal"); // TODO: Update title with other info gMenuWindow = AxWin3_Menu_Create(gMainWindow); @@ -49,25 +55,38 @@ int main(int argc, char *argv[], const char **envp) AxWin3_RichText_SetCursorType (gMainWindow, AXWIN3_RICHTEXT_CURSOR_INV); AxWin3_RichText_SetCursorBlink (gMainWindow, 1); - // - AxWin3_RichText_SetLineCount(gMainWindow, 3); - AxWin3_RichText_SendLine(gMainWindow, 0, "First line!"); - AxWin3_RichText_SendLine(gMainWindow, 2, "Third line! \x01""ff0000A red"); - // - - AxWin3_ResizeWindow(gMainWindow, 600, 400); - AxWin3_MoveWindow(gMainWindow, 50, 50); + tTerminal *term = Display_Init(80, 25, 100); + AxWin3_ResizeWindow(gMainWindow, 80*8, 25*16); + AxWin3_MoveWindow(gMainWindow, 20, 50); AxWin3_ShowWindow(gMainWindow, 1); AxWin3_FocusWindow(gMainWindow); - // Spawn shell - giChildStdout = open("/Devices/FIFO/anon", O_RDWR); - giChildStdin = open("/Devices/FIFO/anon", O_RDWR); + // Create PTY + giPTYHandle = _SysOpen("/Devices/pts/ptmx", OPENFLAG_READ|OPENFLAG_WRITE); + if( giPTYHandle < 0 ) { + perror("Unable to create/open PTY"); + _SysDebug("Unable to create/open PTY: %s", strerror(errno)); + return -1; + } + // - Initialise + { + _SysIOCtl(giPTYHandle, PTY_IOCTL_SETID, "gui0"); + struct ptymode mode = {.InputMode = PTYIMODE_CANON|PTYIMODE_ECHO, .OutputMode=0}; + struct ptydims dims = {.W = 80, .H = 25}; + _SysIOCtl(giPTYHandle, PTY_IOCTL_SETMODE, &mode); + _SysIOCtl(giPTYHandle, PTY_IOCTL_SETDIMS, &dims); + } + // Spawn shell { - int fds[] = {giChildStdin, giChildStdout, giChildStdout}; + int fd = _SysOpen("/Devices/pts/gui0", OPENFLAG_READ|OPENFLAG_WRITE); + int fds[] = {fd, fd, fd}; const char *argv[] = {"CLIShell", NULL}; - _SysSpawn("/Acess/Bin/CLIShell", argv, envp, 3, fds, NULL); + int pid = _SysSpawn("/Acess/Bin/CLIShell", argv, envp, 3, fds, NULL); + if( pid < 0 ) + _SysDebug("ERROR: Shell spawn failed: %s", strerror(errno)); + _SysIOCtl(fd, PTY_IOCTL_SETPGRP, &pid); + _SysClose(fd); } // Main loop @@ -76,17 +95,18 @@ int main(int argc, char *argv[], const char **envp) fd_set fds; FD_ZERO(&fds); - FD_SET(giChildStdout, &fds); - AxWin3_MessageSelect(giChildStdout + 1, &fds); + FD_SET(giPTYHandle, &fds); + AxWin3_MessageSelect(giPTYHandle + 1, &fds); - if( FD_ISSET(giChildStdout, &fds) ) + if( FD_ISSET(giPTYHandle, &fds) ) { + _SysDebug("Activity on child stdout"); // Read and update screen - char buf[32]; - int len = read(giChildStdout, buf, sizeof(buf)); + char buf[128]; + int len = _SysRead(giPTYHandle, buf, sizeof(buf)); if( len <= 0 ) break; - Term_HandleOutput(len, buf); + Term_HandleOutput(term, len, buf); } } @@ -98,38 +118,64 @@ int Term_KeyHandler(tHWND Window, int bPress, uint32_t KeySym, uint32_t Translat static int ctrl_state = 0; // Handle modifiers - #define _bitset(var,bit,set) do{if(set)var|=1<<(bit);else var&=1<<(bit);}while(0) + #define _bitset(var,bit,set) do{if(set)var|=1<<(bit);else var&=~(1<<(bit));}while(0) switch(KeySym) { - case KEY_LCTRL: - _bitset(ctrl_state, 0, bPress); + case KEYSYM_LEFTCTRL: + _bitset(ctrl_state, 0, bPress!=0); return 0; - case KEY_RCTRL: - _bitset(ctrl_state, 0, bPress); + case KEYSYM_RIGHTCTRL: + _bitset(ctrl_state, 1, bPress!=0); return 0; } #undef _bitset // Handle shortcuts // - Ctrl-A -- Ctrl-Z - if( ctrl_state && KeySym >= KEY_a && KeySym <= KEY_z ) + if( ctrl_state && KeySym >= KEYSYM_a && KeySym <= KEYSYM_z ) { - Translated = KeySym - KEY_a + 1; + Translated = KeySym - KEYSYM_a + 1; + _SysDebug("Ctrl-%c: KS %x => Trans %x", 'A'+(KeySym-KEYSYM_a), KeySym, Translated); } - if( Translated ) + // == 2 :: FIRE + if( bPress == 2 ) { - // Encode and send + if( Translated ) + { + char buf[6]; + int len; + + // Encode and send + len = WriteUTF8(buf, Translated); + + _SysDebug("Keystroke %x:%x translated to '%.*s'", KeySym, Translated, len, buf); + _SysWrite(giPTYHandle, buf, len); + + return 0; + } - return 0; - } - - // No translation, look for escape sequences to send - switch(KeySym) - { - case KEY_LEFTARROW: - // str = "\x1b[D"; - break; + // No translation, look for escape sequences to send + const char *str = NULL; + switch(KeySym) + { + case KEYSYM_LEFTARROW: + str = "\x1b[D"; + break; + case KEYSYM_RIGHTARROW: + str = "\x1b[C"; + break; + case KEYSYM_UPARROW: + str = "\x1b[A"; + break; + case KEYSYM_DOWNARROW: + str = "\x1b[B"; + break; + } + if( str ) + { + _SysWrite(giPTYHandle, str, strlen(str)); + } } return 0; } @@ -139,7 +185,7 @@ int Term_MouseHandler(tHWND Window, int bPress, int Button, int Row, int Col) return 0; } -void Term_HandleOutput(int Len, const char *Buf) +void Term_HandleOutput(tTerminal *Term, int Len, const char *Buf) { // TODO: Handle graphical / accelerated modes @@ -148,13 +194,15 @@ void Term_HandleOutput(int Len, const char *Buf) while( ofs < Len ) { - esc_len = Term_HandleVT100(Len - ofs, Buf + ofs); + esc_len = Term_HandleVT100(Term, Len - ofs, Buf + ofs); if( esc_len < 0 ) { - Display_AddText(-esc_len, Buf + ofs); + Display_AddText(Term, -esc_len, Buf + ofs); esc_len = -esc_len; } - Len -= esc_len; ofs += esc_len; + //_SysDebug("Len = %i, ofs = %i", Len, ofs); } + + Display_Flush(Term); }