xref: /freebsd/contrib/bsddialog/lib/libbsddialog.c (revision 8aac90f18aef7c9eea906c3ff9a001ca7b94f375)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2021-2023 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 <string.h>
30 
31 #include "bsddialog.h"
32 #include "bsddialog_theme.h"
33 #include "lib_util.h"
34 
35 #define DEFAULT_COLS_PER_ROW  10   /* Default conf.text.columns_per_row */
36 
37 static bool in_bsddialog_mode = false;
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 	in_bsddialog_mode = true;
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 
69 	hastermcolors = (error == OK && has_colors()) ? true : false;
70 
71 	return (BSDDIALOG_OK);
72 }
73 
74 int bsddialog_init(void)
75 {
76 	enum bsddialog_default_theme theme;
77 
78 	bsddialog_init_notheme();
79 
80 	if (bsddialog_hascolors())
81 		theme = BSDDIALOG_THEME_FLAT;
82 	else
83 		theme = BSDDIALOG_THEME_BLACKWHITE;
84 
85 	if (bsddialog_set_default_theme(theme) != 0) {
86 		bsddialog_end();
87 		in_bsddialog_mode = false;
88 		return (BSDDIALOG_ERROR);
89 	}
90 
91 	return (BSDDIALOG_OK);
92 }
93 
94 int bsddialog_end(void)
95 {
96 	if (endwin() != OK)
97 		RETURN_ERROR("Cannot end curses (endwin)");
98 	in_bsddialog_mode = false;
99 
100 	return (BSDDIALOG_OK);
101 }
102 
103 int bsddialog_backtitle(struct bsddialog_conf *conf, const char *backtitle)
104 {
105 	CHECK_PTR(conf);
106 
107 	move(0, 1);
108 	clrtoeol();
109 	addstr(CHECK_STR(backtitle));
110 	if (conf->no_lines != true) {
111 		if (conf->ascii_lines)
112 			mvhline(1, 1, '-', SCREENCOLS - 2);
113 		else
114 			mvhline_set(1, 1, WACS_HLINE, SCREENCOLS - 2);
115 	}
116 
117 	refresh();
118 
119 	return (BSDDIALOG_OK);
120 }
121 
122 bool bsddialog_inmode(void)
123 {
124 	return (in_bsddialog_mode);
125 }
126 
127 const char *bsddialog_geterror(void)
128 {
129 	return (get_error_string());
130 }
131 
132 int bsddialog_initconf(struct bsddialog_conf *conf)
133 {
134 	CHECK_PTR(conf);
135 
136 	memset(conf, 0, sizeof(struct bsddialog_conf));
137 	conf->y = BSDDIALOG_CENTER;
138 	conf->x = BSDDIALOG_CENTER;
139 	conf->shadow = true;
140 	conf->text.cols_per_row = DEFAULT_COLS_PER_ROW;
141 
142 	return (BSDDIALOG_OK);
143 }
144 
145 void bsddialog_refresh(void)
146 {
147 	refresh();
148 }
149 
150 void bsddialog_clear(unsigned int y)
151 {
152 	move(y, 0);
153 	clrtobot();
154 	refresh();
155 }