xref: /freebsd/contrib/bsddialog/lib/libbsddialog.c (revision 8c4f402881b3a926f1bafdf275b015c6d76a31b2)
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 #include <stdlib.h>
29c76f0793SBaptiste Daroussin #include <string.h>
30c76f0793SBaptiste Daroussin #include <unistd.h>
31c76f0793SBaptiste Daroussin 
32c76f0793SBaptiste Daroussin #ifdef PORTNCURSES
33*8c4f4028SBaptiste Daroussin #include <ncurses/ncurses.h>
34c76f0793SBaptiste Daroussin #else
35*8c4f4028SBaptiste Daroussin #include <ncurses.h>
36c76f0793SBaptiste Daroussin #endif
37c76f0793SBaptiste Daroussin 
38c76f0793SBaptiste Daroussin #include "bsddialog.h"
39c76f0793SBaptiste Daroussin #include "lib_util.h"
40c76f0793SBaptiste Daroussin #include "bsddialog_theme.h"
41c76f0793SBaptiste Daroussin 
42c76f0793SBaptiste Daroussin /*
43*8c4f4028SBaptiste Daroussin  * This file implements public functions not related to a specific dialog.
44*8c4f4028SBaptiste Daroussin  * utils.h/c: private functions to implement the library.
45*8c4f4028SBaptiste Daroussin  * theme.h/c: public API related to themes.
46*8c4f4028SBaptiste Daroussin  * Dialogs implementation:
47c76f0793SBaptiste Daroussin  *  infobox.c    infobox
48c76f0793SBaptiste Daroussin  *  messgebox.c  msgbox - yesno
49f499134dSBaptiste Daroussin  *  menubox.c    buildlist - checklist - menu - mixedlist - radiolist
50c76f0793SBaptiste Daroussin  *  formbox.c    inputbox - passwordbox - form - passwordform - mixedform
51c76f0793SBaptiste Daroussin  *  barbox.c     gauge - mixedgauge - rangebox - pause
52f499134dSBaptiste Daroussin  *  textbox.c    textbox
53c76f0793SBaptiste Daroussin  *  timebox.c    timebox - calendar
54c76f0793SBaptiste Daroussin  */
55c76f0793SBaptiste Daroussin 
56c76f0793SBaptiste Daroussin extern struct bsddialog_theme t;
57c76f0793SBaptiste Daroussin 
58c76f0793SBaptiste Daroussin int bsddialog_init(void)
59c76f0793SBaptiste Daroussin {
60c76f0793SBaptiste Daroussin 	int i, j, c = 1, error = OK;
61c76f0793SBaptiste Daroussin 
62c76f0793SBaptiste Daroussin 	set_error_string("");
63c76f0793SBaptiste Daroussin 
64c76f0793SBaptiste Daroussin 	if(initscr() == NULL)
65c76f0793SBaptiste Daroussin 		RETURN_ERROR("Cannot init ncurses (initscr)");
66c76f0793SBaptiste Daroussin 
67c76f0793SBaptiste Daroussin 	error += keypad(stdscr, TRUE);
68c76f0793SBaptiste Daroussin 	nl();
69c76f0793SBaptiste Daroussin 	error += cbreak();
70c76f0793SBaptiste Daroussin 	error += noecho();
71c76f0793SBaptiste Daroussin 	curs_set(0);
72c76f0793SBaptiste Daroussin 	if(error != OK) {
73c76f0793SBaptiste Daroussin 		bsddialog_end();
74c76f0793SBaptiste Daroussin 		RETURN_ERROR("Cannot init ncurses (keypad and cursor)");
75c76f0793SBaptiste Daroussin 	}
76c76f0793SBaptiste Daroussin 
77c76f0793SBaptiste Daroussin 	error += start_color();
78c76f0793SBaptiste Daroussin 	for (i=0; i<8; i++)
79c76f0793SBaptiste Daroussin 		for(j=0; j<8; j++) {
80c76f0793SBaptiste Daroussin 			error += init_pair(c, i, j);
81c76f0793SBaptiste Daroussin 			c++;
82c76f0793SBaptiste Daroussin 	}
83c76f0793SBaptiste Daroussin 	if(error != OK) {
84c76f0793SBaptiste Daroussin 		bsddialog_end();
85c76f0793SBaptiste Daroussin 		RETURN_ERROR("Cannot init ncurses (colors)");
86c76f0793SBaptiste Daroussin 	}
87c76f0793SBaptiste Daroussin 
88*8c4f4028SBaptiste Daroussin 	if (bsddialog_set_default_theme(BSDDIALOG_THEME_DEFAULT) != 0) {
89*8c4f4028SBaptiste Daroussin 		bsddialog_end();
90*8c4f4028SBaptiste Daroussin 		return (BSDDIALOG_ERROR);
91*8c4f4028SBaptiste Daroussin 	}
92c76f0793SBaptiste Daroussin 
93*8c4f4028SBaptiste Daroussin 	return (0);
94c76f0793SBaptiste Daroussin }
95c76f0793SBaptiste Daroussin 
96c76f0793SBaptiste Daroussin int bsddialog_end(void)
97c76f0793SBaptiste Daroussin {
98c76f0793SBaptiste Daroussin 
99c76f0793SBaptiste Daroussin 	if (endwin() != OK)
100c76f0793SBaptiste Daroussin 		RETURN_ERROR("Cannot end ncurses (endwin)");
101c76f0793SBaptiste Daroussin 
102*8c4f4028SBaptiste Daroussin 	return (0);
103c76f0793SBaptiste Daroussin }
104c76f0793SBaptiste Daroussin 
105f499134dSBaptiste Daroussin int bsddialog_backtitle(struct bsddialog_conf *conf, char *backtitle)
106c76f0793SBaptiste Daroussin {
107c76f0793SBaptiste Daroussin 
108c76f0793SBaptiste Daroussin 	mvaddstr(0, 1, backtitle);
109f499134dSBaptiste Daroussin 	if (conf->no_lines != true)
110f499134dSBaptiste Daroussin 		mvhline(1, 1, conf->ascii_lines ? '-' : ACS_HLINE, COLS-2);
111c76f0793SBaptiste Daroussin 
112c76f0793SBaptiste Daroussin 	refresh();
113c76f0793SBaptiste Daroussin 
114*8c4f4028SBaptiste Daroussin 	return (0);
115c76f0793SBaptiste Daroussin }
116c76f0793SBaptiste Daroussin 
117c76f0793SBaptiste Daroussin const char *bsddialog_geterror(void)
118c76f0793SBaptiste Daroussin {
119c76f0793SBaptiste Daroussin 
120*8c4f4028SBaptiste Daroussin 	return (get_error_string());
121c76f0793SBaptiste Daroussin }
122c76f0793SBaptiste Daroussin 
123*8c4f4028SBaptiste Daroussin int bsddialog_initconf(struct bsddialog_conf *conf)
124c76f0793SBaptiste Daroussin {
125c76f0793SBaptiste Daroussin 
126*8c4f4028SBaptiste Daroussin 	if (conf == NULL)
127*8c4f4028SBaptiste Daroussin 		RETURN_ERROR("conf is NULL");
128*8c4f4028SBaptiste Daroussin 	if (sizeof(*conf) != sizeof(struct bsddialog_conf))
129*8c4f4028SBaptiste Daroussin 		RETURN_ERROR("Bad conf size");
130c76f0793SBaptiste Daroussin 
131c76f0793SBaptiste Daroussin 	memset(conf, 0, sizeof(struct bsddialog_conf));
132*8c4f4028SBaptiste Daroussin 	conf->y = BSDDIALOG_CENTER;
133*8c4f4028SBaptiste Daroussin 	conf->x = BSDDIALOG_CENTER;
134c76f0793SBaptiste Daroussin 	conf->shadow = true;
135*8c4f4028SBaptiste Daroussin 
136*8c4f4028SBaptiste Daroussin 	return (0);
137*8c4f4028SBaptiste Daroussin }
138*8c4f4028SBaptiste Daroussin 
139*8c4f4028SBaptiste Daroussin int bsddialog_clearterminal(void)
140*8c4f4028SBaptiste Daroussin {
141*8c4f4028SBaptiste Daroussin 
142*8c4f4028SBaptiste Daroussin 	if (clear() != OK)
143*8c4f4028SBaptiste Daroussin 		RETURN_ERROR("Cannot clear the terminal");
144*8c4f4028SBaptiste Daroussin 	refresh();
145*8c4f4028SBaptiste Daroussin 
146*8c4f4028SBaptiste Daroussin 	return (0);
147c76f0793SBaptiste Daroussin }
148