aboutsummaryrefslogtreecommitdiff
path: root/src/libterm/window.c
blob: 5d36c8b1e68937f1e02ebd5e3ee33673b9541e58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#include "term.h"

// -----------------------------------------------------------------------
// buffers

static
void 
zero(Row *row, int start, int len)
{
    int i;
	Cell cell = {
		.txt = L' ',
        .pen = {
            .state  = PenNormal,
            .col.fg = -1,
            .col.bg = -1,
        },
	};

	for (i = start; i < len + start; i++)
		row->cells[i] = cell;
	row->dirty = 1;
}

static
void 
roll(Row *start, Row *end, int count)
{
	int n = end - start;

    /* enforce circularity */
	count %= n;
	if (count < 0)
		count += n;

	if (count) {
		char buf[count * sizeof(Row)]; /* XXX: remove VLA */
		memcpy(buf, start, count * sizeof(Row));
		memmove(start, start + count, (n - count) * sizeof(Row));
		memcpy(end - count, buf, count * sizeof(Row));

		for (Row *row = start; row < end; row++)
			row->dirty = 1;
	}
}

/* buffer operations */
static
void 
bclear(Buffer *b)
{
    int i;
	Cell cell = {
		.txt = L' ',
        .pen = {
            .state  = PenNormal,
            .col.fg = -1,
            .col.bg = -1,
        },
	};

	for (i = 0; i < b->h; i++) {
		Row *row = b->row + i;
		for (int j = 0; j < b->w; j++) {
			row->cells[j] = cell;
			row->dirty    = 1;
		}
	}
}

static
void 
bfini(Buffer *b)
{
    int i;

	for (i = 0; i < b->h; i++)
		free(b->row[i].cells);

	free(b->row);

    if (b->scroll.size) {
        for (i = 0; i < b->scroll.size; i++)
            free(b->scroll.buf[i].cells);

        free(b->scroll.buf);
    }
}

static
void 
bscroll(Buffer *b, int s)
{
    Row tmp;
	int i, ssz = b->scroll.bot - b->scroll.top;

	/* work in quanta of screen size */
	if (s > ssz) {
		bscroll(b, ssz);
		bscroll(b, s - ssz);
		return;
	}
	if (s < -ssz) {
		bscroll(b, -ssz);
		bscroll(b, s + ssz);
		return;
	}

	b->scroll.above += s;
    b->scroll.above  = CLAMP(b->scroll.above, 0, b->scroll.size);

	if (s > 0) {
        if (b->scroll.size) {
            for (i = 0; i < s; i++) {
                tmp = b->scroll.top[i];
                b->scroll.top[i] = b->scroll.buf[b->scroll.index];
                b->scroll.buf[b->scroll.index] = tmp;

                b->scroll.index++;
                if (b->scroll.index == b->scroll.size)
                    b->scroll.index = 0;
            }
        } else 
            for (i = 0; i < s; i++)
                zero(b->scroll.top+i, 0, b->maxw);
	}

	roll(b->scroll.top, b->scroll.bot, s);

	if (s < 0) {
        if (b->scroll.size) {
            for (i = (-s) - 1; i >= 0; i--) {
                b->scroll.index--;
                if (b->scroll.index == -1)
                    b->scroll.index = b->scroll.size - 1;

                tmp = b->scroll.top[i];

                b->scroll.top[i] = b->scroll.buf[b->scroll.index];
                b->scroll.buf[b->scroll.index] = tmp;
                b->scroll.top[i].dirty = 1;
            }
        } else
            for (i = (-s) - 1; i >= 0; i--)
                zero(b->scroll.top+i, 0, b->maxw);
	}
}

static
void 
bresize(Buffer *b, int nrow, int ncol)
{
    int r, d;
	Row *row = b->row;
	Row *cur = row + b->cur.row;

	if (b->h != nrow) {
        /* scroll if we can */
		if (cur >= row + nrow)
			bscroll(b, b->cur.row - nrow + 1);
		while (b->h > nrow) {
			free(row[b->h - 1].cells);
			b->h--;
		}

		row = realloc(row, sizeof(Row) * nrow);
	}

	if (b->maxw < ncol) {
        /* expand each row */
		for (r = 0; r < b->h; r++) {
			row[r].cells = realloc(row[r].cells, sizeof(Cell) * ncol);
			if (b->h < ncol)
				zero(row + r, b->w, ncol - b->w);
			row[r].dirty = 1;
		}
        /* expand the scroll buffer */
		Row *sbuf = b->scroll.buf;
		for (r = 0; r < b->scroll.size; r++) {
			sbuf[r].cells = realloc(sbuf[r].cells, sizeof(Cell) * ncol);
			if (b->w < ncol)
				zero(sbuf + r, b->w, ncol - b->w);
		}
		b->maxw = b->w = ncol;
	} else if (b->w != ncol) {
		for (r = 0; r < b->h; r++)
			row[r].dirty = 1;
		b->w = ncol;
	}

	d = 0;
	if (b->h < nrow) {
		while (b->h < nrow) {
			row[b->h].cells = calloc(b->maxw, sizeof(Cell));
			zero(row + b->h, 0, b->maxw);
			b->h++;
		}

		/* prepare for backfill */
		if (cur >= b->scroll.bot - 1) {
			d = b->row + nrow - cur - 1;
			if (d > b->scroll.above)
				d = b->scroll.above;
		}
	}

	b->cur.row  += row - b->row;
	b->scroll.top = row;
	b->scroll.bot = row + nrow;
	b->row = row;

	/* perform backfill */
	if (d > 0) {
		bscroll(b, -d);
		b->cur.row += d;
	}
}

