blob: f5a9fd1a6884bb1f65e9282ae33f6dd8453fdb00 [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
37 * - put terminal title in the statusline
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020038 * - Add a scrollback buffer (contains lines to scroll off the top).
39 * Can use the buf_T lines, store attributes somewhere else?
40 * - When the job ends:
41 * - Write "-- JOB ENDED --" in the terminal.
42 * - Put the terminal contents in the scrollback buffer.
43 * - Free the terminal emulator.
44 * - Display the scrollback buffer (but with attributes).
45 * Make the buffer not modifiable, drop attributes when making changes.
Bram Moolenaar938783d2017-07-16 20:13:26 +020046 * - when closing window and job has not ended, make terminal hidden?
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +020047 * - don't allow exiting Vim when a terminal is still running a job
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020048 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar938783d2017-07-16 20:13:26 +020049 * - command line completion for :terminal
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020050 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020051 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020052 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020053 * - implement "term" for job_start(): more job options when starting a
54 * terminal.
Bram Moolenaar96ca27a2017-07-17 23:20:24 +020055 * - implement term_list() list of buffers with a terminal
56 * - implement term_getsize(buf)
57 * - implement term_setsize(buf)
58 * - implement term_sendkeys(buf, keys) send keystrokes to a terminal
59 * - implement term_wait(buf) wait for screen to be updated
60 * - implement term_scrape(buf, row) inspect terminal screen
61 * - implement term_open(command, options) open terminal window
62 * - implement term_getjob(buf)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +020063 * - implement 'termkey'
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020064 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
65 * conversions.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020066 */
67
68#include "vim.h"
69
70#ifdef FEAT_TERMINAL
71
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020072#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020073# define MIN(x,y) (x < y ? x : y)
74# define MAX(x,y) (x > y ? x : y)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020075#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020076
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020077#include "libvterm/include/vterm.h"
78
Bram Moolenaare4f25e42017-07-07 11:54:15 +020079/* typedef term_T in structs.h */
80struct terminal_S {
81 term_T *tl_next;
82
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020083#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020084 void *tl_winpty_config;
85 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020086#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020087 VTerm *tl_vterm;
Bram Moolenaare4f25e42017-07-07 11:54:15 +020088 job_T *tl_job;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +020089 buf_T *tl_buffer;
Bram Moolenaare4f25e42017-07-07 11:54:15 +020090
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020091 /* last known vterm size */
92 int tl_rows;
93 int tl_cols;
94 /* vterm size does not follow window size */
95 int tl_rows_fixed;
96 int tl_cols_fixed;
97
Bram Moolenaare4f25e42017-07-07 11:54:15 +020098 /* Range of screen rows to update. Zero based. */
99 int tl_dirty_row_start; /* -1 if nothing dirty */
100 int tl_dirty_row_end; /* row below last one to update */
101
102 pos_T tl_cursor;
103};
104
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200105/*
106 * List of all active terminals.
107 */
108static term_T *first_term = NULL;
109
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200110
111#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
112#define KEY_BUF_LEN 200
113
114/*
115 * Functions with separate implementation for MS-Windows and Unix-like systems.
116 */
117static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200118static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200119static void term_free(term_T *term);
120
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200121/**************************************
122 * 1. Generic code for all systems.
123 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200124
125/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200126 * Determine the terminal size from 'termsize' and the current window.
127 * Assumes term->tl_rows and term->tl_cols are zero.
128 */
129 static void
130set_term_and_win_size(term_T *term)
131{
132 if (*curwin->w_p_tms != NUL)
133 {
134 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
135
136 term->tl_rows = atoi((char *)curwin->w_p_tms);
137 term->tl_cols = atoi((char *)p);
138 }
139 if (term->tl_rows == 0)
140 term->tl_rows = curwin->w_height;
141 else
142 {
143 win_setheight_win(term->tl_rows, curwin);
144 term->tl_rows_fixed = TRUE;
145 }
146 if (term->tl_cols == 0)
147 term->tl_cols = curwin->w_width;
148 else
149 {
150 win_setwidth_win(term->tl_cols, curwin);
151 term->tl_cols_fixed = TRUE;
152 }
153}
154
155/*
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200156 * ":terminal": open a terminal window and execute a job in it.
157 */
158 void
159ex_terminal(exarg_T *eap)
160{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200161 exarg_T split_ea;
162 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200163 term_T *term;
Bram Moolenaare173fd02017-07-22 19:03:32 +0200164 char_u *cmd = eap->arg;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200165
166 if (check_restricted() || check_secure())
167 return;
168
169 term = (term_T *)alloc_clear(sizeof(term_T));
170 if (term == NULL)
171 return;
172 term->tl_dirty_row_end = MAX_ROW;
173
174 /* Open a new window or tab. */
175 vim_memset(&split_ea, 0, sizeof(split_ea));
176 split_ea.cmdidx = CMD_new;
177 split_ea.cmd = (char_u *)"new";
178 split_ea.arg = (char_u *)"";
179 ex_splitview(&split_ea);
180 if (curwin == old_curwin)
181 {
182 /* split failed */
183 vim_free(term);
184 return;
185 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200186 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200187 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200188
189 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200190 term->tl_next = first_term;
191 first_term = term;
192
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200193 if (buflist_findname(cmd) == NULL)
194 curbuf->b_ffname = vim_strsave(cmd);
195 else
196 {
197 int i;
198 size_t len = STRLEN(cmd) + 10;
199 char_u *p = alloc(len);
200
201 for (i = 1; p != NULL; ++i)
202 {
203 vim_snprintf((char *)p, len, "%s (%d)", cmd, i);
204 if (buflist_findname(p) == NULL)
205 {
206 curbuf->b_ffname = p;
207 break;
208 }
209 }
210 }
211 curbuf->b_fname = curbuf->b_ffname;
212
213 /* Mark the buffer as changed, so that it's not easy to abandon the job. */
214 curbuf->b_changed = TRUE;
215 curbuf->b_p_ma = FALSE;
216 set_string_option_direct((char_u *)"buftype", -1,
217 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200218
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200219 set_term_and_win_size(term);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200220
Bram Moolenaare173fd02017-07-22 19:03:32 +0200221 if (cmd == NULL || *cmd == NUL)
222 cmd = p_sh;
223
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200224 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaare173fd02017-07-22 19:03:32 +0200225 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200226 {
227 /* store the size we ended up with */
228 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200229 }
230 else
231 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200232 free_terminal(term);
233 curbuf->b_term = NULL;
234
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200235 /* Wiping out the buffer will also close the window and call
236 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200237 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200238 }
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200239
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200240 /* TODO: Setup pty, see mch_call_shell(). */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200241}
242
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200243/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200244 * Free a terminal and everything it refers to.
245 * Kills the job if there is one.
246 * Called when wiping out a buffer.
247 */
248 void
249free_terminal(term_T *term)
250{
251 term_T *tp;
252
253 if (term == NULL)
254 return;
255 if (first_term == term)
256 first_term = term->tl_next;
257 else
258 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
259 if (tp->tl_next == term)
260 {
261 tp->tl_next = term->tl_next;
262 break;
263 }
264
265 if (term->tl_job != NULL)
266 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200267 if (term->tl_job->jv_status != JOB_ENDED
268 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200269 job_stop(term->tl_job, NULL, "kill");
270 job_unref(term->tl_job);
271 }
272
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200273 term_free(term);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200274 vim_free(term);
275}
276
277/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200278 * Write job output "msg[len]" to the vterm.
279 */
280 static void
281term_write_job_output(term_T *term, char_u *msg, size_t len)
282{
283 VTerm *vterm = term->tl_vterm;
284 char_u *p;
285 size_t done;
286 size_t len_now;
287
288 for (done = 0; done < len; done += len_now)
289 {
290 for (p = msg + done; p < msg + len; )
291 {
292 if (*p == NL)
293 break;
294 p += utf_ptr2len_len(p, len - (p - msg));
295 }
296 len_now = p - msg - done;
297 vterm_input_write(vterm, (char *)msg + done, len_now);
298 if (p < msg + len && *p == NL)
299 {
300 /* Convert NL to CR-NL, that appears to work best. */
301 vterm_input_write(vterm, "\r\n", 2);
302 ++len_now;
303 }
304 }
305
306 /* this invokes the damage callbacks */
307 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
308}
309
310/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200311 * Invoked when "msg" output from a job was received. Write it to the terminal
312 * of "buffer".
313 */
314 void
315write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
316{
317 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200318 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200319
320 ch_logn(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200321 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200322
323 /* TODO: only update once in a while. */
324 update_screen(0);
325 setcursor();
326 out_flush();
327}
328
329/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200330 * Convert typed key "c" into bytes to send to the job.
331 * Return the number of bytes in "buf".
332 */
333 static int
334term_convert_key(int c, char *buf)
335{
336 VTerm *vterm = curbuf->b_term->tl_vterm;
337 VTermKey key = VTERM_KEY_NONE;
338 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200339
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200340 switch (c)
341 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200342 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200343 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200344 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
345 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200346 case K_DEL: key = VTERM_KEY_DEL; break;
347 case K_DOWN: key = VTERM_KEY_DOWN; break;
348 case K_END: key = VTERM_KEY_END; break;
349 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
350 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
351 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
352 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
353 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
354 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
355 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
356 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
357 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
358 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
359 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
360 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
361 case K_HOME: key = VTERM_KEY_HOME; break;
362 case K_INS: key = VTERM_KEY_INS; break;
363 case K_K0: key = VTERM_KEY_KP_0; break;
364 case K_K1: key = VTERM_KEY_KP_1; break;
365 case K_K2: key = VTERM_KEY_KP_2; break;
366 case K_K3: key = VTERM_KEY_KP_3; break;
367 case K_K4: key = VTERM_KEY_KP_4; break;
368 case K_K5: key = VTERM_KEY_KP_5; break;
369 case K_K6: key = VTERM_KEY_KP_6; break;
370 case K_K7: key = VTERM_KEY_KP_7; break;
371 case K_K8: key = VTERM_KEY_KP_8; break;
372 case K_K9: key = VTERM_KEY_KP_9; break;
373 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
374 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
375 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
376 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
377 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
378 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
379 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
380 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
381 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
382 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
383 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
384 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
385 case K_LEFT: key = VTERM_KEY_LEFT; break;
386 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
387 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
388 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
389 case K_UP: key = VTERM_KEY_UP; break;
390 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200391
392 case K_MOUSEUP: /* TODO */ break;
393 case K_MOUSEDOWN: /* TODO */ break;
394 case K_MOUSELEFT: /* TODO */ break;
395 case K_MOUSERIGHT: /* TODO */ break;
396
397 case K_LEFTMOUSE: /* TODO */ break;
398 case K_LEFTMOUSE_NM: /* TODO */ break;
399 case K_LEFTDRAG: /* TODO */ break;
400 case K_LEFTRELEASE: /* TODO */ break;
401 case K_LEFTRELEASE_NM: /* TODO */ break;
402 case K_MIDDLEMOUSE: /* TODO */ break;
403 case K_MIDDLEDRAG: /* TODO */ break;
404 case K_MIDDLERELEASE: /* TODO */ break;
405 case K_RIGHTMOUSE: /* TODO */ break;
406 case K_RIGHTDRAG: /* TODO */ break;
407 case K_RIGHTRELEASE: /* TODO */ break;
408 case K_X1MOUSE: /* TODO */ break;
409 case K_X1DRAG: /* TODO */ break;
410 case K_X1RELEASE: /* TODO */ break;
411 case K_X2MOUSE: /* TODO */ break;
412 case K_X2DRAG: /* TODO */ break;
413 case K_X2RELEASE: /* TODO */ break;
414
415 /* TODO: handle all special keys and modifiers that terminal_loop()
416 * does not handle. */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200417 }
418
419 /*
420 * Convert special keys to vterm keys:
421 * - Write keys to vterm: vterm_keyboard_key()
422 * - Write output to channel.
423 */
424 if (key != VTERM_KEY_NONE)
425 /* Special key, let vterm convert it. */
426 vterm_keyboard_key(vterm, key, mod);
427 else
428 /* Normal character, let vterm convert it. */
429 vterm_keyboard_unichar(vterm, c, mod);
430
431 /* Read back the converted escape sequence. */
432 return vterm_output_read(vterm, buf, KEY_BUF_LEN);
433}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200434
Bram Moolenaar938783d2017-07-16 20:13:26 +0200435/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200436 * Wait for input and send it to the job.
437 * Return when the start of a CTRL-W command is typed or anything else that
438 * should be handled as a Normal mode command.
439 */
440 void
441terminal_loop(void)
442{
443 char buf[KEY_BUF_LEN];
444 int c;
445 size_t len;
446 static int mouse_was_outside = FALSE;
447 int dragging_outside = FALSE;
448
449 for (;;)
450 {
451 /* TODO: skip screen update when handling a sequence of keys. */
452 update_screen(0);
453 setcursor();
454 out_flush();
455 ++no_mapping;
456 ++allow_keys;
457 got_int = FALSE;
458 c = vgetc();
459 --no_mapping;
460 --allow_keys;
461
462 /* Catch keys that need to be handled as in Normal mode. */
463 switch (c)
464 {
465 case Ctrl_W:
466 case NUL:
467 case K_ZERO:
468 stuffcharReadbuff(c);
469 return;
470
471 case K_IGNORE: continue;
472
473 case K_LEFTDRAG:
474 case K_MIDDLEDRAG:
475 case K_RIGHTDRAG:
476 case K_X1DRAG:
477 case K_X2DRAG:
478 dragging_outside = mouse_was_outside;
479 /* FALLTHROUGH */
480 case K_LEFTMOUSE:
481 case K_LEFTMOUSE_NM:
482 case K_LEFTRELEASE:
483 case K_LEFTRELEASE_NM:
484 case K_MIDDLEMOUSE:
485 case K_MIDDLERELEASE:
486 case K_RIGHTMOUSE:
487 case K_RIGHTRELEASE:
488 case K_X1MOUSE:
489 case K_X1RELEASE:
490 case K_X2MOUSE:
491 case K_X2RELEASE:
492 if (mouse_row < W_WINROW(curwin)
493 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
494 || mouse_col < W_WINCOL(curwin)
495 || mouse_col >= W_ENDCOL(curwin)
496 || dragging_outside)
497 {
498 /* click outside the current window */
499 stuffcharReadbuff(c);
500 mouse_was_outside = TRUE;
501 return;
502 }
503 }
504 mouse_was_outside = FALSE;
505
506 /* Convert the typed key to a sequence of bytes for the job. */
507 len = term_convert_key(c, buf);
508 if (len > 0)
509 /* TODO: if FAIL is returned, stop? */
510 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
511 (char_u *)buf, len, NULL);
512 }
513}
514
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200515/*
516 * Called when a job has finished.
517 */
518 void
519term_job_ended(job_T *job)
520{
521 if (curbuf->b_term != NULL && curbuf->b_term->tl_job == job)
522 maketitle();
523}
524
525/*
526 * Return TRUE if the job for "buf" is still running.
527 */
528 int
529term_job_running(buf_T *buf)
530{
531 return buf->b_term != NULL && buf->b_term->tl_job != NULL
532 && buf->b_term->tl_job->jv_status == JOB_STARTED;
533}
534
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200535 static void
536position_cursor(win_T *wp, VTermPos *pos)
537{
538 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
539 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
540}
541
542static int handle_damage(VTermRect rect, void *user);
543static int handle_moverect(VTermRect dest, VTermRect src, void *user);
544static int handle_movecursor(VTermPos pos, VTermPos oldpos, int visible, void *user);
545static int handle_resize(int rows, int cols, void *user);
546
547static VTermScreenCallbacks screen_callbacks = {
548 handle_damage, /* damage */
549 handle_moverect, /* moverect */
550 handle_movecursor, /* movecursor */
551 NULL, /* settermprop */
552 NULL, /* bell */
553 handle_resize, /* resize */
554 NULL, /* sb_pushline */
555 NULL /* sb_popline */
556};
557
558 static int
559handle_damage(VTermRect rect, void *user)
560{
561 term_T *term = (term_T *)user;
562
563 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
564 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
565 redraw_buf_later(term->tl_buffer, NOT_VALID);
566 return 1;
567}
568
569 static int
570handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
571{
572 term_T *term = (term_T *)user;
573
574 /* TODO */
575 redraw_buf_later(term->tl_buffer, NOT_VALID);
576 return 1;
577}
578
579 static int
580handle_movecursor(
581 VTermPos pos,
582 VTermPos oldpos UNUSED,
583 int visible UNUSED,
584 void *user)
585{
586 term_T *term = (term_T *)user;
587 win_T *wp;
588 int is_current = FALSE;
589
590 FOR_ALL_WINDOWS(wp)
591 {
592 if (wp->w_buffer == term->tl_buffer)
593 {
594 position_cursor(wp, &pos);
595 if (wp == curwin)
596 is_current = TRUE;
597 }
598 }
599
600 if (is_current)
601 {
602 setcursor();
603 out_flush();
604 }
605
606 return 1;
607}
608
609/*
610 * The job running in the terminal resized the terminal.
611 */
612 static int
613handle_resize(int rows, int cols, void *user)
614{
615 term_T *term = (term_T *)user;
616 win_T *wp;
617
618 term->tl_rows = rows;
619 term->tl_cols = cols;
620 FOR_ALL_WINDOWS(wp)
621 {
622 if (wp->w_buffer == term->tl_buffer)
623 {
624 win_setheight_win(rows, wp);
625 win_setwidth_win(cols, wp);
626 }
627 }
628
629 redraw_buf_later(term->tl_buffer, NOT_VALID);
630 return 1;
631}
632
633/*
Bram Moolenaareeac6772017-07-23 15:48:37 +0200634 * Reverse engineer the RGB value into a cterm color index.
635 * First color is 1. Return 0 if no match found.
636 */
637 static int
638color2index(VTermColor *color)
639{
640 int red = color->red;
641 int blue = color->blue;
642 int green = color->green;
643
644 if (red == 0)
645 {
646 if (green == 0)
647 {
648 if (blue == 0)
649 return 1; /* black */
650 if (blue == 224)
651 return 5; /* blue */
652 }
653 else if (green == 224)
654 {
655 if (blue == 0)
656 return 3; /* green */
657 if (blue == 224)
658 return 7; /* cyan */
659 }
660 }
661 else if (red == 224)
662 {
663 if (green == 0)
664 {
665 if (blue == 0)
666 return 2; /* red */
667 if (blue == 224)
668 return 6; /* magenta */
669 }
670 else if (green == 224)
671 {
672 if (blue == 0)
673 return 4; /* yellow */
674 if (blue == 224)
675 return 8; /* white */
676 }
677 }
678 else if (red == 128)
679 {
680 if (green == 128 && blue == 128)
681 return 9; /* high intensity bladk */
682 }
683 else if (red == 255)
684 {
685 if (green == 64)
686 {
687 if (blue == 64)
688 return 10; /* high intensity red */
689 if (blue == 255)
690 return 14; /* high intensity magenta */
691 }
692 else if (green == 255)
693 {
694 if (blue == 64)
695 return 12; /* high intensity yellow */
696 if (blue == 255)
697 return 16; /* high intensity white */
698 }
699 }
700 else if (red == 64)
701 {
702 if (green == 64)
703 {
704 if (blue == 255)
705 return 13; /* high intensity blue */
706 }
707 else if (green == 255)
708 {
709 if (blue == 64)
710 return 11; /* high intensity green */
711 if (blue == 255)
712 return 15; /* high intensity cyan */
713 }
714 }
715 if (t_colors >= 256)
716 {
717 if (red == blue && red == green)
718 {
719 /* 24-color greyscale */
720 static int cutoff[23] = {
721 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
722 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
723 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
724 int i;
725
726 for (i = 0; i < 23; ++i)
727 if (red < cutoff[i])
728 return i + 233;
729 return 256;
730 }
731
732 /* 216-color cube */
733 return 17 + ((red + 25) / 0x33) * 36
734 + ((green + 25) / 0x33) * 6
735 + (blue + 25) / 0x33;
736 }
737 return 0;
738}
739
740/*
741 * Convert the attributes of a vterm cell into an attribute index.
742 */
743 static int
744cell2attr(VTermScreenCell *cell)
745{
746 int attr = 0;
747
748 if (cell->attrs.bold)
749 attr |= HL_BOLD;
750 if (cell->attrs.underline)
751 attr |= HL_UNDERLINE;
752 if (cell->attrs.italic)
753 attr |= HL_ITALIC;
754 if (cell->attrs.strike)
755 attr |= HL_STANDOUT;
756 if (cell->attrs.reverse)
757 attr |= HL_INVERSE;
758 if (cell->attrs.strike)
759 attr |= HL_UNDERLINE;
760
761#ifdef FEAT_GUI
762 if (gui.in_use)
763 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +0200764 guicolor_T fg, bg;
765
766 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
767 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
768 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +0200769 }
770 else
771#endif
772#ifdef FEAT_TERMGUICOLORS
773 if (p_tgc)
774 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +0200775 guicolor_T fg, bg;
776
777 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
778 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
779
780 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +0200781 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +0200782 else
Bram Moolenaareeac6772017-07-23 15:48:37 +0200783#endif
784 {
785 return get_cterm_attr_idx(attr, color2index(&cell->fg),
786 color2index(&cell->bg));
787 }
788 return 0;
789}
790
791/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200792 * Called to update the window that contains the terminal.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200793 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200794 void
795term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +0200796{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200797 term_T *term = wp->w_buffer->b_term;
798 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200799 VTermScreen *screen = vterm_obtain_screen(vterm);
Bram Moolenaar58556cd2017-07-20 23:04:46 +0200800 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200801 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200802
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200803 /*
804 * If the window was resized a redraw will be triggered and we get here.
805 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
806 */
807 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
808 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +0200809 {
810 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
811 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
812
813 vterm_set_size(vterm, rows, cols);
814 ch_logn(term->tl_job->jv_channel, "Resizing terminal to %d lines",
815 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200816 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +0200817 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +0200818
819 /* The cursor may have been moved when resizing. */
820 vterm_state_get_cursorpos(state, &pos);
821 position_cursor(wp, &pos);
822
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200823 /* TODO: Only redraw what changed. */
824 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +0200825 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200826 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200827 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +0200828
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200829 if (pos.row < term->tl_rows)
830 {
831 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200832 {
833 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200834 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200835
Bram Moolenaareeac6772017-07-23 15:48:37 +0200836 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
837 vim_memset(&cell, 0, sizeof(cell));
838
839 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200840 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200841 if (c == NUL)
842 {
843 ScreenLines[off] = ' ';
844 ScreenLinesUC[off] = NUL;
845 }
846 else
847 {
848#if defined(FEAT_MBYTE)
849 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200850 {
851 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200852 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200853 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200854 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200855 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200856 ScreenLines[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200857 ScreenLinesUC[off] = NUL;
858 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200859#else
860 ScreenLines[off] = c;
861#endif
862 }
Bram Moolenaareeac6772017-07-23 15:48:37 +0200863 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200864
865 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200866 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200867 if (cell.width == 2)
868 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200869 ScreenLines[off] = NUL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200870 ScreenLinesUC[off] = NUL;
871 ++pos.col;
872 ++off;
873 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200874 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200875 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200876 else
877 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200878
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200879 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
880 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +0200881 }
882}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200883
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200884/*
885 * Set job options common for Unix and MS-Windows.
886 */
887 static void
888setup_job_options(jobopt_T *opt, int rows, int cols)
889{
890 clear_job_options(opt);
891 opt->jo_mode = MODE_RAW;
892 opt->jo_out_mode = MODE_RAW;
893 opt->jo_err_mode = MODE_RAW;
894 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200895
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200896 opt->jo_io[PART_OUT] = JIO_BUFFER;
897 opt->jo_io[PART_ERR] = JIO_BUFFER;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200898 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
899
900 opt->jo_modifiable[PART_OUT] = 0;
901 opt->jo_modifiable[PART_ERR] = 0;
902 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
903
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200904 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
905 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +0200906 opt->jo_pty = TRUE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200907 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
908
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200909 opt->jo_term_rows = rows;
910 opt->jo_term_cols = cols;
911}
912
913/*
914 * Create a new vterm and initialize it.
915 */
916 static void
917create_vterm(term_T *term, int rows, int cols)
918{
919 VTerm *vterm;
920 VTermScreen *screen;
921
922 vterm = vterm_new(rows, cols);
923 term->tl_vterm = vterm;
924 screen = vterm_obtain_screen(vterm);
925 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
926 /* TODO: depends on 'encoding'. */
927 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +0200928
929 /* Vterm uses a default black background. Set it to white when
930 * 'background' is "light". */
931 if (*p_bg == 'l')
932 {
933 VTermColor fg, bg;
934
935 fg.red = fg.green = fg.blue = 0;
936 bg.red = bg.green = bg.blue = 255;
937 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
938 }
939
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200940 /* Required to initialize most things. */
941 vterm_screen_reset(screen, 1 /* hard */);
942}
943
944# ifdef WIN3264
945
946#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
947#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
948
949void* (*winpty_config_new)(int, void*);
950void* (*winpty_open)(void*, void*);
951void* (*winpty_spawn_config_new)(int, void*, LPCWSTR, void*, void*, void*);
952BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
953void (*winpty_config_set_initial_size)(void*, int, int);
954LPCWSTR (*winpty_conin_name)(void*);
955LPCWSTR (*winpty_conout_name)(void*);
956LPCWSTR (*winpty_conerr_name)(void*);
957void (*winpty_free)(void*);
958void (*winpty_config_free)(void*);
959void (*winpty_spawn_config_free)(void*);
960void (*winpty_error_free)(void*);
961LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200962BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200963
964/**************************************
965 * 2. MS-Windows implementation.
966 */
967
968#define WINPTY_DLL "winpty.dll"
969
970static HINSTANCE hWinPtyDLL = NULL;
971
972 int
973dyn_winpty_init(void)
974{
975 int i;
976 static struct
977 {
978 char *name;
979 FARPROC *ptr;
980 } winpty_entry[] =
981 {
982 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
983 {"winpty_config_free", (FARPROC*)&winpty_config_free},
984 {"winpty_config_new", (FARPROC*)&winpty_config_new},
985 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
986 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
987 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
988 {"winpty_error_free", (FARPROC*)&winpty_error_free},
989 {"winpty_free", (FARPROC*)&winpty_free},
990 {"winpty_open", (FARPROC*)&winpty_open},
991 {"winpty_spawn", (FARPROC*)&winpty_spawn},
992 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
993 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
994 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200995 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200996 {NULL, NULL}
997 };
998
999 /* No need to initialize twice. */
1000 if (hWinPtyDLL)
1001 return 1;
1002 /* Load winpty.dll */
1003 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
1004 if (!hWinPtyDLL)
1005 {
1006 EMSG2(_(e_loadlib), WINPTY_DLL);
1007 return 0;
1008 }
1009 for (i = 0; winpty_entry[i].name != NULL
1010 && winpty_entry[i].ptr != NULL; ++i)
1011 {
1012 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
1013 winpty_entry[i].name)) == NULL)
1014 {
1015 EMSG2(_(e_loadfunc), winpty_entry[i].name);
1016 return 0;
1017 }
1018 }
1019
1020 return 1;
1021}
1022
1023/*
1024 * Create a new terminal of "rows" by "cols" cells.
1025 * Store a reference in "term".
1026 * Return OK or FAIL.
1027 */
1028 static int
1029term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
1030{
1031 WCHAR *p = enc_to_utf16(cmd, NULL);
1032 channel_T *channel = NULL;
1033 job_T *job = NULL;
1034 jobopt_T opt;
1035 DWORD error;
1036 HANDLE jo = NULL, child_process_handle, child_thread_handle;
1037 void *winpty_err;
1038 void *spawn_config;
1039
1040 if (!dyn_winpty_init())
1041 return FAIL;
1042
1043 if (p == NULL)
1044 return FAIL;
1045
1046 job = job_alloc();
1047 if (job == NULL)
1048 goto failed;
1049
1050 channel = add_channel();
1051 if (channel == NULL)
1052 goto failed;
1053
1054 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
1055 if (term->tl_winpty_config == NULL)
1056 goto failed;
1057
1058 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
1059 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
1060 if (term->tl_winpty == NULL)
1061 goto failed;
1062
1063 spawn_config = winpty_spawn_config_new(
1064 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
1065 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
1066 NULL,
1067 p,
1068 NULL,
1069 NULL,
1070 &winpty_err);
1071 if (spawn_config == NULL)
1072 goto failed;
1073
1074 channel = add_channel();
1075 if (channel == NULL)
1076 goto failed;
1077
1078 job = job_alloc();
1079 if (job == NULL)
1080 goto failed;
1081
1082 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
1083 &child_thread_handle, &error, &winpty_err))
1084 goto failed;
1085
1086 channel_set_pipes(channel,
1087 (sock_T) CreateFileW(
1088 winpty_conin_name(term->tl_winpty),
1089 GENERIC_WRITE, 0, NULL,
1090 OPEN_EXISTING, 0, NULL),
1091 (sock_T) CreateFileW(
1092 winpty_conout_name(term->tl_winpty),
1093 GENERIC_READ, 0, NULL,
1094 OPEN_EXISTING, 0, NULL),
1095 (sock_T) CreateFileW(
1096 winpty_conerr_name(term->tl_winpty),
1097 GENERIC_READ, 0, NULL,
1098 OPEN_EXISTING, 0, NULL));
1099
1100 jo = CreateJobObject(NULL, NULL);
1101 if (jo == NULL)
1102 goto failed;
1103
1104 if (!AssignProcessToJobObject(jo, child_process_handle))
1105 goto failed;
1106
1107 winpty_spawn_config_free(spawn_config);
1108
1109 create_vterm(term, rows, cols);
1110
1111 setup_job_options(&opt, rows, cols);
1112 channel_set_job(channel, job, &opt);
1113
1114 job->jv_channel = channel;
1115 job->jv_proc_info.hProcess = child_process_handle;
1116 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
1117 job->jv_job_object = jo;
1118 job->jv_status = JOB_STARTED;
1119 term->tl_job = job;
1120
1121 return OK;
1122
1123failed:
1124 if (channel != NULL)
1125 channel_clear(channel);
1126 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001127 {
1128 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001129 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001130 }
1131 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001132 if (jo != NULL)
1133 CloseHandle(jo);
1134 if (term->tl_winpty != NULL)
1135 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001136 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001137 if (term->tl_winpty_config != NULL)
1138 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001139 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001140 if (winpty_err != NULL)
1141 {
1142 char_u *msg = utf16_to_enc(
1143 (short_u *)winpty_error_msg(winpty_err), NULL);
1144
1145 EMSG(msg);
1146 winpty_error_free(winpty_err);
1147 }
1148 return FAIL;
1149}
1150
1151/*
1152 * Free the terminal emulator part of "term".
1153 */
1154 static void
1155term_free(term_T *term)
1156{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001157 if (term->tl_winpty != NULL)
1158 winpty_free(term->tl_winpty);
1159 if (term->tl_winpty_config != NULL)
1160 winpty_config_free(term->tl_winpty_config);
1161 if (term->tl_vterm != NULL)
1162 vterm_free(term->tl_vterm);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001163}
1164
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001165/*
1166 * Request size to terminal.
1167 */
1168 static void
1169term_report_winsize(term_T *term, int rows, int cols)
1170{
1171 winpty_set_size(term->tl_winpty, cols, rows, NULL);
1172}
1173
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001174# else
1175
1176/**************************************
1177 * 3. Unix-like implementation.
1178 */
1179
1180/*
1181 * Create a new terminal of "rows" by "cols" cells.
1182 * Start job for "cmd".
1183 * Store the pointers in "term".
1184 * Return OK or FAIL.
1185 */
1186 static int
1187term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
1188{
1189 typval_T argvars[2];
1190 jobopt_T opt;
1191
1192 create_vterm(term, rows, cols);
1193
1194 argvars[0].v_type = VAR_STRING;
1195 argvars[0].vval.v_string = cmd;
1196 setup_job_options(&opt, rows, cols);
1197 term->tl_job = job_start(argvars, &opt);
1198
Bram Moolenaar61a66052017-07-22 18:39:00 +02001199 return term->tl_job != NULL
1200 && term->tl_job->jv_channel != NULL
1201 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001202}
1203
1204/*
1205 * Free the terminal emulator part of "term".
1206 */
1207 static void
1208term_free(term_T *term)
1209{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001210 if (term->tl_vterm != NULL)
1211 vterm_free(term->tl_vterm);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001212}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001213
1214/*
1215 * Request size to terminal.
1216 */
1217 static void
1218term_report_winsize(term_T *term, int rows, int cols)
1219{
1220 /* Use an ioctl() to report the new window size to the job. */
1221 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
1222 {
1223 int fd = -1;
1224 int part;
1225
1226 for (part = PART_OUT; part < PART_COUNT; ++part)
1227 {
1228 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
1229 if (isatty(fd))
1230 break;
1231 }
1232 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
1233 mch_stop_job(term->tl_job, (char_u *)"winch");
1234 }
1235}
1236
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001237# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001238
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001239#endif /* FEAT_TERMINAL */