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