Knowledge Base Nr: 00131 main.cpp - http://www.swe-kaiser.de

Downloads:

linux: template consolenprogramm non-blocking input mit curses

  
//build: g++ main.cpp -o myname -lncurses

#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <termios.h>
#include <signal.h>
#include <string.h>

#include <curses.h>


///////////globale definitionen//////////////////////////



//////////////////main/////////////////////////////////////////
//#define TRACE
#define TRACE printf

int main(int argc, char *argv[])
{
initscr();
cbreak();
fcntl(fileno(stdin), F_SETFL, fcntl(fileno(stdin), F_GETFL) | O_NONBLOCK);

printf("*** %s\r\n*** usage: %s [a] [b] [c]\r\n", argv[0], argv[0]);
TRACE("* args: %s", argv[0]);
for (int n=1; n<argc; n++)
TRACE(" [%s]", argv[n]);
TRACE("\r\n");

while (1)
{
char c = 0;
int r = fread(&c, 1, 1, stdin);
if (r>0)
{
switch (c)
{
case 'a': printf("a");
break;
case 'b': printf("b");
break;
case 'c': printf("c");
break;

default: printf("\r\nunknown command [%c]???\r\n", c);
}
}

//todo
TRACE(".");

fflush(stdout);
usleep(100 * 1000); //100ms
} //while(1)

endwin();

return EXIT_SUCCESS;
}