src/input.c

Tue, 08 Apr 2014 21:13:28 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 08 Apr 2014 21:13:28 +0200
changeset 31
ed440bcd9740
parent 30
a285ee393860
child 32
8a0b85303ee8
permissions
-rw-r--r--

fixed some type bugs, uninitialized memory and async input function

universe@3 1 /*
universe@3 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@3 3 *
universe@3 4 * Copyright 2014 Mike Becker. All rights reserved.
universe@3 5 *
universe@3 6 * Redistribution and use in source and binary forms, with or without
universe@3 7 * modification, are permitted provided that the following conditions are met:
universe@3 8 *
universe@3 9 * 1. Redistributions of source code must retain the above copyright
universe@3 10 * notice, this list of conditions and the following disclaimer.
universe@3 11 *
universe@3 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@3 13 * notice, this list of conditions and the following disclaimer in the
universe@3 14 * documentation and/or other materials provided with the distribution.
universe@3 15 *
universe@3 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@3 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@3 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@3 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@3 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@3 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@3 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@3 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@3 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@3 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@3 26 * POSSIBILITY OF SUCH DAMAGE.
universe@3 27 *
universe@3 28 */
universe@3 29
universe@3 30 #include "input.h"
universe@31 31 #include <ctype.h>
universe@3 32
universe@7 33 void init_colorpairs() {
universe@7 34 init_pair(COL_YB, COLOR_YELLOW, COLOR_BLUE);
universe@7 35 init_pair(COL_BW, COLOR_BLACK, COLOR_WHITE);
universe@7 36 init_pair(COL_WB, COLOR_WHITE, COLOR_BLACK);
universe@7 37 }
universe@7 38
universe@7 39 int prompt_yesno(char *msg) {
universe@7 40 printw("%s (y/n)? ", msg);
universe@7 41 refresh();
universe@3 42 noecho();
universe@3 43 int ch;
universe@3 44 do {
universe@3 45 ch = getch();
universe@3 46 } while (ch != 'y' && ch != 'n');
universe@3 47 echo();
universe@3 48
universe@3 49 return ch == 'y';
universe@3 50 }
universe@30 51
universe@30 52 /**
universe@30 53 * Asynchronous variant of getnstr().
universe@30 54 *
universe@30 55 * Needs halfdelay mode enabled!
universe@30 56 *
universe@30 57 * Warning: you must not call this function for reading into two separate
universe@30 58 * buffers at the same time.
universe@30 59 *
universe@30 60 * Attention: the first byte of the buffer must be zero at the first call, so
universe@30 61 * the buffer pointer is initialized correctly.
universe@30 62 *
universe@30 63 * @param w the window
universe@30 64 * @param y the window y position
universe@30 65 * @param x the window x position
universe@30 66 * @param str the buffer for the read string
universe@30 67 * @param len the length of the buffer
universe@30 68 * @return 0 if reading is in progress and 1 when a complete line is read
universe@30 69 */
universe@30 70 int mvwasyncgetnstr(WINDOW* w, int y, int x, char *str, size_t len) {
universe@30 71 static size_t pos = 0;
universe@30 72
universe@30 73 if (*str == '\0') {
universe@31 74 memset(str, 0, len);
universe@30 75 pos = 0;
universe@30 76 }
universe@30 77
universe@30 78 mvwaddstr(w,y, x, str);
universe@30 79 wrefresh(w);
universe@30 80 int c = wgetch(w);
universe@30 81
universe@30 82 if (c != ERR) {
universe@30 83 switch (c) {
universe@30 84 case '\n':
universe@30 85 str[pos] = '\0';
universe@30 86 pos = 0;
universe@30 87 return 1;
universe@30 88 case KEY_BACKSPACE:
universe@30 89 case KEY_LEFT:
universe@30 90 str[--pos] = '\0';
universe@30 91 break;
universe@30 92 default:
universe@31 93 if (isprint(c) && pos < len-1) {
universe@30 94 str[pos++] = (char) c;
universe@30 95 }
universe@30 96 }
universe@30 97 }
universe@30 98
universe@30 99 return 0;
universe@30 100 }

mercurial