universe@3: /* universe@3: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@3: * universe@3: * Copyright 2014 Mike Becker. All rights reserved. universe@3: * universe@3: * Redistribution and use in source and binary forms, with or without universe@3: * modification, are permitted provided that the following conditions are met: universe@3: * universe@3: * 1. Redistributions of source code must retain the above copyright universe@3: * notice, this list of conditions and the following disclaimer. universe@3: * universe@3: * 2. Redistributions in binary form must reproduce the above copyright universe@3: * notice, this list of conditions and the following disclaimer in the universe@3: * documentation and/or other materials provided with the distribution. universe@3: * universe@3: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@3: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@3: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@3: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@3: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@3: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@3: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@3: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@3: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@3: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@3: * POSSIBILITY OF SUCH DAMAGE. universe@3: * universe@3: */ universe@3: universe@3: #include "input.h" universe@3: universe@7: void init_colorpairs() { universe@7: init_pair(COL_YB, COLOR_YELLOW, COLOR_BLUE); universe@7: init_pair(COL_BW, COLOR_BLACK, COLOR_WHITE); universe@7: init_pair(COL_WB, COLOR_WHITE, COLOR_BLACK); universe@7: } universe@7: universe@7: int prompt_yesno(char *msg) { universe@7: printw("%s (y/n)? ", msg); universe@7: refresh(); universe@3: noecho(); universe@3: int ch; universe@3: do { universe@3: ch = getch(); universe@3: } while (ch != 'y' && ch != 'n'); universe@3: echo(); universe@3: universe@3: return ch == 'y'; universe@3: } universe@30: universe@30: /** universe@30: * Asynchronous variant of getnstr(). universe@30: * universe@30: * Needs halfdelay mode enabled! universe@30: * universe@30: * Warning: you must not call this function for reading into two separate universe@30: * buffers at the same time. universe@30: * universe@30: * Attention: the first byte of the buffer must be zero at the first call, so universe@30: * the buffer pointer is initialized correctly. universe@30: * universe@30: * @param w the window universe@30: * @param y the window y position universe@30: * @param x the window x position universe@30: * @param str the buffer for the read string universe@30: * @param len the length of the buffer universe@30: * @return 0 if reading is in progress and 1 when a complete line is read universe@30: */ universe@30: int mvwasyncgetnstr(WINDOW* w, int y, int x, char *str, size_t len) { universe@30: static size_t pos = 0; universe@30: universe@30: if (*str == '\0') { universe@30: pos = 0; universe@30: } universe@30: universe@30: mvwaddstr(w,y, x, str); universe@30: wrefresh(w); universe@30: int c = wgetch(w); universe@30: universe@30: if (c != ERR) { universe@30: switch (c) { universe@30: case '\n': universe@30: str[pos] = '\0'; universe@30: pos = 0; universe@30: return 1; universe@30: case KEY_BACKSPACE: universe@30: case KEY_LEFT: universe@30: str[--pos] = '\0'; universe@30: break; universe@30: default: universe@30: if (c < 255 && pos < len-1) { universe@30: str[pos++] = (char) c; universe@30: } universe@30: } universe@30: } universe@30: universe@30: return 0; universe@30: }