Fix wrong current column position in the status line.
[eco.git] / term.c
blobdec4b51851705c5536cb5675307a87499be880cf
1 /*
2 * Copyright (C) 2008 Diego Hernan Borghetti.
3 * Eco
4 */
6 #include <sys/ioctl.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <termios.h>
11 #include <errno.h>
12 #include <unistd.h>
14 #include "term.h"
15 #include "config.h"
18 static struct termios otermios; /* original terminal characteristics */
19 static struct termios ntermios; /* charactoristics to use inside */
21 E_Term *e_term_open(void)
23 struct winsize ws;
24 E_Term *tr;
25 char *s;
27 tr= (E_Term *)malloc(sizeof(E_Term));
28 if (!tr) {
29 perror("malloc");
30 exit(-1);
33 tr->name= getenv("TERM");
34 if (!tr->name) {
35 printf("Shell variable TERM not defined!");
36 exit(-1);
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);
42 exit(1);
46 tcgetattr(0, &otermios);
48 ntermios= 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. */
63 tr->col= 999;
64 tr->row= 999;
65 tr->fgcolor= E_TR_WHITE;
66 tr->bgcolor= E_TR_BLACK;
68 if (ioctl(0, TIOCGWINSZ, &ws)) {
69 s= getenv("LINES");
70 if (!s)
71 tr->nrow= 23;
72 else {
73 tr->nrow= atoi(s);
74 tr->nrow--;
77 s= getenv("COLUMNS");
78 if (!s)
79 tr->ncol= 80;
80 else
81 tr->ncol= atoi(s);
83 else {
84 tr->nrow= ws.ws_row-1;
85 tr->ncol= ws.ws_col;
88 return(tr);
91 void e_term_close(E_Term *tr)
93 e_term_fgcol(tr, E_TR_WHITE);
94 e_term_bgcol(tr, E_TR_BLACK);
95 e_term_eeop(tr);
96 tcsetattr(0, TCSADRAIN, &otermios);
99 void e_term_putc(int c)
101 fputc(c, stdout);
104 void e_term_flush(void)
106 int status;
108 status= fflush(stdout);
109 while (status < 0 && errno == EAGAIN) {
110 sleep(1);
111 status= fflush(stdout);
113 if (status < 0)
114 exit(15);
117 int e_term_getc(void)
119 return(255 & fgetc(stdin));
122 void e_term_parm(int n)
124 int q, r;
126 q= n / 10;
127 if (q != 0) {
128 r= q / 10;
129 if (r != 0) {
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);
141 e_term_putc('[');
142 e_term_parm(color + 30);
143 e_term_putc('m');
144 tr->fgcolor= color;
148 void e_term_bgcol(E_Term *tr, int color)
150 if (color != tr->bgcolor) {
151 e_term_putc(E_TR_ESC);
152 e_term_putc('[');
153 e_term_parm(color + 40);
154 e_term_putc('m');
155 tr->bgcolor= color;
159 void e_term_move(int row, int col)
161 e_term_putc(E_TR_ESC);
162 e_term_putc('[');
163 e_term_parm(row + 1);
164 e_term_putc(';');
165 e_term_parm(col + 1);
166 e_term_putc('H');
169 void e_term_eeol(void)
171 e_term_putc(E_TR_ESC);
172 e_term_putc('[');
173 e_term_putc('K');
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);
181 e_term_putc('[');
182 e_term_putc('J');
185 void e_term_rev(E_Term *tr, int state)
187 int ftmp, btmp;
189 e_term_putc(E_TR_ESC);
190 e_term_putc('[');
191 e_term_putc(state ? '7' : '0');
192 e_term_putc('m');
194 if (!state) {
195 ftmp = tr->fgcolor;
196 btmp = tr->bgcolor;
197 tr->fgcolor = -1;
198 tr->bgcolor = -1;
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);
207 e_term_flush();