--- np2/keystat.c 2004/02/18 18:29:29 1.2 +++ np2/keystat.c 2004/02/19 11:32:11 1.4 @@ -1,4 +1,5 @@ #include "compiler.h" +#include "textfile.h" #include "pccore.h" #include "iocore.h" #include "keystat.h" @@ -6,9 +7,8 @@ typedef struct { - UINT8 refer[0x80]; - UINT8 joysync; - UINT8 joylast; + UINT8 ref[0x80]; + UINT8 extkey; UINT8 mouselast; UINT8 padding; UINT8 d_up; @@ -17,175 +17,324 @@ typedef struct { UINT8 d_rt; } KEYSTAT; + + NKEYTBL nkeytbl; static KEYSTAT keystat; -// ---- +void keystat_initialize(void) { + + ZeroMemory(&keystat, sizeof(keystat)); + FillMemory(keystat.ref, sizeof(keystat.ref), NKEYREF_NC); + keystat_tblreset(); +} -void keystat_reset(void) { +void keystat_tblreset(void) { - ZeroMemory(keystat.refer, sizeof(keystat.refer)); + UINT i; + + 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; - BOOL keynochange; -const _NKEYM *user; - UINT i; - - key = data & 0x7f; - keynochange = FALSE; - - // CTRL:カナ 0x71,0x72 bit7==0でトグル処理 (標準処理) - if ((key == 0x71) || (key == 0x72)) { - if (data & 0x80) { - return; +// ---- config... + +static REG8 searchkeynum(const char *str) { + +const KEYNAME *n; +const KEYNAME *nterm; + + n = keyname; + nterm = keyname + (sizeof(keyname) / sizeof(KEYNAME)); + while(n < nterm) { + if (!milstr_cmp(n->str, str)) { + return(n->num); } - data = key | (keystat.refer[key] & 0x80); - keystat.refer[key] ^= 0x80; + n++; } - else if ((key == 0x76) || (key == 0x77)) { // user key - user = np2cfg.userkey + (key - 0x76); - for (i=0; ikeys; i++) { - key = user->key[i] & 0x7f; - if (!((keystat.refer[key] ^ data) & 0x80)) { - keystat.refer[key] ^= 0x80; - keyboard_send((REG8)(key | (data & 0x80))); + return(0xff); +} + +void keystat_tblload(const char *filename) { + + TEXTFILEH tfh; + 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 { - if ((np2cfg.XSHIFT) && - (((key == 0x70) && (np2cfg.XSHIFT & 1)) || - ((key == 0x74) && (np2cfg.XSHIFT & 2)) || - ((key == 0x73) && (np2cfg.XSHIFT & 4)))) { - if (data & 0x80) { - return; + textfile_close(tfh); + +kstbl_err: + 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.refer[key] & 0x80); - keystat.refer[key] ^= 0x80; + keystat.ref[keycode] = ref; } else { - // CTRL:カナ 0x79,0x7a bit7をそのまま通知 - // (ハードウェアでメカニカル処理してる場合) - if ((key == 0x79) || (key == 0x7a)) { - key -= 0x08; - data -= 0x08; + if ((np2cfg.XSHIFT) && + (((keycode == 0x70) && (np2cfg.XSHIFT & 1)) || + ((keycode == 0x74) && (np2cfg.XSHIFT & 2)) || + ((keycode == 0x73) && (np2cfg.XSHIFT & 4)))) { + keycode |= 8; } - if (!((keystat.refer[key] ^ data) & 0x80)) { - keystat.refer[key] ^= 0x80; + shift = keycode & 0x07; + 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 { - keynochange = TRUE; - if (kbexflag[key] & 2) { // キーリピート無し - return; + else { // シフトメカニカル処理 + if (keystat.ref[0x78 + shift] == NKEYREF_NC) { + keystat.ref[0x78 + shift] = ref; + 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 - return; +} + +void keystat_up(const UINT8 *key, REG8 keys, REG8 ref) { + + 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; -const _NKEYM *user; - UINT i; + REG8 i; - key = value & 0x7f; - if ((key != 0x76) && (key != 0x77)) { - if (keystat.refer[key] & 0x80) { - keystat.refer[key] &= ~0x80; - keyboard_send((REG8)(key | 0x80)); + for (i=0; i<0x70; i++) { + if (keystat.ref[i] != NKEYREF_NC) { + keyboard_send(i); } } + for (i=0; i<8; i++) { + if ((keystat.ref[0x70 + i] != NKEYREF_NC) || + (keystat.ref[0x78 + i] != NKEYREF_NC)) { + keyboard_send((REG8)(i + 0x70)); + } + } +} + + +// ---- + +void keystat_keydown(REG8 ref) { + + UINT8 keycode; + UINT8 shift; +const NKEYM3 *nkey3; +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 { - user = np2cfg.userkey + (key - 0x76); - for (i=0; ikeys; i++) { - key = user->key[i] & 0x7f; - if (keystat.refer[key] & 0x80) { - keystat.refer[key] &= ~0x80; - keyboard_send((REG8)(key | 0x80)); - } +#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_keyup(REG8 ref) { + + UINT8 keycode; + UINT8 shift; +const NKEYM3 *nkey3; +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_allrelease(void) { +void keystat_releaseref(REG8 ref) { REG8 i; - for (i=0; i<0x80; i++) { - if (keystat.refer[i] & 0x80) { - keystat.refer[i] &= ~0x80; - keyboard_send((REG8)(i | 0x80)); + for (i=0; i<0x70; i++) { + if (keystat.ref[i] == ref) { + keystat.ref[i] = NKEYREF_NC; + keyboard_send((REG8)(i + 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_resetjoykey(void) { - int i; - REG8 key; + REG8 i; - for (i=0; i<12; i++) { - key = joykeytable[i]; - if (keystat.refer[key] & 0x80) { - keystat.refer[key] &= 0x7f; - keyboard_send((REG8)(key | 0x80)); + keystat.extkey = 0; + for (i=0; i<0x80; i++) { + if (kbexflag[i] & KBEX_JOYKEY) { + keystat_releaseref(i); } } } -void keystat_resendstat(void) { - int i; +void keystat_releasekey(REG8 key) { - for (i=0; i<0x80; i++) { - if (keystat.refer[i]) { - keyboard_send((REG8)i); + key &= 0x7f; + if (key < 0x70) { + if (keystat.ref[key] != NKEYREF_NC) { + keystat.ref[key] = NKEYREF_NC; + keyboard_send((REG8)(key + 0x80)); + } + } + else { + key &= 0x07; + 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)); } } } +void keystat_allrelease(void) { -// ---- - -void keystat_sync(void) { + REG8 i; - keystat.joysync = 0; + for (i=0; i<0x78; i++) { + keystat_releasekey(i); + } } + REG8 keystat_getjoy(void) { - UINT8 flg; -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); + return(~keystat.extkey); } REG8 keystat_getmouse(SINT16 *x, SINT16 *y) { @@ -195,7 +344,7 @@ REG8 keystat_getmouse(SINT16 *x, SINT16 SINT16 tmp; REG8 ret; - btn = keystat_getjoy(); + btn = ~keystat.extkey; acc = btn | keystat.mouselast; keystat.mouselast = (UINT8)btn; tmp = 0; @@ -254,3 +403,23 @@ REG8 keystat_getmouse(SINT16 *x, SINT16 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); + } +} +