C-Menu 0.2.9
A User Interface Toolkit
Loading...
Searching...
No Matches
dwin.c File Reference

Window support for C-Menu - EXPERIMENTAL. More...

#include <cm.h>
#include <errno.h>
#include <math.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <wait.h>
#include <wchar.h>
Include dependency graph for dwin.c:

Go to the source code of this file.

Macros

#define NC   true

Functions

bool open_curses (SIO *sio)
 Initialize NCurses and color settings.
void destroy_curses ()
 Gracefully shut down NCurses and restore terminal settings.
int win_new (int wlines, int wcols, int wbegy, int wbegx, char *wtitle, int flag)
 Create a new window with optional box and title.
void win_redraw (WINDOW *win)
 Redraw the specified window.
void win_resize (int wlines, int wcols, char *title)
 Resize the current window and its box, and update the title.
WINDOW * win_del ()
 Delete the current window and its associated box window.
void restore_wins ()
 Restore all windows after a screen resize.
void cbox (WINDOW *box)
 Draw a box around the specified window.
void win_init_attrs ()
 Initialize window attributes.
int Perror (char *emsg_str)
 Display a simple error message window or print to stderr.
void mvwaddstr_fill (WINDOW *w, int y, int x, char *s, int l)
 For lines shorter than their display area, fill the rest with spaces.
void abend (int ec, char *s)
 Abnormal program termination.
int nf_error (int ec, char *s)
 Display error message and wait for key press.
void list_colors ()
 list colors to stderr
int clr_name_to_idx (char *s)
 Get color index from color name.
void init_hex_clr (int idx, char *s)
 Initialize extended ncurses color from HTML style hex string.
int rgb_clr_to_cube (int)
RGB hex_clr_str_to_rgb (char *s)
 Convert six-digit HTML style hex color code to RGB struct.
RGB xterm256_idx_to_rgb (int idx)
 Convert XTerm 256 color index to RGB color.
int rgb_to_curses_clr (RGB *rgb)
 Get color index for RGB color.
int rgb_to_xterm256_idx (RGB *rgb)
 Convert RGB color to XTerm 256 color index.
void apply_gamma (RGB *rgb)
 Apply gamma correction to RGB color.
Chyronnew_chyron ()
 Create and initialize Chyron structure.
void set_chyron_key (Chyron *, int, char *, int)
void set_chyron_key_cp (Chyron *chyron, int k, char *s, int kc, int cp)
 Set chyron key.
bool is_set_chyron_key (Chyron *chyron, int k)
 Check if function key label is set.
void unset_chyron_key (Chyron *chyron, int k)
 Unset chyron key.
void compile_chyron (Chyron *chyron)
 construct the chyron string from the chyron structure
int get_chyron_key (Chyron *chyron, int x)
 Get keycode from chyron.
Chyrondestroy_chyron (Chyron *chyron)
 Destroy Chyron structure.
int mb_to_cc (cchar_t *cmplx_buf, char *str, attr_t attr, int cpx, int *pos, int maxlen)
 Convert multibyte string to complex character array.
Chyronwait_mk_chyron ()
 Create a Chyron struct for the waiting message.
WINDOW * wait_mk_win (Chyron *chyron, char *title)
 Display a popup waiting message.
int wait_continue (WINDOW *wait_win, Chyron *chyron, int remaining)
 Update the waiting message with remaining time and check for user input.
bool wait_destroy (Chyron *chyron)
 Destroy the waiting message window and chyron.
int xwgetch (WINDOW *win, Chyron *chyron, int n)
 Wrapper for wgetch that handles signals, mouse events, checks for clicks on the chyron line, and accepts a sinigle character answer.
bool init_clr_palette (SIO *sio)
 Initialize color palette based on SIO settings.
cchar_t mkccc (int cp)
 Create a cchar_t with the specified color pair index.
void display_chyron (WINDOW *win, Chyron *chyron, int line, int col)
cchar_t * mk_cmplx_buf (const char *s)
 Create complex character buffer from multibyte string.
int get_clr_pair (int fg, int bg)
 Get color pair index for foreground and background colors.
int answer_yn (char *em0, char *em1, char *em2, char *em3)
 Accept a single letter answer.
