kernel32: No longer init the terminfo database nor application key mode when 0 or...
[wine] / dlls / kernel32 / term.c
1 /*
2  * Interface to terminfo and termcap libraries
3  *
4  * Copyright 2010 Eric Pouech
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdarg.h>
25 #ifdef HAVE_NCURSES_H
26 # include <ncurses.h>
27 #elif defined(HAVE_CURSES_H)
28 # include <curses.h>
29 #endif
30 /* avoid redefinition warnings */
31 #undef KEY_EVENT
32 #undef MOUSE_MOVED
33
34 #if defined(HAVE_CURSES_H) || defined(HAVE_NCURSES_H)
35 #include <term.h>
36 #endif
37
38 #include <windef.h>
39 #include <winbase.h>
40 #include <winnls.h>
41 #include <wincon.h>
42 #include "console_private.h"
43 #include "wine/library.h"
44 #include "wine/debug.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(console);
47
48 static const int vkkeyscan_table[256] =
49 {
50      0,0,0,0,0,0,0,0,8,9,0,0,0,13,0,0,0,0,0,19,145,556,0,0,0,0,0,27,0,0,0,
51      0,32,305,478,307,308,309,311,222,313,304,312,443,188,189,190,191,48,
52      49,50,51,52,53,54,55,56,57,442,186,444,187,446,447,306,321,322,323,
53      324,325,326,327,328,329,330,331,332,333,334,335,336,337,338,339,340,
54      341,342,343,344,345,346,219,220,221,310,445,192,65,66,67,68,69,70,71,
55      72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,475,476,477,
56      448,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
57      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
58      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
59      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,400,0,0,0,0,0,0
60 };
61
62 static const int mapvkey_0[256] =
63 {
64      0,0,0,0,0,0,0,0,14,15,0,0,0,28,0,0,42,29,56,69,58,0,0,0,0,0,0,1,0,0,
65      0,0,57,73,81,79,71,75,72,77,80,0,0,0,55,82,83,0,11,2,3,4,5,6,7,8,9,
66      10,0,0,0,0,0,0,0,30,48,46,32,18,33,34,35,23,36,37,38,50,49,24,25,16,
67      19,31,20,22,47,17,45,21,44,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,55,78,0,74,
68      0,53,59,60,61,62,63,64,65,66,67,68,87,88,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
69      0,0,0,0,0,0,69,70,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
70      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,39,13,51,12,52,53,41,0,0,0,0,0,0,0,0,0,
71      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,26,43,27,40,76,96,0,0,0,0,0,0,0,0,
72      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
73 };
74
75 static inline void init_complex_char(INPUT_RECORD* ir, BOOL down, WORD vk, WORD kc, DWORD cks)
76 {
77     ir->EventType                        = KEY_EVENT;
78     ir->Event.KeyEvent.bKeyDown          = down;
79     ir->Event.KeyEvent.wRepeatCount      = 1;
80     ir->Event.KeyEvent.wVirtualScanCode  = vk;
81     ir->Event.KeyEvent.wVirtualKeyCode   = kc;
82     ir->Event.KeyEvent.dwControlKeyState = cks;
83     ir->Event.KeyEvent.uChar.UnicodeChar = 0;
84 }
85
86 /******************************************************************
87  *              TERM_FillSimpleChar
88  *
89  */
90 unsigned TERM_FillSimpleChar(unsigned real_inchar, INPUT_RECORD* ir)
91 {
92     unsigned            vk;
93     unsigned            inchar;
94     char                ch;
95     unsigned            numEvent = 0;
96     DWORD               cks = 0;
97
98     switch (real_inchar)
99     {
100     case   9: inchar = real_inchar;
101         real_inchar = 27; /* so that we don't think key is ctrl- something */
102         break;
103     case  13:
104     case  10: inchar = '\r';
105         real_inchar = 27; /* Fixme: so that we don't think key is ctrl- something */
106         break;
107     case 127: inchar = '\b';
108         break;
109     default:
110         inchar = real_inchar;
111         break;
112     }
113     if ((inchar & ~0xFF) != 0) FIXME("What a char (%u)\n", inchar);
114     vk = vkkeyscan_table[inchar];
115     if (vk & 0x0100)
116         init_complex_char(&ir[numEvent++], 1, 0x2a, 0x10, SHIFT_PRESSED);
117     if ((vk & 0x0200) || (unsigned char)real_inchar <= 26)
118         init_complex_char(&ir[numEvent++], 1, 0x1d, 0x11, LEFT_CTRL_PRESSED);
119     if (vk & 0x0400)
120         init_complex_char(&ir[numEvent++], 1, 0x38, 0x12, LEFT_ALT_PRESSED);
121
122     ir[numEvent].EventType                        = KEY_EVENT;
123     ir[numEvent].Event.KeyEvent.bKeyDown          = 1;
124     ir[numEvent].Event.KeyEvent.wRepeatCount      = 1;
125     ir[numEvent].Event.KeyEvent.dwControlKeyState = cks;
126     if (vk & 0x0100)
127         ir[numEvent].Event.KeyEvent.dwControlKeyState |= SHIFT_PRESSED;
128     if ((vk & 0x0200) || (unsigned char)real_inchar <= 26)
129         ir[numEvent].Event.KeyEvent.dwControlKeyState |= LEFT_CTRL_PRESSED;
130     if (vk & 0x0400)
131         ir[numEvent].Event.KeyEvent.dwControlKeyState |= LEFT_ALT_PRESSED;
132     ir[numEvent].Event.KeyEvent.wVirtualKeyCode = vk;
133     ir[numEvent].Event.KeyEvent.wVirtualScanCode = mapvkey_0[vk & 0x00ff]; /* VirtualKeyCodes to ScanCode */
134
135     ch = inchar;
136     MultiByteToWideChar(CP_UNIXCP, 0, &ch, 1, &ir[numEvent].Event.KeyEvent.uChar.UnicodeChar, 1);
137     ir[numEvent + 1] = ir[numEvent];
138     ir[numEvent + 1].Event.KeyEvent.bKeyDown = 0;
139
140     numEvent += 2;
141
142     if (vk & 0x0400)
143         init_complex_char(&ir[numEvent++], 0, 0x38, 0x12, LEFT_ALT_PRESSED);
144     if ((vk & 0x0200) || (unsigned char)real_inchar <= 26)
145         init_complex_char(&ir[numEvent++], 0, 0x1d, 0x11, 0);
146     if (vk & 0x0100)
147         init_complex_char(&ir[numEvent++], 0, 0x2a, 0x10, 0);
148     return numEvent;
149 }
150
151 #if defined(SONAME_LIBCURSES) || defined(SONAME_LIBNCURSES)
152
153 #ifdef HAVE_NCURSES_H
154 # define CURSES_NAME "ncurses"
155 #else
156 # define CURSES_NAME "curses"
157 #endif
158
159 static void *nc_handle = NULL;
160
161 #define MAKE_FUNCPTR(f) static typeof(f) * p_##f;
162
163 MAKE_FUNCPTR(putp)
164 MAKE_FUNCPTR(setupterm)
165 MAKE_FUNCPTR(tigetstr)
166 MAKE_FUNCPTR(tparm)
167
168 #undef MAKE_FUNCPTR
169
170 /**********************************************************************/
171
172 static BOOL TERM_bind_libcurses(void)
173 {
174 #ifdef SONAME_LIBNCURSES
175     static const char ncname[] = SONAME_LIBNCURSES;
176 #else
177     static const char ncname[] = SONAME_LIBCURSES;
178 #endif
179
180     if (!(nc_handle = wine_dlopen(ncname, RTLD_NOW, NULL, 0)))
181     {
182         WINE_MESSAGE("Wine cannot find the " CURSES_NAME " library (%s).\n",
183                      ncname);
184         return FALSE;
185     }
186
187 #define LOAD_FUNCPTR(f)                                      \
188     if((p_##f = wine_dlsym(nc_handle, #f, NULL, 0)) == NULL) \
189     {                                                        \
190         WINE_WARN("Can't find symbol %s\n", #f);             \
191         goto sym_not_found;                                  \
192     }
193
194     LOAD_FUNCPTR(putp)
195     LOAD_FUNCPTR(setupterm)
196     LOAD_FUNCPTR(tigetstr)
197     LOAD_FUNCPTR(tparm)
198
199 #undef LOAD_FUNCPTR
200
201     return TRUE;
202
203 sym_not_found:
204     WINE_MESSAGE(
205       "Wine cannot find certain functions that it needs inside the "
206        CURSES_NAME "\nlibrary.  To enable Wine to use " CURSES_NAME
207       " please upgrade your " CURSES_NAME "\nlibraries\n");
208     wine_dlclose(nc_handle, NULL, 0);
209     nc_handle = NULL;
210     return FALSE;
211 }
212
213 #define putp      p_putp
214 #define setupterm p_setupterm
215 #define tigetstr  p_tigetstr
216 #define tparm     p_tparm
217
218 struct dbkey_descr
219 {
220     enum dbkey_kind {dbk_simple, dbk_complex} kind;
221     DWORD_PTR   p1;
222     DWORD_PTR   p2;
223     DWORD_PTR   p3;
224 };
225
226 struct dbkey_pair
227 {
228     const char*         string;
229     struct dbkey_descr  descr;
230 };
231
232 static struct dbkey_pair TERM_dbkey_init[] = {
233     {"kcud1", {dbk_complex, 0x50, 0x28, 0}},
234     {"kcuu1", {dbk_complex, 0x48, 0x26, 0}},
235     {"kcub1", {dbk_complex, 0x4b, 0x25, 0}},
236     {"kcuf1", {dbk_complex, 0x4d, 0x27, 0}},
237     {"khome", {dbk_complex, 0x47, 0x24, 0}},
238     {"kbs",   {dbk_simple,  0x7f, 0x00, 0}},
239     {"kf1",   {dbk_complex, 0x3b, 0x70, 0}},
240     {"kf2",   {dbk_complex, 0x3c, 0x71, 0}},
241     {"kf3",   {dbk_complex, 0x3d, 0x72, 0}},
242     {"kf4",   {dbk_complex, 0x3e, 0x73, 0}},
243     {"kf5",   {dbk_complex, 0x3f, 0x74, 0}},
244     {"kf6",   {dbk_complex, 0x40, 0x75, 0}},
245     {"kf7",   {dbk_complex, 0x41, 0x76, 0}},
246     {"kf8",   {dbk_complex, 0x42, 0x77, 0}},
247     {"kf9",   {dbk_complex, 0x43, 0x78, 0}},
248     {"kf10",  {dbk_complex, 0x44, 0x79, 0}},
249     {"kf11",  {dbk_complex, 0xd9, 0x7a, 0}},
250     {"kf12",  {dbk_complex, 0xda, 0x7b, 0}},
251     {"kdch1", {dbk_complex, 0x53, 0x2e, 0}},
252     {"kich1", {dbk_complex, 0x52, 0x2d, 0}},
253     {"knp",   {dbk_complex, 0x51, 0x22, 0}},
254     {"kpp",   {dbk_complex, 0x49, 0x21, 0}},
255     {"kcbt",  {dbk_simple,  0x09, 0x00, SHIFT_PRESSED}},
256
257     {"kend",  {dbk_complex, 0x4f, 0x23, 0}},
258     /* {"kmous", NULL, }, */
259     {"kDC",   {dbk_complex, 0x53, 0x2e, SHIFT_PRESSED}},
260     {"kEND",  {dbk_complex, 0x4f, 0x23, SHIFT_PRESSED}},
261     {"kHOM",  {dbk_complex, 0x47, 0x24, SHIFT_PRESSED}},
262     {"kIC",   {dbk_complex, 0x52, 0x2d, SHIFT_PRESSED}},
263     {"kLFT",  {dbk_complex, 0x4b, 0x25, SHIFT_PRESSED}},
264     {"kRIT",  {dbk_complex, 0x4d, 0x27, SHIFT_PRESSED}},
265
266     /* Still some keys to manage:
267        KEY_DL           KEY_IL          KEY_EIC         KEY_CLEAR               KEY_EOS
268        KEY_EOL          KEY_SF          KEY_SR          KEY_STAB                KEY_CTAB
269        KEY_CATAB        KEY_ENTER       KEY_SRESET      KEY_RESET               KEY_PRINT
270        KEY_LL           KEY_A1          KEY_A3          KEY_B2                  KEY_C1
271        KEY_C3           KEY_BEG         KEY_CANCEL      KEY_CLOSE               KEY_COMMAND
272        KEY_COPY         KEY_CREATE      KEY_EXIT        KEY_FIND                KEY_HELP
273        KEY_MARK         KEY_MESSAGE     KEY_MOVE        KEY_NEXT                KEY_OPEN
274        KEY_OPTIONS      KEY_PREVIOUS    KEY_REDO        KEY_REFERENCE           KEY_REFRESH
275        KEY_REPLACE      KEY_RESTART     KEY_RESUME      KEY_SAVE                KEY_SBEG
276        KEY_SCANCEL      KEY_SCOMMAND    KEY_SCOPY       KEY_SCREATE             KEY_RESIZE
277        KEY_SDL          KEY_SELECT      KEY_SEOL        KEY_SEXIT               KEY_SFIND
278        KEY_SHELP        KEY_SMESSAGE    KEY_SMOVE       KEY_SNEXT               KEY_SOPTIONS
279        KEY_SPREVIOUS    KEY_SPRINT      KEY_SREDO       KEY_SREPLACE            KEY_SRSUME
280        KEY_SSAVE        KEY_SSUSPEND    KEY_SUNDO       KEY_SUSPEND             KEY_UNDO
281     */
282 };
283
284 static struct dbkey_pair*       TERM_dbkey;
285 static unsigned                 TERM_dbkey_size;
286 static unsigned                 TERM_dbkey_index;
287
288 static BOOL TERM_AddKeyDescr(const char* string, struct dbkey_descr* descr)
289 {
290     if (!string) return FALSE;
291     if (!TERM_dbkey)
292     {
293         TERM_dbkey_size = 32;
294         TERM_dbkey = HeapAlloc(GetProcessHeap(), 0, TERM_dbkey_size * sizeof(struct dbkey_pair));
295         if (!TERM_dbkey) return FALSE;
296     }
297     if (TERM_dbkey_index == TERM_dbkey_size)
298     {
299         struct dbkey_pair*      new;
300
301         new = HeapReAlloc(GetProcessHeap(), 0, TERM_dbkey, (2 * TERM_dbkey_size) * sizeof(struct dbkey_pair));
302         if (!new) return FALSE;
303         TERM_dbkey = new;
304         TERM_dbkey_size *= 2;
305     }
306     TERM_dbkey[TERM_dbkey_index].string = string;
307     TERM_dbkey[TERM_dbkey_index].descr  = *descr;
308     TERM_dbkey_index++;
309     return TRUE;
310 }
311
312 static BOOL TERM_BuildKeyDB(void)
313 {
314     unsigned i;
315     for (i = 0; i < sizeof(TERM_dbkey_init) / sizeof(TERM_dbkey_init[0]); i++)
316     {
317         if (!TERM_AddKeyDescr(tigetstr(TERM_dbkey_init[i].string), &TERM_dbkey_init[i].descr))
318             return FALSE;
319     }
320     return TRUE;
321 }
322
323 static BOOL TERM_init_done /* = FALSE */;
324
325 BOOL TERM_Init(void)
326 {
327     /* if we're not attached to a tty, don't fire the curses support */
328     if (!isatty(0) || !isatty(1)) return FALSE;
329     if (!TERM_bind_libcurses()) return FALSE;
330     if (setupterm(NULL, 1 /* really ?? */, NULL) == -1) return FALSE;
331     TERM_init_done = TRUE;
332     TERM_BuildKeyDB();
333     /* set application key mode */
334     putp(tigetstr("smkx"));
335     return TRUE;
336 }
337
338 BOOL TERM_Exit(void)
339 {
340     if (TERM_init_done)
341     {
342         /* put back the cursor key mode */
343         putp(tigetstr("rmkx"));
344     }
345     return TRUE;
346 }
347
348 /* -1 not found, 0 cannot decide, > 0 found */
349 int TERM_FillInputRecord(const char* in, size_t len, INPUT_RECORD* ir)
350 {
351     unsigned            i;
352     struct dbkey_descr* found = NULL;
353
354     for (i = 0; i < TERM_dbkey_index; i++)
355     {
356         if (!memcmp(TERM_dbkey[i].string, in, len))
357         {
358             if (len < strlen(TERM_dbkey[i].string)) return 0;
359             if (found) return 0;
360             found = &TERM_dbkey[i].descr;
361         }
362     }
363     if (!found) return -1;
364     switch (found->kind)
365     {
366     case dbk_simple:
367         return TERM_FillSimpleChar(found->p1, ir);
368     case dbk_complex:
369         init_complex_char(&ir[0], 1, found->p1, found->p2, ENHANCED_KEY | found->p3);
370         init_complex_char(&ir[1], 0, found->p1, found->p2, ENHANCED_KEY | found->p3);
371         return 2;
372     }
373     return -1;
374 }
375
376 #else
377 BOOL     TERM_Init(void) {return FALSE;}
378 BOOL     TERM_Exit(void) {return FALSE;}
379 int      TERM_FillInputRecord(const char* in, size_t len, INPUT_RECORD* ir) {return -1;}
380 #endif