source: keypress.c @ 0f5b08e

release-1.10release-1.9
Last change on this file since 0f5b08e was f271129, checked in by Jason Gross <jgross@mit.edu>, 13 years ago
Fix up headers The additions to owl.h and some of the removals were done by Alejandro Sedeño <asedeno@mit.edu> in commit 77a0258b3919468fc9d7f7602588ac427ab36e6c. Notes: * I think owl.c lost the need for sys/time.h when we punted select() in favor of glib's main loop. * We don't actually need to include things like stdarg.h, stdio.h, glib/gstdio.h, glib-object.h. I think they get indirectly included via owl.h and/or glib.h. They're left in (or added in to) the files that use functions/types from them. * I'm not entirely sure what sys/socket.h is doing in message.c. It is there from the initial commit. I suspect it might have had something to do with the call to getnameinfo. message.c compiles without it, but http://pubs.opengroup.org/onlinepubs/009695399/functions/getnameinfo.html suggests that we're supposed to include it? *shrugs* I'm leaving it in, for now. (Rather, I'll leave one copy of the #include in.)
  • Property mode set to 100644
File size: 5.1 KB
Line 
1#include "owl.h"
2
3static const struct _owl_keypress_specialmap {
4  int   kj;
5  const char *ks;
6} specialmap[] = {
7#ifdef KEY_CODE_YES
8   { KEY_CODE_YES,      "CODE_YES" }, 
9#endif
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" }, 
113#ifdef KEY_RESIZE
114   { KEY_RESIZE,        "RESIZE" }, 
115#endif
116   { KEY_MAX,           "MAX" }, 
117   { ' ',               "SPACE" }, 
118   { 27,                "ESCAPE" }, 
119   { 127,               "DELETE" }, 
120   { '\r',              "CR" }, 
121   { '\n',              "LF" },
122   { '\t',              "TAB" }, 
123   { 0,                 NULL }
124};
125
126#define OWL_CTRL(key) ((key)&037)
127/* OWL_META is definied in owl.h */
128
129CALLER_OWN char *owl_keypress_tostring(int j, int esc)
130{
131  GString *kb;
132  const struct _owl_keypress_specialmap *sm;
133
134  kb = g_string_new("");
135  for (sm = specialmap; sm->kj!=0; sm++) {
136    if (j == OWL_META(sm->kj) || (esc && j == sm->kj)) {
137      g_string_append(kb, "M-");
138      g_string_append(kb, sm->ks);
139      break;
140    } else if (j == sm->kj) {
141      g_string_append(kb, sm->ks);
142      break;
143    }
144  }
145  if (!kb->str[0]) {
146    if (j & OWL_META(0)) {
147      g_string_append(kb, "M-");
148      j &= ~OWL_META(0);
149    }
150    if ((OWL_CTRL(j) == j)) {
151      g_string_append(kb, "C-");
152      j |= 0x40;
153      if (isupper(j)) j = tolower(j);
154
155    }
156    if (isascii(j)) {
157      g_string_append_c(kb, j);
158    }
159   
160  } 
161  if (!kb->str[0]) {
162    /* not a valid key */
163    g_string_free(kb, true);
164    return NULL;
165  }
166  return g_string_free(kb, false);
167}
168
169
170/* returns ERR on failure, else a keycode */
171int owl_keypress_fromstring(const char *kb)
172{
173  const struct _owl_keypress_specialmap *sm;
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) {
198    j = OWL_CTRL(j);
199  }
200  if (ismeta) {
201    j = OWL_META(j);
202  }
203  return(j);
204}
205
Note: See TracBrowser for help on using the repository browser.