int display_error (char *em0, char *em1, char *em2, char *em3)
 Display an error message window or print to stderr.
bool action_disposition (char *title, char *action_str)
bool waitpid_with_timeout (pid_t pid, int timeout)

Variables

int click_y
int click_x
SCREEN * screen
SIOsio
RGB StdColors [16]
char const colors_text [][10]
const wchar_t bw_ho = BW_HO
const wchar_t bw_ve = BW_VE
const wchar_t bw_tl = BW_RTL
const wchar_t bw_tr = BW_RTR
const wchar_t bw_bl = BW_RBL
const wchar_t bw_br = BW_RBR
const wchar_t bw_lt = BW_LT
const wchar_t bw_rt = BW_RT
const wchar_t bw_sp = BW_SP
double GRAY_GAMMA
double RED_GAMMA
double GREEN_GAMMA
double BLUE_GAMMA
WINDOW * win
WINDOW * win_win [MAXWIN]
WINDOW * win_box [MAXWIN]
int exit_code
unsigned int cmd_key
bool f_sigwench = false
int win_attr
int box_attr
int win_ptr
int m_lines
int m_cols
int m_begy = -1
int m_begx = -1
int mouse_support
int stdin_fd
int stdout_fd
int src_line
char * src_name
char fn [MAXLEN]
char em0 [MAXLEN]
char em1 [MAXLEN]
char em2 [MAXLEN]
char em3 [MAXLEN]
int cp_norm
int cp_win
int cp_box
int cp_reverse
int cp_reverse_highlight
int cp_ln
int clr_cnt = 0
int clr_pair_idx = 1
int clr_pair_cnt = 1
cchar_t CCC_NORM
cchar_t CCC_WIN
cchar_t CCC_BG
cchar_t CCC_BOX
cchar_t CCC_REVERSE
cchar_t CCC_REVERSE_HIGHLIGHT
cchar_t CCC_LN
int tty_fd
int pipe_in
int pipe_out
FILE * ncurses_fp

Detailed Description

Window support for C-Menu - EXPERIMENTAL.

Note
This file contains functions for managing NCurses windows and color settings for the Chyron structure for function key labels and mouse click handling. This file is a work in progress and may be subject to change as the C-Menu project evolves. Generally, don't try to use it yet unless you want complete the half-done code modifications.
Author
Bill Waller Copyright (c) 2025 MIT License billx.nosp@m.wall.nosp@m.er@gm.nosp@m.ail..nosp@m.com
Date
2026-02-09

Definition in file dwin.c.

Macro Definition Documentation

◆ NC

#define NC   true

Definition at line 29 of file dwin.c.

Function Documentation

◆ action_disposition()

bool action_disposition ( char * title,
char * action_str )

Definition at line 1224 of file dwin.c.

1224 {
1225 int len;
1226 int line, col;
1227 WINDOW *action_disposition_win;
1228
1229 if (!f_curses_open) {
1230 fprintf(stderr, "\n%s\n", title);
1231 fprintf(stderr, "%s\n", action_str);
1232 return true;
1233 }
1234 Chyron *chyron = new_chyron();
1235 set_chyron_key(chyron, 10, "F10 Continue", KEY_F(10));
1236 compile_chyron(chyron);
1237 len = max(strlen(title), strlen(action_str));
1238 col = (COLS - len - 4) / 2;
1239 line = (LINES - 4) / 2;
1240 if (win_new(2, len + 2, line, col, title, 0)) {
1241 ssnprintf(em0, MAXLEN - 1, "win_new(%d, %d, %d, %d, %s, %b) failed", 4,
1242 line, line, col, title, 0);
1243 Perror(em0);
1244 }
1245 action_disposition_win = win_win[win_ptr];
1246 mvwaddstr(action_disposition_win, 0, 1, action_str);
1247 display_chyron(action_disposition_win, chyron, 1, 0);
1248 wmove(action_disposition_win, 1, chyron->l);
1249 cmd_key = xwgetch(action_disposition_win, chyron, 1);
1250 win_del();
1251 destroy_chyron(chyron);
1252 return true;
1253}
bool f_curses_open
Definition sig.c:33
#define max(a, b)
max macro evaluates two expressions, returning greatest result.
Definition cm.h:48
#define MAXLEN
Definition curskeys.c:15
unsigned int cmd_key
Definition dwin.c:117
WINDOW * win_win[MAXWIN]
Definition dwin.c:114
void display_chyron(WINDOW *win, Chyron *chyron, int line, int col)
Definition dwin.c:297
int win_ptr
Definition dwin.c:121
char em0[MAXLEN]
Definition dwin.c:132
void set_chyron_key(Chyron *, int, char *, int)
Definition dwin.c:245
int xwgetch(WINDOW *, Chyron *, int)
Wrapper for wgetch that handles signals, mouse events, checks for clicks on the chyron line,...
Definition dwin.c:1359
int win_new(int, int, int, int, char *, int)
Create a new window with optional box and title.
Definition dwin.c:783
WINDOW * win_del()
Delete the current window and its associated box window.
Definition dwin.c:902
Chyron * destroy_chyron(Chyron *chyron)
Destroy Chyron structure.
Definition dwin.c:198
void compile_chyron(Chyron *)
construct the chyron string from the chyron structure
Definition dwin.c:268
Chyron * new_chyron()
Create and initialize Chyron structure.
Definition dwin.c:183
int Perror(char *)
Display a simple error message window or print to stderr.
Definition dwin.c:1110
size_t ssnprintf(char *, size_t, const char *,...)
ssnprintf was designed to be a safer alternative to snprintf.
Definition futil.c:147
Definition cm.h:240
int l
Definition cm.h:244

