File: conf_scr.c

package info (click to toggle)
wavemon 0.8.2-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 848 kB
  • sloc: ansic: 5,273; sh: 3,062; makefile: 59
file content (229 lines) | stat: -rw-r--r-- 5,759 bytes parent folder | download | duplicates (2)
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
/*
 * wavemon - a wireless network monitoring aplication
 *
 * Copyright (c) 2001-2002 Jan Morgenstern <jan@jm-music.de>
 *
 * wavemon is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2, or (at your option) any later
 * version.
 *
 * wavemon is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along
 * with wavemon; see the file COPYING.  If not, write to the Free Software
 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#include "wavemon.h"

/* Make configuration screen fit into half of minimum screen width */
#define CONF_SCREEN_WIDTH	(MIN_SCREEN_COLS / 2)
/*
 * Maximum number of rows in the configuration box. Due to the top
 * border, this is one less than the maximum vertical number of rows.
 */
#define MAX_NUM_CONF_ROWS	(MAXYLEN - 1)

/* GLOBALS */
extern int conf_items;		/* index into array storing menu items */

static WINDOW *w_conf, *w_confpad;
static int first_item, active_item;
static int num_items, list_offset;

static void waddstr_item(WINDOW *w, int y, struct conf_item *item, char hilight)
{
	char s[0x40];

	wattrset(w, COLOR_PAIR(CP_PREF_NORMAL));

	mvwhline(w, y, 0, ' ', CONF_SCREEN_WIDTH);

	if (item->type != t_sep && item->type != t_func) {
		mvwaddstr(w, y, item->dep ? 2 : 0, item->name);

		switch (item->type) {
		case t_int:
			sprintf(s, "%d", *item->v.i);
			break;
		case t_list:
			assert(item->list && item->list[*item->v.i]);
			strncpy(s, item->list[*item->v.i], sizeof(s));
			/* Fall through, dummy statements to pacify gcc -Wall */
		case t_sep:
		case t_func:
			break;
		}

		if (!item->unit) {
			wmove(w, y, CONF_SCREEN_WIDTH - strlen(s));
			if (hilight) {
				wattron(w, A_REVERSE);
				waddstr_b(w, s);
				wattroff(w, A_REVERSE);
			} else
				waddstr_b(w, s);
		} else {
			wmove(w, y, CONF_SCREEN_WIDTH - strlen(s) - strlen(item->unit) - 1);
			if (hilight) {
				wattron(w, A_REVERSE);
				waddstr_b(w, s);
				wattroff(w, A_REVERSE);
			} else
				waddstr_b(w, s);
			mvwaddstr(w, y, CONF_SCREEN_WIDTH - strlen(item->unit), item->unit);
		}
	} else if (item->type == t_sep && item->name) {
		sprintf(s, "- %s -", item->name);
		mvwaddstr(w, y, (CONF_SCREEN_WIDTH  - strlen(s)) / 2, s);
	} else if (item->type == t_func) {
		wmove(w, y, (CONF_SCREEN_WIDTH - strlen(item->name)) / 2);
		if (hilight) {
			wattron(w, A_REVERSE);
			waddstr(w, item->name);
			wattroff(w, A_REVERSE);
		} else
			waddstr(w, item->name);
	}
}

static void change_item(int inum, char sign)
{
	struct conf_item *item = ll_get(conf_items, inum);
	int tmp;

	switch (item->type) {
	case t_int:
		if (*item->v.i + item->inc * sign <= item->max &&
		    *item->v.i + item->inc * sign >= item->min)
			*item->v.i += item->inc * sign;
		break;
	case t_list:
		*item->v.i = *item->v.i + sign;
		tmp = argv_count(item->list);
		if (*item->v.i >= tmp)
			*item->v.i = 0;
		else if (*item->v.i < 0)
			*item->v.i = tmp - 1;
		/* Fall through, dummy statements to pacify gcc -Wall */
	case t_sep:
	case t_func:
		break;
	}
}


static int select_item(int rv, int incr)
{
	struct conf_item *item;

	do {
		rv  += incr;
		item = ll_get(conf_items, rv);

	} while (item->type == t_sep || (item->dep && !*item->dep));

	return rv;
}

/* Perform selection, return offset value to ensure pad fits inside window */
static int m_pref(WINDOW *w_conf, int list_offset, int active_item, int num_items)
{
	int active_line, i, j;

	werase(w_conf);

	for (active_line = i = j = 0; i < num_items; i++) {
		struct conf_item *item = ll_get(conf_items, i);

		if (!item->dep || *item->dep) {
			if (i != active_item)
				waddstr_item(w_conf, j++, item, 0);
			else {
				waddstr_item(w_conf, j, item, 1);
				active_line = j++;
			}
		}
	}

	if (active_line - list_offset > MAX_NUM_CONF_ROWS)
		return active_line - MAX_NUM_CONF_ROWS;
	if (active_line < list_offset)
		return active_line;
	return list_offset;
}

void scr_conf_init(void)
{
	struct conf_item *item;
	conf_get_interface_list();	/* may have changed in the meantime */

	num_items = ll_size(conf_items);
	w_conf    = newwin_title(0, WAV_HEIGHT, "Preferences", false);
	w_confpad = newpad(num_items + 1, CONF_SCREEN_WIDTH);

	if (first_item)			/* already initialized */
		return;
	while ((item = ll_get(conf_items, first_item)) && item->type == t_sep)
		first_item++;
	active_item = first_item;
}

int scr_conf_loop(WINDOW *w_menu)
{
	struct conf_item *item;
	int key;

	list_offset = m_pref(w_confpad, list_offset, active_item, num_items);

	prefresh(w_confpad, list_offset, 0,
		 1,       (WAV_WIDTH - CONF_SCREEN_WIDTH)/2,
		 MAXYLEN, (WAV_WIDTH + CONF_SCREEN_WIDTH)/2);
	wrefresh(w_conf);

	key = wgetch(w_menu);
	switch (key) {
	case KEY_HOME:
		active_item = first_item;
		break;
	case KEY_END:
		active_item = num_items - 1;
		break;
	case KEY_DOWN:
	case KEY_NPAGE:
		active_item = select_item(active_item, 1);
		if (active_item >= num_items) {
			active_item = first_item;
			list_offset = 0;
		}
		break;
	case KEY_UP:
	case KEY_PPAGE:
		active_item = select_item(active_item, -1);
		if (active_item < first_item)
			active_item = num_items - 1;
		break;
	case KEY_LEFT:
		change_item(active_item, -1);
		break;
	case KEY_RIGHT:
		change_item(active_item, 1);
		break;
	case '\r':
		item = ll_get(conf_items, active_item);
		if (item->type == t_func) {
			flash();
			(*item->v.fp)();
		}
	}
	return key;
}

void scr_conf_fini(void)
{
	delwin(w_conf);
	delwin(w_confpad);
}