blob: 0d974eddbe4551d4c1295f00ddd267814497fe07 [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.
15 * 2. The MS-Windows implementation.
16 * Uses a hidden console for the terminal emulator.
17 * 3. The Unix-like implementation.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020018 * Uses libvterm for the terminal emulator directly.
19 *
20 * For each terminal one VTerm is constructed. This uses libvterm. A copy of
21 * that library is in the libvterm directory.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020022 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020023 * When a terminal window is opened, a job is started that will be connected to
24 * the terminal emulator.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020025 *
26 * If the terminal window has keyboard focus, typed keys are converted to the
27 * terminal encoding and writting to the job over a channel.
28 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020029 * If the job produces output, it is written to the terminal emulator. The
30 * terminal emulator invokes callbacks when its screen content changes. The
31 * line range is stored in tl_dirty_row_start and tl_dirty_row_end. Once in a
32 * while, if the terminal window is visible, the screen contents is drawn.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020033 *
34 * TODO:
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020035 * - When 'termsize' is set and dragging the separator the terminal gets messed
36 * up.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020037 * - set buffer options to be scratch, hidden, nomodifiable, etc.
38 * - set buffer name to command, add (1) to avoid duplicates.
Bram Moolenaar96ca27a2017-07-17 23:20:24 +020039 * - If [command] is not given the 'shell' option is used.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020040 * - Add a scrollback buffer (contains lines to scroll off the top).
41 * Can use the buf_T lines, store attributes somewhere else?
42 * - When the job ends:
43 * - Write "-- JOB ENDED --" in the terminal.
44 * - Put the terminal contents in the scrollback buffer.
45 * - Free the terminal emulator.
46 * - Display the scrollback buffer (but with attributes).
47 * Make the buffer not modifiable, drop attributes when making changes.
Bram Moolenaar938783d2017-07-16 20:13:26 +020048 * - when closing window and job has not ended, make terminal hidden?
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020049 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar938783d2017-07-16 20:13:26 +020050 * - command line completion for :terminal
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020051 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020052 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020053 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020054 * - implement "term" for job_start(): more job options when starting a
55 * terminal.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020056 * - implement ":buf {term-buf-name}"
Bram Moolenaar96ca27a2017-07-17 23:20:24 +020057 * - implement term_list() list of buffers with a terminal
58 * - implement term_getsize(buf)
59 * - implement term_setsize(buf)
60 * - implement term_sendkeys(buf, keys) send keystrokes to a terminal
61 * - implement term_wait(buf) wait for screen to be updated
62 * - implement term_scrape(buf, row) inspect terminal screen
63 * - implement term_open(command, options) open terminal window
64 * - implement term_getjob(buf)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +020065 * - implement 'termkey'
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 Moolenaare4f25e42017-07-07 11:54:15 +020068 */
69
70#include "vim.h"
71
72#ifdef FEAT_TERMINAL
73
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020074#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020075# define MIN(x,y) (x < y ? x : y)
76# define MAX(x,y) (x > y ? x : y)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020077#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020078
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020079#include "libvterm/include/vterm.h"
80
Bram Moolenaare4f25e42017-07-07 11:54:15 +020081/* typedef term_T in structs.h */
82struct terminal_S {
83 term_T *tl_next;
84
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020085#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020086 void *tl_winpty_config;
87 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020088#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020089 VTerm *tl_vterm;
Bram Moolenaare4f25e42017-07-07 11:54:15 +020090 job_T *tl_job;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +020091 buf_T *tl_buffer;
Bram Moolenaare4f25e42017-07-07 11:54:15 +020092
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020093 /* last known vterm size */
94 int tl_rows;
95 int tl_cols;
96 /* vterm size does not follow window size */
97 int tl_rows_fixed;
98 int tl_cols_fixed;
99
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200100 /* Range of screen rows to update. Zero based. */
101 int tl_dirty_row_start; /* -1 if nothing dirty */
102 int tl_dirty_row_end; /* row below last one to update */
103
104 pos_T tl_cursor;
105};
106
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200107/*
108 * List of all active terminals.
109 */
110static term_T *first_term = NULL;
111
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200112
113#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
114#define KEY_BUF_LEN 200
115
116/*
117 * Functions with separate implementation for MS-Windows and Unix-like systems.
118 */
119static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd);
120static void term_free(term_T *term);
121
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200122/**************************************
123 * 1. Generic code for all systems.
124 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200125
126/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200127 * Determine the terminal size from 'termsize' and the current window.
128 * Assumes term->tl_rows and term->tl_cols are zero.
129 */
130 static void
131set_term_and_win_size(term_T *term)
132{
133 if (*curwin->w_p_tms != NUL)
134 {
135 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
136
137 term->tl_rows = atoi((char *)curwin->w_p_tms);
138 term->tl_cols = atoi((char *)p);
139 }
140 if (term->tl_rows == 0)
141 term->tl_rows = curwin->w_height;
142 else
143 {
144 win_setheight_win(term->tl_rows, curwin);
145 term->tl_rows_fixed = TRUE;
146 }
147 if (term->tl_cols == 0)
148 term->tl_cols = curwin->w_width;
149 else
150 {
151 win_setwidth_win(term->tl_cols, curwin);
152 term->tl_cols_fixed = TRUE;
153 }
154}
155
156/*
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200157 * ":terminal": open a terminal window and execute a job in it.
158 */
159 void
160ex_terminal(exarg_T *eap)
161{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200162 exarg_T split_ea;
163 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200164 term_T *term;
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 Moolenaar8f84c3a2017-07-22 16:14:44 +0200197 /* System dependent: setup the vterm and start the job in it. */
198 if (term_and_job_init(term, term->tl_rows, term->tl_cols, eap->arg) == OK)
199 {
200 /* store the size we ended up with */
201 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200202 }
203 else
204 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200205 /* Wiping out the buffer will also close the window and call
206 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200207 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200208 }
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200209
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200210 /* TODO: Setup pty, see mch_call_shell(). */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200211}
212
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200213/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200214 * Free a terminal and everything it refers to.
215 * Kills the job if there is one.
216 * Called when wiping out a buffer.
217 */
218 void
219free_terminal(term_T *term)
220{
221 term_T *tp;
222
223 if (term == NULL)
224 return;
225 if (first_term == term)
226 first_term = term->tl_next;
227 else
228 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
229 if (tp->tl_next == term)
230 {
231 tp->tl_next = term->tl_next;
232 break;
233 }
234
235 if (term->tl_job != NULL)
236 {
237 if (term->tl_job->jv_status != JOB_ENDED)
238 job_stop(term->tl_job, NULL, "kill");
239 job_unref(term->tl_job);
240 }
241
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200242 term_free(term);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200243 vim_free(term);
244}
245
246/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200247 * Write job output "msg[len]" to the vterm.
248 */
249 static void
250term_write_job_output(term_T *term, char_u *msg, size_t len)
251{
252 VTerm *vterm = term->tl_vterm;
253 char_u *p;
254 size_t done;
255 size_t len_now;
256
257 for (done = 0; done < len; done += len_now)
258 {
259 for (p = msg + done; p < msg + len; )
260 {
261 if (*p == NL)
262 break;
263 p += utf_ptr2len_len(p, len - (p - msg));
264 }
265 len_now = p - msg - done;
266 vterm_input_write(vterm, (char *)msg + done, len_now);
267 if (p < msg + len && *p == NL)
268 {
269 /* Convert NL to CR-NL, that appears to work best. */
270 vterm_input_write(vterm, "\r\n", 2);
271 ++len_now;
272 }
273 }
274
275 /* this invokes the damage callbacks */
276 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
277}
278
279/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200280 * Invoked when "msg" output from a job was received. Write it to the terminal
281 * of "buffer".
282 */
283 void
284write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
285{
286 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200287 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200288
289 ch_logn(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200290 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200291
292 /* TODO: only update once in a while. */
293 update_screen(0);
294 setcursor();
295 out_flush();
296}
297
298/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200299 * Convert typed key "c" into bytes to send to the job.
300 * Return the number of bytes in "buf".
301 */
302 static int
303term_convert_key(int c, char *buf)
304{
305 VTerm *vterm = curbuf->b_term->tl_vterm;
306 VTermKey key = VTERM_KEY_NONE;
307 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200308
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200309 switch (c)
310 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200311 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200312 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200313 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
314 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200315 case K_DEL: key = VTERM_KEY_DEL; break;
316 case K_DOWN: key = VTERM_KEY_DOWN; break;
317 case K_END: key = VTERM_KEY_END; break;
318 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
319 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
320 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
321 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
322 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
323 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
324 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
325 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
326 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
327 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
328 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
329 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
330 case K_HOME: key = VTERM_KEY_HOME; break;
331 case K_INS: key = VTERM_KEY_INS; break;
332 case K_K0: key = VTERM_KEY_KP_0; break;
333 case K_K1: key = VTERM_KEY_KP_1; break;
334 case K_K2: key = VTERM_KEY_KP_2; break;
335 case K_K3: key = VTERM_KEY_KP_3; break;
336 case K_K4: key = VTERM_KEY_KP_4; break;
337 case K_K5: key = VTERM_KEY_KP_5; break;
338 case K_K6: key = VTERM_KEY_KP_6; break;
339 case K_K7: key = VTERM_KEY_KP_7; break;
340 case K_K8: key = VTERM_KEY_KP_8; break;
341 case K_K9: key = VTERM_KEY_KP_9; break;
342 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
343 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
344 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
345 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
346 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
347 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
348 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
349 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
350 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
351 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
352 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
353 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
354 case K_LEFT: key = VTERM_KEY_LEFT; break;
355 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
356 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
357 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
358 case K_UP: key = VTERM_KEY_UP; break;
359 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200360
361 case K_MOUSEUP: /* TODO */ break;
362 case K_MOUSEDOWN: /* TODO */ break;
363 case K_MOUSELEFT: /* TODO */ break;
364 case K_MOUSERIGHT: /* TODO */ break;
365
366 case K_LEFTMOUSE: /* TODO */ break;
367 case K_LEFTMOUSE_NM: /* TODO */ break;
368 case K_LEFTDRAG: /* TODO */ break;
369 case K_LEFTRELEASE: /* TODO */ break;
370 case K_LEFTRELEASE_NM: /* TODO */ break;
371 case K_MIDDLEMOUSE: /* TODO */ break;
372 case K_MIDDLEDRAG: /* TODO */ break;
373 case K_MIDDLERELEASE: /* TODO */ break;
374 case K_RIGHTMOUSE: /* TODO */ break;
375 case K_RIGHTDRAG: /* TODO */ break;
376 case K_RIGHTRELEASE: /* TODO */ break;
377 case K_X1MOUSE: /* TODO */ break;
378 case K_X1DRAG: /* TODO */ break;
379 case K_X1RELEASE: /* TODO */ break;
380 case K_X2MOUSE: /* TODO */ break;
381 case K_X2DRAG: /* TODO */ break;
382 case K_X2RELEASE: /* TODO */ break;
383
384 /* TODO: handle all special keys and modifiers that terminal_loop()
385 * does not handle. */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200386 }
387
388 /*
389 * Convert special keys to vterm keys:
390 * - Write keys to vterm: vterm_keyboard_key()
391 * - Write output to channel.
392 */
393 if (key != VTERM_KEY_NONE)
394 /* Special key, let vterm convert it. */
395 vterm_keyboard_key(vterm, key, mod);
396 else
397 /* Normal character, let vterm convert it. */
398 vterm_keyboard_unichar(vterm, c, mod);
399
400 /* Read back the converted escape sequence. */
401 return vterm_output_read(vterm, buf, KEY_BUF_LEN);
402}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200403
Bram Moolenaar938783d2017-07-16 20:13:26 +0200404/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200405 * Wait for input and send it to the job.
406 * Return when the start of a CTRL-W command is typed or anything else that
407 * should be handled as a Normal mode command.
408 */
409 void
410terminal_loop(void)
411{
412 char buf[KEY_BUF_LEN];
413 int c;
414 size_t len;
415 static int mouse_was_outside = FALSE;
416 int dragging_outside = FALSE;
417
418 for (;;)
419 {
420 /* TODO: skip screen update when handling a sequence of keys. */
421 update_screen(0);
422 setcursor();
423 out_flush();
424 ++no_mapping;
425 ++allow_keys;
426 got_int = FALSE;
427 c = vgetc();
428 --no_mapping;
429 --allow_keys;
430
431 /* Catch keys that need to be handled as in Normal mode. */
432 switch (c)
433 {
434 case Ctrl_W:
435 case NUL:
436 case K_ZERO:
437 stuffcharReadbuff(c);
438 return;
439
440 case K_IGNORE: continue;
441
442 case K_LEFTDRAG:
443 case K_MIDDLEDRAG:
444 case K_RIGHTDRAG:
445 case K_X1DRAG:
446 case K_X2DRAG:
447 dragging_outside = mouse_was_outside;
448 /* FALLTHROUGH */
449 case K_LEFTMOUSE:
450 case K_LEFTMOUSE_NM:
451 case K_LEFTRELEASE:
452 case K_LEFTRELEASE_NM:
453 case K_MIDDLEMOUSE:
454 case K_MIDDLERELEASE:
455 case K_RIGHTMOUSE:
456 case K_RIGHTRELEASE:
457 case K_X1MOUSE:
458 case K_X1RELEASE:
459 case K_X2MOUSE:
460 case K_X2RELEASE:
461 if (mouse_row < W_WINROW(curwin)
462 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
463 || mouse_col < W_WINCOL(curwin)
464 || mouse_col >= W_ENDCOL(curwin)
465 || dragging_outside)
466 {
467 /* click outside the current window */
468 stuffcharReadbuff(c);
469 mouse_was_outside = TRUE;
470 return;
471 }
472 }
473 mouse_was_outside = FALSE;
474
475 /* Convert the typed key to a sequence of bytes for the job. */
476 len = term_convert_key(c, buf);
477 if (len > 0)
478 /* TODO: if FAIL is returned, stop? */
479 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
480 (char_u *)buf, len, NULL);
481 }
482}
483
484 static void
485position_cursor(win_T *wp, VTermPos *pos)
486{
487 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
488 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
489}
490
491static int handle_damage(VTermRect rect, void *user);
492static int handle_moverect(VTermRect dest, VTermRect src, void *user);
493static int handle_movecursor(VTermPos pos, VTermPos oldpos, int visible, void *user);
494static int handle_resize(int rows, int cols, void *user);
495
496static VTermScreenCallbacks screen_callbacks = {
497 handle_damage, /* damage */
498 handle_moverect, /* moverect */
499 handle_movecursor, /* movecursor */
500 NULL, /* settermprop */
501 NULL, /* bell */
502 handle_resize, /* resize */
503 NULL, /* sb_pushline */
504 NULL /* sb_popline */
505};
506
507 static int
508handle_damage(VTermRect rect, void *user)
509{
510 term_T *term = (term_T *)user;
511
512 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
513 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
514 redraw_buf_later(term->tl_buffer, NOT_VALID);
515 return 1;
516}
517
518 static int
519handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
520{
521 term_T *term = (term_T *)user;
522
523 /* TODO */
524 redraw_buf_later(term->tl_buffer, NOT_VALID);
525 return 1;
526}
527
528 static int
529handle_movecursor(
530 VTermPos pos,
531 VTermPos oldpos UNUSED,
532 int visible UNUSED,
533 void *user)
534{
535 term_T *term = (term_T *)user;
536 win_T *wp;
537 int is_current = FALSE;
538
539 FOR_ALL_WINDOWS(wp)
540 {
541 if (wp->w_buffer == term->tl_buffer)
542 {
543 position_cursor(wp, &pos);
544 if (wp == curwin)
545 is_current = TRUE;
546 }
547 }
548
549 if (is_current)
550 {
551 setcursor();
552 out_flush();
553 }
554
555 return 1;
556}
557
558/*
559 * The job running in the terminal resized the terminal.
560 */
561 static int
562handle_resize(int rows, int cols, void *user)
563{
564 term_T *term = (term_T *)user;
565 win_T *wp;
566
567 term->tl_rows = rows;
568 term->tl_cols = cols;
569 FOR_ALL_WINDOWS(wp)
570 {
571 if (wp->w_buffer == term->tl_buffer)
572 {
573 win_setheight_win(rows, wp);
574 win_setwidth_win(cols, wp);
575 }
576 }
577
578 redraw_buf_later(term->tl_buffer, NOT_VALID);
579 return 1;
580}
581
582/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200583 * Called to update the window that contains the terminal.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200584 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200585 void
586term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +0200587{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200588 term_T *term = wp->w_buffer->b_term;
589 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200590 VTermScreen *screen = vterm_obtain_screen(vterm);
Bram Moolenaar58556cd2017-07-20 23:04:46 +0200591 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200592 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200593
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200594 /*
595 * If the window was resized a redraw will be triggered and we get here.
596 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
597 */
598 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
599 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
600 vterm_set_size(vterm,
601 term->tl_rows_fixed ? term->tl_rows : wp->w_height,
602 term->tl_cols_fixed ? term->tl_cols : wp->w_width);
Bram Moolenaar58556cd2017-07-20 23:04:46 +0200603
604 /* The cursor may have been moved when resizing. */
605 vterm_state_get_cursorpos(state, &pos);
606 position_cursor(wp, &pos);
607
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200608 /* TODO: Only redraw what changed. */
609 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +0200610 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200611 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200612 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +0200613
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200614 if (pos.row < term->tl_rows)
615 {
616 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200617 {
618 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200619 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200620
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200621 vterm_screen_get_cell(screen, pos, &cell);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200622 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200623 if (c == NUL)
624 {
625 ScreenLines[off] = ' ';
626 ScreenLinesUC[off] = NUL;
627 }
628 else
629 {
630#if defined(FEAT_MBYTE)
631 if (enc_utf8 && c >= 0x80)
632 ScreenLinesUC[off] = c;
633 else
634 ScreenLines[off] = c;
635#else
636 ScreenLines[off] = c;
637#endif
638 }
639 /* TODO: use cell.attrs and colors */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200640 ScreenAttrs[off] = 0;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200641
642 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200643 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200644 if (cell.width == 2)
645 {
646 ScreenLines[off] = ' ';
647 ScreenLinesUC[off] = NUL;
648 ++pos.col;
649 ++off;
650 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200651 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200652 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200653 else
654 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200655
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200656 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
657 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +0200658 }
659}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200660
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200661/*
662 * Set job options common for Unix and MS-Windows.
663 */
664 static void
665setup_job_options(jobopt_T *opt, int rows, int cols)
666{
667 clear_job_options(opt);
668 opt->jo_mode = MODE_RAW;
669 opt->jo_out_mode = MODE_RAW;
670 opt->jo_err_mode = MODE_RAW;
671 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
672 opt->jo_io[PART_OUT] = JIO_BUFFER;
673 opt->jo_io[PART_ERR] = JIO_BUFFER;
674 opt->jo_set |= JO_OUT_IO + (JO_OUT_IO << (PART_ERR - PART_OUT));
675 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
676 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +0200677 opt->jo_pty = TRUE;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200678 opt->jo_set |= JO_OUT_BUF + (JO_OUT_BUF << (PART_ERR - PART_OUT));
679 opt->jo_term_rows = rows;
680 opt->jo_term_cols = cols;
681}
682
683/*
684 * Create a new vterm and initialize it.
685 */
686 static void
687create_vterm(term_T *term, int rows, int cols)
688{
689 VTerm *vterm;
690 VTermScreen *screen;
691
692 vterm = vterm_new(rows, cols);
693 term->tl_vterm = vterm;
694 screen = vterm_obtain_screen(vterm);
695 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
696 /* TODO: depends on 'encoding'. */
697 vterm_set_utf8(vterm, 1);
698 /* Required to initialize most things. */
699 vterm_screen_reset(screen, 1 /* hard */);
700}
701
702# ifdef WIN3264
703
704#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
705#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
706
707void* (*winpty_config_new)(int, void*);
708void* (*winpty_open)(void*, void*);
709void* (*winpty_spawn_config_new)(int, void*, LPCWSTR, void*, void*, void*);
710BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
711void (*winpty_config_set_initial_size)(void*, int, int);
712LPCWSTR (*winpty_conin_name)(void*);
713LPCWSTR (*winpty_conout_name)(void*);
714LPCWSTR (*winpty_conerr_name)(void*);
715void (*winpty_free)(void*);
716void (*winpty_config_free)(void*);
717void (*winpty_spawn_config_free)(void*);
718void (*winpty_error_free)(void*);
719LPCWSTR (*winpty_error_msg)(void*);
720
721/**************************************
722 * 2. MS-Windows implementation.
723 */
724
725#define WINPTY_DLL "winpty.dll"
726
727static HINSTANCE hWinPtyDLL = NULL;
728
729 int
730dyn_winpty_init(void)
731{
732 int i;
733 static struct
734 {
735 char *name;
736 FARPROC *ptr;
737 } winpty_entry[] =
738 {
739 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
740 {"winpty_config_free", (FARPROC*)&winpty_config_free},
741 {"winpty_config_new", (FARPROC*)&winpty_config_new},
742 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
743 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
744 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
745 {"winpty_error_free", (FARPROC*)&winpty_error_free},
746 {"winpty_free", (FARPROC*)&winpty_free},
747 {"winpty_open", (FARPROC*)&winpty_open},
748 {"winpty_spawn", (FARPROC*)&winpty_spawn},
749 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
750 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
751 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
752 {NULL, NULL}
753 };
754
755 /* No need to initialize twice. */
756 if (hWinPtyDLL)
757 return 1;
758 /* Load winpty.dll */
759 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
760 if (!hWinPtyDLL)
761 {
762 EMSG2(_(e_loadlib), WINPTY_DLL);
763 return 0;
764 }
765 for (i = 0; winpty_entry[i].name != NULL
766 && winpty_entry[i].ptr != NULL; ++i)
767 {
768 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
769 winpty_entry[i].name)) == NULL)
770 {
771 EMSG2(_(e_loadfunc), winpty_entry[i].name);
772 return 0;
773 }
774 }
775
776 return 1;
777}
778
779/*
780 * Create a new terminal of "rows" by "cols" cells.
781 * Store a reference in "term".
782 * Return OK or FAIL.
783 */
784 static int
785term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
786{
787 WCHAR *p = enc_to_utf16(cmd, NULL);
788 channel_T *channel = NULL;
789 job_T *job = NULL;
790 jobopt_T opt;
791 DWORD error;
792 HANDLE jo = NULL, child_process_handle, child_thread_handle;
793 void *winpty_err;
794 void *spawn_config;
795
796 if (!dyn_winpty_init())
797 return FAIL;
798
799 if (p == NULL)
800 return FAIL;
801
802 job = job_alloc();
803 if (job == NULL)
804 goto failed;
805
806 channel = add_channel();
807 if (channel == NULL)
808 goto failed;
809
810 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
811 if (term->tl_winpty_config == NULL)
812 goto failed;
813
814 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
815 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
816 if (term->tl_winpty == NULL)
817 goto failed;
818
819 spawn_config = winpty_spawn_config_new(
820 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
821 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
822 NULL,
823 p,
824 NULL,
825 NULL,
826 &winpty_err);
827 if (spawn_config == NULL)
828 goto failed;
829
830 channel = add_channel();
831 if (channel == NULL)
832 goto failed;
833
834 job = job_alloc();
835 if (job == NULL)
836 goto failed;
837
838 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
839 &child_thread_handle, &error, &winpty_err))
840 goto failed;
841
842 channel_set_pipes(channel,
843 (sock_T) CreateFileW(
844 winpty_conin_name(term->tl_winpty),
845 GENERIC_WRITE, 0, NULL,
846 OPEN_EXISTING, 0, NULL),
847 (sock_T) CreateFileW(
848 winpty_conout_name(term->tl_winpty),
849 GENERIC_READ, 0, NULL,
850 OPEN_EXISTING, 0, NULL),
851 (sock_T) CreateFileW(
852 winpty_conerr_name(term->tl_winpty),
853 GENERIC_READ, 0, NULL,
854 OPEN_EXISTING, 0, NULL));
855
856 jo = CreateJobObject(NULL, NULL);
857 if (jo == NULL)
858 goto failed;
859
860 if (!AssignProcessToJobObject(jo, child_process_handle))
861 goto failed;
862
863 winpty_spawn_config_free(spawn_config);
864
865 create_vterm(term, rows, cols);
866
867 setup_job_options(&opt, rows, cols);
868 channel_set_job(channel, job, &opt);
869
870 job->jv_channel = channel;
871 job->jv_proc_info.hProcess = child_process_handle;
872 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
873 job->jv_job_object = jo;
874 job->jv_status = JOB_STARTED;
875 term->tl_job = job;
876
877 return OK;
878
879failed:
880 if (channel != NULL)
881 channel_clear(channel);
882 if (job != NULL)
883 job_cleanup(job);
884 if (jo != NULL)
885 CloseHandle(jo);
886 if (term->tl_winpty != NULL)
887 winpty_free(term->tl_winpty);
888 if (term->tl_winpty_config != NULL)
889 winpty_config_free(term->tl_winpty_config);
890 if (winpty_err != NULL)
891 {
892 char_u *msg = utf16_to_enc(
893 (short_u *)winpty_error_msg(winpty_err), NULL);
894
895 EMSG(msg);
896 winpty_error_free(winpty_err);
897 }
898 return FAIL;
899}
900
901/*
902 * Free the terminal emulator part of "term".
903 */
904 static void
905term_free(term_T *term)
906{
907 winpty_free(term->tl_winpty);
908 winpty_config_free(term->tl_winpty_config);
909 vterm_free(term->tl_vterm);
910}
911
912# else
913
914/**************************************
915 * 3. Unix-like implementation.
916 */
917
918/*
919 * Create a new terminal of "rows" by "cols" cells.
920 * Start job for "cmd".
921 * Store the pointers in "term".
922 * Return OK or FAIL.
923 */
924 static int
925term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
926{
927 typval_T argvars[2];
928 jobopt_T opt;
929
930 create_vterm(term, rows, cols);
931
932 argvars[0].v_type = VAR_STRING;
933 argvars[0].vval.v_string = cmd;
934 setup_job_options(&opt, rows, cols);
935 term->tl_job = job_start(argvars, &opt);
936
937 return term->tl_job != NULL ? OK : FAIL;
938}
939
940/*
941 * Free the terminal emulator part of "term".
942 */
943 static void
944term_free(term_T *term)
945{
946 vterm_free(term->tl_vterm);
947}
948# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200949
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200950#endif /* FEAT_TERMINAL */