Diff for /np2/keystat.c between versions 1.2 and 1.10

version 1.2, 2004/02/18 18:29:29 version 1.10, 2004/03/30 20:01:29
Line 1 Line 1
 #include        "compiler.h"  #include        "compiler.h"
   #include        "dosio.h"
   #include        "textfile.h"
 #include        "pccore.h"  #include        "pccore.h"
 #include        "iocore.h"  #include        "iocore.h"
 #include        "keystat.h"  #include        "keystat.h"
Line 6 Line 8
   
   
 typedef struct {  typedef struct {
         UINT8   refer[0x80];          UINT8   ref[0x80];
         UINT8   joysync;          UINT8   extkey;
         UINT8   joylast;  
         UINT8   mouselast;          UINT8   mouselast;
         UINT8   padding;          UINT8   padding;
         UINT8   d_up;          UINT8   d_up;
Line 17  typedef struct { Line 18  typedef struct {
         UINT8   d_rt;          UINT8   d_rt;
 } KEYSTAT;  } KEYSTAT;
   
   
                   NKEYTBL         nkeytbl;
                   KEYCTRL         keyctrl;
 static  KEYSTAT         keystat;  static  KEYSTAT         keystat;
   
   
 // ----  void keystat_initialize(void) {
   
 void keystat_reset(void) {          char    path[MAX_PATH];
   
         ZeroMemory(keystat.refer, sizeof(keystat.refer));          ZeroMemory(&keystat, sizeof(keystat));
           FillMemory(keystat.ref, sizeof(keystat.ref), NKEYREF_NC);
           keystat_tblreset();
           getbiospath(path, "key.txt", sizeof(path));
           keystat_tblload(path);
 }  }
   
 void keystat_senddata(REG8 data) {  void keystat_tblreset(void) {
   
           UINT    i;
   
           ZeroMemory(&nkeytbl, sizeof(nkeytbl));
           for (i=0; i<0x80; i++) {
                   nkeytbl.key[i].keys = 1;
                   nkeytbl.key[i].key[0] = (UINT8)i;
           }
           for (i=0; i<0x10; i++) {
                   nkeytbl.key[i+0x80].keys = 1;
                   nkeytbl.key[i+0x80].key[0] = (UINT8)(i + 0xf0);
           }
   }
   
   void keystat_tblset(REG8 ref, const UINT8 *key, UINT cnt) {
   
           NKEYM   *nkey;
   
           if ((ref >= NKEY_USER) && (ref < (NKEY_USER + NKEY_USERKEYS))) {
                   nkey = (NKEYM *)(nkeytbl.user + (ref - NKEY_USER));
                   cnt = min(cnt, 15);
           }
           else if (ref < NKEY_SYSTEM) {
                   nkey = (NKEYM *)(nkeytbl.key + ref);
                   cnt = min(cnt, 3);
           }
           else {
                   return;
           }
           nkey->keys = (UINT8)cnt;
           if (cnt) {
                   CopyMemory(nkey->key, key, cnt);
           }
   }
   
   
   // ---- config...
   
   static const char str_userkey1[] = "userkey1";
   static const char str_userkey2[] = "userkey2";
   
         REG8            key;  static REG8 searchkeynum(const char *str, BOOL user) {
         BOOL            keynochange;  
 const _NKEYM    *user;  const KEYNAME   *n;
         UINT            i;  const KEYNAME   *nterm;
   
         key = data & 0x7f;          n = keyname;
         keynochange = FALSE;          nterm = keyname + (sizeof(keyname) / sizeof(KEYNAME));
           while(n < nterm) {
         // CTRL:カナ 0x71,0x72 bit7==0でトグル処理 (標準処理)                  if (!milstr_cmp(n->str, str)) {
         if ((key == 0x71) || (key == 0x72)) {                          return(n->num);
                 if (data & 0x80) {                  }
                         return;                  n++;
                 }          }
                 data = key | (keystat.refer[key] & 0x80);          if (user) {
                 keystat.refer[key] ^= 0x80;                  if (!milstr_cmp(str_userkey1, str)) {
         }                          return(NKEY_USER + 0);
         else if ((key == 0x76) || (key == 0x77)) {              // user key                  }
                 user = np2cfg.userkey + (key - 0x76);                  if (!milstr_cmp(str_userkey2, str)) {
                 for (i=0; i<user->keys; i++) {                          return(NKEY_USER + 1);
                         key = user->key[i] & 0x7f;                  }
                         if (!((keystat.refer[key] ^ data) & 0x80)) {          }
                                 keystat.refer[key] ^= 0x80;          return(0xff);
                                 keyboard_send((REG8)(key | (data & 0x80)));  }
   
   void keystat_tblload(const char *filename) {
   
           TEXTFILEH       tfh;
           char            work[256];
           char            *p;
           char            *q;
           char            *r;
           UINT8           ref;
           UINT8           key[15];
           UINT            cnt;
   
           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 = milstr_chr(p, ' ');
                   if (r != NULL) {
                           *r = '\0';
                   }
                   ref = searchkeynum(p, TRUE);
                   if (ref == 0xff) {
                           continue;
                   }
                   cnt = 0;
                   while((q) && (cnt < 16)) {
                           p = milstr_nextword(q);
                           q = milstr_chr(p, ' ');
                           if (q != NULL) {
                                   *q++ = '\0';
                           }
                           key[cnt] = searchkeynum(p, FALSE);
                           if (key[cnt] != 0xff) {
                                   cnt++;
                         }                          }
                 }                  }
                 return;                  keystat_tblset(ref, key, cnt);
           }
           textfile_close(tfh);
   
   kstbl_err:
           return;
   }
   
   
   // ----
   
   void keystat_ctrlreset(void) {
   
           ZeroMemory(&keyctrl, sizeof(keyctrl));
   }
   
   void keystat_ctrlsend(REG8 dat) {
   
   //      return;                                 // まだテスト
   
           if (!keyctrl.reqparam) {
                   keyctrl.mode = dat;
                   switch(dat) {
   #if defined(SUPPORT_PC9801_119)
                           case 0x95:
   #endif
                           case 0x9c:
                           case 0x9d:
                                   keyctrl.reqparam = 1;
                                   keyboard_ctrl(0xfa);
                                   break;
   
   #if defined(SUPPORT_PC9801_119)
                           case 0x96:
                                   keyboard_ctrl(0xfa);
                                   keyboard_ctrl(0xa0);
                                   keyboard_ctrl(0x83);
                                   break;
   #endif
   
                           case 0x9f:
                                   keyboard_ctrl(0xfa);
                                   keyboard_ctrl(0xa0);
                                   keyboard_ctrl(0x80);
                                   break;
   
                           default:
                                   keyboard_ctrl(0xfc);
                                   break;
                   }
         }          }
         else {          else {
                 if ((np2cfg.XSHIFT) &&                  switch(keyctrl.mode) {
                         (((key == 0x70) && (np2cfg.XSHIFT & 1)) ||  #if defined(SUPPORT_PC9801_119)
                         ((key == 0x74) && (np2cfg.XSHIFT & 2)) ||                          case 0x95:
                         ((key == 0x73) && (np2cfg.XSHIFT & 4)))) {                                  keyctrl.kbdtype = dat;
                         if (data & 0x80) {                                  break;
                                 return;  #endif
                           case 0x9c:
                                   keyboard_ctrl(0xfa);
                                   break;
                   }
                   keyctrl.reqparam = 0;
           }
   }
   
   
   // ----
   
   void keystat_down(const UINT8 *key, REG8 keys, REG8 ref) {
   
           UINT8   keydata;
           UINT8   keycode;
           REG8    data;
   
           while(keys--) {
                   keydata = *key++;
                   keycode = (keydata & 0x7f);
                   if (keycode < 0x70) {
                           if ((keystat.ref[keycode] == NKEYREF_NC) ||
                                   (!(kbexflag[keycode] & KBEX_NONREP))) {
                                   keyboard_send(keycode);
                         }                          }
                         data = key | (keystat.refer[key] & 0x80);                          keystat.ref[keycode] = ref;
                         keystat.refer[key] ^= 0x80;  
                 }                  }
                 else {                  else {
                         // CTRL:カナ 0x79,0x7a bit7をそのまま通知  #if defined(SUPPORT_PC9801_119)
                         // (ハードウェアでメカニカル処理してる場合)                          if ((keyctrl.kbdtype != 0x03) && (keycode >= 0x75)) {
                         if ((key == 0x79) || (key == 0x7a)) {                                  continue;
                                 key -= 0x08;                          }
                                 data -= 0x08;  #else
                         }                          if (keycode >= 0x75) {
                         if (!((keystat.refer[key] ^ data) & 0x80)) {                                  continue;
                                 keystat.refer[key] ^= 0x80;                          }
                         }  #endif
                         else {                          if ((np2cfg.XSHIFT) &&
                                 keynochange = TRUE;                                  (((keycode == 0x70) && (np2cfg.XSHIFT & 1)) ||
                                 if (kbexflag[key] & 2) {                        // キーリピート無し                                  ((keycode == 0x74) && (np2cfg.XSHIFT & 2)) ||
                                         return;                                  ((keycode == 0x73) && (np2cfg.XSHIFT & 4)))) {
                                   keydata |= 0x80;
                           }
                           if (!(keydata & 0x80)) {                        // シフト
                                   if (keystat.ref[keycode] == NKEYREF_NC) {
                                           keystat.ref[keycode] = ref;
                                           keyboard_send(keycode);
                                 }                                  }
                         }                          }
                           else {                                                          // シフトメカニカル処理
                                   if (keystat.ref[keycode] == NKEYREF_NC) {
                                           keystat.ref[keycode] = ref;
                                           data = keycode;
                                   }
                                   else {
                                           keystat.ref[keycode] = NKEYREF_NC;
                                           data = (REG8)(keycode + 0x80);
                                   }
                                   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   keydata;
           UINT8   keycode;
   
           while(keys--) {
                   keydata = *key++;
                   keycode = (keydata & 0x7f);
                   if (keycode < 0x70) {
                           if (keystat.ref[keycode] == ref) {
                                   keystat.ref[keycode] = NKEYREF_NC;
                                   keyboard_send((REG8)(keycode + 0x80));
                           }
                   }
                   else {
   #if defined(SUPPORT_PC9801_119)
                           if ((keyctrl.kbdtype != 0x03) && (keycode >= 0x75)) {
                                   continue;
                           }
   #else
                           if (keycode >= 0x75) {
                                   continue;
                           }
   #endif
                           if ((np2cfg.XSHIFT) &&
                                   (((keycode == 0x70) && (np2cfg.XSHIFT & 1)) ||
                                   ((keycode == 0x74) && (np2cfg.XSHIFT & 2)) ||
                                   ((keycode == 0x73) && (np2cfg.XSHIFT & 4)))) {
                                   keydata |= 0x80;
                           }
                           if (!(keydata & 0x80)) {                        // シフト
                                   if (keystat.ref[keycode] != NKEYREF_NC) {
                                           keystat.ref[keycode] = NKEYREF_NC;
                                           keyboard_send((REG8)(keycode + 0x80));
                                   }
                         }                          }
                         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<0x80; i++) {
         if ((key != 0x76) && (key != 0x77)) {                  if (keystat.ref[i] != NKEYREF_NC) {
                 if (keystat.refer[key] & 0x80) {                          keyboard_send(i);
                         keystat.refer[key] &= ~0x80;  
                         keyboard_send((REG8)(key | 0x80));  
                 }                  }
         }          }
         else {  }
                 user = np2cfg.userkey + (key - 0x76);  
                 for (i=0; i<user->keys; i++) {  
                         key = user->key[i] & 0x7f;  // ----
                         if (keystat.refer[key] & 0x80) {  
                                 keystat.refer[key] &= ~0x80;  void keystat_keydown(REG8 ref) {
                                 keyboard_send((REG8)(key | 0x80));  
           UINT8   shift;
   const NKEYM     *nkey;
   
           if ((ref >= NKEY_USER) && (ref < (NKEY_USER + NKEY_USERKEYS))) {
                   nkey = (NKEYM *)(nkeytbl.user + (ref - NKEY_USER));
                   keystat_down(nkey->key, nkey->keys, NKEYREF_USER);
           }
           else if (ref < NKEY_SYSTEM) {
                   if (np2cfg.KEY_MODE) {
                           shift = kbexflag[ref];
                           if (shift & KBEX_JOYKEY) {
                                   keystat.extkey |= (1 << (shift & 7));
                                   return;
                         }                          }
                 }                  }
                   nkey = (NKEYM *)(nkeytbl.key + ref);
                   keystat_down(nkey->key, nkey->keys, ref);
         }          }
 }  }
   
 void keystat_allrelease(void) {  void keystat_keyup(REG8 ref) {
   
         REG8    i;          UINT8   shift;
   const NKEYM     *nkey;
   
         for (i=0; i<0x80; i++) {          if ((ref >= NKEY_USER) && (ref < (NKEY_USER + NKEY_USERKEYS))) {
                 if (keystat.refer[i] & 0x80) {                  nkey = (NKEYM *)(nkeytbl.user + (ref - NKEY_USER));
                         keystat.refer[i] &= ~0x80;                  keystat_up(nkey->key, nkey->keys, NKEYREF_USER);
                         keyboard_send((REG8)(i | 0x80));          }
           else if (ref < NKEY_SYSTEM) {
                   if (np2cfg.KEY_MODE) {
                           shift = kbexflag[ref];
                           if (shift & KBEX_JOYKEY) {
                                   keystat.extkey &= ~(1 << (shift & 7));
                                   return;
                           }
                 }                  }
                   nkey = (NKEYM *)(nkeytbl.key + ref);
                   keystat_up(nkey->key, nkey->keys, ref);
         }          }
 }  }
   
 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<0x80; i++) {
                 key = joykeytable[i];                  if (keystat.ref[i] == ref) {
                 if (keystat.refer[key] & 0x80) {                          keystat.ref[i] = NKEYREF_NC;
                         keystat.refer[key] &= 0x7f;                          keyboard_send((REG8)(i + 0x80));
                         keyboard_send((REG8)(key | 0x80));  
                 }                  }
         }          }
 }  }
   
 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.refer[i]) {                  if (kbexflag[i] & KBEX_JOYKEY) {
                         keyboard_send((REG8)i);                          keystat_releaseref(i);
                 }                  }
         }          }
 }  }
   
   
 // ----  void keystat_releasekey(REG8 key) {
   
           key &= 0x7f;
           if (keystat.ref[key] != NKEYREF_NC) {
                   keystat.ref[key] = NKEYREF_NC;
                   keyboard_send((REG8)(key + 0x80));
           }
   }
   
   void keystat_allrelease(void) {
   
 void keystat_sync(void) {          REG8    i;
   
         keystat.joysync = 0;          for (i=0; i<0x80; i++) {
                   keystat_releasekey(i);
           }
 }  }
   
   
 REG8 keystat_getjoy(void) {  REG8 keystat_getjoy(void) {
   
         UINT8   flg;          return(~keystat.extkey);
 const BYTE      *p;  
         UINT8   bit;  
   
         if (!keystat.joysync) {  
                 keystat.joysync = 1;  
                 flg = 0xff;  
                 p = joykeytable;  
                 for (bit=0x20; bit; bit>>=1) {  
                         if ((keystat.refer[p[0]] & 0x80) ||  
                                 (keystat.refer[p[1]] & 0x80)) {  
                                 flg ^= bit;  
                         }  
                         p += 2;  
                 }  
                 keystat.joylast = flg;  
         }  
         return(keystat.joylast);  
 }  }
   
 REG8 keystat_getmouse(SINT16 *x, SINT16 *y) {  REG8 keystat_getmouse(SINT16 *x, SINT16 *y) {
Line 195  REG8 keystat_getmouse(SINT16 *x, SINT16  Line 422  REG8 keystat_getmouse(SINT16 *x, SINT16 
         SINT16  tmp;          SINT16  tmp;
         REG8    ret;          REG8    ret;
   
         btn = keystat_getjoy();          btn = ~keystat.extkey;
         acc = btn | keystat.mouselast;          acc = btn | keystat.mouselast;
         keystat.mouselast = (UINT8)btn;          keystat.mouselast = (UINT8)btn;
         tmp = 0;          tmp = 0;
Line 254  REG8 keystat_getmouse(SINT16 *x, SINT16  Line 481  REG8 keystat_getmouse(SINT16 *x, SINT16 
         return(ret);          return(ret);
 }  }
   
   
   // ----
   
   // キーコード変更
   
   static REG8 cnvnewcode(REG8 oldcode) {
   
           switch(oldcode) {
                   case 0x71:                              // 通常caps
                           return(0x81);
   
                   case 0x72:                              // 通常カナ
                           return(0x82);
   
                   case 0x79:                              // メカニカルロックcaps
                           return(0x71);
   
                   case 0x7a:                              // メカニカルロックcaps
                           return(0x72);
   
                   case 0x76:
                           return(0x90);           // NKEY_USER + 0
   
                   case 0x77:
                           return(0x91);           // NKEY_USER + 1
   
                   default:
                           return(oldcode);
           }
   }
   
   void keystat_senddata(REG8 data) {
   
           REG8    keycode;
   
           keycode = cnvnewcode((REG8)(data & 0x7f));
           if (!(data & 0x80)) {
                   keystat_keydown(keycode);
           }
           else {
                   keystat_keyup(keycode);
           }
   }
   
   void keystat_forcerelease(REG8 data) {
   
           REG8    keycode;
   
           keycode = cnvnewcode((REG8)(data & 0x7f));
           keystat_releasekey(keycode);
   }
   

Removed from v.1.2  
changed lines
  Added in v.1.10


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