mz: Check device argument by ifindex instead of name prefix
[netsniff-ng.git] / ui.c
blob217796401bb1e64c2734b6b8c780c604b5bad71f
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Subject to the GPL, version 2.
4 */
6 #include <curses.h>
8 #include "ui.h"
9 #include "str.h"
10 #include "xmalloc.h"
12 static struct ui_text *ui_text_alloc(size_t len)
14 struct ui_text *text = xzmalloc(sizeof(*text));
16 text->str = xzmalloc(sizeof(chtype) * len + 1);
17 text->len = len;
19 return text;
22 static void ui_text_len_set(struct ui_text *text, size_t len)
24 if (text->len == len)
25 return;
27 if (text->slen + len > text->len) {
28 text->str = xrealloc(text->str, sizeof(chtype) * len + 1);
29 text->len = len;
32 text->slen = min(len, text->slen);
33 text->str[text->slen] = 0;
36 static void ui_text_attr_insert(struct ui_text *text, int idx, int attr, const char *str)
38 size_t slen = strlen(str);
39 uint32_t i, j;
41 if (idx + slen > text->len)
42 ui_text_len_set(text, idx + slen);
44 for (j = 0, i = idx; i < idx + slen; i++, j++)
45 text->str[i] = str[j] | attr;
48 static void ui_text_free(struct ui_text *text)
50 xfree(text->str);
51 xfree(text);
54 void ui_table_init(struct ui_table *tbl)
56 memset(tbl, 0, sizeof(*tbl));
58 getsyx(tbl->y, tbl->x);
60 tbl->rows_y = tbl->y;
61 tbl->width = COLS;
62 tbl->height = LINES - 2;
63 tbl->col_pad = 1;
64 tbl->row = ui_text_alloc(tbl->width);
66 INIT_LIST_HEAD(&tbl->cols);
69 void ui_table_uninit(struct ui_table *tbl)
71 struct ui_col *col, *tmp;
73 list_for_each_entry_safe(col, tmp, &tbl->cols, entry)
74 xfree(col);
76 ui_text_free(tbl->row);
79 void ui_table_pos_set(struct ui_table *tbl, int y, int x)
81 tbl->y = y;
82 tbl->x = x;
83 tbl->rows_y = y;
86 static struct ui_col *ui_table_col_get(struct ui_table *tbl, uint32_t id)
88 struct ui_col *col;
90 list_for_each_entry(col, &tbl->cols, entry) {
91 if (col->id == id)
92 return col;
95 bug();
98 static void __ui_table_pos_update(struct ui_table *tbl)
100 struct ui_col *col;
101 uint32_t pos = 0;
103 list_for_each_entry(col, &tbl->cols, entry) {
104 col->pos = pos;
105 pos += col->len + tbl->col_pad;
109 void ui_table_col_add(struct ui_table *tbl, uint32_t id, char *name, uint32_t len)
111 struct ui_col *col = xzmalloc(sizeof(*col));
113 col->id = id;
114 col->name = name;
115 col->len = len;
116 col->align = UI_ALIGN_LEFT;
118 list_add_tail(&col->entry, &tbl->cols);
120 __ui_table_pos_update(tbl);
123 void ui_table_col_color_set(struct ui_table *tbl, int col_id, int color)
125 struct ui_col *col = ui_table_col_get(tbl, col_id);
127 col->color = color;
130 void ui_table_col_align_set(struct ui_table *tbl, int col_id, enum ui_align align)
132 struct ui_col *col = ui_table_col_get(tbl, col_id);
134 col->align = align;
137 void ui_table_row_add(struct ui_table *tbl)
139 tbl->rows_y++;
142 void ui_table_clear(struct ui_table *tbl)
144 int y;
146 tbl->rows_y = tbl->y;
148 for (y = tbl->y + 1; y < tbl->y + tbl->height; y++) {
149 mvprintw(y, tbl->x, "%*s", tbl->width, " ");
153 #define UI_ALIGN_COL(col) (((col)->align == UI_ALIGN_LEFT) ? "%-*.*s" : "%*.*s")
155 void ui_table_row_show(struct ui_table *tbl)
157 mvaddchstr(tbl->rows_y, tbl->x, tbl->row->str + tbl->scroll_x);
158 ui_text_len_set(tbl->row, 0);
161 static void __ui_table_row_print(struct ui_table *tbl, struct ui_col *col,
162 int color, const char *str)
164 char tmp[128];
166 slprintf(tmp, sizeof(tmp), UI_ALIGN_COL(col), col->len, col->len, str);
167 ui_text_attr_insert(tbl->row, col->pos, color, tmp);
169 slprintf(tmp, sizeof(tmp), "%*s", tbl->col_pad, " ");
170 ui_text_attr_insert(tbl->row, col->pos + col->len, color, tmp);
173 void ui_table_row_col_set(struct ui_table *tbl, uint32_t col_id, const char *str)
175 struct ui_col *col = ui_table_col_get(tbl, col_id);
177 __ui_table_row_print(tbl, col, col->color, str);
180 void ui_table_header_color_set(struct ui_table *tbl, int color)
182 tbl->hdr_color = color;
185 void ui_table_height_set(struct ui_table *tbl, int height)
187 tbl->height = height;
190 void ui_table_header_print(struct ui_table *tbl)
192 struct ui_col *col;
194 attron(tbl->hdr_color);
195 mvprintw(tbl->y, tbl->x, "%*s", tbl->width, " ");
196 attroff(tbl->hdr_color);
198 list_for_each_entry(col, &tbl->cols, entry) {
199 __ui_table_row_print(tbl, col, tbl->hdr_color, col->name);
203 ui_table_row_show(tbl);
206 #define SCROLL_X_STEP 10
208 void ui_table_event_send(struct ui_table *tbl, enum ui_event_id evt_id)
210 if (evt_id == UI_EVT_SCROLL_RIGHT) {
211 tbl->scroll_x += SCROLL_X_STEP;
212 } else if (evt_id == UI_EVT_SCROLL_LEFT) {
213 tbl->scroll_x -= SCROLL_X_STEP;
214 if (tbl->scroll_x < 0)
215 tbl->scroll_x = 0;