1 /* See LICENSE for licence details. */
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/Xft/Xft.h>
28 #include <fontconfig/fontconfig.h>
36 #define Draw XftDraw *
37 #define Colour XftColor
38 #define Colourmap Colormap
42 #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
44 #elif defined(__FreeBSD__) || defined(__DragonFly__)
50 #define XEMBED_FOCUS_IN 4
51 #define XEMBED_FOCUS_OUT 5
55 #define ESC_BUF_SIZ (128*UTF_SIZ)
56 #define ESC_ARG_SIZ 16
57 #define STR_BUF_SIZ ESC_BUF_SIZ
58 #define STR_ARG_SIZ ESC_ARG_SIZ
59 #define DRAW_BUF_SIZ 20*1024
60 #define XK_ANY_MOD UINT_MAX
62 #define XK_SWITCH_MOD (1<<13)
64 #define REDRAW_TIMEOUT (80*1000) /* 80 ms */
67 #define SERRNO strerror(errno)
68 #define MIN(a, b) ((a) < (b) ? (a) : (b))
69 #define MAX(a, b) ((a) < (b) ? (b) : (a))
70 #define LEN(a) (sizeof(a) / sizeof(a[0]))
71 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
72 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
73 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
74 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
75 #define IS_SET(flag) ((term.mode & (flag)) != 0)
76 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
78 #define VT102ID "\033[?6c"
80 enum glyph_attribute
{
90 enum cursor_movement
{
108 MODE_MOUSEMOTION
= 64,
114 MODE_APPCURSOR
= 2048,
115 MODE_MOUSESGR
= 4096,
121 ESC_STR
= 4, /* DSC, OSC, PM, APC */
123 ESC_STR_END
= 16, /* a final string was encountered */
124 ESC_TEST
= 32, /* Enter in test mode */
133 enum selection_type
{
138 enum selection_snap
{
145 enum { B0
=1, B1
=2, B2
=4, B3
=8, B4
=16, B5
=32, B6
=64, B7
=128 };
147 typedef unsigned char uchar
;
148 typedef unsigned int uint
;
149 typedef unsigned long ulong
;
150 typedef unsigned short ushort
;
153 char c
[UTF_SIZ
]; /* character code */
154 uchar mode
; /* attribute flags */
155 ushort fg
; /* foreground */
156 ushort bg
; /* background */
162 Glyph attr
; /* current char attributes */
168 /* CSI Escape sequence structs */
169 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
171 char buf
[ESC_BUF_SIZ
]; /* raw string */
172 int len
; /* raw string length */
174 int arg
[ESC_ARG_SIZ
];
175 int narg
; /* nb of args */
179 /* STR Escape sequence structs */
180 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
182 char type
; /* ESC type ... */
183 char buf
[STR_BUF_SIZ
]; /* raw string */
184 int len
; /* raw string length */
185 char *args
[STR_ARG_SIZ
];
186 int narg
; /* nb of args */
189 /* Internal representation of the screen */
191 int row
; /* nb row */
192 int col
; /* nb col */
193 Line
*line
; /* screen */
194 Line
*alt
; /* alternate screen */
195 bool *dirty
; /* dirtyness of lines */
196 TCursor c
; /* cursor */
197 int top
; /* top scroll limit */
198 int bot
; /* bottom scroll limit */
199 int mode
; /* terminal mode flags */
200 int esc
; /* escape state flags */
201 bool numlock
; /* lock numbers in keyboard */
205 /* Purely graphic info */
211 Atom xembed
, wmdeletewin
;
217 bool isfixed
; /* is fixed geometry? */
218 int fx
, fy
, fw
, fh
; /* fixed geometry */
219 int tw
, th
; /* tty width and height */
220 int w
, h
; /* window width and height */
221 int ch
; /* char height */
222 int cw
; /* char width */
223 char state
; /* focus, redraw, visible */
230 /* three valued logic variables: 0 indifferent, 1 on, -1 off */
231 signed char appkey
; /* application keypad */
232 signed char appcursor
; /* application cursor */
233 signed char crlf
; /* crlf mode */
236 /* TODO: use better name for vars... */
249 struct timeval tclick1
;
250 struct timeval tclick2
;
263 void (*func
)(const Arg
*);
267 /* function definitions used in config.h */
268 static void clippaste(const Arg
*);
269 static void numlock(const Arg
*);
270 static void selpaste(const Arg
*);
271 static void xzoom(const Arg
*);
273 /* Config.h for applying patches and the configuration. */
289 /* Drawing Context */
291 Colour col
[LEN(colorname
) < 256 ? 256 : LEN(colorname
)];
292 Font font
, bfont
, ifont
, ibfont
;
296 static void die(const char *, ...);
297 static void draw(void);
298 static void redraw(int);
299 static void drawregion(int, int, int, int);
300 static void execsh(void);
301 static void sigchld(int);
302 static void run(void);
304 static void csidump(void);
305 static void csihandle(void);
306 static void csiparse(void);
307 static void csireset(void);
308 static void strdump(void);
309 static void strhandle(void);
310 static void strparse(void);
311 static void strreset(void);
313 static void tclearregion(int, int, int, int);
314 static void tcursor(int);
315 static void tdeletechar(int);
316 static void tdeleteline(int);
317 static void tinsertblank(int);
318 static void tinsertblankline(int);
319 static void tmoveto(int, int);
320 static void tmoveato(int x
, int y
);
321 static void tnew(int, int);
322 static void tnewline(int);
323 static void tputtab(bool);
324 static void tputc(char *, int);
325 static void treset(void);
326 static int tresize(int, int);
327 static void tscrollup(int, int);
328 static void tscrolldown(int, int);
329 static void tsetattr(int*, int);
330 static void tsetchar(char *, Glyph
*, int, int);
331 static void tsetscroll(int, int);
332 static void tswapscreen(void);
333 static void tsetdirt(int, int);
334 static void tsetmode(bool, bool, int *, int);
335 static void tfulldirt(void);
336 static void techo(char *, int);
338 static inline bool match(uint
, uint
);
339 static void ttynew(void);
340 static void ttyread(void);
341 static void ttyresize(void);
342 static void ttywrite(const char *, size_t);
344 static void xdraws(char *, Glyph
, int, int, int, int);
345 static void xhints(void);
346 static void xclear(int, int, int, int);
347 static void xdrawcursor(void);
348 static void xinit(void);
349 static void xloadcols(void);
350 static int xsetcolorname(int, const char *);
351 static int xloadfont(Font
*, FcPattern
*);
352 static void xloadfonts(char *, int);
353 static void xsettitle(char *);
354 static void xresettitle(void);
355 static void xseturgency(int);
356 static void xsetsel(char*);
357 static void xtermclear(int, int, int, int);
358 static void xunloadfonts(void);
359 static void xresize(int, int);
361 static void expose(XEvent
*);
362 static void visibility(XEvent
*);
363 static void unmap(XEvent
*);
364 static char *kmap(KeySym
, uint
);
365 static void kpress(XEvent
*);
366 static void cmessage(XEvent
*);
367 static void cresize(int, int);
368 static void resize(XEvent
*);
369 static void focus(XEvent
*);
370 static void brelease(XEvent
*);
371 static void bpress(XEvent
*);
372 static void bmotion(XEvent
*);
373 static void selnotify(XEvent
*);
374 static void selclear(XEvent
*);
375 static void selrequest(XEvent
*);
377 static void selinit(void);
378 static inline bool selected(int, int);
379 static void selcopy(void);
380 static void selscroll(int, int);
381 static void selsnap(int, int *, int *, int);
383 static int utf8decode(char *, long *);
384 static int utf8encode(long *, char *);
385 static int utf8size(char *);
386 static int isfullutf8(char *, int);
388 static ssize_t
xwrite(int, char *, size_t);
389 static void *xmalloc(size_t);
390 static void *xrealloc(void *, size_t);
391 static void *xcalloc(size_t, size_t);
393 static void (*handler
[LASTEvent
])(XEvent
*) = {
395 [ClientMessage
] = cmessage
,
396 [ConfigureNotify
] = resize
,
397 [VisibilityNotify
] = visibility
,
398 [UnmapNotify
] = unmap
,
402 [MotionNotify
] = bmotion
,
403 [ButtonPress
] = bpress
,
404 [ButtonRelease
] = brelease
,
405 [SelectionClear
] = selclear
,
406 [SelectionNotify
] = selnotify
,
407 [SelectionRequest
] = selrequest
,
414 static CSIEscape csiescseq
;
415 static STREscape strescseq
;
418 static Selection sel
;
419 static int iofd
= -1;
420 static char **opt_cmd
= NULL
;
421 static char *opt_io
= NULL
;
422 static char *opt_title
= NULL
;
423 static char *opt_embed
= NULL
;
424 static char *opt_class
= NULL
;
425 static char *opt_font
= NULL
;
427 static char *usedfont
= NULL
;
428 static int usedfontsize
= 0;
430 /* Font Ring Cache */
445 * Fontcache is a ring buffer, with frccur as current position and frclen as
446 * the current length of used elements.
449 static Fontcache frc
[1024];
450 static int frccur
= -1, frclen
= 0;
453 xwrite(int fd
, char *s
, size_t len
) {
457 ssize_t r
= write(fd
, s
, len
);
467 xmalloc(size_t len
) {
468 void *p
= malloc(len
);
471 die("Out of memory\n");
477 xrealloc(void *p
, size_t len
) {
478 if((p
= realloc(p
, len
)) == NULL
)
479 die("Out of memory\n");
485 xcalloc(size_t nmemb
, size_t size
) {
486 void *p
= calloc(nmemb
, size
);
489 die("Out of memory\n");
495 utf8decode(char *s
, long *u
) {
501 if(~c
& B7
) { /* 0xxxxxxx */
504 } else if((c
& (B7
|B6
|B5
)) == (B7
|B6
)) { /* 110xxxxx */
505 *u
= c
&(B4
|B3
|B2
|B1
|B0
);
507 } else if((c
& (B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) { /* 1110xxxx */
508 *u
= c
&(B3
|B2
|B1
|B0
);
510 } else if((c
& (B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
)) { /* 11110xxx */
517 for(i
= n
, ++s
; i
> 0; --i
, ++rtn
, ++s
) {
519 if((c
& (B7
|B6
)) != B7
) /* 10xxxxxx */
522 *u
|= c
& (B5
|B4
|B3
|B2
|B1
|B0
);
525 if((n
== 1 && *u
< 0x80) ||
526 (n
== 2 && *u
< 0x800) ||
527 (n
== 3 && *u
< 0x10000) ||
528 (*u
>= 0xD800 && *u
<= 0xDFFF)) {
540 utf8encode(long *u
, char *s
) {
548 *sp
= uc
; /* 0xxxxxxx */
550 } else if(*u
< 0x800) {
551 *sp
= (uc
>> 6) | (B7
|B6
); /* 110xxxxx */
553 } else if(uc
< 0x10000) {
554 *sp
= (uc
>> 12) | (B7
|B6
|B5
); /* 1110xxxx */
556 } else if(uc
<= 0x10FFFF) {
557 *sp
= (uc
>> 18) | (B7
|B6
|B5
|B4
); /* 11110xxx */
563 for(i
=n
,++sp
; i
>0; --i
,++sp
)
564 *sp
= ((uc
>> 6*(i
-1)) & (B5
|B4
|B3
|B2
|B1
|B0
)) | B7
; /* 10xxxxxx */
576 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
577 UTF-8 otherwise return 0 */
579 isfullutf8(char *s
, int b
) {
587 } else if((*c1
&(B7
|B6
|B5
)) == (B7
|B6
) && b
== 1) {
589 } else if((*c1
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
) &&
591 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
))) {
593 } else if((*c1
&(B7
|B6
|B5
|B4
|B3
)) == (B7
|B6
|B5
|B4
) &&
595 ((b
== 2) && (*c2
&(B7
|B6
)) == B7
) ||
596 ((b
== 3) && (*c2
&(B7
|B6
)) == B7
&& (*c3
&(B7
|B6
)) == B7
))) {
609 } else if((c
&(B7
|B6
|B5
)) == (B7
|B6
)) {
611 } else if((c
&(B7
|B6
|B5
|B4
)) == (B7
|B6
|B5
)) {
620 memset(&sel
.tclick1
, 0, sizeof(sel
.tclick1
));
621 memset(&sel
.tclick2
, 0, sizeof(sel
.tclick2
));
625 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
626 if(sel
.xtarget
== None
)
627 sel
.xtarget
= XA_STRING
;
635 return LIMIT(x
, 0, term
.col
-1);
643 return LIMIT(y
, 0, term
.row
-1);
647 selected(int x
, int y
) {
650 if(sel
.ey
== y
&& sel
.by
== y
) {
651 bx
= MIN(sel
.bx
, sel
.ex
);
652 ex
= MAX(sel
.bx
, sel
.ex
);
654 return BETWEEN(x
, bx
, ex
);
657 if(sel
.type
== SEL_RECTANGULAR
) {
658 return ((sel
.b
.y
<= y
&& y
<= sel
.e
.y
)
659 && (sel
.b
.x
<= x
&& x
<= sel
.e
.x
));
661 return ((sel
.b
.y
< y
&& y
< sel
.e
.y
)
662 || (y
== sel
.e
.y
&& x
<= sel
.e
.x
))
663 || (y
== sel
.b
.y
&& x
>= sel
.b
.x
664 && (x
<= sel
.e
.x
|| sel
.b
.y
!= sel
.e
.y
));
668 selsnap(int mode
, int *x
, int *y
, int direction
) {
671 while(*x
> 0 && *x
< term
.col
-1 && term
.line
[*y
][*x
+ direction
].c
[0] != ' ') {
677 *x
= (direction
< 0) ? 0 : term
.col
- 1;
687 getbuttoninfo(XEvent
*e
) {
689 uint state
= e
->xbutton
.state
&~Button1Mask
;
691 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
693 sel
.ex
= x2col(e
->xbutton
.x
);
694 sel
.ey
= y2row(e
->xbutton
.y
);
697 || (sel
.by
== sel
.ey
&& sel
.bx
< sel
.ex
)) {
698 selsnap(sel
.snap
, &sel
.bx
, &sel
.by
, -1);
699 selsnap(sel
.snap
, &sel
.ex
, &sel
.ey
, +1);
701 selsnap(sel
.snap
, &sel
.ex
, &sel
.ey
, -1);
702 selsnap(sel
.snap
, &sel
.bx
, &sel
.by
, +1);
705 sel
.b
.x
= sel
.by
< sel
.ey
? sel
.bx
: sel
.ex
;
706 sel
.b
.y
= MIN(sel
.by
, sel
.ey
);
707 sel
.e
.x
= sel
.by
< sel
.ey
? sel
.ex
: sel
.bx
;
708 sel
.e
.y
= MAX(sel
.by
, sel
.ey
);
710 sel
.type
= SEL_REGULAR
;
711 for(type
= 1; type
< LEN(selmasks
); ++type
) {
712 if(match(selmasks
[type
], state
)) {
720 mousereport(XEvent
*e
) {
721 int x
= x2col(e
->xbutton
.x
), y
= y2row(e
->xbutton
.y
),
722 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
,
725 static int ob
, ox
, oy
;
728 if(e
->xbutton
.type
== MotionNotify
) {
729 if(!IS_SET(MODE_MOUSEMOTION
) || (x
== ox
&& y
== oy
))
733 } else if(!IS_SET(MODE_MOUSESGR
)
734 && (e
->xbutton
.type
== ButtonRelease
735 || button
== AnyButton
)) {
741 if(e
->xbutton
.type
== ButtonPress
) {
747 button
+= (state
& ShiftMask
? 4 : 0)
748 + (state
& Mod4Mask
? 8 : 0)
749 + (state
& ControlMask
? 16 : 0);
752 if(IS_SET(MODE_MOUSESGR
)) {
753 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
755 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
756 } else if(x
< 223 && y
< 223) {
757 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
758 32+button
, 32+x
+1, 32+y
+1);
770 if(IS_SET(MODE_MOUSE
)) {
772 } else if(e
->xbutton
.button
== Button1
) {
773 gettimeofday(&now
, NULL
);
774 /* Clear previous selection, logically and visually. */
777 tsetdirt(sel
.b
.y
, sel
.e
.y
);
781 sel
.type
= SEL_REGULAR
;
782 sel
.ex
= sel
.bx
= x2col(e
->xbutton
.x
);
783 sel
.ey
= sel
.by
= y2row(e
->xbutton
.y
);
786 * If user clicks are fasst enough (e.g. below timeouts),
787 * we ignore if his hand slipped left or down and accidentally selected more;
788 * we are just snapping to whatever we're snapping.
790 if(TIMEDIFF(now
, sel
.tclick2
) <= tripleclicktimeout
) {
792 sel
.snap
= SNAP_LINE
;
793 } else if(TIMEDIFF(now
, sel
.tclick1
) <= doubleclicktimeout
) {
794 sel
.snap
= SNAP_WORD
;
798 selsnap(sel
.snap
, &sel
.bx
, &sel
.by
, -1);
799 selsnap(sel
.snap
, &sel
.ex
, &sel
.ey
, 1);
800 sel
.b
.x
= sel
.bx
, sel
.b
.y
= sel
.by
, sel
.e
.x
= sel
.ex
, sel
.e
.y
= sel
.ey
;
801 /* Draw selection, unless it's regular and we don't want to make clicks visible */
803 tsetdirt(sel
.b
.y
, sel
.e
.y
);
806 sel
.tclick2
= sel
.tclick1
;
808 } else if(e
->xbutton
.button
== Button4
) {
810 } else if(e
->xbutton
.button
== Button5
) {
818 int x
, y
, bufsize
, size
;
824 bufsize
= (term
.col
+1) * (sel
.e
.y
-sel
.b
.y
+1) * UTF_SIZ
;
825 ptr
= str
= xmalloc(bufsize
);
827 /* append every set & selected glyph to the selection */
828 for(y
= sel
.b
.y
; y
< sel
.e
.y
+ 1; y
++) {
829 gp
= &term
.line
[y
][0];
830 last
= gp
+ term
.col
;
832 while(--last
>= gp
&& !(selected(last
- gp
, y
) && \
833 strcmp(last
->c
, " ") != 0))
836 for(x
= 0; gp
<= last
; x
++, ++gp
) {
837 if(!selected(x
, y
)) {
841 size
= utf8size(gp
->c
);
842 memcpy(ptr
, gp
->c
, size
);
847 * Copy and pasting of line endings is inconsistent
848 * in the inconsistent terminal and GUI world.
849 * The best solution seems like to produce '\n' when
850 * something is copied from st and convert '\n' to
851 * '\r', when something to be pasted is received by
853 * FIXME: Fix the computer world.
864 selnotify(XEvent
*e
) {
865 ulong nitems
, ofs
, rem
;
867 uchar
*data
, *last
, *repl
;
872 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
873 False
, AnyPropertyType
, &type
, &format
,
874 &nitems
, &rem
, &data
)) {
875 fprintf(stderr
, "Clipboard allocation failed\n");
880 * As seen in selcopy:
881 * Line endings are inconsistent in the terminal and GUI world
882 * copy and pasting. When receiving some selection data,
883 * replace all '\n' with '\r'.
884 * FIXME: Fix the computer world.
887 last
= data
+ nitems
* format
/ 8;
888 while((repl
= memchr(repl
, '\n', last
- repl
))) {
892 ttywrite((const char *)data
, nitems
* format
/ 8);
894 /* number of 32-bit chunks returned */
895 ofs
+= nitems
* format
/ 32;
900 selpaste(const Arg
*dummy
) {
901 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
,
902 xw
.win
, CurrentTime
);
906 clippaste(const Arg
*dummy
) {
909 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
910 XConvertSelection(xw
.dpy
, clipboard
, sel
.xtarget
, XA_PRIMARY
,
911 xw
.win
, CurrentTime
);
915 selclear(XEvent
*e
) {
919 tsetdirt(sel
.b
.y
, sel
.e
.y
);
923 selrequest(XEvent
*e
) {
924 XSelectionRequestEvent
*xsre
;
926 Atom xa_targets
, string
;
928 xsre
= (XSelectionRequestEvent
*) e
;
929 xev
.type
= SelectionNotify
;
930 xev
.requestor
= xsre
->requestor
;
931 xev
.selection
= xsre
->selection
;
932 xev
.target
= xsre
->target
;
933 xev
.time
= xsre
->time
;
937 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
938 if(xsre
->target
== xa_targets
) {
939 /* respond with the supported type */
940 string
= sel
.xtarget
;
941 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
942 XA_ATOM
, 32, PropModeReplace
,
943 (uchar
*) &string
, 1);
944 xev
.property
= xsre
->property
;
945 } else if(xsre
->target
== sel
.xtarget
&& sel
.clip
!= NULL
) {
946 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
947 xsre
->target
, 8, PropModeReplace
,
948 (uchar
*) sel
.clip
, strlen(sel
.clip
));
949 xev
.property
= xsre
->property
;
952 /* all done, send a notification to the listener */
953 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
954 fprintf(stderr
, "Error sending SelectionNotify event\n");
959 /* register the selection for both the clipboard and the primary */
965 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
967 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
968 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
972 brelease(XEvent
*e
) {
973 if(IS_SET(MODE_MOUSE
)) {
978 if(e
->xbutton
.button
== Button2
) {
980 } else if(e
->xbutton
.button
== Button1
) {
983 term
.dirty
[sel
.ey
] = 1;
984 if(sel
.bx
== sel
.ex
&& sel
.by
== sel
.ey
) {
994 int oldey
, oldex
, oldsby
, oldsey
;
996 if(IS_SET(MODE_MOUSE
)) {
1010 if(oldey
!= sel
.ey
|| oldex
!= sel
.ex
) {
1011 tsetdirt(MIN(sel
.b
.y
, oldsby
), MAX(sel
.e
.y
, oldsey
));
1016 die(const char *errstr
, ...) {
1019 va_start(ap
, errstr
);
1020 vfprintf(stderr
, errstr
, ap
);
1028 char *envshell
= getenv("SHELL");
1029 const struct passwd
*pass
= getpwuid(getuid());
1030 char buf
[sizeof(long) * 8 + 1];
1032 unsetenv("COLUMNS");
1034 unsetenv("TERMCAP");
1037 setenv("LOGNAME", pass
->pw_name
, 1);
1038 setenv("USER", pass
->pw_name
, 1);
1039 setenv("SHELL", pass
->pw_shell
, 0);
1040 setenv("HOME", pass
->pw_dir
, 0);
1043 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1044 setenv("WINDOWID", buf
, 1);
1046 signal(SIGCHLD
, SIG_DFL
);
1047 signal(SIGHUP
, SIG_DFL
);
1048 signal(SIGINT
, SIG_DFL
);
1049 signal(SIGQUIT
, SIG_DFL
);
1050 signal(SIGTERM
, SIG_DFL
);
1051 signal(SIGALRM
, SIG_DFL
);
1053 DEFAULT(envshell
, shell
);
1054 setenv("TERM", termname
, 1);
1055 args
= opt_cmd
? opt_cmd
: (char *[]){envshell
, "-i", NULL
};
1056 execvp(args
[0], args
);
1064 if(waitpid(pid
, &stat
, 0) < 0)
1065 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
1067 if(WIFEXITED(stat
)) {
1068 exit(WEXITSTATUS(stat
));
1077 struct winsize w
= {term
.row
, term
.col
, 0, 0};
1079 /* seems to work fine on linux, openbsd and freebsd */
1080 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
1081 die("openpty failed: %s\n", SERRNO
);
1083 switch(pid
= fork()) {
1085 die("fork failed\n");
1088 setsid(); /* create a new process group */
1089 dup2(s
, STDIN_FILENO
);
1090 dup2(s
, STDOUT_FILENO
);
1091 dup2(s
, STDERR_FILENO
);
1092 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
1093 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
1101 signal(SIGCHLD
, sigchld
);
1103 iofd
= (!strcmp(opt_io
, "-")) ?
1105 open(opt_io
, O_WRONLY
| O_CREAT
, 0666);
1107 fprintf(stderr
, "Error opening %s:%s\n",
1108 opt_io
, strerror(errno
));
1118 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
1120 fprintf(stderr
, "\n");
1125 static char buf
[BUFSIZ
];
1126 static int buflen
= 0;
1129 int charsize
; /* size of utf8 char in bytes */
1133 /* append read bytes to unprocessed bytes */
1134 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
1135 die("Couldn't read from shell: %s\n", SERRNO
);
1137 /* process every complete utf8 char */
1140 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
1141 charsize
= utf8decode(ptr
, &utf8c
);
1142 utf8encode(&utf8c
, s
);
1148 /* keep any uncomplete utf8 char for the next call */
1149 memmove(buf
, ptr
, buflen
);
1153 ttywrite(const char *s
, size_t n
) {
1154 if(write(cmdfd
, s
, n
) == -1)
1155 die("write error on tty: %s\n", SERRNO
);
1162 w
.ws_row
= term
.row
;
1163 w
.ws_col
= term
.col
;
1164 w
.ws_xpixel
= xw
.tw
;
1165 w
.ws_ypixel
= xw
.th
;
1166 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
1167 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
1171 tsetdirt(int top
, int bot
) {
1174 LIMIT(top
, 0, term
.row
-1);
1175 LIMIT(bot
, 0, term
.row
-1);
1177 for(i
= top
; i
<= bot
; i
++)
1183 tsetdirt(0, term
.row
-1);
1190 if(mode
== CURSOR_SAVE
) {
1192 } else if(mode
== CURSOR_LOAD
) {
1202 term
.c
= (TCursor
){{
1206 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
1208 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1209 for(i
= tabspaces
; i
< term
.col
; i
+= tabspaces
)
1212 term
.bot
= term
.row
- 1;
1213 term
.mode
= MODE_WRAP
;
1215 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1217 tcursor(CURSOR_SAVE
);
1221 tnew(int col
, int row
) {
1222 memset(&term
, 0, sizeof(Term
));
1231 Line
*tmp
= term
.line
;
1233 term
.line
= term
.alt
;
1235 term
.mode
^= MODE_ALTSCREEN
;
1240 tscrolldown(int orig
, int n
) {
1244 LIMIT(n
, 0, term
.bot
-orig
+1);
1246 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
1248 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
1249 temp
= term
.line
[i
];
1250 term
.line
[i
] = term
.line
[i
-n
];
1251 term
.line
[i
-n
] = temp
;
1254 term
.dirty
[i
-n
] = 1;
1261 tscrollup(int orig
, int n
) {
1264 LIMIT(n
, 0, term
.bot
-orig
+1);
1266 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
1268 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
1269 temp
= term
.line
[i
];
1270 term
.line
[i
] = term
.line
[i
+n
];
1271 term
.line
[i
+n
] = temp
;
1274 term
.dirty
[i
+n
] = 1;
1277 selscroll(orig
, -n
);
1281 selscroll(int orig
, int n
) {
1285 if(BETWEEN(sel
.by
, orig
, term
.bot
) || BETWEEN(sel
.ey
, orig
, term
.bot
)) {
1286 if((sel
.by
+= n
) > term
.bot
|| (sel
.ey
+= n
) < term
.top
) {
1290 if(sel
.type
== SEL_RECTANGULAR
) {
1291 if(sel
.by
< term
.top
)
1293 if(sel
.ey
> term
.bot
)
1296 if(sel
.by
< term
.top
) {
1300 if(sel
.ey
> term
.bot
) {
1305 sel
.b
.y
= sel
.by
, sel
.b
.x
= sel
.bx
;
1306 sel
.e
.y
= sel
.ey
, sel
.e
.x
= sel
.ex
;
1311 tnewline(int first_col
) {
1315 tscrollup(term
.top
, 1);
1319 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1324 char *p
= csiescseq
.buf
, *np
;
1333 csiescseq
.buf
[csiescseq
.len
] = '\0';
1334 while(p
< csiescseq
.buf
+csiescseq
.len
) {
1336 v
= strtol(p
, &np
, 10);
1339 if(v
== LONG_MAX
|| v
== LONG_MIN
)
1341 csiescseq
.arg
[csiescseq
.narg
++] = v
;
1343 if(*p
!= ';' || csiescseq
.narg
== ESC_ARG_SIZ
)
1347 csiescseq
.mode
= *p
;
1350 /* for absolute user moves, when decom is set */
1352 tmoveato(int x
, int y
) {
1353 tmoveto(x
, y
+ ((term
.c
.state
& CURSOR_ORIGIN
) ? term
.top
: 0));
1357 tmoveto(int x
, int y
) {
1360 if(term
.c
.state
& CURSOR_ORIGIN
) {
1365 maxy
= term
.row
- 1;
1367 LIMIT(x
, 0, term
.col
-1);
1368 LIMIT(y
, miny
, maxy
);
1369 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1375 tsetchar(char *c
, Glyph
*attr
, int x
, int y
) {
1376 static char *vt100_0
[62] = { /* 0x41 - 0x7e */
1377 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1378 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1379 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1380 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1381 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1382 "", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1383 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1384 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1388 * The table is proudly stolen from rxvt.
1390 if(attr
->mode
& ATTR_GFX
) {
1391 if(c
[0] >= 0x41 && c
[0] <= 0x7e
1392 && vt100_0
[c
[0] - 0x41]) {
1393 c
= vt100_0
[c
[0] - 0x41];
1398 term
.line
[y
][x
] = *attr
;
1399 memcpy(term
.line
[y
][x
].c
, c
, UTF_SIZ
);
1403 tclearregion(int x1
, int y1
, int x2
, int y2
) {
1407 temp
= x1
, x1
= x2
, x2
= temp
;
1409 temp
= y1
, y1
= y2
, y2
= temp
;
1411 LIMIT(x1
, 0, term
.col
-1);
1412 LIMIT(x2
, 0, term
.col
-1);
1413 LIMIT(y1
, 0, term
.row
-1);
1414 LIMIT(y2
, 0, term
.row
-1);
1416 for(y
= y1
; y
<= y2
; y
++) {
1418 for(x
= x1
; x
<= x2
; x
++) {
1419 term
.line
[y
][x
] = term
.c
.attr
;
1420 memcpy(term
.line
[y
][x
].c
, " ", 2);
1426 tdeletechar(int n
) {
1427 int src
= term
.c
.x
+ n
;
1429 int size
= term
.col
- src
;
1431 term
.dirty
[term
.c
.y
] = 1;
1433 if(src
>= term
.col
) {
1434 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1438 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1439 size
* sizeof(Glyph
));
1440 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1444 tinsertblank(int n
) {
1447 int size
= term
.col
- dst
;
1449 term
.dirty
[term
.c
.y
] = 1;
1451 if(dst
>= term
.col
) {
1452 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1456 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1457 size
* sizeof(Glyph
));
1458 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1462 tinsertblankline(int n
) {
1463 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1466 tscrolldown(term
.c
.y
, n
);
1470 tdeleteline(int n
) {
1471 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1474 tscrollup(term
.c
.y
, n
);
1478 tsetattr(int *attr
, int l
) {
1481 for(i
= 0; i
< l
; i
++) {
1484 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE \
1485 | ATTR_BOLD
| ATTR_ITALIC \
1487 term
.c
.attr
.fg
= defaultfg
;
1488 term
.c
.attr
.bg
= defaultbg
;
1491 term
.c
.attr
.mode
|= ATTR_BOLD
;
1494 term
.c
.attr
.mode
|= ATTR_ITALIC
;
1497 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1499 case 5: /* slow blink */
1500 case 6: /* rapid blink */
1501 term
.c
.attr
.mode
|= ATTR_BLINK
;
1504 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1508 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
1511 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
1514 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1518 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
1521 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1524 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1526 if(BETWEEN(attr
[i
], 0, 255)) {
1527 term
.c
.attr
.fg
= attr
[i
];
1530 "erresc: bad fgcolor %d\n",
1535 "erresc(38): gfx attr %d unknown\n",
1540 term
.c
.attr
.fg
= defaultfg
;
1543 if(i
+ 2 < l
&& attr
[i
+ 1] == 5) {
1545 if(BETWEEN(attr
[i
], 0, 255)) {
1546 term
.c
.attr
.bg
= attr
[i
];
1549 "erresc: bad bgcolor %d\n",
1554 "erresc(48): gfx attr %d unknown\n",
1559 term
.c
.attr
.bg
= defaultbg
;
1562 if(BETWEEN(attr
[i
], 30, 37)) {
1563 term
.c
.attr
.fg
= attr
[i
] - 30;
1564 } else if(BETWEEN(attr
[i
], 40, 47)) {
1565 term
.c
.attr
.bg
= attr
[i
] - 40;
1566 } else if(BETWEEN(attr
[i
], 90, 97)) {
1567 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1568 } else if(BETWEEN(attr
[i
], 100, 107)) {
1569 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
1572 "erresc(default): gfx attr %d unknown\n",
1573 attr
[i
]), csidump();
1581 tsetscroll(int t
, int b
) {
1584 LIMIT(t
, 0, term
.row
-1);
1585 LIMIT(b
, 0, term
.row
-1);
1595 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
1598 tsetmode(bool priv
, bool set
, int *args
, int narg
) {
1602 for(lim
= args
+ narg
; args
< lim
; ++args
) {
1606 case 1: /* DECCKM -- Cursor key */
1607 MODBIT(term
.mode
, set
, MODE_APPCURSOR
);
1609 case 5: /* DECSCNM -- Reverse video */
1611 MODBIT(term
.mode
, set
, MODE_REVERSE
);
1612 if(mode
!= term
.mode
)
1613 redraw(REDRAW_TIMEOUT
);
1615 case 6: /* DECOM -- Origin */
1616 MODBIT(term
.c
.state
, set
, CURSOR_ORIGIN
);
1619 case 7: /* DECAWM -- Auto wrap */
1620 MODBIT(term
.mode
, set
, MODE_WRAP
);
1622 case 0: /* Error (IGNORED) */
1623 case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
1624 case 3: /* DECCOLM -- Column (IGNORED) */
1625 case 4: /* DECSCLM -- Scroll (IGNORED) */
1626 case 8: /* DECARM -- Auto repeat (IGNORED) */
1627 case 18: /* DECPFF -- Printer feed (IGNORED) */
1628 case 19: /* DECPEX -- Printer extent (IGNORED) */
1629 case 42: /* DECNRCM -- National characters (IGNORED) */
1630 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1632 case 25: /* DECTCEM -- Text Cursor Enable Mode */
1633 MODBIT(term
.mode
, !set
, MODE_HIDE
);
1635 case 1000: /* 1000,1002: enable xterm mouse report */
1636 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
1637 MODBIT(term
.mode
, 0, MODE_MOUSEMOTION
);
1640 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
1641 MODBIT(term
.mode
, 0, MODE_MOUSEBTN
);
1644 MODBIT(term
.mode
, set
, MODE_MOUSESGR
);
1646 case 1049: /* = 1047 and 1048 */
1649 if (!allowaltscreen
)
1652 alt
= IS_SET(MODE_ALTSCREEN
);
1654 tclearregion(0, 0, term
.col
-1,
1657 if(set
^ alt
) /* set is always 1 or 0 */
1663 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1667 "erresc: unknown private set/reset mode %d\n",
1673 case 0: /* Error (IGNORED) */
1675 case 2: /* KAM -- keyboard action */
1676 MODBIT(term
.mode
, set
, MODE_KBDLOCK
);
1678 case 4: /* IRM -- Insertion-replacement */
1679 MODBIT(term
.mode
, set
, MODE_INSERT
);
1681 case 12: /* SRM -- Send/Receive */
1682 MODBIT(term
.mode
, !set
, MODE_ECHO
);
1684 case 20: /* LNM -- Linefeed/new line */
1685 MODBIT(term
.mode
, set
, MODE_CRLF
);
1689 "erresc: unknown set/reset mode %d\n",
1701 switch(csiescseq
.mode
) {
1704 fprintf(stderr
, "erresc: unknown csi ");
1708 case '@': /* ICH -- Insert <n> blank char */
1709 DEFAULT(csiescseq
.arg
[0], 1);
1710 tinsertblank(csiescseq
.arg
[0]);
1712 case 'A': /* CUU -- Cursor <n> Up */
1713 DEFAULT(csiescseq
.arg
[0], 1);
1714 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
1716 case 'B': /* CUD -- Cursor <n> Down */
1717 case 'e': /* VPR --Cursor <n> Down */
1718 DEFAULT(csiescseq
.arg
[0], 1);
1719 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
1721 case 'c': /* DA -- Device Attributes */
1722 if(csiescseq
.arg
[0] == 0)
1723 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
1725 case 'C': /* CUF -- Cursor <n> Forward */
1726 case 'a': /* HPR -- Cursor <n> Forward */
1727 DEFAULT(csiescseq
.arg
[0], 1);
1728 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
1730 case 'D': /* CUB -- Cursor <n> Backward */
1731 DEFAULT(csiescseq
.arg
[0], 1);
1732 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
1734 case 'E': /* CNL -- Cursor <n> Down and first col */
1735 DEFAULT(csiescseq
.arg
[0], 1);
1736 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
1738 case 'F': /* CPL -- Cursor <n> Up and first col */
1739 DEFAULT(csiescseq
.arg
[0], 1);
1740 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
1742 case 'g': /* TBC -- Tabulation clear */
1743 switch(csiescseq
.arg
[0]) {
1744 case 0: /* clear current tab stop */
1745 term
.tabs
[term
.c
.x
] = 0;
1747 case 3: /* clear all the tabs */
1748 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1754 case 'G': /* CHA -- Move to <col> */
1756 DEFAULT(csiescseq
.arg
[0], 1);
1757 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
1759 case 'H': /* CUP -- Move to <row> <col> */
1761 DEFAULT(csiescseq
.arg
[0], 1);
1762 DEFAULT(csiescseq
.arg
[1], 1);
1763 tmoveato(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
1765 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
1766 DEFAULT(csiescseq
.arg
[0], 1);
1767 while(csiescseq
.arg
[0]--)
1770 case 'J': /* ED -- Clear screen */
1772 switch(csiescseq
.arg
[0]) {
1774 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1775 if(term
.c
.y
< term
.row
-1) {
1776 tclearregion(0, term
.c
.y
+1, term
.col
-1,
1782 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
1783 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1786 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1792 case 'K': /* EL -- Clear line */
1793 switch(csiescseq
.arg
[0]) {
1795 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1,
1799 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1802 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
1806 case 'S': /* SU -- Scroll <n> line up */
1807 DEFAULT(csiescseq
.arg
[0], 1);
1808 tscrollup(term
.top
, csiescseq
.arg
[0]);
1810 case 'T': /* SD -- Scroll <n> line down */
1811 DEFAULT(csiescseq
.arg
[0], 1);
1812 tscrolldown(term
.top
, csiescseq
.arg
[0]);
1814 case 'L': /* IL -- Insert <n> blank lines */
1815 DEFAULT(csiescseq
.arg
[0], 1);
1816 tinsertblankline(csiescseq
.arg
[0]);
1818 case 'l': /* RM -- Reset Mode */
1819 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
1821 case 'M': /* DL -- Delete <n> lines */
1822 DEFAULT(csiescseq
.arg
[0], 1);
1823 tdeleteline(csiescseq
.arg
[0]);
1825 case 'X': /* ECH -- Erase <n> char */
1826 DEFAULT(csiescseq
.arg
[0], 1);
1827 tclearregion(term
.c
.x
, term
.c
.y
,
1828 term
.c
.x
+ csiescseq
.arg
[0] - 1, term
.c
.y
);
1830 case 'P': /* DCH -- Delete <n> char */
1831 DEFAULT(csiescseq
.arg
[0], 1);
1832 tdeletechar(csiescseq
.arg
[0]);
1834 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
1835 DEFAULT(csiescseq
.arg
[0], 1);
1836 while(csiescseq
.arg
[0]--)
1839 case 'd': /* VPA -- Move to <row> */
1840 DEFAULT(csiescseq
.arg
[0], 1);
1841 tmoveato(term
.c
.x
, csiescseq
.arg
[0]-1);
1843 case 'h': /* SM -- Set terminal mode */
1844 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
1846 case 'm': /* SGR -- Terminal attribute (color) */
1847 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
1849 case 'r': /* DECSTBM -- Set Scrolling Region */
1850 if(csiescseq
.priv
) {
1853 DEFAULT(csiescseq
.arg
[0], 1);
1854 DEFAULT(csiescseq
.arg
[1], term
.row
);
1855 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
1859 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1860 tcursor(CURSOR_SAVE
);
1862 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1863 tcursor(CURSOR_LOAD
);
1874 for(i
= 0; i
< csiescseq
.len
; i
++) {
1875 c
= csiescseq
.buf
[i
] & 0xff;
1878 } else if(c
== '\n') {
1880 } else if(c
== '\r') {
1882 } else if(c
== 0x1b) {
1885 printf("(%02x)", c
);
1893 memset(&csiescseq
, 0, sizeof(csiescseq
));
1902 narg
= strescseq
.narg
;
1904 switch(strescseq
.type
) {
1905 case ']': /* OSC -- Operating System Command */
1906 switch(i
= atoi(strescseq
.args
[0])) {
1911 xsettitle(strescseq
.args
[1]);
1913 case 4: /* color set */
1916 p
= strescseq
.args
[2];
1918 case 104: /* color reset, here p = NULL */
1919 j
= (narg
> 1) ? atoi(strescseq
.args
[1]) : -1;
1920 if (!xsetcolorname(j
, p
)) {
1921 fprintf(stderr
, "erresc: invalid color %s\n", p
);
1924 * TODO if defaultbg color is changed, borders
1931 fprintf(stderr
, "erresc: unknown str ");
1936 case 'k': /* old title set compatibility */
1937 xsettitle(strescseq
.args
[0]);
1939 case 'P': /* DSC -- Device Control String */
1940 case '_': /* APC -- Application Program Command */
1941 case '^': /* PM -- Privacy Message */
1943 fprintf(stderr
, "erresc: unknown str ");
1952 char *p
= strescseq
.buf
;
1955 strescseq
.buf
[strescseq
.len
] = '\0';
1956 while(p
&& strescseq
.narg
< STR_ARG_SIZ
)
1957 strescseq
.args
[strescseq
.narg
++] = strsep(&p
, ";");
1965 printf("ESC%c", strescseq
.type
);
1966 for(i
= 0; i
< strescseq
.len
; i
++) {
1967 c
= strescseq
.buf
[i
] & 0xff;
1970 } else if(isprint(c
)) {
1972 } else if(c
== '\n') {
1974 } else if(c
== '\r') {
1976 } else if(c
== 0x1b) {
1979 printf("(%02x)", c
);
1987 memset(&strescseq
, 0, sizeof(strescseq
));
1991 tputtab(bool forward
) {
1997 for(++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
2002 for(--x
; x
> 0 && !term
.tabs
[x
]; --x
)
2005 tmoveto(x
, term
.c
.y
);
2009 techo(char *buf
, int len
) {
2010 for(; len
> 0; buf
++, len
--) {
2013 if(c
== '\033') { /* escape */
2016 } else if(c
< '\x20') { /* control code */
2017 if(c
!= '\n' && c
!= '\r' && c
!= '\t') {
2031 tputc(char *c
, int len
) {
2033 bool control
= ascii
< '\x20' || ascii
== 0177;
2036 if(xwrite(iofd
, c
, len
) < 0) {
2037 fprintf(stderr
, "Error writing in %s:%s\n",
2038 opt_io
, strerror(errno
));
2045 * STR sequences must be checked before anything else
2046 * because it can use some control codes as part of the sequence.
2048 if(term
.esc
& ESC_STR
) {
2051 term
.esc
= ESC_START
| ESC_STR_END
;
2053 case '\a': /* backwards compatibility to xterm */
2058 if(strescseq
.len
+ len
< sizeof(strescseq
.buf
) - 1) {
2059 memmove(&strescseq
.buf
[strescseq
.len
], c
, len
);
2060 strescseq
.len
+= len
;
2063 * Here is a bug in terminals. If the user never sends
2064 * some code to stop the str or esc command, then st
2065 * will stop responding. But this is better than
2066 * silently failing with unknown characters. At least
2067 * then users will report back.
2069 * In the case users ever get fixed, here is the code:
2081 * Actions of control codes must be performed as soon they arrive
2082 * because they can be embedded inside a control sequence, and
2083 * they must not cause conflicts with sequences.
2091 tmoveto(term
.c
.x
-1, term
.c
.y
);
2094 tmoveto(0, term
.c
.y
);
2099 /* go to first col if the mode is set */
2100 tnewline(IS_SET(MODE_CRLF
));
2102 case '\a': /* BEL */
2103 if(!(xw
.state
& WIN_FOCUSED
))
2106 case '\033': /* ESC */
2108 term
.esc
= ESC_START
;
2110 case '\016': /* SO */
2111 case '\017': /* SI */
2113 * Different charsets are hard to handle. Applications
2114 * should use the right alt charset escapes for the
2115 * only reason they still exist: line drawing. The
2116 * rest is incompatible history st should not support.
2119 case '\032': /* SUB */
2120 case '\030': /* CAN */
2123 case '\005': /* ENQ (IGNORED) */
2124 case '\000': /* NUL (IGNORED) */
2125 case '\021': /* XON (IGNORED) */
2126 case '\023': /* XOFF (IGNORED) */
2127 case 0177: /* DEL (IGNORED) */
2130 } else if(term
.esc
& ESC_START
) {
2131 if(term
.esc
& ESC_CSI
) {
2132 csiescseq
.buf
[csiescseq
.len
++] = ascii
;
2133 if(BETWEEN(ascii
, 0x40, 0x7E)
2134 || csiescseq
.len
>= \
2135 sizeof(csiescseq
.buf
)-1) {
2140 } else if(term
.esc
& ESC_STR_END
) {
2144 } else if(term
.esc
& ESC_ALTCHARSET
) {
2146 case '0': /* Line drawing set */
2147 term
.c
.attr
.mode
|= ATTR_GFX
;
2149 case 'B': /* USASCII */
2150 term
.c
.attr
.mode
&= ~ATTR_GFX
;
2152 case 'A': /* UK (IGNORED) */
2153 case '<': /* multinational charset (IGNORED) */
2154 case '5': /* Finnish (IGNORED) */
2155 case 'C': /* Finnish (IGNORED) */
2156 case 'K': /* German (IGNORED) */
2159 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
2162 } else if(term
.esc
& ESC_TEST
) {
2163 if(ascii
== '8') { /* DEC screen alignment test. */
2164 char E
[UTF_SIZ
] = "E";
2167 for(x
= 0; x
< term
.col
; ++x
) {
2168 for(y
= 0; y
< term
.row
; ++y
)
2169 tsetchar(E
, &term
.c
.attr
, x
, y
);
2176 term
.esc
|= ESC_CSI
;
2179 term
.esc
|= ESC_TEST
;
2181 case 'P': /* DCS -- Device Control String */
2182 case '_': /* APC -- Application Program Command */
2183 case '^': /* PM -- Privacy Message */
2184 case ']': /* OSC -- Operating System Command */
2185 case 'k': /* old title set compatibility */
2187 strescseq
.type
= ascii
;
2188 term
.esc
|= ESC_STR
;
2190 case '(': /* set primary charset G0 */
2191 term
.esc
|= ESC_ALTCHARSET
;
2193 case ')': /* set secondary charset G1 (IGNORED) */
2194 case '*': /* set tertiary charset G2 (IGNORED) */
2195 case '+': /* set quaternary charset G3 (IGNORED) */
2198 case 'D': /* IND -- Linefeed */
2199 if(term
.c
.y
== term
.bot
) {
2200 tscrollup(term
.top
, 1);
2202 tmoveto(term
.c
.x
, term
.c
.y
+1);
2206 case 'E': /* NEL -- Next line */
2207 tnewline(1); /* always go to first col */
2210 case 'H': /* HTS -- Horizontal tab stop */
2211 term
.tabs
[term
.c
.x
] = 1;
2214 case 'M': /* RI -- Reverse index */
2215 if(term
.c
.y
== term
.top
) {
2216 tscrolldown(term
.top
, 1);
2218 tmoveto(term
.c
.x
, term
.c
.y
-1);
2222 case 'Z': /* DECID -- Identify Terminal */
2223 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
2226 case 'c': /* RIS -- Reset to inital state */
2231 case '=': /* DECPAM -- Application keypad */
2232 term
.mode
|= MODE_APPKEYPAD
;
2235 case '>': /* DECPNM -- Normal keypad */
2236 term
.mode
&= ~MODE_APPKEYPAD
;
2239 case '7': /* DECSC -- Save Cursor */
2240 tcursor(CURSOR_SAVE
);
2243 case '8': /* DECRC -- Restore Cursor */
2244 tcursor(CURSOR_LOAD
);
2247 case '\\': /* ST -- Stop */
2251 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
2252 (uchar
) ascii
, isprint(ascii
)? ascii
:'.');
2257 * All characters which form part of a sequence are not
2263 * Display control codes only if we are in graphic mode
2265 if(control
&& !(term
.c
.attr
.mode
& ATTR_GFX
))
2267 if(sel
.bx
!= -1 && BETWEEN(term
.c
.y
, sel
.by
, sel
.ey
))
2269 if(IS_SET(MODE_WRAP
) && term
.c
.state
& CURSOR_WRAPNEXT
)
2270 tnewline(1); /* always go to first col */
2272 if(IS_SET(MODE_INSERT
) && term
.c
.x
+1 < term
.col
) {
2273 memmove(&term
.line
[term
.c
.y
][term
.c
.x
+1],
2274 &term
.line
[term
.c
.y
][term
.c
.x
],
2275 (term
.col
- term
.c
.x
- 1) * sizeof(Glyph
));
2278 tsetchar(c
, &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
2279 if(term
.c
.x
+1 < term
.col
) {
2280 tmoveto(term
.c
.x
+1, term
.c
.y
);
2282 term
.c
.state
|= CURSOR_WRAPNEXT
;
2287 tresize(int col
, int row
) {
2289 int minrow
= MIN(row
, term
.row
);
2290 int mincol
= MIN(col
, term
.col
);
2291 int slide
= term
.c
.y
- row
+ 1;
2295 if(col
< 1 || row
< 1)
2298 /* free unneeded rows */
2302 * slide screen to keep cursor where we expect it -
2303 * tscrollup would work here, but we can optimize to
2304 * memmove because we're freeing the earlier lines
2306 for(/* i = 0 */; i
< slide
; i
++) {
2310 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
2311 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
2313 for(i
+= row
; i
< term
.row
; i
++) {
2318 /* resize to new height */
2319 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
2320 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
2321 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
2322 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
2324 /* resize each row to new width, zero-pad if needed */
2325 for(i
= 0; i
< minrow
; i
++) {
2327 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
2328 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
2331 /* allocate any new rows */
2332 for(/* i == minrow */; i
< row
; i
++) {
2334 term
.line
[i
] = xcalloc(col
, sizeof(Glyph
));
2335 term
.alt
[i
] = xcalloc(col
, sizeof(Glyph
));
2337 if(col
> term
.col
) {
2338 bp
= term
.tabs
+ term
.col
;
2340 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
2341 while(--bp
> term
.tabs
&& !*bp
)
2343 for(bp
+= tabspaces
; bp
< term
.tabs
+ col
; bp
+= tabspaces
)
2346 /* update terminal size */
2349 /* reset scrolling region */
2350 tsetscroll(0, row
-1);
2351 /* make use of the LIMIT in tmoveto */
2352 tmoveto(term
.c
.x
, term
.c
.y
);
2353 /* Clearing both screens */
2356 if(mincol
< col
&& 0 < minrow
) {
2357 tclearregion(mincol
, 0, col
- 1, minrow
- 1);
2359 if(0 < col
&& minrow
< row
) {
2360 tclearregion(0, minrow
, col
- 1, row
- 1);
2363 } while(orig
!= term
.line
);
2369 xresize(int col
, int row
) {
2370 xw
.tw
= MAX(1, col
* xw
.cw
);
2371 xw
.th
= MAX(1, row
* xw
.ch
);
2373 XFreePixmap(xw
.dpy
, xw
.buf
);
2374 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2375 DefaultDepth(xw
.dpy
, xw
.scr
));
2376 XftDrawChange(xw
.draw
, xw
.buf
);
2377 xclear(0, 0, xw
.w
, xw
.h
);
2380 static inline ushort
2381 sixd_to_16bit(int x
) {
2382 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
2388 XRenderColor color
= { .alpha
= 0xffff };
2390 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
2391 for(i
= 0; i
< LEN(colorname
); i
++) {
2394 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, colorname
[i
], &dc
.col
[i
])) {
2395 die("Could not allocate color '%s'\n", colorname
[i
]);
2399 /* load colors [16-255] ; same colors as xterm */
2400 for(i
= 16, r
= 0; r
< 6; r
++) {
2401 for(g
= 0; g
< 6; g
++) {
2402 for(b
= 0; b
< 6; b
++) {
2403 color
.red
= sixd_to_16bit(r
);
2404 color
.green
= sixd_to_16bit(g
);
2405 color
.blue
= sixd_to_16bit(b
);
2406 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &dc
.col
[i
])) {
2407 die("Could not allocate color %d\n", i
);
2414 for(r
= 0; r
< 24; r
++, i
++) {
2415 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
2416 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
,
2418 die("Could not allocate color %d\n", i
);
2424 xsetcolorname(int x
, const char *name
) {
2425 XRenderColor color
= { .alpha
= 0xffff };
2427 if (x
< 0 || x
> LEN(colorname
))
2430 if(16 <= x
&& x
< 16 + 216) {
2431 int r
= (x
- 16) / 36, g
= ((x
- 16) % 36) / 6, b
= (x
- 16) % 6;
2432 color
.red
= sixd_to_16bit(r
);
2433 color
.green
= sixd_to_16bit(g
);
2434 color
.blue
= sixd_to_16bit(b
);
2435 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2436 return 0; /* something went wrong */
2439 } else if (16 + 216 <= x
&& x
< 256) {
2440 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * (x
- (16 + 216));
2441 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2442 return 0; /* something went wrong */
2446 name
= colorname
[x
];
2449 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, &colour
))
2456 xtermclear(int col1
, int row1
, int col2
, int row2
) {
2457 XftDrawRect(xw
.draw
,
2458 &dc
.col
[IS_SET(MODE_REVERSE
) ? defaultfg
: defaultbg
],
2459 borderpx
+ col1
* xw
.cw
,
2460 borderpx
+ row1
* xw
.ch
,
2461 (col2
-col1
+1) * xw
.cw
,
2462 (row2
-row1
+1) * xw
.ch
);
2466 * Absolute coordinates.
2469 xclear(int x1
, int y1
, int x2
, int y2
) {
2470 XftDrawRect(xw
.draw
,
2471 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
2472 x1
, y1
, x2
-x1
, y2
-y1
);
2477 XClassHint
class = {opt_class
? opt_class
: termname
, termname
};
2478 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
2479 XSizeHints
*sizeh
= NULL
;
2481 sizeh
= XAllocSizeHints();
2482 if(xw
.isfixed
== False
) {
2483 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
2484 sizeh
->height
= xw
.h
;
2485 sizeh
->width
= xw
.w
;
2486 sizeh
->height_inc
= xw
.ch
;
2487 sizeh
->width_inc
= xw
.cw
;
2488 sizeh
->base_height
= 2 * borderpx
;
2489 sizeh
->base_width
= 2 * borderpx
;
2491 sizeh
->flags
= PMaxSize
| PMinSize
;
2492 sizeh
->min_width
= sizeh
->max_width
= xw
.fw
;
2493 sizeh
->min_height
= sizeh
->max_height
= xw
.fh
;
2496 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
, &class);
2501 xloadfont(Font
*f
, FcPattern
*pattern
) {
2505 match
= FcFontMatch(NULL
, pattern
, &result
);
2509 if(!(f
->set
= FcFontSort(0, match
, FcTrue
, 0, &result
))) {
2510 FcPatternDestroy(match
);
2514 if(!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
2515 FcPatternDestroy(match
);
2519 f
->pattern
= FcPatternDuplicate(pattern
);
2521 f
->ascent
= f
->match
->ascent
;
2522 f
->descent
= f
->match
->descent
;
2524 f
->rbearing
= f
->match
->max_advance_width
;
2526 f
->height
= f
->ascent
+ f
->descent
;
2527 f
->width
= f
->lbearing
+ f
->rbearing
;
2533 xloadfonts(char *fontstr
, int fontsize
) {
2538 if(fontstr
[0] == '-') {
2539 pattern
= XftXlfdParse(fontstr
, False
, False
);
2541 pattern
= FcNameParse((FcChar8
*)fontstr
);
2545 die("st: can't open font %s\n", fontstr
);
2548 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
2549 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
2550 usedfontsize
= fontsize
;
2552 result
= FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
);
2553 if(result
== FcResultMatch
) {
2554 usedfontsize
= (int)fontval
;
2557 * Default font size is 12, if none given. This is to
2558 * have a known usedfontsize value.
2560 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
2565 FcConfigSubstitute(0, pattern
, FcMatchPattern
);
2566 FcDefaultSubstitute(pattern
);
2568 if(xloadfont(&dc
.font
, pattern
))
2569 die("st: can't open font %s\n", fontstr
);
2571 /* Setting character width and height. */
2572 xw
.cw
= dc
.font
.width
;
2573 xw
.ch
= dc
.font
.height
;
2575 FcPatternDel(pattern
, FC_SLANT
);
2576 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
2577 if(xloadfont(&dc
.ifont
, pattern
))
2578 die("st: can't open font %s\n", fontstr
);
2580 FcPatternDel(pattern
, FC_WEIGHT
);
2581 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
2582 if(xloadfont(&dc
.ibfont
, pattern
))
2583 die("st: can't open font %s\n", fontstr
);
2585 FcPatternDel(pattern
, FC_SLANT
);
2586 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
2587 if(xloadfont(&dc
.bfont
, pattern
))
2588 die("st: can't open font %s\n", fontstr
);
2590 FcPatternDestroy(pattern
);
2594 xunloadfonts(void) {
2598 * Free the loaded fonts in the font cache. This is done backwards
2601 for(i
= 0, ip
= frccur
; i
< frclen
; i
++, ip
--) {
2604 XftFontClose(xw
.dpy
, frc
[ip
].font
);
2609 XftFontClose(xw
.dpy
, dc
.font
.match
);
2610 FcPatternDestroy(dc
.font
.pattern
);
2611 FcFontSetDestroy(dc
.font
.set
);
2612 XftFontClose(xw
.dpy
, dc
.bfont
.match
);
2613 FcPatternDestroy(dc
.bfont
.pattern
);
2614 FcFontSetDestroy(dc
.bfont
.set
);
2615 XftFontClose(xw
.dpy
, dc
.ifont
.match
);
2616 FcPatternDestroy(dc
.ifont
.pattern
);
2617 FcFontSetDestroy(dc
.ifont
.set
);
2618 XftFontClose(xw
.dpy
, dc
.ibfont
.match
);
2619 FcPatternDestroy(dc
.ibfont
.pattern
);
2620 FcFontSetDestroy(dc
.ibfont
.set
);
2624 xzoom(const Arg
*arg
) {
2626 xloadfonts(usedfont
, usedfontsize
+ arg
->i
);
2633 XSetWindowAttributes attrs
;
2639 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
2640 die("Can't open display\n");
2641 xw
.scr
= XDefaultScreen(xw
.dpy
);
2642 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
2646 die("Could not init fontconfig.\n");
2648 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
2649 xloadfonts(usedfont
, 0);
2652 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
2655 /* adjust fixed window geometry */
2657 sw
= DisplayWidth(xw
.dpy
, xw
.scr
);
2658 sh
= DisplayHeight(xw
.dpy
, xw
.scr
);
2660 xw
.fx
= sw
+ xw
.fx
- xw
.fw
- 1;
2662 xw
.fy
= sh
+ xw
.fy
- xw
.fh
- 1;
2667 /* window - default size */
2668 xw
.h
= 2 * borderpx
+ term
.row
* xw
.ch
;
2669 xw
.w
= 2 * borderpx
+ term
.col
* xw
.cw
;
2675 attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
2676 attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
2677 attrs
.bit_gravity
= NorthWestGravity
;
2678 attrs
.event_mask
= FocusChangeMask
| KeyPressMask
2679 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
2680 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
2681 attrs
.colormap
= xw
.cmap
;
2683 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : \
2684 XRootWindow(xw
.dpy
, xw
.scr
);
2685 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.fx
, xw
.fy
,
2686 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
2688 CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
2692 memset(&gcvalues
, 0, sizeof(gcvalues
));
2693 gcvalues
.graphics_exposures
= False
;
2694 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
2696 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2697 DefaultDepth(xw
.dpy
, xw
.scr
));
2698 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
2699 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
2701 /* Xft rendering context */
2702 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
2705 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
2706 XSetLocaleModifiers("@im=local");
2707 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
2708 XSetLocaleModifiers("@im=");
2709 if((xw
.xim
= XOpenIM(xw
.dpy
,
2710 NULL
, NULL
, NULL
)) == NULL
) {
2711 die("XOpenIM failed. Could not open input"
2716 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
2717 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
2718 XNFocusWindow
, xw
.win
, NULL
);
2720 die("XCreateIC failed. Could not obtain input method.\n");
2722 /* white cursor, black outline */
2723 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
2724 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
2725 XRecolorCursor(xw
.dpy
, cursor
,
2726 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
2727 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
2729 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
2730 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
2731 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
2734 XMapWindow(xw
.dpy
, xw
.win
);
2740 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
2741 int winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
,
2742 width
= charlen
* xw
.cw
, xp
, i
;
2744 int u8fl
, u8fblen
, u8cblen
, doesexist
;
2747 Font
*font
= &dc
.font
;
2749 FcPattern
*fcpattern
, *fontpattern
;
2750 FcFontSet
*fcsets
[] = { NULL
};
2751 FcCharSet
*fccharset
;
2752 Colour
*fg
, *bg
, *temp
, revfg
, revbg
;
2753 XRenderColor colfg
, colbg
;
2755 frcflags
= FRC_NORMAL
;
2757 if(base
.mode
& ATTR_ITALIC
) {
2758 if(base
.fg
== defaultfg
)
2759 base
.fg
= defaultitalic
;
2761 frcflags
= FRC_ITALIC
;
2762 } else if((base
.mode
& ATTR_ITALIC
) && (base
.mode
& ATTR_BOLD
)) {
2763 if(base
.fg
== defaultfg
)
2764 base
.fg
= defaultitalic
;
2766 frcflags
= FRC_ITALICBOLD
;
2767 } else if(base
.mode
& ATTR_UNDERLINE
) {
2768 if(base
.fg
== defaultfg
)
2769 base
.fg
= defaultunderline
;
2771 fg
= &dc
.col
[base
.fg
];
2772 bg
= &dc
.col
[base
.bg
];
2774 if(base
.mode
& ATTR_BOLD
) {
2775 if(BETWEEN(base
.fg
, 0, 7)) {
2776 /* basic system colors */
2777 fg
= &dc
.col
[base
.fg
+ 8];
2778 } else if(BETWEEN(base
.fg
, 16, 195)) {
2780 fg
= &dc
.col
[base
.fg
+ 36];
2781 } else if(BETWEEN(base
.fg
, 232, 251)) {
2783 fg
= &dc
.col
[base
.fg
+ 4];
2786 * Those ranges will not be brightened:
2787 * 8 - 15 – bright system colors
2788 * 196 - 231 – highest 256 color cube
2789 * 252 - 255 – brightest colors in greyscale
2792 frcflags
= FRC_BOLD
;
2795 if(IS_SET(MODE_REVERSE
)) {
2796 if(fg
== &dc
.col
[defaultfg
]) {
2797 fg
= &dc
.col
[defaultbg
];
2799 colfg
.red
= ~fg
->color
.red
;
2800 colfg
.green
= ~fg
->color
.green
;
2801 colfg
.blue
= ~fg
->color
.blue
;
2802 colfg
.alpha
= fg
->color
.alpha
;
2803 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &revfg
);
2807 if(bg
== &dc
.col
[defaultbg
]) {
2808 bg
= &dc
.col
[defaultfg
];
2810 colbg
.red
= ~bg
->color
.red
;
2811 colbg
.green
= ~bg
->color
.green
;
2812 colbg
.blue
= ~bg
->color
.blue
;
2813 colbg
.alpha
= bg
->color
.alpha
;
2814 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &revbg
);
2819 if(base
.mode
& ATTR_REVERSE
) {
2825 /* Intelligent cleaning up of the borders. */
2827 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
2828 winy
+ xw
.ch
+ ((y
>= term
.row
-1)? xw
.h
: 0));
2830 if(x
+ charlen
>= term
.col
) {
2831 xclear(winx
+ width
, (y
== 0)? 0 : winy
, xw
.w
,
2832 ((y
>= term
.row
-1)? xw
.h
: (winy
+ xw
.ch
)));
2835 xclear(winx
, 0, winx
+ width
, borderpx
);
2837 xclear(winx
, winy
+ xw
.ch
, winx
+ width
, xw
.h
);
2839 /* Clean up the region we want to draw to. */
2840 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, xw
.ch
);
2842 fcsets
[0] = font
->set
;
2843 for(xp
= winx
; bytelen
> 0;) {
2845 * Search for the range in the to be printed string of glyphs
2846 * that are in the main font. Then print that range. If
2847 * some glyph is found that is not in the font, do the
2855 u8cblen
= utf8decode(s
, &u8char
);
2859 doesexist
= XftCharIndex(xw
.dpy
, font
->match
, u8char
);
2860 if(!doesexist
|| bytelen
<= 0) {
2869 XftDrawStringUtf8(xw
.draw
, fg
,
2871 winy
+ font
->ascent
,
2874 xp
+= font
->width
* u8fl
;
2886 /* Search the font cache. */
2887 for(i
= 0; i
< frclen
; i
++, frp
--) {
2891 if(frc
[frp
].c
== u8char
2892 && frc
[frp
].flags
== frcflags
) {
2897 /* Nothing was found. */
2900 * Nothing was found in the cache. Now use
2901 * some dozen of Fontconfig calls to get the
2902 * font for one single character.
2904 fcpattern
= FcPatternDuplicate(font
->pattern
);
2905 fccharset
= FcCharSetCreate();
2907 FcCharSetAddChar(fccharset
, u8char
);
2908 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
2910 FcPatternAddBool(fcpattern
, FC_SCALABLE
,
2913 FcConfigSubstitute(0, fcpattern
,
2915 FcDefaultSubstitute(fcpattern
);
2917 fontpattern
= FcFontSetMatch(0, fcsets
,
2918 FcTrue
, fcpattern
, &fcres
);
2921 * Overwrite or create the new cache entry.
2925 if(frccur
>= LEN(frc
))
2927 if(frclen
> LEN(frc
)) {
2929 XftFontClose(xw
.dpy
, frc
[frccur
].font
);
2932 frc
[frccur
].font
= XftFontOpenPattern(xw
.dpy
,
2934 frc
[frccur
].c
= u8char
;
2935 frc
[frccur
].flags
= frcflags
;
2937 FcPatternDestroy(fcpattern
);
2938 FcCharSetDestroy(fccharset
);
2943 XftDrawStringUtf8(xw
.draw
, fg
, frc
[frp
].font
,
2944 xp
, winy
+ frc
[frp
].font
->ascent
,
2945 (FcChar8
*)u8c
, u8cblen
);
2951 XftDrawStringUtf8(xw.draw, fg, font->set, winx,
2952 winy + font->ascent, (FcChar8 *)s, bytelen);
2955 if(base
.mode
& ATTR_UNDERLINE
) {
2956 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ font
->ascent
+ 1,
2963 static int oldx
= 0, oldy
= 0;
2965 Glyph g
= {{' '}, ATTR_NULL
, defaultbg
, defaultcs
};
2967 LIMIT(oldx
, 0, term
.col
-1);
2968 LIMIT(oldy
, 0, term
.row
-1);
2970 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
2972 /* remove the old cursor */
2973 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
2974 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
,
2977 /* draw the new one */
2978 if(!(IS_SET(MODE_HIDE
))) {
2979 if(xw
.state
& WIN_FOCUSED
) {
2980 if(IS_SET(MODE_REVERSE
)) {
2981 g
.mode
|= ATTR_REVERSE
;
2987 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, 1, sl
);
2989 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
2990 borderpx
+ term
.c
.x
* xw
.cw
,
2991 borderpx
+ term
.c
.y
* xw
.ch
,
2993 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
2994 borderpx
+ term
.c
.x
* xw
.cw
,
2995 borderpx
+ term
.c
.y
* xw
.ch
,
2997 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
2998 borderpx
+ (term
.c
.x
+ 1) * xw
.cw
- 1,
2999 borderpx
+ term
.c
.y
* xw
.ch
,
3001 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3002 borderpx
+ term
.c
.x
* xw
.cw
,
3003 borderpx
+ (term
.c
.y
+ 1) * xw
.ch
- 1,
3006 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
3012 xsettitle(char *p
) {
3015 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
3017 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
3022 xsettitle(opt_title
? opt_title
: "st");
3026 redraw(int timeout
) {
3027 struct timespec tv
= {0, timeout
* 1000};
3033 nanosleep(&tv
, NULL
);
3034 XSync(xw
.dpy
, False
); /* necessary for a good tput flash */
3040 drawregion(0, 0, term
.col
, term
.row
);
3041 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.w
,
3043 XSetForeground(xw
.dpy
, dc
.gc
,
3044 dc
.col
[IS_SET(MODE_REVERSE
)?
3045 defaultfg
: defaultbg
].pixel
);
3049 drawregion(int x1
, int y1
, int x2
, int y2
) {
3050 int ic
, ib
, x
, y
, ox
, sl
;
3052 char buf
[DRAW_BUF_SIZ
];
3053 bool ena_sel
= sel
.bx
!= -1;
3055 if(sel
.alt
^ IS_SET(MODE_ALTSCREEN
))
3058 if(!(xw
.state
& WIN_VISIBLE
))
3061 for(y
= y1
; y
< y2
; y
++) {
3065 xtermclear(0, y
, term
.col
, y
);
3067 base
= term
.line
[y
][0];
3069 for(x
= x1
; x
< x2
; x
++) {
3070 new = term
.line
[y
][x
];
3071 if(ena_sel
&& *(new.c
) && selected(x
, y
))
3072 new.mode
^= ATTR_REVERSE
;
3073 if(ib
> 0 && (ATTRCMP(base
, new)
3074 || ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
3075 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3083 sl
= utf8size(new.c
);
3084 memcpy(buf
+ib
, new.c
, sl
);
3089 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3095 expose(XEvent
*ev
) {
3096 XExposeEvent
*e
= &ev
->xexpose
;
3098 if(xw
.state
& WIN_REDRAW
) {
3100 xw
.state
&= ~WIN_REDRAW
;
3106 visibility(XEvent
*ev
) {
3107 XVisibilityEvent
*e
= &ev
->xvisibility
;
3109 if(e
->state
== VisibilityFullyObscured
) {
3110 xw
.state
&= ~WIN_VISIBLE
;
3111 } else if(!(xw
.state
& WIN_VISIBLE
)) {
3112 /* need a full redraw for next Expose, not just a buf copy */
3113 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
3119 xw
.state
&= ~WIN_VISIBLE
;
3123 xseturgency(int add
) {
3124 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
3126 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
3127 XSetWMHints(xw
.dpy
, xw
.win
, h
);
3133 XFocusChangeEvent
*e
= &ev
->xfocus
;
3135 if(e
->mode
== NotifyGrab
)
3138 if(ev
->type
== FocusIn
) {
3139 XSetICFocus(xw
.xic
);
3140 xw
.state
|= WIN_FOCUSED
;
3143 XUnsetICFocus(xw
.xic
);
3144 xw
.state
&= ~WIN_FOCUSED
;
3149 match(uint mask
, uint state
) {
3150 state
&= ~(ignoremod
);
3152 if(mask
== XK_NO_MOD
&& state
)
3154 if(mask
!= XK_ANY_MOD
&& mask
!= XK_NO_MOD
&& !state
)
3156 if((state
& mask
) != state
)
3162 numlock(const Arg
*dummy
) {
3167 kmap(KeySym k
, uint state
) {
3172 /* Check for mapped keys out of X11 function keys. */
3173 for(i
= 0; i
< LEN(mappedkeys
); i
++) {
3174 if(mappedkeys
[i
] == k
)
3177 if(i
== LEN(mappedkeys
)) {
3178 if((k
& 0xFFFF) < 0xFD00)
3182 for(kp
= key
; kp
< key
+ LEN(key
); kp
++) {
3188 if(!match(mask
, state
))
3191 if(kp
->appkey
> 0) {
3192 if(!IS_SET(MODE_APPKEYPAD
))
3194 if(term
.numlock
&& kp
->appkey
== 2)
3196 } else if(kp
->appkey
< 0 && IS_SET(MODE_APPKEYPAD
)) {
3200 if((kp
->appcursor
< 0 && IS_SET(MODE_APPCURSOR
)) ||
3202 && !IS_SET(MODE_APPCURSOR
))) {
3206 if((kp
->crlf
< 0 && IS_SET(MODE_CRLF
)) ||
3207 (kp
->crlf
> 0 && !IS_SET(MODE_CRLF
))) {
3218 kpress(XEvent
*ev
) {
3219 XKeyEvent
*e
= &ev
->xkey
;
3221 char xstr
[31], buf
[32], *customkey
, *cp
= buf
;
3226 if(IS_SET(MODE_KBDLOCK
))
3229 len
= XmbLookupString(xw
.xic
, e
, xstr
, sizeof(xstr
), &ksym
, &status
);
3230 e
->state
&= ~Mod2Mask
;
3232 for(bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
3233 if(ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
3234 bp
->func(&(bp
->arg
));
3239 /* 2. custom keys from config.h */
3240 if((customkey
= kmap(ksym
, e
->state
))) {
3241 len
= strlen(customkey
);
3242 memcpy(buf
, customkey
, len
);
3243 /* 2. hardcoded (overrides X lookup) */
3248 if(len
== 1 && e
->state
& Mod1Mask
)
3251 memcpy(cp
, xstr
, len
);
3252 len
= cp
- buf
+ len
;
3256 if(IS_SET(MODE_ECHO
))
3262 cmessage(XEvent
*e
) {
3265 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
3267 if(e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
3268 if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
3269 xw
.state
|= WIN_FOCUSED
;
3271 } else if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
3272 xw
.state
&= ~WIN_FOCUSED
;
3274 } else if(e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
3275 /* Send SIGHUP to shell */
3282 cresize(int width
, int height
) {
3290 col
= (xw
.w
- 2 * borderpx
) / xw
.cw
;
3291 row
= (xw
.h
- 2 * borderpx
) / xw
.ch
;
3300 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
3303 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
3310 int xfd
= XConnectionNumber(xw
.dpy
), xev
;
3311 struct timeval drawtimeout
, *tv
= NULL
, now
, last
;
3313 gettimeofday(&last
, NULL
);
3315 for(xev
= actionfps
;;) {
3317 FD_SET(cmdfd
, &rfd
);
3319 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
) < 0) {
3322 die("select failed: %s\n", SERRNO
);
3325 gettimeofday(&now
, NULL
);
3326 drawtimeout
.tv_sec
= 0;
3327 drawtimeout
.tv_usec
= (1000/xfps
) * 1000;
3330 if(FD_ISSET(cmdfd
, &rfd
))
3333 if(FD_ISSET(xfd
, &rfd
))
3336 if(TIMEDIFF(now
, last
) > \
3337 (xev
? (1000/xfps
) : (1000/actionfps
))) {
3338 while(XPending(xw
.dpy
)) {
3339 XNextEvent(xw
.dpy
, &ev
);
3340 if(XFilterEvent(&ev
, None
))
3342 if(handler
[ev
.type
])
3343 (handler
[ev
.type
])(&ev
);
3350 if(xev
&& !FD_ISSET(xfd
, &rfd
))
3352 if(!FD_ISSET(cmdfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
))
3360 die("%s " VERSION
" (c) 2010-2013 st engineers\n" \
3361 "usage: st [-a] [-v] [-c class] [-f font] [-g geometry] [-o file]" \
3362 " [-t title] [-w windowid] [-e command ...]\n", argv0
);
3366 main(int argc
, char *argv
[]) {
3370 xw
.fw
= xw
.fh
= xw
.fx
= xw
.fy
= 0;
3375 allowaltscreen
= false;
3378 opt_class
= EARGF(usage());
3381 /* eat all remaining arguments */
3386 opt_font
= EARGF(usage());
3389 bitm
= XParseGeometry(EARGF(usage()), &xr
, &yr
, &wr
, &hr
);
3394 if(bitm
& WidthValue
)
3396 if(bitm
& HeightValue
)
3398 if(bitm
& XNegative
&& xw
.fx
== 0)
3400 if(bitm
& XNegative
&& xw
.fy
== 0)
3403 if(xw
.fh
!= 0 && xw
.fw
!= 0)
3407 opt_io
= EARGF(usage());
3410 opt_title
= EARGF(usage());
3413 opt_embed
= EARGF(usage());
3421 setlocale(LC_CTYPE
, "");
3422 XSetLocaleModifiers("");
3428 cresize(xw
.h
, xw
.w
);