References cmd_key, compile_chyron(), destroy_chyron(), display_chyron(), em0, f_curses_open, Chyron::l, new_chyron(), Perror(), set_chyron_key(), ssnprintf(), win_del(), win_new(), win_ptr, win_win, and xwgetch().

Here is the call graph for this function:

◆ display_chyron()

void display_chyron ( WINDOW * win,
Chyron * chyron,
int line,
int col )

Definition at line 297 of file dwin.c.

297 {
298 wmove(win, line, 0);
299 wclrtoeol(win);
300 wmove(win, line, 0);
301 wadd_wchstr(win, chyron->cmplx_buf);
302 wmove(win, line, col);
303 return;
304}
WINDOW * win
Definition dwin.c:113
cchar_t cmplx_buf[MAXLEN]
Definition cm.h:243

References Chyron::cmplx_buf.

Referenced by action_disposition(), answer_yn(), display_error(), display_page(), field_editor(), form_display_fields(), form_post(), form_process(), Perror(), wait_continue(), and wait_mk_win().

Here is the caller graph for this function:

◆ rgb_clr_to_cube()

int rgb_clr_to_cube ( int )

◆ set_chyron_key()

void set_chyron_key ( Chyron * chyron,
int k,
char * s,
int kc )

Definition at line 245 of file dwin.c.

245 {
246 if (*s != '\0')
247 ssnprintf(chyron->key[k]->text, CHYRON_KEY_MAXLEN - 1, "%s", s);
248 else
249 chyron->key[k]->text[0] = '\0';
250 chyron->key[k]->keycode = kc;
251 chyron->key[k]->cp = cp_reverse;
252}
#define CHYRON_KEY_MAXLEN
Definition cm.h:227
int cp_reverse
Definition dwin.c:139
char text[CHYRON_KEY_MAXLEN]
Definition cm.h:233
int cp
Definition cm.h:237
int keycode
Definition cm.h:235
ChyronKey * key[CHYRON_KEYS]
Definition cm.h:241

References ChyronKey::cp, cp_reverse, Chyron::key, ChyronKey::keycode, ssnprintf(), and ChyronKey::text.

Referenced by action_disposition(), answer_yn(), display_error(), form_display_fields(), form_post(), form_process(), Perror(), pick_engine(), and wait_mk_chyron().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ waitpid_with_timeout()

bool waitpid_with_timeout ( pid_t pid,
int timeout )

Definition at line 1431 of file dwin.c.

