Captain's Universe Home
Captain's Universe Home
Cosmic Ray Muon DetectorTeleGarden Pages
Time on MarsBryophyllum Plants
Jupiter Radio AstronomyAncient Pages
Salzburg Tourist GuideEarth Magnetometer
  H O M E     AJAX & MORE     LINUX & MORE     RTAI     XENOMAI     ADEOS IPIPE      
    JAVA & BROWSERS     *NIX     ELECTRONICS     REVIEWS     ARTEMIA     FAIRY SHRIMP      


Curses Example - ncurses Tutorial

Below is a simple ncurses example, which will draw a ncurses-screen, display some info in it (the current time and date). Press "q" to exit the example.

Compilation:
Use the Makefile to compile ncurses.c. Now run "ncurses".

Troubleshooting:
Once I had a very strange behaviour of curses:
"Everything is printed as expected (incl. the screen-box), but as soon as getch() in the while loop is reached, the screen-box disappears."

The solution was to add "refresh();" after "nodelay" in "screen_init". This does not apply here however, but just in case.


ncurses.c
#include <time.h>
#include <curses.h>

int current_getch;
int doloop = 1;
static WINDOW *mainwnd;
static WINDOW *screen;
WINDOW *my_win;

int now_sec, now_min, now_hour, now_day, now_wday, now_month, now_year;
time_t now;
struct tm *now_tm;
	
void screen_init(void) {
   mainwnd = initscr();
   noecho();
   cbreak();
   nodelay(mainwnd, TRUE);
   refresh(); // 1)
   wrefresh(mainwnd);
   screen = newwin(13, 27, 1, 1);
   box(screen, ACS_VLINE, ACS_HLINE);
}

static void update_display(void) {
   curs_set(0);
   mvwprintw(screen,1,1,"-------- HEADER --------");
   mvwprintw(screen,3,6,"TIME: %d:%d:%d", now_hour, now_min, now_sec);
   mvwprintw(screen,5,6,"DATE: %d-%d-%d", now_day, now_month, now_year);
   mvwprintw(screen,7,6,"PRESS q TO END");
   mvwprintw(screen,10,1,"-------- FOOTER --------");
   wrefresh(screen);
   refresh();
}

void screen_end(void) {
   endwin();
}

void maketime(void) {
	// Get the current date/time
	now = time (NULL);
	now_tm = localtime (&now);
   now_sec = now_tm->tm_sec;
	now_min = now_tm->tm_min;
	now_hour = now_tm->tm_hour;
	now_day = now_tm->tm_mday;
	now_wday = now_tm->tm_wday;
	now_month = now_tm->tm_mon + 1;
	now_year = now_tm->tm_year + 1900;
}

int main(void) {
   screen_init();
   while (doloop) {
      current_getch = getch();
      if (current_getch == 113) {
         doloop = 0;
      }
      maketime();
      update_display();
      sleep(1);
   }
   screen_end();
   printf("TEST ENDS\n");
   return 0;
}


Makefile
all:: ncurses

ncurses: ncurses.c
	gcc -o $@ $? -lncurses

clean::
	rm -f ncurses

.PHONY: clean


Last-Modified: Sat, 04 Feb 2006 16:03:18 GMT

Google
 
Web www.captain.at
go to top
© 1996-2010 . All rights reserved.
No reproduction, distribution, publishing or transmission of the copyrighted materials at this site is permitted. Policy
go to top