Diff for /np2/keystat.c between versions 1.1 and 1.4

version 1.1, 2004/02/18 03:24:48 version 1.4, 2004/02/19 11:32:11
Line 1 Line 1
 #include        "compiler.h"  #include        "compiler.h"
   #include        "textfile.h"
 #include        "pccore.h"  #include        "pccore.h"
 #include        "iocore.h"  #include        "iocore.h"
 #include        "keystat.h"  #include        "keystat.h"
   #include        "keystat.tbl"
   
   
 static  UINT8   keystat[0x80];  typedef struct {
           UINT8   ref[0x80];
           UINT8   extkey;
           UINT8   mouselast;
           UINT8   padding;
           UINT8   d_up;
           UINT8   d_dn;
           UINT8   d_lt;
           UINT8   d_rt;
   } KEYSTAT;
   
   
 static const UINT8 joykeytable[12] = {                  NKEYTBL         nkeytbl;
                                 0x2a,   0x34,  static  KEYSTAT         keystat;
                                 0x29,   0x1c,  
                                 0x3c,   0x48,  
                                 0x3b,   0x46,  
                                 0x3d,   0x4b,  
                                 0x3a,   0x43};  
   
 static const UINT8 kbexflag[0x80] = {  
                 //       ESC,  1,  2,  3,  4,  5,  6,  7         ; 00h  
                            0,   0,   0,   0,   0,   0,   0,   0,  
                 //        8,  9,  0,  −,  ^,  ¥,  BS, TAB         ; 08h  
                            0,   0,   0,   0,   0,   0,   0,   0,  
                 //        Q,  W,  E,  R,  T,  Y,  U,  I         ; 10h  
                            0,   0,   0,   0,   0,   0,   0,   0,  
                 //        O,  P,  @,  [, Ret,  A,  S,  D         ; 18h  
                            0,   0,   0,   0,   1,   0,   0,   0,  
                 //        F,  G,  H,  J,  K,  L,  ;,  :         ; 20h  
                            0,   0,   0,   0,   0,   0,   0,   0,  
                 //    ],  Z,  X,  C,  V,  B,  N,  M             ; 28h  
                            0,   1,   1,   0,   0,   0,   0,   0,  
                 //    ,,  .,  /,  _, SPC,XFER,RLUP,RLDN             ; 30h  
                            0,   0,   0,   0,   1,   0,   0,   0,  
                 //       INS, DEL,  ↑,  ←,  →,  ↓,HMCR,HELP         ; 38h  
                            2,   0,   1,   1,   1,   1,   0,   0,  
                 //      <−>,</>,<7>,<8>,<9>,<*>,<4>,<5>         ; 40h  
                            0,   0,   0,   1,   0,   0,   1,   0,  
                 //      <6>,<+>,<1>,<2>,<3>,<=>,<0>,<,>         ; 48h  
                            1,   0,   0,   1,   0,   0,   0,   0,  
                 //      <.>,NFER,vf.1,vf.2,vf.3,vf.4,vf.5,             ; 50h  
                            0,   0,   2,   2,   2,   2,   2,   0,  
                 //          ,    ,    ,    ,    ,    ,HOME,             ; 58h  
                            0,   0,   0,   0,   0,   0,   0,   0,  
                 //      STOP,COPY, f.1, f.2, f.3, f.4, f.5, f.6         ; 60h  
                            0,   0,   2,   2,   2,   2,   2,   2,  
                 //       f.7, f.8, f.9, f10,    ,    ,    ,             ; 68h  
                            2,   2,   2,   2,   0,   0,   0,   0,  
                 //       SFT,CAPS,KANA,GRPH,CTRL,    ,    ,             ; 70h  
                            2,   2,   2,   2,   2,   0,   0,   0,  
                 //          ,    ,    ,    ,    ,    ,    ,             ; 78h  
                            0,   0,   0,   0,   0,   0,   0,   0};  
   
   
 // ----  void keystat_initialize(void) {
   
           ZeroMemory(&keystat, sizeof(keystat));
           FillMemory(keystat.ref, sizeof(keystat.ref), NKEYREF_NC);
           keystat_tblreset();
   }
   
   void keystat_tblreset(void) {
   
 void keystat_reset(void) {          UINT    i;
   
         ZeroMemory(keystat, sizeof(keystat));          ZeroMemory(&nkeytbl, sizeof(nkeytbl));
           for (i=0; i<0x80; i++) {
                   nkeytbl.key[i].keys = 1;
                   nkeytbl.key[i].key[0] = (UINT8)i;
           }
 }  }
   
 void keystat_senddata(REG8 data) {  
   
         REG8            key;  // ---- config...
         BOOL            keynochange;  
 const _NKEYM    *user;  static REG8 searchkeynum(const char *str) {
         UINT            i;  
   const KEYNAME   *n;
         key = data & 0x7f;  const KEYNAME   *nterm;
         keynochange = FALSE;  
           n = keyname;
         // CTRL:カナ 0x71,0x72 bit7==0でトグル処理 (標準処理)          nterm = keyname + (sizeof(keyname) / sizeof(KEYNAME));
         if ((key == 0x71) || (key == 0x72)) {          while(n < nterm) {
                 if (data & 0x80) {                  if (!milstr_cmp(n->str, str)) {
                         return;                          return(n->num);
                 }                  }
                 data = key | (keystat[key] & 0x80);                  n++;
                 keystat[key] ^= 0x80;  
         }          }
         else if ((key == 0x76) || (key == 0x77)) {              // user key          return(0xff);
                 user = np2cfg.userkey + (key - 0x76);  }
                 for (i=0; i<user->keys; i++) {  
                         key = user->key[i] & 0x7f;  void keystat_tblload(const char *filename) {
                         if (!((keystat[key] ^ data) & 0x80)) {  
                                 keystat[key] ^= 0x80;          TEXTFILEH       tfh;
                                 keyboard_send((REG8)(key | (data & 0x80)));          char            work[128];
           char            *p;
           char            *q;
           char            *r;
           REG8            key;
   
           tfh = textfile_open(filename, 0x800);
           if (tfh == NULL) {
                   goto kstbl_err;
           }
           while(textfile_read(tfh, work, sizeof(work)) == SUCCESS) {
                   p = milstr_nextword(work);
                   q = milstr_chr(p, '\t');
                   if (q == NULL) {
                           q = milstr_chr(p, '=');
                   }
                   if (q == NULL) {
                           continue;
                   }
                   *q++ = '\0';
                   r = (char *)milstr_chr(p, ' ');
                   if (r != NULL) {
                           *r = '\0';
                   }
                   key = searchkeynum(p);
   
                   while(q) {
                           p = milstr_nextword(q);
                           q = milstr_chr(p, ' ');
                           if (q != NULL) {
                                   *q++ = '\0';
                         }                          }
                           key = searchkeynum(p);
   
                 }                  }
                 return;  
         }          }
         else {          textfile_close(tfh);
                 if ((np2cfg.XSHIFT) &&  
                         (((key == 0x70) && (np2cfg.XSHIFT & 1)) ||  kstbl_err:
                         ((key == 0x74) && (np2cfg.XSHIFT & 2)) ||          return;
                         ((key == 0x73) && (np2cfg.XSHIFT & 4)))) {  }
                         if (data & 0x80) {  
                                 return;  
   // ----
   
   void keystat_down(const UINT8 *key, REG8 keys, REG8 ref) {
   
           UINT8   keycode;
           UINT8   shift;
           REG8    data;
   
           while(keys--) {
                   keycode = *key++;
                   keycode = (keycode & 0x7f);
                   if (keycode < 0x70) {
                           if ((keystat.ref[keycode] == NKEYREF_NC) ||
                                   (!(kbexflag[keycode] & KBEX_NONREP))) {
                                   keyboard_send(keycode);
                         }                          }
                         data = key | (keystat[key] & 0x80);                          keystat.ref[keycode] = ref;
                         keystat[key] ^= 0x80;  
                 }                  }
                 else {                  else {
                         // CTRL:カナ 0x79,0x7a bit7をそのまま通知                          if ((np2cfg.XSHIFT) &&
                         // (ハードウェアでメカニカル処理してる場合)                                  (((keycode == 0x70) && (np2cfg.XSHIFT & 1)) ||
                         if ((key == 0x79) || (key == 0x7a)) {                                  ((keycode == 0x74) && (np2cfg.XSHIFT & 2)) ||
                                 key -= 0x08;                                  ((keycode == 0x73) && (np2cfg.XSHIFT & 4)))) {
                                 data -= 0x08;                                  keycode |= 8;
                         }                          }
                         if (!((keystat[key] ^ data) & 0x80)) {                          shift = keycode & 0x07;
                                 keystat[key] ^= 0x80;                          if (!(keycode & 8)) {                   // シフト
                                   if ((keystat.ref[0x70 + shift] == NKEYREF_NC) &&
                                           (keystat.ref[0x78 + shift] == NKEYREF_NC)) {
                                           keyboard_send((REG8)(0x70 + shift));
                                   }
                                   if (keystat.ref[0x70 + shift] > ref) {
                                           keystat.ref[0x70 + shift] = ref;
                                   }
                         }                          }
                         else {                          else {                                                          // シフトメカニカル処理
                                 keynochange = TRUE;                                  if (keystat.ref[0x78 + shift] == NKEYREF_NC) {
                                 if (kbexflag[key] & 2) {                        // キーリピート無し                                          keystat.ref[0x78 + shift] = ref;
                                         return;                                          data = (REG8)(0x70 + shift);
                                   }
                                   else {
                                           keystat.ref[0x78 + shift] = NKEYREF_NC;
                                           data = (REG8)(0xf0 + shift);
                                   }
                                   if (keystat.ref[0x70 + shift] == NKEYREF_NC) {
                                           keyboard_send(data);
                                 }                                  }
                         }                          }
                 }                  }
         }          }
         if ((!np2cfg.KEY_MODE) || (!(kbexflag[key] & 1))) {  }
                 if (keynochange) {  
                         if (data & 0x80) {                                              // ver0.30  void keystat_up(const UINT8 *key, REG8 keys, REG8 ref) {
                                 return;  
           UINT8   keycode;
           UINT8   shift;
   
           while(keys--) {
                   keycode = *key++;
                   keycode = (keycode & 0x7f);
                   if (keycode < 0x70) {
                           if (keystat.ref[keycode] == ref) {
                                   keystat.ref[keycode] = NKEYREF_NC;
                                   keyboard_send((REG8)(keycode + 0x80));
                           }
                   }
                   else {
                           if ((np2cfg.XSHIFT) &&
                                   (((keycode == 0x70) && (np2cfg.XSHIFT & 1)) ||
                                   ((keycode == 0x74) && (np2cfg.XSHIFT & 2)) ||
                                   ((keycode == 0x73) && (np2cfg.XSHIFT & 4)))) {
                                   keycode |= 8;
                           }
                           shift = keycode & 0x07;
                           if (!(keycode & 8)) {                   // シフト
                                   if (keystat.ref[0x70 + shift] == ref) {
                                           keystat.ref[0x70 + shift] = NKEYREF_NC;
                                           if (keystat.ref[0x78 + shift] == NKEYREF_NC) {
                                                   keyboard_send((REG8)(shift + 0xf0));
                                           }
                                   }
                         }                          }
                         keyboard_send((REG8)(data ^ 0x80));  
                 }                  }
                 keyboard_send(data);  
         }          }
 }  }
   
 void keystat_forcerelease(REG8 value) {  void keystat_resendstat(void) {
   
         REG8            key;          REG8    i;
 const _NKEYM    *user;  
         UINT            i;  
   
         key = value & 0x7f;          for (i=0; i<0x70; i++) {
         if ((key != 0x76) && (key != 0x77)) {                  if (keystat.ref[i] != NKEYREF_NC) {
                 if (keystat[key] & 0x80) {                          keyboard_send(i);
                         keystat[key] &= ~0x80;  
                         keyboard_send((REG8)(key | 0x80));  
                 }                  }
         }          }
         else {          for (i=0; i<8; i++) {
                 user = np2cfg.userkey + (key - 0x76);                  if ((keystat.ref[0x70 + i] != NKEYREF_NC) ||
                 for (i=0; i<user->keys; i++) {                          (keystat.ref[0x78 + i] != NKEYREF_NC)) {
                         key = user->key[i] & 0x7f;                          keyboard_send((REG8)(i + 0x70));
                         if (keystat[key] & 0x80) {  
                                 keystat[key] &= ~0x80;  
                                 keyboard_send((REG8)(key | 0x80));  
                         }  
                 }                  }
         }          }
 }  }
   
 void keystat_resetcopyhelp(void) {  
   
         REG8    i;  // ----
   
   void keystat_keydown(REG8 ref) {
   
         for (i=0x60; i<0x62; i++) {          UINT8           keycode;
                 if (keystat[i] & 0x80) {          UINT8           shift;
                         keystat[i] &= 0x7f;  const NKEYM3    *nkey3;
                         keyboard_send((REG8)(i | 0x80));  const NKEYM15   *nkey15;
   
           keycode = ref & 0x7f;
           if (np2cfg.KEY_MODE) {
                   shift = kbexflag[keycode];
                   if (shift & KBEX_JOYKEY) {
                           keystat.extkey |= (1 << (shift & 7));
                           return;
                 }                  }
         }          }
           if ((keycode != NKEY_USER1) && (keycode != NKEY_USER2)) {
                   nkey3 = nkeytbl.key + keycode;
                   keystat_down(nkey3->key, nkey3->keys, keycode);
           }
           else {
   #if 0
                   nkey15 = nkeytbl.user + (keycode - NKEY_USER1);
   #else
                   nkey15 = (NKEYM15 *)(np2cfg.userkey + (keycode - NKEY_USER1));
   #endif
                   keystat_down(nkey15->key, nkey15->keys, NKEYREF_USER);
           }
 }  }
   
 void keystat_allrelease(void) {  void keystat_keyup(REG8 ref) {
   
         REG8    i;  
   
         for (i=0; i<0x80; i++) {          UINT8           keycode;
                 if (keystat[i] & 0x80) {          UINT8           shift;
                         keystat[i] &= ~0x80;  const NKEYM3    *nkey3;
                         keyboard_send((REG8)(i | 0x80));  const NKEYM15   *nkey15;
   
           keycode = ref & 0x7f;
           if (np2cfg.KEY_MODE) {
                   shift = kbexflag[keycode];
                   if (shift & KBEX_JOYKEY) {
                           keystat.extkey &= ~(1 << (shift & 7));
                           return;
                 }                  }
         }          }
           if ((keycode != NKEY_USER1) && (keycode != NKEY_USER2)) {
                   nkey3 = nkeytbl.key + keycode;
                   keystat_up(nkey3->key, nkey3->keys, keycode);
           }
           else {
   #if 0
                   nkey15 = nkeytbl.user + (keycode - NKEY_USER1);
   #else
                   nkey15 = (NKEYM15 *)(np2cfg.userkey + (keycode - NKEY_USER1));
   #endif
                   keystat_up(nkey15->key, nkey15->keys, NKEYREF_USER);
           }
 }  }
   
 void keystat_resetjoykey(void) {  void keystat_releaseref(REG8 ref) {
   
         int             i;          REG8    i;
         REG8    key;  
   
         for (i=0; i<12; i++) {          for (i=0; i<0x70; i++) {
                 key = joykeytable[i];                  if (keystat.ref[i] == ref) {
                 if (keystat[key] & 0x80) {                          keystat.ref[i] = NKEYREF_NC;
                         keystat[key] &= 0x7f;                          keyboard_send((REG8)(i + 0x80));
                         keyboard_send((REG8)(key | 0x80));                  }
           }
           for (i=0; i<0x10; i++) {
                   if (keystat.ref[0x70 + i] == ref) {
                           keystat.ref[0x70 + i] = NKEYREF_NC;
                           if (keystat.ref[0x78 ^ i] == NKEYREF_NC) {
                                   keyboard_send((REG8)((i & 7) + 0xe0));
                           }
                 }                  }
         }          }
 }  }
   
 void keystat_resendstat(void) {  void keystat_resetjoykey(void) {
   
         int             i;          REG8    i;
   
           keystat.extkey = 0;
         for (i=0; i<0x80; i++) {          for (i=0; i<0x80; i++) {
                 if (keystat[i]) {                  if (kbexflag[i] & KBEX_JOYKEY) {
                         keyboard_send((REG8)i);                          keystat_releaseref(i);
                 }                  }
         }          }
 }  }
   
   
 // ----  void keystat_releasekey(REG8 key) {
   
 typedef struct {          key &= 0x7f;
         UINT8   joysync;          if (key < 0x70) {
         UINT8   joylast;                  if (keystat.ref[key] != NKEYREF_NC) {
         UINT8   mouselast;                          keystat.ref[key] = NKEYREF_NC;
         UINT8   padding;                          keyboard_send((REG8)(key + 0x80));
         UINT8   d_up;                  }
         UINT8   d_dn;          }
         UINT8   d_lt;          else {
         UINT8   d_rt;                  key &= 0x07;
 } KEYEXT;                  if ((keystat.ref[0x70 + key] != NKEYREF_NC) ||
                           (keystat.ref[0x78 + key] != NKEYREF_NC)) {
                           keystat.ref[0x70 + key] = NKEYREF_NC;
                           keystat.ref[0x78 + key] = NKEYREF_NC;
                           keyboard_send((REG8)(key + 0xf0));
                   }
           }
   }
   
 static  KEYEXT  keyext;  void keystat_allrelease(void) {
 static const UINT8 mousedelta[] = {1, 1, 1, 1,  
                                                                         2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 4};  
 #define MOUSESTEPMAX ((sizeof(mousedelta) / sizeof(UINT8)) - 1)  
   
 void keystat_sync(void) {          REG8    i;
   
         keyext.joysync = 0;          for (i=0; i<0x78; i++) {
                   keystat_releasekey(i);
           }
 }  }
   
   
 REG8 keystat_getjoy(void) {  REG8 keystat_getjoy(void) {
   
         BYTE    flg;          return(~keystat.extkey);
 const BYTE      *p;  
         BYTE    bit;  
   
         if (!keyext.joysync) {  
                 keyext.joysync = 1;  
                 flg = 0xff;  
                 p = joykeytable;  
                 for (bit=0x20; bit; bit>>=1) {  
                         if ((keystat[p[0]] & 0x80) || (keystat[p[1]] & 0x80)) {  
                                 flg ^= bit;  
                         }  
                         p += 2;  
                 }  
                 keyext.joylast = flg;  
         }  
         return(keyext.joylast);  
 }  }
   
 REG8 keystat_getmouse(SINT16 *x, SINT16 *y) {  REG8 keystat_getmouse(SINT16 *x, SINT16 *y) {
   
         REG8    btn;          REG8    btn;
         BYTE    acc;          UINT8   acc;
         SINT16  tmp;          SINT16  tmp;
         BYTE    ret;          REG8    ret;
   
         btn = keystat_getjoy();          btn = ~keystat.extkey;
         acc = btn | keyext.mouselast;          acc = btn | keystat.mouselast;
         keyext.mouselast = (UINT8)btn;          keystat.mouselast = (UINT8)btn;
         tmp = 0;          tmp = 0;
         if (!(btn & 1)) {          if (!(btn & 1)) {
                 tmp -= mousedelta[keyext.d_up];                  tmp -= mousedelta[keystat.d_up];
         }          }
         if (!(acc & 1)) {          if (!(acc & 1)) {
                 if (keyext.d_up < MOUSESTEPMAX) {                  if (keystat.d_up < MOUSESTEPMAX) {
                         keyext.d_up++;                          keystat.d_up++;
                 }                  }
         }          }
         else {          else {
                 keyext.d_up = 0;                  keystat.d_up = 0;
         }          }
         if (!(btn & 2)) {          if (!(btn & 2)) {
                 tmp += mousedelta[keyext.d_dn];                  tmp += mousedelta[keystat.d_dn];
         }          }
         if (!(acc & 2)) {          if (!(acc & 2)) {
                 if (keyext.d_dn < MOUSESTEPMAX) {                  if (keystat.d_dn < MOUSESTEPMAX) {
                         keyext.d_dn++;                          keystat.d_dn++;
                 }                  }
         }          }
         else {          else {
                 keyext.d_dn = 0;                  keystat.d_dn = 0;
         }          }
         *y += tmp;          *y += tmp;
   
         tmp = 0;          tmp = 0;
         if (!(btn & 4)) {          if (!(btn & 4)) {
                 tmp -= mousedelta[keyext.d_lt];                  tmp -= mousedelta[keystat.d_lt];
         }          }
         if (!(acc & 4)) {          if (!(acc & 4)) {
                 if (keyext.d_lt < MOUSESTEPMAX) {                  if (keystat.d_lt < MOUSESTEPMAX) {
                         keyext.d_lt++;                          keystat.d_lt++;
                 }                  }
         }          }
         else {          else {
                 keyext.d_lt = 0;                  keystat.d_lt = 0;
         }          }
         if (!(btn & 8)) {          if (!(btn & 8)) {
                 tmp += mousedelta[keyext.d_rt];                  tmp += mousedelta[keystat.d_rt];
         }          }
         if (!(acc & 8)) {          if (!(acc & 8)) {
                 if (keyext.d_rt < MOUSESTEPMAX) {                  if (keystat.d_rt < MOUSESTEPMAX) {
                         keyext.d_rt++;                          keystat.d_rt++;
                 }                  }
         }          }
         else {          else {
                 keyext.d_rt = 0;                  keystat.d_rt = 0;
         }          }
         *x += tmp;          *x += tmp;
   
Line 311  REG8 keystat_getmouse(SINT16 *x, SINT16  Line 403  REG8 keystat_getmouse(SINT16 *x, SINT16 
         return(ret);          return(ret);
 }  }
   
   
   // ----
   
   void keystat_senddata(REG8 data) {
   
           UINT8   keycode;
   
           keycode = data & 0x7f;
           if ((keycode == 0x71) || (keycode == 0x72) ||
                   (keycode == 0x79) || (keycode == 0x7a)) {
                   keycode ^= 8;
           }
           if (!(data & 0x80)) {
                   keystat_keydown(keycode);
           }
           else {
                   keystat_keyup(keycode);
           }
   }
   

Removed from v.1.1  
changed lines
  Added in v.1.4


RetroPC.NET-CVS <cvs@retropc.net>