blob: 93715d51a59b172e71f6576973049b7d5d6df86d [file] [log] [blame]
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Terminal window support, see ":help :terminal".
12 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020013 * There are three parts:
14 * 1. Generic code for all systems.
Bram Moolenaarb13501f2017-07-22 22:32:56 +020015 * Uses libvterm for the terminal emulator.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020016 * 2. The MS-Windows implementation.
Bram Moolenaarb13501f2017-07-22 22:32:56 +020017 * Uses winpty.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020018 * 3. The Unix-like implementation.
Bram Moolenaarb13501f2017-07-22 22:32:56 +020019 * Uses pseudo-tty's (pty's).
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020020 *
21 * For each terminal one VTerm is constructed. This uses libvterm. A copy of
22 * that library is in the libvterm directory.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020023 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020024 * When a terminal window is opened, a job is started that will be connected to
25 * the terminal emulator.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020026 *
27 * If the terminal window has keyboard focus, typed keys are converted to the
Bram Moolenaar8a773062017-07-24 22:29:21 +020028 * terminal encoding and writing to the job over a channel.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020029 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020030 * If the job produces output, it is written to the terminal emulator. The
31 * terminal emulator invokes callbacks when its screen content changes. The
32 * line range is stored in tl_dirty_row_start and tl_dirty_row_end. Once in a
33 * while, if the terminal window is visible, the screen contents is drawn.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020034 *
35 * TODO:
Bram Moolenaar1f2903c2017-07-23 19:51:01 +020036 * - do not store terminal buffer in viminfo
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020037 * - Add a scrollback buffer (contains lines to scroll off the top).
38 * Can use the buf_T lines, store attributes somewhere else?
39 * - When the job ends:
40 * - Write "-- JOB ENDED --" in the terminal.
41 * - Put the terminal contents in the scrollback buffer.
42 * - Free the terminal emulator.
43 * - Display the scrollback buffer (but with attributes).
44 * Make the buffer not modifiable, drop attributes when making changes.
Bram Moolenaar21554412017-07-24 21:44:43 +020045 * - Need an option or argument to drop the window+buffer right away, to be
46 * used for a shell or Vim.
47 * - add a character in :ls output
Bram Moolenaar938783d2017-07-16 20:13:26 +020048 * - when closing window and job has not ended, make terminal hidden?
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +020049 * - don't allow exiting Vim when a terminal is still running a job
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020050 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar938783d2017-07-16 20:13:26 +020051 * - command line completion for :terminal
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020052 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020053 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020054 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020055 * - implement "term" for job_start(): more job options when starting a
56 * terminal.
Bram Moolenaar96ca27a2017-07-17 23:20:24 +020057 * - implement term_list() list of buffers with a terminal
58 * - implement term_getsize(buf)
59 * - implement term_setsize(buf)
60 * - implement term_sendkeys(buf, keys) send keystrokes to a terminal
61 * - implement term_wait(buf) wait for screen to be updated
62 * - implement term_scrape(buf, row) inspect terminal screen
63 * - implement term_open(command, options) open terminal window
64 * - implement term_getjob(buf)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020065 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
66 * conversions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020067 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020068 */
69
70#include "vim.h"
71
72#ifdef FEAT_TERMINAL
73
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020074#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020075# define MIN(x,y) (x < y ? x : y)
76# define MAX(x,y) (x > y ? x : y)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020077#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020078
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020079#include "libvterm/include/vterm.h"
80
Bram Moolenaare4f25e42017-07-07 11:54:15 +020081/* typedef term_T in structs.h */
82struct terminal_S {
83 term_T *tl_next;
84
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020085#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020086 void *tl_winpty_config;
87 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020088#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020089 VTerm *tl_vterm;
Bram Moolenaare4f25e42017-07-07 11:54:15 +020090 job_T *tl_job;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +020091 buf_T *tl_buffer;
Bram Moolenaare4f25e42017-07-07 11:54:15 +020092
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020093 /* last known vterm size */
94 int tl_rows;
95 int tl_cols;
96 /* vterm size does not follow window size */
97 int tl_rows_fixed;
98 int tl_cols_fixed;
99
Bram Moolenaar21554412017-07-24 21:44:43 +0200100 char_u *tl_title; /* NULL or allocated */
101 char_u *tl_status_text; /* NULL or allocated */
102
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200103 /* Range of screen rows to update. Zero based. */
104 int tl_dirty_row_start; /* -1 if nothing dirty */
105 int tl_dirty_row_end; /* row below last one to update */
106
107 pos_T tl_cursor;
108};
109
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200110/*
111 * List of all active terminals.
112 */
113static term_T *first_term = NULL;
114
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200115
116#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
117#define KEY_BUF_LEN 200
118
119/*
120 * Functions with separate implementation for MS-Windows and Unix-like systems.
121 */
122static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200123static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200124static void term_free(term_T *term);
125
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200126/**************************************
127 * 1. Generic code for all systems.
128 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200129
130/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200131 * Determine the terminal size from 'termsize' and the current window.
132 * Assumes term->tl_rows and term->tl_cols are zero.
133 */
134 static void
135set_term_and_win_size(term_T *term)
136{
137 if (*curwin->w_p_tms != NUL)
138 {
139 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
140
141 term->tl_rows = atoi((char *)curwin->w_p_tms);
142 term->tl_cols = atoi((char *)p);
143 }
144 if (term->tl_rows == 0)
145 term->tl_rows = curwin->w_height;
146 else
147 {
148 win_setheight_win(term->tl_rows, curwin);
149 term->tl_rows_fixed = TRUE;
150 }
151 if (term->tl_cols == 0)
152 term->tl_cols = curwin->w_width;
153 else
154 {
155 win_setwidth_win(term->tl_cols, curwin);
156 term->tl_cols_fixed = TRUE;
157 }
158}
159
160/*
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200161 * ":terminal": open a terminal window and execute a job in it.
162 */
163 void
164ex_terminal(exarg_T *eap)
165{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200166 exarg_T split_ea;
167 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200168 term_T *term;
Bram Moolenaare173fd02017-07-22 19:03:32 +0200169 char_u *cmd = eap->arg;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200170
171 if (check_restricted() || check_secure())
172 return;
173
174 term = (term_T *)alloc_clear(sizeof(term_T));
175 if (term == NULL)
176 return;
177 term->tl_dirty_row_end = MAX_ROW;
178
179 /* Open a new window or tab. */
180 vim_memset(&split_ea, 0, sizeof(split_ea));
181 split_ea.cmdidx = CMD_new;
182 split_ea.cmd = (char_u *)"new";
183 split_ea.arg = (char_u *)"";
184 ex_splitview(&split_ea);
185 if (curwin == old_curwin)
186 {
187 /* split failed */
188 vim_free(term);
189 return;
190 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200191 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200192 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200193
194 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200195 term->tl_next = first_term;
196 first_term = term;
197
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200198 if (buflist_findname(cmd) == NULL)
199 curbuf->b_ffname = vim_strsave(cmd);
200 else
201 {
202 int i;
203 size_t len = STRLEN(cmd) + 10;
204 char_u *p = alloc(len);
205
206 for (i = 1; p != NULL; ++i)
207 {
208 vim_snprintf((char *)p, len, "%s (%d)", cmd, i);
209 if (buflist_findname(p) == NULL)
210 {
211 curbuf->b_ffname = p;
212 break;
213 }
214 }
215 }
216 curbuf->b_fname = curbuf->b_ffname;
217
218 /* Mark the buffer as changed, so that it's not easy to abandon the job. */
219 curbuf->b_changed = TRUE;
220 curbuf->b_p_ma = FALSE;
221 set_string_option_direct((char_u *)"buftype", -1,
222 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200223
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200224 set_term_and_win_size(term);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200225
Bram Moolenaare173fd02017-07-22 19:03:32 +0200226 if (cmd == NULL || *cmd == NUL)
227 cmd = p_sh;
228
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200229 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaare173fd02017-07-22 19:03:32 +0200230 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200231 {
232 /* store the size we ended up with */
233 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200234 }
235 else
236 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200237 free_terminal(term);
238 curbuf->b_term = NULL;
239
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200240 /* Wiping out the buffer will also close the window and call
241 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200242 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200243 }
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200244
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200245 /* TODO: Setup pty, see mch_call_shell(). */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200246}
247
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200248/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200249 * Free a terminal and everything it refers to.
250 * Kills the job if there is one.
251 * Called when wiping out a buffer.
252 */
253 void
254free_terminal(term_T *term)
255{
256 term_T *tp;
257
258 if (term == NULL)
259 return;
260 if (first_term == term)
261 first_term = term->tl_next;
262 else
263 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
264 if (tp->tl_next == term)
265 {
266 tp->tl_next = term->tl_next;
267 break;
268 }
269
270 if (term->tl_job != NULL)
271 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200272 if (term->tl_job->jv_status != JOB_ENDED
273 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200274 job_stop(term->tl_job, NULL, "kill");
275 job_unref(term->tl_job);
276 }
277
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200278 term_free(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200279 vim_free(term->tl_title);
280 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200281 vim_free(term);
282}
283
284/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200285 * Write job output "msg[len]" to the vterm.
286 */
287 static void
288term_write_job_output(term_T *term, char_u *msg, size_t len)
289{
290 VTerm *vterm = term->tl_vterm;
291 char_u *p;
292 size_t done;
293 size_t len_now;
294
295 for (done = 0; done < len; done += len_now)
296 {
297 for (p = msg + done; p < msg + len; )
298 {
299 if (*p == NL)
300 break;
301 p += utf_ptr2len_len(p, len - (p - msg));
302 }
303 len_now = p - msg - done;
304 vterm_input_write(vterm, (char *)msg + done, len_now);
305 if (p < msg + len && *p == NL)
306 {
307 /* Convert NL to CR-NL, that appears to work best. */
308 vterm_input_write(vterm, "\r\n", 2);
309 ++len_now;
310 }
311 }
312
313 /* this invokes the damage callbacks */
314 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
315}
316
317/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200318 * Invoked when "msg" output from a job was received. Write it to the terminal
319 * of "buffer".
320 */
321 void
322write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
323{
324 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200325 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200326
327 ch_logn(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200328 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200329
330 /* TODO: only update once in a while. */
331 update_screen(0);
332 setcursor();
333 out_flush();
334}
335
336/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200337 * Convert typed key "c" into bytes to send to the job.
338 * Return the number of bytes in "buf".
339 */
340 static int
341term_convert_key(int c, char *buf)
342{
343 VTerm *vterm = curbuf->b_term->tl_vterm;
344 VTermKey key = VTERM_KEY_NONE;
345 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200346
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200347 switch (c)
348 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200349 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200350 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200351 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
352 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200353 case K_DEL: key = VTERM_KEY_DEL; break;
354 case K_DOWN: key = VTERM_KEY_DOWN; break;
355 case K_END: key = VTERM_KEY_END; break;
356 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
357 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
358 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
359 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
360 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
361 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
362 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
363 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
364 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
365 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
366 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
367 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
368 case K_HOME: key = VTERM_KEY_HOME; break;
369 case K_INS: key = VTERM_KEY_INS; break;
370 case K_K0: key = VTERM_KEY_KP_0; break;
371 case K_K1: key = VTERM_KEY_KP_1; break;
372 case K_K2: key = VTERM_KEY_KP_2; break;
373 case K_K3: key = VTERM_KEY_KP_3; break;
374 case K_K4: key = VTERM_KEY_KP_4; break;
375 case K_K5: key = VTERM_KEY_KP_5; break;
376 case K_K6: key = VTERM_KEY_KP_6; break;
377 case K_K7: key = VTERM_KEY_KP_7; break;
378 case K_K8: key = VTERM_KEY_KP_8; break;
379 case K_K9: key = VTERM_KEY_KP_9; break;
380 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
381 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
382 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
383 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
384 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
385 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
386 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
387 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
388 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
389 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
390 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
391 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
392 case K_LEFT: key = VTERM_KEY_LEFT; break;
393 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
394 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
395 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
396 case K_UP: key = VTERM_KEY_UP; break;
397 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200398
399 case K_MOUSEUP: /* TODO */ break;
400 case K_MOUSEDOWN: /* TODO */ break;
401 case K_MOUSELEFT: /* TODO */ break;
402 case K_MOUSERIGHT: /* TODO */ break;
403
404 case K_LEFTMOUSE: /* TODO */ break;
405 case K_LEFTMOUSE_NM: /* TODO */ break;
406 case K_LEFTDRAG: /* TODO */ break;
407 case K_LEFTRELEASE: /* TODO */ break;
408 case K_LEFTRELEASE_NM: /* TODO */ break;
409 case K_MIDDLEMOUSE: /* TODO */ break;
410 case K_MIDDLEDRAG: /* TODO */ break;
411 case K_MIDDLERELEASE: /* TODO */ break;
412 case K_RIGHTMOUSE: /* TODO */ break;
413 case K_RIGHTDRAG: /* TODO */ break;
414 case K_RIGHTRELEASE: /* TODO */ break;
415 case K_X1MOUSE: /* TODO */ break;
416 case K_X1DRAG: /* TODO */ break;
417 case K_X1RELEASE: /* TODO */ break;
418 case K_X2MOUSE: /* TODO */ break;
419 case K_X2DRAG: /* TODO */ break;
420 case K_X2RELEASE: /* TODO */ break;
421
422 /* TODO: handle all special keys and modifiers that terminal_loop()
423 * does not handle. */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200424 }
425
426 /*
427 * Convert special keys to vterm keys:
428 * - Write keys to vterm: vterm_keyboard_key()
429 * - Write output to channel.
430 */
431 if (key != VTERM_KEY_NONE)
432 /* Special key, let vterm convert it. */
433 vterm_keyboard_key(vterm, key, mod);
434 else
435 /* Normal character, let vterm convert it. */
436 vterm_keyboard_unichar(vterm, c, mod);
437
438 /* Read back the converted escape sequence. */
439 return vterm_output_read(vterm, buf, KEY_BUF_LEN);
440}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200441
Bram Moolenaar938783d2017-07-16 20:13:26 +0200442/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200443 * Wait for input and send it to the job.
444 * Return when the start of a CTRL-W command is typed or anything else that
445 * should be handled as a Normal mode command.
446 */
447 void
448terminal_loop(void)
449{
450 char buf[KEY_BUF_LEN];
451 int c;
452 size_t len;
453 static int mouse_was_outside = FALSE;
454 int dragging_outside = FALSE;
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200455 int termkey = 0;
456
457 if (*curwin->w_p_tk != NUL)
458 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200459
460 for (;;)
461 {
462 /* TODO: skip screen update when handling a sequence of keys. */
463 update_screen(0);
464 setcursor();
465 out_flush();
466 ++no_mapping;
467 ++allow_keys;
468 got_int = FALSE;
469 c = vgetc();
470 --no_mapping;
471 --allow_keys;
472
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200473 if (c == (termkey == 0 ? Ctrl_W : termkey))
474 {
475 stuffcharReadbuff(Ctrl_W);
476 return;
477 }
478
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200479 /* Catch keys that need to be handled as in Normal mode. */
480 switch (c)
481 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200482 case NUL:
483 case K_ZERO:
484 stuffcharReadbuff(c);
485 return;
486
487 case K_IGNORE: continue;
488
489 case K_LEFTDRAG:
490 case K_MIDDLEDRAG:
491 case K_RIGHTDRAG:
492 case K_X1DRAG:
493 case K_X2DRAG:
494 dragging_outside = mouse_was_outside;
495 /* FALLTHROUGH */
496 case K_LEFTMOUSE:
497 case K_LEFTMOUSE_NM:
498 case K_LEFTRELEASE:
499 case K_LEFTRELEASE_NM:
500 case K_MIDDLEMOUSE:
501 case K_MIDDLERELEASE:
502 case K_RIGHTMOUSE:
503 case K_RIGHTRELEASE:
504 case K_X1MOUSE:
505 case K_X1RELEASE:
506 case K_X2MOUSE:
507 case K_X2RELEASE:
508 if (mouse_row < W_WINROW(curwin)
509 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
510 || mouse_col < W_WINCOL(curwin)
511 || mouse_col >= W_ENDCOL(curwin)
512 || dragging_outside)
513 {
514 /* click outside the current window */
515 stuffcharReadbuff(c);
516 mouse_was_outside = TRUE;
517 return;
518 }
519 }
520 mouse_was_outside = FALSE;
521
522 /* Convert the typed key to a sequence of bytes for the job. */
523 len = term_convert_key(c, buf);
524 if (len > 0)
525 /* TODO: if FAIL is returned, stop? */
526 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
527 (char_u *)buf, len, NULL);
528 }
529}
530
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200531/*
532 * Called when a job has finished.
533 */
534 void
535term_job_ended(job_T *job)
536{
Bram Moolenaar21554412017-07-24 21:44:43 +0200537 term_T *term;
538 int did_one = FALSE;
539
540 for (term = first_term; term != NULL; term = term->tl_next)
541 if (term->tl_job == job)
542 {
543 vim_free(term->tl_title);
544 term->tl_title = NULL;
545 vim_free(term->tl_status_text);
546 term->tl_status_text = NULL;
547 redraw_buf_and_status_later(term->tl_buffer, VALID);
548 did_one = TRUE;
549 }
550 if (did_one)
551 {
552 redraw_statuslines();
553 setcursor();
554 out_flush();
555 }
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200556 if (curbuf->b_term != NULL && curbuf->b_term->tl_job == job)
557 maketitle();
558}
559
560/*
561 * Return TRUE if the job for "buf" is still running.
562 */
Bram Moolenaar21554412017-07-24 21:44:43 +0200563 static int
564term_job_running(term_T *term)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200565{
Bram Moolenaar21554412017-07-24 21:44:43 +0200566 return term->tl_job != NULL && term->tl_job->jv_status == JOB_STARTED;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200567}
568
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200569 static void
570position_cursor(win_T *wp, VTermPos *pos)
571{
572 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
573 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
574}
575
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200576 static int
577handle_damage(VTermRect rect, void *user)
578{
579 term_T *term = (term_T *)user;
580
581 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
582 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
583 redraw_buf_later(term->tl_buffer, NOT_VALID);
584 return 1;
585}
586
587 static int
588handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
589{
590 term_T *term = (term_T *)user;
591
592 /* TODO */
593 redraw_buf_later(term->tl_buffer, NOT_VALID);
594 return 1;
595}
596
597 static int
598handle_movecursor(
599 VTermPos pos,
600 VTermPos oldpos UNUSED,
601 int visible UNUSED,
602 void *user)
603{
604 term_T *term = (term_T *)user;
605 win_T *wp;
606 int is_current = FALSE;
607
608 FOR_ALL_WINDOWS(wp)
609 {
610 if (wp->w_buffer == term->tl_buffer)
611 {
612 position_cursor(wp, &pos);
613 if (wp == curwin)
614 is_current = TRUE;
615 }
616 }
617
618 if (is_current)
619 {
620 setcursor();
621 out_flush();
622 }
623
624 return 1;
625}
626
Bram Moolenaar21554412017-07-24 21:44:43 +0200627 static int
628handle_settermprop(
629 VTermProp prop,
630 VTermValue *value,
631 void *user)
632{
633 term_T *term = (term_T *)user;
634
635 switch (prop)
636 {
637 case VTERM_PROP_TITLE:
638 vim_free(term->tl_title);
639 term->tl_title = vim_strsave((char_u *)value->string);
640 vim_free(term->tl_status_text);
641 term->tl_status_text = NULL;
642 if (term == curbuf->b_term)
643 maketitle();
644 return 1;
645 default:
646 break;
647 }
648 return 0;
649}
650
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200651/*
652 * The job running in the terminal resized the terminal.
653 */
654 static int
655handle_resize(int rows, int cols, void *user)
656{
657 term_T *term = (term_T *)user;
658 win_T *wp;
659
660 term->tl_rows = rows;
661 term->tl_cols = cols;
662 FOR_ALL_WINDOWS(wp)
663 {
664 if (wp->w_buffer == term->tl_buffer)
665 {
666 win_setheight_win(rows, wp);
667 win_setwidth_win(cols, wp);
668 }
669 }
670
671 redraw_buf_later(term->tl_buffer, NOT_VALID);
672 return 1;
673}
674
Bram Moolenaar21554412017-07-24 21:44:43 +0200675static VTermScreenCallbacks screen_callbacks = {
676 handle_damage, /* damage */
677 handle_moverect, /* moverect */
678 handle_movecursor, /* movecursor */
679 handle_settermprop, /* settermprop */
680 NULL, /* bell */
681 handle_resize, /* resize */
682 NULL, /* sb_pushline */
683 NULL /* sb_popline */
684};
685
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200686/*
Bram Moolenaareeac6772017-07-23 15:48:37 +0200687 * Reverse engineer the RGB value into a cterm color index.
688 * First color is 1. Return 0 if no match found.
689 */
690 static int
691color2index(VTermColor *color)
692{
693 int red = color->red;
694 int blue = color->blue;
695 int green = color->green;
696
697 if (red == 0)
698 {
699 if (green == 0)
700 {
701 if (blue == 0)
702 return 1; /* black */
703 if (blue == 224)
704 return 5; /* blue */
705 }
706 else if (green == 224)
707 {
708 if (blue == 0)
709 return 3; /* green */
710 if (blue == 224)
711 return 7; /* cyan */
712 }
713 }
714 else if (red == 224)
715 {
716 if (green == 0)
717 {
718 if (blue == 0)
719 return 2; /* red */
720 if (blue == 224)
721 return 6; /* magenta */
722 }
723 else if (green == 224)
724 {
725 if (blue == 0)
726 return 4; /* yellow */
727 if (blue == 224)
728 return 8; /* white */
729 }
730 }
731 else if (red == 128)
732 {
733 if (green == 128 && blue == 128)
Bram Moolenaar8a773062017-07-24 22:29:21 +0200734 return 9; /* high intensity black */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200735 }
736 else if (red == 255)
737 {
738 if (green == 64)
739 {
740 if (blue == 64)
741 return 10; /* high intensity red */
742 if (blue == 255)
743 return 14; /* high intensity magenta */
744 }
745 else if (green == 255)
746 {
747 if (blue == 64)
748 return 12; /* high intensity yellow */
749 if (blue == 255)
750 return 16; /* high intensity white */
751 }
752 }
753 else if (red == 64)
754 {
755 if (green == 64)
756 {
757 if (blue == 255)
758 return 13; /* high intensity blue */
759 }
760 else if (green == 255)
761 {
762 if (blue == 64)
763 return 11; /* high intensity green */
764 if (blue == 255)
765 return 15; /* high intensity cyan */
766 }
767 }
768 if (t_colors >= 256)
769 {
770 if (red == blue && red == green)
771 {
772 /* 24-color greyscale */
773 static int cutoff[23] = {
774 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
775 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
776 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
777 int i;
778
779 for (i = 0; i < 23; ++i)
780 if (red < cutoff[i])
781 return i + 233;
782 return 256;
783 }
784
785 /* 216-color cube */
786 return 17 + ((red + 25) / 0x33) * 36
787 + ((green + 25) / 0x33) * 6
788 + (blue + 25) / 0x33;
789 }
790 return 0;
791}
792
793/*
794 * Convert the attributes of a vterm cell into an attribute index.
795 */
796 static int
797cell2attr(VTermScreenCell *cell)
798{
799 int attr = 0;
800
801 if (cell->attrs.bold)
802 attr |= HL_BOLD;
803 if (cell->attrs.underline)
804 attr |= HL_UNDERLINE;
805 if (cell->attrs.italic)
806 attr |= HL_ITALIC;
807 if (cell->attrs.strike)
808 attr |= HL_STANDOUT;
809 if (cell->attrs.reverse)
810 attr |= HL_INVERSE;
811 if (cell->attrs.strike)
812 attr |= HL_UNDERLINE;
813
814#ifdef FEAT_GUI
815 if (gui.in_use)
816 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +0200817 guicolor_T fg, bg;
818
819 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
820 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
821 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +0200822 }
823 else
824#endif
825#ifdef FEAT_TERMGUICOLORS
826 if (p_tgc)
827 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +0200828 guicolor_T fg, bg;
829
830 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
831 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
832
833 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +0200834 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +0200835 else
Bram Moolenaareeac6772017-07-23 15:48:37 +0200836#endif
837 {
838 return get_cterm_attr_idx(attr, color2index(&cell->fg),
839 color2index(&cell->bg));
840 }
841 return 0;
842}
843
844/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200845 * Called to update the window that contains the terminal.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200846 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200847 void
848term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +0200849{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200850 term_T *term = wp->w_buffer->b_term;
851 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200852 VTermScreen *screen = vterm_obtain_screen(vterm);
Bram Moolenaar58556cd2017-07-20 23:04:46 +0200853 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200854 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200855
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200856 /*
857 * If the window was resized a redraw will be triggered and we get here.
858 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
859 */
860 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
861 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +0200862 {
863 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
864 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
865
866 vterm_set_size(vterm, rows, cols);
867 ch_logn(term->tl_job->jv_channel, "Resizing terminal to %d lines",
868 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200869 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +0200870 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +0200871
872 /* The cursor may have been moved when resizing. */
873 vterm_state_get_cursorpos(state, &pos);
874 position_cursor(wp, &pos);
875
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200876 /* TODO: Only redraw what changed. */
877 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +0200878 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200879 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200880 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +0200881
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200882 if (pos.row < term->tl_rows)
883 {
884 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200885 {
886 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200887 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200888
Bram Moolenaareeac6772017-07-23 15:48:37 +0200889 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
890 vim_memset(&cell, 0, sizeof(cell));
891
892 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200893 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200894 if (c == NUL)
895 {
896 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +0200897#if defined(FEAT_MBYTE)
898 if (enc_utf8)
899 ScreenLinesUC[off] = NUL;
900#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200901 }
902 else
903 {
904#if defined(FEAT_MBYTE)
905 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200906 {
907 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200908 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200909 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200910 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200911 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200912 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +0200913 if (enc_utf8)
914 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200915 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200916#else
917 ScreenLines[off] = c;
918#endif
919 }
Bram Moolenaareeac6772017-07-23 15:48:37 +0200920 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200921
922 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200923 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200924 if (cell.width == 2)
925 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200926 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +0200927#if defined(FEAT_MBYTE)
928 if (enc_utf8)
929 ScreenLinesUC[off] = NUL;
930#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200931 ++pos.col;
932 ++off;
933 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200934 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200935 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200936 else
937 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200938
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200939 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
940 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +0200941 }
942}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200943
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200944/*
945 * Set job options common for Unix and MS-Windows.
946 */
947 static void
948setup_job_options(jobopt_T *opt, int rows, int cols)
949{
950 clear_job_options(opt);
951 opt->jo_mode = MODE_RAW;
952 opt->jo_out_mode = MODE_RAW;
953 opt->jo_err_mode = MODE_RAW;
954 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200955
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200956 opt->jo_io[PART_OUT] = JIO_BUFFER;
957 opt->jo_io[PART_ERR] = JIO_BUFFER;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200958 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
959
960 opt->jo_modifiable[PART_OUT] = 0;
961 opt->jo_modifiable[PART_ERR] = 0;
962 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
963
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200964 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
965 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +0200966 opt->jo_pty = TRUE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200967 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
968
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200969 opt->jo_term_rows = rows;
970 opt->jo_term_cols = cols;
971}
972
973/*
974 * Create a new vterm and initialize it.
975 */
976 static void
977create_vterm(term_T *term, int rows, int cols)
978{
979 VTerm *vterm;
980 VTermScreen *screen;
981
982 vterm = vterm_new(rows, cols);
983 term->tl_vterm = vterm;
984 screen = vterm_obtain_screen(vterm);
985 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
986 /* TODO: depends on 'encoding'. */
987 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +0200988
989 /* Vterm uses a default black background. Set it to white when
990 * 'background' is "light". */
991 if (*p_bg == 'l')
992 {
993 VTermColor fg, bg;
994
995 fg.red = fg.green = fg.blue = 0;
996 bg.red = bg.green = bg.blue = 255;
997 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
998 }
999
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001000 /* Required to initialize most things. */
1001 vterm_screen_reset(screen, 1 /* hard */);
1002}
1003
Bram Moolenaar21554412017-07-24 21:44:43 +02001004/*
1005 * Return the text to show for the buffer name and status.
1006 */
1007 char_u *
1008term_get_status_text(term_T *term)
1009{
1010 if (term->tl_status_text == NULL)
1011 {
1012 char_u *txt;
1013 size_t len;
1014
1015 if (term->tl_title != NULL)
1016 txt = term->tl_title;
1017 else if (term_job_running(term))
1018 txt = (char_u *)_("running");
1019 else
1020 txt = (char_u *)_("finished");
1021 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
1022 term->tl_status_text = alloc(len);
1023 if (term->tl_status_text != NULL)
1024 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1025 term->tl_buffer->b_fname, txt);
1026 }
1027 return term->tl_status_text;
1028}
1029
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001030# ifdef WIN3264
1031
1032#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
1033#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
1034
Bram Moolenaar8a773062017-07-24 22:29:21 +02001035void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001036void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02001037void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001038BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
1039void (*winpty_config_set_initial_size)(void*, int, int);
1040LPCWSTR (*winpty_conin_name)(void*);
1041LPCWSTR (*winpty_conout_name)(void*);
1042LPCWSTR (*winpty_conerr_name)(void*);
1043void (*winpty_free)(void*);
1044void (*winpty_config_free)(void*);
1045void (*winpty_spawn_config_free)(void*);
1046void (*winpty_error_free)(void*);
1047LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001048BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001049
1050/**************************************
1051 * 2. MS-Windows implementation.
1052 */
1053
1054#define WINPTY_DLL "winpty.dll"
1055
1056static HINSTANCE hWinPtyDLL = NULL;
1057
1058 int
1059dyn_winpty_init(void)
1060{
1061 int i;
1062 static struct
1063 {
1064 char *name;
1065 FARPROC *ptr;
1066 } winpty_entry[] =
1067 {
1068 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
1069 {"winpty_config_free", (FARPROC*)&winpty_config_free},
1070 {"winpty_config_new", (FARPROC*)&winpty_config_new},
1071 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
1072 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
1073 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
1074 {"winpty_error_free", (FARPROC*)&winpty_error_free},
1075 {"winpty_free", (FARPROC*)&winpty_free},
1076 {"winpty_open", (FARPROC*)&winpty_open},
1077 {"winpty_spawn", (FARPROC*)&winpty_spawn},
1078 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
1079 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
1080 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001081 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001082 {NULL, NULL}
1083 };
1084
1085 /* No need to initialize twice. */
1086 if (hWinPtyDLL)
1087 return 1;
1088 /* Load winpty.dll */
1089 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
1090 if (!hWinPtyDLL)
1091 {
1092 EMSG2(_(e_loadlib), WINPTY_DLL);
1093 return 0;
1094 }
1095 for (i = 0; winpty_entry[i].name != NULL
1096 && winpty_entry[i].ptr != NULL; ++i)
1097 {
1098 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
1099 winpty_entry[i].name)) == NULL)
1100 {
1101 EMSG2(_(e_loadfunc), winpty_entry[i].name);
1102 return 0;
1103 }
1104 }
1105
1106 return 1;
1107}
1108
1109/*
1110 * Create a new terminal of "rows" by "cols" cells.
1111 * Store a reference in "term".
1112 * Return OK or FAIL.
1113 */
1114 static int
1115term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
1116{
1117 WCHAR *p = enc_to_utf16(cmd, NULL);
1118 channel_T *channel = NULL;
1119 job_T *job = NULL;
1120 jobopt_T opt;
1121 DWORD error;
1122 HANDLE jo = NULL, child_process_handle, child_thread_handle;
1123 void *winpty_err;
1124 void *spawn_config;
1125
1126 if (!dyn_winpty_init())
1127 return FAIL;
1128
1129 if (p == NULL)
1130 return FAIL;
1131
1132 job = job_alloc();
1133 if (job == NULL)
1134 goto failed;
1135
1136 channel = add_channel();
1137 if (channel == NULL)
1138 goto failed;
1139
1140 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
1141 if (term->tl_winpty_config == NULL)
1142 goto failed;
1143
1144 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
1145 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
1146 if (term->tl_winpty == NULL)
1147 goto failed;
1148
1149 spawn_config = winpty_spawn_config_new(
1150 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
1151 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
1152 NULL,
1153 p,
1154 NULL,
1155 NULL,
1156 &winpty_err);
1157 if (spawn_config == NULL)
1158 goto failed;
1159
1160 channel = add_channel();
1161 if (channel == NULL)
1162 goto failed;
1163
1164 job = job_alloc();
1165 if (job == NULL)
1166 goto failed;
1167
1168 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
1169 &child_thread_handle, &error, &winpty_err))
1170 goto failed;
1171
1172 channel_set_pipes(channel,
1173 (sock_T) CreateFileW(
1174 winpty_conin_name(term->tl_winpty),
1175 GENERIC_WRITE, 0, NULL,
1176 OPEN_EXISTING, 0, NULL),
1177 (sock_T) CreateFileW(
1178 winpty_conout_name(term->tl_winpty),
1179 GENERIC_READ, 0, NULL,
1180 OPEN_EXISTING, 0, NULL),
1181 (sock_T) CreateFileW(
1182 winpty_conerr_name(term->tl_winpty),
1183 GENERIC_READ, 0, NULL,
1184 OPEN_EXISTING, 0, NULL));
1185
1186 jo = CreateJobObject(NULL, NULL);
1187 if (jo == NULL)
1188 goto failed;
1189
1190 if (!AssignProcessToJobObject(jo, child_process_handle))
1191 goto failed;
1192
1193 winpty_spawn_config_free(spawn_config);
1194
1195 create_vterm(term, rows, cols);
1196
1197 setup_job_options(&opt, rows, cols);
1198 channel_set_job(channel, job, &opt);
1199
1200 job->jv_channel = channel;
1201 job->jv_proc_info.hProcess = child_process_handle;
1202 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
1203 job->jv_job_object = jo;
1204 job->jv_status = JOB_STARTED;
1205 term->tl_job = job;
1206
1207 return OK;
1208
1209failed:
1210 if (channel != NULL)
1211 channel_clear(channel);
1212 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001213 {
1214 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001215 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001216 }
1217 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001218 if (jo != NULL)
1219 CloseHandle(jo);
1220 if (term->tl_winpty != NULL)
1221 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001222 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001223 if (term->tl_winpty_config != NULL)
1224 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001225 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001226 if (winpty_err != NULL)
1227 {
1228 char_u *msg = utf16_to_enc(
1229 (short_u *)winpty_error_msg(winpty_err), NULL);
1230
1231 EMSG(msg);
1232 winpty_error_free(winpty_err);
1233 }
1234 return FAIL;
1235}
1236
1237/*
1238 * Free the terminal emulator part of "term".
1239 */
1240 static void
1241term_free(term_T *term)
1242{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001243 if (term->tl_winpty != NULL)
1244 winpty_free(term->tl_winpty);
1245 if (term->tl_winpty_config != NULL)
1246 winpty_config_free(term->tl_winpty_config);
1247 if (term->tl_vterm != NULL)
1248 vterm_free(term->tl_vterm);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001249}
1250
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001251/*
1252 * Request size to terminal.
1253 */
1254 static void
1255term_report_winsize(term_T *term, int rows, int cols)
1256{
1257 winpty_set_size(term->tl_winpty, cols, rows, NULL);
1258}
1259
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001260# else
1261
1262/**************************************
1263 * 3. Unix-like implementation.
1264 */
1265
1266/*
1267 * Create a new terminal of "rows" by "cols" cells.
1268 * Start job for "cmd".
1269 * Store the pointers in "term".
1270 * Return OK or FAIL.
1271 */
1272 static int
1273term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
1274{
1275 typval_T argvars[2];
1276 jobopt_T opt;
1277
1278 create_vterm(term, rows, cols);
1279
1280 argvars[0].v_type = VAR_STRING;
1281 argvars[0].vval.v_string = cmd;
1282 setup_job_options(&opt, rows, cols);
1283 term->tl_job = job_start(argvars, &opt);
1284
Bram Moolenaar61a66052017-07-22 18:39:00 +02001285 return term->tl_job != NULL
1286 && term->tl_job->jv_channel != NULL
1287 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001288}
1289
1290/*
1291 * Free the terminal emulator part of "term".
1292 */
1293 static void
1294term_free(term_T *term)
1295{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001296 if (term->tl_vterm != NULL)
1297 vterm_free(term->tl_vterm);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001298}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001299
1300/*
1301 * Request size to terminal.
1302 */
1303 static void
1304term_report_winsize(term_T *term, int rows, int cols)
1305{
1306 /* Use an ioctl() to report the new window size to the job. */
1307 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
1308 {
1309 int fd = -1;
1310 int part;
1311
1312 for (part = PART_OUT; part < PART_COUNT; ++part)
1313 {
1314 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
1315 if (isatty(fd))
1316 break;
1317 }
1318 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
1319 mch_stop_job(term->tl_job, (char_u *)"winch");
1320 }
1321}
1322
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001323# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001324
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001325#endif /* FEAT_TERMINAL */