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/ncurses.h> 34 #else 35 #include <ncurses.h> 36 #endif 37 38 #include "bsddialog.h" 39 #include "lib_util.h" 40 #include "bsddialog_theme.h" 41 42 /* 43 * This file implements public functions not related to a specific dialog. 44 * utils.h/c: private functions to implement the library. 45 * theme.h/c: public API related to themes. 46 * Dialogs 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 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_DEFAULT) != 0) { 89 bsddialog_end(); 90 return (BSDDIALOG_ERROR); 91 } 92 93 return (0); 94 } 95 96 int bsddialog_end(void) 97 { 98 99 if (endwin() != OK) 100 RETURN_ERROR("Cannot end ncurses (endwin)"); 101 102 return (0); 103 } 104 105 int bsddialog_backtitle(struct bsddialog_conf *conf, char *backtitle) 106 { 107 108 mvaddstr(0, 1, backtitle); 109 if (conf->no_lines != true) 110 mvhline(1, 1, conf->ascii_lines ? '-' : ACS_HLINE, COLS-2); 111 112 refresh(); 113 114 return (0); 115 } 116 117 const char *bsddialog_geterror(void) 118 { 119 120 return (get_error_string()); 121 } 122 123 int bsddialog_initconf(struct bsddialog_conf *conf) 124 { 125 126 if (conf == NULL) 127 RETURN_ERROR("conf is NULL"); 128 if (sizeof(*conf) != sizeof(struct bsddialog_conf)) 129 RETURN_ERROR("Bad conf size"); 130 131 memset(conf, 0, sizeof(struct bsddialog_conf)); 132 conf->y = BSDDIALOG_CENTER; 133 conf->x = BSDDIALOG_CENTER; 134 conf->shadow = true; 135 136 return (0); 137 } 138 139 int bsddialog_clearterminal(void) 140 { 141 142 if (clear() != OK) 143 RETURN_ERROR("Cannot clear the terminal"); 144 refresh(); 145 146 return (0); 147 } 148