home *** CD-ROM | disk | FTP | other *** search
- /*
- * keypc.c: hterm keyboard decoder for IBM-PC, AX and J-3100
- *
- * Author: HIRANO Satoshi
- * (C) 1989 Halca Computer Science Laboratory TM
- * University of Tokyo
- *
- * 2.2 89/05/16 Halca.Hirano remove all of kana char; V2.2 distribution
- * 2.3 89/06/15 Halca.Hirano
- * 1 remove #else
- * 2.4 89/06/20 Halca.Hirano separate machine dependent
- * 2.5 89/07/17 Halca.Hirano add key table.
- * 2.6 89/07/20 Halca.Hirano save/load key table
- * ----- V2.3.-1.1 distribution ----
- * 2.7 89/08/07 Halca.Hirano add mouse
- * 2.8 89/09/14 Halca.Hirano fix mouse bug (cursor doesn't mode correctly.)
- * ---- V2.4.0 distribution ----
- * 2.9 89/11/10 Tominaga@Titech ported to J3100
- * ---- V2.4.2 distribution ----
- * 3.0 89/12/03 Halca.Hirano
- * add BS key watcher
- * ---- V2.5.1 distribution ----
- * 3.1 89/12/16 Tominaga@Titech extend META-keys
- * Now META-CTRL-A generates ESC ^A
- * 3.2 89/12/24 Halca.Hirano
- * fix key table loading bug. I made an attempt to WRITE APPLCursor
- * and APPLPad!!
- * 3.3 90/06/27 Zundoko@enterprise
- * generate '|' by SHIFT-\ key on J3100
- *
- *
- * NOTE: for AX users
- * Scan code from ATOK japanese front end processor is defined below.
- * xxyy xx < 0x81, xx is scan code, yy is ASCII code
- * 00xx xx >= 0x81, ((xx<<8)|nextchar) is shift JIS code
- * xx00 special key scan code (xx may be same as
- * shift JIS upper byte)
- *
- * NOTE: for J-3100 users
- * Key code from BIOS thru ATOK japanese front end processor is as follows:
- * xxyy if xx=0xf0, yy is a former byte of a shift-JIS character
- * if xx=0xf1, yy is a latter byte of a shift-JIS character
- * otherwise, xx is a scan code and yy is ASCII code
- *
- * $Header: keypc.cv 1.13 90/07/04 20:41:30 hirano Exp $
- */
-
-
- #include <stdio.h>
- #include <io.h>
- #include "option.h"
- #include "config.h"
- #include "hterm.h"
- #include "default.h"
- #include "keytabpc.h"
- #include "global.h"
-
- #define S_SHIFT(p) ((p) & 0x03) /* left/right SHIFT is pressed */
- #define S_CTRL(p) ((p) & 0x04) /* CTRL is pressed */
- #define S_META(p) ((p) & 0x08) /* ALT is pressed */
- #define S_NUMLOCK(p) ((p) & 0x20) /* NUMLOCK state */
- #define SCAN_ESC 0x011b /* scan code for ESC key */
- #define SCAN_BS 0x0e08 /* scan code for SHIFT+BS key */
- #define SCAN_CTRL_BS 0x0e7f /* scan code for CTRL+BS key */
- #define SCAN_ENTER 0x1c0d /* scan code for ENTER */
- #define SCAN_SPACE 0x3920 /* scan code for space */
- #define SCAN_N_HENKAN 0xa700 /* scan code for henkan */
- #define SCAN_C_HENKAN 0xa900 /* scan code for CTRL-henkan */
- #define SCAN_M_HENKAN 0xaa00 /* scan code for META-henkan */
- #define SCAN_N_MUHENKAN 0xab00 /* scan code for muhenkan */
- #define SCAN_C_MUHENKAN 0xad00 /* scan code for CTRL-muhenkan */
- #define SCAN_M_MUHENKAN 0xae00 /* scan code for META-muhenkan */
-
- #define MAX_KEYBUF (MAX_FUNKEY+3)
- static u_short keyBuf[MAX_KEYBUF]; /* key data buffer */
- static int keyBufInPtr;
- static int keyBufOutPtr;
- static int keyBufLength;
-
- void putBuf(unsigned short c);
- short putStrKeyBuf(char *s);
- void keyMode(void );
- void mouseSetHandler(int onOff);
- void mouseOnOff(int onOff);
- void mouseSetPosition(int x,int y);
- void far mouseEvent(int event,int x,int y);
-
- void keyInit()
- /* iniz keyboard mode */
- {
- clickFlag = DEFAULT_KEY_CLICK;
- applKeypad = DEFAULT_APP_KEYBOARD;
- applCursor = DEFAULT_CURSOR_KEY_MODE;
- newline = DEFAULT_NEWLINE_MODE;
- asckey = YES; /* always ascii keyboard */
- mouseSpeed = DEFAULT_MOUSE_SPEED;
- bsKeyCount = keyCount = 0;
- bsKeyRatio = DEFAULT_BS_KEY_RATIO;
- keyBufInPtr = keyBufOutPtr = keyBufLength = 0;
- keyReInit();
- }
-
- void keyReInit()
- {
- mouseInit();
- }
-
- void keySetup()
- /* setup keyboard mode */
- {}
-
- void keyEnd()
- {
- mouseEnd();
- }
-
- int checkKey()
- /*
- * return 0 if key buffer is empty
- */
- {
- if (checkBIOSKey())
- return(YES);
- /* check shift state */
- rg.h.ah = 2;
- int86(KEY_BIOS, &rg, &rg);
- if (rg.h.al & 0x0f) /* check right/left shift, ctrl, alt */
- return(YES);
- return(NO);
- }
-
- short keyin()
- /*
- * check keyboard status and return key if available
- * return:
- * -1 key is not available
- * 00xx ASCII, kana
- * xxyy hterm kanji code
- * xx00 special key code
- */
- {
- short c, c2;
- register u_char scan;
- u_char shiftState;
-
- /*
- * if keyBuf is not empty, return char from it.
- */
- if (keyBufLength > 0) {
- --keyBufLength;
- c = keyBuf[keyBufOutPtr];
- if (++keyBufOutPtr >= MAX_KEYBUF)
- keyBufOutPtr = 0;
- return(c);
- }
-
- #ifdef SOFT_FONT
- if (softFont && blinkCursor && (blinkTimer <= 0)) {
- blinkSoftCursor();
- }
- #endif /* SOFT_FONT */
-
- if ((c = inputKey()) == -1)
- return(-1);
-
- timerValue = timerLoadValue;
- keyCount++;
-
- /* get shift state */
- rg.h.ah = 2;
- int86(KEY_BIOS, &rg, &rg);
- shiftState = rg.h.al;
-
- if (clickFlag == YES)
- click();
-
- switch (c) { /* keys to be treated with care */
- case SCAN_ESC:
- if (S_SHIFT(shiftState))
- sendBreak(1); /* short break */
- else if (S_CTRL(shiftState))
- sendBreak(2); /* long break */
- else
- return(ESC);
- return(-1);
- case SCAN_BS:
- case SCAN_CTRL_BS:
- bsKeyCount++;
- if (bsDel == BS_DEL)
- return(DEL);
- return(BS);
- case SCAN_ENTER:
- return(CR);
- case SCAN_SPACE:
- case SCAN_N_HENKAN: /* AX henkan key */
- case SCAN_N_MUHENKAN: /* AX muhenkan key */
- if (S_META(shiftState)) { /* ALT */
- if (S_CTRL(shiftState)) {
- putBuf(0);
- } else {
- putBuf(SPACE);
- }
- return(ESC);
- } else if (S_CTRL(shiftState)) {
- return(0);
- }
- return(SPACE);
- case SCAN_C_HENKAN:
- case SCAN_C_MUHENKAN: /* ctrl space */
- return(0);
- case SCAN_M_HENKAN: /* alt space */
- case SCAN_M_MUHENKAN:
- putBuf(SPACE);
- return(ESC);
- case 0x372a: /* tenkey * key */
- if (applKeypad) /* application keypad */
- return(putStrKeyBuf(APPLPad[0]));
- else /* ten key */
- return(c & 0xff);
- #ifdef AX
- case 0x565c:
- case 0x567c:
- case 0x561c: /* AX \| key */
- return(c & 0xff);
- #endif /* AX */
- #ifdef J3100
- case 0x557c: /* J-3100 SHIFT-\ Zundoko's patch */
- case 0x555c: /* J-3100 \ key */
- case 0x551c: /* J-3100 CTRL-\ */
- if (S_META(shiftState)) {
- putBuf(c & 0xff);
- return(ESC);
- } else {
- return(c & 0xff);
- }
- case 0x1a1d: /* J-3100 CTRL-[ key */
- return(ESC); /* adjust to ESC */
- case 0x1b1c: /* J-3100 CTRL-] key */
- return(0x1d); /* adjust to ^] */
- #endif /* J3100 */
- }
-
- scan = c >> 8;
-
- #ifdef KANJI
- #ifndef J3100
- if (scan == 0) {
- if (isSJIS1(c)) { /*if kanji start */
- while ((c2 = inputKey()) == -1)
- ;
- return(SJIStoJIS(c, c2));
- } else if (scan == 0)
- return((u_short)c);
- }
- #else /* J3100 */
- if (scan == 0 || scan == 0xf0 || scan == 0xf1) {
- c &= 0xff; /* adjust kanji code from FEP */
- if (isSJIS1(c)) { /*if kanji start */
- while ((c2 = inputKey()) == -1)
- ;
- c2 &= 0xff; /* adjust this also */
- return(SJIStoJIS(c, c2));
- } else if (scan == 0) {
- return((u_short)c);
- }
- }
- #endif /* J3100 */
- #endif /* KANJI */
-
- if (S_META(shiftState)) { /* ALT pushed */
- if (S_CTRL(shiftState)) { /* ALT-CTRL */
- if (0x78 <= scan && scan <= 0x83) {
- /*
- * number key with META-CTRL
- */
- if ((c = metaCtrlTable[scan - 0x78]) != -1) {
- putBuf(c);
- return(ESC);
- } else {
- return(-1);
- }
- } else if (0x01 <= scan && scan <= 0x35) {
- /*
- * typewriter key with META-CTRL
- */
- if ((c = metaCtrlTable[scan]) != -1) {
- putBuf(c);
- return(ESC);
- } else {
- return(-1);
- }
- } else {
- /* no code assigned this key */
- return(-1);
- }
- } else if (S_SHIFT(shiftState)) { /* ALT-SHIFT */
- if (0x78 <= scan && scan <= 0x83) {
- /*
- * number key with META-SHIFT
- */
- if ((c = metaShiftTable[scan - 0x78]) != -1) {
- putBuf(c);
- return(ESC);
- } else {
- return(-1);
- }
- } else if (0x01 <= scan && scan <= 0x35) {
- /*
- * typewriter key with META-SHIFT
- */
- if ((c = metaShiftTable[scan]) != -1) {
- putBuf(c);
- return(ESC);
- } else {
- return(-1);
- }
- } else {
- /* no code is assigned to this key */
- return(-1);
- }
- } else { /* ALT only */
- if (0x78 <= scan && scan <= 0x83) {
- /*
- * number key with META
- */
- if ((c = metaTable[scan - 0x78]) != -1) {
- putBuf(c);
- return(ESC);
- } else {
- return(-1);
- }
- } else if (0x01 <= scan && scan <= 0x35) {
- /*
- * typewriter key with META
- */
- if ((c = metaTable[scan]) != -1) {
- putBuf(c);
- return(ESC);
- } else {
- return(-1);
- }
- } else if (SCAN_M_PF1 <= scan && scan <= SCAN_M_PF10) {
- /*
- * ALT PF key
- */
- return(METAFunkey[scan - SCAN_M_PF1]);
- } else {
- /* no META supported for this code */
- return(-1);
- }
- } /* with META, if "CTRL or SHIFT is pushed or not" */
- } else { /* no ALT */
- if (S_CTRL(shiftState)) { /* CTRL */
- if (SCAN_C_PF1 <= scan && scan <= SCAN_C_PF10) {
- /*
- * CTRL PF key
- */
- return(CTRLFunkey[scan - SCAN_C_PF1]);
- } else if (SCAN_C_PAD_BEGIN <= scan && scan <= SCAN_C_PAD_END) {
- /*
- * CTRL-pad/cursor key
- */
- c = CTRLCursor[scan-SCAN_C_PAD_BEGIN];
- return(c ? c : -1);
- } else if (0x01 <= scan && scan <= 0x35) {
- /*
- * typewrite key with CTRL
- */
- return(c & 0xff);
- } else {
- /*
- * no code
- */
- return(-1);
- }
- } else if (S_SHIFT(shiftState)) { /* SHIFT */
- if (SCAN_S_PF1 <= scan && scan <= SCAN_S_PF10) {
- /*
- * SHIFT PF key
- */
- return(putStrKeyBuf(SHIFTFunkey[scan - SCAN_S_PF1]));
- } else if (SCAN_PF1 <= scan && scan <= SCAN_PF10) {
- /*
- * normal PF key
- */
- return(putStrKeyBuf(NORMALFunkey[scan - SCAN_PF1]));
- } else if (SCAN_PAD_BEGIN <= scan && scan <= SCAN_PAD_END) {
- /*
- * normal pad key; non numlock (SHIFT should be ignored)
- */
- scan = scan - SCAN_PAD_BEGIN;
- if ((c & 0xff) && ((c & 0xff) != 0xe0)) { /* keypad is tenkey */
- if (applKeypad && mode == M_COMM) /* application keypad */
- return(putStrKeyBuf(APPLPad[scan+1 /* 1 means '*' key */]));
- else /* number key */
- return(c & 0xff);
- } else {
- if (mode != M_COMM) /* in setup or history editor */
- return(SETUPCursor[scan]);
- else if (applCursor) /* application cursor */
- return(putStrKeyBuf(APPLCursor[scan]));
- else /* normal cursor */
- return(putStrKeyBuf(NORMALCursor[scan]));
- }
- } else if (0x01 <= scan && scan <= 0x35) {
- /*
- * typewriter key with SHIFT
- */
- return(c & 0xff);
- } else {
- /*
- * no code for this
- */
- return(-1);
- }
- } else { /* normal */
- if (SCAN_PF1 <= scan && scan <= SCAN_PF10) {
- /*
- * normal PF key
- */
- return(putStrKeyBuf(NORMALFunkey[scan - SCAN_PF1]));
- } else if (SCAN_PAD_BEGIN <= scan && scan <= SCAN_PAD_END) {
- /*
- * normal pad key; non numlock
- */
- scan = scan - SCAN_PAD_BEGIN;
- if ((c & 0xff) && ((c & 0xff) != 0xe0)) { /* keypad is tenkey */
- if (applKeypad && mode == M_COMM) /* application keypad */
- return(putStrKeyBuf(APPLPad[scan+1 /* 1 means '*' key */]));
- else /* number key */
- return(c & 0xff);
- } else {
- if (mode != M_COMM) /* in setup or history editor */
- return(SETUPCursor[scan]);
- else if (applCursor) /* application cursor */
- return(putStrKeyBuf(APPLCursor[scan]));
- else /* normal cursor */
- return(putStrKeyBuf(NORMALCursor[scan]));
- }
- } else if (0x01 <= scan && scan <= 0x35) {
- /*
- * typewriter key (return its ASCII code)
- */
- return(c & 0xff);
- } else {
- /*
- * no code
- */
- return(-1);
- }
- } /* without META, if "CTRL or SHIFT is pushed or not" */
- } /* if "META is pushed or not" */
-
- /* ignore if fallen down here, but it cannot! */
- return(-1);
- }
-
- static void putBuf(c)
- u_short c;
- {
- if (keyBufLength >= MAX_KEYBUF) {
- bell(); /* bell is more proper than click */
- return;
- }
- keyBuf[keyBufInPtr] = c;
- if (++keyBufInPtr >= MAX_KEYBUF)
- keyBufInPtr = 0;
- keyBufLength++;
- if (c == DEL)
- bsKeyCount++; /* for BS key watcher */
- }
-
- static short putStrKeyBuf(s)
- register char *s;
- {
- register char *p;
-
- if (*s == '\0')
- return(-1);
- for (p = s + 1; *p; p++)
- putBuf(*p);
- return(*s);
- }
-
- void keyMode()
- {
- /* nothing to do */
- }
-
- void setBSDel(bsDelFlag)
- int bsDelFlag;
- {
- /* not needed */
- }
-
- #ifdef SETUP
- void editPFKey()
- {
- editKeyTable(funkeyName, NORMALFunkey, NUM_FUNKEY, MAX_FUNKEY, FUNKEY_NAME_LEN);
- }
-
- void editSPFKey()
- {
- editKeyTable(funkeyName, SHIFTFunkey, NUM_FUNKEY, MAX_FUNKEY, FUNKEY_NAME_LEN);
- }
-
- void editCursorKey()
- {
- editKeyTable(&keypadName[1], NORMALCursor, NUM_PADKEY, MAX_FUNKEY, KEYPAD_NAME_LEN);
- }
-
- void editAppCursorKey()
- {
- editKeyTable(&keypadName[1], APPLCursor, NUM_PADKEY, MAX_FUNKEY, KEYPAD_NAME_LEN);
- }
-
- void editAppKeyPad()
- {
- editKeyTable(keypadName, APPLPad, NUM_PADKEY+1, MAX_FUNKEY, KEYPAD_NAME_LEN);
- }
-
- void editNormalKeys(){}
- void editShiftKeys(){}
- void editCtrlKeys(){}
- void editKanaKeys(){}
- void editShKanaKeys(){}
-
- void editBinding()
- {
- editKeyTable(bindName, (u_char (*)[MAX_FUNKEY])bindTab, NUM_BIND, 1, BIND_NAME_LEN);
- }
-
- void saveKey(fd)
- int fd;
- /*
- * save key pad string, cursor key string, pf key string
- */
- {
- write(fd, (char *)NORMALFunkey, sizeof(NORMALFunkey));
- write(fd, (char *)SHIFTFunkey, sizeof(SHIFTFunkey));
- write(fd, (char *)NORMALCursor, sizeof(NORMALCursor));
- write(fd, (char *)APPLCursor, sizeof(APPLCursor));
- write(fd, (char *)APPLPad, sizeof(APPLPad));
- write(fd, (char *)bindTab, sizeof(bindTab));
- }
- #endif /* SETUP */
-
- void loadKey(fd)
- int fd;
- /*
- * load key pad string, cursor key string, pf key string
- */
- {
- read(fd, (char *)NORMALFunkey, sizeof(NORMALFunkey));
- read(fd, (char *)SHIFTFunkey, sizeof(SHIFTFunkey));
- read(fd, (char *)NORMALCursor, sizeof(NORMALCursor));
- read(fd, (char *)APPLCursor, sizeof(APPLCursor));
- read(fd, (char *)APPLPad, sizeof(APPLPad));
- read(fd, (char *)bindTab, sizeof(bindTab));
- }
-
- #ifdef MOUSE
-
- /*
- *
- * mouse related functions
- *
- */
- int mouseExists;
- int mouseOn;
- int mouseX, mouseY, mouseOriginX, mouseOriginY;
- int mouseLowX, mouseHighX, mouseLowY, mouseHighY;
- void (FAR *mouseHandler)();
- #define INT_MOUSE 0x33
-
- void mouseInit()
- /*
- * initialize mouse
- */
- {
- rg.x.ax = 0;
- int86(INT_MOUSE, &rg, &rg); /* check mouse environment */
- if (rg.x.ax == 0)
- return; /* no mouse */
- /* get current position */
- rg.x.ax = 3;
- int86(INT_MOUSE, &rg, &rg);
- mouseOriginX = mouseX = rg.x.cx;
- mouseOriginY = mouseY = rg.x.dx;
- mouseLowX = mouseOriginX - 100; mouseHighX = mouseOriginX + 100;
- mouseLowY = mouseOriginY - 80; mouseHighY = mouseOriginY + 80;
- mouseSetHandler(YES);
- }
-
- void mouseEnd()
- {
- mouseSetHandler(NO); /* remove handler */
- }
-
- void mouseSetHandler(onOff)
- int onOff;
- {
- struct SREGS segRegs;
-
- mouseHandler = getMouseIntAddress();
- segread(&segRegs);
- rg.x.ax = 0x0c;
- /*
- * if onOff = YES, set move, left/right press handler
- * else mask all event
- */
- rg.x.cx = onOff ? 0x0b : 0;
- segRegs.es = FP_SEG(mouseHandler);
- rg.x.dx = FP_OFF(mouseHandler);
- int86x(INT_MOUSE, &rg, &rg, &segRegs); /* set interrupt handler */
- }
-
- void mouseOnOff(onOff)
- int onOff;
- {
- rg.x.ax = onOff ? 1 : 2;
- int86(INT_MOUSE, &rg, &rg);
- }
-
- void mouseSetPosition(x, y)
- int x, y;
- {
- rg.x.ax = 4;
- rg.x.cx = x;
- rg.x.dx = y;
- int86(INT_MOUSE, &rg, &rg);
- }
-
- void FAR mouseEvent(event, x, y)
- /*
- * mouse event interrupt handler
- */
- int event, x, y;
- {
- u_short c = 0;
- int dx = x - mouseX;
- int dy = y - mouseY;
- int thresh = (mouseSpeed + 1) * MOUSE_SPEED_BASE;
-
- spl7();
- switch (event) {
- case 1: /* cursor is moved. */
- if (mode != M_COMM) {
- if (mode == M_SETUP)
- thresh *= 2; /* for selecting menu */
- if (dy > thresh)
- c = DOWN_KEY;
- else if (dy < -thresh)
- c = UP_KEY;
- if (c) {
- putBuf(c);
- mouseY = y;
- c = 0;
- }
- if (dx > thresh)
- c = RIGHT_KEY;
- else if (dx < -thresh)
- c = LEFT_KEY;
- if (c)
- mouseX = x;
- }
- if (x < mouseLowX || x > mouseHighX || y < mouseLowY || y > mouseHighY) {
- /* recenter mouse position */
- mouseSetPosition(mouseOriginX, mouseOriginY);
- mouseX = mouseOriginX;
- mouseY = mouseOriginY;
- }
- break;
- case 2: /* left botton is pressed. */
- if (mode == M_HISTORY)
- c = INS_KEY;
- else if (mode == M_SETUP)
- c = CR;
- else /* communication mode */
- c = PF_HIST_EDIT; /* call history editor */
- break;
- case 8: /* right botton is pressed. */
- if (mode == M_HISTORY)
- c = CR;
- else if (mode == M_SETUP)
- c = ESC;
- else /* communication mode */
- c = SETUP_KEY; /* call set-up */
- break;
- }
- /*
- * put emulated key into BIOS key buffer
- */
- if (c)
- putBuf(c);
- /*
- * THIS may be bug!
- */
- splx();
- }
- #else
- void FAR mouseEvent() {}
- void mouseOnOff() {}
- void mouseSetPosition() {}
- void mouseEnd() {}
- void mouseInit() {}
- #endif /* MOUSE */
-
-