Merge branch 'master' of git://localhost/acess2
[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 <stdio.h>
12 #include "include/display.h"
13 #include "include/vt100.h"
14
15 // === PROTOTYPES ===
16  int    main(int argc, char *argv[], const char **envp);
17  int    Term_KeyHandler(tHWND Window, int bPress, uint32_t KeySym, uint32_t Translated);
18  int    Term_MouseHandler(tHWND Window, int bPress, int Button, int Row, int Col);
19
20 // === GLOBALS ===
21 tHWND   gMainWindow;
22 tHWND   gMenuWindow;
23  int    giChildStdin;
24  int    giChildStdout;
25
26 // === CODE ===
27 int main(int argc, char *argv[], const char **envp)
28 {
29         AxWin3_Connect(NULL);
30         
31         // --- Build up window
32         gMainWindow = AxWin3_RichText_CreateWindow(NULL, 0);
33         AxWin3_SetWindowTitle(gMainWindow, "Terminal"); // TODO: Update title with other info
34
35         gMenuWindow = AxWin3_Menu_Create(gMainWindow);
36         AxWin3_Menu_AddItem(gMenuWindow, "Copy\tWin+C", NULL, NULL, 0, NULL);
37         AxWin3_Menu_AddItem(gMenuWindow, "Paste\tWin+V", NULL, NULL, 0, NULL);
38         // TODO: Populate menu  
39
40
41         // TODO: Tabs?
42         
43         AxWin3_RichText_SetKeyHandler   (gMainWindow, Term_KeyHandler);
44         AxWin3_RichText_SetMouseHandler (gMainWindow, Term_MouseHandler);
45         AxWin3_RichText_SetDefaultColour(gMainWindow, 0xFFFFFF);
46         AxWin3_RichText_SetBackground   (gMainWindow, 0x000000);
47         AxWin3_RichText_SetFont         (gMainWindow, "#monospace", 10);
48         AxWin3_RichText_SetCursorPos    (gMainWindow, 0, 0);
49         AxWin3_RichText_SetCursorType   (gMainWindow, AXWIN3_RICHTEXT_CURSOR_INV);
50         AxWin3_RichText_SetCursorBlink  (gMainWindow, 1);
51
52         // <testing>
53         AxWin3_RichText_SetLineCount(gMainWindow, 3);
54         AxWin3_RichText_SendLine(gMainWindow, 0, "First line!");
55         AxWin3_RichText_SendLine(gMainWindow, 2, "Third line! \x01""ff0000A red");
56         // </testing>
57
58         AxWin3_ResizeWindow(gMainWindow, 600, 400);
59         AxWin3_MoveWindow(gMainWindow, 50, 50);
60         AxWin3_ShowWindow(gMainWindow, 1);
61         AxWin3_FocusWindow(gMainWindow);
62
63         // Spawn shell
64         giChildStdout = open("/Devices/FIFO/anon", O_RDWR);
65         giChildStdin = open("/Devices/FIFO/anon", O_RDWR);
66
67         {
68                  int    fds[] = {giChildStdin, giChildStdout, giChildStdout};
69                 const char      *argv[] = {"CLIShell", NULL};
70                 _SysSpawn("/Acess/Bin/CLIShell", argv, envp, 3, fds, NULL);
71         }
72
73         // Main loop
74         for( ;; )
75         {
76                 fd_set  fds;
77                 
78                 FD_ZERO(&fds);
79                 FD_SET(giChildStdout, &fds);
80                 AxWin3_MessageSelect(giChildStdout + 1, &fds);
81                 
82                 if( FD_ISSET(giChildStdout, &fds) )
83                 {
84                         // Read and update screen
85                         char    buf[32];
86                         int len = read(giChildStdout, buf, sizeof(buf));
87                         if( len <= 0 )  break;
88                         
89                         Term_HandleOutput(len, buf);
90                 }
91         }
92
93         return 0;
94 }
95
96 int Term_KeyHandler(tHWND Window, int bPress, uint32_t KeySym, uint32_t Translated)
97 {
98         static int      ctrl_state = 0;
99
100         // Handle modifiers
101         #define _bitset(var,bit,set) do{if(set)var|=1<<(bit);else var&=1<<(bit);}while(0)
102         switch(KeySym)
103         {
104         case KEY_LCTRL:
105                 _bitset(ctrl_state, 0, bPress);
106                 return 0;
107         case KEY_RCTRL:
108                 _bitset(ctrl_state, 0, bPress);
109                 return 0;
110         }
111         #undef _bitset
112
113         // Handle shortcuts
114         // - Ctrl-A -- Ctrl-Z
115         if( ctrl_state && KeySym >= KEY_a && KeySym <= KEY_z )
116         {
117                 Translated = KeySym - KEY_a + 1;
118         }
119
120         if( Translated )
121         {
122                 // Encode and send
123                 
124                 return 0;
125         }
126         
127         // No translation, look for escape sequences to send
128         switch(KeySym)
129         {
130         case KEY_LEFTARROW:
131         //      str = "\x1b[D";
132                 break;
133         }
134         return 0;
135 }
136
137 int Term_MouseHandler(tHWND Window, int bPress, int Button, int Row, int Col)
138 {
139         return 0;
140 }
141
142 void Term_HandleOutput(int Len, const char *Buf)
143 {
144         // TODO: Handle graphical / accelerated modes
145
146          int    ofs = 0;
147          int    esc_len = 0;
148
149         while( ofs < Len )
150         {
151                 esc_len = Term_HandleVT100(Len - ofs, Buf + ofs);
152                 if( esc_len < 0 ) {
153                         Display_AddText(-esc_len, Buf + ofs);
154                         esc_len = -esc_len;
155                 }
156                 Len -= esc_len;
157                 ofs += esc_len;
158         }
159 }
160

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