4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
25 int screen_redraw_cell_border1(struct window_pane
*, u_int
, u_int
);
26 int screen_redraw_cell_border(struct client
*, u_int
, u_int
);
27 int screen_redraw_check_cell(struct client
*, u_int
, u_int
);
28 void screen_redraw_draw_number(struct client
*, struct window_pane
*);
31 #define CELL_LEFTRIGHT 1
32 #define CELL_TOPBOTTOM 2
33 #define CELL_TOPLEFT 3
34 #define CELL_TOPRIGHT 4
35 #define CELL_BOTTOMLEFT 5
36 #define CELL_BOTTOMRIGHT 6
37 #define CELL_TOPJOIN 7
38 #define CELL_BOTTOMJOIN 8
39 #define CELL_LEFTJOIN 9
40 #define CELL_RIGHTJOIN 10
42 #define CELL_OUTSIDE 12
44 /* Check if cell is on the border of a particular pane. */
46 screen_redraw_cell_border1(struct window_pane
*wp
, u_int px
, u_int py
)
49 if (px
>= wp
->xoff
&& px
< wp
->xoff
+ wp
->sx
&&
50 py
>= wp
->yoff
&& py
< wp
->yoff
+ wp
->sy
)
53 /* Left/right borders. */
54 if ((wp
->yoff
== 0 || py
>= wp
->yoff
- 1) && py
<= wp
->yoff
+ wp
->sy
) {
55 if (wp
->xoff
!= 0 && px
== wp
->xoff
- 1)
57 if (px
== wp
->xoff
+ wp
->sx
)
61 /* Top/bottom borders. */
62 if ((wp
->xoff
== 0 || px
>= wp
->xoff
- 1) && px
<= wp
->xoff
+ wp
->sx
) {
63 if (wp
->yoff
!= 0 && py
== wp
->yoff
- 1)
65 if (py
== wp
->yoff
+ wp
->sy
)
73 /* Check if a cell is on the pane border. */
75 screen_redraw_cell_border(struct client
*c
, u_int px
, u_int py
)
77 struct window
*w
= c
->session
->curw
->window
;
78 struct window_pane
*wp
;
81 /* Check all the panes. */
82 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
83 if (!window_pane_visible(wp
))
85 if ((retval
= screen_redraw_cell_border1(wp
, px
, py
)) != -1)
92 /* Check if cell inside a pane. */
94 screen_redraw_check_cell(struct client
*c
, u_int px
, u_int py
)
96 struct window
*w
= c
->session
->curw
->window
;
97 struct window_pane
*wp
;
100 if (px
> w
->sx
|| py
> w
->sy
)
101 return (CELL_OUTSIDE
);
103 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
104 if (!window_pane_visible(wp
))
107 /* If outside the pane and its border, skip it. */
108 if ((wp
->xoff
!= 0 && px
< wp
->xoff
- 1) ||
109 px
> wp
->xoff
+ wp
->sx
||
110 (wp
->yoff
!= 0 && py
< wp
->yoff
- 1) ||
111 py
> wp
->yoff
+ wp
->sy
)
114 /* If definitely inside, return so. */
115 if (!screen_redraw_cell_border(c
, px
, py
))
116 return (CELL_INSIDE
);
119 * Construct a bitmask of whether the cells to the left (bit
120 * 4), right, top, and bottom (bit 1) of this cell are borders.
123 if (px
== 0 || screen_redraw_cell_border(c
, px
- 1, py
))
125 if (px
<= w
->sx
&& screen_redraw_cell_border(c
, px
+ 1, py
))
127 if (py
== 0 || screen_redraw_cell_border(c
, px
, py
- 1))
129 if (py
<= w
->sy
&& screen_redraw_cell_border(c
, px
, py
+ 1))
133 * Figure out what kind of border this cell is. Only one bit
134 * set doesn't make sense (can't have a border cell with no
138 case 15: /* 1111, left right top bottom */
140 case 14: /* 1110, left right top */
141 return (CELL_BOTTOMJOIN
);
142 case 13: /* 1101, left right bottom */
143 return (CELL_TOPJOIN
);
144 case 12: /* 1100, left right */
145 return (CELL_TOPBOTTOM
);
146 case 11: /* 1011, left top bottom */
147 return (CELL_RIGHTJOIN
);
148 case 10: /* 1010, left top */
149 return (CELL_BOTTOMRIGHT
);
150 case 9: /* 1001, left bottom */
151 return (CELL_TOPRIGHT
);
152 case 7: /* 0111, right top bottom */
153 return (CELL_LEFTJOIN
);
154 case 6: /* 0110, right top */
155 return (CELL_BOTTOMLEFT
);
156 case 5: /* 0101, right bottom */
157 return (CELL_TOPLEFT
);
158 case 3: /* 0011, top bottom */
159 return (CELL_LEFTRIGHT
);
163 return (CELL_OUTSIDE
);
166 /* Redraw entire screen. */
168 screen_redraw_screen(struct client
*c
, int status_only
, int borders_only
)
170 struct window
*w
= c
->session
->curw
->window
;
171 struct tty
*tty
= &c
->tty
;
172 struct window_pane
*wp
;
173 struct grid_cell active_gc
, other_gc
;
176 const u_char
*base
, *ptr
;
177 u_char ch
, border
[20];
179 /* Get status line, er, status. */
180 if (c
->message_string
!= NULL
|| c
->prompt_string
!= NULL
)
183 status
= options_get_number(&c
->session
->options
, "status");
185 /* If only drawing status and it is present, don't need the rest. */
186 if (status_only
&& status
) {
187 tty_draw_line(tty
, &c
->status
, 0, 0, tty
->sy
- 1);
192 /* Set up pane border attributes. */
193 memcpy(&other_gc
, &grid_default_cell
, sizeof other_gc
);
194 memcpy(&active_gc
, &grid_default_cell
, sizeof active_gc
);
195 active_gc
.data
= other_gc
.data
= 'x'; /* not space */
196 fg
= options_get_number(&c
->session
->options
, "pane-border-fg");
197 colour_set_fg(&other_gc
, fg
);
198 bg
= options_get_number(&c
->session
->options
, "pane-border-bg");
199 colour_set_bg(&other_gc
, bg
);
200 fg
= options_get_number(&c
->session
->options
, "pane-active-border-fg");
201 colour_set_fg(&active_gc
, fg
);
202 bg
= options_get_number(&c
->session
->options
, "pane-active-border-bg");
203 colour_set_bg(&active_gc
, bg
);
205 /* Draw background and borders. */
206 strlcpy(border
, " |-....--||+.", sizeof border
);
207 if (tty_term_has(tty
->term
, TTYC_ACSC
)) {
208 base
= " xqlkmjwvtun~";
209 for (ptr
= base
; *ptr
!= '\0'; ptr
++) {
210 if ((ch
= tty_get_acs(tty
, *ptr
)) != '\0')
211 border
[ptr
- base
] = ch
;
213 other_gc
.attr
|= GRID_ATTR_CHARSET
;
214 active_gc
.attr
|= GRID_ATTR_CHARSET
;
216 for (j
= 0; j
< tty
->sy
- status
; j
++) {
217 if (status_only
&& j
!= tty
->sy
- 1)
219 for (i
= 0; i
< tty
->sx
; i
++) {
220 type
= screen_redraw_check_cell(c
, i
, j
);
221 if (type
== CELL_INSIDE
)
223 if (screen_redraw_cell_border1(w
->active
, i
, j
) == 1)
224 tty_attributes(tty
, &active_gc
);
226 tty_attributes(tty
, &other_gc
);
227 tty_cursor(tty
, i
, j
);
228 tty_putc(tty
, border
[type
]);
232 /* If only drawing borders, that's it. */
236 /* Draw the panes, if necessary. */
237 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
238 if (!window_pane_visible(wp
))
240 for (i
= 0; i
< wp
->sy
; i
++) {
241 if (status_only
&& wp
->yoff
+ i
!= tty
->sy
- 1)
243 tty_draw_line(tty
, wp
->screen
, i
, wp
->xoff
, wp
->yoff
);
245 if (c
->flags
& CLIENT_IDENTIFY
)
246 screen_redraw_draw_number(c
, wp
);
249 /* Draw the status line. */
251 tty_draw_line(tty
, &c
->status
, 0, 0, tty
->sy
- 1);
255 /* Draw a single pane. */
257 screen_redraw_pane(struct client
*c
, struct window_pane
*wp
)
261 for (i
= 0; i
< wp
->sy
; i
++)
262 tty_draw_line(&c
->tty
, wp
->screen
, i
, wp
->xoff
, wp
->yoff
);
266 /* Draw number on a pane. */
268 screen_redraw_draw_number(struct client
*c
, struct window_pane
*wp
)
270 struct tty
*tty
= &c
->tty
;
271 struct session
*s
= c
->session
;
272 struct options
*oo
= &s
->options
;
273 struct window
*w
= wp
->window
;
275 u_int idx
, px
, py
, i
, j
, xoff
, yoff
;
276 int colour
, active_colour
;
280 idx
= window_pane_index(w
, wp
);
281 len
= xsnprintf(buf
, sizeof buf
, "%u", idx
);
285 colour
= options_get_number(oo
, "display-panes-colour");
286 active_colour
= options_get_number(oo
, "display-panes-active-colour");
288 px
= wp
->sx
/ 2; py
= wp
->sy
/ 2;
289 xoff
= wp
->xoff
; yoff
= wp
->yoff
;
291 if (wp
->sx
< len
* 6 || wp
->sy
< 5) {
292 tty_cursor(tty
, xoff
+ px
- len
/ 2, yoff
+ py
);
293 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
294 gc
.data
= '_'; /* not space */
296 colour_set_fg(&gc
, active_colour
);
298 colour_set_fg(&gc
, colour
);
299 tty_attributes(tty
, &gc
);
307 memcpy(&gc
, &grid_default_cell
, sizeof gc
);
308 gc
.data
= '_'; /* not space */
310 colour_set_bg(&gc
, active_colour
);
312 colour_set_bg(&gc
, colour
);
313 tty_attributes(tty
, &gc
);
314 for (ptr
= buf
; *ptr
!= '\0'; ptr
++) {
315 if (*ptr
< '0' || *ptr
> '9')
319 for (j
= 0; j
< 5; j
++) {
320 for (i
= px
; i
< px
+ 5; i
++) {
321 tty_cursor(tty
, xoff
+ i
, yoff
+ py
+ j
);
322 if (clock_table
[idx
][j
][i
- px
])