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

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