Kernel - Changed PTY code to not expose userland server node as a file
[tpg/acess2.git] / Usermode / Applications / gui_shell_src / main.c
1 /*
2  * Acess GUI Terminal
3  * - By John Hodge (thePowersGang)
4  *
5  * main.c
6  * - Core
7  */
8 #include <axwin3/axwin.h>
9 #include <axwin3/menu.h>
10 #include <axwin3/richtext.h>
11 #include <axwin3/keysyms.h>
12 #include <stdio.h>
13 #include <acess/sys.h>
14 #include "include/display.h"
15 #include "include/vt100.h"
16 #include <string.h>
17 #include <unicode.h>
18 #include <errno.h>
19 #include <acess/devices/pty.h>
20
21 // === PROTOTYPES ===
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);
26
27 // === GLOBALS ===
28 tHWND   gMainWindow;
29 tHWND   gMenuWindow;
30  int    giPTYHandle;
31
32 // === CODE ===
33 int main(int argc, char *argv[], const char **envp)
34 {
35         AxWin3_Connect(NULL);
36         
37         // --- Build up window
38         gMainWindow = AxWin3_RichText_CreateWindow(NULL, AXWIN3_RICHTEXT_READONLY);
39         AxWin3_SetWindowTitle(gMainWindow, "Terminal"); // TODO: Update title with other info
40
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  
45
46
47         // TODO: Tabs?
48         
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);
57
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);
63
64         // Create PTY
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));
69                 return -1;
70         }
71         // - Initialise
72         {
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);
78         }
79
80         // Spawn shell
81         {
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);
86                 if( pid < 0 )
87                         _SysDebug("ERROR: Shell spawn failed: %s", strerror(errno));
88                 _SysClose(fd);
89         }
90
91         // Main loop
92         for( ;; )
93         {
94                 fd_set  fds;
95                 
96                 FD_ZERO(&fds);
97                 FD_SET(giPTYHandle, &fds);
98                 AxWin3_MessageSelect(giPTYHandle + 1, &fds);
99                 
100                 if( FD_ISSET(giPTYHandle, &fds) )
101                 {
102                         _SysDebug("Activity on child stdout");
103                         // Read and update screen
104                         char    buf[128];
105                         int len = _SysRead(giPTYHandle, buf, sizeof(buf));
106                         if( len <= 0 )  break;
107                         
108                         Term_HandleOutput(len, buf);
109                 }
110         }
111
112         return 0;
113 }
114
115 int Term_KeyHandler(tHWND Window, int bPress, uint32_t KeySym, uint32_t Translated)
116 {
117         static int      ctrl_state = 0;
118
119         // Handle modifiers
120         #define _bitset(var,bit,set) do{if(set)var|=1<<(bit);else var&=~(1<<(bit));}while(0)
121         switch(KeySym)
122         {
123         case KEYSYM_LEFTCTRL:
124                 _bitset(ctrl_state, 0, bPress!=0);
125                 return 0;
126         case KEYSYM_RIGHTCTRL:
127                 _bitset(ctrl_state, 1, bPress!=0);
128                 return 0;
129         }
130         #undef _bitset
131
132         // Handle shortcuts
133         // - Ctrl-A -- Ctrl-Z
134         if( ctrl_state && KeySym >= KEYSYM_a && KeySym <= KEYSYM_z )
135         {
136                 Translated = KeySym - KEYSYM_a + 1;
137                 _SysDebug("Ctrl-%c: KS %x => Trans %x", 'A'+(KeySym-KEYSYM_a), KeySym, Translated);
138         }
139
140         // == 2 :: FIRE
141         if( bPress == 2 )
142         {
143                 if( Translated )
144                 {
145                         char    buf[6];
146                          int    len;
147                         
148                         // Encode and send
149                         len = WriteUTF8(buf, Translated);
150                         
151                         _SysDebug("Keystroke %x:%x translated to '%.*s'", KeySym, Translated, len, buf);
152                         _SysWrite(giPTYHandle, buf, len);
153                         
154                         return 0;
155                 }
156                 
157                 // No translation, look for escape sequences to send
158                 const char *str = NULL;
159                 switch(KeySym)
160                 {
161                 case KEYSYM_LEFTARROW:
162                         str = "\x1b[D";
163                         break;
164                 case KEYSYM_RIGHTARROW:
165                         str = "\x1b[C";
166                         break;
167                 case KEYSYM_UPARROW:
168                         str = "\x1b[A";
169                         break;
170                 case KEYSYM_DOWNARROW:
171                         str = "\x1b[B";
172                         break;
173                 }
174                 if( str )
175                 {
176                         _SysWrite(giPTYHandle, str, strlen(str));
177                 }
178         }
179         return 0;
180 }
181
182 int Term_MouseHandler(tHWND Window, int bPress, int Button, int Row, int Col)
183 {
184         return 0;
185 }
186
187 void Term_HandleOutput(int Len, const char *Buf)
188 {
189         // TODO: Handle graphical / accelerated modes
190
191          int    ofs = 0;
192          int    esc_len = 0;
193
194         while( ofs < Len )
195         {
196                 esc_len = Term_HandleVT100(Len - ofs, Buf + ofs);
197                 if( esc_len < 0 ) {
198                         Display_AddText(-esc_len, Buf + ofs);
199                         esc_len = -esc_len;
200                 }
201                 ofs += esc_len;
202                 //_SysDebug("Len = %i, ofs = %i", Len, ofs);
203         }
204         
205         Display_Flush();
206 }
207

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