src/ui-event.c
void keypress_from_text(struct keypress *buf, size_t len, const char *str)
{
	size_t cur = 0;
	byte mods = 0;

	memset(buf, 0, len * sizeof *buf);

#define STORE(buffer, pos, mod, cod) \
	{ \
		int p = (pos); \
		keycode_t c = (cod); \
		byte m = (mod); \
\
		if ((m & KC_MOD_CONTROL) && ENCODE_KTRL(c)) { \
			m &= ~KC_MOD_CONTROL; \
			c = KTRL(c); \
		} \
\
		buffer[p].mods = m; \
		buffer[p].code = c; \
	}

	/* Analyze the "ascii" string */
	while (*str && cur < len) {
		buf[cur].type = EVT_KBRD;

		if (*str == '\\') {
			str++;
			if (*str == '\0') break;

			switch (*str) {
				/* Hex-mode */
				case 'x': {
					if (isxdigit((unsigned char)(*(str + 1))) &&
							isxdigit((unsigned char)(*(str + 2)))) {
						int v1 = dehex(*++str) * 16;
						int v2 = dehex(*++str);
						/* store a nice hex digit */
						STORE(buf, cur++, mods, v1 + v2);
					} else {
						/* invalids get ignored */
						STORE(buf, cur++, mods, '?');
					}
					break;
				}

				case 'a': STORE(buf, cur++, mods, '\a'); break;
				case '\\': STORE(buf, cur++, mods, '\\'); break;
				case '^': STORE(buf, cur++, mods, '^'); break;
				case '[': STORE(buf, cur++, mods, '['); break;
				default: STORE(buf, cur++, mods, *str); break;
			}

			mods = 0;

			/* Skip the final char */
			str++;
		} else if (*str == '[') {
			/* parse non-ascii keycodes */
			char *end;
			keycode_t kc;

			if (*str++ == 0) return;

			end = strchr(str, (unsigned char) ']');
			if (!end) return;

			kc = keycode_find_code(str, (size_t) (end - str));
			if (!kc) return;

			STORE(buf, cur++, mods, kc);
			mods = 0;
			str = end + 1;
		} else if (*str == '{') {
			/* Specify modifier for next character */
			str++;
			if (*str == '\0' || !strchr(str, (unsigned char) '}'))
				return;

			/* analyze modifier chars */
			while (*str != '}') {
				switch (*str) {
					case '^': mods |= KC_MOD_CONTROL; break;
					case 'S': mods |= KC_MOD_SHIFT; break;
					case 'A': mods |= KC_MOD_ALT; break;
					case 'M': mods |= KC_MOD_META; break;
					case 'K': mods |= KC_MOD_KEYPAD; break;
					default:
						return;
				}

				str++;
			}

			/* skip ending bracket */
			str++;
		} else if (*str == '^') {
			mods |= KC_MOD_CONTROL;
			str++;
		} else {
			/* everything else */
			STORE(buf, cur++, mods, *str++);
			mods = 0;
		}
	}

	/* Terminate */
	cur = MIN(cur, len);
	buf[cur].type = EVT_NONE;
}
/**
 * Convert an encoding of a set of keypresses into actual keypresses.
 */
