为什么 init_pair 在 COLOR_PAIRS 范围的一半时失败?

Why does init_pair fail for half of the COLOR_PAIRS range?

在我的平台上:

通过此 makefile 配置构建:

pkgenv=PKG_CONFIG_PATH=/usr/local/opt/ncurses/lib/pkgconfig pkg-config ncursesw

flags=-Wall -std=c17 -ggdb
cflags=$(flags) $(shell $(pkgenv) --cflags)
ldflags=$(flags) $(shell $(pkgenv) --libs)

这个最小可重现示例失败了:

#include <assert.h>
#include <ncursesw/ncurses.h>

int main() {
    assert(NULL != initscr());
    assert(has_colors());
    assert(can_change_color());

    assert(ERR != start_color());
    assert(COLOR_PAIRS == 0x10000);
    assert(COLORS == 256);
    assert(ERR != init_pair(1, 9, 10));
    assert(ERR != init_pair(0x7FFE, 9, 10));
    // OK up to here

    // Fails
    assert(ERR != init_pair(0x7FFF, 9, 10));

    endwin();
    puts("OK");
    return 0;
}

但是为什么呢? COLOR_PAIRS 报告 (65536) 和 init_pair 接受 (1-32766) 似乎是两个不同的东西。为了它的价值,浏览头文件,

#define NCURSES_PAIRS_T short

奇怪的是,他们为此选择了签名数量。

颜色对的 standard type 是(有符号的)short,在大多数机器上是 16 位。原因是它可以追溯到 1980 年代,当时 space(和颜色可用性)更为有限。

ncurses 6.1 has extensions that allow 32-bit values (e.g., using init_extended_pair), which brew's formula 似乎允许,但到目前为止还没有人对此发表评论。

init_pair 手册页的 Portability 部分所述

X/Open Curses does not specify a limit for the number of colors and color pairs which a terminal can support. However, in its use of short for the parameters, it carries over SVr4's implementation detail for the compiled terminfo database, which uses signed 16-bit numbers. This implementation provides extended versions of those functions which use short parameters, allowing applications to use larger color- and pair- numbers.

为了您的娱乐,屏幕截图here使用了 ncurses 6.1 的扩展号码功能