aboutsummaryrefslogtreecommitdiff
path: root/src/include/ipxe/widget.h
blob: 0d8af1030d99bdb391e7b7e36c6f3641451aecca (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
#ifndef _IPXE_WIDGET_H
#define _IPXE_WIDGET_H

/** @file
 *
 * Text widgets
 *
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

#include <curses.h>
#include <ipxe/list.h>

/** A text widget set */
struct widgets {
	/** List of widgets (in tab order) */
	struct list_head list;
};

/** A text widget */
struct widget {
	/** List of widgets (in tab order) */
	struct list_head list;
	/** Widget operations */
	struct widget_operations *op;

	/** Row */
	unsigned int row;
	/** Starting column */
	unsigned int col;
	/** Width */
	unsigned int width;
	/** Flags */
	unsigned int flags;
};

/** Text widget flags */
enum widget_flags {
	/** Widget may have input focus */
	WIDGET_EDITABLE = 0x0001,
	/** Widget contains a secret */
	WIDGET_SECRET = 0x0002,
};

/** Text widget operations */
struct widget_operations {
	/**
	 * Draw widget
	 *
	 * @v widget		Text widget
	 */
	void ( * draw ) ( struct widget *widget );
	/**
	 * Edit widget
	 *
	 * @v widget		Text widget
	 * @v key		Key pressed by user
	 * @ret key		Key returned to application, or zero
	 *
	 * This will not update the display: you must call the draw()
	 * method to ensure that any changes to an editable widget are
	 * displayed to the user.
	 */
	int ( * edit ) ( struct widget *widget, int key );
};

/**
 * Initialise text widget set
 *
 * @v widgets		Text widget set
 */
static inline __attribute__ (( always_inline )) void
init_widgets ( struct widgets *widgets ) {

	INIT_LIST_HEAD ( &widgets->list );
}

/**
 * Initialise text widget
 *
 * @v widget		Text widget
 * @v op		Text widget operations
 * @v row		Row
 * @v col		Starting column
 * @v width		Width
 */
static inline __attribute__ (( always_inline )) void
init_widget ( struct widget *widget, struct widget_operations *op,
	      unsigned int row, unsigned int col, unsigned int width,
	      unsigned int flags ) {

	widget->op = op;
	widget->row = row;
	widget->col = col;
	widget->width = width;
	widget->flags = flags;
}

/**
 * Append text widget
 *
 * @v widgets		Text widget set
 * @v widget		Text widget
 */
static inline __attribute__ (( always_inline )) void
add_widget ( struct widgets *widgets, struct widget *widget ) {

	list_add_tail ( &widget->list, &widgets->list );
}

/**
 * Draw text widget
 *
 * @v widget		Text widget
 */
static inline __attribute__ (( always_inline )) void
draw_widget ( struct widget *widget ) {

	widget->op->draw ( widget );
}

/**
 * Edit text widget
 *
 * @v widget		Text widget
 * @v key		Key pressed by user
 * @ret key		Key returned to application, or zero
 *
 * This will not update the display: you must call draw_widget() to
 * ensure that any changes to an editable widget are displayed to the
 * user.
 */
static inline __attribute__ (( always_inline )) int
edit_widget ( struct widget *widget, int key ) {

	return widget->op->edit ( widget, key );
}

extern int widget_ui ( struct widgets *widgets );

#endif /* _IPXE_WIDGET_H */