[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 | |
---|
[7d4fbcd] | 131 | /* returns 0 on success */ |
---|
[cf83b7a] | 132 | int owl_keypress_tostring(int j, int esc, char *buff, int bufflen) |
---|
| 133 | { |
---|
[7d4fbcd] | 134 | char kb[64], kb2[2]; |
---|
[369a7f0] | 135 | const struct _owl_keypress_specialmap *sm; |
---|
[7d4fbcd] | 136 | |
---|
| 137 | *kb = '\0'; |
---|
| 138 | for (sm = specialmap; sm->kj!=0; sm++) { |
---|
[f87c490] | 139 | if (j == OWL_META(sm->kj) || (esc && j == sm->kj)) { |
---|
[7d4fbcd] | 140 | strcat(kb, "M-"); |
---|
| 141 | strcat(kb, sm->ks); |
---|
| 142 | break; |
---|
| 143 | } else if (j == sm->kj) { |
---|
| 144 | strcat(kb, sm->ks); |
---|
| 145 | break; |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | if (!*kb) { |
---|
[428834d] | 149 | if (j & OWL_META(0)) { |
---|
[7d4fbcd] | 150 | strcat(kb, "M-"); |
---|
[f87c490] | 151 | j &= ~OWL_META(0); |
---|
[7d4fbcd] | 152 | } |
---|
[f87c490] | 153 | if ((OWL_CTRL(j) == j)) { |
---|
[7d4fbcd] | 154 | strcat(kb, "C-"); |
---|
[948b942] | 155 | j |= 0x40; |
---|
| 156 | if (isupper(j)) j = tolower(j); |
---|
[7d4fbcd] | 157 | |
---|
| 158 | } |
---|
| 159 | if (isascii(j)) { |
---|
| 160 | kb2[0] = j; |
---|
| 161 | kb2[1] = 0; |
---|
| 162 | strcat(kb, kb2); |
---|
| 163 | } |
---|
[428834d] | 164 | |
---|
[7d4fbcd] | 165 | } |
---|
| 166 | if (!*kb) { |
---|
| 167 | /* not a valid key */ |
---|
| 168 | strncpy(buff, "INVALID", bufflen); |
---|
| 169 | return(-1); |
---|
| 170 | } |
---|
| 171 | strncpy(buff, kb, bufflen); |
---|
| 172 | return(0); |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | |
---|
| 176 | /* returns ERR on failure, else a keycode */ |
---|
[e19eb97] | 177 | int owl_keypress_fromstring(const char *kb) |
---|
[cf83b7a] | 178 | { |
---|
[369a7f0] | 179 | const struct _owl_keypress_specialmap *sm; |
---|
[7d4fbcd] | 180 | int ismeta=0, isctrl=0; |
---|
| 181 | int j = ERR; |
---|
| 182 | |
---|
| 183 | while (*kb && kb[1] == '-' && (kb[0] == 'C' || kb[0] == 'M')) { |
---|
| 184 | if ((kb[0] == 'C') && (kb[1] == '-')) { |
---|
| 185 | isctrl = 1; |
---|
| 186 | kb+=2; |
---|
| 187 | } |
---|
| 188 | if ((kb[0] == 'M') && (kb[1] == '-')) { |
---|
| 189 | ismeta = 1; |
---|
| 190 | kb+=2; |
---|
| 191 | } |
---|
| 192 | } |
---|
| 193 | if (isascii(kb[0]) && !kb[1]) { |
---|
| 194 | j = kb[0]; |
---|
| 195 | } else { |
---|
| 196 | for (sm = specialmap; sm->kj!=0; sm++) { |
---|
| 197 | if (!strcmp(sm->ks, kb)) { |
---|
| 198 | j = sm->kj; |
---|
| 199 | } |
---|
| 200 | } |
---|
| 201 | } |
---|
| 202 | if (j==ERR) return(ERR); |
---|
| 203 | if (isctrl) { |
---|
[f87c490] | 204 | j = OWL_CTRL(j); |
---|
[7d4fbcd] | 205 | } |
---|
| 206 | if (ismeta) { |
---|
[f87c490] | 207 | j = OWL_META(j); |
---|
[7d4fbcd] | 208 | } |
---|
| 209 | return(j); |
---|
| 210 | } |
---|
| 211 | |
---|