File: frontend.cc

package info (click to toggle)
sdcc 3.8.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 99,212 kB
  • sloc: ansic: 918,594; cpp: 69,526; makefile: 56,790; sh: 29,616; asm: 12,364; perl: 12,136; yacc: 7,179; lisp: 1,672; python: 812; lex: 773; awk: 495; sed: 89
file content (162 lines) | stat: -rw-r--r-- 3,509 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/******************************************************************************
 * to emulate the serial input and output of an 8051 controller               *
 * frontend.cc - the ncurses frontend                                         *
 ******************************************************************************/
#include <sys/types.h>
#include <iostream>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <curses.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include "frontend.hh"

Viewer::Viewer()
{
	/* initalise the output screen */
	initscr();
	cbreak();
	noecho();
	nl();
	intrflush(stdscr,FALSE);
	keypad(stdscr, TRUE);
	
	/* clear the screen and off you go */
	refresh();

	// get the coordinates for the box
	/* create the subwindow */
	win_c.min_x = win_c.min_y = 0;
	getmaxyx(stdscr, win_c.max_y, win_c.max_x);

	/* define the boxed size */
	topleft.x = win_c.min_x + 1;
	bottomright.x = win_c.max_x - 2;
	topleft.y = win_c.min_y + 1;
	bottomright.y = win_c.max_y - 2;
	middle_y = (int)((bottomright.y-topleft.y)/2)+1;
	middle_x = (int)((bottomright.x-topleft.x)/2)+1;

	// draw the two subwindows
	inp_c.min_x = outp_c.min_x = topleft.x;
	inp_c.max_x = outp_c.max_x = bottomright.x;
	inp_c.min_y = topleft.y;
	inp_c.max_y = middle_y-topleft.y;
	outp_c.min_y = middle_y+1;
	outp_c.max_y = bottomright.y-middle_y;
	inp = subwin(stdscr, inp_c.max_y, inp_c.max_x, inp_c.min_y, inp_c.min_x);
	outp = subwin(stdscr, outp_c.max_y, outp_c.max_x, outp_c.min_y,outp_c.min_x);

	// initalise the windows
	touchwin(inp);
	werase(inp);
	wrefresh(inp);
	scrollok(inp, TRUE);

	touchwin(outp);
	werase(outp);
	wrefresh(outp);
	scrollok(outp, TRUE);
	refresh();

	nodelay(inp, TRUE);

	// flush the input buffers
	flushinp();
	
	move(topleft.x,topleft.y);
	DrawBox();
}

Viewer::~Viewer()
{
	delwin(inp);
	delwin(outp);
	erase();
	refresh();
	endwin();
}

void Viewer::DrawBox(void)
{
	int height, width;
	COORDINATES current;

	// save the current position
	getyx(stdscr, current.y, current.x);

	height = (bottomright.y - topleft.y)+1;
	width = (bottomright.x - topleft.y)+1;

	mvaddch(topleft.y-1, topleft.x-1, ACS_ULCORNER);
	mvaddch(topleft.y-1, bottomright.x+1, ACS_URCORNER);
	mvaddch(bottomright.y+1, bottomright.x+1, ACS_LRCORNER);
	mvaddch(bottomright.y+1, topleft.x-1, ACS_LLCORNER);

	/* wmove (screen, y, x) */
	/* top */
	move(topleft.y-1, topleft.x);
	hline(ACS_HLINE, width);
	/* bottom */
	move(bottomright.y+1, topleft.x);
	hline(ACS_HLINE, width);
	move(bottomright.y+1, topleft.x);
	hline(ACS_HLINE, width);
	
	/* left */
	move(topleft.y, topleft.x-1);
	vline(ACS_VLINE, height);

	/* right */
	move(topleft.y, bottomright.x+1);
	vline(ACS_VLINE, height);

	/* the divider */
	mvaddch(middle_y, bottomright.x+1, ACS_RTEE);
	mvaddch(middle_y, topleft.x-1, ACS_LTEE);
	hline(ACS_HLINE, width);

	// the window titles
	mvaddstr(inp_c.min_y-1, middle_x-(strlen("Input")/2), "Input");
	mvaddstr(middle_y, middle_x-(strlen("Output")/2), "Output");
	move(current.y, current.x);
	refresh();
}

void Viewer::AddStrOutWin(char *string)
{
	waddstr(outp, string);
	wrefresh(outp);
}

void Viewer::GetStrInWin(char *string)
{
	if(wgetstr(inp, string) == ERR) {
		string[0] = 0;
	} else {
		waddstr(inp, string);
		wrefresh(inp);
	}
}

void Viewer::AddChOutWin(char b)
{
	waddch(outp, b);
	wrefresh(outp);
}

char Viewer::GetChInWin(void)
{
	int b = wgetch(inp);

	if(b==ERR) {
		b=0;
	} else {
		waddch(inp, (chtype)b);
		wrefresh(inp);
	}

	return((char)b);
}