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