static
bool 
binit(Buffer *b, int cols, int rows, int scroll)
{
    int size;

    b->pen.state  = PenNormal;
    b->pen.col.fg = b->pen.col.bg = -1;

    size = MAX(scroll, 0);
	if (size && !(b->scroll.buf = calloc(size, sizeof(Row))))
		return false;

	b->scroll.size = size;
	bresize(b, rows, cols);

    b->cur  = (Dot){0};
    b->save = b->cur;

	return true;
}

static
void 
bboundary(Buffer *b, Row **bs, Row **be, Row **as, Row **ae) 
{
	if (bs)
		*bs = nil;
	if (be)
		*be = nil;
	if (as)
		*as = nil;
	if (ae)
		*ae = nil;
	if (!b->scroll.size)
		return;

	if (b->scroll.above) {
		if (bs)
			*bs = &b->scroll.buf[(b->scroll.index - b->scroll.above + b->scroll.size) % b->scroll.size];
		if (be)
			*be = &b->scroll.buf[(b->scroll.index-1 + b->scroll.size) % b->scroll.size];
	}
	if (b->scroll.below) {
		if (as)
			*as = &b->scroll.buf[b->scroll.index];
		if (ae)
			*ae = &b->scroll.buf[(b->scroll.index + b->scroll.below-1) % b->scroll.size];
	}
}

static
Row *
browfirst(Buffer *b) 
{
	Row *bstart;
	if (!b->scroll.size || !b->scroll.above)
		return b->row;
	bboundary(b, &bstart, nil, nil, nil);
	return bstart;
}

static
Row *
browlast(Buffer *b) 
{
	Row *aend;
	if (!b->scroll.size || !b->scroll.below)
		return b->row + b->h - 1;
	bboundary(b, nil, nil, nil, &aend);
	return aend;
}

static
Row *
brownext(Buffer *b, Row *row)
{
	Row *before_start, *before_end, *after_start, *after_end;
	Row *first = b->row, *last = b->row + b->h - 1;

	if (!row)
		return nil;

	bboundary(b, &before_start, &before_end, &after_start, &after_end);

	if (row >= first && row < last)
		return ++row;
	if (row == last)
		return after_start;
	if (row == before_end)
		return first;
	if (row == after_end)
		return nil;
	if (row == &b->scroll.buf[b->scroll.size - 1])
		return b->scroll.buf;
	return ++row;
}

static
Row *
bprevrow(Buffer *b, Row *row)
{
	Row *before_start, *before_end, *after_start, *after_end;
	Row *first = b->row, *last = b->row + b->h - 1;

	if (!row)
		return nil;

	bboundary(b, &before_start, &before_end, &after_start, &after_end);

	if (row > first && row <= last)
		return --row;
	if (row == first)
		return before_end;
	if (row == before_start)
		return nil;
	if (row == after_start)
		return last;
	if (row == b->scroll.buf)
		return &b->scroll.buf[b->scroll.size - 1];
	return --row;
}

// -----------------------------------------------------------------------
// windows

Window *
wmake(Window *root, int top, int left, int w, int h, int scroll)
{
    Window *child, *it;

    child = calloc(1, sizeof(*child));
    child->top    = top;
    child->left   = left;
    child->parent = root;
    if (root) {
        if (root->child) {
            for (it = root->child; it->link != nil; it = it->link)
                ;
            it->link    = child;
        } else
            root->child = child;

        child->curvis = root->curvis;
        child->blink  = root->blink;
    }

    if (!binit((Buffer*)child, w, h, scroll)) {
        free(child);
        return nil;
    }

    return child;
}

void
wfree(Window *win)
{
    free(win);
}

void
wresize(Window *win, int w, int h)
{
    bresize((Buffer*)win, w, h);
}

/* TODO: more sophisticated damage tracking */
void
wputrune(Window *win, rune r)
{
    Row  *row  = win->row + win->cur.row;
    Cell *cell = row->cells + win->cur.col;

    cell->pen = win->pen;
    cell->txt = r;

    if (win->cur.col++ >= win->w) {
        win->cur.col = 0;
        if (win->cur.row++ >= win->h)
            win->cur.row = win->h-1;
    }
    row->dirty = 1;
}

void
wscroll(Window *win, int s)
{
    bscroll((Buffer*)win, s);
}