2 #include "roteprivate.h"
8 RoteTerm *rote_vt_create(int rows, int cols) {
12 if (rows <= 0 || cols <= 0) return NULL;
14 if (! (rt = (RoteTerm*) malloc(sizeof(RoteTerm))) ) return NULL;
15 memset(rt, 0, sizeof(RoteTerm));
17 /* record dimensions */
21 /* create the cell matrix */
22 rt->cells = (RoteCell**) malloc(sizeof(RoteCell*) * rt->rows);
23 for (i = 0; i < rt->rows; i++) {
25 rt->cells[i] = (RoteCell*) malloc(sizeof(RoteCell) * rt->cols);
27 /* fill row with spaces */
28 for (j = 0; j < rt->cols; j++) {
29 rt->cells[i][j].ch = 0x20; /* a space */
30 rt->cells[i][j].attr = 0x70; /* white text, black background */
34 /* allocate dirtiness array */
35 rt->line_dirty = (bool*) malloc(sizeof(bool) * rt->rows);
37 /* initialization of other public fields */
38 rt->crow = rt->ccol = 0;
39 rt->curattr = 0x70; /* white text over black background */
41 /* allocate private data */
42 rt->pd = (RoteTermPrivate*) malloc(sizeof(RoteTermPrivate));
43 memset(rt->pd, 0, sizeof(RoteTermPrivate));
45 rt->pd->pty = -1; /* no pty for now */
47 /* initial scrolling area is the whole window */
48 rt->pd->scrolltop = 0;
49 rt->pd->scrollbottom = rt->rows - 1;
52 fprintf(stderr, "Created a %d x %d terminal.\n", rt->rows, rt->cols);
58 void rote_vt_destroy(RoteTerm *rt) {
64 for (i = 0; i < rt->rows; i++) free(rt->cells[i]);
69 static void default_cur_set_attr(WINDOW *win, unsigned char attr) {
70 int cp = ROTE_ATTR_BG(attr) * 8 + 7 - ROTE_ATTR_FG(attr);
71 if (!cp) wattrset(win, A_NORMAL);
72 else wattrset(win, COLOR_PAIR(cp));
74 if (ROTE_ATTR_BOLD(attr)) wattron(win, A_BOLD);
75 if (ROTE_ATTR_BLINK(attr)) wattron(win, A_BLINK);
78 static inline unsigned char ensure_printable(unsigned char ch)
79 { return ch >= 32 ? ch : 32; }
81 void rote_vt_draw(RoteTerm *rt, WINDOW *win, int srow, int scol,
82 void (*cur_set_attr)(WINDOW*,unsigned char)) {
87 if (!cur_set_attr) cur_set_attr = default_cur_set_attr;
88 for (i = 0; i < rt->rows; i++) {
89 wmove(win, srow + i, scol);
90 for (j = 0; j < rt->cols; j++) {
91 (*cur_set_attr)(win, rt->cells[i][j].attr);
92 waddch(win, ensure_printable(rt->cells[i][j].ch));
96 wmove(win, srow + rt->crow, scol + rt->ccol);
99 pid_t rote_vt_forkpty(RoteTerm *rt, const char *command) {
103 ws.ws_row = rt->rows;
104 ws.ws_col = rt->cols;
105 ws.ws_xpixel = ws.ws_ypixel = 0;
107 childpid = forkpty(&rt->pd->pty, NULL, NULL, &ws);
108 if (childpid < 0) return -1;
111 /* we are the child, running under the slave side of the pty. */
113 /* Cajole application into using linux-console-compatible escape
114 * sequences (which is what we are prepared to interpret) */
115 setenv("TERM", "linux", 1);
117 /* Now we will exec /bin/sh -c command. */
118 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
120 fprintf(stderr, "\nexecl() failed.\nCommand: '%s'\n", command);
121 exit(127); /* error exec'ing */
124 /* if we got here we are the parent process */
125 rt->childpid = childpid;
129 void rote_vt_forsake_child(RoteTerm *rt) {
130 if (rt->pd->pty >= 0) close(rt->pd->pty);
135 void rote_vt_update(RoteTerm *rt) {
137 struct timeval tvzero;
140 if (rt->pd->pty < 0) return; /* nothing to pump */
143 /* check if pty has something to say */
144 FD_ZERO(&ifs); FD_SET(rt->pd->pty, &ifs);
145 tvzero.tv_sec = 0; tvzero.tv_usec = 0;
147 if (select(rt->pd->pty + 1, &ifs, NULL, NULL, &tvzero) <= 0)
148 return; /* nothing to read, or select() failed */
150 /* read what we can. This is guaranteed not to block, since
151 * select() told us there was something to read. */
152 bytesread = read(rt->pd->pty, buf, 512);
153 if (bytesread <= 0) return;
155 /* inject the data into the terminal */
156 rote_vt_inject(rt, buf, bytesread);
160 void rote_vt_write(RoteTerm *rt, const char *data, int len) {
161 if (rt->pd->pty < 0) {
162 /* no pty, so just inject the data plain and simple */
163 rote_vt_inject(rt, data, len);
167 /* write data to pty. Keep calling write() until we have written
170 int byteswritten = write(rt->pd->pty, data, len);
171 if (byteswritten < 0) {
172 /* very ugly way to inform the error. Improvements welcome! */
173 static char errormsg[] = "\n(ROTE: pty write() error)\n";
174 rote_vt_inject(rt, errormsg, strlen(errormsg));
178 data += byteswritten;
183 void rote_vt_install_handler(RoteTerm *rt, rote_es_handler_t handler) {
184 rt->pd->handler = handler;
187 void *rote_vt_take_snapshot(RoteTerm *rt) {
189 int bytes_per_row = sizeof(RoteCell) * rt->cols;
190 void *buf = malloc(bytes_per_row * rt->rows);
193 for (i = 0; i < rt->rows; i++, ptr += bytes_per_row)
194 memcpy(ptr, rt->cells[i], bytes_per_row);
199 void rote_vt_restore_snapshot(RoteTerm *rt, void *snapbuf) {
201 int bytes_per_row = sizeof(RoteCell) * rt->cols;
203 for (i = 0; i < rt->rows; i++, snapbuf += bytes_per_row) {
204 rt->line_dirty[i] = true;
205 memcpy(rt->cells[i], snapbuf, bytes_per_row);