Annotation of src/usr.bin/tmux/cmd-display-panes.c, Revision 1.25
1.25 ! nicm 1: /* $OpenBSD: cmd-display-panes.c,v 1.24 2019/05/07 20:01:41 nicm Exp $ */
1.1 nicm 2:
3: /*
1.12 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>
20:
1.13 nicm 21: #include <stdlib.h>
1.24 nicm 22: #include <string.h>
1.13 nicm 23:
1.1 nicm 24: #include "tmux.h"
25:
26: /*
27: * Display panes on a client.
28: */
29:
1.16 nicm 30: static enum cmd_retval cmd_display_panes_exec(struct cmd *,
31: struct cmdq_item *);
1.13 nicm 32:
1.1 nicm 33: const struct cmd_entry cmd_display_panes_entry = {
1.10 nicm 34: .name = "display-panes",
35: .alias = "displayp",
36:
1.23 nicm 37: .args = { "bd:t:", 0, 1 },
38: .usage = "[-b] [-d duration] " CMD_TARGET_CLIENT_USAGE " [template]",
1.10 nicm 39:
1.14 nicm 40: .flags = CMD_AFTERHOOK,
1.10 nicm 41: .exec = cmd_display_panes_exec
1.1 nicm 42: };
43:
1.24 nicm 44: struct cmd_display_panes_data {
45: struct cmdq_item *item;
46: char *command;
47: };
48:
49: static void
50: cmd_display_panes_draw_pane(struct screen_redraw_ctx *ctx,
51: struct window_pane *wp)
1.1 nicm 52: {
1.24 nicm 53: struct client *c = ctx->c;
54: struct tty *tty = &c->tty;
55: struct session *s = c->session;
56: struct options *oo = s->options;
57: struct window *w = wp->window;
58: struct grid_cell gc;
59: u_int idx, px, py, i, j, xoff, yoff, sx, sy;
60: int colour, active_colour;
61: char buf[16], *ptr;
62: size_t len;
63:
64: if (wp->xoff + wp->sx <= ctx->ox ||
65: wp->xoff >= ctx->ox + ctx->sx ||
66: wp->yoff + wp->sy <= ctx->oy ||
67: wp->yoff >= ctx->oy + ctx->sy)
68: return;
69:
70: if (wp->xoff >= ctx->ox && wp->xoff + wp->sx <= ctx->ox + ctx->sx) {
71: /* All visible. */
72: xoff = wp->xoff - ctx->ox;
73: sx = wp->sx;
74: } else if (wp->xoff < ctx->ox &&
75: wp->xoff + wp->sx > ctx->ox + ctx->sx) {
76: /* Both left and right not visible. */
77: xoff = 0;
78: sx = ctx->sx;
79: } else if (wp->xoff < ctx->ox) {
80: /* Left not visible. */
81: xoff = 0;
82: sx = wp->sx - (ctx->ox - wp->xoff);
83: } else {
84: /* Right not visible. */
85: xoff = wp->xoff - ctx->ox;
86: sx = wp->sx - xoff;
87: }
88: if (wp->yoff >= ctx->oy && wp->yoff + wp->sy <= ctx->oy + ctx->sy) {
89: /* All visible. */
90: yoff = wp->yoff - ctx->oy;
91: sy = wp->sy;
92: } else if (wp->yoff < ctx->oy &&
93: wp->yoff + wp->sy > ctx->oy + ctx->sy) {
94: /* Both top and bottom not visible. */
95: yoff = 0;
96: sy = ctx->sy;
97: } else if (wp->yoff < ctx->oy) {
98: /* Top not visible. */
99: yoff = 0;
100: sy = wp->sy - (ctx->oy - wp->yoff);
101: } else {
102: /* Bottom not visible. */
103: yoff = wp->yoff - ctx->oy;
104: sy = wp->sy - yoff;
105: }
1.19 nicm 106:
1.24 nicm 107: if (ctx->statustop)
108: yoff += ctx->statuslines;
109: px = sx / 2;
110: py = sy / 2;
111:
112: if (window_pane_index(wp, &idx) != 0)
113: fatalx("index not found");
114: len = xsnprintf(buf, sizeof buf, "%u", idx);
115:
116: if (sx < len)
117: return;
118: colour = options_get_number(oo, "display-panes-colour");
119: active_colour = options_get_number(oo, "display-panes-active-colour");
120:
121: if (sx < len * 6 || sy < 5) {
122: tty_cursor(tty, xoff + px - len / 2, yoff + py);
123: goto draw_text;
124: }
1.13 nicm 125:
1.24 nicm 126: px -= len * 3;
127: py -= 2;
1.13 nicm 128:
1.24 nicm 129: memcpy(&gc, &grid_default_cell, sizeof gc);
130: if (w->active == wp)
131: gc.bg = active_colour;
1.13 nicm 132: else
1.24 nicm 133: gc.bg = colour;
134: gc.flags |= GRID_FLAG_NOPALETTE;
135:
136: tty_attributes(tty, &gc, wp);
137: for (ptr = buf; *ptr != '\0'; ptr++) {
138: if (*ptr < '0' || *ptr > '9')
139: continue;
140: idx = *ptr - '0';
141:
142: for (j = 0; j < 5; j++) {
143: for (i = px; i < px + 5; i++) {
144: tty_cursor(tty, xoff + i, yoff + py + j);
145: if (window_clock_table[idx][j][i - px])
146: tty_putc(tty, ' ');
147: }
148: }
149: px += 6;
150: }
151:
152: len = xsnprintf(buf, sizeof buf, "%ux%u", wp->sx, wp->sy);
153: if (sx < len || sy < 6)
154: return;
155: tty_cursor(tty, xoff + sx - len, yoff);
156:
157: draw_text:
158: memcpy(&gc, &grid_default_cell, sizeof gc);
159: if (w->active == wp)
160: gc.fg = active_colour;
1.23 nicm 161: else
1.24 nicm 162: gc.fg = colour;
163: gc.flags |= GRID_FLAG_NOPALETTE;
1.13 nicm 164:
1.24 nicm 165: tty_attributes(tty, &gc, wp);
166: tty_puts(tty, buf);
167:
168: tty_cursor(tty, 0, 0);
169: }
170:
171: static void
172: cmd_display_panes_draw(struct client *c, struct screen_redraw_ctx *ctx)
173: {
174: struct window *w = c->session->curw->window;
175: struct window_pane *wp;
176:
177: log_debug("%s: %s @%u", __func__, c->name, w->id);
1.1 nicm 178:
1.24 nicm 179: TAILQ_FOREACH(wp, &w->panes, entry) {
180: if (window_pane_visible(wp))
181: cmd_display_panes_draw_pane(ctx, wp);
182: }
1.13 nicm 183: }
184:
1.15 nicm 185: static enum cmd_retval
1.16 nicm 186: cmd_display_panes_error(struct cmdq_item *item, void *data)
1.15 nicm 187: {
188: char *error = data;
189:
1.16 nicm 190: cmdq_error(item, "%s", error);
1.15 nicm 191: free(error);
192:
193: return (CMD_RETURN_NORMAL);
194: }
195:
1.13 nicm 196: static void
1.24 nicm 197: cmd_display_panes_free(struct client *c)
198: {
199: struct cmd_display_panes_data *cdata = c->overlay_data;
200:
201: if (cdata->item != NULL)
202: cdata->item->flags &= ~CMDQ_WAITING;
203: free(cdata->command);
204: free(cdata);
205: }
206:
207: static int
208: cmd_display_panes_key(struct client *c, struct key_event *event)
1.13 nicm 209: {
1.24 nicm 210: struct cmd_display_panes_data *cdata = c->overlay_data;
211: struct cmd_list *cmdlist;
212: struct cmdq_item *new_item;
213: char *cmd, *expanded, *cause;
214: struct window *w = c->session->curw->window;
215: struct window_pane *wp;
1.13 nicm 216:
1.24 nicm 217: if (event->key < '0' || event->key > '9')
218: return (1);
219:
220: wp = window_pane_at_index(w, event->key - '0');
1.15 nicm 221: if (wp == NULL)
1.24 nicm 222: return (1);
223: window_unzoom(w);
1.21 nicm 224:
1.15 nicm 225: xasprintf(&expanded, "%%%u", wp->id);
1.24 nicm 226: cmd = cmd_template_replace(cdata->command, expanded, 1);
1.15 nicm 227:
1.17 nicm 228: cmdlist = cmd_string_parse(cmd, NULL, 0, &cause);
1.21 nicm 229: if (cmdlist == NULL && cause != NULL)
230: new_item = cmdq_get_callback(cmd_display_panes_error, cause);
231: else if (cmdlist == NULL)
232: new_item = NULL;
233: else {
1.16 nicm 234: new_item = cmdq_get_command(cmdlist, NULL, NULL, 0);
1.15 nicm 235: cmd_list_free(cmdlist);
236: }
1.23 nicm 237: if (new_item != NULL) {
1.24 nicm 238: if (cdata->item != NULL)
239: cmdq_insert_after(cdata->item, new_item);
1.23 nicm 240: else
241: cmdq_append(c, new_item);
242: }
1.15 nicm 243:
244: free(cmd);
245: free(expanded);
1.24 nicm 246: return (1);
247: }
248:
249: static enum cmd_retval
250: cmd_display_panes_exec(struct cmd *self, struct cmdq_item *item)
251: {
252: struct args *args = self->args;
253: struct client *c;
254: struct session *s;
255: u_int delay;
256: char *cause;
257: struct cmd_display_panes_data *cdata;
258:
259: if ((c = cmd_find_client(item, args_get(args, 't'), 0)) == NULL)
260: return (CMD_RETURN_ERROR);
261: s = c->session;
262:
263: if (c->overlay_draw != NULL)
264: return (CMD_RETURN_NORMAL);
1.13 nicm 265:
1.24 nicm 266: if (args_has(args, 'd')) {
267: delay = args_strtonum(args, 'd', 0, UINT_MAX, &cause);
268: if (cause != NULL) {
269: cmdq_error(item, "delay %s", cause);
270: free(cause);
271: return (CMD_RETURN_ERROR);
272: }
273: } else
274: delay = options_get_number(s->options, "display-panes-time");
275:
276: cdata = xmalloc(sizeof *cdata);
277: if (args->argc != 0)
278: cdata->command = xstrdup(args->argv[0]);
279: else
280: cdata->command = xstrdup("select-pane -t '%%'");
281: if (args_has(args, 'b'))
282: cdata->item = NULL;
283: else
284: cdata->item = item;
1.21 nicm 285:
1.24 nicm 286: server_client_set_overlay(c, delay, cmd_display_panes_draw,
287: cmd_display_panes_key, cmd_display_panes_free, cdata);
1.21 nicm 288:
1.24 nicm 289: if (args_has(args, 'b'))
290: return (CMD_RETURN_NORMAL);
291: return (CMD_RETURN_WAIT);
1.1 nicm 292: }