1431 {
1432 int status;
1433 int rc;
1434 Chyron *wait_chyron;
1435 WINDOW *wait_win;
1436 int remaining = timeout;
1437
1438 usleep(100000); // Sleep for 200ms */
1439 rc = waitpid(pid, &status, WNOHANG);
1440 if (rc == pid)
1441 return true;
1442 if (rc == -1) {
1443 ssnprintf(em0, MAXLEN - 1, "Error waiting for process %d", pid);
1444 Perror(em0);
1445 return false;
1446 }
1447 wait_chyron = wait_mk_chyron();
1448 ssnprintf(em0, MAXLEN - 1, "Waiting for process %d to finish...", pid);
1449 wait_win = wait_mk_win(wait_chyron, em0);
1450 cmd_key = 0;
1451 while (rc == 0 && remaining > 0 && cmd_key != KEY_F(9)) {
1452 cmd_key = wait_continue(wait_win, wait_chyron, remaining);
1453 if (cmd_key == KEY_F(9))
1454 break;
1455 remaining--;
1456 }
1457 wait_destroy(wait_chyron);
1458 if (rc == pid)
1459 return true;
1460 return false;
1461}
bool wait_destroy(Chyron *)
Destroy the waiting message window and chyron.
Definition dwin.c:1203
int wait_continue(WINDOW *, Chyron *, int)
Update the waiting message with remaining time and check for user input.
Definition dwin.c:1215
WINDOW * wait_mk_win(Chyron *, char *)
Display a popup waiting message.
Definition dwin.c:1172
Chyron * wait_mk_chyron()
Create a Chyron struct for the waiting message.
Definition dwin.c:1161

References cmd_key, em0, Perror(), ssnprintf(), wait_continue(), wait_destroy(), wait_mk_chyron(), and wait_mk_win().

Referenced by form_process(), init_pick(), and view_init_input().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ BLUE_GAMMA

double BLUE_GAMMA
Initial value:
=
1.2

Gamma correction value for blue colors. Set in .minitrc

Definition at line 110 of file dwin.c.

Referenced by apply_gamma(), and open_curses().

◆ box_attr

int box_attr

Definition at line 120 of file dwin.c.

◆ bw_bl

const wchar_t bw_bl = BW_RBL

bottom left corner

Definition at line 99 of file dwin.c.

Referenced by cbox().

◆ bw_br

const wchar_t bw_br = BW_RBR

bottom right corner

Definition at line 100 of file dwin.c.

Referenced by cbox().

◆ bw_ho

const wchar_t bw_ho = BW_HO

horizontal line

Definition at line 95 of file dwin.c.

Referenced by cbox().

◆ bw_lt

const wchar_t bw_lt = BW_LT

left tee

Definition at line 101 of file dwin.c.

Referenced by win_new(), and win_resize().

◆ bw_rt

const wchar_t bw_rt = BW_RT

right tee

Definition at line 102 of file dwin.c.

Referenced by win_new(), and win_resize().

◆ bw_sp

const wchar_t bw_sp = BW_SP

tee space

Definition at line 103 of file dwin.c.

Referenced by win_new(), and win_resize().

◆ bw_tl

const wchar_t bw_tl = BW_RTL

top left corner

Definition at line 97 of file dwin.c.

Referenced by cbox().

◆ bw_tr

const wchar_t bw_tr = BW_RTR

top right corner

Definition at line 98 of file dwin.c.

Referenced by cbox().

◆ bw_ve

const wchar_t bw_ve = BW_VE

vertical line

Definition at line 96 of file dwin.c.

Referenced by cbox().

◆ CCC_BG

cchar_t CCC_BG

Definition at line 147 of file dwin.c.

◆ CCC_BOX

cchar_t CCC_BOX

box color pair complex character

Definition at line 148 of file dwin.c.

Referenced by open_curses(), win_new(), and win_resize().

◆ CCC_LN

cchar_t CCC_LN

Definition at line 151 of file dwin.c.

Referenced by init_view_boxwin(), init_view_full_screen(), and open_curses().

◆ CCC_NORM

cchar_t CCC_NORM

normal color pair complex character

Definition at line 145 of file dwin.c.

Referenced by open_curses(), and win_del().

◆ CCC_REVERSE

cchar_t CCC_REVERSE

reverse color pair complex character

Definition at line 149 of file dwin.c.

Referenced by open_curses().

◆ CCC_REVERSE_HIGHLIGHT

cchar_t CCC_REVERSE_HIGHLIGHT

Definition at line 150 of file dwin.c.

