diff options
| author | Anselm R Garbe <garbeam@gmail.com> | 2009-05-10 13:17:09 +0100 |
|---|---|---|
| committer | Anselm R Garbe <garbeam@gmail.com> | 2009-05-10 13:17:09 +0100 |
| commit | d58dd3b8bc42ed31232e4145696d7dacb117a31c (patch) | |
| tree | dfe3362a600e9491a687cd765158b70d3328333d /st.c | |
| parent | add accidentally omitted word (diff) | |
| download | st-d58dd3b8bc42ed31232e4145696d7dacb117a31c.tar.gz st-d58dd3b8bc42ed31232e4145696d7dacb117a31c.zip | |
backport of local changes
Diffstat (limited to '')
| -rw-r--r-- | st.c | 926 |
1 files changed, 915 insertions, 11 deletions
| @@ -1,17 +1,921 @@ | |||
| 1 | /* See LICENSE file for copyright and license details. */ | 1 | /* See LICENSE for licence details. */ |
| 2 | #include <stdio.h> | 2 | #include "st.h" |
| 3 | #include <stdlib.h> | 3 | |
| 4 | #include <string.h> | 4 | /* Globals */ |
| 5 | DC dc; | ||
| 6 | XWindow xw; | ||
| 7 | Term term; | ||
| 8 | Escseq escseq; | ||
| 9 | int cmdfd; | ||
| 10 | int running; | ||
| 11 | |||
| 12 | void | ||
| 13 | die(const char *errstr, ...) { | ||
| 14 | va_list ap; | ||
| 15 | |||
| 16 | va_start(ap, errstr); | ||
| 17 | vfprintf(stderr, errstr, ap); | ||
| 18 | va_end(ap); | ||
| 19 | exit(EXIT_FAILURE); | ||
| 20 | } | ||
| 21 | |||
| 22 | void | ||
| 23 | execsh(void) { | ||
| 24 | char *args[3] = {SHELL, "-i", NULL}; | ||
| 25 | putenv("TERM=" TNAME); | ||
| 26 | execvp(SHELL, args); | ||
| 27 | } | ||
| 28 | |||
| 29 | void | ||
| 30 | xbell(void) { /* visual bell */ | ||
| 31 | XRectangle r = { 0, 0, xw.w, xw.h }; | ||
| 32 | XSetForeground(xw.dis, dc.gc, dc.col[BellCol]); | ||
| 33 | XFillRectangles(xw.dis, xw.win, dc.gc, &r, 1); | ||
| 34 | XFlush(xw.dis); | ||
| 35 | usleep(30000); | ||
| 36 | draw(SCredraw); | ||
| 37 | } | ||
| 38 | |||
| 39 | void | ||
| 40 | ttynew(void) { | ||
| 41 | int m, s; | ||
| 42 | pid_t pid; | ||
| 43 | char *pts; | ||
| 44 | |||
| 45 | if((m = posix_openpt(O_RDWR | O_NOCTTY)) < 0) | ||
| 46 | die("openpt"); | ||
| 47 | if(grantpt(m) == -1) | ||
| 48 | die("grandpt"); | ||
| 49 | if(unlockpt(m) == -1) | ||
| 50 | die("unlockpt"); | ||
| 51 | if((pts = ptsname(m)) == NULL) | ||
| 52 | die("ptsname"); | ||
| 53 | if((s = open(pts, O_RDWR | O_NOCTTY)) < 0) | ||
| 54 | die("slave open"); | ||
| 55 | fcntl(s, F_SETFL, O_NDELAY); | ||
| 56 | switch(pid = fork()) { | ||
| 57 | case -1: | ||
| 58 | die("fork"); | ||
| 59 | break; | ||
| 60 | case 0: | ||
| 61 | setsid(); /* create a new process group */ | ||
| 62 | dup2(s, STDIN_FILENO); | ||
| 63 | dup2(s, STDOUT_FILENO); | ||
| 64 | dup2(s, STDERR_FILENO); | ||
| 65 | if(ioctl(s, TIOCSCTTY, NULL) < 0) | ||
| 66 | die("slave TTIOCSTTY"); | ||
| 67 | execsh(); | ||
| 68 | break; | ||
| 69 | default: | ||
| 70 | close(s); | ||
| 71 | cmdfd = m; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | void | ||
| 76 | dump(char c) { | ||
| 77 | static int col; | ||
| 78 | fprintf(stderr, " %02x %c ", c, isprint(c)?c:'.'); | ||
| 79 | if(++col % 10 == 0) | ||
| 80 | fprintf(stderr, "\n"); | ||
| 81 | } | ||
| 82 | |||
| 83 | void | ||
| 84 | ttyread(void) { | ||
| 85 | char buf[BUFSIZ] = {0}; | ||
| 86 | int ret; | ||
| 87 | |||
| 88 | switch(ret = read(cmdfd, buf, BUFSIZ)) { | ||
| 89 | case -1: /* error or exit */ | ||
| 90 | /* XXX: be more precise */ | ||
| 91 | running = 0; | ||
| 92 | break; | ||
| 93 | default: | ||
| 94 | tputs(buf, ret); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | void | ||
| 99 | ttywrite(char *s, size_t n) { | ||
| 100 | if(write(cmdfd, s, n) == -1) | ||
| 101 | die("write error on tty."); | ||
| 102 | } | ||
| 103 | |||
| 104 | void | ||
| 105 | ttyresize(int x, int y) { | ||
| 106 | struct winsize w; | ||
| 107 | |||
| 108 | w.ws_row = term.row; | ||
| 109 | w.ws_col = term.col; | ||
| 110 | w.ws_xpixel = w.ws_ypixel = 0; | ||
| 111 | if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0) | ||
| 112 | fprintf(stderr, "Couldn't set window size: %m\n"); | ||
| 113 | } | ||
| 5 | 114 | ||
| 6 | int | 115 | int |
| 7 | main(int argc, char *argv[]) { | 116 | escfinal(char c) { |
| 8 | if(argc == 2 && !strcmp("-v", argv[1])) { | 117 | if(escseq.len == 1) |
| 9 | fprintf(stderr, "st-"VERSION", © 2007-2008 st engineers, see LICENSE for details\n"); | 118 | switch(c) { |
| 10 | exit(EXIT_SUCCESS); | 119 | case '[': |
| 120 | case ']': | ||
| 121 | case '(': | ||
| 122 | return 0; | ||
| 123 | case '=': | ||
| 124 | case '>': | ||
| 125 | default: | ||
| 126 | return 1; | ||
| 127 | } | ||
| 128 | else if(BETWEEN(c, 0x40, 0x7E)) | ||
| 129 | return 1; | ||
| 130 | return 0; | ||
| 131 | } | ||
| 132 | |||
| 133 | void | ||
| 134 | tcpos(int mode) { | ||
| 135 | static int x = 0; | ||
| 136 | static int y = 0; | ||
| 137 | |||
| 138 | if(mode == CSsave) | ||
| 139 | x = term.c.x, y = term.c.y; | ||
| 140 | else if(mode == CSload) | ||
| 141 | tmoveto(x, y); | ||
| 142 | } | ||
| 143 | |||
| 144 | void | ||
| 145 | tnew(int col, int row) { /* screen size */ | ||
| 146 | term.row = row, term.col = col; | ||
| 147 | term.top = 0, term.bot = term.row - 1; | ||
| 148 | /* mode */ | ||
| 149 | term.mode = TMwrap; | ||
| 150 | /* cursor */ | ||
| 151 | term.c.attr.mode = ATnone; | ||
| 152 | term.c.attr.fg = DefaultFG; | ||
| 153 | term.c.attr.bg = DefaultBG; | ||
| 154 | term.c.x = term.c.y = 0; | ||
| 155 | term.c.hidden = 0; | ||
| 156 | /* allocate screen */ | ||
| 157 | term.line = calloc(term.row, sizeof(Line)); | ||
| 158 | for(row = 0 ; row < term.row; row++) | ||
| 159 | term.line[row] = calloc(term.col, sizeof(Glyph)); | ||
| 160 | } | ||
| 161 | |||
| 162 | void | ||
| 163 | tscroll(void) { | ||
| 164 | Line temp = term.line[term.top]; | ||
| 165 | int i; | ||
| 166 | |||
| 167 | for(i = term.top; i < term.bot; i++) | ||
| 168 | term.line[i] = term.line[i+1]; | ||
| 169 | memset(temp, 0, sizeof(Glyph) * term.col); | ||
| 170 | term.line[term.bot] = temp; | ||
| 171 | xscroll(); | ||
| 172 | } | ||
| 173 | |||
| 174 | void | ||
| 175 | tnewline(void) { | ||
| 176 | int y = term.c.y + 1; | ||
| 177 | |||
| 178 | if(y > term.bot) { | ||
| 179 | tscroll(), y = term.bot; | ||
| 11 | } | 180 | } |
| 12 | else if(argc != 1) { | 181 | tmoveto(0, y); |
| 13 | fprintf(stderr, "usage: st [-v]\n"); | 182 | } |
| 14 | exit(EXIT_FAILURE); | 183 | |
| 184 | int | ||
| 185 | escaddc(char c) { | ||
| 186 | escseq.buf[escseq.len++] = c; | ||
| 187 | if(escfinal(c) || escseq.len >= ESCSIZ) { | ||
| 188 | escparse(), eschandle(); | ||
| 189 | return 0; | ||
| 15 | } | 190 | } |
| 191 | return 1; | ||
| 192 | } | ||
| 193 | |||
| 194 | void | ||
| 195 | escparse(void) { | ||
| 196 | /* int noarg = 1; */ | ||
| 197 | char *p = escseq.buf; | ||
| 198 | |||
| 199 | escseq.narg = 0; | ||
| 200 | switch(escseq.pre = *p++) { | ||
| 201 | case '[': /* CSI */ | ||
| 202 | if(*p == '?') | ||
| 203 | escseq.priv = 1, p++; | ||
| 204 | |||
| 205 | while(p < escseq.buf+escseq.len) { | ||
| 206 | while(isdigit(*p)) { | ||
| 207 | escseq.arg[escseq.narg] *= 10; | ||
| 208 | escseq.arg[escseq.narg] += *(p++) - '0'/*, noarg = 0 */; | ||
| 209 | } | ||
| 210 | if(*p == ';') | ||
| 211 | escseq.narg++, p++; | ||
| 212 | else { | ||
| 213 | escseq.mode = *p; | ||
| 214 | escseq.narg++; | ||
| 215 | return; | ||
| 216 | } | ||
| 217 | } | ||
| 218 | break; | ||
| 219 | case '(': | ||
| 220 | /* humf charset stuff */ | ||
| 221 | break; | ||
| 222 | } | ||
| 223 | } | ||
| 224 | |||
| 225 | void | ||
| 226 | tmoveto(int x, int y) { | ||
| 227 | term.c.x = x < 0 ? 0 : x >= term.col ? term.col-1 : x; | ||
| 228 | term.c.y = y < 0 ? 0 : y >= term.row ? term.row-1 : y; | ||
| 229 | } | ||
| 230 | |||
| 231 | void | ||
| 232 | tcursor(int dir) { | ||
| 233 | int xi = term.c.x, yi = term.c.y; | ||
| 234 | int xf = xi, yf = yi; | ||
| 235 | |||
| 236 | switch(dir) { | ||
| 237 | case CSup: | ||
| 238 | yf--; | ||
| 239 | break; | ||
| 240 | case CSdown: | ||
| 241 | yf++; | ||
| 242 | break; | ||
| 243 | case CSleft: | ||
| 244 | xf--; | ||
| 245 | if(xf < 0) { | ||
| 246 | xf = term.col-1, yf--; | ||
| 247 | if(yf < term.top) | ||
| 248 | yf = term.top, xf = 0; | ||
| 249 | } | ||
| 250 | break; | ||
| 251 | case CSright: | ||
| 252 | xf++; | ||
| 253 | if(xf >= term.col) { | ||
| 254 | xf = 0, yf++; | ||
| 255 | if(yf > term.bot) | ||
| 256 | yf = term.bot, tscroll(); | ||
| 257 | } | ||
| 258 | break; | ||
| 259 | } | ||
| 260 | tmoveto(xf, yf); | ||
| 261 | } | ||
| 262 | |||
| 263 | void | ||
| 264 | tsetchar(char c) { | ||
| 265 | term.line[term.c.y][term.c.x] = term.c.attr; | ||
| 266 | term.line[term.c.y][term.c.x].c = c; | ||
| 267 | term.line[term.c.y][term.c.x].state |= CRset | CRupdate; | ||
| 268 | } | ||
| 269 | |||
| 270 | void | ||
| 271 | tclearregion(int x1, int y1, int x2, int y2) { | ||
| 272 | int x, y; | ||
| 273 | |||
| 274 | LIMIT(x1, 0, term.col-1); | ||
| 275 | LIMIT(x2, 0, term.col-1); | ||
| 276 | LIMIT(y1, 0, term.row-1); | ||
| 277 | LIMIT(y2, 0, term.row-1); | ||
| 278 | |||
| 279 | /* XXX: could be optimized */ | ||
| 280 | for(x = x1; x <= x2; x++) | ||
| 281 | for(y = y1; y <= y2; y++) | ||
| 282 | memset(&term.line[y][x], 0, sizeof(Glyph)); | ||
| 283 | |||
| 284 | xclear(x1, y1, x2, y2); | ||
| 285 | } | ||
| 286 | |||
| 287 | void | ||
| 288 | tdeletechar(int n) { | ||
| 289 | int src = term.c.x + n; | ||
| 290 | int dst = term.c.x; | ||
| 291 | int size = term.col - src; | ||
| 292 | |||
| 293 | if(src >= term.col) { | ||
| 294 | tclearregion(term.c.x, term.c.y, term.col-1, term.c.y); | ||
| 295 | return; | ||
| 296 | } | ||
| 297 | memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph)); | ||
| 298 | tclearregion(term.col-size, term.c.y, term.col-1, term.c.y); | ||
| 299 | } | ||
| 300 | |||
| 301 | void | ||
| 302 | tinsertblank(int n) { | ||
| 303 | int src = term.c.x; | ||
| 304 | int dst = src + n; | ||
| 305 | int size = term.col - n - src; | ||
| 306 | |||
| 307 | if(dst >= term.col) { | ||
| 308 | tclearregion(term.c.x, term.c.y, term.col-1, term.c.y); | ||
| 309 | return; | ||
| 310 | } | ||
| 311 | memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src], size * sizeof(Glyph)); | ||
| 312 | tclearregion(src, term.c.y, dst, term.c.y); | ||
| 313 | } | ||
| 314 | |||
| 315 | void | ||
| 316 | tinsertblankline (int n) { | ||
| 317 | int i; | ||
| 318 | Line blank; | ||
| 319 | int bot = term.bot; | ||
| 320 | |||
| 321 | if(term.c.y > term.bot) | ||
| 322 | bot = term.row - 1; | ||
| 323 | else if(term.c.y < term.top) | ||
| 324 | bot = term.top - 1; | ||
| 325 | if(term.c.y + n >= bot) { | ||
| 326 | tclearregion(0, term.c.y, term.col-1, bot); | ||
| 327 | return; | ||
| 328 | } | ||
| 329 | for(i = bot; i >= term.c.y+n; i--) { | ||
| 330 | /* swap deleted line <-> blanked line */ | ||
| 331 | blank = term.line[i]; | ||
| 332 | term.line[i] = term.line[i-n]; | ||
| 333 | term.line[i-n] = blank; | ||
| 334 | /* blank it */ | ||
| 335 | memset(blank, 0, term.col * sizeof(Glyph)); | ||
| 336 | } | ||
| 337 | } | ||
| 338 | |||
| 339 | |||
| 340 | void | ||
| 341 | tdeleteline(int n) { | ||
| 342 | int i; | ||
| 343 | Line blank; | ||
| 344 | int bot = term.bot; | ||
| 345 | |||
| 346 | if(term.c.y > term.bot) | ||
| 347 | bot = term.row - 1; | ||
| 348 | else if(term.c.y < term.top) | ||
| 349 | bot = term.top - 1; | ||
| 350 | if(term.c.y + n >= bot) { | ||
| 351 | tclearregion(0, term.c.y, term.col-1, bot); | ||
| 352 | return; | ||
| 353 | } | ||
| 354 | for(i = term.c.y; i <= bot-n; i++) { | ||
| 355 | /* swap deleted line <-> blanked line */ | ||
| 356 | blank = term.line[i]; | ||
| 357 | term.line[i] = term.line[i+n]; | ||
| 358 | term.line[i+n] = blank; | ||
| 359 | /* blank it */ | ||
| 360 | memset(blank, 0, term.col * sizeof(Glyph)); | ||
| 361 | } | ||
| 362 | } | ||
| 363 | |||
| 364 | void | ||
| 365 | tsetattr(int *attr, int l) { | ||
| 366 | int i; | ||
| 367 | |||
| 368 | #ifdef TRUECOLOR /* ESC [ ? <fg/bg> ; <r> ; <g> ; <b> m */ | ||
| 369 | Color col; | ||
| 370 | if(escseq.priv && escseq.len == 4) { /* True color extension :) */ | ||
| 371 | col = (escseq.arg[1]<<16) + (escseq.arg[2]<<8) + escseq.arg[3]; | ||
| 372 | switch(escseq.arg[0]) { | ||
| 373 | case 3: /* foreground */ | ||
| 374 | term.c.attr.fg = col; | ||
| 375 | break; | ||
| 376 | case 4: /* background */ | ||
| 377 | term.c.attr.bg = col; | ||
| 378 | break; | ||
| 379 | } | ||
| 380 | } | ||
| 381 | else | ||
| 382 | #endif | ||
| 383 | for(i = 0; i < l; i++) { | ||
| 384 | switch(attr[i]) { | ||
| 385 | case 0: | ||
| 386 | memset(&term.c.attr, 0, sizeof(term.c.attr)); | ||
| 387 | term.c.attr.fg = DefaultFG; | ||
| 388 | term.c.attr.bg = DefaultBG; | ||
| 389 | break; | ||
| 390 | case 1: | ||
| 391 | term.c.attr.mode |= ATbold; | ||
| 392 | break; | ||
| 393 | case 4: | ||
| 394 | term.c.attr.mode |= ATunderline; | ||
| 395 | break; | ||
| 396 | case 7: | ||
| 397 | term.c.attr.mode |= ATreverse; | ||
| 398 | break; | ||
| 399 | case 8: | ||
| 400 | term.c.hidden = CShide; | ||
| 401 | break; | ||
| 402 | case 22: | ||
| 403 | term.c.attr.mode &= ~ATbold; | ||
| 404 | break; | ||
| 405 | case 24: | ||
| 406 | term.c.attr.mode &= ~ATunderline; | ||
| 407 | break; | ||
| 408 | case 27: | ||
| 409 | term.c.attr.mode &= ~ATreverse; | ||
| 410 | break; | ||
| 411 | case 39: | ||
| 412 | term.c.attr.fg = DefaultFG; | ||
| 413 | break; | ||
| 414 | case 49: | ||
| 415 | term.c.attr.fg = DefaultBG; | ||
| 416 | break; | ||
| 417 | default: | ||
| 418 | if(BETWEEN(attr[i], 30, 37)) | ||
| 419 | term.c.attr.fg = attr[i] - 30; | ||
| 420 | else if(BETWEEN(attr[i], 40, 47)) | ||
| 421 | term.c.attr.bg = attr[i] - 40; | ||
| 422 | break; | ||
| 423 | } | ||
| 424 | } | ||
| 425 | } | ||
| 426 | |||
| 427 | void | ||
| 428 | tsetscroll(int t, int b) { | ||
| 429 | int temp; | ||
| 430 | |||
| 431 | LIMIT(t, 0, term.row-1); | ||
| 432 | LIMIT(b, 0, term.row-1); | ||
| 433 | if(t > b) { | ||
| 434 | temp = t; | ||
| 435 | t = b; | ||
| 436 | b = temp; | ||
| 437 | } | ||
| 438 | term.top = t; | ||
| 439 | term.bot = b; | ||
| 440 | } | ||
| 441 | |||
| 442 | |||
| 443 | void | ||
| 444 | eschandle(void) { | ||
| 445 | /* escdump(); */ | ||
| 446 | switch(escseq.pre) { | ||
| 447 | case '[': | ||
| 448 | switch(escseq.mode) { | ||
| 449 | case '@': /* Insert <n> blank char */ | ||
| 450 | DEFAULT(escseq.arg[0], 1); | ||
| 451 | tinsertblank(escseq.arg[0]); | ||
| 452 | break; | ||
| 453 | case 'A': /* Cursor <n> Up */ | ||
| 454 | case 'e': | ||
| 455 | DEFAULT(escseq.arg[0], 1); | ||
| 456 | tmoveto(term.c.x, term.c.y-escseq.arg[0]); | ||
| 457 | break; | ||
| 458 | case 'B': /* Cursor <n> Down */ | ||
| 459 | DEFAULT(escseq.arg[0], 1); | ||
| 460 | tmoveto(term.c.x, term.c.y+escseq.arg[0]); | ||
| 461 | break; | ||
| 462 | case 'C': /* Cursor <n> Forward */ | ||
| 463 | case 'a': | ||
| 464 | DEFAULT(escseq.arg[0], 1); | ||
| 465 | tmoveto(term.c.x+escseq.arg[0], term.c.y); | ||
| 466 | break; | ||
| 467 | case 'D': /* Cursor <n> Backward */ | ||
| 468 | DEFAULT(escseq.arg[0], 1); | ||
| 469 | tmoveto(term.c.x-escseq.arg[0], term.c.y); | ||
| 470 | break; | ||
| 471 | case 'E': /* Cursor <n> Down and first col */ | ||
| 472 | DEFAULT(escseq.arg[0], 1); | ||
| 473 | tmoveto(0, term.c.y+escseq.arg[0]); | ||
| 474 | break; | ||
| 475 | case 'F': /* Cursor <n> Up and first col */ | ||
| 476 | DEFAULT(escseq.arg[0], 1); | ||
| 477 | tmoveto(0, term.c.y-escseq.arg[0]); | ||
| 478 | break; | ||
| 479 | case 'G': /* Move to <col> */ | ||
| 480 | case '`': | ||
| 481 | DEFAULT(escseq.arg[0], 1); | ||
| 482 | tmoveto(escseq.arg[0]-1, term.c.y); | ||
| 483 | break; | ||
| 484 | case 'H': /* Move to <row> <col> */ | ||
| 485 | case 'f': | ||
| 486 | DEFAULT(escseq.arg[0], 1); | ||
| 487 | DEFAULT(escseq.arg[1], 1); | ||
| 488 | tmoveto(escseq.arg[1]-1, escseq.arg[0]-1); | ||
| 489 | break; | ||
| 490 | case 'J': /* Clear screen */ | ||
| 491 | switch(escseq.arg[0]) { | ||
| 492 | case 0: /* below */ | ||
| 493 | tclearregion(term.c.x, term.c.y, term.col-1, term.row-1); | ||
| 494 | break; | ||
| 495 | case 1: /* above */ | ||
| 496 | tclearregion(0, 0, term.c.x, term.c.y); | ||
| 497 | break; | ||
| 498 | case 2: /* all */ | ||
| 499 | tclearregion(0, 0, term.col-1, term.row-1); | ||
| 500 | break; | ||
| 501 | } | ||
| 502 | break; | ||
| 503 | case 'K': /* Clear line */ | ||
| 504 | switch(escseq.arg[0]) { | ||
| 505 | case 0: /* right */ | ||
| 506 | tclearregion(term.c.x, term.c.y, term.col-1, term.c.y); | ||
| 507 | break; | ||
| 508 | case 1: /* left */ | ||
| 509 | tclearregion(0, term.c.y, term.c.x, term.c.y); | ||
| 510 | break; | ||
| 511 | case 2: /* all */ | ||
| 512 | tclearregion(0, term.c.y, term.col-1, term.c.y); | ||
| 513 | break; | ||
| 514 | } | ||
| 515 | break; | ||
| 516 | case 'L': /* Insert <n> blank lines */ | ||
| 517 | DEFAULT(escseq.arg[0], 1); | ||
| 518 | tinsertblankline(escseq.arg[0]); | ||
| 519 | break; | ||
| 520 | case 'M': /* Delete <n> lines */ | ||
| 521 | DEFAULT(escseq.arg[0], 1); | ||
| 522 | tdeleteline(escseq.arg[0]); | ||
| 523 | break; | ||
| 524 | case 'P': /* Delete <n> char */ | ||
| 525 | DEFAULT(escseq.arg[0], 1); | ||
| 526 | tdeletechar(escseq.arg[0]); | ||
| 527 | break; | ||
| 528 | case 'd': /* Move to <row> */ | ||
| 529 | DEFAULT(escseq.arg[0], 1); | ||
| 530 | tmoveto(term.c.x, escseq.arg[0]-1); | ||
| 531 | break; | ||
| 532 | case 'h': /* Set terminal mode */ | ||
| 533 | break; | ||
| 534 | case 'm': /* Terminal attribute (color) */ | ||
| 535 | tsetattr(escseq.arg, escseq.narg); | ||
| 536 | break; | ||
| 537 | case 'r': | ||
| 538 | if(escseq.priv) | ||
| 539 | ; | ||
| 540 | else { | ||
| 541 | DEFAULT(escseq.arg[0], 1); | ||
| 542 | DEFAULT(escseq.arg[1], term.row); | ||
| 543 | tsetscroll(escseq.arg[0]-1, escseq.arg[1]-1); | ||
| 544 | } | ||
| 545 | break; | ||
| 546 | case 's': /* Save cursor position */ | ||
| 547 | tcpos(CSsave); | ||
| 548 | break; | ||
| 549 | case 'u': /* Load cursor position */ | ||
| 550 | tcpos(CSload); | ||
| 551 | break; | ||
| 552 | } | ||
| 553 | break; | ||
| 554 | } | ||
| 555 | } | ||
| 556 | |||
| 557 | void | ||
| 558 | escdump(void) { | ||
| 559 | int i; | ||
| 560 | puts("------"); | ||
| 561 | printf("rawbuf : %s\n", escseq.buf); | ||
| 562 | printf("prechar : %c\n", escseq.pre); | ||
| 563 | printf("private : %c\n", escseq.priv ? '?' : ' '); | ||
| 564 | printf("narg : %d\n", escseq.narg); | ||
| 565 | if(escseq.narg) { | ||
| 566 | for(i = 0; i < escseq.narg; i++) | ||
| 567 | printf("\targ %d = %d\n", i, escseq.arg[i]); | ||
| 568 | } | ||
| 569 | printf("mode : %c\n", escseq.mode); | ||
| 570 | } | ||
| 571 | |||
| 572 | void | ||
| 573 | escreset(void) { | ||
| 574 | memset(&escseq, 0, sizeof(escseq)); | ||
| 575 | } | ||
| 576 | |||
| 577 | void | ||
| 578 | tputc(char c) { | ||
| 579 | static int inesc = 0; | ||
| 580 | |||
| 581 | dump(c); | ||
| 582 | /* start of escseq */ | ||
| 583 | if(c == '\033') | ||
| 584 | escreset(), inesc = 1; | ||
| 585 | else if(inesc) { | ||
| 586 | inesc = escaddc(c); | ||
| 587 | } /* normal char */ | ||
| 588 | else switch(c) { | ||
| 589 | default: | ||
| 590 | tsetchar(c); | ||
| 591 | tcursor(CSright); | ||
| 592 | break; | ||
| 593 | case '\b': | ||
| 594 | tcursor(CSleft); | ||
| 595 | break; | ||
| 596 | case '\r': | ||
| 597 | tmoveto(0, term.c.y); | ||
| 598 | break; | ||
| 599 | case '\n': | ||
| 600 | tnewline(); | ||
| 601 | break; | ||
| 602 | case '\a': | ||
| 603 | xbell(); | ||
| 604 | break; | ||
| 605 | } | ||
| 606 | } | ||
| 607 | |||
| 608 | void | ||
| 609 | tputs(char *s, int len) { | ||
| 610 | for(; len > 0; len--) | ||
| 611 | tputc(*s++); | ||
| 612 | } | ||
| 613 | |||
| 614 | void | ||
| 615 | tdump(void) { | ||
| 616 | int row, col; | ||
| 617 | Glyph c; | ||
| 618 | |||
| 619 | for(row = 0; row < term.row; row++) { | ||
| 620 | for(col = 0; col < term.col; col++) { | ||
| 621 | if(col == term.c.x && row == term.c.y) | ||
| 622 | putchar('#'); | ||
| 623 | else { | ||
| 624 | c = term.line[row][col]; | ||
| 625 | putchar(c.state & CRset ? c.c : '.'); | ||
| 626 | } | ||
| 627 | } | ||
| 628 | putchar('\n'); | ||
| 629 | } | ||
| 630 | } | ||
| 631 | |||
| 632 | void | ||
| 633 | tresize(int col, int row) { | ||
| 634 | int i; | ||
| 635 | Line *line; | ||
| 636 | int minrow = MIN(row, term.row); | ||
| 637 | int mincol = MIN(col, term.col); | ||
| 638 | |||
| 639 | if(col < 1 || row < 1) | ||
| 640 | return; | ||
| 641 | line = calloc(row, sizeof(Line)); | ||
| 642 | for(i = 0 ; i < row; i++) | ||
| 643 | line[i] = calloc(col, sizeof(Glyph)); | ||
| 644 | for(i = 0 ; i < minrow; i++) { | ||
| 645 | memcpy(line[i], term.line[i], mincol * sizeof(Glyph)); | ||
| 646 | free(term.line[i]); | ||
| 647 | } | ||
| 648 | free(term.line); | ||
| 649 | LIMIT(term.c.x, 0, col-1); | ||
| 650 | LIMIT(term.c.y, 0, row-1); | ||
| 651 | LIMIT(term.top, 0, row-1); | ||
| 652 | LIMIT(term.bot, 0, row-1); | ||
| 653 | // if(term.bot == term.row-1) | ||
| 654 | term.bot = row-1; | ||
| 655 | term.line = line; | ||
| 656 | term.col = col, term.row = row; | ||
| 657 | } | ||
| 658 | |||
| 659 | unsigned long | ||
| 660 | xgetcol(const char *s) { | ||
| 661 | XColor color; | ||
| 662 | Colormap cmap = DefaultColormap(xw.dis, xw.scr); | ||
| 663 | |||
| 664 | if(!XAllocNamedColor(xw.dis, cmap, s, &color, &color)) { | ||
| 665 | color.pixel = WhitePixel(xw.dis, xw.scr); | ||
| 666 | fprintf(stderr, "Could not allocate color '%s'\n", s); | ||
| 667 | } | ||
| 668 | return color.pixel; | ||
| 669 | } | ||
| 670 | |||
| 671 | |||
| 672 | void | ||
| 673 | xclear(int x1, int y1, int x2, int y2) { | ||
| 674 | XClearArea(xw.dis, xw.win, | ||
| 675 | x1 * xw.cw, y1 * xw.ch, | ||
| 676 | (x2-x1+1) * xw.cw, (y2-y1+1) * xw.ch, | ||
| 677 | False); | ||
| 678 | } | ||
| 679 | |||
| 680 | |||
| 681 | void | ||
| 682 | xscroll(void) { | ||
| 683 | int srcy = (term.top+1) * xw.ch; | ||
| 684 | int dsty = term.top * xw.ch; | ||
| 685 | int height = (term.bot-term.top) * xw.ch; | ||
| 686 | |||
| 687 | xcursor(CShide); | ||
| 688 | XCopyArea(xw.dis, xw.win, xw.win, dc.gc, 0, srcy, xw.w, height, 0, dsty); | ||
| 689 | xclear(0, term.bot, term.col-1, term.bot); | ||
| 690 | } | ||
| 691 | |||
| 692 | |||
| 693 | |||
| 694 | |||
| 695 | void | ||
| 696 | xinit(void) { | ||
| 697 | XGCValues values; | ||
| 698 | unsigned long valuemask; | ||
| 699 | XClassHint chint; | ||
| 700 | XWMHints wmhint; | ||
| 701 | XSizeHints shint; | ||
| 702 | char *args[] = {NULL}; | ||
| 703 | int i; | ||
| 704 | |||
| 705 | xw.dis = XOpenDisplay(NULL); | ||
| 706 | xw.scr = XDefaultScreen(xw.dis); | ||
| 707 | /* font */ | ||
| 708 | dc.font = XLoadQueryFont(xw.dis, FONT); | ||
| 709 | xw.cw = dc.font->max_bounds.rbearing - dc.font->min_bounds.lbearing; | ||
| 710 | xw.ch = dc.font->ascent + dc.font->descent + LINESPACE; | ||
| 711 | /* colors */ | ||
| 712 | for(i = 0; i < LEN(colorname); i++) | ||
| 713 | dc.col[i] = xgetcol(colorname[i]); | ||
| 714 | term.c.attr.fg = DefaultFG; | ||
| 715 | term.c.attr.bg = DefaultBG; | ||
| 716 | term.c.attr.mode = ATnone; | ||
| 717 | /* windows */ | ||
| 718 | xw.h = term.row * xw.ch; | ||
| 719 | xw.w = term.col * xw.cw; | ||
| 720 | /* XXX: this BORDER is useless after the first resize, handle it in xdraws() */ | ||
| 721 | xw.win = XCreateSimpleWindow(xw.dis, XRootWindow(xw.dis, xw.scr), 0, 0, | ||
| 722 | xw.w, xw.h, BORDER, | ||
| 723 | dc.col[DefaultBG], | ||
| 724 | dc.col[DefaultBG]); | ||
| 725 | /* gc */ | ||
| 726 | values.foreground = XWhitePixel(xw.dis, xw.scr); | ||
| 727 | values.font = dc.font->fid; | ||
| 728 | valuemask = GCForeground | GCFont; | ||
| 729 | dc.gc = XCreateGC(xw.dis, xw.win, valuemask, &values); | ||
| 730 | XMapWindow(xw.dis, xw.win); | ||
| 731 | /* wm stuff */ | ||
| 732 | chint.res_name = TNAME, chint.res_class = TNAME; | ||
| 733 | wmhint.input = 1, wmhint.flags = InputHint; | ||
| 734 | shint.height_inc = xw.ch, shint.width_inc = xw.cw; | ||
| 735 | shint.height = xw.h, shint.width = xw.w; | ||
| 736 | shint.flags = PSize | PResizeInc; | ||
| 737 | XSetWMProperties(xw.dis, xw.win, NULL, NULL, &args[0], 0, &shint, &wmhint, &chint); | ||
| 738 | XStoreName(xw.dis, xw.win, TNAME); | ||
| 739 | XSync(xw.dis, 0); | ||
| 740 | } | ||
| 741 | |||
| 742 | void | ||
| 743 | xdrawc(int x, int y, Glyph g) { | ||
| 744 | XRectangle r = { x * xw.cw, y * xw.ch, xw.cw, xw.ch }; | ||
| 745 | unsigned long xfg, xbg; | ||
| 746 | |||
| 747 | /* reverse video */ | ||
| 748 | if(g.mode & ATreverse) | ||
| 749 | xfg = dc.col[g.bg], xbg = dc.col[g.fg]; | ||
| 750 | else | ||
| 751 | xfg = dc.col[g.fg], xbg = dc.col[g.bg]; | ||
| 752 | /* background */ | ||
| 753 | XSetForeground(xw.dis, dc.gc, xbg); | ||
| 754 | XFillRectangles(xw.dis, xw.win, dc.gc, &r, 1); | ||
| 755 | /* string */ | ||
| 756 | XSetForeground(xw.dis, dc.gc, xfg); | ||
| 757 | XDrawString(xw.dis, xw.win, dc.gc, r.x, r.y+dc.font->ascent, &(g.c), 1); | ||
| 758 | if(g.mode & ATbold) /* XXX: bold hack (draw again at x+1) */ | ||
| 759 | XDrawString(xw.dis, xw.win, dc.gc, r.x+1, r.y+dc.font->ascent, &(g.c), 1); | ||
| 760 | /* underline */ | ||
| 761 | if(g.mode & ATunderline) { | ||
| 762 | r.y += dc.font->ascent + 1; | ||
| 763 | XDrawLine(xw.dis, xw.win, dc.gc, r.x, r.y, r.x+r.width-1, r.y); | ||
| 764 | } | ||
| 765 | } | ||
| 766 | |||
| 767 | void | ||
| 768 | xcursor(int mode) { | ||
| 769 | static int oldx = 0; | ||
| 770 | static int oldy = 0; | ||
| 771 | Glyph g = {' ', ATnone, DefaultBG, DefaultCS, 0}; | ||
| 772 | |||
| 773 | if(term.line[term.c.y][term.c.x].state & CRset) | ||
| 774 | g.c = term.line[term.c.y][term.c.x].c; | ||
| 775 | /* remove the old cursor */ | ||
| 776 | if(term.line[oldy][oldx].state & CRset) | ||
| 777 | xdrawc(oldx, oldy, term.line[oldy][oldx]); | ||
| 778 | else xclear(oldx, oldy, oldx, oldy); /* XXX: maybe a bug */ | ||
| 779 | if(mode == CSdraw && !term.c.hidden) { | ||
| 780 | xdrawc(term.c.x, term.c.y, g); | ||
| 781 | oldx = term.c.x, oldy = term.c.y; | ||
| 782 | } | ||
| 783 | } | ||
| 784 | |||
| 785 | |||
| 786 | void | ||
| 787 | draw(int redraw_all) { | ||
| 788 | int x, y; | ||
| 789 | int changed, set; | ||
| 790 | |||
| 791 | if(redraw_all) | ||
| 792 | XClearWindow(xw.dis, xw.win); | ||
| 793 | /* XXX: drawing could be optimised */ | ||
| 794 | for(y = 0; y < term.row; y++) { | ||
| 795 | for(x = 0; x < term.col; x++) { | ||
| 796 | changed = term.line[y][x].state & CRupdate; | ||
| 797 | set = term.line[y][x].state & CRset; | ||
| 798 | if((changed && set) || (redraw_all && set)) { | ||
| 799 | term.line[y][x].state &= ~CRupdate; | ||
| 800 | xdrawc(x, y, term.line[y][x]); | ||
| 801 | } | ||
| 802 | } | ||
| 803 | } | ||
| 804 | xcursor(CSdraw); | ||
| 805 | } | ||
| 806 | |||
| 807 | void | ||
| 808 | kpress(XKeyEvent *e) { | ||
| 809 | KeySym ksym; | ||
| 810 | char buf[32]; | ||
| 811 | int len; | ||
| 812 | int meta; | ||
| 813 | int shift; | ||
| 814 | |||
| 815 | meta = e->state & Mod4Mask; | ||
| 816 | shift = e->state & ShiftMask; | ||
| 817 | len = XLookupString(e, buf, sizeof(buf), &ksym, NULL); | ||
| 818 | if(len > 0) { | ||
| 819 | buf[sizeof(buf)-1] = '\0'; | ||
| 820 | if(meta && len == 1) | ||
| 821 | ttywrite("\033", 1); | ||
| 822 | ttywrite(buf, len); | ||
| 823 | return; | ||
| 824 | } | ||
| 825 | switch(ksym) { | ||
| 826 | #ifdef DEBUG1 | ||
| 827 | default: | ||
| 828 | printf("errkey: %d\n", (int)ksym); | ||
| 829 | break; | ||
| 830 | #endif | ||
| 831 | case XK_Up: | ||
| 832 | case XK_Down: | ||
| 833 | case XK_Left: | ||
| 834 | case XK_Right: | ||
| 835 | sprintf(buf, "\033[%c", "DACB"[ksym - XK_Left]); | ||
| 836 | ttywrite(buf, 3); | ||
| 837 | break; | ||
| 838 | case XK_Delete: ttywrite(KEYDELETE, sizeof(KEYDELETE)-1); break; | ||
| 839 | case XK_Home: ttywrite( KEYHOME, sizeof( KEYHOME)-1); break; | ||
| 840 | case XK_End: ttywrite( KEYEND, sizeof( KEYEND)-1); break; | ||
| 841 | case XK_Prior: ttywrite( KEYPREV, sizeof( KEYPREV)-1); break; | ||
| 842 | case XK_Next: ttywrite( KEYNEXT, sizeof( KEYNEXT)-1); break; | ||
| 843 | case XK_Insert: | ||
| 844 | /* XXX: paste X clipboard */ | ||
| 845 | if(shift); | ||
| 846 | break; | ||
| 847 | } | ||
| 848 | } | ||
| 849 | |||
| 850 | void | ||
| 851 | resize(XEvent *e) { | ||
| 852 | int col, row; | ||
| 853 | col = e->xconfigure.width / xw.cw; | ||
| 854 | row = e->xconfigure.height / xw.ch; | ||
| 855 | |||
| 856 | if(term.col != col && term.row != row) { | ||
| 857 | tresize(col, row); | ||
| 858 | ttyresize(col, row); | ||
| 859 | xw.w = e->xconfigure.width; | ||
| 860 | xw.h = e->xconfigure.height; | ||
| 861 | draw(SCredraw); | ||
| 862 | } | ||
| 863 | } | ||
| 864 | |||
| 865 | |||
| 866 | void | ||
| 867 | run(void) { | ||
| 868 | int ret; | ||
| 869 | XEvent ev; | ||
| 870 | fd_set rfd; | ||
| 871 | struct timeval tv = {0, 10000}; | ||
| 872 | |||
| 873 | running = 1; | ||
| 874 | XSelectInput(xw.dis, xw.win, ExposureMask | KeyPressMask | StructureNotifyMask); | ||
| 875 | XResizeWindow(xw.dis, xw.win, xw.w , xw.h); /* seems to fix the resize bug in wmii */ | ||
| 876 | while(running) { | ||
| 877 | while(XPending(xw.dis)) { | ||
| 878 | XNextEvent(xw.dis, &ev); | ||
| 879 | switch (ev.type) { | ||
| 880 | default: | ||
| 881 | break; | ||
| 882 | case KeyPress: | ||
| 883 | kpress(&ev.xkey); | ||
| 884 | break; | ||
| 885 | case Expose: | ||
| 886 | draw(SCredraw); | ||
| 887 | break; | ||
| 888 | case ConfigureNotify: | ||
| 889 | resize(&ev); | ||
| 890 | break; | ||
| 891 | } | ||
| 892 | } | ||
| 893 | FD_ZERO(&rfd); | ||
| 894 | FD_SET(cmdfd, &rfd); | ||
| 895 | ret = select(cmdfd+1, &rfd, NULL, NULL, &tv); | ||
| 896 | if(ret < 0) { | ||
| 897 | fprintf(stderr, "select: %m\n"); | ||
| 898 | running = 0; | ||
| 899 | } | ||
| 900 | if(!ret) | ||
| 901 | continue; | ||
| 902 | if(FD_ISSET(cmdfd, &rfd)) { | ||
| 903 | ttyread(); | ||
| 904 | draw(SCupdate); | ||
| 905 | } | ||
| 906 | } | ||
| 907 | } | ||
| 908 | |||
| 909 | int | ||
| 910 | main(int argc, char *argv[]) { | ||
| 911 | if(argc == 2 && !strncmp("-v", argv[1], 3)) | ||
| 912 | die("st-"VERSION", © 2009 st engineers\n"); | ||
| 913 | else if(argc != 1) | ||
| 914 | die("usage: st [-v]\n"); | ||
| 915 | setlocale(LC_CTYPE, ""); | ||
| 916 | tnew(80, 24); | ||
| 917 | ttynew(); | ||
| 918 | xinit(); | ||
| 919 | run(); | ||
| 16 | return 0; | 920 | return 0; |
| 17 | } | 921 | } |
