From e2ee5ee6114eb74bb08cb9abe5a3020203e92688 Mon Sep 17 00:00:00 2001 From: Michael Forney Date: Fri, 20 Jan 2017 00:06:39 -0800 Subject: Split X-specific code into x.c --- win.h | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 win.h (limited to 'win.h') diff --git a/win.h b/win.h new file mode 100644 index 0000000..d684797 --- /dev/null +++ b/win.h @@ -0,0 +1,29 @@ +/* See LICENSE for license details. */ + +/* X modifiers */ +#define XK_ANY_MOD UINT_MAX +#define XK_NO_MOD 0 +#define XK_SWITCH_MOD (1<<13) + +typedef XftGlyphFontSpec GlyphFontSpec; + +void draw(void); +void drawregion(int, int, int, int); +void run(void); + +void xbell(int); +void xclipcopy(void); +void xclippaste(void); +void xhints(void); +void xinit(void); +void xloadcols(void); +int xsetcolorname(int, const char *); +void xloadfonts(char *, double); +void xsetenv(void); +void xsettitle(char *); +void xsetpointermotion(int); +void xseturgency(int); +void xunloadfonts(void); +void xresize(int, int); +void xselpaste(void); +unsigned long xwinid(void); -- cgit v1.2.3 From e7ed326d2e914a57017c9f34459824614075519b Mon Sep 17 00:00:00 2001 From: "osandov@osandov.com" Date: Sat, 18 Mar 2017 11:55:04 +0100 Subject: Support xterm Ms feature to set clipboard This is used by, e.g., tmux. --- st.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ st.info | 1 + win.h | 1 + x.c | 1 - 4 files changed, 59 insertions(+), 1 deletion(-) (limited to 'win.h') diff --git a/st.c b/st.c index 1e4196e..d7bd32a 100644 --- a/st.c +++ b/st.c @@ -198,6 +198,8 @@ static char utf8encodebyte(Rune, size_t); static char *utf8strchr(char *s, Rune u); static size_t utf8validate(Rune *, size_t); +static char *base64dec(const char *); + static ssize_t xwrite(int, const char *, size_t); static void *xrealloc(void *, size_t); @@ -369,6 +371,48 @@ utf8validate(Rune *u, size_t i) return i; } +static const char base64_digits[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, + 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, -1, 0, 0, 0, 0, 1, + 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, + 22, 23, 24, 25, 0, 0, 0, 0, 0, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, + 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 +}; + +char * +base64dec(const char *src) +{ + size_t in_len = strlen(src); + char *result, *dst; + + if (in_len % 4) + return NULL; + result = dst = xmalloc(in_len / 4 * 3 + 1); + while (*src) { + int a = base64_digits[(unsigned char) *src++]; + int b = base64_digits[(unsigned char) *src++]; + int c = base64_digits[(unsigned char) *src++]; + int d = base64_digits[(unsigned char) *src++]; + + *dst++ = (a << 2) | ((b & 0x30) >> 4); + if (c == -1) + break; + *dst++ = ((b & 0x0f) << 4) | ((c & 0x3c) >> 2); + if (d == -1) + break; + *dst++ = ((c & 0x03) << 6) | d; + } + *dst = '\0'; + return result; +} + void selinit(void) { @@ -1820,6 +1864,19 @@ strhandle(void) if (narg > 1) xsettitle(strescseq.args[1]); return; + case 52: + if (narg > 2) { + char *dec; + + dec = base64dec(strescseq.args[2]); + if (dec) { + xsetsel(dec, CurrentTime); + clipcopy(NULL); + } else { + fprintf(stderr, "erresc: invalid base64\n"); + } + } + return; case 4: /* color set */ if (narg < 3) break; diff --git a/st.info b/st.info index 13cc8eb..0b928af 100644 --- a/st.info +++ b/st.info @@ -189,6 +189,7 @@ st| simpleterm, Se, Ss, Tc, + Ms=\E]52;%p1%s;%p2%s\007, st-256color| simpleterm with 256 colors, use=st, diff --git a/win.h b/win.h index d684797..428111c 100644 --- a/win.h +++ b/win.h @@ -27,3 +27,4 @@ void xunloadfonts(void); void xresize(int, int); void xselpaste(void); unsigned long xwinid(void); +void xsetsel(char *, Time); diff --git a/x.c b/x.c index 6474a01..743b084 100644 --- a/x.c +++ b/x.c @@ -88,7 +88,6 @@ static void xclear(int, int, int, int); static void xdrawcursor(void); static int xgeommasktogravity(int); static int xloadfont(Font *, FcPattern *); -static void xsetsel(char *, Time); static void xunloadfont(Font *); static void expose(XEvent *); -- cgit v1.2.3 From 3e44ee5569a81ba6f06e1ecd19bf0ceb1e97f18d Mon Sep 17 00:00:00 2001 From: "Devin J. Pohly" Date: Tue, 10 Oct 2017 10:30:23 -0500 Subject: Call xsetenv() in main process instead of child This makes xsetenv internal to x.c, and allows iso14755's external command to use $WINDOWID instead of having to snprintf it again. (The same benefit will apply to the externalpipe patch.) The xwinid function is no longer needed. Signed-off-by: Devin J. Pohly --- st.c | 8 ++------ win.h | 2 -- x.c | 8 ++------ 3 files changed, 4 insertions(+), 14 deletions(-) (limited to 'win.h') diff --git a/st.c b/st.c index 7c7ddff..668b312 100644 --- a/st.c +++ b/st.c @@ -60,7 +60,7 @@ char *argv0; #define ISDELIM(u) (utf8strchr(worddelimiters, u) != NULL) /* constants */ -#define ISO14755CMD "dmenu -w %lu -p codepoint: pw_dir, 1); setenv("TERM", termname, 1); - xsetenv(); signal(SIGCHLD, SIG_DFL); signal(SIGHUP, SIG_DFL); @@ -1993,14 +1992,11 @@ tprinter(char *s, size_t len) void iso14755(const Arg *arg) { - unsigned long id = xwinid(); - char cmd[sizeof(ISO14755CMD) + NUMMAXLEN(id)]; FILE *p; char *us, *e, codepoint[9], uc[UTF_SIZ]; unsigned long utf32; - snprintf(cmd, sizeof(cmd), ISO14755CMD, id); - if (!(p = popen(cmd, "r"))) + if (!(p = popen(ISO14755CMD, "r"))) return; us = fgets(codepoint, sizeof(codepoint), p); diff --git a/win.h b/win.h index 428111c..423c114 100644 --- a/win.h +++ b/win.h @@ -19,12 +19,10 @@ void xinit(void); void xloadcols(void); int xsetcolorname(int, const char *); void xloadfonts(char *, double); -void xsetenv(void); void xsettitle(char *); void xsetpointermotion(int); void xseturgency(int); void xunloadfonts(void); void xresize(int, int); void xselpaste(void); -unsigned long xwinid(void); void xsetsel(char *, Time); diff --git a/x.c b/x.c index c484dfc..df2a88c 100644 --- a/x.c +++ b/x.c @@ -89,6 +89,7 @@ static void xdrawcursor(void); static int xgeommasktogravity(int); static int xloadfont(Font *, FcPattern *); static void xunloadfont(Font *); +static void xsetenv(void); static void expose(XEvent *); static void visibility(XEvent *); @@ -1487,12 +1488,6 @@ xbell(int vol) XkbBell(xw.dpy, xw.win, vol, (Atom)NULL); } -unsigned long -xwinid(void) -{ - return xw.win; -} - void focus(XEvent *ev) { @@ -1765,6 +1760,7 @@ run: XSetLocaleModifiers(""); tnew(MAX(cols, 1), MAX(rows, 1)); xinit(); + xsetenv(); selinit(); run(); -- cgit v1.2.3 From 3518dba2a5fb57f601b74528ddeb67f173e4024b Mon Sep 17 00:00:00 2001 From: "Devin J. Pohly" Date: Tue, 10 Oct 2017 11:11:27 -0500 Subject: Move usage() to be with run() in x.c run/usage/xinit are now all internal to x.c Signed-off-by: Devin J. Pohly --- st.c | 15 --------------- st.h | 2 -- win.h | 2 -- x.c | 18 ++++++++++++++++++ 4 files changed, 18 insertions(+), 19 deletions(-) (limited to 'win.h') diff --git a/st.c b/st.c index 668b312..6338510 100644 --- a/st.c +++ b/st.c @@ -28,8 +28,6 @@ #include #include -char *argv0; - #define Glyph Glyph_ #define Font Font_ @@ -2687,16 +2685,3 @@ cresize(int width, int height) tresize(col, row); xresize(col, row); } - -void -usage(void) -{ - die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]" - " [-n name] [-o file]\n" - " [-T title] [-t title] [-w windowid]" - " [[-e] command [args ...]]\n" - " %s [-aiv] [-c class] [-f font] [-g geometry]" - " [-n name] [-o file]\n" - " [-T title] [-t title] [-w windowid] -l line" - " [stty_args ...]\n", argv0, argv0); -} diff --git a/st.h b/st.h index 44d4938..28a751d 100644 --- a/st.h +++ b/st.h @@ -214,8 +214,6 @@ size_t utf8encode(Rune, char *); void *xmalloc(size_t); char *xstrdup(char *); -void usage(void); - /* Globals */ extern TermWindow win; extern Term term; diff --git a/win.h b/win.h index 423c114..60cecb1 100644 --- a/win.h +++ b/win.h @@ -9,13 +9,11 @@ typedef XftGlyphFontSpec GlyphFontSpec; void draw(void); void drawregion(int, int, int, int); -void run(void); void xbell(int); void xclipcopy(void); void xclippaste(void); void xhints(void); -void xinit(void); void xloadcols(void); int xsetcolorname(int, const char *); void xloadfonts(char *, double); diff --git a/x.c b/x.c index df2a88c..f660ca3 100644 --- a/x.c +++ b/x.c @@ -15,6 +15,7 @@ #include #include +static char *argv0; #include "arg.h" #define Glyph Glyph_ @@ -87,6 +88,7 @@ static void xdrawglyph(Glyph, int, int); static void xclear(int, int, int, int); static void xdrawcursor(void); static int xgeommasktogravity(int); +static void xinit(void); static int xloadfont(Font *, FcPattern *); static void xunloadfont(Font *); static void xsetenv(void); @@ -110,6 +112,9 @@ static void selcopy(Time); static void getbuttoninfo(XEvent *); static void mousereport(XEvent *); +static void run(void); +static void usage(void); + static void (*handler[LASTEvent])(XEvent *) = { [KeyPress] = kpress, [ClientMessage] = cmessage, @@ -1698,6 +1703,19 @@ run(void) } } +void +usage(void) +{ + die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]" + " [-n name] [-o file]\n" + " [-T title] [-t title] [-w windowid]" + " [[-e] command [args ...]]\n" + " %s [-aiv] [-c class] [-f font] [-g geometry]" + " [-n name] [-o file]\n" + " [-T title] [-t title] [-w windowid] -l line" + " [stty_args ...]\n", argv0, argv0); +} + int main(int argc, char *argv[]) { -- cgit v1.2.3 From d5275012b45149a2a6e94679609aacca478221ad Mon Sep 17 00:00:00 2001 From: "Devin J. Pohly" Date: Tue, 10 Oct 2017 11:30:36 -0500 Subject: Move zoom functions into x.c This makes x(un)loadfonts internal to x.c. Needed to reorder includes and move a typedef to keep the compiler happy. Signed-off-by: Devin J. Pohly --- st.c | 36 +----------------------------------- st.h | 1 + win.h | 7 +++---- x.c | 35 ++++++++++++++++++++++++++++++++++- 4 files changed, 39 insertions(+), 40 deletions(-) (limited to 'win.h') diff --git a/st.c b/st.c index 6338510..f1f7bc1 100644 --- a/st.c +++ b/st.c @@ -31,8 +31,8 @@ #define Glyph Glyph_ #define Font Font_ -#include "win.h" #include "st.h" +#include "win.h" #if defined(__linux) #include @@ -128,9 +128,6 @@ static void clipcopy(const Arg *); static void clippaste(const Arg *); static void numlock(const Arg *); static void selpaste(const Arg *); -static void zoom(const Arg *); -static void zoomabs(const Arg *); -static void zoomreset(const Arg *); static void printsel(const Arg *); static void printscreen(const Arg *) ; static void iso14755(const Arg *); @@ -2573,37 +2570,6 @@ tresize(int col, int row) term.c = c; } -void -zoom(const Arg *arg) -{ - Arg larg; - - larg.f = usedfontsize + arg->f; - zoomabs(&larg); -} - -void -zoomabs(const Arg *arg) -{ - xunloadfonts(); - xloadfonts(usedfont, arg->f); - cresize(0, 0); - ttyresize(); - redraw(); - xhints(); -} - -void -zoomreset(const Arg *arg) -{ - Arg larg; - - if (defaultfontsize > 0) { - larg.f = defaultfontsize; - zoomabs(&larg); - } -} - void resettitle(void) { diff --git a/st.h b/st.h index 28a751d..9ece72f 100644 --- a/st.h +++ b/st.h @@ -100,6 +100,7 @@ typedef struct { } Glyph; typedef Glyph *Line; +typedef XftGlyphFontSpec GlyphFontSpec; typedef struct { Glyph attr; /* current char attributes */ diff --git a/win.h b/win.h index 60cecb1..ea45e60 100644 --- a/win.h +++ b/win.h @@ -5,8 +5,6 @@ #define XK_NO_MOD 0 #define XK_SWITCH_MOD (1<<13) -typedef XftGlyphFontSpec GlyphFontSpec; - void draw(void); void drawregion(int, int, int, int); @@ -16,11 +14,12 @@ void xclippaste(void); void xhints(void); void xloadcols(void); int xsetcolorname(int, const char *); -void xloadfonts(char *, double); void xsettitle(char *); void xsetpointermotion(int); void xseturgency(int); -void xunloadfonts(void); void xresize(int, int); void xselpaste(void); void xsetsel(char *, Time); +void zoom(const Arg *); +void zoomabs(const Arg *); +void zoomreset(const Arg *); diff --git a/x.c b/x.c index f660ca3..426ca28 100644 --- a/x.c +++ b/x.c @@ -21,8 +21,8 @@ static char *argv0; #define Glyph Glyph_ #define Font Font_ -#include "win.h" #include "st.h" +#include "win.h" /* XEMBED messages */ #define XEMBED_FOCUS_IN 4 @@ -90,7 +90,9 @@ static void xdrawcursor(void); static int xgeommasktogravity(int); static void xinit(void); static int xloadfont(Font *, FcPattern *); +static void xloadfonts(char *, double); static void xunloadfont(Font *); +static void xunloadfonts(void); static void xsetenv(void); static void expose(XEvent *); @@ -164,6 +166,37 @@ typedef struct { static Fontcache frc[16]; static int frclen = 0; +void +zoom(const Arg *arg) +{ + Arg larg; + + larg.f = usedfontsize + arg->f; + zoomabs(&larg); +} + +void +zoomabs(const Arg *arg) +{ + xunloadfonts(); + xloadfonts(usedfont, arg->f); + cresize(0, 0); + ttyresize(); + redraw(); + xhints(); +} + +void +zoomreset(const Arg *arg) +{ + Arg larg; + + if (defaultfontsize > 0) { + larg.f = defaultfontsize; + zoomabs(&larg); + } +} + void getbuttoninfo(XEvent *e) { -- cgit v1.2.3 From 626b0ae40c71b6c1e02ece79bf033432647381a6 Mon Sep 17 00:00:00 2001 From: "Devin J. Pohly" Date: Tue, 10 Oct 2017 12:01:18 -0500 Subject: Move window urgency handling entirely into x.c This allows us to make xseturgency internal. Signed-off-by: Devin J. Pohly --- config.def.h | 2 +- st.c | 5 +---- st.h | 1 + win.h | 3 +-- x.c | 8 ++++++-- 5 files changed, 10 insertions(+), 9 deletions(-) (limited to 'win.h') diff --git a/config.def.h b/config.def.h index 877afab..dd94be2 100644 --- a/config.def.h +++ b/config.def.h @@ -60,7 +60,7 @@ unsigned int cursorthickness = 2; * bell volume. It must be a value between -100 and 100. Use 0 for disabling * it */ -static int bellvolume = 0; +int bellvolume = 0; /* default TERM value */ char termname[] = "st-256color"; diff --git a/st.c b/st.c index f1f7bc1..df43fac 100644 --- a/st.c +++ b/st.c @@ -2176,10 +2176,7 @@ tcontrolcode(uchar ascii) /* backwards compatibility to xterm */ strhandle(); } else { - if (!(win.state & WIN_FOCUSED)) - xseturgency(1); - if (bellvolume) - xbell(bellvolume); + xbell(); } break; case '\033': /* ESC */ diff --git a/st.h b/st.h index 9ece72f..ad94351 100644 --- a/st.h +++ b/st.h @@ -246,6 +246,7 @@ extern int allowaltscreen; extern unsigned int xfps; extern unsigned int actionfps; extern unsigned int cursorthickness; +extern int bellvolume; extern unsigned int blinktimeout; extern char termname[]; extern const char *colorname[]; diff --git a/win.h b/win.h index ea45e60..dee0b7f 100644 --- a/win.h +++ b/win.h @@ -8,7 +8,7 @@ void draw(void); void drawregion(int, int, int, int); -void xbell(int); +void xbell(void); void xclipcopy(void); void xclippaste(void); void xhints(void); @@ -16,7 +16,6 @@ void xloadcols(void); int xsetcolorname(int, const char *); void xsettitle(char *); void xsetpointermotion(int); -void xseturgency(int); void xresize(int, int); void xselpaste(void); void xsetsel(char *, Time); diff --git a/x.c b/x.c index 426ca28..5b3c97b 100644 --- a/x.c +++ b/x.c @@ -94,6 +94,7 @@ static void xloadfonts(char *, double); static void xunloadfont(Font *); static void xunloadfonts(void); static void xsetenv(void); +static void xseturgency(int); static void expose(XEvent *); static void visibility(XEvent *); @@ -1521,9 +1522,12 @@ xseturgency(int add) } void -xbell(int vol) +xbell(void) { - XkbBell(xw.dpy, xw.win, vol, (Atom)NULL); + if (!(win.state & WIN_FOCUSED)) + xseturgency(1); + if (bellvolume) + XkbBell(xw.dpy, xw.win, bellvolume, (Atom)NULL); } void -- cgit v1.2.3 From a8314643b1aeaa2187dad71dc5748aaac1760c1b Mon Sep 17 00:00:00 2001 From: "Devin J. Pohly" Date: Tue, 10 Oct 2017 12:46:53 -0500 Subject: Move window-manipulating functions into x.c xresize is now internal to x.c Signed-off-by: Devin J. Pohly --- st.c | 36 ------------------------------------ st.h | 9 +-------- win.h | 1 - x.c | 45 ++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 45 insertions(+), 46 deletions(-) (limited to 'win.h') diff --git a/st.c b/st.c index 540b487..75c191d 100644 --- a/st.c +++ b/st.c @@ -165,7 +165,6 @@ static void tnewline(int); static void tputtab(int); static void tputc(Rune); static void treset(void); -static void tresize(int, int); static void tscrollup(int, int); static void tscrolldown(int, int); static void tsetattr(int *, int); @@ -419,24 +418,6 @@ selinit(void) sel.clipboard = NULL; } -int -x2col(int x) -{ - x -= borderpx; - x /= win.cw; - - return LIMIT(x, 0, term.col-1); -} - -int -y2row(int y) -{ - y -= borderpx; - y /= win.ch; - - return LIMIT(y, 0, term.row-1); -} - int tlinelen(int y) { @@ -2620,20 +2601,3 @@ kmap(KeySym k, uint state) return NULL; } - -void -cresize(int width, int height) -{ - int col, row; - - if (width != 0) - win.w = width; - if (height != 0) - win.h = height; - - col = (win.w - 2 * borderpx) / win.cw; - row = (win.h - 2 * borderpx) / win.ch; - - tresize(col, row); - xresize(col, row); -} diff --git a/st.h b/st.h index 2199c13..5d44411 100644 --- a/st.h +++ b/st.h @@ -80,11 +80,6 @@ enum selection_snap { SNAP_LINE = 2 }; -enum window_state { - WIN_VISIBLE = 1, - WIN_FOCUSED = 2 -}; - typedef unsigned char uchar; typedef unsigned int uint; typedef unsigned long ulong; @@ -186,6 +181,7 @@ void redraw(void); int tattrset(int); void tnew(int, int); +void tresize(int, int); void tsetdirt(int, int); void tsetdirtattr(int); int match(uint, uint); @@ -198,15 +194,12 @@ void ttywrite(const char *, size_t); void resettitle(void); char *kmap(KeySym, uint); -void cresize(int, int); void selclear(void); void selinit(void); void selnormalize(void); int selected(int, int); char *getsel(void); -int x2col(int); -int y2row(int); size_t utf8decode(char *, Rune *, size_t); size_t utf8encode(Rune, char *); diff --git a/win.h b/win.h index dee0b7f..00113c5 100644 --- a/win.h +++ b/win.h @@ -16,7 +16,6 @@ void xloadcols(void); int xsetcolorname(int, const char *); void xsettitle(char *); void xsetpointermotion(int); -void xresize(int, int); void xselpaste(void); void xsetsel(char *, Time); void zoom(const Arg *); diff --git a/x.c b/x.c index 186e408..01ef1b0 100644 --- a/x.c +++ b/x.c @@ -88,12 +88,16 @@ static void xclear(int, int, int, int); static void xdrawcursor(void); static int xgeommasktogravity(int); static void xinit(void); +static void cresize(int, int); +static void xresize(int, int); static int xloadfont(Font *, FcPattern *); static void xloadfonts(char *, double); static void xunloadfont(Font *); static void xunloadfonts(void); static void xsetenv(void); static void xseturgency(int); +static int x2col(int); +static int y2row(int); static void expose(XEvent *); static void visibility(XEvent *); @@ -109,7 +113,6 @@ static void propnotify(XEvent *); static void selnotify(XEvent *); static void selclear_(XEvent *); static void selrequest(XEvent *); - static void selcopy(Time); static void getbuttoninfo(XEvent *); static void mousereport(XEvent *); @@ -148,6 +151,11 @@ static DC dc; static XWindow xw; static XSelection xsel; +enum window_state { + WIN_VISIBLE = 1, + WIN_FOCUSED = 2 +}; + /* Font Ring Cache */ enum { FRC_NORMAL, @@ -200,6 +208,24 @@ zoomreset(const Arg *arg) } } +int +x2col(int x) +{ + x -= borderpx; + x /= win.cw; + + return LIMIT(x, 0, term.col-1); +} + +int +y2row(int y) +{ + y -= borderpx; + y /= win.ch; + + return LIMIT(y, 0, term.row-1); +} + void getbuttoninfo(XEvent *e) { @@ -596,6 +622,23 @@ bmotion(XEvent *e) tsetdirt(MIN(sel.nb.y, oldsby), MAX(sel.ne.y, oldsey)); } +void +cresize(int width, int height) +{ + int col, row; + + if (width != 0) + win.w = width; + if (height != 0) + win.h = height; + + col = (win.w - 2 * borderpx) / win.cw; + row = (win.h - 2 * borderpx) / win.ch; + + tresize(col, row); + xresize(col, row); +} + void xresize(int col, int row) { -- cgit v1.2.3 From 65976c1a29f2945c3cfb6af74cd6440cf193021d Mon Sep 17 00:00:00 2001 From: "Devin J. Pohly" Date: Tue, 17 Oct 2017 15:21:04 -0500 Subject: Move config.h include from st.c to x.c config.h includes references to KeySyms and other X stuff. Until we come up with a cleaner way to separate configuration, it is simpler (leads to more code removal) to have this here. Signed-off-by: Devin J. Pohly --- config.def.h | 73 ++++++++++++++++++++++++++++++------------------------------ st.c | 47 ++++---------------------------------- st.h | 50 ++++++++++++----------------------------- win.h | 3 --- x.c | 37 ++++++++++++++++++++++++++++++ 5 files changed, 91 insertions(+), 119 deletions(-) (limited to 'win.h') diff --git a/config.def.h b/config.def.h index 18cb31c..1c181ab 100644 --- a/config.def.h +++ b/config.def.h @@ -5,8 +5,8 @@ * * font: see http://freedesktop.org/software/fontconfig/fontconfig-user.html */ -char font[] = "Liberation Mono:pixelsize=12:antialias=true:autohint=true"; -int borderpx = 2; +static char *font = "Liberation Mono:pixelsize=12:antialias=true:autohint=true"; +static int borderpx = 2; /* * What program is execed by st depends of these precedence rules: @@ -16,54 +16,54 @@ int borderpx = 2; * 4: value of shell in /etc/passwd * 5: value of shell in config.h */ -static char shell[] = "/bin/sh"; -static char *utmp = NULL; -static char stty_args[] = "stty raw pass8 nl -echo -iexten -cstopb 38400"; +char *shell = "/bin/sh"; +char *utmp = NULL; +char *stty_args = "stty raw pass8 nl -echo -iexten -cstopb 38400"; /* identification sequence returned in DA and DECID */ -static char vtiden[] = "\033[?6c"; +char *vtiden = "\033[?6c"; /* Kerning / character bounding-box multipliers */ -float cwscale = 1.0; -float chscale = 1.0; +static float cwscale = 1.0; +static float chscale = 1.0; /* * word delimiter string * * More advanced example: " `'\"()[]{}" */ -static char worddelimiters[] = " "; +char *worddelimiters = " "; /* selection timeouts (in milliseconds) */ -unsigned int doubleclicktimeout = 300; -unsigned int tripleclicktimeout = 600; +static unsigned int doubleclicktimeout = 300; +static unsigned int tripleclicktimeout = 600; /* alt screens */ int allowaltscreen = 1; /* frames per second st should at maximum draw to the screen */ -unsigned int xfps = 120; -unsigned int actionfps = 30; +static unsigned int xfps = 120; +static unsigned int actionfps = 30; /* * blinking timeout (set to 0 to disable blinking) for the terminal blinking * attribute. */ -unsigned int blinktimeout = 800; +static unsigned int blinktimeout = 800; /* * thickness of underline and bar cursors */ -unsigned int cursorthickness = 2; +static unsigned int cursorthickness = 2; /* * bell volume. It must be a value between -100 and 100. Use 0 for disabling * it */ -int bellvolume = 0; +static int bellvolume = 0; /* default TERM value */ -char termname[] = "st-256color"; +char *termname = "st-256color"; /* * spaces per tab @@ -80,10 +80,10 @@ char termname[] = "st-256color"; * * stty tabs */ -static unsigned int tabspaces = 8; +unsigned int tabspaces = 8; /* Terminal colors (16 first used in escape sequence) */ -const char *colorname[] = { +static const char *colorname[] = { /* 8 normal colors */ "black", "red3", @@ -118,8 +118,8 @@ const char *colorname[] = { */ unsigned int defaultfg = 7; unsigned int defaultbg = 0; -unsigned int defaultcs = 256; -unsigned int defaultrcs = 257; +static unsigned int defaultcs = 256; +static unsigned int defaultrcs = 257; /* * Default shape of cursor @@ -128,33 +128,33 @@ unsigned int defaultrcs = 257; * 6: Bar ("|") * 7: Snowman ("☃") */ -unsigned int cursorshape = 2; +static unsigned int cursorshape = 2; /* * Default columns and rows numbers */ -unsigned int cols = 80; -unsigned int rows = 24; +static unsigned int cols = 80; +static unsigned int rows = 24; /* * Default colour and shape of the mouse cursor */ -unsigned int mouseshape = XC_xterm; -unsigned int mousefg = 7; -unsigned int mousebg = 0; +static unsigned int mouseshape = XC_xterm; +static unsigned int mousefg = 7; +static unsigned int mousebg = 0; /* * Color used to display font attributes when fontconfig selected a font which * doesn't match the ones requested. */ -unsigned int defaultattr = 11; +static unsigned int defaultattr = 11; /* * Internal mouse shortcuts. * Beware that overloading Button1 will disable the selection. */ -MouseShortcut mshortcuts[] = { +static MouseShortcut mshortcuts[] = { /* button mask string */ { Button4, XK_ANY_MOD, "\031" }, { Button5, XK_ANY_MOD, "\005" }, @@ -164,7 +164,7 @@ MouseShortcut mshortcuts[] = { #define MODKEY Mod1Mask #define TERMMOD (ControlMask|ShiftMask) -Shortcut shortcuts[] = { +static Shortcut shortcuts[] = { /* mask keysym function argument */ { XK_ANY_MOD, XK_Break, sendbreak, {.i = 0} }, { ControlMask, XK_Print, toggleprinter, {.i = 0} }, @@ -209,26 +209,26 @@ Shortcut shortcuts[] = { * If you want keys other than the X11 function keys (0xFD00 - 0xFFFF) * to be mapped below, add them to this array. */ -KeySym mappedkeys[] = { -1 }; +static KeySym mappedkeys[] = { -1 }; /* * State bits to ignore when matching key or button events. By default, * numlock (Mod2Mask) and keyboard layout (XK_SWITCH_MOD) are ignored. */ -uint ignoremod = Mod2Mask|XK_SWITCH_MOD; +static uint ignoremod = Mod2Mask|XK_SWITCH_MOD; /* * Override mouse-select while mask is active (when MODE_MOUSE is set). * Note that if you want to use ShiftMask with selmasks, set this to an other * modifier, set to 0 to not use it. */ -uint forceselmod = ShiftMask; +static uint forceselmod = ShiftMask; /* * This is the huge key array which defines all compatibility to the Linux * world. Please decide about changes wisely. */ -Key key[] = { +static Key key[] = { /* keysym mask string appkey appcursor crlf */ { XK_KP_Home, ShiftMask, "\033[2J", 0, -1, 0}, { XK_KP_Home, ShiftMask, "\033[1;2H", 0, +1, 0}, @@ -451,7 +451,7 @@ Key key[] = { * ButtonRelease and MotionNotify. * If no match is found, regular selection is used. */ -uint selmasks[] = { +static uint selmasks[] = { [SEL_RECTANGULAR] = Mod1Mask, }; @@ -459,8 +459,7 @@ uint selmasks[] = { * Printable characters in ASCII, used to estimate the advance width * of single wide characters. */ -char ascii_printable[] = +static char ascii_printable[] = " !\"#$%&'()*+,-./0123456789:;<=>?" "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_" "`abcdefghijklmnopqrstuvwxyz{|}~"; - diff --git a/st.c b/st.c index 641f896..ec747cc 100644 --- a/st.c +++ b/st.c @@ -109,19 +109,6 @@ typedef struct { int narg; /* nb of args */ } STREscape; -/* function definitions used in config.h */ -static void clipcopy(const Arg *); -static void clippaste(const Arg *); -static void numlock(const Arg *); -static void selpaste(const Arg *); -static void printsel(const Arg *); -static void printscreen(const Arg *) ; -static void iso14755(const Arg *); -static void toggleprinter(const Arg *); -static void sendbreak(const Arg *); - -/* config.h for applying patches and the configuration. */ -#include "config.h" static void execsh(char **); static void stty(char **); @@ -199,14 +186,6 @@ static uchar utfmask[UTF_SIZ + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8}; static Rune utfmin[UTF_SIZ + 1] = { 0, 0, 0x80, 0x800, 0x10000}; static Rune utfmax[UTF_SIZ + 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF}; -/* config.h array lengths */ -size_t colornamelen = LEN(colorname); -size_t mshortcutslen = LEN(mshortcuts); -size_t shortcutslen = LEN(shortcuts); -size_t selmaskslen = LEN(selmasks); -size_t keyslen = LEN(key); -size_t mappedkeyslen = LEN(mappedkeys); - ssize_t xwrite(int fd, const char *s, size_t len) { @@ -585,24 +564,6 @@ getsel(void) return str; } -void -selpaste(const Arg *dummy) -{ - xselpaste(); -} - -void -clipcopy(const Arg *dummy) -{ - xclipcopy(); -} - -void -clippaste(const Arg *dummy) -{ - xclippaste(); -} - void selclear(void) { @@ -1572,7 +1533,7 @@ csihandle(void) break; case 'c': /* DA -- Device Attributes */ if (csiescseq.arg[0] == 0) - ttywrite(vtiden, sizeof(vtiden) - 1); + ttywrite(vtiden, strlen(vtiden)); break; case 'C': /* CUF -- Cursor Forward */ case 'a': /* HPR -- Cursor Forward */ @@ -1791,7 +1752,7 @@ strhandle(void) dec = base64dec(strescseq.args[2]); if (dec) { xsetsel(dec, CurrentTime); - clipcopy(NULL); + xclipcopy(); } else { fprintf(stderr, "erresc: invalid base64\n"); } @@ -2134,7 +2095,7 @@ tcontrolcode(uchar ascii) case 0x99: /* TODO: SGCI */ break; case 0x9a: /* DECID -- Identify Terminal */ - ttywrite(vtiden, sizeof(vtiden) - 1); + ttywrite(vtiden, strlen(vtiden)); break; case 0x9b: /* TODO: CSI */ case 0x9c: /* TODO: ST */ @@ -2206,7 +2167,7 @@ eschandle(uchar ascii) } break; case 'Z': /* DECID -- Identify Terminal */ - ttywrite(vtiden, sizeof(vtiden) - 1); + ttywrite(vtiden, strlen(vtiden)); break; case 'c': /* RIS -- Reset to inital state */ treset(); diff --git a/st.h b/st.h index 3d9b6e7..9314607 100644 --- a/st.h +++ b/st.h @@ -190,6 +190,13 @@ typedef struct { void die(const char *, ...); void redraw(void); +void iso14755(const Arg *); +void numlock(const Arg *); +void printscreen(const Arg *); +void printsel(const Arg *); +void sendbreak(const Arg *); +void toggleprinter(const Arg *); + int tattrset(int); void tnew(int, int); void tresize(int, int); @@ -225,42 +232,13 @@ extern pid_t pid; extern int oldbutton; /* config.h globals */ -extern char font[]; -extern int borderpx; -extern float cwscale; -extern float chscale; -extern unsigned int doubleclicktimeout; -extern unsigned int tripleclicktimeout; +extern char *shell; +extern char *utmp; +extern char *stty_args; +extern char *vtiden; +extern char *worddelimiters; extern int allowaltscreen; -extern unsigned int xfps; -extern unsigned int actionfps; -extern unsigned int cursorthickness; -extern int bellvolume; -extern unsigned int blinktimeout; -extern char termname[]; -extern const char *colorname[]; -extern size_t colornamelen; +extern char *termname; +extern unsigned int tabspaces; extern unsigned int defaultfg; extern unsigned int defaultbg; -extern unsigned int defaultcs; -extern unsigned int defaultrcs; -extern unsigned int cursorshape; -extern unsigned int cols; -extern unsigned int rows; -extern unsigned int mouseshape; -extern unsigned int mousefg; -extern unsigned int mousebg; -extern unsigned int defaultattr; -extern MouseShortcut mshortcuts[]; -extern size_t mshortcutslen; -extern Shortcut shortcuts[]; -extern size_t shortcutslen; -extern KeySym mappedkeys[]; -extern size_t mappedkeyslen; -extern uint ignoremod; -extern uint forceselmod; -extern Key key[]; -extern size_t keyslen; -extern uint selmasks[]; -extern size_t selmaskslen; -extern char ascii_printable[]; diff --git a/win.h b/win.h index 00113c5..b7022ec 100644 --- a/win.h +++ b/win.h @@ -18,6 +18,3 @@ void xsettitle(char *); void xsetpointermotion(int); void xselpaste(void); void xsetsel(char *, Time); -void zoom(const Arg *); -void zoomabs(const Arg *); -void zoomreset(const Arg *); diff --git a/x.c b/x.c index e267961..cb8c351 100644 --- a/x.c +++ b/x.c @@ -20,6 +20,25 @@ static char *argv0; #include "st.h" #include "win.h" +/* function definitions used in config.h */ +static void clipcopy(const Arg *); +static void clippaste(const Arg *); +static void selpaste(const Arg *); +static void zoom(const Arg *); +static void zoomabs(const Arg *); +static void zoomreset(const Arg *); + +/* config.h for applying patches and the configuration. */ +#include "config.h" + +/* config.h array lengths */ +size_t colornamelen = LEN(colorname); +size_t mshortcutslen = LEN(mshortcuts); +size_t shortcutslen = LEN(shortcuts); +size_t selmaskslen = LEN(selmasks); +size_t keyslen = LEN(key); +size_t mappedkeyslen = LEN(mappedkeys); + /* XEMBED messages */ #define XEMBED_FOCUS_IN 4 #define XEMBED_FOCUS_OUT 5 @@ -188,6 +207,24 @@ static char *opt_line = NULL; static char *opt_name = NULL; static char *opt_title = NULL; +void +clipcopy(const Arg *dummy) +{ + xclipcopy(); +} + +void +clippaste(const Arg *dummy) +{ + xclippaste(); +} + +void +selpaste(const Arg *dummy) +{ + xselpaste(); +} + void zoom(const Arg *arg) { -- cgit v1.2.3 From 428f01969aaf48ffa2983746c0a397bcc8946799 Mon Sep 17 00:00:00 2001 From: "Devin J. Pohly" Date: Tue, 17 Oct 2017 15:43:32 -0500 Subject: Inline clipboard functions No need to keep a function that only calls another function in the same file. Signed-off-by: Devin J. Pohly --- win.h | 2 -- x.c | 50 +++++++++++++++++++------------------------------- 2 files changed, 19 insertions(+), 33 deletions(-) (limited to 'win.h') diff --git a/win.h b/win.h index b7022ec..beb458d 100644 --- a/win.h +++ b/win.h @@ -10,11 +10,9 @@ void drawregion(int, int, int, int); void xbell(void); void xclipcopy(void); -void xclippaste(void); void xhints(void); void xloadcols(void); int xsetcolorname(int, const char *); void xsettitle(char *); void xsetpointermotion(int); -void xselpaste(void); void xsetsel(char *, Time); diff --git a/x.c b/x.c index cb8c351..46356fe 100644 --- a/x.c +++ b/x.c @@ -210,19 +210,33 @@ static char *opt_title = NULL; void clipcopy(const Arg *dummy) { - xclipcopy(); + Atom clipboard; + + if (sel.clipboard != NULL) + free(sel.clipboard); + + if (sel.primary != NULL) { + sel.clipboard = xstrdup(sel.primary); + clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0); + XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime); + } } void clippaste(const Arg *dummy) { - xclippaste(); + Atom clipboard; + + clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0); + XConvertSelection(xw.dpy, clipboard, xsel.xtarget, clipboard, + xw.win, CurrentTime); } void selpaste(const Arg *dummy) { - xselpaste(); + XConvertSelection(xw.dpy, XA_PRIMARY, xsel.xtarget, XA_PRIMARY, + xw.win, CurrentTime); } void @@ -518,36 +532,10 @@ selnotify(XEvent *e) XDeleteProperty(xw.dpy, xw.win, (int)property); } -void -xselpaste(void) -{ - XConvertSelection(xw.dpy, XA_PRIMARY, xsel.xtarget, XA_PRIMARY, - xw.win, CurrentTime); -} - void xclipcopy(void) { - Atom clipboard; - - if (sel.clipboard != NULL) - free(sel.clipboard); - - if (sel.primary != NULL) { - sel.clipboard = xstrdup(sel.primary); - clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0); - XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime); - } -} - -void -xclippaste(void) -{ - Atom clipboard; - - clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0); - XConvertSelection(xw.dpy, clipboard, xsel.xtarget, clipboard, - xw.win, CurrentTime); + clipcopy(NULL); } void @@ -634,7 +622,7 @@ brelease(XEvent *e) } if (e->xbutton.button == Button2) { - xselpaste(); + selpaste(NULL); } else if (e->xbutton.button == Button1) { if (sel.mode == SEL_READY) { getbuttoninfo(e); -- cgit v1.2.3 From 323d38da20c8a1d295ab1dbc0fc7ce947ef824e1 Mon Sep 17 00:00:00 2001 From: "Devin J. Pohly" Date: Mon, 6 Nov 2017 17:57:45 -0600 Subject: Make win variable internal to x.c There was only a single reference to the `win` variable in st.c, so exporting that to x.c allows us to rid ourselves of another extern. Signed-off-by: Devin J. Pohly --- st.c | 6 +----- st.h | 1 - win.h | 1 + x.c | 11 +++++++++++ 4 files changed, 13 insertions(+), 6 deletions(-) (limited to 'win.h') diff --git a/st.c b/st.c index ec747cc..fdf697b 100644 --- a/st.c +++ b/st.c @@ -170,7 +170,6 @@ static char *base64dec(const char *); static ssize_t xwrite(int, const char *, size_t); /* Globals */ -TermWindow win; Term term; Selection sel; int cmdfd; @@ -1683,11 +1682,8 @@ csihandle(void) case ' ': switch (csiescseq.mode[1]) { case 'q': /* DECSCUSR -- Set Cursor Style */ - DEFAULT(csiescseq.arg[0], 1); - if (!BETWEEN(csiescseq.arg[0], 0, 6)) { + if (xsetcursor(csiescseq.arg[0])) goto unknown; - } - win.cursor = csiescseq.arg[0]; break; default: goto unknown; diff --git a/st.h b/st.h index 71c79f4..8637d35 100644 --- a/st.h +++ b/st.h @@ -201,7 +201,6 @@ void *xrealloc(void *, size_t); char *xstrdup(char *); /* Globals */ -extern TermWindow win; extern Term term; extern Selection sel; extern int cmdfd; diff --git a/win.h b/win.h index beb458d..c6a5337 100644 --- a/win.h +++ b/win.h @@ -14,5 +14,6 @@ void xhints(void); void xloadcols(void); int xsetcolorname(int, const char *); void xsettitle(char *); +int xsetcursor(int); void xsetpointermotion(int); void xsetsel(char *, Time); diff --git a/x.c b/x.c index 24f6991..04e2e05 100644 --- a/x.c +++ b/x.c @@ -187,6 +187,7 @@ static void (*handler[LASTEvent])(XEvent *) = { static DC dc; static XWindow xw; static XSelection xsel; +static TermWindow win; enum window_state { WIN_VISIBLE = 1, @@ -1615,6 +1616,16 @@ xsetpointermotion(int set) XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask, &xw.attrs); } +int +xsetcursor(int cursor) +{ + DEFAULT(cursor, 1); + if (!BETWEEN(cursor, 0, 6)) + return 1; + win.cursor = cursor; + return 0; +} + void xseturgency(int add) { -- cgit v1.2.3 From 3bb900cd6c1c7a5364bd79bce63fdd8711bc878b Mon Sep 17 00:00:00 2001 From: "Devin J. Pohly" Date: Mon, 6 Nov 2017 18:25:58 -0600 Subject: Remove Time argument from xsetsel This is an X type and should be internal to x.c. The selcopy() function was a single line and only used in one place, so it was inlined to reduce LOC. Signed-off-by: Devin J. Pohly --- st.c | 2 +- win.h | 2 +- x.c | 18 +++++++++--------- 3 files changed, 11 insertions(+), 11 deletions(-) (limited to 'win.h') diff --git a/st.c b/st.c index fdf697b..bcb6473 100644 --- a/st.c +++ b/st.c @@ -1747,7 +1747,7 @@ strhandle(void) dec = base64dec(strescseq.args[2]); if (dec) { - xsetsel(dec, CurrentTime); + xsetsel(dec); xclipcopy(); } else { fprintf(stderr, "erresc: invalid base64\n"); diff --git a/win.h b/win.h index c6a5337..f95a679 100644 --- a/win.h +++ b/win.h @@ -16,4 +16,4 @@ int xsetcolorname(int, const char *); void xsettitle(char *); int xsetcursor(int); void xsetpointermotion(int); -void xsetsel(char *, Time); +void xsetsel(char *); diff --git a/x.c b/x.c index 04e2e05..a332ac9 100644 --- a/x.c +++ b/x.c @@ -148,7 +148,7 @@ static void propnotify(XEvent *); static void selnotify(XEvent *); static void selclear_(XEvent *); static void selrequest(XEvent *); -static void selcopy(Time); +static void setsel(char *, Time); static void getbuttoninfo(XEvent *); static void mousereport(XEvent *); static char *kmap(KeySym, uint); @@ -440,12 +440,6 @@ bpress(XEvent *e) } } -void -selcopy(Time t) -{ - xsetsel(getsel(), t); -} - void propnotify(XEvent *e) { @@ -620,7 +614,7 @@ selrequest(XEvent *e) } void -xsetsel(char *str, Time t) +setsel(char *str, Time t) { free(sel.primary); sel.primary = str; @@ -630,6 +624,12 @@ xsetsel(char *str, Time t) selclear_(NULL); } +void +xsetsel(char *str) +{ + setsel(str, CurrentTime); +} + void brelease(XEvent *e) { @@ -643,7 +643,7 @@ brelease(XEvent *e) } else if (e->xbutton.button == Button1) { if (sel.mode == SEL_READY) { getbuttoninfo(e); - selcopy(e->xbutton.time); + setsel(getsel(), e->xbutton.time); } else selclear_(NULL); sel.mode = SEL_IDLE; -- cgit v1.2.3 From 8b564c1a3f51c08e64c2f589852a02b8595d44ca Mon Sep 17 00:00:00 2001 From: "Devin J. Pohly" Date: Mon, 6 Nov 2017 18:30:45 -0600 Subject: Remove X and fontconfig from st.c None of the X-related includes are needed any longer. In addition, move the X modifier defines into x.c, as they are not used outside. Signed-off-by: Devin J. Pohly --- st.c | 5 ----- win.h | 5 ----- x.c | 5 +++++ 3 files changed, 5 insertions(+), 10 deletions(-) (limited to 'win.h') diff --git a/st.c b/st.c index bcb6473..9cfa547 100644 --- a/st.c +++ b/st.c @@ -21,13 +21,8 @@ #include #include #include -#include #include -/* X11 */ -#include -#include - #include "st.h" #include "win.h" diff --git a/win.h b/win.h index f95a679..123662e 100644 --- a/win.h +++ b/win.h @@ -1,10 +1,5 @@ /* See LICENSE for license details. */ -/* X modifiers */ -#define XK_ANY_MOD UINT_MAX -#define XK_NO_MOD 0 -#define XK_SWITCH_MOD (1<<13) - void draw(void); void drawregion(int, int, int, int); diff --git a/x.c b/x.c index a332ac9..e5b236d 100644 --- a/x.c +++ b/x.c @@ -44,6 +44,11 @@ typedef struct { signed char crlf; /* crlf mode */ } Key; +/* X modifiers */ +#define XK_ANY_MOD UINT_MAX +#define XK_NO_MOD 0 +#define XK_SWITCH_MOD (1<<13) + /* function definitions used in config.h */ static void clipcopy(const Arg *); static void clippaste(const Arg *); -- cgit v1.2.3 From 05c66cb37d9ff278a3e0c45682c4b5e7945deb42 Mon Sep 17 00:00:00 2001 From: "Devin J. Pohly" Date: Fri, 23 Feb 2018 14:16:52 -0600 Subject: Split mode bits between Term and TermWindow Moves the mode bits used by x.c from Term to TermWindow, absorbing UI/input-related mode bits (visible/focused/numlock) along the way. This is gradually reducing external references to Term. Since TermWindow is already internal to x.c, we add xsetmode() to allow st to modify window bits in accordance with escape sequences. IS_SET() is redefined accordingly (term.mode in st.c, win.mode in x.c). Signed-off-by: Devin J. Pohly --- st.c | 64 ++++++++++++++++++++++++++++++++-------------------------------- st.h | 33 +-------------------------------- win.h | 24 ++++++++++++++++++++++++ x.c | 50 +++++++++++++++++++++++++++++++------------------- 4 files changed, 88 insertions(+), 83 deletions(-) (limited to 'win.h') diff --git a/st.c b/st.c index 3ebf8c6..01791a5 100644 --- a/st.c +++ b/st.c @@ -42,6 +42,7 @@ #define STR_ARG_SIZ ESC_ARG_SIZ /* macros */ +#define IS_SET(flag) ((term.mode & (flag)) != 0) #define NUMMAXLEN(x) ((int)(sizeof(x) * 2.56 + 0.5) + 1) #define ISCONTROLC0(c) (BETWEEN(c, 0, 0x1f) || (c) == '\177') #define ISCONTROLC1(c) (BETWEEN(c, 0x80, 0x9f)) @@ -51,6 +52,17 @@ /* constants */ #define ISO14755CMD "dmenu -w \"$WINDOWID\" -p codepoint: ': /* DECPNM -- Normal keypad */ - term.mode &= ~MODE_APPKEYPAD; + xsetmode(0, MODE_APPKEYPAD); break; case '7': /* DECSC -- Save Cursor */ tcursor(CURSOR_SAVE); @@ -2526,9 +2532,3 @@ redraw(void) tfulldirt(); draw(); } - -void -numlock(const Arg *dummy) -{ - term.numlock ^= 1; -} diff --git a/st.h b/st.h index d7738a0..3382d61 100644 --- a/st.h +++ b/st.h @@ -13,7 +13,6 @@ #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x) #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || \ (a).bg != (b).bg) -#define IS_SET(flag) ((term.mode & (flag)) != 0) #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + \ (t1.tv_nsec-t2.tv_nsec)/1E6) #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit))) @@ -37,34 +36,6 @@ enum glyph_attribute { ATTR_BOLD_FAINT = ATTR_BOLD | ATTR_FAINT, }; -enum term_mode { - MODE_WRAP = 1 << 0, - MODE_INSERT = 1 << 1, - MODE_APPKEYPAD = 1 << 2, - MODE_ALTSCREEN = 1 << 3, - MODE_CRLF = 1 << 4, - MODE_MOUSEBTN = 1 << 5, - MODE_MOUSEMOTION = 1 << 6, - MODE_REVERSE = 1 << 7, - MODE_KBDLOCK = 1 << 8, - MODE_HIDE = 1 << 9, - MODE_ECHO = 1 << 10, - MODE_APPCURSOR = 1 << 11, - MODE_MOUSESGR = 1 << 12, - MODE_8BIT = 1 << 13, - MODE_BLINK = 1 << 14, - MODE_FBLINK = 1 << 15, - MODE_FOCUS = 1 << 16, - MODE_MOUSEX10 = 1 << 17, - MODE_MOUSEMANY = 1 << 18, - MODE_BRCKTPASTE = 1 << 19, - MODE_PRINT = 1 << 20, - MODE_UTF8 = 1 << 21, - MODE_SIXEL = 1 << 22, - MODE_MOUSE = MODE_MOUSEBTN|MODE_MOUSEMOTION|MODE_MOUSEX10\ - |MODE_MOUSEMANY, -}; - enum selection_mode { SEL_IDLE = 0, SEL_EMPTY = 1, @@ -120,7 +91,6 @@ typedef struct { char trantbl[4]; /* charset table translation */ int charset; /* current charset */ int icharset; /* selected charset for sequence */ - int numlock; /* lock numbers in keyboard */ int *tabs; } Term; @@ -130,7 +100,7 @@ typedef struct { int w, h; /* window width and height */ int ch; /* char height */ int cw; /* char width */ - char state; /* focus, redraw, visible */ + int mode; /* window state/mode flags */ int cursor; /* cursor style */ } TermWindow; @@ -163,7 +133,6 @@ void die(const char *, ...); void redraw(void); void iso14755(const Arg *); -void numlock(const Arg *); void printscreen(const Arg *); void printsel(const Arg *); void sendbreak(const Arg *); diff --git a/win.h b/win.h index 123662e..1e08b16 100644 --- a/win.h +++ b/win.h @@ -1,5 +1,28 @@ /* See LICENSE for license details. */ +enum win_mode { + MODE_VISIBLE = 1 << 0, + MODE_FOCUSED = 1 << 1, + MODE_APPKEYPAD = 1 << 2, + MODE_MOUSEBTN = 1 << 3, + MODE_MOUSEMOTION = 1 << 4, + MODE_REVERSE = 1 << 5, + MODE_KBDLOCK = 1 << 6, + MODE_HIDE = 1 << 7, + MODE_APPCURSOR = 1 << 8, + MODE_MOUSESGR = 1 << 9, + MODE_8BIT = 1 << 10, + MODE_BLINK = 1 << 11, + MODE_FBLINK = 1 << 12, + MODE_FOCUS = 1 << 13, + MODE_MOUSEX10 = 1 << 14, + MODE_MOUSEMANY = 1 << 15, + MODE_BRCKTPASTE = 1 << 16, + MODE_NUMLOCK = 1 << 17, + MODE_MOUSE = MODE_MOUSEBTN|MODE_MOUSEMOTION|MODE_MOUSEX10\ + |MODE_MOUSEMANY, +}; + void draw(void); void drawregion(int, int, int, int); @@ -10,5 +33,6 @@ void xloadcols(void); int xsetcolorname(int, const char *); void xsettitle(char *); int xsetcursor(int); +void xsetmode(int, unsigned int); void xsetpointermotion(int); void xsetsel(char *); diff --git a/x.c b/x.c index 76fb910..c5826cf 100644 --- a/x.c +++ b/x.c @@ -51,6 +51,7 @@ typedef struct { /* function definitions used in config.h */ static void clipcopy(const Arg *); static void clippaste(const Arg *); +static void numlock(const Arg *); static void selpaste(const Arg *); static void zoom(const Arg *); static void zoomabs(const Arg *); @@ -64,6 +65,7 @@ static void zoomreset(const Arg *); #define XEMBED_FOCUS_OUT 5 /* macros */ +#define IS_SET(flag) ((win.mode & (flag)) != 0) #define TRUERED(x) (((x) & 0xff0000) >> 8) #define TRUEGREEN(x) (((x) & 0xff00)) #define TRUEBLUE(x) (((x) & 0xff) << 8) @@ -196,11 +198,6 @@ static XWindow xw; static XSelection xsel; static TermWindow win; -enum window_state { - WIN_VISIBLE = 1, - WIN_FOCUSED = 2 -}; - /* Font Ring Cache */ enum { FRC_NORMAL, @@ -263,6 +260,12 @@ selpaste(const Arg *dummy) xw.win, CurrentTime); } +void +numlock(const Arg *dummy) +{ + win.mode ^= MODE_NUMLOCK; +} + void zoom(const Arg *arg) { @@ -1090,6 +1093,7 @@ xinit(void) XChangeProperty(xw.dpy, xw.win, xw.netwmpid, XA_CARDINAL, 32, PropModeReplace, (uchar *)&thispid, 1); + win.mode = MODE_NUMLOCK; resettitle(); XMapWindow(xw.dpy, xw.win); xhints(); @@ -1319,14 +1323,13 @@ xdrawglyphfontspecs(const XftGlyphFontSpec *specs, Glyph base, int len, int x, i fg = &revfg; } - if (base.mode & ATTR_REVERSE) { temp = fg; fg = bg; bg = temp; } - if (base.mode & ATTR_BLINK && term.mode & MODE_BLINK) + if (base.mode & ATTR_BLINK && win.mode & MODE_BLINK) fg = bg; if (base.mode & ATTR_INVISIBLE) @@ -1440,7 +1443,7 @@ xdrawcursor(void) return; /* draw the new one */ - if (win.state & WIN_FOCUSED) { + if (IS_SET(MODE_FOCUSED)) { switch (win.cursor) { case 7: /* st extension: snowman */ utf8decode("☃", &g.u, UTF_SIZ); @@ -1527,7 +1530,7 @@ drawregion(int x1, int y1, int x2, int y2) Glyph base, new; XftGlyphFontSpec *specs; - if (!(win.state & WIN_VISIBLE)) + if (!(IS_SET(MODE_VISIBLE))) return; for (y = y1; y < y2; y++) { @@ -1575,13 +1578,13 @@ visibility(XEvent *ev) { XVisibilityEvent *e = &ev->xvisibility; - MODBIT(win.state, e->state != VisibilityFullyObscured, WIN_VISIBLE); + MODBIT(win.mode, e->state != VisibilityFullyObscured, MODE_VISIBLE); } void unmap(XEvent *ev) { - win.state &= ~WIN_VISIBLE; + win.mode &= ~MODE_VISIBLE; } void @@ -1591,6 +1594,15 @@ xsetpointermotion(int set) XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask, &xw.attrs); } +void +xsetmode(int set, unsigned int flags) +{ + int mode = win.mode; + MODBIT(win.mode, set, flags); + if ((win.mode & MODE_REVERSE) != (mode & MODE_REVERSE)) + redraw(); +} + int xsetcursor(int cursor) { @@ -1614,7 +1626,7 @@ xseturgency(int add) void xbell(void) { - if (!(win.state & WIN_FOCUSED)) + if (!(IS_SET(MODE_FOCUSED))) xseturgency(1); if (bellvolume) XkbBell(xw.dpy, xw.win, bellvolume, (Atom)NULL); @@ -1630,13 +1642,13 @@ focus(XEvent *ev) if (ev->type == FocusIn) { XSetICFocus(xw.xic); - win.state |= WIN_FOCUSED; + win.mode |= MODE_FOCUSED; xseturgency(0); if (IS_SET(MODE_FOCUS)) ttywrite("\033[I", 3, 0); } else { XUnsetICFocus(xw.xic); - win.state &= ~WIN_FOCUSED; + win.mode &= ~MODE_FOCUSED; if (IS_SET(MODE_FOCUS)) ttywrite("\033[O", 3, 0); } @@ -1673,7 +1685,7 @@ kmap(KeySym k, uint state) if (IS_SET(MODE_APPKEYPAD) ? kp->appkey < 0 : kp->appkey > 0) continue; - if (term.numlock && kp->appkey == 2) + if (IS_SET(MODE_NUMLOCK) && kp->appkey == 2) continue; if (IS_SET(MODE_APPCURSOR) ? kp->appcursor < 0 : kp->appcursor > 0) @@ -1742,10 +1754,10 @@ cmessage(XEvent *e) */ if (e->xclient.message_type == xw.xembed && e->xclient.format == 32) { if (e->xclient.data.l[1] == XEMBED_FOCUS_IN) { - win.state |= WIN_FOCUSED; + win.mode |= MODE_FOCUSED; xseturgency(0); } else if (e->xclient.data.l[1] == XEMBED_FOCUS_OUT) { - win.state &= ~WIN_FOCUSED; + win.mode &= ~MODE_FOCUSED; } } else if (e->xclient.data.l[0] == xw.wmdeletewin) { /* Send SIGHUP to shell */ @@ -1810,7 +1822,7 @@ run(void) if (blinktimeout) { blinkset = tattrset(ATTR_BLINK); if (!blinkset) - MODBIT(term.mode, 0, MODE_BLINK); + MODBIT(win.mode, 0, MODE_BLINK); } } @@ -1825,7 +1837,7 @@ run(void) dodraw = 0; if (blinktimeout && TIMEDIFF(now, lastblink) > blinktimeout) { tsetdirtattr(ATTR_BLINK); - term.mode ^= MODE_BLINK; + win.mode ^= MODE_BLINK; lastblink = now; dodraw = 1; } -- cgit v1.2.3 From 88d8293fb4ba150a5f19d58d133b5db93d9dcfa5 Mon Sep 17 00:00:00 2001 From: "Devin J. Pohly" Date: Sat, 24 Feb 2018 14:53:23 -0600 Subject: Move win-agnostic parts of draw/drawregion to st.c Introduces three functions to encapsulate X-specific behavior: * xdrawline: draws a portion of a single line (used by drawregion) * xbegindraw: called to prepare for drawing (will be useful for e.g. Wayland) and returns true if drawing should happen * xfinishdraw: called to finish drawing (used by draw) Signed-off-by: Devin J. Pohly --- st.c | 25 +++++++++++++++++++++ st.h | 1 + win.h | 7 +++--- x.c | 79 ++++++++++++++++++++++++++++++------------------------------------- 4 files changed, 65 insertions(+), 47 deletions(-) (limited to 'win.h') diff --git a/st.c b/st.c index 01791a5..504239e 100644 --- a/st.c +++ b/st.c @@ -166,6 +166,8 @@ static int32_t tdefcolor(int *, int *, int); static void tdeftran(char); static void tstrsequence(uchar); +static void drawregion(int, int, int, int); + static void selscroll(int, int); static void selsnap(int *, int *, int); @@ -2526,6 +2528,29 @@ resettitle(void) xsettitle(NULL); } +void +drawregion(int x1, int y1, int x2, int y2) +{ + int y; + for (y = y1; y < y2; y++) { + if (!term.dirty[y]) + continue; + + term.dirty[y] = 0; + xdrawline(term.line[y], x1, y, x2); + } +} + +void +draw(void) +{ + if (!xstartdraw()) + return; + drawregion(0, 0, term.col, term.row); + xdrawcursor(); + xfinishdraw(); +} + void redraw(void) { diff --git a/st.h b/st.h index 3382d61..7026de8 100644 --- a/st.h +++ b/st.h @@ -131,6 +131,7 @@ typedef union { void die(const char *, ...); void redraw(void); +void draw(void); void iso14755(const Arg *); void printscreen(const Arg *); diff --git a/win.h b/win.h index 1e08b16..6e662af 100644 --- a/win.h +++ b/win.h @@ -23,12 +23,12 @@ enum win_mode { |MODE_MOUSEMANY, }; -void draw(void); -void drawregion(int, int, int, int); - void xbell(void); void xclipcopy(void); +void xdrawcursor(void); +void xdrawline(Line, int, int, int); void xhints(void); +void xfinishdraw(void); void xloadcols(void); int xsetcolorname(int, const char *); void xsettitle(char *); @@ -36,3 +36,4 @@ int xsetcursor(int); void xsetmode(int, unsigned int); void xsetpointermotion(int); void xsetsel(char *); +int xstartdraw(void); diff --git a/x.c b/x.c index c5826cf..96944ee 100644 --- a/x.c +++ b/x.c @@ -129,7 +129,6 @@ static int xmakeglyphfontspecs(XftGlyphFontSpec *, const Glyph *, int, int, int) static void xdrawglyphfontspecs(const XftGlyphFontSpec *, Glyph, int, int, int); static void xdrawglyph(Glyph, int, int); static void xclear(int, int, int, int); -static void xdrawcursor(void); static int xgeommasktogravity(int); static void xinit(void); static void cresize(int, int); @@ -1512,59 +1511,51 @@ xsettitle(char *p) XFree(prop.value); } -void -draw(void) +int +xstartdraw(void) { - drawregion(0, 0, term.col, term.row); - XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, 0, 0, win.w, - win.h, 0, 0); - XSetForeground(xw.dpy, dc.gc, - dc.col[IS_SET(MODE_REVERSE)? - defaultfg : defaultbg].pixel); + return IS_SET(MODE_VISIBLE); } void -drawregion(int x1, int y1, int x2, int y2) +xdrawline(Line line, int x1, int y1, int x2) { - int i, x, y, ox, numspecs; + int i, x, ox, numspecs; Glyph base, new; - XftGlyphFontSpec *specs; + XftGlyphFontSpec *specs = xw.specbuf; - if (!(IS_SET(MODE_VISIBLE))) - return; - - for (y = y1; y < y2; y++) { - if (!term.dirty[y]) + numspecs = xmakeglyphfontspecs(specs, &line[x1], x2 - x1, x1, y1); + i = ox = 0; + for (x = x1; x < x2 && i < numspecs; x++) { + new = line[x]; + if (new.mode == ATTR_WDUMMY) continue; - - term.dirty[y] = 0; - - specs = xw.specbuf; - numspecs = xmakeglyphfontspecs(specs, &term.line[y][x1], x2 - x1, x1, y); - - i = ox = 0; - for (x = x1; x < x2 && i < numspecs; x++) { - new = term.line[y][x]; - if (new.mode == ATTR_WDUMMY) - continue; - if (selected(x, y)) - new.mode ^= ATTR_REVERSE; - if (i > 0 && ATTRCMP(base, new)) { - xdrawglyphfontspecs(specs, base, i, ox, y); - specs += i; - numspecs -= i; - i = 0; - } - if (i == 0) { - ox = x; - base = new; - } - i++; + if (selected(x, y1)) + new.mode ^= ATTR_REVERSE; + if (i > 0 && ATTRCMP(base, new)) { + xdrawglyphfontspecs(specs, base, i, ox, y1); + specs += i; + numspecs -= i; + i = 0; + } + if (i == 0) { + ox = x; + base = new; } - if (i > 0) - xdrawglyphfontspecs(specs, base, i, ox, y); + i++; } - xdrawcursor(); + if (i > 0) + xdrawglyphfontspecs(specs, base, i, ox, y1); +} + +void +xfinishdraw(void) +{ + XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, 0, 0, win.w, + win.h, 0, 0); + XSetForeground(xw.dpy, dc.gc, + dc.col[IS_SET(MODE_REVERSE)? + defaultfg : defaultbg].pixel); } void -- cgit v1.2.3 From a5dc1b46976b2252f9d7bb68f126c4b0f351dd1a Mon Sep 17 00:00:00 2001 From: "Devin J. Pohly" Date: Sat, 24 Feb 2018 14:58:54 -0600 Subject: Pull term references out of xdrawcursor Gradually reducing x.c dependency on Term object. Old and new cursor glyph/position are passed to xdrawcursor. (There may be an opportunity to refactor further if we can unify "clear old cursor" and "draw new cursor" functionality.) Signed-off-by: Devin J. Pohly --- st.c | 15 ++++++++++++++- st.h | 4 +++- win.h | 2 +- x.c | 61 ++++++++++++++++++++++--------------------------------------- 4 files changed, 40 insertions(+), 42 deletions(-) (limited to 'win.h') diff --git a/st.c b/st.c index 504239e..4bf6378 100644 --- a/st.c +++ b/st.c @@ -2544,10 +2544,23 @@ drawregion(int x1, int y1, int x2, int y2) void draw(void) { + int cx = term.c.x; + if (!xstartdraw()) return; + + /* adjust cursor position */ + LIMIT(term.ocx, 0, term.col-1); + LIMIT(term.ocy, 0, term.row-1); + if (term.line[term.ocy][term.ocx].mode & ATTR_WDUMMY) + term.ocx--; + if (term.line[term.c.y][cx].mode & ATTR_WDUMMY) + cx--; + drawregion(0, 0, term.col, term.row); - xdrawcursor(); + xdrawcursor(cx, term.c.y, term.line[term.c.y][cx], + term.ocx, term.ocy, term.line[term.ocy][term.ocx]); + term.ocx = cx, term.ocy = term.c.y; xfinishdraw(); } diff --git a/st.h b/st.h index 7026de8..27c48cf 100644 --- a/st.h +++ b/st.h @@ -82,8 +82,10 @@ typedef struct { int col; /* nb col */ Line *line; /* screen */ Line *alt; /* alternate screen */ - int *dirty; /* dirtyness of lines */ + int *dirty; /* dirtyness of lines */ TCursor c; /* cursor */ + int ocx; /* old cursor col */ + int ocy; /* old cursor row */ int top; /* top scroll limit */ int bot; /* bottom scroll limit */ int mode; /* terminal mode flags */ diff --git a/win.h b/win.h index 6e662af..7a866fd 100644 --- a/win.h +++ b/win.h @@ -25,7 +25,7 @@ enum win_mode { void xbell(void); void xclipcopy(void); -void xdrawcursor(void); +void xdrawcursor(int, int, Glyph, int, int, Glyph); void xdrawline(Line, int, int, int); void xhints(void); void xfinishdraw(void); diff --git a/x.c b/x.c index 96944ee..d205ca7 100644 --- a/x.c +++ b/x.c @@ -1387,41 +1387,26 @@ xdrawglyph(Glyph g, int x, int y) } void -xdrawcursor(void) +xdrawcursor(int cx, int cy, Glyph g, int ox, int oy, Glyph og) { - static int oldx = 0, oldy = 0; - int curx; - Glyph g = {' ', ATTR_NULL, defaultbg, defaultcs}, og; Color drawcol; - LIMIT(oldx, 0, term.col-1); - LIMIT(oldy, 0, term.row-1); - - curx = term.c.x; - - /* adjust position if in dummy */ - if (term.line[oldy][oldx].mode & ATTR_WDUMMY) - oldx--; - if (term.line[term.c.y][curx].mode & ATTR_WDUMMY) - curx--; - /* remove the old cursor */ - og = term.line[oldy][oldx]; - if (selected(oldx, oldy)) + if (selected(ox, oy)) og.mode ^= ATTR_REVERSE; - xdrawglyph(og, oldx, oldy); - - g.u = term.line[term.c.y][term.c.x].u; - g.mode |= term.line[term.c.y][term.c.x].mode & - (ATTR_BOLD | ATTR_ITALIC | ATTR_UNDERLINE | ATTR_STRUCK); + xdrawglyph(og, ox, oy); /* * Select the right color for the right mode. */ + g.mode &= ATTR_BOLD|ATTR_ITALIC|ATTR_UNDERLINE|ATTR_STRUCK|ATTR_WIDE; + g.fg = defaultbg; + g.bg = defaultcs; + if (IS_SET(MODE_REVERSE)) { g.mode |= ATTR_REVERSE; g.bg = defaultfg; - if (selected(term.c.x, term.c.y)) { + if (selected(cx, cy)) { drawcol = dc.col[defaultcs]; g.fg = defaultrcs; } else { @@ -1429,7 +1414,7 @@ xdrawcursor(void) g.fg = defaultcs; } } else { - if (selected(term.c.x, term.c.y)) { + if (selected(cx, cy)) { drawcol = dc.col[defaultrcs]; g.fg = defaultfg; g.bg = defaultrcs; @@ -1449,44 +1434,42 @@ xdrawcursor(void) case 0: /* Blinking Block */ case 1: /* Blinking Block (Default) */ case 2: /* Steady Block */ - g.mode |= term.line[term.c.y][curx].mode & ATTR_WIDE; - xdrawglyph(g, term.c.x, term.c.y); + xdrawglyph(g, cx, cy); break; case 3: /* Blinking Underline */ case 4: /* Steady Underline */ XftDrawRect(xw.draw, &drawcol, - borderpx + curx * win.cw, - borderpx + (term.c.y + 1) * win.ch - \ + borderpx + cx * win.cw, + borderpx + (cy + 1) * win.ch - \ cursorthickness, win.cw, cursorthickness); break; case 5: /* Blinking bar */ case 6: /* Steady bar */ XftDrawRect(xw.draw, &drawcol, - borderpx + curx * win.cw, - borderpx + term.c.y * win.ch, + borderpx + cx * win.cw, + borderpx + cy * win.ch, cursorthickness, win.ch); break; } } else { XftDrawRect(xw.draw, &drawcol, - borderpx + curx * win.cw, - borderpx + term.c.y * win.ch, + borderpx + cx * win.cw, + borderpx + cy * win.ch, win.cw - 1, 1); XftDrawRect(xw.draw, &drawcol, - borderpx + curx * win.cw, - borderpx + term.c.y * win.ch, + borderpx + cx * win.cw, + borderpx + cy * win.ch, 1, win.ch - 1); XftDrawRect(xw.draw, &drawcol, - borderpx + (curx + 1) * win.cw - 1, - borderpx + term.c.y * win.ch, + borderpx + (cx + 1) * win.cw - 1, + borderpx + cy * win.ch, 1, win.ch - 1); XftDrawRect(xw.draw, &drawcol, - borderpx + curx * win.cw, - borderpx + (term.c.y + 1) * win.ch - 1, + borderpx + cx * win.cw, + borderpx + (cy + 1) * win.ch - 1, win.cw, 1); } - oldx = curx, oldy = term.c.y; } void -- cgit v1.2.3 From e0215d53770a9b6bc6e5d7b9a603ecd34dbd7100 Mon Sep 17 00:00:00 2001 From: "Devin J. Pohly" Date: Sat, 24 Feb 2018 16:32:20 -0600 Subject: Reduce visibility wherever possible When possible, declare functions/variables static and move struct definitions out of headers. In order to allow utf8decode to become internal, use codepoint for DECSCUSR extension directly. Signed-off-by: Devin J. Pohly --- st.c | 31 ++++++++++++++++++++++++++++++- st.h | 40 ---------------------------------------- win.h | 1 - x.c | 15 +++++++++++++-- 4 files changed, 43 insertions(+), 44 deletions(-) (limited to 'win.h') diff --git a/st.c b/st.c index ce32cc0..f46aab6 100644 --- a/st.c +++ b/st.c @@ -36,6 +36,7 @@ /* Arbitrary sizes */ #define UTF_INVALID 0xFFFD +#define UTF_SIZ 4 #define ESC_BUF_SIZ (128*UTF_SIZ) #define ESC_ARG_SIZ 16 #define STR_BUF_SIZ ESC_BUF_SIZ @@ -95,6 +96,31 @@ enum escape_state { ESC_DCS =128, }; +typedef struct { + Glyph attr; /* current char attributes */ + int x; + int y; + char state; +} TCursor; + +typedef struct { + int mode; + int type; + int snap; + /* + * Selection variables: + * nb – normalized coordinates of the beginning of the selection + * ne – normalized coordinates of the end of the selection + * ob – original coordinates of the beginning of the selection + * oe – original coordinates of the end of the selection + */ + struct { + int x, y; + } nb, ne, ob, oe; + + int alt; +} Selection; + /* Internal representation of the screen */ typedef struct { int row; /* nb row */ @@ -187,15 +213,18 @@ static void tstrsequence(uchar); static void drawregion(int, int, int, int); +static void selnormalize(void); static void selscroll(int, int); static void selsnap(int *, int *, int); +static size_t utf8decode(const char *, Rune *, size_t); static Rune utf8decodebyte(char, size_t *); static char utf8encodebyte(Rune, size_t); -static char *utf8strchr(char *s, Rune u); +static char *utf8strchr(char *, Rune); static size_t utf8validate(Rune *, size_t); static char *base64dec(const char *); +static char base64dec_getc(const char **); static ssize_t xwrite(int, const char *, size_t); diff --git a/st.h b/st.h index 0a7472b..1015fc6 100644 --- a/st.h +++ b/st.h @@ -1,8 +1,5 @@ /* See LICENSE for license details. */ -/* Arbitrary sizes */ -#define UTF_SIZ 4 - /* macros */ #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MAX(a, b) ((a) < (b) ? (b) : (a)) @@ -69,41 +66,6 @@ typedef struct { typedef Glyph *Line; -typedef struct { - Glyph attr; /* current char attributes */ - int x; - int y; - char state; -} TCursor; - -/* Purely graphic info */ -typedef struct { - int tw, th; /* tty width and height */ - int w, h; /* window width and height */ - int ch; /* char height */ - int cw; /* char width */ - int mode; /* window state/mode flags */ - int cursor; /* cursor style */ -} TermWindow; - -typedef struct { - int mode; - int type; - int snap; - /* - * Selection variables: - * nb – normalized coordinates of the beginning of the selection - * ne – normalized coordinates of the end of the selection - * ob – original coordinates of the beginning of the selection - * oe – original coordinates of the end of the selection - */ - struct { - int x, y; - } nb, ne, ob, oe; - - int alt; -} Selection; - typedef union { int i; uint ui; @@ -137,11 +99,9 @@ void selclear(void); void selinit(void); void selstart(int, int, int); void selextend(int, int, int, int); -void selnormalize(void); int selected(int, int); char *getsel(void); -size_t utf8decode(const char *, Rune *, size_t); size_t utf8encode(Rune, char *); void *xmalloc(size_t); diff --git a/win.h b/win.h index 7a866fd..31f327d 100644 --- a/win.h +++ b/win.h @@ -27,7 +27,6 @@ void xbell(void); void xclipcopy(void); void xdrawcursor(int, int, Glyph, int, int, Glyph); void xdrawline(Line, int, int, int); -void xhints(void); void xfinishdraw(void); void xloadcols(void); int xsetcolorname(int, const char *); diff --git a/x.c b/x.c index 970d6dd..f7b0528 100644 --- a/x.c +++ b/x.c @@ -75,6 +75,15 @@ typedef XftColor Color; typedef XftGlyphFontSpec GlyphFontSpec; /* Purely graphic info */ +typedef struct { + int tw, th; /* tty width and height */ + int w, h; /* window width and height */ + int ch; /* char height */ + int cw; /* char width */ + int mode; /* window state/mode flags */ + int cursor; /* cursor style */ +} TermWindow; + typedef struct { Display *dpy; Colormap cmap; @@ -133,6 +142,8 @@ static int xgeommasktogravity(int); static void xinit(int, int); static void cresize(int, int); static void xresize(int, int); +static void xhints(void); +static int xloadcolor(int, const char *, Color *); static int xloadfont(Font *, FcPattern *); static void xloadfonts(char *, double); static void xunloadfont(Font *); @@ -1430,8 +1441,8 @@ xdrawcursor(int cx, int cy, Glyph g, int ox, int oy, Glyph og) /* draw the new one */ if (IS_SET(MODE_FOCUSED)) { switch (win.cursor) { - case 7: /* st extension: snowman */ - utf8decode("☃", &g.u, UTF_SIZ); + case 7: /* st extension: snowman (U+2603) */ + g.u = 0x2603; case 0: /* Blinking Block */ case 1: /* Blinking Block (Default) */ case 2: /* Steady Block */ -- cgit v1.2.3 From e85b6b64660214121164ea97fb098eaa4935f7db Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Tue, 12 Feb 2019 18:41:41 +0100 Subject: better Input Method Editor (IME) support Features: - Allow input methods swap with hotkey (E.g. left ctrl + left shift). - Over-the-spot pre-editing style, pre-edit data placed over insertion point. - Restart IME without segmentation fault. TODO: - Automatically pickup IME if st started before IME --- st.c | 1 + win.h | 1 + x.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++----------------- 3 files changed, 54 insertions(+), 17 deletions(-) (limited to 'win.h') diff --git a/st.c b/st.c index b8e6077..cf8687e 100644 --- a/st.c +++ b/st.c @@ -2594,6 +2594,7 @@ draw(void) term.ocx, term.ocy, term.line[term.ocy][term.ocx]); term.ocx = cx, term.ocy = term.c.y; xfinishdraw(); + xximspot(term.ocx, term.ocy); } void diff --git a/win.h b/win.h index 31f327d..a6ef1b9 100644 --- a/win.h +++ b/win.h @@ -36,3 +36,4 @@ void xsetmode(int, unsigned int); void xsetpointermotion(int); void xsetsel(char *); int xstartdraw(void); +void xximspot(int, int); diff --git a/x.c b/x.c index 0422421..865dacc 100644 --- a/x.c +++ b/x.c @@ -139,6 +139,9 @@ static void xdrawglyphfontspecs(const XftGlyphFontSpec *, Glyph, int, int, int); static void xdrawglyph(Glyph, int, int); static void xclear(int, int, int, int); static int xgeommasktogravity(int); +static void ximopen(Display *); +static void ximinstantiate(Display *, XPointer, XPointer); +static void ximdestroy(XIM, XPointer, XPointer); static void xinit(int, int); static void cresize(int, int); static void xresize(int, int); @@ -996,6 +999,43 @@ xunloadfonts(void) xunloadfont(&dc.ibfont); } +void +ximopen(Display *dpy) +{ + XIMCallback destroy = { .client_data = NULL, .callback = ximdestroy }; + + if ((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) { + XSetLocaleModifiers("@im=local"); + if ((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) { + XSetLocaleModifiers("@im="); + if ((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) + die("XOpenIM failed. Could not open input device.\n"); + } + } + if (XSetIMValues(xw.xim, XNDestroyCallback, &destroy, NULL) != NULL) + die("XSetIMValues failed. Could not set input method value.\n"); + xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, + XNClientWindow, xw.win, XNFocusWindow, xw.win, NULL); + if (xw.xic == NULL) + die("XCreateIC failed. Could not obtain input method.\n"); +} + +void +ximinstantiate(Display *dpy, XPointer client, XPointer call) +{ + ximopen(dpy); + XUnregisterIMInstantiateCallback(xw.dpy, NULL, NULL, NULL, + ximinstantiate, NULL); +} + +void +ximdestroy(XIM xim, XPointer client, XPointer call) +{ + xw.xim = NULL; + XRegisterIMInstantiateCallback(xw.dpy, NULL, NULL, NULL, + ximinstantiate, NULL); +} + void xinit(int cols, int rows) { @@ -1033,7 +1073,7 @@ xinit(int cols, int rows) xw.attrs.background_pixel = dc.col[defaultbg].pixel; xw.attrs.border_pixel = dc.col[defaultbg].pixel; xw.attrs.bit_gravity = NorthWestGravity; - xw.attrs.event_mask = FocusChangeMask | KeyPressMask + xw.attrs.event_mask = FocusChangeMask | KeyPressMask | KeyReleaseMask | ExposureMask | VisibilityChangeMask | StructureNotifyMask | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask; xw.attrs.colormap = xw.cmap; @@ -1061,22 +1101,7 @@ xinit(int cols, int rows) xw.draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap); /* input methods */ - if ((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) { - XSetLocaleModifiers("@im=local"); - if ((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) { - XSetLocaleModifiers("@im="); - if ((xw.xim = XOpenIM(xw.dpy, - NULL, NULL, NULL)) == NULL) { - die("XOpenIM failed. Could not open input" - " device.\n"); - } - } - } - xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing - | XIMStatusNothing, XNClientWindow, xw.win, - XNFocusWindow, xw.win, NULL); - if (xw.xic == NULL) - die("XCreateIC failed. Could not obtain input method.\n"); + ximopen(xw.dpy); /* white cursor, black outline */ cursor = XCreateFontCursor(xw.dpy, mouseshape); @@ -1554,6 +1579,16 @@ xfinishdraw(void) defaultfg : defaultbg].pixel); } +void +xximspot(int x, int y) +{ + XPoint spot = { borderpx + x * win.cw, borderpx + (y + 1) * win.ch }; + XVaNestedList attr = XVaCreateNestedList(0, XNSpotLocation, &spot, NULL); + + XSetICValues(xw.xic, XNPreeditAttributes, attr, NULL); + XFree(attr); +} + void expose(XEvent *ev) { -- cgit v1.2.3 From 28b4c822c5c0acec300fdf15c6e3ede9f5e2335d Mon Sep 17 00:00:00 2001 From: John Collis Date: Sun, 6 Sep 2020 17:53:41 +1200 Subject: ST: Add WM_ICON_NAME property support Also added _NET_WM_ICON_NAME. --- st.c | 9 +++++++++ win.h | 1 + x.c | 16 +++++++++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) (limited to 'win.h') diff --git a/st.c b/st.c index 76b7e0d..ae7fa63 100644 --- a/st.c +++ b/st.c @@ -1844,6 +1844,7 @@ strhandle(void) { char *p = NULL, *dec; int j, narg, par; + static int winname = 0; term.esc &= ~(ESC_STR_END|ESC_STR); strparse(); @@ -1853,7 +1854,15 @@ strhandle(void) case ']': /* OSC -- Operating System Command */ switch (par) { case 0: + if (narg > 1) { + xsettitle(strescseq.args[1]); + xseticontitle(strescseq.args[1]); + } + return; case 1: + if (narg > 1) + xseticontitle(strescseq.args[1]); + return; case 2: if (narg > 1) xsettitle(strescseq.args[1]); diff --git a/win.h b/win.h index a6ef1b9..e6e4369 100644 --- a/win.h +++ b/win.h @@ -30,6 +30,7 @@ void xdrawline(Line, int, int, int); void xfinishdraw(void); void xloadcols(void); int xsetcolorname(int, const char *); +void xseticontitle(char *); void xsettitle(char *); int xsetcursor(int); void xsetmode(int, unsigned int); diff --git a/x.c b/x.c index 210f184..120e495 100644 --- a/x.c +++ b/x.c @@ -93,7 +93,7 @@ typedef struct { Window win; Drawable buf; GlyphFontSpec *specbuf; /* font spec buffer used for rendering */ - Atom xembed, wmdeletewin, netwmname, netwmpid; + Atom xembed, wmdeletewin, netwmname, netwmiconname, netwmpid; struct { XIM xim; XIC xic; @@ -1186,6 +1186,7 @@ xinit(int cols, int rows) xw.xembed = XInternAtom(xw.dpy, "_XEMBED", False); xw.wmdeletewin = XInternAtom(xw.dpy, "WM_DELETE_WINDOW", False); xw.netwmname = XInternAtom(xw.dpy, "_NET_WM_NAME", False); + xw.netwmiconname = XInternAtom(xw.dpy, "_NET_WM_ICON_NAME", False); XSetWMProtocols(xw.dpy, xw.win, &xw.wmdeletewin, 1); xw.netwmpid = XInternAtom(xw.dpy, "_NET_WM_PID", False); @@ -1579,6 +1580,19 @@ xsetenv(void) setenv("WINDOWID", buf, 1); } +void +xseticontitle(char *p) +{ + XTextProperty prop; + DEFAULT(p, opt_title); + + Xutf8TextListToTextProperty(xw.dpy, &p, 1, XUTF8StringStyle, + &prop); + XSetWMIconName(xw.dpy, xw.win, &prop); + XSetTextProperty(xw.dpy, xw.win, &prop, xw.netwmiconname); + XFree(prop.value); +} + void xsettitle(char *p) { -- cgit v1.2.3 From 8629d9a1da72cc18568a8f146307b0e939b77ebf Mon Sep 17 00:00:00 2001 From: NRK Date: Fri, 7 Jan 2022 23:21:04 +0600 Subject: code-golfing: cleanup osc color related code * adds missing function prototype * move xgetcolor() prototype to win.h (that's where all the other x.c func prototype seems to be declared at) * check for snprintf error/truncation * reduces code duplication for osc 10/11/12 * unify osc_color_response() and osc4_color_response() into a single function the latter two was suggested by Quentin Rameau in his patch review on the hackers list. --- st.c | 90 ++++++++++++++++++++++++------------------------------------------- st.h | 2 -- win.h | 1 + 3 files changed, 33 insertions(+), 60 deletions(-) (limited to 'win.h') diff --git a/st.c b/st.c index f43cfd3..6ba467d 100644 --- a/st.c +++ b/st.c @@ -161,6 +161,7 @@ static void csidump(void); static void csihandle(void); static void csiparse(void); static void csireset(void); +static void osc_color_response(int, int, int); static int eschandle(uchar); static void strdump(void); static void strhandle(void); @@ -1835,39 +1836,28 @@ csireset(void) } void -osc4_color_response(int num) +osc_color_response(int num, int index, int is_osc4) { int n; char buf[32]; unsigned char r, g, b; - if (xgetcolor(num, &r, &g, &b)) { - fprintf(stderr, "erresc: failed to fetch osc4 color %d\n", num); + if (xgetcolor(is_osc4 ? num : index, &r, &g, &b)) { + fprintf(stderr, "erresc: failed to fetch %s color %d\n", + is_osc4 ? "osc4" : "osc", + is_osc4 ? num : index); return; } - n = snprintf(buf, sizeof buf, "\033]4;%d;rgb:%02x%02x/%02x%02x/%02x%02x\007", - num, r, r, g, g, b, b); - - ttywrite(buf, n, 1); -} - -void -osc_color_response(int index, int num) -{ - int n; - char buf[32]; - unsigned char r, g, b; - - if (xgetcolor(index, &r, &g, &b)) { - fprintf(stderr, "erresc: failed to fetch osc color %d\n", index); - return; + n = snprintf(buf, sizeof buf, "\033]%s%d;rgb:%02x%02x/%02x%02x/%02x%02x\007", + is_osc4 ? "4;" : "", num, r, r, g, g, b, b); + if (n < 0 || n >= sizeof(buf)) { + fprintf(stderr, "error: %s while printing %s response\n", + n < 0 ? "snprintf failed" : "truncation occurred", + is_osc4 ? "osc4" : "osc"); + } else { + ttywrite(buf, n, 1); } - - n = snprintf(buf, sizeof buf, "\033]%d;rgb:%02x%02x/%02x%02x/%02x%02x\007", - num, r, r, g, g, b, b); - - ttywrite(buf, n, 1); } void @@ -1875,6 +1865,11 @@ strhandle(void) { char *p = NULL, *dec; int j, narg, par; + const struct { int idx; char *str; } osc_table[] = { + { defaultfg, "foreground" }, + { defaultbg, "background" }, + { defaultcs, "cursor" } + }; term.esc &= ~(ESC_STR_END|ESC_STR); strparse(); @@ -1909,43 +1904,22 @@ strhandle(void) } return; case 10: - if (narg < 2) - break; - - p = strescseq.args[1]; - - if (!strcmp(p, "?")) - osc_color_response(defaultfg, 10); - else if (xsetcolorname(defaultfg, p)) - fprintf(stderr, "erresc: invalid foreground color: %s\n", p); - else - tfulldirt(); - return; case 11: - if (narg < 2) - break; - - p = strescseq.args[1]; - - if (!strcmp(p, "?")) - osc_color_response(defaultbg, 11); - else if (xsetcolorname(defaultbg, p)) - fprintf(stderr, "erresc: invalid background color: %s\n", p); - else - tfulldirt(); - return; case 12: if (narg < 2) break; - p = strescseq.args[1]; - - if (!strcmp(p, "?")) - osc_color_response(defaultcs, 12); - else if (xsetcolorname(defaultcs, p)) - fprintf(stderr, "erresc: invalid cursor color: %s\n", p); - else + if ((j = par - 10) < 0 || j >= LEN(osc_table)) + break; /* shouldn't be possible */ + + if (!strcmp(p, "?")) { + osc_color_response(par, osc_table[j].idx, 0); + } else if (xsetcolorname(osc_table[j].idx, p)) { + fprintf(stderr, "erresc: invalid %s color: %s\n", + osc_table[j].str, p); + } else { tfulldirt(); + } return; case 4: /* color set */ if (narg < 3) @@ -1955,9 +1929,9 @@ strhandle(void) case 104: /* color reset */ j = (narg > 1) ? atoi(strescseq.args[1]) : -1; - if (p && !strcmp(p, "?")) - osc4_color_response(j); - else if (xsetcolorname(j, p)) { + if (p && !strcmp(p, "?")) { + osc_color_response(j, 0, 1); + } else if (xsetcolorname(j, p)) { if (par == 104 && narg <= 1) return; /* color reset without parameter */ fprintf(stderr, "erresc: invalid color j=%d, p=%s\n", diff --git a/st.h b/st.h index 519b9bd..fd3b0d8 100644 --- a/st.h +++ b/st.h @@ -111,8 +111,6 @@ void *xmalloc(size_t); void *xrealloc(void *, size_t); char *xstrdup(const char *); -int xgetcolor(int x, unsigned char *r, unsigned char *g, unsigned char *b); - /* config.h globals */ extern char *utmp; extern char *scroll; diff --git a/win.h b/win.h index e6e4369..6de960d 100644 --- a/win.h +++ b/win.h @@ -30,6 +30,7 @@ void xdrawline(Line, int, int, int); void xfinishdraw(void); void xloadcols(void); int xsetcolorname(int, const char *); +int xgetcolor(int, unsigned char *, unsigned char *, unsigned char *); void xseticontitle(char *); void xsettitle(char *); int xsetcursor(int); -- cgit v1.2.3