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