Xinqi Bao's Git
5f40ddda626fb06dead056fb5e2ef09a43557ec7
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 */
100 unsigned long col
[LEN(colorname
)];
105 static void die(const char *errstr
, ...);
106 static void draw(int);
107 static void execsh(void);
108 static void sigchld(int);
109 static void run(void);
111 static int escaddc(char);
112 static int escfinal(char);
113 static void escdump(void);
114 static void eschandle(void);
115 static void escparse(void);
116 static void escreset(void);
118 static void tclearregion(int, int, int, int);
119 static void tcpos(int);
120 static void tcursor(int);
121 static void tdeletechar(int);
122 static void tdeleteline(int);
123 static void tinsertblank(int);
124 static void tinsertblankline(int);
125 static void tmoveto(int, int);
126 static void tnew(int, int);
127 static void tnewline(void);
128 static void tputc(char);
129 static void tputs(char*, int);
130 static void tresize(int, int);
131 static void tscroll(void);
132 static void tsetattr(int*, int);
133 static void tsetchar(char);
134 static void tsetscroll(int, int);
136 static void ttynew(void);
137 static void ttyread(void);
138 static void ttyresize(int, int);
139 static void ttywrite(const char *, size_t);
141 static unsigned long xgetcol(const char *);
142 static void xclear(int, int, int, int);
143 static void xcursor(int);
144 static void xdrawc(int, int, Glyph
);
145 static void xinit(void);
146 static void xscroll(void);
148 static void expose(XEvent
*);
149 static void kpress(XEvent
*);
150 static void resize(XEvent
*);
152 static void (*handler
[LASTEvent
])(XEvent
*) = {
155 [ConfigureNotify
] = resize
162 static Escseq escseq
;
173 for(row
= 0; row
< term
.row
; row
++) {
174 for(col
= 0; col
< term
.col
; col
++) {
175 if(col
== term
.c
.x
&& row
== term
.c
.y
)
178 c
= term
.line
[row
][col
];
179 putchar(c
.state
& CRset
? c
.c
: '.');
188 die(const char *errstr
, ...) {
191 va_start(ap
, errstr
);
192 vfprintf(stderr
, errstr
, ap
);
199 char *args
[3] = {SHELL
, "-i", NULL
};
200 putenv("TERM=" TNAME
);
205 xbell(void) { /* visual bell */
206 XRectangle r
= { 0, 0, xw
.w
, xw
.h
};
207 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[BellCol
]);
208 XFillRectangles(xw
.dis
, xw
.win
, dc
.gc
, &r
, 1);
216 if(waitpid(pid
, &stat
, 0) < 0)
217 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
219 exit(WEXITSTATUS(stat
));
229 if((m
= posix_openpt(O_RDWR
| O_NOCTTY
)) < 0)
230 die("openpt failed: %s\n", SERRNO
);
232 die("grandpt failed: %s\n", SERRNO
);
234 die("unlockpt failed: %s\n", SERRNO
);
235 if(!(pts
= ptsname(m
)))
236 die("ptsname failed: %s\n", SERRNO
);
237 if((s
= open(pts
, O_RDWR
| O_NOCTTY
)) < 0)
238 die("Couldn't open slave: %s\n", SERRNO
);
239 fcntl(s
, F_SETFL
, O_NDELAY
);
240 switch(pid
= fork()) {
242 die("fork failed\n");
245 setsid(); /* create a new process group */
246 dup2(s
, STDIN_FILENO
);
247 dup2(s
, STDOUT_FILENO
);
248 dup2(s
, STDERR_FILENO
);
249 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
250 die("ioctl TTIOCSTTY failed: %s\n", SERRNO
);
256 signal(SIGCHLD
, sigchld
);
263 fprintf(stderr
, " %02x %c ", c
, isprint(c
)?c
:'.');
265 fprintf(stderr
, "\n");
270 char buf
[BUFSIZ
] = {0};
273 switch(ret
= read(cmdfd
, buf
, BUFSIZ
)) {
275 die("Couldn't read from shell: %s\n", SERRNO
);
283 ttywrite(const char *s
, size_t n
) {
284 if(write(cmdfd
, s
, n
) == -1)
285 die("write error on tty: %s\n", SERRNO
);
289 ttyresize(int x
, int y
) {
294 w
.ws_xpixel
= w
.ws_ypixel
= 0;
295 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
296 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
312 else if(BETWEEN(c
, 0x40, 0x7E))
323 x
= term
.c
.x
, y
= term
.c
.y
;
324 else if(mode
== CSload
)
329 tnew(int col
, int row
) { /* screen size */
330 term
.row
= row
, term
.col
= col
;
331 term
.top
= 0, term
.bot
= term
.row
- 1;
335 term
.c
.attr
.mode
= ATnone
;
336 term
.c
.attr
.fg
= DefaultFG
;
337 term
.c
.attr
.bg
= DefaultBG
;
338 term
.c
.x
= term
.c
.y
= 0;
340 /* allocate screen */
341 term
.line
= calloc(term
.row
, sizeof(Line
));
342 for(row
= 0 ; row
< term
.row
; row
++)
343 term
.line
[row
] = calloc(term
.col
, sizeof(Glyph
));
348 Line temp
= term
.line
[term
.top
];
350 /* X stuff _before_ the line swapping (results in wrong line index) */
352 for(i
= term
.top
; i
< term
.bot
; i
++)
353 term
.line
[i
] = term
.line
[i
+1];
354 memset(temp
, 0, sizeof(Glyph
) * term
.col
);
355 term
.line
[term
.bot
] = temp
;
360 int y
= term
.c
.y
+ 1;
362 tscroll(), y
= term
.bot
;
368 escseq
.buf
[escseq
.len
++] = c
;
369 if(escfinal(c
) || escseq
.len
>= ESCSIZ
) {
370 escparse(), eschandle();
379 char *p
= escseq
.buf
;
382 switch(escseq
.pre
= *p
++) {
385 escseq
.priv
= 1, p
++;
387 while(p
< escseq
.buf
+escseq
.len
) {
389 escseq
.arg
[escseq
.narg
] *= 10;
390 escseq
.arg
[escseq
.narg
] += *(p
++) - '0'/*, noarg = 0 */;
402 /* XXX: graphic character set */
408 tmoveto(int x
, int y
) {
409 term
.c
.x
= x
< 0 ? 0 : x
>= term
.col
? term
.col
-1 : x
;
410 term
.c
.y
= y
< 0 ? 0 : y
>= term
.row
? term
.row
-1 : y
;
415 int xf
= term
.c
.x
, yf
= term
.c
.y
;
427 xf
= term
.col
-1, yf
--;
429 yf
= term
.top
, xf
= 0;
437 yf
= term
.bot
, tscroll();
446 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
447 term
.line
[term
.c
.y
][term
.c
.x
].c
= c
;
448 term
.line
[term
.c
.y
][term
.c
.x
].state
|= CRset
| CRupdate
;
452 tclearregion(int x1
, int y1
, int x2
, int y2
) {
455 LIMIT(x1
, 0, term
.col
-1);
456 LIMIT(x2
, 0, term
.col
-1);
457 LIMIT(y1
, 0, term
.row
-1);
458 LIMIT(y2
, 0, term
.row
-1);
460 /* XXX: could be optimized */
461 for(x
= x1
; x
<= x2
; x
++)
462 for(y
= y1
; y
<= y2
; y
++)
463 memset(&term
.line
[y
][x
], 0, sizeof(Glyph
));
465 xclear(x1
, y1
, x2
, y2
);
470 int src
= term
.c
.x
+ n
;
472 int size
= term
.col
- src
;
474 if(src
>= 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(term
.col
-size
, term
.c
.y
, term
.col
-1, term
.c
.y
);
483 tinsertblank(int n
) {
486 int size
= term
.col
- n
- src
;
488 if(dst
>= term
.col
) {
489 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
492 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
493 tclearregion(src
, term
.c
.y
, dst
, term
.c
.y
);
497 tsetlinestate(int n
, int state
) {
499 for(i
= 0; i
< term
.col
; i
++)
500 term
.line
[n
][i
].state
|= state
;
504 tinsertblankline (int n
) {
509 if(term
.c
.y
> term
.bot
)
511 else if(term
.c
.y
< term
.top
)
513 if(term
.c
.y
+ n
>= bot
) {
514 tclearregion(0, term
.c
.y
, term
.col
-1, bot
);
517 for(i
= bot
; i
>= term
.c
.y
+n
; i
--) {
518 /* swap deleted line <-> blanked line */
519 blank
= term
.line
[i
];
520 term
.line
[i
] = term
.line
[i
-n
];
521 term
.line
[i
-n
] = blank
;
523 memset(blank
, 0, term
.col
* sizeof(Glyph
));
524 tsetlinestate(i
, CRupdate
);
525 tsetlinestate(i
-n
, CRupdate
);
535 if(term
.c
.y
> term
.bot
)
537 else if(term
.c
.y
< term
.top
)
539 if(term
.c
.y
+ n
>= bot
) {
540 tclearregion(0, term
.c
.y
, term
.col
-1, bot
);
543 for(i
= term
.c
.y
; i
<= bot
-n
; i
++) {
544 /* swap deleted line <-> blanked line */
545 blank
= term
.line
[i
];
546 term
.line
[i
] = term
.line
[i
+n
];
547 term
.line
[i
+n
] = blank
;
549 memset(blank
, 0, term
.col
* sizeof(Glyph
));
550 tsetlinestate(i
, CRupdate
);
551 tsetlinestate(i
-n
, CRupdate
);
556 tsetattr(int *attr
, int l
) {
559 for(i
= 0; i
< l
; i
++) {
562 memset(&term
.c
.attr
, 0, sizeof(term
.c
.attr
));
563 term
.c
.attr
.fg
= DefaultFG
;
564 term
.c
.attr
.bg
= DefaultBG
;
567 term
.c
.attr
.mode
|= ATbold
;
570 term
.c
.attr
.mode
|= ATunderline
;
573 term
.c
.attr
.mode
|= ATreverse
;
576 term
.c
.hidden
= CShide
;
579 term
.c
.attr
.mode
&= ~ATbold
;
582 term
.c
.attr
.mode
&= ~ATunderline
;
585 term
.c
.attr
.mode
&= ~ATreverse
;
588 term
.c
.attr
.fg
= DefaultFG
;
591 term
.c
.attr
.fg
= DefaultBG
;
594 if(BETWEEN(attr
[i
], 30, 37))
595 term
.c
.attr
.fg
= attr
[i
] - 30;
596 else if(BETWEEN(attr
[i
], 40, 47))
597 term
.c
.attr
.bg
= attr
[i
] - 40;
604 tsetscroll(int t
, int b
) {
607 LIMIT(t
, 0, term
.row
-1);
608 LIMIT(b
, 0, term
.row
-1);
624 switch(escseq
.mode
) {
627 fprintf(stderr
, "erresc: unknown sequence\n");
630 case '@': /* Insert <n> blank char */
631 DEFAULT(escseq
.arg
[0], 1);
632 tinsertblank(escseq
.arg
[0]);
634 case 'A': /* Cursor <n> Up */
636 DEFAULT(escseq
.arg
[0], 1);
637 tmoveto(term
.c
.x
, term
.c
.y
-escseq
.arg
[0]);
639 case 'B': /* Cursor <n> Down */
640 DEFAULT(escseq
.arg
[0], 1);
641 tmoveto(term
.c
.x
, term
.c
.y
+escseq
.arg
[0]);
643 case 'C': /* Cursor <n> Forward */
645 DEFAULT(escseq
.arg
[0], 1);
646 tmoveto(term
.c
.x
+escseq
.arg
[0], term
.c
.y
);
648 case 'D': /* Cursor <n> Backward */
649 DEFAULT(escseq
.arg
[0], 1);
650 tmoveto(term
.c
.x
-escseq
.arg
[0], term
.c
.y
);
652 case 'E': /* Cursor <n> Down and first col */
653 DEFAULT(escseq
.arg
[0], 1);
654 tmoveto(0, term
.c
.y
+escseq
.arg
[0]);
656 case 'F': /* Cursor <n> Up and first col */
657 DEFAULT(escseq
.arg
[0], 1);
658 tmoveto(0, term
.c
.y
-escseq
.arg
[0]);
660 case 'G': /* Move to <col> */
662 DEFAULT(escseq
.arg
[0], 1);
663 tmoveto(escseq
.arg
[0]-1, term
.c
.y
);
665 case 'H': /* Move to <row> <col> */
667 DEFAULT(escseq
.arg
[0], 1);
668 DEFAULT(escseq
.arg
[1], 1);
669 tmoveto(escseq
.arg
[1]-1, escseq
.arg
[0]-1);
671 case 'J': /* Clear screen */
672 switch(escseq
.arg
[0]) {
674 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.row
-1);
677 tclearregion(0, 0, term
.c
.x
, term
.c
.y
);
680 tclearregion(0, 0, term
.col
-1, term
.row
-1);
684 case 'K': /* Clear line */
685 switch(escseq
.arg
[0]) {
687 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
690 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
693 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
697 case 'L': /* Insert <n> blank lines */
698 DEFAULT(escseq
.arg
[0], 1);
699 tinsertblankline(escseq
.arg
[0]);
702 if(escseq
.priv
&& escseq
.arg
[0] == 25)
705 case 'M': /* Delete <n> lines */
706 DEFAULT(escseq
.arg
[0], 1);
707 tdeleteline(escseq
.arg
[0]);
709 case 'P': /* Delete <n> char */
710 DEFAULT(escseq
.arg
[0], 1);
711 tdeletechar(escseq
.arg
[0]);
713 case 'd': /* Move to <row> */
714 DEFAULT(escseq
.arg
[0], 1);
715 tmoveto(term
.c
.x
, escseq
.arg
[0]-1);
717 case 'h': /* Set terminal mode */
718 if(escseq
.priv
&& escseq
.arg
[0] == 25)
721 case 'm': /* Terminal attribute (color) */
722 tsetattr(escseq
.arg
, escseq
.narg
);
728 DEFAULT(escseq
.arg
[0], 1);
729 DEFAULT(escseq
.arg
[1], term
.row
);
730 tsetscroll(escseq
.arg
[0]-1, escseq
.arg
[1]-1);
733 case 's': /* Save cursor position */
736 case 'u': /* Load cursor position */
747 printf("rawbuf : %s\n", escseq
.buf
);
748 printf("prechar : %c\n", escseq
.pre
);
749 printf("private : %c\n", escseq
.priv
? '?' : ' ');
750 printf("narg : %d\n", escseq
.narg
);
752 for(i
= 0; i
< escseq
.narg
; i
++)
753 printf("\targ %d = %d\n", i
, escseq
.arg
[i
]);
754 printf("mode : %c\n", escseq
.mode
);
759 memset(&escseq
, 0, sizeof(escseq
));
764 int space
= TAB
- term
.c
.x
% TAB
;
766 if(term
.c
.x
+ space
>= term
.col
)
769 for(; space
> 0; space
--)
775 static int inesc
= 0;
779 /* start of escseq */
781 escreset(), inesc
= 1;
797 tmoveto(0, term
.c
.y
);
809 tputs(char *s
, int len
) {
810 for(; len
> 0; len
--)
815 tresize(int col
, int row
) {
818 int minrow
= MIN(row
, term
.row
);
819 int mincol
= MIN(col
, term
.col
);
821 if(col
< 1 || row
< 1)
824 line
= calloc(row
, sizeof(Line
));
825 for(i
= 0 ; i
< row
; i
++)
826 line
[i
] = calloc(col
, sizeof(Glyph
));
828 for(i
= 0 ; i
< minrow
; i
++)
829 memcpy(line
[i
], term
.line
[i
], mincol
* sizeof(Glyph
));
831 for(i
= 0; i
< term
.row
; i
++)
835 LIMIT(term
.c
.x
, 0, col
-1);
836 LIMIT(term
.c
.y
, 0, row
-1);
837 LIMIT(term
.top
, 0, row
-1);
838 LIMIT(term
.bot
, 0, row
-1);
842 term
.col
= col
, term
.row
= row
;
846 xgetcol(const char *s
) {
848 Colormap cmap
= DefaultColormap(xw
.dis
, xw
.scr
);
850 if(!XAllocNamedColor(xw
.dis
, cmap
, s
, &color
, &color
)) {
851 color
.pixel
= WhitePixel(xw
.dis
, xw
.scr
);
852 fprintf(stderr
, "Could not allocate color '%s'\n", s
);
858 xclear(int x1
, int y1
, int x2
, int y2
) {
859 XClearArea(xw
.dis
, xw
.win
,
860 x1
* xw
.cw
, y1
* xw
.ch
,
861 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
,
867 int srcy
= (term
.top
+1) * xw
.ch
;
868 int dsty
= term
.top
* xw
.ch
;
869 int height
= (term
.bot
-term
.top
) * xw
.ch
;
872 XCopyArea(xw
.dis
, xw
.win
, xw
.win
, dc
.gc
, 0, srcy
, xw
.w
, height
, 0, dsty
);
873 xclear(0, term
.bot
, term
.col
-1, term
.bot
);
879 unsigned long valuemask
;
883 char *args
[] = {NULL
};
886 xw
.dis
= XOpenDisplay(NULL
);
887 xw
.scr
= XDefaultScreen(xw
.dis
);
889 die("Can't open display\n");
892 if(!(dc
.font
= XLoadQueryFont(xw
.dis
, FONT
)))
893 die("Can't load font %s\n", FONT
);
895 xw
.cw
= dc
.font
->max_bounds
.rbearing
- dc
.font
->min_bounds
.lbearing
;
896 xw
.ch
= dc
.font
->ascent
+ dc
.font
->descent
+ LINESPACE
;
899 for(i
= 0; i
< LEN(colorname
); i
++)
900 dc
.col
[i
] = xgetcol(colorname
[i
]);
902 term
.c
.attr
.fg
= DefaultFG
;
903 term
.c
.attr
.bg
= DefaultBG
;
904 term
.c
.attr
.mode
= ATnone
;
906 xw
.h
= term
.row
* xw
.ch
;
907 xw
.w
= term
.col
* xw
.cw
;
908 /* XXX: this BORDER is useless after the first resize, handle it in xdraws() */
909 xw
.win
= XCreateSimpleWindow(xw
.dis
, XRootWindow(xw
.dis
, xw
.scr
), 0, 0,
914 values
.foreground
= XWhitePixel(xw
.dis
, xw
.scr
);
915 values
.font
= dc
.font
->fid
;
916 valuemask
= GCForeground
| GCFont
;
917 dc
.gc
= XCreateGC(xw
.dis
, xw
.win
, valuemask
, &values
);
918 XMapWindow(xw
.dis
, xw
.win
);
920 chint
.res_name
= TNAME
, chint
.res_class
= TNAME
;
921 wmhint
.input
= 1, wmhint
.flags
= InputHint
;
922 shint
.height_inc
= xw
.ch
, shint
.width_inc
= xw
.cw
;
923 shint
.height
= xw
.h
, shint
.width
= xw
.w
;
924 shint
.flags
= PSize
| PResizeInc
;
925 XSetWMProperties(xw
.dis
, xw
.win
, NULL
, NULL
, &args
[0], 0, &shint
, &wmhint
, &chint
);
926 XStoreName(xw
.dis
, xw
.win
, TNAME
);
931 xdrawc(int x
, int y
, Glyph g
) {
932 XRectangle r
= { x
* xw
.cw
, y
* xw
.ch
, xw
.cw
, xw
.ch
};
933 unsigned long xfg
, xbg
;
936 if(g
.mode
& ATreverse
)
937 xfg
= dc
.col
[g
.bg
], xbg
= dc
.col
[g
.fg
];
939 xfg
= dc
.col
[g
.fg
], xbg
= dc
.col
[g
.bg
];
941 XSetForeground(xw
.dis
, dc
.gc
, xbg
);
942 XFillRectangles(xw
.dis
, xw
.win
, dc
.gc
, &r
, 1);
944 XSetForeground(xw
.dis
, dc
.gc
, xfg
);
945 XDrawString(xw
.dis
, xw
.win
, dc
.gc
, r
.x
, r
.y
+dc
.font
->ascent
, &(g
.c
), 1);
946 if(g
.mode
& ATbold
) /* XXX: bold hack (draw again at x+1) */
947 XDrawString(xw
.dis
, xw
.win
, dc
.gc
, r
.x
+1, r
.y
+dc
.font
->ascent
, &(g
.c
), 1);
949 if(g
.mode
& ATunderline
) {
950 r
.y
+= dc
.font
->ascent
+ 1;
951 XDrawLine(xw
.dis
, xw
.win
, dc
.gc
, r
.x
, r
.y
, r
.x
+r
.width
-1, r
.y
);
959 Glyph g
= {' ', ATnone
, DefaultBG
, DefaultCS
, 0};
961 LIMIT(oldx
, 0, term
.col
-1);
962 LIMIT(oldy
, 0, term
.row
-1);
964 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& CRset
)
965 g
.c
= term
.line
[term
.c
.y
][term
.c
.x
].c
;
966 /* remove the old cursor */
967 if(term
.line
[oldy
][oldx
].state
& CRset
)
968 xdrawc(oldx
, oldy
, term
.line
[oldy
][oldx
]);
970 xclear(oldx
, oldy
, oldx
, oldy
);
971 /* draw the new one */
973 xdrawc(term
.c
.x
, term
.c
.y
, g
);
974 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
979 draw(int redraw_all
) {
984 XClearWindow(xw
.dis
, xw
.win
);
986 /* XXX: drawing could be optimised */
987 for(y
= 0; y
< term
.row
; y
++) {
988 for(x
= 0; x
< term
.col
; x
++) {
989 changed
= term
.line
[y
][x
].state
& CRupdate
;
990 set
= term
.line
[y
][x
].state
& CRset
;
991 if(redraw_all
|| changed
) {
992 term
.line
[y
][x
].state
&= ~CRupdate
;
994 xdrawc(x
, y
, term
.line
[y
][x
]);
1004 expose(XEvent
*ev
) {
1009 kpress(XEvent
*ev
) {
1010 XKeyEvent
*e
= &ev
->xkey
;
1017 meta
= e
->state
& Mod1Mask
;
1018 shift
= e
->state
& ShiftMask
;
1019 len
= XLookupString(e
, buf
, sizeof(buf
), &ksym
, NULL
);
1021 ttywrite(key
[ksym
], strlen(key
[ksym
]));
1023 buf
[sizeof(buf
)-1] = '\0';
1024 if(meta
&& len
== 1)
1025 ttywrite("\033", 1);
1031 /* XXX: paste X clipboard */;
1034 fprintf(stderr
, "errkey: %d\n", (int)ksym
);
1042 col
= e
->xconfigure
.width
/ xw
.cw
;
1043 row
= e
->xconfigure
.height
/ xw
.ch
;
1045 if(term
.col
!= col
|| term
.row
!= row
) {
1047 ttyresize(col
, row
);
1048 xw
.w
= e
->xconfigure
.width
;
1049 xw
.h
= e
->xconfigure
.height
;
1058 int xfd
= XConnectionNumber(xw
.dis
);
1061 XSelectInput(xw
.dis
, xw
.win
, ExposureMask
| KeyPressMask
| StructureNotifyMask
);
1062 XResizeWindow(xw
.dis
, xw
.win
, xw
.w
, xw
.h
); /* fix resize bug in wmii (?) */
1066 FD_SET(cmdfd
, &rfd
);
1068 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, NULL
) == -1) {
1071 die("select failed: %s\n", SERRNO
);
1073 if(FD_ISSET(cmdfd
, &rfd
)) {
1077 while(XPending(xw
.dis
)) {
1078 XNextEvent(xw
.dis
, &ev
);
1079 if(handler
[ev
.type
])
1080 (handler
[ev
.type
])(&ev
);
1086 main(int argc
, char *argv
[]) {
1087 if(argc
== 2 && !strncmp("-v", argv
[1], 3))
1088 die("st-" VERSION
", © 2009 st engineers\n");
1090 die("usage: st [-v]\n");
1091 setlocale(LC_CTYPE
, "");