comparison src/readline-2-event-hook.patch @ 3340:098ca5276ab8

Add readline patch for interrupt and CPU usage. * src/readline-2-event-hook.patch: New patch to allow interrupting readline and throttle the event-hook loop.
author Michael Goffioul <michael.goffioul@gmail.com>
date Fri, 29 Nov 2013 18:50:20 -0500
parents
children
comparison
equal deleted inserted replaced
3339:77edea1af4f1 3340:098ca5276ab8
1 diff -ur readline-6.2-orig/input.c readline-6.2/input.c
2 --- readline-6.2-orig/input.c 2013-11-24 19:19:30 -0500
3 +++ readline-6.2/input.c 2013-11-29 11:19:59 -0500
4 @@ -54,6 +54,11 @@
5 #include <stdio.h>
6 #include <errno.h>
7
8 +#if defined (__MINGW32__)
9 +# define WIN32_LEAN_AND_MEAN
10 +# include <windows.h>
11 +#endif
12 +
13 #if !defined (errno)
14 extern int errno;
15 #endif /* !errno */
16 @@ -96,6 +101,11 @@
17 static unsigned char ibuffer[512];
18 static int ibuffer_len = sizeof (ibuffer) - 1;
19
20 +#if defined (__MINGW32__)
21 +static int _win32_getch (void);
22 +static int _win32_kbhit (void);
23 +#endif
24 +
25 #define any_typein (push_index != pop_index)
26
27 int
28 @@ -219,8 +229,8 @@
29 #if defined (__MINGW32__)
30 /* Use getch/_kbhit to check for available console input, in the same way
31 that we read it normally. */
32 - chars_avail = isatty (tty) ? _kbhit () : 0;
33 - result = 0;
34 + chars_avail = isatty (tty) ? _win32_kbhit () : 0;
35 + result = 0;
36 #endif
37
38 /* If there's nothing available, don't waste time trying to read
39 @@ -457,6 +467,123 @@
40 return (c);
41 }
42
43 +#if defined (__MINGW32__)
44 +
45 +#define _WIN32_READ_NOCHAR (-2)
46 +
47 +static char _win32_buf[16] = {'0'};
48 +static int _win32_bufidx = 0;
49 +
50 +static int
51 +_win32_getch_internal (int block)
52 +{
53 + INPUT_RECORD rec;
54 + DWORD evRead, waitResult;
55 + HANDLE hInput = (HANDLE) _get_osfhandle (fileno (rl_instream));
56 +
57 + if (_win32_bufidx > 0)
58 + return _win32_buf[--_win32_bufidx];
59 +
60 + hInput = (HANDLE) _get_osfhandle (fileno (rl_instream));
61 +
62 + do
63 + {
64 + if (! block)
65 + {
66 + if (WaitForSingleObject(hInput, _keyboard_input_timeout/1000) != WAIT_OBJECT_0)
67 + return _WIN32_READ_NOCHAR;
68 + }
69 +
70 + if (!ReadConsoleInput(hInput, &rec, 1, &evRead) || evRead != 1)
71 + return EOF;
72 +
73 + switch (rec.EventType)
74 + {
75 + case KEY_EVENT:
76 + if ((rec.Event.KeyEvent.bKeyDown &&
77 + (rec.Event.KeyEvent.wVirtualKeyCode < VK_SHIFT ||
78 + rec.Event.KeyEvent.wVirtualKeyCode > VK_MENU)) ||
79 + (!rec.Event.KeyEvent.bKeyDown &&
80 + rec.Event.KeyEvent.wVirtualKeyCode == VK_MENU &&
81 + rec.Event.KeyEvent.uChar.AsciiChar))
82 + {
83 + if (rec.Event.KeyEvent.uChar.AsciiChar)
84 + {
85 + if (rec.Event.KeyEvent.uChar.AsciiChar < 0 ||
86 + (rec.Event.KeyEvent.uChar.AsciiChar < 32 &&
87 + !(rec.Event.KeyEvent.dwControlKeyState & (RIGHT_CTRL_PRESSED|LEFT_CTRL_PRESSED))))
88 + {
89 + char c = rec.Event.KeyEvent.uChar.AsciiChar;
90 + if (GetOEMCP () == GetConsoleCP ())
91 + OemToCharBuff (&c, &c, 1);
92 + return (int)(unsigned char)c;
93 + }
94 + else
95 + return (int)rec.Event.KeyEvent.uChar.UnicodeChar;
96 + }
97 + else
98 + switch (rec.Event.KeyEvent.wVirtualKeyCode)
99 + {
100 + case VK_UP:
101 + _win32_buf[_win32_bufidx++] = 'H';
102 + return 0340;
103 + case VK_DOWN:
104 + _win32_buf[_win32_bufidx++] = 'P';
105 + return 0340;
106 + case VK_RIGHT:
107 + _win32_buf[_win32_bufidx++] = 'M';
108 + return 0340;
109 + case VK_LEFT:
110 + _win32_buf[_win32_bufidx++] = 'K';
111 + return 0340;
112 + case VK_HOME:
113 + _win32_buf[_win32_bufidx++] = 'G';
114 + return 0340;
115 + case VK_END:
116 + _win32_buf[_win32_bufidx++] = 'O';
117 + return 0340;
118 + case VK_DELETE:
119 + _win32_buf[_win32_bufidx++] = 'S';
120 + return 0340;
121 + default:
122 + break;
123 + }
124 + }
125 + break;
126 +
127 + case WINDOW_BUFFER_SIZE_EVENT:
128 + rl_resize_terminal ();
129 + break;
130 +
131 + default:
132 + break;
133 + }
134 + }
135 + while (1);
136 +}
137 +
138 +static int
139 +_win32_kbhit (void)
140 +{
141 + int result;
142 +
143 + result = _win32_getch_internal (0);
144 + if (result == _WIN32_READ_NOCHAR
145 + || result == EOF)
146 + return 0;
147 + _win32_buf[_win32_bufidx++] = result;
148 +
149 + return _win32_bufidx;
150 +}
151 +
152 +static int
153 +_win32_getch (void)
154 +{
155 + return _win32_getch_internal (1);
156 +}
157 +
158 +#endif
159 +
160 int
161 rl_getc (stream)
162 FILE *stream;
163 @@ -471,9 +598,9 @@
164 #if defined (__MINGW32__)
165 if (isatty (fileno (stream)))
166 {
167 - int c = _getch ();
168 + int c = _win32_getch ();
169 if (c == 0xe0)
170 - rl_execute_next (_getch ());
171 + rl_execute_next (_win32_getch ());
172 return (c);
173 }
174 #endif