xref: /freebsd/contrib/bsddialog/lib/bsddialog.h (revision c03c5b1c)
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.1"
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 	const char *f1_file;
77 	const char *f1_message;
78 	int *get_height;
79 	int *get_width;
80 	bool no_lines;
81 	bool shadow;
82 	unsigned int sleep;
83 	const char *title;
84 	int y;
85 	int x;
86 	struct {
87 		bool enable_esc;
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 		int  securech;
102 		bool value_without_ok;
103 	} form;
104 	struct {
105 		bool without_ok;
106 		const char *ok_label;
107 		bool with_extra;
108 		const char *extra_label;
109 		bool without_cancel;
110 		const char *cancel_label;
111 		bool default_cancel;
112 		bool with_help;
113 		const char *help_label;
114 		const char *generic1_label;
115 		const char *generic2_label;
116 		const char *default_label;
117 	} button;
118 };
119 
120 struct bsddialog_menuitem {
121 	const char *prefix;
122 	bool on;
123 	unsigned int depth;
124 	const char *name;
125 	const char *desc;
126 	const char *bottomdesc;
127 };
128 
129 enum bsddialog_grouptype {
130 	BSDDIALOG_CHECKLIST,
131 	BSDDIALOG_RADIOLIST,
132 	BSDDIALOG_SEPARATOR,
133 };
134 
135 struct bsddialog_menugroup {
136 	enum bsddialog_grouptype type;
137 	unsigned int nitems;
138 	struct bsddialog_menuitem *items;
139 };
140 
141 struct bsddialog_formitem {
142 	const char *label;
143 	unsigned int ylabel;
144 	unsigned int xlabel;
145 
146 	const char *init;
147 	unsigned int yfield;
148 	unsigned int xfield;
149 	unsigned int fieldlen;
150 	unsigned int maxvaluelen;
151 	char *value;
152 	unsigned int flags;
153 
154 	const char *bottomdesc;
155 };
156 
157 int bsddialog_init(void);
158 int bsddialog_end(void);
159 int bsddialog_backtitle(struct bsddialog_conf *conf, const char *backtitle);
160 int bsddialog_initconf(struct bsddialog_conf *conf);
161 int bsddialog_clearterminal(void);
162 const char *bsddialog_geterror(void);
163 
164 /* Dialogs */
165 int
166 bsddialog_checklist(struct bsddialog_conf *conf, const char *text, int rows,
167     int cols, unsigned int menurows, unsigned int nitems,
168     struct bsddialog_menuitem *items, int *focusitem);
169 
170 int
171 bsddialog_datebox(struct bsddialog_conf *conf, const char *text, int rows,
172     int cols, unsigned int *yy, unsigned int *mm, unsigned int *dd);
173 
174 int
175 bsddialog_form(struct bsddialog_conf *conf, const char *text, int rows,
176     int cols, unsigned int formheight, unsigned int nitems,
177     struct bsddialog_formitem *items);
178 
179 int
180 bsddialog_gauge(struct bsddialog_conf *conf, const char *text, int rows,
181     int cols, unsigned int perc, int fd, const char *sep);
182 
183 int
184 bsddialog_infobox(struct bsddialog_conf *conf, const char *text, int rows,
185     int cols);
186 
187 int
188 bsddialog_menu(struct bsddialog_conf *conf, const char *text, int rows,
189     int cols, unsigned int menurows, unsigned int nitems,
190     struct bsddialog_menuitem *items, int *focusitem);
191 
192 int
193 bsddialog_mixedgauge(struct bsddialog_conf *conf, const char *text, int rows,
194     int cols, unsigned int mainperc, unsigned int nminibars,
195     const char **minilabels, int *minipercs);
196 
197 int
198 bsddialog_mixedlist(struct bsddialog_conf *conf, const char *text, int rows,
199     int cols, unsigned int menurows, unsigned int ngroups,
200     struct bsddialog_menugroup *groups, int *focuslist, int *focusitem);
201 
202 int
203 bsddialog_msgbox(struct bsddialog_conf *conf, const char *text, int rows,
204     int cols);
205 
206 int
207 bsddialog_pause(struct bsddialog_conf *conf, const char *text, int rows,
208     int cols, unsigned int seconds);
209 
210 int
211 bsddialog_radiolist(struct bsddialog_conf *conf, const char *text, int rows,
212     int cols, unsigned int menurows, unsigned int nitems,
213     struct bsddialog_menuitem *items, int *focusitem);
214 
215 int
216 bsddialog_rangebox(struct bsddialog_conf *conf, const char *text, int rows,
217     int cols, int min, int max, int *value);
218 
219 int
220 bsddialog_textbox(struct bsddialog_conf *conf, const char *file, int rows,
221     int cols);
222 
223 int
224 bsddialog_timebox(struct bsddialog_conf *conf, const char *text, int rows,
225     int cols, unsigned int *hh, unsigned int *mm, unsigned int *ss);
226 
227 int
228 bsddialog_yesno(struct bsddialog_conf *conf, const char *text, int rows,
229     int cols);
230 
231 #endif
232