blob: ea6a7e64bcaee27da7b71ee39acba6dc4752ffa5 [file] [log] [blame]
Steve Kondikae271bc2015-11-15 02:50:53 +01001/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2018-2022,2023 Thomas E. Dickey *
3 * Copyright 2005-2016,2017 Free Software Foundation, Inc. *
Steve Kondikae271bc2015-11-15 02:50:53 +01004 * *
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/*
micky3879b9f5e72025-07-08 18:04:53 -040030 * $Id: demo_altkeys.c,v 1.17 2023/02/25 18:08:02 tom Exp $
Steve Kondikae271bc2015-11-15 02:50:53 +010031 *
32 * Demonstrate the define_key() function.
33 * Thomas Dickey - 2005/10/22
34 */
35
micky3879b9f5e72025-07-08 18:04:53 -040036#define NEED_TIME_H
Steve Kondikae271bc2015-11-15 02:50:53 +010037#include <test.priv.h>
38
39#if defined(NCURSES_VERSION) && NCURSES_EXT_FUNCS
40
Steve Kondikae271bc2015-11-15 02:50:53 +010041#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 */
47static void
48log_last_line(WINDOW *win)
49{
50 FILE *fp;
Steve Kondikae271bc2015-11-15 02:50:53 +010051
52 if ((fp = fopen(MY_LOGFILE, "a")) != 0) {
micky3879b9f5e72025-07-08 18:04:53 -040053 char temp[256];
54 int y, x, n;
Steve Kondikae271bc2015-11-15 02:50:53 +010055 int need = sizeof(temp) - 1;
micky3879b9f5e72025-07-08 18:04:53 -040056
Steve Kondikae271bc2015-11-15 02:50:53 +010057 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
micky3879b9f5e72025-07-08 18:04:53 -040074static void
75usage(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* */
91VERSION_COMMON()
92/* *INDENT-ON* */
93
Steve Kondikae271bc2015-11-15 02:50:53 +010094int
micky3879b9f5e72025-07-08 18:04:53 -040095main(int argc, char *argv[])
Steve Kondikae271bc2015-11-15 02:50:53 +010096{
97 int n;
98 int ch;
micky3879b9f5e72025-07-08 18:04:53 -040099 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 Kondikae271bc2015-11-15 02:50:53 +0100113
114 unlink(MY_LOGFILE);
115
micky3879b9f5e72025-07-08 18:04:53 -0400116 setlocale(LC_ALL, "");
117 if (newterm(0, stdout, stdin) == 0) {
118 fprintf(stderr, "Cannot initialize terminal\n");
119 ExitProgram(EXIT_FAILURE);
120 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100121 (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];
micky3879b9f5e72025-07-08 18:04:53 -0400133 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "\033%c", n);
Steve Kondikae271bc2015-11-15 02:50:53 +0100134 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) {
micky3879b9f5e72025-07-08 18:04:53 -0400139 size_t need = strlen(value) + 2;
140 char *temp = typeMalloc(char, need);
141 _nc_SPRINTF(temp, _nc_SLIMIT(need) "\033%s", value);
Steve Kondikae271bc2015-11-15 02:50:53 +0100142 define_key(temp, n + MY_KEYS);
143 free(temp);
144 free(value);
145 }
146 }
147
micky3879b9f5e72025-07-08 18:04:53 -0400148 GetClockTime(&previous);
Steve Kondikae271bc2015-11-15 02:50:53 +0100149
150 while ((ch = getch()) != ERR) {
151 bool escaped = (ch >= MY_KEYS);
152 const char *name = keyname(escaped ? (ch - MY_KEYS) : ch);
micky3879b9f5e72025-07-08 18:04:53 -0400153 TimeType current;
Steve Kondikae271bc2015-11-15 02:50:53 +0100154
micky3879b9f5e72025-07-08 18:04:53 -0400155 GetClockTime(&current);
156 printw("%6.03f ", ElapsedSeconds(&previous, &current));
Steve Kondikae271bc2015-11-15 02:50:53 +0100157 previous = current;
Steve Kondikae271bc2015-11-15 02:50:53 +0100158 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
171int
172main(void)
173{
174 printf("This program requires the ncurses library\n");
175 ExitProgram(EXIT_FAILURE);
176}
177#endif