This is a very ugly hack which supports just a single stdio function: fgetc(). Others could be added, btw. It works by setting a timer, an if the alarm goes off before a single character is read, a -2 value is returned instead (remember : -1 means EOF)
It won't work with any of the other curses's wgetXXX(), functions, which may call fgetc() (etc) directly. YMMV.
But, in the general case, I think you should investigate wgetch_events().
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <setjmp.h>
sigjmp_buf the_jump;
int my_fgetc(FILE *fp, unsigned timeout);
void sig_handler(int signr);
void sig_handler(int signr) {
switch (signr) {
case SIGALRM:
siglongjmp(the_jump,1);
break;
default:
break;
}
}
int my_fgetc(FILE *fp, unsigned timeout)
{
alarm( timeout);
switch (sigsetjmp(the_jump, -1)) {
case 0:
alarm(0);
return fgetc (fp);
case 1:
return -2;
default:
return -3;
}
}
int main()
{
int rc;
signal(SIGALRM, sig_handler);
rc = setvbuf(stdin, NULL, _IONBF, 0);
printf("setvbuf(_IONBF) = %d\n", rc);
while (1) {
rc = my_fgetc(stdin, 1);
printf("my_fgetc(NULL) = %d\n", rc);
}
return 0;
}