Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can the original terminal palette be acquired, preferably using ncurses routines?

I'm developing an Ncurses application that uses its own palette via init_color()(assuming can_change_colors() returned non-zero). If COLORS is less than 256, I modify only the first 8 colors (assuming COLORS is at least 8). Otherwise, I modify up to the first 256.

The ncurses documentation claims that calling endwin() on stdscr will reset the palette. In Gnome-terminal, however, I'm not seeing the palette restored by Ncurses. Instead, whatever palette I last used is carried beyond my application. My first question, then, is why the palette is not being properly restored (everything else — cursor location, screen text, etc — is).

I could work around this (I know this is already questionable) by acquiring the palette on startup, and restoring it manually. I'm attempting to do this with a color_content() loop following a successful call to start_color(). Unfortunately, this seems to return the default ncurses palette, not the palette actually being used (tested with Gnome-terminal's default palette, and a configured Solarized palette). I've verified this by checking against the values in ncurses/base/lib_color.c from Debian's ncurses-5.9 sources.

So:

  • Why isn't the palette restored by a successful call to endwin(stdscr)?

  • How can I acquire the "true" palette on startup? Ncurses is preferred, but Unix- or even Linux-specific ioctl()s etc would suffice for now.

Relevant code can be found on Github. What's there currently might not match exactly up with what I described above, as I've been experimenting with various techniques without success.

like image 999
nick black Avatar asked Dec 31 '11 04:12

nick black


1 Answers

So, with the bounty expiring today, I've gone ahead and done some thorough source diving. I believe I have the (rather discouraging) answers:

(1) endwin() is resetting the palette, but it appears to reset it to the definition from the terminfo config for the TERM value. palette changes by any application aren't portably written into the terminfo database (obviously) or context (rather more unfortunately), and are thus unavailable to ncurses across applications.

(2) there is no general way to do this. the proof is by contradiction, and follows directly from (1).

alas, poor Yorick! a fellow of infinite jest.

unless someone demonstrates me as being out of my mind, i consider this question resolved.

like image 80
nick black Avatar answered Dec 18 '22 14:12

nick black