CURSES and TERM on Mint

Questions about applications and software
Forum rules
Before you post please read this

CURSES and TERM on Mint

Postby celem on Thu Jan 05, 2012 5:34 pm

I am having trouble with keyboard input using the ncurses library. Mint defaults to TERM == xterm
Code: Select all
$ecomer@pennyroyal echo $TERM
xterm


Code, such as the snippit below fails because KEY_UP is defined in curses.h and ncurses.h as 0x403 but getch() returns 0x103 from the keyboard:

Code: Select all
    keypad(mainwin, TRUE);
...
    while ( (ch = getch()) != 'q' ) {
   switch ( ch ) {
   case KEY_UP:
...


curses.h and ncurses.h define:

Code: Select all
#define KEY_UP      0403      /* up-arrow key */



Terminfo for xterm is:
Code: Select all
ecomer@pennyroyal:/usr/share/terminfo/x$ captoinfo xterm
"xterm", line 1, col 1: Illegal character (expected alphanumeric or @%&*!#) - '^Z'
ecomer@pennyroyal:/usr/share/terminfo/x$ infocmp xterm
#   Reconstructed via infocmp from file: /lib/terminfo/x/xterm
xterm|X11 terminal emulator,
...
   cup=\E[%i%p1%d;%p2%dH, cuu=\E[%p1%dA, cuu1=\E[A,
...


So, xterm is outputting "\E[A", the DEC VT220 sequence for up-arrow but ncurses is translating it into 0x103 instead of 0x403.

What am I missing?
celem
Level 2
Level 2
 
Posts: 91
Joined: Mon Aug 18, 2008 10:39 pm
Location: North Carolina

Linux Mint is funded by ads and donations.
 

Re: CURSES and TERM on Mint

Postby DrHu on Thu Jan 05, 2012 7:29 pm

The termininfo database (data) is the file that manages terminal keys/responses..

Playing around with the default bash TERM environment may cause you some problems, however you can use some utilities to setup term keys or just try another term type eg Linux (VT100 terminal type equivalent)
http://tldp.org/HOWTO/Text-Terminal-HOWTO-16.html
  • 16.6 TERM Variable
    The Environment variable TERM should be set to the name of terminal which you are using. If TERM hasn't been set yet and you don't know the name of your terminal see What is the terminfo name of my terminal ?. It is normally set by the terminal_type parameter passed to the getty program (look at it in the /etc/inittab file). This name must be in the Terminfo data base. Just type "set" at the command line to see what TERM is set to (or type: tset -q). At a console (monitor) TERM is set to "linux" which is the PC monitor emulating a fictitious terminal model named "linux". Since "linux" is close to a vt100 terminal and many text terminals are also, the "linux" designation will sometimes work as a temporary expedient with a text terminal.
  • One way to do this is to use "reset" (same as "tset"; see the manual page)...

Again you can probably badly mange your bash terminal by mucking around with this..

Also thinking you may need to check the keycodes being generated by your actual keyboard, maybe there is no single standard type..
http://linux.die.net/man/1/showkey
http://www.comptechdoc.org/os/linux/use ... minal.html
--some other utils to work with the keycodes.. (xmodmap etc.)
User avatar
DrHu
Level 16
Level 16
 
Posts: 6792
Joined: Wed Jun 17, 2009 8:20 pm

Re: CURSES and TERM on Mint

Postby celem on Thu Jan 05, 2012 7:42 pm

Thank you for your reply.

I am not using a terminal, just the standard keyboard and screen of the PC. The default TERM of Mint is xterm and this doesn't work. Setting TERM to vt100 or linux does not help. Setting TERM to console is rejected (console is not in TERMINFO).

It seems to me that the "default" TERM should be compatible with default curses, yet it is not.
celem
Level 2
Level 2
 
Posts: 91
Joined: Mon Aug 18, 2008 10:39 pm
Location: North Carolina

Re: CURSES and TERM on Mint

Postby Thomas Dickey on Thu Jan 05, 2012 9:13 pm

curses.h defines KEY_UP as 0403 (octal), which happens to be the same as 0x103 (hexadecimal)
Thomas Dickey
Level 1
Level 1
 
Posts: 11
Joined: Wed Jul 14, 2010 6:27 pm

Re: CURSES and TERM on Mint

Postby Thomas Dickey on Thu Jan 05, 2012 9:17 pm

celem wrote:
Code: Select all
    keypad(mainwin, TRUE);
...
    while ( (ch = getch()) != 'q' ) {
   switch ( ch ) {
   case KEY_UP:
...

What am I missing?


The usual problem (which is not possible to be certain, since the example is incomplete)
is using a "char" rather than an "int" to store the result from getch.
Thomas Dickey
Level 1
Level 1
 
Posts: 11
Joined: Wed Jul 14, 2010 6:27 pm

Re: CURSES and TERM on Mint

Postby celem on Thu Jan 05, 2012 9:37 pm

Thomas Dickey - By George, you're right there. I'll pour over the code a bit, putting in more diagnostics and see what is happening. Thanks for the wake up.
celem
Level 2
Level 2
 
Posts: 91
Joined: Mon Aug 18, 2008 10:39 pm
Location: North Carolina

Re: CURSES and TERM on Mint

Postby celem on Thu Jan 05, 2012 10:04 pm

Just ran with some diagnostic tests and curses IS correctly handling the KEY_UP, etc. data. The problem is with the mvwin() function. It isn't working. This is just some boilerplate code from a tutorial on cruses and I see nothing wrong with it. I haven't used curses in more than a decade and I am doing some familiarization for an anticipated project.

Code: Select all
/*
  CURWIN1.C
  =========
  (c) Copyright Paul Griffiths 1999
  Email: mail@paulgriffiths.net

  Moving windows with ncurses.
*/


#include <stdlib.h>
#include <stdio.h>
#include <curses.h>


int main(void) {
   
    WINDOW * mainwin, * childwin;
    int      ch;


    /*  Set the dimensions and initial
   position for our child window   */

    int      width = 23, height = 7;
    int      rows  = 25, cols   = 80;
    int      x = (cols - width)  / 2;
    int      y = (rows - height) / 2;


    /*  Initialize ncurses  */

    if ( (mainwin = initscr()) == NULL ) {
   fprintf(stderr, "Error initialising ncurses.\n");
   exit(EXIT_FAILURE);
    }
   

    /*  Switch of echoing and enable keypad (for arrow keys)  */

    noecho();
    keypad(mainwin, TRUE);


    /*  Make our child window, and add
   a border and some text to it.   */

    childwin = subwin(mainwin, height, width, y, x);
    box(childwin, 0, 0);
    mvwaddstr(childwin, 1, 4, "Move the window");
    mvwaddstr(childwin, 2, 2, "with the arrow keys");
    mvwaddstr(childwin, 3, 6, "or HOME/END");
    mvwaddstr(childwin, 5, 3, "Press 'q' to quit");

    refresh();


    /*  Loop until user hits 'q' to quit  */

    while ( (ch = getch()) != 'q' ) {
      switch ( ch ) {
         case KEY_UP:
            if ( y > 0 )
            --y;
            break;

         case KEY_DOWN:
            if ( y < (rows - height) )
            ++y;
            break;

         case KEY_LEFT:
            if ( x > 0 )
            --x;
            break;

         case KEY_RIGHT:
            if ( x < (cols - width) )
            ++x;
            break;

         case KEY_HOME:
            x = 0;
            y = 0;
            break;

         case KEY_END:
            x = (cols - width);
            y = (rows - height);
            break;
      }
      mvwin(childwin, y, x);
    }
    /*  Clean up after ourselves  */
    delwin(childwin);
    delwin(mainwin);
    endwin();
    refresh();

    return EXIT_SUCCESS;
}
celem
Level 2
Level 2
 
Posts: 91
Joined: Mon Aug 18, 2008 10:39 pm
Location: North Carolina

Re: CURSES and TERM on Mint

Postby Thomas Dickey on Sat Jan 07, 2012 10:04 am

celem wrote:Just ran with some diagnostic tests and curses IS correctly handling the KEY_UP, etc. data. The problem is with the mvwin() function. It isn't working. This is just some boilerplate code from a tutorial on cruses and I see nothing wrong with it. I haven't used curses in more than a decade and I am doing some familiarization for an anticipated project.


Actually, mvwin() works, but not precisely as it did in 1997. That was changed after some discussion and investigation, in 2006,
as noted here: http://invisible-island.net/ncurses/NEWS.html#t20060218

There is an example (test/movewindow.c) in ncurses' source which demonstrates mvwin.
Also, cdk has an example (position_ex) which uses mvwin.
Thomas Dickey
Level 1
Level 1
 
Posts: 11
Joined: Wed Jul 14, 2010 6:27 pm

Re: CURSES and TERM on Mint

Postby celem on Sat Jan 07, 2012 11:09 am

Thomas Dickey - thanks for the pointer. I downloaded a file of examples from that site that contain many uses of mvwin() that apparently worked on an IBM-PC when they were written for PD-curses. They don't even begin to compile on my Mint box but I'll try to get one of the simpler examples working, if possible. There are some pretty complex examples of curses there, such as the xmas.c code that has Santa's reindeer moving around the screen as subwin() using mvwin().
http://invisible-island.net/ncurses/ncurses-examples.html

Aah - I just detected that "you" are Invisible-island!
celem
Level 2
Level 2
 
Posts: 91
Joined: Mon Aug 18, 2008 10:39 pm
Location: North Carolina

Re: CURSES and TERM on Mint

Postby Thomas Dickey on Sat Jan 07, 2012 2:55 pm

celem wrote:Thomas Dickey - thanks for the pointer. I downloaded a file of examples from that site that contain many uses of mvwin() that apparently worked on an IBM-PC when they were written for PD-curses. They don't even begin to compile on my Mint box but I'll try to get one of the simpler examples working, if possible. There are some pretty complex examples of curses there, such as the xmas.c code that has Santa's reindeer moving around the screen as subwin() using mvwin().
http://invisible-island.net/ncurses/ncurses-examples.html

Aah - I just detected that "you" are Invisible-island!


yes (all of the ncurses-examples "should" work, though I generally do just spot-checks, for retesting based on what's changing in the libraries).
Thomas Dickey
Level 1
Level 1
 
Posts: 11
Joined: Wed Jul 14, 2010 6:27 pm

Re: CURSES and TERM on Mint

Postby celem on Sat Jan 07, 2012 8:54 pm

Thomas Dickey - I have been successful in compiling the examples - only with ./configure, make, which explicitly uses certain options that are apparently necessary:

.i.e,
Code: Select all
gcc -g -O2  -o worm ../ncurses-examples-20110404/worm.o -L/lib64  -I. -I. -I../test -DHAVE_CONFIG_H  -D_GNU_SOURCE -g -O2  `echo "-lform -lmenu -lpanel -lncurses    " | sed -e 's/-lform.*-lpanel[^ ]*//'`  -lm -lm


I'll pour over the code after awhile to see what I can learn.
celem
Level 2
Level 2
 
Posts: 91
Joined: Mon Aug 18, 2008 10:39 pm
Location: North Carolina

Re: CURSES and TERM on Mint

Postby Thomas Dickey on Sat Jan 07, 2012 9:32 pm

celem wrote:Thomas Dickey - I have been successful in compiling the examples - only with ./configure, make, which explicitly uses certain options that are apparently necessary:

.i.e,
Code: Select all
gcc -g -O2  -o worm ../ncurses-examples-20110404/worm.o -L/lib64  -I. -I. -I../test -DHAVE_CONFIG_H  -D_GNU_SOURCE -g -O2  `echo "-lform -lmenu -lpanel -lncurses    " | sed -e 's/-lform.*-lpanel[^ ]*//'`  -lm -lm


I'll pour over the code after awhile to see what I can learn.

ok. The makefile is more complicated than you would use ordinarily (since it is a special case of the ncurses makefiles).
I think that movewindow.c is what you are most interested in.
Thomas Dickey
Level 1
Level 1
 
Posts: 11
Joined: Wed Jul 14, 2010 6:27 pm

Re: CURSES and TERM on Mint

Postby celem on Sat Jan 07, 2012 9:47 pm

movewindow.c fails. I create a subwindow with 's' but 't' (move subwindow) displays "error" in the bottom left corner of the screen and the sub-window is not moved. I am not convinced that something is not wrong with mvwin - unless I am really messing up. The help displayed with '?' seems pretty straightforward.
celem
Level 2
Level 2
 
Posts: 91
Joined: Mon Aug 18, 2008 10:39 pm
Location: North Carolina

Re: CURSES and TERM on Mint

Postby celem on Sat Jan 07, 2012 9:47 pm

movewindow.c fails. I create a subwindow with 's' but 't' (move subwindow) displays "error" in the bottom left corner of the screen and the sub-window is not moved. I am not convinced that something is not wrong with mvwin - unless I am really messing up. The help displayed with '?' seems pretty straightforward.
celem
Level 2
Level 2
 
Posts: 91
Joined: Mon Aug 18, 2008 10:39 pm
Location: North Carolina

Re: CURSES and TERM on Mint

Postby Thomas Dickey on Sat Jan 07, 2012 10:04 pm

celem wrote:movewindow.c fails. I create a subwindow with 's' but 't' (move subwindow) displays "error" in the bottom left corner of the screen and the sub-window is not moved. I am not convinced that something is not wrong with mvwin - unless I am really messing up. The help displayed with '?' seems pretty straightforward.


hmm - for that, I'll have to check (perhaps tomorrow). In a quick check, it didn't look right,
but it's been a while since I did much with that.

Argh. The short answer is that I started to rewrite this feature as part of some comparisons
I was making (against IRIX64 - seems that Solaris preferred to dump core), but didn't finish it
(long story).

So I'll work on this feature, and presumably will have a good version later this week.
Thomas Dickey
Level 1
Level 1
 
Posts: 11
Joined: Wed Jul 14, 2010 6:27 pm

Re: CURSES and TERM on Mint

Postby Thomas Dickey on Sat Jan 14, 2012 9:36 pm

Thomas Dickey wrote:
celem wrote:So I'll work on this feature, and presumably will have a good version later this week.


I made a newer version, which seems to handle the mvwin's for subwindows as I intended.
There is still some work (for later) regarding derwin's in that program, but the current
version should do what you're asking about here.
Thomas Dickey
Level 1
Level 1
 
Posts: 11
Joined: Wed Jul 14, 2010 6:27 pm

Re: CURSES and TERM on Mint

Postby celem on Mon Jan 16, 2012 12:40 am

Thomas Dickey: where is the new code? The code at http://invisible-island.net/ncurses/ncurses-examples.html appears to be the same as before and behaves the same.
celem
Level 2
Level 2
 
Posts: 91
Joined: Mon Aug 18, 2008 10:39 pm
Location: North Carolina

Re: CURSES and TERM on Mint

Postby Thomas Dickey on Tue Jan 17, 2012 6:38 am

celem wrote:Thomas Dickey: where is the new code? The code at http://invisible-island.net/ncurses/ncurses-examples.html appears to be the same as before and behaves the same.

That's the right url. I modified the movewindow.c example in more than one way (seemed to work...).

As a quick check, the "?" screen will show a "D" option rather than "t". The 'm', 'M' and 'D' commands allow you to move the windows "continuously". The "M" command moves the subwindows with the current window.

One aspect of curses subwindows that might not be apparent is that they share space with the parent window, so "moving" the subwindows leaves the parent window with the original data (and moving them independently shows this). Before the changes in 2006 that I referred to, mvwin attempted to move its subwindows, but it turned out that made it incompatible and (for some uses) inconsistent.
Thomas Dickey
Level 1
Level 1
 
Posts: 11
Joined: Wed Jul 14, 2010 6:27 pm

Re: CURSES and TERM on Mint

Postby celem on Fri Jan 27, 2012 11:37 pm

Possibly the action is as intended, but 'M' moves the sub-window and 'm' copies the sub-window, rather than moves it. See the attachment - I drew a sub-window than used 'm' to move to another location. It just copied it. Also I see no upper-case 'D' command. The folder that downloads from is named 'ncurses-examples-20110404', which seems wrong for you to made a modification in January, unless you kept the folder name. I downloaded from http://invisible-island.net/ncurses/ncurses-examples.html
Attachments
Screenshot-1.png
No uppercase D command
Screenshot-1.png (62.52 KiB) Viewed 1438 times
Screenshot.png
m copies rather than moves
Screenshot.png (44.99 KiB) Viewed 1438 times
celem
Level 2
Level 2
 
Posts: 91
Joined: Mon Aug 18, 2008 10:39 pm
Location: North Carolina

Re: CURSES and TERM on Mint

Postby Thomas Dickey on Mon Jan 30, 2012 7:38 pm

celem wrote:Possibly the action is as intended, but 'M' moves the sub-window and 'm' copies the sub-window, rather than moves it. See the attachment - I drew a sub-window than used 'm' to move to another location. It just copied it. Also I see no upper-case 'D' command. The folder that downloads from is named 'ncurses-examples-20110404', which seems wrong for you to made a modification in January, unless you kept the folder name. I downloaded from http://invisible-island.net/ncurses/ncurses-examples.html


That folder is for the most recent stable release of ncurses (5.9). To see the current code, you can look in ftp://invisible-island.net/ncurses-examples/current/ For things that have a reasonable amount of demand, I add a "current" link from the webpage. For this particular package, I do not recall an earlier discussion (most packagers go straight to ftp).

Even with current code, it may not behave precisely as you would expect. When I move a subwindow, the parent window (because they share storage) displays the contents of the subwindow in the cells which are shared. I verified the behavior by comparing it against other implementations (such as Solaris) to see that the test program behaves the same with different implementations. With ncurses, I do get a little more (I can use the mouse), but the functionality that is common to both should behave the same.

Just to simplify things, I'll update the ncurses-examples page to provide the release- and current-links.
Attachments
help.png
help-screen for "movewindow"
help.png (11.16 KiB) Viewed 1401 times
Thomas Dickey
Level 1
Level 1
 
Posts: 11
Joined: Wed Jul 14, 2010 6:27 pm

Linux Mint is funded by ads and donations.
 
Next

Return to Software & Applications

Who is online

Users browsing this forum: bishop3337, lmmx, Majestic-12 [Bot], Pierre and 12 guests