1/**************************************************************************** 2 * Copyright (c) 2005-2006,2008 Free Software Foundation, Inc. * 3 * * 4 * Permission is hereby granted, free of charge, to any person obtaining a * 5 * copy of this software and associated documentation files (the * 6 * "Software"), to deal in the Software without restriction, including * 7 * without limitation the rights to use, copy, modify, merge, publish, * 8 * distribute, distribute with modifications, sublicense, and/or sell * 9 * copies of the Software, and to permit persons to whom the Software is * 10 * furnished to do so, subject to the following conditions: * 11 * * 12 * The above copyright notice and this permission notice shall be included * 13 * in all copies or substantial portions of the Software. * 14 * * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 22 * * 23 * Except as contained in this notice, the name(s) of the above copyright * 24 * holders shall not be used in advertising or otherwise to promote the * 25 * sale, use or other dealings in this Software without prior written * 26 * authorization. * 27 ****************************************************************************/ 28/* 29 * $Id: demo_altkeys.c,v 1.8 2008/04/12 22:06:22 tom Exp $ 30 * 31 * Demonstrate the define_key() function. 32 * Thomas Dickey - 2005/10/22 33 */ 34 35#include <test.priv.h> 36 37#if defined(NCURSES_VERSION) && NCURSES_EXT_FUNCS 38 39#if TIME_WITH_SYS_TIME 40# include <sys/time.h> 41# include <time.h> 42#else 43# if HAVE_SYS_TIME_H 44# include <sys/time.h> 45# else 46# include <time.h> 47# endif 48#endif 49 50#define MY_LOGFILE "demo_altkeys.log" 51#define MY_KEYS (KEY_MAX + 1) 52 53/* 54 * Log the most recently-written line to our logfile 55 */ 56static void 57log_last_line(WINDOW *win) 58{ 59 FILE *fp; 60 int y, x, n; 61 char temp[256]; 62 63 if ((fp = fopen(MY_LOGFILE, "a")) != 0) { 64 int need = sizeof(temp) - 1; 65 if (need > COLS) 66 need = COLS; 67 getyx(win, y, x); 68 wmove(win, y - 1, 0); 69 n = winnstr(win, temp, need); 70 while (n-- > 0) { 71 if (isspace(UChar(temp[n]))) 72 temp[n] = '\0'; 73 else 74 break; 75 } 76 wmove(win, y, x); 77 fprintf(fp, "%s\n", temp); 78 fclose(fp); 79 } 80} 81 82int 83main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED) 84{ 85 int n; 86 int ch; 87#if HAVE_GETTIMEOFDAY 88 int secs, msecs; 89 struct timeval current, previous; 90#endif 91 92 unlink(MY_LOGFILE); 93 94 newterm(0, stdout, stdin); 95 (void) cbreak(); /* take input chars one at a time, no wait for \n */ 96 (void) noecho(); /* don't echo input */ 97 98 scrollok(stdscr, TRUE); 99 keypad(stdscr, TRUE); 100 move(0, 0); 101 102 /* we do the define_key() calls after keypad(), since the first call to 103 * keypad() initializes the corresponding data. 104 */ 105 for (n = 0; n < 255; ++n) { 106 char temp[10]; 107 sprintf(temp, "\033%c", n); 108 define_key(temp, n + MY_KEYS); 109 } 110 for (n = KEY_MIN; n < KEY_MAX; ++n) { 111 char *value; 112 if ((value = keybound(n, 0)) != 0) { 113 char *temp = typeMalloc(char, strlen(value) + 2); 114 sprintf(temp, "\033%s", value); 115 define_key(temp, n + MY_KEYS); 116 free(temp); 117 free(value); 118 } 119 } 120 121#if HAVE_GETTIMEOFDAY 122 gettimeofday(&previous, 0); 123#endif 124 125 while ((ch = getch()) != ERR) { 126 bool escaped = (ch >= MY_KEYS); 127 const char *name = keyname(escaped ? (ch - MY_KEYS) : ch); 128 129#if HAVE_GETTIMEOFDAY 130 gettimeofday(¤t, 0); 131 secs = current.tv_sec - previous.tv_sec; 132 msecs = (current.tv_usec - previous.tv_usec) / 1000; 133 if (msecs < 0) { 134 msecs += 1000; 135 --secs; 136 } 137 if (msecs >= 1000) { 138 secs += msecs / 1000; 139 msecs %= 1000; 140 } 141 printw("%6d.%03d ", secs, msecs); 142 previous = current; 143#endif 144 printw("Keycode %d, name %s%s\n", 145 ch, 146 escaped ? "ESC-" : "", 147 name != 0 ? name : "<null>"); 148 log_last_line(stdscr); 149 clrtoeol(); 150 if (ch == 'q') 151 break; 152 } 153 endwin(); 154 ExitProgram(EXIT_SUCCESS); 155} 156#else 157int 158main(void) 159{ 160 printf("This program requires the ncurses library\n"); 161 ExitProgram(EXIT_FAILURE); 162} 163#endif 164