aboutsummaryrefslogtreecommitdiff
path: root/src/hci/tui/widget_ui.c
blob: 5079eefdd7b9a61a4022bff5bf8307794101ff4f (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
/*
 * Copyright (C) 2024 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program 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 of the
 * License, or any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * You can also choose to distribute this program under the terms of
 * the Unmodified Binary Distribution Licence (as given in the file
 * COPYING.UBDL), provided that you have satisfied its requirements.
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

/** @file
 *
 * Text widget UI
 *
 */

#include <errno.h>
#include <curses.h>
#include <ipxe/ansicol.h>
#include <ipxe/widget.h>

/**
 * Find editable widget in widget set
 *
 * @v widgets		Text widget set
 * @v index		Editable widget index
 * @ret widget		Editable widget, or NULL
 */
static struct widget * find_widget ( struct widgets *widgets,
				     unsigned int index ) {
	struct widget *widget;

	list_for_each_entry ( widget, &widgets->list, list ) {
		if ( ! ( widget->flags & WIDGET_EDITABLE ) )
			continue;
		if ( index-- == 0 )
			return widget;
	}
	return NULL;
}

/**
 * Text widget user interface main loop
 *
 * @v widgets		Text widget set
 * @ret rc		Return status code
 */
static int widget_ui_loop ( struct widgets *widgets ) {
	struct widget *widget;
	unsigned int current;
	unsigned int count;
	int key;

	/* Draw all widgets */
	list_for_each_entry ( widget, &widgets->list, list )
		draw_widget ( widgets, widget );

	/* Count editable widgets */
	count = 0;
	while ( find_widget ( widgets, count ) != NULL )
		count++;

	/* Main loop */
	current = 0;
	while ( 1 ) {

		/* Identify current widget */
		widget = find_widget ( widgets, current );
		if ( ! widget )
			return -ENOENT;

		/* Redraw current widget */
		draw_widget ( widgets, widget );

		/* Process keypress */
		key = edit_widget ( widgets, widget, getkey ( 0 ) );
		switch ( key ) {
		case KEY_UP:
			if ( current > 0 )
				current--;
			break;
		case KEY_DOWN:
			if ( ++current == count )
				current--;
			break;
		case TAB:
			if ( ++current == count )
				current = 0;
			break;
		case KEY_ENTER:
			current++;
			if ( current >= count )
				return 0;
			break;
		case CTRL_C:
		case ESC:
			return -ECANCELED;
		default:
			/* Do nothing for unrecognised keys or edit errors */
			break;
		}
	}
}

/**
 * Present text widget user interface
 *
 * @v widgets		Text widget set
 * @ret rc		Return status code
 */
int widget_ui ( struct widgets *widgets ) {
	int rc;

	/* Initialise UI */
	initscr();
	start_color();
	color_set ( CPAIR_NORMAL, NULL );
	erase();

	/* Run main loop */
	rc = widget_ui_loop ( widgets );

	/* Terminate UI */
	color_set ( CPAIR_NORMAL, NULL );
	endwin();

	return rc;
}