Next: , Previous: , Up: Top   [Contents][Index]


2 An example of using the GnuDOS library

This is a sample program that demonstrates how to use the GnuDOS library utilities:

#include "console/dialogs.h"
#include "console/screen.h"
#include "console/kbd.h"

void sighandler(int signo)
{
    //do what ever needs to be done here. The following line is just an example.
    fprintf(stderr, "SIGNAL %d received\n", signo);
}

int main(int argc, char *argv[])
{
  if(!catchSignals())
  {
    fprintf(stderr, "Error catching signals. Exiting.\n");
    exit(1);
  }
  if(!initTerminal())
  {
    fprintf(stderr, "Error initializing keyboard. Aborting.\n");
    exit(1);
  }
  
  getScreenSize();		//gets screen size
  clearScreenC(WHITE, BGBLACK);	//clear the screen
  //loads color arrays with default values
  loadDefaultColors();
  setScreenColors(FG_COLOR[COLOR_WINDOW], BG_COLOR[COLOR_WINDOW]);
    
  msgBox("This was an example", OK, INFO);
  drawBox(2, 2, SCREEN_H-2, SCREEN_W-2, " Example ", YES);
  locate(3, 3); printf("Hello GnuDOS!");
  locate(4, 3); printf("This is an example Window.");
  locate(5, 3); printf("Press ENTER to exit...");
  while(1)
  {
    if(getKey() == ENTER_KEY) break;
  }
  
  clearScreen();
  //very important to restore keyboard state to its
  //previous state before exiting
  restoreTerminal();
  exit(0);
}

Note that including the header file "dialogs.h" automatically includes both "screen.h" and "kbd.h", as the dialogs utility uses both of the other two.

And now, REMEMBER two things:

  1. a call to initTerminal() must be invoked before using the library
  2. a call restoreTerminal() must be done before exiting the program

For deatils about these functions please see See Kbd.

If you forget point (2), you will leave the user’s terminal in raw mode, which (under console) means he/she will not be able to do virtually anything (not even switching terminal by CTRL+ALT+F key!). The only way out is a reboot!. And I am talking about hard reboot by pressing the power button or restart key. Under X it is less worse, usually the user will need to close the xterm or kill the process. Still though, it is IMPERATIVE to call restoreTerminal() before exiting your program!.
To make sure no funny things happen (like your progrm crashing for whatever reason, or your admin killing it, to name a few) before you call restoreTerminal(), you better use the catchSignals() function of the See Dialogs, utility. Remember though that there are some signals that can’t be caught by your program, like the SIGSTOP and SIGKILL signals. This is why we used the catchSignals() function instead of the catchAllSignals() function.


Next: , Previous: , Up: Top   [Contents][Index]