home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / SVGALIB / SVGALIB1.TAR / svgalib / demos / keytest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-16  |  2.8 KB  |  123 lines

  1. /* Program to test the svgalib keyboard functions. */
  2.  
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <signal.h>
  6. #include <vga.h>
  7. #include <vgagl.h>
  8. #include <vgakeyboard.h>
  9.  
  10.  
  11.  
  12. static int newcolor() {
  13.     if (BYTESPERPIXEL == 1)
  14.         return random() % 15 + 1;
  15.     return gl_rgbcolor(random() & 255, random() & 255, random() & 255);
  16. }
  17.  
  18.  
  19. static void timeout() {
  20.     keyboard_close();
  21.     vga_setmode(TEXT);
  22.     exit(-1);
  23. }
  24.  
  25. void main() {
  26.     int vgamode, color, leftpressed;
  27.     int x, y;
  28.     vga_init();
  29.     vgamode = vga_getdefaultmode();
  30.     if (vgamode == -1)    
  31.         vgamode = G320x200x256;
  32.     
  33.     if (!vga_hasmode(vgamode)) {
  34.         printf("Mode not available.\n");
  35.         exit(-1);
  36.     }
  37.  
  38.     printf("\nWARNING: This program will set the keyboard to RAW mode.\n"
  39.            "The keyboard routines in svgalib have not been tested\n"
  40.            "very much. There may be no recovery if something goes\n"
  41.            "wrong.\n\n"
  42.            "Press ctrl-c now to bail out, enter to continue.\n"
  43.            "In the test itself, use 'q' or Escape to quit.\n"
  44.            "It will also terminate after 60 seconds.\n"
  45.            "Use any cursor keys to move, keypad 0 or enter to change color.\n");
  46.  
  47.     getchar();
  48.  
  49.     vga_setmode(vgamode);
  50.     gl_setcontextvga(vgamode);
  51.     gl_enableclipping();
  52.  
  53.     signal(SIGALRM, timeout);
  54.     signal(SIGSEGV, timeout);    /* Just to be sure. */
  55.  
  56.     /* This installs the default handler, which is good enough for most */
  57.     /* purposes. */
  58.     if (keyboard_init()) {
  59.         printf("Could not initialize keyboard.\n");
  60.         exit(-1);
  61.     }
  62.     /* Translate to 4 keypad cursor keys, and unify enter key. */
  63.     keyboard_translatekeys(TRANSLATE_CURSORKEYS | TRANSLATE_KEYPADENTER);
  64.     /* (TRANSLATE_DIAGONAL seems to give problems.) */
  65.  
  66.     alarm(60);    /* Terminate after 60 seconds for safety. */
  67.  
  68.     x = WIDTH / 2;
  69.     y = HEIGHT / 2;
  70.     color = newcolor();
  71.     leftpressed = 0;
  72.     for (;;) {
  73.         /* Draw moving box. */
  74.         gl_fillbox(x, y, 5, 5, color);
  75.  
  76.         /* Draw key status bar at top of screen. */
  77.         gl_putbox(0, 0, 128, 1, keyboard_getstate());
  78.  
  79.         /* Wait about 1/100th of a second. */
  80.         /* Note that use of this function makes things less */
  81.         /* smooth because of timer latency. */
  82.          usleep(10000);
  83.  
  84.         keyboard_update();
  85.  
  86.         /* Move. */
  87.         if (keyboard_keypressed(SCANCODE_CURSORLEFT))
  88.             x--;
  89.         if (keyboard_keypressed(SCANCODE_CURSORRIGHT))
  90.             x++;
  91.         if (keyboard_keypressed(SCANCODE_CURSORUP))
  92.             y--;
  93.         if (keyboard_keypressed(SCANCODE_CURSORDOWN))
  94.             y++;
  95.  
  96.         /* Boundary checks. */
  97.         if (x < 0) x = 0;
  98.         if (x >= WIDTH) x = WIDTH - 1;
  99.         if (y < 1) y = 1;
  100.         if (y >= HEIGHT) y = HEIGHT - 1;
  101.  
  102.         /* Check for color change. */
  103.         if (keyboard_keypressed(SCANCODE_KEYPAD0) ||
  104.         keyboard_keypressed(SCANCODE_ENTER)) {
  105.             if (!leftpressed) {
  106.                 color = newcolor();
  107.                 leftpressed = 1;
  108.             }
  109.         }
  110.         else
  111.             leftpressed = 0;
  112.  
  113.         if (keyboard_keypressed(SCANCODE_Q) ||
  114.         keyboard_keypressed(SCANCODE_ESCAPE))
  115.             break;
  116.     }
  117.  
  118.     keyboard_close();    /* Don't forget this! */
  119.  
  120.     vga_setmode(TEXT);
  121.     exit(0);
  122. }
  123.