Convert an encoding of a set of keypresses into actual keypresses.
Convert an encoding of a set of keypresses into actual keypresses .
void keypress_from_text(struct keypress *buf, size_t len, const char *str)
{
	size_t cur = 0;
	byte mods = 0;
	memset(buf, 0, len * sizeof *buf);
#define STORE(buffer, pos, mod, cod) \
	{ \
		int p = (pos); \
		keycode_t c = (cod); \
		byte m = (mod); \
\
		if ((m & KC_MOD_CONTROL) && ENCODE_KTRL(c)) { \
			m &= ~KC_MOD_CONTROL; \
			c = KTRL(c); \
		} \
\
		buffer[p].mods = m; \
		buffer[p].code = c; \
	}
	while (*str && cur < len) {
		buf[cur].type = EVT_KBRD;
		if (*str == '\\') {
			str++;
			if (*str == '\0') break;
			switch (*str) {
				case 'x': {
					if (isxdigit((unsigned char)(*(str + 1))) &&
							isxdigit((unsigned char)(*(str + 2)))) {
						int v1 = dehex(*++str) * 16;
						int v2 = dehex(*++str);
						STORE(buf, cur++, mods, v1 + v2);
					} else {
						STORE(buf, cur++, mods, '?');
					}
					break;
				}
				case 'a': STORE(buf, cur++, mods, '\a'); break;
				case '\\': STORE(buf, cur++, mods, '\\'); break;
				case '^': STORE(buf, cur++, mods, '^'); break;
				case '[': STORE(buf, cur++, mods, '['); break;
				default: STORE(buf, cur++, mods, *str); break;
			}
			mods = 0;
			str++;
		} else if (*str == '[') {
			char *end;
			keycode_t kc;
			if (*str++ == 0) return;
			end = strchr(str, (unsigned char) ']');
			if (!end) return;
			kc = keycode_find_code(str, (size_t) (end - str));
			if (!kc) return;
			STORE(buf, cur++, mods, kc);
			mods = 0;
			str = end + 1;
		} else if (*str == '{') {
			str++;
			if (*str == '\0' || !strchr(str, (unsigned char) '}'))
				return;
			while (*str != '}') {
				switch (*str) {
					case '^': mods |= KC_MOD_CONTROL; break;
					case 'S': mods |= KC_MOD_SHIFT; break;
					case 'A': mods |= KC_MOD_ALT; break;
					case 'M': mods |= KC_MOD_META; break;
					case 'K': mods |= KC_MOD_KEYPAD; break;
					default:
						return;
				}
				str++;
			}
			str++;
		} else if (*str == '^') {
			mods |= KC_MOD_CONTROL;
			str++;
		} else {
			STORE(buf, cur++, mods, *str++);
			mods = 0;
		}
	}
	cur = MIN(cur, len);
	buf[cur].type = EVT_NONE;
}
void keypress_from_text ( struct keypress * buf , size_t len , const char * str ) { size_t cur = 0 ; byte mods = 0 ; memset ( buf , 0 , len * sizeof * buf ) ; #define STORE ( buffer , pos , mod , cod )  \
	{ \
		int p = (pos); \
		keycode_t c = (cod); \
		byte m = (mod); \
\
		if ((m & KC_MOD_CONTROL) && ENCODE_KTRL(c)) { \
			m &= ~KC_MOD_CONTROL; \
			c = KTRL(c); \
		} \
\
		buffer[p].mods = m; \
		buffer[p].code = c; \
	} while ( * str && cur < len ) { buf [ cur ] . type = EVT_KBRD ; if ( * str == ' \\ ' ) { str ++ ; if ( * str == ' \0 ' ) break ; switch ( * str ) { case ' ' : { if ( isxdigit ( ( unsigned char ) ( * ( str + 1 ) ) ) && isxdigit ( ( unsigned char ) ( * ( str + 2 ) ) ) ) { int v1 = dehex ( * ++ str ) * 16 ; int v2 = dehex ( * ++ str ) ; STORE ( buf , cur ++ , mods , v1 + v2 ) ; } else { STORE ( buf , cur ++ , mods , ' ' ) ; } break ; } case ' ' : STORE ( buf , cur ++ , mods , ' \a ' ) ; break ; case ' \\ ' : STORE ( buf , cur ++ , mods , ' \\ ' ) ; break ; case ' ' : STORE ( buf , cur ++ , mods , ' ' ) ; break ; case ' ' : STORE ( buf , cur ++ , mods , ' ' ) ; break ; default : STORE ( buf , cur ++ , mods , * str ) ; break ; } mods = 0 ; str ++ ; } else if ( * str == ' ' ) { char * end ; keycode_t kc ; if ( * str ++ == 0 ) return ; end = strchr ( str , ( unsigned char ) ' ' ) ; if ( ! end ) return ; kc = keycode_find_code ( str , ( size_t ) ( end - str ) ) ; if ( ! kc ) return ; STORE ( buf , cur ++ , mods , kc ) ; mods = 0 ; str = end + 1 ; } else if ( * str == ' ' ) { str ++ ; if ( * str == ' \0 ' || ! strchr ( str , ( unsigned char ) ' ' ) ) return ; while ( * str != ' ' ) { switch ( * str ) { case ' ' : mods |= KC_MOD_CONTROL ; break ; case ' ' : mods |= KC_MOD_SHIFT ; break ; case ' ' : mods |= KC_MOD_ALT ; break ; case ' ' : mods |= KC_MOD_META ; break ; case ' ' : mods |= KC_MOD_KEYPAD ; break ; default : return ; } str ++ ; } str ++ ; } else if ( * str == ' ' ) { mods |= KC_MOD_CONTROL ; str ++ ; } else { STORE ( buf , cur ++ , mods , * str ++ ) ; mods = 0 ; } } cur = MIN ( cur , len ) ; buf [ cur ] . type = EVT_NONE ; }
Convert an encoding of a set of keypresses into actual keypresses.
Convert an encoding of a set of keypresses into actual keypresses .
/* Analyze the "ascii" string */ /* Hex-mode */ /* store a nice hex digit */ /* invalids get ignored */ /* Skip the final char */ /* parse non-ascii keycodes */ /* Specify modifier for next character */ /* analyze modifier chars */ /* skip ending bracket */ /* everything else */ /* Terminate */