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