3 * - By John Hodge (thePowersGang)
8 #include <axwin3/axwin.h>
9 #include <axwin3/menu.h>
10 #include <axwin3/richtext.h>
11 #include <axwin3/keysyms.h>
13 #include <acess/sys.h>
14 #include "include/display.h"
15 #include "include/vt100.h"
19 #include <acess/devices/pty.h>
22 int main(int argc, char *argv[], const char **envp);
23 int Term_KeyHandler(tHWND Window, int bPress, uint32_t KeySym, uint32_t Translated);
24 int Term_MouseHandler(tHWND Window, int bPress, int Button, int Row, int Col);
25 void Term_HandleOutput(int Len, const char *Buf);
33 int main(int argc, char *argv[], const char **envp)
37 // --- Build up window
38 gMainWindow = AxWin3_RichText_CreateWindow(NULL, AXWIN3_RICHTEXT_READONLY);
39 AxWin3_SetWindowTitle(gMainWindow, "Terminal"); // TODO: Update title with other info
41 gMenuWindow = AxWin3_Menu_Create(gMainWindow);
42 AxWin3_Menu_AddItem(gMenuWindow, "Copy\tWin+C", NULL, NULL, 0, NULL);
43 AxWin3_Menu_AddItem(gMenuWindow, "Paste\tWin+V", NULL, NULL, 0, NULL);
44 // TODO: Populate menu
49 AxWin3_RichText_SetKeyHandler (gMainWindow, Term_KeyHandler);
50 AxWin3_RichText_SetMouseHandler (gMainWindow, Term_MouseHandler);
51 AxWin3_RichText_SetDefaultColour(gMainWindow, 0xFFFFFF);
52 AxWin3_RichText_SetBackground (gMainWindow, 0x000000);
53 AxWin3_RichText_SetFont (gMainWindow, "#monospace", 10);
54 AxWin3_RichText_SetCursorPos (gMainWindow, 0, 0);
55 AxWin3_RichText_SetCursorType (gMainWindow, AXWIN3_RICHTEXT_CURSOR_INV);
56 AxWin3_RichText_SetCursorBlink (gMainWindow, 1);
58 Display_Init(80, 25, 100);
59 AxWin3_ResizeWindow(gMainWindow, 80*8, 25*16);
60 AxWin3_MoveWindow(gMainWindow, 20, 50);
61 AxWin3_ShowWindow(gMainWindow, 1);
62 AxWin3_FocusWindow(gMainWindow);
65 giPTYHandle = _SysOpen("/Devices/pts/gui0", OPENFLAG_READ|OPENFLAG_WRITE|OPENFLAG_CREATE);
66 if( giPTYHandle < 0 ) {
67 perror("Unable to create/open PTY");
68 _SysDebug("Unable to create/open PTY: %s", strerror(errno));
73 struct ptymode mode = {.InputMode = PTYIMODE_CANON|PTYIMODE_ECHO, .OutputMode=0};
74 struct ptydims dims = {.W = 80, .H = 25};
75 _SysIOCtl(giPTYHandle, PTY_IOCTL_SETMODE, &mode);
76 _SysIOCtl(giPTYHandle, PTY_IOCTL_SETDIMS, &dims);
81 int fd = _SysOpen("/Devices/pts/gui0c", OPENFLAG_READ|OPENFLAG_WRITE);
82 int fds[] = {fd, fd, fd};
83 const char *argv[] = {"CLIShell", NULL};
84 int pid = _SysSpawn("/Acess/Bin/CLIShell", argv, envp, 3, fds, NULL);
86 _SysDebug("ERROR: Shell spawn failed: %s", strerror(errno));
96 FD_SET(giPTYHandle, &fds);
97 AxWin3_MessageSelect(giPTYHandle + 1, &fds);
99 if( FD_ISSET(giPTYHandle, &fds) )
101 _SysDebug("Activity on child stdout");
102 // Read and update screen
104 int len = _SysRead(giPTYHandle, buf, sizeof(buf));
105 if( len <= 0 ) break;
107 Term_HandleOutput(len, buf);
114 int Term_KeyHandler(tHWND Window, int bPress, uint32_t KeySym, uint32_t Translated)
116 static int ctrl_state = 0;
119 #define _bitset(var,bit,set) do{if(set)var|=1<<(bit);else var&=~(1<<(bit));}while(0)
122 case KEYSYM_LEFTCTRL:
123 _bitset(ctrl_state, 0, bPress!=0);
125 case KEYSYM_RIGHTCTRL:
126 _bitset(ctrl_state, 1, bPress!=0);
132 // - Ctrl-A -- Ctrl-Z
133 if( ctrl_state && KeySym >= KEYSYM_a && KeySym <= KEYSYM_z )
135 Translated = KeySym - KEYSYM_a + 1;
136 _SysDebug("Ctrl-%c: KS %x => Trans %x", 'A'+(KeySym-KEYSYM_a), KeySym, Translated);
148 len = WriteUTF8(buf, Translated);
150 _SysDebug("Keystroke %x:%x translated to '%.*s'", KeySym, Translated, len, buf);
151 _SysWrite(giPTYHandle, buf, len);
156 // No translation, look for escape sequences to send
157 const char *str = NULL;
160 case KEYSYM_LEFTARROW:
163 case KEYSYM_RIGHTARROW:
169 case KEYSYM_DOWNARROW:
175 _SysWrite(giPTYHandle, str, strlen(str));
181 int Term_MouseHandler(tHWND Window, int bPress, int Button, int Row, int Col)
186 void Term_HandleOutput(int Len, const char *Buf)
188 // TODO: Handle graphical / accelerated modes
195 esc_len = Term_HandleVT100(Len - ofs, Buf + ofs);
197 Display_AddText(-esc_len, Buf + ofs);
201 //_SysDebug("Len = %i, ofs = %i", Len, ofs);