blob: 7d807acbcb1049b25246a1387477ab72d7db3219 [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 Moolenaar61a66052017-07-22 18:39:00 +0200206 free_terminal(term);
207 curbuf->b_term = NULL;
208
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200209 /* Wiping out the buffer will also close the window and call
210 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200211 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200212 }
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200213
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200214 /* TODO: Setup pty, see mch_call_shell(). */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200215}
216
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200217/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200218 * Free a terminal and everything it refers to.
219 * Kills the job if there is one.
220 * Called when wiping out a buffer.
221 */
222 void
223free_terminal(term_T *term)
224{
225 term_T *tp;
226
227 if (term == NULL)
228 return;
229 if (first_term == term)
230 first_term = term->tl_next;
231 else
232 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
233 if (tp->tl_next == term)
234 {
235 tp->tl_next = term->tl_next;
236 break;
237 }
238
239 if (term->tl_job != NULL)
240 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200241 if (term->tl_job->jv_status != JOB_ENDED
242 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200243 job_stop(term->tl_job, NULL, "kill");
244 job_unref(term->tl_job);
245 }
246
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200247 term_free(term);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200248 vim_free(term);
249}
250
251/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200252 * Write job output "msg[len]" to the vterm.
253 */
254 static void
255term_write_job_output(term_T *term, char_u *msg, size_t len)
256{
257 VTerm *vterm = term->tl_vterm;
258 char_u *p;
259 size_t done;
260 size_t len_now;
261
262 for (done = 0; done < len; done += len_now)
263 {
264 for (p = msg + done; p < msg + len; )
265 {
266 if (*p == NL)
267 break;
268 p += utf_ptr2len_len(p, len - (p - msg));
269 }
270 len_now = p - msg - done;
271 vterm_input_write(vterm, (char *)msg + done, len_now);
272 if (p < msg + len && *p == NL)
273 {
274 /* Convert NL to CR-NL, that appears to work best. */
275 vterm_input_write(vterm, "\r\n", 2);
276 ++len_now;
277 }
278 }
279
280 /* this invokes the damage callbacks */
281 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
282}
283
284/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200285 * Invoked when "msg" output from a job was received. Write it to the terminal
286 * of "buffer".
287 */
288 void
289write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
290{
291 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200292 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200293
294 ch_logn(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200295 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200296
297 /* TODO: only update once in a while. */
298 update_screen(0);
299 setcursor();
300 out_flush();
301}
302
303/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200304 * Convert typed key "c" into bytes to send to the job.
305 * Return the number of bytes in "buf".
306 */
307 static int
308term_convert_key(int c, char *buf)
309{
310 VTerm *vterm = curbuf->b_term->tl_vterm;
311 VTermKey key = VTERM_KEY_NONE;
312 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200313
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200314 switch (c)
315 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200316 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200317 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200318 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
319 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200320 case K_DEL: key = VTERM_KEY_DEL; break;
321 case K_DOWN: key = VTERM_KEY_DOWN; break;
322 case K_END: key = VTERM_KEY_END; break;
323 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
324 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
325 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
326 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
327 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
328 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
329 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
330 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
331 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
332 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
333 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
334 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
335 case K_HOME: key = VTERM_KEY_HOME; break;
336 case K_INS: key = VTERM_KEY_INS; break;
337 case K_K0: key = VTERM_KEY_KP_0; break;
338 case K_K1: key = VTERM_KEY_KP_1; break;
339 case K_K2: key = VTERM_KEY_KP_2; break;
340 case K_K3: key = VTERM_KEY_KP_3; break;
341 case K_K4: key = VTERM_KEY_KP_4; break;
342 case K_K5: key = VTERM_KEY_KP_5; break;
343 case K_K6: key = VTERM_KEY_KP_6; break;
344 case K_K7: key = VTERM_KEY_KP_7; break;
345 case K_K8: key = VTERM_KEY_KP_8; break;
346 case K_K9: key = VTERM_KEY_KP_9; break;
347 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
348 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
349 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
350 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
351 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
352 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
353 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
354 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
355 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
356 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
357 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
358 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
359 case K_LEFT: key = VTERM_KEY_LEFT; break;
360 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
361 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
362 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
363 case K_UP: key = VTERM_KEY_UP; break;
364 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200365
366 case K_MOUSEUP: /* TODO */ break;
367 case K_MOUSEDOWN: /* TODO */ break;
368 case K_MOUSELEFT: /* TODO */ break;
369 case K_MOUSERIGHT: /* TODO */ break;
370
371 case K_LEFTMOUSE: /* TODO */ break;
372 case K_LEFTMOUSE_NM: /* TODO */ break;
373 case K_LEFTDRAG: /* TODO */ break;
374 case K_LEFTRELEASE: /* TODO */ break;
375 case K_LEFTRELEASE_NM: /* TODO */ break;
376 case K_MIDDLEMOUSE: /* TODO */ break;
377 case K_MIDDLEDRAG: /* TODO */ break;
378 case K_MIDDLERELEASE: /* TODO */ break;
379 case K_RIGHTMOUSE: /* TODO */ break;
380 case K_RIGHTDRAG: /* TODO */ break;
381 case K_RIGHTRELEASE: /* TODO */ break;
382 case K_X1MOUSE: /* TODO */ break;
383 case K_X1DRAG: /* TODO */ break;
384 case K_X1RELEASE: /* TODO */ break;
385 case K_X2MOUSE: /* TODO */ break;
386 case K_X2DRAG: /* TODO */ break;
387 case K_X2RELEASE: /* TODO */ break;
388
389 /* TODO: handle all special keys and modifiers that terminal_loop()
390 * does not handle. */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200391 }
392
393 /*
394 * Convert special keys to vterm keys:
395 * - Write keys to vterm: vterm_keyboard_key()
396 * - Write output to channel.
397 */
398 if (key != VTERM_KEY_NONE)
399 /* Special key, let vterm convert it. */
400 vterm_keyboard_key(vterm, key, mod);
401 else
402 /* Normal character, let vterm convert it. */
403 vterm_keyboard_unichar(vterm, c, mod);
404
405 /* Read back the converted escape sequence. */
406 return vterm_output_read(vterm, buf, KEY_BUF_LEN);
407}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200408
Bram Moolenaar938783d2017-07-16 20:13:26 +0200409/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200410 * Wait for input and send it to the job.
411 * Return when the start of a CTRL-W command is typed or anything else that
412 * should be handled as a Normal mode command.
413 */
414 void
415terminal_loop(void)
416{
417 char buf[KEY_BUF_LEN];
418 int c;
419 size_t len;
420 static int mouse_was_outside = FALSE;
421 int dragging_outside = FALSE;
422
423 for (;;)
424 {
425 /* TODO: skip screen update when handling a sequence of keys. */
426 update_screen(0);
427 setcursor();
428 out_flush();
429 ++no_mapping;
430 ++allow_keys;
431 got_int = FALSE;
432 c = vgetc();
433 --no_mapping;
434 --allow_keys;
435
436 /* Catch keys that need to be handled as in Normal mode. */
437 switch (c)
438 {
439 case Ctrl_W:
440 case NUL:
441 case K_ZERO:
442 stuffcharReadbuff(c);
443 return;
444
445 case K_IGNORE: continue;
446
447 case K_LEFTDRAG:
448 case K_MIDDLEDRAG:
449 case K_RIGHTDRAG:
450 case K_X1DRAG:
451 case K_X2DRAG:
452 dragging_outside = mouse_was_outside;
453 /* FALLTHROUGH */
454 case K_LEFTMOUSE:
455 case K_LEFTMOUSE_NM:
456 case K_LEFTRELEASE:
457 case K_LEFTRELEASE_NM:
458 case K_MIDDLEMOUSE:
459 case K_MIDDLERELEASE:
460 case K_RIGHTMOUSE:
461 case K_RIGHTRELEASE:
462 case K_X1MOUSE:
463 case K_X1RELEASE:
464 case K_X2MOUSE:
465 case K_X2RELEASE:
466 if (mouse_row < W_WINROW(curwin)
467 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
468 || mouse_col < W_WINCOL(curwin)
469 || mouse_col >= W_ENDCOL(curwin)
470 || dragging_outside)
471 {
472 /* click outside the current window */
473 stuffcharReadbuff(c);
474 mouse_was_outside = TRUE;
475 return;
476 }
477 }
478 mouse_was_outside = FALSE;
479
480 /* Convert the typed key to a sequence of bytes for the job. */
481 len = term_convert_key(c, buf);
482 if (len > 0)
483 /* TODO: if FAIL is returned, stop? */
484 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
485 (char_u *)buf, len, NULL);
486 }
487}
488
489 static void
490position_cursor(win_T *wp, VTermPos *pos)
491{
492 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
493 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
494}
495
496static int handle_damage(VTermRect rect, void *user);
497static int handle_moverect(VTermRect dest, VTermRect src, void *user);
498static int handle_movecursor(VTermPos pos, VTermPos oldpos, int visible, void *user);
499static int handle_resize(int rows, int cols, void *user);
500
501static VTermScreenCallbacks screen_callbacks = {
502 handle_damage, /* damage */
503 handle_moverect, /* moverect */
504 handle_movecursor, /* movecursor */
505 NULL, /* settermprop */
506 NULL, /* bell */
507 handle_resize, /* resize */
508 NULL, /* sb_pushline */
509 NULL /* sb_popline */
510};
511
512 static int
513handle_damage(VTermRect rect, void *user)
514{
515 term_T *term = (term_T *)user;
516
517 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
518 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
519 redraw_buf_later(term->tl_buffer, NOT_VALID);
520 return 1;
521}
522
523 static int
524handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
525{
526 term_T *term = (term_T *)user;
527
528 /* TODO */
529 redraw_buf_later(term->tl_buffer, NOT_VALID);
530 return 1;
531}
532
533 static int
534handle_movecursor(
535 VTermPos pos,
536 VTermPos oldpos UNUSED,
537 int visible UNUSED,
538 void *user)
539{
540 term_T *term = (term_T *)user;
541 win_T *wp;
542 int is_current = FALSE;
543
544 FOR_ALL_WINDOWS(wp)
545 {
546 if (wp->w_buffer == term->tl_buffer)
547 {
548 position_cursor(wp, &pos);
549 if (wp == curwin)
550 is_current = TRUE;
551 }
552 }
553
554 if (is_current)
555 {
556 setcursor();
557 out_flush();
558 }
559
560 return 1;
561}
562
563/*
564 * The job running in the terminal resized the terminal.
565 */
566 static int
567handle_resize(int rows, int cols, void *user)
568{
569 term_T *term = (term_T *)user;
570 win_T *wp;
571
572 term->tl_rows = rows;
573 term->tl_cols = cols;
574 FOR_ALL_WINDOWS(wp)
575 {
576 if (wp->w_buffer == term->tl_buffer)
577 {
578 win_setheight_win(rows, wp);
579 win_setwidth_win(cols, wp);
580 }
581 }
582
583 redraw_buf_later(term->tl_buffer, NOT_VALID);
584 return 1;
585}
586
587/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200588 * Called to update the window that contains the terminal.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200589 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200590 void
591term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +0200592{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200593 term_T *term = wp->w_buffer->b_term;
594 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200595 VTermScreen *screen = vterm_obtain_screen(vterm);
Bram Moolenaar58556cd2017-07-20 23:04:46 +0200596 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200597 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200598
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200599 /*
600 * If the window was resized a redraw will be triggered and we get here.
601 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
602 */
603 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
604 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
605 vterm_set_size(vterm,
606 term->tl_rows_fixed ? term->tl_rows : wp->w_height,
607 term->tl_cols_fixed ? term->tl_cols : wp->w_width);
Bram Moolenaar58556cd2017-07-20 23:04:46 +0200608
609 /* The cursor may have been moved when resizing. */
610 vterm_state_get_cursorpos(state, &pos);
611 position_cursor(wp, &pos);
612
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200613 /* TODO: Only redraw what changed. */
614 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +0200615 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200616 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200617 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +0200618
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200619 if (pos.row < term->tl_rows)
620 {
621 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200622 {
623 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200624 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200625
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200626 vterm_screen_get_cell(screen, pos, &cell);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200627 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200628 if (c == NUL)
629 {
630 ScreenLines[off] = ' ';
631 ScreenLinesUC[off] = NUL;
632 }
633 else
634 {
635#if defined(FEAT_MBYTE)
636 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200637 {
638 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200639 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200640 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200641 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200642 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200643 ScreenLines[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200644 ScreenLinesUC[off] = NUL;
645 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200646#else
647 ScreenLines[off] = c;
648#endif
649 }
650 /* TODO: use cell.attrs and colors */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200651 ScreenAttrs[off] = 0;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200652
653 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200654 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200655 if (cell.width == 2)
656 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200657 ScreenLines[off] = NUL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200658 ScreenLinesUC[off] = NUL;
659 ++pos.col;
660 ++off;
661 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200662 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200663 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200664 else
665 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200666
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200667 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
668 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +0200669 }
670}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200671
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200672/*
673 * Set job options common for Unix and MS-Windows.
674 */
675 static void
676setup_job_options(jobopt_T *opt, int rows, int cols)
677{
678 clear_job_options(opt);
679 opt->jo_mode = MODE_RAW;
680 opt->jo_out_mode = MODE_RAW;
681 opt->jo_err_mode = MODE_RAW;
682 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
683 opt->jo_io[PART_OUT] = JIO_BUFFER;
684 opt->jo_io[PART_ERR] = JIO_BUFFER;
685 opt->jo_set |= JO_OUT_IO + (JO_OUT_IO << (PART_ERR - PART_OUT));
686 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
687 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +0200688 opt->jo_pty = TRUE;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200689 opt->jo_set |= JO_OUT_BUF + (JO_OUT_BUF << (PART_ERR - PART_OUT));
690 opt->jo_term_rows = rows;
691 opt->jo_term_cols = cols;
692}
693
694/*
695 * Create a new vterm and initialize it.
696 */
697 static void
698create_vterm(term_T *term, int rows, int cols)
699{
700 VTerm *vterm;
701 VTermScreen *screen;
702
703 vterm = vterm_new(rows, cols);
704 term->tl_vterm = vterm;
705 screen = vterm_obtain_screen(vterm);
706 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
707 /* TODO: depends on 'encoding'. */
708 vterm_set_utf8(vterm, 1);
709 /* Required to initialize most things. */
710 vterm_screen_reset(screen, 1 /* hard */);
711}
712
713# ifdef WIN3264
714
715#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
716#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
717
718void* (*winpty_config_new)(int, void*);
719void* (*winpty_open)(void*, void*);
720void* (*winpty_spawn_config_new)(int, void*, LPCWSTR, void*, void*, void*);
721BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
722void (*winpty_config_set_initial_size)(void*, int, int);
723LPCWSTR (*winpty_conin_name)(void*);
724LPCWSTR (*winpty_conout_name)(void*);
725LPCWSTR (*winpty_conerr_name)(void*);
726void (*winpty_free)(void*);
727void (*winpty_config_free)(void*);
728void (*winpty_spawn_config_free)(void*);
729void (*winpty_error_free)(void*);
730LPCWSTR (*winpty_error_msg)(void*);
731
732/**************************************
733 * 2. MS-Windows implementation.
734 */
735
736#define WINPTY_DLL "winpty.dll"
737
738static HINSTANCE hWinPtyDLL = NULL;
739
740 int
741dyn_winpty_init(void)
742{
743 int i;
744 static struct
745 {
746 char *name;
747 FARPROC *ptr;
748 } winpty_entry[] =
749 {
750 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
751 {"winpty_config_free", (FARPROC*)&winpty_config_free},
752 {"winpty_config_new", (FARPROC*)&winpty_config_new},
753 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
754 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
755 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
756 {"winpty_error_free", (FARPROC*)&winpty_error_free},
757 {"winpty_free", (FARPROC*)&winpty_free},
758 {"winpty_open", (FARPROC*)&winpty_open},
759 {"winpty_spawn", (FARPROC*)&winpty_spawn},
760 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
761 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
762 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
763 {NULL, NULL}
764 };
765
766 /* No need to initialize twice. */
767 if (hWinPtyDLL)
768 return 1;
769 /* Load winpty.dll */
770 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
771 if (!hWinPtyDLL)
772 {
773 EMSG2(_(e_loadlib), WINPTY_DLL);
774 return 0;
775 }
776 for (i = 0; winpty_entry[i].name != NULL
777 && winpty_entry[i].ptr != NULL; ++i)
778 {
779 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
780 winpty_entry[i].name)) == NULL)
781 {
782 EMSG2(_(e_loadfunc), winpty_entry[i].name);
783 return 0;
784 }
785 }
786
787 return 1;
788}
789
790/*
791 * Create a new terminal of "rows" by "cols" cells.
792 * Store a reference in "term".
793 * Return OK or FAIL.
794 */
795 static int
796term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
797{
798 WCHAR *p = enc_to_utf16(cmd, NULL);
799 channel_T *channel = NULL;
800 job_T *job = NULL;
801 jobopt_T opt;
802 DWORD error;
803 HANDLE jo = NULL, child_process_handle, child_thread_handle;
804 void *winpty_err;
805 void *spawn_config;
806
807 if (!dyn_winpty_init())
808 return FAIL;
809
810 if (p == NULL)
811 return FAIL;
812
813 job = job_alloc();
814 if (job == NULL)
815 goto failed;
816
817 channel = add_channel();
818 if (channel == NULL)
819 goto failed;
820
821 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
822 if (term->tl_winpty_config == NULL)
823 goto failed;
824
825 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
826 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
827 if (term->tl_winpty == NULL)
828 goto failed;
829
830 spawn_config = winpty_spawn_config_new(
831 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
832 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
833 NULL,
834 p,
835 NULL,
836 NULL,
837 &winpty_err);
838 if (spawn_config == NULL)
839 goto failed;
840
841 channel = add_channel();
842 if (channel == NULL)
843 goto failed;
844
845 job = job_alloc();
846 if (job == NULL)
847 goto failed;
848
849 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
850 &child_thread_handle, &error, &winpty_err))
851 goto failed;
852
853 channel_set_pipes(channel,
854 (sock_T) CreateFileW(
855 winpty_conin_name(term->tl_winpty),
856 GENERIC_WRITE, 0, NULL,
857 OPEN_EXISTING, 0, NULL),
858 (sock_T) CreateFileW(
859 winpty_conout_name(term->tl_winpty),
860 GENERIC_READ, 0, NULL,
861 OPEN_EXISTING, 0, NULL),
862 (sock_T) CreateFileW(
863 winpty_conerr_name(term->tl_winpty),
864 GENERIC_READ, 0, NULL,
865 OPEN_EXISTING, 0, NULL));
866
867 jo = CreateJobObject(NULL, NULL);
868 if (jo == NULL)
869 goto failed;
870
871 if (!AssignProcessToJobObject(jo, child_process_handle))
872 goto failed;
873
874 winpty_spawn_config_free(spawn_config);
875
876 create_vterm(term, rows, cols);
877
878 setup_job_options(&opt, rows, cols);
879 channel_set_job(channel, job, &opt);
880
881 job->jv_channel = channel;
882 job->jv_proc_info.hProcess = child_process_handle;
883 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
884 job->jv_job_object = jo;
885 job->jv_status = JOB_STARTED;
886 term->tl_job = job;
887
888 return OK;
889
890failed:
891 if (channel != NULL)
892 channel_clear(channel);
893 if (job != NULL)
894 job_cleanup(job);
895 if (jo != NULL)
896 CloseHandle(jo);
897 if (term->tl_winpty != NULL)
898 winpty_free(term->tl_winpty);
899 if (term->tl_winpty_config != NULL)
900 winpty_config_free(term->tl_winpty_config);
901 if (winpty_err != NULL)
902 {
903 char_u *msg = utf16_to_enc(
904 (short_u *)winpty_error_msg(winpty_err), NULL);
905
906 EMSG(msg);
907 winpty_error_free(winpty_err);
908 }
909 return FAIL;
910}
911
912/*
913 * Free the terminal emulator part of "term".
914 */
915 static void
916term_free(term_T *term)
917{
918 winpty_free(term->tl_winpty);
919 winpty_config_free(term->tl_winpty_config);
920 vterm_free(term->tl_vterm);
921}
922
923# else
924
925/**************************************
926 * 3. Unix-like implementation.
927 */
928
929/*
930 * Create a new terminal of "rows" by "cols" cells.
931 * Start job for "cmd".
932 * Store the pointers in "term".
933 * Return OK or FAIL.
934 */
935 static int
936term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
937{
938 typval_T argvars[2];
939 jobopt_T opt;
940
941 create_vterm(term, rows, cols);
942
943 argvars[0].v_type = VAR_STRING;
944 argvars[0].vval.v_string = cmd;
945 setup_job_options(&opt, rows, cols);
946 term->tl_job = job_start(argvars, &opt);
947
Bram Moolenaar61a66052017-07-22 18:39:00 +0200948 return term->tl_job != NULL
949 && term->tl_job->jv_channel != NULL
950 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200951}
952
953/*
954 * Free the terminal emulator part of "term".
955 */
956 static void
957term_free(term_T *term)
958{
959 vterm_free(term->tl_vterm);
960}
961# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200962
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200963#endif /* FEAT_TERMINAL */