1c76f0793SBaptiste Daroussin /*- 2c76f0793SBaptiste Daroussin * SPDX-License-Identifier: BSD-2-Clause 3c76f0793SBaptiste Daroussin * 4*263660c0SAlfonso Siciliano * Copyright (c) 2021-2022 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 28*263660c0SAlfonso Siciliano #include <curses.h> 29d93b4d32SBaptiste Daroussin #include <stdlib.h> 30d93b4d32SBaptiste Daroussin #include <string.h> 31d93b4d32SBaptiste Daroussin #include <unistd.h> 32c76f0793SBaptiste Daroussin 33c76f0793SBaptiste Daroussin #include "bsddialog.h" 34c76f0793SBaptiste Daroussin #include "bsddialog_theme.h" 35*263660c0SAlfonso Siciliano #include "lib_util.h" 36c76f0793SBaptiste Daroussin 37c76f0793SBaptiste Daroussin extern struct bsddialog_theme t; 38c76f0793SBaptiste Daroussin 39c76f0793SBaptiste Daroussin int bsddialog_init(void) 40c76f0793SBaptiste Daroussin { 41d93b4d32SBaptiste Daroussin int i, j, c, error; 428ea2d22eSAlfonso Siciliano enum bsddialog_default_theme theme; 43c76f0793SBaptiste Daroussin 44c76f0793SBaptiste Daroussin set_error_string(""); 45c76f0793SBaptiste Daroussin 46c76f0793SBaptiste Daroussin if (initscr() == NULL) 478ea2d22eSAlfonso Siciliano RETURN_ERROR("Cannot init curses (initscr)"); 48c76f0793SBaptiste Daroussin 49d93b4d32SBaptiste Daroussin error = OK; 50c76f0793SBaptiste Daroussin error += keypad(stdscr, TRUE); 51c76f0793SBaptiste Daroussin nl(); 52c76f0793SBaptiste Daroussin error += cbreak(); 53c76f0793SBaptiste Daroussin error += noecho(); 54c76f0793SBaptiste Daroussin curs_set(0); 55c76f0793SBaptiste Daroussin if (error != OK) { 56c76f0793SBaptiste Daroussin bsddialog_end(); 578ea2d22eSAlfonso Siciliano RETURN_ERROR("Cannot init curses (keypad and cursor)"); 58c76f0793SBaptiste Daroussin } 59c76f0793SBaptiste Daroussin 60d93b4d32SBaptiste Daroussin c = 1; 61c76f0793SBaptiste Daroussin error += start_color(); 62c76f0793SBaptiste Daroussin for (i = 0; i < 8; i++) 63c76f0793SBaptiste Daroussin for (j = 0; j < 8; j++) { 64c76f0793SBaptiste Daroussin error += init_pair(c, i, j); 65c76f0793SBaptiste Daroussin c++; 66c76f0793SBaptiste Daroussin } 67c76f0793SBaptiste Daroussin 688ea2d22eSAlfonso Siciliano if (error == OK) 698ea2d22eSAlfonso Siciliano theme = BSDDIALOG_THEME_DEFAULT; 708ea2d22eSAlfonso Siciliano else 718ea2d22eSAlfonso Siciliano theme = BSDDIALOG_THEME_BLACKWHITE; 728ea2d22eSAlfonso Siciliano 738ea2d22eSAlfonso Siciliano if (bsddialog_set_default_theme(theme) != 0) { 748c4f4028SBaptiste Daroussin bsddialog_end(); 758c4f4028SBaptiste Daroussin return (BSDDIALOG_ERROR); 768c4f4028SBaptiste Daroussin } 77c76f0793SBaptiste Daroussin 78d93b4d32SBaptiste Daroussin return (BSDDIALOG_OK); 79c76f0793SBaptiste Daroussin } 80c76f0793SBaptiste Daroussin 81c76f0793SBaptiste Daroussin int bsddialog_end(void) 82c76f0793SBaptiste Daroussin { 83c76f0793SBaptiste Daroussin if (endwin() != OK) 84*263660c0SAlfonso Siciliano RETURN_ERROR("Cannot end curses (endwin)"); 85c76f0793SBaptiste Daroussin 86d93b4d32SBaptiste Daroussin return (BSDDIALOG_OK); 87c76f0793SBaptiste Daroussin } 88c76f0793SBaptiste Daroussin 89*263660c0SAlfonso Siciliano int bsddialog_backtitle(struct bsddialog_conf *conf, const char *backtitle) 90c76f0793SBaptiste Daroussin { 91c76f0793SBaptiste Daroussin mvaddstr(0, 1, backtitle); 92f499134dSBaptiste Daroussin if (conf->no_lines != true) 93*263660c0SAlfonso Siciliano mvhline(1, 1, conf->ascii_lines ? '-' : ACS_HLINE, 94*263660c0SAlfonso Siciliano SCREENCOLS - 2); 95c76f0793SBaptiste Daroussin 96c76f0793SBaptiste Daroussin refresh(); 97c76f0793SBaptiste Daroussin 98d93b4d32SBaptiste Daroussin return (BSDDIALOG_OK); 99c76f0793SBaptiste Daroussin } 100c76f0793SBaptiste Daroussin 101c76f0793SBaptiste Daroussin const char *bsddialog_geterror(void) 102c76f0793SBaptiste Daroussin { 1038c4f4028SBaptiste Daroussin return (get_error_string()); 104c76f0793SBaptiste Daroussin } 105c76f0793SBaptiste Daroussin 1068c4f4028SBaptiste Daroussin int bsddialog_initconf(struct bsddialog_conf *conf) 107c76f0793SBaptiste Daroussin { 1088c4f4028SBaptiste Daroussin if (conf == NULL) 1098c4f4028SBaptiste Daroussin RETURN_ERROR("conf is NULL"); 1108c4f4028SBaptiste Daroussin if (sizeof(*conf) != sizeof(struct bsddialog_conf)) 1118c4f4028SBaptiste Daroussin RETURN_ERROR("Bad conf size"); 112c76f0793SBaptiste Daroussin 113c76f0793SBaptiste Daroussin memset(conf, 0, sizeof(struct bsddialog_conf)); 1148c4f4028SBaptiste Daroussin conf->y = BSDDIALOG_CENTER; 1158c4f4028SBaptiste Daroussin conf->x = BSDDIALOG_CENTER; 116c76f0793SBaptiste Daroussin conf->shadow = true; 1178c4f4028SBaptiste Daroussin 118d93b4d32SBaptiste Daroussin return (BSDDIALOG_OK); 1198c4f4028SBaptiste Daroussin } 1208c4f4028SBaptiste Daroussin 1218c4f4028SBaptiste Daroussin int bsddialog_clearterminal(void) 1228c4f4028SBaptiste Daroussin { 1238c4f4028SBaptiste Daroussin if (clear() != OK) 1248c4f4028SBaptiste Daroussin RETURN_ERROR("Cannot clear the terminal"); 1258c4f4028SBaptiste Daroussin refresh(); 1268c4f4028SBaptiste Daroussin 127d93b4d32SBaptiste Daroussin return (BSDDIALOG_OK); 128c76f0793SBaptiste Daroussin }