blob: f3cb12428095d71bf950043d2be47f880e77c563 [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 Moolenaare4f25e42017-07-07 11:54:15 +020036 * - set buffer options to be scratch, hidden, nomodifiable, etc.
37 * - set buffer name to command, add (1) to avoid duplicates.
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 Moolenaare4f25e42017-07-07 11:54:15 +020055 * - implement ":buf {term-buf-name}"
Bram Moolenaar96ca27a2017-07-17 23:20:24 +020056 * - implement term_list() list of buffers with a terminal
57 * - implement term_getsize(buf)
58 * - implement term_setsize(buf)
59 * - implement term_sendkeys(buf, keys) send keystrokes to a terminal
60 * - implement term_wait(buf) wait for screen to be updated
61 * - implement term_scrape(buf, row) inspect terminal screen
62 * - implement term_open(command, options) open terminal window
63 * - implement term_getjob(buf)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +020064 * - implement 'termkey'
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 Moolenaare4f25e42017-07-07 11:54:15 +020067 */
68
69#include "vim.h"
70
71#ifdef FEAT_TERMINAL
72
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020073#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020074# define MIN(x,y) (x < y ? x : y)
75# define MAX(x,y) (x > y ? x : y)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020076#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020077
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020078#include "libvterm/include/vterm.h"
79
Bram Moolenaare4f25e42017-07-07 11:54:15 +020080/* typedef term_T in structs.h */
81struct terminal_S {
82 term_T *tl_next;
83
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020084#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020085 void *tl_winpty_config;
86 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020087#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020088 VTerm *tl_vterm;
Bram Moolenaare4f25e42017-07-07 11:54:15 +020089 job_T *tl_job;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +020090 buf_T *tl_buffer;
Bram Moolenaare4f25e42017-07-07 11:54:15 +020091
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020092 /* last known vterm size */
93 int tl_rows;
94 int tl_cols;
95 /* vterm size does not follow window size */
96 int tl_rows_fixed;
97 int tl_cols_fixed;
98
Bram Moolenaare4f25e42017-07-07 11:54:15 +020099 /* Range of screen rows to update. Zero based. */
100 int tl_dirty_row_start; /* -1 if nothing dirty */
101 int tl_dirty_row_end; /* row below last one to update */
102
103 pos_T tl_cursor;
104};
105
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200106/*
107 * List of all active terminals.
108 */
109static term_T *first_term = NULL;
110
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200111
112#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
113#define KEY_BUF_LEN 200
114
115/*
116 * Functions with separate implementation for MS-Windows and Unix-like systems.
117 */
118static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd);
119static 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
193 /* TODO: set buffer type, hidden, etc. */
194
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200195 set_term_and_win_size(term);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200196
Bram Moolenaare173fd02017-07-22 19:03:32 +0200197 if (cmd == NULL || *cmd == NUL)
198 cmd = p_sh;
199
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200200 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaare173fd02017-07-22 19:03:32 +0200201 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200202 {
203 /* store the size we ended up with */
204 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200205 }
206 else
207 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200208 free_terminal(term);
209 curbuf->b_term = NULL;
210
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200211 /* Wiping out the buffer will also close the window and call
212 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200213 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200214 }
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200215
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200216 /* TODO: Setup pty, see mch_call_shell(). */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200217}
218
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200219/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200220 * Free a terminal and everything it refers to.
221 * Kills the job if there is one.
222 * Called when wiping out a buffer.
223 */
224 void
225free_terminal(term_T *term)
226{
227 term_T *tp;
228
229 if (term == NULL)
230 return;
231 if (first_term == term)
232 first_term = term->tl_next;
233 else
234 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
235 if (tp->tl_next == term)
236 {
237 tp->tl_next = term->tl_next;
238 break;
239 }
240
241 if (term->tl_job != NULL)
242 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200243 if (term->tl_job->jv_status != JOB_ENDED
244 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200245 job_stop(term->tl_job, NULL, "kill");
246 job_unref(term->tl_job);
247 }
248
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200249 term_free(term);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200250 vim_free(term);
251}
252
253/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200254 * Write job output "msg[len]" to the vterm.
255 */
256 static void
257term_write_job_output(term_T *term, char_u *msg, size_t len)
258{
259 VTerm *vterm = term->tl_vterm;
260 char_u *p;
261 size_t done;
262 size_t len_now;
263
264 for (done = 0; done < len; done += len_now)
265 {
266 for (p = msg + done; p < msg + len; )
267 {
268 if (*p == NL)
269 break;
270 p += utf_ptr2len_len(p, len - (p - msg));
271 }
272 len_now = p - msg - done;
273 vterm_input_write(vterm, (char *)msg + done, len_now);
274 if (p < msg + len && *p == NL)
275 {
276 /* Convert NL to CR-NL, that appears to work best. */
277 vterm_input_write(vterm, "\r\n", 2);
278 ++len_now;
279 }
280 }
281
282 /* this invokes the damage callbacks */
283 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
284}
285
286/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200287 * Invoked when "msg" output from a job was received. Write it to the terminal
288 * of "buffer".
289 */
290 void
291write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
292{
293 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200294 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200295
296 ch_logn(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200297 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200298
299 /* TODO: only update once in a while. */
300 update_screen(0);
301 setcursor();
302 out_flush();
303}
304
305/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200306 * Convert typed key "c" into bytes to send to the job.
307 * Return the number of bytes in "buf".
308 */
309 static int
310term_convert_key(int c, char *buf)
311{
312 VTerm *vterm = curbuf->b_term->tl_vterm;
313 VTermKey key = VTERM_KEY_NONE;
314 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200315
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200316 switch (c)
317 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200318 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200319 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200320 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
321 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200322 case K_DEL: key = VTERM_KEY_DEL; break;
323 case K_DOWN: key = VTERM_KEY_DOWN; break;
324 case K_END: key = VTERM_KEY_END; break;
325 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
326 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
327 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
328 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
329 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
330 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
331 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
332 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
333 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
334 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
335 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
336 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
337 case K_HOME: key = VTERM_KEY_HOME; break;
338 case K_INS: key = VTERM_KEY_INS; break;
339 case K_K0: key = VTERM_KEY_KP_0; break;
340 case K_K1: key = VTERM_KEY_KP_1; break;
341 case K_K2: key = VTERM_KEY_KP_2; break;
342 case K_K3: key = VTERM_KEY_KP_3; break;
343 case K_K4: key = VTERM_KEY_KP_4; break;
344 case K_K5: key = VTERM_KEY_KP_5; break;
345 case K_K6: key = VTERM_KEY_KP_6; break;
346 case K_K7: key = VTERM_KEY_KP_7; break;
347 case K_K8: key = VTERM_KEY_KP_8; break;
348 case K_K9: key = VTERM_KEY_KP_9; break;
349 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
350 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
351 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
352 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
353 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
354 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
355 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
356 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
357 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
358 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
359 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
360 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
361 case K_LEFT: key = VTERM_KEY_LEFT; break;
362 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
363 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
364 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
365 case K_UP: key = VTERM_KEY_UP; break;
366 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200367
368 case K_MOUSEUP: /* TODO */ break;
369 case K_MOUSEDOWN: /* TODO */ break;
370 case K_MOUSELEFT: /* TODO */ break;
371 case K_MOUSERIGHT: /* TODO */ break;
372
373 case K_LEFTMOUSE: /* TODO */ break;
374 case K_LEFTMOUSE_NM: /* TODO */ break;
375 case K_LEFTDRAG: /* TODO */ break;
376 case K_LEFTRELEASE: /* TODO */ break;
377 case K_LEFTRELEASE_NM: /* TODO */ break;
378 case K_MIDDLEMOUSE: /* TODO */ break;
379 case K_MIDDLEDRAG: /* TODO */ break;
380 case K_MIDDLERELEASE: /* TODO */ break;
381 case K_RIGHTMOUSE: /* TODO */ break;
382 case K_RIGHTDRAG: /* TODO */ break;
383 case K_RIGHTRELEASE: /* TODO */ break;
384 case K_X1MOUSE: /* TODO */ break;
385 case K_X1DRAG: /* TODO */ break;
386 case K_X1RELEASE: /* TODO */ break;
387 case K_X2MOUSE: /* TODO */ break;
388 case K_X2DRAG: /* TODO */ break;
389 case K_X2RELEASE: /* TODO */ break;
390
391 /* TODO: handle all special keys and modifiers that terminal_loop()
392 * does not handle. */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200393 }
394
395 /*
396 * Convert special keys to vterm keys:
397 * - Write keys to vterm: vterm_keyboard_key()
398 * - Write output to channel.
399 */
400 if (key != VTERM_KEY_NONE)
401 /* Special key, let vterm convert it. */
402 vterm_keyboard_key(vterm, key, mod);
403 else
404 /* Normal character, let vterm convert it. */
405 vterm_keyboard_unichar(vterm, c, mod);
406
407 /* Read back the converted escape sequence. */
408 return vterm_output_read(vterm, buf, KEY_BUF_LEN);
409}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200410
Bram Moolenaar938783d2017-07-16 20:13:26 +0200411/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200412 * Wait for input and send it to the job.
413 * Return when the start of a CTRL-W command is typed or anything else that
414 * should be handled as a Normal mode command.
415 */
416 void
417terminal_loop(void)
418{
419 char buf[KEY_BUF_LEN];
420 int c;
421 size_t len;
422 static int mouse_was_outside = FALSE;
423 int dragging_outside = FALSE;
424
425 for (;;)
426 {
427 /* TODO: skip screen update when handling a sequence of keys. */
428 update_screen(0);
429 setcursor();
430 out_flush();
431 ++no_mapping;
432 ++allow_keys;
433 got_int = FALSE;
434 c = vgetc();
435 --no_mapping;
436 --allow_keys;
437
438 /* Catch keys that need to be handled as in Normal mode. */
439 switch (c)
440 {
441 case Ctrl_W:
442 case NUL:
443 case K_ZERO:
444 stuffcharReadbuff(c);
445 return;
446
447 case K_IGNORE: continue;
448
449 case K_LEFTDRAG:
450 case K_MIDDLEDRAG:
451 case K_RIGHTDRAG:
452 case K_X1DRAG:
453 case K_X2DRAG:
454 dragging_outside = mouse_was_outside;
455 /* FALLTHROUGH */
456 case K_LEFTMOUSE:
457 case K_LEFTMOUSE_NM:
458 case K_LEFTRELEASE:
459 case K_LEFTRELEASE_NM:
460 case K_MIDDLEMOUSE:
461 case K_MIDDLERELEASE:
462 case K_RIGHTMOUSE:
463 case K_RIGHTRELEASE:
464 case K_X1MOUSE:
465 case K_X1RELEASE:
466 case K_X2MOUSE:
467 case K_X2RELEASE:
468 if (mouse_row < W_WINROW(curwin)
469 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
470 || mouse_col < W_WINCOL(curwin)
471 || mouse_col >= W_ENDCOL(curwin)
472 || dragging_outside)
473 {
474 /* click outside the current window */
475 stuffcharReadbuff(c);
476 mouse_was_outside = TRUE;
477 return;
478 }
479 }
480 mouse_was_outside = FALSE;
481
482 /* Convert the typed key to a sequence of bytes for the job. */
483 len = term_convert_key(c, buf);
484 if (len > 0)
485 /* TODO: if FAIL is returned, stop? */
486 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
487 (char_u *)buf, len, NULL);
488 }
489}
490
491 static void
492position_cursor(win_T *wp, VTermPos *pos)
493{
494 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
495 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
496}
497
498static int handle_damage(VTermRect rect, void *user);
499static int handle_moverect(VTermRect dest, VTermRect src, void *user);
500static int handle_movecursor(VTermPos pos, VTermPos oldpos, int visible, void *user);
501static int handle_resize(int rows, int cols, void *user);
502
503static VTermScreenCallbacks screen_callbacks = {
504 handle_damage, /* damage */
505 handle_moverect, /* moverect */
506 handle_movecursor, /* movecursor */
507 NULL, /* settermprop */
508 NULL, /* bell */
509 handle_resize, /* resize */
510 NULL, /* sb_pushline */
511 NULL /* sb_popline */
512};
513
514 static int
515handle_damage(VTermRect rect, void *user)
516{
517 term_T *term = (term_T *)user;
518
519 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
520 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
521 redraw_buf_later(term->tl_buffer, NOT_VALID);
522 return 1;
523}
524
525 static int
526handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
527{
528 term_T *term = (term_T *)user;
529
530 /* TODO */
531 redraw_buf_later(term->tl_buffer, NOT_VALID);
532 return 1;
533}
534
535 static int
536handle_movecursor(
537 VTermPos pos,
538 VTermPos oldpos UNUSED,
539 int visible UNUSED,
540 void *user)
541{
542 term_T *term = (term_T *)user;
543 win_T *wp;
544 int is_current = FALSE;
545
546 FOR_ALL_WINDOWS(wp)
547 {
548 if (wp->w_buffer == term->tl_buffer)
549 {
550 position_cursor(wp, &pos);
551 if (wp == curwin)
552 is_current = TRUE;
553 }
554 }
555
556 if (is_current)
557 {
558 setcursor();
559 out_flush();
560 }
561
562 return 1;
563}
564
565/*
566 * The job running in the terminal resized the terminal.
567 */
568 static int
569handle_resize(int rows, int cols, void *user)
570{
571 term_T *term = (term_T *)user;
572 win_T *wp;
573
574 term->tl_rows = rows;
575 term->tl_cols = cols;
576 FOR_ALL_WINDOWS(wp)
577 {
578 if (wp->w_buffer == term->tl_buffer)
579 {
580 win_setheight_win(rows, wp);
581 win_setwidth_win(cols, wp);
582 }
583 }
584
585 redraw_buf_later(term->tl_buffer, NOT_VALID);
586 return 1;
587}
588
589/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200590 * Called to update the window that contains the terminal.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200591 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200592 void
593term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +0200594{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200595 term_T *term = wp->w_buffer->b_term;
596 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200597 VTermScreen *screen = vterm_obtain_screen(vterm);
Bram Moolenaar58556cd2017-07-20 23:04:46 +0200598 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200599 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200600
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200601 /*
602 * If the window was resized a redraw will be triggered and we get here.
603 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
604 */
605 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
606 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +0200607 {
608 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
609 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
610
611 vterm_set_size(vterm, rows, cols);
612 ch_logn(term->tl_job->jv_channel, "Resizing terminal to %d lines",
613 rows);
614
615#if defined(UNIX)
616 /* Use an ioctl() to report the new window size to the job. */
617 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
618 {
619 int fd = -1;
620 int part;
621
622 for (part = PART_OUT; part < PART_COUNT; ++part)
623 {
624 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
625 if (isatty(fd))
626 break;
627 }
628 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
629 mch_stop_job(term->tl_job, (char_u *)"winch");
630 }
631#endif
632 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +0200633
634 /* The cursor may have been moved when resizing. */
635 vterm_state_get_cursorpos(state, &pos);
636 position_cursor(wp, &pos);
637
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200638 /* TODO: Only redraw what changed. */
639 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +0200640 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200641 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200642 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +0200643
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200644 if (pos.row < term->tl_rows)
645 {
646 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200647 {
648 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200649 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200650
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200651 vterm_screen_get_cell(screen, pos, &cell);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200652 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200653 if (c == NUL)
654 {
655 ScreenLines[off] = ' ';
656 ScreenLinesUC[off] = NUL;
657 }
658 else
659 {
660#if defined(FEAT_MBYTE)
661 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200662 {
663 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200664 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200665 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200666 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200667 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200668 ScreenLines[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200669 ScreenLinesUC[off] = NUL;
670 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200671#else
672 ScreenLines[off] = c;
673#endif
674 }
675 /* TODO: use cell.attrs and colors */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200676 ScreenAttrs[off] = 0;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200677
678 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200679 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200680 if (cell.width == 2)
681 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200682 ScreenLines[off] = NUL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200683 ScreenLinesUC[off] = NUL;
684 ++pos.col;
685 ++off;
686 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200687 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200688 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200689 else
690 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200691
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200692 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
693 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +0200694 }
695}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200696
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200697/*
698 * Set job options common for Unix and MS-Windows.
699 */
700 static void
701setup_job_options(jobopt_T *opt, int rows, int cols)
702{
703 clear_job_options(opt);
704 opt->jo_mode = MODE_RAW;
705 opt->jo_out_mode = MODE_RAW;
706 opt->jo_err_mode = MODE_RAW;
707 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
708 opt->jo_io[PART_OUT] = JIO_BUFFER;
709 opt->jo_io[PART_ERR] = JIO_BUFFER;
710 opt->jo_set |= JO_OUT_IO + (JO_OUT_IO << (PART_ERR - PART_OUT));
711 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
712 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +0200713 opt->jo_pty = TRUE;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200714 opt->jo_set |= JO_OUT_BUF + (JO_OUT_BUF << (PART_ERR - PART_OUT));
715 opt->jo_term_rows = rows;
716 opt->jo_term_cols = cols;
717}
718
719/*
720 * Create a new vterm and initialize it.
721 */
722 static void
723create_vterm(term_T *term, int rows, int cols)
724{
725 VTerm *vterm;
726 VTermScreen *screen;
727
728 vterm = vterm_new(rows, cols);
729 term->tl_vterm = vterm;
730 screen = vterm_obtain_screen(vterm);
731 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
732 /* TODO: depends on 'encoding'. */
733 vterm_set_utf8(vterm, 1);
734 /* Required to initialize most things. */
735 vterm_screen_reset(screen, 1 /* hard */);
736}
737
738# ifdef WIN3264
739
740#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
741#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
742
743void* (*winpty_config_new)(int, void*);
744void* (*winpty_open)(void*, void*);
745void* (*winpty_spawn_config_new)(int, void*, LPCWSTR, void*, void*, void*);
746BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
747void (*winpty_config_set_initial_size)(void*, int, int);
748LPCWSTR (*winpty_conin_name)(void*);
749LPCWSTR (*winpty_conout_name)(void*);
750LPCWSTR (*winpty_conerr_name)(void*);
751void (*winpty_free)(void*);
752void (*winpty_config_free)(void*);
753void (*winpty_spawn_config_free)(void*);
754void (*winpty_error_free)(void*);
755LPCWSTR (*winpty_error_msg)(void*);
756
757/**************************************
758 * 2. MS-Windows implementation.
759 */
760
761#define WINPTY_DLL "winpty.dll"
762
763static HINSTANCE hWinPtyDLL = NULL;
764
765 int
766dyn_winpty_init(void)
767{
768 int i;
769 static struct
770 {
771 char *name;
772 FARPROC *ptr;
773 } winpty_entry[] =
774 {
775 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
776 {"winpty_config_free", (FARPROC*)&winpty_config_free},
777 {"winpty_config_new", (FARPROC*)&winpty_config_new},
778 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
779 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
780 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
781 {"winpty_error_free", (FARPROC*)&winpty_error_free},
782 {"winpty_free", (FARPROC*)&winpty_free},
783 {"winpty_open", (FARPROC*)&winpty_open},
784 {"winpty_spawn", (FARPROC*)&winpty_spawn},
785 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
786 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
787 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
788 {NULL, NULL}
789 };
790
791 /* No need to initialize twice. */
792 if (hWinPtyDLL)
793 return 1;
794 /* Load winpty.dll */
795 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
796 if (!hWinPtyDLL)
797 {
798 EMSG2(_(e_loadlib), WINPTY_DLL);
799 return 0;
800 }
801 for (i = 0; winpty_entry[i].name != NULL
802 && winpty_entry[i].ptr != NULL; ++i)
803 {
804 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
805 winpty_entry[i].name)) == NULL)
806 {
807 EMSG2(_(e_loadfunc), winpty_entry[i].name);
808 return 0;
809 }
810 }
811
812 return 1;
813}
814
815/*
816 * Create a new terminal of "rows" by "cols" cells.
817 * Store a reference in "term".
818 * Return OK or FAIL.
819 */
820 static int
821term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
822{
823 WCHAR *p = enc_to_utf16(cmd, NULL);
824 channel_T *channel = NULL;
825 job_T *job = NULL;
826 jobopt_T opt;
827 DWORD error;
828 HANDLE jo = NULL, child_process_handle, child_thread_handle;
829 void *winpty_err;
830 void *spawn_config;
831
832 if (!dyn_winpty_init())
833 return FAIL;
834
835 if (p == NULL)
836 return FAIL;
837
838 job = job_alloc();
839 if (job == NULL)
840 goto failed;
841
842 channel = add_channel();
843 if (channel == NULL)
844 goto failed;
845
846 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
847 if (term->tl_winpty_config == NULL)
848 goto failed;
849
850 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
851 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
852 if (term->tl_winpty == NULL)
853 goto failed;
854
855 spawn_config = winpty_spawn_config_new(
856 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
857 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
858 NULL,
859 p,
860 NULL,
861 NULL,
862 &winpty_err);
863 if (spawn_config == NULL)
864 goto failed;
865
866 channel = add_channel();
867 if (channel == NULL)
868 goto failed;
869
870 job = job_alloc();
871 if (job == NULL)
872 goto failed;
873
874 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
875 &child_thread_handle, &error, &winpty_err))
876 goto failed;
877
878 channel_set_pipes(channel,
879 (sock_T) CreateFileW(
880 winpty_conin_name(term->tl_winpty),
881 GENERIC_WRITE, 0, NULL,
882 OPEN_EXISTING, 0, NULL),
883 (sock_T) CreateFileW(
884 winpty_conout_name(term->tl_winpty),
885 GENERIC_READ, 0, NULL,
886 OPEN_EXISTING, 0, NULL),
887 (sock_T) CreateFileW(
888 winpty_conerr_name(term->tl_winpty),
889 GENERIC_READ, 0, NULL,
890 OPEN_EXISTING, 0, NULL));
891
892 jo = CreateJobObject(NULL, NULL);
893 if (jo == NULL)
894 goto failed;
895
896 if (!AssignProcessToJobObject(jo, child_process_handle))
897 goto failed;
898
899 winpty_spawn_config_free(spawn_config);
900
901 create_vterm(term, rows, cols);
902
903 setup_job_options(&opt, rows, cols);
904 channel_set_job(channel, job, &opt);
905
906 job->jv_channel = channel;
907 job->jv_proc_info.hProcess = child_process_handle;
908 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
909 job->jv_job_object = jo;
910 job->jv_status = JOB_STARTED;
911 term->tl_job = job;
912
913 return OK;
914
915failed:
916 if (channel != NULL)
917 channel_clear(channel);
918 if (job != NULL)
919 job_cleanup(job);
920 if (jo != NULL)
921 CloseHandle(jo);
922 if (term->tl_winpty != NULL)
923 winpty_free(term->tl_winpty);
924 if (term->tl_winpty_config != NULL)
925 winpty_config_free(term->tl_winpty_config);
926 if (winpty_err != NULL)
927 {
928 char_u *msg = utf16_to_enc(
929 (short_u *)winpty_error_msg(winpty_err), NULL);
930
931 EMSG(msg);
932 winpty_error_free(winpty_err);
933 }
934 return FAIL;
935}
936
937/*
938 * Free the terminal emulator part of "term".
939 */
940 static void
941term_free(term_T *term)
942{
943 winpty_free(term->tl_winpty);
944 winpty_config_free(term->tl_winpty_config);
945 vterm_free(term->tl_vterm);
946}
947
948# else
949
950/**************************************
951 * 3. Unix-like implementation.
952 */
953
954/*
955 * Create a new terminal of "rows" by "cols" cells.
956 * Start job for "cmd".
957 * Store the pointers in "term".
958 * Return OK or FAIL.
959 */
960 static int
961term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
962{
963 typval_T argvars[2];
964 jobopt_T opt;
965
966 create_vterm(term, rows, cols);
967
968 argvars[0].v_type = VAR_STRING;
969 argvars[0].vval.v_string = cmd;
970 setup_job_options(&opt, rows, cols);
971 term->tl_job = job_start(argvars, &opt);
972
Bram Moolenaar61a66052017-07-22 18:39:00 +0200973 return term->tl_job != NULL
974 && term->tl_job->jv_channel != NULL
975 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200976}
977
978/*
979 * Free the terminal emulator part of "term".
980 */
981 static void
982term_free(term_T *term)
983{
984 vterm_free(term->tl_vterm);
985}
986# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200987
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200988#endif /* FEAT_TERMINAL */