xref: /freebsd/contrib/bsddialog/lib/libbsddialog.c (revision 833a452e9f082a7982a31c21f0da437dbbe0a39d)
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 #include <curses.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 
33 #include "bsddialog.h"
34 #include "bsddialog_theme.h"
35 #include "lib_util.h"
36 
37 extern struct bsddialog_theme t;
38 
39 int bsddialog_init(void)
40 {
41 	int i, j, c, error;
42 	enum bsddialog_default_theme theme;
43 
44 	set_error_string("");
45 
46 	if (initscr() == NULL)
47 		RETURN_ERROR("Cannot init curses (initscr)");
48 
49 	error = OK;
50 	error += keypad(stdscr, TRUE);
51 	nl();
52 	error += cbreak();
53 	error += noecho();
54 	curs_set(0);
55 	if (error != OK) {
56 		bsddialog_end();
57 		RETURN_ERROR("Cannot init curses (keypad and cursor)");
58 	}
59 
60 	c = 1;
61 	error += start_color();
62 	for (i = 0; i < 8; i++)
63 		for (j = 0; j < 8; j++) {
64 			error += init_pair(c, i, j);
65 			c++;
66 	}
67 
68 	if (error == OK)
69 		theme = BSDDIALOG_THEME_DEFAULT;
70 	else
71 		theme = BSDDIALOG_THEME_BLACKWHITE;
72 
73 	if (bsddialog_set_default_theme(theme) != 0) {
74 		bsddialog_end();
75 		return (BSDDIALOG_ERROR);
76 	}
77 
78 	return (BSDDIALOG_OK);
79 }
80 
81 int bsddialog_end(void)
82 {
83 	if (endwin() != OK)
84 		RETURN_ERROR("Cannot end curses (endwin)");
85 
86 	return (BSDDIALOG_OK);
87 }
88 
89 int bsddialog_backtitle(struct bsddialog_conf *conf, const char *backtitle)
90 {
91 	mvaddstr(0, 1, backtitle);
92 	if (conf->no_lines != true)
93 		mvhline(1, 1, conf->ascii_lines ? '-' : ACS_HLINE,
94 		    SCREENCOLS - 2);
95 
96 	refresh();
97 
98 	return (BSDDIALOG_OK);
99 }
100 
101 const char *bsddialog_geterror(void)
102 {
103 	return (get_error_string());
104 }
105 
106 int bsddialog_initconf(struct bsddialog_conf *conf)
107 {
108 	if (conf == NULL)
109 		RETURN_ERROR("conf is NULL");
110 	if (sizeof(*conf) != sizeof(struct bsddialog_conf))
111 		RETURN_ERROR("Bad conf size");
112 
113 	memset(conf, 0, sizeof(struct bsddialog_conf));
114 	conf->y = BSDDIALOG_CENTER;
115 	conf->x = BSDDIALOG_CENTER;
116 	conf->shadow = true;
117 
118 	return (BSDDIALOG_OK);
119 }
120 
121 int bsddialog_clearterminal(void)
122 {
123 	if (clear() != OK)
124 		RETURN_ERROR("Cannot clear the terminal");
125 	refresh();
126 
127 	return (BSDDIALOG_OK);
128 }