e0cf78d9892874dae3b7e0db94bcd0a1beffd62b
[fw/sdcc] / sim / ucsim / gui.src / serio.src / frontend.cc
1 /******************************************************************************
2  * to emulate the serial input and output of an 8051 controller               *
3  * frontend.cc - the ncurses frontend                                         *
4  ******************************************************************************/
5 #include <sys/types.h>
6 #include <iostream.h>
7 #include <stdlib.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <curses.h>
11 #include <errno.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include "frontend.hh"
15
16 Viewer::Viewer()
17 {
18         /* initalise the output screen */
19         initscr();
20         cbreak();
21         noecho();
22         nl();
23         intrflush(stdscr,FALSE);
24         keypad(stdscr, TRUE);
25         
26         /* clear the screen and off you go */
27         refresh();
28
29         // get the coordinates for the box
30         /* create the subwindow */
31         win_c.min_x = win_c.min_y = 0;
32         getmaxyx(stdscr, win_c.max_y, win_c.max_x);
33
34         /* define the boxed size */
35         topleft.x = win_c.min_x + 1;
36         bottomright.x = win_c.max_x - 2;
37         topleft.y = win_c.min_y + 1;
38         bottomright.y = win_c.max_y - 2;
39         middle_y = (int)((bottomright.y-topleft.y)/2)+1;
40         middle_x = (int)((bottomright.x-topleft.x)/2)+1;
41
42         // draw the two subwindows
43         inp_c.min_x = outp_c.min_x = topleft.x;
44         inp_c.max_x = outp_c.max_x = bottomright.x;
45         inp_c.min_y = topleft.y;
46         inp_c.max_y = middle_y-topleft.y;
47         outp_c.min_y = middle_y+1;
48         outp_c.max_y = bottomright.y-middle_y;
49         inp = subwin(stdscr, inp_c.max_y, inp_c.max_x, inp_c.min_y, inp_c.min_x);
50         outp = subwin(stdscr, outp_c.max_y, outp_c.max_x, outp_c.min_y,outp_c.min_x);
51
52         // initalise the windows
53         touchwin(inp);
54         werase(inp);
55         wrefresh(inp);
56         scrollok(inp, TRUE);
57
58         touchwin(outp);
59         werase(outp);
60         wrefresh(outp);
61         scrollok(outp, TRUE);
62         refresh();
63
64         nodelay(inp, TRUE);
65
66         // flush the input buffers
67         flushinp();
68         
69         move(topleft.x,topleft.y);
70         DrawBox();
71 }
72
73 Viewer::~Viewer()
74 {
75         delwin(inp);
76         delwin(outp);
77         erase();
78         refresh();
79         endwin();
80 }
81
82 void Viewer::DrawBox(void)
83 {
84         int height, width;
85         COORDINATES current;
86
87         // save the current position
88         getyx(stdscr, current.y, current.x);
89
90         height = (bottomright.y - topleft.y)+1;
91         width = (bottomright.x - topleft.y)+1;
92
93         mvaddch(topleft.y-1, topleft.x-1, ACS_ULCORNER);
94         mvaddch(topleft.y-1, bottomright.x+1, ACS_URCORNER);
95         mvaddch(bottomright.y+1, bottomright.x+1, ACS_LRCORNER);
96         mvaddch(bottomright.y+1, topleft.x-1, ACS_LLCORNER);
97
98         /* wmove (screen, y, x) */
99         /* top */
100         move(topleft.y-1, topleft.x);
101         hline(ACS_HLINE, width);
102         /* bottom */
103         move(bottomright.y+1, topleft.x);
104         hline(ACS_HLINE, width);
105         move(bottomright.y+1, topleft.x);
106         hline(ACS_HLINE, width);
107         
108         /* left */
109         move(topleft.y, topleft.x-1);
110         vline(ACS_VLINE, height);
111
112         /* right */
113         move(topleft.y, bottomright.x+1);
114         vline(ACS_VLINE, height);
115
116         /* the divider */
117         mvaddch(middle_y, bottomright.x+1, ACS_RTEE);
118         mvaddch(middle_y, topleft.x-1, ACS_LTEE);
119         hline(ACS_HLINE, width);
120
121         // the window titles
122         mvaddstr(inp_c.min_y-1, middle_x-(strlen("Input")/2), "Input");
123         mvaddstr(middle_y, middle_x-(strlen("Output")/2), "Output");
124         move(current.y, current.x);
125         refresh();
126 }
127
128 void Viewer::AddStrOutWin(char *string)
129 {
130         waddstr(outp, string);
131         wrefresh(outp);
132 }
133
134 void Viewer::GetStrInWin(char *string)
135 {
136         if(wgetstr(inp, string) == ERR) {
137                 string[0] = 0;
138         } else {
139                 waddstr(inp, string);
140                 wrefresh(inp);
141         }
142 }
143
144 void Viewer::AddChOutWin(char b)
145 {
146         waddch(outp, b);
147         wrefresh(outp);
148 }
149
150 char Viewer::GetChInWin(void)
151 {
152         int b = wgetch(inp);
153
154         if(b==ERR) {
155                 b=0;
156         } else {
157                 waddch(inp, (chtype)b);
158                 wrefresh(inp);
159         }
160
161         return((char)b);
162 }