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