summaryrefslogtreecommitdiff
path: root/ui.c
blob: 6eeff83a414b2b4ab3306b62e33ce612723e5eac (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
/*
 * netsniff-ng - the packet sniffing beast
 * Subject to the GPL, version 2.
 */

#include <curses.h>

#include "ui.h"
#include "str.h"
#include "screen.h"
#include "xmalloc.h"
#include "urcu-list-compat.h"

static struct ui_text *ui_text_alloc(size_t len)
{
	struct ui_text *text = xzmalloc(sizeof(*text));

	text->str = xzmalloc(sizeof(chtype) * len + 1);
	text->len = len;

	return text;
}

static void ui_text_len_set(struct ui_text *text, size_t len)
{
	if (text->len == len)
		return;

	if (text->slen + len > text->len) {
		text->str = xrealloc(text->str, sizeof(chtype) * len + 1);
		text->len = len;
	}

	text->slen = min(len, text->slen);
	text->str[text->slen] = 0;
}

static void ui_text_attr_insert(struct ui_text *text, int idx, int attr, const char *str)
{
	size_t slen = strlen(str);
	uint32_t i, j;

	if (idx + slen > text->len)
		ui_text_len_set(text, idx + slen);

	for (j = 0, i = idx; i < idx + slen; i++, j++)
		text->str[i] = str[j] | attr;
}

static void ui_text_free(struct ui_text *text)
{
	xfree(text->str);
	xfree(text);
}

void ui_table_init(struct ui_table *tbl)
{
	memset(tbl, 0, sizeof(*tbl));

	getsyx(tbl->y, tbl->x);

	tbl->rows_y  = tbl->y;
	tbl->width   = COLS;
	tbl->height  = LINES - 2;
	tbl->col_pad = 1;
	tbl->row     = ui_text_alloc(tbl->width);
	tbl->delim   = " ";

	CDS_INIT_LIST_HEAD(&tbl->cols);
}

void ui_table_uninit(struct ui_table *tbl)
{
	struct ui_col *col, *tmp;

	cds_list_for_each_entry_safe(col, tmp, &tbl->cols, entry)
		xfree(col);

	ui_text_free(tbl->row);
}

void ui_table_pos_set(struct ui_table *tbl, int y, int x)
{
	tbl->y      = y;
	tbl->x      = x;
	tbl->rows_y = y;
}

static struct ui_col *ui_table_col_get(struct ui_table *tbl, uint32_t id)
{
	struct ui_col *col;

	cds_list_for_each_entry(col, &tbl->cols, entry) {
		if (col->id == id)
			return col;
	}

	bug();
}

static void __ui_table_pos_update(struct ui_table *tbl)
{
	struct ui_col *col;
	uint32_t pos = 0;

	cds_list_for_each_entry(col, &tbl->cols, entry) {
		col->pos  = pos;
		pos      += col->len + tbl->col_pad;
	}
}

void ui_table_col_add(struct ui_table *tbl, uint32_t id, const char *name, uint32_t len)
{
	struct ui_col *col = xzmalloc(sizeof(*col));

	col->id    = id;
	col->name  = name;
	col->len   = len;
	col->align = UI_ALIGN_LEFT;

	cds_list_add_tail(&col->entry, &tbl->cols);

	__ui_table_pos_update(tbl);
}

void ui_table_col_color_set(struct ui_table *tbl, int col_id, int color)
{
	struct ui_col *col = ui_table_col_get(tbl, col_id);

	col->color = color;
}

void ui_table_col_align_set(struct ui_table *tbl, int col_id, enum ui_align align)
{
	struct ui_col *col = ui_table_col_get(tbl, col_id);

	col->align = align;
}

void ui_table_col_delim_set(struct ui_table *tbl, const char *delim)
{
	tbl->delim = delim;
}

void ui_table_row_add(struct ui_table *tbl)
{
	tbl->rows_y++;
}

void ui_table_clear(struct ui_table *tbl)
{
	int y;

	tbl->rows_y = tbl->y;

	for (y = tbl->y + 1; y < tbl->y + tbl->height; y++) {
		mvprintw(y, tbl->x, "%*s", tbl->width, " ");
	}
}

#define UI_ALIGN_COL(col) (((col)->align == UI_ALIGN_LEFT) ? "%-*.*s" : "%*.*s")

void ui_table_row_show(struct ui_table *tbl)
{
	mvaddchstr(tbl->rows_y, tbl->x, tbl->row->str + tbl->scroll_x);
	ui_text_len_set(tbl->row, 0);
}

static void __ui_table_row_print(struct ui_table *tbl, struct ui_col *col,
				 int color, const char *str)
{
	char tmp[128];

	slprintf(tmp, sizeof(tmp), UI_ALIGN_COL(col), col->len, col->len, str);
	ui_text_attr_insert(tbl->row, col->pos, color, tmp);

	slprintf(tmp, sizeof(tmp), "%*s", tbl->col_pad, tbl->delim);
	ui_text_attr_insert(tbl->row, col->pos + col->len, color, tmp);
}

void ui_table_row_col_set(struct ui_table *tbl, uint32_t col_id, const char *str)
{
	struct ui_col *col = ui_table_col_get(tbl, col_id);

	__ui_table_row_print(tbl, col, col->color, str);
}

void ui_table_header_color_set(struct ui_table *tbl, int color)
{
	tbl->hdr_color = color;
}

void ui_table_height_set(struct ui_table *tbl, int height)
{
	tbl->height = height;
}

void ui_table_header_print(struct ui_table *tbl)
{
	struct ui_col *col;

	attron(tbl->hdr_color);
	mvprintw(tbl->y, tbl->x, "%*s", tbl->width, " ");
	attroff(tbl->hdr_color);

	cds_list_for_each_entry(col, &tbl->cols, entry) {
		__ui_table_row_print(tbl, col, tbl->hdr_color, col->name);
	}

	ui_table_row_show(tbl);
}

#define SCROLL_X_STEP 10

void ui_table_event_send(struct ui_table *tbl, enum ui_event_id evt_id)
{
	switch (evt_id) {
	case UI_EVT_SCROLL_RIGHT:
		tbl->scroll_x += SCROLL_X_STEP;
		break;

	case UI_EVT_SCROLL_LEFT:
		tbl->scroll_x -= SCROLL_X_STEP;
		if (tbl->scroll_x < 0)
			tbl->scroll_x = 0;
		break;

	case UI_EVT_SCROLL_UP:
		tbl->scroll_y--;
		if (tbl->scroll_y < 0)
			tbl->scroll_y = 0;
		break;

	case UI_EVT_SCROLL_DOWN:
		tbl->scroll_y++;
		break;

	default: /* pass the rest events */
		return;
	}
}

void ui_table_data_iter_set(struct ui_table *tbl, void * (* iter)(void *data))
{
	tbl->data_iter = iter;
}

void ui_table_data_bind_set(struct ui_table *tbl,
			    void (* bind)(struct ui_table *tbl, const void *data))
{
	tbl->data_bind = bind;
}

void ui_table_data_bind(struct ui_table *tbl)
{
	void *data;
	int i = 0;

	bug_on(!tbl);
	bug_on(!tbl->data_iter);
	bug_on(!tbl->data_bind);

	ui_table_clear(tbl);
	ui_table_header_print(tbl);

	tbl->data_count = 0;

	data = tbl->data_iter(NULL);
	for (; data; data = tbl->data_iter(data)) {
		tbl->data_count++;

		if (i++ < tbl->scroll_y)
			continue;

		tbl->data_bind(tbl, data);
	}

	if (tbl->scroll_y > i)
		tbl->scroll_y = i;
}

int ui_table_data_count(struct ui_table *tbl)
{
	return tbl->data_count;
}

int ui_table_scroll_height(struct ui_table *tbl)
{
	return tbl->scroll_y;
}

struct ui_tab *ui_tab_create(void)
{
	struct ui_tab *tab;

	tab = xzmalloc(sizeof(*tab));

	ui_table_init(&tab->tbl);
	ui_table_col_delim_set(&tab->tbl, "|");
	tab->tbl.width = 0;

	return tab;
}

void ui_tab_destroy(struct ui_tab *tab)
{
	ui_table_uninit(&tab->tbl);
	xfree(tab);
}

void ui_tab_pos_set(struct ui_tab *tab, int y, int x)
{
	ui_table_pos_set(&tab->tbl, y, x);
}

void ui_tab_event_cb_set(struct ui_tab *tab, ui_tab_event_cb cb)
{
	tab->on_tab_event = cb;
}

void ui_tab_active_color_set(struct ui_tab *tab, int color)
{
	ui_table_header_color_set(&tab->tbl, color);
	tab->color = color;
}

void ui_tab_show(struct ui_tab *tab)
{
	struct ui_col *col;

	if (tab->on_tab_event)
		tab->on_tab_event(tab, UI_TAB_EVT_OPEN, tab->active->id);

	cds_list_for_each_entry(col, &tab->tbl.cols, entry)
		__ui_table_row_print(&tab->tbl, col, col->color, col->name);

	ui_table_row_show(&tab->tbl);
}

void ui_tab_entry_add(struct ui_tab *tab, uint32_t id, const char *name)
{
	struct ui_col *col;

	ui_table_col_add(&tab->tbl, id, name, strlen(name) + 1);

	col = ui_table_col_get(&tab->tbl, id);

	if (!tab->active)
		tab->active = col;

	if (tab->active == col)
		ui_table_col_color_set(&tab->tbl, id, tab->color);
	else
		ui_table_col_color_set(&tab->tbl, id, tab->color | A_REVERSE);
}

void ui_tab_event_send(struct ui_tab *tab, uint32_t id)
{
	struct ui_col *curr, *next;

	if (id != UI_EVT_SELECT_NEXT)
		return;

	curr = tab->active;

	if (curr == cds_list_last_entry(&tab->tbl.cols, struct ui_col, entry))
		next = cds_list_first_entry(&tab->tbl.cols, struct ui_col, entry);
	else
		next = cds_list_next_entry(curr, entry);

	curr->color = tab->color | A_REVERSE;
	next->color = tab->color;

	tab->active = next;
}