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