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