Annotation of src/usr.bin/tmux/server-client.c, Revision 1.380
1.380 ! nicm 1: /* $OpenBSD: server-client.c,v 1.379 2021/08/13 06:52:51 nicm Exp $ */
1.1 nicm 2:
3: /*
1.181 nicm 4: * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 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>
1.92 nicm 20: #include <sys/ioctl.h>
1.162 nicm 21: #include <sys/uio.h>
1.1 nicm 22:
1.114 benno 23: #include <errno.h>
1.12 nicm 24: #include <event.h>
1.1 nicm 25: #include <fcntl.h>
1.162 nicm 26: #include <imsg.h>
1.98 nicm 27: #include <paths.h>
28: #include <stdlib.h>
1.1 nicm 29: #include <string.h>
1.4 nicm 30: #include <time.h>
1.1 nicm 31: #include <unistd.h>
32:
33: #include "tmux.h"
34:
1.190 nicm 35: static void server_client_free(int, short, void *);
1.344 nicm 36: static void server_client_check_pane_resize(struct window_pane *);
1.345 nicm 37: static void server_client_check_pane_buffer(struct window_pane *);
1.344 nicm 38: static void server_client_check_window_resize(struct window *);
1.276 nicm 39: static key_code server_client_check_mouse(struct client *, struct key_event *);
1.190 nicm 40: static void server_client_repeat_timer(int, short, void *);
1.192 nicm 41: static void server_client_click_timer(int, short, void *);
1.190 nicm 42: static void server_client_check_exit(struct client *);
43: static void server_client_check_redraw(struct client *);
1.366 nicm 44: static void server_client_check_modes(struct client *);
1.190 nicm 45: static void server_client_set_title(struct client *);
46: static void server_client_reset_state(struct client *);
47: static int server_client_assume_paste(struct session *);
1.373 nicm 48: static void server_client_update_latest(struct client *);
1.190 nicm 49:
50: static void server_client_dispatch(struct imsg *, void *);
51: static void server_client_dispatch_command(struct client *, struct imsg *);
52: static void server_client_dispatch_identify(struct client *, struct imsg *);
53: static void server_client_dispatch_shell(struct client *);
1.233 nicm 54:
1.341 nicm 55: /* Compare client windows. */
56: static int
57: server_client_window_cmp(struct client_window *cw1,
58: struct client_window *cw2)
59: {
60: if (cw1->window < cw2->window)
61: return (-1);
62: if (cw1->window > cw2->window)
63: return (1);
64: return (0);
65: }
66: RB_GENERATE(client_windows, client_window, entry, server_client_window_cmp);
67:
1.233 nicm 68: /* Number of attached clients. */
69: u_int
70: server_client_how_many(void)
71: {
72: struct client *c;
73: u_int n;
74:
75: n = 0;
76: TAILQ_FOREACH(c, &clients, entry) {
1.352 nicm 77: if (c->session != NULL && (~c->flags & CLIENT_UNATTACHEDFLAGS))
1.233 nicm 78: n++;
79: }
80: return (n);
81: }
1.140 nicm 82:
1.281 nicm 83: /* Overlay timer callback. */
1.214 nicm 84: static void
1.281 nicm 85: server_client_overlay_timer(__unused int fd, __unused short events, void *data)
1.214 nicm 86: {
1.281 nicm 87: server_client_clear_overlay(data);
1.214 nicm 88: }
89:
1.281 nicm 90: /* Set an overlay on client. */
1.214 nicm 91: void
1.309 nicm 92: server_client_set_overlay(struct client *c, u_int delay,
93: overlay_check_cb checkcb, overlay_mode_cb modecb,
94: overlay_draw_cb drawcb, overlay_key_cb keycb, overlay_free_cb freecb,
1.376 nicm 95: overlay_resize_cb resizecb, void *data)
1.214 nicm 96: {
97: struct timeval tv;
98:
1.282 nicm 99: if (c->overlay_draw != NULL)
100: server_client_clear_overlay(c);
101:
1.214 nicm 102: tv.tv_sec = delay / 1000;
103: tv.tv_usec = (delay % 1000) * 1000L;
104:
1.281 nicm 105: if (event_initialized(&c->overlay_timer))
106: evtimer_del(&c->overlay_timer);
107: evtimer_set(&c->overlay_timer, server_client_overlay_timer, c);
1.242 nicm 108: if (delay != 0)
1.281 nicm 109: evtimer_add(&c->overlay_timer, &tv);
110:
1.309 nicm 111: c->overlay_check = checkcb;
112: c->overlay_mode = modecb;
1.281 nicm 113: c->overlay_draw = drawcb;
114: c->overlay_key = keycb;
115: c->overlay_free = freecb;
1.376 nicm 116: c->overlay_resize = resizecb;
1.281 nicm 117: c->overlay_data = data;
1.214 nicm 118:
1.378 nicm 119: if (c->overlay_check == NULL)
120: c->tty.flags |= TTY_FREEZE;
1.309 nicm 121: if (c->overlay_mode == NULL)
122: c->tty.flags |= TTY_NOCURSOR;
1.214 nicm 123: server_redraw_client(c);
124: }
125:
1.281 nicm 126: /* Clear overlay mode on client. */
1.309 nicm 127: void
1.281 nicm 128: server_client_clear_overlay(struct client *c)
1.214 nicm 129: {
1.281 nicm 130: if (c->overlay_draw == NULL)
1.214 nicm 131: return;
132:
1.281 nicm 133: if (event_initialized(&c->overlay_timer))
134: evtimer_del(&c->overlay_timer);
135:
136: if (c->overlay_free != NULL)
1.380 ! nicm 137: c->overlay_free(c, c->overlay_data);
1.281 nicm 138:
1.309 nicm 139: c->overlay_check = NULL;
140: c->overlay_mode = NULL;
1.281 nicm 141: c->overlay_draw = NULL;
142: c->overlay_key = NULL;
1.309 nicm 143: c->overlay_free = NULL;
144: c->overlay_data = NULL;
1.214 nicm 145:
146: c->tty.flags &= ~(TTY_FREEZE|TTY_NOCURSOR);
147: server_redraw_client(c);
148: }
149:
1.140 nicm 150: /* Check if this client is inside this server. */
151: int
152: server_client_check_nested(struct client *c)
153: {
154: struct environ_entry *envent;
155: struct window_pane *wp;
156:
1.164 nicm 157: envent = environ_find(c->environ, "TMUX");
1.140 nicm 158: if (envent == NULL || *envent->value == '\0')
159: return (0);
160:
161: RB_FOREACH(wp, window_pane_tree, &all_window_panes) {
1.216 nicm 162: if (strcmp(wp->tty, c->ttyname) == 0)
1.140 nicm 163: return (1);
164: }
165: return (0);
166: }
1.1 nicm 167:
1.132 nicm 168: /* Set client key table. */
169: void
1.178 nicm 170: server_client_set_key_table(struct client *c, const char *name)
1.132 nicm 171: {
1.178 nicm 172: if (name == NULL)
173: name = server_client_get_key_table(c);
174:
1.132 nicm 175: key_bindings_unref_table(c->keytable);
176: c->keytable = key_bindings_get_table(name, 1);
177: c->keytable->references++;
178: }
179:
1.178 nicm 180: /* Get default key table. */
181: const char *
182: server_client_get_key_table(struct client *c)
183: {
184: struct session *s = c->session;
185: const char *name;
186:
187: if (s == NULL)
188: return ("root");
189:
190: name = options_get_string(s->options, "key-table");
191: if (*name == '\0')
192: return ("root");
193: return (name);
194: }
195:
1.223 nicm 196: /* Is this table the default key table? */
197: static int
198: server_client_is_default_key_table(struct client *c, struct key_table *table)
1.191 nicm 199: {
1.223 nicm 200: return (strcmp(table->name, server_client_get_key_table(c)) == 0);
1.191 nicm 201: }
202:
1.1 nicm 203: /* Create a new client. */
1.246 nicm 204: struct client *
1.1 nicm 205: server_client_create(int fd)
206: {
207: struct client *c;
208:
1.49 nicm 209: setblocking(fd, 0);
1.1 nicm 210:
211: c = xcalloc(1, sizeof *c);
1.141 nicm 212: c->references = 1;
1.162 nicm 213: c->peer = proc_add_peer(server_proc, fd, server_client_dispatch, c);
1.25 nicm 214:
1.10 nicm 215: if (gettimeofday(&c->creation_time, NULL) != 0)
1.1 nicm 216: fatal("gettimeofday failed");
1.11 nicm 217: memcpy(&c->activity_time, &c->creation_time, sizeof c->activity_time);
1.111 nicm 218:
1.164 nicm 219: c->environ = environ_create();
1.1 nicm 220:
1.146 nicm 221: c->fd = -1;
1.351 nicm 222: c->out_fd = -1;
1.145 nicm 223:
1.316 nicm 224: c->queue = cmdq_new();
1.341 nicm 225: RB_INIT(&c->windows);
1.345 nicm 226: RB_INIT(&c->files);
1.94 nicm 227:
1.1 nicm 228: c->tty.sx = 80;
229: c->tty.sy = 24;
230:
1.271 nicm 231: status_init(c);
1.93 nicm 232: c->flags |= CLIENT_FOCUSED;
233:
1.132 nicm 234: c->keytable = key_bindings_get_table("root", 1);
235: c->keytable->references++;
236:
1.17 nicm 237: evtimer_set(&c->repeat_timer, server_client_repeat_timer, c);
1.192 nicm 238: evtimer_set(&c->click_timer, server_client_click_timer, c);
1.17 nicm 239:
1.135 nicm 240: TAILQ_INSERT_TAIL(&clients, c, entry);
1.157 nicm 241: log_debug("new client %p", c);
1.246 nicm 242: return (c);
1.72 nicm 243: }
244:
245: /* Open client terminal if needed. */
246: int
1.119 nicm 247: server_client_open(struct client *c, char **cause)
1.72 nicm 248: {
1.339 nicm 249: const char *ttynam = _PATH_TTY;
250:
1.75 nicm 251: if (c->flags & CLIENT_CONTROL)
252: return (0);
1.120 nicm 253:
1.339 nicm 254: if (strcmp(c->ttyname, ttynam) == 0||
255: ((isatty(STDIN_FILENO) &&
256: (ttynam = ttyname(STDIN_FILENO)) != NULL &&
257: strcmp(c->ttyname, ttynam) == 0) ||
258: (isatty(STDOUT_FILENO) &&
259: (ttynam = ttyname(STDOUT_FILENO)) != NULL &&
260: strcmp(c->ttyname, ttynam) == 0) ||
261: (isatty(STDERR_FILENO) &&
262: (ttynam = ttyname(STDERR_FILENO)) != NULL &&
263: strcmp(c->ttyname, ttynam) == 0))) {
264: xasprintf(cause, "can't use %s", c->ttyname);
1.120 nicm 265: return (-1);
266: }
1.75 nicm 267:
1.72 nicm 268: if (!(c->flags & CLIENT_TERMINAL)) {
1.116 nicm 269: *cause = xstrdup("not a terminal");
1.72 nicm 270: return (-1);
271: }
272:
1.119 nicm 273: if (tty_open(&c->tty, cause) != 0)
1.72 nicm 274: return (-1);
275:
276: return (0);
1.1 nicm 277: }
278:
1.373 nicm 279: /* Lost an attached client. */
280: static void
281: server_client_attached_lost(struct client *c)
282: {
283: struct session *s = c->session;
284: struct window *w;
285: struct client *loop;
286: struct client *found;
287:
288: log_debug("lost attached client %p", c);
289:
290: /*
291: * By this point the session in the client has been cleared so walk all
292: * windows to find any with this client as the latest.
293: */
294: RB_FOREACH(w, windows, &windows) {
295: if (w->latest != c)
296: continue;
297:
298: found = NULL;
299: TAILQ_FOREACH(loop, &clients, entry) {
300: s = loop->session;
301: if (loop == c || s == NULL || s->curw->window != w)
302: continue;
1.379 nicm 303: if (found == NULL || timercmp(&loop->activity_time,
304: &found->activity_time, >))
1.373 nicm 305: found = loop;
306: }
307: if (found != NULL)
308: server_client_update_latest(found);
309: }
310: }
311:
1.379 nicm 312:
313: /* Set client session. */
314: void
315: server_client_set_session(struct client *c, struct session *s)
316: {
317: struct session *old = c->session;
318:
319: if (s != NULL && c->session != NULL && c->session != s)
320: c->last_session = c->session;
321: else if (s == NULL)
322: c->last_session = NULL;
323: c->session = s;
324: c->flags |= CLIENT_FOCUSED;
325: recalculate_sizes();
326:
327: if (old != NULL && old->curw != NULL)
328: window_update_focus(old->curw->window);
329: if (s != NULL) {
330: window_update_focus(s->curw->window);
331: session_update_activity(s, NULL);
332: gettimeofday(&s->last_attached_time, NULL);
333: s->curw->flags &= ~WINLINK_ALERTFLAGS;
334: s->curw->window->latest = c;
335: alerts_check_session(s);
336: tty_update_client_offset(c);
337: status_timer_start(c);
338: notify_client("client-session-changed", c);
339: server_redraw_client(c);
340: }
341:
342: server_check_unattached();
343: server_update_socket();
344: }
345:
1.1 nicm 346: /* Lost a client. */
347: void
348: server_client_lost(struct client *c)
349: {
1.340 nicm 350: struct client_file *cf, *cf1;
1.341 nicm 351: struct client_window *cw, *cw1;
1.1 nicm 352:
1.141 nicm 353: c->flags |= CLIENT_DEAD;
354:
1.281 nicm 355: server_client_clear_overlay(c);
1.141 nicm 356: status_prompt_clear(c);
357: status_message_clear(c);
358:
1.340 nicm 359: RB_FOREACH_SAFE(cf, client_files, &c->files, cf1) {
1.300 nicm 360: cf->error = EINTR;
361: file_fire_done(cf);
362: }
1.341 nicm 363: RB_FOREACH_SAFE(cw, client_windows, &c->windows, cw1) {
364: RB_REMOVE(client_windows, &c->windows, cw);
365: free(cw);
366: }
1.141 nicm 367:
1.135 nicm 368: TAILQ_REMOVE(&clients, c, entry);
1.157 nicm 369: log_debug("lost client %p", c);
1.1 nicm 370:
1.373 nicm 371: if (c->flags & CLIENT_ATTACHED) {
372: server_client_attached_lost(c);
1.371 nicm 373: notify_client("client-detached", c);
1.373 nicm 374: }
1.371 nicm 375:
1.349 nicm 376: if (c->flags & CLIENT_CONTROL)
377: control_stop(c);
1.1 nicm 378: if (c->flags & CLIENT_TERMINAL)
379: tty_free(&c->tty);
1.109 nicm 380: free(c->ttyname);
1.333 nicm 381:
1.329 nicm 382: free(c->term_name);
1.333 nicm 383: free(c->term_type);
1.370 nicm 384: tty_term_free_list(c->term_caps, c->term_ncaps);
1.1 nicm 385:
1.270 nicm 386: status_free(c);
1.1 nicm 387:
1.77 nicm 388: free(c->title);
1.165 nicm 389: free((void *)c->cwd);
1.1 nicm 390:
1.17 nicm 391: evtimer_del(&c->repeat_timer);
1.192 nicm 392: evtimer_del(&c->click_timer);
1.17 nicm 393:
1.132 nicm 394: key_bindings_unref_table(c->keytable);
395:
1.77 nicm 396: free(c->message_string);
1.116 nicm 397: if (event_initialized(&c->message_timer))
1.69 nicm 398: evtimer_del(&c->message_timer);
1.1 nicm 399:
1.259 nicm 400: free(c->prompt_saved);
1.77 nicm 401: free(c->prompt_string);
402: free(c->prompt_buffer);
1.61 nicm 403:
1.228 nicm 404: format_lost_client(c);
1.164 nicm 405: environ_free(c->environ);
1.1 nicm 406:
1.162 nicm 407: proc_remove_peer(c->peer);
408: c->peer = NULL;
1.13 nicm 409:
1.351 nicm 410: if (c->out_fd != -1)
411: close(c->out_fd);
1.348 nicm 412: if (c->fd != -1) {
413: close(c->fd);
414: c->fd = -1;
415: }
1.142 nicm 416: server_client_unref(c);
1.71 nicm 417:
418: server_add_accept(0); /* may be more file descriptors now */
1.1 nicm 419:
420: recalculate_sizes();
1.41 nicm 421: server_check_unattached();
1.19 nicm 422: server_update_socket();
1.141 nicm 423: }
424:
425: /* Remove reference from a client. */
426: void
1.142 nicm 427: server_client_unref(struct client *c)
1.141 nicm 428: {
1.157 nicm 429: log_debug("unref client %p (%d references)", c, c->references);
1.141 nicm 430:
431: c->references--;
432: if (c->references == 0)
433: event_once(-1, EV_TIMEOUT, server_client_free, c, NULL);
434: }
435:
436: /* Free dead client. */
1.190 nicm 437: static void
1.170 nicm 438: server_client_free(__unused int fd, __unused short events, void *arg)
1.141 nicm 439: {
440: struct client *c = arg;
441:
1.157 nicm 442: log_debug("free client %p (%d references)", c, c->references);
1.141 nicm 443:
1.316 nicm 444: cmdq_free(c->queue);
1.194 nicm 445:
1.216 nicm 446: if (c->references == 0) {
447: free((void *)c->name);
1.141 nicm 448: free(c);
1.216 nicm 449: }
1.9 nicm 450: }
451:
1.220 nicm 452: /* Suspend a client. */
453: void
454: server_client_suspend(struct client *c)
455: {
1.232 nicm 456: struct session *s = c->session;
1.220 nicm 457:
1.352 nicm 458: if (s == NULL || (c->flags & CLIENT_UNATTACHEDFLAGS))
1.220 nicm 459: return;
460:
461: tty_stop_tty(&c->tty);
462: c->flags |= CLIENT_SUSPENDED;
463: proc_send(c->peer, MSG_SUSPEND, -1, NULL, 0);
464: }
465:
1.174 nicm 466: /* Detach a client. */
467: void
468: server_client_detach(struct client *c, enum msgtype msgtype)
469: {
1.232 nicm 470: struct session *s = c->session;
1.174 nicm 471:
1.352 nicm 472: if (s == NULL || (c->flags & CLIENT_UNATTACHEDFLAGS))
1.174 nicm 473: return;
474:
1.352 nicm 475: c->flags |= CLIENT_EXIT;
476:
477: c->exit_type = CLIENT_EXIT_DETACH;
478: c->exit_msgtype = msgtype;
479: c->exit_session = xstrdup(s->name);
1.207 nicm 480: }
481:
1.208 nicm 482: /* Execute command to replace a client. */
1.207 nicm 483: void
484: server_client_exec(struct client *c, const char *cmd)
485: {
486: struct session *s = c->session;
1.208 nicm 487: char *msg;
488: const char *shell;
1.207 nicm 489: size_t cmdsize, shellsize;
490:
491: if (*cmd == '\0')
492: return;
493: cmdsize = strlen(cmd) + 1;
494:
495: if (s != NULL)
496: shell = options_get_string(s->options, "default-shell");
497: else
498: shell = options_get_string(global_s_options, "default-shell");
1.308 nicm 499: if (!checkshell(shell))
500: shell = _PATH_BSHELL;
1.207 nicm 501: shellsize = strlen(shell) + 1;
502:
503: msg = xmalloc(cmdsize + shellsize);
504: memcpy(msg, cmd, cmdsize);
505: memcpy(msg + cmdsize, shell, shellsize);
506:
507: proc_send(c->peer, MSG_EXEC, -1, msg, cmdsize + shellsize);
508: free(msg);
1.174 nicm 509: }
510:
1.66 nicm 511: /* Check for mouse keys. */
1.190 nicm 512: static key_code
1.276 nicm 513: server_client_check_mouse(struct client *c, struct key_event *event)
1.66 nicm 514: {
1.276 nicm 515: struct mouse_event *m = &event->m;
1.192 nicm 516: struct session *s = c->session;
1.272 nicm 517: struct winlink *wl;
1.192 nicm 518: struct window_pane *wp;
1.261 nicm 519: u_int x, y, b, sx, sy, px, py;
1.307 nicm 520: int ignore = 0;
1.192 nicm 521: key_code key;
522: struct timeval tv;
1.272 nicm 523: struct style_range *sr;
1.283 nicm 524: enum { NOTYPE,
525: MOVE,
526: DOWN,
527: UP,
528: DRAG,
529: WHEEL,
1.311 nicm 530: SECOND,
1.283 nicm 531: DOUBLE,
532: TRIPLE } type = NOTYPE;
1.276 nicm 533: enum { NOWHERE,
534: PANE,
535: STATUS,
536: STATUS_LEFT,
537: STATUS_RIGHT,
538: STATUS_DEFAULT,
1.283 nicm 539: BORDER } where = NOWHERE;
1.131 nicm 540:
1.261 nicm 541: log_debug("%s mouse %02x at %u,%u (last %u,%u) (%d)", c->name, m->b,
542: m->x, m->y, m->lx, m->ly, c->tty.mouse_drag_flag);
1.131 nicm 543:
544: /* What type of event is this? */
1.306 nicm 545: if (event->key == KEYC_DOUBLECLICK) {
546: type = DOUBLE;
547: x = m->x, y = m->y, b = m->b;
1.307 nicm 548: ignore = 1;
1.306 nicm 549: log_debug("double-click at %u,%u", x, y);
550: } else if ((m->sgr_type != ' ' &&
1.209 nicm 551: MOUSE_DRAG(m->sgr_b) &&
552: MOUSE_BUTTONS(m->sgr_b) == 3) ||
553: (m->sgr_type == ' ' &&
554: MOUSE_DRAG(m->b) &&
555: MOUSE_BUTTONS(m->b) == 3 &&
556: MOUSE_BUTTONS(m->lb) == 3)) {
557: type = MOVE;
558: x = m->x, y = m->y, b = 0;
559: log_debug("move at %u,%u", x, y);
560: } else if (MOUSE_DRAG(m->b)) {
1.131 nicm 561: type = DRAG;
562: if (c->tty.mouse_drag_flag) {
563: x = m->x, y = m->y, b = m->b;
1.278 nicm 564: if (x == m->lx && y == m->ly)
565: return (KEYC_UNKNOWN);
1.131 nicm 566: log_debug("drag update at %u,%u", x, y);
567: } else {
1.277 nicm 568: x = m->lx, y = m->ly, b = m->lb;
1.131 nicm 569: log_debug("drag start at %u,%u", x, y);
570: }
571: } else if (MOUSE_WHEEL(m->b)) {
572: type = WHEEL;
573: x = m->x, y = m->y, b = m->b;
574: log_debug("wheel at %u,%u", x, y);
1.200 nicm 575: } else if (MOUSE_RELEASE(m->b)) {
1.131 nicm 576: type = UP;
577: x = m->x, y = m->y, b = m->lb;
578: log_debug("up at %u,%u", x, y);
579: } else {
1.192 nicm 580: if (c->flags & CLIENT_DOUBLECLICK) {
581: evtimer_del(&c->click_timer);
582: c->flags &= ~CLIENT_DOUBLECLICK;
583: if (m->b == c->click_button) {
1.311 nicm 584: type = SECOND;
585: x = m->x, y = m->y, b = m->b;
586: log_debug("second-click at %u,%u", x, y);
1.306 nicm 587: c->flags |= CLIENT_TRIPLECLICK;
1.192 nicm 588: }
589: } else if (c->flags & CLIENT_TRIPLECLICK) {
590: evtimer_del(&c->click_timer);
591: c->flags &= ~CLIENT_TRIPLECLICK;
592: if (m->b == c->click_button) {
593: type = TRIPLE;
594: x = m->x, y = m->y, b = m->b;
595: log_debug("triple-click at %u,%u", x, y);
596: goto have_event;
597: }
1.311 nicm 598: } else {
599: type = DOWN;
600: x = m->x, y = m->y, b = m->b;
601: log_debug("down at %u,%u", x, y);
1.307 nicm 602: c->flags |= CLIENT_DOUBLECLICK;
1.311 nicm 603: }
1.192 nicm 604:
605: if (KEYC_CLICK_TIMEOUT != 0) {
1.306 nicm 606: memcpy(&c->click_event, m, sizeof c->click_event);
1.192 nicm 607: c->click_button = m->b;
608:
1.312 nicm 609: log_debug("click timer started");
1.192 nicm 610: tv.tv_sec = KEYC_CLICK_TIMEOUT / 1000;
611: tv.tv_usec = (KEYC_CLICK_TIMEOUT % 1000) * 1000L;
612: evtimer_del(&c->click_timer);
613: evtimer_add(&c->click_timer, &tv);
614: }
1.131 nicm 615: }
1.192 nicm 616:
617: have_event:
1.131 nicm 618: if (type == NOTYPE)
1.177 nicm 619: return (KEYC_UNKNOWN);
1.131 nicm 620:
1.258 nicm 621: /* Save the session. */
1.131 nicm 622: m->s = s->id;
1.258 nicm 623: m->w = -1;
1.307 nicm 624: m->ignore = ignore;
1.131 nicm 625:
626: /* Is this on the status line? */
627: m->statusat = status_at_line(c);
1.292 nicm 628: m->statuslines = status_line_size(c);
1.272 nicm 629: if (m->statusat != -1 &&
630: y >= (u_int)m->statusat &&
1.292 nicm 631: y < m->statusat + m->statuslines) {
1.272 nicm 632: sr = status_get_range(c, x, y - m->statusat);
1.274 nicm 633: if (sr == NULL) {
634: where = STATUS_DEFAULT;
635: } else {
636: switch (sr->type) {
637: case STYLE_RANGE_NONE:
1.273 nicm 638: return (KEYC_UNKNOWN);
1.274 nicm 639: case STYLE_RANGE_LEFT:
640: where = STATUS_LEFT;
641: break;
642: case STYLE_RANGE_RIGHT:
643: where = STATUS_RIGHT;
644: break;
645: case STYLE_RANGE_WINDOW:
1.298 nicm 646: wl = winlink_find_by_index(&s->windows,
647: sr->argument);
1.274 nicm 648: if (wl == NULL)
649: return (KEYC_UNKNOWN);
650: m->w = wl->window->id;
1.273 nicm 651:
1.274 nicm 652: where = STATUS;
653: break;
654: }
1.258 nicm 655: }
656: }
1.131 nicm 657:
658: /* Not on status line. Adjust position and check for border or pane. */
659: if (where == NOWHERE) {
1.261 nicm 660: px = x;
1.292 nicm 661: if (m->statusat == 0 && y >= m->statuslines)
662: py = y - m->statuslines;
1.131 nicm 663: else if (m->statusat > 0 && y >= (u_int)m->statusat)
1.261 nicm 664: py = m->statusat - 1;
665: else
666: py = y;
667:
668: tty_window_offset(&c->tty, &m->ox, &m->oy, &sx, &sy);
669: log_debug("mouse window @%u at %u,%u (%ux%u)",
670: s->curw->window->id, m->ox, m->oy, sx, sy);
671: if (px > sx || py > sy)
672: return (KEYC_UNKNOWN);
673: px = px + m->ox;
674: py = py + m->oy;
1.131 nicm 675:
1.260 nicm 676: /* Try the pane borders if not zoomed. */
677: if (~s->curw->window->flags & WINDOW_ZOOMED) {
678: TAILQ_FOREACH(wp, &s->curw->window->panes, entry) {
1.261 nicm 679: if ((wp->xoff + wp->sx == px &&
680: wp->yoff <= 1 + py &&
681: wp->yoff + wp->sy >= py) ||
682: (wp->yoff + wp->sy == py &&
683: wp->xoff <= 1 + px &&
684: wp->xoff + wp->sx >= px))
1.260 nicm 685: break;
686: }
687: if (wp != NULL)
688: where = BORDER;
1.131 nicm 689: }
1.260 nicm 690:
691: /* Otherwise try inside the pane. */
692: if (where == NOWHERE) {
1.261 nicm 693: wp = window_get_active_at(s->curw->window, px, py);
1.260 nicm 694: if (wp != NULL)
1.131 nicm 695: where = PANE;
1.315 nicm 696: else
697: return (KEYC_UNKNOWN);
1.131 nicm 698: }
1.260 nicm 699: if (where == PANE)
700: log_debug("mouse %u,%u on pane %%%u", x, y, wp->id);
701: else if (where == BORDER)
702: log_debug("mouse on pane %%%u border", wp->id);
1.131 nicm 703: m->wp = wp->id;
704: m->w = wp->window->id;
705: } else
706: m->wp = -1;
707:
708: /* Stop dragging if needed. */
1.200 nicm 709: if (type != DRAG && type != WHEEL && c->tty.mouse_drag_flag) {
1.131 nicm 710: if (c->tty.mouse_drag_release != NULL)
711: c->tty.mouse_drag_release(c, m);
712:
713: c->tty.mouse_drag_update = NULL;
714: c->tty.mouse_drag_release = NULL;
715:
1.182 nicm 716: /*
1.183 nicm 717: * End a mouse drag by passing a MouseDragEnd key corresponding
718: * to the button that started the drag.
1.182 nicm 719: */
720: switch (c->tty.mouse_drag_flag) {
721: case 1:
722: if (where == PANE)
1.183 nicm 723: key = KEYC_MOUSEDRAGEND1_PANE;
1.182 nicm 724: if (where == STATUS)
1.183 nicm 725: key = KEYC_MOUSEDRAGEND1_STATUS;
1.258 nicm 726: if (where == STATUS_LEFT)
727: key = KEYC_MOUSEDRAGEND1_STATUS_LEFT;
728: if (where == STATUS_RIGHT)
729: key = KEYC_MOUSEDRAGEND1_STATUS_RIGHT;
1.274 nicm 730: if (where == STATUS_DEFAULT)
731: key = KEYC_MOUSEDRAGEND1_STATUS_DEFAULT;
1.182 nicm 732: if (where == BORDER)
1.183 nicm 733: key = KEYC_MOUSEDRAGEND1_BORDER;
1.182 nicm 734: break;
735: case 2:
736: if (where == PANE)
1.183 nicm 737: key = KEYC_MOUSEDRAGEND2_PANE;
1.182 nicm 738: if (where == STATUS)
1.183 nicm 739: key = KEYC_MOUSEDRAGEND2_STATUS;
1.258 nicm 740: if (where == STATUS_LEFT)
741: key = KEYC_MOUSEDRAGEND2_STATUS_LEFT;
742: if (where == STATUS_RIGHT)
743: key = KEYC_MOUSEDRAGEND2_STATUS_RIGHT;
1.274 nicm 744: if (where == STATUS_DEFAULT)
745: key = KEYC_MOUSEDRAGEND2_STATUS_DEFAULT;
1.182 nicm 746: if (where == BORDER)
1.183 nicm 747: key = KEYC_MOUSEDRAGEND2_BORDER;
1.182 nicm 748: break;
749: case 3:
750: if (where == PANE)
1.183 nicm 751: key = KEYC_MOUSEDRAGEND3_PANE;
1.182 nicm 752: if (where == STATUS)
1.183 nicm 753: key = KEYC_MOUSEDRAGEND3_STATUS;
1.258 nicm 754: if (where == STATUS_LEFT)
755: key = KEYC_MOUSEDRAGEND3_STATUS_LEFT;
756: if (where == STATUS_RIGHT)
757: key = KEYC_MOUSEDRAGEND3_STATUS_RIGHT;
1.274 nicm 758: if (where == STATUS_DEFAULT)
759: key = KEYC_MOUSEDRAGEND3_STATUS_DEFAULT;
1.182 nicm 760: if (where == BORDER)
1.183 nicm 761: key = KEYC_MOUSEDRAGEND3_BORDER;
1.182 nicm 762: break;
763: default:
764: key = KEYC_MOUSE;
765: break;
766: }
1.131 nicm 767: c->tty.mouse_drag_flag = 0;
1.305 nicm 768: goto out;
1.131 nicm 769: }
770:
771: /* Convert to a key binding. */
1.177 nicm 772: key = KEYC_UNKNOWN;
1.131 nicm 773: switch (type) {
774: case NOTYPE:
775: break;
1.209 nicm 776: case MOVE:
777: if (where == PANE)
778: key = KEYC_MOUSEMOVE_PANE;
779: if (where == STATUS)
780: key = KEYC_MOUSEMOVE_STATUS;
1.274 nicm 781: if (where == STATUS_LEFT)
782: key = KEYC_MOUSEMOVE_STATUS_LEFT;
783: if (where == STATUS_RIGHT)
784: key = KEYC_MOUSEMOVE_STATUS_RIGHT;
785: if (where == STATUS_DEFAULT)
786: key = KEYC_MOUSEMOVE_STATUS_DEFAULT;
1.209 nicm 787: if (where == BORDER)
788: key = KEYC_MOUSEMOVE_BORDER;
789: break;
1.131 nicm 790: case DRAG:
1.204 nicm 791: if (c->tty.mouse_drag_update != NULL)
792: key = KEYC_DRAGGING;
793: else {
1.131 nicm 794: switch (MOUSE_BUTTONS(b)) {
795: case 0:
796: if (where == PANE)
797: key = KEYC_MOUSEDRAG1_PANE;
798: if (where == STATUS)
799: key = KEYC_MOUSEDRAG1_STATUS;
1.258 nicm 800: if (where == STATUS_LEFT)
801: key = KEYC_MOUSEDRAG1_STATUS_LEFT;
802: if (where == STATUS_RIGHT)
803: key = KEYC_MOUSEDRAG1_STATUS_RIGHT;
1.274 nicm 804: if (where == STATUS_DEFAULT)
805: key = KEYC_MOUSEDRAG1_STATUS_DEFAULT;
1.131 nicm 806: if (where == BORDER)
807: key = KEYC_MOUSEDRAG1_BORDER;
808: break;
809: case 1:
810: if (where == PANE)
811: key = KEYC_MOUSEDRAG2_PANE;
812: if (where == STATUS)
813: key = KEYC_MOUSEDRAG2_STATUS;
1.258 nicm 814: if (where == STATUS_LEFT)
815: key = KEYC_MOUSEDRAG2_STATUS_LEFT;
816: if (where == STATUS_RIGHT)
817: key = KEYC_MOUSEDRAG2_STATUS_RIGHT;
1.274 nicm 818: if (where == STATUS_DEFAULT)
819: key = KEYC_MOUSEDRAG2_STATUS_DEFAULT;
1.131 nicm 820: if (where == BORDER)
821: key = KEYC_MOUSEDRAG2_BORDER;
822: break;
823: case 2:
824: if (where == PANE)
825: key = KEYC_MOUSEDRAG3_PANE;
826: if (where == STATUS)
827: key = KEYC_MOUSEDRAG3_STATUS;
1.258 nicm 828: if (where == STATUS_LEFT)
829: key = KEYC_MOUSEDRAG3_STATUS_LEFT;
830: if (where == STATUS_RIGHT)
831: key = KEYC_MOUSEDRAG3_STATUS_RIGHT;
1.274 nicm 832: if (where == STATUS_DEFAULT)
833: key = KEYC_MOUSEDRAG3_STATUS_DEFAULT;
1.131 nicm 834: if (where == BORDER)
835: key = KEYC_MOUSEDRAG3_BORDER;
836: break;
837: }
838: }
1.66 nicm 839:
1.182 nicm 840: /*
841: * Begin a drag by setting the flag to a non-zero value that
842: * corresponds to the mouse button in use.
843: */
844: c->tty.mouse_drag_flag = MOUSE_BUTTONS(b) + 1;
1.131 nicm 845: break;
846: case WHEEL:
847: if (MOUSE_BUTTONS(b) == MOUSE_WHEEL_UP) {
848: if (where == PANE)
849: key = KEYC_WHEELUP_PANE;
850: if (where == STATUS)
851: key = KEYC_WHEELUP_STATUS;
1.258 nicm 852: if (where == STATUS_LEFT)
853: key = KEYC_WHEELUP_STATUS_LEFT;
854: if (where == STATUS_RIGHT)
855: key = KEYC_WHEELUP_STATUS_RIGHT;
1.274 nicm 856: if (where == STATUS_DEFAULT)
857: key = KEYC_WHEELUP_STATUS_DEFAULT;
1.131 nicm 858: if (where == BORDER)
859: key = KEYC_WHEELUP_BORDER;
860: } else {
861: if (where == PANE)
862: key = KEYC_WHEELDOWN_PANE;
863: if (where == STATUS)
864: key = KEYC_WHEELDOWN_STATUS;
1.274 nicm 865: if (where == STATUS_LEFT)
866: key = KEYC_WHEELDOWN_STATUS_LEFT;
867: if (where == STATUS_RIGHT)
868: key = KEYC_WHEELDOWN_STATUS_RIGHT;
869: if (where == STATUS_DEFAULT)
870: key = KEYC_WHEELDOWN_STATUS_DEFAULT;
1.131 nicm 871: if (where == BORDER)
872: key = KEYC_WHEELDOWN_BORDER;
873: }
874: break;
875: case UP:
876: switch (MOUSE_BUTTONS(b)) {
877: case 0:
878: if (where == PANE)
879: key = KEYC_MOUSEUP1_PANE;
880: if (where == STATUS)
881: key = KEYC_MOUSEUP1_STATUS;
1.258 nicm 882: if (where == STATUS_LEFT)
883: key = KEYC_MOUSEUP1_STATUS_LEFT;
884: if (where == STATUS_RIGHT)
885: key = KEYC_MOUSEUP1_STATUS_RIGHT;
1.274 nicm 886: if (where == STATUS_DEFAULT)
887: key = KEYC_MOUSEUP1_STATUS_DEFAULT;
1.131 nicm 888: if (where == BORDER)
889: key = KEYC_MOUSEUP1_BORDER;
890: break;
891: case 1:
892: if (where == PANE)
893: key = KEYC_MOUSEUP2_PANE;
894: if (where == STATUS)
895: key = KEYC_MOUSEUP2_STATUS;
1.258 nicm 896: if (where == STATUS_LEFT)
897: key = KEYC_MOUSEUP2_STATUS_LEFT;
898: if (where == STATUS_RIGHT)
899: key = KEYC_MOUSEUP2_STATUS_RIGHT;
1.274 nicm 900: if (where == STATUS_DEFAULT)
901: key = KEYC_MOUSEUP2_STATUS_DEFAULT;
1.131 nicm 902: if (where == BORDER)
903: key = KEYC_MOUSEUP2_BORDER;
904: break;
905: case 2:
906: if (where == PANE)
907: key = KEYC_MOUSEUP3_PANE;
908: if (where == STATUS)
909: key = KEYC_MOUSEUP3_STATUS;
1.258 nicm 910: if (where == STATUS_LEFT)
911: key = KEYC_MOUSEUP3_STATUS_LEFT;
912: if (where == STATUS_RIGHT)
913: key = KEYC_MOUSEUP3_STATUS_RIGHT;
1.274 nicm 914: if (where == STATUS_DEFAULT)
915: key = KEYC_MOUSEUP3_STATUS_DEFAULT;
1.131 nicm 916: if (where == BORDER)
917: key = KEYC_MOUSEUP3_BORDER;
918: break;
919: }
920: break;
921: case DOWN:
922: switch (MOUSE_BUTTONS(b)) {
923: case 0:
924: if (where == PANE)
925: key = KEYC_MOUSEDOWN1_PANE;
926: if (where == STATUS)
927: key = KEYC_MOUSEDOWN1_STATUS;
1.258 nicm 928: if (where == STATUS_LEFT)
929: key = KEYC_MOUSEDOWN1_STATUS_LEFT;
930: if (where == STATUS_RIGHT)
931: key = KEYC_MOUSEDOWN1_STATUS_RIGHT;
1.274 nicm 932: if (where == STATUS_DEFAULT)
933: key = KEYC_MOUSEDOWN1_STATUS_DEFAULT;
1.131 nicm 934: if (where == BORDER)
935: key = KEYC_MOUSEDOWN1_BORDER;
936: break;
937: case 1:
938: if (where == PANE)
939: key = KEYC_MOUSEDOWN2_PANE;
940: if (where == STATUS)
941: key = KEYC_MOUSEDOWN2_STATUS;
1.258 nicm 942: if (where == STATUS_LEFT)
943: key = KEYC_MOUSEDOWN2_STATUS_LEFT;
944: if (where == STATUS_RIGHT)
945: key = KEYC_MOUSEDOWN2_STATUS_RIGHT;
1.274 nicm 946: if (where == STATUS_DEFAULT)
947: key = KEYC_MOUSEDOWN2_STATUS_DEFAULT;
1.131 nicm 948: if (where == BORDER)
949: key = KEYC_MOUSEDOWN2_BORDER;
950: break;
951: case 2:
952: if (where == PANE)
953: key = KEYC_MOUSEDOWN3_PANE;
954: if (where == STATUS)
955: key = KEYC_MOUSEDOWN3_STATUS;
1.258 nicm 956: if (where == STATUS_LEFT)
957: key = KEYC_MOUSEDOWN3_STATUS_LEFT;
958: if (where == STATUS_RIGHT)
959: key = KEYC_MOUSEDOWN3_STATUS_RIGHT;
1.274 nicm 960: if (where == STATUS_DEFAULT)
961: key = KEYC_MOUSEDOWN3_STATUS_DEFAULT;
1.131 nicm 962: if (where == BORDER)
963: key = KEYC_MOUSEDOWN3_BORDER;
1.311 nicm 964: break;
965: }
966: break;
967: case SECOND:
968: switch (MOUSE_BUTTONS(b)) {
969: case 0:
970: if (where == PANE)
971: key = KEYC_SECONDCLICK1_PANE;
972: if (where == STATUS)
973: key = KEYC_SECONDCLICK1_STATUS;
974: if (where == STATUS_LEFT)
975: key = KEYC_SECONDCLICK1_STATUS_LEFT;
976: if (where == STATUS_RIGHT)
977: key = KEYC_SECONDCLICK1_STATUS_RIGHT;
978: if (where == STATUS_DEFAULT)
979: key = KEYC_SECONDCLICK1_STATUS_DEFAULT;
980: if (where == BORDER)
981: key = KEYC_SECONDCLICK1_BORDER;
982: break;
983: case 1:
984: if (where == PANE)
985: key = KEYC_SECONDCLICK2_PANE;
986: if (where == STATUS)
987: key = KEYC_SECONDCLICK2_STATUS;
988: if (where == STATUS_LEFT)
989: key = KEYC_SECONDCLICK2_STATUS_LEFT;
990: if (where == STATUS_RIGHT)
991: key = KEYC_SECONDCLICK2_STATUS_RIGHT;
992: if (where == STATUS_DEFAULT)
993: key = KEYC_SECONDCLICK2_STATUS_DEFAULT;
994: if (where == BORDER)
995: key = KEYC_SECONDCLICK2_BORDER;
996: break;
997: case 2:
998: if (where == PANE)
999: key = KEYC_SECONDCLICK3_PANE;
1000: if (where == STATUS)
1001: key = KEYC_SECONDCLICK3_STATUS;
1002: if (where == STATUS_LEFT)
1003: key = KEYC_SECONDCLICK3_STATUS_LEFT;
1004: if (where == STATUS_RIGHT)
1005: key = KEYC_SECONDCLICK3_STATUS_RIGHT;
1006: if (where == STATUS_DEFAULT)
1007: key = KEYC_SECONDCLICK3_STATUS_DEFAULT;
1008: if (where == BORDER)
1009: key = KEYC_SECONDCLICK3_BORDER;
1.131 nicm 1010: break;
1.66 nicm 1011: }
1.131 nicm 1012: break;
1.192 nicm 1013: case DOUBLE:
1014: switch (MOUSE_BUTTONS(b)) {
1015: case 0:
1016: if (where == PANE)
1017: key = KEYC_DOUBLECLICK1_PANE;
1018: if (where == STATUS)
1019: key = KEYC_DOUBLECLICK1_STATUS;
1.258 nicm 1020: if (where == STATUS_LEFT)
1021: key = KEYC_DOUBLECLICK1_STATUS_LEFT;
1022: if (where == STATUS_RIGHT)
1023: key = KEYC_DOUBLECLICK1_STATUS_RIGHT;
1.274 nicm 1024: if (where == STATUS_DEFAULT)
1025: key = KEYC_DOUBLECLICK1_STATUS_DEFAULT;
1.192 nicm 1026: if (where == BORDER)
1027: key = KEYC_DOUBLECLICK1_BORDER;
1028: break;
1029: case 1:
1030: if (where == PANE)
1031: key = KEYC_DOUBLECLICK2_PANE;
1032: if (where == STATUS)
1033: key = KEYC_DOUBLECLICK2_STATUS;
1.258 nicm 1034: if (where == STATUS_LEFT)
1035: key = KEYC_DOUBLECLICK2_STATUS_LEFT;
1036: if (where == STATUS_RIGHT)
1037: key = KEYC_DOUBLECLICK2_STATUS_RIGHT;
1.274 nicm 1038: if (where == STATUS_DEFAULT)
1039: key = KEYC_DOUBLECLICK2_STATUS_DEFAULT;
1.192 nicm 1040: if (where == BORDER)
1041: key = KEYC_DOUBLECLICK2_BORDER;
1042: break;
1043: case 2:
1044: if (where == PANE)
1045: key = KEYC_DOUBLECLICK3_PANE;
1046: if (where == STATUS)
1047: key = KEYC_DOUBLECLICK3_STATUS;
1.258 nicm 1048: if (where == STATUS_LEFT)
1049: key = KEYC_DOUBLECLICK3_STATUS_LEFT;
1050: if (where == STATUS_RIGHT)
1051: key = KEYC_DOUBLECLICK3_STATUS_RIGHT;
1.274 nicm 1052: if (where == STATUS_DEFAULT)
1053: key = KEYC_DOUBLECLICK3_STATUS_DEFAULT;
1.192 nicm 1054: if (where == BORDER)
1055: key = KEYC_DOUBLECLICK3_BORDER;
1056: break;
1057: }
1058: break;
1059: case TRIPLE:
1060: switch (MOUSE_BUTTONS(b)) {
1061: case 0:
1062: if (where == PANE)
1063: key = KEYC_TRIPLECLICK1_PANE;
1064: if (where == STATUS)
1065: key = KEYC_TRIPLECLICK1_STATUS;
1.258 nicm 1066: if (where == STATUS_LEFT)
1067: key = KEYC_TRIPLECLICK1_STATUS_LEFT;
1068: if (where == STATUS_RIGHT)
1069: key = KEYC_TRIPLECLICK1_STATUS_RIGHT;
1.274 nicm 1070: if (where == STATUS_DEFAULT)
1071: key = KEYC_TRIPLECLICK1_STATUS_DEFAULT;
1.192 nicm 1072: if (where == BORDER)
1073: key = KEYC_TRIPLECLICK1_BORDER;
1074: break;
1075: case 1:
1076: if (where == PANE)
1077: key = KEYC_TRIPLECLICK2_PANE;
1078: if (where == STATUS)
1079: key = KEYC_TRIPLECLICK2_STATUS;
1.258 nicm 1080: if (where == STATUS_LEFT)
1081: key = KEYC_TRIPLECLICK2_STATUS_LEFT;
1082: if (where == STATUS_RIGHT)
1083: key = KEYC_TRIPLECLICK2_STATUS_RIGHT;
1.274 nicm 1084: if (where == STATUS_DEFAULT)
1085: key = KEYC_TRIPLECLICK2_STATUS_DEFAULT;
1.192 nicm 1086: if (where == BORDER)
1087: key = KEYC_TRIPLECLICK2_BORDER;
1088: break;
1089: case 2:
1090: if (where == PANE)
1091: key = KEYC_TRIPLECLICK3_PANE;
1092: if (where == STATUS)
1093: key = KEYC_TRIPLECLICK3_STATUS;
1.258 nicm 1094: if (where == STATUS_LEFT)
1095: key = KEYC_TRIPLECLICK3_STATUS_LEFT;
1096: if (where == STATUS_RIGHT)
1097: key = KEYC_TRIPLECLICK3_STATUS_RIGHT;
1.274 nicm 1098: if (where == STATUS_DEFAULT)
1099: key = KEYC_TRIPLECLICK3_STATUS_DEFAULT;
1.192 nicm 1100: if (where == BORDER)
1101: key = KEYC_TRIPLECLICK3_BORDER;
1102: break;
1103: }
1104: break;
1.66 nicm 1105: }
1.177 nicm 1106: if (key == KEYC_UNKNOWN)
1107: return (KEYC_UNKNOWN);
1.66 nicm 1108:
1.305 nicm 1109: out:
1.131 nicm 1110: /* Apply modifiers if any. */
1111: if (b & MOUSE_MASK_META)
1.342 nicm 1112: key |= KEYC_META;
1.131 nicm 1113: if (b & MOUSE_MASK_CTRL)
1114: key |= KEYC_CTRL;
1115: if (b & MOUSE_MASK_SHIFT)
1116: key |= KEYC_SHIFT;
1.66 nicm 1117:
1.305 nicm 1118: if (log_get_level() != 0)
1.343 nicm 1119: log_debug("mouse key is %s", key_string_lookup_key (key, 1));
1.131 nicm 1120: return (key);
1.66 nicm 1121: }
1122:
1.82 nicm 1123: /* Is this fast enough to probably be a paste? */
1.190 nicm 1124: static int
1.82 nicm 1125: server_client_assume_paste(struct session *s)
1126: {
1127: struct timeval tv;
1.84 nicm 1128: int t;
1.82 nicm 1129:
1.163 nicm 1130: if ((t = options_get_number(s->options, "assume-paste-time")) == 0)
1.83 nicm 1131: return (0);
1.82 nicm 1132:
1133: timersub(&s->activity_time, &s->last_activity_time, &tv);
1.171 nicm 1134: if (tv.tv_sec == 0 && tv.tv_usec < t * 1000) {
1135: log_debug("session %s pasting (flag %d)", s->name,
1136: !!(s->flags & SESSION_PASTING));
1137: if (s->flags & SESSION_PASTING)
1138: return (1);
1139: s->flags |= SESSION_PASTING;
1140: return (0);
1141: }
1142: log_debug("session %s not pasting", s->name);
1143: s->flags &= ~SESSION_PASTING;
1.83 nicm 1144: return (0);
1.82 nicm 1145: }
1146:
1.295 nicm 1147: /* Has the latest client changed? */
1148: static void
1149: server_client_update_latest(struct client *c)
1150: {
1151: struct window *w;
1152:
1153: if (c->session == NULL)
1154: return;
1155: w = c->session->curw->window;
1156:
1157: if (w->latest == c)
1158: return;
1159: w->latest = c;
1160:
1161: if (options_get_number(w->options, "window-size") == WINDOW_SIZE_LATEST)
1.355 nicm 1162: recalculate_size(w, 0);
1.377 nicm 1163:
1164: notify_client("client-active", c);
1.295 nicm 1165: }
1166:
1.276 nicm 1167: /*
1168: * Handle data key input from client. This owns and can modify the key event it
1169: * is given and is responsible for freeing it.
1170: */
1.280 nicm 1171: static enum cmd_retval
1.276 nicm 1172: server_client_key_callback(struct cmdq_item *item, void *data)
1.18 nicm 1173: {
1.316 nicm 1174: struct client *c = cmdq_get_client(item);
1.276 nicm 1175: struct key_event *event = data;
1176: key_code key = event->key;
1177: struct mouse_event *m = &event->m;
1.267 nicm 1178: struct session *s = c->session;
1179: struct winlink *wl;
1180: struct window_pane *wp;
1181: struct window_mode_entry *wme;
1182: struct timeval tv;
1183: struct key_table *table, *first;
1184: struct key_binding *bd;
1185: int xtimeout, flags;
1186: struct cmd_find_state fs;
1187: key_code key0;
1.18 nicm 1188:
1189: /* Check the client is good to accept input. */
1.303 nicm 1190: if (s == NULL || (c->flags & CLIENT_UNATTACHEDFLAGS))
1.276 nicm 1191: goto out;
1.264 nicm 1192: wl = s->curw;
1.18 nicm 1193:
1194: /* Update the activity timer. */
1195: if (gettimeofday(&c->activity_time, NULL) != 0)
1196: fatal("gettimeofday failed");
1.149 nicm 1197: session_update_activity(s, &c->activity_time);
1.18 nicm 1198:
1199: /* Check for mouse keys. */
1.204 nicm 1200: m->valid = 0;
1.306 nicm 1201: if (key == KEYC_MOUSE || key == KEYC_DOUBLECLICK) {
1.30 nicm 1202: if (c->flags & CLIENT_READONLY)
1.276 nicm 1203: goto out;
1204: key = server_client_check_mouse(c, event);
1.177 nicm 1205: if (key == KEYC_UNKNOWN)
1.276 nicm 1206: goto out;
1.131 nicm 1207:
1208: m->valid = 1;
1209: m->key = key;
1.203 nicm 1210:
1211: /*
1.204 nicm 1212: * Mouse drag is in progress, so fire the callback (now that
1213: * the mouse event is valid).
1.203 nicm 1214: */
1.305 nicm 1215: if ((key & KEYC_MASK_KEY) == KEYC_DRAGGING) {
1.204 nicm 1216: c->tty.mouse_drag_update(c, m);
1.276 nicm 1217: goto out;
1.204 nicm 1218: }
1.338 nicm 1219: event->key = key;
1.276 nicm 1220: }
1.18 nicm 1221:
1.202 nicm 1222: /* Find affected pane. */
1.243 nicm 1223: if (!KEYC_IS_MOUSE(key) || cmd_find_from_mouse(&fs, m, 0) != 0)
1.341 nicm 1224: cmd_find_from_client(&fs, c, 0);
1.227 nicm 1225: wp = fs.wp;
1.202 nicm 1226:
1227: /* Forward mouse keys if disabled. */
1.206 nicm 1228: if (KEYC_IS_MOUSE(key) && !options_get_number(s->options, "mouse"))
1.253 nicm 1229: goto forward_key;
1.202 nicm 1230:
1.132 nicm 1231: /* Treat everything as a regular key when pasting is detected. */
1.161 nicm 1232: if (!KEYC_IS_MOUSE(key) && server_client_assume_paste(s))
1.253 nicm 1233: goto forward_key;
1.18 nicm 1234:
1.191 nicm 1235: /*
1236: * Work out the current key table. If the pane is in a mode, use
1237: * the mode table instead of the default key table.
1238: */
1.223 nicm 1239: if (server_client_is_default_key_table(c, c->keytable) &&
1240: wp != NULL &&
1.267 nicm 1241: (wme = TAILQ_FIRST(&wp->modes)) != NULL &&
1242: wme->mode->key_table != NULL)
1243: table = key_bindings_get_table(wme->mode->key_table(wme), 1);
1.223 nicm 1244: else
1.191 nicm 1245: table = c->keytable;
1.223 nicm 1246: first = table;
1.191 nicm 1247:
1.253 nicm 1248: table_changed:
1.205 nicm 1249: /*
1250: * The prefix always takes precedence and forces a switch to the prefix
1251: * table, unless we are already there.
1252: */
1.343 nicm 1253: key0 = (key & (KEYC_MASK_KEY|KEYC_MASK_MODIFIERS));
1.239 nicm 1254: if ((key0 == (key_code)options_get_number(s->options, "prefix") ||
1255: key0 == (key_code)options_get_number(s->options, "prefix2")) &&
1.205 nicm 1256: strcmp(table->name, "prefix") != 0) {
1257: server_client_set_key_table(c, "prefix");
1258: server_status_client(c);
1.276 nicm 1259: goto out;
1.205 nicm 1260: }
1.238 nicm 1261: flags = c->flags;
1.205 nicm 1262:
1.262 nicm 1263: try_again:
1.223 nicm 1264: /* Log key table. */
1265: if (wp == NULL)
1266: log_debug("key table %s (no pane)", table->name);
1267: else
1268: log_debug("key table %s (pane %%%u)", table->name, wp->id);
1.225 nicm 1269: if (c->flags & CLIENT_REPEAT)
1270: log_debug("currently repeating");
1.223 nicm 1271:
1.132 nicm 1272: /* Try to see if there is a key binding in the current table. */
1.254 nicm 1273: bd = key_bindings_get(table, key0);
1.132 nicm 1274: if (bd != NULL) {
1275: /*
1276: * Key was matched in this table. If currently repeating but a
1277: * non-repeating binding was found, stop repeating and try
1278: * again in the root table.
1279: */
1.222 nicm 1280: if ((c->flags & CLIENT_REPEAT) &&
1281: (~bd->flags & KEY_BINDING_REPEAT)) {
1.262 nicm 1282: log_debug("found in key table %s (not repeating)",
1283: table->name);
1.178 nicm 1284: server_client_set_key_table(c, NULL);
1.262 nicm 1285: first = table = c->keytable;
1.132 nicm 1286: c->flags &= ~CLIENT_REPEAT;
1.85 nicm 1287: server_status_client(c);
1.253 nicm 1288: goto table_changed;
1.18 nicm 1289: }
1.223 nicm 1290: log_debug("found in key table %s", table->name);
1.82 nicm 1291:
1.132 nicm 1292: /*
1293: * Take a reference to this table to make sure the key binding
1294: * doesn't disappear.
1295: */
1296: table->references++;
1297:
1298: /*
1299: * If this is a repeating key, start the timer. Otherwise reset
1300: * the client back to the root table.
1301: */
1.163 nicm 1302: xtimeout = options_get_number(s->options, "repeat-time");
1.222 nicm 1303: if (xtimeout != 0 && (bd->flags & KEY_BINDING_REPEAT)) {
1.132 nicm 1304: c->flags |= CLIENT_REPEAT;
1305:
1306: tv.tv_sec = xtimeout / 1000;
1307: tv.tv_usec = (xtimeout % 1000) * 1000L;
1308: evtimer_del(&c->repeat_timer);
1309: evtimer_add(&c->repeat_timer, &tv);
1310: } else {
1.18 nicm 1311: c->flags &= ~CLIENT_REPEAT;
1.178 nicm 1312: server_client_set_key_table(c, NULL);
1.18 nicm 1313: }
1.132 nicm 1314: server_status_client(c);
1315:
1.227 nicm 1316: /* Execute the key binding. */
1.317 nicm 1317: key_bindings_dispatch(bd, item, c, event, &fs);
1.132 nicm 1318: key_bindings_unref_table(table);
1.276 nicm 1319: goto out;
1.18 nicm 1320: }
1321:
1.132 nicm 1322: /*
1.253 nicm 1323: * No match, try the ANY key.
1324: */
1325: if (key0 != KEYC_ANY) {
1326: key0 = KEYC_ANY;
1327: goto try_again;
1328: }
1329:
1330: /*
1.223 nicm 1331: * No match in this table. If not in the root table or if repeating,
1332: * switch the client back to the root table and try again.
1.132 nicm 1333: */
1.223 nicm 1334: log_debug("not found in key table %s", table->name);
1335: if (!server_client_is_default_key_table(c, table) ||
1336: (c->flags & CLIENT_REPEAT)) {
1.262 nicm 1337: log_debug("trying in root table");
1.178 nicm 1338: server_client_set_key_table(c, NULL);
1.262 nicm 1339: table = c->keytable;
1340: if (c->flags & CLIENT_REPEAT)
1341: first = table;
1.18 nicm 1342: c->flags &= ~CLIENT_REPEAT;
1.132 nicm 1343: server_status_client(c);
1.253 nicm 1344: goto table_changed;
1.18 nicm 1345: }
1346:
1.223 nicm 1347: /*
1348: * No match in the root table either. If this wasn't the first table
1349: * tried, don't pass the key to the pane.
1350: */
1.238 nicm 1351: if (first != table && (~flags & CLIENT_REPEAT)) {
1.178 nicm 1352: server_client_set_key_table(c, NULL);
1.132 nicm 1353: server_status_client(c);
1.276 nicm 1354: goto out;
1.161 nicm 1355: }
1356:
1.253 nicm 1357: forward_key:
1.161 nicm 1358: if (c->flags & CLIENT_READONLY)
1.276 nicm 1359: goto out;
1.161 nicm 1360: if (wp != NULL)
1.264 nicm 1361: window_pane_key(wp, c, s, wl, key, m);
1.276 nicm 1362:
1363: out:
1.347 nicm 1364: if (s != NULL && key != KEYC_FOCUS_OUT)
1.295 nicm 1365: server_client_update_latest(c);
1.276 nicm 1366: free(event);
1367: return (CMD_RETURN_NORMAL);
1.280 nicm 1368: }
1369:
1370: /* Handle a key event. */
1371: int
1372: server_client_handle_key(struct client *c, struct key_event *event)
1373: {
1374: struct session *s = c->session;
1375: struct cmdq_item *item;
1376:
1377: /* Check the client is good to accept input. */
1.303 nicm 1378: if (s == NULL || (c->flags & CLIENT_UNATTACHEDFLAGS))
1.280 nicm 1379: return (0);
1380:
1381: /*
1.291 nicm 1382: * Key presses in overlay mode and the command prompt are a special
1383: * case. The queue might be blocked so they need to be processed
1384: * immediately rather than queued.
1.280 nicm 1385: */
1.291 nicm 1386: if (~c->flags & CLIENT_READONLY) {
1.372 nicm 1387: if (c->message_string != NULL) {
1388: if (c->message_ignore_keys)
1389: return (0);
1390: status_message_clear(c);
1391: }
1.291 nicm 1392: if (c->overlay_key != NULL) {
1.380 ! nicm 1393: switch (c->overlay_key(c, c->overlay_data, event)) {
1.291 nicm 1394: case 0:
1395: return (0);
1396: case 1:
1397: server_client_clear_overlay(c);
1398: return (0);
1399: }
1.290 nicm 1400: }
1.293 nicm 1401: server_client_clear_overlay(c);
1.335 nicm 1402: if (c->prompt_string != NULL) {
1403: if (status_prompt_key(c, event->key) == 0)
1404: return (0);
1405: }
1.280 nicm 1406: }
1407:
1408: /*
1409: * Add the key to the queue so it happens after any commands queued by
1410: * previous keys.
1411: */
1412: item = cmdq_get_callback(server_client_key_callback, event);
1413: cmdq_append(c, item);
1414: return (1);
1.18 nicm 1415: }
1416:
1.2 nicm 1417: /* Client functions that need to happen every loop. */
1418: void
1419: server_client_loop(void)
1420: {
1421: struct client *c;
1422: struct window *w;
1423: struct window_pane *wp;
1.344 nicm 1424:
1425: /* Check for window resize. This is done before redrawing. */
1426: RB_FOREACH(w, windows, &windows)
1427: server_client_check_window_resize(w);
1.2 nicm 1428:
1.344 nicm 1429: /* Check clients. */
1.135 nicm 1430: TAILQ_FOREACH(c, &clients, entry) {
1.36 nicm 1431: server_client_check_exit(c);
1432: if (c->session != NULL) {
1.366 nicm 1433: server_client_check_modes(c);
1.36 nicm 1434: server_client_check_redraw(c);
1435: server_client_reset_state(c);
1436: }
1.2 nicm 1437: }
1438:
1439: /*
1440: * Any windows will have been redrawn as part of clients, so clear
1.379 nicm 1441: * their flags now.
1.2 nicm 1442: */
1.134 nicm 1443: RB_FOREACH(w, windows, &windows) {
1.91 nicm 1444: TAILQ_FOREACH(wp, &w->panes, entry) {
1.289 nicm 1445: if (wp->fd != -1) {
1.344 nicm 1446: server_client_check_pane_resize(wp);
1.345 nicm 1447: server_client_check_pane_buffer(wp);
1.101 nicm 1448: }
1.326 nicm 1449: wp->flags &= ~PANE_REDRAW;
1.91 nicm 1450: }
1.150 nicm 1451: check_window_name(w);
1.2 nicm 1452: }
1.92 nicm 1453: }
1454:
1.344 nicm 1455: /* Check if window needs to be resized. */
1456: static void
1457: server_client_check_window_resize(struct window *w)
1458: {
1459: struct winlink *wl;
1460:
1461: if (~w->flags & WINDOW_RESIZE)
1462: return;
1463:
1464: TAILQ_FOREACH(wl, &w->winlinks, wentry) {
1465: if (wl->session->attached != 0 && wl->session->curw == wl)
1466: break;
1467: }
1468: if (wl == NULL)
1469: return;
1470:
1471: log_debug("%s: resizing window @%u", __func__, w->id);
1472: resize_window(w, w->new_sx, w->new_sy, w->new_xpixel, w->new_ypixel);
1473: }
1474:
1.356 nicm 1475: /* Resize timer event. */
1.188 nicm 1476: static void
1.356 nicm 1477: server_client_resize_timer(__unused int fd, __unused short events, void *data)
1.92 nicm 1478: {
1.356 nicm 1479: struct window_pane *wp = data;
1.92 nicm 1480:
1.356 nicm 1481: log_debug("%s: %%%u resize timer expired", __func__, wp->id);
1482: evtimer_del(&wp->resize_timer);
1.188 nicm 1483: }
1484:
1.374 nicm 1485: /* Check if pane should be resized. */
1.294 nicm 1486: static void
1.374 nicm 1487: server_client_check_pane_resize(struct window_pane *wp)
1.294 nicm 1488: {
1.374 nicm 1489: struct window_pane_resize *r;
1490: struct window_pane_resize *r1;
1491: struct window_pane_resize *first;
1492: struct window_pane_resize *last;
1493: struct timeval tv = { .tv_usec = 250000 };
1.294 nicm 1494:
1.374 nicm 1495: if (TAILQ_EMPTY(&wp->resize_queue))
1496: return;
1.294 nicm 1497:
1.356 nicm 1498: if (!event_initialized(&wp->resize_timer))
1499: evtimer_set(&wp->resize_timer, server_client_resize_timer, wp);
1.374 nicm 1500: if (evtimer_pending(&wp->resize_timer, NULL))
1501: return;
1.356 nicm 1502:
1503: log_debug("%s: %%%u needs to be resized", __func__, wp->id);
1.374 nicm 1504: TAILQ_FOREACH(r, &wp->resize_queue, entry) {
1505: log_debug("queued resize: %ux%u -> %ux%u", r->osx, r->osy,
1506: r->sx, r->sy);
1.356 nicm 1507: }
1.188 nicm 1508:
1.374 nicm 1509: /*
1510: * There are three cases that matter:
1511: *
1512: * - Only one resize. It can just be applied.
1513: *
1514: * - Multiple resizes and the ending size is different from the
1515: * starting size. We can discard all resizes except the most recent.
1516: *
1517: * - Multiple resizes and the ending size is the same as the starting
1518: * size. We must resize at least twice to force the application to
1519: * redraw. So apply the first and leave the last on the queue for
1520: * next time.
1521: */
1522: first = TAILQ_FIRST(&wp->resize_queue);
1523: last = TAILQ_LAST(&wp->resize_queue, window_pane_resizes);
1524: if (first == last) {
1525: /* Only one resize. */
1526: window_pane_send_resize(wp, first->sx, first->sy);
1527: TAILQ_REMOVE(&wp->resize_queue, first, entry);
1528: free(first);
1529: } else if (last->sx != first->osx || last->sy != first->osy) {
1530: /* Multiple resizes ending up with a different size. */
1531: window_pane_send_resize(wp, last->sx, last->sy);
1532: TAILQ_FOREACH_SAFE(r, &wp->resize_queue, entry, r1) {
1533: TAILQ_REMOVE(&wp->resize_queue, r, entry);
1534: free(r);
1535: }
1.356 nicm 1536: } else {
1537: /*
1.374 nicm 1538: * Multiple resizes ending up with the same size. There will
1539: * not be more than one to the same size in succession so we
1540: * can just use the last-but-one on the list and leave the last
1541: * for later. We reduce the time until the next check to avoid
1542: * a long delay between the resizes.
1.356 nicm 1543: */
1.374 nicm 1544: r = TAILQ_PREV(last, window_pane_resizes, entry);
1545: window_pane_send_resize(wp, r->sx, r->sy);
1546: TAILQ_FOREACH_SAFE(r, &wp->resize_queue, entry, r1) {
1547: if (r == last)
1548: break;
1549: TAILQ_REMOVE(&wp->resize_queue, r, entry);
1550: free(r);
1.356 nicm 1551: }
1.374 nicm 1552: tv.tv_usec = 10000;
1.356 nicm 1553: }
1.374 nicm 1554: evtimer_add(&wp->resize_timer, &tv);
1.345 nicm 1555: }
1.374 nicm 1556:
1.345 nicm 1557:
1558: /* Check pane buffer size. */
1559: static void
1560: server_client_check_pane_buffer(struct window_pane *wp)
1561: {
1562: struct evbuffer *evb = wp->event->input;
1563: size_t minimum;
1564: struct client *c;
1.346 nicm 1565: struct window_pane_offset *wpo;
1566: int off = 1, flag;
1567: u_int attached_clients = 0;
1.353 nicm 1568: size_t new_size;
1.345 nicm 1569:
1570: /*
1.352 nicm 1571: * Work out the minimum used size. This is the most that can be removed
1572: * from the buffer.
1.345 nicm 1573: */
1.352 nicm 1574: minimum = wp->offset.used;
1575: if (wp->pipe_fd != -1 && wp->pipe_offset.used < minimum)
1576: minimum = wp->pipe_offset.used;
1.345 nicm 1577: TAILQ_FOREACH(c, &clients, entry) {
1578: if (c->session == NULL)
1579: continue;
1.346 nicm 1580: attached_clients++;
1581:
1582: if (~c->flags & CLIENT_CONTROL) {
1.345 nicm 1583: off = 0;
1584: continue;
1585: }
1.346 nicm 1586: wpo = control_pane_offset(c, wp, &flag);
1587: if (wpo == NULL) {
1.345 nicm 1588: off = 0;
1.346 nicm 1589: continue;
1590: }
1591: if (!flag)
1592: off = 0;
1593:
1.353 nicm 1594: window_pane_get_new_data(wp, wpo, &new_size);
1595: log_debug("%s: %s has %zu bytes used and %zu left for %%%u",
1596: __func__, c->name, wpo->used - wp->base_offset, new_size,
1597: wp->id);
1598: if (wpo->used < minimum)
1.352 nicm 1599: minimum = wpo->used;
1.345 nicm 1600: }
1.346 nicm 1601: if (attached_clients == 0)
1602: off = 0;
1.345 nicm 1603: minimum -= wp->base_offset;
1604: if (minimum == 0)
1605: goto out;
1606:
1607: /* Drain the buffer. */
1.352 nicm 1608: log_debug("%s: %%%u has %zu minimum (of %zu) bytes used", __func__,
1609: wp->id, minimum, EVBUFFER_LENGTH(evb));
1.345 nicm 1610: evbuffer_drain(evb, minimum);
1611:
1612: /*
1613: * Adjust the base offset. If it would roll over, all the offsets into
1614: * the buffer need to be adjusted.
1615: */
1616: if (wp->base_offset > SIZE_MAX - minimum) {
1617: log_debug("%s: %%%u base offset has wrapped", __func__, wp->id);
1618: wp->offset.used -= wp->base_offset;
1.352 nicm 1619: if (wp->pipe_fd != -1)
1.345 nicm 1620: wp->pipe_offset.used -= wp->base_offset;
1621: TAILQ_FOREACH(c, &clients, entry) {
1622: if (c->session == NULL || (~c->flags & CLIENT_CONTROL))
1623: continue;
1.346 nicm 1624: wpo = control_pane_offset(c, wp, &flag);
1.352 nicm 1625: if (wpo != NULL && !flag)
1.346 nicm 1626: wpo->used -= wp->base_offset;
1.345 nicm 1627: }
1628: wp->base_offset = minimum;
1629: } else
1630: wp->base_offset += minimum;
1631:
1632: out:
1633: /*
1634: * If there is data remaining, and there are no clients able to consume
1.351 nicm 1635: * it, do not read any more. This is true when there are attached
1636: * clients, all of which are control clients which are not able to
1637: * accept any more data.
1.345 nicm 1638: */
1.352 nicm 1639: log_debug("%s: pane %%%u is %s", __func__, wp->id, off ? "off" : "on");
1.345 nicm 1640: if (off)
1641: bufferevent_disable(wp->event, EV_READ);
1642: else
1643: bufferevent_enable(wp->event, EV_READ);
1.91 nicm 1644: }
1645:
1.18 nicm 1646: /*
1647: * Update cursor position and mode settings. The scroll region and attributes
1648: * are cleared when idle (waiting for an event) as this is the most likely time
1649: * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
1650: * compromise between excessive resets and likelihood of an interrupt.
1651: *
1652: * tty_region/tty_reset/tty_update_mode already take care of not resetting
1653: * things that are already in their default state.
1654: */
1.190 nicm 1655: static void
1.18 nicm 1656: server_client_reset_state(struct client *c)
1.1 nicm 1657: {
1.332 nicm 1658: struct tty *tty = &c->tty;
1.18 nicm 1659: struct window *w = c->session->curw->window;
1.341 nicm 1660: struct window_pane *wp = server_client_get_pane(c), *loop;
1.336 nicm 1661: struct screen *s = NULL;
1.163 nicm 1662: struct options *oo = c->session->options;
1.336 nicm 1663: int mode = 0, cursor, flags;
1.261 nicm 1664: u_int cx = 0, cy = 0, ox, oy, sx, sy;
1.60 nicm 1665:
1.186 nicm 1666: if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.86 nicm 1667: return;
1668:
1.332 nicm 1669: /* Disable the block flag. */
1670: flags = (tty->flags & TTY_BLOCK);
1671: tty->flags &= ~TTY_BLOCK;
1672:
1.309 nicm 1673: /* Get mode from overlay if any, else from screen. */
1674: if (c->overlay_draw != NULL) {
1.336 nicm 1675: if (c->overlay_mode != NULL)
1.380 ! nicm 1676: s = c->overlay_mode(c, c->overlay_data, &cx, &cy);
1.336 nicm 1677: } else
1.309 nicm 1678: s = wp->screen;
1.336 nicm 1679: if (s != NULL)
1.309 nicm 1680: mode = s->mode;
1.375 nicm 1681: if (log_get_level() != 0) {
1682: log_debug("%s: client %s mode %s", __func__, c->name,
1683: screen_mode_to_string(mode));
1684: }
1.309 nicm 1685:
1686: /* Reset region and margin. */
1.332 nicm 1687: tty_region_off(tty);
1688: tty_margin_off(tty);
1.1 nicm 1689:
1.261 nicm 1690: /* Move cursor to pane cursor and offset. */
1.309 nicm 1691: if (c->overlay_draw == NULL) {
1692: cursor = 0;
1.332 nicm 1693: tty_window_offset(tty, &ox, &oy, &sx, &sy);
1.309 nicm 1694: if (wp->xoff + s->cx >= ox && wp->xoff + s->cx <= ox + sx &&
1695: wp->yoff + s->cy >= oy && wp->yoff + s->cy <= oy + sy) {
1696: cursor = 1;
1.261 nicm 1697:
1.309 nicm 1698: cx = wp->xoff + s->cx - ox;
1699: cy = wp->yoff + s->cy - oy;
1.261 nicm 1700:
1.309 nicm 1701: if (status_at_line(c) == 0)
1702: cy += status_line_size(c);
1703: }
1704: if (!cursor)
1705: mode &= ~MODE_CURSOR;
1.261 nicm 1706: }
1.332 nicm 1707: log_debug("%s: cursor to %u,%u", __func__, cx, cy);
1708: tty_cursor(tty, cx, cy);
1.1 nicm 1709:
1.50 nicm 1710: /*
1.131 nicm 1711: * Set mouse mode if requested. To support dragging, always use button
1712: * mode.
1.57 nicm 1713: */
1.209 nicm 1714: if (options_get_number(oo, "mouse")) {
1.309 nicm 1715: if (c->overlay_draw == NULL) {
1.363 nicm 1716: mode &= ~ALL_MOUSE_MODES;
1.309 nicm 1717: TAILQ_FOREACH(loop, &w->panes, entry) {
1718: if (loop->screen->mode & MODE_MOUSE_ALL)
1719: mode |= MODE_MOUSE_ALL;
1720: }
1.209 nicm 1721: }
1722: if (~mode & MODE_MOUSE_ALL)
1723: mode |= MODE_MOUSE_BUTTON;
1724: }
1.215 nicm 1725:
1726: /* Clear bracketed paste mode if at the prompt. */
1.309 nicm 1727: if (c->overlay_draw == NULL && c->prompt_string != NULL)
1.215 nicm 1728: mode &= ~MODE_BRACKETPASTE;
1.48 nicm 1729:
1730: /* Set the terminal mode and reset attributes. */
1.332 nicm 1731: tty_update_mode(tty, mode, s);
1732: tty_reset(tty);
1.330 nicm 1733:
1.332 nicm 1734: /* All writing must be done, send a sync end (if it was started). */
1735: tty_sync_end(tty);
1736: tty->flags |= flags;
1.17 nicm 1737: }
1738:
1739: /* Repeat time callback. */
1.190 nicm 1740: static void
1.170 nicm 1741: server_client_repeat_timer(__unused int fd, __unused short events, void *data)
1.17 nicm 1742: {
1743: struct client *c = data;
1744:
1.85 nicm 1745: if (c->flags & CLIENT_REPEAT) {
1.178 nicm 1746: server_client_set_key_table(c, NULL);
1.132 nicm 1747: c->flags &= ~CLIENT_REPEAT;
1748: server_status_client(c);
1.85 nicm 1749: }
1.192 nicm 1750: }
1751:
1752: /* Double-click callback. */
1753: static void
1754: server_client_click_timer(__unused int fd, __unused short events, void *data)
1755: {
1.306 nicm 1756: struct client *c = data;
1757: struct key_event *event;
1758:
1759: log_debug("click timer expired");
1.192 nicm 1760:
1.306 nicm 1761: if (c->flags & CLIENT_TRIPLECLICK) {
1762: /*
1763: * Waiting for a third click that hasn't happened, so this must
1764: * have been a double click.
1765: */
1766: event = xmalloc(sizeof *event);
1767: event->key = KEYC_DOUBLECLICK;
1768: memcpy(&event->m, &c->click_event, sizeof event->m);
1769: if (!server_client_handle_key(c, event))
1770: free(event);
1771: }
1.192 nicm 1772: c->flags &= ~(CLIENT_DOUBLECLICK|CLIENT_TRIPLECLICK);
1.1 nicm 1773: }
1774:
1.36 nicm 1775: /* Check if client should be exited. */
1.190 nicm 1776: static void
1.36 nicm 1777: server_client_check_exit(struct client *c)
1778: {
1.300 nicm 1779: struct client_file *cf;
1.352 nicm 1780: const char *name = c->exit_session;
1.357 nicm 1781: char *data;
1782: size_t size, msize;
1.300 nicm 1783:
1.354 nicm 1784: if (c->flags & (CLIENT_DEAD|CLIENT_EXITED))
1785: return;
1786: if (~c->flags & CLIENT_EXIT)
1.36 nicm 1787: return;
1788:
1.352 nicm 1789: if (c->flags & CLIENT_CONTROL) {
1.355 nicm 1790: control_discard(c);
1.352 nicm 1791: if (!control_all_done(c))
1792: return;
1793: }
1.300 nicm 1794: RB_FOREACH(cf, client_files, &c->files) {
1795: if (EVBUFFER_LENGTH(cf->buffer) != 0)
1796: return;
1797: }
1.286 nicm 1798: c->flags |= CLIENT_EXITED;
1.352 nicm 1799:
1800: switch (c->exit_type) {
1801: case CLIENT_EXIT_RETURN:
1.367 nicm 1802: if (c->exit_message != NULL)
1.357 nicm 1803: msize = strlen(c->exit_message) + 1;
1.367 nicm 1804: else
1805: msize = 0;
1806: size = (sizeof c->retval) + msize;
1.357 nicm 1807: data = xmalloc(size);
1808: memcpy(data, &c->retval, sizeof c->retval);
1809: if (c->exit_message != NULL)
1810: memcpy(data + sizeof c->retval, c->exit_message, msize);
1811: proc_send(c->peer, MSG_EXIT, -1, data, size);
1812: free(data);
1.352 nicm 1813: break;
1814: case CLIENT_EXIT_SHUTDOWN:
1815: proc_send(c->peer, MSG_SHUTDOWN, -1, NULL, 0);
1816: break;
1817: case CLIENT_EXIT_DETACH:
1818: proc_send(c->peer, c->exit_msgtype, -1, name, strlen(name) + 1);
1819: break;
1820: }
1821: free(c->exit_session);
1.357 nicm 1822: free(c->exit_message);
1.38 nicm 1823: }
1824:
1.218 nicm 1825: /* Redraw timer callback. */
1826: static void
1827: server_client_redraw_timer(__unused int fd, __unused short events,
1.299 nicm 1828: __unused void *data)
1.218 nicm 1829: {
1830: log_debug("redraw timer fired");
1.366 nicm 1831: }
1832:
1833: /*
1834: * Check if modes need to be updated. Only modes in the current window are
1835: * updated and it is done when the status line is redrawn.
1836: */
1837: static void
1838: server_client_check_modes(struct client *c)
1839: {
1840: struct window *w = c->session->curw->window;
1841: struct window_pane *wp;
1842: struct window_mode_entry *wme;
1843:
1844: if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1845: return;
1846: if (~c->flags & CLIENT_REDRAWSTATUS)
1847: return;
1848: TAILQ_FOREACH(wp, &w->panes, entry) {
1849: wme = TAILQ_FIRST(&wp->modes);
1850: if (wme != NULL && wme->mode->update != NULL)
1851: wme->mode->update(wme);
1852: }
1.218 nicm 1853: }
1854:
1.1 nicm 1855: /* Check for client redraws. */
1.190 nicm 1856: static void
1.1 nicm 1857: server_client_check_redraw(struct client *c)
1858: {
1859: struct session *s = c->session;
1.137 nicm 1860: struct tty *tty = &c->tty;
1.326 nicm 1861: struct window *w = c->session->curw->window;
1.1 nicm 1862: struct window_pane *wp;
1.325 nicm 1863: int needed, flags, mode = tty->mode, new_flags = 0;
1.326 nicm 1864: int redraw;
1865: u_int bit = 0;
1.218 nicm 1866: struct timeval tv = { .tv_usec = 1000 };
1867: static struct event ev;
1868: size_t left;
1.1 nicm 1869:
1.86 nicm 1870: if (c->flags & (CLIENT_CONTROL|CLIENT_SUSPENDED))
1.79 nicm 1871: return;
1.257 nicm 1872: if (c->flags & CLIENT_ALLREDRAWFLAGS) {
1.325 nicm 1873: log_debug("%s: redraw%s%s%s%s%s", c->name,
1.257 nicm 1874: (c->flags & CLIENT_REDRAWWINDOW) ? " window" : "",
1875: (c->flags & CLIENT_REDRAWSTATUS) ? " status" : "",
1.282 nicm 1876: (c->flags & CLIENT_REDRAWBORDERS) ? " borders" : "",
1.325 nicm 1877: (c->flags & CLIENT_REDRAWOVERLAY) ? " overlay" : "",
1878: (c->flags & CLIENT_REDRAWPANES) ? " panes" : "");
1.257 nicm 1879: }
1.218 nicm 1880:
1881: /*
1882: * If there is outstanding data, defer the redraw until it has been
1883: * consumed. We can just add a timer to get out of the event loop and
1884: * end up back here.
1885: */
1886: needed = 0;
1.256 nicm 1887: if (c->flags & CLIENT_ALLREDRAWFLAGS)
1.218 nicm 1888: needed = 1;
1889: else {
1.326 nicm 1890: TAILQ_FOREACH(wp, &w->panes, entry) {
1.218 nicm 1891: if (wp->flags & PANE_REDRAW) {
1892: needed = 1;
1893: break;
1894: }
1895: }
1.325 nicm 1896: if (needed)
1897: new_flags |= CLIENT_REDRAWPANES;
1.218 nicm 1898: }
1.241 nicm 1899: if (needed && (left = EVBUFFER_LENGTH(tty->out)) != 0) {
1900: log_debug("%s: redraw deferred (%zu left)", c->name, left);
1901: if (!evtimer_initialized(&ev))
1902: evtimer_set(&ev, server_client_redraw_timer, NULL);
1903: if (!evtimer_pending(&ev, NULL)) {
1.218 nicm 1904: log_debug("redraw timer started");
1905: evtimer_add(&ev, &tv);
1.241 nicm 1906: }
1.327 nicm 1907:
1908: if (~c->flags & CLIENT_REDRAWWINDOW) {
1.326 nicm 1909: TAILQ_FOREACH(wp, &w->panes, entry) {
1.327 nicm 1910: if (wp->flags & PANE_REDRAW) {
1911: log_debug("%s: pane %%%u needs redraw",
1912: c->name, wp->id);
1.326 nicm 1913: c->redraw_panes |= (1 << bit);
1.327 nicm 1914: }
1.326 nicm 1915: if (++bit == 64) {
1916: /*
1917: * If more that 64 panes, give up and
1918: * just redraw the window.
1919: */
1920: new_flags &= CLIENT_REDRAWPANES;
1921: new_flags |= CLIENT_REDRAWWINDOW;
1922: break;
1923: }
1924: }
1.327 nicm 1925: if (c->redraw_panes != 0)
1926: c->flags |= CLIENT_REDRAWPANES;
1.326 nicm 1927: }
1.325 nicm 1928: c->flags |= new_flags;
1.241 nicm 1929: return;
1930: } else if (needed)
1.218 nicm 1931: log_debug("%s: redraw needed", c->name);
1.79 nicm 1932:
1.219 nicm 1933: flags = tty->flags & (TTY_BLOCK|TTY_FREEZE|TTY_NOCURSOR);
1.326 nicm 1934: tty->flags = (tty->flags & ~(TTY_BLOCK|TTY_FREEZE))|TTY_NOCURSOR;
1.137 nicm 1935:
1.256 nicm 1936: if (~c->flags & CLIENT_REDRAWWINDOW) {
1937: /*
1938: * If not redrawing the entire window, check whether each pane
1939: * needs to be redrawn.
1940: */
1.326 nicm 1941: TAILQ_FOREACH(wp, &w->panes, entry) {
1942: redraw = 0;
1943: if (wp->flags & PANE_REDRAW)
1944: redraw = 1;
1945: else if (c->flags & CLIENT_REDRAWPANES)
1946: redraw = !!(c->redraw_panes & (1 << bit));
1.328 nicm 1947: bit++;
1.326 nicm 1948: if (!redraw)
1949: continue;
1950: log_debug("%s: redrawing pane %%%u", __func__, wp->id);
1951: screen_redraw_pane(c, wp);
1.1 nicm 1952: }
1.331 nicm 1953: c->redraw_panes = 0;
1.325 nicm 1954: c->flags &= ~CLIENT_REDRAWPANES;
1.1 nicm 1955: }
1956:
1.256 nicm 1957: if (c->flags & CLIENT_ALLREDRAWFLAGS) {
1958: if (options_get_number(s->options, "set-titles"))
1959: server_client_set_title(c);
1960: screen_redraw_screen(c);
1961: }
1.1 nicm 1962:
1.326 nicm 1963: tty->flags = (tty->flags & ~TTY_NOCURSOR)|(flags & TTY_NOCURSOR);
1.320 nicm 1964: tty_update_mode(tty, mode, NULL);
1.326 nicm 1965: tty->flags = (tty->flags & ~(TTY_BLOCK|TTY_FREEZE|TTY_NOCURSOR))|flags;
1.1 nicm 1966:
1.256 nicm 1967: c->flags &= ~(CLIENT_ALLREDRAWFLAGS|CLIENT_STATUSFORCE);
1.230 nicm 1968:
1969: if (needed) {
1970: /*
1971: * We would have deferred the redraw unless the output buffer
1972: * was empty, so we can record how many bytes the redraw
1973: * generated.
1974: */
1975: c->redraw = EVBUFFER_LENGTH(tty->out);
1976: log_debug("%s: redraw added %zu bytes", c->name, c->redraw);
1977: }
1.1 nicm 1978: }
1979:
1980: /* Set client title. */
1.190 nicm 1981: static void
1.1 nicm 1982: server_client_set_title(struct client *c)
1983: {
1.128 nicm 1984: struct session *s = c->session;
1985: const char *template;
1986: char *title;
1987: struct format_tree *ft;
1.1 nicm 1988:
1.163 nicm 1989: template = options_get_string(s->options, "set-titles-string");
1.25 nicm 1990:
1.228 nicm 1991: ft = format_create(c, NULL, FORMAT_NONE, 0);
1.128 nicm 1992: format_defaults(ft, c, NULL, NULL, NULL);
1993:
1.269 nicm 1994: title = format_expand_time(ft, template);
1.1 nicm 1995: if (c->title == NULL || strcmp(title, c->title) != 0) {
1.77 nicm 1996: free(c->title);
1.1 nicm 1997: c->title = xstrdup(title);
1998: tty_set_title(&c->tty, c->title);
1999: }
1.77 nicm 2000: free(title);
1.128 nicm 2001:
2002: format_free(ft);
1.1 nicm 2003: }
2004:
2005: /* Dispatch message from client. */
1.190 nicm 2006: static void
1.162 nicm 2007: server_client_dispatch(struct imsg *imsg, void *arg)
1.1 nicm 2008: {
1.300 nicm 2009: struct client *c = arg;
2010: ssize_t datalen;
2011: struct session *s;
1.1 nicm 2012:
1.162 nicm 2013: if (c->flags & CLIENT_DEAD)
2014: return;
2015:
2016: if (imsg == NULL) {
2017: server_client_lost(c);
2018: return;
2019: }
1.1 nicm 2020:
1.162 nicm 2021: datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1.1 nicm 2022:
1.162 nicm 2023: switch (imsg->hdr.type) {
1.370 nicm 2024: case MSG_IDENTIFY_CLIENTPID:
2025: case MSG_IDENTIFY_CWD:
2026: case MSG_IDENTIFY_ENVIRON:
1.329 nicm 2027: case MSG_IDENTIFY_FEATURES:
1.162 nicm 2028: case MSG_IDENTIFY_FLAGS:
1.361 nicm 2029: case MSG_IDENTIFY_LONGFLAGS:
1.370 nicm 2030: case MSG_IDENTIFY_STDIN:
2031: case MSG_IDENTIFY_STDOUT:
1.162 nicm 2032: case MSG_IDENTIFY_TERM:
1.370 nicm 2033: case MSG_IDENTIFY_TERMINFO:
1.162 nicm 2034: case MSG_IDENTIFY_TTYNAME:
2035: case MSG_IDENTIFY_DONE:
2036: server_client_dispatch_identify(c, imsg);
2037: break;
2038: case MSG_COMMAND:
2039: server_client_dispatch_command(c, imsg);
2040: break;
2041: case MSG_RESIZE:
2042: if (datalen != 0)
2043: fatalx("bad MSG_RESIZE size");
1.1 nicm 2044:
1.162 nicm 2045: if (c->flags & CLIENT_CONTROL)
1.1 nicm 2046: break;
1.295 nicm 2047: server_client_update_latest(c);
1.236 nicm 2048: tty_resize(&c->tty);
2049: recalculate_sizes();
1.376 nicm 2050: if (c->overlay_resize == NULL)
2051: server_client_clear_overlay(c);
2052: else
1.380 ! nicm 2053: c->overlay_resize(c, c->overlay_data);
1.236 nicm 2054: server_redraw_client(c);
1.174 nicm 2055: if (c->session != NULL)
1.196 nicm 2056: notify_client("client-resized", c);
1.162 nicm 2057: break;
2058: case MSG_EXITING:
2059: if (datalen != 0)
2060: fatalx("bad MSG_EXITING size");
1.379 nicm 2061: server_client_set_session(c, NULL);
1.162 nicm 2062: tty_close(&c->tty);
2063: proc_send(c->peer, MSG_EXITED, -1, NULL, 0);
2064: break;
2065: case MSG_WAKEUP:
2066: case MSG_UNLOCK:
2067: if (datalen != 0)
2068: fatalx("bad MSG_WAKEUP size");
1.121 nicm 2069:
1.162 nicm 2070: if (!(c->flags & CLIENT_SUSPENDED))
2071: break;
2072: c->flags &= ~CLIENT_SUSPENDED;
1.10 nicm 2073:
1.365 nicm 2074: if (c->fd == -1 || c->session == NULL) /* exited already */
1.1 nicm 2075: break;
1.162 nicm 2076: s = c->session;
1.1 nicm 2077:
1.162 nicm 2078: if (gettimeofday(&c->activity_time, NULL) != 0)
2079: fatal("gettimeofday failed");
2080:
2081: tty_start_tty(&c->tty);
2082: server_redraw_client(c);
2083: recalculate_sizes();
1.184 nicm 2084:
2085: if (s != NULL)
2086: session_update_activity(s, &c->activity_time);
1.162 nicm 2087: break;
2088: case MSG_SHELL:
2089: if (datalen != 0)
2090: fatalx("bad MSG_SHELL size");
1.1 nicm 2091:
1.162 nicm 2092: server_client_dispatch_shell(c);
2093: break;
1.300 nicm 2094: case MSG_WRITE_READY:
1.369 nicm 2095: file_write_ready(&c->files, imsg);
1.300 nicm 2096: break;
2097: case MSG_READ:
1.369 nicm 2098: file_read_data(&c->files, imsg);
1.300 nicm 2099: break;
2100: case MSG_READ_DONE:
1.369 nicm 2101: file_read_done(&c->files, imsg);
1.300 nicm 2102: break;
1.1 nicm 2103: }
2104: }
2105:
1.194 nicm 2106: /* Callback when command is done. */
2107: static enum cmd_retval
1.195 nicm 2108: server_client_command_done(struct cmdq_item *item, __unused void *data)
1.194 nicm 2109: {
1.316 nicm 2110: struct client *c = cmdq_get_client(item);
1.194 nicm 2111:
2112: if (~c->flags & CLIENT_ATTACHED)
2113: c->flags |= CLIENT_EXIT;
1.352 nicm 2114: else if (~c->flags & CLIENT_EXIT)
1.314 nicm 2115: tty_send_requests(&c->tty);
1.194 nicm 2116: return (CMD_RETURN_NORMAL);
2117: }
2118:
1.1 nicm 2119: /* Handle command message. */
1.190 nicm 2120: static void
1.162 nicm 2121: server_client_dispatch_command(struct client *c, struct imsg *imsg)
1.1 nicm 2122: {
1.300 nicm 2123: struct msg_command data;
1.108 nicm 2124: char *buf;
2125: size_t len;
2126: int argc;
2127: char **argv, *cause;
1.285 nicm 2128: struct cmd_parse_result *pr;
1.246 nicm 2129:
2130: if (c->flags & CLIENT_EXIT)
2131: return;
1.108 nicm 2132:
2133: if (imsg->hdr.len - IMSG_HEADER_SIZE < sizeof data)
2134: fatalx("bad MSG_COMMAND size");
2135: memcpy(&data, imsg->data, sizeof data);
2136:
1.124 nicm 2137: buf = (char *)imsg->data + sizeof data;
1.108 nicm 2138: len = imsg->hdr.len - IMSG_HEADER_SIZE - sizeof data;
2139: if (len > 0 && buf[len - 1] != '\0')
2140: fatalx("bad MSG_COMMAND string");
2141:
2142: argc = data.argc;
2143: if (cmd_unpack_argv(buf, len, argc, &argv) != 0) {
1.194 nicm 2144: cause = xstrdup("command too long");
1.1 nicm 2145: goto error;
2146: }
2147:
2148: if (argc == 0) {
2149: argc = 1;
2150: argv = xcalloc(1, sizeof *argv);
2151: *argv = xstrdup("new-session");
2152: }
2153:
1.285 nicm 2154: pr = cmd_parse_from_arguments(argc, argv, NULL);
2155: switch (pr->status) {
2156: case CMD_PARSE_EMPTY:
2157: cause = xstrdup("empty command");
1.1 nicm 2158: goto error;
1.285 nicm 2159: case CMD_PARSE_ERROR:
2160: cause = pr->error;
2161: goto error;
2162: case CMD_PARSE_SUCCESS:
2163: break;
1.1 nicm 2164: }
2165: cmd_free_argv(argc, argv);
2166:
1.318 nicm 2167: cmdq_append(c, cmdq_get_command(pr->cmdlist, NULL));
1.194 nicm 2168: cmdq_append(c, cmdq_get_callback(server_client_command_done, NULL));
1.285 nicm 2169:
2170: cmd_list_free(pr->cmdlist);
1.1 nicm 2171: return;
2172:
2173: error:
1.285 nicm 2174: cmd_free_argv(argc, argv);
2175:
1.284 nicm 2176: cmdq_append(c, cmdq_get_error(cause));
2177: free(cause);
1.88 nicm 2178:
1.36 nicm 2179: c->flags |= CLIENT_EXIT;
1.1 nicm 2180: }
2181:
2182: /* Handle identify message. */
1.190 nicm 2183: static void
1.162 nicm 2184: server_client_dispatch_identify(struct client *c, struct imsg *imsg)
1.1 nicm 2185: {
1.165 nicm 2186: const char *data, *home;
1.232 nicm 2187: size_t datalen;
1.329 nicm 2188: int flags, feat;
1.361 nicm 2189: uint64_t longflags;
1.216 nicm 2190: char *name;
1.109 nicm 2191:
2192: if (c->flags & CLIENT_IDENTIFIED)
2193: fatalx("out-of-order identify message");
2194:
2195: data = imsg->data;
2196: datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
2197:
2198: switch (imsg->hdr.type) {
1.329 nicm 2199: case MSG_IDENTIFY_FEATURES:
2200: if (datalen != sizeof feat)
2201: fatalx("bad MSG_IDENTIFY_FEATURES size");
2202: memcpy(&feat, data, sizeof feat);
2203: c->term_features |= feat;
2204: log_debug("client %p IDENTIFY_FEATURES %s", c,
2205: tty_get_features(feat));
2206: break;
1.109 nicm 2207: case MSG_IDENTIFY_FLAGS:
2208: if (datalen != sizeof flags)
2209: fatalx("bad MSG_IDENTIFY_FLAGS size");
2210: memcpy(&flags, data, sizeof flags);
2211: c->flags |= flags;
1.158 nicm 2212: log_debug("client %p IDENTIFY_FLAGS %#x", c, flags);
1.361 nicm 2213: break;
2214: case MSG_IDENTIFY_LONGFLAGS:
2215: if (datalen != sizeof longflags)
2216: fatalx("bad MSG_IDENTIFY_LONGFLAGS size");
2217: memcpy(&longflags, data, sizeof longflags);
2218: c->flags |= longflags;
2219: log_debug("client %p IDENTIFY_LONGFLAGS %#llx", c,
2220: (unsigned long long)longflags);
1.109 nicm 2221: break;
2222: case MSG_IDENTIFY_TERM:
1.110 nicm 2223: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 2224: fatalx("bad MSG_IDENTIFY_TERM string");
1.329 nicm 2225: if (*data == '\0')
2226: c->term_name = xstrdup("unknown");
2227: else
2228: c->term_name = xstrdup(data);
1.158 nicm 2229: log_debug("client %p IDENTIFY_TERM %s", c, data);
1.370 nicm 2230: break;
2231: case MSG_IDENTIFY_TERMINFO:
2232: if (datalen == 0 || data[datalen - 1] != '\0')
2233: fatalx("bad MSG_IDENTIFY_TERMINFO string");
2234: c->term_caps = xreallocarray(c->term_caps, c->term_ncaps + 1,
2235: sizeof *c->term_caps);
2236: c->term_caps[c->term_ncaps++] = xstrdup(data);
2237: log_debug("client %p IDENTIFY_TERMINFO %s", c, data);
1.109 nicm 2238: break;
2239: case MSG_IDENTIFY_TTYNAME:
1.110 nicm 2240: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 2241: fatalx("bad MSG_IDENTIFY_TTYNAME string");
2242: c->ttyname = xstrdup(data);
1.158 nicm 2243: log_debug("client %p IDENTIFY_TTYNAME %s", c, data);
1.109 nicm 2244: break;
2245: case MSG_IDENTIFY_CWD:
1.155 nicm 2246: if (datalen == 0 || data[datalen - 1] != '\0')
2247: fatalx("bad MSG_IDENTIFY_CWD string");
1.165 nicm 2248: if (access(data, X_OK) == 0)
2249: c->cwd = xstrdup(data);
2250: else if ((home = find_home()) != NULL)
2251: c->cwd = xstrdup(home);
2252: else
2253: c->cwd = xstrdup("/");
1.158 nicm 2254: log_debug("client %p IDENTIFY_CWD %s", c, data);
1.109 nicm 2255: break;
2256: case MSG_IDENTIFY_STDIN:
2257: if (datalen != 0)
2258: fatalx("bad MSG_IDENTIFY_STDIN size");
2259: c->fd = imsg->fd;
1.158 nicm 2260: log_debug("client %p IDENTIFY_STDIN %d", c, imsg->fd);
1.109 nicm 2261: break;
1.351 nicm 2262: case MSG_IDENTIFY_STDOUT:
2263: if (datalen != 0)
2264: fatalx("bad MSG_IDENTIFY_STDOUT size");
2265: c->out_fd = imsg->fd;
2266: log_debug("client %p IDENTIFY_STDOUT %d", c, imsg->fd);
2267: break;
1.109 nicm 2268: case MSG_IDENTIFY_ENVIRON:
1.110 nicm 2269: if (datalen == 0 || data[datalen - 1] != '\0')
1.109 nicm 2270: fatalx("bad MSG_IDENTIFY_ENVIRON string");
2271: if (strchr(data, '=') != NULL)
1.312 nicm 2272: environ_put(c->environ, data, 0);
1.158 nicm 2273: log_debug("client %p IDENTIFY_ENVIRON %s", c, data);
1.143 nicm 2274: break;
2275: case MSG_IDENTIFY_CLIENTPID:
2276: if (datalen != sizeof c->pid)
2277: fatalx("bad MSG_IDENTIFY_CLIENTPID size");
2278: memcpy(&c->pid, data, sizeof c->pid);
1.158 nicm 2279: log_debug("client %p IDENTIFY_CLIENTPID %ld", c, (long)c->pid);
1.109 nicm 2280: break;
2281: default:
2282: break;
2283: }
2284:
2285: if (imsg->hdr.type != MSG_IDENTIFY_DONE)
2286: return;
2287: c->flags |= CLIENT_IDENTIFIED;
1.75 nicm 2288:
1.216 nicm 2289: if (*c->ttyname != '\0')
2290: name = xstrdup(c->ttyname);
2291: else
2292: xasprintf(&name, "client-%ld", (long)c->pid);
2293: c->name = name;
2294: log_debug("client %p name is %s", c, c->name);
2295:
1.362 nicm 2296: if (c->flags & CLIENT_CONTROL)
1.300 nicm 2297: control_start(c);
1.351 nicm 2298: else if (c->fd != -1) {
1.348 nicm 2299: if (tty_init(&c->tty, c) != 0) {
1.288 nicm 2300: close(c->fd);
2301: c->fd = -1;
2302: } else {
2303: tty_resize(&c->tty);
2304: c->flags |= CLIENT_TERMINAL;
2305: }
1.351 nicm 2306: close(c->out_fd);
2307: c->out_fd = -1;
1.75 nicm 2308: }
1.1 nicm 2309:
1.288 nicm 2310: /*
1.362 nicm 2311: * If this is the first client, load configuration files. Any later
2312: * clients are allowed to continue with their command even if the
2313: * config has not been loaded - they might have been run from inside it
1.288 nicm 2314: */
2315: if ((~c->flags & CLIENT_EXIT) &&
1.362 nicm 2316: !cfg_finished &&
2317: c == TAILQ_FIRST(&clients))
1.288 nicm 2318: start_cfg();
1.1 nicm 2319: }
2320:
2321: /* Handle shell message. */
1.190 nicm 2322: static void
1.162 nicm 2323: server_client_dispatch_shell(struct client *c)
1.1 nicm 2324: {
1.107 nicm 2325: const char *shell;
1.25 nicm 2326:
1.163 nicm 2327: shell = options_get_string(global_s_options, "default-shell");
1.308 nicm 2328: if (!checkshell(shell))
1.1 nicm 2329: shell = _PATH_BSHELL;
1.240 nicm 2330: proc_send(c->peer, MSG_SHELL, -1, shell, strlen(shell) + 1);
1.25 nicm 2331:
1.162 nicm 2332: proc_kill_peer(c->peer);
1.213 nicm 2333: }
2334:
2335: /* Get client working directory. */
2336: const char *
1.250 nicm 2337: server_client_get_cwd(struct client *c, struct session *s)
1.213 nicm 2338: {
1.250 nicm 2339: const char *home;
1.213 nicm 2340:
1.265 nicm 2341: if (!cfg_finished && cfg_client != NULL)
2342: return (cfg_client->cwd);
1.213 nicm 2343: if (c != NULL && c->session == NULL && c->cwd != NULL)
2344: return (c->cwd);
1.250 nicm 2345: if (s != NULL && s->cwd != NULL)
2346: return (s->cwd);
1.213 nicm 2347: if (c != NULL && (s = c->session) != NULL && s->cwd != NULL)
2348: return (s->cwd);
1.250 nicm 2349: if ((home = find_home()) != NULL)
2350: return (home);
2351: return ("/");
1.337 nicm 2352: }
2353:
1.355 nicm 2354: /* Get control client flags. */
2355: static uint64_t
2356: server_client_control_flags(struct client *c, const char *next)
2357: {
2358: if (strcmp(next, "pause-after") == 0) {
2359: c->pause_age = 0;
2360: return (CLIENT_CONTROL_PAUSEAFTER);
2361: }
2362: if (sscanf(next, "pause-after=%u", &c->pause_age) == 1) {
2363: c->pause_age *= 1000;
2364: return (CLIENT_CONTROL_PAUSEAFTER);
2365: }
2366: if (strcmp(next, "no-output") == 0)
2367: return (CLIENT_CONTROL_NOOUTPUT);
1.358 nicm 2368: if (strcmp(next, "wait-exit") == 0)
2369: return (CLIENT_CONTROL_WAITEXIT);
1.355 nicm 2370: return (0);
2371: }
2372:
1.337 nicm 2373: /* Set client flags. */
2374: void
2375: server_client_set_flags(struct client *c, const char *flags)
2376: {
2377: char *s, *copy, *next;
1.351 nicm 2378: uint64_t flag;
2379: int not;
1.337 nicm 2380:
2381: s = copy = xstrdup (flags);
2382: while ((next = strsep(&s, ",")) != NULL) {
2383: not = (*next == '!');
2384: if (not)
2385: next++;
2386:
1.355 nicm 2387: if (c->flags & CLIENT_CONTROL)
2388: flag = server_client_control_flags(c, next);
2389: else
2390: flag = 0;
1.350 nicm 2391: if (strcmp(next, "read-only") == 0)
1.337 nicm 2392: flag = CLIENT_READONLY;
2393: else if (strcmp(next, "ignore-size") == 0)
2394: flag = CLIENT_IGNORESIZE;
1.341 nicm 2395: else if (strcmp(next, "active-pane") == 0)
2396: flag = CLIENT_ACTIVEPANE;
1.350 nicm 2397: if (flag == 0)
1.337 nicm 2398: continue;
2399:
2400: log_debug("client %s set flag %s", c->name, next);
2401: if (not)
2402: c->flags &= ~flag;
2403: else
2404: c->flags |= flag;
1.346 nicm 2405: if (flag == CLIENT_CONTROL_NOOUTPUT)
1.352 nicm 2406: control_reset_offsets(c);
1.337 nicm 2407: }
2408: free(copy);
1.358 nicm 2409: proc_send(c->peer, MSG_FLAGS, -1, &c->flags, sizeof c->flags);
1.337 nicm 2410: }
2411:
1.340 nicm 2412: /* Get client flags. This is only flags useful to show to users. */
1.337 nicm 2413: const char *
2414: server_client_get_flags(struct client *c)
2415: {
1.355 nicm 2416: static char s[256];
2417: char tmp[32];
1.337 nicm 2418:
2419: *s = '\0';
2420: if (c->flags & CLIENT_ATTACHED)
2421: strlcat(s, "attached,", sizeof s);
1.368 nicm 2422: if (c->flags & CLIENT_FOCUSED)
2423: strlcat(s, "focused,", sizeof s);
1.337 nicm 2424: if (c->flags & CLIENT_CONTROL)
2425: strlcat(s, "control-mode,", sizeof s);
2426: if (c->flags & CLIENT_IGNORESIZE)
2427: strlcat(s, "ignore-size,", sizeof s);
2428: if (c->flags & CLIENT_CONTROL_NOOUTPUT)
2429: strlcat(s, "no-output,", sizeof s);
1.358 nicm 2430: if (c->flags & CLIENT_CONTROL_WAITEXIT)
2431: strlcat(s, "wait-exit,", sizeof s);
1.355 nicm 2432: if (c->flags & CLIENT_CONTROL_PAUSEAFTER) {
2433: xsnprintf(tmp, sizeof tmp, "pause-after=%u,",
2434: c->pause_age / 1000);
2435: strlcat(s, tmp, sizeof s);
2436: }
1.337 nicm 2437: if (c->flags & CLIENT_READONLY)
2438: strlcat(s, "read-only,", sizeof s);
1.341 nicm 2439: if (c->flags & CLIENT_ACTIVEPANE)
2440: strlcat(s, "active-pane,", sizeof s);
1.337 nicm 2441: if (c->flags & CLIENT_SUSPENDED)
2442: strlcat(s, "suspended,", sizeof s);
2443: if (c->flags & CLIENT_UTF8)
2444: strlcat(s, "UTF-8,", sizeof s);
2445: if (*s != '\0')
2446: s[strlen(s) - 1] = '\0';
2447: return (s);
1.341 nicm 2448: }
2449:
2450: /* Get client window. */
2451: static struct client_window *
2452: server_client_get_client_window(struct client *c, u_int id)
2453: {
2454: struct client_window cw = { .window = id };
2455:
2456: return (RB_FIND(client_windows, &c->windows, &cw));
2457: }
2458:
2459: /* Get client active pane. */
2460: struct window_pane *
2461: server_client_get_pane(struct client *c)
2462: {
2463: struct session *s = c->session;
2464: struct client_window *cw;
2465:
2466: if (s == NULL)
2467: return (NULL);
2468:
2469: if (~c->flags & CLIENT_ACTIVEPANE)
2470: return (s->curw->window->active);
2471: cw = server_client_get_client_window(c, s->curw->window->id);
2472: if (cw == NULL)
2473: return (s->curw->window->active);
2474: return (cw->pane);
2475: }
2476:
2477: /* Set client active pane. */
2478: void
2479: server_client_set_pane(struct client *c, struct window_pane *wp)
2480: {
2481: struct session *s = c->session;
2482: struct client_window *cw;
2483:
2484: if (s == NULL)
2485: return;
2486:
2487: cw = server_client_get_client_window(c, s->curw->window->id);
2488: if (cw == NULL) {
2489: cw = xcalloc(1, sizeof *cw);
2490: cw->window = s->curw->window->id;
2491: RB_INSERT(client_windows, &c->windows, cw);
2492: }
2493: cw->pane = wp;
2494: log_debug("%s pane now %%%u", c->name, wp->id);
2495: }
2496:
2497: /* Remove pane from client lists. */
2498: void
2499: server_client_remove_pane(struct window_pane *wp)
2500: {
2501: struct client *c;
2502: struct window *w = wp->window;
2503: struct client_window *cw;
2504:
2505: TAILQ_FOREACH(c, &clients, entry) {
2506: cw = server_client_get_client_window(c, w->id);
2507: if (cw != NULL && cw->pane == wp) {
2508: RB_REMOVE(client_windows, &c->windows, cw);
2509: free(cw);
2510: }
2511: }
1.1 nicm 2512: }