tmux  3.2a
About: tmux is a terminal multiplexer that lets you switch easily between several programs in one terminal.
  Fossies Dox: tmux-3.2a.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

cmd-display-panes.c
Go to the documentation of this file.
1 /* $OpenBSD$ */
2 
3 /*
4  * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "tmux.h"
25 
26 /*
27  * Display panes on a client.
28  */
29 
30 static enum cmd_retval cmd_display_panes_exec(struct cmd *,
31  struct cmdq_item *);
32 
33 const struct cmd_entry cmd_display_panes_entry = {
34  .name = "display-panes",
35  .alias = "displayp",
36 
37  .args = { "bd:Nt:", 0, 1 },
38  .usage = "[-bN] [-d duration] " CMD_TARGET_CLIENT_USAGE " [template]",
39 
42 };
43 
45  struct cmdq_item *item;
46  char *command;
47 };
48 
49 static void
51  struct window_pane *wp)
52 {
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 fgc, bgc;
59  u_int pane, idx, px, py, i, j, xoff, yoff, sx, sy;
60  int colour, active_colour;
61  char buf[16], lbuf[16], rbuf[16], *ptr;
62  size_t len, llen, rlen;
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  }
106 
107  if (ctx->statustop)
108  yoff += ctx->statuslines;
109  px = sx / 2;
110  py = sy / 2;
111 
112  if (window_pane_index(wp, &pane) != 0)
113  fatalx("index not found");
114  len = xsnprintf(buf, sizeof buf, "%u", pane);
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  memcpy(&fgc, &grid_default_cell, sizeof fgc);
122  memcpy(&bgc, &grid_default_cell, sizeof bgc);
123  if (w->active == wp) {
124  fgc.fg = active_colour;
125  bgc.bg = active_colour;
126  } else {
127  fgc.fg = colour;
128  bgc.bg = colour;
129  }
130 
131  rlen = xsnprintf(rbuf, sizeof rbuf, "%ux%u", wp->sx, wp->sy);
132  if (pane > 9 && pane < 35)
133  llen = xsnprintf(lbuf, sizeof lbuf, "%c", 'a' + (pane - 10));
134  else
135  llen = 0;
136 
137  if (sx < len * 6 || sy < 5) {
138  tty_attributes(tty, &fgc, &grid_default_cell, NULL);
139  if (sx >= len + llen + 1) {
140  len += llen + 1;
141  tty_cursor(tty, xoff + px - len / 2, yoff + py);
142  tty_putn(tty, buf, len, len);
143  tty_putn(tty, " ", 1, 1);
144  tty_putn(tty, lbuf, llen, llen);
145  } else {
146  tty_cursor(tty, xoff + px - len / 2, yoff + py);
147  tty_putn(tty, buf, len, len);
148  }
149  goto out;
150  }
151 
152  px -= len * 3;
153  py -= 2;
154 
155  tty_attributes(tty, &bgc, &grid_default_cell, NULL);
156  for (ptr = buf; *ptr != '\0'; ptr++) {
157  if (*ptr < '0' || *ptr > '9')
158  continue;
159  idx = *ptr - '0';
160 
161  for (j = 0; j < 5; j++) {
162  for (i = px; i < px + 5; i++) {
163  tty_cursor(tty, xoff + i, yoff + py + j);
164  if (window_clock_table[idx][j][i - px])
165  tty_putc(tty, ' ');
166  }
167  }
168  px += 6;
169  }
170 
171  if (sy <= 6)
172  goto out;
173  tty_attributes(tty, &fgc, &grid_default_cell, NULL);
174  if (rlen != 0 && sx >= rlen) {
175  tty_cursor(tty, xoff + sx - rlen, yoff);
176  tty_putn(tty, rbuf, rlen, rlen);
177  }
178  if (llen != 0) {
179  tty_cursor(tty, xoff + sx / 2 + len * 3 - llen - 1,
180  yoff + py + 5);
181  tty_putn(tty, lbuf, llen, llen);
182  }
183 
184 out:
185  tty_cursor(tty, 0, 0);
186 }
187 
188 static void
190 {
191  struct window *w = c->session->curw->window;
192  struct window_pane *wp;
193 
194  log_debug("%s: %s @%u", __func__, c->name, w->id);
195 
196  TAILQ_FOREACH(wp, &w->panes, entry) {
197  if (window_pane_visible(wp))
199  }
200 }
201 
202 static void
204 {
205  struct cmd_display_panes_data *cdata = c->overlay_data;
206 
207  if (cdata->item != NULL)
208  cmdq_continue(cdata->item);
209  free(cdata->command);
210  free(cdata);
211 }
212 
213 static int
214 cmd_display_panes_key(struct client *c, struct key_event *event)
215 {
216  struct cmd_display_panes_data *cdata = c->overlay_data;
217  char *cmd, *expanded, *error;
218  struct window *w = c->session->curw->window;
219  struct window_pane *wp;
221  u_int index;
222  key_code key;
223 
224  if (event->key >= '0' && event->key <= '9')
225  index = event->key - '0';
226  else if ((event->key & KEYC_MASK_MODIFIERS) == 0) {
227  key = (event->key & KEYC_MASK_KEY);
228  if (key >= 'a' && key <= 'z')
229  index = 10 + (key - 'a');
230  else
231  return (-1);
232  } else
233  return (-1);
234 
235  wp = window_pane_at_index(w, index);
236  if (wp == NULL)
237  return (1);
238  window_unzoom(w);
239 
240  xasprintf(&expanded, "%%%u", wp->id);
241  cmd = cmd_template_replace(cdata->command, expanded, 1);
242 
243  status = cmd_parse_and_append(cmd, NULL, c, NULL, &error);
244  if (status == CMD_PARSE_ERROR) {
245  cmdq_append(c, cmdq_get_error(error));
246  free(error);
247  }
248 
249  free(cmd);
250  free(expanded);
251  return (1);
252 }
253 
254 static enum cmd_retval
255 cmd_display_panes_exec(struct cmd *self, struct cmdq_item *item)
256 {
257  struct args *args = cmd_get_args(self);
258  struct client *tc = cmdq_get_target_client(item);
259  struct session *s = tc->session;
260  u_int delay;
261  char *cause;
262  struct cmd_display_panes_data *cdata;
263 
264  if (tc->overlay_draw != NULL)
265  return (CMD_RETURN_NORMAL);
266 
267  if (args_has(args, 'd')) {
268  delay = args_strtonum(args, 'd', 0, UINT_MAX, &cause);
269  if (cause != NULL) {
270  cmdq_error(item, "delay %s", cause);
271  free(cause);
272  return (CMD_RETURN_ERROR);
273  }
274  } else
275  delay = options_get_number(s->options, "display-panes-time");
276 
277  cdata = xmalloc(sizeof *cdata);
278  if (args->argc != 0)
279  cdata->command = xstrdup(args->argv[0]);
280  else
281  cdata->command = xstrdup("select-pane -t '%%'");
282  if (args_has(args, 'b'))
283  cdata->item = NULL;
284  else
285  cdata->item = item;
286 
287  if (args_has(args, 'N')) {
288  server_client_set_overlay(tc, delay, NULL, NULL,
290  cdata);
291  } else {
292  server_client_set_overlay(tc, delay, NULL, NULL,
294  cmd_display_panes_free, cdata);
295  }
296 
297  if (args_has(args, 'b'))
298  return (CMD_RETURN_NORMAL);
299  return (CMD_RETURN_WAIT);
300 }
int args_has(struct args *args, u_char flag)
Definition: arguments.c:259
long long args_strtonum(struct args *args, u_char flag, long long minval, long long maxval, char **cause)
Definition: arguments.c:355
static void cmd_display_panes_draw_pane(struct screen_redraw_ctx *ctx, struct window_pane *wp)
static void cmd_display_panes_free(struct client *c)
static enum cmd_retval cmd_display_panes_exec(struct cmd *, struct cmdq_item *)
static int cmd_display_panes_key(struct client *c, struct key_event *event)
const struct cmd_entry cmd_display_panes_entry
static void cmd_display_panes_draw(struct client *c, struct screen_redraw_ctx *ctx)
enum cmd_parse_status cmd_parse_and_append(const char *s, struct cmd_parse_input *pi, struct client *c, struct cmdq_state *state, char **error)
Definition: cmd-parse.c:641
struct client * cmdq_get_target_client(struct cmdq_item *item)
Definition: cmd-queue.c:157
struct cmdq_item * cmdq_get_error(const char *error)
Definition: cmd-queue.c:671
struct cmdq_item * cmdq_append(struct client *c, struct cmdq_item *item)
Definition: cmd-queue.c:288
void cmdq_continue(struct cmdq_item *item)
Definition: cmd-queue.c:434
void cmdq_error(struct cmdq_item *item, const char *fmt,...)
Definition: cmd-queue.c:833
struct args * cmd_get_args(struct cmd *cmd)
Definition: cmd.c:393
char * cmd_template_replace(const char *template, const char *s, int idx)
Definition: cmd.c:778
const struct grid_cell grid_default_cell
Definition: grid.c:39
key_code key
Definition: key-string.c:32
void fatalx(const char *msg,...)
Definition: log.c:159
void log_debug(const char *msg,...)
Definition: log.c:130
long long options_get_number(struct options *oo, const char *name)
Definition: options.c:699
void server_client_set_overlay(struct client *c, u_int delay, overlay_check_cb checkcb, overlay_mode_cb modecb, overlay_draw_cb drawcb, overlay_key_cb keycb, overlay_free_cb freecb, void *data)
Definition: server-client.c:90
Definition: tmux.h:1435
int argc
Definition: tmux.h:1437
char ** argv
Definition: tmux.h:1438
Definition: tmux.h:1608
struct tty tty
Definition: tmux.h:1640
void * overlay_data
Definition: tmux.h:1757
const char * name
Definition: tmux.h:1609
struct session * session
Definition: tmux.h:1743
overlay_draw_cb overlay_draw
Definition: tmux.h:1754
struct cmdq_item * item
Definition: tmux.h:1525
const char * name
Definition: tmux.h:1526
Definition: cmd.c:212
int bg
Definition: tmux.h:693
int fg
Definition: tmux.h:692
struct client * c
Definition: tmux.h:866
u_int statuslines
Definition: tmux.h:868
Definition: tmux.h:1179
struct winlink * curw
Definition: tmux.h:1192
struct options * options
Definition: tmux.h:1199
Definition: tmux.h:1304
u_int xoff
Definition: tmux.h:971
u_int yoff
Definition: tmux.h:972
int status
Definition: tmux.h:999
u_int sy
Definition: tmux.h:969
struct bufferevent * event
Definition: tmux.h:1002
struct window * window
Definition: tmux.h:962
u_int sx
Definition: tmux.h:968
u_int id
Definition: tmux.h:959
Definition: tmux.h:1041
u_int id
Definition: tmux.h:1042
struct window_panes panes
Definition: tmux.h:1056
struct window_pane * active
Definition: tmux.h:1054
const char window_clock_table[14][5][5]
Definition: window-clock.c:53
void tty_attributes(struct tty *, const struct grid_cell *, const struct grid_cell *, int *)
Definition: tty.c:2250
cmd_retval
Definition: tmux.h:1475
@ CMD_RETURN_NORMAL
Definition: tmux.h:1477
@ CMD_RETURN_ERROR
Definition: tmux.h:1476
@ CMD_RETURN_WAIT
Definition: tmux.h:1478
#define CMD_TARGET_CLIENT_USAGE
Definition: tmux.h:1861
int window_pane_index(struct window_pane *, u_int *)
Definition: window.c:756
#define CMD_AFTERHOOK
Definition: tmux.h:1541
unsigned long long key_code
Definition: tmux.h:177
void tty_putc(struct tty *, u_char)
Definition: tty.c:563
cmd_parse_status
Definition: tmux.h:1483
@ CMD_PARSE_ERROR
Definition: tmux.h:1485
#define CMD_CLIENT_TFLAG
Definition: tmux.h:1543
int window_pane_visible(struct window_pane *)
Definition: window.c:1189
struct window_pane * window_pane_at_index(struct window *, u_int)
Definition: window.c:718
void tty_cursor(struct tty *, u_int, u_int)
Definition: tty.c:2111
#define KEYC_MASK_MODIFIERS
Definition: tmux.h:136
void tty_putn(struct tty *, const void *, size_t, u_int)
Definition: tty.c:601
int window_unzoom(struct window *)
Definition: window.c:611
#define KEYC_MASK_KEY
Definition: tmux.h:138
void * xmalloc(size_t size)
Definition: xmalloc.c:27
int xsnprintf(char *str, size_t len, const char *fmt,...)
Definition: xmalloc.c:135
int xasprintf(char **ret, const char *fmt,...)
Definition: xmalloc.c:109
char * xstrdup(const char *str)
Definition: xmalloc.c:89