xref: /freebsd/contrib/bsddialog/lib/bsddialog.h (revision 6e71235e558ef579605e7f35b02f983b9a246a4a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2021-2023 Alfonso Sabato Siciliano
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #ifndef _LIBBSDDIALOG_H_
29 #define _LIBBSDDIALOG_H_
30 
31 #include <stdbool.h>
32 
33 #define LIBBSDDIALOG_VERSION     "1.0"
34 
35 /* Return values */
36 #define BSDDIALOG_ERROR          -1
37 #define BSDDIALOG_OK              0
38 #define BSDDIALOG_YES             BSDDIALOG_OK
39 #define BSDDIALOG_CANCEL          1
40 #define BSDDIALOG_NO              BSDDIALOG_CANCEL
41 #define BSDDIALOG_HELP            2
42 #define BSDDIALOG_EXTRA           3
43 #define BSDDIALOG_TIMEOUT         4
44 #define BSDDIALOG_ESC             5
45 #define BSDDIALOG_LEFT1           6
46 #define BSDDIALOG_LEFT2           7
47 #define BSDDIALOG_LEFT3           8
48 #define BSDDIALOG_RIGHT1          9
49 #define BSDDIALOG_RIGHT2          10
50 #define BSDDIALOG_RIGHT3          11
51 
52 /* Size and position */
53 #define BSDDIALOG_FULLSCREEN     -1
54 #define BSDDIALOG_AUTOSIZE        0
55 #define BSDDIALOG_CENTER         -1
56 
57 /* Mixedgauge */
58 #define BSDDIALOG_MG_SUCCEEDED   -1
59 #define BSDDIALOG_MG_FAILED      -2
60 #define BSDDIALOG_MG_PASSED      -3
61 #define BSDDIALOG_MG_COMPLETED   -4
62 #define BSDDIALOG_MG_CHECKED     -5
63 #define BSDDIALOG_MG_DONE        -6
64 #define BSDDIALOG_MG_SKIPPED     -7
65 #define BSDDIALOG_MG_INPROGRESS  -8
66 #define BSDDIALOG_MG_BLANK       -9
67 #define BSDDIALOG_MG_NA          -10
68 #define BSDDIALOG_MG_PENDING     -11
69 
70 /* Form */
71 #define BSDDIALOG_FIELDHIDDEN      1U
72 #define BSDDIALOG_FIELDREADONLY    2U
73 #define BSDDIALOG_FIELDNOCOLOR     4U
74 #define BSDDIALOG_FIELDCURSOREND   8U
75 #define BSDDIALOG_FIELDEXTEND      16U
76 #define BSDDIALOG_FIELDSINGLEBYTE  32U
77 
78 struct bsddialog_conf {
79 	bool ascii_lines;
80 	unsigned int auto_minheight;
81 	unsigned int auto_minwidth;
82 	unsigned int auto_topmargin;
83 	unsigned int auto_downmargin;
84 	const char *bottomtitle;
85 	bool clear;
86 	int *get_height;
87 	int *get_width;
88 	bool no_lines;
89 	bool shadow;
90 	unsigned int sleep;
91 	const char *title;
92 	int y;
93 	int x;
94 	struct {
95 		bool enable_esc;
96 		const char *f1_file;
97 		const char *f1_message;
98 	} key;
99 	struct {
100 		unsigned int cols_per_row;
101 		bool escape;
102 		unsigned int tablen;
103 	} text;
104 	struct {
105 		bool align_left;
106 		bool no_desc;
107 		bool no_name;
108 		bool shortcut_buttons;
109 	} menu;
110 	struct {
111 		char securech;
112 		char *securembch;
113 		bool value_wchar;
114 	} form;
115 	struct {
116 		const char *format;
117 	} date;
118 	struct {
119 		bool always_active;
120 		const char *left1_label;
121 		const char *left2_label;
122 		const char *left3_label;
123 		bool without_ok;
124 		const char *ok_label;
125 		bool with_extra;
126 		const char *extra_label;
127 		bool without_cancel;
128 		const char *cancel_label;
129 		bool default_cancel;
130 		bool with_help;
131 		const char *help_label;
132 		const char *right1_label;
133 		const char *right2_label;
134 		const char *right3_label;
135 		const char *default_label;
136 	} button;
137 };
138 
139 struct bsddialog_menuitem {
140 	const char *prefix;
141 	bool on;
142 	unsigned int depth;
143 	const char *name;
144 	const char *desc;
145 	const char *bottomdesc;
146 };
147 
148 enum bsddialog_menutype {
149 	BSDDIALOG_CHECKLIST,
150 	BSDDIALOG_RADIOLIST,
151 	BSDDIALOG_SEPARATOR,
152 };
153 
154 struct bsddialog_menugroup {
155 	enum bsddialog_menutype type;
156 	unsigned int nitems;
157 	struct bsddialog_menuitem *items;
158 	unsigned int min_on; /* unused for now */
159 };
160 
161 struct bsddialog_formitem {
162 	const char *label;
163 	unsigned int ylabel;
164 	unsigned int xlabel;
165 
166 	const char *init;
167 	unsigned int yfield;
168 	unsigned int xfield;
169 	unsigned int fieldlen;
170 	unsigned int maxvaluelen;
171 	char *value;
172 	unsigned int flags;
173 
174 	const char *bottomdesc;
175 };
176 
177 int bsddialog_init(void);
178 int bsddialog_init_notheme(void);
179 bool bsddialog_inmode(void);
180 int bsddialog_end(void);
181 int bsddialog_backtitle(struct bsddialog_conf *conf, const char *backtitle);
182 int bsddialog_initconf(struct bsddialog_conf *conf);
183 void bsddialog_clear(unsigned int y);
184 void bsddialog_refresh(void);
185 const char *bsddialog_geterror(void);
186 
187 /* Dialogs */
188 int
189 bsddialog_calendar(struct bsddialog_conf *conf, const char *text, int rows,
190     int cols, unsigned int *year, unsigned int *month, unsigned int *day);
191 
192 int
193 bsddialog_checklist(struct bsddialog_conf *conf, const char *text, int rows,
194     int cols, unsigned int menurows, unsigned int nitems,
195     struct bsddialog_menuitem *items, int *focusitem);
196 
197 int
198 bsddialog_datebox(struct bsddialog_conf *conf, const char *text, int rows,
199     int cols, unsigned int *year, unsigned int *month, unsigned int *day);
200 
201 int
202 bsddialog_form(struct bsddialog_conf *conf, const char *text, int rows,
203     int cols, unsigned int formheight, unsigned int nitems,
204     struct bsddialog_formitem *items, int *focusitem);
205 
206 int
207 bsddialog_gauge(struct bsddialog_conf *conf, const char *text, int rows,
208     int cols, unsigned int perc, int fd, const char *sep, const char *end);
209 
210 int
211 bsddialog_infobox(struct bsddialog_conf *conf, const char *text, int rows,
212     int cols);
213 
214 int
215 bsddialog_menu(struct bsddialog_conf *conf, const char *text, int rows,
216     int cols, unsigned int menurows, unsigned int nitems,
217     struct bsddialog_menuitem *items, int *focusitem);
218 
219 int
220 bsddialog_mixedgauge(struct bsddialog_conf *conf, const char *text, int rows,
221     int cols, unsigned int mainperc, unsigned int nminibars,
222     const char **minilabels, int *minipercs);
223 
224 int
225 bsddialog_mixedlist(struct bsddialog_conf *conf, const char *text, int rows,
226     int cols, unsigned int menurows, unsigned int ngroups,
227     struct bsddialog_menugroup *groups, int *focuslist, int *focusitem);
228 
229 int
230 bsddialog_msgbox(struct bsddialog_conf *conf, const char *text, int rows,
231     int cols);
232 
233 int
234 bsddialog_pause(struct bsddialog_conf *conf, const char *text, int rows,
235     int cols, unsigned int *seconds);
236 
237 int
238 bsddialog_radiolist(struct bsddialog_conf *conf, const char *text, int rows,
239     int cols, unsigned int menurows, unsigned int nitems,
240     struct bsddialog_menuitem *items, int *focusitem);
241 
242 int
243 bsddialog_rangebox(struct bsddialog_conf *conf, const char *text, int rows,
244     int cols, int min, int max, int *value);
245 
246 int
247 bsddialog_textbox(struct bsddialog_conf *conf, const char *file, int rows,
248     int cols);
249 
250 int
251 bsddialog_timebox(struct bsddialog_conf *conf, const char *text, int rows,
252     int cols, unsigned int *hh, unsigned int *mm, unsigned int *ss);
253 
254 int
255 bsddialog_yesno(struct bsddialog_conf *conf, const char *text, int rows,
256     int cols);
257 
258 #endif
259