[hackers] [st][PATCH] Allow cursor blink to stop when typing

From: jvyden <jvyden_AT_volatile.bz>
Date: Fri, 15 May 2020 09:30:58 -0400

Heyo. I modified the blinking_cursor patch to stop when the user start typing, to avoid
distractions. This is also a pretty common feature in other terminal emulators with blinking
features, so I'd imagine users like me would want it.

Let me know if I did something majorly wrong with this approach.

---
 config.def.h | 22 +++++++++++++++++-----
 x.c          | 39 +++++++++++++++++++++++++++++++++++----
 2 files changed, 52 insertions(+), 9 deletions(-)
diff --git a/config.def.h b/config.def.h
index fdbacfd..39a619f 100644
--- a/config.def.h
+++ b/config.def.h
_AT_@ -63,6 +63,13 @@ static unsigned int blinktimeout = 800;
  */
 static unsigned int cursorthickness = 2;
 
+/*
+ * Controls whether cursor blinking resets when typing.
+ * 0: off
+ * 1: on
+*/
+static int typingResetsBlink = 1;
+
 /*
  * bell volume. It must be a value between -100 and 100. Use 0 for disabling
  * it
_AT_@ -129,13 +136,18 @@ static unsigned int defaultcs = 256;
 static unsigned int defaultrcs = 257;
 
 /*
- * Default shape of cursor
- * 2: Block ("█")
- * 4: Underline ("_")
- * 6: Bar ("|")
+ * https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h4-Functions-using-CSI-_-ordered-by-the-final-character-lparen-s-rparen:CSI-Ps-SP-q.1D81
+ * Default style of cursor
+ * 0: blinking block
+ * 1: blinking block (default)
+ * 2: steady block ("█")
+ * 3: blinking underline
+ * 4: steady underline ("_")
+ * 5: blinking bar
+ * 6: steady bar ("|")
  * 7: Snowman ("☃")
  */
-static unsigned int cursorshape = 2;
+static unsigned int cursorstyle = 1;
 
 /*
  * Default columns and rows numbers
diff --git a/x.c b/x.c
index 1dc44d6..c6503ee 100644
--- a/x.c
+++ b/x.c
_AT_@ -254,6 +254,8 @@ static char *opt_title = NULL;
 
 static int oldbutton = 3; /* button event on startup: 3 = release */
 
+static struct timespec lastblink;
+
 void
 clipcopy(const Arg *dummy)
 {
_AT_@ -1528,13 +1530,20 @@ xdrawcursor(int cx, int cy, Glyph g, int ox, int oy, Glyph og)
 		switch (win.cursor) {
 		case 7: /* st extension: snowman (U+2603) */
 			g.u = 0x2603;
-			/* FALLTHROUGH */
+			xdrawglyph(g, cx, cy);
+			break;
 		case 0: /* Blinking Block */
 		case 1: /* Blinking Block (Default) */
+			if (IS_SET(MODE_BLINK))
+				break;
+			/* FALLTHROUGH */
 		case 2: /* Steady Block */
 			xdrawglyph(g, cx, cy);
 			break;
 		case 3: /* Blinking Underline */
+			if (IS_SET(MODE_BLINK))
+				break;
+			/* FALLTHROUGH */
 		case 4: /* Steady Underline */
 			XftDrawRect(xw.draw, &drawcol,
 					borderpx + cx * win.cw,
_AT_@ -1543,6 +1552,9 @@ xdrawcursor(int cx, int cy, Glyph g, int ox, int oy, Glyph og)
 					win.cw, cursorthickness);
 			break;
 		case 5: /* Blinking bar */
+			if (IS_SET(MODE_BLINK))
+				break;
+			/* FALLTHROUGH */
 		case 6: /* Steady bar */
 			XftDrawRect(xw.draw, &drawcol,
 					borderpx + cx * win.cw,
_AT_@ -1830,6 +1842,23 @@ kpress(XEvent *ev)
 			len = 2;
 		}
 	}
+
+	if(typingResetsBlink) {
+		int blinkcursor = win.cursor == 0 || win.cursor == 1 ||
+						  win.cursor == 3 || win.cursor == 5;
+		if (blinktimeout && (blinkcursor || tattrset(ATTR_BLINK)))
+		{
+			struct timespec now;
+			clock_gettime(CLOCK_MONOTONIC, &now);
+			lastblink = now;
+			if (IS_SET(MODE_BLINK))
+			{
+				win.mode ^= MODE_BLINK;
+			}
+		}
+	}
+
+
 	ttywrite(buf, len, 1);
 }
 
_AT_@ -1869,8 +1898,10 @@ run(void)
 	int w = win.w, h = win.h;
 	fd_set rfd;
 	int xfd = XConnectionNumber(xw.dpy), ttyfd, xev, drawing;
-	struct timespec seltv, *tv, now, lastblink, trigger;
+	struct timespec seltv, *tv, now, trigger;
 	double timeout;
+	int blinkcursor = win.cursor == 0 || win.cursor == 1 ||
+	                  win.cursor == 3 || win.cursor == 5;
 
 	/* Waiting for window mapping */
 	do {
_AT_@ -1947,7 +1978,7 @@ run(void)
 
 		/* idle detected or maxlatency exhausted -> draw */
 		timeout = -1;
-		if (blinktimeout && tattrset(ATTR_BLINK)) {
+		if (blinktimeout && (blinkcursor || tattrset(ATTR_BLINK))) {
 			timeout = blinktimeout - TIMEDIFF(now, lastblink);
 			if (timeout <= 0) {
 				if (-timeout > blinktimeout) /* start visible */
_AT_@ -1983,7 +2014,7 @@ main(int argc, char *argv[])
 {
 	xw.l = xw.t = 0;
 	xw.isfixed = False;
-	win.cursor = cursorshape;
+	win.cursor = cursorstyle;
 
 	ARGBEGIN {
 	case 'a':
-- 
2.26.2
Received on Fri May 15 2020 - 15:30:58 CEST

This archive was generated by hypermail 2.3.0 : Fri May 15 2020 - 15:36:37 CEST