Xinqi Bao's Git
1 /* See LICENSE for licence details. */
2 #define _XOPEN_SOURCE 600
12 #include <sys/ioctl.h>
13 #include <sys/select.h>
15 #include <sys/types.h>
19 #include <X11/keysym.h>
20 #include <X11/Xutil.h>
28 #define SERRNO strerror(errno)
29 #define MIN(a, b) ((a) < (b) ? (a) : (b))
30 #define MAX(a, b) ((a) < (b) ? (b) : (a))
31 #define LEN(a) (sizeof(a) / sizeof(a[0]))
32 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
33 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
34 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
36 /* Attribute, Cursor, Character state, Terminal mode, Screen draw mode */
37 enum { ATnone
=0 , ATreverse
=1 , ATunderline
=2, ATbold
=4 };
38 enum { CSup
, CSdown
, CSright
, CSleft
, CShide
, CSdraw
, CSwrap
, CSsave
, CSload
};
39 enum { CRset
=1, CRupdate
=2 };
40 enum { TMwrap
=1, TMinsert
=2 };
41 enum { SCupdate
, SCredraw
};
51 char c
; /* character code */
52 char mode
; /* attribute flags */
53 Color fg
; /* foreground */
54 Color bg
; /* background */
55 char state
; /* state flag */
61 Glyph attr
; /* current char attributes */
67 /* Escape sequence structs */
68 /* ESC <pre> [[ [<priv>] <arg> [;]] <mode>] */
70 char buf
[ESCSIZ
+1]; /* raw string */
71 int len
; /* raw string length */
75 int narg
; /* nb of args */
79 /* Internal representation of the screen */
83 Line
* line
; /* screen */
84 TCursor c
; /* cursor */
85 int top
; /* top scroll limit */
86 int bot
; /* bottom scroll limit */
87 int mode
; /* terminal mode */
90 /* Purely graphic info */
95 int w
; /* window width */
96 int h
; /* window height */
97 int ch
; /* char height */
98 int cw
; /* char width */
103 /* Drawing Context */
105 unsigned long col
[LEN(colorname
)];
110 static void die(const char *errstr
, ...);
111 static void draw(int);
112 static void execsh(void);
113 static void sigchld(int);
114 static void run(void);
116 static int escaddc(char);
117 static int escfinal(char);
118 static void escdump(void);
119 static void eschandle(void);
120 static void escparse(void);
121 static void escreset(void);
123 static void tclearregion(int, int, int, int);
124 static void tcpos(int);
125 static void tcursor(int);
126 static void tdeletechar(int);
127 static void tdeleteline(int);
128 static void tdump(void);
129 static void tinsertblank(int);
130 static void tinsertblankline(int);
131 static void tmoveto(int, int);
132 static void tnew(int, int);
133 static void tnewline(void);
134 static void tputc(char);
135 static void tputs(char*, int);
136 static void tresize(int, int);
137 static void tscroll(void);
138 static void tsetattr(int*, int);
139 static void tsetchar(char);
140 static void tsetscroll(int, int);
142 static void ttynew(void);
143 static void ttyread(void);
144 static void ttyresize(int, int);
145 static void ttywrite(const char *, size_t);
147 static unsigned long xgetcol(const char *);
148 static void xclear(int, int, int, int);
149 static void xcursor(int);
150 static void xdrawc(int, int, Glyph
);
151 static void xinit(void);
152 static void xscroll(void);
154 static void expose(XEvent
*);
155 static void kpress(XEvent
*);
156 static void resize(XEvent
*);
158 static void (*handler
[LASTEvent
])(XEvent
*) = {
161 [ConfigureNotify
] = resize
168 static Escseq escseq
;
174 die(const char *errstr
, ...) {
177 va_start(ap
, errstr
);
178 vfprintf(stderr
, errstr
, ap
);
185 char *args
[3] = {SHELL
, "-i", NULL
};
186 putenv("TERM=" TNAME
);
191 xbell(void) { /* visual bell */
192 XRectangle r
= { 0, 0, xw
.w
, xw
.h
};
193 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[BellCol
]);
194 XFillRectangles(xw
.dis
, xw
.win
, dc
.gc
, &r
, 1);
202 if(waitpid(pid
, &stat
, 0) < 0)
203 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
205 exit(WEXITSTATUS(stat
));
215 if((m
= posix_openpt(O_RDWR
| O_NOCTTY
)) < 0)
216 die("openpt failed: %s\n", SERRNO
);
218 die("grandpt failed: %s\n", SERRNO
);
220 die("unlockpt failed: %s\n", SERRNO
);
221 if(!(pts
= ptsname(m
)))
222 die("ptsname failed: %s\n", SERRNO
);
223 if((s
= open(pts
, O_RDWR
| O_NOCTTY
)) < 0)
224 die("Couldn't open slave: %s\n", SERRNO
);
225 fcntl(s
, F_SETFL
, O_NDELAY
);
226 switch(pid
= fork()) {
228 die("fork failed\n");
231 setsid(); /* create a new process group */
232 dup2(s
, STDIN_FILENO
);
233 dup2(s
, STDOUT_FILENO
);
234 dup2(s
, STDERR_FILENO
);
235 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
236 die("ioctl TTIOCSTTY failed: %s\n", SERRNO
);
242 signal(SIGCHLD
, sigchld
);
249 fprintf(stderr
, " %02x %c ", c
, isprint(c
)?c
:'.');
251 fprintf(stderr
, "\n");
256 char buf
[BUFSIZ
] = {0};
259 switch(ret
= read(cmdfd
, buf
, BUFSIZ
)) {
261 die("Couldn't read from shell: %s\n", SERRNO
);
269 ttywrite(const char *s
, size_t n
) {
270 if(write(cmdfd
, s
, n
) == -1)
271 die("write error on tty: %s\n", SERRNO
);
275 ttyresize(int x
, int y
) {
280 w
.ws_xpixel
= w
.ws_ypixel
= 0;
281 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
282 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
298 else if(BETWEEN(c
, 0x40, 0x7E))
309 x
= term
.c
.x
, y
= term
.c
.y
;
310 else if(mode
== CSload
)
315 tnew(int col
, int row
) { /* screen size */
316 term
.row
= row
, term
.col
= col
;
317 term
.top
= 0, term
.bot
= term
.row
- 1;
321 term
.c
.attr
.mode
= ATnone
;
322 term
.c
.attr
.fg
= DefaultFG
;
323 term
.c
.attr
.bg
= DefaultBG
;
324 term
.c
.x
= term
.c
.y
= 0;
326 /* allocate screen */
327 term
.line
= calloc(term
.row
, sizeof(Line
));
328 for(row
= 0 ; row
< term
.row
; row
++)
329 term
.line
[row
] = calloc(term
.col
, sizeof(Glyph
));
334 Line temp
= term
.line
[term
.top
];
336 /* X stuff _before_ the line swapping (results in wrong line index) */
338 for(i
= term
.top
; i
< term
.bot
; i
++)
339 term
.line
[i
] = term
.line
[i
+1];
340 memset(temp
, 0, sizeof(Glyph
) * term
.col
);
341 term
.line
[term
.bot
] = temp
;
346 int y
= term
.c
.y
+ 1;
348 tscroll(), y
= term
.bot
;
354 escseq
.buf
[escseq
.len
++] = c
;
355 if(escfinal(c
) || escseq
.len
>= ESCSIZ
) {
356 escparse(), eschandle();
365 char *p
= escseq
.buf
;
368 switch(escseq
.pre
= *p
++) {
371 escseq
.priv
= 1, p
++;
373 while(p
< escseq
.buf
+escseq
.len
) {
375 escseq
.arg
[escseq
.narg
] *= 10;
376 escseq
.arg
[escseq
.narg
] += *(p
++) - '0'/*, noarg = 0 */;
388 /* XXX: graphic character set */
394 tmoveto(int x
, int y
) {
395 term
.c
.x
= x
< 0 ? 0 : x
>= term
.col
? term
.col
-1 : x
;
396 term
.c
.y
= y
< 0 ? 0 : y
>= term
.row
? term
.row
-1 : y
;
401 int xf
= term
.c
.x
, yf
= term
.c
.y
;
413 xf
= term
.col
-1, yf
--;
415 yf
= term
.top
, xf
= 0;
423 yf
= term
.bot
, tscroll();
432 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
433 term
.line
[term
.c
.y
][term
.c
.x
].c
= c
;
434 term
.line
[term
.c
.y
][term
.c
.x
].state
|= CRset
| CRupdate
;
438 tclearregion(int x1
, int y1
, int x2
, int y2
) {
441 LIMIT(x1
, 0, term
.col
-1);
442 LIMIT(x2
, 0, term
.col
-1);
443 LIMIT(y1
, 0, term
.row
-1);
444 LIMIT(y2
, 0, term
.row
-1);
446 /* XXX: could be optimized */
447 for(x
= x1
; x
<= x2
; x
++)
448 for(y
= y1
; y
<= y2
; y
++)
449 memset(&term
.line
[y
][x
], 0, sizeof(Glyph
));
451 xclear(x1
, y1
, x2
, y2
);
456 int src
= term
.c
.x
+ n
;
458 int size
= term
.col
- src
;
460 if(src
>= term
.col
) {
461 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
464 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
465 tclearregion(term
.col
-size
, term
.c
.y
, term
.col
-1, term
.c
.y
);
469 tinsertblank(int n
) {
472 int size
= term
.col
- n
- src
;
474 if(dst
>= term
.col
) {
475 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
478 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
479 tclearregion(src
, term
.c
.y
, dst
, term
.c
.y
);
483 tsetlinestate(int n
, int state
) {
485 for(i
= 0; i
< term
.col
; i
++)
486 term
.line
[n
][i
].state
|= state
;
490 tinsertblankline (int n
) {
495 if(term
.c
.y
> term
.bot
)
497 else if(term
.c
.y
< term
.top
)
499 if(term
.c
.y
+ n
>= bot
) {
500 tclearregion(0, term
.c
.y
, term
.col
-1, bot
);
503 for(i
= bot
; i
>= term
.c
.y
+n
; i
--) {
504 /* swap deleted line <-> blanked line */
505 blank
= term
.line
[i
];
506 term
.line
[i
] = term
.line
[i
-n
];
507 term
.line
[i
-n
] = blank
;
509 memset(blank
, 0, term
.col
* sizeof(Glyph
));
510 tsetlinestate(i
, CRupdate
);
511 tsetlinestate(i
-n
, CRupdate
);
521 if(term
.c
.y
> term
.bot
)
523 else if(term
.c
.y
< term
.top
)
525 if(term
.c
.y
+ n
>= bot
) {
526 tclearregion(0, term
.c
.y
, term
.col
-1, bot
);
529 for(i
= term
.c
.y
; i
<= bot
-n
; i
++) {
530 /* swap deleted line <-> blanked line */
531 blank
= term
.line
[i
];
532 term
.line
[i
] = term
.line
[i
+n
];
533 term
.line
[i
+n
] = blank
;
535 memset(blank
, 0, term
.col
* sizeof(Glyph
));
536 tsetlinestate(i
, CRupdate
);
537 tsetlinestate(i
-n
, CRupdate
);
542 tsetattr(int *attr
, int l
) {
545 for(i
= 0; i
< l
; i
++) {
548 memset(&term
.c
.attr
, 0, sizeof(term
.c
.attr
));
549 term
.c
.attr
.fg
= DefaultFG
;
550 term
.c
.attr
.bg
= DefaultBG
;
553 term
.c
.attr
.mode
|= ATbold
;
556 term
.c
.attr
.mode
|= ATunderline
;
559 term
.c
.attr
.mode
|= ATreverse
;
562 term
.c
.hidden
= CShide
;
565 term
.c
.attr
.mode
&= ~ATbold
;
568 term
.c
.attr
.mode
&= ~ATunderline
;
571 term
.c
.attr
.mode
&= ~ATreverse
;
574 term
.c
.attr
.fg
= DefaultFG
;
577 term
.c
.attr
.fg
= DefaultBG
;
580 if(BETWEEN(attr
[i
], 30, 37))
581 term
.c
.attr
.fg
= attr
[i
] - 30;
582 else if(BETWEEN(attr
[i
], 40, 47))
583 term
.c
.attr
.bg
= attr
[i
] - 40;
590 tsetscroll(int t
, int b
) {
593 LIMIT(t
, 0, term
.row
-1);
594 LIMIT(b
, 0, term
.row
-1);
610 switch(escseq
.mode
) {
613 fprintf(stderr
, "erresc: unknown sequence\n");
616 case '@': /* Insert <n> blank char */
617 DEFAULT(escseq
.arg
[0], 1);
618 tinsertblank(escseq
.arg
[0]);
620 case 'A': /* Cursor <n> Up */
622 DEFAULT(escseq
.arg
[0], 1);
623 tmoveto(term
.c
.x
, term
.c
.y
-escseq
.arg
[0]);
625 case 'B': /* Cursor <n> Down */
626 DEFAULT(escseq
.arg
[0], 1);
627 tmoveto(term
.c
.x
, term
.c
.y
+escseq
.arg
[0]);
629 case 'C': /* Cursor <n> Forward */
631 DEFAULT(escseq
.arg
[0], 1);
632 tmoveto(term
.c
.x
+escseq
.arg
[0], term
.c
.y
);
634 case 'D': /* Cursor <n> Backward */
635 DEFAULT(escseq
.arg
[0], 1);
636 tmoveto(term
.c
.x
-escseq
.arg
[0], term
.c
.y
);
638 case 'E': /* Cursor <n> Down and first col */
639 DEFAULT(escseq
.arg
[0], 1);
640 tmoveto(0, term
.c
.y
+escseq
.arg
[0]);
642 case 'F': /* Cursor <n> Up and first col */
643 DEFAULT(escseq
.arg
[0], 1);
644 tmoveto(0, term
.c
.y
-escseq
.arg
[0]);
646 case 'G': /* Move to <col> */
648 DEFAULT(escseq
.arg
[0], 1);
649 tmoveto(escseq
.arg
[0]-1, term
.c
.y
);
651 case 'H': /* Move to <row> <col> */
653 DEFAULT(escseq
.arg
[0], 1);
654 DEFAULT(escseq
.arg
[1], 1);
655 tmoveto(escseq
.arg
[1]-1, escseq
.arg
[0]-1);
657 case 'J': /* Clear screen */
658 switch(escseq
.arg
[0]) {
660 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.row
-1);
663 tclearregion(0, 0, term
.c
.x
, term
.c
.y
);
666 tclearregion(0, 0, term
.col
-1, term
.row
-1);
670 case 'K': /* Clear line */
671 switch(escseq
.arg
[0]) {
673 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
676 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
679 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
683 case 'L': /* Insert <n> blank lines */
684 DEFAULT(escseq
.arg
[0], 1);
685 tinsertblankline(escseq
.arg
[0]);
688 if(escseq
.priv
&& escseq
.arg
[0] == 25)
691 case 'M': /* Delete <n> lines */
692 DEFAULT(escseq
.arg
[0], 1);
693 tdeleteline(escseq
.arg
[0]);
695 case 'P': /* Delete <n> char */
696 DEFAULT(escseq
.arg
[0], 1);
697 tdeletechar(escseq
.arg
[0]);
699 case 'd': /* Move to <row> */
700 DEFAULT(escseq
.arg
[0], 1);
701 tmoveto(term
.c
.x
, escseq
.arg
[0]-1);
703 case 'h': /* Set terminal mode */
704 if(escseq
.priv
&& escseq
.arg
[0] == 25)
707 case 'm': /* Terminal attribute (color) */
708 tsetattr(escseq
.arg
, escseq
.narg
);
714 DEFAULT(escseq
.arg
[0], 1);
715 DEFAULT(escseq
.arg
[1], term
.row
);
716 tsetscroll(escseq
.arg
[0]-1, escseq
.arg
[1]-1);
719 case 's': /* Save cursor position */
722 case 'u': /* Load cursor position */
733 printf("rawbuf : %s\n", escseq
.buf
);
734 printf("prechar : %c\n", escseq
.pre
);
735 printf("private : %c\n", escseq
.priv
? '?' : ' ');
736 printf("narg : %d\n", escseq
.narg
);
738 for(i
= 0; i
< escseq
.narg
; i
++)
739 printf("\targ %d = %d\n", i
, escseq
.arg
[i
]);
740 printf("mode : %c\n", escseq
.mode
);
745 memset(&escseq
, 0, sizeof(escseq
));
750 int space
= TAB
- term
.c
.x
% TAB
;
752 if(term
.c
.x
+ space
>= term
.col
)
755 for(; space
> 0; space
--)
761 static int inesc
= 0;
765 /* start of escseq */
767 escreset(), inesc
= 1;
783 tmoveto(0, term
.c
.y
);
795 tputs(char *s
, int len
) {
796 for(; len
> 0; len
--)
805 for(row
= 0; row
< term
.row
; row
++) {
806 for(col
= 0; col
< term
.col
; col
++) {
807 if(col
== term
.c
.x
&& row
== term
.c
.y
)
810 c
= term
.line
[row
][col
];
811 putchar(c
.state
& CRset
? c
.c
: '.');
819 tresize(int col
, int row
) {
822 int minrow
= MIN(row
, term
.row
);
823 int mincol
= MIN(col
, term
.col
);
825 if(col
< 1 || row
< 1)
828 line
= calloc(row
, sizeof(Line
));
829 for(i
= 0 ; i
< row
; i
++)
830 line
[i
] = calloc(col
, sizeof(Glyph
));
832 for(i
= 0 ; i
< minrow
; i
++)
833 memcpy(line
[i
], term
.line
[i
], mincol
* sizeof(Glyph
));
835 for(i
= 0; i
< term
.row
; i
++)
839 LIMIT(term
.c
.x
, 0, col
-1);
840 LIMIT(term
.c
.y
, 0, row
-1);
841 LIMIT(term
.top
, 0, row
-1);
842 LIMIT(term
.bot
, 0, row
-1);
846 term
.col
= col
, term
.row
= row
;
850 xgetcol(const char *s
) {
852 Colormap cmap
= DefaultColormap(xw
.dis
, xw
.scr
);
854 if(!XAllocNamedColor(xw
.dis
, cmap
, s
, &color
, &color
)) {
855 color
.pixel
= WhitePixel(xw
.dis
, xw
.scr
);
856 fprintf(stderr
, "Could not allocate color '%s'\n", s
);
862 xclear(int x1
, int y1
, int x2
, int y2
) {
863 XClearArea(xw
.dis
, xw
.win
,
864 x1
* xw
.cw
, y1
* xw
.ch
,
865 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
,
871 int srcy
= (term
.top
+1) * xw
.ch
;
872 int dsty
= term
.top
* xw
.ch
;
873 int height
= (term
.bot
-term
.top
) * xw
.ch
;
876 XCopyArea(xw
.dis
, xw
.win
, xw
.win
, dc
.gc
, 0, srcy
, xw
.w
, height
, 0, dsty
);
877 xclear(0, term
.bot
, term
.col
-1, term
.bot
);
883 unsigned long valuemask
;
887 char *args
[] = {NULL
};
890 xw
.dis
= XOpenDisplay(NULL
);
891 xw
.scr
= XDefaultScreen(xw
.dis
);
893 die("Can't open display\n");
896 if(!(dc
.font
= XLoadQueryFont(xw
.dis
, FONT
)))
897 die("Can't load font %s\n", FONT
);
899 xw
.cw
= dc
.font
->max_bounds
.rbearing
- dc
.font
->min_bounds
.lbearing
;
900 xw
.ch
= dc
.font
->ascent
+ dc
.font
->descent
+ LINESPACE
;
903 for(i
= 0; i
< LEN(colorname
); i
++)
904 dc
.col
[i
] = xgetcol(colorname
[i
]);
906 term
.c
.attr
.fg
= DefaultFG
;
907 term
.c
.attr
.bg
= DefaultBG
;
908 term
.c
.attr
.mode
= ATnone
;
910 xw
.h
= term
.row
* xw
.ch
;
911 xw
.w
= term
.col
* xw
.cw
;
912 /* XXX: this BORDER is useless after the first resize, handle it in xdraws() */
913 xw
.win
= XCreateSimpleWindow(xw
.dis
, XRootWindow(xw
.dis
, xw
.scr
), 0, 0,
918 values
.foreground
= XWhitePixel(xw
.dis
, xw
.scr
);
919 values
.font
= dc
.font
->fid
;
920 valuemask
= GCForeground
| GCFont
;
921 dc
.gc
= XCreateGC(xw
.dis
, xw
.win
, valuemask
, &values
);
922 XMapWindow(xw
.dis
, xw
.win
);
924 chint
.res_name
= TNAME
, chint
.res_class
= TNAME
;
925 wmhint
.input
= 1, wmhint
.flags
= InputHint
;
926 shint
.height_inc
= xw
.ch
, shint
.width_inc
= xw
.cw
;
927 shint
.height
= xw
.h
, shint
.width
= xw
.w
;
928 shint
.flags
= PSize
| PResizeInc
;
929 XSetWMProperties(xw
.dis
, xw
.win
, NULL
, NULL
, &args
[0], 0, &shint
, &wmhint
, &chint
);
930 XStoreName(xw
.dis
, xw
.win
, TNAME
);
935 xdrawc(int x
, int y
, Glyph g
) {
936 XRectangle r
= { x
* xw
.cw
, y
* xw
.ch
, xw
.cw
, xw
.ch
};
937 unsigned long xfg
, xbg
;
940 if(g
.mode
& ATreverse
)
941 xfg
= dc
.col
[g
.bg
], xbg
= dc
.col
[g
.fg
];
943 xfg
= dc
.col
[g
.fg
], xbg
= dc
.col
[g
.bg
];
945 XSetForeground(xw
.dis
, dc
.gc
, xbg
);
946 XFillRectangles(xw
.dis
, xw
.win
, dc
.gc
, &r
, 1);
948 XSetForeground(xw
.dis
, dc
.gc
, xfg
);
949 XDrawString(xw
.dis
, xw
.win
, dc
.gc
, r
.x
, r
.y
+dc
.font
->ascent
, &(g
.c
), 1);
950 if(g
.mode
& ATbold
) /* XXX: bold hack (draw again at x+1) */
951 XDrawString(xw
.dis
, xw
.win
, dc
.gc
, r
.x
+1, r
.y
+dc
.font
->ascent
, &(g
.c
), 1);
953 if(g
.mode
& ATunderline
) {
954 r
.y
+= dc
.font
->ascent
+ 1;
955 XDrawLine(xw
.dis
, xw
.win
, dc
.gc
, r
.x
, r
.y
, r
.x
+r
.width
-1, r
.y
);
963 Glyph g
= {' ', ATnone
, DefaultBG
, DefaultCS
, 0};
965 LIMIT(oldx
, 0, term
.col
-1);
966 LIMIT(oldy
, 0, term
.row
-1);
968 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& CRset
)
969 g
.c
= term
.line
[term
.c
.y
][term
.c
.x
].c
;
970 /* remove the old cursor */
971 if(term
.line
[oldy
][oldx
].state
& CRset
)
972 xdrawc(oldx
, oldy
, term
.line
[oldy
][oldx
]);
974 xclear(oldx
, oldy
, oldx
, oldy
);
975 /* draw the new one */
977 xdrawc(term
.c
.x
, term
.c
.y
, g
);
978 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
983 draw(int redraw_all
) {
988 XClearWindow(xw
.dis
, xw
.win
);
990 /* XXX: drawing could be optimised */
991 for(y
= 0; y
< term
.row
; y
++) {
992 for(x
= 0; x
< term
.col
; x
++) {
993 changed
= term
.line
[y
][x
].state
& CRupdate
;
994 set
= term
.line
[y
][x
].state
& CRset
;
995 if(redraw_all
|| changed
) {
996 term
.line
[y
][x
].state
&= ~CRupdate
;
998 xdrawc(x
, y
, term
.line
[y
][x
]);
1008 expose(XEvent
*ev
) {
1013 kpress(XEvent
*ev
) {
1014 XKeyEvent
*e
= &ev
->xkey
;
1021 meta
= e
->state
& Mod1Mask
;
1022 shift
= e
->state
& ShiftMask
;
1023 len
= XLookupString(e
, buf
, sizeof(buf
), &ksym
, NULL
);
1025 ttywrite(key
[ksym
], strlen(key
[ksym
]));
1027 buf
[sizeof(buf
)-1] = '\0';
1028 if(meta
&& len
== 1)
1029 ttywrite("\033", 1);
1035 /* XXX: paste X clipboard */;
1038 fprintf(stderr
, "errkey: %d\n", (int)ksym
);
1046 col
= e
->xconfigure
.width
/ xw
.cw
;
1047 row
= e
->xconfigure
.height
/ xw
.ch
;
1049 if(term
.col
!= col
|| term
.row
!= row
) {
1051 ttyresize(col
, row
);
1052 xw
.w
= e
->xconfigure
.width
;
1053 xw
.h
= e
->xconfigure
.height
;
1062 int xfd
= XConnectionNumber(xw
.dis
);
1065 XSelectInput(xw
.dis
, xw
.win
, ExposureMask
| KeyPressMask
| StructureNotifyMask
);
1066 XResizeWindow(xw
.dis
, xw
.win
, xw
.w
, xw
.h
); /* fix resize bug in wmii (?) */
1070 FD_SET(cmdfd
, &rfd
);
1072 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, NULL
) == -1) {
1075 die("select failed: %s\n", SERRNO
);
1077 if(FD_ISSET(cmdfd
, &rfd
)) {
1081 while(XPending(xw
.dis
)) {
1082 XNextEvent(xw
.dis
, &ev
);
1083 if(handler
[ev
.type
])
1084 (handler
[ev
.type
])(&ev
);
1090 main(int argc
, char *argv
[]) {
1091 if(argc
== 2 && !strncmp("-v", argv
[1], 3))
1092 die("st-" VERSION
", © 2009 st engineers\n");
1094 die("usage: st [-v]\n");
1095 setlocale(LC_CTYPE
, "");