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