xref: /freebsd/contrib/bsddialog/lib/libbsddialog.c (revision 8ea2d22e6d54d492b6b169014841eb27d4617459)
1c76f0793SBaptiste Daroussin /*-
2c76f0793SBaptiste Daroussin  * SPDX-License-Identifier: BSD-2-Clause
3c76f0793SBaptiste Daroussin  *
4c76f0793SBaptiste Daroussin  * Copyright (c) 2021 Alfonso Sabato Siciliano
5c76f0793SBaptiste Daroussin  *
6c76f0793SBaptiste Daroussin  * Redistribution and use in source and binary forms, with or without
7c76f0793SBaptiste Daroussin  * modification, are permitted provided that the following conditions
8c76f0793SBaptiste Daroussin  * are met:
9c76f0793SBaptiste Daroussin  * 1. Redistributions of source code must retain the above copyright
10c76f0793SBaptiste Daroussin  *    notice, this list of conditions and the following disclaimer.
11c76f0793SBaptiste Daroussin  * 2. Redistributions in binary form must reproduce the above copyright
12c76f0793SBaptiste Daroussin  *    notice, this list of conditions and the following disclaimer in the
13c76f0793SBaptiste Daroussin  *    documentation and/or other materials provided with the distribution.
14c76f0793SBaptiste Daroussin  *
15c76f0793SBaptiste Daroussin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16c76f0793SBaptiste Daroussin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17c76f0793SBaptiste Daroussin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18c76f0793SBaptiste Daroussin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19c76f0793SBaptiste Daroussin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20c76f0793SBaptiste Daroussin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21c76f0793SBaptiste Daroussin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22c76f0793SBaptiste Daroussin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23c76f0793SBaptiste Daroussin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24c76f0793SBaptiste Daroussin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25c76f0793SBaptiste Daroussin  * SUCH DAMAGE.
26c76f0793SBaptiste Daroussin  */
27c76f0793SBaptiste Daroussin 
28c76f0793SBaptiste Daroussin #ifdef PORTNCURSES
298c4f4028SBaptiste Daroussin #include <ncurses/ncurses.h>
30c76f0793SBaptiste Daroussin #else
318c4f4028SBaptiste Daroussin #include <ncurses.h>
32c76f0793SBaptiste Daroussin #endif
33d93b4d32SBaptiste Daroussin #include <stdlib.h>
34d93b4d32SBaptiste Daroussin #include <string.h>
35d93b4d32SBaptiste Daroussin #include <unistd.h>
36c76f0793SBaptiste Daroussin 
37c76f0793SBaptiste Daroussin #include "bsddialog.h"
38c76f0793SBaptiste Daroussin #include "lib_util.h"
39c76f0793SBaptiste Daroussin #include "bsddialog_theme.h"
40c76f0793SBaptiste Daroussin 
41c76f0793SBaptiste Daroussin /*
428c4f4028SBaptiste Daroussin  * This file implements public functions not related to a specific dialog.
43d93b4d32SBaptiste Daroussin  * lib_utils.h/c: private functions to implement the library.
448c4f4028SBaptiste Daroussin  * theme.h/c: public API related to themes.
458c4f4028SBaptiste Daroussin  * Dialogs implementation:
46c76f0793SBaptiste Daroussin  *  infobox.c    infobox
47c76f0793SBaptiste Daroussin  *  messgebox.c  msgbox - yesno
48f499134dSBaptiste Daroussin  *  menubox.c    buildlist - checklist - menu - mixedlist - radiolist
49c76f0793SBaptiste Daroussin  *  formbox.c    inputbox - passwordbox - form - passwordform - mixedform
50d93b4d32SBaptiste Daroussin  *  barbox.c     gauge - mixedgauge - rangebox - pause - progressview
51f499134dSBaptiste Daroussin  *  textbox.c    textbox
52c76f0793SBaptiste Daroussin  *  timebox.c    timebox - calendar
53c76f0793SBaptiste Daroussin  */
54c76f0793SBaptiste Daroussin 
55c76f0793SBaptiste Daroussin extern struct bsddialog_theme t;
56c76f0793SBaptiste Daroussin 
57c76f0793SBaptiste Daroussin int bsddialog_init(void)
58c76f0793SBaptiste Daroussin {
59d93b4d32SBaptiste Daroussin 	int i, j, c, error;
60*8ea2d22eSAlfonso Siciliano 	enum bsddialog_default_theme theme;
61c76f0793SBaptiste Daroussin 
62c76f0793SBaptiste Daroussin 	set_error_string("");
63c76f0793SBaptiste Daroussin 
64c76f0793SBaptiste Daroussin 	if (initscr() == NULL)
65*8ea2d22eSAlfonso Siciliano 		RETURN_ERROR("Cannot init curses (initscr)");
66c76f0793SBaptiste Daroussin 
67d93b4d32SBaptiste Daroussin 	error = OK;
68c76f0793SBaptiste Daroussin 	error += keypad(stdscr, TRUE);
69c76f0793SBaptiste Daroussin 	nl();
70c76f0793SBaptiste Daroussin 	error += cbreak();
71c76f0793SBaptiste Daroussin 	error += noecho();
72c76f0793SBaptiste Daroussin 	curs_set(0);
73c76f0793SBaptiste Daroussin 	if (error != OK) {
74c76f0793SBaptiste Daroussin 		bsddialog_end();
75*8ea2d22eSAlfonso Siciliano 		RETURN_ERROR("Cannot init curses (keypad and cursor)");
76c76f0793SBaptiste Daroussin 	}
77c76f0793SBaptiste Daroussin 
78d93b4d32SBaptiste Daroussin 	c = 1;
79c76f0793SBaptiste Daroussin 	error += start_color();
80c76f0793SBaptiste Daroussin 	for (i=0; i<8; i++)
81c76f0793SBaptiste Daroussin 		for(j=0; j<8; j++) {
82c76f0793SBaptiste Daroussin 			error += init_pair(c, i, j);
83c76f0793SBaptiste Daroussin 			c++;
84c76f0793SBaptiste Daroussin 	}
85c76f0793SBaptiste Daroussin 
86*8ea2d22eSAlfonso Siciliano 	if (error == OK)
87*8ea2d22eSAlfonso Siciliano 		theme = BSDDIALOG_THEME_DEFAULT;
88*8ea2d22eSAlfonso Siciliano 	else
89*8ea2d22eSAlfonso Siciliano 		theme = BSDDIALOG_THEME_BLACKWHITE;
90*8ea2d22eSAlfonso Siciliano 
91*8ea2d22eSAlfonso Siciliano 	if (bsddialog_set_default_theme(theme) != 0) {
928c4f4028SBaptiste Daroussin 		bsddialog_end();
938c4f4028SBaptiste Daroussin 		return (BSDDIALOG_ERROR);
948c4f4028SBaptiste Daroussin 	}
95c76f0793SBaptiste Daroussin 
96d93b4d32SBaptiste Daroussin 	return (BSDDIALOG_OK);
97c76f0793SBaptiste Daroussin }
98c76f0793SBaptiste Daroussin 
99c76f0793SBaptiste Daroussin int bsddialog_end(void)
100c76f0793SBaptiste Daroussin {
101c76f0793SBaptiste Daroussin 	if (endwin() != OK)
102c76f0793SBaptiste Daroussin 		RETURN_ERROR("Cannot end ncurses (endwin)");
103c76f0793SBaptiste Daroussin 
104d93b4d32SBaptiste Daroussin 	return (BSDDIALOG_OK);
105c76f0793SBaptiste Daroussin }
106c76f0793SBaptiste Daroussin 
107f499134dSBaptiste Daroussin int bsddialog_backtitle(struct bsddialog_conf *conf, char *backtitle)
108c76f0793SBaptiste Daroussin {
109c76f0793SBaptiste Daroussin 	mvaddstr(0, 1, backtitle);
110f499134dSBaptiste Daroussin 	if (conf->no_lines != true)
111f499134dSBaptiste Daroussin 		mvhline(1, 1, conf->ascii_lines ? '-' : ACS_HLINE, COLS-2);
112c76f0793SBaptiste Daroussin 
113c76f0793SBaptiste Daroussin 	refresh();
114c76f0793SBaptiste Daroussin 
115d93b4d32SBaptiste Daroussin 	return (BSDDIALOG_OK);
116c76f0793SBaptiste Daroussin }
117c76f0793SBaptiste Daroussin 
118c76f0793SBaptiste Daroussin const char *bsddialog_geterror(void)
119c76f0793SBaptiste Daroussin {
1208c4f4028SBaptiste Daroussin 	return (get_error_string());
121c76f0793SBaptiste Daroussin }
122c76f0793SBaptiste Daroussin 
1238c4f4028SBaptiste Daroussin int bsddialog_initconf(struct bsddialog_conf *conf)
124c76f0793SBaptiste Daroussin {
1258c4f4028SBaptiste Daroussin 	if (conf == NULL)
1268c4f4028SBaptiste Daroussin 		RETURN_ERROR("conf is NULL");
1278c4f4028SBaptiste Daroussin 	if (sizeof(*conf) != sizeof(struct bsddialog_conf))
1288c4f4028SBaptiste Daroussin 		RETURN_ERROR("Bad conf size");
129c76f0793SBaptiste Daroussin 
130c76f0793SBaptiste Daroussin 	memset(conf, 0, sizeof(struct bsddialog_conf));
1318c4f4028SBaptiste Daroussin 	conf->y = BSDDIALOG_CENTER;
1328c4f4028SBaptiste Daroussin 	conf->x = BSDDIALOG_CENTER;
133c76f0793SBaptiste Daroussin 	conf->shadow = true;
1348c4f4028SBaptiste Daroussin 
135d93b4d32SBaptiste Daroussin 	return (BSDDIALOG_OK);
1368c4f4028SBaptiste Daroussin }
1378c4f4028SBaptiste Daroussin 
1388c4f4028SBaptiste Daroussin int bsddialog_clearterminal(void)
1398c4f4028SBaptiste Daroussin {
1408c4f4028SBaptiste Daroussin 	if (clear() != OK)
1418c4f4028SBaptiste Daroussin 		RETURN_ERROR("Cannot clear the terminal");
1428c4f4028SBaptiste Daroussin 	refresh();
1438c4f4028SBaptiste Daroussin 
144d93b4d32SBaptiste Daroussin 	return (BSDDIALOG_OK);
145c76f0793SBaptiste Daroussin }
146