/src/tmux/server-client.c
Line | Count | Source |
1 | | /* $OpenBSD$ */ |
2 | | |
3 | | /* |
4 | | * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com> |
5 | | * |
6 | | * Permission to use, copy, modify, and distribute this software for any |
7 | | * purpose with or without fee is hereby granted, provided that the above |
8 | | * copyright notice and this permission notice appear in all copies. |
9 | | * |
10 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
11 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
12 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
13 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
14 | | * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER |
15 | | * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING |
16 | | * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 | | */ |
18 | | |
19 | | #include <sys/types.h> |
20 | | #include <sys/ioctl.h> |
21 | | #include <sys/uio.h> |
22 | | |
23 | | #include <errno.h> |
24 | | #include <fcntl.h> |
25 | | #include <stdlib.h> |
26 | | #include <string.h> |
27 | | #include <time.h> |
28 | | #include <unistd.h> |
29 | | |
30 | | #include "tmux.h" |
31 | | |
32 | | static void server_client_free(int, short, void *); |
33 | | static void server_client_check_pane_resize(struct window_pane *); |
34 | | static void server_client_check_pane_buffer(struct window_pane *); |
35 | | static void server_client_check_window_resize(struct window *); |
36 | | static key_code server_client_check_mouse(struct client *, struct key_event *); |
37 | | static void server_client_repeat_timer(int, short, void *); |
38 | | static void server_client_click_timer(int, short, void *); |
39 | | static void server_client_check_exit(struct client *); |
40 | | static void server_client_check_redraw(struct client *); |
41 | | static void server_client_check_modes(struct client *); |
42 | | static void server_client_set_title(struct client *); |
43 | | static void server_client_set_path(struct client *); |
44 | | static void server_client_reset_state(struct client *); |
45 | | static void server_client_update_latest(struct client *); |
46 | | static void server_client_dispatch(struct imsg *, void *); |
47 | | static int server_client_dispatch_command(struct client *, struct imsg *); |
48 | | static int server_client_dispatch_identify(struct client *, struct imsg *); |
49 | | static int server_client_dispatch_shell(struct client *); |
50 | | static void server_client_report_theme(struct client *, enum client_theme); |
51 | | |
52 | | /* Compare client windows. */ |
53 | | static int |
54 | | server_client_window_cmp(struct client_window *cw1, |
55 | | struct client_window *cw2) |
56 | 0 | { |
57 | 0 | if (cw1->window < cw2->window) |
58 | 0 | return (-1); |
59 | 0 | if (cw1->window > cw2->window) |
60 | 0 | return (1); |
61 | 0 | return (0); |
62 | 0 | } |
63 | 0 | RB_GENERATE(client_windows, client_window, entry, server_client_window_cmp); Unexecuted instantiation: client_windows_RB_REMOVE_COLOR Unexecuted instantiation: client_windows_RB_REMOVE Unexecuted instantiation: client_windows_RB_INSERT Unexecuted instantiation: client_windows_RB_FIND Unexecuted instantiation: client_windows_RB_NFIND Unexecuted instantiation: client_windows_RB_MINMAX |
64 | 0 |
|
65 | 0 | /* Number of attached clients. */ |
66 | 0 | u_int |
67 | 0 | server_client_how_many(void) |
68 | 0 | { |
69 | 0 | struct client *c; |
70 | 0 | u_int n; |
71 | |
|
72 | 0 | n = 0; |
73 | 0 | TAILQ_FOREACH(c, &clients, entry) { |
74 | 0 | if (c->session != NULL && (~c->flags & CLIENT_UNATTACHEDFLAGS)) |
75 | 0 | n++; |
76 | 0 | } |
77 | 0 | return (n); |
78 | 0 | } |
79 | | |
80 | | /* Overlay timer callback. */ |
81 | | static void |
82 | | server_client_overlay_timer(__unused int fd, __unused short events, void *data) |
83 | 0 | { |
84 | 0 | server_client_clear_overlay(data); |
85 | 0 | } |
86 | | |
87 | | /* Set an overlay on client. */ |
88 | | void |
89 | | server_client_set_overlay(struct client *c, u_int delay, |
90 | | overlay_check_cb checkcb, overlay_mode_cb modecb, |
91 | | overlay_draw_cb drawcb, overlay_key_cb keycb, overlay_free_cb freecb, |
92 | | overlay_resize_cb resizecb, void *data) |
93 | 0 | { |
94 | 0 | struct timeval tv; |
95 | |
|
96 | 0 | if (c->overlay_draw != NULL) |
97 | 0 | server_client_clear_overlay(c); |
98 | |
|
99 | 0 | tv.tv_sec = delay / 1000; |
100 | 0 | tv.tv_usec = (delay % 1000) * 1000L; |
101 | |
|
102 | 0 | if (event_initialized(&c->overlay_timer)) |
103 | 0 | evtimer_del(&c->overlay_timer); |
104 | 0 | evtimer_set(&c->overlay_timer, server_client_overlay_timer, c); |
105 | 0 | if (delay != 0) |
106 | 0 | evtimer_add(&c->overlay_timer, &tv); |
107 | |
|
108 | 0 | c->overlay_check = checkcb; |
109 | 0 | c->overlay_mode = modecb; |
110 | 0 | c->overlay_draw = drawcb; |
111 | 0 | c->overlay_key = keycb; |
112 | 0 | c->overlay_free = freecb; |
113 | 0 | c->overlay_resize = resizecb; |
114 | 0 | c->overlay_data = data; |
115 | |
|
116 | 0 | if (c->overlay_check == NULL) |
117 | 0 | c->tty.flags |= TTY_FREEZE; |
118 | 0 | if (c->overlay_mode == NULL) |
119 | 0 | c->tty.flags |= TTY_NOCURSOR; |
120 | 0 | window_update_focus(c->session->curw->window); |
121 | 0 | server_redraw_client(c); |
122 | 0 | } |
123 | | |
124 | | /* Clear overlay mode on client. */ |
125 | | void |
126 | | server_client_clear_overlay(struct client *c) |
127 | 0 | { |
128 | 0 | if (c->overlay_draw == NULL) |
129 | 0 | return; |
130 | | |
131 | 0 | if (event_initialized(&c->overlay_timer)) |
132 | 0 | evtimer_del(&c->overlay_timer); |
133 | |
|
134 | 0 | if (c->overlay_free != NULL) |
135 | 0 | c->overlay_free(c, c->overlay_data); |
136 | |
|
137 | 0 | c->overlay_check = NULL; |
138 | 0 | c->overlay_mode = NULL; |
139 | 0 | c->overlay_draw = NULL; |
140 | 0 | c->overlay_key = NULL; |
141 | 0 | c->overlay_free = NULL; |
142 | 0 | c->overlay_resize = NULL; |
143 | 0 | c->overlay_data = NULL; |
144 | |
|
145 | 0 | c->tty.flags &= ~(TTY_FREEZE|TTY_NOCURSOR); |
146 | 0 | if (c->session != NULL) |
147 | 0 | window_update_focus(c->session->curw->window); |
148 | 0 | server_redraw_client(c); |
149 | 0 | } |
150 | | |
151 | | /* Are these ranges empty? That is, nothing is visible. */ |
152 | | int |
153 | | server_client_ranges_is_empty(struct visible_ranges *r) |
154 | 0 | { |
155 | 0 | u_int i; |
156 | |
|
157 | 0 | for (i = 0; i < r->used; i++) { |
158 | 0 | if (r->ranges[i].nx != 0) |
159 | 0 | return (0); |
160 | 0 | } |
161 | 0 | return (1); |
162 | 0 | } |
163 | | |
164 | | /* Ensure we have space for at least n ranges. */ |
165 | | void |
166 | | server_client_ensure_ranges(struct visible_ranges *r, u_int n) |
167 | 0 | { |
168 | 0 | if (r->size >= n) |
169 | 0 | return; |
170 | 0 | r->ranges = xrecallocarray(r->ranges, r->size, n, sizeof *r->ranges); |
171 | 0 | r->size = n; |
172 | 0 | } |
173 | | |
174 | | /* |
175 | | * Given overlay position and dimensions, return parts of the input range which |
176 | | * are visible. |
177 | | */ |
178 | | void |
179 | | server_client_overlay_range(u_int x, u_int y, u_int sx, u_int sy, u_int px, |
180 | | u_int py, u_int nx, struct visible_ranges *r) |
181 | 0 | { |
182 | 0 | u_int ox, onx; |
183 | | |
184 | | /* Trivial case of no overlap in the y direction. */ |
185 | 0 | if (py < y || py > y + sy - 1) { |
186 | 0 | server_client_ensure_ranges(r, 1); |
187 | 0 | r->ranges[0].px = px; |
188 | 0 | r->ranges[0].nx = nx; |
189 | 0 | r->used = 1; |
190 | 0 | return; |
191 | 0 | } |
192 | 0 | server_client_ensure_ranges(r, 2); |
193 | | |
194 | | /* Visible bit to the left of the popup. */ |
195 | 0 | if (px < x) { |
196 | 0 | r->ranges[0].px = px; |
197 | 0 | r->ranges[0].nx = x - px; |
198 | 0 | if (r->ranges[0].nx > nx) |
199 | 0 | r->ranges[0].nx = nx; |
200 | 0 | } else { |
201 | 0 | r->ranges[0].px = 0; |
202 | 0 | r->ranges[0].nx = 0; |
203 | 0 | } |
204 | | |
205 | | /* Visible bit to the right of the popup. */ |
206 | 0 | ox = x + sx; |
207 | 0 | if (px > ox) |
208 | 0 | ox = px; |
209 | 0 | onx = px + nx; |
210 | 0 | if (onx > ox) { |
211 | 0 | r->ranges[1].px = ox; |
212 | 0 | r->ranges[1].nx = onx - ox; |
213 | 0 | } else { |
214 | 0 | r->ranges[1].px = 0; |
215 | 0 | r->ranges[1].nx = 0; |
216 | 0 | } |
217 | 0 | r->used = 2; |
218 | 0 | } |
219 | | |
220 | | /* Check if this client is inside this server. */ |
221 | | int |
222 | | server_client_check_nested(struct client *c) |
223 | 0 | { |
224 | 0 | struct environ_entry *envent; |
225 | 0 | struct window_pane *wp; |
226 | |
|
227 | 0 | envent = environ_find(c->environ, "TMUX"); |
228 | 0 | if (envent == NULL || *envent->value == '\0') |
229 | 0 | return (0); |
230 | | |
231 | 0 | RB_FOREACH(wp, window_pane_tree, &all_window_panes) { |
232 | 0 | if (strcmp(wp->tty, c->ttyname) == 0) |
233 | 0 | return (1); |
234 | 0 | } |
235 | 0 | return (0); |
236 | 0 | } |
237 | | |
238 | | /* Set client key table. */ |
239 | | void |
240 | | server_client_set_key_table(struct client *c, const char *name) |
241 | 0 | { |
242 | 0 | if (name == NULL) |
243 | 0 | name = server_client_get_key_table(c); |
244 | |
|
245 | 0 | key_bindings_unref_table(c->keytable); |
246 | 0 | c->keytable = key_bindings_get_table(name, 1); |
247 | 0 | c->keytable->references++; |
248 | 0 | if (gettimeofday(&c->keytable->activity_time, NULL) != 0) |
249 | 0 | fatal("gettimeofday failed"); |
250 | 0 | } |
251 | | |
252 | | static uint64_t |
253 | | server_client_key_table_activity_diff(struct client *c) |
254 | 0 | { |
255 | 0 | struct timeval diff; |
256 | |
|
257 | 0 | timersub(&c->activity_time, &c->keytable->activity_time, &diff); |
258 | 0 | return ((diff.tv_sec * 1000ULL) + (diff.tv_usec / 1000ULL)); |
259 | 0 | } |
260 | | |
261 | | /* Get default key table. */ |
262 | | const char * |
263 | | server_client_get_key_table(struct client *c) |
264 | 0 | { |
265 | 0 | struct session *s = c->session; |
266 | 0 | const char *name; |
267 | |
|
268 | 0 | if (s == NULL) |
269 | 0 | return ("root"); |
270 | | |
271 | 0 | name = options_get_string(s->options, "key-table"); |
272 | 0 | if (*name == '\0') |
273 | 0 | return ("root"); |
274 | 0 | return (name); |
275 | 0 | } |
276 | | |
277 | | /* Is this table the default key table? */ |
278 | | static int |
279 | | server_client_is_default_key_table(struct client *c, struct key_table *table) |
280 | 0 | { |
281 | 0 | return (strcmp(table->name, server_client_get_key_table(c)) == 0); |
282 | 0 | } |
283 | | |
284 | | /* Create a new client. */ |
285 | | struct client * |
286 | | server_client_create(int fd) |
287 | 0 | { |
288 | 0 | struct client *c; |
289 | |
|
290 | 0 | setblocking(fd, 0); |
291 | |
|
292 | 0 | c = xcalloc(1, sizeof *c); |
293 | 0 | c->references = 1; |
294 | 0 | c->peer = proc_add_peer(server_proc, fd, server_client_dispatch, c); |
295 | |
|
296 | 0 | if (gettimeofday(&c->creation_time, NULL) != 0) |
297 | 0 | fatal("gettimeofday failed"); |
298 | 0 | memcpy(&c->activity_time, &c->creation_time, sizeof c->activity_time); |
299 | |
|
300 | 0 | c->environ = environ_create(); |
301 | |
|
302 | 0 | c->fd = -1; |
303 | 0 | c->out_fd = -1; |
304 | |
|
305 | 0 | c->queue = cmdq_new(); |
306 | 0 | RB_INIT(&c->windows); |
307 | 0 | RB_INIT(&c->files); |
308 | |
|
309 | 0 | c->tty.sx = 80; |
310 | 0 | c->tty.sy = 24; |
311 | 0 | c->theme = THEME_UNKNOWN; |
312 | |
|
313 | 0 | status_init(c); |
314 | 0 | c->flags |= CLIENT_FOCUSED; |
315 | |
|
316 | 0 | c->keytable = key_bindings_get_table("root", 1); |
317 | 0 | c->keytable->references++; |
318 | |
|
319 | 0 | evtimer_set(&c->repeat_timer, server_client_repeat_timer, c); |
320 | 0 | evtimer_set(&c->click_timer, server_client_click_timer, c); |
321 | |
|
322 | 0 | c->click_wp = -1; |
323 | |
|
324 | 0 | TAILQ_INIT(&c->input_requests); |
325 | |
|
326 | 0 | TAILQ_INSERT_TAIL(&clients, c, entry); |
327 | 0 | log_debug("new client %p", c); |
328 | 0 | return (c); |
329 | 0 | } |
330 | | |
331 | | /* Open client terminal if needed. */ |
332 | | int |
333 | | server_client_open(struct client *c, char **cause) |
334 | 0 | { |
335 | 0 | const char *ttynam = _PATH_TTY; |
336 | |
|
337 | 0 | if (c->flags & CLIENT_CONTROL) |
338 | 0 | return (0); |
339 | | |
340 | 0 | if (strcmp(c->ttyname, ttynam) == 0|| |
341 | 0 | ((isatty(STDIN_FILENO) && |
342 | 0 | (ttynam = ttyname(STDIN_FILENO)) != NULL && |
343 | 0 | strcmp(c->ttyname, ttynam) == 0) || |
344 | 0 | (isatty(STDOUT_FILENO) && |
345 | 0 | (ttynam = ttyname(STDOUT_FILENO)) != NULL && |
346 | 0 | strcmp(c->ttyname, ttynam) == 0) || |
347 | 0 | (isatty(STDERR_FILENO) && |
348 | 0 | (ttynam = ttyname(STDERR_FILENO)) != NULL && |
349 | 0 | strcmp(c->ttyname, ttynam) == 0))) { |
350 | 0 | xasprintf(cause, "can't use %s", c->ttyname); |
351 | 0 | return (-1); |
352 | 0 | } |
353 | | |
354 | 0 | if (!(c->flags & CLIENT_TERMINAL)) { |
355 | 0 | *cause = xstrdup("not a terminal"); |
356 | 0 | return (-1); |
357 | 0 | } |
358 | | |
359 | 0 | if (tty_open(&c->tty, cause) != 0) |
360 | 0 | return (-1); |
361 | | |
362 | 0 | return (0); |
363 | 0 | } |
364 | | |
365 | | /* Lost an attached client. */ |
366 | | static void |
367 | | server_client_attached_lost(struct client *c) |
368 | 0 | { |
369 | 0 | struct session *s; |
370 | 0 | struct window *w; |
371 | 0 | struct client *loop; |
372 | 0 | struct client *found; |
373 | |
|
374 | 0 | log_debug("lost attached client %p", c); |
375 | | |
376 | | /* |
377 | | * By this point the session in the client has been cleared so walk all |
378 | | * windows to find any with this client as the latest. |
379 | | */ |
380 | 0 | RB_FOREACH(w, windows, &windows) { |
381 | 0 | if (w->latest != c) |
382 | 0 | continue; |
383 | | |
384 | 0 | found = NULL; |
385 | 0 | TAILQ_FOREACH(loop, &clients, entry) { |
386 | 0 | s = loop->session; |
387 | 0 | if (loop == c || s == NULL || s->curw->window != w) |
388 | 0 | continue; |
389 | 0 | if (found == NULL || timercmp(&loop->activity_time, |
390 | 0 | &found->activity_time, >)) |
391 | 0 | found = loop; |
392 | 0 | } |
393 | 0 | if (found != NULL) |
394 | 0 | server_client_update_latest(found); |
395 | 0 | } |
396 | 0 | } |
397 | | |
398 | | /* Set client session. */ |
399 | | void |
400 | | server_client_set_session(struct client *c, struct session *s) |
401 | 0 | { |
402 | 0 | struct session *old = c->session; |
403 | |
|
404 | 0 | if (s != NULL && c->session != NULL && c->session != s) |
405 | 0 | c->last_session = c->session; |
406 | 0 | else if (s == NULL) |
407 | 0 | c->last_session = NULL; |
408 | 0 | c->session = s; |
409 | 0 | c->flags |= CLIENT_FOCUSED; |
410 | |
|
411 | 0 | if (old != NULL && old->curw != NULL) |
412 | 0 | window_update_focus(old->curw->window); |
413 | 0 | if (s != NULL) { |
414 | 0 | s->curw->window->latest = c; |
415 | 0 | recalculate_sizes(); |
416 | 0 | window_update_focus(s->curw->window); |
417 | 0 | session_update_activity(s, NULL); |
418 | 0 | session_theme_changed(s); |
419 | 0 | gettimeofday(&s->last_attached_time, NULL); |
420 | 0 | s->curw->flags &= ~WINLINK_ALERTFLAGS; |
421 | 0 | alerts_check_session(s); |
422 | 0 | tty_update_client_offset(c); |
423 | 0 | status_timer_start(c); |
424 | 0 | notify_client("client-session-changed", c); |
425 | 0 | server_redraw_client(c); |
426 | 0 | } |
427 | |
|
428 | 0 | server_check_unattached(); |
429 | 0 | server_update_socket(); |
430 | 0 | } |
431 | | |
432 | | /* Lost a client. */ |
433 | | void |
434 | | server_client_lost(struct client *c) |
435 | 0 | { |
436 | 0 | struct client_file *cf, *cf1; |
437 | 0 | struct client_window *cw, *cw1; |
438 | |
|
439 | 0 | c->flags |= CLIENT_DEAD; |
440 | |
|
441 | 0 | server_client_clear_overlay(c); |
442 | 0 | status_prompt_clear(c); |
443 | 0 | status_message_clear(c); |
444 | |
|
445 | 0 | RB_FOREACH_SAFE(cf, client_files, &c->files, cf1) { |
446 | 0 | cf->error = EINTR; |
447 | 0 | file_fire_done(cf); |
448 | 0 | } |
449 | 0 | RB_FOREACH_SAFE(cw, client_windows, &c->windows, cw1) { |
450 | 0 | RB_REMOVE(client_windows, &c->windows, cw); |
451 | 0 | free(cw); |
452 | 0 | } |
453 | |
|
454 | 0 | TAILQ_REMOVE(&clients, c, entry); |
455 | 0 | log_debug("lost client %p", c); |
456 | |
|
457 | 0 | if (c->flags & CLIENT_ATTACHED) { |
458 | 0 | server_client_attached_lost(c); |
459 | 0 | notify_client("client-detached", c); |
460 | 0 | } |
461 | |
|
462 | 0 | if (c->flags & CLIENT_CONTROL) |
463 | 0 | control_stop(c); |
464 | 0 | if (c->flags & CLIENT_TERMINAL) |
465 | 0 | tty_free(&c->tty); |
466 | 0 | free(c->ttyname); |
467 | 0 | free(c->clipboard_panes); |
468 | |
|
469 | 0 | free(c->term_name); |
470 | 0 | free(c->term_type); |
471 | 0 | tty_term_free_list(c->term_caps, c->term_ncaps); |
472 | |
|
473 | 0 | status_free(c); |
474 | 0 | input_cancel_requests(c); |
475 | |
|
476 | 0 | free(c->title); |
477 | 0 | free((void *)c->cwd); |
478 | |
|
479 | 0 | evtimer_del(&c->repeat_timer); |
480 | 0 | evtimer_del(&c->click_timer); |
481 | |
|
482 | 0 | key_bindings_unref_table(c->keytable); |
483 | |
|
484 | 0 | free(c->message_string); |
485 | 0 | if (event_initialized(&c->message_timer)) |
486 | 0 | evtimer_del(&c->message_timer); |
487 | |
|
488 | 0 | free(c->prompt_saved); |
489 | 0 | free(c->prompt_string); |
490 | 0 | free(c->prompt_buffer); |
491 | |
|
492 | 0 | format_lost_client(c); |
493 | 0 | environ_free(c->environ); |
494 | |
|
495 | 0 | proc_remove_peer(c->peer); |
496 | 0 | c->peer = NULL; |
497 | |
|
498 | 0 | if (c->out_fd != -1) |
499 | 0 | close(c->out_fd); |
500 | 0 | if (c->fd != -1) { |
501 | 0 | close(c->fd); |
502 | 0 | c->fd = -1; |
503 | 0 | } |
504 | 0 | server_client_unref(c); |
505 | |
|
506 | 0 | server_add_accept(0); /* may be more file descriptors now */ |
507 | |
|
508 | 0 | recalculate_sizes(); |
509 | 0 | server_check_unattached(); |
510 | 0 | server_update_socket(); |
511 | 0 | } |
512 | | |
513 | | /* Remove reference from a client. */ |
514 | | void |
515 | | server_client_unref(struct client *c) |
516 | 0 | { |
517 | 0 | log_debug("unref client %p (%d references)", c, c->references); |
518 | |
|
519 | 0 | c->references--; |
520 | 0 | if (c->references == 0) |
521 | 0 | event_once(-1, EV_TIMEOUT, server_client_free, c, NULL); |
522 | 0 | } |
523 | | |
524 | | /* Free dead client. */ |
525 | | static void |
526 | | server_client_free(__unused int fd, __unused short events, void *arg) |
527 | 0 | { |
528 | 0 | struct client *c = arg; |
529 | |
|
530 | 0 | log_debug("free client %p (%d references)", c, c->references); |
531 | |
|
532 | 0 | cmdq_free(c->queue); |
533 | |
|
534 | 0 | if (c->references == 0) { |
535 | 0 | free((void *)c->name); |
536 | 0 | free((void *)c->user); |
537 | 0 | free(c); |
538 | 0 | } |
539 | 0 | } |
540 | | |
541 | | /* Suspend a client. */ |
542 | | void |
543 | | server_client_suspend(struct client *c) |
544 | 0 | { |
545 | 0 | struct session *s = c->session; |
546 | |
|
547 | 0 | if (s == NULL || (c->flags & CLIENT_UNATTACHEDFLAGS)) |
548 | 0 | return; |
549 | | |
550 | 0 | tty_stop_tty(&c->tty); |
551 | 0 | c->flags |= CLIENT_SUSPENDED; |
552 | 0 | proc_send(c->peer, MSG_SUSPEND, -1, NULL, 0); |
553 | 0 | } |
554 | | |
555 | | /* Detach a client. */ |
556 | | void |
557 | | server_client_detach(struct client *c, enum msgtype msgtype) |
558 | 0 | { |
559 | 0 | struct session *s = c->session; |
560 | |
|
561 | 0 | if (s == NULL || (c->flags & CLIENT_NODETACHFLAGS)) |
562 | 0 | return; |
563 | | |
564 | 0 | c->flags |= CLIENT_EXIT; |
565 | |
|
566 | 0 | c->exit_type = CLIENT_EXIT_DETACH; |
567 | 0 | c->exit_msgtype = msgtype; |
568 | 0 | c->exit_session = xstrdup(s->name); |
569 | 0 | } |
570 | | |
571 | | /* Execute command to replace a client. */ |
572 | | void |
573 | | server_client_exec(struct client *c, const char *cmd) |
574 | 0 | { |
575 | 0 | struct session *s = c->session; |
576 | 0 | char *msg; |
577 | 0 | const char *shell; |
578 | 0 | size_t cmdsize, shellsize; |
579 | |
|
580 | 0 | if (*cmd == '\0') |
581 | 0 | return; |
582 | 0 | cmdsize = strlen(cmd) + 1; |
583 | |
|
584 | 0 | if (s != NULL) |
585 | 0 | shell = options_get_string(s->options, "default-shell"); |
586 | 0 | else |
587 | 0 | shell = options_get_string(global_s_options, "default-shell"); |
588 | 0 | if (!checkshell(shell)) |
589 | 0 | shell = _PATH_BSHELL; |
590 | 0 | shellsize = strlen(shell) + 1; |
591 | |
|
592 | 0 | msg = xmalloc(cmdsize + shellsize); |
593 | 0 | memcpy(msg, cmd, cmdsize); |
594 | 0 | memcpy(msg + cmdsize, shell, shellsize); |
595 | |
|
596 | 0 | proc_send(c->peer, MSG_EXEC, -1, msg, cmdsize + shellsize); |
597 | 0 | free(msg); |
598 | 0 | } |
599 | | |
600 | | static enum key_code_mouse_location |
601 | | server_client_check_mouse_in_pane(struct window_pane *wp, u_int px, u_int py, |
602 | | u_int *sl_mpos) |
603 | 0 | { |
604 | 0 | struct window *w = wp->window; |
605 | 0 | struct options *wo = w->options; |
606 | 0 | struct window_pane *fwp; |
607 | 0 | int pane_status, sb, sb_pos, sb_w, sb_pad; |
608 | 0 | int pane_status_line, sl_top, sl_bottom; |
609 | 0 | int bdr_bottom, bdr_top, bdr_right; |
610 | |
|
611 | 0 | sb = options_get_number(wo, "pane-scrollbars"); |
612 | 0 | sb_pos = options_get_number(wo, "pane-scrollbars-position"); |
613 | 0 | pane_status = options_get_number(wo, "pane-border-status"); |
614 | |
|
615 | 0 | if (window_pane_show_scrollbar(wp, sb)) { |
616 | 0 | sb_w = wp->scrollbar_style.width; |
617 | 0 | sb_pad = wp->scrollbar_style.pad; |
618 | 0 | } else { |
619 | 0 | sb_w = 0; |
620 | 0 | sb_pad = 0; |
621 | 0 | } |
622 | |
|
623 | 0 | if (pane_status == PANE_STATUS_TOP) |
624 | 0 | pane_status_line = wp->yoff - 1; |
625 | 0 | else if (pane_status == PANE_STATUS_BOTTOM) |
626 | 0 | pane_status_line = wp->yoff + wp->sy; |
627 | 0 | else |
628 | 0 | pane_status_line = -1; /* not used */ |
629 | | |
630 | | /* Check if point is within the pane or scrollbar. */ |
631 | 0 | if (((pane_status != PANE_STATUS_OFF && |
632 | 0 | (int)py != pane_status_line && py != wp->yoff + wp->sy) || |
633 | 0 | (wp->yoff == 0 && py < wp->sy) || |
634 | 0 | (py >= wp->yoff && py < wp->yoff + wp->sy)) && |
635 | 0 | ((sb_pos == PANE_SCROLLBARS_RIGHT && |
636 | 0 | (int)px < (int)wp->xoff + (int)wp->sx + sb_pad + sb_w) || |
637 | 0 | (sb_pos == PANE_SCROLLBARS_LEFT && |
638 | 0 | (int)px < (int)wp->xoff + (int)wp->sx - sb_pad - sb_w))) { |
639 | | /* Check if in the scrollbar. */ |
640 | 0 | if ((sb_pos == PANE_SCROLLBARS_RIGHT && |
641 | 0 | ((int)px >= (int)wp->xoff + (int)wp->sx + sb_pad && |
642 | 0 | (int)px < (int)wp->xoff + (int)wp->sx + sb_pad + sb_w)) || |
643 | 0 | (sb_pos == PANE_SCROLLBARS_LEFT && |
644 | 0 | ((int)px >= (int)wp->xoff - sb_pad - sb_w && |
645 | 0 | (int)px < (int)wp->xoff - sb_pad))) { |
646 | | /* Check where inside the scrollbar. */ |
647 | 0 | sl_top = wp->yoff + wp->sb_slider_y; |
648 | 0 | sl_bottom = (wp->yoff + wp->sb_slider_y + |
649 | 0 | wp->sb_slider_h - 1); |
650 | 0 | if ((int)py < sl_top) |
651 | 0 | return (KEYC_MOUSE_LOCATION_SCROLLBAR_UP); |
652 | 0 | else if ((int)py >= sl_top && |
653 | 0 | (int)py <= sl_bottom) { |
654 | 0 | *sl_mpos = (py - wp->sb_slider_y - wp->yoff); |
655 | 0 | return (KEYC_MOUSE_LOCATION_SCROLLBAR_SLIDER); |
656 | 0 | } else /* py > sl_bottom */ |
657 | 0 | return (KEYC_MOUSE_LOCATION_SCROLLBAR_DOWN); |
658 | 0 | } else { |
659 | | /* Must be inside the pane. */ |
660 | 0 | return (KEYC_MOUSE_LOCATION_PANE); |
661 | 0 | } |
662 | 0 | } else { |
663 | | /* Try the pane borders. */ |
664 | 0 | TAILQ_FOREACH(fwp, &w->panes, entry) { |
665 | 0 | if ((w->flags & WINDOW_ZOOMED) && |
666 | 0 | (~fwp->flags & PANE_ZOOMED)) |
667 | 0 | continue; |
668 | 0 | bdr_top = fwp->yoff - 1; |
669 | 0 | bdr_bottom = fwp->yoff + (int)fwp->sy; |
670 | 0 | if (sb_pos == PANE_SCROLLBARS_LEFT) |
671 | 0 | bdr_right = fwp->xoff + (int)fwp->sx; |
672 | 0 | else |
673 | | /* PANE_SCROLLBARS_RIGHT or none. */ |
674 | 0 | bdr_right = fwp->xoff + (int)fwp->sx + |
675 | 0 | sb_pad + sb_w; |
676 | 0 | if ((int)py >= (int)fwp->yoff - 1 && |
677 | 0 | py <= fwp->yoff + fwp->sy) { |
678 | 0 | if ((int)px == bdr_right) |
679 | 0 | break; |
680 | 0 | } |
681 | 0 | if ((int)px >= (int)fwp->xoff - 1 && |
682 | 0 | px <= fwp->xoff + fwp->sx) { |
683 | 0 | if ((int)py == bdr_bottom || (int)py == bdr_top) |
684 | 0 | break; |
685 | 0 | } |
686 | 0 | } |
687 | 0 | if (fwp != NULL) |
688 | 0 | return (KEYC_MOUSE_LOCATION_BORDER); |
689 | 0 | } |
690 | 0 | return (KEYC_MOUSE_LOCATION_NOWHERE); |
691 | 0 | } |
692 | | |
693 | | /* Check for mouse keys. */ |
694 | | static key_code |
695 | | server_client_check_mouse(struct client *c, struct key_event *event) |
696 | 0 | { |
697 | 0 | struct mouse_event *m = &event->m; |
698 | 0 | struct session *s = c->session, *fs; |
699 | 0 | struct window *w = s->curw->window; |
700 | 0 | struct winlink *fwl; |
701 | 0 | struct window_pane *wp, *fwp; |
702 | 0 | u_int x, y, sx, sy, px, py, n, sl_mpos = 0; |
703 | 0 | u_int b, bn; |
704 | 0 | int ignore = 0; |
705 | 0 | key_code key; |
706 | 0 | struct timeval tv; |
707 | 0 | struct style_range *sr; |
708 | 0 | enum key_code_type type = KEYC_TYPE_NOTYPE; |
709 | 0 | enum key_code_mouse_location loc = KEYC_MOUSE_LOCATION_NOWHERE; |
710 | |
|
711 | 0 | log_debug("%s mouse %02x at %u,%u (last %u,%u) (%d)", c->name, m->b, |
712 | 0 | m->x, m->y, m->lx, m->ly, c->tty.mouse_drag_flag); |
713 | | |
714 | | /* What type of event is this? */ |
715 | 0 | if (event->key == KEYC_DOUBLECLICK) { |
716 | 0 | type = KEYC_TYPE_DOUBLECLICK; |
717 | 0 | x = m->x, y = m->y, b = m->b; |
718 | 0 | ignore = 1; |
719 | 0 | log_debug("double-click at %u,%u", x, y); |
720 | 0 | } else if ((m->sgr_type != ' ' && |
721 | 0 | MOUSE_DRAG(m->sgr_b) && |
722 | 0 | MOUSE_RELEASE(m->sgr_b)) || |
723 | 0 | (m->sgr_type == ' ' && |
724 | 0 | MOUSE_DRAG(m->b) && |
725 | 0 | MOUSE_RELEASE(m->b) && |
726 | 0 | MOUSE_RELEASE(m->lb))) { |
727 | 0 | type = KEYC_TYPE_MOUSEMOVE; |
728 | 0 | x = m->x, y = m->y, b = 0; |
729 | 0 | log_debug("move at %u,%u", x, y); |
730 | 0 | } else if (MOUSE_DRAG(m->b)) { |
731 | 0 | type = KEYC_TYPE_MOUSEDRAG; |
732 | 0 | if (c->tty.mouse_drag_flag) { |
733 | 0 | x = m->x, y = m->y, b = m->b; |
734 | 0 | if (x == m->lx && y == m->ly) |
735 | 0 | return (KEYC_UNKNOWN); |
736 | 0 | log_debug("drag update at %u,%u", x, y); |
737 | 0 | } else { |
738 | 0 | x = m->lx, y = m->ly, b = m->lb; |
739 | 0 | log_debug("drag start at %u,%u", x, y); |
740 | 0 | } |
741 | 0 | } else if (MOUSE_WHEEL(m->b)) { |
742 | 0 | if ((m->b & MOUSE_MASK_BUTTONS) == MOUSE_WHEEL_UP) |
743 | 0 | type = KEYC_TYPE_WHEELUP; |
744 | 0 | else |
745 | 0 | type = KEYC_TYPE_WHEELDOWN; |
746 | 0 | x = m->x, y = m->y, b = m->b; |
747 | 0 | log_debug("wheel at %u,%u", x, y); |
748 | 0 | } else if (MOUSE_RELEASE(m->b)) { |
749 | 0 | type = KEYC_TYPE_MOUSEUP; |
750 | 0 | x = m->x, y = m->y, b = m->lb; |
751 | 0 | if (m->sgr_type == 'm') |
752 | 0 | b = m->sgr_b; |
753 | 0 | log_debug("up at %u,%u", x, y); |
754 | 0 | } else { |
755 | 0 | if (c->flags & CLIENT_DOUBLECLICK) { |
756 | 0 | evtimer_del(&c->click_timer); |
757 | 0 | c->flags &= ~CLIENT_DOUBLECLICK; |
758 | 0 | type = KEYC_TYPE_SECONDCLICK; |
759 | 0 | x = m->x, y = m->y, b = m->b; |
760 | 0 | log_debug("second-click at %u,%u", x, y); |
761 | 0 | c->flags |= CLIENT_TRIPLECLICK; |
762 | 0 | } else if (c->flags & CLIENT_TRIPLECLICK) { |
763 | 0 | evtimer_del(&c->click_timer); |
764 | 0 | c->flags &= ~CLIENT_TRIPLECLICK; |
765 | 0 | type = KEYC_TYPE_TRIPLECLICK; |
766 | 0 | x = m->x, y = m->y, b = m->b; |
767 | 0 | log_debug("triple-click at %u,%u", x, y); |
768 | 0 | goto have_event; |
769 | 0 | } |
770 | | |
771 | | /* DOWN is the only remaining event type. */ |
772 | 0 | if (type == KEYC_TYPE_NOTYPE) { |
773 | 0 | type = KEYC_TYPE_MOUSEDOWN; |
774 | 0 | x = m->x, y = m->y, b = m->b; |
775 | 0 | log_debug("down at %u,%u", x, y); |
776 | 0 | c->flags |= CLIENT_DOUBLECLICK; |
777 | 0 | } |
778 | 0 | } |
779 | | |
780 | 0 | have_event: |
781 | 0 | if (type == KEYC_TYPE_NOTYPE) |
782 | 0 | return (KEYC_UNKNOWN); |
783 | | |
784 | | /* Save the session. */ |
785 | 0 | m->s = s->id; |
786 | 0 | m->w = -1; |
787 | 0 | m->wp = -1; |
788 | 0 | m->ignore = ignore; |
789 | | |
790 | | /* Is this on the status line? */ |
791 | 0 | m->statusat = status_at_line(c); |
792 | 0 | m->statuslines = status_line_size(c); |
793 | 0 | if (m->statusat != -1 && |
794 | 0 | y >= (u_int)m->statusat && |
795 | 0 | y < m->statusat + m->statuslines) { |
796 | 0 | sr = status_get_range(c, x, y - m->statusat); |
797 | 0 | if (sr == NULL) { |
798 | 0 | loc = KEYC_MOUSE_LOCATION_STATUS_DEFAULT; |
799 | 0 | } else { |
800 | 0 | switch (sr->type) { |
801 | 0 | case STYLE_RANGE_NONE: |
802 | 0 | return (KEYC_UNKNOWN); |
803 | 0 | case STYLE_RANGE_LEFT: |
804 | 0 | log_debug("mouse range: left"); |
805 | 0 | loc = KEYC_MOUSE_LOCATION_STATUS_LEFT; |
806 | 0 | break; |
807 | 0 | case STYLE_RANGE_RIGHT: |
808 | 0 | log_debug("mouse range: right"); |
809 | 0 | loc = KEYC_MOUSE_LOCATION_STATUS_RIGHT; |
810 | 0 | break; |
811 | 0 | case STYLE_RANGE_PANE: |
812 | 0 | fwp = window_pane_find_by_id(sr->argument); |
813 | 0 | if (fwp == NULL) |
814 | 0 | return (KEYC_UNKNOWN); |
815 | 0 | m->wp = sr->argument; |
816 | |
|
817 | 0 | log_debug("mouse range: pane %%%u", m->wp); |
818 | 0 | loc = KEYC_MOUSE_LOCATION_STATUS; |
819 | 0 | break; |
820 | 0 | case STYLE_RANGE_WINDOW: |
821 | 0 | fwl = winlink_find_by_index(&s->windows, |
822 | 0 | sr->argument); |
823 | 0 | if (fwl == NULL) |
824 | 0 | return (KEYC_UNKNOWN); |
825 | 0 | m->w = fwl->window->id; |
826 | |
|
827 | 0 | log_debug("mouse range: window @%u", m->w); |
828 | 0 | loc = KEYC_MOUSE_LOCATION_STATUS; |
829 | 0 | break; |
830 | 0 | case STYLE_RANGE_SESSION: |
831 | 0 | fs = session_find_by_id(sr->argument); |
832 | 0 | if (fs == NULL) |
833 | 0 | return (KEYC_UNKNOWN); |
834 | 0 | m->s = sr->argument; |
835 | |
|
836 | 0 | log_debug("mouse range: session $%u", m->s); |
837 | 0 | loc = KEYC_MOUSE_LOCATION_STATUS; |
838 | 0 | break; |
839 | 0 | case STYLE_RANGE_USER: |
840 | 0 | log_debug("mouse range: user"); |
841 | 0 | loc = KEYC_MOUSE_LOCATION_STATUS; |
842 | 0 | break; |
843 | 0 | case STYLE_RANGE_CONTROL: |
844 | 0 | n = sr->argument; /* parsing keeps this < 10 */ |
845 | 0 | log_debug("mouse range: control %u", n); |
846 | 0 | loc = KEYC_MOUSE_LOCATION_CONTROL0 + n; |
847 | 0 | break; |
848 | 0 | } |
849 | 0 | } |
850 | 0 | } |
851 | | |
852 | | /* |
853 | | * Not on status line. Adjust position and check for border, pane, or |
854 | | * scrollbar. |
855 | | */ |
856 | 0 | if (loc == KEYC_MOUSE_LOCATION_NOWHERE) { |
857 | 0 | if (c->tty.mouse_scrolling_flag) |
858 | 0 | loc = KEYC_MOUSE_LOCATION_SCROLLBAR_SLIDER; |
859 | 0 | else { |
860 | 0 | px = x; |
861 | 0 | if (m->statusat == 0 && y >= m->statuslines) |
862 | 0 | py = y - m->statuslines; |
863 | 0 | else if (m->statusat > 0 && y >= (u_int)m->statusat) |
864 | 0 | py = m->statusat - 1; |
865 | 0 | else |
866 | 0 | py = y; |
867 | |
|
868 | 0 | tty_window_offset(&c->tty, &m->ox, &m->oy, &sx, &sy); |
869 | 0 | log_debug("mouse window @%u at %u,%u (%ux%u)", |
870 | 0 | w->id, m->ox, m->oy, sx, sy); |
871 | 0 | if (px > sx || py > sy) |
872 | 0 | return (KEYC_UNKNOWN); |
873 | 0 | px = px + m->ox; |
874 | 0 | py = py + m->oy; |
875 | | |
876 | | /* Try inside the pane. */ |
877 | 0 | wp = window_get_active_at(w, px, py); |
878 | 0 | if (wp == NULL) |
879 | 0 | return (KEYC_UNKNOWN); |
880 | 0 | loc = server_client_check_mouse_in_pane(wp, px, py, |
881 | 0 | &sl_mpos); |
882 | |
|
883 | 0 | if (loc == KEYC_MOUSE_LOCATION_PANE) { |
884 | 0 | log_debug("mouse %u,%u on pane %%%u", x, y, |
885 | 0 | wp->id); |
886 | 0 | } else if (loc == KEYC_MOUSE_LOCATION_BORDER) { |
887 | 0 | sr = window_pane_border_status_get_range(wp, px, |
888 | 0 | py); |
889 | 0 | if (sr != NULL) { |
890 | 0 | n = sr->argument; |
891 | 0 | loc = KEYC_MOUSE_LOCATION_CONTROL0 + n; |
892 | 0 | } |
893 | 0 | log_debug("mouse on pane %%%u border", wp->id); |
894 | 0 | } else if (loc == KEYC_MOUSE_LOCATION_SCROLLBAR_UP || |
895 | 0 | loc == KEYC_MOUSE_LOCATION_SCROLLBAR_SLIDER || |
896 | 0 | loc == KEYC_MOUSE_LOCATION_SCROLLBAR_DOWN) { |
897 | 0 | log_debug("mouse on pane %%%u scrollbar", |
898 | 0 | wp->id); |
899 | 0 | } |
900 | 0 | m->wp = wp->id; |
901 | 0 | m->w = wp->window->id; |
902 | 0 | } |
903 | 0 | } |
904 | | |
905 | | /* Reset click type or add a click timer if needed. */ |
906 | 0 | if (type == KEYC_TYPE_MOUSEDOWN || |
907 | 0 | type == KEYC_TYPE_SECONDCLICK || |
908 | 0 | type == KEYC_TYPE_TRIPLECLICK) { |
909 | 0 | if (type != KEYC_TYPE_MOUSEDOWN && |
910 | 0 | (m->b != c->click_button || |
911 | 0 | loc != (enum key_code_mouse_location)c->click_loc || |
912 | 0 | m->wp != c->click_wp)) { |
913 | 0 | type = KEYC_TYPE_MOUSEDOWN; |
914 | 0 | log_debug("click sequence reset at %u,%u", x, y); |
915 | 0 | c->flags &= ~CLIENT_TRIPLECLICK; |
916 | 0 | c->flags |= CLIENT_DOUBLECLICK; |
917 | 0 | } |
918 | |
|
919 | 0 | if (type != KEYC_TYPE_TRIPLECLICK && KEYC_CLICK_TIMEOUT != 0) { |
920 | 0 | memcpy(&c->click_event, m, sizeof c->click_event); |
921 | 0 | c->click_button = m->b; |
922 | 0 | c->click_loc = loc; |
923 | 0 | c->click_wp = m->wp; |
924 | |
|
925 | 0 | log_debug("click timer started"); |
926 | 0 | tv.tv_sec = KEYC_CLICK_TIMEOUT / 1000; |
927 | 0 | tv.tv_usec = (KEYC_CLICK_TIMEOUT % 1000) * 1000L; |
928 | 0 | evtimer_del(&c->click_timer); |
929 | 0 | evtimer_add(&c->click_timer, &tv); |
930 | 0 | } |
931 | 0 | } |
932 | |
|
933 | 0 | key = KEYC_UNKNOWN; |
934 | | |
935 | | /* Stop dragging if needed. */ |
936 | 0 | if (type != KEYC_TYPE_MOUSEDRAG && |
937 | 0 | type != KEYC_TYPE_WHEELUP && |
938 | 0 | type != KEYC_TYPE_WHEELDOWN && |
939 | 0 | type != KEYC_TYPE_DOUBLECLICK && |
940 | 0 | type != KEYC_TYPE_TRIPLECLICK && |
941 | 0 | c->tty.mouse_drag_flag != 0) { |
942 | 0 | if (c->tty.mouse_drag_release != NULL) |
943 | 0 | c->tty.mouse_drag_release(c, m); |
944 | |
|
945 | 0 | c->tty.mouse_drag_update = NULL; |
946 | 0 | c->tty.mouse_drag_release = NULL; |
947 | 0 | c->tty.mouse_scrolling_flag = 0; |
948 | | |
949 | | /* |
950 | | * End a mouse drag by passing a MouseDragEnd key corresponding |
951 | | * to the button that started the drag. |
952 | | */ |
953 | 0 | type = KEYC_TYPE_MOUSEDRAGEND; |
954 | 0 | c->tty.mouse_drag_flag = 0; |
955 | 0 | c->tty.mouse_slider_mpos = -1; |
956 | 0 | } |
957 | | |
958 | | /* Convert to a key binding. */ |
959 | 0 | if (type == KEYC_TYPE_MOUSEMOVE && loc == KEYC_MOUSE_LOCATION_PANE) { |
960 | 0 | key = KEYC_MOUSEMOVE_PANE; |
961 | 0 | if (wp != NULL && |
962 | 0 | wp != w->active && |
963 | 0 | options_get_number(s->options, "focus-follows-mouse")) { |
964 | 0 | window_set_active_pane(w, wp, 1); |
965 | 0 | server_redraw_window_borders(w); |
966 | 0 | server_status_window(w); |
967 | 0 | } |
968 | 0 | } |
969 | 0 | if (type == KEYC_TYPE_MOUSEDRAG) { |
970 | 0 | if (c->tty.mouse_drag_update != NULL) |
971 | 0 | key = KEYC_DRAGGING; |
972 | | |
973 | | /* |
974 | | * Begin a drag by setting the flag to a non-zero value that |
975 | | * corresponds to the mouse button in use. If starting to drag |
976 | | * the scrollbar, store the relative position in the slider |
977 | | * where the user grabbed. |
978 | | */ |
979 | 0 | c->tty.mouse_drag_flag = MOUSE_BUTTONS(b) + 1; |
980 | 0 | if (c->tty.mouse_scrolling_flag == 0 && |
981 | 0 | loc == KEYC_MOUSE_LOCATION_SCROLLBAR_SLIDER) { |
982 | 0 | c->tty.mouse_scrolling_flag = 1; |
983 | 0 | if (m->statusat == 0) { |
984 | 0 | c->tty.mouse_slider_mpos = sl_mpos + |
985 | 0 | m->statuslines; |
986 | 0 | } else |
987 | 0 | c->tty.mouse_slider_mpos = sl_mpos; |
988 | 0 | } |
989 | 0 | } |
990 | |
|
991 | 0 | if (key == KEYC_UNKNOWN) { |
992 | | /* Adjust the button number. */ |
993 | 0 | if (MOUSE_BUTTONS(b) == MOUSE_BUTTON_1) |
994 | 0 | bn = 1; |
995 | 0 | else if (MOUSE_BUTTONS(b) == MOUSE_BUTTON_2) |
996 | 0 | bn = 2; |
997 | 0 | else if (MOUSE_BUTTONS(b) == MOUSE_BUTTON_3) |
998 | 0 | bn = 3; |
999 | 0 | else if (MOUSE_BUTTONS(b) == MOUSE_BUTTON_6) |
1000 | 0 | bn = 6; |
1001 | 0 | else if (MOUSE_BUTTONS(b) == MOUSE_BUTTON_7) |
1002 | 0 | bn = 7; |
1003 | 0 | else if (MOUSE_BUTTONS(b) == MOUSE_BUTTON_8) |
1004 | 0 | bn = 8; |
1005 | 0 | else if (MOUSE_BUTTONS(b) == MOUSE_BUTTON_9) |
1006 | 0 | bn = 9; |
1007 | 0 | else if (MOUSE_BUTTONS(b) == MOUSE_BUTTON_10) |
1008 | 0 | bn = 10; |
1009 | 0 | else if (MOUSE_BUTTONS(b) == MOUSE_BUTTON_11) |
1010 | 0 | bn = 11; |
1011 | 0 | else |
1012 | 0 | bn = 0; |
1013 | 0 | key = KEYC_MAKE_MOUSE_KEY(type, bn, loc); |
1014 | 0 | } |
1015 | | |
1016 | | /* Apply modifiers if any. */ |
1017 | 0 | if (b & MOUSE_MASK_META) |
1018 | 0 | key |= KEYC_META; |
1019 | 0 | if (b & MOUSE_MASK_CTRL) |
1020 | 0 | key |= KEYC_CTRL; |
1021 | 0 | if (b & MOUSE_MASK_SHIFT) |
1022 | 0 | key |= KEYC_SHIFT; |
1023 | |
|
1024 | 0 | if (log_get_level() != 0) |
1025 | 0 | log_debug("mouse key is %s", key_string_lookup_key (key, 1)); |
1026 | 0 | return (key); |
1027 | 0 | } |
1028 | | |
1029 | | /* Is this a bracket paste key? */ |
1030 | | static int |
1031 | | server_client_is_bracket_paste(struct client *c, key_code key) |
1032 | 0 | { |
1033 | 0 | if ((key & KEYC_MASK_KEY) == KEYC_PASTE_START) { |
1034 | 0 | c->flags |= CLIENT_BRACKETPASTING; |
1035 | 0 | log_debug("%s: bracket paste on", c->name); |
1036 | 0 | return (0); |
1037 | 0 | } |
1038 | | |
1039 | 0 | if ((key & KEYC_MASK_KEY) == KEYC_PASTE_END) { |
1040 | 0 | c->flags &= ~CLIENT_BRACKETPASTING; |
1041 | 0 | log_debug("%s: bracket paste off", c->name); |
1042 | 0 | return (0); |
1043 | 0 | } |
1044 | | |
1045 | 0 | return !!(c->flags & CLIENT_BRACKETPASTING); |
1046 | 0 | } |
1047 | | |
1048 | | /* Is this fast enough to probably be a paste? */ |
1049 | | static int |
1050 | | server_client_is_assume_paste(struct client *c) |
1051 | 0 | { |
1052 | 0 | struct session *s = c->session; |
1053 | 0 | struct timeval tv; |
1054 | 0 | int t; |
1055 | |
|
1056 | 0 | if (c->flags & CLIENT_BRACKETPASTING) |
1057 | 0 | return (0); |
1058 | 0 | if ((t = options_get_number(s->options, "assume-paste-time")) == 0) |
1059 | 0 | return (0); |
1060 | | |
1061 | 0 | timersub(&c->activity_time, &c->last_activity_time, &tv); |
1062 | 0 | if (tv.tv_sec == 0 && tv.tv_usec < t * 1000) { |
1063 | 0 | if (c->flags & CLIENT_ASSUMEPASTING) |
1064 | 0 | return (1); |
1065 | 0 | c->flags |= CLIENT_ASSUMEPASTING; |
1066 | 0 | log_debug("%s: assume paste on", c->name); |
1067 | 0 | return (0); |
1068 | 0 | } |
1069 | 0 | if (c->flags & CLIENT_ASSUMEPASTING) { |
1070 | 0 | c->flags &= ~CLIENT_ASSUMEPASTING; |
1071 | 0 | log_debug("%s: assume paste off", c->name); |
1072 | 0 | } |
1073 | 0 | return (0); |
1074 | 0 | } |
1075 | | |
1076 | | /* Has the latest client changed? */ |
1077 | | static void |
1078 | | server_client_update_latest(struct client *c) |
1079 | 0 | { |
1080 | 0 | struct window *w; |
1081 | |
|
1082 | 0 | if (c->session == NULL) |
1083 | 0 | return; |
1084 | 0 | w = c->session->curw->window; |
1085 | |
|
1086 | 0 | if (w->latest == c) |
1087 | 0 | return; |
1088 | 0 | w->latest = c; |
1089 | |
|
1090 | 0 | if (options_get_number(w->options, "window-size") == WINDOW_SIZE_LATEST) |
1091 | 0 | recalculate_size(w, 0); |
1092 | |
|
1093 | 0 | notify_client("client-active", c); |
1094 | 0 | } |
1095 | | |
1096 | | /* Get repeat time. */ |
1097 | | static u_int |
1098 | | server_client_repeat_time(struct client *c, struct key_binding *bd) |
1099 | 0 | { |
1100 | 0 | struct session *s = c->session; |
1101 | 0 | u_int repeat, initial; |
1102 | |
|
1103 | 0 | if (~bd->flags & KEY_BINDING_REPEAT) |
1104 | 0 | return (0); |
1105 | 0 | repeat = options_get_number(s->options, "repeat-time"); |
1106 | 0 | if (repeat == 0) |
1107 | 0 | return (0); |
1108 | 0 | if ((~c->flags & CLIENT_REPEAT) || bd->key != c->last_key) { |
1109 | 0 | initial = options_get_number(s->options, "initial-repeat-time"); |
1110 | 0 | if (initial != 0) |
1111 | 0 | repeat = initial; |
1112 | 0 | } |
1113 | 0 | return (repeat); |
1114 | 0 | } |
1115 | | |
1116 | | /* |
1117 | | * Handle data key input from client. This owns and can modify the key event it |
1118 | | * is given and is responsible for freeing it. |
1119 | | */ |
1120 | | static enum cmd_retval |
1121 | | server_client_key_callback(struct cmdq_item *item, void *data) |
1122 | 0 | { |
1123 | 0 | struct client *c = cmdq_get_client(item); |
1124 | 0 | struct key_event *event = data; |
1125 | 0 | key_code key = event->key; |
1126 | 0 | struct mouse_event *m = &event->m; |
1127 | 0 | struct session *s = c->session; |
1128 | 0 | struct winlink *wl; |
1129 | 0 | struct window_pane *wp; |
1130 | 0 | struct window_mode_entry *wme; |
1131 | 0 | struct timeval tv; |
1132 | 0 | struct key_table *table, *first; |
1133 | 0 | struct key_binding *bd; |
1134 | 0 | u_int repeat; |
1135 | 0 | uint64_t flags, prefix_delay; |
1136 | 0 | struct cmd_find_state fs; |
1137 | 0 | key_code key0, prefix, prefix2; |
1138 | | |
1139 | | /* Check the client is good to accept input. */ |
1140 | 0 | if (s == NULL || (c->flags & CLIENT_UNATTACHEDFLAGS)) |
1141 | 0 | goto out; |
1142 | 0 | wl = s->curw; |
1143 | | |
1144 | | /* Update the activity timer. */ |
1145 | 0 | memcpy(&c->last_activity_time, &c->activity_time, |
1146 | 0 | sizeof c->last_activity_time); |
1147 | 0 | if (gettimeofday(&c->activity_time, NULL) != 0) |
1148 | 0 | fatal("gettimeofday failed"); |
1149 | 0 | session_update_activity(s, &c->activity_time); |
1150 | | |
1151 | | /* Check for mouse keys. */ |
1152 | 0 | m->valid = 0; |
1153 | 0 | if (key == KEYC_MOUSE || key == KEYC_DOUBLECLICK) { |
1154 | 0 | if (c->flags & CLIENT_READONLY) |
1155 | 0 | goto out; |
1156 | 0 | key = server_client_check_mouse(c, event); |
1157 | 0 | if (key == KEYC_UNKNOWN) |
1158 | 0 | goto out; |
1159 | | |
1160 | 0 | m->valid = 1; |
1161 | 0 | m->key = key; |
1162 | | |
1163 | | /* |
1164 | | * Mouse drag is in progress, so fire the callback (now that |
1165 | | * the mouse event is valid). |
1166 | | */ |
1167 | 0 | if ((key & KEYC_MASK_KEY) == KEYC_DRAGGING) { |
1168 | 0 | c->tty.mouse_drag_update(c, m); |
1169 | 0 | goto out; |
1170 | 0 | } |
1171 | 0 | event->key = key; |
1172 | 0 | } |
1173 | | |
1174 | | /* Find affected pane. */ |
1175 | 0 | if (!KEYC_IS_MOUSE(key) || cmd_find_from_mouse(&fs, m, 0) != 0) |
1176 | 0 | cmd_find_from_client(&fs, c, 0); |
1177 | 0 | wp = fs.wp; |
1178 | | |
1179 | | /* Forward mouse keys if disabled. */ |
1180 | 0 | if (KEYC_IS_MOUSE(key) && !options_get_number(s->options, "mouse")) |
1181 | 0 | goto forward_key; |
1182 | | |
1183 | | /* Forward if bracket pasting. */ |
1184 | 0 | if (server_client_is_bracket_paste (c, key)) |
1185 | 0 | goto paste_key; |
1186 | | |
1187 | | /* Treat everything as a regular key when pasting is detected. */ |
1188 | 0 | if (!KEYC_IS_MOUSE(key) && |
1189 | 0 | key != KEYC_FOCUS_IN && |
1190 | 0 | key != KEYC_FOCUS_OUT && |
1191 | 0 | (~key & KEYC_SENT) && |
1192 | 0 | server_client_is_assume_paste(c)) |
1193 | 0 | goto paste_key; |
1194 | | |
1195 | | /* |
1196 | | * Work out the current key table. If the pane is in a mode, use |
1197 | | * the mode table instead of the default key table. |
1198 | | */ |
1199 | 0 | if (server_client_is_default_key_table(c, c->keytable) && |
1200 | 0 | wp != NULL && |
1201 | 0 | (wme = TAILQ_FIRST(&wp->modes)) != NULL && |
1202 | 0 | wme->mode->key_table != NULL) |
1203 | 0 | table = key_bindings_get_table(wme->mode->key_table(wme), 1); |
1204 | 0 | else |
1205 | 0 | table = c->keytable; |
1206 | 0 | first = table; |
1207 | |
|
1208 | 0 | table_changed: |
1209 | | /* |
1210 | | * The prefix always takes precedence and forces a switch to the prefix |
1211 | | * table, unless we are already there. |
1212 | | */ |
1213 | 0 | prefix = (key_code)options_get_number(s->options, "prefix"); |
1214 | 0 | prefix2 = (key_code)options_get_number(s->options, "prefix2"); |
1215 | 0 | key0 = (key & (KEYC_MASK_KEY|KEYC_MASK_MODIFIERS)); |
1216 | 0 | if ((key0 == (prefix & (KEYC_MASK_KEY|KEYC_MASK_MODIFIERS)) || |
1217 | 0 | key0 == (prefix2 & (KEYC_MASK_KEY|KEYC_MASK_MODIFIERS))) && |
1218 | 0 | strcmp(table->name, "prefix") != 0) { |
1219 | 0 | server_client_set_key_table(c, "prefix"); |
1220 | 0 | server_status_client(c); |
1221 | 0 | goto out; |
1222 | 0 | } |
1223 | 0 | flags = c->flags; |
1224 | |
|
1225 | 0 | try_again: |
1226 | | /* Log key table. */ |
1227 | 0 | if (wp == NULL) |
1228 | 0 | log_debug("key table %s (no pane)", table->name); |
1229 | 0 | else |
1230 | 0 | log_debug("key table %s (pane %%%u)", table->name, wp->id); |
1231 | 0 | if (c->flags & CLIENT_REPEAT) |
1232 | 0 | log_debug("currently repeating"); |
1233 | |
|
1234 | 0 | bd = key_bindings_get(table, key0); |
1235 | | |
1236 | | /* |
1237 | | * If prefix-timeout is enabled and we're in the prefix table, see if |
1238 | | * the timeout has been exceeded. Revert to the root table if so. |
1239 | | */ |
1240 | 0 | prefix_delay = options_get_number(global_options, "prefix-timeout"); |
1241 | 0 | if (prefix_delay > 0 && |
1242 | 0 | strcmp(table->name, "prefix") == 0 && |
1243 | 0 | server_client_key_table_activity_diff(c) > prefix_delay) { |
1244 | | /* |
1245 | | * If repeating is active and this is a repeating binding, |
1246 | | * ignore the timeout. |
1247 | | */ |
1248 | 0 | if (bd != NULL && |
1249 | 0 | (c->flags & CLIENT_REPEAT) && |
1250 | 0 | (bd->flags & KEY_BINDING_REPEAT)) { |
1251 | 0 | log_debug("prefix timeout ignored, repeat is active"); |
1252 | 0 | } else { |
1253 | 0 | log_debug("prefix timeout exceeded"); |
1254 | 0 | server_client_set_key_table(c, NULL); |
1255 | 0 | first = table = c->keytable; |
1256 | 0 | server_status_client(c); |
1257 | 0 | goto table_changed; |
1258 | 0 | } |
1259 | 0 | } |
1260 | | |
1261 | | /* Try to see if there is a key binding in the current table. */ |
1262 | 0 | if (bd != NULL) { |
1263 | | /* |
1264 | | * Key was matched in this table. If currently repeating but a |
1265 | | * non-repeating binding was found, stop repeating and try |
1266 | | * again in the root table. |
1267 | | */ |
1268 | 0 | if ((c->flags & CLIENT_REPEAT) && |
1269 | 0 | (~bd->flags & KEY_BINDING_REPEAT)) { |
1270 | 0 | log_debug("found in key table %s (not repeating)", |
1271 | 0 | table->name); |
1272 | 0 | server_client_set_key_table(c, NULL); |
1273 | 0 | first = table = c->keytable; |
1274 | 0 | c->flags &= ~CLIENT_REPEAT; |
1275 | 0 | server_status_client(c); |
1276 | 0 | goto table_changed; |
1277 | 0 | } |
1278 | 0 | log_debug("found in key table %s", table->name); |
1279 | | |
1280 | | /* |
1281 | | * Take a reference to this table to make sure the key binding |
1282 | | * doesn't disappear. |
1283 | | */ |
1284 | 0 | table->references++; |
1285 | | |
1286 | | /* |
1287 | | * If this is a repeating key, start the timer. Otherwise reset |
1288 | | * the client back to the root table. |
1289 | | */ |
1290 | 0 | repeat = server_client_repeat_time(c, bd); |
1291 | 0 | if (repeat != 0) { |
1292 | 0 | c->flags |= CLIENT_REPEAT; |
1293 | 0 | c->last_key = bd->key; |
1294 | |
|
1295 | 0 | tv.tv_sec = repeat / 1000; |
1296 | 0 | tv.tv_usec = (repeat % 1000) * 1000L; |
1297 | 0 | evtimer_del(&c->repeat_timer); |
1298 | 0 | evtimer_add(&c->repeat_timer, &tv); |
1299 | 0 | } else { |
1300 | 0 | c->flags &= ~CLIENT_REPEAT; |
1301 | 0 | server_client_set_key_table(c, NULL); |
1302 | 0 | } |
1303 | 0 | server_status_client(c); |
1304 | | |
1305 | | /* Execute the key binding. */ |
1306 | 0 | key_bindings_dispatch(bd, item, c, event, &fs); |
1307 | 0 | key_bindings_unref_table(table); |
1308 | 0 | goto out; |
1309 | 0 | } |
1310 | | |
1311 | | /* |
1312 | | * No match, try the ANY key. |
1313 | | */ |
1314 | 0 | if (key0 != KEYC_ANY) { |
1315 | 0 | key0 = KEYC_ANY; |
1316 | 0 | goto try_again; |
1317 | 0 | } |
1318 | | |
1319 | | /* |
1320 | | * Binding movement keys is useless since we only turn them on when the |
1321 | | * application requests, so don't let them exit the prefix table. |
1322 | | */ |
1323 | 0 | if (key == KEYC_MOUSEMOVE_PANE || |
1324 | 0 | key == KEYC_MOUSEMOVE_STATUS || |
1325 | 0 | key == KEYC_MOUSEMOVE_STATUS_LEFT || |
1326 | 0 | key == KEYC_MOUSEMOVE_STATUS_RIGHT || |
1327 | 0 | key == KEYC_MOUSEMOVE_STATUS_DEFAULT || |
1328 | 0 | key == KEYC_MOUSEMOVE_BORDER) |
1329 | 0 | goto forward_key; |
1330 | | |
1331 | | /* |
1332 | | * No match in this table. If not in the root table or if repeating |
1333 | | * switch the client back to the root table and try again. |
1334 | | */ |
1335 | 0 | log_debug("not found in key table %s", table->name); |
1336 | 0 | if (!server_client_is_default_key_table(c, table) || |
1337 | 0 | (c->flags & CLIENT_REPEAT)) { |
1338 | 0 | log_debug("trying in root table"); |
1339 | 0 | server_client_set_key_table(c, NULL); |
1340 | 0 | table = c->keytable; |
1341 | 0 | if (c->flags & CLIENT_REPEAT) |
1342 | 0 | first = table; |
1343 | 0 | c->flags &= ~CLIENT_REPEAT; |
1344 | 0 | server_status_client(c); |
1345 | 0 | goto table_changed; |
1346 | 0 | } |
1347 | | |
1348 | | /* |
1349 | | * No match in the root table either. If this wasn't the first table |
1350 | | * tried, don't pass the key to the pane. |
1351 | | */ |
1352 | 0 | if (first != table && (~flags & CLIENT_REPEAT)) { |
1353 | 0 | server_client_set_key_table(c, NULL); |
1354 | 0 | server_status_client(c); |
1355 | 0 | goto out; |
1356 | 0 | } |
1357 | | |
1358 | 0 | forward_key: |
1359 | 0 | if (wp != NULL && |
1360 | 0 | (wp->flags & PANE_EXITED) && |
1361 | 0 | !KEYC_IS_MOUSE(key) && |
1362 | 0 | !KEYC_IS_PASTE(key) && |
1363 | 0 | options_get_number(wp->options, "remain-on-exit") == 3) { |
1364 | 0 | options_set_number(wp->options, "remain-on-exit", 0); |
1365 | 0 | server_destroy_pane(wp, 0); |
1366 | 0 | goto out; |
1367 | 0 | } |
1368 | 0 | if (c->flags & CLIENT_READONLY) |
1369 | 0 | goto out; |
1370 | 0 | if (wp != NULL) |
1371 | 0 | window_pane_key(wp, c, s, wl, key, m); |
1372 | 0 | goto out; |
1373 | | |
1374 | 0 | paste_key: |
1375 | 0 | if (c->flags & CLIENT_READONLY) |
1376 | 0 | goto out; |
1377 | 0 | if (event->buf != NULL) |
1378 | 0 | window_pane_paste(wp, key, event->buf, event->len); |
1379 | 0 | key = KEYC_NONE; |
1380 | 0 | goto out; |
1381 | | |
1382 | 0 | out: |
1383 | 0 | if (s != NULL && key != KEYC_FOCUS_OUT) |
1384 | 0 | server_client_update_latest(c); |
1385 | 0 | free(event->buf); |
1386 | 0 | free(event); |
1387 | 0 | return (CMD_RETURN_NORMAL); |
1388 | 0 | } |
1389 | | |
1390 | | /* Handle a key event. */ |
1391 | | int |
1392 | | server_client_handle_key(struct client *c, struct key_event *event) |
1393 | 0 | { |
1394 | 0 | struct session *s = c->session; |
1395 | 0 | struct cmdq_item *item; |
1396 | | |
1397 | | /* Check the client is good to accept input. */ |
1398 | 0 | if (s == NULL || (c->flags & CLIENT_UNATTACHEDFLAGS)) |
1399 | 0 | return (0); |
1400 | | |
1401 | | /* |
1402 | | * Handle theme reporting keys before overlays so they work even when a |
1403 | | * popup is open. |
1404 | | */ |
1405 | 0 | if (event->key == KEYC_REPORT_LIGHT_THEME) { |
1406 | 0 | server_client_report_theme(c, THEME_LIGHT); |
1407 | 0 | return (0); |
1408 | 0 | } |
1409 | 0 | if (event->key == KEYC_REPORT_DARK_THEME) { |
1410 | 0 | server_client_report_theme(c, THEME_DARK); |
1411 | 0 | return (0); |
1412 | 0 | } |
1413 | | |
1414 | | /* |
1415 | | * Key presses in overlay mode and the command prompt are a special |
1416 | | * case. The queue might be blocked so they need to be processed |
1417 | | * immediately rather than queued. |
1418 | | */ |
1419 | 0 | if (~c->flags & CLIENT_READONLY) { |
1420 | 0 | if (c->message_string != NULL) { |
1421 | 0 | if (c->message_ignore_keys) |
1422 | 0 | return (0); |
1423 | 0 | status_message_clear(c); |
1424 | 0 | } |
1425 | 0 | if (c->overlay_key != NULL) { |
1426 | 0 | switch (c->overlay_key(c, c->overlay_data, event)) { |
1427 | 0 | case 0: |
1428 | 0 | return (0); |
1429 | 0 | case 1: |
1430 | 0 | server_client_clear_overlay(c); |
1431 | 0 | return (0); |
1432 | 0 | } |
1433 | 0 | } |
1434 | 0 | server_client_clear_overlay(c); |
1435 | 0 | if (c->prompt_string != NULL) { |
1436 | 0 | if (status_prompt_key(c, event->key) == 0) |
1437 | 0 | return (0); |
1438 | 0 | } |
1439 | 0 | } |
1440 | | |
1441 | | /* |
1442 | | * Add the key to the queue so it happens after any commands queued by |
1443 | | * previous keys. |
1444 | | */ |
1445 | 0 | item = cmdq_get_callback(server_client_key_callback, event); |
1446 | 0 | cmdq_append(c, item); |
1447 | 0 | return (1); |
1448 | 0 | } |
1449 | | |
1450 | | /* Client functions that need to happen every loop. */ |
1451 | | void |
1452 | | server_client_loop(void) |
1453 | 0 | { |
1454 | 0 | struct client *c; |
1455 | 0 | struct window *w; |
1456 | 0 | struct window_pane *wp; |
1457 | 0 | struct window_mode_entry *wme; |
1458 | | |
1459 | | /* Check for window resize. This is done before redrawing. */ |
1460 | 0 | RB_FOREACH(w, windows, &windows) |
1461 | 0 | server_client_check_window_resize(w); |
1462 | | |
1463 | | /* Notify modes that pane styles may have changed. */ |
1464 | 0 | RB_FOREACH(w, windows, &windows) { |
1465 | 0 | TAILQ_FOREACH(wp, &w->panes, entry) { |
1466 | 0 | if (wp->flags & PANE_STYLECHANGED) { |
1467 | 0 | wme = TAILQ_FIRST(&wp->modes); |
1468 | 0 | if (wme != NULL && |
1469 | 0 | wme->mode->style_changed != NULL) |
1470 | 0 | wme->mode->style_changed(wme); |
1471 | 0 | } |
1472 | 0 | } |
1473 | 0 | } |
1474 | | |
1475 | | /* Check clients. */ |
1476 | 0 | TAILQ_FOREACH(c, &clients, entry) { |
1477 | 0 | server_client_check_exit(c); |
1478 | 0 | if (c->session != NULL && c->session->curw != NULL) { |
1479 | 0 | server_client_check_modes(c); |
1480 | 0 | server_client_check_redraw(c); |
1481 | 0 | server_client_reset_state(c); |
1482 | 0 | } |
1483 | 0 | } |
1484 | | |
1485 | | /* |
1486 | | * Any windows will have been redrawn as part of clients, so clear |
1487 | | * their flags now. |
1488 | | */ |
1489 | 0 | RB_FOREACH(w, windows, &windows) { |
1490 | 0 | TAILQ_FOREACH(wp, &w->panes, entry) { |
1491 | 0 | if (wp->fd != -1) { |
1492 | 0 | server_client_check_pane_resize(wp); |
1493 | 0 | server_client_check_pane_buffer(wp); |
1494 | 0 | } |
1495 | 0 | wp->flags &= ~(PANE_REDRAW|PANE_REDRAWSCROLLBAR); |
1496 | 0 | } |
1497 | 0 | check_window_name(w); |
1498 | 0 | } |
1499 | | |
1500 | | /* Send theme updates. */ |
1501 | 0 | RB_FOREACH(w, windows, &windows) { |
1502 | 0 | TAILQ_FOREACH(wp, &w->panes, entry) |
1503 | 0 | window_pane_send_theme_update(wp); |
1504 | 0 | } |
1505 | 0 | } |
1506 | | |
1507 | | /* Check if window needs to be resized. */ |
1508 | | static void |
1509 | | server_client_check_window_resize(struct window *w) |
1510 | 0 | { |
1511 | 0 | struct winlink *wl; |
1512 | |
|
1513 | 0 | if (~w->flags & WINDOW_RESIZE) |
1514 | 0 | return; |
1515 | | |
1516 | 0 | TAILQ_FOREACH(wl, &w->winlinks, wentry) { |
1517 | 0 | if (wl->session->attached != 0 && wl->session->curw == wl) |
1518 | 0 | break; |
1519 | 0 | } |
1520 | 0 | if (wl == NULL) |
1521 | 0 | return; |
1522 | | |
1523 | 0 | log_debug("%s: resizing window @%u", __func__, w->id); |
1524 | 0 | resize_window(w, w->new_sx, w->new_sy, w->new_xpixel, w->new_ypixel); |
1525 | 0 | } |
1526 | | |
1527 | | /* Resize timer event. */ |
1528 | | static void |
1529 | | server_client_resize_timer(__unused int fd, __unused short events, void *data) |
1530 | 0 | { |
1531 | 0 | struct window_pane *wp = data; |
1532 | |
|
1533 | 0 | log_debug("%s: %%%u resize timer expired", __func__, wp->id); |
1534 | 0 | evtimer_del(&wp->resize_timer); |
1535 | 0 | } |
1536 | | |
1537 | | /* Check if pane should be resized. */ |
1538 | | static void |
1539 | | server_client_check_pane_resize(struct window_pane *wp) |
1540 | 0 | { |
1541 | 0 | struct window_pane_resize *r; |
1542 | 0 | struct window_pane_resize *r1; |
1543 | 0 | struct window_pane_resize *first; |
1544 | 0 | struct window_pane_resize *last; |
1545 | 0 | struct timeval tv = { .tv_usec = 250000 }; |
1546 | |
|
1547 | 0 | if (TAILQ_EMPTY(&wp->resize_queue)) |
1548 | 0 | return; |
1549 | | |
1550 | 0 | if (!event_initialized(&wp->resize_timer)) |
1551 | 0 | evtimer_set(&wp->resize_timer, server_client_resize_timer, wp); |
1552 | 0 | if (evtimer_pending(&wp->resize_timer, NULL)) |
1553 | 0 | return; |
1554 | | |
1555 | 0 | log_debug("%s: %%%u needs to be resized", __func__, wp->id); |
1556 | 0 | TAILQ_FOREACH(r, &wp->resize_queue, entry) { |
1557 | 0 | log_debug("queued resize: %ux%u -> %ux%u", r->osx, r->osy, |
1558 | 0 | r->sx, r->sy); |
1559 | 0 | } |
1560 | | |
1561 | | /* |
1562 | | * There are three cases that matter: |
1563 | | * |
1564 | | * - Only one resize. It can just be applied. |
1565 | | * |
1566 | | * - Multiple resizes and the ending size is different from the |
1567 | | * starting size. We can discard all resizes except the most recent. |
1568 | | * |
1569 | | * - Multiple resizes and the ending size is the same as the starting |
1570 | | * size. We must resize at least twice to force the application to |
1571 | | * redraw. So apply the first and leave the last on the queue for |
1572 | | * next time. |
1573 | | */ |
1574 | 0 | first = TAILQ_FIRST(&wp->resize_queue); |
1575 | 0 | last = TAILQ_LAST(&wp->resize_queue, window_pane_resizes); |
1576 | 0 | if (first == last) { |
1577 | | /* Only one resize. */ |
1578 | 0 | window_pane_send_resize(wp, first->sx, first->sy); |
1579 | 0 | TAILQ_REMOVE(&wp->resize_queue, first, entry); |
1580 | 0 | free(first); |
1581 | 0 | } else if (last->sx != first->osx || last->sy != first->osy) { |
1582 | | /* Multiple resizes ending up with a different size. */ |
1583 | 0 | window_pane_send_resize(wp, last->sx, last->sy); |
1584 | 0 | TAILQ_FOREACH_SAFE(r, &wp->resize_queue, entry, r1) { |
1585 | 0 | TAILQ_REMOVE(&wp->resize_queue, r, entry); |
1586 | 0 | free(r); |
1587 | 0 | } |
1588 | 0 | } else { |
1589 | | /* |
1590 | | * Multiple resizes ending up with the same size. There will |
1591 | | * not be more than one to the same size in succession so we |
1592 | | * can just use the last-but-one on the list and leave the last |
1593 | | * for later. We reduce the time until the next check to avoid |
1594 | | * a long delay between the resizes. |
1595 | | */ |
1596 | 0 | r = TAILQ_PREV(last, window_pane_resizes, entry); |
1597 | 0 | window_pane_send_resize(wp, r->sx, r->sy); |
1598 | 0 | TAILQ_FOREACH_SAFE(r, &wp->resize_queue, entry, r1) { |
1599 | 0 | if (r == last) |
1600 | 0 | break; |
1601 | 0 | TAILQ_REMOVE(&wp->resize_queue, r, entry); |
1602 | 0 | free(r); |
1603 | 0 | } |
1604 | 0 | tv.tv_usec = 10000; |
1605 | 0 | } |
1606 | 0 | evtimer_add(&wp->resize_timer, &tv); |
1607 | 0 | } |
1608 | | |
1609 | | /* Check pane buffer size. */ |
1610 | | static void |
1611 | | server_client_check_pane_buffer(struct window_pane *wp) |
1612 | 0 | { |
1613 | 0 | struct evbuffer *evb = wp->event->input; |
1614 | 0 | size_t minimum; |
1615 | 0 | struct client *c; |
1616 | 0 | struct window_pane_offset *wpo; |
1617 | 0 | int off = 1, flag; |
1618 | 0 | u_int attached_clients = 0; |
1619 | 0 | size_t new_size; |
1620 | | |
1621 | | /* |
1622 | | * Work out the minimum used size. This is the most that can be removed |
1623 | | * from the buffer. |
1624 | | */ |
1625 | 0 | minimum = wp->offset.used; |
1626 | 0 | if (wp->pipe_fd != -1 && wp->pipe_offset.used < minimum) |
1627 | 0 | minimum = wp->pipe_offset.used; |
1628 | 0 | TAILQ_FOREACH(c, &clients, entry) { |
1629 | 0 | if (c->session == NULL) |
1630 | 0 | continue; |
1631 | 0 | attached_clients++; |
1632 | |
|
1633 | 0 | if (~c->flags & CLIENT_CONTROL) { |
1634 | 0 | off = 0; |
1635 | 0 | continue; |
1636 | 0 | } |
1637 | 0 | wpo = control_pane_offset(c, wp, &flag); |
1638 | 0 | if (wpo == NULL) { |
1639 | 0 | if (!flag) |
1640 | 0 | off = 0; |
1641 | 0 | continue; |
1642 | 0 | } |
1643 | 0 | if (!flag) |
1644 | 0 | off = 0; |
1645 | |
|
1646 | 0 | window_pane_get_new_data(wp, wpo, &new_size); |
1647 | 0 | log_debug("%s: %s has %zu bytes used and %zu left for %%%u", |
1648 | 0 | __func__, c->name, wpo->used - wp->base_offset, new_size, |
1649 | 0 | wp->id); |
1650 | 0 | if (wpo->used < minimum) |
1651 | 0 | minimum = wpo->used; |
1652 | 0 | } |
1653 | 0 | if (attached_clients == 0) |
1654 | 0 | off = 0; |
1655 | 0 | minimum -= wp->base_offset; |
1656 | 0 | if (minimum == 0) |
1657 | 0 | goto out; |
1658 | | |
1659 | | /* Drain the buffer. */ |
1660 | 0 | log_debug("%s: %%%u has %zu minimum (of %zu) bytes used", __func__, |
1661 | 0 | wp->id, minimum, EVBUFFER_LENGTH(evb)); |
1662 | 0 | evbuffer_drain(evb, minimum); |
1663 | | |
1664 | | /* |
1665 | | * Adjust the base offset. If it would roll over, all the offsets into |
1666 | | * the buffer need to be adjusted. |
1667 | | */ |
1668 | 0 | if (wp->base_offset > SIZE_MAX - minimum) { |
1669 | 0 | log_debug("%s: %%%u base offset has wrapped", __func__, wp->id); |
1670 | 0 | wp->offset.used -= wp->base_offset; |
1671 | 0 | if (wp->pipe_fd != -1) |
1672 | 0 | wp->pipe_offset.used -= wp->base_offset; |
1673 | 0 | TAILQ_FOREACH(c, &clients, entry) { |
1674 | 0 | if (c->session == NULL || (~c->flags & CLIENT_CONTROL)) |
1675 | 0 | continue; |
1676 | 0 | wpo = control_pane_offset(c, wp, &flag); |
1677 | 0 | if (wpo != NULL && !flag) |
1678 | 0 | wpo->used -= wp->base_offset; |
1679 | 0 | } |
1680 | 0 | wp->base_offset = minimum; |
1681 | 0 | } else |
1682 | 0 | wp->base_offset += minimum; |
1683 | |
|
1684 | 0 | out: |
1685 | | /* |
1686 | | * If there is data remaining, and there are no clients able to consume |
1687 | | * it, do not read any more. This is true when there are attached |
1688 | | * clients, all of which are control clients which are not able to |
1689 | | * accept any more data. |
1690 | | */ |
1691 | 0 | log_debug("%s: pane %%%u is %s", __func__, wp->id, off ? "off" : "on"); |
1692 | 0 | if (off) |
1693 | 0 | bufferevent_disable(wp->event, EV_READ); |
1694 | 0 | else |
1695 | 0 | bufferevent_enable(wp->event, EV_READ); |
1696 | 0 | } |
1697 | | |
1698 | | /* |
1699 | | * Update cursor position and mode settings. The scroll region and attributes |
1700 | | * are cleared when idle (waiting for an event) as this is the most likely time |
1701 | | * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a |
1702 | | * compromise between excessive resets and likelihood of an interrupt. |
1703 | | * |
1704 | | * tty_region/tty_reset/tty_update_mode already take care of not resetting |
1705 | | * things that are already in their default state. |
1706 | | */ |
1707 | | static void |
1708 | | server_client_reset_state(struct client *c) |
1709 | 0 | { |
1710 | 0 | struct tty *tty = &c->tty; |
1711 | 0 | struct window *w = c->session->curw->window; |
1712 | 0 | struct window_pane *wp = server_client_get_pane(c), *loop; |
1713 | 0 | struct screen *s = NULL; |
1714 | 0 | struct options *oo = c->session->options; |
1715 | 0 | int mode = 0, cursor, flags; |
1716 | 0 | u_int cx = 0, cy = 0, ox, oy, sx, sy, n; |
1717 | |
|
1718 | 0 | if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED)) |
1719 | 0 | return; |
1720 | | |
1721 | | /* Disable the block flag. */ |
1722 | 0 | flags = (tty->flags & TTY_BLOCK); |
1723 | 0 | tty->flags &= ~TTY_BLOCK; |
1724 | | |
1725 | | /* Get mode from overlay if any, else from screen. */ |
1726 | 0 | if (c->overlay_draw != NULL) { |
1727 | 0 | if (c->overlay_mode != NULL) |
1728 | 0 | s = c->overlay_mode(c, c->overlay_data, &cx, &cy); |
1729 | 0 | } else if (c->prompt_string == NULL) |
1730 | 0 | s = wp->screen; |
1731 | 0 | else |
1732 | 0 | s = c->status.active; |
1733 | 0 | if (s != NULL) |
1734 | 0 | mode = s->mode; |
1735 | 0 | if (log_get_level() != 0) { |
1736 | 0 | log_debug("%s: client %s mode %s", __func__, c->name, |
1737 | 0 | screen_mode_to_string(mode)); |
1738 | 0 | } |
1739 | | |
1740 | | /* Reset region and margin. */ |
1741 | 0 | tty_region_off(tty); |
1742 | 0 | tty_margin_off(tty); |
1743 | | |
1744 | | /* Move cursor to pane cursor and offset. */ |
1745 | 0 | if (c->prompt_string != NULL) { |
1746 | 0 | n = options_get_number(oo, "status-position"); |
1747 | 0 | if (n == 0) |
1748 | 0 | cy = status_prompt_line_at(c); |
1749 | 0 | else { |
1750 | 0 | n = status_line_size(c) - status_prompt_line_at(c); |
1751 | 0 | if (n <= tty->sy) |
1752 | 0 | cy = tty->sy - n; |
1753 | 0 | else |
1754 | 0 | cy = tty->sy - 1; |
1755 | 0 | } |
1756 | 0 | cx = c->prompt_cursor; |
1757 | 0 | } else if (c->overlay_draw == NULL) { |
1758 | 0 | cursor = 0; |
1759 | 0 | tty_window_offset(tty, &ox, &oy, &sx, &sy); |
1760 | 0 | if (wp->xoff + s->cx >= ox && wp->xoff + s->cx <= ox + sx && |
1761 | 0 | wp->yoff + s->cy >= oy && wp->yoff + s->cy <= oy + sy) { |
1762 | 0 | cursor = 1; |
1763 | |
|
1764 | 0 | cx = wp->xoff + s->cx - ox; |
1765 | 0 | cy = wp->yoff + s->cy - oy; |
1766 | |
|
1767 | 0 | if (status_at_line(c) == 0) |
1768 | 0 | cy += status_line_size(c); |
1769 | 0 | } |
1770 | 0 | if (!cursor) |
1771 | 0 | mode &= ~MODE_CURSOR; |
1772 | 0 | } |
1773 | 0 | log_debug("%s: cursor to %u,%u", __func__, cx, cy); |
1774 | 0 | tty_cursor(tty, cx, cy); |
1775 | | |
1776 | | /* |
1777 | | * Set mouse mode if requested. To support dragging, always use button |
1778 | | * mode. For focus-follows-mouse, we need all-motion mode to receive |
1779 | | * movement events. |
1780 | | */ |
1781 | 0 | if (options_get_number(oo, "mouse")) { |
1782 | 0 | if (c->overlay_draw == NULL) { |
1783 | 0 | mode &= ~ALL_MOUSE_MODES; |
1784 | 0 | TAILQ_FOREACH(loop, &w->panes, entry) { |
1785 | 0 | if (loop->screen->mode & MODE_MOUSE_ALL) |
1786 | 0 | mode |= MODE_MOUSE_ALL; |
1787 | 0 | } |
1788 | 0 | } |
1789 | 0 | if (options_get_number(oo, "focus-follows-mouse")) |
1790 | 0 | mode |= MODE_MOUSE_ALL; |
1791 | 0 | else if (~mode & MODE_MOUSE_ALL) |
1792 | 0 | mode |= MODE_MOUSE_BUTTON; |
1793 | 0 | } |
1794 | | |
1795 | | /* Clear bracketed paste mode if at the prompt. */ |
1796 | 0 | if (c->overlay_draw == NULL && c->prompt_string != NULL) |
1797 | 0 | mode &= ~MODE_BRACKETPASTE; |
1798 | | |
1799 | | /* Set the terminal mode and reset attributes. */ |
1800 | 0 | tty_update_mode(tty, mode, s); |
1801 | 0 | tty_reset(tty); |
1802 | | |
1803 | | /* All writing must be done, send a sync end (if it was started). */ |
1804 | 0 | tty_sync_end(tty); |
1805 | 0 | tty->flags |= flags; |
1806 | 0 | } |
1807 | | |
1808 | | /* Repeat time callback. */ |
1809 | | static void |
1810 | | server_client_repeat_timer(__unused int fd, __unused short events, void *data) |
1811 | 0 | { |
1812 | 0 | struct client *c = data; |
1813 | |
|
1814 | 0 | if (c->flags & CLIENT_REPEAT) { |
1815 | 0 | server_client_set_key_table(c, NULL); |
1816 | 0 | c->flags &= ~CLIENT_REPEAT; |
1817 | 0 | server_status_client(c); |
1818 | 0 | } |
1819 | 0 | } |
1820 | | |
1821 | | /* Double-click callback. */ |
1822 | | static void |
1823 | | server_client_click_timer(__unused int fd, __unused short events, void *data) |
1824 | 0 | { |
1825 | 0 | struct client *c = data; |
1826 | 0 | struct key_event *event; |
1827 | |
|
1828 | 0 | log_debug("click timer expired"); |
1829 | |
|
1830 | 0 | if (c->flags & CLIENT_TRIPLECLICK) { |
1831 | | /* |
1832 | | * Waiting for a third click that hasn't happened, so this must |
1833 | | * have been a double click. |
1834 | | */ |
1835 | 0 | event = xcalloc(1, sizeof *event); |
1836 | 0 | event->key = KEYC_DOUBLECLICK; |
1837 | 0 | memcpy(&event->m, &c->click_event, sizeof event->m); |
1838 | 0 | if (!server_client_handle_key(c, event)) { |
1839 | 0 | free(event->buf); |
1840 | 0 | free(event); |
1841 | 0 | } |
1842 | 0 | } |
1843 | 0 | c->flags &= ~(CLIENT_DOUBLECLICK|CLIENT_TRIPLECLICK); |
1844 | 0 | } |
1845 | | |
1846 | | /* Check if client should be exited. */ |
1847 | | static void |
1848 | | server_client_check_exit(struct client *c) |
1849 | 0 | { |
1850 | 0 | struct client_file *cf; |
1851 | 0 | const char *name = c->exit_session; |
1852 | 0 | char *data; |
1853 | 0 | size_t size, msize; |
1854 | |
|
1855 | 0 | if (c->flags & (CLIENT_DEAD|CLIENT_EXITED)) |
1856 | 0 | return; |
1857 | 0 | if (~c->flags & CLIENT_EXIT) |
1858 | 0 | return; |
1859 | | |
1860 | 0 | if (c->flags & CLIENT_CONTROL) { |
1861 | 0 | control_discard(c); |
1862 | 0 | if (!control_all_done(c)) |
1863 | 0 | return; |
1864 | 0 | } |
1865 | 0 | RB_FOREACH(cf, client_files, &c->files) { |
1866 | 0 | if (EVBUFFER_LENGTH(cf->buffer) != 0) |
1867 | 0 | return; |
1868 | 0 | } |
1869 | 0 | c->flags |= CLIENT_EXITED; |
1870 | |
|
1871 | 0 | switch (c->exit_type) { |
1872 | 0 | case CLIENT_EXIT_RETURN: |
1873 | 0 | if (c->exit_message != NULL) |
1874 | 0 | msize = strlen(c->exit_message) + 1; |
1875 | 0 | else |
1876 | 0 | msize = 0; |
1877 | 0 | size = (sizeof c->retval) + msize; |
1878 | 0 | data = xmalloc(size); |
1879 | 0 | memcpy(data, &c->retval, sizeof c->retval); |
1880 | 0 | if (c->exit_message != NULL) |
1881 | 0 | memcpy(data + sizeof c->retval, c->exit_message, msize); |
1882 | 0 | proc_send(c->peer, MSG_EXIT, -1, data, size); |
1883 | 0 | free(data); |
1884 | 0 | break; |
1885 | 0 | case CLIENT_EXIT_SHUTDOWN: |
1886 | 0 | proc_send(c->peer, MSG_SHUTDOWN, -1, NULL, 0); |
1887 | 0 | break; |
1888 | 0 | case CLIENT_EXIT_DETACH: |
1889 | 0 | proc_send(c->peer, c->exit_msgtype, -1, name, strlen(name) + 1); |
1890 | 0 | break; |
1891 | 0 | } |
1892 | 0 | free(c->exit_session); |
1893 | 0 | free(c->exit_message); |
1894 | 0 | } |
1895 | | |
1896 | | /* Redraw timer callback. */ |
1897 | | static void |
1898 | | server_client_redraw_timer(__unused int fd, __unused short events, |
1899 | | __unused void *data) |
1900 | 0 | { |
1901 | 0 | log_debug("redraw timer fired"); |
1902 | 0 | } |
1903 | | |
1904 | | /* |
1905 | | * Check if modes need to be updated. Only modes in the current window are |
1906 | | * updated and it is done when the status line is redrawn. |
1907 | | */ |
1908 | | static void |
1909 | | server_client_check_modes(struct client *c) |
1910 | 0 | { |
1911 | 0 | struct window *w = c->session->curw->window; |
1912 | 0 | struct window_pane *wp; |
1913 | 0 | struct window_mode_entry *wme; |
1914 | |
|
1915 | 0 | if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED)) |
1916 | 0 | return; |
1917 | 0 | if (~c->flags & CLIENT_REDRAWSTATUS) |
1918 | 0 | return; |
1919 | 0 | TAILQ_FOREACH(wp, &w->panes, entry) { |
1920 | 0 | wme = TAILQ_FIRST(&wp->modes); |
1921 | 0 | if (wme != NULL && wme->mode->update != NULL) |
1922 | 0 | wme->mode->update(wme); |
1923 | 0 | } |
1924 | 0 | } |
1925 | | |
1926 | | /* Check for client redraws. */ |
1927 | | static void |
1928 | | server_client_check_redraw(struct client *c) |
1929 | 0 | { |
1930 | 0 | struct session *s = c->session; |
1931 | 0 | struct tty *tty = &c->tty; |
1932 | 0 | struct window *w = c->session->curw->window; |
1933 | 0 | struct window_pane *wp; |
1934 | 0 | int needed, tty_flags, mode = tty->mode; |
1935 | 0 | uint64_t client_flags = 0; |
1936 | 0 | int redraw_pane, redraw_scrollbar_only; |
1937 | 0 | u_int bit = 0; |
1938 | 0 | struct timeval tv = { .tv_usec = 1000 }; |
1939 | 0 | static struct event ev; |
1940 | 0 | size_t left; |
1941 | |
|
1942 | 0 | if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED)) |
1943 | 0 | return; |
1944 | 0 | if (c->flags & CLIENT_ALLREDRAWFLAGS) { |
1945 | 0 | log_debug("%s: redraw%s%s%s%s%s%s", c->name, |
1946 | 0 | (c->flags & CLIENT_REDRAWWINDOW) ? " window" : "", |
1947 | 0 | (c->flags & CLIENT_REDRAWSTATUS) ? " status" : "", |
1948 | 0 | (c->flags & CLIENT_REDRAWBORDERS) ? " borders" : "", |
1949 | 0 | (c->flags & CLIENT_REDRAWOVERLAY) ? " overlay" : "", |
1950 | 0 | (c->flags & CLIENT_REDRAWPANES) ? " panes" : "", |
1951 | 0 | (c->flags & CLIENT_REDRAWSCROLLBARS) ? " scrollbars" : ""); |
1952 | 0 | } |
1953 | | |
1954 | | /* |
1955 | | * If there is outstanding data, defer the redraw until it has been |
1956 | | * consumed. We can just add a timer to get out of the event loop and |
1957 | | * end up back here. |
1958 | | */ |
1959 | 0 | needed = 0; |
1960 | 0 | if (c->flags & CLIENT_ALLREDRAWFLAGS) |
1961 | 0 | needed = 1; |
1962 | 0 | else { |
1963 | 0 | TAILQ_FOREACH(wp, &w->panes, entry) { |
1964 | 0 | if (wp->flags & PANE_REDRAW) { |
1965 | 0 | needed = 1; |
1966 | 0 | client_flags |= CLIENT_REDRAWPANES; |
1967 | 0 | break; |
1968 | 0 | } |
1969 | 0 | if (wp->flags & PANE_REDRAWSCROLLBAR) { |
1970 | 0 | needed = 1; |
1971 | 0 | client_flags |= CLIENT_REDRAWSCROLLBARS; |
1972 | | /* no break - later panes may need redraw */ |
1973 | 0 | } |
1974 | 0 | } |
1975 | 0 | } |
1976 | 0 | if (needed && (left = EVBUFFER_LENGTH(tty->out)) != 0) { |
1977 | 0 | log_debug("%s: redraw deferred (%zu left)", c->name, left); |
1978 | 0 | if (!evtimer_initialized(&ev)) |
1979 | 0 | evtimer_set(&ev, server_client_redraw_timer, NULL); |
1980 | 0 | if (!evtimer_pending(&ev, NULL)) { |
1981 | 0 | log_debug("redraw timer started"); |
1982 | 0 | evtimer_add(&ev, &tv); |
1983 | 0 | } |
1984 | |
|
1985 | 0 | if (~c->flags & CLIENT_REDRAWWINDOW) { |
1986 | 0 | TAILQ_FOREACH(wp, &w->panes, entry) { |
1987 | 0 | if (wp->flags & (PANE_REDRAW)) { |
1988 | 0 | log_debug("%s: pane %%%u needs redraw", |
1989 | 0 | c->name, wp->id); |
1990 | 0 | c->redraw_panes |= (1 << bit); |
1991 | 0 | } else if (wp->flags & PANE_REDRAWSCROLLBAR) { |
1992 | 0 | log_debug("%s: pane %%%u scrollbar " |
1993 | 0 | "needs redraw", c->name, wp->id); |
1994 | 0 | c->redraw_scrollbars |= (1 << bit); |
1995 | 0 | } |
1996 | 0 | if (++bit == 64) { |
1997 | | /* |
1998 | | * If more that 64 panes, give up and |
1999 | | * just redraw the window. |
2000 | | */ |
2001 | 0 | client_flags &= ~(CLIENT_REDRAWPANES| |
2002 | 0 | CLIENT_REDRAWSCROLLBARS); |
2003 | 0 | client_flags |= CLIENT_REDRAWWINDOW; |
2004 | 0 | break; |
2005 | 0 | } |
2006 | 0 | } |
2007 | 0 | if (c->redraw_panes != 0) |
2008 | 0 | c->flags |= CLIENT_REDRAWPANES; |
2009 | 0 | if (c->redraw_scrollbars != 0) |
2010 | 0 | c->flags |= CLIENT_REDRAWSCROLLBARS; |
2011 | 0 | } |
2012 | 0 | c->flags |= client_flags; |
2013 | 0 | return; |
2014 | 0 | } else if (needed) |
2015 | 0 | log_debug("%s: redraw needed", c->name); |
2016 | | |
2017 | 0 | tty_flags = tty->flags & (TTY_BLOCK|TTY_FREEZE|TTY_NOCURSOR); |
2018 | 0 | tty->flags = (tty->flags & ~(TTY_BLOCK|TTY_FREEZE))|TTY_NOCURSOR; |
2019 | |
|
2020 | 0 | if (~c->flags & CLIENT_REDRAWWINDOW) { |
2021 | | /* |
2022 | | * If not redrawing the entire window, check whether each pane |
2023 | | * needs to be redrawn. |
2024 | | */ |
2025 | 0 | TAILQ_FOREACH(wp, &w->panes, entry) { |
2026 | 0 | redraw_pane = 0; |
2027 | 0 | redraw_scrollbar_only = 0; |
2028 | 0 | if (wp->flags & PANE_REDRAW) |
2029 | 0 | redraw_pane = 1; |
2030 | 0 | else if (c->flags & CLIENT_REDRAWPANES) { |
2031 | 0 | if (c->redraw_panes & (1 << bit)) |
2032 | 0 | redraw_pane = 1; |
2033 | 0 | } else if (c->flags & CLIENT_REDRAWSCROLLBARS) { |
2034 | 0 | if (c->redraw_scrollbars & (1 << bit)) |
2035 | 0 | redraw_scrollbar_only = 1; |
2036 | 0 | } |
2037 | 0 | bit++; |
2038 | 0 | if (!redraw_pane && !redraw_scrollbar_only) |
2039 | 0 | continue; |
2040 | 0 | if (redraw_scrollbar_only) { |
2041 | 0 | log_debug("%s: redrawing (scrollbar only) pane " |
2042 | 0 | "%%%u", __func__, wp->id); |
2043 | 0 | } else { |
2044 | 0 | log_debug("%s: redrawing pane %%%u", __func__, |
2045 | 0 | wp->id); |
2046 | 0 | } |
2047 | 0 | screen_redraw_pane(c, wp, redraw_scrollbar_only); |
2048 | 0 | } |
2049 | 0 | c->redraw_panes = 0; |
2050 | 0 | c->redraw_scrollbars = 0; |
2051 | 0 | c->flags &= ~(CLIENT_REDRAWPANES|CLIENT_REDRAWSCROLLBARS); |
2052 | 0 | } |
2053 | |
|
2054 | 0 | if (c->flags & CLIENT_ALLREDRAWFLAGS) { |
2055 | 0 | if (options_get_number(s->options, "set-titles")) { |
2056 | 0 | server_client_set_title(c); |
2057 | 0 | server_client_set_path(c); |
2058 | 0 | } |
2059 | 0 | screen_redraw_screen(c); |
2060 | 0 | } |
2061 | |
|
2062 | 0 | tty->flags = (tty->flags & ~TTY_NOCURSOR)|(tty_flags & TTY_NOCURSOR); |
2063 | 0 | tty_update_mode(tty, mode, NULL); |
2064 | 0 | tty->flags = (tty->flags & ~(TTY_BLOCK|TTY_FREEZE|TTY_NOCURSOR))| |
2065 | 0 | tty_flags; |
2066 | |
|
2067 | 0 | c->flags &= ~(CLIENT_ALLREDRAWFLAGS|CLIENT_STATUSFORCE); |
2068 | |
|
2069 | 0 | if (needed) { |
2070 | | /* |
2071 | | * We would have deferred the redraw unless the output buffer |
2072 | | * was empty, so we can record how many bytes the redraw |
2073 | | * generated. |
2074 | | */ |
2075 | 0 | c->redraw = EVBUFFER_LENGTH(tty->out); |
2076 | 0 | log_debug("%s: redraw added %zu bytes", c->name, c->redraw); |
2077 | 0 | } |
2078 | 0 | } |
2079 | | |
2080 | | /* Set client title. */ |
2081 | | static void |
2082 | | server_client_set_title(struct client *c) |
2083 | 0 | { |
2084 | 0 | struct session *s = c->session; |
2085 | 0 | const char *template; |
2086 | 0 | char *title; |
2087 | 0 | struct format_tree *ft; |
2088 | |
|
2089 | 0 | template = options_get_string(s->options, "set-titles-string"); |
2090 | |
|
2091 | 0 | ft = format_create(c, NULL, FORMAT_NONE, 0); |
2092 | 0 | format_defaults(ft, c, NULL, NULL, NULL); |
2093 | |
|
2094 | 0 | title = format_expand_time(ft, template); |
2095 | 0 | if (c->title == NULL || strcmp(title, c->title) != 0) { |
2096 | 0 | free(c->title); |
2097 | 0 | c->title = xstrdup(title); |
2098 | 0 | tty_set_title(&c->tty, c->title); |
2099 | 0 | } |
2100 | 0 | free(title); |
2101 | |
|
2102 | 0 | format_free(ft); |
2103 | 0 | } |
2104 | | |
2105 | | /* Set client path. */ |
2106 | | static void |
2107 | | server_client_set_path(struct client *c) |
2108 | 0 | { |
2109 | 0 | struct session *s = c->session; |
2110 | 0 | const char *path; |
2111 | |
|
2112 | 0 | if (s->curw == NULL) |
2113 | 0 | return; |
2114 | 0 | if (s->curw->window->active->base.path == NULL) |
2115 | 0 | path = ""; |
2116 | 0 | else |
2117 | 0 | path = s->curw->window->active->base.path; |
2118 | 0 | if (c->path == NULL || strcmp(path, c->path) != 0) { |
2119 | 0 | free(c->path); |
2120 | 0 | c->path = xstrdup(path); |
2121 | 0 | tty_set_path(&c->tty, c->path); |
2122 | 0 | } |
2123 | 0 | } |
2124 | | |
2125 | | /* Dispatch message from client. */ |
2126 | | static void |
2127 | | server_client_dispatch(struct imsg *imsg, void *arg) |
2128 | 0 | { |
2129 | 0 | struct client *c = arg; |
2130 | 0 | ssize_t datalen; |
2131 | 0 | struct session *s; |
2132 | |
|
2133 | 0 | if (c->flags & CLIENT_DEAD) |
2134 | 0 | return; |
2135 | | |
2136 | 0 | if (imsg == NULL) { |
2137 | 0 | server_client_lost(c); |
2138 | 0 | return; |
2139 | 0 | } |
2140 | | |
2141 | 0 | datalen = imsg->hdr.len - IMSG_HEADER_SIZE; |
2142 | |
|
2143 | 0 | switch (imsg->hdr.type) { |
2144 | 0 | case MSG_IDENTIFY_CLIENTPID: |
2145 | 0 | case MSG_IDENTIFY_CWD: |
2146 | 0 | case MSG_IDENTIFY_ENVIRON: |
2147 | 0 | case MSG_IDENTIFY_FEATURES: |
2148 | 0 | case MSG_IDENTIFY_FLAGS: |
2149 | 0 | case MSG_IDENTIFY_LONGFLAGS: |
2150 | 0 | case MSG_IDENTIFY_STDIN: |
2151 | 0 | case MSG_IDENTIFY_STDOUT: |
2152 | 0 | case MSG_IDENTIFY_TERM: |
2153 | 0 | case MSG_IDENTIFY_TERMINFO: |
2154 | 0 | case MSG_IDENTIFY_TTYNAME: |
2155 | 0 | case MSG_IDENTIFY_DONE: |
2156 | 0 | if (server_client_dispatch_identify(c, imsg) != 0) |
2157 | 0 | goto bad; |
2158 | 0 | break; |
2159 | 0 | case MSG_COMMAND: |
2160 | 0 | if (server_client_dispatch_command(c, imsg) != 0) |
2161 | 0 | goto bad; |
2162 | 0 | break; |
2163 | 0 | case MSG_RESIZE: |
2164 | 0 | if (datalen != 0) |
2165 | 0 | goto bad; |
2166 | | |
2167 | 0 | if (c->flags & CLIENT_CONTROL) |
2168 | 0 | break; |
2169 | 0 | server_client_update_latest(c); |
2170 | 0 | tty_resize(&c->tty); |
2171 | 0 | tty_repeat_requests(&c->tty, 0); |
2172 | 0 | recalculate_sizes(); |
2173 | 0 | if (c->overlay_resize == NULL) |
2174 | 0 | server_client_clear_overlay(c); |
2175 | 0 | else |
2176 | 0 | c->overlay_resize(c, c->overlay_data); |
2177 | 0 | server_redraw_client(c); |
2178 | 0 | if (c->session != NULL) |
2179 | 0 | notify_client("client-resized", c); |
2180 | 0 | break; |
2181 | 0 | case MSG_EXITING: |
2182 | 0 | if (datalen != 0) |
2183 | 0 | goto bad; |
2184 | 0 | server_client_set_session(c, NULL); |
2185 | 0 | recalculate_sizes(); |
2186 | 0 | tty_close(&c->tty); |
2187 | 0 | proc_send(c->peer, MSG_EXITED, -1, NULL, 0); |
2188 | 0 | break; |
2189 | 0 | case MSG_WAKEUP: |
2190 | 0 | case MSG_UNLOCK: |
2191 | 0 | if (datalen != 0) |
2192 | 0 | goto bad; |
2193 | | |
2194 | 0 | if (!(c->flags & CLIENT_SUSPENDED)) |
2195 | 0 | break; |
2196 | 0 | c->flags &= ~CLIENT_SUSPENDED; |
2197 | |
|
2198 | 0 | if (c->fd == -1 || c->session == NULL) /* exited already */ |
2199 | 0 | break; |
2200 | 0 | s = c->session; |
2201 | |
|
2202 | 0 | if (gettimeofday(&c->activity_time, NULL) != 0) |
2203 | 0 | fatal("gettimeofday failed"); |
2204 | | |
2205 | 0 | tty_start_tty(&c->tty); |
2206 | 0 | server_redraw_client(c); |
2207 | 0 | recalculate_sizes(); |
2208 | |
|
2209 | 0 | if (s != NULL) |
2210 | 0 | session_update_activity(s, &c->activity_time); |
2211 | 0 | break; |
2212 | 0 | case MSG_SHELL: |
2213 | 0 | if (datalen != 0) |
2214 | 0 | goto bad; |
2215 | 0 | if (server_client_dispatch_shell(c) != 0) |
2216 | 0 | goto bad; |
2217 | 0 | break; |
2218 | 0 | case MSG_WRITE_READY: |
2219 | 0 | file_write_ready(&c->files, imsg); |
2220 | 0 | break; |
2221 | 0 | case MSG_READ: |
2222 | 0 | file_read_data(&c->files, imsg); |
2223 | 0 | break; |
2224 | 0 | case MSG_READ_DONE: |
2225 | 0 | file_read_done(&c->files, imsg); |
2226 | 0 | break; |
2227 | 0 | } |
2228 | | |
2229 | 0 | return; |
2230 | | |
2231 | 0 | bad: |
2232 | 0 | log_debug("client %p invalid message type %d", c, imsg->hdr.type); |
2233 | 0 | proc_kill_peer(c->peer); |
2234 | 0 | } |
2235 | | |
2236 | | /* Callback when command is not allowed. */ |
2237 | | static enum cmd_retval |
2238 | | server_client_read_only(struct cmdq_item *item, __unused void *data) |
2239 | 0 | { |
2240 | 0 | cmdq_error(item, "client is read-only"); |
2241 | 0 | return (CMD_RETURN_ERROR); |
2242 | 0 | } |
2243 | | |
2244 | | /* Callback for default command. */ |
2245 | | static enum cmd_retval |
2246 | | server_client_default_command(struct cmdq_item *item, __unused void *data) |
2247 | 0 | { |
2248 | 0 | struct client *c = cmdq_get_client(item); |
2249 | 0 | struct cmd_list *cmdlist; |
2250 | 0 | struct cmdq_item *new_item; |
2251 | |
|
2252 | 0 | cmdlist = options_get_command(global_options, "default-client-command"); |
2253 | 0 | if ((c->flags & CLIENT_READONLY) && |
2254 | 0 | !cmd_list_all_have(cmdlist, CMD_READONLY)) |
2255 | 0 | new_item = cmdq_get_callback(server_client_read_only, NULL); |
2256 | 0 | else |
2257 | 0 | new_item = cmdq_get_command(cmdlist, NULL); |
2258 | 0 | cmdq_insert_after(item, new_item); |
2259 | 0 | return (CMD_RETURN_NORMAL); |
2260 | 0 | } |
2261 | | |
2262 | | /* Callback when command is done. */ |
2263 | | static enum cmd_retval |
2264 | | server_client_command_done(struct cmdq_item *item, __unused void *data) |
2265 | 0 | { |
2266 | 0 | struct client *c = cmdq_get_client(item); |
2267 | |
|
2268 | 0 | if (~c->flags & CLIENT_ATTACHED) |
2269 | 0 | c->flags |= CLIENT_EXIT; |
2270 | 0 | else if (~c->flags & CLIENT_EXIT) { |
2271 | 0 | if (c->flags & CLIENT_CONTROL) |
2272 | 0 | control_ready(c); |
2273 | 0 | tty_send_requests(&c->tty); |
2274 | 0 | } |
2275 | 0 | return (CMD_RETURN_NORMAL); |
2276 | 0 | } |
2277 | | |
2278 | | /* Handle command message. */ |
2279 | | static int |
2280 | | server_client_dispatch_command(struct client *c, struct imsg *imsg) |
2281 | 0 | { |
2282 | 0 | struct msg_command data; |
2283 | 0 | char *buf; |
2284 | 0 | size_t len; |
2285 | 0 | int argc = 0; |
2286 | 0 | char **argv, *cause; |
2287 | 0 | struct cmd_parse_result *pr; |
2288 | 0 | struct args_value *values; |
2289 | 0 | struct cmdq_item *new_item; |
2290 | |
|
2291 | 0 | if (c->flags & CLIENT_EXIT) |
2292 | 0 | return (0); |
2293 | | |
2294 | 0 | if (imsg->hdr.len - IMSG_HEADER_SIZE < sizeof data) |
2295 | 0 | return (-1); |
2296 | 0 | memcpy(&data, imsg->data, sizeof data); |
2297 | |
|
2298 | 0 | buf = (char *)imsg->data + sizeof data; |
2299 | 0 | len = imsg->hdr.len - IMSG_HEADER_SIZE - sizeof data; |
2300 | 0 | if (len > 0 && buf[len - 1] != '\0') |
2301 | 0 | return (-1); |
2302 | | |
2303 | 0 | if (cmd_unpack_argv(buf, len, data.argc, &argv) != 0) { |
2304 | 0 | cause = xstrdup("command too long"); |
2305 | 0 | goto error; |
2306 | 0 | } |
2307 | | |
2308 | 0 | argc = data.argc; |
2309 | 0 | if (argc == 0) { |
2310 | 0 | new_item = cmdq_get_callback(server_client_default_command, |
2311 | 0 | NULL); |
2312 | 0 | } else { |
2313 | 0 | values = args_from_vector(argc, argv); |
2314 | 0 | pr = cmd_parse_from_arguments(values, argc, NULL); |
2315 | 0 | switch (pr->status) { |
2316 | 0 | case CMD_PARSE_ERROR: |
2317 | 0 | cause = pr->error; |
2318 | 0 | goto error; |
2319 | 0 | case CMD_PARSE_SUCCESS: |
2320 | 0 | break; |
2321 | 0 | } |
2322 | 0 | args_free_values(values, argc); |
2323 | 0 | free(values); |
2324 | 0 | cmd_free_argv(argc, argv); |
2325 | 0 | if ((c->flags & CLIENT_READONLY) && |
2326 | 0 | !cmd_list_all_have(pr->cmdlist, CMD_READONLY)) { |
2327 | 0 | new_item = cmdq_get_callback(server_client_read_only, |
2328 | 0 | NULL); |
2329 | 0 | } else |
2330 | 0 | new_item = cmdq_get_command(pr->cmdlist, NULL); |
2331 | 0 | cmd_list_free(pr->cmdlist); |
2332 | 0 | } |
2333 | 0 | cmdq_append(c, new_item); |
2334 | 0 | cmdq_append(c, cmdq_get_callback(server_client_command_done, NULL)); |
2335 | |
|
2336 | 0 | return (0); |
2337 | | |
2338 | 0 | error: |
2339 | 0 | cmd_free_argv(argc, argv); |
2340 | |
|
2341 | 0 | cmdq_append(c, cmdq_get_error(cause)); |
2342 | 0 | free(cause); |
2343 | |
|
2344 | 0 | c->flags |= CLIENT_EXIT; |
2345 | 0 | return (0); |
2346 | 0 | } |
2347 | | |
2348 | | /* Handle identify message. */ |
2349 | | static int |
2350 | | server_client_dispatch_identify(struct client *c, struct imsg *imsg) |
2351 | 0 | { |
2352 | 0 | const char *data, *home; |
2353 | 0 | size_t datalen; |
2354 | 0 | int flags, feat; |
2355 | 0 | uint64_t longflags; |
2356 | 0 | char *name; |
2357 | |
|
2358 | 0 | if (c->flags & CLIENT_IDENTIFIED) |
2359 | 0 | return (-1); |
2360 | | |
2361 | 0 | data = imsg->data; |
2362 | 0 | datalen = imsg->hdr.len - IMSG_HEADER_SIZE; |
2363 | |
|
2364 | 0 | switch (imsg->hdr.type) { |
2365 | 0 | case MSG_IDENTIFY_FEATURES: |
2366 | 0 | if (datalen != sizeof feat) |
2367 | 0 | return (-1); |
2368 | 0 | memcpy(&feat, data, sizeof feat); |
2369 | 0 | c->term_features |= feat; |
2370 | 0 | log_debug("client %p IDENTIFY_FEATURES %s", c, |
2371 | 0 | tty_get_features(feat)); |
2372 | 0 | break; |
2373 | 0 | case MSG_IDENTIFY_FLAGS: |
2374 | 0 | if (datalen != sizeof flags) |
2375 | 0 | return (-1); |
2376 | 0 | memcpy(&flags, data, sizeof flags); |
2377 | 0 | c->flags |= flags; |
2378 | 0 | log_debug("client %p IDENTIFY_FLAGS %#x", c, flags); |
2379 | 0 | break; |
2380 | 0 | case MSG_IDENTIFY_LONGFLAGS: |
2381 | 0 | if (datalen != sizeof longflags) |
2382 | 0 | return (-1); |
2383 | 0 | memcpy(&longflags, data, sizeof longflags); |
2384 | 0 | c->flags |= longflags; |
2385 | 0 | log_debug("client %p IDENTIFY_LONGFLAGS %#llx", c, |
2386 | 0 | (unsigned long long)longflags); |
2387 | 0 | break; |
2388 | 0 | case MSG_IDENTIFY_TERM: |
2389 | 0 | if (datalen == 0 || data[datalen - 1] != '\0') |
2390 | 0 | return (-1); |
2391 | 0 | c->term_name = xstrdup(data); |
2392 | 0 | log_debug("client %p IDENTIFY_TERM %s", c, data); |
2393 | 0 | break; |
2394 | 0 | case MSG_IDENTIFY_TERMINFO: |
2395 | 0 | if (datalen == 0 || data[datalen - 1] != '\0') |
2396 | 0 | return (-1); |
2397 | 0 | c->term_caps = xreallocarray(c->term_caps, c->term_ncaps + 1, |
2398 | 0 | sizeof *c->term_caps); |
2399 | 0 | c->term_caps[c->term_ncaps++] = xstrdup(data); |
2400 | 0 | log_debug("client %p IDENTIFY_TERMINFO %s", c, data); |
2401 | 0 | break; |
2402 | 0 | case MSG_IDENTIFY_TTYNAME: |
2403 | 0 | if (datalen == 0 || data[datalen - 1] != '\0') |
2404 | 0 | return (-1); |
2405 | 0 | c->ttyname = xstrdup(data); |
2406 | 0 | log_debug("client %p IDENTIFY_TTYNAME %s", c, data); |
2407 | 0 | break; |
2408 | 0 | case MSG_IDENTIFY_CWD: |
2409 | 0 | if (datalen == 0 || data[datalen - 1] != '\0') |
2410 | 0 | return (-1); |
2411 | 0 | if (access(data, X_OK) == 0) |
2412 | 0 | c->cwd = xstrdup(data); |
2413 | 0 | else if ((home = find_home()) != NULL) |
2414 | 0 | c->cwd = xstrdup(home); |
2415 | 0 | else |
2416 | 0 | c->cwd = xstrdup("/"); |
2417 | 0 | log_debug("client %p IDENTIFY_CWD %s", c, data); |
2418 | 0 | break; |
2419 | 0 | case MSG_IDENTIFY_STDIN: |
2420 | 0 | if (datalen != 0) |
2421 | 0 | return (-1); |
2422 | 0 | c->fd = imsg_get_fd(imsg); |
2423 | 0 | log_debug("client %p IDENTIFY_STDIN %d", c, c->fd); |
2424 | 0 | break; |
2425 | 0 | case MSG_IDENTIFY_STDOUT: |
2426 | 0 | if (datalen != 0) |
2427 | 0 | return (-1); |
2428 | 0 | c->out_fd = imsg_get_fd(imsg); |
2429 | 0 | log_debug("client %p IDENTIFY_STDOUT %d", c, c->out_fd); |
2430 | 0 | break; |
2431 | 0 | case MSG_IDENTIFY_ENVIRON: |
2432 | 0 | if (datalen == 0 || data[datalen - 1] != '\0') |
2433 | 0 | return (-1); |
2434 | 0 | if (strchr(data, '=') != NULL) |
2435 | 0 | environ_put(c->environ, data, 0); |
2436 | 0 | log_debug("client %p IDENTIFY_ENVIRON %s", c, data); |
2437 | 0 | break; |
2438 | 0 | case MSG_IDENTIFY_CLIENTPID: |
2439 | 0 | if (datalen != sizeof c->pid) |
2440 | 0 | return (-1); |
2441 | 0 | memcpy(&c->pid, data, sizeof c->pid); |
2442 | 0 | log_debug("client %p IDENTIFY_CLIENTPID %ld", c, (long)c->pid); |
2443 | 0 | break; |
2444 | 0 | default: |
2445 | 0 | break; |
2446 | 0 | } |
2447 | | |
2448 | 0 | if (imsg->hdr.type != MSG_IDENTIFY_DONE) |
2449 | 0 | return (0); |
2450 | 0 | c->flags |= CLIENT_IDENTIFIED; |
2451 | |
|
2452 | 0 | if (c->term_name == NULL || *c->term_name == '\0') { |
2453 | 0 | free(c->term_name); |
2454 | 0 | c->term_name = xstrdup("unknown"); |
2455 | 0 | } |
2456 | |
|
2457 | 0 | if (c->ttyname != NULL && *c->ttyname != '\0') |
2458 | 0 | name = xstrdup(c->ttyname); |
2459 | 0 | else |
2460 | 0 | xasprintf(&name, "client-%ld", (long)c->pid); |
2461 | 0 | c->name = name; |
2462 | 0 | log_debug("client %p name is %s", c, c->name); |
2463 | |
|
2464 | | #ifdef __CYGWIN__ |
2465 | | c->fd = open(c->ttyname, O_RDWR|O_NOCTTY); |
2466 | | c->out_fd = dup(c->fd); |
2467 | | #endif |
2468 | |
|
2469 | 0 | if (c->flags & CLIENT_CONTROL) |
2470 | 0 | control_start(c); |
2471 | 0 | else if (c->fd != -1) { |
2472 | 0 | if (tty_init(&c->tty, c) != 0) { |
2473 | 0 | close(c->fd); |
2474 | 0 | c->fd = -1; |
2475 | 0 | } else { |
2476 | 0 | tty_resize(&c->tty); |
2477 | 0 | c->flags |= CLIENT_TERMINAL; |
2478 | 0 | } |
2479 | 0 | if (c->out_fd != -1) |
2480 | 0 | close(c->out_fd); |
2481 | 0 | c->out_fd = -1; |
2482 | 0 | } |
2483 | | |
2484 | | /* |
2485 | | * If this is the first client, load configuration files. Any later |
2486 | | * clients are allowed to continue with their command even if the |
2487 | | * config has not been loaded - they might have been run from inside it |
2488 | | */ |
2489 | 0 | if ((~c->flags & CLIENT_EXIT) && |
2490 | 0 | !cfg_finished && |
2491 | 0 | c == TAILQ_FIRST(&clients)) |
2492 | 0 | start_cfg(); |
2493 | |
|
2494 | 0 | return (0); |
2495 | 0 | } |
2496 | | |
2497 | | /* Handle shell message. */ |
2498 | | static int |
2499 | | server_client_dispatch_shell(struct client *c) |
2500 | 0 | { |
2501 | 0 | const char *shell; |
2502 | |
|
2503 | 0 | shell = options_get_string(global_s_options, "default-shell"); |
2504 | 0 | if (!checkshell(shell)) |
2505 | 0 | shell = _PATH_BSHELL; |
2506 | 0 | proc_send(c->peer, MSG_SHELL, -1, shell, strlen(shell) + 1); |
2507 | |
|
2508 | 0 | proc_kill_peer(c->peer); |
2509 | 0 | return (0); |
2510 | 0 | } |
2511 | | |
2512 | | /* Get client working directory. */ |
2513 | | const char * |
2514 | | server_client_get_cwd(struct client *c, struct session *s) |
2515 | 0 | { |
2516 | 0 | const char *home; |
2517 | |
|
2518 | 0 | if (!cfg_finished && cfg_client != NULL) |
2519 | 0 | return (cfg_client->cwd); |
2520 | 0 | if (c != NULL && c->session == NULL && c->cwd != NULL) |
2521 | 0 | return (c->cwd); |
2522 | 0 | if (s != NULL && s->cwd != NULL) |
2523 | 0 | return (s->cwd); |
2524 | 0 | if (c != NULL && (s = c->session) != NULL && s->cwd != NULL) |
2525 | 0 | return (s->cwd); |
2526 | 0 | if ((home = find_home()) != NULL) |
2527 | 0 | return (home); |
2528 | 0 | return ("/"); |
2529 | 0 | } |
2530 | | |
2531 | | /* Get control client flags. */ |
2532 | | static uint64_t |
2533 | | server_client_control_flags(struct client *c, const char *next) |
2534 | 0 | { |
2535 | 0 | if (strcmp(next, "pause-after") == 0) { |
2536 | 0 | c->pause_age = 0; |
2537 | 0 | return (CLIENT_CONTROL_PAUSEAFTER); |
2538 | 0 | } |
2539 | 0 | if (sscanf(next, "pause-after=%u", &c->pause_age) == 1) { |
2540 | 0 | c->pause_age *= 1000; |
2541 | 0 | return (CLIENT_CONTROL_PAUSEAFTER); |
2542 | 0 | } |
2543 | 0 | if (strcmp(next, "no-output") == 0) |
2544 | 0 | return (CLIENT_CONTROL_NOOUTPUT); |
2545 | 0 | if (strcmp(next, "wait-exit") == 0) |
2546 | 0 | return (CLIENT_CONTROL_WAITEXIT); |
2547 | 0 | return (0); |
2548 | 0 | } |
2549 | | |
2550 | | /* Set client flags. */ |
2551 | | void |
2552 | | server_client_set_flags(struct client *c, const char *flags) |
2553 | 0 | { |
2554 | 0 | char *s, *copy, *next; |
2555 | 0 | uint64_t flag; |
2556 | 0 | int not; |
2557 | |
|
2558 | 0 | s = copy = xstrdup(flags); |
2559 | 0 | while ((next = strsep(&s, ",")) != NULL) { |
2560 | 0 | not = (*next == '!'); |
2561 | 0 | if (not) |
2562 | 0 | next++; |
2563 | |
|
2564 | 0 | if (c->flags & CLIENT_CONTROL) |
2565 | 0 | flag = server_client_control_flags(c, next); |
2566 | 0 | else |
2567 | 0 | flag = 0; |
2568 | 0 | if (strcmp(next, "read-only") == 0) |
2569 | 0 | flag = CLIENT_READONLY; |
2570 | 0 | else if (strcmp(next, "ignore-size") == 0) |
2571 | 0 | flag = CLIENT_IGNORESIZE; |
2572 | 0 | else if (strcmp(next, "active-pane") == 0) |
2573 | 0 | flag = CLIENT_ACTIVEPANE; |
2574 | 0 | else if (strcmp(next, "no-detach-on-destroy") == 0) |
2575 | 0 | flag = CLIENT_NO_DETACH_ON_DESTROY; |
2576 | 0 | if (flag == 0) |
2577 | 0 | continue; |
2578 | | |
2579 | 0 | log_debug("client %s set flag %s", c->name, next); |
2580 | 0 | if (not) { |
2581 | 0 | if (c->flags & CLIENT_READONLY) |
2582 | 0 | flag &= ~CLIENT_READONLY; |
2583 | 0 | c->flags &= ~flag; |
2584 | 0 | } else |
2585 | 0 | c->flags |= flag; |
2586 | 0 | if (flag == CLIENT_CONTROL_NOOUTPUT) |
2587 | 0 | control_reset_offsets(c); |
2588 | 0 | } |
2589 | 0 | free(copy); |
2590 | 0 | proc_send(c->peer, MSG_FLAGS, -1, &c->flags, sizeof c->flags); |
2591 | 0 | } |
2592 | | |
2593 | | /* Get client flags. This is only flags useful to show to users. */ |
2594 | | const char * |
2595 | | server_client_get_flags(struct client *c) |
2596 | 0 | { |
2597 | 0 | static char s[256]; |
2598 | 0 | char tmp[32]; |
2599 | |
|
2600 | 0 | *s = '\0'; |
2601 | 0 | if (c->flags & CLIENT_ATTACHED) |
2602 | 0 | strlcat(s, "attached,", sizeof s); |
2603 | 0 | if (c->flags & CLIENT_FOCUSED) |
2604 | 0 | strlcat(s, "focused,", sizeof s); |
2605 | 0 | if (c->flags & CLIENT_CONTROL) |
2606 | 0 | strlcat(s, "control-mode,", sizeof s); |
2607 | 0 | if (c->flags & CLIENT_IGNORESIZE) |
2608 | 0 | strlcat(s, "ignore-size,", sizeof s); |
2609 | 0 | if (c->flags & CLIENT_NO_DETACH_ON_DESTROY) |
2610 | 0 | strlcat(s, "no-detach-on-destroy,", sizeof s); |
2611 | 0 | if (c->flags & CLIENT_CONTROL_NOOUTPUT) |
2612 | 0 | strlcat(s, "no-output,", sizeof s); |
2613 | 0 | if (c->flags & CLIENT_CONTROL_WAITEXIT) |
2614 | 0 | strlcat(s, "wait-exit,", sizeof s); |
2615 | 0 | if (c->flags & CLIENT_CONTROL_PAUSEAFTER) { |
2616 | 0 | xsnprintf(tmp, sizeof tmp, "pause-after=%u,", |
2617 | 0 | c->pause_age / 1000); |
2618 | 0 | strlcat(s, tmp, sizeof s); |
2619 | 0 | } |
2620 | 0 | if (c->flags & CLIENT_READONLY) |
2621 | 0 | strlcat(s, "read-only,", sizeof s); |
2622 | 0 | if (c->flags & CLIENT_ACTIVEPANE) |
2623 | 0 | strlcat(s, "active-pane,", sizeof s); |
2624 | 0 | if (c->flags & CLIENT_SUSPENDED) |
2625 | 0 | strlcat(s, "suspended,", sizeof s); |
2626 | 0 | if (c->flags & CLIENT_UTF8) |
2627 | 0 | strlcat(s, "UTF-8,", sizeof s); |
2628 | 0 | if (*s != '\0') |
2629 | 0 | s[strlen(s) - 1] = '\0'; |
2630 | 0 | return (s); |
2631 | 0 | } |
2632 | | |
2633 | | /* Get client window. */ |
2634 | | struct client_window * |
2635 | | server_client_get_client_window(struct client *c, u_int id) |
2636 | 0 | { |
2637 | 0 | struct client_window cw = { .window = id }; |
2638 | |
|
2639 | 0 | return (RB_FIND(client_windows, &c->windows, &cw)); |
2640 | 0 | } |
2641 | | |
2642 | | /* Add client window. */ |
2643 | | struct client_window * |
2644 | | server_client_add_client_window(struct client *c, u_int id) |
2645 | 0 | { |
2646 | 0 | struct client_window *cw; |
2647 | |
|
2648 | 0 | cw = server_client_get_client_window(c, id); |
2649 | 0 | if (cw == NULL) { |
2650 | 0 | cw = xcalloc(1, sizeof *cw); |
2651 | 0 | cw->window = id; |
2652 | 0 | RB_INSERT(client_windows, &c->windows, cw); |
2653 | 0 | } |
2654 | 0 | return (cw); |
2655 | 0 | } |
2656 | | |
2657 | | /* Get client active pane. */ |
2658 | | struct window_pane * |
2659 | | server_client_get_pane(struct client *c) |
2660 | 0 | { |
2661 | 0 | struct session *s = c->session; |
2662 | 0 | struct client_window *cw; |
2663 | |
|
2664 | 0 | if (s == NULL) |
2665 | 0 | return (NULL); |
2666 | | |
2667 | 0 | if (~c->flags & CLIENT_ACTIVEPANE) |
2668 | 0 | return (s->curw->window->active); |
2669 | 0 | cw = server_client_get_client_window(c, s->curw->window->id); |
2670 | 0 | if (cw == NULL) |
2671 | 0 | return (s->curw->window->active); |
2672 | 0 | return (cw->pane); |
2673 | 0 | } |
2674 | | |
2675 | | /* Set client active pane. */ |
2676 | | void |
2677 | | server_client_set_pane(struct client *c, struct window_pane *wp) |
2678 | 0 | { |
2679 | 0 | struct session *s = c->session; |
2680 | 0 | struct client_window *cw; |
2681 | |
|
2682 | 0 | if (s == NULL) |
2683 | 0 | return; |
2684 | | |
2685 | 0 | cw = server_client_add_client_window(c, s->curw->window->id); |
2686 | 0 | cw->pane = wp; |
2687 | 0 | log_debug("%s pane now %%%u", c->name, wp->id); |
2688 | 0 | } |
2689 | | |
2690 | | /* Remove pane from client lists. */ |
2691 | | void |
2692 | | server_client_remove_pane(struct window_pane *wp) |
2693 | 0 | { |
2694 | 0 | struct client *c; |
2695 | 0 | struct window *w = wp->window; |
2696 | 0 | struct client_window *cw; |
2697 | |
|
2698 | 0 | TAILQ_FOREACH(c, &clients, entry) { |
2699 | 0 | cw = server_client_get_client_window(c, w->id); |
2700 | 0 | if (cw != NULL && cw->pane == wp) { |
2701 | 0 | RB_REMOVE(client_windows, &c->windows, cw); |
2702 | 0 | free(cw); |
2703 | 0 | } |
2704 | 0 | } |
2705 | 0 | } |
2706 | | |
2707 | | /* Print to a client. */ |
2708 | | void |
2709 | | server_client_print(struct client *c, int parse, struct evbuffer *evb) |
2710 | 0 | { |
2711 | 0 | void *data = EVBUFFER_DATA(evb); |
2712 | 0 | size_t size = EVBUFFER_LENGTH(evb); |
2713 | 0 | struct window_pane *wp; |
2714 | 0 | struct window_mode_entry *wme; |
2715 | 0 | char *sanitized, *msg, *line, empty = '\0'; |
2716 | |
|
2717 | 0 | if (!parse) { |
2718 | 0 | utf8_stravisx(&msg, data, size, |
2719 | 0 | VIS_OCTAL|VIS_CSTYLE|VIS_NOSLASH); |
2720 | 0 | } else { |
2721 | 0 | if (size == 0) |
2722 | 0 | msg = ∅ |
2723 | 0 | else { |
2724 | 0 | msg = EVBUFFER_DATA(evb); |
2725 | 0 | if (msg[size - 1] != '\0') |
2726 | 0 | evbuffer_add(evb, "", 1); |
2727 | 0 | } |
2728 | 0 | } |
2729 | 0 | log_debug("%s: %s", __func__, msg); |
2730 | |
|
2731 | 0 | if (c == NULL) |
2732 | 0 | goto out; |
2733 | | |
2734 | 0 | if (c->session == NULL || (c->flags & CLIENT_CONTROL)) { |
2735 | 0 | if (~c->flags & CLIENT_UTF8) { |
2736 | 0 | sanitized = utf8_sanitize(msg); |
2737 | 0 | if (c->flags & CLIENT_CONTROL) |
2738 | 0 | control_write(c, "%s", sanitized); |
2739 | 0 | else |
2740 | 0 | file_print(c, "%s\n", sanitized); |
2741 | 0 | free(sanitized); |
2742 | 0 | } else { |
2743 | 0 | if (c->flags & CLIENT_CONTROL) |
2744 | 0 | control_write(c, "%s", msg); |
2745 | 0 | else |
2746 | 0 | file_print(c, "%s\n", msg); |
2747 | 0 | } |
2748 | 0 | goto out; |
2749 | 0 | } |
2750 | | |
2751 | 0 | wp = server_client_get_pane(c); |
2752 | 0 | wme = TAILQ_FIRST(&wp->modes); |
2753 | 0 | if (wme == NULL || wme->mode != &window_view_mode) |
2754 | 0 | window_pane_set_mode(wp, NULL, &window_view_mode, NULL, NULL); |
2755 | 0 | if (parse) { |
2756 | 0 | do { |
2757 | 0 | line = evbuffer_readln(evb, NULL, EVBUFFER_EOL_LF); |
2758 | 0 | if (line != NULL) { |
2759 | 0 | window_copy_add(wp, 1, "%s", line); |
2760 | 0 | free(line); |
2761 | 0 | } |
2762 | 0 | } while (line != NULL); |
2763 | |
|
2764 | 0 | size = EVBUFFER_LENGTH(evb); |
2765 | 0 | if (size != 0) { |
2766 | 0 | line = EVBUFFER_DATA(evb); |
2767 | 0 | window_copy_add(wp, 1, "%.*s", (int)size, line); |
2768 | 0 | } |
2769 | 0 | } else |
2770 | 0 | window_copy_add(wp, 0, "%s", msg); |
2771 | |
|
2772 | 0 | out: |
2773 | 0 | if (!parse) |
2774 | 0 | free(msg); |
2775 | 0 | } |
2776 | | |
2777 | | static void |
2778 | | server_client_report_theme(struct client *c, enum client_theme theme) |
2779 | 0 | { |
2780 | 0 | if (theme == THEME_LIGHT) { |
2781 | 0 | c->theme = THEME_LIGHT; |
2782 | 0 | notify_client("client-light-theme", c); |
2783 | 0 | } else { |
2784 | 0 | c->theme = THEME_DARK; |
2785 | 0 | notify_client("client-dark-theme", c); |
2786 | 0 | } |
2787 | | |
2788 | | /* |
2789 | | * Request foreground and background colour again. Don't forward 2031 to |
2790 | | * panes until a response is received. |
2791 | | */ |
2792 | 0 | tty_repeat_requests(&c->tty, 1); |
2793 | 0 | } |