Referenced by open_curses().

◆ CCC_WIN

cchar_t CCC_WIN

window color pair complex character

Definition at line 146 of file dwin.c.

Referenced by init_view_boxwin(), init_view_full_screen(), open_curses(), win_new(), and win_resize().

◆ click_x

int click_x

the x coordinate of a mouse click

Definition at line 45 of file dwin.c.

Referenced by field_editor(), form_post(), form_process(), menu_cmd_processor(), picker(), and xwgetch().

◆ click_y

int click_y

the y coordinate of a mouse click

Definition at line 44 of file dwin.c.

Referenced by field_editor(), form_post(), form_process(), menu_cmd_processor(), picker(), and xwgetch().

◆ clr_cnt

int clr_cnt = 0

number of colors used

Definition at line 142 of file dwin.c.

Referenced by init_clr_palette(), and rgb_to_curses_clr().

◆ clr_pair_cnt

int clr_pair_cnt = 1

number of color pairs supported by the terminal

Definition at line 144 of file dwin.c.

Referenced by get_clr_pair().

◆ clr_pair_idx

int clr_pair_idx = 1

current color pair index

Definition at line 143 of file dwin.c.

Referenced by parse_ansi_str().

◆ cmd_key

unsigned int cmd_key

the command key for the current command, for error messages and other output

Definition at line 117 of file dwin.c.

Referenced by action_disposition(), answer_yn(), display_error(), field_navigator(), form_read_data(), init_pick(), view_init_input(), wait_continue(), and waitpid_with_timeout().

◆ colors_text

char const colors_text[][10]
Initial value:
= {
"black", "red", "green", "yellow", "blue", "magenta", "cyan",
"white", "orange", "bg", "abg", "bblack", "bred", "bgreen",
"byellow", "bblue", "bcyan", "bmagenta", "bwhite", "borange", ""}

colors_text

Note
Color names for .minitrc overrides
These names are used in .minitrc to specify color overrides
The order of these names corresponds to the colors_enum values

Definition at line 91 of file dwin.c.

91 {
92 "black", "red", "green", "yellow", "blue", "magenta", "cyan",
93 "white", "orange", "bg", "abg", "bblack", "bred", "bgreen",
94 "byellow", "bblue", "bcyan", "bmagenta", "bwhite", "borange", ""};

Referenced by clr_name_to_idx(), and list_colors().

◆ cp_box

int cp_box

box color pair index

Definition at line 138 of file dwin.c.

Referenced by open_curses().

◆ cp_ln

int cp_ln

line number color pair index

Definition at line 141 of file dwin.c.

Referenced by open_curses().

◆ cp_norm

int cp_norm

normal color pair index

Definition at line 136 of file dwin.c.

Referenced by open_curses().

◆ cp_reverse

int cp_reverse

reverse color pair index

Definition at line 139 of file dwin.c.

Referenced by compile_chyron(), field_editor(), open_curses(), and set_chyron_key().

◆ cp_reverse_highlight

int cp_reverse_highlight

reverse highlight color pair index

Definition at line 140 of file dwin.c.

Referenced by field_editor(), form_process(), and open_curses().

◆ cp_win

int cp_win

window color pair index

Definition at line 137 of file dwin.c.

Referenced by fmt_line(), mk_cmplx_buf(), open_curses(), and search().

◆ em0

◆ em1

◆ em2

◆ em3

char em3[MAXLEN]

error message string for error messages

Definition at line 135 of file dwin.c.

Referenced by verify_dir(), and verify_file().

◆ exit_code

int exit_code

the exit code for the program, for error messages and other output

Definition at line 116 of file dwin.c.

◆ f_sigwench

bool f_sigwench = false

Definition at line 118 of file dwin.c.

◆ fn

char fn[MAXLEN]

function name for error messages

Definition at line 131 of file dwin.c.

Referenced by verify_dir(), and verify_file().

◆ GRAY_GAMMA

double GRAY_GAMMA
Initial value:
=
1.2

Gamma correction value for gray colors. Set in .minitrc

Definition at line 104 of file dwin.c.

Referenced by apply_gamma(), and open_curses().

◆ GREEN_GAMMA

double GREEN_GAMMA
Initial value:
=
1.2

