1 /* See LICENSE for licence details. */
2 #define _XOPEN_SOURCE 600
14 #include <sys/ioctl.h>
15 #include <sys/select.h>
18 #include <sys/types.h>
22 #include <X11/Xatom.h>
24 #include <X11/Xutil.h>
25 #include <X11/cursorfont.h>
26 #include <X11/keysym.h>
27 #include <X11/extensions/Xdbe.h>
28 #include <X11/Xft/Xft.h>
29 #include <fontconfig/fontconfig.h>
35 #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
37 #elif defined(__FreeBSD__) || defined(__DragonFly__)
42 "st " VERSION " (c) 2010-2012 st engineers\n" \
43 "usage: st [-v] [-c class] [-f font] [-g geometry] [-o file]" \
44 " [-t title] [-w windowid] [-e command ...]\n"
47 #define XEMBED_FOCUS_IN 4
48 #define XEMBED_FOCUS_OUT 5
51 #define ESC_BUF_SIZ 256
52 #define ESC_ARG_SIZ 16
53 #define STR_BUF_SIZ 256
54 #define STR_ARG_SIZ 16
55 #define DRAW_BUF_SIZ 20*1024
57 #define XK_NO_MOD UINT_MAX
60 #define REDRAW_TIMEOUT (80*1000) /* 80 ms */
62 #define SERRNO strerror(errno)
63 #define MIN(a, b) ((a) < (b) ? (a) : (b))
64 #define MAX(a, b) ((a) < (b) ? (b) : (a))
65 #define LEN(a) (sizeof(a) / sizeof(a[0]))
66 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
67 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
68 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
69 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
70 #define IS_SET(flag) (term.mode & (flag))
71 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
72 #define X2COL(x) (((x) - BORDER)/xw.cw)
73 #define Y2ROW(y) (((y) - BORDER)/xw.ch)
75 enum glyph_attribute
{
85 enum cursor_movement
{
112 MODE_MOUSEMOTION
= 64,
121 ESC_STR
= 4, /* DSC, OSC, PM, APC */
123 ESC_STR_END
= 16, /* a final string was encountered */
134 enum { B0
=1, B1
=2, B2
=4, B3
=8, B4
=16, B5
=32, B6
=64, B7
=128 };
136 typedef unsigned char uchar
;
137 typedef unsigned int uint
;
138 typedef unsigned long ulong
;
139 typedef unsigned short ushort
;
142 char c
[UTF_SIZ
]; /* character code */
143 uchar mode
; /* attribute flags */
144 ushort fg
; /* foreground */
145 ushort bg
; /* background */
146 uchar state
; /* state flags */
152 Glyph attr
; /* current char attributes */
158 /* CSI Escape sequence structs */
159 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
161 char buf
[ESC_BUF_SIZ
]; /* raw string */
162 int len
; /* raw string length */
164 int arg
[ESC_ARG_SIZ
];
165 int narg
; /* nb of args */
169 /* STR Escape sequence structs */
170 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
172 char type
; /* ESC type ... */
173 char buf
[STR_BUF_SIZ
]; /* raw string */
174 int len
; /* raw string length */
175 char *args
[STR_ARG_SIZ
];
176 int narg
; /* nb of args */
179 /* Internal representation of the screen */
181 int row
; /* nb row */
182 int col
; /* nb col */
183 Line
* line
; /* screen */
184 Line
* alt
; /* alternate screen */
185 bool* dirty
; /* dirtyness of lines */
186 TCursor c
; /* cursor */
187 int top
; /* top scroll limit */
188 int bot
; /* bottom scroll limit */
189 int mode
; /* terminal mode flags */
190 int esc
; /* escape state flags */
194 /* Purely graphic info */
200 Atom xembed
, wmdeletewin
;
206 bool isfixed
; /* is fixed geometry? */
207 int fx
, fy
, fw
, fh
; /* fixed geometry */
208 int tw
, th
; /* tty width and height */
209 int w
; /* window width */
210 int h
; /* window height */
211 int ch
; /* char height */
212 int cw
; /* char width */
213 char state
; /* focus, redraw, visible */
222 /* TODO: use better name for vars... */
227 struct {int x
, y
;} b
, e
;
231 struct timeval tclick1
;
232 struct timeval tclick2
;
248 /* Drawing Context */
250 XftColor xft_col
[LEN(colorname
) < 256 ? 256 : LEN(colorname
)];
252 Font font
, bfont
, ifont
, ibfont
;
255 static void die(const char*, ...);
256 static void draw(void);
257 static void redraw(void);
258 static void drawregion(int, int, int, int);
259 static void execsh(void);
260 static void sigchld(int);
261 static void run(void);
263 static void csidump(void);
264 static void csihandle(void);
265 static void csiparse(void);
266 static void csireset(void);
267 static void strdump(void);
268 static void strhandle(void);
269 static void strparse(void);
270 static void strreset(void);
272 static void tclearregion(int, int, int, int);
273 static void tcursor(int);
274 static void tdeletechar(int);
275 static void tdeleteline(int);
276 static void tinsertblank(int);
277 static void tinsertblankline(int);
278 static void tmoveto(int, int);
279 static void tnew(int, int);
280 static void tnewline(int);
281 static void tputtab(bool);
282 static void tputc(char*, int);
283 static void treset(void);
284 static int tresize(int, int);
285 static void tscrollup(int, int);
286 static void tscrolldown(int, int);
287 static void tsetattr(int*, int);
288 static void tsetchar(char*);
289 static void tsetscroll(int, int);
290 static void tswapscreen(void);
291 static void tsetdirt(int, int);
292 static void tsetmode(bool, bool, int *, int);
293 static void tfulldirt(void);
295 static void ttynew(void);
296 static void ttyread(void);
297 static void ttyresize(void);
298 static void ttywrite(const char *, size_t);
300 static void xdraws(char *, Glyph
, int, int, int, int);
301 static void xhints(void);
302 static void xclear(int, int, int, int);
303 static void xclearborders(void);
304 static void xdrawcursor(void);
305 static void xinit(void);
306 static void xloadcols(void);
307 static void xresettitle(void);
308 static void xseturgency(int);
309 static void xsetsel(char*);
310 static void xtermclear(int, int, int, int);
311 static void xresize(int, int);
313 static void expose(XEvent
*);
314 static void visibility(XEvent
*);
315 static void unmap(XEvent
*);
316 static char* kmap(KeySym
, uint
);
317 static void kpress(XEvent
*);
318 static void cmessage(XEvent
*);
319 static void resize(XEvent
*);
320 static void focus(XEvent
*);
321 static void brelease(XEvent
*);
322 static void bpress(XEvent
*);
323 static void bmotion(XEvent
*);
324 static void selnotify(XEvent
*);
325 static void selclear(XEvent
*);
326 static void selrequest(XEvent
*);
328 static void selinit(void);
329 static inline bool selected(int, int);
330 static void selcopy(void);
331 static void selpaste(void);
332 static void selscroll(int, int);
334 static int utf8decode(char *, long *);
335 static int utf8encode(long *, char *);
336 static int utf8size(char *);
337 static int isfullutf8(char *, int);
339 static void *xmalloc(size_t);
340 static void *xrealloc(void *, size_t);
341 static void *xcalloc(size_t nmemb
, size_t size
);
342 static char *smstrcat(char *, ...);
344 static void (*handler
[LASTEvent
])(XEvent
*) = {
346 [ClientMessage
] = cmessage
,
347 [ConfigureNotify
] = resize
,
348 [VisibilityNotify
] = visibility
,
349 [UnmapNotify
] = unmap
,
353 [MotionNotify
] = bmotion
,
354 [ButtonPress
] = bpress
,
355 [ButtonRelease
] = brelease
,
356 [SelectionClear
] = selclear
,
357 [SelectionNotify
] = selnotify
,
358 [SelectionRequest
] = selrequest
,
365 static CSIEscape csiescseq
;
366 static STREscape strescseq
;
369 static Selection sel
;
370 static int iofd
= -1;
371 static char **opt_cmd
= NULL
;
372 static char *opt_io
= NULL
;
373 static char *opt_title
= NULL
;
374 static char *opt_embed
= NULL
;
375 static char *opt_class
= NULL
;
376 static char *opt_font
= NULL
;
379 xmalloc(size_t len
) {
380 void *p
= malloc(len
);
382 die("Out of memory\n");
387 xrealloc(void *p
, size_t len
) {
388 if((p
= realloc(p
, len
)) == NULL
)
389 die("Out of memory\n");
394 xcalloc(size_t nmemb
, size_t size
) {
395 void *p
= calloc(nmemb
, size
);
397 die("Out of memory\n");
402 smstrcat(char *src
, ...)
408 len
= slen
= strlen(src
);
410 va_start(fmtargs
, src
);
412 v
= va_arg(fmtargs
, char *);
419 p
= ret
= xmalloc(len
+1);
420 memmove(p
, src
, slen
);
423 va_start(fmtargs
, src
);
425 v
= va_arg(fmtargs
, char *);
440 utf8decode(char *s
, long *u
) {
446 if(~c
& B7
) { /* 0xxxxxxx */
449 } else if((c
& (B7
|B6
|B5
)) == (B7
|B6
)) { /* 110xxxxx */
450 *u
= c
&(B4
|B3
|B2
|B1
|B0
);
452 } else if((c
& (B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) { /* 1110xxxx */
453 *u
= c
&(B3
|B2
|B1
|B0
);
455 } else if((c
& (B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
)) { /* 11110xxx */
460 for(i
= n
, ++s
; i
> 0; --i
, ++rtn
, ++s
) {
462 if((c
& (B7
|B6
)) != B7
) /* 10xxxxxx */
465 *u
|= c
& (B5
|B4
|B3
|B2
|B1
|B0
);
467 if((n
== 1 && *u
< 0x80) ||
468 (n
== 2 && *u
< 0x800) ||
469 (n
== 3 && *u
< 0x10000) ||
470 (*u
>= 0xD800 && *u
<= 0xDFFF))
479 utf8encode(long *u
, char *s
) {
487 *sp
= uc
; /* 0xxxxxxx */
489 } else if(*u
< 0x800) {
490 *sp
= (uc
>> 6) | (B7
|B6
); /* 110xxxxx */
492 } else if(uc
< 0x10000) {
493 *sp
= (uc
>> 12) | (B7
|B6
|B5
); /* 1110xxxx */
495 } else if(uc
<= 0x10FFFF) {
496 *sp
= (uc
>> 18) | (B7
|B6
|B5
|B4
); /* 11110xxx */
501 for(i
=n
,++sp
; i
>0; --i
,++sp
)
502 *sp
= ((uc
>> 6*(i
-1)) & (B5
|B4
|B3
|B2
|B1
|B0
)) | B7
; /* 10xxxxxx */
512 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
513 UTF-8 otherwise return 0 */
515 isfullutf8(char *s
, int b
) {
523 else if((*c1
&(B7
|B6
|B5
)) == (B7
|B6
) && b
== 1)
525 else if((*c1
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
) &&
527 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
)))
529 else if((*c1
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
) &&
531 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
) ||
532 ((b
== 3) && (*c2
&(B7
|B6
)) == B7
&& (*c3
&(B7
|B6
)) == B7
)))
544 else if((c
&(B7
|B6
|B5
)) == (B7
|B6
))
546 else if((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
))
554 memset(&sel
.tclick1
, 0, sizeof(sel
.tclick1
));
555 memset(&sel
.tclick2
, 0, sizeof(sel
.tclick2
));
559 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
560 if(sel
.xtarget
== None
)
561 sel
.xtarget
= XA_STRING
;
565 selected(int x
, int y
) {
566 if(sel
.ey
== y
&& sel
.by
== y
) {
567 int bx
= MIN(sel
.bx
, sel
.ex
);
568 int ex
= MAX(sel
.bx
, sel
.ex
);
569 return BETWEEN(x
, bx
, ex
);
571 return ((sel
.b
.y
< y
&&y
< sel
.e
.y
) || (y
==sel
.e
.y
&& x
<=sel
.e
.x
))
572 || (y
==sel
.b
.y
&& x
>=sel
.b
.x
&& (x
<=sel
.e
.x
|| sel
.b
.y
!=sel
.e
.y
));
576 getbuttoninfo(XEvent
*e
, int *b
, int *x
, int *y
) {
578 *b
= e
->xbutton
.button
;
580 *x
= X2COL(e
->xbutton
.x
);
581 *y
= Y2ROW(e
->xbutton
.y
);
582 sel
.b
.x
= sel
.by
< sel
.ey
? sel
.bx
: sel
.ex
;
583 sel
.b
.y
= MIN(sel
.by
, sel
.ey
);
584 sel
.e
.x
= sel
.by
< sel
.ey
? sel
.ex
: sel
.bx
;
585 sel
.e
.y
= MAX(sel
.by
, sel
.ey
);
589 mousereport(XEvent
*e
) {
590 int x
= X2COL(e
->xbutton
.x
);
591 int y
= Y2ROW(e
->xbutton
.y
);
592 int button
= e
->xbutton
.button
;
593 int state
= e
->xbutton
.state
;
594 char buf
[] = { '\033', '[', 'M', 0, 32+x
+1, 32+y
+1 };
595 static int ob
, ox
, oy
;
598 if(e
->xbutton
.type
== MotionNotify
) {
599 if(!IS_SET(MODE_MOUSEMOTION
) || (x
== ox
&& y
== oy
))
603 } else if(e
->xbutton
.type
== ButtonRelease
|| button
== AnyButton
) {
609 if(e
->xbutton
.type
== ButtonPress
) {
615 buf
[3] = 32 + button
+ (state
& ShiftMask
? 4 : 0)
616 + (state
& Mod4Mask
? 8 : 0)
617 + (state
& ControlMask
? 16 : 0);
619 ttywrite(buf
, sizeof(buf
));
624 if(IS_SET(MODE_MOUSE
))
626 else if(e
->xbutton
.button
== Button1
) {
629 tsetdirt(sel
.b
.y
, sel
.e
.y
);
633 sel
.ex
= sel
.bx
= X2COL(e
->xbutton
.x
);
634 sel
.ey
= sel
.by
= Y2ROW(e
->xbutton
.y
);
641 int x
, y
, bufsize
, is_selected
= 0;
647 bufsize
= (term
.col
+1) * (sel
.e
.y
-sel
.b
.y
+1) * UTF_SIZ
;
648 ptr
= str
= xmalloc(bufsize
);
650 /* append every set & selected glyph to the selection */
651 for(y
= 0; y
< term
.row
; y
++) {
652 for(x
= 0; x
< term
.col
; x
++) {
655 Glyph
*gp
= &term
.line
[y
][x
];
657 if(!(is_selected
= selected(x
, y
)))
659 p
= (gp
->state
& GLYPH_SET
) ? gp
->c
: " ";
661 memcpy(ptr
, p
, size
);
664 /* \n at the end of every selected line except for the last one */
665 if(is_selected
&& y
< sel
.e
.y
)
670 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
675 selnotify(XEvent
*e
) {
676 ulong nitems
, ofs
, rem
;
683 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
684 False
, AnyPropertyType
, &type
, &format
,
685 &nitems
, &rem
, &data
)) {
686 fprintf(stderr
, "Clipboard allocation failed\n");
689 ttywrite((const char *) data
, nitems
* format
/ 8);
691 /* number of 32-bit chunks returned */
692 ofs
+= nitems
* format
/ 32;
698 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
, xw
.win
, CurrentTime
);
701 void selclear(XEvent
*e
) {
705 tsetdirt(sel
.b
.y
, sel
.e
.y
);
709 selrequest(XEvent
*e
) {
710 XSelectionRequestEvent
*xsre
;
714 xsre
= (XSelectionRequestEvent
*) e
;
715 xev
.type
= SelectionNotify
;
716 xev
.requestor
= xsre
->requestor
;
717 xev
.selection
= xsre
->selection
;
718 xev
.target
= xsre
->target
;
719 xev
.time
= xsre
->time
;
723 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
724 if(xsre
->target
== xa_targets
) {
725 /* respond with the supported type */
726 Atom string
= sel
.xtarget
;
727 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
728 XA_ATOM
, 32, PropModeReplace
,
729 (uchar
*) &string
, 1);
730 xev
.property
= xsre
->property
;
731 } else if(xsre
->target
== sel
.xtarget
&& sel
.clip
!= NULL
) {
732 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
733 xsre
->target
, 8, PropModeReplace
,
734 (uchar
*) sel
.clip
, strlen(sel
.clip
));
735 xev
.property
= xsre
->property
;
738 /* all done, send a notification to the listener */
739 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
740 fprintf(stderr
, "Error sending SelectionNotify event\n");
745 /* register the selection for both the clipboard and the primary */
751 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
753 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
754 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
758 brelease(XEvent
*e
) {
759 if(IS_SET(MODE_MOUSE
)) {
763 if(e
->xbutton
.button
== Button2
)
765 else if(e
->xbutton
.button
== Button1
) {
767 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
768 term
.dirty
[sel
.ey
] = 1;
769 if(sel
.bx
== sel
.ex
&& sel
.by
== sel
.ey
) {
772 gettimeofday(&now
, NULL
);
774 if(TIMEDIFF(now
, sel
.tclick2
) <= TRIPLECLICK_TIMEOUT
) {
775 /* triple click on the line */
776 sel
.b
.x
= sel
.bx
= 0;
777 sel
.e
.x
= sel
.ex
= term
.col
;
778 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
780 } else if(TIMEDIFF(now
, sel
.tclick1
) <= DOUBLECLICK_TIMEOUT
) {
781 /* double click to select word */
783 while(sel
.bx
> 0 && term
.line
[sel
.ey
][sel
.bx
-1].state
& GLYPH_SET
&&
784 term
.line
[sel
.ey
][sel
.bx
-1].c
[0] != ' ') sel
.bx
--;
786 while(sel
.ex
< term
.col
-1 && term
.line
[sel
.ey
][sel
.ex
+1].state
& GLYPH_SET
&&
787 term
.line
[sel
.ey
][sel
.ex
+1].c
[0] != ' ') sel
.ex
++;
789 sel
.b
.y
= sel
.e
.y
= sel
.ey
;
795 memcpy(&sel
.tclick2
, &sel
.tclick1
, sizeof(struct timeval
));
796 gettimeofday(&sel
.tclick1
, NULL
);
801 if(IS_SET(MODE_MOUSE
)) {
806 int oldey
= sel
.ey
, oldex
= sel
.ex
;
807 getbuttoninfo(e
, NULL
, &sel
.ex
, &sel
.ey
);
809 if(oldey
!= sel
.ey
|| oldex
!= sel
.ex
) {
810 int starty
= MIN(oldey
, sel
.ey
);
811 int endy
= MAX(oldey
, sel
.ey
);
812 tsetdirt(starty
, endy
);
818 die(const char *errstr
, ...) {
821 va_start(ap
, errstr
);
822 vfprintf(stderr
, errstr
, ap
);
830 char *envshell
= getenv("SHELL");
836 signal(SIGCHLD
, SIG_DFL
);
837 signal(SIGHUP
, SIG_DFL
);
838 signal(SIGINT
, SIG_DFL
);
839 signal(SIGQUIT
, SIG_DFL
);
840 signal(SIGTERM
, SIG_DFL
);
841 signal(SIGALRM
, SIG_DFL
);
843 DEFAULT(envshell
, SHELL
);
844 putenv("TERM="TNAME
);
845 args
= opt_cmd
? opt_cmd
: (char*[]){envshell
, "-i", NULL
};
846 execvp(args
[0], args
);
854 if(waitpid(pid
, &stat
, 0) < 0)
855 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
857 if(WIFEXITED(stat
)) {
858 exit(WEXITSTATUS(stat
));
868 /* seems to work fine on linux, openbsd and freebsd */
869 struct winsize w
= {term
.row
, term
.col
, 0, 0};
870 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
871 die("openpty failed: %s\n", SERRNO
);
873 switch(pid
= fork()) {
875 die("fork failed\n");
878 setsid(); /* create a new process group */
879 dup2(s
, STDIN_FILENO
);
880 dup2(s
, STDOUT_FILENO
);
881 dup2(s
, STDERR_FILENO
);
882 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
883 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
891 signal(SIGCHLD
, sigchld
);
893 if(!strcmp(opt_io
, "-")) {
894 iofd
= STDOUT_FILENO
;
896 if((iofd
= open(opt_io
, O_WRONLY
| O_CREAT
, 0666)) < 0) {
897 fprintf(stderr
, "Error opening %s:%s\n",
898 opt_io
, strerror(errno
));
908 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
910 fprintf(stderr
, "\n");
915 static char buf
[BUFSIZ
];
916 static int buflen
= 0;
919 int charsize
; /* size of utf8 char in bytes */
923 /* append read bytes to unprocessed bytes */
924 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
925 die("Couldn't read from shell: %s\n", SERRNO
);
927 /* process every complete utf8 char */
930 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
931 charsize
= utf8decode(ptr
, &utf8c
);
932 utf8encode(&utf8c
, s
);
938 /* keep any uncomplete utf8 char for the next call */
939 memmove(buf
, ptr
, buflen
);
943 ttywrite(const char *s
, size_t n
) {
944 if(write(cmdfd
, s
, n
) == -1)
945 die("write error on tty: %s\n", SERRNO
);
956 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
957 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
961 tsetdirt(int top
, int bot
)
965 LIMIT(top
, 0, term
.row
-1);
966 LIMIT(bot
, 0, term
.row
-1);
968 for(i
= top
; i
<= bot
; i
++)
975 tsetdirt(0, term
.row
-1);
982 if(mode
== CURSOR_SAVE
)
984 else if(mode
== CURSOR_LOAD
)
985 term
.c
= c
, tmoveto(c
.x
, c
.y
);
995 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
997 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
998 for(i
= TAB
; i
< term
.col
; i
+= TAB
)
1000 term
.top
= 0, term
.bot
= term
.row
- 1;
1001 term
.mode
= MODE_WRAP
;
1003 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1007 tnew(int col
, int row
) {
1008 /* set screen size */
1009 term
.row
= row
, term
.col
= col
;
1010 term
.line
= xmalloc(term
.row
* sizeof(Line
));
1011 term
.alt
= xmalloc(term
.row
* sizeof(Line
));
1012 term
.dirty
= xmalloc(term
.row
* sizeof(*term
.dirty
));
1013 term
.tabs
= xmalloc(term
.col
* sizeof(*term
.tabs
));
1015 for(row
= 0; row
< term
.row
; row
++) {
1016 term
.line
[row
] = xmalloc(term
.col
* sizeof(Glyph
));
1017 term
.alt
[row
] = xmalloc(term
.col
* sizeof(Glyph
));
1018 term
.dirty
[row
] = 0;
1020 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1027 Line
* tmp
= term
.line
;
1028 term
.line
= term
.alt
;
1030 term
.mode
^= MODE_ALTSCREEN
;
1035 tscrolldown(int orig
, int n
) {
1039 LIMIT(n
, 0, term
.bot
-orig
+1);
1041 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
1043 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
1044 temp
= term
.line
[i
];
1045 term
.line
[i
] = term
.line
[i
-n
];
1046 term
.line
[i
-n
] = temp
;
1049 term
.dirty
[i
-n
] = 1;
1056 tscrollup(int orig
, int n
) {
1059 LIMIT(n
, 0, term
.bot
-orig
+1);
1061 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
1063 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
1064 temp
= term
.line
[i
];
1065 term
.line
[i
] = term
.line
[i
+n
];
1066 term
.line
[i
+n
] = temp
;
1069 term
.dirty
[i
+n
] = 1;
1072 selscroll(orig
, -n
);
1076 selscroll(int orig
, int n
) {
1080 if(BETWEEN(sel
.by
, orig
, term
.bot
) || BETWEEN(sel
.ey
, orig
, term
.bot
)) {
1081 if((sel
.by
+= n
) > term
.bot
|| (sel
.ey
+= n
) < term
.top
) {
1085 if(sel
.by
< term
.top
) {
1089 if(sel
.ey
> term
.bot
) {
1093 sel
.b
.y
= sel
.by
, sel
.b
.x
= sel
.bx
;
1094 sel
.e
.y
= sel
.ey
, sel
.e
.x
= sel
.ex
;
1099 tnewline(int first_col
) {
1102 tscrollup(term
.top
, 1);
1105 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1110 /* int noarg = 1; */
1111 char *p
= csiescseq
.buf
;
1115 csiescseq
.priv
= 1, p
++;
1117 while(p
< csiescseq
.buf
+csiescseq
.len
) {
1118 while(isdigit(*p
)) {
1119 csiescseq
.arg
[csiescseq
.narg
] *= 10;
1120 csiescseq
.arg
[csiescseq
.narg
] += *p
++ - '0'/*, noarg = 0 */;
1122 if(*p
== ';' && csiescseq
.narg
+1 < ESC_ARG_SIZ
)
1123 csiescseq
.narg
++, p
++;
1125 csiescseq
.mode
= *p
;
1133 tmoveto(int x
, int y
) {
1134 LIMIT(x
, 0, term
.col
-1);
1135 LIMIT(y
, 0, term
.row
-1);
1136 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1144 * The table is proudly stolen from rxvt.
1146 if(term
.c
.attr
.mode
& ATTR_GFX
) {
1147 char *vt100_0
[62] = { /* 0x41 - 0x7e */
1148 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1149 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1150 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1151 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1152 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1153 "", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1154 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1155 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1158 if(c
[0] >= 0x41 && c
[0] <= 0x7e
1159 && vt100_0
[c
[0] - 0x41]) {
1160 c
= vt100_0
[c
[0] - 0x41];
1164 term
.dirty
[term
.c
.y
] = 1;
1165 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
1166 memcpy(term
.line
[term
.c
.y
][term
.c
.x
].c
, c
, UTF_SIZ
);
1167 term
.line
[term
.c
.y
][term
.c
.x
].state
|= GLYPH_SET
;
1171 tclearregion(int x1
, int y1
, int x2
, int y2
) {
1175 temp
= x1
, x1
= x2
, x2
= temp
;
1177 temp
= y1
, y1
= y2
, y2
= temp
;
1179 LIMIT(x1
, 0, term
.col
-1);
1180 LIMIT(x2
, 0, term
.col
-1);
1181 LIMIT(y1
, 0, term
.row
-1);
1182 LIMIT(y2
, 0, term
.row
-1);
1184 for(y
= y1
; y
<= y2
; y
++) {
1186 for(x
= x1
; x
<= x2
; x
++)
1187 term
.line
[y
][x
].state
= 0;
1192 tdeletechar(int n
) {
1193 int src
= term
.c
.x
+ n
;
1195 int size
= term
.col
- src
;
1197 term
.dirty
[term
.c
.y
] = 1;
1199 if(src
>= term
.col
) {
1200 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1203 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
1204 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1208 tinsertblank(int n
) {
1211 int size
= term
.col
- dst
;
1213 term
.dirty
[term
.c
.y
] = 1;
1215 if(dst
>= term
.col
) {
1216 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1219 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
1220 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1224 tinsertblankline(int n
) {
1225 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1228 tscrolldown(term
.c
.y
, n
);
1232 tdeleteline(int n
) {
1233 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1236 tscrollup(term
.c
.y
, n
);
1240 tsetattr(int *attr
, int l
) {
1243 for(i
= 0; i
< l
; i
++) {
1246 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE
| ATTR_BOLD \
1247 | ATTR_ITALIC
| ATTR_BLINK
);
1248 term
.c
.attr
.fg
= DefaultFG
;
1249 term
.c
.attr
.bg
= DefaultBG
;
1252 term
.c
.attr
.mode
|= ATTR_BOLD
;
1254 case 3: /* enter standout (highlight) */
1255 term
.c
.attr
.mode
|= ATTR_ITALIC
;
1258 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1261 term
.c
.attr
.mode
|= ATTR_BLINK
;
1264 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1268 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
1270 case 23: /* leave standout (highlight) mode */
1271 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
1274 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1277 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
1280 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1283 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1285 if(BETWEEN(attr
[i
], 0, 255))
1286 term
.c
.attr
.fg
= attr
[i
];
1288 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[i
]);
1291 fprintf(stderr
, "erresc(38): gfx attr %d unknown\n", attr
[i
]);
1294 term
.c
.attr
.fg
= DefaultFG
;
1297 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1299 if(BETWEEN(attr
[i
], 0, 255))
1300 term
.c
.attr
.bg
= attr
[i
];
1302 fprintf(stderr
, "erresc: bad bgcolor %d\n", attr
[i
]);
1305 fprintf(stderr
, "erresc(48): gfx attr %d unknown\n", attr
[i
]);
1308 term
.c
.attr
.bg
= DefaultBG
;
1311 if(BETWEEN(attr
[i
], 30, 37))
1312 term
.c
.attr
.fg
= attr
[i
] - 30;
1313 else if(BETWEEN(attr
[i
], 40, 47))
1314 term
.c
.attr
.bg
= attr
[i
] - 40;
1315 else if(BETWEEN(attr
[i
], 90, 97))
1316 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1317 else if(BETWEEN(attr
[i
], 100, 107))
1318 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
1320 fprintf(stderr
, "erresc(default): gfx attr %d unknown\n", attr
[i
]), csidump();
1327 tsetscroll(int t
, int b
) {
1330 LIMIT(t
, 0, term
.row
-1);
1331 LIMIT(b
, 0, term
.row
-1);
1341 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
1344 tsetmode(bool priv
, bool set
, int *args
, int narg
) {
1347 for(lim
= args
+ narg
; args
< lim
; ++args
) {
1351 case 1: /* DECCKM -- Cursor key */
1352 MODBIT(term
.mode
, set
, MODE_APPKEYPAD
);
1354 case 5: /* DECSCNM -- Reverse video */
1356 MODBIT(term
.mode
, set
, MODE_REVERSE
);
1357 if(mode
!= term
.mode
)
1360 case 6: /* XXX: DECOM -- Origin */
1362 case 7: /* DECAWM -- Auto wrap */
1363 MODBIT(term
.mode
, set
, MODE_WRAP
);
1365 case 8: /* XXX: DECARM -- Auto repeat */
1367 case 0: /* Error (IGNORED) */
1368 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1371 MODBIT(term
.c
.state
, !set
, CURSOR_HIDE
);
1373 case 1000: /* 1000,1002: enable xterm mouse report */
1374 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
1377 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
1379 case 1049: /* = 1047 and 1048 */
1382 if(IS_SET(MODE_ALTSCREEN
))
1383 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1384 if((set
&& !IS_SET(MODE_ALTSCREEN
)) ||
1385 (!set
&& IS_SET(MODE_ALTSCREEN
))) {
1392 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1395 /* case 2: DECANM -- ANSI/VT52 (NOT SUPPOURTED) */
1396 /* case 3: DECCOLM -- Column (NOT SUPPORTED) */
1397 /* case 4: DECSCLM -- Scroll (NOT SUPPORTED) */
1398 /* case 18: DECPFF -- Printer feed (NOT SUPPORTED) */
1399 /* case 19: DECPEX -- Printer extent (NOT SUPPORTED) */
1400 /* case 42: DECNRCM -- National characters (NOT SUPPORTED) */
1402 "erresc: unknown private set/reset mode %d\n",
1408 case 0: /* Error (IGNORED) */
1410 case 2: /* KAM -- keyboard action */
1411 MODBIT(term
.mode
, set
, MODE_KBDLOCK
);
1413 case 4: /* IRM -- Insertion-replacement */
1414 MODBIT(term
.mode
, set
, MODE_INSERT
);
1416 case 12: /* XXX: SRM -- Send/Receive */
1418 case 20: /* LNM -- Linefeed/new line */
1419 MODBIT(term
.mode
, set
, MODE_CRLF
);
1423 "erresc: unknown set/reset mode %d\n",
1435 switch(csiescseq
.mode
) {
1438 fprintf(stderr
, "erresc: unknown csi ");
1442 case '@': /* ICH -- Insert <n> blank char */
1443 DEFAULT(csiescseq
.arg
[0], 1);
1444 tinsertblank(csiescseq
.arg
[0]);
1446 case 'A': /* CUU -- Cursor <n> Up */
1448 DEFAULT(csiescseq
.arg
[0], 1);
1449 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
1451 case 'B': /* CUD -- Cursor <n> Down */
1452 DEFAULT(csiescseq
.arg
[0], 1);
1453 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
1455 case 'C': /* CUF -- Cursor <n> Forward */
1457 DEFAULT(csiescseq
.arg
[0], 1);
1458 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
1460 case 'D': /* CUB -- Cursor <n> Backward */
1461 DEFAULT(csiescseq
.arg
[0], 1);
1462 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
1464 case 'E': /* CNL -- Cursor <n> Down and first col */
1465 DEFAULT(csiescseq
.arg
[0], 1);
1466 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
1468 case 'F': /* CPL -- Cursor <n> Up and first col */
1469 DEFAULT(csiescseq
.arg
[0], 1);
1470 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
1472 case 'g': /* TBC -- Tabulation clear */
1473 switch (csiescseq
.arg
[0]) {
1474 case 0: /* clear current tab stop */
1475 term
.tabs
[term
.c
.x
] = 0;
1477 case 3: /* clear all the tabs */
1478 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1484 case 'G': /* CHA -- Move to <col> */
1486 DEFAULT(csiescseq
.arg
[0], 1);
1487 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
1489 case 'H': /* CUP -- Move to <row> <col> */
1491 DEFAULT(csiescseq
.arg
[0], 1);
1492 DEFAULT(csiescseq
.arg
[1], 1);
1493 tmoveto(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
1495 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
1496 DEFAULT(csiescseq
.arg
[0], 1);
1497 while(csiescseq
.arg
[0]--)
1500 case 'J': /* ED -- Clear screen */
1502 switch(csiescseq
.arg
[0]) {
1504 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1505 if(term
.c
.y
< term
.row
-1)
1506 tclearregion(0, term
.c
.y
+1, term
.col
-1, term
.row
-1);
1510 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
1511 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1514 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1520 case 'K': /* EL -- Clear line */
1521 switch(csiescseq
.arg
[0]) {
1523 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1526 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1529 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
1533 case 'S': /* SU -- Scroll <n> line up */
1534 DEFAULT(csiescseq
.arg
[0], 1);
1535 tscrollup(term
.top
, csiescseq
.arg
[0]);
1537 case 'T': /* SD -- Scroll <n> line down */
1538 DEFAULT(csiescseq
.arg
[0], 1);
1539 tscrolldown(term
.top
, csiescseq
.arg
[0]);
1541 case 'L': /* IL -- Insert <n> blank lines */
1542 DEFAULT(csiescseq
.arg
[0], 1);
1543 tinsertblankline(csiescseq
.arg
[0]);
1545 case 'l': /* RM -- Reset Mode */
1546 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
1548 case 'M': /* DL -- Delete <n> lines */
1549 DEFAULT(csiescseq
.arg
[0], 1);
1550 tdeleteline(csiescseq
.arg
[0]);
1552 case 'X': /* ECH -- Erase <n> char */
1553 DEFAULT(csiescseq
.arg
[0], 1);
1554 tclearregion(term
.c
.x
, term
.c
.y
, term
.c
.x
+ csiescseq
.arg
[0], term
.c
.y
);
1556 case 'P': /* DCH -- Delete <n> char */
1557 DEFAULT(csiescseq
.arg
[0], 1);
1558 tdeletechar(csiescseq
.arg
[0]);
1560 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
1561 DEFAULT(csiescseq
.arg
[0], 1);
1562 while(csiescseq
.arg
[0]--)
1565 case 'd': /* VPA -- Move to <row> */
1566 DEFAULT(csiescseq
.arg
[0], 1);
1567 tmoveto(term
.c
.x
, csiescseq
.arg
[0]-1);
1569 case 'h': /* SM -- Set terminal mode */
1570 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
1572 case 'm': /* SGR -- Terminal attribute (color) */
1573 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
1575 case 'r': /* DECSTBM -- Set Scrolling Region */
1579 DEFAULT(csiescseq
.arg
[0], 1);
1580 DEFAULT(csiescseq
.arg
[1], term
.row
);
1581 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
1585 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1586 tcursor(CURSOR_SAVE
);
1588 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1589 tcursor(CURSOR_LOAD
);
1598 for(i
= 0; i
< csiescseq
.len
; i
++) {
1599 uint c
= csiescseq
.buf
[i
] & 0xff;
1600 if(isprint(c
)) putchar(c
);
1601 else if(c
== '\n') printf("(\\n)");
1602 else if(c
== '\r') printf("(\\r)");
1603 else if(c
== 0x1b) printf("(\\e)");
1604 else printf("(%02x)", c
);
1611 memset(&csiescseq
, 0, sizeof(csiescseq
));
1619 * TODO: make this being useful in case of color palette change.
1625 switch(strescseq
.type
) {
1626 case ']': /* OSC -- Operating System Command */
1632 * TODO: Handle special chars in string, like umlauts.
1635 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
+2);
1639 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
+1);
1641 case '4': /* TODO: Set color (arg0) to "rgb:%hexr/$hexg/$hexb" (arg1) */
1644 fprintf(stderr
, "erresc: unknown str ");
1649 case 'k': /* old title set compatibility */
1650 XStoreName(xw
.dpy
, xw
.win
, strescseq
.buf
);
1652 case 'P': /* DSC -- Device Control String */
1653 case '_': /* APC -- Application Program Command */
1654 case '^': /* PM -- Privacy Message */
1656 fprintf(stderr
, "erresc: unknown str ");
1666 * TODO: Implement parsing like for CSI when required.
1667 * Format: ESC type cmd ';' arg0 [';' argn] ESC \
1675 printf("ESC%c", strescseq
.type
);
1676 for(i
= 0; i
< strescseq
.len
; i
++) {
1677 uint c
= strescseq
.buf
[i
] & 0xff;
1678 if(isprint(c
)) putchar(c
);
1679 else if(c
== '\n') printf("(\\n)");
1680 else if(c
== '\r') printf("(\\r)");
1681 else if(c
== 0x1b) printf("(\\e)");
1682 else printf("(%02x)", c
);
1689 memset(&strescseq
, 0, sizeof(strescseq
));
1693 tputtab(bool forward
) {
1699 for(++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
1704 for(--x
; x
> 0 && !term
.tabs
[x
]; --x
)
1707 tmoveto(x
, term
.c
.y
);
1711 tputc(char *c
, int len
) {
1715 write(iofd
, c
, len
);
1722 tmoveto(term
.c
.x
-1, term
.c
.y
);
1725 tmoveto(0, term
.c
.y
);
1730 /* go to first col if the mode is set */
1731 tnewline(IS_SET(MODE_CRLF
));
1734 if(term
.esc
& ESC_STR
)
1737 if(!(xw
.state
& WIN_FOCUSED
))
1742 term
.esc
= ESC_START
;
1746 if(term
.esc
& ESC_START
) {
1747 if(term
.esc
& ESC_CSI
) {
1748 csiescseq
.buf
[csiescseq
.len
++] = ascii
;
1749 if(BETWEEN(ascii
, 0x40, 0x7E) || csiescseq
.len
>= ESC_BUF_SIZ
) {
1751 csiparse(), csihandle();
1753 } else if(term
.esc
& ESC_STR
) {
1756 term
.esc
= ESC_START
| ESC_STR_END
;
1758 case '\a': /* backwards compatibility to xterm */
1763 strescseq
.buf
[strescseq
.len
++] = ascii
;
1764 if(strescseq
.len
+1 >= STR_BUF_SIZ
) {
1769 } else if(term
.esc
& ESC_STR_END
) {
1773 } else if(term
.esc
& ESC_ALTCHARSET
) {
1775 case '0': /* Line drawing set */
1776 term
.c
.attr
.mode
|= ATTR_GFX
;
1778 case 'B': /* USASCII */
1779 term
.c
.attr
.mode
&= ~ATTR_GFX
;
1781 case 'A': /* UK (IGNORED) */
1782 case '<': /* multinational charset (IGNORED) */
1783 case '5': /* Finnish (IGNORED) */
1784 case 'C': /* Finnish (IGNORED) */
1785 case 'K': /* German (IGNORED) */
1788 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
1794 term
.esc
|= ESC_CSI
;
1796 case 'P': /* DCS -- Device Control String */
1797 case '_': /* APC -- Application Program Command */
1798 case '^': /* PM -- Privacy Message */
1799 case ']': /* OSC -- Operating System Command */
1800 case 'k': /* old title set compatibility */
1802 strescseq
.type
= ascii
;
1803 term
.esc
|= ESC_STR
;
1805 case '(': /* set primary charset G0 */
1806 term
.esc
|= ESC_ALTCHARSET
;
1808 case ')': /* set secondary charset G1 (IGNORED) */
1809 case '*': /* set tertiary charset G2 (IGNORED) */
1810 case '+': /* set quaternary charset G3 (IGNORED) */
1813 case 'D': /* IND -- Linefeed */
1814 if(term
.c
.y
== term
.bot
)
1815 tscrollup(term
.top
, 1);
1817 tmoveto(term
.c
.x
, term
.c
.y
+1);
1820 case 'E': /* NEL -- Next line */
1821 tnewline(1); /* always go to first col */
1824 case 'H': /* HTS -- Horizontal tab stop */
1825 term
.tabs
[term
.c
.x
] = 1;
1828 case 'M': /* RI -- Reverse index */
1829 if(term
.c
.y
== term
.top
)
1830 tscrolldown(term
.top
, 1);
1832 tmoveto(term
.c
.x
, term
.c
.y
-1);
1835 case 'c': /* RIS -- Reset to inital state */
1841 case '=': /* DECPAM -- Application keypad */
1842 term
.mode
|= MODE_APPKEYPAD
;
1845 case '>': /* DECPNM -- Normal keypad */
1846 term
.mode
&= ~MODE_APPKEYPAD
;
1849 case '7': /* DECSC -- Save Cursor */
1850 tcursor(CURSOR_SAVE
);
1853 case '8': /* DECRC -- Restore Cursor */
1854 tcursor(CURSOR_LOAD
);
1857 case '\\': /* ST -- Stop */
1861 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
1862 (uchar
) ascii
, isprint(ascii
)?ascii
:'.');
1867 if(sel
.bx
!= -1 && BETWEEN(term
.c
.y
, sel
.by
, sel
.ey
))
1869 if(ascii
>= '\020' || term
.c
.attr
.mode
& ATTR_GFX
) {
1870 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
1871 tnewline(1); /* always go to first col */
1873 if(term
.c
.x
+1 < term
.col
)
1874 tmoveto(term
.c
.x
+1, term
.c
.y
);
1876 term
.c
.state
|= CURSOR_WRAPNEXT
;
1882 tresize(int col
, int row
) {
1884 int minrow
= MIN(row
, term
.row
);
1885 int mincol
= MIN(col
, term
.col
);
1886 int slide
= term
.c
.y
- row
+ 1;
1888 if(col
< 1 || row
< 1)
1891 /* free unneeded rows */
1894 /* slide screen to keep cursor where we expect it -
1895 * tscrollup would work here, but we can optimize to
1896 * memmove because we're freeing the earlier lines */
1897 for(/* i = 0 */; i
< slide
; i
++) {
1901 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
1902 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
1904 for(i
+= row
; i
< term
.row
; i
++) {
1909 /* resize to new height */
1910 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
1911 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
1912 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
1913 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
1915 /* resize each row to new width, zero-pad if needed */
1916 for(i
= 0; i
< minrow
; i
++) {
1918 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
1919 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
1920 for(x
= mincol
; x
< col
; x
++) {
1921 term
.line
[i
][x
].state
= 0;
1922 term
.alt
[i
][x
].state
= 0;
1926 /* allocate any new rows */
1927 for(/* i == minrow */; i
< row
; i
++) {
1929 term
.line
[i
] = xcalloc(col
, sizeof(Glyph
));
1930 term
.alt
[i
] = xcalloc(col
, sizeof(Glyph
));
1932 if(col
> term
.col
) {
1933 bool *bp
= term
.tabs
+ term
.col
;
1935 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
1936 while(--bp
> term
.tabs
&& !*bp
)
1938 for(bp
+= TAB
; bp
< term
.tabs
+ col
; bp
+= TAB
)
1941 /* update terminal size */
1942 term
.col
= col
, term
.row
= row
;
1943 /* make use of the LIMIT in tmoveto */
1944 tmoveto(term
.c
.x
, term
.c
.y
);
1945 /* reset scrolling region */
1946 tsetscroll(0, row
-1);
1952 xresize(int col
, int row
) {
1953 xw
.tw
= MAX(1, 2*BORDER
+ col
* xw
.cw
);
1954 xw
.th
= MAX(1, 2*BORDER
+ row
* xw
.ch
);
1956 XftDrawChange(xw
.xft_draw
, xw
.buf
);
1962 XRenderColor xft_color
= { .alpha
= 0 };
1964 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
1965 for(i
= 0; i
< LEN(colorname
); i
++) {
1968 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, colorname
[i
], &dc
.xft_col
[i
])) {
1969 die("Could not allocate color '%s'\n", colorname
[i
]);
1973 /* load colors [16-255] ; same colors as xterm */
1974 for(i
= 16, r
= 0; r
< 6; r
++) {
1975 for(g
= 0; g
< 6; g
++) {
1976 for(b
= 0; b
< 6; b
++) {
1977 xft_color
.red
= r
== 0 ? 0 : 0x3737 + 0x2828 * r
;
1978 xft_color
.green
= g
== 0 ? 0 : 0x3737 + 0x2828 * g
;
1979 xft_color
.blue
= b
== 0 ? 0 : 0x3737 + 0x2828 * b
;
1980 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &xft_color
, &dc
.xft_col
[i
])) {
1981 die("Could not allocate color %d\n", i
);
1988 for(r
= 0; r
< 24; r
++, i
++) {
1989 xft_color
.red
= xft_color
.green
= xft_color
.blue
= 0x0808 + 0x0a0a * r
;
1990 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &xft_color
, &dc
.xft_col
[i
])) {
1991 die("Could not allocate color %d\n", i
);
1997 xtermclear(int col1
, int row1
, int col2
, int row2
) {
1998 XftDrawRect(xw
.xft_draw
,
1999 &dc
.xft_col
[IS_SET(MODE_REVERSE
) ? DefaultFG
: DefaultBG
],
2000 BORDER
+ col1
* xw
.cw
,
2001 BORDER
+ row1
* xw
.ch
,
2002 (col2
-col1
+1) * xw
.cw
,
2003 (row2
-row1
+1) * xw
.ch
);
2007 * Absolute coordinates.
2010 xclear(int x1
, int y1
, int x2
, int y2
) {
2011 XftDrawRect(xw
.xft_draw
,
2012 &dc
.xft_col
[IS_SET(MODE_REVERSE
) ? DefaultFG
: DefaultBG
],
2013 x1
, y1
, x2
-x1
, y2
-y1
);
2017 xclearborders(void) {
2018 /* top and left border */
2019 xclear(0, 0, BORDER
, xw
.h
);
2020 xclear(0, 0, xw
.w
, BORDER
);
2022 /* lower and right border */
2023 xclear(BORDER
, xw
.th
- 1, xw
.w
, xw
.h
);
2024 /* Will just draw what hasn't been drawn by the previous call. */
2025 xclear(xw
.tw
- 1, BORDER
, xw
.w
, xw
.h
- xw
.th
- 2);
2030 XClassHint
class = {opt_class
? opt_class
: TNAME
, TNAME
};
2031 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
2032 XSizeHints
*sizeh
= NULL
;
2034 sizeh
= XAllocSizeHints();
2035 if(xw
.isfixed
== False
) {
2036 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
2037 sizeh
->height
= xw
.h
;
2038 sizeh
->width
= xw
.w
;
2039 sizeh
->height_inc
= xw
.ch
;
2040 sizeh
->width_inc
= xw
.cw
;
2041 sizeh
->base_height
= 2*BORDER
;
2042 sizeh
->base_width
= 2*BORDER
;
2044 sizeh
->flags
= PMaxSize
| PMinSize
;
2045 sizeh
->min_width
= sizeh
->max_width
= xw
.fw
;
2046 sizeh
->min_height
= sizeh
->max_height
= xw
.fh
;
2049 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
, &class);
2054 xinitfont(Font
*f
, char *fontstr
) {
2055 FcPattern
*pattern
, *match
;
2058 pattern
= FcNameParse((FcChar8
*)fontstr
);
2060 die("st: can't open font %s\n", fontstr
);
2062 match
= XftFontMatch(xw
.dpy
, xw
.scr
, pattern
, &result
);
2063 FcPatternDestroy(pattern
);
2065 die("st: can't open font %s\n", fontstr
);
2066 if(!(f
->xft_set
= XftFontOpenPattern(xw
.dpy
, match
))) {
2067 FcPatternDestroy(match
);
2068 die("st: can't open font %s.\n", fontstr
);
2071 f
->ascent
= f
->xft_set
->ascent
;
2072 f
->descent
= f
->xft_set
->descent
;
2074 f
->rbearing
= f
->xft_set
->max_advance_width
;
2076 f
->height
= f
->xft_set
->height
;
2077 f
->width
= f
->lbearing
+ f
->rbearing
;
2081 initfonts(char *fontstr
) {
2084 xinitfont(&dc
.font
, fontstr
);
2085 xw
.cw
= dc
.font
.width
;
2086 xw
.ch
= dc
.font
.height
;
2088 fstr
= smstrcat(fontstr
, ":weight=bold", NULL
);
2089 xinitfont(&dc
.bfont
, fstr
);
2092 fstr
= smstrcat(fontstr
, ":slant=italic,oblique", NULL
);
2093 xinitfont(&dc
.ifont
, fstr
);
2096 fstr
= smstrcat(fontstr
, ":weight=bold:slant=italic,oblique", NULL
);
2097 xinitfont(&dc
.ibfont
, fstr
);
2103 XSetWindowAttributes attrs
;
2106 int sw
, sh
, major
, minor
;
2108 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
2109 die("Can't open display\n");
2110 xw
.scr
= XDefaultScreen(xw
.dpy
);
2111 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
2114 initfonts((opt_font
!= NULL
)? opt_font
: FONT
);
2117 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
2120 /* adjust fixed window geometry */
2122 sw
= DisplayWidth(xw
.dpy
, xw
.scr
);
2123 sh
= DisplayHeight(xw
.dpy
, xw
.scr
);
2125 xw
.fx
= sw
+ xw
.fx
- xw
.fw
- 1;
2127 xw
.fy
= sh
+ xw
.fy
- xw
.fh
- 1;
2132 /* window - default size */
2133 xw
.h
= 2*BORDER
+ term
.row
* xw
.ch
;
2134 xw
.w
= 2*BORDER
+ term
.col
* xw
.cw
;
2139 attrs
.background_pixel
= dc
.xft_col
[DefaultBG
].pixel
;
2140 attrs
.border_pixel
= dc
.xft_col
[DefaultBG
].pixel
;
2141 attrs
.bit_gravity
= NorthWestGravity
;
2142 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
2143 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
2144 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
2145 attrs
.colormap
= xw
.cmap
;
2147 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : XRootWindow(xw
.dpy
, xw
.scr
);
2148 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.fx
, xw
.fy
,
2149 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
2151 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
2155 /* double buffering */
2156 if(!XdbeQueryExtension(xw
.dpy
, &major
, &minor
))
2157 die("Xdbe extension is not present\n");
2158 xw
.buf
= XdbeAllocateBackBufferName(xw
.dpy
, xw
.win
, XdbeCopied
);
2160 /* Xft rendering context */
2161 xw
.xft_draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
2164 xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
);
2165 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
2166 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
2167 XNFocusWindow
, xw
.win
, NULL
);
2169 dc
.gc
= XCreateGC(xw
.dpy
, xw
.win
, 0, NULL
);
2171 /* white cursor, black outline */
2172 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
2173 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
2174 XRecolorCursor(xw
.dpy
, cursor
,
2175 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
2176 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
2178 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
2179 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
2180 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
2183 XMapWindow(xw
.dpy
, xw
.win
);
2189 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
2190 int winx
= BORDER
+x
*xw
.cw
, winy
= BORDER
+y
*xw
.ch
,
2191 width
= charlen
*xw
.cw
;
2192 Font
*font
= &dc
.font
;
2194 XftColor
*fg
= &dc
.xft_col
[base
.fg
], *bg
= &dc
.xft_col
[base
.bg
],
2195 *temp
, revfg
, revbg
;
2196 XRenderColor colfg
, colbg
;
2198 if(base
.mode
& ATTR_REVERSE
)
2199 temp
= fg
, fg
= bg
, bg
= temp
;
2201 if(base
.mode
& ATTR_BOLD
) {
2202 if(BETWEEN(base
.fg
, 0, 7)) {
2203 /* basic system colors */
2204 fg
= &dc
.xft_col
[base
.fg
+ 8];
2205 } else if(BETWEEN(base
.fg
, 16, 195)) {
2207 fg
= &dc
.xft_col
[base
.fg
+ 36];
2208 } else if(BETWEEN(base
.fg
, 232, 251)) {
2210 fg
= &dc
.xft_col
[base
.fg
+ 4];
2213 * Those ranges will not be brightened:
2214 * 8 - 15 – bright system colors
2215 * 196 - 231 – highest 256 color cube
2216 * 252 - 255 – brightest colors in grescale
2221 if(base
.mode
& ATTR_ITALIC
)
2223 if(base
.mode
& (ATTR_ITALIC
|ATTR_ITALIC
))
2226 if(IS_SET(MODE_REVERSE
)) {
2227 if(fg
== &dc
.xft_col
[DefaultFG
]) {
2228 fg
= &dc
.xft_col
[DefaultBG
];
2230 colfg
.red
= ~fg
->color
.red
;
2231 colfg
.green
= ~fg
->color
.green
;
2232 colfg
.blue
= ~fg
->color
.blue
;
2233 colfg
.alpha
= fg
->color
.alpha
;
2234 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
2238 if(bg
== &dc
.xft_col
[DefaultBG
]) {
2239 bg
= &dc
.xft_col
[DefaultFG
];
2241 colbg
.red
= ~bg
->color
.red
;
2242 colbg
.green
= ~bg
->color
.green
;
2243 colbg
.blue
= ~bg
->color
.blue
;
2244 colbg
.alpha
= bg
->color
.alpha
;
2245 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &revbg
);
2250 XftTextExtentsUtf8(xw
.dpy
, font
->xft_set
, (FcChar8
*)s
, bytelen
,
2252 width
= extents
.xOff
;
2254 XftDrawRect(xw
.xft_draw
, bg
, winx
, winy
, width
, xw
.ch
);
2255 XftDrawStringUtf8(xw
.xft_draw
, fg
, font
->xft_set
, winx
,
2256 winy
+ font
->ascent
, (FcChar8
*)s
, bytelen
);
2258 if(base
.mode
& ATTR_UNDERLINE
) {
2259 XftDrawRect(xw
.xft_draw
, fg
, winx
, winy
+ font
->ascent
+ 1,
2266 static int oldx
= 0;
2267 static int oldy
= 0;
2269 Glyph g
= {{' '}, ATTR_NULL
, DefaultBG
, DefaultCS
, 0};
2271 LIMIT(oldx
, 0, term
.col
-1);
2272 LIMIT(oldy
, 0, term
.row
-1);
2274 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& GLYPH_SET
)
2275 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
2277 /* remove the old cursor */
2278 if(term
.line
[oldy
][oldx
].state
& GLYPH_SET
) {
2279 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
2280 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
, oldy
, 1, sl
);
2282 xtermclear(oldx
, oldy
, oldx
, oldy
);
2284 /* draw the new one */
2285 if(!(term
.c
.state
& CURSOR_HIDE
)) {
2286 if(!(xw
.state
& WIN_FOCUSED
))
2289 if(IS_SET(MODE_REVERSE
))
2290 g
.mode
|= ATTR_REVERSE
, g
.fg
= DefaultCS
, g
.bg
= DefaultFG
;
2293 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
2294 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
2300 XStoreName(xw
.dpy
, xw
.win
, opt_title
? opt_title
: "st");
2305 struct timespec tv
= {0, REDRAW_TIMEOUT
* 1000};
2310 XSync(xw
.dpy
, False
); /* necessary for a good tput flash */
2311 nanosleep(&tv
, NULL
);
2316 XdbeSwapInfo swpinfo
[1] = {{xw
.win
, XdbeCopied
}};
2318 drawregion(0, 0, term
.col
, term
.row
);
2319 XdbeSwapBuffers(xw
.dpy
, swpinfo
, 1);
2323 drawregion(int x1
, int y1
, int x2
, int y2
) {
2324 int ic
, ib
, x
, y
, ox
, sl
;
2326 char buf
[DRAW_BUF_SIZ
];
2327 bool ena_sel
= sel
.bx
!= -1, alt
= IS_SET(MODE_ALTSCREEN
);
2329 if((sel
.alt
&& !alt
) || (!sel
.alt
&& alt
))
2331 if(!(xw
.state
& WIN_VISIBLE
))
2334 for(y
= y1
; y
< y2
; y
++) {
2337 xtermclear(0, y
, term
.col
, y
);
2339 base
= term
.line
[y
][0];
2341 for(x
= x1
; x
< x2
; x
++) {
2342 new = term
.line
[y
][x
];
2343 if(ena_sel
&& *(new.c
) && selected(x
, y
))
2344 new.mode
^= ATTR_REVERSE
;
2345 if(ib
> 0 && (!(new.state
& GLYPH_SET
) || ATTRCMP(base
, new) ||
2346 ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
2347 xdraws(buf
, base
, ox
, y
, ic
, ib
);
2350 if(new.state
& GLYPH_SET
) {
2355 sl
= utf8size(new.c
);
2356 memcpy(buf
+ib
, new.c
, sl
);
2362 xdraws(buf
, base
, ox
, y
, ic
, ib
);
2368 expose(XEvent
*ev
) {
2369 XExposeEvent
*e
= &ev
->xexpose
;
2370 if(xw
.state
& WIN_REDRAW
) {
2372 xw
.state
&= ~WIN_REDRAW
;
2377 visibility(XEvent
*ev
) {
2378 XVisibilityEvent
*e
= &ev
->xvisibility
;
2379 if(e
->state
== VisibilityFullyObscured
)
2380 xw
.state
&= ~WIN_VISIBLE
;
2381 else if(!(xw
.state
& WIN_VISIBLE
))
2382 /* need a full redraw for next Expose, not just a buf copy */
2383 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
2388 xw
.state
&= ~WIN_VISIBLE
;
2392 xseturgency(int add
) {
2393 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
2394 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
2395 XSetWMHints(xw
.dpy
, xw
.win
, h
);
2401 if(ev
->type
== FocusIn
) {
2402 xw
.state
|= WIN_FOCUSED
;
2405 xw
.state
&= ~WIN_FOCUSED
;
2409 kmap(KeySym k
, uint state
) {
2412 for(i
= 0; i
< LEN(key
); i
++) {
2413 uint mask
= key
[i
].mask
;
2414 if(key
[i
].k
== k
&& ((state
& mask
) == mask
|| (mask
== XK_NO_MOD
&& !state
)))
2415 return (char*)key
[i
].s
;
2421 kpress(XEvent
*ev
) {
2422 XKeyEvent
*e
= &ev
->xkey
;
2431 if (IS_SET(MODE_KBDLOCK
))
2433 meta
= e
->state
& Mod1Mask
;
2434 shift
= e
->state
& ShiftMask
;
2435 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof(buf
), &ksym
, &status
);
2437 /* 1. custom keys from config.h */
2438 if((customkey
= kmap(ksym
, e
->state
)))
2439 ttywrite(customkey
, strlen(customkey
));
2440 /* 2. hardcoded (overrides X lookup) */
2447 /* XXX: shift up/down doesn't work */
2448 sprintf(buf
, "\033%c%c", IS_SET(MODE_APPKEYPAD
) ? 'O' : '[', (shift
? "dacb":"DACB")[ksym
- XK_Left
]);
2456 if(IS_SET(MODE_CRLF
))
2457 ttywrite("\r\n", 2);
2464 if(meta
&& len
== 1)
2465 ttywrite("\033", 1);
2473 cmessage(XEvent
*e
) {
2475 http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html */
2476 if(e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
2477 if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
2478 xw
.state
|= WIN_FOCUSED
;
2480 } else if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
2481 xw
.state
&= ~WIN_FOCUSED
;
2483 } else if(e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
2484 /* Send SIGHUP to shell */
2494 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
2497 xw
.w
= e
->xconfigure
.width
;
2498 xw
.h
= e
->xconfigure
.height
;
2499 col
= (xw
.w
- 2*BORDER
) / xw
.cw
;
2500 row
= (xw
.h
- 2*BORDER
) / xw
.ch
;
2501 if(col
== term
.col
&& row
== term
.row
)
2514 int xfd
= XConnectionNumber(xw
.dpy
), i
;
2515 struct timeval drawtimeout
, *tv
= NULL
;
2519 FD_SET(cmdfd
, &rfd
);
2521 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
) < 0) {
2524 die("select failed: %s\n", SERRNO
);
2528 * Stop after a certain number of reads so the user does not
2529 * feel like the system is stuttering.
2531 if(i
< 1000 && FD_ISSET(cmdfd
, &rfd
)) {
2535 * Just wait a bit so it isn't disturbing the
2536 * user and the system is able to write something.
2538 drawtimeout
.tv_sec
= 0;
2539 drawtimeout
.tv_usec
= 5;
2546 while(XPending(xw
.dpy
)) {
2547 XNextEvent(xw
.dpy
, &ev
);
2548 if(XFilterEvent(&ev
, xw
.win
))
2550 if(handler
[ev
.type
])
2551 (handler
[ev
.type
])(&ev
);
2560 main(int argc
, char *argv
[]) {
2561 int i
, bitm
, xr
, yr
;
2564 xw
.fw
= xw
.fh
= xw
.fx
= xw
.fy
= 0;
2567 for(i
= 1; i
< argc
; i
++) {
2568 switch(argv
[i
][0] != '-' || argv
[i
][2] ? -1 : argv
[i
][1]) {
2571 opt_class
= argv
[i
];
2574 /* eat every remaining arguments */
2586 bitm
= XParseGeometry(argv
[i
], &xr
, &yr
, &wr
, &hr
);
2591 if(bitm
& WidthValue
)
2593 if(bitm
& HeightValue
)
2595 if(bitm
& XNegative
&& xw
.fx
== 0)
2597 if(bitm
& XNegative
&& xw
.fy
== 0)
2600 if(xw
.fh
!= 0 && xw
.fw
!= 0)
2609 opt_title
= argv
[i
];
2616 opt_embed
= argv
[i
];
2622 setlocale(LC_CTYPE
, "");