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