xref: /freebsd/contrib/bsddialog/lib/bsddialog.h (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2021-2022 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     "0.2"
34 
35 /* Exit status */
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_GENERIC1        6
46 #define BSDDIALOG_GENERIC2        7
47 
48 /* Size and position */
49 #define BSDDIALOG_FULLSCREEN     -1
50 #define BSDDIALOG_AUTOSIZE        0
51 #define BSDDIALOG_CENTER         -1
52 
53 /* Mixedgauge */
54 #define BSDDIALOG_MG_SUCCEEDED   -1
55 #define BSDDIALOG_MG_FAILED      -2
56 #define BSDDIALOG_MG_PASSED      -3
57 #define BSDDIALOG_MG_COMPLETED   -4
58 #define BSDDIALOG_MG_CHECKED     -5
59 #define BSDDIALOG_MG_DONE        -6
60 #define BSDDIALOG_MG_SKIPPED     -7
61 #define BSDDIALOG_MG_INPROGRESS  -8
62 #define BSDDIALOG_MG_BLANK       -9
63 #define BSDDIALOG_MG_NA          -10
64 #define BSDDIALOG_MG_PENDING     -11
65 
66 /* Form */
67 #define BSDDIALOG_FIELDHIDDEN    1U
68 #define BSDDIALOG_FIELDREADONLY  2U
69 
70 struct bsddialog_conf {
71 	bool ascii_lines;
72 	unsigned int auto_minheight;
73 	unsigned int auto_minwidth;
74 	const char *bottomtitle;
75 	bool clear;
76 	int *get_height;
77 	int *get_width;
78 	bool no_lines;
79 	bool shadow;
80 	unsigned int sleep;
81 	const char *title;
82 	int y;
83 	int x;
84 	struct {
85 		bool enable_esc;
86 		const char *f1_file;
87 		const char *f1_message;
88 	} key;
89 	struct {
90 		bool highlight;
91 		unsigned int tablen;
92 	} text;
93 	struct {
94 		bool align_left;
95 		bool no_desc;
96 		bool no_name;
97 		bool on_without_ok;
98 		bool shortcut_buttons;
99 	} menu;
100 	struct {
101 		bool enable_wchar;
102 		int  securech;
103 		bool value_without_ok;
104 	} form;
105 	struct {
106 		bool without_ok;
107 		const char *ok_label;
108 		bool with_extra;
109 		const char *extra_label;
110 		bool without_cancel;
111 		const char *cancel_label;
112 		bool default_cancel;
113 		bool with_help;
114 		const char *help_label;
115 		const char *generic1_label;
116 		const char *generic2_label;
117 		const char *default_label;
118 	} button;
119 };
120 
121 struct bsddialog_menuitem {
122 	const char *prefix;
123 	bool on;
124 	unsigned int depth;
125 	const char *name;
126 	const char *desc;
127 	const char *bottomdesc;
128 };
129 
130 enum bsddialog_grouptype {
131 	BSDDIALOG_CHECKLIST,
132 	BSDDIALOG_RADIOLIST,
133 	BSDDIALOG_SEPARATOR,
134 };
135 
136 struct bsddialog_menugroup {
137 	enum bsddialog_grouptype type;
138 	unsigned int nitems;
139 	struct bsddialog_menuitem *items;
140 };
141 
142 struct bsddialog_formitem {
143 	const char *label;
144 	unsigned int ylabel;
145 	unsigned int xlabel;
146 
147 	const char *init;
148 	unsigned int yfield;
149 	unsigned int xfield;
150 	unsigned int fieldlen;
151 	unsigned int maxvaluelen;
152 	char *value;
153 	unsigned int flags;
154 
155 	const char *bottomdesc;
156 };
157 
158 int bsddialog_init(void);
159 int bsddialog_end(void);
160 int bsddialog_backtitle(struct bsddialog_conf *conf, const char *backtitle);
161 int bsddialog_initconf(struct bsddialog_conf *conf);
162 int bsddialog_clearterminal(void);
163 const char *bsddialog_geterror(void);
164 
165 /* Dialogs */
166 int
167 bsddialog_checklist(struct bsddialog_conf *conf, const char *text, int rows,
168     int cols, unsigned int menurows, unsigned int nitems,
169     struct bsddialog_menuitem *items, int *focusitem);
170 
171 int
172 bsddialog_datebox(struct bsddialog_conf *conf, const char *text, int rows,
173     int cols, unsigned int *yy, unsigned int *mm, unsigned int *dd);
174 
175 int
176 bsddialog_form(struct bsddialog_conf *conf, const char *text, int rows,
177     int cols, unsigned int formheight, unsigned int nitems,
178     struct bsddialog_formitem *items);
179 
180 int
181 bsddialog_gauge(struct bsddialog_conf *conf, const char *text, int rows,
182     int cols, unsigned int perc, int fd, const char *sep);
183 
184 int
185 bsddialog_infobox(struct bsddialog_conf *conf, const char *text, int rows,
186     int cols);
187 
188 int
189 bsddialog_menu(struct bsddialog_conf *conf, const char *text, int rows,
190     int cols, unsigned int menurows, unsigned int nitems,
191     struct bsddialog_menuitem *items, int *focusitem);
192 
193 int
194 bsddialog_mixedgauge(struct bsddialog_conf *conf, const char *text, int rows,
195     int cols, unsigned int mainperc, unsigned int nminibars,
196     const char **minilabels, int *minipercs);
197 
198 int
199 bsddialog_mixedlist(struct bsddialog_conf *conf, const char *text, int rows,
200     int cols, unsigned int menurows, unsigned int ngroups,
201     struct bsddialog_menugroup *groups, int *focuslist, int *focusitem);
202 
203 int
204 bsddialog_msgbox(struct bsddialog_conf *conf, const char *text, int rows,
205     int cols);
206 
207 int
208 bsddialog_pause(struct bsddialog_conf *conf, const char *text, int rows,
209     int cols, unsigned int seconds);
210 
211 int
212 bsddialog_radiolist(struct bsddialog_conf *conf, const char *text, int rows,
213     int cols, unsigned int menurows, unsigned int nitems,
214     struct bsddialog_menuitem *items, int *focusitem);
215 
216 int
217 bsddialog_rangebox(struct bsddialog_conf *conf, const char *text, int rows,
218     int cols, int min, int max, int *value);
219 
220 int
221 bsddialog_textbox(struct bsddialog_conf *conf, const char *file, int rows,
222     int cols);
223 
224 int
225 bsddialog_timebox(struct bsddialog_conf *conf, const char *text, int rows,
226     int cols, unsigned int *hh, unsigned int *mm, unsigned int *ss);
227 
228 int
229 bsddialog_yesno(struct bsddialog_conf *conf, const char *text, int rows,
230     int cols);
231 
232 #endif
233