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
};
46 char c
; /* character code */
47 char mode
; /* attribute flags */
48 Color fg
; /* foreground */
49 Color bg
; /* background */
50 char state
; /* state flag */
56 Glyph attr
; /* current char attributes */
62 /* Escape sequence structs */
63 /* ESC <pre> [[ [<priv>] <arg> [;]] <mode>] */
65 char buf
[ESCSIZ
+1]; /* raw string */
66 int len
; /* raw string length */
70 int narg
; /* nb of args */
74 /* Internal representation of the screen */
78 Line
* line
; /* screen */
79 TCursor c
; /* cursor */
80 int top
; /* top scroll limit */
81 int bot
; /* bottom scroll limit */
82 int mode
; /* terminal mode */
85 /* Purely graphic info */
90 int w
; /* window width */
91 int h
; /* window height */
92 int ch
; /* char height */
93 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 tinsertblank(int);
129 static void tinsertblankline(int);
130 static void tmoveto(int, int);
131 static void tnew(int, int);
132 static void tnewline(void);
133 static void tputc(char);
134 static void tputs(char*, int);
135 static void tresize(int, int);
136 static void tscroll(void);
137 static void tsetattr(int*, int);
138 static void tsetchar(char);
139 static void tsetscroll(int, int);
141 static void ttynew(void);
142 static void ttyread(void);
143 static void ttyresize(int, int);
144 static void ttywrite(const char *, size_t);
146 static unsigned long xgetcol(const char *);
147 static void xclear(int, int, int, int);
148 static void xcursor(int);
149 static void xdrawc(int, int, Glyph
);
150 static void xinit(void);
151 static void xscroll(void);
153 static void expose(XEvent
*);
154 static char * kmap(KeySym
);
155 static void kpress(XEvent
*);
156 static void resize(XEvent
*);
158 static void (*handler
[LASTEvent
])(XEvent
*) = {
161 [ConfigureNotify
] = resize
168 static Escseq escseq
;
179 for(row
= 0; row
< term
.row
; row
++) {
180 for(col
= 0; col
< term
.col
; col
++) {
181 if(col
== term
.c
.x
&& row
== term
.c
.y
)
184 c
= term
.line
[row
][col
];
185 putchar(c
.state
& CRset
? c
.c
: '.');
194 die(const char *errstr
, ...) {
197 va_start(ap
, errstr
);
198 vfprintf(stderr
, errstr
, ap
);
205 char *args
[3] = {SHELL
, "-i", NULL
};
206 putenv("TERM=" TNAME
);
211 xbell(void) { /* visual bell */
212 XRectangle r
= { 0, 0, xw
.w
, xw
.h
};
213 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[BellCol
]);
214 XFillRectangles(xw
.dis
, xw
.win
, dc
.gc
, &r
, 1);
222 if(waitpid(pid
, &stat
, 0) < 0)
223 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
225 exit(WEXITSTATUS(stat
));
235 if((m
= posix_openpt(O_RDWR
| O_NOCTTY
)) < 0)
236 die("openpt failed: %s\n", SERRNO
);
238 die("grandpt failed: %s\n", SERRNO
);
240 die("unlockpt failed: %s\n", SERRNO
);
241 if(!(pts
= ptsname(m
)))
242 die("ptsname failed: %s\n", SERRNO
);
243 if((s
= open(pts
, O_RDWR
| O_NOCTTY
)) < 0)
244 die("Couldn't open slave: %s\n", SERRNO
);
245 fcntl(s
, F_SETFL
, O_NDELAY
);
246 switch(pid
= fork()) {
248 die("fork failed\n");
251 setsid(); /* create a new process group */
252 dup2(s
, STDIN_FILENO
);
253 dup2(s
, STDOUT_FILENO
);
254 dup2(s
, STDERR_FILENO
);
255 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
256 die("ioctl TTIOCSTTY failed: %s\n", SERRNO
);
262 signal(SIGCHLD
, sigchld
);
269 fprintf(stderr
, " %02x %c ", c
, isprint(c
)?c
:'.');
271 fprintf(stderr
, "\n");
276 char buf
[BUFSIZ
] = {0};
279 switch(ret
= read(cmdfd
, buf
, BUFSIZ
)) {
281 die("Couldn't read from shell: %s\n", SERRNO
);
289 ttywrite(const char *s
, size_t n
) {
290 if(write(cmdfd
, s
, n
) == -1)
291 die("write error on tty: %s\n", SERRNO
);
295 ttyresize(int x
, int y
) {
300 w
.ws_xpixel
= w
.ws_ypixel
= 0;
301 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
302 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
318 else if(BETWEEN(c
, 0x40, 0x7E))
329 x
= term
.c
.x
, y
= term
.c
.y
;
330 else if(mode
== CSload
)
335 tnew(int col
, int row
) { /* screen size */
336 term
.row
= row
, term
.col
= col
;
337 term
.top
= 0, term
.bot
= term
.row
- 1;
341 term
.c
.attr
.mode
= ATnone
;
342 term
.c
.attr
.fg
= DefaultFG
;
343 term
.c
.attr
.bg
= DefaultBG
;
344 term
.c
.x
= term
.c
.y
= 0;
346 /* allocate screen */
347 term
.line
= calloc(term
.row
, sizeof(Line
));
348 for(row
= 0 ; row
< term
.row
; row
++)
349 term
.line
[row
] = calloc(term
.col
, sizeof(Glyph
));
354 Line temp
= term
.line
[term
.top
];
356 /* X stuff _before_ the line swapping (results in wrong line index) */
358 for(i
= term
.top
; i
< term
.bot
; i
++)
359 term
.line
[i
] = term
.line
[i
+1];
360 memset(temp
, 0, sizeof(Glyph
) * term
.col
);
361 term
.line
[term
.bot
] = temp
;
366 int y
= term
.c
.y
+ 1;
368 tscroll(), y
= term
.bot
;
374 escseq
.buf
[escseq
.len
++] = c
;
375 if(escfinal(c
) || escseq
.len
>= ESCSIZ
) {
376 escparse(), eschandle();
385 char *p
= escseq
.buf
;
388 switch(escseq
.pre
= *p
++) {
391 escseq
.priv
= 1, p
++;
393 while(p
< escseq
.buf
+escseq
.len
) {
395 escseq
.arg
[escseq
.narg
] *= 10;
396 escseq
.arg
[escseq
.narg
] += *(p
++) - '0'/*, noarg = 0 */;
408 /* XXX: graphic character set */
414 tmoveto(int x
, int y
) {
415 term
.c
.x
= x
< 0 ? 0 : x
>= term
.col
? term
.col
-1 : x
;
416 term
.c
.y
= y
< 0 ? 0 : y
>= term
.row
? term
.row
-1 : y
;
421 int xf
= term
.c
.x
, yf
= term
.c
.y
;
433 xf
= term
.col
-1, yf
--;
435 yf
= term
.top
, xf
= 0;
443 yf
= term
.bot
, tscroll();
452 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
453 term
.line
[term
.c
.y
][term
.c
.x
].c
= c
;
454 term
.line
[term
.c
.y
][term
.c
.x
].state
|= CRset
| CRupdate
;
458 tclearregion(int x1
, int y1
, int x2
, int y2
) {
461 LIMIT(x1
, 0, term
.col
-1);
462 LIMIT(x2
, 0, term
.col
-1);
463 LIMIT(y1
, 0, term
.row
-1);
464 LIMIT(y2
, 0, term
.row
-1);
466 /* XXX: could be optimized */
467 for(x
= x1
; x
<= x2
; x
++)
468 for(y
= y1
; y
<= y2
; y
++)
469 memset(&term
.line
[y
][x
], 0, sizeof(Glyph
));
471 xclear(x1
, y1
, x2
, y2
);
476 int src
= term
.c
.x
+ n
;
478 int size
= term
.col
- src
;
480 if(src
>= term
.col
) {
481 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
484 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
485 tclearregion(term
.col
-size
, term
.c
.y
, term
.col
-1, term
.c
.y
);
489 tinsertblank(int n
) {
492 int size
= term
.col
- n
- src
;
494 if(dst
>= term
.col
) {
495 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
498 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
499 tclearregion(src
, term
.c
.y
, dst
, term
.c
.y
);
503 tsetlinestate(int n
, int state
) {
505 for(i
= 0; i
< term
.col
; i
++)
506 term
.line
[n
][i
].state
|= state
;
510 tinsertblankline (int n
) {
515 if(term
.c
.y
> term
.bot
)
517 else if(term
.c
.y
< term
.top
)
519 if(term
.c
.y
+ n
>= bot
) {
520 tclearregion(0, term
.c
.y
, term
.col
-1, bot
);
523 for(i
= bot
; i
>= term
.c
.y
+n
; i
--) {
524 /* swap deleted line <-> blanked line */
525 blank
= term
.line
[i
];
526 term
.line
[i
] = term
.line
[i
-n
];
527 term
.line
[i
-n
] = blank
;
529 memset(blank
, 0, term
.col
* sizeof(Glyph
));
530 tsetlinestate(i
, CRupdate
);
531 tsetlinestate(i
-n
, CRupdate
);
541 if(term
.c
.y
> term
.bot
)
543 else if(term
.c
.y
< term
.top
)
545 if(term
.c
.y
+ n
>= bot
) {
546 tclearregion(0, term
.c
.y
, term
.col
-1, bot
);
549 for(i
= term
.c
.y
; i
<= bot
-n
; i
++) {
550 /* swap deleted line <-> blanked line */
551 blank
= term
.line
[i
];
552 term
.line
[i
] = term
.line
[i
+n
];
553 term
.line
[i
+n
] = blank
;
555 memset(blank
, 0, term
.col
* sizeof(Glyph
));
556 tsetlinestate(i
, CRupdate
);
557 tsetlinestate(i
-n
, CRupdate
);
562 tsetattr(int *attr
, int l
) {
565 for(i
= 0; i
< l
; i
++) {
568 memset(&term
.c
.attr
, 0, sizeof(term
.c
.attr
));
569 term
.c
.attr
.fg
= DefaultFG
;
570 term
.c
.attr
.bg
= DefaultBG
;
573 term
.c
.attr
.mode
|= ATbold
;
576 term
.c
.attr
.mode
|= ATunderline
;
579 term
.c
.attr
.mode
|= ATreverse
;
582 term
.c
.hidden
= CShide
;
585 term
.c
.attr
.mode
&= ~ATbold
;
588 term
.c
.attr
.mode
&= ~ATunderline
;
591 term
.c
.attr
.mode
&= ~ATreverse
;
594 term
.c
.attr
.fg
= DefaultFG
;
597 term
.c
.attr
.fg
= DefaultBG
;
600 if(BETWEEN(attr
[i
], 30, 37))
601 term
.c
.attr
.fg
= attr
[i
] - 30;
602 else if(BETWEEN(attr
[i
], 40, 47))
603 term
.c
.attr
.bg
= attr
[i
] - 40;
610 tsetscroll(int t
, int b
) {
613 LIMIT(t
, 0, term
.row
-1);
614 LIMIT(b
, 0, term
.row
-1);
630 switch(escseq
.mode
) {
633 fprintf(stderr
, "erresc: unknown sequence\n");
636 case '@': /* Insert <n> blank char */
637 DEFAULT(escseq
.arg
[0], 1);
638 tinsertblank(escseq
.arg
[0]);
640 case 'A': /* Cursor <n> Up */
642 DEFAULT(escseq
.arg
[0], 1);
643 tmoveto(term
.c
.x
, term
.c
.y
-escseq
.arg
[0]);
645 case 'B': /* Cursor <n> Down */
646 DEFAULT(escseq
.arg
[0], 1);
647 tmoveto(term
.c
.x
, term
.c
.y
+escseq
.arg
[0]);
649 case 'C': /* Cursor <n> Forward */
651 DEFAULT(escseq
.arg
[0], 1);
652 tmoveto(term
.c
.x
+escseq
.arg
[0], term
.c
.y
);
654 case 'D': /* Cursor <n> Backward */
655 DEFAULT(escseq
.arg
[0], 1);
656 tmoveto(term
.c
.x
-escseq
.arg
[0], term
.c
.y
);
658 case 'E': /* Cursor <n> Down and first col */
659 DEFAULT(escseq
.arg
[0], 1);
660 tmoveto(0, term
.c
.y
+escseq
.arg
[0]);
662 case 'F': /* Cursor <n> Up and first col */
663 DEFAULT(escseq
.arg
[0], 1);
664 tmoveto(0, term
.c
.y
-escseq
.arg
[0]);
666 case 'G': /* Move to <col> */
668 DEFAULT(escseq
.arg
[0], 1);
669 tmoveto(escseq
.arg
[0]-1, term
.c
.y
);
671 case 'H': /* Move to <row> <col> */
673 DEFAULT(escseq
.arg
[0], 1);
674 DEFAULT(escseq
.arg
[1], 1);
675 tmoveto(escseq
.arg
[1]-1, escseq
.arg
[0]-1);
677 case 'J': /* Clear screen */
678 switch(escseq
.arg
[0]) {
680 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.row
-1);
683 tclearregion(0, 0, term
.c
.x
, term
.c
.y
);
686 tclearregion(0, 0, term
.col
-1, term
.row
-1);
690 case 'K': /* Clear line */
691 switch(escseq
.arg
[0]) {
693 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
696 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
699 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
703 case 'L': /* Insert <n> blank lines */
704 DEFAULT(escseq
.arg
[0], 1);
705 tinsertblankline(escseq
.arg
[0]);
708 if(escseq
.priv
&& escseq
.arg
[0] == 25)
711 case 'M': /* Delete <n> lines */
712 DEFAULT(escseq
.arg
[0], 1);
713 tdeleteline(escseq
.arg
[0]);
715 case 'P': /* Delete <n> char */
716 DEFAULT(escseq
.arg
[0], 1);
717 tdeletechar(escseq
.arg
[0]);
719 case 'd': /* Move to <row> */
720 DEFAULT(escseq
.arg
[0], 1);
721 tmoveto(term
.c
.x
, escseq
.arg
[0]-1);
723 case 'h': /* Set terminal mode */
724 if(escseq
.priv
&& escseq
.arg
[0] == 25)
727 case 'm': /* Terminal attribute (color) */
728 tsetattr(escseq
.arg
, escseq
.narg
);
734 DEFAULT(escseq
.arg
[0], 1);
735 DEFAULT(escseq
.arg
[1], term
.row
);
736 tsetscroll(escseq
.arg
[0]-1, escseq
.arg
[1]-1);
739 case 's': /* Save cursor position */
742 case 'u': /* Load cursor position */
753 printf("rawbuf : %s\n", escseq
.buf
);
754 printf("prechar : %c\n", escseq
.pre
);
755 printf("private : %c\n", escseq
.priv
? '?' : ' ');
756 printf("narg : %d\n", escseq
.narg
);
758 for(i
= 0; i
< escseq
.narg
; i
++)
759 printf("\targ %d = %d\n", i
, escseq
.arg
[i
]);
760 printf("mode : %c\n", escseq
.mode
);
765 memset(&escseq
, 0, sizeof(escseq
));
770 int space
= TAB
- term
.c
.x
% TAB
;
772 if(term
.c
.x
+ space
>= term
.col
)
775 for(; space
> 0; space
--)
781 static int inesc
= 0;
785 /* start of escseq */
787 escreset(), inesc
= 1;
803 tmoveto(0, term
.c
.y
);
815 tputs(char *s
, int len
) {
816 for(; len
> 0; len
--)
821 tresize(int col
, int row
) {
824 int minrow
= MIN(row
, term
.row
);
825 int mincol
= MIN(col
, term
.col
);
827 if(col
< 1 || row
< 1)
830 line
= calloc(row
, sizeof(Line
));
831 for(i
= 0 ; i
< row
; i
++)
832 line
[i
] = calloc(col
, sizeof(Glyph
));
834 for(i
= 0 ; i
< minrow
; i
++)
835 memcpy(line
[i
], term
.line
[i
], mincol
* sizeof(Glyph
));
837 for(i
= 0; i
< term
.row
; i
++)
841 LIMIT(term
.c
.x
, 0, col
-1);
842 LIMIT(term
.c
.y
, 0, row
-1);
843 LIMIT(term
.top
, 0, row
-1);
844 LIMIT(term
.bot
, 0, row
-1);
848 term
.col
= col
, term
.row
= row
;
852 xgetcol(const char *s
) {
854 Colormap cmap
= DefaultColormap(xw
.dis
, xw
.scr
);
856 if(!XAllocNamedColor(xw
.dis
, cmap
, s
, &color
, &color
)) {
857 color
.pixel
= WhitePixel(xw
.dis
, xw
.scr
);
858 fprintf(stderr
, "Could not allocate color '%s'\n", s
);
864 xclear(int x1
, int y1
, int x2
, int y2
) {
865 XClearArea(xw
.dis
, xw
.win
,
866 x1
* xw
.cw
, y1
* xw
.ch
,
867 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
,
873 int srcy
= (term
.top
+1) * xw
.ch
;
874 int dsty
= term
.top
* xw
.ch
;
875 int height
= (term
.bot
-term
.top
) * xw
.ch
;
878 XCopyArea(xw
.dis
, xw
.win
, xw
.win
, dc
.gc
, 0, srcy
, xw
.w
, height
, 0, dsty
);
879 xclear(0, term
.bot
, term
.col
-1, term
.bot
);
885 unsigned long valuemask
;
889 char *args
[] = {NULL
};
892 xw
.dis
= XOpenDisplay(NULL
);
893 xw
.scr
= XDefaultScreen(xw
.dis
);
895 die("Can't open display\n");
898 if(!(dc
.font
= XLoadQueryFont(xw
.dis
, FONT
)))
899 die("Can't load font %s\n", FONT
);
901 xw
.cw
= dc
.font
->max_bounds
.rbearing
- dc
.font
->min_bounds
.lbearing
;
902 xw
.ch
= dc
.font
->ascent
+ dc
.font
->descent
+ LINESPACE
;
905 for(i
= 0; i
< LEN(colorname
); i
++)
906 dc
.col
[i
] = xgetcol(colorname
[i
]);
908 term
.c
.attr
.fg
= DefaultFG
;
909 term
.c
.attr
.bg
= DefaultBG
;
910 term
.c
.attr
.mode
= ATnone
;
912 xw
.h
= term
.row
* xw
.ch
;
913 xw
.w
= term
.col
* xw
.cw
;
914 /* XXX: this BORDER is useless after the first resize, handle it in xdraws() */
915 xw
.win
= XCreateSimpleWindow(xw
.dis
, XRootWindow(xw
.dis
, xw
.scr
), 0, 0,
920 values
.foreground
= XWhitePixel(xw
.dis
, xw
.scr
);
921 values
.font
= dc
.font
->fid
;
922 valuemask
= GCForeground
| GCFont
;
923 dc
.gc
= XCreateGC(xw
.dis
, xw
.win
, valuemask
, &values
);
924 XMapWindow(xw
.dis
, xw
.win
);
926 chint
.res_name
= TNAME
, chint
.res_class
= TNAME
;
927 wmhint
.input
= 1, wmhint
.flags
= InputHint
;
928 shint
.height_inc
= xw
.ch
, shint
.width_inc
= xw
.cw
;
929 shint
.height
= xw
.h
, shint
.width
= xw
.w
;
930 shint
.flags
= PSize
| PResizeInc
;
931 XSetWMProperties(xw
.dis
, xw
.win
, NULL
, NULL
, &args
[0], 0, &shint
, &wmhint
, &chint
);
932 XStoreName(xw
.dis
, xw
.win
, TNAME
);
937 xdrawc(int x
, int y
, Glyph g
) {
938 XRectangle r
= { x
* xw
.cw
, y
* xw
.ch
, xw
.cw
, xw
.ch
};
939 unsigned long xfg
, xbg
;
942 if(g
.mode
& ATreverse
)
943 xfg
= dc
.col
[g
.bg
], xbg
= dc
.col
[g
.fg
];
945 xfg
= dc
.col
[g
.fg
], xbg
= dc
.col
[g
.bg
];
947 XSetForeground(xw
.dis
, dc
.gc
, xbg
);
948 XFillRectangles(xw
.dis
, xw
.win
, dc
.gc
, &r
, 1);
950 XSetForeground(xw
.dis
, dc
.gc
, xfg
);
951 XDrawString(xw
.dis
, xw
.win
, dc
.gc
, r
.x
, r
.y
+dc
.font
->ascent
, &(g
.c
), 1);
952 if(g
.mode
& ATbold
) /* XXX: bold hack (draw again at x+1) */
953 XDrawString(xw
.dis
, xw
.win
, dc
.gc
, r
.x
+1, r
.y
+dc
.font
->ascent
, &(g
.c
), 1);
955 if(g
.mode
& ATunderline
) {
956 r
.y
+= dc
.font
->ascent
+ 1;
957 XDrawLine(xw
.dis
, xw
.win
, dc
.gc
, r
.x
, r
.y
, r
.x
+r
.width
-1, r
.y
);
965 Glyph g
= {' ', ATnone
, DefaultBG
, DefaultCS
, 0};
967 LIMIT(oldx
, 0, term
.col
-1);
968 LIMIT(oldy
, 0, term
.row
-1);
970 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& CRset
)
971 g
.c
= term
.line
[term
.c
.y
][term
.c
.x
].c
;
972 /* remove the old cursor */
973 if(term
.line
[oldy
][oldx
].state
& CRset
)
974 xdrawc(oldx
, oldy
, term
.line
[oldy
][oldx
]);
976 xclear(oldx
, oldy
, oldx
, oldy
);
977 /* draw the new one */
979 xdrawc(term
.c
.x
, term
.c
.y
, g
);
980 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
985 draw(int redraw_all
) {
990 XClearWindow(xw
.dis
, xw
.win
);
992 /* XXX: drawing could be optimised */
993 for(y
= 0; y
< term
.row
; y
++) {
994 for(x
= 0; x
< term
.col
; x
++) {
995 changed
= term
.line
[y
][x
].state
& CRupdate
;
996 set
= term
.line
[y
][x
].state
& CRset
;
997 if(redraw_all
|| changed
) {
998 term
.line
[y
][x
].state
&= ~CRupdate
;
1000 xdrawc(x
, y
, term
.line
[y
][x
]);
1010 expose(XEvent
*ev
) {
1017 for(i
= 0; i
< LEN(key
); i
++)
1019 return (char*)key
[i
].s
;
1024 kpress(XEvent
*ev
) {
1025 XKeyEvent
*e
= &ev
->xkey
;
1033 meta
= e
->state
& Mod1Mask
;
1034 shift
= e
->state
& ShiftMask
;
1035 len
= XLookupString(e
, buf
, sizeof(buf
), &ksym
, NULL
);
1037 if(customkey
= kmap(ksym
))
1038 ttywrite(customkey
, strlen(customkey
));
1040 buf
[sizeof(buf
)-1] = '\0';
1041 if(meta
&& len
== 1)
1042 ttywrite("\033", 1);
1048 /* XXX: paste X clipboard */;
1051 fprintf(stderr
, "errkey: %d\n", (int)ksym
);
1059 col
= e
->xconfigure
.width
/ xw
.cw
;
1060 row
= e
->xconfigure
.height
/ xw
.ch
;
1062 if(term
.col
!= col
|| term
.row
!= row
) {
1064 ttyresize(col
, row
);
1065 xw
.w
= e
->xconfigure
.width
;
1066 xw
.h
= e
->xconfigure
.height
;
1075 int xfd
= XConnectionNumber(xw
.dis
);
1078 XSelectInput(xw
.dis
, xw
.win
, ExposureMask
| KeyPressMask
| StructureNotifyMask
);
1079 XResizeWindow(xw
.dis
, xw
.win
, xw
.w
, xw
.h
); /* fix resize bug in wmii (?) */
1083 FD_SET(cmdfd
, &rfd
);
1085 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, NULL
) == -1) {
1088 die("select failed: %s\n", SERRNO
);
1090 if(FD_ISSET(cmdfd
, &rfd
)) {
1094 while(XPending(xw
.dis
)) {
1095 XNextEvent(xw
.dis
, &ev
);
1096 if(handler
[ev
.type
])
1097 (handler
[ev
.type
])(&ev
);
1103 main(int argc
, char *argv
[]) {
1104 if(argc
== 2 && !strncmp("-v", argv
[1], 3))
1105 die("st-" VERSION
", © 2009 st engineers\n");
1107 die("usage: st [-v]\n");
1108 setlocale(LC_CTYPE
, "");