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