[7d4fbcd] | 1 | #include <ctype.h> |
---|
| 2 | #include <string.h> |
---|
| 3 | #include "owl.h" |
---|
| 4 | |
---|
[369a7f0] | 5 | static const struct _owl_keypress_specialmap { |
---|
[7d4fbcd] | 6 | int kj; |
---|
[e19eb97] | 7 | const char *ks; |
---|
[7d4fbcd] | 8 | } specialmap[] = { |
---|
[ac70242] | 9 | #ifdef KEY_CODE_YES |
---|
[7d4fbcd] | 10 | { KEY_CODE_YES, "CODE_YES" }, |
---|
[ac70242] | 11 | #endif |
---|
[7d4fbcd] | 12 | { KEY_MIN, "MIN" }, |
---|
| 13 | { KEY_BREAK, "BREAK" }, |
---|
| 14 | { KEY_DOWN, "DOWN" }, |
---|
| 15 | { KEY_UP, "UP" }, |
---|
| 16 | { KEY_LEFT, "LEFT" }, |
---|
| 17 | { KEY_RIGHT, "RIGHT" }, |
---|
| 18 | { KEY_HOME, "HOME" }, |
---|
| 19 | { KEY_BACKSPACE, "BACKSPACE" }, |
---|
| 20 | { KEY_F0, "F0" }, |
---|
| 21 | { KEY_F(1), "F1" }, |
---|
| 22 | { KEY_F(2), "F2" }, |
---|
| 23 | { KEY_F(3), "F3" }, |
---|
| 24 | { KEY_F(4), "F4" }, |
---|
| 25 | { KEY_F(5), "F5" }, |
---|
| 26 | { KEY_F(6), "F6" }, |
---|
| 27 | { KEY_F(7), "F7" }, |
---|
| 28 | { KEY_F(8), "F8" }, |
---|
| 29 | { KEY_F(9), "F9" }, |
---|
| 30 | { KEY_F(10), "F10" }, |
---|
| 31 | { KEY_F(11), "F11" }, |
---|
| 32 | { KEY_F(12), "F12" }, |
---|
| 33 | { KEY_DL, "DL" }, |
---|
| 34 | { KEY_IL, "IL" }, |
---|
| 35 | { KEY_DC, "DC" }, |
---|
| 36 | { KEY_IC, "IC" }, |
---|
| 37 | { KEY_EIC, "EIC" }, |
---|
| 38 | { KEY_CLEAR, "CLEAR" }, |
---|
| 39 | { KEY_EOS, "EOS" }, |
---|
| 40 | { KEY_EOL, "EOL" }, |
---|
| 41 | { KEY_SF, "SF" }, |
---|
| 42 | { KEY_SR, "SR" }, |
---|
| 43 | { KEY_NPAGE, "NPAGE" }, |
---|
| 44 | { KEY_PPAGE, "PPAGE" }, |
---|
| 45 | { KEY_STAB, "STAB" }, |
---|
| 46 | { KEY_CTAB, "CTAB" }, |
---|
| 47 | { KEY_CATAB, "CATAB" }, |
---|
| 48 | { KEY_ENTER, "ENTER" }, |
---|
| 49 | { KEY_SRESET, "SRESET" }, |
---|
| 50 | { KEY_RESET, "RESET" }, |
---|
| 51 | { KEY_PRINT, "PRINT" }, |
---|
| 52 | { KEY_LL, "LL" }, |
---|
| 53 | { KEY_A1, "A1" }, |
---|
| 54 | { KEY_A3, "A3" }, |
---|
| 55 | { KEY_B2, "B2" }, |
---|
| 56 | { KEY_C1, "C1" }, |
---|
| 57 | { KEY_C3, "C3" }, |
---|
| 58 | { KEY_BTAB, "BTAB" }, |
---|
| 59 | { KEY_BEG, "BEG" }, |
---|
| 60 | { KEY_CANCEL, "CANCEL" }, |
---|
| 61 | { KEY_CLOSE, "CLOSE" }, |
---|
| 62 | { KEY_COMMAND, "COMMAND" }, |
---|
| 63 | { KEY_COPY, "COPY" }, |
---|
| 64 | { KEY_CREATE, "CREATE" }, |
---|
| 65 | { KEY_END, "END" }, |
---|
| 66 | { KEY_EXIT, "EXIT" }, |
---|
| 67 | { KEY_FIND, "FIND" }, |
---|
| 68 | { KEY_HELP, "HELP" }, |
---|
| 69 | { KEY_MARK, "MARK" }, |
---|
| 70 | { KEY_MESSAGE, "MESSAGE" }, |
---|
| 71 | { KEY_MOVE, "MOVE" }, |
---|
| 72 | { KEY_NEXT, "NEXT" }, |
---|
| 73 | { KEY_OPEN, "OPEN" }, |
---|
| 74 | { KEY_OPTIONS, "OPTIONS" }, |
---|
| 75 | { KEY_PREVIOUS, "PREVIOUS" }, |
---|
| 76 | { KEY_REDO, "REDO" }, |
---|
| 77 | { KEY_REFERENCE, "REFERENCE" }, |
---|
| 78 | { KEY_REFRESH, "REFRESH" }, |
---|
| 79 | { KEY_REPLACE, "REPLACE" }, |
---|
| 80 | { KEY_RESTART, "RESTART" }, |
---|
| 81 | { KEY_RESUME, "RESUME" }, |
---|
| 82 | { KEY_SAVE, "SAVE" }, |
---|
| 83 | { KEY_SBEG, "SBEG" }, |
---|
| 84 | { KEY_SCANCEL, "SCANCEL" }, |
---|
| 85 | { KEY_SCOMMAND, "SCOMMAND" }, |
---|
| 86 | { KEY_SCOPY, "SCOPY" }, |
---|
| 87 | { KEY_SCREATE, "SCREATE" }, |
---|
| 88 | { KEY_SDC, "SDC" }, |
---|
| 89 | { KEY_SDL, "SDL" }, |
---|
| 90 | { KEY_SELECT, "SELECT" }, |
---|
| 91 | { KEY_SEND, "SEND" }, |
---|
| 92 | { KEY_SEOL, "SEOL" }, |
---|
| 93 | { KEY_SEXIT, "SEXIT" }, |
---|
| 94 | { KEY_SFIND, "SFIND" }, |
---|
| 95 | { KEY_SHELP, "SHELP" }, |
---|
| 96 | { KEY_SHOME, "SHOME" }, |
---|
| 97 | { KEY_SIC, "SIC" }, |
---|
| 98 | { KEY_SLEFT, "SLEFT" }, |
---|
| 99 | { KEY_SMESSAGE, "SMESSAGE" }, |
---|
| 100 | { KEY_SMOVE, "SMOVE" }, |
---|
| 101 | { KEY_SNEXT, "SNEXT" }, |
---|
| 102 | { KEY_SOPTIONS, "SOPTIONS" }, |
---|
| 103 | { KEY_SPREVIOUS, "SPREVIOUS" }, |
---|
| 104 | { KEY_SPRINT, "SPRINT" }, |
---|
| 105 | { KEY_SREDO, "SREDO" }, |
---|
| 106 | { KEY_SREPLACE, "SREPLACE" }, |
---|
| 107 | { KEY_SRIGHT, "SRIGHT" }, |
---|
| 108 | { KEY_SRSUME, "SRSUME" }, |
---|
| 109 | { KEY_SSAVE, "SSAVE" }, |
---|
| 110 | { KEY_SSUSPEND, "SSUSPEND" }, |
---|
| 111 | { KEY_SUNDO, "SUNDO" }, |
---|
| 112 | { KEY_SUSPEND, "SUSPEND" }, |
---|
| 113 | { KEY_UNDO, "UNDO" }, |
---|
| 114 | { KEY_MOUSE, "MOUSE" }, |
---|
[ac70242] | 115 | #ifdef KEY_RESIZE |
---|
[7d4fbcd] | 116 | { KEY_RESIZE, "RESIZE" }, |
---|
[ac70242] | 117 | #endif |
---|
[7d4fbcd] | 118 | { KEY_MAX, "MAX" }, |
---|
| 119 | { ' ', "SPACE" }, |
---|
| 120 | { 27, "ESCAPE" }, |
---|
| 121 | { 127, "DELETE" }, |
---|
| 122 | { '\r', "CR" }, |
---|
[6551eae] | 123 | { '\n', "LF" }, |
---|
| 124 | { '\t', "TAB" }, |
---|
[7d4fbcd] | 125 | { 0, NULL } |
---|
| 126 | }; |
---|
| 127 | |
---|
[f87c490] | 128 | #define OWL_CTRL(key) ((key)&037) |
---|
| 129 | /* OWL_META is definied in owl.h */ |
---|
| 130 | |
---|
[d427f08] | 131 | char G_GNUC_WARN_UNUSED_RESULT *owl_keypress_tostring(int j, int esc) |
---|
[cf83b7a] | 132 | { |
---|
[d07af84] | 133 | GString *kb; |
---|
[369a7f0] | 134 | const struct _owl_keypress_specialmap *sm; |
---|
[7d4fbcd] | 135 | |
---|
[d07af84] | 136 | kb = g_string_new(""); |
---|
[7d4fbcd] | 137 | for (sm = specialmap; sm->kj!=0; sm++) { |
---|
[f87c490] | 138 | if (j == OWL_META(sm->kj) || (esc && j == sm->kj)) { |
---|
[d07af84] | 139 | g_string_append(kb, "M-"); |
---|
| 140 | g_string_append(kb, sm->ks); |
---|
[7d4fbcd] | 141 | break; |
---|
| 142 | } else if (j == sm->kj) { |
---|
[d07af84] | 143 | g_string_append(kb, sm->ks); |
---|
[7d4fbcd] | 144 | break; |
---|
| 145 | } |
---|
| 146 | } |
---|
[d07af84] | 147 | if (!kb->str[0]) { |
---|
[428834d] | 148 | if (j & OWL_META(0)) { |
---|
[d07af84] | 149 | g_string_append(kb, "M-"); |
---|
[f87c490] | 150 | j &= ~OWL_META(0); |
---|
[7d4fbcd] | 151 | } |
---|
[f87c490] | 152 | if ((OWL_CTRL(j) == j)) { |
---|
[d07af84] | 153 | g_string_append(kb, "C-"); |
---|
[948b942] | 154 | j |= 0x40; |
---|
| 155 | if (isupper(j)) j = tolower(j); |
---|
[7d4fbcd] | 156 | |
---|
| 157 | } |
---|
| 158 | if (isascii(j)) { |
---|
[d07af84] | 159 | g_string_append_c(kb, j); |
---|
[7d4fbcd] | 160 | } |
---|
[428834d] | 161 | |
---|
[7d4fbcd] | 162 | } |
---|
[d07af84] | 163 | if (!kb->str[0]) { |
---|
[7d4fbcd] | 164 | /* not a valid key */ |
---|
[d07af84] | 165 | g_string_free(kb, true); |
---|
| 166 | return NULL; |
---|
[7d4fbcd] | 167 | } |
---|
[d07af84] | 168 | return g_string_free(kb, false); |
---|
[7d4fbcd] | 169 | } |
---|
| 170 | |
---|
| 171 | |
---|
| 172 | /* returns ERR on failure, else a keycode */ |
---|
[e19eb97] | 173 | int owl_keypress_fromstring(const char *kb) |
---|
[cf83b7a] | 174 | { |
---|
[369a7f0] | 175 | const struct _owl_keypress_specialmap *sm; |
---|
[7d4fbcd] | 176 | int ismeta=0, isctrl=0; |
---|
| 177 | int j = ERR; |
---|
| 178 | |
---|
| 179 | while (*kb && kb[1] == '-' && (kb[0] == 'C' || kb[0] == 'M')) { |
---|
| 180 | if ((kb[0] == 'C') && (kb[1] == '-')) { |
---|
| 181 | isctrl = 1; |
---|
| 182 | kb+=2; |
---|
| 183 | } |
---|
| 184 | if ((kb[0] == 'M') && (kb[1] == '-')) { |
---|
| 185 | ismeta = 1; |
---|
| 186 | kb+=2; |
---|
| 187 | } |
---|
| 188 | } |
---|
| 189 | if (isascii(kb[0]) && !kb[1]) { |
---|
| 190 | j = kb[0]; |
---|
| 191 | } else { |
---|
| 192 | for (sm = specialmap; sm->kj!=0; sm++) { |
---|
| 193 | if (!strcmp(sm->ks, kb)) { |
---|
| 194 | j = sm->kj; |
---|
| 195 | } |
---|
| 196 | } |
---|
| 197 | } |
---|
| 198 | if (j==ERR) return(ERR); |
---|
| 199 | if (isctrl) { |
---|
[f87c490] | 200 | j = OWL_CTRL(j); |
---|
[7d4fbcd] | 201 | } |
---|
| 202 | if (ismeta) { |
---|
[f87c490] | 203 | j = OWL_META(j); |
---|
[7d4fbcd] | 204 | } |
---|
| 205 | return(j); |
---|
| 206 | } |
---|
| 207 | |
---|