2 * Copyright (C) 2008 Diego Hernan Borghetti.
18 static struct termios otermios
; /* original terminal characteristics */
19 static struct termios ntermios
; /* charactoristics to use inside */
21 E_Term
*e_term_open(void)
27 tr
= (E_Term
*)malloc(sizeof(E_Term
));
33 tr
->name
= getenv("TERM");
35 printf("Shell variable TERM not defined!");
39 if (strcmp(tr
->name
, "vt100") && strcmp(tr
->name
, "linux") &&
40 strcmp (tr
->name
, "xterm") && strcmp(tr
->name
, "ansi")) {
41 printf("Invalid terminal type: [%s]", tr
->name
);
46 tcgetattr(0, &otermios
);
49 ntermios
.c_iflag
&= ~(IGNBRK
| BRKINT
| IGNPAR
| PARMRK
50 | INPCK
| INLCR
| IGNCR
| ICRNL
| IXON
);
52 ntermios
.c_oflag
&= ~(OPOST
| ONLCR
| OLCUC
| OCRNL
| ONOCR
| ONLRET
);
54 ntermios
.c_lflag
&= ~(ISIG
| ICANON
| XCASE
| ECHO
| ECHOE
| ECHOK
55 | ECHONL
| NOFLSH
| TOSTOP
| ECHOCTL
|
56 ECHOPRT
| ECHOKE
| FLUSHO
| PENDIN
| IEXTEN
);
58 ntermios
.c_cc
[VMIN
] = 1;
59 ntermios
.c_cc
[VTIME
] = 0;
60 tcsetattr(0, TCSADRAIN
, &ntermios
);
62 /* init default entrys. */
65 tr
->fgcolor
= E_TR_WHITE
;
66 tr
->bgcolor
= E_TR_BLACK
;
68 if (ioctl(0, TIOCGWINSZ
, &ws
)) {
84 tr
->nrow
= ws
.ws_row
-1;
91 void e_term_close(E_Term
*tr
)
93 e_term_fgcol(tr
, E_TR_WHITE
);
94 e_term_bgcol(tr
, E_TR_BLACK
);
96 tcsetattr(0, TCSADRAIN
, &otermios
);
99 void e_term_putc(int c
)
104 void e_term_flush(void)
108 status
= fflush(stdout
);
109 while (status
< 0 && errno
== EAGAIN
) {
111 status
= fflush(stdout
);
117 int e_term_getc(void)
119 return(255 & fgetc(stdin
));
122 void e_term_parm(int n
)
130 e_term_putc((r
% 10) + '0');
132 e_term_putc((q
% 10) + '0');
134 e_term_putc((n
% 10) + '0');
137 void e_term_fgcol(E_Term
*tr
, int color
)
139 if (color
!= tr
->fgcolor
) {
140 e_term_putc(E_TR_ESC
);
142 e_term_parm(color
+ 30);
148 void e_term_bgcol(E_Term
*tr
, int color
)
150 if (color
!= tr
->bgcolor
) {
151 e_term_putc(E_TR_ESC
);
153 e_term_parm(color
+ 40);
159 void e_term_move(int row
, int col
)
161 e_term_putc(E_TR_ESC
);
163 e_term_parm(row
+ 1);
165 e_term_parm(col
+ 1);
169 void e_term_eeol(void)
171 e_term_putc(E_TR_ESC
);
176 void e_term_eeop(E_Term
*tr
)
178 e_term_fgcol(tr
, tr
->fgcolor
);
179 e_term_bgcol(tr
, tr
->bgcolor
);
180 e_term_putc(E_TR_ESC
);
185 void e_term_rev(E_Term
*tr
, int state
)
189 e_term_putc(E_TR_ESC
);
191 e_term_putc(state
? '7' : '0');
199 e_term_fgcol(tr
, ftmp
);
200 e_term_bgcol(tr
, btmp
);
204 void e_term_beep ( void )
206 e_term_putc(E_TR_BEL
);