• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /****************************************************************************
2  * Copyright 2018-2022,2023 Thomas E. Dickey                                *
3  * Copyright 2005-2016,2017 Free Software Foundation, Inc.                  *
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 /*
30  * $Id: demo_altkeys.c,v 1.17 2023/02/25 18:08:02 tom Exp $
31  *
32  * Demonstrate the define_key() function.
33  * Thomas Dickey - 2005/10/22
34  */
35 
36 #define NEED_TIME_H
37 #include <test.priv.h>
38 
39 #if defined(NCURSES_VERSION) && NCURSES_EXT_FUNCS
40 
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
log_last_line(WINDOW * win)48 log_last_line(WINDOW *win)
49 {
50     FILE *fp;
51 
52     if ((fp = fopen(MY_LOGFILE, "a")) != 0) {
53 	char temp[256];
54 	int y, x, n;
55 	int need = sizeof(temp) - 1;
56 
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 
74 static void
usage(int ok)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* */
VERSION_COMMON()91 VERSION_COMMON()
92 /* *INDENT-ON* */
93 
94 int
95 main(int argc, char *argv[])
96 {
97     int n;
98     int ch;
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);
113 
114     unlink(MY_LOGFILE);
115 
116     setlocale(LC_ALL, "");
117     if (newterm(0, stdout, stdin) == 0) {
118 	fprintf(stderr, "Cannot initialize terminal\n");
119 	ExitProgram(EXIT_FAILURE);
120     }
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];
133 	_nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "\033%c", n);
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) {
139 	    size_t need = strlen(value) + 2;
140 	    char *temp = typeMalloc(char, need);
141 	    _nc_SPRINTF(temp, _nc_SLIMIT(need) "\033%s", value);
142 	    define_key(temp, n + MY_KEYS);
143 	    free(temp);
144 	    free(value);
145 	}
146     }
147 
148     GetClockTime(&previous);
149 
150     while ((ch = getch()) != ERR) {
151 	bool escaped = (ch >= MY_KEYS);
152 	const char *name = keyname(escaped ? (ch - MY_KEYS) : ch);
153 	TimeType current;
154 
155 	GetClockTime(&current);
156 	printw("%6.03f ", ElapsedSeconds(&previous, &current));
157 	previous = current;
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
main(void)172 main(void)
173 {
174     printf("This program requires the ncurses library\n");
175     ExitProgram(EXIT_FAILURE);
176 }
177 #endif
178