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