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