home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / EXTRA-ST / APPLE-II / APPLE2-V.TAR / apple2 / keys.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-09  |  12.0 KB  |  480 lines

  1. #include <vga.h>
  2. #include <signal.h>
  3. #include <sys/time.h>
  4. #include <vgamouse.h>
  5. #include "keys.h"
  6. #include "colors.h"
  7.  
  8. #define     TIMER_DELAY        30000L
  9.  
  10. typedef enum { False, True } Tr;    /* --- Domain of Truth values
  11.                            (using the notation by
  12.                            John Allen -
  13.                         "The Anatomy of LISP") --- */
  14.  
  15. #define    K *1024
  16.  
  17. extern unsigned char apple_ii_64k[64 K];
  18.  
  19. int flash_cnt = 0;
  20.  
  21. unsigned char        lock_menu_mode = False;
  22.  
  23. #define MAX_KEY_BUF_LEN    1 K
  24.  
  25. #define MenuKeyBufEmptyP() (menu_key_buf_ptr == 0)
  26.  
  27. int             menu_key_buf[MAX_KEY_BUF_LEN];
  28. int            menu_key_buf_ptr = 0;
  29.  
  30. /* ----------------------------------------------------
  31.     Keymap. Mapping scancodes to Apple II+ US Keyboard
  32.    ---------------------------------------------------- */
  33.  
  34. int apple_ii_keymap_plain[128] =
  35.     { -1 , 27 , '1', '2', '3', '4', '5', '6',    /* 00-07   */
  36.       '7', '8', '9', '0', ':', '-', '-', 27 ,   /* 08-15   */
  37.       'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I',   /* 16-23   */
  38.       'O', 'P', -1 , -1 , 13 , -1 , 'A', 'S',   /* 24-31   */
  39.       'D', 'F', 'G', 'H', 'J', 'K', 'L', ';',   /* 32-39   */
  40.       8  , -1 , -1 , 21 , 'Z', 'X', 'C', 'V',   /* 40-47   */
  41.       'B', 'N', 'M', ',', '.', '/', -1 , -1 ,   /* 48-55   */
  42.       -1 , ' ', -1 , F1 , F2 , F3 , F4 , F5 ,   /* 56-63   */
  43.       F6 , F7 , F8 , F9 , F10, -1 , -1 , JUL,   /* 64-71   */
  44.       J_U, JUR, S_D, J_L, J_C, J_R, S_I, JDL,   /* 72-79   */
  45.       J_D, JDR, -1 , -1 , -1 , F11, F12, -1 ,   /* 80-87   */
  46.       -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,   /* 88-95   */
  47.       -1 , -1 , -1 , RST, -1 , -1 , -1 , -1 ,   /* 96-103  */
  48.       -1 , 8  , 21 , JB1, -1 , JB2, -1 , JB0,   /* 104-111 */
  49.       -1 , -1 , -1 , -1 , -1 , -1 , -1 , BOT,   /* 112-119 */
  50.       -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 };  /* 120-127 */
  51.  
  52. int apple_ii_keymap_ctrl[128] =
  53.     { -1 , 027, '1', '2', '3', '4', '5', '6',    /* 00-07   */
  54.       '7', '8', '9', '0', ':', '-', '-', 27 ,   /* 08-15   */
  55.       17 , 23 , 5  , 18 , 20 , 25 , 21 , 9  ,   /* 16-23   */
  56.       15 , 16 , -1 , -1 , 13 , -1 , 1  , 19 ,   /* 24-31   */
  57.       4  , 6  , 7  , 8  , 10 , 11 , 12 , ';',   /* 32-39   */
  58.       8  , -1 , -1 , 21 , 26 , 24 , 3  , 22 ,   /* 40-47   */
  59.       2  , 14 , 13 , ',', '.', '/', -1 , -1 ,   /* 48-55   */
  60.       -1 , ' ', -1 , F1 , F2 , F3 , F4 , F5 ,   /* 56-63   */
  61.       F6 , F7 , F8 , F9 , F10, -1 , -1 , JUL,   /* 64-71   */
  62.       J_U, JUR, S_D, J_L, J_C, J_R, S_I, JDL,   /* 72-79   */
  63.       J_D, JDR, -1 , -1 , -1 , F11, F12, -1 ,   /* 80-87   */
  64.       -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,   /* 88-95   */
  65.       -1 , -1 , -1 , RST, -1 , -1 , -1 , -1 ,   /* 96-103  */
  66.       -1 , 8  , 21 , JB1, -1 , JB2, -1 , JB0,   /* 104-111 */
  67.       -1 , -1 , -1 , -1 , -1 , -1 , -1 , BOT,   /* 112-119 */
  68.       -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 };  /* 120-127 */
  69.  
  70. int apple_ii_keymap_shifted[128] =
  71.     { -1 , 27 , '!', '"', '#', '$', '%', '&',    /* 00-07   */
  72.       39 , '(', ')', '0', '*', '=', '=', 27 ,   /* 08-15   */
  73.       'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I',   /* 16-23   */
  74.       'O', 'P', -1 , -1 , 13 , -1 , 'A', 'S',   /* 24-31   */
  75.       'D', 'F', 'G', 'H', 'J', 'K', 'L', '+',   /* 32-39   */
  76.       8  , -1 , -1 , 21 , 'Z', 'X', 'C', 'V',   /* 40-47   */
  77.       'B', 'N', 'M', '<', '>', '?', -1 , -1 ,   /* 48-55   */
  78.       -1 , ' ', -1 , F1 , F2 , F3 , F4 , F5 ,   /* 56-63   */
  79.       F6 , F7 , F8 , F9 , F10, -1 , -1 , JUL,   /* 64-71   */
  80.       J_U, JUR, S_D, J_L, J_C, J_R, S_I, JDL,   /* 72-79   */
  81.       J_D, JDR, -1 , -1 , -1 , F11, F12, -1 ,   /* 80-87   */
  82.       -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,   /* 88-95   */
  83.       -1 , -1 , -1 , RST, -1 , -1 , -1 , -1 ,   /* 96-103  */
  84.       -1 , 8  , 21 , JB1, -1 , JB2, -1 , JB0,   /* 104-111 */
  85.       -1 , -1 , -1 , -1 , -1 , -1 , -1 , BOT,   /* 112-119 */
  86.       -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 };  /* 120-127 */
  87.  
  88. extern unsigned char    vmode_page2;
  89. extern unsigned char    vmode_active;
  90.  
  91. extern unsigned char    exception_flag;
  92. extern unsigned char    exception_type;
  93.  
  94. extern unsigned short    apple_speed;
  95. unsigned short        max_speed = 0;
  96.  
  97. short        joy_step = 2;
  98. short        joy_x = 127;
  99. short        joy_y = 127;
  100. short        joy_center_x = 127;
  101. short        joy_center_y = 127;
  102. unsigned char    joy_button0 = 0;
  103. unsigned char    joy_button1 = 0;
  104. unsigned char    joy_button2 = 0;
  105. short       joy_mode = 0;
  106.  
  107. char        key_pressed[ 256 ];
  108.  
  109. void c_read_raw_keyboard();
  110.  
  111. void c_wait_menu_buf_empty()
  112. {
  113.    while (menu_key_buf_ptr > 0) { }
  114. }
  115.  
  116. void c_lock_menu_mode_on()
  117. {
  118.     lock_menu_mode = True;
  119. }
  120.  
  121. void c_lock_menu_mode_off()
  122. {
  123.     lock_menu_mode = False;
  124. }
  125.  
  126. void c_safety_quit()
  127. {
  128.     vga_setmode(TEXT);
  129.     keyboard_close();
  130.     exit( 0 );
  131. }
  132.  
  133. void c_keyboard_off()
  134. {
  135.     static struct itimerval tval, old;
  136.  
  137.     tval.it_interval.tv_sec = 0L;
  138.     tval.it_interval.tv_usec = 0L;
  139.     tval.it_value.tv_sec = 0L;
  140.     tval.it_value.tv_usec = 0L;
  141.  
  142.     setitimer( ITIMER_VIRTUAL, &tval, &old );
  143. }
  144.  
  145. void c_keyboard_on()
  146. {
  147.     static struct itimerval tval, old;
  148.  
  149.     tval.it_interval.tv_sec = 0L;
  150.     tval.it_interval.tv_usec = TIMER_DELAY;
  151.     tval.it_value.tv_sec = 0L;
  152.     tval.it_value.tv_usec = TIMER_DELAY;
  153.  
  154.     setitimer( ITIMER_VIRTUAL, &tval, &old );
  155.     keyboard_seteventhandler( c_read_raw_keyboard );
  156. }
  157.  
  158. void c_update_keyboard()
  159. {
  160.     static int    mouse_dx = 0, mouse_dy = 0, mouse_cnt = 0;
  161.     static int    mouse_buttons_used;
  162.  
  163.     signal( SIGVTALRM, c_update_keyboard );
  164.  
  165.     keyboard_update();
  166.  
  167.     if (!lock_menu_mode && !MenuKeyBufEmptyP())
  168.     {
  169.         int    key = menu_key_buf[--menu_key_buf_ptr];
  170.     menu_key_buf_ptr = 0;
  171.         c_lock_menu_mode_on();
  172.     switch (key)
  173.         {
  174.     case F1:
  175.         keyboard_close();
  176.         c_keyboard_off();
  177.         c_interface_select_diskette( 0 );
  178.             if (keyboard_init())
  179.             {
  180.                 printf("Error: Could not switch to RAW keyboard mode.\n");
  181.             exit(-1);
  182.             }
  183.         c_keyboard_on();
  184.         break;
  185.     case F2:
  186.         keyboard_close();
  187.         c_keyboard_off();
  188.         c_interface_select_diskette( 1 );
  189.             if (keyboard_init())
  190.             {
  191.                 printf("Error: Could not switch to RAW keyboard mode.\n");
  192.             exit(-1);
  193.             }
  194.         c_keyboard_on();
  195.         break;
  196.     case F4:
  197.         keyboard_close();
  198.         c_keyboard_off();
  199.         vga_getch();
  200.             c_setpage( vmode_active );
  201.             c_setscreen( vmode_page2 );
  202.             c_update_video_screen();
  203.             if (keyboard_init())
  204.             {
  205.                 printf("Error: Could not switch to RAW keyboard mode.\n");
  206.             exit(-1);
  207.             }
  208.         c_keyboard_on();
  209.         break;
  210.     case F5:
  211.         keyboard_close();
  212.         c_keyboard_off();
  213.         c_interface_keyboard_layout();
  214.             if (keyboard_init())
  215.             {
  216.                 printf("Error: Could not switch to RAW keyboard mode.\n");
  217.             exit(-1);
  218.             }
  219.         c_keyboard_on();
  220.         break;
  221.     case F8:
  222.         keyboard_close();
  223.         c_keyboard_off();
  224.         c_interface_words();
  225.             if (keyboard_init())
  226.             {
  227.                 printf("Error: Could not switch to RAW keyboard mode.\n");
  228.             exit(-1);
  229.             }
  230.         c_keyboard_on();
  231.         break;
  232.     case F9:
  233.         if (max_speed != 0)
  234.             apple_speed = max_speed, max_speed = 0;
  235.         else
  236.             max_speed = apple_speed, apple_speed = 1;
  237.         break;
  238.     case F10:
  239.         if (max_speed != 0)
  240.         apple_speed = max_speed, max_speed = 0;
  241.         keyboard_close();
  242.         c_keyboard_off();
  243.         c_interface_parameters();
  244.             if (keyboard_init())
  245.             {
  246.                 printf("Error: Could not switch to RAW keyboard mode.\n");
  247.             exit(-1);
  248.             }
  249.         c_keyboard_on();
  250.         break;
  251.     }
  252.         c_lock_menu_mode_off();
  253.     }
  254.  
  255. #ifdef MOUSE_EMULATION
  256.     mouse_update();
  257.  
  258.     mouse_dx = mouse_getx();
  259.     mouse_dy = mouse_gety();
  260.     if (mouse_dx != 0 || mouse_dy != 0)
  261.     {
  262.         mouse_setposition( 0, 0 );
  263.     mouse_cnt = 4;
  264.  
  265.     joy_x = joy_center_x + mouse_dx * 8;
  266.     if (joy_x < 0)
  267.         joy_x = 0;
  268.     else
  269.     if (joy_x > 255)
  270.         joy_x = 255;
  271.     joy_y = joy_center_y + mouse_dy * 8;
  272.     if (joy_y < 0)
  273.         joy_y = 0;
  274.     else
  275.     if (joy_y > 255)
  276.         joy_y = 255;
  277.     }
  278.  
  279.     if (--mouse_cnt == 0)
  280.     joy_x = joy_center_x, joy_y = joy_center_y;
  281.  
  282.     {
  283.     int mouse_buttons = mouse_getbutton();
  284.     if (mouse_buttons & MOUSE_LEFTBUTTON)
  285.         joy_button0 = 0x80, mouse_buttons_used |= MOUSE_LEFTBUTTON;
  286.     else
  287.     if ((mouse_buttons_used & MOUSE_LEFTBUTTON) && joy_button0 == 0x80)
  288.         joy_button0 = 0, mouse_buttons_used &= (~MOUSE_LEFTBUTTON);
  289.  
  290.     if (mouse_buttons & MOUSE_RIGHTBUTTON)
  291.         joy_button1 = 0x80, mouse_buttons_used |= MOUSE_RIGHTBUTTON;
  292.     else
  293.     if ((mouse_buttons_used & MOUSE_RIGHTBUTTON) && joy_button1 == 0x80)
  294.         joy_button1 = 0, mouse_buttons_used &= (~MOUSE_RIGHTBUTTON);
  295.  
  296.         if (mouse_buttons & MOUSE_MIDDLEBUTTON)
  297.         joy_button2 = 0x80, mouse_buttons_used |= MOUSE_MIDDLEBUTTON;
  298.     else
  299.     if ((mouse_buttons_used & MOUSE_MIDDLEBUTTON) && joy_button2 == 0x80)
  300.         joy_button2 = 0, mouse_buttons_used &= (~MOUSE_MIDDLEBUTTON);
  301.     }
  302.  
  303. #endif
  304.     flash_cnt += (int)TIMER_DELAY;
  305.     if (flash_cnt > 333333)
  306.     {
  307.     flash_cnt = 0;
  308.     vga_setpalette( COLOR_FLASHING_BLACK, 0, 0, 0 );
  309.     vga_setpalette( COLOR_FLASHING_WHITE, 63, 63, 63 );
  310.     }
  311.  
  312.     if (flash_cnt == (200000 / TIMER_DELAY) * TIMER_DELAY)
  313.     {
  314.     vga_setpalette( COLOR_FLASHING_BLACK, 63, 63, 63 );
  315.     vga_setpalette( COLOR_FLASHING_WHITE, 0, 0, 0 );    
  316.     }
  317.  
  318.     if (key_pressed[ SCODE_J_U ])
  319.     {
  320.         if (joy_mode == 0)
  321.         if (joy_y > joy_step)
  322.             joy_y -= joy_step;
  323.         else
  324.             joy_y = 0;
  325.     else
  326.         joy_y = 0;
  327.     }
  328.  
  329.     if (key_pressed[ SCODE_J_D ])
  330.     {
  331.         if (joy_mode == 0)
  332.         if (joy_y < 255 - joy_step)
  333.             joy_y += joy_step;
  334.         else
  335.             joy_y = 255;
  336.     else
  337.         joy_y = 255;
  338.     }
  339.  
  340.     if (key_pressed[ SCODE_J_L ])
  341.     {
  342.         if (joy_mode == 0)
  343.         if (joy_x > joy_step)
  344.             joy_x -= joy_step;
  345.         else
  346.             joy_x = 0;
  347.     else
  348.         joy_x = 0;
  349.     }
  350.  
  351.     if (key_pressed[ SCODE_J_R ])
  352.     {
  353.         if (joy_mode == 0)
  354.         if (joy_x < 255 - joy_step)
  355.             joy_x += joy_step;
  356.         else
  357.             joy_x = 255;
  358.     else
  359.         joy_x = 255;
  360.     }
  361.  
  362.     if (joy_mode == 2)
  363.         joy_x = joy_y = 256;
  364. }
  365.  
  366. void c_read_raw_keyboard( int scancode, int pressed )
  367. {
  368.     static char        file_name[1024];
  369.     int            *keymap = apple_ii_keymap_plain;
  370.  
  371.     if (key_pressed[ SCODE_L_CTRL ] ||
  372.     key_pressed[ SCODE_R_CTRL ])
  373.     keymap = apple_ii_keymap_ctrl;
  374.     else
  375.     if (key_pressed[ SCODE_L_SHIFT ] ||
  376.     key_pressed[ SCODE_R_SHIFT ])
  377.         keymap = apple_ii_keymap_shifted;
  378.  
  379.     if (pressed)
  380.     {
  381.     if (!key_pressed[ scancode ])
  382.     {
  383.         key_pressed[ scancode ] = True;
  384.         if ((unsigned int)(keymap[ scancode ]) < 255)
  385.         {
  386.             apple_ii_64k[ 0xC000 ] = keymap[ scancode ] | 0x80;
  387.         return;
  388.         }
  389.     }
  390.  
  391.     switch (keymap[ scancode ])
  392.     {
  393.     case RST:
  394.         exception_flag = 1;
  395.         exception_type = 0;
  396.         break;
  397.     case BOT:
  398.         exception_flag = 1;
  399.         exception_type = 1;
  400.         break;
  401.     case J_C:
  402.         joy_x = joy_center_x;
  403.         joy_y = joy_center_y;
  404.         break;
  405.     case JB0:
  406.         joy_button0 = 0x80;
  407.         break;
  408.     case JB1:
  409.         joy_button1 = 0x80;
  410.         break;
  411.     case JB2:
  412.         joy_button2 = 0x80;
  413.         break;
  414.     default:
  415.         break;
  416.     }
  417.  
  418.     if (keymap[ scancode ] >= F1 &&
  419.         keymap[ scancode ] <= F10 &&
  420.         menu_key_buf_ptr < MAX_KEY_BUF_LEN)
  421.             menu_key_buf[ menu_key_buf_ptr++ ] = keymap[ scancode ];
  422.  
  423.     }
  424.     else
  425.     {
  426.         key_pressed[ scancode ] = False;
  427.     switch (keymap[ scancode ])
  428.     {
  429.     case JB0:
  430.         joy_button0 = 0x00;
  431.         break;
  432.     case JB1:
  433.         joy_button1 = 0x00;
  434.         break;
  435.     case JB2:
  436.         joy_button2 = 0x00;
  437.         break;
  438.     }
  439.     }
  440. }
  441.  
  442. /* -------------------------------------------------------------------------
  443.     void c_initialize_keyboard()
  444.    ------------------------------------------------------------------------- */
  445.  
  446. void c_initialize_keyboard()
  447. {
  448.     if (keyboard_init())
  449.     {
  450.         printf("Error: Could not switch to RAW keyboard mode.\n");
  451.     exit(-1);
  452.     }
  453.  
  454.     signal( SIGVTALRM, c_update_keyboard );
  455.     keyboard_seteventhandler( c_read_raw_keyboard );
  456.     signal( SIGALRM, c_safety_quit );
  457.  
  458. /*    alarm( 120 ); */
  459.  
  460.     c_keyboard_on();
  461. }
  462.  
  463. void c_initialize_mouse()
  464. {
  465. #ifdef MOUSE_EMULATION
  466.     mouse_init( "/dev/mouse", MOUSE_MICROSOFT, 1200 );
  467.     mouse_setdefaulteventhandler();
  468.     mouse_setposition( 0, 0 );
  469.     mouse_setxrange( -32, 32 );
  470.     mouse_setyrange( -32, 32 );
  471. #endif
  472. }
  473.  
  474. void c_mouse_close()
  475. {
  476. #ifdef MOUSE_EMULATION
  477.     mouse_close();
  478. #endif
  479. }
  480.