1

In version 6.1, ncurses introduce init_extended_pair to extend limit of possible color pairs above short limit. In my experiment everything works till value 255. For values 256 and greater, there is no error, but foreground and background have default values. For values 32767 and greater function return error.

enter image description here

Program return:

COLOR_PAIRS: 65536
                  Error: 32767

What is the proper why to create large number of color pairs? In my case I need at least 65536 of them. (Tested on Ubuntu 19.04)

#include <iostream>
#include <ncurses.h>

// g++ main.cpp -l:libncursesw.so.6.1 -ltinfo

int main() {
    initscr();
    start_color();

    std::cout << "COLOR_PAIRS: " << COLOR_PAIRS << std::endl;

    init_extended_color(2, 999, 0, 0);
    init_extended_color(3, 0, 999, 0);

    int pair1 = 255;
    if (init_extended_pair(pair1, 2, 3) == ERR)
        std::cout << "Error: " << pair1 << std::endl;

    attron(COLOR_PAIR(pair1));
    mvprintw(2, 1, "pair255");
    attroff(COLOR_PAIR(pair1));

    int pair2 = 256;
    if (init_extended_pair(pair2, 2, 3) == ERR)
        std::cout << "Error: " << pair2 << std::endl;

    attron(COLOR_PAIR(pair2));
    mvprintw(3, 1, "pair256");
    attroff(COLOR_PAIR(pair2));

    int pair3 = 32767; // 2^15-1
    if (init_extended_pair(pair3, 3, 2) == ERR)
        std::cout << "Error: " << pair3 << std::endl;

    attron(COLOR_PAIR(pair3));
    mvprintw(4, 1, "pair32767");
    attroff(COLOR_PAIR(pair3));

    refresh();
    getch();
    endwin();

    return 0;
}

Edit: Regarding similar problem How to enable 32k color pairs in ncurses?. In my case COLOR_PAIRS return value 65536 not 256, more over question is from 2015, and init_extended_pair was added to library on 2017.04.01, and released in version 6.1 January 27, 2018. Despite this I rebuild libncursesw6 package with --enable-ext-colors (--enable-widec was already available), but I get same result.

NiegodziwyBeru
  • 864
  • 1
  • 10
  • 19
  • Hi @MarcSances, I'm afraid that's not my problem. I'm using newer API -`init_extended_pair` was not available in 2015, and `COLOR_PAIRS` is returning value `65536` not `256`. – NiegodziwyBeru May 04 '19 at 14:42

1 Answers1

1

Actually (running this against ncurses 6.1 development), I do not see a failure from init_extended_pair. At first glance, the problem appeared to be this chunk:

attron(COLOR_PAIR(pair3));
mvprintw(4, 1, "pair32767");
attroff(COLOR_PAIR(pair3));

Those attron/attroff are legacy functions. You should use attr_on and attr_off. The macro form of attron and attroff (which is normally used instead of the function) is

#define wattron(win,at)         wattr_on(win, NCURSES_CAST(attr_t, at), NULL)
#define wattroff(win,at)        wattr_off(win, NCURSES_CAST(attr_t, at), NULL)

But in either case, the data is the "same": what fits in attr_t (a 32-bit value). In some other functions, the color-pair is passed through separately, and ncurses 6.1 provides for passing pairs larger than 16-bits via the opts parameter. These particular functions aren't extended in that way.

However, your program is returning an error for init_extended_pair. That could be any of (a few) returns from _nc_init_pair, but the principal one uses ValidPair:

#define ValidPair(sp,pair) \
((sp != 0) && (pair >= 0) && (pair < sp->_pair_limit) && sp->_coloron)

To check this, I ran the code against current ncurses6, with TERM=xterm-256color and TERM=xterm-direct. Both worked, though the init_extended_color in the latter fails (as expected). I can see that failure by compiling ncurses with TRACE, and turning the trace on with NCURSES_TRACE=0x220. Here's a screenshot of the trace, for example:

screenshot showing ncurses traces

The current code is available from the ncurses homepage (here). If you are able to reproduce the problem using current code, you might want to discuss it on the bug-ncurses mailing list. Otherwise (see mailing list), the Debian package is the reference for the version you are using.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • Hi @thomas-dickey, and how should I call this two functions? In link you have provided there is note: _"...wattr_on and wattr_off are not used by this implementation except to check that they are NULL"_ (I believe that `attr_on/off` are aliases to `wattr_on/off`, and in source code `opts` is set to `GCC_UNUSED` (base/lib_wattron.c:48). Second/first problem is with `init_extended_pair` that for `pair3` return `ERR` and this problem occur before I even call `attron/off`. Note that, for `pair3` I expect inverted colors (green foreground, red background). – NiegodziwyBeru May 05 '19 at 14:42
  • I don't see `TERM` mentioned. If I run your example with `TERM=xterm-256color`, then`init_extended_pair` succeeds. It won't work with `TERM=xterm-direct`, because the RGB palette is unchangeable. The [package rules](https://github.com/ThomasDickey/ncurses-snapshots/blob/master/package/debian/rules) are in the ncurses source. As for "aliases", it helps to read ``... – Thomas Dickey May 05 '19 at 15:44
  • I'm looking at the "except to check" aspect, will update answer in a while :-) – Thomas Dickey May 05 '19 at 16:55
  • Yes, I have xterm-256color: `echo $TERM xterm-256color` Maybe I will try on another distribution. – NiegodziwyBeru May 05 '19 at 16:56
  • Distribution doesn't matter much: just the source-version and how you have configured ncurses. – Thomas Dickey May 06 '19 at 07:59