1 /* See LICENSE for licence details. */
15 #include <sys/ioctl.h>
16 #include <sys/select.h>
19 #include <sys/types.h>
24 #include <X11/Xatom.h>
26 #include <X11/Xutil.h>
27 #include <X11/cursorfont.h>
28 #include <X11/keysym.h>
29 #include <X11/Xft/Xft.h>
30 #include <fontconfig/fontconfig.h>
39 #define Draw XftDraw *
40 #define Colour XftColor
41 #define Colourmap Colormap
42 #define Rectangle XRectangle
46 #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
48 #elif defined(__FreeBSD__) || defined(__DragonFly__)
54 #define XEMBED_FOCUS_IN 4
55 #define XEMBED_FOCUS_OUT 5
59 #define ESC_BUF_SIZ (128*UTF_SIZ)
60 #define ESC_ARG_SIZ 16
61 #define STR_BUF_SIZ ESC_BUF_SIZ
62 #define STR_ARG_SIZ ESC_ARG_SIZ
63 #define DRAW_BUF_SIZ 20*1024
64 #define XK_ANY_MOD UINT_MAX
66 #define XK_SWITCH_MOD (1<<13)
68 #define REDRAW_TIMEOUT (80*1000) /* 80 ms */
71 #define SERRNO strerror(errno)
72 #define MIN(a, b) ((a) < (b) ? (a) : (b))
73 #define MAX(a, b) ((a) < (b) ? (b) : (a))
74 #define LEN(a) (sizeof(a) / sizeof(a[0]))
75 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
76 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
77 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
78 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
79 #define IS_SET(flag) ((term.mode & (flag)) != 0)
80 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_usec-t2.tv_usec)/1000)
81 #define CEIL(x) (((x) != (int) (x)) ? (x) + 1 : (x))
83 #define TRUECOLOR(r,g,b) (1 << 24 | (r) << 16 | (g) << 8 | (b))
84 #define IS_TRUECOL(x) (1 << 24 & (x))
85 #define TRUERED(x) (((x) & 0xff0000) >> 8)
86 #define TRUEGREEN(x) (((x) & 0xff00))
87 #define TRUEBLUE(x) (((x) & 0xff) << 8)
90 #define VT102ID "\033[?6c"
92 enum glyph_attribute
{
105 enum cursor_movement
{
123 MODE_MOUSEMOTION
= 64,
128 MODE_APPCURSOR
= 2048,
129 MODE_MOUSESGR
= 4096,
134 MODE_MOUSEX10
= 131072,
135 MODE_MOUSEMANY
= 262144,
136 MODE_BRCKTPASTE
= 524288,
137 MODE_PRINT
= 1048576,
138 MODE_MOUSE
= MODE_MOUSEBTN
|MODE_MOUSEMOTION
|MODE_MOUSEX10\
155 ESC_STR
= 4, /* DSC, OSC, PM, APC */
157 ESC_STR_END
= 16, /* a final string was encountered */
158 ESC_TEST
= 32, /* Enter in test mode */
167 enum selection_type
{
172 enum selection_snap
{
177 typedef unsigned char uchar
;
178 typedef unsigned int uint
;
179 typedef unsigned long ulong
;
180 typedef unsigned short ushort
;
183 char c
[UTF_SIZ
]; /* character code */
184 ushort mode
; /* attribute flags */
185 uint32_t fg
; /* foreground */
186 uint32_t bg
; /* background */
192 Glyph attr
; /* current char attributes */
198 /* CSI Escape sequence structs */
199 /* ESC '[' [[ [<priv>] <arg> [;]] <mode>] */
201 char buf
[ESC_BUF_SIZ
]; /* raw string */
202 int len
; /* raw string length */
204 int arg
[ESC_ARG_SIZ
];
205 int narg
; /* nb of args */
209 /* STR Escape sequence structs */
210 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
212 char type
; /* ESC type ... */
213 char buf
[STR_BUF_SIZ
]; /* raw string */
214 int len
; /* raw string length */
215 char *args
[STR_ARG_SIZ
];
216 int narg
; /* nb of args */
219 /* Internal representation of the screen */
221 int row
; /* nb row */
222 int col
; /* nb col */
223 Line
*line
; /* screen */
224 Line
*alt
; /* alternate screen */
225 bool *dirty
; /* dirtyness of lines */
226 TCursor c
; /* cursor */
227 int top
; /* top scroll limit */
228 int bot
; /* bottom scroll limit */
229 int mode
; /* terminal mode flags */
230 int esc
; /* escape state flags */
231 char trantbl
[4]; /* charset table translation */
232 int charset
; /* current charset */
233 int icharset
; /* selected charset for sequence */
234 bool numlock
; /* lock numbers in keyboard */
238 /* Purely graphic info */
244 Atom xembed
, wmdeletewin
, netwmname
, netwmpid
;
249 XSetWindowAttributes attrs
;
251 bool isfixed
; /* is fixed geometry? */
252 int fx
, fy
, fw
, fh
; /* fixed geometry */
253 int tw
, th
; /* tty width and height */
254 int w
, h
; /* window width and height */
255 int ch
; /* char height */
256 int cw
; /* char width */
257 char state
; /* focus, redraw, visible */
270 /* three valued logic variables: 0 indifferent, 1 on, -1 off */
271 signed char appkey
; /* application keypad */
272 signed char appcursor
; /* application cursor */
273 signed char crlf
; /* crlf mode */
281 * Selection variables:
282 * nb – normalized coordinates of the beginning of the selection
283 * ne – normalized coordinates of the end of the selection
284 * ob – original coordinates of the beginning of the selection
285 * oe – original coordinates of the end of the selection
294 struct timeval tclick1
;
295 struct timeval tclick2
;
308 void (*func
)(const Arg
*);
312 /* function definitions used in config.h */
313 static void clippaste(const Arg
*);
314 static void numlock(const Arg
*);
315 static void selpaste(const Arg
*);
316 static void xzoom(const Arg
*);
317 static void printsel(const Arg
*);
318 static void printscreen(const Arg
*) ;
319 static void toggleprinter(const Arg
*);
321 /* Config.h for applying patches and the configuration. */
337 /* Drawing Context */
339 Colour col
[LEN(colorname
) < 256 ? 256 : LEN(colorname
)];
340 Font font
, bfont
, ifont
, ibfont
;
344 static void die(const char *, ...);
345 static void draw(void);
346 static void redraw(int);
347 static void drawregion(int, int, int, int);
348 static void execsh(void);
349 static void sigchld(int);
350 static void run(void);
352 static void csidump(void);
353 static void csihandle(void);
354 static void csiparse(void);
355 static void csireset(void);
356 static void strdump(void);
357 static void strhandle(void);
358 static void strparse(void);
359 static void strreset(void);
361 static int tattrset(int);
362 static void tprinter(char *s
, size_t len
);
363 static void tdumpsel(void);
364 static void tdumpline(int);
365 static void tdump(void);
366 static void tclearregion(int, int, int, int);
367 static void tcursor(int);
368 static void tdeletechar(int);
369 static void tdeleteline(int);
370 static void tinsertblank(int);
371 static void tinsertblankline(int);
372 static void tmoveto(int, int);
373 static void tmoveato(int x
, int y
);
374 static void tnew(int, int);
375 static void tnewline(int);
376 static void tputtab(bool);
377 static void tputc(char *, int);
378 static void treset(void);
379 static int tresize(int, int);
380 static void tscrollup(int, int);
381 static void tscrolldown(int, int);
382 static void tsetattr(int*, int);
383 static void tsetchar(char *, Glyph
*, int, int);
384 static void tsetscroll(int, int);
385 static void tswapscreen(void);
386 static void tsetdirt(int, int);
387 static void tsetdirtattr(int);
388 static void tsetmode(bool, bool, int *, int);
389 static void tfulldirt(void);
390 static void techo(char *, int);
391 static int32_t tdefcolor(int *, int *, int);
392 static void tselcs(void);
393 static void tdeftran(char);
394 static inline bool match(uint
, uint
);
395 static void ttynew(void);
396 static void ttyread(void);
397 static void ttyresize(void);
398 static void ttysend(char *, size_t);
399 static void ttywrite(const char *, size_t);
401 static void xdraws(char *, Glyph
, int, int, int, int);
402 static void xhints(void);
403 static void xclear(int, int, int, int);
404 static void xdrawcursor(void);
405 static void xinit(void);
406 static void xloadcols(void);
407 static int xsetcolorname(int, const char *);
408 static int xloadfont(Font
*, FcPattern
*);
409 static void xloadfonts(char *, double);
410 static int xloadfontset(Font
*);
411 static void xsettitle(char *);
412 static void xresettitle(void);
413 static void xsetpointermotion(int);
414 static void xseturgency(int);
415 static void xsetsel(char*);
416 static void xtermclear(int, int, int, int);
417 static void xunloadfont(Font
*f
);
418 static void xunloadfonts(void);
419 static void xresize(int, int);
421 static void expose(XEvent
*);
422 static void visibility(XEvent
*);
423 static void unmap(XEvent
*);
424 static char *kmap(KeySym
, uint
);
425 static void kpress(XEvent
*);
426 static void cmessage(XEvent
*);
427 static void cresize(int, int);
428 static void resize(XEvent
*);
429 static void focus(XEvent
*);
430 static void brelease(XEvent
*);
431 static void bpress(XEvent
*);
432 static void bmotion(XEvent
*);
433 static void selnotify(XEvent
*);
434 static void selclear(XEvent
*);
435 static void selrequest(XEvent
*);
437 static void selinit(void);
438 static void selsort(void);
439 static inline bool selected(int, int);
440 static char *getsel(void);
441 static void selcopy(void);
442 static void selscroll(int, int);
443 static void selsnap(int, int *, int *, int);
445 static int utf8decode(char *, long *);
446 static int utf8encode(long *, char *);
447 static int utf8size(char *);
448 static int isfullutf8(char *, int);
450 static ssize_t
xwrite(int, char *, size_t);
451 static void *xmalloc(size_t);
452 static void *xrealloc(void *, size_t);
453 static char *xstrdup(char *s
);
455 static void (*handler
[LASTEvent
])(XEvent
*) = {
457 [ClientMessage
] = cmessage
,
458 [ConfigureNotify
] = resize
,
459 [VisibilityNotify
] = visibility
,
460 [UnmapNotify
] = unmap
,
464 [MotionNotify
] = bmotion
,
465 [ButtonPress
] = bpress
,
466 [ButtonRelease
] = brelease
,
467 [SelectionClear
] = selclear
,
468 [SelectionNotify
] = selnotify
,
469 [SelectionRequest
] = selrequest
,
476 static CSIEscape csiescseq
;
477 static STREscape strescseq
;
480 static Selection sel
;
481 static int iofd
= STDOUT_FILENO
;
482 static char **opt_cmd
= NULL
;
483 static char *opt_io
= NULL
;
484 static char *opt_title
= NULL
;
485 static char *opt_embed
= NULL
;
486 static char *opt_class
= NULL
;
487 static char *opt_font
= NULL
;
488 static int oldbutton
= 3; /* button event on startup: 3 = release */
490 static char *usedfont
= NULL
;
491 static double usedfontsize
= 0;
493 /* Font Ring Cache */
506 /* Fontcache is an array now. A new font will be appended to the array. */
507 static Fontcache frc
[16];
508 static int frclen
= 0;
511 xwrite(int fd
, char *s
, size_t len
) {
515 ssize_t r
= write(fd
, s
, len
);
525 xmalloc(size_t len
) {
526 void *p
= malloc(len
);
529 die("Out of memory\n");
535 xrealloc(void *p
, size_t len
) {
536 if((p
= realloc(p
, len
)) == NULL
)
537 die("Out of memory\n");
547 die("Out of memory\n");
553 utf8decode(char *s
, long *u
) {
559 if(~c
& 0x80) { /* 0xxxxxxx */
562 } else if((c
& 0xE0) == 0xC0) { /* 110xxxxx */
565 } else if((c
& 0xF0) == 0xE0) { /* 1110xxxx */
568 } else if((c
& 0xF8) == 0xF0) { /* 11110xxx */
575 for(i
= n
, ++s
; i
> 0; --i
, ++rtn
, ++s
) {
577 if((c
& 0xC0) != 0x80) /* 10xxxxxx */
583 if((n
== 1 && *u
< 0x80) ||
584 (n
== 2 && *u
< 0x800) ||
585 (n
== 3 && *u
< 0x10000) ||
586 (*u
>= 0xD800 && *u
<= 0xDFFF)) {
598 utf8encode(long *u
, char *s
) {
606 *sp
= uc
; /* 0xxxxxxx */
608 } else if(*u
< 0x800) {
609 *sp
= (uc
>> 6) | 0xC0; /* 110xxxxx */
611 } else if(uc
< 0x10000) {
612 *sp
= (uc
>> 12) | 0xE0; /* 1110xxxx */
614 } else if(uc
<= 0x10FFFF) {
615 *sp
= (uc
>> 18) | 0xF0; /* 11110xxx */
621 for(i
=n
,++sp
; i
>0; --i
,++sp
)
622 *sp
= ((uc
>> 6*(i
-1)) & 0x3F) | 0x80; /* 10xxxxxx */
634 /* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode
635 UTF-8 otherwise return 0 */
637 isfullutf8(char *s
, int b
) {
645 } else if((*c1
& 0xE0) == 0xC0 && b
== 1) {
647 } else if((*c1
& 0xF0) == 0xE0 &&
649 ((b
== 2) && (*c2
& 0xC0) == 0x80))) {
651 } else if((*c1
& 0xF8) == 0xF0 &&
653 ((b
== 2) && (*c2
& 0xC0) == 0x80) ||
654 ((b
== 3) && (*c2
& 0xC0) == 0x80 && (*c3
& 0xC0) == 0x80))) {
667 } else if((c
& 0xE0) == 0xC0) {
669 } else if((c
& 0xF0) == 0xE0) {
678 memset(&sel
.tclick1
, 0, sizeof(sel
.tclick1
));
679 memset(&sel
.tclick2
, 0, sizeof(sel
.tclick2
));
683 sel
.xtarget
= XInternAtom(xw
.dpy
, "UTF8_STRING", 0);
684 if(sel
.xtarget
== None
)
685 sel
.xtarget
= XA_STRING
;
693 return LIMIT(x
, 0, term
.col
-1);
701 return LIMIT(y
, 0, term
.row
-1);
706 if(sel
.ob
.y
== sel
.oe
.y
) {
707 sel
.nb
.x
= MIN(sel
.ob
.x
, sel
.oe
.x
);
708 sel
.ne
.x
= MAX(sel
.ob
.x
, sel
.oe
.x
);
710 sel
.nb
.x
= sel
.ob
.y
< sel
.oe
.y
? sel
.ob
.x
: sel
.oe
.x
;
711 sel
.ne
.x
= sel
.ob
.y
< sel
.oe
.y
? sel
.oe
.x
: sel
.ob
.x
;
713 sel
.nb
.y
= MIN(sel
.ob
.y
, sel
.oe
.y
);
714 sel
.ne
.y
= MAX(sel
.ob
.y
, sel
.oe
.y
);
718 selected(int x
, int y
) {
719 if(sel
.ne
.y
== y
&& sel
.nb
.y
== y
)
720 return BETWEEN(x
, sel
.nb
.x
, sel
.ne
.x
);
722 if(sel
.type
== SEL_RECTANGULAR
) {
723 return ((sel
.nb
.y
<= y
&& y
<= sel
.ne
.y
)
724 && (sel
.nb
.x
<= x
&& x
<= sel
.ne
.x
));
727 return ((sel
.nb
.y
< y
&& y
< sel
.ne
.y
)
728 || (y
== sel
.ne
.y
&& x
<= sel
.ne
.x
))
729 || (y
== sel
.nb
.y
&& x
>= sel
.nb
.x
730 && (x
<= sel
.ne
.x
|| sel
.nb
.y
!= sel
.ne
.y
));
734 selsnap(int mode
, int *x
, int *y
, int direction
) {
740 * Snap around if the word wraps around at the end or
741 * beginning of a line.
744 if(direction
< 0 && *x
<= 0) {
745 if(*y
> 0 && term
.line
[*y
- 1][term
.col
-1].mode
753 if(direction
> 0 && *x
>= term
.col
-1) {
754 if(*y
< term
.row
-1 && term
.line
[*y
][*x
].mode
763 if(term
.line
[*y
][*x
+direction
].mode
& ATTR_WDUMMY
) {
768 if(strchr(worddelimiters
,
769 term
.line
[*y
][*x
+direction
].c
[0])) {
778 * Snap around if the the previous line or the current one
779 * has set ATTR_WRAP at its end. Then the whole next or
780 * previous line will be selected.
782 *x
= (direction
< 0) ? 0 : term
.col
- 1;
783 if(direction
< 0 && *y
> 0) {
784 for(; *y
> 0; *y
+= direction
) {
785 if(!(term
.line
[*y
-1][term
.col
-1].mode
790 } else if(direction
> 0 && *y
< term
.row
-1) {
791 for(; *y
< term
.row
; *y
+= direction
) {
792 if(!(term
.line
[*y
][term
.col
-1].mode
801 * Select the whole line when the end of line is reached.
805 while(--i
> 0 && term
.line
[*y
][i
].c
[0] == ' ')
815 getbuttoninfo(XEvent
*e
) {
817 uint state
= e
->xbutton
.state
&~Button1Mask
;
819 sel
.alt
= IS_SET(MODE_ALTSCREEN
);
821 sel
.oe
.x
= x2col(e
->xbutton
.x
);
822 sel
.oe
.y
= y2row(e
->xbutton
.y
);
824 if(sel
.ob
.y
< sel
.oe
.y
825 || (sel
.ob
.y
== sel
.oe
.y
&& sel
.ob
.x
< sel
.oe
.x
)) {
826 selsnap(sel
.snap
, &sel
.ob
.x
, &sel
.ob
.y
, -1);
827 selsnap(sel
.snap
, &sel
.oe
.x
, &sel
.oe
.y
, +1);
829 selsnap(sel
.snap
, &sel
.oe
.x
, &sel
.oe
.y
, -1);
830 selsnap(sel
.snap
, &sel
.ob
.x
, &sel
.ob
.y
, +1);
834 sel
.type
= SEL_REGULAR
;
835 for(type
= 1; type
< LEN(selmasks
); ++type
) {
836 if(match(selmasks
[type
], state
)) {
844 mousereport(XEvent
*e
) {
845 int x
= x2col(e
->xbutton
.x
), y
= y2row(e
->xbutton
.y
),
846 button
= e
->xbutton
.button
, state
= e
->xbutton
.state
,
852 if(e
->xbutton
.type
== MotionNotify
) {
853 if(x
== ox
&& y
== oy
)
855 if(!IS_SET(MODE_MOUSEMOTION
) && !IS_SET(MODE_MOUSEMANY
))
857 /* MOUSE_MOTION: no reporting if no button is pressed */
858 if(IS_SET(MODE_MOUSEMOTION
) && oldbutton
== 3)
861 button
= oldbutton
+ 32;
865 if(!IS_SET(MODE_MOUSESGR
) && e
->xbutton
.type
== ButtonRelease
) {
872 if(e
->xbutton
.type
== ButtonPress
) {
876 } else if(e
->xbutton
.type
== ButtonRelease
) {
878 /* MODE_MOUSEX10: no button release reporting */
879 if(IS_SET(MODE_MOUSEX10
))
884 if(!IS_SET(MODE_MOUSEX10
)) {
885 button
+= (state
& ShiftMask
? 4 : 0)
886 + (state
& Mod4Mask
? 8 : 0)
887 + (state
& ControlMask
? 16 : 0);
891 if(IS_SET(MODE_MOUSESGR
)) {
892 len
= snprintf(buf
, sizeof(buf
), "\033[<%d;%d;%d%c",
894 e
->xbutton
.type
== ButtonRelease
? 'm' : 'M');
895 } else if(x
< 223 && y
< 223) {
896 len
= snprintf(buf
, sizeof(buf
), "\033[M%c%c%c",
897 32+button
, 32+x
+1, 32+y
+1);
910 if(IS_SET(MODE_MOUSE
)) {
915 for(mk
= mshortcuts
; mk
< mshortcuts
+ LEN(mshortcuts
); mk
++) {
916 if(e
->xbutton
.button
== mk
->b
917 && match(mk
->mask
, e
->xbutton
.state
)) {
918 ttysend(mk
->s
, strlen(mk
->s
));
923 if(e
->xbutton
.button
== Button1
) {
924 gettimeofday(&now
, NULL
);
926 /* Clear previous selection, logically and visually. */
929 sel
.type
= SEL_REGULAR
;
930 sel
.oe
.x
= sel
.ob
.x
= x2col(e
->xbutton
.x
);
931 sel
.oe
.y
= sel
.ob
.y
= y2row(e
->xbutton
.y
);
934 * If the user clicks below predefined timeouts specific
935 * snapping behaviour is exposed.
937 if(TIMEDIFF(now
, sel
.tclick2
) <= tripleclicktimeout
) {
938 sel
.snap
= SNAP_LINE
;
939 } else if(TIMEDIFF(now
, sel
.tclick1
) <= doubleclicktimeout
) {
940 sel
.snap
= SNAP_WORD
;
944 selsnap(sel
.snap
, &sel
.ob
.x
, &sel
.ob
.y
, -1);
945 selsnap(sel
.snap
, &sel
.oe
.x
, &sel
.oe
.y
, +1);
949 * Draw selection, unless it's regular and we don't want to
950 * make clicks visible
954 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
956 sel
.tclick2
= sel
.tclick1
;
964 int x
, y
, bufsize
, size
, i
, ex
;
970 bufsize
= (term
.col
+1) * (sel
.ne
.y
-sel
.nb
.y
+1) * UTF_SIZ
;
971 ptr
= str
= xmalloc(bufsize
);
973 /* append every set & selected glyph to the selection */
974 for(y
= sel
.nb
.y
; y
< sel
.ne
.y
+ 1; y
++) {
975 gp
= &term
.line
[y
][0];
976 last
= &gp
[term
.col
-1];
978 while(last
>= gp
&& !(selected(last
- gp
, y
) &&
979 strcmp(last
->c
, " ") != 0)) {
983 for(x
= 0; gp
<= last
; x
++, ++gp
) {
984 if(!selected(x
, y
) || (gp
->mode
& ATTR_WDUMMY
))
987 size
= utf8size(gp
->c
);
988 memcpy(ptr
, gp
->c
, size
);
993 * Copy and pasting of line endings is inconsistent
994 * in the inconsistent terminal and GUI world.
995 * The best solution seems like to produce '\n' when
996 * something is copied from st and convert '\n' to
997 * '\r', when something to be pasted is received by
999 * FIXME: Fix the computer world.
1001 if(y
< sel
.ne
.y
&& x
> 0 && !((gp
-1)->mode
& ATTR_WRAP
))
1005 * If the last selected line expands in the selection
1006 * after the visible text '\n' is appended.
1010 while(--i
> 0 && term
.line
[y
][i
].c
[0] == ' ')
1013 if(sel
.nb
.y
== sel
.ne
.y
&& sel
.ne
.x
< sel
.nb
.x
)
1030 selnotify(XEvent
*e
) {
1031 ulong nitems
, ofs
, rem
;
1033 uchar
*data
, *last
, *repl
;
1038 if(XGetWindowProperty(xw
.dpy
, xw
.win
, XA_PRIMARY
, ofs
, BUFSIZ
/4,
1039 False
, AnyPropertyType
, &type
, &format
,
1040 &nitems
, &rem
, &data
)) {
1041 fprintf(stderr
, "Clipboard allocation failed\n");
1046 * As seen in selcopy:
1047 * Line endings are inconsistent in the terminal and GUI world
1048 * copy and pasting. When receiving some selection data,
1049 * replace all '\n' with '\r'.
1050 * FIXME: Fix the computer world.
1053 last
= data
+ nitems
* format
/ 8;
1054 while((repl
= memchr(repl
, '\n', last
- repl
))) {
1058 if(IS_SET(MODE_BRCKTPASTE
))
1059 ttywrite("\033[200~", 6);
1060 ttysend((char *)data
, nitems
* format
/ 8);
1061 if(IS_SET(MODE_BRCKTPASTE
))
1062 ttywrite("\033[201~", 6);
1064 /* number of 32-bit chunks returned */
1065 ofs
+= nitems
* format
/ 32;
1070 selpaste(const Arg
*dummy
) {
1071 XConvertSelection(xw
.dpy
, XA_PRIMARY
, sel
.xtarget
, XA_PRIMARY
,
1072 xw
.win
, CurrentTime
);
1076 clippaste(const Arg
*dummy
) {
1079 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1080 XConvertSelection(xw
.dpy
, clipboard
, sel
.xtarget
, XA_PRIMARY
,
1081 xw
.win
, CurrentTime
);
1085 selclear(XEvent
*e
) {
1089 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
1093 selrequest(XEvent
*e
) {
1094 XSelectionRequestEvent
*xsre
;
1095 XSelectionEvent xev
;
1096 Atom xa_targets
, string
;
1098 xsre
= (XSelectionRequestEvent
*) e
;
1099 xev
.type
= SelectionNotify
;
1100 xev
.requestor
= xsre
->requestor
;
1101 xev
.selection
= xsre
->selection
;
1102 xev
.target
= xsre
->target
;
1103 xev
.time
= xsre
->time
;
1105 xev
.property
= None
;
1107 xa_targets
= XInternAtom(xw
.dpy
, "TARGETS", 0);
1108 if(xsre
->target
== xa_targets
) {
1109 /* respond with the supported type */
1110 string
= sel
.xtarget
;
1111 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
1112 XA_ATOM
, 32, PropModeReplace
,
1113 (uchar
*) &string
, 1);
1114 xev
.property
= xsre
->property
;
1115 } else if(xsre
->target
== sel
.xtarget
&& sel
.clip
!= NULL
) {
1116 XChangeProperty(xsre
->display
, xsre
->requestor
, xsre
->property
,
1117 xsre
->target
, 8, PropModeReplace
,
1118 (uchar
*) sel
.clip
, strlen(sel
.clip
));
1119 xev
.property
= xsre
->property
;
1122 /* all done, send a notification to the listener */
1123 if(!XSendEvent(xsre
->display
, xsre
->requestor
, True
, 0, (XEvent
*) &xev
))
1124 fprintf(stderr
, "Error sending SelectionNotify event\n");
1128 xsetsel(char *str
) {
1129 /* register the selection for both the clipboard and the primary */
1135 XSetSelectionOwner(xw
.dpy
, XA_PRIMARY
, xw
.win
, CurrentTime
);
1137 clipboard
= XInternAtom(xw
.dpy
, "CLIPBOARD", 0);
1138 XSetSelectionOwner(xw
.dpy
, clipboard
, xw
.win
, CurrentTime
);
1142 brelease(XEvent
*e
) {
1143 if(IS_SET(MODE_MOUSE
)) {
1148 if(e
->xbutton
.button
== Button2
) {
1150 } else if(e
->xbutton
.button
== Button1
) {
1158 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
1163 bmotion(XEvent
*e
) {
1164 int oldey
, oldex
, oldsby
, oldsey
;
1166 if(IS_SET(MODE_MOUSE
)) {
1181 if(oldey
!= sel
.oe
.y
|| oldex
!= sel
.oe
.x
)
1182 tsetdirt(MIN(sel
.nb
.y
, oldsby
), MAX(sel
.ne
.y
, oldsey
));
1186 die(const char *errstr
, ...) {
1189 va_start(ap
, errstr
);
1190 vfprintf(stderr
, errstr
, ap
);
1198 char *envshell
= getenv("SHELL");
1199 const struct passwd
*pass
= getpwuid(getuid());
1200 char buf
[sizeof(long) * 8 + 1];
1202 unsetenv("COLUMNS");
1204 unsetenv("TERMCAP");
1207 setenv("LOGNAME", pass
->pw_name
, 1);
1208 setenv("USER", pass
->pw_name
, 1);
1209 setenv("SHELL", pass
->pw_shell
, 0);
1210 setenv("HOME", pass
->pw_dir
, 0);
1213 snprintf(buf
, sizeof(buf
), "%lu", xw
.win
);
1214 setenv("WINDOWID", buf
, 1);
1216 signal(SIGCHLD
, SIG_DFL
);
1217 signal(SIGHUP
, SIG_DFL
);
1218 signal(SIGINT
, SIG_DFL
);
1219 signal(SIGQUIT
, SIG_DFL
);
1220 signal(SIGTERM
, SIG_DFL
);
1221 signal(SIGALRM
, SIG_DFL
);
1223 DEFAULT(envshell
, shell
);
1224 setenv("TERM", termname
, 1);
1225 args
= opt_cmd
? opt_cmd
: (char *[]){envshell
, "-i", NULL
};
1226 execvp(args
[0], args
);
1234 if(waitpid(pid
, &stat
, 0) < 0)
1235 die("Waiting for pid %hd failed: %s\n", pid
, SERRNO
);
1237 if(WIFEXITED(stat
)) {
1238 exit(WEXITSTATUS(stat
));
1247 struct winsize w
= {term
.row
, term
.col
, 0, 0};
1249 /* seems to work fine on linux, openbsd and freebsd */
1250 if(openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
1251 die("openpty failed: %s\n", SERRNO
);
1253 switch(pid
= fork()) {
1255 die("fork failed\n");
1258 setsid(); /* create a new process group */
1259 dup2(s
, STDIN_FILENO
);
1260 dup2(s
, STDOUT_FILENO
);
1261 dup2(s
, STDERR_FILENO
);
1262 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
1263 die("ioctl TIOCSCTTY failed: %s\n", SERRNO
);
1271 signal(SIGCHLD
, sigchld
);
1273 term
.mode
|= MODE_PRINT
;
1274 iofd
= (!strcmp(opt_io
, "-")) ?
1276 open(opt_io
, O_WRONLY
| O_CREAT
, 0666);
1278 fprintf(stderr
, "Error opening %s:%s\n",
1279 opt_io
, strerror(errno
));
1289 fprintf(stderr
, " %02x '%c' ", c
, isprint(c
)?c
:'.');
1291 fprintf(stderr
, "\n");
1296 static char buf
[BUFSIZ
];
1297 static int buflen
= 0;
1300 int charsize
; /* size of utf8 char in bytes */
1304 /* append read bytes to unprocessed bytes */
1305 if((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
1306 die("Couldn't read from shell: %s\n", SERRNO
);
1308 /* process every complete utf8 char */
1311 while(buflen
>= UTF_SIZ
|| isfullutf8(ptr
,buflen
)) {
1312 charsize
= utf8decode(ptr
, &utf8c
);
1313 utf8encode(&utf8c
, s
);
1319 /* keep any uncomplete utf8 char for the next call */
1320 memmove(buf
, ptr
, buflen
);
1324 ttywrite(const char *s
, size_t n
) {
1325 if(write(cmdfd
, s
, n
) == -1)
1326 die("write error on tty: %s\n", SERRNO
);
1330 ttysend(char *s
, size_t n
) {
1332 if(IS_SET(MODE_ECHO
))
1340 w
.ws_row
= term
.row
;
1341 w
.ws_col
= term
.col
;
1342 w
.ws_xpixel
= xw
.tw
;
1343 w
.ws_ypixel
= xw
.th
;
1344 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
1345 fprintf(stderr
, "Couldn't set window size: %s\n", SERRNO
);
1349 tattrset(int attr
) {
1352 for(i
= 0; i
< term
.row
-1; i
++) {
1353 for(j
= 0; j
< term
.col
-1; j
++) {
1354 if(term
.line
[i
][j
].mode
& attr
)
1363 tsetdirt(int top
, int bot
) {
1366 LIMIT(top
, 0, term
.row
-1);
1367 LIMIT(bot
, 0, term
.row
-1);
1369 for(i
= top
; i
<= bot
; i
++)
1374 tsetdirtattr(int attr
) {
1377 for(i
= 0; i
< term
.row
-1; i
++) {
1378 for(j
= 0; j
< term
.col
-1; j
++) {
1379 if(term
.line
[i
][j
].mode
& attr
) {
1389 tsetdirt(0, term
.row
-1);
1394 static TCursor c
[2];
1395 bool alt
= IS_SET(MODE_ALTSCREEN
);
1397 if(mode
== CURSOR_SAVE
) {
1399 } else if(mode
== CURSOR_LOAD
) {
1401 tmoveto(c
[alt
].x
, c
[alt
].y
);
1409 term
.c
= (TCursor
){{
1413 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
1415 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1416 for(i
= tabspaces
; i
< term
.col
; i
+= tabspaces
)
1419 term
.bot
= term
.row
- 1;
1420 term
.mode
= MODE_WRAP
;
1421 memset(term
.trantbl
, sizeof(term
.trantbl
), CS_USA
);
1424 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1426 tcursor(CURSOR_SAVE
);
1430 tnew(int col
, int row
) {
1431 term
= (Term
){ .c
= { .attr
= { .fg
= defaultfg
, .bg
= defaultbg
} } };
1440 Line
*tmp
= term
.line
;
1442 term
.line
= term
.alt
;
1444 term
.mode
^= MODE_ALTSCREEN
;
1449 tscrolldown(int orig
, int n
) {
1453 LIMIT(n
, 0, term
.bot
-orig
+1);
1455 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
1457 for(i
= term
.bot
; i
>= orig
+n
; i
--) {
1458 temp
= term
.line
[i
];
1459 term
.line
[i
] = term
.line
[i
-n
];
1460 term
.line
[i
-n
] = temp
;
1463 term
.dirty
[i
-n
] = 1;
1470 tscrollup(int orig
, int n
) {
1473 LIMIT(n
, 0, term
.bot
-orig
+1);
1475 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
1477 for(i
= orig
; i
<= term
.bot
-n
; i
++) {
1478 temp
= term
.line
[i
];
1479 term
.line
[i
] = term
.line
[i
+n
];
1480 term
.line
[i
+n
] = temp
;
1483 term
.dirty
[i
+n
] = 1;
1486 selscroll(orig
, -n
);
1490 selscroll(int orig
, int n
) {
1494 if(BETWEEN(sel
.ob
.y
, orig
, term
.bot
) || BETWEEN(sel
.oe
.y
, orig
, term
.bot
)) {
1495 if((sel
.ob
.y
+= n
) > term
.bot
|| (sel
.oe
.y
+= n
) < term
.top
) {
1499 if(sel
.type
== SEL_RECTANGULAR
) {
1500 if(sel
.ob
.y
< term
.top
)
1501 sel
.ob
.y
= term
.top
;
1502 if(sel
.oe
.y
> term
.bot
)
1503 sel
.oe
.y
= term
.bot
;
1505 if(sel
.ob
.y
< term
.top
) {
1506 sel
.ob
.y
= term
.top
;
1509 if(sel
.oe
.y
> term
.bot
) {
1510 sel
.oe
.y
= term
.bot
;
1511 sel
.oe
.x
= term
.col
;
1519 tnewline(int first_col
) {
1523 tscrollup(term
.top
, 1);
1527 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1532 char *p
= csiescseq
.buf
, *np
;
1541 csiescseq
.buf
[csiescseq
.len
] = '\0';
1542 while(p
< csiescseq
.buf
+csiescseq
.len
) {
1544 v
= strtol(p
, &np
, 10);
1547 if(v
== LONG_MAX
|| v
== LONG_MIN
)
1549 csiescseq
.arg
[csiescseq
.narg
++] = v
;
1551 if(*p
!= ';' || csiescseq
.narg
== ESC_ARG_SIZ
)
1555 csiescseq
.mode
= *p
;
1558 /* for absolute user moves, when decom is set */
1560 tmoveato(int x
, int y
) {
1561 tmoveto(x
, y
+ ((term
.c
.state
& CURSOR_ORIGIN
) ? term
.top
: 0));
1565 tmoveto(int x
, int y
) {
1568 if(term
.c
.state
& CURSOR_ORIGIN
) {
1573 maxy
= term
.row
- 1;
1575 LIMIT(x
, 0, term
.col
-1);
1576 LIMIT(y
, miny
, maxy
);
1577 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1583 tsetchar(char *c
, Glyph
*attr
, int x
, int y
) {
1584 static char *vt100_0
[62] = { /* 0x41 - 0x7e */
1585 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1586 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1587 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1588 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1589 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1590 "", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1591 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1592 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1596 * The table is proudly stolen from rxvt.
1598 if(attr
->mode
& ATTR_GFX
) {
1599 if(c
[0] >= 0x41 && c
[0] <= 0x7e
1600 && vt100_0
[c
[0] - 0x41]) {
1601 c
= vt100_0
[c
[0] - 0x41];
1605 if(term
.line
[y
][x
].mode
& ATTR_WIDE
) {
1606 if(x
+1 < term
.col
) {
1607 term
.line
[y
][x
+1].c
[0] = ' ';
1608 term
.line
[y
][x
+1].mode
&= ~ATTR_WDUMMY
;
1610 } else if(term
.line
[y
][x
].mode
& ATTR_WDUMMY
) {
1611 term
.line
[y
][x
-1].c
[0] = ' ';
1612 term
.line
[y
][x
-1].mode
&= ~ATTR_WIDE
;
1616 term
.line
[y
][x
] = *attr
;
1617 memcpy(term
.line
[y
][x
].c
, c
, UTF_SIZ
);
1621 tclearregion(int x1
, int y1
, int x2
, int y2
) {
1625 temp
= x1
, x1
= x2
, x2
= temp
;
1627 temp
= y1
, y1
= y2
, y2
= temp
;
1629 LIMIT(x1
, 0, term
.col
-1);
1630 LIMIT(x2
, 0, term
.col
-1);
1631 LIMIT(y1
, 0, term
.row
-1);
1632 LIMIT(y2
, 0, term
.row
-1);
1634 for(y
= y1
; y
<= y2
; y
++) {
1636 for(x
= x1
; x
<= x2
; x
++) {
1639 term
.line
[y
][x
] = term
.c
.attr
;
1640 memcpy(term
.line
[y
][x
].c
, " ", 2);
1646 tdeletechar(int n
) {
1647 int src
= term
.c
.x
+ n
;
1649 int size
= term
.col
- src
;
1651 term
.dirty
[term
.c
.y
] = 1;
1653 if(src
>= term
.col
) {
1654 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1658 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1659 size
* sizeof(Glyph
));
1660 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1664 tinsertblank(int n
) {
1667 int size
= term
.col
- dst
;
1669 term
.dirty
[term
.c
.y
] = 1;
1671 if(dst
>= term
.col
) {
1672 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1676 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
],
1677 size
* sizeof(Glyph
));
1678 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1682 tinsertblankline(int n
) {
1683 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1686 tscrolldown(term
.c
.y
, n
);
1690 tdeleteline(int n
) {
1691 if(term
.c
.y
< term
.top
|| term
.c
.y
> term
.bot
)
1694 tscrollup(term
.c
.y
, n
);
1698 tdefcolor(int *attr
, int *npar
, int l
) {
1702 switch (attr
[*npar
+ 1]) {
1703 case 2: /* direct colour in RGB space */
1704 if (*npar
+ 4 >= l
) {
1706 "erresc(38): Incorrect number of parameters (%d)\n",
1710 r
= attr
[*npar
+ 2];
1711 g
= attr
[*npar
+ 3];
1712 b
= attr
[*npar
+ 4];
1714 if(!BETWEEN(r
, 0, 255) || !BETWEEN(g
, 0, 255) || !BETWEEN(b
, 0, 255))
1715 fprintf(stderr
, "erresc: bad rgb color (%d,%d,%d)\n",
1718 idx
= TRUECOLOR(r
, g
, b
);
1720 case 5: /* indexed colour */
1721 if (*npar
+ 2 >= l
) {
1723 "erresc(38): Incorrect number of parameters (%d)\n",
1728 if(!BETWEEN(attr
[*npar
], 0, 255))
1729 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[*npar
]);
1733 case 0: /* implemented defined (only foreground) */
1734 case 1: /* transparent */
1735 case 3: /* direct colour in CMY space */
1736 case 4: /* direct colour in CMYK space */
1739 "erresc(38): gfx attr %d unknown\n", attr
[*npar
]);
1746 tsetattr(int *attr
, int l
) {
1750 for(i
= 0; i
< l
; i
++) {
1753 term
.c
.attr
.mode
&= ~(ATTR_REVERSE
| ATTR_UNDERLINE \
1754 | ATTR_BOLD
| ATTR_ITALIC \
1756 term
.c
.attr
.fg
= defaultfg
;
1757 term
.c
.attr
.bg
= defaultbg
;
1760 term
.c
.attr
.mode
|= ATTR_BOLD
;
1763 term
.c
.attr
.mode
|= ATTR_ITALIC
;
1766 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1768 case 5: /* slow blink */
1769 case 6: /* rapid blink */
1770 term
.c
.attr
.mode
|= ATTR_BLINK
;
1773 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1777 term
.c
.attr
.mode
&= ~ATTR_BOLD
;
1780 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
1783 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1787 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
1790 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1793 if ((idx
= tdefcolor(attr
, &i
, l
)) >= 0)
1794 term
.c
.attr
.fg
= idx
;
1797 term
.c
.attr
.fg
= defaultfg
;
1800 if ((idx
= tdefcolor(attr
, &i
, l
)) >= 0)
1801 term
.c
.attr
.bg
= idx
;
1804 term
.c
.attr
.bg
= defaultbg
;
1807 if(BETWEEN(attr
[i
], 30, 37)) {
1808 term
.c
.attr
.fg
= attr
[i
] - 30;
1809 } else if(BETWEEN(attr
[i
], 40, 47)) {
1810 term
.c
.attr
.bg
= attr
[i
] - 40;
1811 } else if(BETWEEN(attr
[i
], 90, 97)) {
1812 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1813 } else if(BETWEEN(attr
[i
], 100, 107)) {
1814 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
1817 "erresc(default): gfx attr %d unknown\n",
1818 attr
[i
]), csidump();
1826 tsetscroll(int t
, int b
) {
1829 LIMIT(t
, 0, term
.row
-1);
1830 LIMIT(b
, 0, term
.row
-1);
1840 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
1843 tsetmode(bool priv
, bool set
, int *args
, int narg
) {
1847 for(lim
= args
+ narg
; args
< lim
; ++args
) {
1851 case 1: /* DECCKM -- Cursor key */
1852 MODBIT(term
.mode
, set
, MODE_APPCURSOR
);
1854 case 5: /* DECSCNM -- Reverse video */
1856 MODBIT(term
.mode
, set
, MODE_REVERSE
);
1857 if(mode
!= term
.mode
)
1858 redraw(REDRAW_TIMEOUT
);
1860 case 6: /* DECOM -- Origin */
1861 MODBIT(term
.c
.state
, set
, CURSOR_ORIGIN
);
1864 case 7: /* DECAWM -- Auto wrap */
1865 MODBIT(term
.mode
, set
, MODE_WRAP
);
1867 case 0: /* Error (IGNORED) */
1868 case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
1869 case 3: /* DECCOLM -- Column (IGNORED) */
1870 case 4: /* DECSCLM -- Scroll (IGNORED) */
1871 case 8: /* DECARM -- Auto repeat (IGNORED) */
1872 case 18: /* DECPFF -- Printer feed (IGNORED) */
1873 case 19: /* DECPEX -- Printer extent (IGNORED) */
1874 case 42: /* DECNRCM -- National characters (IGNORED) */
1875 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1877 case 25: /* DECTCEM -- Text Cursor Enable Mode */
1878 MODBIT(term
.mode
, !set
, MODE_HIDE
);
1880 case 9: /* X10 mouse compatibility mode */
1881 xsetpointermotion(0);
1882 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1883 MODBIT(term
.mode
, set
, MODE_MOUSEX10
);
1885 case 1000: /* 1000: report button press */
1886 xsetpointermotion(0);
1887 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1888 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
1890 case 1002: /* 1002: report motion on button press */
1891 xsetpointermotion(0);
1892 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1893 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
1895 case 1003: /* 1003: enable all mouse motions */
1896 xsetpointermotion(set
);
1897 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1898 MODBIT(term
.mode
, set
, MODE_MOUSEMANY
);
1900 case 1004: /* 1004: send focus events to tty */
1901 MODBIT(term
.mode
, set
, MODE_FOCUS
);
1903 case 1006: /* 1006: extended reporting mode */
1904 MODBIT(term
.mode
, set
, MODE_MOUSESGR
);
1907 MODBIT(term
.mode
, set
, MODE_8BIT
);
1909 case 1049: /* swap screen & set/restore cursor as xterm */
1910 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1911 case 47: /* swap screen */
1913 if (!allowaltscreen
)
1915 alt
= IS_SET(MODE_ALTSCREEN
);
1917 tclearregion(0, 0, term
.col
-1,
1920 if(set
^ alt
) /* set is always 1 or 0 */
1926 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1928 case 2004: /* 2004: bracketed paste mode */
1929 MODBIT(term
.mode
, set
, MODE_BRCKTPASTE
);
1931 /* Not implemented mouse modes. See comments there. */
1932 case 1001: /* mouse highlight mode; can hang the
1933 terminal by design when implemented. */
1934 case 1005: /* UTF-8 mouse mode; will confuse
1935 applications not supporting UTF-8
1937 case 1015: /* urxvt mangled mouse mode; incompatible
1938 and can be mistaken for other control
1942 "erresc: unknown private set/reset mode %d\n",
1948 case 0: /* Error (IGNORED) */
1950 case 2: /* KAM -- keyboard action */
1951 MODBIT(term
.mode
, set
, MODE_KBDLOCK
);
1953 case 4: /* IRM -- Insertion-replacement */
1954 MODBIT(term
.mode
, set
, MODE_INSERT
);
1956 case 12: /* SRM -- Send/Receive */
1957 MODBIT(term
.mode
, !set
, MODE_ECHO
);
1959 case 20: /* LNM -- Linefeed/new line */
1960 MODBIT(term
.mode
, set
, MODE_CRLF
);
1964 "erresc: unknown set/reset mode %d\n",
1977 switch(csiescseq
.mode
) {
1980 fprintf(stderr
, "erresc: unknown csi ");
1984 case '@': /* ICH -- Insert <n> blank char */
1985 DEFAULT(csiescseq
.arg
[0], 1);
1986 tinsertblank(csiescseq
.arg
[0]);
1988 case 'A': /* CUU -- Cursor <n> Up */
1989 DEFAULT(csiescseq
.arg
[0], 1);
1990 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
1992 case 'B': /* CUD -- Cursor <n> Down */
1993 case 'e': /* VPR --Cursor <n> Down */
1994 DEFAULT(csiescseq
.arg
[0], 1);
1995 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
1997 case 'i': /* MC -- Media Copy */
1998 switch(csiescseq
.arg
[0]) {
2003 tdumpline(term
.c
.y
);
2009 term
.mode
&= ~MODE_PRINT
;
2012 term
.mode
|= MODE_PRINT
;
2016 case 'c': /* DA -- Device Attributes */
2017 if(csiescseq
.arg
[0] == 0)
2018 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
2020 case 'C': /* CUF -- Cursor <n> Forward */
2021 case 'a': /* HPR -- Cursor <n> Forward */
2022 DEFAULT(csiescseq
.arg
[0], 1);
2023 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
2025 case 'D': /* CUB -- Cursor <n> Backward */
2026 DEFAULT(csiescseq
.arg
[0], 1);
2027 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
2029 case 'E': /* CNL -- Cursor <n> Down and first col */
2030 DEFAULT(csiescseq
.arg
[0], 1);
2031 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
2033 case 'F': /* CPL -- Cursor <n> Up and first col */
2034 DEFAULT(csiescseq
.arg
[0], 1);
2035 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
2037 case 'g': /* TBC -- Tabulation clear */
2038 switch(csiescseq
.arg
[0]) {
2039 case 0: /* clear current tab stop */
2040 term
.tabs
[term
.c
.x
] = 0;
2042 case 3: /* clear all the tabs */
2043 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
2049 case 'G': /* CHA -- Move to <col> */
2051 DEFAULT(csiescseq
.arg
[0], 1);
2052 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
2054 case 'H': /* CUP -- Move to <row> <col> */
2056 DEFAULT(csiescseq
.arg
[0], 1);
2057 DEFAULT(csiescseq
.arg
[1], 1);
2058 tmoveato(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
2060 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
2061 DEFAULT(csiescseq
.arg
[0], 1);
2062 while(csiescseq
.arg
[0]--)
2065 case 'J': /* ED -- Clear screen */
2067 switch(csiescseq
.arg
[0]) {
2069 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
2070 if(term
.c
.y
< term
.row
-1) {
2071 tclearregion(0, term
.c
.y
+1, term
.col
-1,
2077 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
2078 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
2081 tclearregion(0, 0, term
.col
-1, term
.row
-1);
2087 case 'K': /* EL -- Clear line */
2088 switch(csiescseq
.arg
[0]) {
2090 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1,
2094 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
2097 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
2101 case 'S': /* SU -- Scroll <n> line up */
2102 DEFAULT(csiescseq
.arg
[0], 1);
2103 tscrollup(term
.top
, csiescseq
.arg
[0]);
2105 case 'T': /* SD -- Scroll <n> line down */
2106 DEFAULT(csiescseq
.arg
[0], 1);
2107 tscrolldown(term
.top
, csiescseq
.arg
[0]);
2109 case 'L': /* IL -- Insert <n> blank lines */
2110 DEFAULT(csiescseq
.arg
[0], 1);
2111 tinsertblankline(csiescseq
.arg
[0]);
2113 case 'l': /* RM -- Reset Mode */
2114 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
2116 case 'M': /* DL -- Delete <n> lines */
2117 DEFAULT(csiescseq
.arg
[0], 1);
2118 tdeleteline(csiescseq
.arg
[0]);
2120 case 'X': /* ECH -- Erase <n> char */
2121 DEFAULT(csiescseq
.arg
[0], 1);
2122 tclearregion(term
.c
.x
, term
.c
.y
,
2123 term
.c
.x
+ csiescseq
.arg
[0] - 1, term
.c
.y
);
2125 case 'P': /* DCH -- Delete <n> char */
2126 DEFAULT(csiescseq
.arg
[0], 1);
2127 tdeletechar(csiescseq
.arg
[0]);
2129 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
2130 DEFAULT(csiescseq
.arg
[0], 1);
2131 while(csiescseq
.arg
[0]--)
2134 case 'd': /* VPA -- Move to <row> */
2135 DEFAULT(csiescseq
.arg
[0], 1);
2136 tmoveato(term
.c
.x
, csiescseq
.arg
[0]-1);
2138 case 'h': /* SM -- Set terminal mode */
2139 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
2141 case 'm': /* SGR -- Terminal attribute (color) */
2142 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
2144 case 'n': /* DSR – Device Status Report (cursor position) */
2145 if (csiescseq
.arg
[0] == 6) {
2146 len
= snprintf(buf
, sizeof(buf
),"\033[%i;%iR",
2147 term
.c
.y
+1, term
.c
.x
+1);
2151 case 'r': /* DECSTBM -- Set Scrolling Region */
2152 if(csiescseq
.priv
) {
2155 DEFAULT(csiescseq
.arg
[0], 1);
2156 DEFAULT(csiescseq
.arg
[1], term
.row
);
2157 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
2161 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
2162 tcursor(CURSOR_SAVE
);
2164 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
2165 tcursor(CURSOR_LOAD
);
2176 for(i
= 0; i
< csiescseq
.len
; i
++) {
2177 c
= csiescseq
.buf
[i
] & 0xff;
2180 } else if(c
== '\n') {
2182 } else if(c
== '\r') {
2184 } else if(c
== 0x1b) {
2187 printf("(%02x)", c
);
2195 memset(&csiescseq
, 0, sizeof(csiescseq
));
2204 narg
= strescseq
.narg
;
2205 par
= atoi(strescseq
.args
[0]);
2207 switch(strescseq
.type
) {
2208 case ']': /* OSC -- Operating System Command */
2214 xsettitle(strescseq
.args
[1]);
2216 case 4: /* color set */
2219 p
= strescseq
.args
[2];
2221 case 104: /* color reset, here p = NULL */
2222 j
= (narg
> 1) ? atoi(strescseq
.args
[1]) : -1;
2223 if (!xsetcolorname(j
, p
)) {
2224 fprintf(stderr
, "erresc: invalid color %s\n", p
);
2227 * TODO if defaultbg color is changed, borders
2235 case 'k': /* old title set compatibility */
2236 xsettitle(strescseq
.args
[0]);
2238 case 'P': /* DSC -- Device Control String */
2239 case '_': /* APC -- Application Program Command */
2240 case '^': /* PM -- Privacy Message */
2244 fprintf(stderr
, "erresc: unknown str ");
2250 char *p
= strescseq
.buf
;
2253 strescseq
.buf
[strescseq
.len
] = '\0';
2254 while(p
&& strescseq
.narg
< STR_ARG_SIZ
)
2255 strescseq
.args
[strescseq
.narg
++] = strsep(&p
, ";");
2263 printf("ESC%c", strescseq
.type
);
2264 for(i
= 0; i
< strescseq
.len
; i
++) {
2265 c
= strescseq
.buf
[i
] & 0xff;
2268 } else if(isprint(c
)) {
2270 } else if(c
== '\n') {
2272 } else if(c
== '\r') {
2274 } else if(c
== 0x1b) {
2277 printf("(%02x)", c
);
2285 memset(&strescseq
, 0, sizeof(strescseq
));
2289 tprinter(char *s
, size_t len
) {
2290 if(iofd
!= -1 && xwrite(iofd
, s
, len
) < 0) {
2291 fprintf(stderr
, "Error writing in %s:%s\n",
2292 opt_io
, strerror(errno
));
2299 toggleprinter(const Arg
*arg
) {
2300 term
.mode
^= MODE_PRINT
;
2304 printscreen(const Arg
*arg
) {
2309 printsel(const Arg
*arg
) {
2319 tprinter(ptr
, strlen(ptr
));
2327 bp
= &term
.line
[n
][0];
2328 end
= &bp
[term
.col
-1];
2329 while(end
> bp
&& !strcmp(" ", end
->c
))
2331 if(bp
!= end
|| strcmp(bp
->c
, " ")) {
2332 for( ;bp
<= end
; ++bp
)
2333 tprinter(bp
->c
, strlen(bp
->c
));
2342 for(i
= 0; i
< term
.row
; ++i
)
2347 tputtab(bool forward
) {
2353 for(++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
2358 for(--x
; x
> 0 && !term
.tabs
[x
]; --x
)
2361 tmoveto(x
, term
.c
.y
);
2365 techo(char *buf
, int len
) {
2366 for(; len
> 0; buf
++, len
--) {
2369 if(c
== '\033') { /* escape */
2372 } else if(c
< '\x20') { /* control code */
2373 if(c
!= '\n' && c
!= '\r' && c
!= '\t') {
2387 tdeftran(char ascii
) {
2389 static char tbl
[][2] = {
2390 {'0', CS_GRAPHIC0
}, {'1', CS_GRAPHIC1
}, {'A', CS_UK
},
2391 {'B', CS_USA
}, {'<', CS_MULTI
}, {'K', CS_GER
},
2392 {'5', CS_FIN
}, {'C', CS_FIN
},
2396 for (bp
= &tbl
[0]; (c
= (*bp
)[0]) && c
!= ascii
; ++bp
)
2400 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
2402 term
.trantbl
[term
.icharset
] = (*bp
)[1];
2407 if (term
.trantbl
[term
.charset
] == CS_GRAPHIC0
)
2408 term
.c
.attr
.mode
|= ATTR_GFX
;
2410 term
.c
.attr
.mode
&= ~ATTR_GFX
;
2414 tputc(char *c
, int len
) {
2416 bool control
= ascii
< '\x20' || ascii
== 0177;
2423 utf8decode(c
, &u8char
);
2424 width
= wcwidth(u8char
);
2427 if(IS_SET(MODE_PRINT
))
2431 * STR sequences must be checked before anything else
2432 * because it can use some control codes as part of the sequence.
2434 if(term
.esc
& ESC_STR
) {
2437 term
.esc
= ESC_START
| ESC_STR_END
;
2439 case '\a': /* backwards compatibility to xterm */
2444 if(strescseq
.len
+ len
< sizeof(strescseq
.buf
) - 1) {
2445 memmove(&strescseq
.buf
[strescseq
.len
], c
, len
);
2446 strescseq
.len
+= len
;
2449 * Here is a bug in terminals. If the user never sends
2450 * some code to stop the str or esc command, then st
2451 * will stop responding. But this is better than
2452 * silently failing with unknown characters. At least
2453 * then users will report back.
2455 * In the case users ever get fixed, here is the code:
2467 * Actions of control codes must be performed as soon they arrive
2468 * because they can be embedded inside a control sequence, and
2469 * they must not cause conflicts with sequences.
2477 tmoveto(term
.c
.x
-1, term
.c
.y
);
2480 tmoveto(0, term
.c
.y
);
2485 /* go to first col if the mode is set */
2486 tnewline(IS_SET(MODE_CRLF
));
2488 case '\a': /* BEL */
2489 if(!(xw
.state
& WIN_FOCUSED
))
2492 XBell(xw
.dpy
, bellvolume
);
2494 case '\033': /* ESC */
2496 term
.esc
= ESC_START
;
2498 case '\016': /* SO */
2502 case '\017': /* SI */
2506 case '\032': /* SUB */
2507 case '\030': /* CAN */
2510 case '\005': /* ENQ (IGNORED) */
2511 case '\000': /* NUL (IGNORED) */
2512 case '\021': /* XON (IGNORED) */
2513 case '\023': /* XOFF (IGNORED) */
2514 case 0177: /* DEL (IGNORED) */
2517 } else if(term
.esc
& ESC_START
) {
2518 if(term
.esc
& ESC_CSI
) {
2519 csiescseq
.buf
[csiescseq
.len
++] = ascii
;
2520 if(BETWEEN(ascii
, 0x40, 0x7E)
2521 || csiescseq
.len
>= \
2522 sizeof(csiescseq
.buf
)-1) {
2527 } else if(term
.esc
& ESC_STR_END
) {
2531 } else if(term
.esc
& ESC_ALTCHARSET
) {
2535 } else if(term
.esc
& ESC_TEST
) {
2536 if(ascii
== '8') { /* DEC screen alignment test. */
2537 char E
[UTF_SIZ
] = "E";
2540 for(x
= 0; x
< term
.col
; ++x
) {
2541 for(y
= 0; y
< term
.row
; ++y
)
2542 tsetchar(E
, &term
.c
.attr
, x
, y
);
2549 term
.esc
|= ESC_CSI
;
2552 term
.esc
|= ESC_TEST
;
2554 case 'P': /* DCS -- Device Control String */
2555 case '_': /* APC -- Application Program Command */
2556 case '^': /* PM -- Privacy Message */
2557 case ']': /* OSC -- Operating System Command */
2558 case 'k': /* old title set compatibility */
2560 strescseq
.type
= ascii
;
2561 term
.esc
|= ESC_STR
;
2563 case '(': /* set primary charset G0 */
2564 case ')': /* set secondary charset G1 */
2565 case '*': /* set tertiary charset G2 */
2566 case '+': /* set quaternary charset G3 */
2567 term
.icharset
= ascii
- '(';
2568 term
.esc
|= ESC_ALTCHARSET
;
2570 case 'D': /* IND -- Linefeed */
2571 if(term
.c
.y
== term
.bot
) {
2572 tscrollup(term
.top
, 1);
2574 tmoveto(term
.c
.x
, term
.c
.y
+1);
2578 case 'E': /* NEL -- Next line */
2579 tnewline(1); /* always go to first col */
2582 case 'H': /* HTS -- Horizontal tab stop */
2583 term
.tabs
[term
.c
.x
] = 1;
2586 case 'M': /* RI -- Reverse index */
2587 if(term
.c
.y
== term
.top
) {
2588 tscrolldown(term
.top
, 1);
2590 tmoveto(term
.c
.x
, term
.c
.y
-1);
2594 case 'Z': /* DECID -- Identify Terminal */
2595 ttywrite(VT102ID
, sizeof(VT102ID
) - 1);
2598 case 'c': /* RIS -- Reset to inital state */
2604 case '=': /* DECPAM -- Application keypad */
2605 term
.mode
|= MODE_APPKEYPAD
;
2608 case '>': /* DECPNM -- Normal keypad */
2609 term
.mode
&= ~MODE_APPKEYPAD
;
2612 case '7': /* DECSC -- Save Cursor */
2613 tcursor(CURSOR_SAVE
);
2616 case '8': /* DECRC -- Restore Cursor */
2617 tcursor(CURSOR_LOAD
);
2620 case '\\': /* ST -- Stop */
2624 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
2625 (uchar
) ascii
, isprint(ascii
)? ascii
:'.');
2630 * All characters which form part of a sequence are not
2636 * Display control codes only if we are in graphic mode
2638 if(control
&& !(term
.c
.attr
.mode
& ATTR_GFX
))
2640 if(sel
.ob
.x
!= -1 && BETWEEN(term
.c
.y
, sel
.ob
.y
, sel
.oe
.y
))
2642 if(IS_SET(MODE_WRAP
) && (term
.c
.state
& CURSOR_WRAPNEXT
)) {
2643 term
.line
[term
.c
.y
][term
.c
.x
].mode
|= ATTR_WRAP
;
2647 if(IS_SET(MODE_INSERT
) && term
.c
.x
+1 < term
.col
) {
2648 memmove(&term
.line
[term
.c
.y
][term
.c
.x
+1],
2649 &term
.line
[term
.c
.y
][term
.c
.x
],
2650 (term
.col
- term
.c
.x
- 1) * sizeof(Glyph
));
2653 if(term
.c
.x
+width
> term
.col
)
2656 tsetchar(c
, &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
2659 term
.line
[term
.c
.y
][term
.c
.x
].mode
|= ATTR_WIDE
;
2660 if(term
.c
.x
+1 < term
.col
) {
2661 term
.line
[term
.c
.y
][term
.c
.x
+1].c
[0] = '\0';
2662 term
.line
[term
.c
.y
][term
.c
.x
+1].mode
= ATTR_WDUMMY
;
2665 if(term
.c
.x
+width
< term
.col
) {
2666 tmoveto(term
.c
.x
+width
, term
.c
.y
);
2668 term
.c
.state
|= CURSOR_WRAPNEXT
;
2673 tresize(int col
, int row
) {
2675 int minrow
= MIN(row
, term
.row
);
2676 int mincol
= MIN(col
, term
.col
);
2677 int slide
= term
.c
.y
- row
+ 1;
2681 if(col
< 1 || row
< 1)
2684 /* free unneeded rows */
2688 * slide screen to keep cursor where we expect it -
2689 * tscrollup would work here, but we can optimize to
2690 * memmove because we're freeing the earlier lines
2692 for(/* i = 0 */; i
< slide
; i
++) {
2696 memmove(term
.line
, term
.line
+ slide
, row
* sizeof(Line
));
2697 memmove(term
.alt
, term
.alt
+ slide
, row
* sizeof(Line
));
2699 for(i
+= row
; i
< term
.row
; i
++) {
2704 /* resize to new height */
2705 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
2706 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
2707 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
2708 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
2710 /* resize each row to new width, zero-pad if needed */
2711 for(i
= 0; i
< minrow
; i
++) {
2713 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
2714 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
2717 /* allocate any new rows */
2718 for(/* i == minrow */; i
< row
; i
++) {
2720 term
.line
[i
] = xmalloc(col
* sizeof(Glyph
));
2721 term
.alt
[i
] = xmalloc(col
* sizeof(Glyph
));
2723 if(col
> term
.col
) {
2724 bp
= term
.tabs
+ term
.col
;
2726 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
2727 while(--bp
> term
.tabs
&& !*bp
)
2729 for(bp
+= tabspaces
; bp
< term
.tabs
+ col
; bp
+= tabspaces
)
2732 /* update terminal size */
2735 /* reset scrolling region */
2736 tsetscroll(0, row
-1);
2737 /* make use of the LIMIT in tmoveto */
2738 tmoveto(term
.c
.x
, term
.c
.y
);
2739 /* Clearing both screens */
2742 if(mincol
< col
&& 0 < minrow
) {
2743 tclearregion(mincol
, 0, col
- 1, minrow
- 1);
2745 if(0 < col
&& minrow
< row
) {
2746 tclearregion(0, minrow
, col
- 1, row
- 1);
2749 } while(orig
!= term
.line
);
2755 xresize(int col
, int row
) {
2756 xw
.tw
= MAX(1, col
* xw
.cw
);
2757 xw
.th
= MAX(1, row
* xw
.ch
);
2759 XFreePixmap(xw
.dpy
, xw
.buf
);
2760 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
2761 DefaultDepth(xw
.dpy
, xw
.scr
));
2762 XftDrawChange(xw
.draw
, xw
.buf
);
2763 xclear(0, 0, xw
.w
, xw
.h
);
2766 static inline ushort
2767 sixd_to_16bit(int x
) {
2768 return x
== 0 ? 0 : 0x3737 + 0x2828 * x
;
2774 XRenderColor color
= { .alpha
= 0xffff };
2779 for (cp
= dc
.col
; cp
< dc
.col
+ LEN(dc
.col
); ++cp
)
2780 XftColorFree(xw
.dpy
, xw
.vis
, xw
.cmap
, cp
);
2783 /* load colors [0-15] colors and [256-LEN(colorname)[ (config.h) */
2784 for(i
= 0; i
< LEN(colorname
); i
++) {
2787 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, colorname
[i
], &dc
.col
[i
])) {
2788 die("Could not allocate color '%s'\n", colorname
[i
]);
2792 /* load colors [16-255] ; same colors as xterm */
2793 for(i
= 16, r
= 0; r
< 6; r
++) {
2794 for(g
= 0; g
< 6; g
++) {
2795 for(b
= 0; b
< 6; b
++) {
2796 color
.red
= sixd_to_16bit(r
);
2797 color
.green
= sixd_to_16bit(g
);
2798 color
.blue
= sixd_to_16bit(b
);
2799 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &dc
.col
[i
])) {
2800 die("Could not allocate color %d\n", i
);
2807 for(r
= 0; r
< 24; r
++, i
++) {
2808 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * r
;
2809 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
,
2811 die("Could not allocate color %d\n", i
);
2818 xsetcolorname(int x
, const char *name
) {
2819 XRenderColor color
= { .alpha
= 0xffff };
2821 if (x
< 0 || x
> LEN(colorname
))
2824 if(16 <= x
&& x
< 16 + 216) {
2825 int r
= (x
- 16) / 36, g
= ((x
- 16) % 36) / 6, b
= (x
- 16) % 6;
2826 color
.red
= sixd_to_16bit(r
);
2827 color
.green
= sixd_to_16bit(g
);
2828 color
.blue
= sixd_to_16bit(b
);
2829 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2830 return 0; /* something went wrong */
2833 } else if (16 + 216 <= x
&& x
< 256) {
2834 color
.red
= color
.green
= color
.blue
= 0x0808 + 0x0a0a * (x
- (16 + 216));
2835 if(!XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &color
, &colour
))
2836 return 0; /* something went wrong */
2840 name
= colorname
[x
];
2843 if(!XftColorAllocName(xw
.dpy
, xw
.vis
, xw
.cmap
, name
, &colour
))
2850 xtermclear(int col1
, int row1
, int col2
, int row2
) {
2851 XftDrawRect(xw
.draw
,
2852 &dc
.col
[IS_SET(MODE_REVERSE
) ? defaultfg
: defaultbg
],
2853 borderpx
+ col1
* xw
.cw
,
2854 borderpx
+ row1
* xw
.ch
,
2855 (col2
-col1
+1) * xw
.cw
,
2856 (row2
-row1
+1) * xw
.ch
);
2860 * Absolute coordinates.
2863 xclear(int x1
, int y1
, int x2
, int y2
) {
2864 XftDrawRect(xw
.draw
,
2865 &dc
.col
[IS_SET(MODE_REVERSE
)? defaultfg
: defaultbg
],
2866 x1
, y1
, x2
-x1
, y2
-y1
);
2871 XClassHint
class = {opt_class
? opt_class
: termname
, termname
};
2872 XWMHints wm
= {.flags
= InputHint
, .input
= 1};
2873 XSizeHints
*sizeh
= NULL
;
2875 sizeh
= XAllocSizeHints();
2876 if(xw
.isfixed
== False
) {
2877 sizeh
->flags
= PSize
| PResizeInc
| PBaseSize
;
2878 sizeh
->height
= xw
.h
;
2879 sizeh
->width
= xw
.w
;
2880 sizeh
->height_inc
= xw
.ch
;
2881 sizeh
->width_inc
= xw
.cw
;
2882 sizeh
->base_height
= 2 * borderpx
;
2883 sizeh
->base_width
= 2 * borderpx
;
2885 sizeh
->flags
= PMaxSize
| PMinSize
;
2886 sizeh
->min_width
= sizeh
->max_width
= xw
.fw
;
2887 sizeh
->min_height
= sizeh
->max_height
= xw
.fh
;
2890 XSetWMProperties(xw
.dpy
, xw
.win
, NULL
, NULL
, NULL
, 0, sizeh
, &wm
,
2896 xloadfont(Font
*f
, FcPattern
*pattern
) {
2900 match
= FcFontMatch(NULL
, pattern
, &result
);
2904 if(!(f
->match
= XftFontOpenPattern(xw
.dpy
, match
))) {
2905 FcPatternDestroy(match
);
2910 f
->pattern
= FcPatternDuplicate(pattern
);
2912 f
->ascent
= f
->match
->ascent
;
2913 f
->descent
= f
->match
->descent
;
2915 f
->rbearing
= f
->match
->max_advance_width
;
2917 f
->height
= f
->ascent
+ f
->descent
;
2918 f
->width
= f
->lbearing
+ f
->rbearing
;
2924 xloadfonts(char *fontstr
, double fontsize
) {
2926 FcResult r_sz
, r_psz
;
2929 if(fontstr
[0] == '-') {
2930 pattern
= XftXlfdParse(fontstr
, False
, False
);
2932 pattern
= FcNameParse((FcChar8
*)fontstr
);
2936 die("st: can't open font %s\n", fontstr
);
2939 FcPatternDel(pattern
, FC_PIXEL_SIZE
);
2940 FcPatternDel(pattern
, FC_SIZE
);
2941 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, (double)fontsize
);
2942 usedfontsize
= fontsize
;
2944 r_psz
= FcPatternGetDouble(pattern
, FC_PIXEL_SIZE
, 0, &fontval
);
2945 r_sz
= FcPatternGetDouble(pattern
, FC_SIZE
, 0, &fontval
);
2946 if(r_psz
== FcResultMatch
) {
2947 usedfontsize
= fontval
;
2948 } else if(r_sz
== FcResultMatch
) {
2952 * Default font size is 12, if none given. This is to
2953 * have a known usedfontsize value.
2955 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, 12);
2960 FcConfigSubstitute(0, pattern
, FcMatchPattern
);
2961 FcDefaultSubstitute(pattern
);
2963 if(xloadfont(&dc
.font
, pattern
))
2964 die("st: can't open font %s\n", fontstr
);
2966 if(usedfontsize
< 0) {
2967 FcPatternGetDouble(dc
.font
.match
->pattern
,
2968 FC_PIXEL_SIZE
, 0, &fontval
);
2969 usedfontsize
= fontval
;
2972 /* Setting character width and height. */
2973 xw
.cw
= CEIL(dc
.font
.width
* cwscale
);
2974 xw
.ch
= CEIL(dc
.font
.height
* chscale
);
2976 FcPatternDel(pattern
, FC_SLANT
);
2977 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ITALIC
);
2978 if(xloadfont(&dc
.ifont
, pattern
))
2979 die("st: can't open font %s\n", fontstr
);
2981 FcPatternDel(pattern
, FC_WEIGHT
);
2982 FcPatternAddInteger(pattern
, FC_WEIGHT
, FC_WEIGHT_BOLD
);
2983 if(xloadfont(&dc
.ibfont
, pattern
))
2984 die("st: can't open font %s\n", fontstr
);
2986 FcPatternDel(pattern
, FC_SLANT
);
2987 FcPatternAddInteger(pattern
, FC_SLANT
, FC_SLANT_ROMAN
);
2988 if(xloadfont(&dc
.bfont
, pattern
))
2989 die("st: can't open font %s\n", fontstr
);
2991 FcPatternDestroy(pattern
);
2995 xloadfontset(Font
*f
) {
2998 if(!(f
->set
= FcFontSort(0, f
->pattern
, FcTrue
, 0, &result
)))
3004 xunloadfont(Font
*f
) {
3005 XftFontClose(xw
.dpy
, f
->match
);
3006 FcPatternDestroy(f
->pattern
);
3008 FcFontSetDestroy(f
->set
);
3012 xunloadfonts(void) {
3015 /* Free the loaded fonts in the font cache. */
3016 for(i
= 0; i
< frclen
; i
++) {
3017 XftFontClose(xw
.dpy
, frc
[i
].font
);
3021 xunloadfont(&dc
.font
);
3022 xunloadfont(&dc
.bfont
);
3023 xunloadfont(&dc
.ifont
);
3024 xunloadfont(&dc
.ibfont
);
3028 xzoom(const Arg
*arg
) {
3030 xloadfonts(usedfont
, usedfontsize
+ arg
->i
);
3041 pid_t thispid
= getpid();
3043 if(!(xw
.dpy
= XOpenDisplay(NULL
)))
3044 die("Can't open display\n");
3045 xw
.scr
= XDefaultScreen(xw
.dpy
);
3046 xw
.vis
= XDefaultVisual(xw
.dpy
, xw
.scr
);
3050 die("Could not init fontconfig.\n");
3052 usedfont
= (opt_font
== NULL
)? font
: opt_font
;
3053 xloadfonts(usedfont
, 0);
3056 xw
.cmap
= XDefaultColormap(xw
.dpy
, xw
.scr
);
3059 /* adjust fixed window geometry */
3061 sw
= DisplayWidth(xw
.dpy
, xw
.scr
);
3062 sh
= DisplayHeight(xw
.dpy
, xw
.scr
);
3064 xw
.fx
= sw
+ xw
.fx
- xw
.fw
- 1;
3066 xw
.fy
= sh
+ xw
.fy
- xw
.fh
- 1;
3071 /* window - default size */
3072 xw
.h
= 2 * borderpx
+ term
.row
* xw
.ch
;
3073 xw
.w
= 2 * borderpx
+ term
.col
* xw
.cw
;
3079 xw
.attrs
.background_pixel
= dc
.col
[defaultbg
].pixel
;
3080 xw
.attrs
.border_pixel
= dc
.col
[defaultbg
].pixel
;
3081 xw
.attrs
.bit_gravity
= NorthWestGravity
;
3082 xw
.attrs
.event_mask
= FocusChangeMask
| KeyPressMask
3083 | ExposureMask
| VisibilityChangeMask
| StructureNotifyMask
3084 | ButtonMotionMask
| ButtonPressMask
| ButtonReleaseMask
;
3085 xw
.attrs
.colormap
= xw
.cmap
;
3087 parent
= opt_embed
? strtol(opt_embed
, NULL
, 0) : \
3088 XRootWindow(xw
.dpy
, xw
.scr
);
3089 xw
.win
= XCreateWindow(xw
.dpy
, parent
, xw
.fx
, xw
.fy
,
3090 xw
.w
, xw
.h
, 0, XDefaultDepth(xw
.dpy
, xw
.scr
), InputOutput
,
3091 xw
.vis
, CWBackPixel
| CWBorderPixel
| CWBitGravity
3092 | CWEventMask
| CWColormap
, &xw
.attrs
);
3094 memset(&gcvalues
, 0, sizeof(gcvalues
));
3095 gcvalues
.graphics_exposures
= False
;
3096 dc
.gc
= XCreateGC(xw
.dpy
, parent
, GCGraphicsExposures
,
3098 xw
.buf
= XCreatePixmap(xw
.dpy
, xw
.win
, xw
.w
, xw
.h
,
3099 DefaultDepth(xw
.dpy
, xw
.scr
));
3100 XSetForeground(xw
.dpy
, dc
.gc
, dc
.col
[defaultbg
].pixel
);
3101 XFillRectangle(xw
.dpy
, xw
.buf
, dc
.gc
, 0, 0, xw
.w
, xw
.h
);
3103 /* Xft rendering context */
3104 xw
.draw
= XftDrawCreate(xw
.dpy
, xw
.buf
, xw
.vis
, xw
.cmap
);
3107 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
3108 XSetLocaleModifiers("@im=local");
3109 if((xw
.xim
= XOpenIM(xw
.dpy
, NULL
, NULL
, NULL
)) == NULL
) {
3110 XSetLocaleModifiers("@im=");
3111 if((xw
.xim
= XOpenIM(xw
.dpy
,
3112 NULL
, NULL
, NULL
)) == NULL
) {
3113 die("XOpenIM failed. Could not open input"
3118 xw
.xic
= XCreateIC(xw
.xim
, XNInputStyle
, XIMPreeditNothing
3119 | XIMStatusNothing
, XNClientWindow
, xw
.win
,
3120 XNFocusWindow
, xw
.win
, NULL
);
3122 die("XCreateIC failed. Could not obtain input method.\n");
3124 /* white cursor, black outline */
3125 cursor
= XCreateFontCursor(xw
.dpy
, XC_xterm
);
3126 XDefineCursor(xw
.dpy
, xw
.win
, cursor
);
3127 XRecolorCursor(xw
.dpy
, cursor
,
3128 &(XColor
){.red
= 0xffff, .green
= 0xffff, .blue
= 0xffff},
3129 &(XColor
){.red
= 0x0000, .green
= 0x0000, .blue
= 0x0000});
3131 xw
.xembed
= XInternAtom(xw
.dpy
, "_XEMBED", False
);
3132 xw
.wmdeletewin
= XInternAtom(xw
.dpy
, "WM_DELETE_WINDOW", False
);
3133 xw
.netwmname
= XInternAtom(xw
.dpy
, "_NET_WM_NAME", False
);
3134 XSetWMProtocols(xw
.dpy
, xw
.win
, &xw
.wmdeletewin
, 1);
3136 xw
.netwmpid
= XInternAtom(xw
.dpy
, "_NET_WM_PID", False
);
3137 XChangeProperty(xw
.dpy
, xw
.win
, xw
.netwmpid
, XA_CARDINAL
, 32,
3138 PropModeReplace
, (unsigned char *)&thispid
, 1);
3141 XMapWindow(xw
.dpy
, xw
.win
);
3147 xdraws(char *s
, Glyph base
, int x
, int y
, int charlen
, int bytelen
) {
3148 int winx
= borderpx
+ x
* xw
.cw
, winy
= borderpx
+ y
* xw
.ch
,
3149 width
= charlen
* xw
.cw
, xp
, i
;
3151 int u8fl
, u8fblen
, u8cblen
, doesexist
;
3154 Font
*font
= &dc
.font
;
3156 FcPattern
*fcpattern
, *fontpattern
;
3157 FcFontSet
*fcsets
[] = { NULL
};
3158 FcCharSet
*fccharset
;
3159 Colour
*fg
, *bg
, *temp
, revfg
, revbg
, truefg
, truebg
;
3160 XRenderColor colfg
, colbg
;
3164 frcflags
= FRC_NORMAL
;
3166 if(base
.mode
& ATTR_ITALIC
) {
3167 if(base
.fg
== defaultfg
)
3168 base
.fg
= defaultitalic
;
3170 frcflags
= FRC_ITALIC
;
3171 } else if((base
.mode
& ATTR_ITALIC
) && (base
.mode
& ATTR_BOLD
)) {
3172 if(base
.fg
== defaultfg
)
3173 base
.fg
= defaultitalic
;
3175 frcflags
= FRC_ITALICBOLD
;
3176 } else if(base
.mode
& ATTR_UNDERLINE
) {
3177 if(base
.fg
== defaultfg
)
3178 base
.fg
= defaultunderline
;
3181 if(IS_TRUECOL(base
.fg
)) {
3182 colfg
.alpha
= 0xffff;
3183 colfg
.red
= TRUERED(base
.fg
);
3184 colfg
.green
= TRUEGREEN(base
.fg
);
3185 colfg
.blue
= TRUEBLUE(base
.fg
);
3186 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
, &truefg
);
3189 fg
= &dc
.col
[base
.fg
];
3192 if(IS_TRUECOL(base
.bg
)) {
3193 colbg
.alpha
= 0xffff;
3194 colbg
.green
= TRUEGREEN(base
.bg
);
3195 colbg
.red
= TRUERED(base
.bg
);
3196 colbg
.blue
= TRUEBLUE(base
.bg
);
3197 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
, &truebg
);
3200 bg
= &dc
.col
[base
.bg
];
3203 if(base
.mode
& ATTR_BOLD
) {
3204 if(BETWEEN(base
.fg
, 0, 7)) {
3205 /* basic system colors */
3206 fg
= &dc
.col
[base
.fg
+ 8];
3207 } else if(BETWEEN(base
.fg
, 16, 195)) {
3209 fg
= &dc
.col
[base
.fg
+ 36];
3210 } else if(BETWEEN(base
.fg
, 232, 251)) {
3212 fg
= &dc
.col
[base
.fg
+ 4];
3215 * Those ranges will not be brightened:
3216 * 8 - 15 – bright system colors
3217 * 196 - 231 – highest 256 color cube
3218 * 252 - 255 – brightest colors in greyscale
3221 frcflags
= FRC_BOLD
;
3224 if(IS_SET(MODE_REVERSE
)) {
3225 if(fg
== &dc
.col
[defaultfg
]) {
3226 fg
= &dc
.col
[defaultbg
];
3228 colfg
.red
= ~fg
->color
.red
;
3229 colfg
.green
= ~fg
->color
.green
;
3230 colfg
.blue
= ~fg
->color
.blue
;
3231 colfg
.alpha
= fg
->color
.alpha
;
3232 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colfg
,
3237 if(bg
== &dc
.col
[defaultbg
]) {
3238 bg
= &dc
.col
[defaultfg
];
3240 colbg
.red
= ~bg
->color
.red
;
3241 colbg
.green
= ~bg
->color
.green
;
3242 colbg
.blue
= ~bg
->color
.blue
;
3243 colbg
.alpha
= bg
->color
.alpha
;
3244 XftColorAllocValue(xw
.dpy
, xw
.vis
, xw
.cmap
, &colbg
,
3250 if(base
.mode
& ATTR_REVERSE
) {
3256 if(base
.mode
& ATTR_BLINK
&& term
.mode
& MODE_BLINK
)
3259 /* Intelligent cleaning up of the borders. */
3261 xclear(0, (y
== 0)? 0 : winy
, borderpx
,
3262 winy
+ xw
.ch
+ ((y
>= term
.row
-1)? xw
.h
: 0));
3264 if(x
+ charlen
>= term
.col
) {
3265 xclear(winx
+ width
, (y
== 0)? 0 : winy
, xw
.w
,
3266 ((y
>= term
.row
-1)? xw
.h
: (winy
+ xw
.ch
)));
3269 xclear(winx
, 0, winx
+ width
, borderpx
);
3271 xclear(winx
, winy
+ xw
.ch
, winx
+ width
, xw
.h
);
3273 /* Clean up the region we want to draw to. */
3274 XftDrawRect(xw
.draw
, bg
, winx
, winy
, width
, xw
.ch
);
3276 /* Set the clip region because Xft is sometimes dirty. */
3281 XftDrawSetClipRectangles(xw
.draw
, winx
, winy
, &r
, 1);
3283 for(xp
= winx
; bytelen
> 0;) {
3285 * Search for the range in the to be printed string of glyphs
3286 * that are in the main font. Then print that range. If
3287 * some glyph is found that is not in the font, do the
3293 oneatatime
= font
->width
!= xw
.cw
;
3296 u8cblen
= utf8decode(s
, &u8char
);
3300 doesexist
= XftCharExists(xw
.dpy
, font
->match
, u8char
);
3301 if(oneatatime
|| !doesexist
|| bytelen
<= 0) {
3302 if(oneatatime
|| bytelen
<= 0) {
3310 XftDrawStringUtf8(xw
.draw
, fg
,
3312 winy
+ font
->ascent
,
3330 /* Search the font cache. */
3331 for(i
= 0; i
< frclen
; i
++) {
3332 if(XftCharExists(xw
.dpy
, frc
[i
].font
, u8char
)
3333 && frc
[i
].flags
== frcflags
) {
3338 /* Nothing was found. */
3342 fcsets
[0] = font
->set
;
3345 * Nothing was found in the cache. Now use
3346 * some dozen of Fontconfig calls to get the
3347 * font for one single character.
3349 * Xft and fontconfig are design failures.
3351 fcpattern
= FcPatternDuplicate(font
->pattern
);
3352 fccharset
= FcCharSetCreate();
3354 FcCharSetAddChar(fccharset
, u8char
);
3355 FcPatternAddCharSet(fcpattern
, FC_CHARSET
,
3357 FcPatternAddBool(fcpattern
, FC_SCALABLE
,
3360 FcConfigSubstitute(0, fcpattern
,
3362 FcDefaultSubstitute(fcpattern
);
3364 fontpattern
= FcFontSetMatch(0, fcsets
,
3365 FcTrue
, fcpattern
, &fcres
);
3368 * Overwrite or create the new cache entry.
3370 if(frclen
>= LEN(frc
)) {
3371 frclen
= LEN(frc
) - 1;
3372 XftFontClose(xw
.dpy
, frc
[frclen
].font
);
3375 frc
[frclen
].font
= XftFontOpenPattern(xw
.dpy
,
3377 frc
[frclen
].flags
= frcflags
;
3382 FcPatternDestroy(fcpattern
);
3383 FcCharSetDestroy(fccharset
);
3386 XftDrawStringUtf8(xw
.draw
, fg
, frc
[i
].font
,
3387 xp
, winy
+ frc
[i
].font
->ascent
,
3388 (FcChar8
*)u8c
, u8cblen
);
3390 xp
+= xw
.cw
* wcwidth(u8char
);
3394 * This is how the loop above actually should be. Why does the
3395 * application have to care about font details?
3397 * I have to repeat: Xft and Fontconfig are design failures.
3400 XftDrawStringUtf8(xw.draw, fg, font->set, winx,
3401 winy + font->ascent, (FcChar8 *)s, bytelen);
3404 if(base
.mode
& ATTR_UNDERLINE
) {
3405 XftDrawRect(xw
.draw
, fg
, winx
, winy
+ font
->ascent
+ 1,
3409 /* Reset clip to none. */
3410 XftDrawSetClip(xw
.draw
, 0);
3415 static int oldx
= 0, oldy
= 0;
3416 int sl
, width
, curx
;
3417 Glyph g
= {{' '}, ATTR_NULL
, defaultbg
, defaultcs
};
3419 LIMIT(oldx
, 0, term
.col
-1);
3420 LIMIT(oldy
, 0, term
.row
-1);
3424 /* adjust position if in dummy */
3425 if(term
.line
[oldy
][oldx
].mode
& ATTR_WDUMMY
)
3427 if(term
.line
[term
.c
.y
][curx
].mode
& ATTR_WDUMMY
)
3430 memcpy(g
.c
, term
.line
[term
.c
.y
][term
.c
.x
].c
, UTF_SIZ
);
3432 /* remove the old cursor */
3433 sl
= utf8size(term
.line
[oldy
][oldx
].c
);
3434 width
= (term
.line
[oldy
][oldx
].mode
& ATTR_WIDE
)? 2 : 1;
3435 xdraws(term
.line
[oldy
][oldx
].c
, term
.line
[oldy
][oldx
], oldx
,
3438 /* draw the new one */
3439 if(!(IS_SET(MODE_HIDE
))) {
3440 if(xw
.state
& WIN_FOCUSED
) {
3441 if(IS_SET(MODE_REVERSE
)) {
3442 g
.mode
|= ATTR_REVERSE
;
3448 width
= (term
.line
[term
.c
.y
][curx
].mode
& ATTR_WIDE
)\
3450 xdraws(g
.c
, g
, term
.c
.x
, term
.c
.y
, width
, sl
);
3452 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3453 borderpx
+ curx
* xw
.cw
,
3454 borderpx
+ term
.c
.y
* xw
.ch
,
3456 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3457 borderpx
+ curx
* xw
.cw
,
3458 borderpx
+ term
.c
.y
* xw
.ch
,
3460 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3461 borderpx
+ (curx
+ 1) * xw
.cw
- 1,
3462 borderpx
+ term
.c
.y
* xw
.ch
,
3464 XftDrawRect(xw
.draw
, &dc
.col
[defaultcs
],
3465 borderpx
+ curx
* xw
.cw
,
3466 borderpx
+ (term
.c
.y
+ 1) * xw
.ch
- 1,
3469 oldx
= curx
, oldy
= term
.c
.y
;
3475 xsettitle(char *p
) {
3478 Xutf8TextListToTextProperty(xw
.dpy
, &p
, 1, XUTF8StringStyle
,
3480 XSetWMName(xw
.dpy
, xw
.win
, &prop
);
3481 XSetTextProperty(xw
.dpy
, xw
.win
, &prop
, xw
.netwmname
);
3487 xsettitle(opt_title
? opt_title
: "st");
3491 redraw(int timeout
) {
3492 struct timespec tv
= {0, timeout
* 1000};
3498 nanosleep(&tv
, NULL
);
3499 XSync(xw
.dpy
, False
); /* necessary for a good tput flash */
3505 drawregion(0, 0, term
.col
, term
.row
);
3506 XCopyArea(xw
.dpy
, xw
.buf
, xw
.win
, dc
.gc
, 0, 0, xw
.w
,
3508 XSetForeground(xw
.dpy
, dc
.gc
,
3509 dc
.col
[IS_SET(MODE_REVERSE
)?
3510 defaultfg
: defaultbg
].pixel
);
3514 drawregion(int x1
, int y1
, int x2
, int y2
) {
3515 int ic
, ib
, x
, y
, ox
, sl
;
3517 char buf
[DRAW_BUF_SIZ
];
3518 bool ena_sel
= sel
.ob
.x
!= -1;
3521 if(sel
.alt
^ IS_SET(MODE_ALTSCREEN
))
3524 if(!(xw
.state
& WIN_VISIBLE
))
3527 for(y
= y1
; y
< y2
; y
++) {
3531 xtermclear(0, y
, term
.col
, y
);
3533 base
= term
.line
[y
][0];
3535 for(x
= x1
; x
< x2
; x
++) {
3536 new = term
.line
[y
][x
];
3537 if(new.mode
== ATTR_WDUMMY
)
3539 if(ena_sel
&& selected(x
, y
))
3540 new.mode
^= ATTR_REVERSE
;
3541 if(ib
> 0 && (ATTRCMP(base
, new)
3542 || ib
>= DRAW_BUF_SIZ
-UTF_SIZ
)) {
3543 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3551 sl
= utf8decode(new.c
, &u8char
);
3552 memcpy(buf
+ib
, new.c
, sl
);
3554 ic
+= (new.mode
& ATTR_WIDE
)? 2 : 1;
3557 xdraws(buf
, base
, ox
, y
, ic
, ib
);
3563 expose(XEvent
*ev
) {
3564 XExposeEvent
*e
= &ev
->xexpose
;
3566 if(xw
.state
& WIN_REDRAW
) {
3568 xw
.state
&= ~WIN_REDRAW
;
3574 visibility(XEvent
*ev
) {
3575 XVisibilityEvent
*e
= &ev
->xvisibility
;
3577 if(e
->state
== VisibilityFullyObscured
) {
3578 xw
.state
&= ~WIN_VISIBLE
;
3579 } else if(!(xw
.state
& WIN_VISIBLE
)) {
3580 /* need a full redraw for next Expose, not just a buf copy */
3581 xw
.state
|= WIN_VISIBLE
| WIN_REDRAW
;
3587 xw
.state
&= ~WIN_VISIBLE
;
3591 xsetpointermotion(int set
) {
3592 MODBIT(xw
.attrs
.event_mask
, set
, PointerMotionMask
);
3593 XChangeWindowAttributes(xw
.dpy
, xw
.win
, CWEventMask
, &xw
.attrs
);
3597 xseturgency(int add
) {
3598 XWMHints
*h
= XGetWMHints(xw
.dpy
, xw
.win
);
3600 h
->flags
= add
? (h
->flags
| XUrgencyHint
) : (h
->flags
& ~XUrgencyHint
);
3601 XSetWMHints(xw
.dpy
, xw
.win
, h
);
3607 XFocusChangeEvent
*e
= &ev
->xfocus
;
3609 if(e
->mode
== NotifyGrab
)
3612 if(ev
->type
== FocusIn
) {
3613 XSetICFocus(xw
.xic
);
3614 xw
.state
|= WIN_FOCUSED
;
3616 if(IS_SET(MODE_FOCUS
))
3617 ttywrite("\033[I", 3);
3619 XUnsetICFocus(xw
.xic
);
3620 xw
.state
&= ~WIN_FOCUSED
;
3621 if(IS_SET(MODE_FOCUS
))
3622 ttywrite("\033[O", 3);
3627 match(uint mask
, uint state
) {
3628 return mask
== XK_ANY_MOD
|| mask
== (state
& ~ignoremod
);
3632 numlock(const Arg
*dummy
) {
3637 kmap(KeySym k
, uint state
) {
3641 /* Check for mapped keys out of X11 function keys. */
3642 for(i
= 0; i
< LEN(mappedkeys
); i
++) {
3643 if(mappedkeys
[i
] == k
)
3646 if(i
== LEN(mappedkeys
)) {
3647 if((k
& 0xFFFF) < 0xFD00)
3651 for(kp
= key
; kp
< key
+ LEN(key
); kp
++) {
3655 if(!match(kp
->mask
, state
))
3658 if(IS_SET(MODE_APPKEYPAD
) ? kp
->appkey
< 0 : kp
->appkey
> 0)
3660 if(term
.numlock
&& kp
->appkey
== 2)
3663 if(IS_SET(MODE_APPCURSOR
) ? kp
->appcursor
< 0 : kp
->appcursor
> 0)
3666 if(IS_SET(MODE_CRLF
) ? kp
->crlf
< 0 : kp
->crlf
> 0)
3676 kpress(XEvent
*ev
) {
3677 XKeyEvent
*e
= &ev
->xkey
;
3679 char buf
[32], *customkey
;
3685 if(IS_SET(MODE_KBDLOCK
))
3688 len
= XmbLookupString(xw
.xic
, e
, buf
, sizeof buf
, &ksym
, &status
);
3690 for(bp
= shortcuts
; bp
< shortcuts
+ LEN(shortcuts
); bp
++) {
3691 if(ksym
== bp
->keysym
&& match(bp
->mod
, e
->state
)) {
3692 bp
->func(&(bp
->arg
));
3697 /* 2. custom keys from config.h */
3698 if((customkey
= kmap(ksym
, e
->state
))) {
3699 ttysend(customkey
, strlen(customkey
));
3703 /* 3. composed string from input method */
3706 if(len
== 1 && e
->state
& Mod1Mask
) {
3707 if(IS_SET(MODE_8BIT
)) {
3710 len
= utf8encode(&c
, buf
);
3723 cmessage(XEvent
*e
) {
3726 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
3728 if(e
->xclient
.message_type
== xw
.xembed
&& e
->xclient
.format
== 32) {
3729 if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_IN
) {
3730 xw
.state
|= WIN_FOCUSED
;
3732 } else if(e
->xclient
.data
.l
[1] == XEMBED_FOCUS_OUT
) {
3733 xw
.state
&= ~WIN_FOCUSED
;
3735 } else if(e
->xclient
.data
.l
[0] == xw
.wmdeletewin
) {
3736 /* Send SIGHUP to shell */
3743 cresize(int width
, int height
) {
3751 col
= (xw
.w
- 2 * borderpx
) / xw
.cw
;
3752 row
= (xw
.h
- 2 * borderpx
) / xw
.ch
;
3761 if(e
->xconfigure
.width
== xw
.w
&& e
->xconfigure
.height
== xw
.h
)
3764 cresize(e
->xconfigure
.width
, e
->xconfigure
.height
);
3770 int w
= xw
.w
, h
= xw
.h
;
3772 int xfd
= XConnectionNumber(xw
.dpy
), xev
, blinkset
= 0, dodraw
= 0;
3773 struct timeval drawtimeout
, *tv
= NULL
, now
, last
, lastblink
;
3775 /* Waiting for window mapping */
3777 XNextEvent(xw
.dpy
, &ev
);
3778 if(ev
.type
== ConfigureNotify
) {
3779 w
= ev
.xconfigure
.width
;
3780 h
= ev
.xconfigure
.height
;
3781 } else if(ev
.type
== MapNotify
) {
3790 cresize(xw
.fw
, xw
.fh
);
3792 gettimeofday(&lastblink
, NULL
);
3793 gettimeofday(&last
, NULL
);
3795 for(xev
= actionfps
;;) {
3799 FD_SET(cmdfd
, &rfd
);
3802 if(select(MAX(xfd
, cmdfd
)+1, &rfd
, NULL
, NULL
, tv
) < 0) {
3805 die("select failed: %s\n", SERRNO
);
3807 if(FD_ISSET(cmdfd
, &rfd
)) {
3810 blinkset
= tattrset(ATTR_BLINK
);
3812 MODBIT(term
.mode
, 0, MODE_BLINK
);
3816 if(FD_ISSET(xfd
, &rfd
))
3819 gettimeofday(&now
, NULL
);
3820 drawtimeout
.tv_sec
= 0;
3821 drawtimeout
.tv_usec
= (1000/xfps
) * 1000;
3825 if(blinktimeout
&& TIMEDIFF(now
, lastblink
) > blinktimeout
) {
3826 tsetdirtattr(ATTR_BLINK
);
3827 term
.mode
^= MODE_BLINK
;
3828 gettimeofday(&lastblink
, NULL
);
3831 deltatime
= TIMEDIFF(now
, last
);
3832 if(deltatime
> (xev
? (1000/xfps
) : (1000/actionfps
))
3839 while(XPending(xw
.dpy
)) {
3840 XNextEvent(xw
.dpy
, &ev
);
3841 if(XFilterEvent(&ev
, None
))
3843 if(handler
[ev
.type
])
3844 (handler
[ev
.type
])(&ev
);
3850 if(xev
&& !FD_ISSET(xfd
, &rfd
))
3852 if(!FD_ISSET(cmdfd
, &rfd
) && !FD_ISSET(xfd
, &rfd
)) {
3854 if(TIMEDIFF(now
, lastblink
) \
3856 drawtimeout
.tv_usec
= 1;
3858 drawtimeout
.tv_usec
= (1000 * \
3873 die("%s " VERSION
" (c) 2010-2013 st engineers\n" \
3874 "usage: st [-a] [-v] [-c class] [-f font] [-g geometry] [-o file]" \
3875 " [-t title] [-w windowid] [-e command ...]\n", argv0
);
3879 main(int argc
, char *argv
[]) {
3884 xw
.fw
= xw
.fh
= xw
.fx
= xw
.fy
= 0;
3889 allowaltscreen
= false;
3892 opt_class
= EARGF(usage());
3895 /* eat all remaining arguments */
3898 if(argv
[1] != NULL
&& opt_title
== NULL
) {
3899 titles
= xstrdup(argv
[1]);
3900 opt_title
= basename(titles
);
3905 opt_font
= EARGF(usage());
3908 bitm
= XParseGeometry(EARGF(usage()), &xr
, &yr
, &wr
, &hr
);
3913 if(bitm
& WidthValue
)
3915 if(bitm
& HeightValue
)
3917 if(bitm
& XNegative
&& xw
.fx
== 0)
3919 if(bitm
& YNegative
&& xw
.fy
== 0)
3922 if(xw
.fh
!= 0 && xw
.fw
!= 0)
3926 opt_io
= EARGF(usage());
3929 opt_title
= EARGF(usage());
3932 opt_embed
= EARGF(usage());
3940 setlocale(LC_CTYPE
, "");
3941 XSetLocaleModifiers("");