| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "console.h" |
| 4 |
#include "line-edit.h" |
| 5 |
#include "string-util.h" |
| 6 |
#include "util.h" |
| 7 |
|
| 8 |
static void cursor_left(size_t *cursor, size_t *first) { |
| 9 |
assert(cursor); |
| 10 |
assert(first); |
| 11 |
|
| 12 |
if ((*cursor) > 0) |
| 13 |
(*cursor)--; |
| 14 |
else if ((*first) > 0) |
| 15 |
(*first)--; |
| 16 |
} |
| 17 |
|
| 18 |
static void cursor_right(size_t *cursor, size_t *first, size_t x_max, size_t len) { |
| 19 |
assert(cursor); |
| 20 |
assert(first); |
| 21 |
|
| 22 |
if ((*cursor)+1 < x_max) |
| 23 |
(*cursor)++; |
| 24 |
else if ((*first) + (*cursor) < len) |
| 25 |
(*first)++; |
| 26 |
} |
| 27 |
|
| 28 |
bool line_edit(char16_t **line_in, size_t x_max, size_t y_pos) { |
| 29 |
_cleanup_free_ char16_t *line = NULL, *print = NULL; |
| 30 |
size_t size, len, first = 0, cursor = 0, clear = 0; |
| 31 |
|
| 32 |
/* Edit the line and return true if it should be executed, false if not. */ |
| 33 |
|
| 34 |
assert(line_in); |
| 35 |
|
| 36 |
len = strlen16(*line_in); |
| 37 |
size = len + 1024; |
| 38 |
line = xnew(char16_t, size); |
| 39 |
print = xnew(char16_t, x_max + 1); |
| 40 |
strcpy16(line, strempty(*line_in)); |
| 41 |
|
| 42 |
for (;;) { |
| 43 |
EFI_STATUS err; |
| 44 |
uint64_t key; |
| 45 |
size_t j, cursor_color = EFI_TEXT_ATTR_SWAP(COLOR_EDIT); |
| 46 |
|
| 47 |
j = MIN(len - first, x_max); |
| 48 |
memcpy(print, line + first, j * sizeof(char16_t)); |
| 49 |
while (clear > 0 && j < x_max) { |
| 50 |
clear--; |
| 51 |
print[j++] = ' '; |
| 52 |
} |
| 53 |
print[j] = '\0'; |
| 54 |
|
| 55 |
/* See comment at edit_line() call site for why we start at 1. */ |
| 56 |
print_at(1, y_pos, COLOR_EDIT, print); |
| 57 |
|
| 58 |
if (!print[cursor]) |
| 59 |
print[cursor] = ' '; |
| 60 |
print[cursor+1] = '\0'; |
| 61 |
do { |
| 62 |
print_at(cursor + 1, y_pos, cursor_color, print + cursor); |
| 63 |
cursor_color = EFI_TEXT_ATTR_SWAP(cursor_color); |
| 64 |
|
| 65 |
err = console_key_read(&key, 750 * 1000); |
| 66 |
if (!IN_SET(err, EFI_SUCCESS, EFI_TIMEOUT, EFI_NOT_READY)) |
| 67 |
return false; |
| 68 |
|
| 69 |
print_at(cursor + 1, y_pos, COLOR_EDIT, print + cursor); |
| 70 |
} while (err != EFI_SUCCESS); |
| 71 |
|
| 72 |
switch (key) { |
| 73 |
case KEYPRESS(0, SCAN_ESC, 0): |
| 74 |
case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'c'): |
| 75 |
case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'g'): |
| 76 |
case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('c')): |
| 77 |
case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('g')): |
| 78 |
return false; |
| 79 |
|
| 80 |
case KEYPRESS(0, SCAN_HOME, 0): |
| 81 |
case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'a'): |
| 82 |
case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('a')): |
| 83 |
/* beginning-of-line */ |
| 84 |
cursor = 0; |
| 85 |
first = 0; |
| 86 |
continue; |
| 87 |
|
| 88 |
case KEYPRESS(0, SCAN_END, 0): |
| 89 |
case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'e'): |
| 90 |
case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('e')): |
| 91 |
/* end-of-line */ |
| 92 |
cursor = len - first; |
| 93 |
if (cursor+1 >= x_max) { |
| 94 |
cursor = x_max-1; |
| 95 |
first = len - (x_max-1); |
| 96 |
} |
| 97 |
continue; |
| 98 |
|
| 99 |
case KEYPRESS(0, SCAN_DOWN, 0): |
| 100 |
case KEYPRESS(EFI_ALT_PRESSED, 0, 'f'): |
| 101 |
case KEYPRESS(EFI_CONTROL_PRESSED, SCAN_RIGHT, 0): |
| 102 |
/* forward-word */ |
| 103 |
while (line[first + cursor] == ' ') |
| 104 |
cursor_right(&cursor, &first, x_max, len); |
| 105 |
while (line[first + cursor] && line[first + cursor] != ' ') |
| 106 |
cursor_right(&cursor, &first, x_max, len); |
| 107 |
continue; |
| 108 |
|
| 109 |
case KEYPRESS(0, SCAN_UP, 0): |
| 110 |
case KEYPRESS(EFI_ALT_PRESSED, 0, 'b'): |
| 111 |
case KEYPRESS(EFI_CONTROL_PRESSED, SCAN_LEFT, 0): |
| 112 |
/* backward-word */ |
| 113 |
if ((first + cursor) > 0 && line[first + cursor-1] == ' ') { |
| 114 |
cursor_left(&cursor, &first); |
| 115 |
while ((first + cursor) > 0 && line[first + cursor] == ' ') |
| 116 |
cursor_left(&cursor, &first); |
| 117 |
} |
| 118 |
while ((first + cursor) > 0 && line[first + cursor-1] != ' ') |
| 119 |
cursor_left(&cursor, &first); |
| 120 |
continue; |
| 121 |
|
| 122 |
case KEYPRESS(0, SCAN_RIGHT, 0): |
| 123 |
case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'f'): |
| 124 |
case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('f')): |
| 125 |
/* forward-char */ |
| 126 |
if (first + cursor == len) |
| 127 |
continue; |
| 128 |
cursor_right(&cursor, &first, x_max, len); |
| 129 |
continue; |
| 130 |
|
| 131 |
case KEYPRESS(0, SCAN_LEFT, 0): |
| 132 |
case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'b'): |
| 133 |
case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('b')): |
| 134 |
/* backward-char */ |
| 135 |
cursor_left(&cursor, &first); |
| 136 |
continue; |
| 137 |
|
| 138 |
case KEYPRESS(EFI_CONTROL_PRESSED, SCAN_DELETE, 0): |
| 139 |
case KEYPRESS(EFI_ALT_PRESSED, 0, 'd'): |
| 140 |
/* kill-word */ |
| 141 |
clear = 0; |
| 142 |
|
| 143 |
size_t k; |
| 144 |
for (k = first + cursor; k < len && line[k] == ' '; k++) |
| 145 |
clear++; |
| 146 |
for (; k < len && line[k] != ' '; k++) |
| 147 |
clear++; |
| 148 |
|
| 149 |
for (size_t i = first + cursor; i + clear < len; i++) |
| 150 |
line[i] = line[i + clear]; |
| 151 |
len -= clear; |
| 152 |
line[len] = '\0'; |
| 153 |
continue; |
| 154 |
|
| 155 |
case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'w'): |
| 156 |
case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('w')): |
| 157 |
case KEYPRESS(EFI_ALT_PRESSED, 0, '\b'): |
| 158 |
/* backward-kill-word */ |
| 159 |
clear = 0; |
| 160 |
if ((first + cursor) > 0 && line[first + cursor-1] == ' ') { |
| 161 |
cursor_left(&cursor, &first); |
| 162 |
clear++; |
| 163 |
while ((first + cursor) > 0 && line[first + cursor] == ' ') { |
| 164 |
cursor_left(&cursor, &first); |
| 165 |
clear++; |
| 166 |
} |
| 167 |
} |
| 168 |
while ((first + cursor) > 0 && line[first + cursor-1] != ' ') { |
| 169 |
cursor_left(&cursor, &first); |
| 170 |
clear++; |
| 171 |
} |
| 172 |
|
| 173 |
for (size_t i = first + cursor; i + clear < len; i++) |
| 174 |
line[i] = line[i + clear]; |
| 175 |
len -= clear; |
| 176 |
line[len] = '\0'; |
| 177 |
continue; |
| 178 |
|
| 179 |
case KEYPRESS(0, SCAN_DELETE, 0): |
| 180 |
case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'd'): |
| 181 |
case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('d')): |
| 182 |
if (len == 0) |
| 183 |
continue; |
| 184 |
if (first + cursor == len) |
| 185 |
continue; |
| 186 |
for (size_t i = first + cursor; i < len; i++) |
| 187 |
line[i] = line[i+1]; |
| 188 |
clear = 1; |
| 189 |
len--; |
| 190 |
continue; |
| 191 |
|
| 192 |
case KEYPRESS(EFI_CONTROL_PRESSED, 0, 'k'): |
| 193 |
case KEYPRESS(EFI_CONTROL_PRESSED, 0, CHAR_CTRL('k')): |
| 194 |
/* kill-line */ |
| 195 |
line[first + cursor] = '\0'; |
| 196 |
clear = len - (first + cursor); |
| 197 |
len = first + cursor; |
| 198 |
continue; |
| 199 |
|
| 200 |
case KEYPRESS(0, 0, '\n'): |
| 201 |
case KEYPRESS(0, 0, '\r'): |
| 202 |
case KEYPRESS(0, SCAN_F3, 0): /* EZpad Mini 4s firmware sends malformed events */ |
| 203 |
case KEYPRESS(0, SCAN_F3, '\r'): /* Teclast X98+ II firmware sends malformed events */ |
| 204 |
if (!streq16(line, *line_in)) { |
| 205 |
free(*line_in); |
| 206 |
*line_in = TAKE_PTR(line); |
| 207 |
} |
| 208 |
return true; |
| 209 |
|
| 210 |
case KEYPRESS(0, 0, '\b'): |
| 211 |
if (len == 0) |
| 212 |
continue; |
| 213 |
if (first == 0 && cursor == 0) |
| 214 |
continue; |
| 215 |
for (size_t i = first + cursor-1; i < len; i++) |
| 216 |
line[i] = line[i+1]; |
| 217 |
clear = 1; |
| 218 |
len--; |
| 219 |
if (cursor > 0) |
| 220 |
cursor--; |
| 221 |
if (cursor > 0 || first == 0) |
| 222 |
continue; |
| 223 |
/* show full line if it fits */ |
| 224 |
if (len < x_max) { |
| 225 |
cursor = first; |
| 226 |
first = 0; |
| 227 |
continue; |
| 228 |
} |
| 229 |
/* jump left to see what we delete */ |
| 230 |
if (first > 10) { |
| 231 |
first -= 10; |
| 232 |
cursor = 10; |
| 233 |
} else { |
| 234 |
cursor = first; |
| 235 |
first = 0; |
| 236 |
} |
| 237 |
continue; |
| 238 |
|
| 239 |
case KEYPRESS(0, 0, ' ') ... KEYPRESS(0, 0, '~'): |
| 240 |
case KEYPRESS(0, 0, 0x80) ... KEYPRESS(0, 0, 0xffff): |
| 241 |
if (len+1 == size) |
| 242 |
continue; |
| 243 |
for (size_t i = len; i > first + cursor; i--) |
| 244 |
line[i] = line[i-1]; |
| 245 |
line[first + cursor] = KEYCHAR(key); |
| 246 |
len++; |
| 247 |
line[len] = '\0'; |
| 248 |
if (cursor+1 < x_max) |
| 249 |
cursor++; |
| 250 |
else if (first + cursor < len) |
| 251 |
first++; |
| 252 |
continue; |
| 253 |
} |
| 254 |
} |
| 255 |
} |