🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #2896

Re: Curses - need help

#2896
From: P E Smee
Date: Fri, 17 May 1991 10:48
56 lines
2620 bytes
In article <cs442a07.674280127@zaphod> taa65@ccvax.iastate.edu writes:
>Hi, I need help with curses, I dont know if this is the place to post 
>this article but I will try anyway.
>
>Working on a unix system, how do you get it to check if a keypress is
>waiting for you?  I have it in nonl() raw() noecho(), and I have a part
>of code that says:

This is most certainly not the right newsgroup (comp.lang.c would have
been better, or comp.unix.questions) but it'll probably save bandwidth
to answer you rather than getting into an argument about it.

Following is a chunk of code lifted from one of my curses
applications.  The basic idea of it is to refresh the screen if there
is no typeahead pending, but to delay the refresh until all user
commands have been processed if the user has gotten ahead of us.  In
the networked app I use it in, it can save blasting up menu screens
that the user is not going to look at anyway, so makes things look
faster. :-)  It uses 'select', see select(2).  I'm not sure if it is
the canonical or best method, but it works.


/* Don't remember whether you need both of the following .h's, or just one
   or the other.  Also, you may need a few more declarations.  Sorry... */

#include <signal.h>;
#include <sys/time.h>;

static void wcondrefresh (where)

WINDOW * where;

{
    int rfds;

    /* Note that I'm actually waiting 5000 micro-secs to see if the user
       is about to type something in.  (Gives network time to drain.)
       For a zero time wait, use timeout initialized to {0, 0}.  Passing
       a NULL timeval ptr means wait forever.  See select(2). */

    static struct timeval timeout = {0, 5000};

    rfds = 1 << fileno (stdin);
    if (select (1, &rfds, (int *) NULL, (int *) NULL, &timeout) <= 0) {
	(void) wrefresh (where);  /* No pending input, go ahead and refresh */
	}
    else {
	    /* There is pending input, or an error occurred during the
	       select() processing, do something else. */
	}
    return;
    }

-- 
Paul Smee, Computing Service, University of Bristol, Bristol BS8 1UD, UK
 P.Smee@bristol.ac.uk - ..!uunet!ukc!bsmail!p.smee - Tel +44 272 303132
Technical Details
Message-ID: <1991May17.104855.13533@gdr.bath.ac.uk>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!crdgw1!uunet!mcsun!ukc!dcl-cs!gdt!exspes
References: <cs442a07.674280127@zaphod>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!crdgw1!uunet!mcsun!ukc!dcl-cs!gdt!exspes
From: exspes@gdr.bath.ac.uk (P E Smee)
Newsgroups: comp.std.c
Subject: Re: Curses - need help
Message-ID: <1991May17.104855.13533@gdr.bath.ac.uk>
Date: 17 May 91 10:48:55 GMT
References: <cs442a07.674280127@zaphod>
Reply-To: P.Smee@bristol.ac.uk (Paul Smee)
Organization: University of Bristol c/o University of Bath
Lines: 56