blob: f75ac38c3fae6999fea973473970c29b90088aa4 [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 Moolenaar1c844932017-07-24 23:36:41 +020036 * - include functions from #1871
37 * - do not store terminal buffer in viminfo. Or prefix term:// ?
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 Moolenaar21554412017-07-24 21:44:43 +020046 * - Need an option or argument to drop the window+buffer right away, to be
47 * used for a shell or Vim.
48 * - add a character in :ls output
Bram Moolenaar938783d2017-07-16 20:13:26 +020049 * - when closing window and job has not ended, make terminal hidden?
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +020050 * - don't allow exiting Vim when a terminal is still running a job
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020051 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar938783d2017-07-16 20:13:26 +020052 * - command line completion for :terminal
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020053 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020054 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020055 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020056 * - implement "term" for job_start(): more job options when starting a
57 * terminal.
Bram Moolenaar96ca27a2017-07-17 23:20:24 +020058 * - implement term_list() list of buffers with a terminal
59 * - implement term_getsize(buf)
60 * - implement term_setsize(buf)
61 * - implement term_sendkeys(buf, keys) send keystrokes to a terminal
62 * - implement term_wait(buf) wait for screen to be updated
63 * - implement term_scrape(buf, row) inspect terminal screen
64 * - implement term_open(command, options) open terminal window
65 * - implement term_getjob(buf)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020066 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
67 * conversions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020068 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020069 */
70
71#include "vim.h"
72
73#ifdef FEAT_TERMINAL
74
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020075#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020076# define MIN(x,y) (x < y ? x : y)
77# define MAX(x,y) (x > y ? x : y)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020078#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020079
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020080#include "libvterm/include/vterm.h"
81
Bram Moolenaare4f25e42017-07-07 11:54:15 +020082/* typedef term_T in structs.h */
83struct terminal_S {
84 term_T *tl_next;
85
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020086#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020087 void *tl_winpty_config;
88 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020089#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020090 VTerm *tl_vterm;
Bram Moolenaare4f25e42017-07-07 11:54:15 +020091 job_T *tl_job;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +020092 buf_T *tl_buffer;
Bram Moolenaare4f25e42017-07-07 11:54:15 +020093
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020094 /* last known vterm size */
95 int tl_rows;
96 int tl_cols;
97 /* vterm size does not follow window size */
98 int tl_rows_fixed;
99 int tl_cols_fixed;
100
Bram Moolenaar21554412017-07-24 21:44:43 +0200101 char_u *tl_title; /* NULL or allocated */
102 char_u *tl_status_text; /* NULL or allocated */
103
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200104 /* Range of screen rows to update. Zero based. */
105 int tl_dirty_row_start; /* -1 if nothing dirty */
106 int tl_dirty_row_end; /* row below last one to update */
107
108 pos_T tl_cursor;
109};
110
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200111/*
112 * List of all active terminals.
113 */
114static term_T *first_term = NULL;
115
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200116
117#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
118#define KEY_BUF_LEN 200
119
120/*
121 * Functions with separate implementation for MS-Windows and Unix-like systems.
122 */
123static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200124static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200125static void term_free(term_T *term);
126
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200127/**************************************
128 * 1. Generic code for all systems.
129 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200130
131/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200132 * Determine the terminal size from 'termsize' and the current window.
133 * Assumes term->tl_rows and term->tl_cols are zero.
134 */
135 static void
136set_term_and_win_size(term_T *term)
137{
138 if (*curwin->w_p_tms != NUL)
139 {
140 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
141
142 term->tl_rows = atoi((char *)curwin->w_p_tms);
143 term->tl_cols = atoi((char *)p);
144 }
145 if (term->tl_rows == 0)
146 term->tl_rows = curwin->w_height;
147 else
148 {
149 win_setheight_win(term->tl_rows, curwin);
150 term->tl_rows_fixed = TRUE;
151 }
152 if (term->tl_cols == 0)
153 term->tl_cols = curwin->w_width;
154 else
155 {
156 win_setwidth_win(term->tl_cols, curwin);
157 term->tl_cols_fixed = TRUE;
158 }
159}
160
161/*
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200162 * ":terminal": open a terminal window and execute a job in it.
163 */
164 void
165ex_terminal(exarg_T *eap)
166{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200167 exarg_T split_ea;
168 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200169 term_T *term;
Bram Moolenaare173fd02017-07-22 19:03:32 +0200170 char_u *cmd = eap->arg;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200171
172 if (check_restricted() || check_secure())
173 return;
174
175 term = (term_T *)alloc_clear(sizeof(term_T));
176 if (term == NULL)
177 return;
178 term->tl_dirty_row_end = MAX_ROW;
179
180 /* Open a new window or tab. */
181 vim_memset(&split_ea, 0, sizeof(split_ea));
182 split_ea.cmdidx = CMD_new;
183 split_ea.cmd = (char_u *)"new";
184 split_ea.arg = (char_u *)"";
185 ex_splitview(&split_ea);
186 if (curwin == old_curwin)
187 {
188 /* split failed */
189 vim_free(term);
190 return;
191 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200192 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200193 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200194
195 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200196 term->tl_next = first_term;
197 first_term = term;
198
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200199 if (buflist_findname(cmd) == NULL)
200 curbuf->b_ffname = vim_strsave(cmd);
201 else
202 {
203 int i;
204 size_t len = STRLEN(cmd) + 10;
205 char_u *p = alloc(len);
206
207 for (i = 1; p != NULL; ++i)
208 {
209 vim_snprintf((char *)p, len, "%s (%d)", cmd, i);
210 if (buflist_findname(p) == NULL)
211 {
212 curbuf->b_ffname = p;
213 break;
214 }
215 }
216 }
217 curbuf->b_fname = curbuf->b_ffname;
218
219 /* Mark the buffer as changed, so that it's not easy to abandon the job. */
220 curbuf->b_changed = TRUE;
221 curbuf->b_p_ma = FALSE;
222 set_string_option_direct((char_u *)"buftype", -1,
223 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200224
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200225 set_term_and_win_size(term);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200226
Bram Moolenaare173fd02017-07-22 19:03:32 +0200227 if (cmd == NULL || *cmd == NUL)
228 cmd = p_sh;
229
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200230 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaare173fd02017-07-22 19:03:32 +0200231 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200232 {
233 /* store the size we ended up with */
234 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200235 }
236 else
237 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200238 free_terminal(term);
239 curbuf->b_term = NULL;
240
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200241 /* Wiping out the buffer will also close the window and call
242 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200243 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200244 }
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200245
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200246 /* TODO: Setup pty, see mch_call_shell(). */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200247}
248
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200249/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200250 * Free a terminal and everything it refers to.
251 * Kills the job if there is one.
252 * Called when wiping out a buffer.
253 */
254 void
255free_terminal(term_T *term)
256{
257 term_T *tp;
258
259 if (term == NULL)
260 return;
261 if (first_term == term)
262 first_term = term->tl_next;
263 else
264 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
265 if (tp->tl_next == term)
266 {
267 tp->tl_next = term->tl_next;
268 break;
269 }
270
271 if (term->tl_job != NULL)
272 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200273 if (term->tl_job->jv_status != JOB_ENDED
274 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200275 job_stop(term->tl_job, NULL, "kill");
276 job_unref(term->tl_job);
277 }
278
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200279 term_free(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200280 vim_free(term->tl_title);
281 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200282 vim_free(term);
283}
284
285/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200286 * Write job output "msg[len]" to the vterm.
287 */
288 static void
289term_write_job_output(term_T *term, char_u *msg, size_t len)
290{
291 VTerm *vterm = term->tl_vterm;
292 char_u *p;
293 size_t done;
294 size_t len_now;
295
296 for (done = 0; done < len; done += len_now)
297 {
298 for (p = msg + done; p < msg + len; )
299 {
300 if (*p == NL)
301 break;
302 p += utf_ptr2len_len(p, len - (p - msg));
303 }
304 len_now = p - msg - done;
305 vterm_input_write(vterm, (char *)msg + done, len_now);
306 if (p < msg + len && *p == NL)
307 {
308 /* Convert NL to CR-NL, that appears to work best. */
309 vterm_input_write(vterm, "\r\n", 2);
310 ++len_now;
311 }
312 }
313
314 /* this invokes the damage callbacks */
315 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
316}
317
Bram Moolenaar1c844932017-07-24 23:36:41 +0200318 static void
319update_cursor()
320{
321 /* TODO: this should not always be needed */
322 setcursor();
323 out_flush();
324#ifdef FEAT_GUI
325 if (gui.in_use)
326 gui_update_cursor(FALSE, FALSE);
327#endif
328}
329
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200330/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200331 * Invoked when "msg" output from a job was received. Write it to the terminal
332 * of "buffer".
333 */
334 void
335write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
336{
337 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200338 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200339
340 ch_logn(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200341 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200342
343 /* TODO: only update once in a while. */
344 update_screen(0);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200345 update_cursor();
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200346}
347
348/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200349 * Convert typed key "c" into bytes to send to the job.
350 * Return the number of bytes in "buf".
351 */
352 static int
353term_convert_key(int c, char *buf)
354{
355 VTerm *vterm = curbuf->b_term->tl_vterm;
356 VTermKey key = VTERM_KEY_NONE;
357 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200358
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200359 switch (c)
360 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200361 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200362 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200363 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
364 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200365 case K_DEL: key = VTERM_KEY_DEL; break;
366 case K_DOWN: key = VTERM_KEY_DOWN; break;
367 case K_END: key = VTERM_KEY_END; break;
368 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
369 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
370 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
371 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
372 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
373 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
374 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
375 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
376 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
377 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
378 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
379 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
380 case K_HOME: key = VTERM_KEY_HOME; break;
381 case K_INS: key = VTERM_KEY_INS; break;
382 case K_K0: key = VTERM_KEY_KP_0; break;
383 case K_K1: key = VTERM_KEY_KP_1; break;
384 case K_K2: key = VTERM_KEY_KP_2; break;
385 case K_K3: key = VTERM_KEY_KP_3; break;
386 case K_K4: key = VTERM_KEY_KP_4; break;
387 case K_K5: key = VTERM_KEY_KP_5; break;
388 case K_K6: key = VTERM_KEY_KP_6; break;
389 case K_K7: key = VTERM_KEY_KP_7; break;
390 case K_K8: key = VTERM_KEY_KP_8; break;
391 case K_K9: key = VTERM_KEY_KP_9; break;
392 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
393 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
394 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
395 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
396 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
397 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
398 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
399 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
400 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
401 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
402 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
403 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
404 case K_LEFT: key = VTERM_KEY_LEFT; break;
405 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
406 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
407 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
408 case K_UP: key = VTERM_KEY_UP; break;
409 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200410
411 case K_MOUSEUP: /* TODO */ break;
412 case K_MOUSEDOWN: /* TODO */ break;
413 case K_MOUSELEFT: /* TODO */ break;
414 case K_MOUSERIGHT: /* TODO */ break;
415
416 case K_LEFTMOUSE: /* TODO */ break;
417 case K_LEFTMOUSE_NM: /* TODO */ break;
418 case K_LEFTDRAG: /* TODO */ break;
419 case K_LEFTRELEASE: /* TODO */ break;
420 case K_LEFTRELEASE_NM: /* TODO */ break;
421 case K_MIDDLEMOUSE: /* TODO */ break;
422 case K_MIDDLEDRAG: /* TODO */ break;
423 case K_MIDDLERELEASE: /* TODO */ break;
424 case K_RIGHTMOUSE: /* TODO */ break;
425 case K_RIGHTDRAG: /* TODO */ break;
426 case K_RIGHTRELEASE: /* TODO */ break;
427 case K_X1MOUSE: /* TODO */ break;
428 case K_X1DRAG: /* TODO */ break;
429 case K_X1RELEASE: /* TODO */ break;
430 case K_X2MOUSE: /* TODO */ break;
431 case K_X2DRAG: /* TODO */ break;
432 case K_X2RELEASE: /* TODO */ break;
433
434 /* TODO: handle all special keys and modifiers that terminal_loop()
435 * does not handle. */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200436 }
437
438 /*
439 * Convert special keys to vterm keys:
440 * - Write keys to vterm: vterm_keyboard_key()
441 * - Write output to channel.
442 */
443 if (key != VTERM_KEY_NONE)
444 /* Special key, let vterm convert it. */
445 vterm_keyboard_key(vterm, key, mod);
446 else
447 /* Normal character, let vterm convert it. */
448 vterm_keyboard_unichar(vterm, c, mod);
449
450 /* Read back the converted escape sequence. */
451 return vterm_output_read(vterm, buf, KEY_BUF_LEN);
452}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200453
Bram Moolenaar938783d2017-07-16 20:13:26 +0200454/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200455 * Wait for input and send it to the job.
456 * Return when the start of a CTRL-W command is typed or anything else that
457 * should be handled as a Normal mode command.
458 */
459 void
460terminal_loop(void)
461{
462 char buf[KEY_BUF_LEN];
463 int c;
464 size_t len;
465 static int mouse_was_outside = FALSE;
466 int dragging_outside = FALSE;
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200467 int termkey = 0;
468
469 if (*curwin->w_p_tk != NUL)
470 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200471
472 for (;;)
473 {
474 /* TODO: skip screen update when handling a sequence of keys. */
475 update_screen(0);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200476 update_cursor();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200477 ++no_mapping;
478 ++allow_keys;
479 got_int = FALSE;
480 c = vgetc();
481 --no_mapping;
482 --allow_keys;
483
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200484 if (c == (termkey == 0 ? Ctrl_W : termkey))
485 {
486 stuffcharReadbuff(Ctrl_W);
487 return;
488 }
489
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200490 /* Catch keys that need to be handled as in Normal mode. */
491 switch (c)
492 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200493 case NUL:
494 case K_ZERO:
495 stuffcharReadbuff(c);
496 return;
497
498 case K_IGNORE: continue;
499
500 case K_LEFTDRAG:
501 case K_MIDDLEDRAG:
502 case K_RIGHTDRAG:
503 case K_X1DRAG:
504 case K_X2DRAG:
505 dragging_outside = mouse_was_outside;
506 /* FALLTHROUGH */
507 case K_LEFTMOUSE:
508 case K_LEFTMOUSE_NM:
509 case K_LEFTRELEASE:
510 case K_LEFTRELEASE_NM:
511 case K_MIDDLEMOUSE:
512 case K_MIDDLERELEASE:
513 case K_RIGHTMOUSE:
514 case K_RIGHTRELEASE:
515 case K_X1MOUSE:
516 case K_X1RELEASE:
517 case K_X2MOUSE:
518 case K_X2RELEASE:
519 if (mouse_row < W_WINROW(curwin)
520 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
521 || mouse_col < W_WINCOL(curwin)
522 || mouse_col >= W_ENDCOL(curwin)
523 || dragging_outside)
524 {
525 /* click outside the current window */
526 stuffcharReadbuff(c);
527 mouse_was_outside = TRUE;
528 return;
529 }
530 }
531 mouse_was_outside = FALSE;
532
533 /* Convert the typed key to a sequence of bytes for the job. */
534 len = term_convert_key(c, buf);
535 if (len > 0)
536 /* TODO: if FAIL is returned, stop? */
537 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
538 (char_u *)buf, len, NULL);
539 }
540}
541
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200542/*
543 * Called when a job has finished.
544 */
545 void
546term_job_ended(job_T *job)
547{
Bram Moolenaar21554412017-07-24 21:44:43 +0200548 term_T *term;
549 int did_one = FALSE;
550
551 for (term = first_term; term != NULL; term = term->tl_next)
552 if (term->tl_job == job)
553 {
554 vim_free(term->tl_title);
555 term->tl_title = NULL;
556 vim_free(term->tl_status_text);
557 term->tl_status_text = NULL;
558 redraw_buf_and_status_later(term->tl_buffer, VALID);
559 did_one = TRUE;
560 }
561 if (did_one)
562 {
563 redraw_statuslines();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200564 update_cursor();
Bram Moolenaar21554412017-07-24 21:44:43 +0200565 }
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200566 if (curbuf->b_term != NULL && curbuf->b_term->tl_job == job)
567 maketitle();
568}
569
570/*
571 * Return TRUE if the job for "buf" is still running.
572 */
Bram Moolenaar21554412017-07-24 21:44:43 +0200573 static int
574term_job_running(term_T *term)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200575{
Bram Moolenaar21554412017-07-24 21:44:43 +0200576 return term->tl_job != NULL && term->tl_job->jv_status == JOB_STARTED;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200577}
578
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200579 static void
580position_cursor(win_T *wp, VTermPos *pos)
581{
582 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
583 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
584}
585
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200586 static int
587handle_damage(VTermRect rect, void *user)
588{
589 term_T *term = (term_T *)user;
590
591 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
592 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
593 redraw_buf_later(term->tl_buffer, NOT_VALID);
594 return 1;
595}
596
597 static int
598handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
599{
600 term_T *term = (term_T *)user;
601
602 /* TODO */
603 redraw_buf_later(term->tl_buffer, NOT_VALID);
604 return 1;
605}
606
607 static int
608handle_movecursor(
609 VTermPos pos,
610 VTermPos oldpos UNUSED,
611 int visible UNUSED,
612 void *user)
613{
614 term_T *term = (term_T *)user;
615 win_T *wp;
616 int is_current = FALSE;
617
618 FOR_ALL_WINDOWS(wp)
619 {
620 if (wp->w_buffer == term->tl_buffer)
621 {
622 position_cursor(wp, &pos);
623 if (wp == curwin)
624 is_current = TRUE;
625 }
626 }
627
628 if (is_current)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200629 update_cursor();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200630
631 return 1;
632}
633
Bram Moolenaar21554412017-07-24 21:44:43 +0200634 static int
635handle_settermprop(
636 VTermProp prop,
637 VTermValue *value,
638 void *user)
639{
640 term_T *term = (term_T *)user;
641
642 switch (prop)
643 {
644 case VTERM_PROP_TITLE:
645 vim_free(term->tl_title);
646 term->tl_title = vim_strsave((char_u *)value->string);
647 vim_free(term->tl_status_text);
648 term->tl_status_text = NULL;
649 if (term == curbuf->b_term)
650 maketitle();
651 return 1;
652 default:
653 break;
654 }
655 return 0;
656}
657
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200658/*
659 * The job running in the terminal resized the terminal.
660 */
661 static int
662handle_resize(int rows, int cols, void *user)
663{
664 term_T *term = (term_T *)user;
665 win_T *wp;
666
667 term->tl_rows = rows;
668 term->tl_cols = cols;
669 FOR_ALL_WINDOWS(wp)
670 {
671 if (wp->w_buffer == term->tl_buffer)
672 {
673 win_setheight_win(rows, wp);
674 win_setwidth_win(cols, wp);
675 }
676 }
677
678 redraw_buf_later(term->tl_buffer, NOT_VALID);
679 return 1;
680}
681
Bram Moolenaar21554412017-07-24 21:44:43 +0200682static VTermScreenCallbacks screen_callbacks = {
683 handle_damage, /* damage */
684 handle_moverect, /* moverect */
685 handle_movecursor, /* movecursor */
686 handle_settermprop, /* settermprop */
687 NULL, /* bell */
688 handle_resize, /* resize */
689 NULL, /* sb_pushline */
690 NULL /* sb_popline */
691};
692
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200693/*
Bram Moolenaareeac6772017-07-23 15:48:37 +0200694 * Reverse engineer the RGB value into a cterm color index.
695 * First color is 1. Return 0 if no match found.
696 */
697 static int
698color2index(VTermColor *color)
699{
700 int red = color->red;
701 int blue = color->blue;
702 int green = color->green;
703
704 if (red == 0)
705 {
706 if (green == 0)
707 {
708 if (blue == 0)
709 return 1; /* black */
710 if (blue == 224)
711 return 5; /* blue */
712 }
713 else if (green == 224)
714 {
715 if (blue == 0)
716 return 3; /* green */
717 if (blue == 224)
718 return 7; /* cyan */
719 }
720 }
721 else if (red == 224)
722 {
723 if (green == 0)
724 {
725 if (blue == 0)
726 return 2; /* red */
727 if (blue == 224)
728 return 6; /* magenta */
729 }
730 else if (green == 224)
731 {
732 if (blue == 0)
733 return 4; /* yellow */
734 if (blue == 224)
735 return 8; /* white */
736 }
737 }
738 else if (red == 128)
739 {
740 if (green == 128 && blue == 128)
Bram Moolenaar8a773062017-07-24 22:29:21 +0200741 return 9; /* high intensity black */
Bram Moolenaareeac6772017-07-23 15:48:37 +0200742 }
743 else if (red == 255)
744 {
745 if (green == 64)
746 {
747 if (blue == 64)
748 return 10; /* high intensity red */
749 if (blue == 255)
750 return 14; /* high intensity magenta */
751 }
752 else if (green == 255)
753 {
754 if (blue == 64)
755 return 12; /* high intensity yellow */
756 if (blue == 255)
757 return 16; /* high intensity white */
758 }
759 }
760 else if (red == 64)
761 {
762 if (green == 64)
763 {
764 if (blue == 255)
765 return 13; /* high intensity blue */
766 }
767 else if (green == 255)
768 {
769 if (blue == 64)
770 return 11; /* high intensity green */
771 if (blue == 255)
772 return 15; /* high intensity cyan */
773 }
774 }
775 if (t_colors >= 256)
776 {
777 if (red == blue && red == green)
778 {
779 /* 24-color greyscale */
780 static int cutoff[23] = {
781 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
782 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
783 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
784 int i;
785
786 for (i = 0; i < 23; ++i)
787 if (red < cutoff[i])
788 return i + 233;
789 return 256;
790 }
791
792 /* 216-color cube */
793 return 17 + ((red + 25) / 0x33) * 36
794 + ((green + 25) / 0x33) * 6
795 + (blue + 25) / 0x33;
796 }
797 return 0;
798}
799
800/*
801 * Convert the attributes of a vterm cell into an attribute index.
802 */
803 static int
804cell2attr(VTermScreenCell *cell)
805{
806 int attr = 0;
807
808 if (cell->attrs.bold)
809 attr |= HL_BOLD;
810 if (cell->attrs.underline)
811 attr |= HL_UNDERLINE;
812 if (cell->attrs.italic)
813 attr |= HL_ITALIC;
814 if (cell->attrs.strike)
815 attr |= HL_STANDOUT;
816 if (cell->attrs.reverse)
817 attr |= HL_INVERSE;
818 if (cell->attrs.strike)
819 attr |= HL_UNDERLINE;
820
821#ifdef FEAT_GUI
822 if (gui.in_use)
823 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +0200824 guicolor_T fg, bg;
825
826 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
827 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
828 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +0200829 }
830 else
831#endif
832#ifdef FEAT_TERMGUICOLORS
833 if (p_tgc)
834 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +0200835 guicolor_T fg, bg;
836
837 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
838 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
839
840 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +0200841 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +0200842 else
Bram Moolenaareeac6772017-07-23 15:48:37 +0200843#endif
844 {
845 return get_cterm_attr_idx(attr, color2index(&cell->fg),
846 color2index(&cell->bg));
847 }
848 return 0;
849}
850
851/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200852 * Called to update the window that contains the terminal.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200853 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200854 void
855term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +0200856{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200857 term_T *term = wp->w_buffer->b_term;
858 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200859 VTermScreen *screen = vterm_obtain_screen(vterm);
Bram Moolenaar58556cd2017-07-20 23:04:46 +0200860 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200861 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200862
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200863 /*
864 * If the window was resized a redraw will be triggered and we get here.
865 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
866 */
867 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
868 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +0200869 {
870 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
871 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
872
873 vterm_set_size(vterm, rows, cols);
874 ch_logn(term->tl_job->jv_channel, "Resizing terminal to %d lines",
875 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200876 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +0200877 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +0200878
879 /* The cursor may have been moved when resizing. */
880 vterm_state_get_cursorpos(state, &pos);
881 position_cursor(wp, &pos);
882
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200883 /* TODO: Only redraw what changed. */
884 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +0200885 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200886 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200887 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +0200888
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200889 if (pos.row < term->tl_rows)
890 {
891 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200892 {
893 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200894 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200895
Bram Moolenaareeac6772017-07-23 15:48:37 +0200896 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
897 vim_memset(&cell, 0, sizeof(cell));
898
899 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200900 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200901 if (c == NUL)
902 {
903 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +0200904#if defined(FEAT_MBYTE)
905 if (enc_utf8)
906 ScreenLinesUC[off] = NUL;
907#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200908 }
909 else
910 {
911#if defined(FEAT_MBYTE)
912 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200913 {
914 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200915 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200916 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200917 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200918 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200919 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +0200920 if (enc_utf8)
921 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200922 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200923#else
924 ScreenLines[off] = c;
925#endif
926 }
Bram Moolenaareeac6772017-07-23 15:48:37 +0200927 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200928
929 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200930 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200931 if (cell.width == 2)
932 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200933 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +0200934#if defined(FEAT_MBYTE)
935 if (enc_utf8)
936 ScreenLinesUC[off] = NUL;
937#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200938 ++pos.col;
939 ++off;
940 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200941 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200942 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200943 else
944 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200945
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200946 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
947 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +0200948 }
949}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200950
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200951/*
952 * Set job options common for Unix and MS-Windows.
953 */
954 static void
955setup_job_options(jobopt_T *opt, int rows, int cols)
956{
957 clear_job_options(opt);
958 opt->jo_mode = MODE_RAW;
959 opt->jo_out_mode = MODE_RAW;
960 opt->jo_err_mode = MODE_RAW;
961 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200962
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200963 opt->jo_io[PART_OUT] = JIO_BUFFER;
964 opt->jo_io[PART_ERR] = JIO_BUFFER;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200965 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
966
967 opt->jo_modifiable[PART_OUT] = 0;
968 opt->jo_modifiable[PART_ERR] = 0;
969 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
970
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200971 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
972 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +0200973 opt->jo_pty = TRUE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200974 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
975
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200976 opt->jo_term_rows = rows;
977 opt->jo_term_cols = cols;
978}
979
980/*
981 * Create a new vterm and initialize it.
982 */
983 static void
984create_vterm(term_T *term, int rows, int cols)
985{
986 VTerm *vterm;
987 VTermScreen *screen;
988
989 vterm = vterm_new(rows, cols);
990 term->tl_vterm = vterm;
991 screen = vterm_obtain_screen(vterm);
992 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
993 /* TODO: depends on 'encoding'. */
994 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +0200995
996 /* Vterm uses a default black background. Set it to white when
997 * 'background' is "light". */
998 if (*p_bg == 'l')
999 {
1000 VTermColor fg, bg;
1001
1002 fg.red = fg.green = fg.blue = 0;
1003 bg.red = bg.green = bg.blue = 255;
1004 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1005 }
1006
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001007 /* Required to initialize most things. */
1008 vterm_screen_reset(screen, 1 /* hard */);
1009}
1010
Bram Moolenaar21554412017-07-24 21:44:43 +02001011/*
1012 * Return the text to show for the buffer name and status.
1013 */
1014 char_u *
1015term_get_status_text(term_T *term)
1016{
1017 if (term->tl_status_text == NULL)
1018 {
1019 char_u *txt;
1020 size_t len;
1021
1022 if (term->tl_title != NULL)
1023 txt = term->tl_title;
1024 else if (term_job_running(term))
1025 txt = (char_u *)_("running");
1026 else
1027 txt = (char_u *)_("finished");
1028 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
1029 term->tl_status_text = alloc(len);
1030 if (term->tl_status_text != NULL)
1031 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1032 term->tl_buffer->b_fname, txt);
1033 }
1034 return term->tl_status_text;
1035}
1036
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001037# ifdef WIN3264
1038
1039#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
1040#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
1041
Bram Moolenaar8a773062017-07-24 22:29:21 +02001042void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001043void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02001044void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001045BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
1046void (*winpty_config_set_initial_size)(void*, int, int);
1047LPCWSTR (*winpty_conin_name)(void*);
1048LPCWSTR (*winpty_conout_name)(void*);
1049LPCWSTR (*winpty_conerr_name)(void*);
1050void (*winpty_free)(void*);
1051void (*winpty_config_free)(void*);
1052void (*winpty_spawn_config_free)(void*);
1053void (*winpty_error_free)(void*);
1054LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001055BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001056
1057/**************************************
1058 * 2. MS-Windows implementation.
1059 */
1060
1061#define WINPTY_DLL "winpty.dll"
1062
1063static HINSTANCE hWinPtyDLL = NULL;
1064
1065 int
1066dyn_winpty_init(void)
1067{
1068 int i;
1069 static struct
1070 {
1071 char *name;
1072 FARPROC *ptr;
1073 } winpty_entry[] =
1074 {
1075 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
1076 {"winpty_config_free", (FARPROC*)&winpty_config_free},
1077 {"winpty_config_new", (FARPROC*)&winpty_config_new},
1078 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
1079 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
1080 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
1081 {"winpty_error_free", (FARPROC*)&winpty_error_free},
1082 {"winpty_free", (FARPROC*)&winpty_free},
1083 {"winpty_open", (FARPROC*)&winpty_open},
1084 {"winpty_spawn", (FARPROC*)&winpty_spawn},
1085 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
1086 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
1087 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001088 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001089 {NULL, NULL}
1090 };
1091
1092 /* No need to initialize twice. */
1093 if (hWinPtyDLL)
1094 return 1;
1095 /* Load winpty.dll */
1096 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
1097 if (!hWinPtyDLL)
1098 {
1099 EMSG2(_(e_loadlib), WINPTY_DLL);
1100 return 0;
1101 }
1102 for (i = 0; winpty_entry[i].name != NULL
1103 && winpty_entry[i].ptr != NULL; ++i)
1104 {
1105 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
1106 winpty_entry[i].name)) == NULL)
1107 {
1108 EMSG2(_(e_loadfunc), winpty_entry[i].name);
1109 return 0;
1110 }
1111 }
1112
1113 return 1;
1114}
1115
1116/*
1117 * Create a new terminal of "rows" by "cols" cells.
1118 * Store a reference in "term".
1119 * Return OK or FAIL.
1120 */
1121 static int
1122term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
1123{
1124 WCHAR *p = enc_to_utf16(cmd, NULL);
1125 channel_T *channel = NULL;
1126 job_T *job = NULL;
1127 jobopt_T opt;
1128 DWORD error;
1129 HANDLE jo = NULL, child_process_handle, child_thread_handle;
1130 void *winpty_err;
1131 void *spawn_config;
1132
1133 if (!dyn_winpty_init())
1134 return FAIL;
1135
1136 if (p == NULL)
1137 return FAIL;
1138
1139 job = job_alloc();
1140 if (job == NULL)
1141 goto failed;
1142
1143 channel = add_channel();
1144 if (channel == NULL)
1145 goto failed;
1146
1147 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
1148 if (term->tl_winpty_config == NULL)
1149 goto failed;
1150
1151 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
1152 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
1153 if (term->tl_winpty == NULL)
1154 goto failed;
1155
1156 spawn_config = winpty_spawn_config_new(
1157 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
1158 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
1159 NULL,
1160 p,
1161 NULL,
1162 NULL,
1163 &winpty_err);
1164 if (spawn_config == NULL)
1165 goto failed;
1166
1167 channel = add_channel();
1168 if (channel == NULL)
1169 goto failed;
1170
1171 job = job_alloc();
1172 if (job == NULL)
1173 goto failed;
1174
1175 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
1176 &child_thread_handle, &error, &winpty_err))
1177 goto failed;
1178
1179 channel_set_pipes(channel,
1180 (sock_T) CreateFileW(
1181 winpty_conin_name(term->tl_winpty),
1182 GENERIC_WRITE, 0, NULL,
1183 OPEN_EXISTING, 0, NULL),
1184 (sock_T) CreateFileW(
1185 winpty_conout_name(term->tl_winpty),
1186 GENERIC_READ, 0, NULL,
1187 OPEN_EXISTING, 0, NULL),
1188 (sock_T) CreateFileW(
1189 winpty_conerr_name(term->tl_winpty),
1190 GENERIC_READ, 0, NULL,
1191 OPEN_EXISTING, 0, NULL));
1192
1193 jo = CreateJobObject(NULL, NULL);
1194 if (jo == NULL)
1195 goto failed;
1196
1197 if (!AssignProcessToJobObject(jo, child_process_handle))
1198 goto failed;
1199
1200 winpty_spawn_config_free(spawn_config);
1201
1202 create_vterm(term, rows, cols);
1203
1204 setup_job_options(&opt, rows, cols);
1205 channel_set_job(channel, job, &opt);
1206
1207 job->jv_channel = channel;
1208 job->jv_proc_info.hProcess = child_process_handle;
1209 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
1210 job->jv_job_object = jo;
1211 job->jv_status = JOB_STARTED;
1212 term->tl_job = job;
1213
1214 return OK;
1215
1216failed:
1217 if (channel != NULL)
1218 channel_clear(channel);
1219 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001220 {
1221 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001222 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001223 }
1224 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001225 if (jo != NULL)
1226 CloseHandle(jo);
1227 if (term->tl_winpty != NULL)
1228 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001229 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001230 if (term->tl_winpty_config != NULL)
1231 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001232 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001233 if (winpty_err != NULL)
1234 {
1235 char_u *msg = utf16_to_enc(
1236 (short_u *)winpty_error_msg(winpty_err), NULL);
1237
1238 EMSG(msg);
1239 winpty_error_free(winpty_err);
1240 }
1241 return FAIL;
1242}
1243
1244/*
1245 * Free the terminal emulator part of "term".
1246 */
1247 static void
1248term_free(term_T *term)
1249{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001250 if (term->tl_winpty != NULL)
1251 winpty_free(term->tl_winpty);
1252 if (term->tl_winpty_config != NULL)
1253 winpty_config_free(term->tl_winpty_config);
1254 if (term->tl_vterm != NULL)
1255 vterm_free(term->tl_vterm);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001256}
1257
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001258/*
1259 * Request size to terminal.
1260 */
1261 static void
1262term_report_winsize(term_T *term, int rows, int cols)
1263{
1264 winpty_set_size(term->tl_winpty, cols, rows, NULL);
1265}
1266
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001267# else
1268
1269/**************************************
1270 * 3. Unix-like implementation.
1271 */
1272
1273/*
1274 * Create a new terminal of "rows" by "cols" cells.
1275 * Start job for "cmd".
1276 * Store the pointers in "term".
1277 * Return OK or FAIL.
1278 */
1279 static int
1280term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
1281{
1282 typval_T argvars[2];
1283 jobopt_T opt;
1284
1285 create_vterm(term, rows, cols);
1286
1287 argvars[0].v_type = VAR_STRING;
1288 argvars[0].vval.v_string = cmd;
1289 setup_job_options(&opt, rows, cols);
1290 term->tl_job = job_start(argvars, &opt);
1291
Bram Moolenaar61a66052017-07-22 18:39:00 +02001292 return term->tl_job != NULL
1293 && term->tl_job->jv_channel != NULL
1294 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001295}
1296
1297/*
1298 * Free the terminal emulator part of "term".
1299 */
1300 static void
1301term_free(term_T *term)
1302{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001303 if (term->tl_vterm != NULL)
1304 vterm_free(term->tl_vterm);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001305}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001306
1307/*
1308 * Request size to terminal.
1309 */
1310 static void
1311term_report_winsize(term_T *term, int rows, int cols)
1312{
1313 /* Use an ioctl() to report the new window size to the job. */
1314 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
1315 {
1316 int fd = -1;
1317 int part;
1318
1319 for (part = PART_OUT; part < PART_COUNT; ++part)
1320 {
1321 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
1322 if (isatty(fd))
1323 break;
1324 }
1325 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
1326 mch_stop_job(term->tl_job, (char_u *)"winch");
1327 }
1328}
1329
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001330# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001331
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001332#endif /* FEAT_TERMINAL */