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