]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/vt/vt_buf.c
o Fix scroll calculations.
[FreeBSD/FreeBSD.git] / sys / dev / vt / vt_buf.c
1 /*-
2  * Copyright (c) 2009 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Ed Schouten under sponsorship from the
6  * FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/lock.h>
36 #include <sys/malloc.h>
37 #include <sys/mutex.h>
38 #include <sys/systm.h>
39
40 #include <dev/vt/vt.h>
41
42 static MALLOC_DEFINE(M_VTBUF, "vtbuf", "vt buffer");
43
44 #define VTBUF_LOCK(vb)          mtx_lock_spin(&(vb)->vb_lock)
45 #define VTBUF_UNLOCK(vb)        mtx_unlock_spin(&(vb)->vb_lock)
46 /*
47  * line4
48  * line5 <--- curroffset (terminal output to that line)
49  * line0
50  * line1                  <--- roffset (history display from that point)
51  * line2
52  * line3
53  */
54 int
55 vthistory_seek(struct vt_buf *vb, int offset, int whence)
56 {
57         int diff, top, bottom, roffset;
58
59         /* No scrolling if not enabled. */
60         if ((vb->vb_flags & VBF_SCROLL) == 0) {
61                 if (vb->vb_roffset != vb->vb_curroffset) {
62                         vb->vb_roffset = vb->vb_curroffset;
63                         return (0xffff);
64                 }
65                 return (0); /* No changes */
66         }
67         top = (vb->vb_flags & VBF_HISTORY_FULL)?
68             (vb->vb_curroffset + vb->vb_scr_size.tp_row):vb->vb_history_size;
69         bottom = vb->vb_curroffset + vb->vb_history_size;
70
71         /*
72          * Operate on copy of offset value, since it temporary can be bigger
73          * than amount of rows in buffer.
74          */
75         roffset = vb->vb_roffset + vb->vb_history_size;
76         switch (whence) {
77         case VHS_SET:
78                 roffset = offset + vb->vb_history_size;
79                 break;
80         case VHS_CUR:
81                 roffset += offset;
82                 break;
83         case VHS_END:
84                 /* Go to current offset. */
85                 roffset = vb->vb_curroffset + vb->vb_history_size;
86                 break;
87         }
88
89         roffset = (roffset < top)?top:roffset;
90         roffset = (roffset > bottom)?bottom:roffset;
91
92         roffset %= vb->vb_history_size;
93
94         if (vb->vb_roffset != roffset) {
95                 diff = vb->vb_roffset - roffset;
96                 vb->vb_roffset = roffset;
97                 /*
98                  * Offset changed, please update Nth lines on sceen.
99                  * +N - Nth lines at top;
100                  * -N - Nth lines at bottom.
101                  */
102                 return (diff);
103         }
104         return (0); /* No changes */
105 }
106
107 void
108 vthistory_addlines(struct vt_buf *vb, int offset)
109 {
110
111         vb->vb_curroffset += offset;
112         if (vb->vb_curroffset < 0)
113                 vb->vb_curroffset = 0;
114         vb->vb_curroffset %= vb->vb_history_size;
115         if ((vb->vb_flags & VBF_SCROLL) == 0) {
116                 vb->vb_roffset = vb->vb_curroffset;
117         }
118 }
119
120 void
121 vthistory_getpos(const struct vt_buf *vb, unsigned int *offset)
122 {
123
124         *offset = vb->vb_roffset;
125 }
126
127 static inline uint64_t
128 vtbuf_dirty_axis(unsigned int begin, unsigned int end)
129 {
130         uint64_t left, right, mask;
131
132         /*
133          * Mark all bits between begin % 64 and end % 64 dirty.
134          * This code is functionally equivalent to:
135          *
136          *      for (i = begin; i < end; i++)
137          *              mask |= (uint64_t)1 << (i % 64);
138          */
139
140         /* Obvious case. Mark everything dirty. */
141         if (end - begin >= 64)
142                 return (VBM_DIRTY);
143
144         /* 1....0; used bits on the left. */
145         left = VBM_DIRTY << begin % 64;
146         /* 0....1; used bits on the right. */
147         right = VBM_DIRTY >> -end % 64;
148
149         /*
150          * Only take the intersection.  If the result of that is 0, it
151          * means that the selection crossed a 64 bit boundary along the
152          * way, which means we have to take the complement.
153          */
154         mask = left & right;
155         if (mask == 0)
156                 mask = left | right;
157         return (mask);
158 }
159
160 static inline void
161 vtbuf_dirty(struct vt_buf *vb, const term_rect_t *area)
162 {
163
164         VTBUF_LOCK(vb);
165         if (vb->vb_dirtyrect.tr_begin.tp_row > area->tr_begin.tp_row)
166                 vb->vb_dirtyrect.tr_begin.tp_row = area->tr_begin.tp_row;
167         if (vb->vb_dirtyrect.tr_begin.tp_col > area->tr_begin.tp_col)
168                 vb->vb_dirtyrect.tr_begin.tp_col = area->tr_begin.tp_col;
169         if (vb->vb_dirtyrect.tr_end.tp_row < area->tr_end.tp_row)
170                 vb->vb_dirtyrect.tr_end.tp_row = area->tr_end.tp_row;
171         if (vb->vb_dirtyrect.tr_end.tp_col < area->tr_end.tp_col)
172                 vb->vb_dirtyrect.tr_end.tp_col = area->tr_end.tp_col;
173         vb->vb_dirtymask.vbm_row |=
174             vtbuf_dirty_axis(area->tr_begin.tp_row, area->tr_end.tp_row);
175         vb->vb_dirtymask.vbm_col |=
176             vtbuf_dirty_axis(area->tr_begin.tp_col, area->tr_end.tp_col);
177         VTBUF_UNLOCK(vb);
178 }
179
180 static inline void
181 vtbuf_dirty_cell(struct vt_buf *vb, const term_pos_t *p)
182 {
183         term_rect_t area;
184
185         area.tr_begin = *p;
186         area.tr_end.tp_row = p->tp_row + 1;
187         area.tr_end.tp_col = p->tp_col + 1;
188         vtbuf_dirty(vb, &area);
189 }
190
191 static void
192 vtbuf_make_undirty(struct vt_buf *vb)
193 {
194
195         vb->vb_dirtyrect.tr_begin = vb->vb_scr_size;
196         vb->vb_dirtyrect.tr_end.tp_row = vb->vb_dirtyrect.tr_end.tp_col = 0;
197         vb->vb_dirtymask.vbm_row = vb->vb_dirtymask.vbm_col = 0;
198 }
199
200 void
201 vtbuf_undirty(struct vt_buf *vb, term_rect_t *r, struct vt_bufmask *m)
202 {
203
204         VTBUF_LOCK(vb);
205         *r = vb->vb_dirtyrect;
206         *m = vb->vb_dirtymask;
207         vtbuf_make_undirty(vb);
208         VTBUF_UNLOCK(vb);
209 }
210
211 void
212 vtbuf_copy(struct vt_buf *vb, const term_rect_t *r, const term_pos_t *p2)
213 {
214         const term_pos_t *p1 = &r->tr_begin;
215         term_rect_t area;
216         unsigned int rows, cols;
217         int pr, rdiff;
218
219         KASSERT(r->tr_begin.tp_row < vb->vb_scr_size.tp_row,
220             ("vtbuf_copy begin.tp_row %d must be less than screen width %d",
221                 r->tr_begin.tp_row, vb->vb_scr_size.tp_row));
222         KASSERT(r->tr_begin.tp_col < vb->vb_scr_size.tp_col,
223             ("vtbuf_copy begin.tp_col %d must be less than screen height %d",
224                 r->tr_begin.tp_col, vb->vb_scr_size.tp_col));
225
226         KASSERT(r->tr_end.tp_row <= vb->vb_scr_size.tp_row,
227             ("vtbuf_copy end.tp_row %d must be less than screen width %d",
228                 r->tr_end.tp_row, vb->vb_scr_size.tp_row));
229         KASSERT(r->tr_end.tp_col <= vb->vb_scr_size.tp_col,
230             ("vtbuf_copy end.tp_col %d must be less than screen height %d",
231                 r->tr_end.tp_col, vb->vb_scr_size.tp_col));
232
233         KASSERT(p2->tp_row < vb->vb_scr_size.tp_row,
234             ("vtbuf_copy tp_row %d must be less than screen width %d",
235                 p2->tp_row, vb->vb_scr_size.tp_row));
236         KASSERT(p2->tp_col < vb->vb_scr_size.tp_col,
237             ("vtbuf_copy tp_col %d must be less than screen height %d",
238                 p2->tp_col, vb->vb_scr_size.tp_col));
239
240         rows = r->tr_end.tp_row - r->tr_begin.tp_row;
241         rdiff = r->tr_begin.tp_row - p2->tp_row;
242         cols = r->tr_end.tp_col - r->tr_begin.tp_col;
243         if (r->tr_begin.tp_row > p2->tp_row && r->tr_begin.tp_col == 0 &&
244             r->tr_end.tp_col == vb->vb_scr_size.tp_col && /* Full row. */
245             (rows + rdiff) == vb->vb_scr_size.tp_row && /* Whole screen. */
246             rdiff > 0) { /* Only forward dirrection. Do not eat history. */
247                 vthistory_addlines(vb, rdiff);
248         } else if (p2->tp_row < p1->tp_row) {
249                 /* Handle overlapping copies of line segments. */
250                 /* Move data up. */
251                 for (pr = 0; pr < rows; pr++)
252                         memmove(
253                             &VTBUF_FIELD(vb, p2->tp_row + pr, p2->tp_col),
254                             &VTBUF_FIELD(vb, p1->tp_row + pr, p1->tp_col),
255                             cols * sizeof(term_char_t));
256         } else {
257                 /* Move data down. */
258                 for (pr = rows - 1; pr >= 0; pr--)
259                         memmove(
260                             &VTBUF_FIELD(vb, p2->tp_row + pr, p2->tp_col),
261                             &VTBUF_FIELD(vb, p1->tp_row + pr, p1->tp_col),
262                             cols * sizeof(term_char_t));
263         }
264
265         area.tr_begin = *p2;
266         area.tr_end.tp_row = MIN(p2->tp_row + rows, vb->vb_scr_size.tp_row);
267         area.tr_end.tp_col = MIN(p2->tp_col + cols, vb->vb_scr_size.tp_col);
268         vtbuf_dirty(vb, &area);
269 }
270
271 static void
272 vtbuf_fill(struct vt_buf *vb, const term_rect_t *r, term_char_t c)
273 {
274         unsigned int pr, pc;
275         term_char_t *row;
276
277         for (pr = r->tr_begin.tp_row; pr < r->tr_end.tp_row; pr++) {
278                 row = vb->vb_rows[(vb->vb_curroffset + pr) %
279                     VTBUF_MAX_HEIGHT(vb)];
280                 for (pc = r->tr_begin.tp_col; pc < r->tr_end.tp_col; pc++) {
281                         row[pc] = c;
282                 }
283         }
284 }
285
286 void
287 vtbuf_fill_locked(struct vt_buf *vb, const term_rect_t *r, term_char_t c)
288 {
289         KASSERT(r->tr_begin.tp_row < vb->vb_scr_size.tp_row,
290             ("vtbuf_fill_locked begin.tp_row %d must be < screen width %d",
291                 r->tr_begin.tp_row, vb->vb_scr_size.tp_row));
292         KASSERT(r->tr_begin.tp_col < vb->vb_scr_size.tp_col,
293             ("vtbuf_fill_locked begin.tp_col %d must be < screen height %d",
294                 r->tr_begin.tp_col, vb->vb_scr_size.tp_col));
295
296         KASSERT(r->tr_end.tp_row <= vb->vb_scr_size.tp_row,
297             ("vtbuf_fill_locked end.tp_row %d must be <= screen width %d",
298                 r->tr_end.tp_row, vb->vb_scr_size.tp_row));
299         KASSERT(r->tr_end.tp_col <= vb->vb_scr_size.tp_col,
300             ("vtbuf_fill_locked end.tp_col %d must be <= screen height %d",
301                 r->tr_end.tp_col, vb->vb_scr_size.tp_col));
302
303         VTBUF_LOCK(vb);
304         vtbuf_fill(vb, r, c);
305         VTBUF_UNLOCK(vb);
306
307         vtbuf_dirty(vb, r);
308 }
309
310 static void
311 vtbuf_init_rows(struct vt_buf *vb)
312 {
313         int r;
314
315         vb->vb_history_size = MAX(vb->vb_history_size, vb->vb_scr_size.tp_row);
316
317         for (r = 0; r < vb->vb_history_size; r++)
318                 vb->vb_rows[r] = &vb->vb_buffer[r *
319                     vb->vb_scr_size.tp_col];
320 }
321
322 void
323 vtbuf_init_early(struct vt_buf *vb)
324 {
325
326         vb->vb_flags |= VBF_CURSOR;
327         vb->vb_roffset = 0;
328         vb->vb_curroffset = 0;
329
330         vtbuf_init_rows(vb);
331         vtbuf_make_undirty(vb);
332         if ((vb->vb_flags & VBF_MTX_INIT) == 0) {
333                 mtx_init(&vb->vb_lock, "vtbuf", NULL, MTX_SPIN);
334                 vb->vb_flags |= VBF_MTX_INIT;
335         }
336 }
337
338 void
339 vtbuf_init(struct vt_buf *vb, const term_pos_t *p)
340 {
341         int sz;
342
343         vb->vb_scr_size = *p;
344         vb->vb_history_size = VBF_DEFAULT_HISTORY_SIZE;
345
346         if ((vb->vb_flags & VBF_STATIC) == 0) {
347                 sz = vb->vb_history_size * p->tp_col * sizeof(term_char_t);
348                 vb->vb_buffer = malloc(sz, M_VTBUF, M_WAITOK | M_ZERO);
349
350                 sz = vb->vb_history_size * sizeof(term_char_t *);
351                 vb->vb_rows = malloc(sz, M_VTBUF, M_WAITOK | M_ZERO);
352         }
353
354         vtbuf_init_early(vb);
355 }
356
357 void
358 vtbuf_sethistory_size(struct vt_buf *vb, int size)
359 {
360         term_pos_t p;
361
362         /* With same size */
363         p.tp_row = vb->vb_scr_size.tp_row;
364         p.tp_col = vb->vb_scr_size.tp_col;
365         vtbuf_grow(vb, &p, size);
366 }
367
368 void
369 vtbuf_grow(struct vt_buf *vb, const term_pos_t *p, int history_size)
370 {
371         term_char_t *old, *new, **rows, **oldrows, **copyrows, *row;
372         int bufsize, rowssize, w, h, c, r;
373         term_rect_t rect;
374
375         history_size = MAX(history_size, p->tp_row);
376
377         if (history_size > vb->vb_history_size || p->tp_col >
378             vb->vb_scr_size.tp_col) {
379                 /* Allocate new buffer. */
380                 bufsize = history_size * p->tp_col * sizeof(term_char_t);
381                 new = malloc(bufsize, M_VTBUF, M_WAITOK | M_ZERO);
382                 rowssize = history_size * sizeof(term_pos_t *);
383                 rows = malloc(rowssize, M_VTBUF, M_WAITOK | M_ZERO);
384
385                 /* Toggle it. */
386                 VTBUF_LOCK(vb);
387                 old = vb->vb_flags & VBF_STATIC ? NULL : vb->vb_buffer;
388                 oldrows = vb->vb_flags & VBF_STATIC ? NULL : vb->vb_rows;
389                 copyrows = vb->vb_rows;
390                 w = vb->vb_scr_size.tp_col;
391                 h = vb->vb_history_size;
392
393                 vb->vb_history_size = history_size;
394                 vb->vb_buffer = new;
395                 vb->vb_rows = rows;
396                 vb->vb_flags &= ~VBF_STATIC;
397                 vb->vb_scr_size = *p;
398                 vtbuf_init_rows(vb);
399
400                 /* Copy history and fill extra space. */
401                 for (r = 0; r < history_size; r ++) {
402                         row = rows[r];
403                         if (r < h) { /* Copy. */
404                                 memmove(rows[r], copyrows[r],
405                                     MIN(p->tp_col, w) * sizeof(term_char_t));
406                                 for (c = MIN(p->tp_col, w); c < p->tp_col;
407                                     c++) {
408                                         row[c] = VTBUF_SPACE_CHAR;
409                                 }
410                         } else { /* Just fill. */
411                                 rect.tr_begin.tp_col = 0;
412                                 rect.tr_begin.tp_row = r;
413                                 rect.tr_end.tp_col = p->tp_col;
414                                 rect.tr_end.tp_row = p->tp_row;
415                                 vtbuf_fill(vb, &rect, VTBUF_SPACE_CHAR);
416                                 break;
417                         }
418                 }
419                 vtbuf_make_undirty(vb);
420                 VTBUF_UNLOCK(vb);
421                 /* Deallocate old buffer. */
422                 free(old, M_VTBUF);
423                 free(oldrows, M_VTBUF);
424         }
425 }
426
427 void
428 vtbuf_putchar(struct vt_buf *vb, const term_pos_t *p, term_char_t c)
429 {
430         term_char_t *row;
431
432         KASSERT(p->tp_row < vb->vb_scr_size.tp_row,
433             ("vtbuf_putchar tp_row %d must be less than screen width %d",
434                 p->tp_row, vb->vb_scr_size.tp_row));
435         KASSERT(p->tp_col < vb->vb_scr_size.tp_col,
436             ("vtbuf_putchar tp_col %d must be less than screen height %d",
437                 p->tp_col, vb->vb_scr_size.tp_col));
438
439         row = vb->vb_rows[(vb->vb_curroffset + p->tp_row) %
440             VTBUF_MAX_HEIGHT(vb)];
441         if (row[p->tp_col] != c) {
442                 VTBUF_LOCK(vb);
443                 row[p->tp_col] = c;
444                 VTBUF_UNLOCK(vb);
445                 vtbuf_dirty_cell(vb, p);
446         }
447 }
448
449 void
450 vtbuf_cursor_position(struct vt_buf *vb, const term_pos_t *p)
451 {
452
453         if (vb->vb_flags & VBF_CURSOR) {
454                 vtbuf_dirty_cell(vb, &vb->vb_cursor);
455                 vb->vb_cursor = *p;
456                 vtbuf_dirty_cell(vb, &vb->vb_cursor);
457         } else {
458                 vb->vb_cursor = *p;
459         }
460 }
461
462 void
463 vtbuf_cursor_visibility(struct vt_buf *vb, int yes)
464 {
465         int oflags, nflags;
466
467         VTBUF_LOCK(vb);
468         oflags = vb->vb_flags;
469         if (yes)
470                 vb->vb_flags |= VBF_CURSOR;
471         else
472                 vb->vb_flags &= ~VBF_CURSOR;
473         nflags = vb->vb_flags;
474         VTBUF_UNLOCK(vb);
475
476         if (oflags != nflags)
477                 vtbuf_dirty_cell(vb, &vb->vb_cursor);
478 }