/****************************************************************************
- * Copyright (c) 2017 Free Software Foundation, Inc. *
+ * Copyright 2018-2020,2021 Thomas E. Dickey *
+ * Copyright 2017 Free Software Foundation, Inc. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the *
* authorization. *
****************************************************************************/
/*
- * $Id: demo_new_pair.c,v 1.10 2017/03/10 09:47:15 tom Exp $
+ * $Id: demo_new_pair.c,v 1.24 2021/02/21 01:24:06 tom Exp $
*
* Demonstrate the alloc_pair() function.
*/
#include <test.priv.h>
#include <time.h>
+#include <popup_msg.h>
#if HAVE_ALLOC_PAIR && USE_WIDEC_SUPPORT
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
#define MAX_BITS 8 /* all but A_ALTCHARSET */
#define MAX_ATTR ((1<<MAX_BITS)-1)
attr_t bits[MAX_BITS];
init = TRUE;
- bits[limit++] = A_NORMAL;
+ bits[limit++] = WA_NORMAL;
if (valid_cap("smso"))
- bits[limit++] = A_STANDOUT;
+ bits[limit++] = WA_STANDOUT;
if (valid_cap("smul"))
- bits[limit++] = A_UNDERLINE;
+ bits[limit++] = WA_UNDERLINE;
if (valid_cap("rev"))
- bits[limit++] = A_REVERSE;
+ bits[limit++] = WA_REVERSE;
if (valid_cap("blink"))
- bits[limit++] = A_BLINK;
+ bits[limit++] = WA_BLINK;
if (valid_cap("dim"))
- bits[limit++] = A_DIM;
+ bits[limit++] = WA_DIM;
if (valid_cap("bold"))
- bits[limit++] = A_BOLD;
+ bits[limit++] = WA_BOLD;
for (j = 0; j < limit; ++j) {
for (k = 0; k < limit; ++k) {
table[j * limit + k] = bits[j] | bits[k];
return result;
}
-static void
-show_help(const char **help)
-{
- WINDOW *mywin = newwin(LINES, COLS, 0, 0);
- int n;
-
- wmove(mywin, 1, 1);
- for (n = 0; help[n] != 0; ++n) {
- wmove(mywin, 1 + n, 2);
- wprintw(mywin, "%.*s\n", COLS - 4, help[n]);
- }
- box(mywin, 0, 0);
- wgetch(mywin);
- delwin(mywin);
- touchwin(stdscr);
- refresh();
-}
-
static time_t
now(void)
{
"Repeatedly print using all possible color combinations.",
"",
"Options:",
+ " -g use getcchar to check setcchar",
" -i use init_pair rather than alloc_pair",
" -p start in paged-mode",
" -s start in single-step mode",
};
bool done = FALSE;
+ bool check_set = FALSE;
bool clobber = FALSE;
bool hascolor = FALSE;
bool use_init = FALSE;
wchar_t wch[2];
time_t start = now();
long total_cells = 0;
+ FILE *output = 0;
setlocale(LC_ALL, "");
- while ((ch = getopt(argc, argv, "ipsw")) != -1) {
+ while ((ch = getopt(argc, argv, "gipsw")) != -1) {
switch (ch) {
+ case 'g':
+ check_set = TRUE;
+ break;
case 'i':
use_init = TRUE;
break;
}
}
- if (newterm(NULL, stderr, stdin) == 0)
- usage();
+ if (isatty(fileno(stderr))) {
+ output = stderr;
+ } else if ((ch = open("/dev/tty", O_WRONLY)) >= 0) {
+ output = fdopen(ch, "w");
+ } else {
+ fprintf(stderr, "cannot open terminal for output\n");
+ ExitProgram(EXIT_FAILURE);
+ }
+ if (newterm(NULL, output, stdin) == 0) {
+ fprintf(stderr, "Cannot initialize terminal\n");
+ fclose(output);
+ ExitProgram(EXIT_FAILURE);
+ }
(void) cbreak(); /* read chars without wait for \n */
(void) noecho(); /* don't echo input */
update_modes();
int my_pair;
switch (getch()) {
- case '?':
- show_help(help);
+ case HELP_KEY_1:
+ popup_msg(stdscr, help);
break;
case 'p':
/* step-by-page */
break;
wch[0] = use_wide ? 0xff03 : '#';
wch[1] = 0;
- setcchar(&temp, wch, my_attrs, (short) my_pair, NULL);
+ setcchar(&temp, wch, my_attrs,
+ (short) my_pair,
+ (use_init ? NULL : (void *) &my_pair));
+
+ if (check_set) {
+ int problem = 0;
+ wchar_t chk_wch[2];
+ attr_t chk_attrs = 0;
+ short chk_pair = 0;
+ int chk_pair2 = 0;
+
+#define AllButColor(a) ((a) & (A_ATTRIBUTES & ~A_COLOR))
+
+ if (getcchar(&temp, NULL, &chk_attrs, &chk_pair,
+ (use_init ? NULL : (void *) &chk_pair2)) != 2) {
+ problem = 1;
+ } else if (getcchar(&temp, chk_wch, &chk_attrs, &chk_pair,
+ (use_init ? NULL : (void *) &chk_pair2)) != OK) {
+ problem = 2;
+ } else if (chk_wch[0] != wch[0]) {
+ problem = 3;
+ } else if (AllButColor(my_attrs) != AllButColor(chk_attrs)) {
+ problem = 4;
+ } else if (my_pair != chk_pair) {
+ problem = 4;
+ } else if (!use_init && (my_pair != chk_pair2)) {
+ problem = 5;
+ }
+ if (problem) {
+ wch[0] = (wchar_t) (problem + '0');
+ setcchar(&temp, wch, my_attrs,
+ (short) my_pair,
+ (use_init ? NULL : (void *) &my_pair));
+ }
+ }
+
/*
* At the end of a page, move the cursor to the home position.
*/
total_cells += 1 + (use_wide ? 1 : 0);
++current;
}
- endwin();
+ stop_curses();
+ fclose(output);
printf("%.1f cells/second\n",
(double) (total_cells) / (double) (now() - start));