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(tTerminal *Term, 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 tTerminal *term = 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/ptmx", OPENFLAG_READ|OPENFLAG_WRITE);
66 if( giPTYHandle < 0 ) {
67 perror("Unable to create/open PTY");
68 _SysDebug("Unable to create/open PTY: %s", strerror(errno));
73 _SysIOCtl(giPTYHandle, PTY_IOCTL_SETID, "gui0");
74 struct ptymode mode = {.InputMode = PTYIMODE_CANON|PTYIMODE_ECHO, .OutputMode=0};
75 struct ptydims dims = {.W = 80, .H = 25};
76 _SysIOCtl(giPTYHandle, PTY_IOCTL_SETMODE, &mode);
77 _SysIOCtl(giPTYHandle, PTY_IOCTL_SETDIMS, &dims);
82 int fd = _SysOpen("/Devices/pts/gui0", OPENFLAG_READ|OPENFLAG_WRITE);
83 int fds[] = {fd, fd, fd};
84 const char *argv[] = {"CLIShell", NULL};
85 int pid = _SysSpawn("/Acess/Bin/CLIShell", argv, envp, 3, fds, NULL);
87 _SysDebug("ERROR: Shell spawn failed: %s", strerror(errno));
88 _SysIOCtl(fd, PTY_IOCTL_SETPGRP, &pid);
98 FD_SET(giPTYHandle, &fds);
99 AxWin3_MessageSelect(giPTYHandle + 1, &fds);
101 if( FD_ISSET(giPTYHandle, &fds) )
103 _SysDebug("Activity on child stdout");
104 // Read and update screen
106 int len = _SysRead(giPTYHandle, buf, sizeof(buf));
107 if( len <= 0 ) break;
109 Term_HandleOutput(term, len, buf);
116 int Term_KeyHandler(tHWND Window, int bPress, uint32_t KeySym, uint32_t Translated)
118 static int ctrl_state = 0;
121 #define _bitset(var,bit,set) do{if(set)var|=1<<(bit);else var&=~(1<<(bit));}while(0)
124 case KEYSYM_LEFTCTRL:
125 _bitset(ctrl_state, 0, bPress!=0);
127 case KEYSYM_RIGHTCTRL:
128 _bitset(ctrl_state, 1, bPress!=0);
134 // - Ctrl-A -- Ctrl-Z
135 if( ctrl_state && KeySym >= KEYSYM_a && KeySym <= KEYSYM_z )
137 Translated = KeySym - KEYSYM_a + 1;
138 _SysDebug("Ctrl-%c: KS %x => Trans %x", 'A'+(KeySym-KEYSYM_a), KeySym, Translated);
150 len = WriteUTF8(buf, Translated);
152 _SysDebug("Keystroke %x:%x translated to '%.*s'", KeySym, Translated, len, buf);
153 _SysWrite(giPTYHandle, buf, len);
158 // No translation, look for escape sequences to send
159 const char *str = NULL;
162 case KEYSYM_LEFTARROW:
165 case KEYSYM_RIGHTARROW:
171 case KEYSYM_DOWNARROW:
177 _SysWrite(giPTYHandle, str, strlen(str));
183 int Term_MouseHandler(tHWND Window, int bPress, int Button, int Row, int Col)
188 void Term_HandleOutput(tTerminal *Term, int Len, const char *Buf)
190 // TODO: Handle graphical / accelerated modes
192 //_SysDebug("Term_HandleOutput: %i \"%.*s\"", Len, Len, Buf);
199 esc_len = Term_HandleVT100(Term, Len - ofs, Buf + ofs);
201 Display_AddText(Term, -esc_len, Buf + ofs);
205 //_SysDebug("Len = %i, ofs = %i", Len, ofs);