blob: aafd7e89fa4a810844f9dfebc148030f500ac12b [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 Moolenaar9f1f49b2017-07-22 18:14:17 +020049 * - don't allow exiting Vim when a terminal is still running a job
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020050 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar938783d2017-07-16 20:13:26 +020051 * - command line completion for :terminal
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020052 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020053 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020054 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020055 * - implement "term" for job_start(): more job options when starting a
56 * terminal.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020057 * - implement ":buf {term-buf-name}"
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 Moolenaarcb8bbe92017-07-16 13:48:22 +020066 * - implement 'termkey'
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020067 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
68 * conversions.
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 Moolenaare4f25e42017-07-07 11:54:15 +0200101 /* Range of screen rows to update. Zero based. */
102 int tl_dirty_row_start; /* -1 if nothing dirty */
103 int tl_dirty_row_end; /* row below last one to update */
104
105 pos_T tl_cursor;
106};
107
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200108/*
109 * List of all active terminals.
110 */
111static term_T *first_term = NULL;
112
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200113
114#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
115#define KEY_BUF_LEN 200
116
117/*
118 * Functions with separate implementation for MS-Windows and Unix-like systems.
119 */
120static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd);
121static void term_free(term_T *term);
122
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200123/**************************************
124 * 1. Generic code for all systems.
125 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200126
127/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200128 * Determine the terminal size from 'termsize' and the current window.
129 * Assumes term->tl_rows and term->tl_cols are zero.
130 */
131 static void
132set_term_and_win_size(term_T *term)
133{
134 if (*curwin->w_p_tms != NUL)
135 {
136 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
137
138 term->tl_rows = atoi((char *)curwin->w_p_tms);
139 term->tl_cols = atoi((char *)p);
140 }
141 if (term->tl_rows == 0)
142 term->tl_rows = curwin->w_height;
143 else
144 {
145 win_setheight_win(term->tl_rows, curwin);
146 term->tl_rows_fixed = TRUE;
147 }
148 if (term->tl_cols == 0)
149 term->tl_cols = curwin->w_width;
150 else
151 {
152 win_setwidth_win(term->tl_cols, curwin);
153 term->tl_cols_fixed = TRUE;
154 }
155}
156
157/*
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200158 * ":terminal": open a terminal window and execute a job in it.
159 */
160 void
161ex_terminal(exarg_T *eap)
162{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200163 exarg_T split_ea;
164 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200165 term_T *term;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200166
167 if (check_restricted() || check_secure())
168 return;
169
170 term = (term_T *)alloc_clear(sizeof(term_T));
171 if (term == NULL)
172 return;
173 term->tl_dirty_row_end = MAX_ROW;
174
175 /* Open a new window or tab. */
176 vim_memset(&split_ea, 0, sizeof(split_ea));
177 split_ea.cmdidx = CMD_new;
178 split_ea.cmd = (char_u *)"new";
179 split_ea.arg = (char_u *)"";
180 ex_splitview(&split_ea);
181 if (curwin == old_curwin)
182 {
183 /* split failed */
184 vim_free(term);
185 return;
186 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200187 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200188 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200189
190 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200191 term->tl_next = first_term;
192 first_term = term;
193
194 /* TODO: set buffer type, hidden, etc. */
195
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200196 set_term_and_win_size(term);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200197
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200198 /* System dependent: setup the vterm and start the job in it. */
199 if (term_and_job_init(term, term->tl_rows, term->tl_cols, eap->arg) == OK)
200 {
201 /* store the size we ended up with */
202 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200203 }
204 else
205 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200206 /* Wiping out the buffer will also close the window and call
207 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200208 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200209 }
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200210
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200211 /* TODO: Setup pty, see mch_call_shell(). */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200212}
213
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200214/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200215 * Free a terminal and everything it refers to.
216 * Kills the job if there is one.
217 * Called when wiping out a buffer.
218 */
219 void
220free_terminal(term_T *term)
221{
222 term_T *tp;
223
224 if (term == NULL)
225 return;
226 if (first_term == term)
227 first_term = term->tl_next;
228 else
229 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
230 if (tp->tl_next == term)
231 {
232 tp->tl_next = term->tl_next;
233 break;
234 }
235
236 if (term->tl_job != NULL)
237 {
238 if (term->tl_job->jv_status != JOB_ENDED)
239 job_stop(term->tl_job, NULL, "kill");
240 job_unref(term->tl_job);
241 }
242
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200243 term_free(term);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200244 vim_free(term);
245}
246
247/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200248 * Write job output "msg[len]" to the vterm.
249 */
250 static void
251term_write_job_output(term_T *term, char_u *msg, size_t len)
252{
253 VTerm *vterm = term->tl_vterm;
254 char_u *p;
255 size_t done;
256 size_t len_now;
257
258 for (done = 0; done < len; done += len_now)
259 {
260 for (p = msg + done; p < msg + len; )
261 {
262 if (*p == NL)
263 break;
264 p += utf_ptr2len_len(p, len - (p - msg));
265 }
266 len_now = p - msg - done;
267 vterm_input_write(vterm, (char *)msg + done, len_now);
268 if (p < msg + len && *p == NL)
269 {
270 /* Convert NL to CR-NL, that appears to work best. */
271 vterm_input_write(vterm, "\r\n", 2);
272 ++len_now;
273 }
274 }
275
276 /* this invokes the damage callbacks */
277 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
278}
279
280/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200281 * Invoked when "msg" output from a job was received. Write it to the terminal
282 * of "buffer".
283 */
284 void
285write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
286{
287 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200288 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200289
290 ch_logn(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200291 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200292
293 /* TODO: only update once in a while. */
294 update_screen(0);
295 setcursor();
296 out_flush();
297}
298
299/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200300 * Convert typed key "c" into bytes to send to the job.
301 * Return the number of bytes in "buf".
302 */
303 static int
304term_convert_key(int c, char *buf)
305{
306 VTerm *vterm = curbuf->b_term->tl_vterm;
307 VTermKey key = VTERM_KEY_NONE;
308 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200309
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200310 switch (c)
311 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200312 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200313 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200314 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
315 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200316 case K_DEL: key = VTERM_KEY_DEL; break;
317 case K_DOWN: key = VTERM_KEY_DOWN; break;
318 case K_END: key = VTERM_KEY_END; break;
319 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
320 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
321 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
322 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
323 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
324 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
325 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
326 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
327 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
328 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
329 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
330 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
331 case K_HOME: key = VTERM_KEY_HOME; break;
332 case K_INS: key = VTERM_KEY_INS; break;
333 case K_K0: key = VTERM_KEY_KP_0; break;
334 case K_K1: key = VTERM_KEY_KP_1; break;
335 case K_K2: key = VTERM_KEY_KP_2; break;
336 case K_K3: key = VTERM_KEY_KP_3; break;
337 case K_K4: key = VTERM_KEY_KP_4; break;
338 case K_K5: key = VTERM_KEY_KP_5; break;
339 case K_K6: key = VTERM_KEY_KP_6; break;
340 case K_K7: key = VTERM_KEY_KP_7; break;
341 case K_K8: key = VTERM_KEY_KP_8; break;
342 case K_K9: key = VTERM_KEY_KP_9; break;
343 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
344 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
345 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
346 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
347 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
348 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
349 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
350 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
351 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
352 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
353 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
354 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
355 case K_LEFT: key = VTERM_KEY_LEFT; break;
356 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
357 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
358 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
359 case K_UP: key = VTERM_KEY_UP; break;
360 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200361
362 case K_MOUSEUP: /* TODO */ break;
363 case K_MOUSEDOWN: /* TODO */ break;
364 case K_MOUSELEFT: /* TODO */ break;
365 case K_MOUSERIGHT: /* TODO */ break;
366
367 case K_LEFTMOUSE: /* TODO */ break;
368 case K_LEFTMOUSE_NM: /* TODO */ break;
369 case K_LEFTDRAG: /* TODO */ break;
370 case K_LEFTRELEASE: /* TODO */ break;
371 case K_LEFTRELEASE_NM: /* TODO */ break;
372 case K_MIDDLEMOUSE: /* TODO */ break;
373 case K_MIDDLEDRAG: /* TODO */ break;
374 case K_MIDDLERELEASE: /* TODO */ break;
375 case K_RIGHTMOUSE: /* TODO */ break;
376 case K_RIGHTDRAG: /* TODO */ break;
377 case K_RIGHTRELEASE: /* TODO */ break;
378 case K_X1MOUSE: /* TODO */ break;
379 case K_X1DRAG: /* TODO */ break;
380 case K_X1RELEASE: /* TODO */ break;
381 case K_X2MOUSE: /* TODO */ break;
382 case K_X2DRAG: /* TODO */ break;
383 case K_X2RELEASE: /* TODO */ break;
384
385 /* TODO: handle all special keys and modifiers that terminal_loop()
386 * does not handle. */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200387 }
388
389 /*
390 * Convert special keys to vterm keys:
391 * - Write keys to vterm: vterm_keyboard_key()
392 * - Write output to channel.
393 */
394 if (key != VTERM_KEY_NONE)
395 /* Special key, let vterm convert it. */
396 vterm_keyboard_key(vterm, key, mod);
397 else
398 /* Normal character, let vterm convert it. */
399 vterm_keyboard_unichar(vterm, c, mod);
400
401 /* Read back the converted escape sequence. */
402 return vterm_output_read(vterm, buf, KEY_BUF_LEN);
403}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200404
Bram Moolenaar938783d2017-07-16 20:13:26 +0200405/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200406 * Wait for input and send it to the job.
407 * Return when the start of a CTRL-W command is typed or anything else that
408 * should be handled as a Normal mode command.
409 */
410 void
411terminal_loop(void)
412{
413 char buf[KEY_BUF_LEN];
414 int c;
415 size_t len;
416 static int mouse_was_outside = FALSE;
417 int dragging_outside = FALSE;
418
419 for (;;)
420 {
421 /* TODO: skip screen update when handling a sequence of keys. */
422 update_screen(0);
423 setcursor();
424 out_flush();
425 ++no_mapping;
426 ++allow_keys;
427 got_int = FALSE;
428 c = vgetc();
429 --no_mapping;
430 --allow_keys;
431
432 /* Catch keys that need to be handled as in Normal mode. */
433 switch (c)
434 {
435 case Ctrl_W:
436 case NUL:
437 case K_ZERO:
438 stuffcharReadbuff(c);
439 return;
440
441 case K_IGNORE: continue;
442
443 case K_LEFTDRAG:
444 case K_MIDDLEDRAG:
445 case K_RIGHTDRAG:
446 case K_X1DRAG:
447 case K_X2DRAG:
448 dragging_outside = mouse_was_outside;
449 /* FALLTHROUGH */
450 case K_LEFTMOUSE:
451 case K_LEFTMOUSE_NM:
452 case K_LEFTRELEASE:
453 case K_LEFTRELEASE_NM:
454 case K_MIDDLEMOUSE:
455 case K_MIDDLERELEASE:
456 case K_RIGHTMOUSE:
457 case K_RIGHTRELEASE:
458 case K_X1MOUSE:
459 case K_X1RELEASE:
460 case K_X2MOUSE:
461 case K_X2RELEASE:
462 if (mouse_row < W_WINROW(curwin)
463 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
464 || mouse_col < W_WINCOL(curwin)
465 || mouse_col >= W_ENDCOL(curwin)
466 || dragging_outside)
467 {
468 /* click outside the current window */
469 stuffcharReadbuff(c);
470 mouse_was_outside = TRUE;
471 return;
472 }
473 }
474 mouse_was_outside = FALSE;
475
476 /* Convert the typed key to a sequence of bytes for the job. */
477 len = term_convert_key(c, buf);
478 if (len > 0)
479 /* TODO: if FAIL is returned, stop? */
480 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
481 (char_u *)buf, len, NULL);
482 }
483}
484
485 static void
486position_cursor(win_T *wp, VTermPos *pos)
487{
488 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
489 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
490}
491
492static int handle_damage(VTermRect rect, void *user);
493static int handle_moverect(VTermRect dest, VTermRect src, void *user);
494static int handle_movecursor(VTermPos pos, VTermPos oldpos, int visible, void *user);
495static int handle_resize(int rows, int cols, void *user);
496
497static VTermScreenCallbacks screen_callbacks = {
498 handle_damage, /* damage */
499 handle_moverect, /* moverect */
500 handle_movecursor, /* movecursor */
501 NULL, /* settermprop */
502 NULL, /* bell */
503 handle_resize, /* resize */
504 NULL, /* sb_pushline */
505 NULL /* sb_popline */
506};
507
508 static int
509handle_damage(VTermRect rect, void *user)
510{
511 term_T *term = (term_T *)user;
512
513 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
514 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
515 redraw_buf_later(term->tl_buffer, NOT_VALID);
516 return 1;
517}
518
519 static int
520handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
521{
522 term_T *term = (term_T *)user;
523
524 /* TODO */
525 redraw_buf_later(term->tl_buffer, NOT_VALID);
526 return 1;
527}
528
529 static int
530handle_movecursor(
531 VTermPos pos,
532 VTermPos oldpos UNUSED,
533 int visible UNUSED,
534 void *user)
535{
536 term_T *term = (term_T *)user;
537 win_T *wp;
538 int is_current = FALSE;
539
540 FOR_ALL_WINDOWS(wp)
541 {
542 if (wp->w_buffer == term->tl_buffer)
543 {
544 position_cursor(wp, &pos);
545 if (wp == curwin)
546 is_current = TRUE;
547 }
548 }
549
550 if (is_current)
551 {
552 setcursor();
553 out_flush();
554 }
555
556 return 1;
557}
558
559/*
560 * The job running in the terminal resized the terminal.
561 */
562 static int
563handle_resize(int rows, int cols, void *user)
564{
565 term_T *term = (term_T *)user;
566 win_T *wp;
567
568 term->tl_rows = rows;
569 term->tl_cols = cols;
570 FOR_ALL_WINDOWS(wp)
571 {
572 if (wp->w_buffer == term->tl_buffer)
573 {
574 win_setheight_win(rows, wp);
575 win_setwidth_win(cols, wp);
576 }
577 }
578
579 redraw_buf_later(term->tl_buffer, NOT_VALID);
580 return 1;
581}
582
583/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200584 * Called to update the window that contains the terminal.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200585 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200586 void
587term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +0200588{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200589 term_T *term = wp->w_buffer->b_term;
590 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200591 VTermScreen *screen = vterm_obtain_screen(vterm);
Bram Moolenaar58556cd2017-07-20 23:04:46 +0200592 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200593 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200594
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200595 /*
596 * If the window was resized a redraw will be triggered and we get here.
597 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
598 */
599 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
600 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
601 vterm_set_size(vterm,
602 term->tl_rows_fixed ? term->tl_rows : wp->w_height,
603 term->tl_cols_fixed ? term->tl_cols : wp->w_width);
Bram Moolenaar58556cd2017-07-20 23:04:46 +0200604
605 /* The cursor may have been moved when resizing. */
606 vterm_state_get_cursorpos(state, &pos);
607 position_cursor(wp, &pos);
608
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200609 /* TODO: Only redraw what changed. */
610 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +0200611 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200612 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200613 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +0200614
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200615 if (pos.row < term->tl_rows)
616 {
617 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200618 {
619 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200620 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200621
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200622 vterm_screen_get_cell(screen, pos, &cell);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200623 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200624 if (c == NUL)
625 {
626 ScreenLines[off] = ' ';
627 ScreenLinesUC[off] = NUL;
628 }
629 else
630 {
631#if defined(FEAT_MBYTE)
632 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200633 {
634 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200635 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200636 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200637 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200638 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200639 ScreenLines[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200640 ScreenLinesUC[off] = NUL;
641 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200642#else
643 ScreenLines[off] = c;
644#endif
645 }
646 /* TODO: use cell.attrs and colors */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200647 ScreenAttrs[off] = 0;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200648
649 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200650 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200651 if (cell.width == 2)
652 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200653 ScreenLines[off] = NUL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200654 ScreenLinesUC[off] = NUL;
655 ++pos.col;
656 ++off;
657 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200658 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200659 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200660 else
661 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200662
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200663 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
664 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +0200665 }
666}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200667
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200668/*
669 * Set job options common for Unix and MS-Windows.
670 */
671 static void
672setup_job_options(jobopt_T *opt, int rows, int cols)
673{
674 clear_job_options(opt);
675 opt->jo_mode = MODE_RAW;
676 opt->jo_out_mode = MODE_RAW;
677 opt->jo_err_mode = MODE_RAW;
678 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
679 opt->jo_io[PART_OUT] = JIO_BUFFER;
680 opt->jo_io[PART_ERR] = JIO_BUFFER;
681 opt->jo_set |= JO_OUT_IO + (JO_OUT_IO << (PART_ERR - PART_OUT));
682 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
683 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +0200684 opt->jo_pty = TRUE;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200685 opt->jo_set |= JO_OUT_BUF + (JO_OUT_BUF << (PART_ERR - PART_OUT));
686 opt->jo_term_rows = rows;
687 opt->jo_term_cols = cols;
688}
689
690/*
691 * Create a new vterm and initialize it.
692 */
693 static void
694create_vterm(term_T *term, int rows, int cols)
695{
696 VTerm *vterm;
697 VTermScreen *screen;
698
699 vterm = vterm_new(rows, cols);
700 term->tl_vterm = vterm;
701 screen = vterm_obtain_screen(vterm);
702 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
703 /* TODO: depends on 'encoding'. */
704 vterm_set_utf8(vterm, 1);
705 /* Required to initialize most things. */
706 vterm_screen_reset(screen, 1 /* hard */);
707}
708
709# ifdef WIN3264
710
711#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
712#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
713
714void* (*winpty_config_new)(int, void*);
715void* (*winpty_open)(void*, void*);
716void* (*winpty_spawn_config_new)(int, void*, LPCWSTR, void*, void*, void*);
717BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
718void (*winpty_config_set_initial_size)(void*, int, int);
719LPCWSTR (*winpty_conin_name)(void*);
720LPCWSTR (*winpty_conout_name)(void*);
721LPCWSTR (*winpty_conerr_name)(void*);
722void (*winpty_free)(void*);
723void (*winpty_config_free)(void*);
724void (*winpty_spawn_config_free)(void*);
725void (*winpty_error_free)(void*);
726LPCWSTR (*winpty_error_msg)(void*);
727
728/**************************************
729 * 2. MS-Windows implementation.
730 */
731
732#define WINPTY_DLL "winpty.dll"
733
734static HINSTANCE hWinPtyDLL = NULL;
735
736 int
737dyn_winpty_init(void)
738{
739 int i;
740 static struct
741 {
742 char *name;
743 FARPROC *ptr;
744 } winpty_entry[] =
745 {
746 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
747 {"winpty_config_free", (FARPROC*)&winpty_config_free},
748 {"winpty_config_new", (FARPROC*)&winpty_config_new},
749 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
750 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
751 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
752 {"winpty_error_free", (FARPROC*)&winpty_error_free},
753 {"winpty_free", (FARPROC*)&winpty_free},
754 {"winpty_open", (FARPROC*)&winpty_open},
755 {"winpty_spawn", (FARPROC*)&winpty_spawn},
756 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
757 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
758 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
759 {NULL, NULL}
760 };
761
762 /* No need to initialize twice. */
763 if (hWinPtyDLL)
764 return 1;
765 /* Load winpty.dll */
766 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
767 if (!hWinPtyDLL)
768 {
769 EMSG2(_(e_loadlib), WINPTY_DLL);
770 return 0;
771 }
772 for (i = 0; winpty_entry[i].name != NULL
773 && winpty_entry[i].ptr != NULL; ++i)
774 {
775 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
776 winpty_entry[i].name)) == NULL)
777 {
778 EMSG2(_(e_loadfunc), winpty_entry[i].name);
779 return 0;
780 }
781 }
782
783 return 1;
784}
785
786/*
787 * Create a new terminal of "rows" by "cols" cells.
788 * Store a reference in "term".
789 * Return OK or FAIL.
790 */
791 static int
792term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
793{
794 WCHAR *p = enc_to_utf16(cmd, NULL);
795 channel_T *channel = NULL;
796 job_T *job = NULL;
797 jobopt_T opt;
798 DWORD error;
799 HANDLE jo = NULL, child_process_handle, child_thread_handle;
800 void *winpty_err;
801 void *spawn_config;
802
803 if (!dyn_winpty_init())
804 return FAIL;
805
806 if (p == NULL)
807 return FAIL;
808
809 job = job_alloc();
810 if (job == NULL)
811 goto failed;
812
813 channel = add_channel();
814 if (channel == NULL)
815 goto failed;
816
817 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
818 if (term->tl_winpty_config == NULL)
819 goto failed;
820
821 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
822 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
823 if (term->tl_winpty == NULL)
824 goto failed;
825
826 spawn_config = winpty_spawn_config_new(
827 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
828 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
829 NULL,
830 p,
831 NULL,
832 NULL,
833 &winpty_err);
834 if (spawn_config == NULL)
835 goto failed;
836
837 channel = add_channel();
838 if (channel == NULL)
839 goto failed;
840
841 job = job_alloc();
842 if (job == NULL)
843 goto failed;
844
845 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
846 &child_thread_handle, &error, &winpty_err))
847 goto failed;
848
849 channel_set_pipes(channel,
850 (sock_T) CreateFileW(
851 winpty_conin_name(term->tl_winpty),
852 GENERIC_WRITE, 0, NULL,
853 OPEN_EXISTING, 0, NULL),
854 (sock_T) CreateFileW(
855 winpty_conout_name(term->tl_winpty),
856 GENERIC_READ, 0, NULL,
857 OPEN_EXISTING, 0, NULL),
858 (sock_T) CreateFileW(
859 winpty_conerr_name(term->tl_winpty),
860 GENERIC_READ, 0, NULL,
861 OPEN_EXISTING, 0, NULL));
862
863 jo = CreateJobObject(NULL, NULL);
864 if (jo == NULL)
865 goto failed;
866
867 if (!AssignProcessToJobObject(jo, child_process_handle))
868 goto failed;
869
870 winpty_spawn_config_free(spawn_config);
871
872 create_vterm(term, rows, cols);
873
874 setup_job_options(&opt, rows, cols);
875 channel_set_job(channel, job, &opt);
876
877 job->jv_channel = channel;
878 job->jv_proc_info.hProcess = child_process_handle;
879 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
880 job->jv_job_object = jo;
881 job->jv_status = JOB_STARTED;
882 term->tl_job = job;
883
884 return OK;
885
886failed:
887 if (channel != NULL)
888 channel_clear(channel);
889 if (job != NULL)
890 job_cleanup(job);
891 if (jo != NULL)
892 CloseHandle(jo);
893 if (term->tl_winpty != NULL)
894 winpty_free(term->tl_winpty);
895 if (term->tl_winpty_config != NULL)
896 winpty_config_free(term->tl_winpty_config);
897 if (winpty_err != NULL)
898 {
899 char_u *msg = utf16_to_enc(
900 (short_u *)winpty_error_msg(winpty_err), NULL);
901
902 EMSG(msg);
903 winpty_error_free(winpty_err);
904 }
905 return FAIL;
906}
907
908/*
909 * Free the terminal emulator part of "term".
910 */
911 static void
912term_free(term_T *term)
913{
914 winpty_free(term->tl_winpty);
915 winpty_config_free(term->tl_winpty_config);
916 vterm_free(term->tl_vterm);
917}
918
919# else
920
921/**************************************
922 * 3. Unix-like implementation.
923 */
924
925/*
926 * Create a new terminal of "rows" by "cols" cells.
927 * Start job for "cmd".
928 * Store the pointers in "term".
929 * Return OK or FAIL.
930 */
931 static int
932term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
933{
934 typval_T argvars[2];
935 jobopt_T opt;
936
937 create_vterm(term, rows, cols);
938
939 argvars[0].v_type = VAR_STRING;
940 argvars[0].vval.v_string = cmd;
941 setup_job_options(&opt, rows, cols);
942 term->tl_job = job_start(argvars, &opt);
943
944 return term->tl_job != NULL ? OK : FAIL;
945}
946
947/*
948 * Free the terminal emulator part of "term".
949 */
950 static void
951term_free(term_T *term)
952{
953 vterm_free(term->tl_vterm);
954}
955# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200956
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200957#endif /* FEAT_TERMINAL */