Display timer same time as input
Hi all,
I want to display a timer the same time I am accepting input in a
curses(3) program. I am using alarm(3) as my timer. The problem is that
the timer is not updated when I am accepting input; it is updated after I
press a key. Here is the code:
Code:
#include <ctype.h>
#include <curses.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
static char _hour = 0, _min = 0, _sec = 0;
void disp_timer()
{
char sprintf_buf[10];
sprintf(sprintf_buf, "%02i:%02i:%02i", _hour, _min, _sec);
mvaddstr(3, 3, sprintf_buf);
}
void alarm_handler(int sig)
{
_sec++;
if(_sec == 60)
{
_sec = 0;
_min++;
if(_min == 60)
{
_min = 0;
_hour++;
}
}
disp_timer();
signal(SIGALRM, alarm_handler); /* watch on alarm again */
alarm(1);
}
void finish(int sig)
{
endwin();
exit(0);
}
int main()
{
int i;
char again, ch;
char sprintf_buf[15];
initscr();
noecho();
cbreak();
disp_timer();
signal(SIGINT, finish); /* ^C exits and cleans up program */
signal(SIGALRM, alarm_handler);
alarm(1);
again = 1;
do
{
for(i = 0; i < 10; i++)
{
clear();
disp_timer();
sprintf(sprintf_buf, "Question %i: ", i);
mvaddstr(0, 0, sprintf_buf);
getch();
sprintf(sprintf_buf, "Answer: %i", i);
mvaddstr(1, 0, sprintf_buf);
getch();
}
clear();
disp_timer();
mvaddstr(0, 0, "Again (y/n)? ");
ch = getch();
if(toupper(ch) == 'N')
again = 0;
} while(again);
finish(0);
return 0;
}
Any ideas?
Thanks,
Yasir