1c76f0793SBaptiste Daroussin /*- 2c76f0793SBaptiste Daroussin * SPDX-License-Identifier: BSD-2-Clause 3c76f0793SBaptiste Daroussin * 4c76f0793SBaptiste Daroussin * Copyright (c) 2021 Alfonso Sabato Siciliano 5c76f0793SBaptiste Daroussin * 6c76f0793SBaptiste Daroussin * Redistribution and use in source and binary forms, with or without 7c76f0793SBaptiste Daroussin * modification, are permitted provided that the following conditions 8c76f0793SBaptiste Daroussin * are met: 9c76f0793SBaptiste Daroussin * 1. Redistributions of source code must retain the above copyright 10c76f0793SBaptiste Daroussin * notice, this list of conditions and the following disclaimer. 11c76f0793SBaptiste Daroussin * 2. Redistributions in binary form must reproduce the above copyright 12c76f0793SBaptiste Daroussin * notice, this list of conditions and the following disclaimer in the 13c76f0793SBaptiste Daroussin * documentation and/or other materials provided with the distribution. 14c76f0793SBaptiste Daroussin * 15c76f0793SBaptiste Daroussin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16c76f0793SBaptiste Daroussin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17c76f0793SBaptiste Daroussin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18c76f0793SBaptiste Daroussin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19c76f0793SBaptiste Daroussin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20c76f0793SBaptiste Daroussin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21c76f0793SBaptiste Daroussin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22c76f0793SBaptiste Daroussin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23c76f0793SBaptiste Daroussin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24c76f0793SBaptiste Daroussin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25c76f0793SBaptiste Daroussin * SUCH DAMAGE. 26c76f0793SBaptiste Daroussin */ 27c76f0793SBaptiste Daroussin 28c76f0793SBaptiste Daroussin #ifdef PORTNCURSES 298c4f4028SBaptiste Daroussin #include <ncurses/ncurses.h> 30c76f0793SBaptiste Daroussin #else 318c4f4028SBaptiste Daroussin #include <ncurses.h> 32c76f0793SBaptiste Daroussin #endif 33*d93b4d32SBaptiste Daroussin #include <stdlib.h> 34*d93b4d32SBaptiste Daroussin #include <string.h> 35*d93b4d32SBaptiste Daroussin #include <unistd.h> 36c76f0793SBaptiste Daroussin 37c76f0793SBaptiste Daroussin #include "bsddialog.h" 38c76f0793SBaptiste Daroussin #include "lib_util.h" 39c76f0793SBaptiste Daroussin #include "bsddialog_theme.h" 40c76f0793SBaptiste Daroussin 41c76f0793SBaptiste Daroussin /* 428c4f4028SBaptiste Daroussin * This file implements public functions not related to a specific dialog. 43*d93b4d32SBaptiste Daroussin * lib_utils.h/c: private functions to implement the library. 448c4f4028SBaptiste Daroussin * theme.h/c: public API related to themes. 458c4f4028SBaptiste Daroussin * Dialogs implementation: 46c76f0793SBaptiste Daroussin * infobox.c infobox 47c76f0793SBaptiste Daroussin * messgebox.c msgbox - yesno 48f499134dSBaptiste Daroussin * menubox.c buildlist - checklist - menu - mixedlist - radiolist 49c76f0793SBaptiste Daroussin * formbox.c inputbox - passwordbox - form - passwordform - mixedform 50*d93b4d32SBaptiste Daroussin * barbox.c gauge - mixedgauge - rangebox - pause - progressview 51f499134dSBaptiste Daroussin * textbox.c textbox 52c76f0793SBaptiste Daroussin * timebox.c timebox - calendar 53c76f0793SBaptiste Daroussin */ 54c76f0793SBaptiste Daroussin 55c76f0793SBaptiste Daroussin extern struct bsddialog_theme t; 56c76f0793SBaptiste Daroussin 57c76f0793SBaptiste Daroussin int bsddialog_init(void) 58c76f0793SBaptiste Daroussin { 59*d93b4d32SBaptiste Daroussin int i, j, c, error; 60c76f0793SBaptiste Daroussin 61c76f0793SBaptiste Daroussin set_error_string(""); 62c76f0793SBaptiste Daroussin 63c76f0793SBaptiste Daroussin if(initscr() == NULL) 64c76f0793SBaptiste Daroussin RETURN_ERROR("Cannot init ncurses (initscr)"); 65c76f0793SBaptiste Daroussin 66*d93b4d32SBaptiste Daroussin error = OK; 67c76f0793SBaptiste Daroussin error += keypad(stdscr, TRUE); 68c76f0793SBaptiste Daroussin nl(); 69c76f0793SBaptiste Daroussin error += cbreak(); 70c76f0793SBaptiste Daroussin error += noecho(); 71c76f0793SBaptiste Daroussin curs_set(0); 72c76f0793SBaptiste Daroussin if(error != OK) { 73c76f0793SBaptiste Daroussin bsddialog_end(); 74c76f0793SBaptiste Daroussin RETURN_ERROR("Cannot init ncurses (keypad and cursor)"); 75c76f0793SBaptiste Daroussin } 76c76f0793SBaptiste Daroussin 77*d93b4d32SBaptiste Daroussin c = 1; 78c76f0793SBaptiste Daroussin error += start_color(); 79c76f0793SBaptiste Daroussin for (i=0; i<8; i++) 80c76f0793SBaptiste Daroussin for(j=0; j<8; j++) { 81c76f0793SBaptiste Daroussin error += init_pair(c, i, j); 82c76f0793SBaptiste Daroussin c++; 83c76f0793SBaptiste Daroussin } 84c76f0793SBaptiste Daroussin if(error != OK) { 85c76f0793SBaptiste Daroussin bsddialog_end(); 86c76f0793SBaptiste Daroussin RETURN_ERROR("Cannot init ncurses (colors)"); 87c76f0793SBaptiste Daroussin } 88c76f0793SBaptiste Daroussin 898c4f4028SBaptiste Daroussin if (bsddialog_set_default_theme(BSDDIALOG_THEME_DEFAULT) != 0) { 908c4f4028SBaptiste Daroussin bsddialog_end(); 918c4f4028SBaptiste Daroussin return (BSDDIALOG_ERROR); 928c4f4028SBaptiste Daroussin } 93c76f0793SBaptiste Daroussin 94*d93b4d32SBaptiste Daroussin return (BSDDIALOG_OK); 95c76f0793SBaptiste Daroussin } 96c76f0793SBaptiste Daroussin 97c76f0793SBaptiste Daroussin int bsddialog_end(void) 98c76f0793SBaptiste Daroussin { 99c76f0793SBaptiste Daroussin if (endwin() != OK) 100c76f0793SBaptiste Daroussin RETURN_ERROR("Cannot end ncurses (endwin)"); 101c76f0793SBaptiste Daroussin 102*d93b4d32SBaptiste Daroussin return (BSDDIALOG_OK); 103c76f0793SBaptiste Daroussin } 104c76f0793SBaptiste Daroussin 105f499134dSBaptiste Daroussin int bsddialog_backtitle(struct bsddialog_conf *conf, char *backtitle) 106c76f0793SBaptiste Daroussin { 107c76f0793SBaptiste Daroussin mvaddstr(0, 1, backtitle); 108f499134dSBaptiste Daroussin if (conf->no_lines != true) 109f499134dSBaptiste Daroussin mvhline(1, 1, conf->ascii_lines ? '-' : ACS_HLINE, COLS-2); 110c76f0793SBaptiste Daroussin 111c76f0793SBaptiste Daroussin refresh(); 112c76f0793SBaptiste Daroussin 113*d93b4d32SBaptiste Daroussin return (BSDDIALOG_OK); 114c76f0793SBaptiste Daroussin } 115c76f0793SBaptiste Daroussin 116c76f0793SBaptiste Daroussin const char *bsddialog_geterror(void) 117c76f0793SBaptiste Daroussin { 1188c4f4028SBaptiste Daroussin return (get_error_string()); 119c76f0793SBaptiste Daroussin } 120c76f0793SBaptiste Daroussin 1218c4f4028SBaptiste Daroussin int bsddialog_initconf(struct bsddialog_conf *conf) 122c76f0793SBaptiste Daroussin { 1238c4f4028SBaptiste Daroussin if (conf == NULL) 1248c4f4028SBaptiste Daroussin RETURN_ERROR("conf is NULL"); 1258c4f4028SBaptiste Daroussin if (sizeof(*conf) != sizeof(struct bsddialog_conf)) 1268c4f4028SBaptiste Daroussin RETURN_ERROR("Bad conf size"); 127c76f0793SBaptiste Daroussin 128c76f0793SBaptiste Daroussin memset(conf, 0, sizeof(struct bsddialog_conf)); 1298c4f4028SBaptiste Daroussin conf->y = BSDDIALOG_CENTER; 1308c4f4028SBaptiste Daroussin conf->x = BSDDIALOG_CENTER; 131c76f0793SBaptiste Daroussin conf->shadow = true; 1328c4f4028SBaptiste Daroussin 133*d93b4d32SBaptiste Daroussin return (BSDDIALOG_OK); 1348c4f4028SBaptiste Daroussin } 1358c4f4028SBaptiste Daroussin 1368c4f4028SBaptiste Daroussin int bsddialog_clearterminal(void) 1378c4f4028SBaptiste Daroussin { 1388c4f4028SBaptiste Daroussin if (clear() != OK) 1398c4f4028SBaptiste Daroussin RETURN_ERROR("Cannot clear the terminal"); 1408c4f4028SBaptiste Daroussin refresh(); 1418c4f4028SBaptiste Daroussin 142*d93b4d32SBaptiste Daroussin return (BSDDIALOG_OK); 143c76f0793SBaptiste Daroussin } 144