Kernel/VFS - Added warning (instead of LOG) when GetNodeFromInode is unsupported
[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/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));
69                 return -1;
70         }
71         // - Initialise
72         {
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);
77         }
78
79         // Spawn shell
80         {
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);
85                 if( pid < 0 )
86                         _SysDebug("ERROR: Shell spawn failed: %s", strerror(errno));
87                 _SysClose(fd);
88         }
89
90         // Main loop
91         for( ;; )
92         {
93                 fd_set  fds;
94                 
95                 FD_ZERO(&fds);
96                 FD_SET(giPTYHandle, &fds);
97                 AxWin3_MessageSelect(giPTYHandle + 1, &fds);
98                 
99                 if( FD_ISSET(giPTYHandle, &fds) )
100                 {
101                         _SysDebug("Activity on child stdout");
102                         // Read and update screen
103                         char    buf[128];
104                         int len = _SysRead(giPTYHandle, buf, sizeof(buf));
105                         if( len <= 0 )  break;
106                         
107                         Term_HandleOutput(len, buf);
108                 }
109         }
110
111         return 0;
112 }
113
114 int Term_KeyHandler(tHWND Window, int bPress, uint32_t KeySym, uint32_t Translated)
115 {
116         static int      ctrl_state = 0;
117
118         // Handle modifiers
119         #define _bitset(var,bit,set) do{if(set)var|=1<<(bit);else var&=~(1<<(bit));}while(0)
120         switch(KeySym)
121         {
122         case KEYSYM_LEFTCTRL:
123                 _bitset(ctrl_state, 0, bPress!=0);
124                 return 0;
125         case KEYSYM_RIGHTCTRL:
126                 _bitset(ctrl_state, 1, bPress!=0);
127                 return 0;
128         }
129         #undef _bitset
130
131         // Handle shortcuts
132         // - Ctrl-A -- Ctrl-Z
133         if( ctrl_state && KeySym >= KEYSYM_a && KeySym <= KEYSYM_z )
134         {
135                 Translated = KeySym - KEYSYM_a + 1;
136                 _SysDebug("Ctrl-%c: KS %x => Trans %x", 'A'+(KeySym-KEYSYM_a), KeySym, Translated);
137         }
138
139         // == 2 :: FIRE
140         if( bPress == 2 )
141         {
142                 if( Translated )
143                 {
144                         char    buf[6];
145                          int    len;
146                         
147                         // Encode and send
148                         len = WriteUTF8(buf, Translated);
149                         
150                         _SysDebug("Keystroke %x:%x translated to '%.*s'", KeySym, Translated, len, buf);
151                         _SysWrite(giPTYHandle, buf, len);
152                         
153                         return 0;
154                 }
155                 
156                 // No translation, look for escape sequences to send
157                 const char *str = NULL;
158                 switch(KeySym)
159                 {
160                 case KEYSYM_LEFTARROW:
161                         str = "\x1b[D";
162                         break;
163                 case KEYSYM_RIGHTARROW:
164                         str = "\x1b[C";
165                         break;
166                 case KEYSYM_UPARROW:
167                         str = "\x1b[A";
168                         break;
169                 case KEYSYM_DOWNARROW:
170                         str = "\x1b[B";
171                         break;
172                 }
173                 if( str )
174                 {
175                         _SysWrite(giPTYHandle, str, strlen(str));
176                 }
177         }
178         return 0;
179 }
180
181 int Term_MouseHandler(tHWND Window, int bPress, int Button, int Row, int Col)
182 {
183         return 0;
184 }
185
186 void Term_HandleOutput(int Len, const char *Buf)
187 {
188         // TODO: Handle graphical / accelerated modes
189
190          int    ofs = 0;
191          int    esc_len = 0;
192
193         while( ofs < Len )
194         {
195                 esc_len = Term_HandleVT100(Len - ofs, Buf + ofs);
196                 if( esc_len < 0 ) {
197                         Display_AddText(-esc_len, Buf + ofs);
198                         esc_len = -esc_len;
199                 }
200                 ofs += esc_len;
201                 //_SysDebug("Len = %i, ofs = %i", Len, ofs);
202         }
203         
204         Display_Flush();
205 }
206

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