Gamma correction value for green colors. Set in .minitrc

Definition at line 108 of file dwin.c.

Referenced by apply_gamma(), and open_curses().

◆ m_begx

int m_begx = -1

Definition at line 125 of file dwin.c.

◆ m_begy

int m_begy = -1

Definition at line 124 of file dwin.c.

◆ m_cols

int m_cols

Definition at line 123 of file dwin.c.

◆ m_lines

int m_lines

Definition at line 122 of file dwin.c.

◆ mouse_support

int mouse_support

Definition at line 126 of file dwin.c.

◆ ncurses_fp

FILE* ncurses_fp

Definition at line 154 of file dwin.c.

Referenced by destroy_curses(), and open_curses().

◆ pipe_in

int pipe_in

Definition at line 153 of file dwin.c.

◆ pipe_out

int pipe_out

Definition at line 153 of file dwin.c.

◆ RED_GAMMA

double RED_GAMMA
Initial value:
=
1.2

Gamma correction value for red colors. Set in .minitrc

Definition at line 106 of file dwin.c.

Referenced by apply_gamma(), and open_curses().

◆ screen

SCREEN* screen

Definition at line 75 of file dwin.c.

Referenced by destroy_curses(), and open_curses().

◆ sio

SIO* sio

Global pointer to SIO struct for terminal and color settings

Definition at line 77 of file dwin.c.

◆ src_line

int src_line

the line number of the source file being processed, for error messages

Definition at line 129 of file dwin.c.

Referenced by verify_dir(), and verify_file().

◆ src_name

char* src_name

the name of the source file being processed, for error messages

Definition at line 130 of file dwin.c.

Referenced by verify_dir(), and verify_file().

◆ StdColors

RGB StdColors[16]
Initial value:
= {
{0, 0, 0}, {128, 0, 0}, {0, 128, 0}, {128, 128, 0},
{0, 0, 128}, {128, 0, 128}, {0, 128, 128}, {192, 192, 192},
{128, 128, 128}, {255, 0, 0}, {0, 255, 0}, {255, 255, 0},
{0, 0, 255}, {255, 0, 255}, {0, 255, 255}, {255, 255, 255}}

StdColors

Note
Standard 16 colors for xterm256 color conversions
These colors can be overridden in ".minitrc"

Definition at line 82 of file dwin.c.

82 {
83 {0, 0, 0}, {128, 0, 0}, {0, 128, 0}, {128, 128, 0},
84 {0, 0, 128}, {128, 0, 128}, {0, 128, 128}, {192, 192, 192},
85 {128, 128, 128}, {255, 0, 0}, {0, 255, 0}, {255, 255, 0},
86 {0, 0, 255}, {255, 0, 255}, {0, 255, 255}, {255, 255, 255}};

Referenced by init_hex_clr(), and xterm256_idx_to_rgb().

◆ stdin_fd

int stdin_fd

Definition at line 127 of file dwin.c.

◆ stdout_fd

int stdout_fd

Definition at line 128 of file dwin.c.

◆ tty_fd

int tty_fd

Global file/pipe numbers

Definition at line 153 of file dwin.c.

Referenced by picker().

◆ win

WINDOW* win

generic window pointer, used for various purposes

Definition at line 113 of file dwin.c.

◆ win_attr

int win_attr

Ncurses attributes for the current window, such as color pair, bold, etc.

Definition at line 119 of file dwin.c.

◆ win_box

WINDOW* win_box[MAXWIN]

array of pointers to box windows, indexed by window ID

Definition at line 115 of file dwin.c.

Referenced by destroy_curses(), form_display_screen(), menu_engine(), open_pick_win(), popup_ckeys(), restore_wins(), win_del(), win_new(), and win_resize().

◆ win_ptr

int win_ptr

Pointer to the current window pair, box and window, which can be used to keep track of the currently active window and its associated box.

Definition at line 121 of file dwin.c.

Referenced by action_disposition(), answer_yn(), destroy_curses(), display_error(), form_display_screen(), menu_engine(), open_curses(), open_pick_win(), Perror(), popup_ckeys(), restore_wins(), wait_mk_win(), win_del(), win_new(), and win_resize().

◆ win_win

WINDOW* win_win[MAXWIN]