[dev] [st] Keyboard patches

2012-11-13 Thread Roberto E. Vargas Caballero
Hello,

This is a new path series that fix some problems related to the
keyboard, and also add the key definitions that were missed in st.

Best regards.
From 22e55d15af79e23a6f78c685a2ed73c3f10db1e1 Mon Sep 17 00:00:00 2001
From: Roberto E. Vargas Caballero k...@shike2.com
Date: Thu, 8 Nov 2012 16:32:28 +0100
Subject: Add SRM sequence

This sequence enable/disable the local echo.
---
 st.c |   76 --
 1 file changed, 56 insertions(+), 20 deletions(-)

diff --git a/st.c b/st.c
index 8b18607..8bf9337 100644
--- a/st.c
+++ b/st.c
@@ -110,17 +110,18 @@ enum glyph_state {
 };
 
 enum term_mode {
-	MODE_WRAP	= 1,
+	MODE_WRAP	 = 1,
 	MODE_INSERT  = 2,
 	MODE_APPKEYPAD   = 4,
 	MODE_ALTSCREEN   = 8,
-	MODE_CRLF	= 16,
+	MODE_CRLF	 = 16,
 	MODE_MOUSEBTN= 32,
 	MODE_MOUSEMOTION = 64,
 	MODE_MOUSE   = 32|64,
 	MODE_REVERSE = 128,
 	MODE_KBDLOCK = 256,
-	MODE_HIDE  = 512
+	MODE_HIDE	 = 512,
+	MODE_ECHO	 = 1024
 };
 
 enum escape_state {
@@ -320,6 +321,7 @@ static void tswapscreen(void);
 static void tsetdirt(int, int);
 static void tsetmode(bool, bool, int *, int);
 static void tfulldirt(void);
+static void techo(char *, int);
 
 static void ttynew(void);
 static void ttyread(void);
@@ -1534,7 +1536,8 @@ tsetmode(bool priv, bool set, int *args, int narg) {
 			case 4:  /* IRM -- Insertion-replacement */
 MODBIT(term.mode, set, MODE_INSERT);
 break;
-			case 12: /* XXX: SRM -- Send/Receive */
+			case 12: /* SRM -- Send/Receive */
+MODBIT(term.mode, !set, MODE_ECHO);
 break;
 			case 20: /* LNM -- Linefeed/new line */
 MODBIT(term.mode, set, MODE_CRLF);
@@ -1849,6 +1852,28 @@ tputtab(bool forward) {
 }
 
 void
+techo(char *buf, int len) {
+	for(; len  0; buf++, len--) {
+		char c = *buf;
+
+		if(c == '\033') {		/* escape */
+			tputc(^, 1);
+			tputc([, 1);
+		} else if (c  '\x20') {	/* control code */
+			if(c != '\n'  c != '\r'  c != '\t') {
+c |= '\x40';
+tputc(^, 1);
+			}
+			tputc(c, 1);
+		} else {
+			break;
+		}
+	}
+	if (len)
+		tputc(buf, len);
+}
+
+void
 tputc(char *c, int len) {
 	uchar ascii = *c;
 	bool control = ascii  '\x20' || ascii == 0177;
@@ -2679,7 +2704,7 @@ void
 kpress(XEvent *ev) {
 	XKeyEvent *e = ev-xkey;
 	KeySym ksym;
-	char buf[32], *customkey;
+	char xstr[31], buf[32], *customkey, *cp = buf;
 	int len, meta, shift, i;
 	Status status;
 
@@ -2688,7 +2713,7 @@ kpress(XEvent *ev) {
 
 	meta = e-state  Mod1Mask;
 	shift = e-state  ShiftMask;
-	len = XmbLookupString(xw.xic, e, buf, sizeof(buf), ksym, status);
+	len = XmbLookupString(xw.xic, e, xstr, sizeof(xstr), ksym, status);
 
 	/* 1. shortcuts */
 	for(i = 0; i  LEN(shortcuts); i++) {
@@ -2702,7 +2727,8 @@ kpress(XEvent *ev) {
 
 	/* 2. custom keys from config.h */
 	if((customkey = kmap(ksym, e-state))) {
-		ttywrite(customkey, strlen(customkey));
+		len = strlen(customkey);
+		memcpy(buf, customkey, len);
 	/* 2. hardcoded (overrides X lookup) */
 	} else {
 		switch(ksym) {
@@ -2714,34 +2740,44 @@ kpress(XEvent *ev) {
 			sprintf(buf, \033%c%c,
 IS_SET(MODE_APPKEYPAD) ? 'O' : '[',
 (shift ? dacb:DACB)[ksym - XK_Left]);
-			ttywrite(buf, 3);
+			len = 3;
 			break;
 		case XK_Insert:
-			if(shift)
+			if(shift) {
 selpaste();
+return;
+			}
+			memcpy(buf, xstr, len);
 			break;
 		case XK_Return:
+			len = 0;
 			if(meta)
-ttywrite(\033, 1);
+*cp++ = '\033', len++;
 
-			if(IS_SET(MODE_CRLF)) {
-ttywrite(\r\n, 2);
-			} else {
-ttywrite(\r, 1);
-			}
+			*cp++ = '\r', len++;
+
+			if(IS_SET(MODE_CRLF))
+*cp = '\n', len++;
 			break;
 			/* 3. X lookup  */
 		default:
-			if(len  0) {
-if(meta  len == 1)
-	ttywrite(\033, 1);
-ttywrite(buf, len);
-			}
+			if(len == 0)
+return;
+
+			if (len == 1  meta)
+*cp++ = '\033';
+
+			memcpy(cp, xstr, len);
+			len = cp - buf + len;
 			break;
 		}
 	}
+	ttywrite(buf, len);
+	if(IS_SET(MODE_ECHO))
+		techo(buf, len);
 }
 
+
 void
 cmessage(XEvent *e) {
 	/* See xembed specs
-- 
1.7.10.4

From f19b777a627598c1e94512b8e9027793e820b75d Mon Sep 17 00:00:00 2001
From: Roberto E. Vargas Caballero k...@shike2.com
Date: Fri, 9 Nov 2012 11:21:59 +0100
Subject: Fix keypad mode and cursor mode

Keypad mode is used for detecting when keys in the auxiliary keypad are
pressed, while cursor mode is used for detecting when a cursor is pressed,
but they are different modes.

St was mixing both modes and DECPAM and DECPNM modified the cursor mode, and
this was incorrect.
---
 st.c|5 +++--
 st.info |4 ++--
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/st.c b/st.c
index 8bf9337..683a0e9 100644
--- a/st.c
+++ b/st.c
@@ -121,7 +121,8 @@ enum term_mode {
 	MODE_REVERSE = 128,
 	MODE_KBDLOCK = 256,
 	MODE_HIDE	 = 512,
-	MODE_ECHO	 = 1024
+	MODE_ECHO	 = 1024,
+	MODE_APPCURSOR	 = 2048
 };
 
 enum escape_state {
@@ -1471,7 +1472,7 @@ tsetmode(bool priv, bool set, int *args, int narg) {
 			switch(*args) {
 		

Re: [dev] [st] Keyboard patches

2012-11-13 Thread Brandon Invergo
   This is a new path series that fix some problems related to the
 keyboard, and also add the key definitions that were missed in st.


I'm glad that you're thorough and you've put a lot of work into making
st robust, but how do these patches help us in 2012-soon-to-be-2013?

-brandon



Re: [dev] [st] Keyboard patches

2012-11-13 Thread Kurt H Maier
On Tue, Nov 13, 2012 at 07:34:00PM +0100, Brandon Invergo wrote:
 
 I'm glad that you're thorough and you've put a lot of work into making
 st robust, but how do these patches help us in 2012-soon-to-be-2013?
 

What?



Re: [dev] [st] Keyboard patches

2012-11-13 Thread Christoph Lohmann
Greetings.

On Tue, 13 Nov 2012 20:07:05 +0100 Brandon Invergo bran...@invergo.net wrote:
  This is a new path series that fix some problems related to the
  keyboard, and also add the key definitions that were missed in st.
 
 
 I'm glad that you're thorough and you've put a lot of work into making
 st robust, but how do these patches help us in 2012-soon-to-be-2013?

The  patches  are removing the hardcoded values in st and make them more
configurable to how terminals work. In the  future  important  shortcuts
that  were  missed  but  some asshole is using them hardcoded in its so‐
called »ncurses application« will be easier to add. But yes, most of  it
is compatibility bloat.


Sincerely,

Christoph




Re: [dev] [st] Keyboard patches

2012-11-13 Thread Roberto E. Vargas Caballero
 The  patches  are removing the hardcoded values in st and make them more
 configurable to how terminals work. In the  future  important  shortcuts

Not only does this. There is some conditions that can not be handle with the
previous code. All the keypad keys can generate two values depend of the
keypad mode, one of them is the ansi sequence that the own terminal
understand for this function (KX_KP_Up generates \033[A, that is the
sequence for CUU), or one code that identify to the key (KX_KP_Up generates
\033Ox). This is done in this way because usual programs don't have to worry
about them, because they generate correct sequences and they don't need to
know if the sequence come from the key or the output of the program. But
when you are in raw mode you want to know what key was pressed, and the ansi
sequence does't give to you this information. you couldn't put any of these
values in the Key array because they depend of the keypad mode or cursor
mode, so the Key array was not useful.

 that  were  missed  but  some asshole is using them hardcoded in its so‐
 called »ncurses application« will be easier to add. But yes, most of  it
 is compatibility bloat.

I use a lot of these sequences. I know that it is not the typical usage of a
terminal emulator by a novice, but some of us have to use terminals for
other things. When you use telnet or ssh they disable the kernel echo in the
terminal and let to the other side the task of generating the
echo. Sometimes I have to connect to systems which don't generate the echo
(due to some reasons that are not important here) and It is very important
for me see what I am writing.

Best regards,