blob: ef3c3c241d8b68e47826cc1f26e06b69a64ed2e0 [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
28 * terminal encoding and writting to the job over a channel.
29 *
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)
734 return 9; /* high intensity bladk */
735 }
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] = ' ';
897 ScreenLinesUC[off] = NUL;
898 }
899 else
900 {
901#if defined(FEAT_MBYTE)
902 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200903 {
904 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200905 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200906 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200907 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200908 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200909 ScreenLines[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200910 ScreenLinesUC[off] = NUL;
911 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200912#else
913 ScreenLines[off] = c;
914#endif
915 }
Bram Moolenaareeac6772017-07-23 15:48:37 +0200916 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200917
918 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200919 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200920 if (cell.width == 2)
921 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200922 ScreenLines[off] = NUL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200923 ScreenLinesUC[off] = NUL;
924 ++pos.col;
925 ++off;
926 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200927 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200928 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200929 else
930 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200931
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200932 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
933 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +0200934 }
935}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200936
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200937/*
938 * Set job options common for Unix and MS-Windows.
939 */
940 static void
941setup_job_options(jobopt_T *opt, int rows, int cols)
942{
943 clear_job_options(opt);
944 opt->jo_mode = MODE_RAW;
945 opt->jo_out_mode = MODE_RAW;
946 opt->jo_err_mode = MODE_RAW;
947 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200948
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200949 opt->jo_io[PART_OUT] = JIO_BUFFER;
950 opt->jo_io[PART_ERR] = JIO_BUFFER;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200951 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
952
953 opt->jo_modifiable[PART_OUT] = 0;
954 opt->jo_modifiable[PART_ERR] = 0;
955 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
956
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200957 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
958 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +0200959 opt->jo_pty = TRUE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200960 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
961
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200962 opt->jo_term_rows = rows;
963 opt->jo_term_cols = cols;
964}
965
966/*
967 * Create a new vterm and initialize it.
968 */
969 static void
970create_vterm(term_T *term, int rows, int cols)
971{
972 VTerm *vterm;
973 VTermScreen *screen;
974
975 vterm = vterm_new(rows, cols);
976 term->tl_vterm = vterm;
977 screen = vterm_obtain_screen(vterm);
978 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
979 /* TODO: depends on 'encoding'. */
980 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +0200981
982 /* Vterm uses a default black background. Set it to white when
983 * 'background' is "light". */
984 if (*p_bg == 'l')
985 {
986 VTermColor fg, bg;
987
988 fg.red = fg.green = fg.blue = 0;
989 bg.red = bg.green = bg.blue = 255;
990 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
991 }
992
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200993 /* Required to initialize most things. */
994 vterm_screen_reset(screen, 1 /* hard */);
995}
996
Bram Moolenaar21554412017-07-24 21:44:43 +0200997/*
998 * Return the text to show for the buffer name and status.
999 */
1000 char_u *
1001term_get_status_text(term_T *term)
1002{
1003 if (term->tl_status_text == NULL)
1004 {
1005 char_u *txt;
1006 size_t len;
1007
1008 if (term->tl_title != NULL)
1009 txt = term->tl_title;
1010 else if (term_job_running(term))
1011 txt = (char_u *)_("running");
1012 else
1013 txt = (char_u *)_("finished");
1014 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
1015 term->tl_status_text = alloc(len);
1016 if (term->tl_status_text != NULL)
1017 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1018 term->tl_buffer->b_fname, txt);
1019 }
1020 return term->tl_status_text;
1021}
1022
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001023# ifdef WIN3264
1024
1025#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
1026#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
1027
1028void* (*winpty_config_new)(int, void*);
1029void* (*winpty_open)(void*, void*);
1030void* (*winpty_spawn_config_new)(int, void*, LPCWSTR, void*, void*, void*);
1031BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
1032void (*winpty_config_set_initial_size)(void*, int, int);
1033LPCWSTR (*winpty_conin_name)(void*);
1034LPCWSTR (*winpty_conout_name)(void*);
1035LPCWSTR (*winpty_conerr_name)(void*);
1036void (*winpty_free)(void*);
1037void (*winpty_config_free)(void*);
1038void (*winpty_spawn_config_free)(void*);
1039void (*winpty_error_free)(void*);
1040LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001041BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001042
1043/**************************************
1044 * 2. MS-Windows implementation.
1045 */
1046
1047#define WINPTY_DLL "winpty.dll"
1048
1049static HINSTANCE hWinPtyDLL = NULL;
1050
1051 int
1052dyn_winpty_init(void)
1053{
1054 int i;
1055 static struct
1056 {
1057 char *name;
1058 FARPROC *ptr;
1059 } winpty_entry[] =
1060 {
1061 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
1062 {"winpty_config_free", (FARPROC*)&winpty_config_free},
1063 {"winpty_config_new", (FARPROC*)&winpty_config_new},
1064 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
1065 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
1066 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
1067 {"winpty_error_free", (FARPROC*)&winpty_error_free},
1068 {"winpty_free", (FARPROC*)&winpty_free},
1069 {"winpty_open", (FARPROC*)&winpty_open},
1070 {"winpty_spawn", (FARPROC*)&winpty_spawn},
1071 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
1072 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
1073 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001074 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001075 {NULL, NULL}
1076 };
1077
1078 /* No need to initialize twice. */
1079 if (hWinPtyDLL)
1080 return 1;
1081 /* Load winpty.dll */
1082 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
1083 if (!hWinPtyDLL)
1084 {
1085 EMSG2(_(e_loadlib), WINPTY_DLL);
1086 return 0;
1087 }
1088 for (i = 0; winpty_entry[i].name != NULL
1089 && winpty_entry[i].ptr != NULL; ++i)
1090 {
1091 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
1092 winpty_entry[i].name)) == NULL)
1093 {
1094 EMSG2(_(e_loadfunc), winpty_entry[i].name);
1095 return 0;
1096 }
1097 }
1098
1099 return 1;
1100}
1101
1102/*
1103 * Create a new terminal of "rows" by "cols" cells.
1104 * Store a reference in "term".
1105 * Return OK or FAIL.
1106 */
1107 static int
1108term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
1109{
1110 WCHAR *p = enc_to_utf16(cmd, NULL);
1111 channel_T *channel = NULL;
1112 job_T *job = NULL;
1113 jobopt_T opt;
1114 DWORD error;
1115 HANDLE jo = NULL, child_process_handle, child_thread_handle;
1116 void *winpty_err;
1117 void *spawn_config;
1118
1119 if (!dyn_winpty_init())
1120 return FAIL;
1121
1122 if (p == NULL)
1123 return FAIL;
1124
1125 job = job_alloc();
1126 if (job == NULL)
1127 goto failed;
1128
1129 channel = add_channel();
1130 if (channel == NULL)
1131 goto failed;
1132
1133 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
1134 if (term->tl_winpty_config == NULL)
1135 goto failed;
1136
1137 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
1138 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
1139 if (term->tl_winpty == NULL)
1140 goto failed;
1141
1142 spawn_config = winpty_spawn_config_new(
1143 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
1144 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
1145 NULL,
1146 p,
1147 NULL,
1148 NULL,
1149 &winpty_err);
1150 if (spawn_config == NULL)
1151 goto failed;
1152
1153 channel = add_channel();
1154 if (channel == NULL)
1155 goto failed;
1156
1157 job = job_alloc();
1158 if (job == NULL)
1159 goto failed;
1160
1161 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
1162 &child_thread_handle, &error, &winpty_err))
1163 goto failed;
1164
1165 channel_set_pipes(channel,
1166 (sock_T) CreateFileW(
1167 winpty_conin_name(term->tl_winpty),
1168 GENERIC_WRITE, 0, NULL,
1169 OPEN_EXISTING, 0, NULL),
1170 (sock_T) CreateFileW(
1171 winpty_conout_name(term->tl_winpty),
1172 GENERIC_READ, 0, NULL,
1173 OPEN_EXISTING, 0, NULL),
1174 (sock_T) CreateFileW(
1175 winpty_conerr_name(term->tl_winpty),
1176 GENERIC_READ, 0, NULL,
1177 OPEN_EXISTING, 0, NULL));
1178
1179 jo = CreateJobObject(NULL, NULL);
1180 if (jo == NULL)
1181 goto failed;
1182
1183 if (!AssignProcessToJobObject(jo, child_process_handle))
1184 goto failed;
1185
1186 winpty_spawn_config_free(spawn_config);
1187
1188 create_vterm(term, rows, cols);
1189
1190 setup_job_options(&opt, rows, cols);
1191 channel_set_job(channel, job, &opt);
1192
1193 job->jv_channel = channel;
1194 job->jv_proc_info.hProcess = child_process_handle;
1195 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
1196 job->jv_job_object = jo;
1197 job->jv_status = JOB_STARTED;
1198 term->tl_job = job;
1199
1200 return OK;
1201
1202failed:
1203 if (channel != NULL)
1204 channel_clear(channel);
1205 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001206 {
1207 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001208 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001209 }
1210 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001211 if (jo != NULL)
1212 CloseHandle(jo);
1213 if (term->tl_winpty != NULL)
1214 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001215 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001216 if (term->tl_winpty_config != NULL)
1217 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001218 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001219 if (winpty_err != NULL)
1220 {
1221 char_u *msg = utf16_to_enc(
1222 (short_u *)winpty_error_msg(winpty_err), NULL);
1223
1224 EMSG(msg);
1225 winpty_error_free(winpty_err);
1226 }
1227 return FAIL;
1228}
1229
1230/*
1231 * Free the terminal emulator part of "term".
1232 */
1233 static void
1234term_free(term_T *term)
1235{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001236 if (term->tl_winpty != NULL)
1237 winpty_free(term->tl_winpty);
1238 if (term->tl_winpty_config != NULL)
1239 winpty_config_free(term->tl_winpty_config);
1240 if (term->tl_vterm != NULL)
1241 vterm_free(term->tl_vterm);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001242}
1243
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001244/*
1245 * Request size to terminal.
1246 */
1247 static void
1248term_report_winsize(term_T *term, int rows, int cols)
1249{
1250 winpty_set_size(term->tl_winpty, cols, rows, NULL);
1251}
1252
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001253# else
1254
1255/**************************************
1256 * 3. Unix-like implementation.
1257 */
1258
1259/*
1260 * Create a new terminal of "rows" by "cols" cells.
1261 * Start job for "cmd".
1262 * Store the pointers in "term".
1263 * Return OK or FAIL.
1264 */
1265 static int
1266term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
1267{
1268 typval_T argvars[2];
1269 jobopt_T opt;
1270
1271 create_vterm(term, rows, cols);
1272
1273 argvars[0].v_type = VAR_STRING;
1274 argvars[0].vval.v_string = cmd;
1275 setup_job_options(&opt, rows, cols);
1276 term->tl_job = job_start(argvars, &opt);
1277
Bram Moolenaar61a66052017-07-22 18:39:00 +02001278 return term->tl_job != NULL
1279 && term->tl_job->jv_channel != NULL
1280 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001281}
1282
1283/*
1284 * Free the terminal emulator part of "term".
1285 */
1286 static void
1287term_free(term_T *term)
1288{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001289 if (term->tl_vterm != NULL)
1290 vterm_free(term->tl_vterm);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001291}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001292
1293/*
1294 * Request size to terminal.
1295 */
1296 static void
1297term_report_winsize(term_T *term, int rows, int cols)
1298{
1299 /* Use an ioctl() to report the new window size to the job. */
1300 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
1301 {
1302 int fd = -1;
1303 int part;
1304
1305 for (part = PART_OUT; part < PART_COUNT; ++part)
1306 {
1307 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
1308 if (isatty(fd))
1309 break;
1310 }
1311 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
1312 mch_stop_job(term->tl_job, (char_u *)"winch");
1313 }
1314}
1315
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001316# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001317
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001318#endif /* FEAT_TERMINAL */