openbsd-misc
[Top] [All Lists]

ncurses and terminal types

To: misc@openbsd.org
Subject: ncurses and terminal types
From: Phlucks <phlucks@dqc.org>
Date: Thu, 28 Feb 2002 15:39:38 -0800 (PST)
Sender: owner-misc@openbsd.org
I am playing around with ncurses sample code and I am having a lot of
problems getting certain ncurses functions to work with various terminals
in OpenBSD.

My main problems are function keys, and color.

The only terminal that seems to support both of these in OpenBSD is
xterm-xfree86, and it only supports both when used in XF4.

xterm in XF4 doesn't support function keys in ncurses.

in a console screen, xterm *does* support function keys, but not color.
The only terminal I used that supported color in a console was ansi, and
it didn't work with anything else in curses...

For the project I am working on, it would be a big help if there was a
terminal type that supported both funtion keys and color in a console.
To illustrate my problem, here is some sample code that uses color and
function keys.  If the prompt to press F1 is blue, and pressing F1 exits
the program, then that terminal type works.

the only terminal that I have tried that works is xterm-xfree86, and it
only works in XF4...

compile like so: gcc filename.c -o filename -lcurses

____________begin sample code____________________
/* File path: basics/other_border.c */
#include <ncurses.h>

typedef struct _win_border_struct {
    chtype  ls, rs, ts, bs,
            tl, tr, bl, br;
}WIN_BORDER;

typedef struct _WIN_struct {
    int startx, starty;
    int height, width;
    WIN_BORDER border;
}WIN;

void init_win_params(WIN *p_win);
void print_win_params(WIN *p_win);
void create_box(WIN *win, int bool);

int main(int argc, char *argv[])
{    WIN win;
    int ch;

    initscr();      /* Start curses mode         */
    start_color();  /* Start the color functionality */
    cbreak();       /* Line buffering disabled, Pass on
                     * every thing to me         */
    keypad(stdscr, TRUE);        /* I need that nifty F1     */
    noecho();
    init_pair(1, COLOR_CYAN, COLOR_BLACK);

    /* Initialize the window parameters */
    init_win_params(&win);
    print_win_params(&win);

    attron(COLOR_PAIR(1));
    printw("Press F1 to exit");
    refresh();
    attroff(COLOR_PAIR(1));

    create_box(&win, TRUE);
    while((ch = getch()) != KEY_F(1))
    {    switch(ch)
        {    case KEY_LEFT:
                create_box(&win, FALSE);
                --win.startx;
                create_box(&win, TRUE);
                break;
            case KEY_RIGHT:
                create_box(&win, FALSE);
                ++win.startx;
                create_box(&win, TRUE);
                break;
            case KEY_UP:
                create_box(&win, FALSE);
                --win.starty;
                create_box(&win, TRUE);
                break;
            case KEY_DOWN:
                create_box(&win, FALSE);
                ++win.starty;
                create_box(&win, TRUE);
                break;
        }
    }
    endwin();   /* End curses mode */
    return 0;
}
void init_win_params(WIN *p_win)
{
    p_win->height = 3;
    p_win->width = 10;
    p_win->starty = (LINES - p_win->height)/2;
    p_win->startx = (COLS - p_win->width)/2;

    p_win->border.ls = '|';
    p_win->border.rs = '|';
    p_win->border.ts = '-';
    p_win->border.bs = '-';
    p_win->border.tl = '+';
    p_win->border.tr = '+';
    p_win->border.bl = '+';
    p_win->border.br = '+';

}
void print_win_params(WIN *p_win)
{
#ifdef _DEBUG
    mvprintw(25, 0, "%d %d %d %d", p_win->startx, p_win->starty,
                p_win->width, p_win->height);
    refresh();
#endif
}
void create_box(WIN *p_win, int bool)
{    int i, j;
    int x, y, w, h;

    x = p_win->startx;
    y = p_win->starty;
    w = p_win->width;
    h = p_win->height;

    if(bool == TRUE)
    {    mvaddch(y, x, p_win->border.tl);
        mvaddch(y, x + w, p_win->border.tr);
        mvaddch(y + h, x, p_win->border.bl);
        mvaddch(y + h, x + w, p_win->border.br);
        mvhline(y, x + 1, p_win->border.ts, w - 1);
        mvhline(y + h, x + 1, p_win->border.bs, w - 1);
        mvvline(y + 1, x, p_win->border.ls, h - 1);
        mvvline(y + 1, x + w, p_win->border.rs, h - 1);

    }
    else
        for(j = y; j <= y + h; ++j)
            for(i = x; i <= x + w; ++i)
                mvaddch(j, i, ' ');

    refresh();

}
_________________end sample code____________________________

deeper explanations as to why seemingly simular terminals behave poorly
with ncurses and what I can do to work around this wouldbe great...

TimH

<Prev in Thread] Current Thread [Next in Thread>
  • ncurses and terminal types, Phlucks <=