1 |
|
|
/* $OpenBSD: screen.c,v 1.39 2016/07/15 00:49:08 nicm Exp $ */ |
2 |
|
|
|
3 |
|
|
/* |
4 |
|
|
* Copyright (c) 2007 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 |
|
|
#include <unistd.h> |
24 |
|
|
|
25 |
|
|
#include "tmux.h" |
26 |
|
|
|
27 |
|
|
void screen_resize_x(struct screen *, u_int); |
28 |
|
|
void screen_resize_y(struct screen *, u_int); |
29 |
|
|
|
30 |
|
|
/* Create a new screen. */ |
31 |
|
|
void |
32 |
|
|
screen_init(struct screen *s, u_int sx, u_int sy, u_int hlimit) |
33 |
|
|
{ |
34 |
|
|
s->grid = grid_create(sx, sy, hlimit); |
35 |
|
|
s->title = xstrdup(""); |
36 |
|
|
|
37 |
|
|
s->cstyle = 0; |
38 |
|
|
s->ccolour = xstrdup(""); |
39 |
|
|
s->tabs = NULL; |
40 |
|
|
|
41 |
|
|
s->dirty = NULL; |
42 |
|
|
s->dirtysize = 0; |
43 |
|
|
|
44 |
|
|
screen_reinit(s); |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
/* Reinitialise screen. */ |
48 |
|
|
void |
49 |
|
|
screen_reinit(struct screen *s) |
50 |
|
|
{ |
51 |
|
|
s->cx = 0; |
52 |
|
|
s->cy = 0; |
53 |
|
|
|
54 |
|
|
s->rupper = 0; |
55 |
|
|
s->rlower = screen_size_y(s) - 1; |
56 |
|
|
|
57 |
|
|
s->mode = MODE_CURSOR | MODE_WRAP; |
58 |
|
|
|
59 |
|
|
screen_reset_tabs(s); |
60 |
|
|
|
61 |
|
|
grid_clear_lines(s->grid, s->grid->hsize, s->grid->sy); |
62 |
|
|
|
63 |
|
|
screen_clear_selection(s); |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
/* Destroy a screen. */ |
67 |
|
|
void |
68 |
|
|
screen_free(struct screen *s) |
69 |
|
|
{ |
70 |
|
|
free(s->dirty); |
71 |
|
|
free(s->tabs); |
72 |
|
|
free(s->title); |
73 |
|
|
free(s->ccolour); |
74 |
|
|
grid_destroy(s->grid); |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
/* Reset tabs to default, eight spaces apart. */ |
78 |
|
|
void |
79 |
|
|
screen_reset_tabs(struct screen *s) |
80 |
|
|
{ |
81 |
|
|
u_int i; |
82 |
|
|
|
83 |
|
|
free(s->tabs); |
84 |
|
|
|
85 |
|
|
if ((s->tabs = bit_alloc(screen_size_x(s))) == NULL) |
86 |
|
|
fatal("bit_alloc failed"); |
87 |
|
|
for (i = 8; i < screen_size_x(s); i += 8) |
88 |
|
|
bit_set(s->tabs, i); |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
/* Set screen cursor style. */ |
92 |
|
|
void |
93 |
|
|
screen_set_cursor_style(struct screen *s, u_int style) |
94 |
|
|
{ |
95 |
|
|
if (style <= 6) |
96 |
|
|
s->cstyle = style; |
97 |
|
|
} |
98 |
|
|
|
99 |
|
|
/* Set screen cursor colour. */ |
100 |
|
|
void |
101 |
|
|
screen_set_cursor_colour(struct screen *s, const char *colour) |
102 |
|
|
{ |
103 |
|
|
free(s->ccolour); |
104 |
|
|
s->ccolour = xstrdup(colour); |
105 |
|
|
} |
106 |
|
|
|
107 |
|
|
/* Set screen title. */ |
108 |
|
|
void |
109 |
|
|
screen_set_title(struct screen *s, const char *title) |
110 |
|
|
{ |
111 |
|
|
free(s->title); |
112 |
|
|
s->title = xstrdup(title); |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
/* Resize screen. */ |
116 |
|
|
void |
117 |
|
|
screen_resize(struct screen *s, u_int sx, u_int sy, int reflow) |
118 |
|
|
{ |
119 |
|
|
if (sx < 1) |
120 |
|
|
sx = 1; |
121 |
|
|
if (sy < 1) |
122 |
|
|
sy = 1; |
123 |
|
|
|
124 |
|
|
if (sx != screen_size_x(s)) { |
125 |
|
|
screen_resize_x(s, sx); |
126 |
|
|
|
127 |
|
|
/* |
128 |
|
|
* It is unclear what should happen to tabs on resize. xterm |
129 |
|
|
* seems to try and maintain them, rxvt resets them. Resetting |
130 |
|
|
* is simpler and more reliable so let's do that. |
131 |
|
|
*/ |
132 |
|
|
screen_reset_tabs(s); |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
if (sy != screen_size_y(s)) |
136 |
|
|
screen_resize_y(s, sy); |
137 |
|
|
|
138 |
|
|
if (reflow) |
139 |
|
|
screen_reflow(s, sx); |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
void |
143 |
|
|
screen_resize_x(struct screen *s, u_int sx) |
144 |
|
|
{ |
145 |
|
|
struct grid *gd = s->grid; |
146 |
|
|
|
147 |
|
|
if (sx == 0) |
148 |
|
|
fatalx("zero size"); |
149 |
|
|
|
150 |
|
|
/* |
151 |
|
|
* Treat resizing horizontally simply: just ensure the cursor is |
152 |
|
|
* on-screen and change the size. Don't bother to truncate any lines - |
153 |
|
|
* then the data should be accessible if the size is then increased. |
154 |
|
|
* |
155 |
|
|
* The only potential wrinkle is if UTF-8 double-width characters are |
156 |
|
|
* left in the last column, but UTF-8 terminals should deal with this |
157 |
|
|
* sanely. |
158 |
|
|
*/ |
159 |
|
|
if (s->cx >= sx) |
160 |
|
|
s->cx = sx - 1; |
161 |
|
|
gd->sx = sx; |
162 |
|
|
} |
163 |
|
|
|
164 |
|
|
void |
165 |
|
|
screen_resize_y(struct screen *s, u_int sy) |
166 |
|
|
{ |
167 |
|
|
struct grid *gd = s->grid; |
168 |
|
|
u_int needed, available, oldy, i; |
169 |
|
|
|
170 |
|
|
if (sy == 0) |
171 |
|
|
fatalx("zero size"); |
172 |
|
|
oldy = screen_size_y(s); |
173 |
|
|
|
174 |
|
|
/* |
175 |
|
|
* When resizing: |
176 |
|
|
* |
177 |
|
|
* If the height is decreasing, delete lines from the bottom until |
178 |
|
|
* hitting the cursor, then push lines from the top into the history. |
179 |
|
|
* |
180 |
|
|
* When increasing, pull as many lines as possible from the history to |
181 |
|
|
* the top, then fill the remaining with blanks at the bottom. |
182 |
|
|
*/ |
183 |
|
|
|
184 |
|
|
/* Size decreasing. */ |
185 |
|
|
if (sy < oldy) { |
186 |
|
|
needed = oldy - sy; |
187 |
|
|
|
188 |
|
|
/* Delete as many lines as possible from the bottom. */ |
189 |
|
|
available = oldy - 1 - s->cy; |
190 |
|
|
if (available > 0) { |
191 |
|
|
if (available > needed) |
192 |
|
|
available = needed; |
193 |
|
|
grid_view_delete_lines(gd, oldy - available, available); |
194 |
|
|
} |
195 |
|
|
needed -= available; |
196 |
|
|
|
197 |
|
|
/* |
198 |
|
|
* Now just increase the history size, if possible, to take |
199 |
|
|
* over the lines which are left. If history is off, delete |
200 |
|
|
* lines from the top. |
201 |
|
|
*/ |
202 |
|
|
available = s->cy; |
203 |
|
|
if (gd->flags & GRID_HISTORY) |
204 |
|
|
gd->hsize += needed; |
205 |
|
|
else if (needed > 0 && available > 0) { |
206 |
|
|
if (available > needed) |
207 |
|
|
available = needed; |
208 |
|
|
grid_view_delete_lines(gd, 0, available); |
209 |
|
|
} |
210 |
|
|
s->cy -= needed; |
211 |
|
|
} |
212 |
|
|
|
213 |
|
|
/* Resize line arrays. */ |
214 |
|
|
gd->linedata = xreallocarray(gd->linedata, gd->hsize + sy, |
215 |
|
|
sizeof *gd->linedata); |
216 |
|
|
|
217 |
|
|
/* Size increasing. */ |
218 |
|
|
if (sy > oldy) { |
219 |
|
|
needed = sy - oldy; |
220 |
|
|
|
221 |
|
|
/* |
222 |
|
|
* Try to pull as much as possible out of the history, if is |
223 |
|
|
* is enabled. |
224 |
|
|
*/ |
225 |
|
|
available = gd->hsize; |
226 |
|
|
if (gd->flags & GRID_HISTORY && available > 0) { |
227 |
|
|
if (available > needed) |
228 |
|
|
available = needed; |
229 |
|
|
gd->hsize -= available; |
230 |
|
|
s->cy += available; |
231 |
|
|
} else |
232 |
|
|
available = 0; |
233 |
|
|
needed -= available; |
234 |
|
|
|
235 |
|
|
/* Then fill the rest in with blanks. */ |
236 |
|
|
for (i = gd->hsize + sy - needed; i < gd->hsize + sy; i++) |
237 |
|
|
memset(&gd->linedata[i], 0, sizeof gd->linedata[i]); |
238 |
|
|
} |
239 |
|
|
|
240 |
|
|
/* Set the new size, and reset the scroll region. */ |
241 |
|
|
gd->sy = sy; |
242 |
|
|
s->rupper = 0; |
243 |
|
|
s->rlower = screen_size_y(s) - 1; |
244 |
|
|
} |
245 |
|
|
|
246 |
|
|
/* Set selection. */ |
247 |
|
|
void |
248 |
|
|
screen_set_selection(struct screen *s, u_int sx, u_int sy, |
249 |
|
|
u_int ex, u_int ey, u_int rectflag, struct grid_cell *gc) |
250 |
|
|
{ |
251 |
|
|
struct screen_sel *sel = &s->sel; |
252 |
|
|
|
253 |
|
|
memcpy(&sel->cell, gc, sizeof sel->cell); |
254 |
|
|
sel->flag = 1; |
255 |
|
|
sel->rectflag = rectflag; |
256 |
|
|
|
257 |
|
|
sel->sx = sx; sel->sy = sy; |
258 |
|
|
sel->ex = ex; sel->ey = ey; |
259 |
|
|
} |
260 |
|
|
|
261 |
|
|
/* Clear selection. */ |
262 |
|
|
void |
263 |
|
|
screen_clear_selection(struct screen *s) |
264 |
|
|
{ |
265 |
|
|
struct screen_sel *sel = &s->sel; |
266 |
|
|
|
267 |
|
|
sel->flag = 0; |
268 |
|
|
sel->lineflag = LINE_SEL_NONE; |
269 |
|
|
} |
270 |
|
|
|
271 |
|
|
/* Check if cell in selection. */ |
272 |
|
|
int |
273 |
|
|
screen_check_selection(struct screen *s, u_int px, u_int py) |
274 |
|
|
{ |
275 |
|
|
struct screen_sel *sel = &s->sel; |
276 |
|
|
u_int xx; |
277 |
|
|
|
278 |
|
|
if (!sel->flag) |
279 |
|
|
return (0); |
280 |
|
|
|
281 |
|
|
if (sel->rectflag) { |
282 |
|
|
if (sel->sy < sel->ey) { |
283 |
|
|
/* start line < end line -- downward selection. */ |
284 |
|
|
if (py < sel->sy || py > sel->ey) |
285 |
|
|
return (0); |
286 |
|
|
} else if (sel->sy > sel->ey) { |
287 |
|
|
/* start line > end line -- upward selection. */ |
288 |
|
|
if (py > sel->sy || py < sel->ey) |
289 |
|
|
return (0); |
290 |
|
|
} else { |
291 |
|
|
/* starting line == ending line. */ |
292 |
|
|
if (py != sel->sy) |
293 |
|
|
return (0); |
294 |
|
|
} |
295 |
|
|
|
296 |
|
|
/* |
297 |
|
|
* Need to include the selection start row, but not the cursor |
298 |
|
|
* row, which means the selection changes depending on which |
299 |
|
|
* one is on the left. |
300 |
|
|
*/ |
301 |
|
|
if (sel->ex < sel->sx) { |
302 |
|
|
/* Cursor (ex) is on the left. */ |
303 |
|
|
if (px < sel->ex) |
304 |
|
|
return (0); |
305 |
|
|
|
306 |
|
|
if (px > sel->sx) |
307 |
|
|
return (0); |
308 |
|
|
} else { |
309 |
|
|
/* Selection start (sx) is on the left. */ |
310 |
|
|
if (px < sel->sx) |
311 |
|
|
return (0); |
312 |
|
|
|
313 |
|
|
if (px > sel->ex) |
314 |
|
|
return (0); |
315 |
|
|
} |
316 |
|
|
} else { |
317 |
|
|
/* |
318 |
|
|
* Like emacs, keep the top-left-most character, and drop the |
319 |
|
|
* bottom-right-most, regardless of copy direction. |
320 |
|
|
*/ |
321 |
|
|
if (sel->sy < sel->ey) { |
322 |
|
|
/* starting line < ending line -- downward selection. */ |
323 |
|
|
if (py < sel->sy || py > sel->ey) |
324 |
|
|
return (0); |
325 |
|
|
|
326 |
|
|
if (py == sel->sy && px < sel->sx) |
327 |
|
|
return (0); |
328 |
|
|
|
329 |
|
|
if (py == sel->ey && px > sel->ex) |
330 |
|
|
return (0); |
331 |
|
|
} else if (sel->sy > sel->ey) { |
332 |
|
|
/* starting line > ending line -- upward selection. */ |
333 |
|
|
if (py > sel->sy || py < sel->ey) |
334 |
|
|
return (0); |
335 |
|
|
|
336 |
|
|
if (py == sel->ey && px < sel->ex) |
337 |
|
|
return (0); |
338 |
|
|
|
339 |
|
|
if (sel->modekeys == MODEKEY_EMACS) |
340 |
|
|
xx = sel->sx - 1; |
341 |
|
|
else |
342 |
|
|
xx = sel->sx; |
343 |
|
|
if (py == sel->sy && px > xx) |
344 |
|
|
return (0); |
345 |
|
|
} else { |
346 |
|
|
/* starting line == ending line. */ |
347 |
|
|
if (py != sel->sy) |
348 |
|
|
return (0); |
349 |
|
|
|
350 |
|
|
if (sel->ex < sel->sx) { |
351 |
|
|
/* cursor (ex) is on the left */ |
352 |
|
|
if (sel->modekeys == MODEKEY_EMACS) |
353 |
|
|
xx = sel->sx - 1; |
354 |
|
|
else |
355 |
|
|
xx = sel->sx; |
356 |
|
|
if (px > xx || px < sel->ex) |
357 |
|
|
return (0); |
358 |
|
|
} else { |
359 |
|
|
/* selection start (sx) is on the left */ |
360 |
|
|
if (px < sel->sx || px > sel->ex) |
361 |
|
|
return (0); |
362 |
|
|
} |
363 |
|
|
} |
364 |
|
|
} |
365 |
|
|
|
366 |
|
|
return (1); |
367 |
|
|
} |
368 |
|
|
|
369 |
|
|
/* Reflow wrapped lines. */ |
370 |
|
|
void |
371 |
|
|
screen_reflow(struct screen *s, u_int new_x) |
372 |
|
|
{ |
373 |
|
|
struct grid *old = s->grid; |
374 |
|
|
u_int change; |
375 |
|
|
|
376 |
|
|
s->grid = grid_create(old->sx, old->sy, old->hlimit); |
377 |
|
|
|
378 |
|
|
change = grid_reflow(s->grid, old, new_x); |
379 |
|
|
if (change < s->cy) |
380 |
|
|
s->cy -= change; |
381 |
|
|
else |
382 |
|
|
s->cy = 0; |
383 |
|
|
} |