1
0
Fork 0

Updates send_string functionality, adds terminal feature (#1657)

* implement basic terminal stuff

* modify send_string to read normal strings too

* add files bc yeah. working pgm detected

* pgm detection apparently not working

* adds send string keycodes, additional keycode support in send string

* implement arguments

* [terminal] add help command

* [terminal] adds keycode and keymap functions

* [terminal] adds nop.h, documentation

* update macro docs
This commit is contained in:
Jack Humbert 2017-09-12 00:43:10 -04:00 committed by GitHub
parent a4ff8b91f7
commit 7ad924bae5
14 changed files with 749 additions and 57 deletions

View file

@ -248,4 +248,7 @@
Q__NOTE(_GS5), \
HD_NOTE(_C6),
#define TERMINAL_SOUND \
E__NOTE(_C5 )
#endif

View file

@ -0,0 +1,252 @@
/* Copyright 2017 Jack Humbert
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "process_terminal.h"
#include <string.h>
#include "version.h"
#include <stdio.h>
#include <math.h>
bool terminal_enabled = false;
char buffer[80] = "";
char newline[2] = "\n";
char arguments[6][20];
__attribute__ ((weak))
const char terminal_prompt[8] = "> ";
#ifdef AUDIO_ENABLE
#ifndef TERMINAL_SONG
#define TERMINAL_SONG SONG(TERMINAL_SOUND)
#endif
float terminal_song[][2] = TERMINAL_SONG;
#define TERMINAL_BELL() PLAY_SONG(terminal_song)
#else
#define TERMINAL_BELL()
#endif
__attribute__ ((weak))
const char keycode_to_ascii_lut[58] = {
0, 0, 0, 0,
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 0, 0, 0, '\t',
' ', '-', '=', '[', ']', '\\', 0, ';', '\'', '`', ',', '.', '/'
};
__attribute__ ((weak))
const char shifted_keycode_to_ascii_lut[58] = {
0, 0, 0, 0,
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'!', '@', '#', '$', '%', '^', '&', '*', '(', ')', 0, 0, 0, '\t',
' ', '_', '+', '{', '}', '|', 0, ':', '\'', '~', '<', '>', '?'
};
struct stringcase {
char* string;
void (*func)(void);
} typedef stringcase;
void enable_terminal(void) {
terminal_enabled = true;
strcpy(buffer, "");
for (int i = 0; i < 6; i++)
strcpy(arguments[i], "");
// select all text to start over
// SEND_STRING(SS_LCTRL("a"));
send_string(terminal_prompt);
}
void disable_terminal(void) {
terminal_enabled = false;
}
void terminal_about(void) {
SEND_STRING("QMK Firmware\n");
SEND_STRING(" v");
SEND_STRING(QMK_VERSION);
SEND_STRING("\n"SS_TAP(X_HOME)" Built: ");
SEND_STRING(QMK_BUILDDATE);
send_string(newline);
#ifdef TERMINAL_HELP
if (strlen(arguments[1]) != 0) {
SEND_STRING("You entered: ");
send_string(arguments[1]);
send_string(newline);
}
#endif
}
void terminal_help(void);
extern const uint16_t keymaps[][MATRIX_ROWS][MATRIX_COLS];
void terminal_keycode(void) {
if (strlen(arguments[1]) != 0 && strlen(arguments[2]) != 0 && strlen(arguments[3]) != 0) {
char keycode_dec[5];
char keycode_hex[5];
uint16_t layer = strtol(arguments[1], (char **)NULL, 10);
uint16_t row = strtol(arguments[2], (char **)NULL, 10);
uint16_t col = strtol(arguments[3], (char **)NULL, 10);
uint16_t keycode = pgm_read_word(&keymaps[layer][row][col]);
itoa(keycode, keycode_dec, 10);
itoa(keycode, keycode_hex, 16);
SEND_STRING("0x");
send_string(keycode_hex);
SEND_STRING(" (");
send_string(keycode_dec);
SEND_STRING(")\n");
} else {
#ifdef TERMINAL_HELP
SEND_STRING("usage: keycode <layer> <row> <col>\n");
#endif
}
}
void terminal_keymap(void) {
if (strlen(arguments[1]) != 0) {
uint16_t layer = strtol(arguments[1], (char **)NULL, 10);
for (int r = 0; r < MATRIX_ROWS; r++) {
for (int c = 0; c < MATRIX_COLS; c++) {
uint16_t keycode = pgm_read_word(&keymaps[layer][r][c]);
char keycode_s[8];
sprintf(keycode_s, "0x%04x, ", keycode);
send_string(keycode_s);
}
send_string(newline);
}
} else {
#ifdef TERMINAL_HELP
SEND_STRING("usage: keymap <layer>\n");
#endif
}
}
stringcase terminal_cases[] = {
{ "about", terminal_about },
{ "help", terminal_help },
{ "keycode", terminal_keycode },
{ "keymap", terminal_keymap },
{ "exit", disable_terminal }
};
void terminal_help(void) {
SEND_STRING("commands available:\n ");
for( stringcase* case_p = terminal_cases; case_p != terminal_cases + sizeof( terminal_cases ) / sizeof( terminal_cases[0] ); case_p++ ) {
send_string(case_p->string);
SEND_STRING(" ");
}
send_string(newline);
}
void command_not_found(void) {
SEND_STRING("command \"");
send_string(buffer);
SEND_STRING("\" not found\n");
}
void process_terminal_command(void) {
// we capture return bc of the order of events, so we need to manually send a newline
send_string(newline);
char * pch;
uint8_t i = 0;
pch = strtok(buffer, " ");
while (pch != NULL) {
strcpy(arguments[i], pch);
pch = strtok(NULL, " ");
i++;
}
bool command_found = false;
for( stringcase* case_p = terminal_cases; case_p != terminal_cases + sizeof( terminal_cases ) / sizeof( terminal_cases[0] ); case_p++ ) {
if( 0 == strcmp( case_p->string, buffer ) ) {
command_found = true;
(*case_p->func)();
break;
}
}
if (!command_found)
command_not_found();
if (terminal_enabled) {
strcpy(buffer, "");
for (int i = 0; i < 6; i++)
strcpy(arguments[i], "");
SEND_STRING(SS_TAP(X_HOME));
send_string(terminal_prompt);
}
}
bool process_terminal(uint16_t keycode, keyrecord_t *record) {
if (keycode == TERM_ON && record->event.pressed) {
enable_terminal();
return false;
}
if (terminal_enabled && record->event.pressed) {
if (keycode == TERM_OFF && record->event.pressed) {
disable_terminal();
return false;
}
if (keycode < 256) {
uint8_t str_len;
char char_to_add;
switch (keycode) {
case KC_ENTER:
process_terminal_command();
return false; break;
case KC_ESC:
SEND_STRING("\n");
enable_terminal();
return false; break;
case KC_BSPC:
str_len = strlen(buffer);
if (str_len > 0) {
buffer[str_len-1] = 0;
return true;
} else {
TERMINAL_BELL();
return false;
} break;
case KC_LEFT:
case KC_RIGHT:
case KC_UP:
case KC_DOWN:
return false; break;
default:
if (keycode <= 58) {
char_to_add = 0;
if (get_mods() & (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT))) {
char_to_add = shifted_keycode_to_ascii_lut[keycode];
} else if (get_mods() == 0) {
char_to_add = keycode_to_ascii_lut[keycode];
}
if (char_to_add != 0) {
strncat(buffer, &char_to_add, 1);
}
} break;
}
}
}
return true;
}

View file

@ -0,0 +1,27 @@
/* Copyright 2017 Jack Humbert
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PROCESS_TERMINAL_H
#define PROCESS_TERMINAL_H
#include "quantum.h"
extern const char keycode_to_ascii_lut[58];
extern const char shifted_keycode_to_ascii_lut[58];
extern const char terminal_prompt[8];
bool process_terminal(uint16_t keycode, keyrecord_t *record);
#endif

View file

@ -0,0 +1,25 @@
/* Copyright 2017 Jack Humbert
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PROCESS_TERMINAL_H
#define PROCESS_TERMINAL_H
#include "quantum.h"
#define TERM_ON KC_NO
#define TERM_OFF KC_NO
#endif

View file

@ -237,6 +237,9 @@ bool process_record_quantum(keyrecord_t *record) {
#endif
#ifdef UNICODEMAP_ENABLE
process_unicode_map(keycode, record) &&
#endif
#ifdef TERMINAL_ENABLE
process_terminal(keycode, record) &&
#endif
true)) {
return false;
@ -600,21 +603,29 @@ void send_string(const char *str) {
send_string_with_delay(str, 0);
}
void send_string_P(const char *str) {
send_string_with_delay_P(str, 0);
}
void send_string_with_delay(const char *str, uint8_t interval) {
while (1) {
uint8_t keycode;
uint8_t ascii_code = pgm_read_byte(str);
char ascii_code = *str;
if (!ascii_code) break;
keycode = pgm_read_byte(&ascii_to_keycode_lut[ascii_code]);
if (pgm_read_byte(&ascii_to_shift_lut[ascii_code])) {
register_code(KC_LSFT);
register_code(keycode);
unregister_code(keycode);
unregister_code(KC_LSFT);
}
else {
register_code(keycode);
unregister_code(keycode);
if (ascii_code == 1) {
// tap
uint8_t keycode = *(++str);
register_code(keycode);
unregister_code(keycode);
} else if (ascii_code == 2) {
// down
uint8_t keycode = *(++str);
register_code(keycode);
} else if (ascii_code == 3) {
// up
uint8_t keycode = *(++str);
unregister_code(keycode);
} else {
send_char(ascii_code);
}
++str;
// interval
@ -622,6 +633,46 @@ void send_string_with_delay(const char *str, uint8_t interval) {
}
}
void send_string_with_delay_P(const char *str, uint8_t interval) {
while (1) {
char ascii_code = pgm_read_byte(str);
if (!ascii_code) break;
if (ascii_code == 1) {
// tap
uint8_t keycode = pgm_read_byte(++str);
register_code(keycode);
unregister_code(keycode);
} else if (ascii_code == 2) {
// down
uint8_t keycode = pgm_read_byte(++str);
register_code(keycode);
} else if (ascii_code == 3) {
// up
uint8_t keycode = pgm_read_byte(++str);
unregister_code(keycode);
} else {
send_char(ascii_code);
}
++str;
// interval
{ uint8_t ms = interval; while (ms--) wait_ms(1); }
}
}
void send_char(char ascii_code) {
uint8_t keycode;
keycode = pgm_read_byte(&ascii_to_keycode_lut[(uint8_t)ascii_code]);
if (pgm_read_byte(&ascii_to_shift_lut[(uint8_t)ascii_code])) {
register_code(KC_LSFT);
register_code(keycode);
unregister_code(keycode);
unregister_code(KC_LSFT);
} else {
register_code(keycode);
unregister_code(keycode);
}
}
void set_single_persistent_default_layer(uint8_t default_layer) {
#if defined(AUDIO_ENABLE) && defined(DEFAULT_LAYER_SONGS)
PLAY_SONG(default_layer_songs[default_layer]);

View file

@ -40,7 +40,7 @@
#include "action_util.h"
#include <stdlib.h>
#include "print.h"
#include "send_string_keycodes.h"
extern uint32_t default_layer_state;
@ -103,11 +103,32 @@ extern uint32_t default_layer_state;
#include "process_key_lock.h"
#endif
#define SEND_STRING(str) send_string(PSTR(str))
#ifdef TERMINAL_ENABLE
#include "process_terminal.h"
#else
#include "process_terminal_nop.h"
#endif
#define STRINGIZE(z) #z
#define ADD_SLASH_X(y) STRINGIZE(\x ## y)
#define SYMBOL_STR(x) ADD_SLASH_X(x)
#define SS_TAP(keycode) "\1" SYMBOL_STR(keycode)
#define SS_DOWN(keycode) "\2" SYMBOL_STR(keycode)
#define SS_UP(keycode) "\3" SYMBOL_STR(keycode)
#define SS_LCTRL(string) SS_DOWN(X_LCTRL) string SS_UP(X_LCTRL)
#define SS_LGUI(string) SS_DOWN(X_LGUI) string SS_UP(X_LGUI)
#define SS_LALT(string) SS_DOWN(X_LALT) string SS_UP(X_LALT)
#define SEND_STRING(str) send_string_P(PSTR(str))
extern const bool ascii_to_shift_lut[0x80];
extern const uint8_t ascii_to_keycode_lut[0x80];
void send_string(const char *str);
void send_string_with_delay(const char *str, uint8_t interval);
void send_string_P(const char *str);
void send_string_with_delay_P(const char *str, uint8_t interval);
void send_char(char ascii_code);
// For tri-layer
void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3);

View file

@ -431,6 +431,11 @@ enum quantum_keycodes {
KC_LOCK,
#endif
#ifdef TERMINAL_ENABLE
TERM_ON,
TERM_OFF,
#endif
// always leave at the end
SAFE_RANGE
};

View file

@ -0,0 +1,168 @@
#ifndef SEND_STRING_KEYCODES
#define SEND_STRING_KEYCODES
#define X_NO 00
#define X_ROLL_OVER 01
#define X_POST_FAIL 02
#define X_UNDEFINED 03
#define X_A 04
#define X_B 05
#define X_C 06
#define X_D 07
#define X_E 08
#define X_F 09
#define X_G 0A
#define X_H 0B
#define X_I 0C
#define X_J 0D
#define X_K 0E
#define X_L 0F
#define X_M 10
#define X_N 11
#define X_O 12
#define X_P 13
#define X_Q 14
#define X_R 15
#define X_S 16
#define X_T 17
#define X_U 18
#define X_V 19
#define X_W 1A
#define X_X 1B
#define X_Y 1C
#define X_Z 1D
#define X_1 1E
#define X_2 1F
#define X_3 20
#define X_4 21
#define X_5 22
#define X_6 23
#define X_7 24
#define X_8 25
#define X_9 26
#define X_0 27
#define X_ENTER 28
#define X_ESCAPE 29
#define X_BSPACE 2A
#define X_TAB 2B
#define X_SPACE 2C
#define X_MINUS 2D
#define X_EQUAL 2E
#define X_LBRACKET 2F
#define X_RBRACKET 30
#define X_BSLASH 31
#define X_NONUS_HASH 32
#define X_SCOLON 33
#define X_QUOTE 34
#define X_GRAVE 35
#define X_COMMA 36
#define X_DOT 37
#define X_SLASH 38
#define X_CAPSLOCK 39
#define X_F1 3A
#define X_F2 3B
#define X_F3 3C
#define X_F4 3D
#define X_F5 3E
#define X_F6 3F
#define X_F7 40
#define X_F8 41
#define X_F9 42
#define X_F10 43
#define X_F11 44
#define X_F12 45
#define X_PSCREEN 46
#define X_SCROLLLOCK 47
#define X_PAUSE 48
#define X_INSERT 49
#define X_HOME 4A
#define X_PGUP 4B
#define X_DELETE 4C
#define X_END 4D
#define X_PGDOWN 4E
#define X_RIGHT 4F
#define X_LEFT 50
#define X_DOWN 51
#define X_UP 52
#define X_NUMLOCK 53
#define X_KP_SLASH 54
#define X_KP_ASTERISK 55
#define X_KP_MINUS 56
#define X_KP_PLUS 57
#define X_KP_ENTER 58
#define X_KP_1 59
#define X_KP_2 5A
#define X_KP_3 5B
#define X_KP_4 5C
#define X_KP_5 5D
#define X_KP_6 5E
#define X_KP_7 5F
#define X_KP_8 60
#define X_KP_9 61
#define X_KP_0 62
#define X_KP_DOT 63
#define X_NONUS_BSLASH 64
#define X_APPLICATION 65
#define X_POWER 66
#define X_KP_EQUAL 67
#define X_F13 68
#define X_F14 69
#define X_F15 6A
#define X_F16 6B
#define X_F17 6C
#define X_F18 6D
#define X_F19 6E
#define X_F20 6F
#define X_F21 70
#define X_F22 71
#define X_F23 72
#define X_F24 73
#define X_EXECUTE 74
#define X_HELP 75
#define X_MENU 76
#define X_SELECT 77
#define X_STOP 78
#define X_AGAIN 79
#define X_UNDO 7A
#define X_CUT 7B
#define X_COPY 7C
#define X_PASTE 7D
#define X_FIND 7E
#define X__MUTE 7F
#define X__VOLUP 80
#define X__VOLDOWN 81
#define X_LOCKING_CAPS 82
#define X_LOCKING_NUM 83
#define X_LOCKING_SCROLL 84
#define X_KP_COMMA 85
#define X_KP_EQUAL_AS400 86
#define X_INT1 87
#define X_INT2 88
#define X_INT3 89
#define X_INT4 8A
#define X_INT5 8B
#define X_INT6 8C
#define X_INT7 8D
#define X_INT8 8E
#define X_INT9 8F
#define X_LANG1 90
#define X_LANG2 91
#define X_LANG3 92
#define X_LANG4 93
#define X_LANG5 94
#define X_LANG6 95
#define X_LANG7 96
#define X_LANG8 97
#define X_LANG9 98
/* Modifiers */
#define X_LCTRL e0
#define X_LSHIFT e1
#define X_LALT e2
#define X_LGUI e3
#define X_RCTRL e4
#define X_RSHIFT e5
#define X_RALT e6
#define X_RGUI e7
#endif