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
50 * formbox.c inputbox - passwordbox - form - passwordform - mixedform
51 * barbox.c gauge - mixedgauge - rangebox - pause
52 * textbox.c textbox
53 * timebox.c timebox - calendar
54 */
55
56 extern struct bsddialog_theme t;
57
bsddialog_init(void)58 int bsddialog_init(void)
59 {
60 int i, j, c = 1, error = OK;
61
62 set_error_string("");
63
64 if(initscr() == NULL)
65 RETURN_ERROR("Cannot init ncurses (initscr)");
66
67 error += keypad(stdscr, TRUE);
68 nl();
69 error += cbreak();
70 error += noecho();
71 curs_set(0);
72 if(error != OK) {
73 bsddialog_end();
74 RETURN_ERROR("Cannot init ncurses (keypad and cursor)");
75 }
76
77 error += start_color();
78 for (i=0; i<8; i++)
79 for(j=0; j<8; j++) {
80 error += init_pair(c, i, j);
81 c++;
82 }
83 if(error != OK) {
84 bsddialog_end();
85 RETURN_ERROR("Cannot init ncurses (colors)");
86 }
87
88 if (bsddialog_set_default_theme(BSDDIALOG_THEME_DIALOG) != 0)
89 error = BSDDIALOG_ERROR;
90
91 return error;
92 }
93
bsddialog_end(void)94 int bsddialog_end(void)
95 {
96
97 if (endwin() != OK)
98 RETURN_ERROR("Cannot end ncurses (endwin)");
99
100 return 0;
101 }
102
bsddialog_backtitle(struct bsddialog_conf * conf,char * backtitle)103 int bsddialog_backtitle(struct bsddialog_conf *conf, char *backtitle)
104 {
105
106 mvaddstr(0, 1, backtitle);
107 if (conf->no_lines != true)
108 mvhline(1, 1, conf->ascii_lines ? '-' : ACS_HLINE, COLS-2);
109
110 refresh();
111
112 return 0;
113 }
114
bsddialog_geterror(void)115 const char *bsddialog_geterror(void)
116 {
117
118 return get_error_string();
119 }
120
bsddialog_terminalheight(void)121 int bsddialog_terminalheight(void)
122 {
123
124 return LINES;
125 }
126
bsddialog_terminalwidth(void)127 int bsddialog_terminalwidth(void)
128 {
129
130 return COLS;
131 }
132
bsddialog_initconf(struct bsddialog_conf * conf)133 void bsddialog_initconf(struct bsddialog_conf *conf)
134 {
135
136 memset(conf, 0, sizeof(struct bsddialog_conf));
137 conf->x = conf->y = BSDDIALOG_CENTER;
138 conf->shadow = true;
139 }
140