home *** CD-ROM | disk | FTP | other *** search
- #ifdef X11
-
- #include "jam.h"
- #include "def.h"
- #include "keyname.h"
- #include "ttydef.h"
- #include "chrdef.h"
-
- #include "keysym.h"
- #ifdef HP
- # include "HPkeysym.h"
- #endif
-
- #define MAXCHARS 512
- static KCHAR *kbdinput = 0;
- static KCHAR *putback_kbdinput = 0;
- static int kbdcount = 0;
- static int putback_kbdcount = 0;
-
- static BOOL s_IgnoreWM_CHAR = FALSE;
-
- /* Needed by all sorts of things
- */
- static BOOL bCapLock = FALSE;
- static int bShifted = 0;
- static int bCtrled = 0;
- static int bAlted = 0;
-
- /* map subfunctions
- */
- static BOOL rn_(mapFkey, (KeySym *pk, BOOL shift, BOOL ctrl, BOOL alt));
- static BOOL rn_(mapKPkey, (KeySym *pk));
- static BOOL rn_(mapAlphaKey, (KeySym *pk, BOOL shift, BOOL ctrl, BOOL alt,
- XKeyEvent *e));
-
- /* alloc the internal event queues
- */
- void InitInput()
- {
- if (!(kbdinput = (KCHAR *)calloc(MAXCHARS, sizeof(KCHAR))))
- WindowMessage("Unable to init kbdinput", TRUE);
- if (!(putback_kbdinput = (KCHAR *)calloc(MAXCHARS, sizeof(KCHAR))))
- WindowMessage("Unable to init putback_kbdinput", TRUE);
- }
-
- /* putback kchar to to input queue
- */
- void PutbackKchar(c)
- KCHAR c;
- {
- if (putback_kbdcount < MAXCHARS)
- {
- putback_kbdinput[putback_kbdcount] = c;
- putback_kbdcount++;
- }
- }
-
- /* if putback'ed events, push to normal input stream now
- */
- void CheckForPutback()
- {
- int i, j;
-
- /* if not enough room, die
- */
- if ((MAXCHARS - kbdcount) < putback_kbdcount)
- putback_kbdcount = 0;
- else
- for (j = putback_kbdcount, i = 0; i < j; i++)
- {
- AddKchar(putback_kbdinput[i]);
- putback_kbdcount--;
- }
- }
-
- /* push normal string to input buf
- */
- void AddString(s)
- char *s;
- {
- while (s && *s)
- AddKchar((KCHAR)*s++);
- }
-
- /* add kchar to internal queue of collected/manufactured events
- */
- void AddKchar(c)
- KCHAR c;
- {
- if (kbdcount < MAXCHARS)
- {
- kbdinput[kbdcount] = c;
- kbdcount++;
- }
- else
- ttbeep();
- }
-
- /* Get shift state of event; is mouse or keyboard or error!
- */
- BOOL GetShiftState(e)
- XEvent *e;
- {
- XKeyEvent *event = (XKeyEvent *)e;
-
- return((event->state & ShiftMask) || (event->state & LockMask));
- }
- /* Get ctrl state of event; is mouse or keyboard or error!
- */
- BOOL GetCtrlState(e)
- XEvent *e;
- {
- XKeyEvent *event = (XKeyEvent *)e;
-
- return(event->state & ControlMask);
- }
-
- /* return first inqueued char from event into
- * pkchar (if NON_NULL) and return status
- */
- BOOL WindowReturnKCHAR(pkchar)
- KCHAR *pkchar;
- {
- register int i;
- BOOL result = FALSE;
-
- /* if something, remove from queue head
- * and push the queue down
- * (should use head/tail pointers!)
- */
- if (kbdcount > 0)
- {
- result = TRUE;
- if (pkchar)
- {
- *pkchar = kbdinput[0];
- kbdcount--;
- for (i = 0; i < kbdcount; i++)
- kbdinput[i] = kbdinput[i+1];
- }
- }
- return(result);
- }
-
- /* map windows event to kchar
- */
- BOOL WindowMapKey(event, down)
- XKeyEvent *event;
- BOOL down;
- {
- KeySym key;
- KCHAR wKey;
- BOOL mapped;
- char unknown[NLINE * 2];
-
- key = XLookupKeysym(event, 0);
- wKey = 0;
- mapped = FALSE;
-
- #ifdef Xlib_loses_keys
- if ((key == XK_Shift_L) || (key == XK_Shift_R) ||
- (key == XK_Caps_Lock) ||
- (key == XK_Control_L) || (key == XK_Control_R) ||
- (key == XK_Escape) || (key == XK_Meta_L) || (key == XK_Meta_R) ||
- (key == XK_Alt_L) || (key == XK_Alt_R) || (key == XK_Multi_key))
- {
- if (down)
- printf("\nNew state on 'down'\n");
- GetKeyStates(); /* recompute all values */
- return(FALSE);
- }
- else
- #else
- bShifted = (event->state & ShiftMask) || (event->state & LockMask);
- bCtrled = event->state & ControlMask;
- bAlted = (event->state & Mod1Mask) || (event->state & Mod2Mask) ||
- (event->state & Mod3Mask) || (event->state & Mod4Mask) ||
- (event->state & Mod5Mask);
- #endif
-
- #ifdef DEBUGKEYS
- printf("keycode %d [%s]\n", event->keycode, XKeysymToString(key));
- #endif
-
- if (!down)
- return(FALSE); /* ignore other up events */
-
- /* grab some keys up front
- */
- switch (key)
- {
- case XK_space: /* really printable, but caught up here */
- if (bCtrled)
- {
- ExtendedFunction(function_name(setmark));
- return (TRUE);
- }
- else
- wKey = ' ';
- break;
-
- #ifdef HP
- case XK_DeleteLine:
- bCtrled = TRUE; /* force it */
- #endif
- case XK_BackSpace:
- if (bCtrled) /* hack to move to 0, kill whole line */
- {
- ExtendedFunction(function_name(gotobol));
- ExtendedFunction(function_name(killline));
- ExtendedFunction(function_name(killline));
- return(TRUE);
- }
- else if (bShifted)
- {
- ExtendedFunction(function_name(delbword));
- return(TRUE);
- }
- else
- wKey = 0x7f; /* ttyio mapped it this way too, ick ! */
- break;
-
- case XK_Delete:
- wKey = 0x7f;
- break;
- #ifdef HP
- case XK_DeleteChar:
- wKey = KDELETE;
- break;
- #endif
-
- case XK_Tab:
- if (bShifted || bAlted)
- {
- ExtendedFunction(function_name(hardtab));
- return(TRUE);
- }
- wKey = CCHR('I');
- break;
-
- case XK_Linefeed:
- wKey = CCHR('N');
- break;
-
- case XK_Return:
- wKey = CCHR('M');
- break;
-
- case XK_Escape: /* should we keep this?? */
- return (FALSE);
-
- case XK_Home:
- if (bCtrled)
- wKey = KCHOME;
- else
- wKey = KHOME;
- break;
-
- case XK_Left:
- if (bCtrled)
- wKey = KCLEFT;
- else
- wKey = KLEFT;
- break;
-
- case XK_Up:
- if (bCtrled)
- {
- if (bAlted)
- ExtendedFunction(function_name(prevwind));
- else
- ExtendedFunction(function_name(back1page));
- return(TRUE);
- }
- else
- wKey = KUP;
- break;
-
- case XK_Right:
- if (bCtrled)
- wKey = KCRIGHT;
- else
- wKey = KRIGHT;
- break;
-
- case XK_Down:
- if (bCtrled)
- {
- if (bAlted)
- ExtendedFunction(function_name(nextwind));
- else
- ExtendedFunction(function_name(forw1page));
- return (TRUE);
- }
- else
- wKey = KDOWN;
- break;
-
- case XK_Prior:
- if (bCtrled)
- wKey = KCPGUP;
- else
- wKey = KPGUP;
- break;
-
- case XK_Next:
- if (bCtrled)
- wKey = KCPGDN;
- else
- wKey = KPGDN;
- break;
-
- case XK_End:
- if (bCtrled)
- wKey = KCEND;
- else
- wKey = KEND;
- break;
-
- case XK_Select:
- if (bShifted)
- wKey = KSSELECT;
- else
- wKey = KSELECT;
- break;
-
- case XK_Find:
- if (bShifted)
- wKey = KSFIND;
- else
- wKey = KFIND;
- break;
-
- case XK_Insert:
- wKey = KINSERT;
- break;
-
- default: /* wasn't handled yet, try below */
- break;
- }
-
- /* if key was successfully mapped above, load to input queue
- * and return
- */
- if (wKey != 0)
- {
- AddKchar(wKey);
- return (TRUE);
- }
-
- /* try mapping alphanumeric or special keys
- */
- if ((key >= XK_space) && (key <= XK_asciitilde))
- mapped = mapAlphaKey(&key, bShifted, bCtrled, bAlted, event);
-
- else if ((key >= XK_F1) && (key <= XK_F12))
- mapped = mapFkey(&key, bShifted, bCtrled, bAlted);
-
- else if ((key >= XK_KP_Enter) && (key <= XK_KP_9))
- mapped = mapKPkey(&key);
-
- if (mapped)
- {
- wKey = (KCHAR)key;
- AddKchar(wKey);
- return (TRUE);
- }
-
- /* unsupported key/message combination
- */
- if ((key == XK_Shift_L) || (key == XK_Shift_R) ||
- (key == XK_Caps_Lock) ||
- (key == XK_Control_L) || (key == XK_Control_R) ||
- (key == XK_Escape) || (key == XK_Meta_L) || (key == XK_Meta_R) ||
- (key == XK_Alt_L) || (key == XK_Alt_R) || (key == XK_Multi_key))
- ;
- else
- {
- sprintf(unknown, "Unknown key %x", key);
- ewprintf(unknown);
- }
- return (FALSE);
- }
- /* function key mapping
- */
- static BOOL mapFkey(pwKey, bShifted, bCtrled, bAlted)
- KeySym *pwKey;
- BOOL bShifted;
- BOOL bCtrled;
- BOOL bAlted;
- {
- KeySym wKey = *pwKey;
- BOOL mapped = TRUE;
-
- switch(wKey)
- {
- case XK_F1:
- if (bShifted && bCtrled) wKey = KCSF1;
- else if (bShifted) wKey = KSF1;
- else if (bCtrled) wKey = KCF1;
- else if (bAlted) wKey = KMF1;
- else wKey = KF1;
- break;
-
- case XK_F2:
- if (bShifted && bCtrled) wKey = KCSF2;
- else if (bShifted) wKey = KSF2;
- else if (bCtrled) wKey = KCF2;
- else if (bAlted) wKey = KMF2;
- else wKey = KF2;
- break;
-
- case XK_F3:
- if (bShifted && bCtrled) wKey = KCSF3;
- else if (bShifted) wKey = KSF3;
- else if (bCtrled) wKey = KCF3;
- else if (bAlted) wKey = KMF3;
- else wKey = KF3;
- break;
-
- case XK_F4:
- if (bShifted && bCtrled) wKey = KCSF4;
- else if (bShifted) wKey = KSF4;
- else if (bCtrled) wKey = KCF4;
- else if (bAlted) wKey = KMF4;
- else wKey = KF4;
- break;
-
- case XK_F5:
- if (bShifted && bCtrled) wKey = KCSF5;
- else if (bShifted) wKey = KSF5;
- else if (bCtrled) wKey = KCF5;
- else if (bAlted) wKey = KMF5;
- else wKey = KF5;
- break;
-
- case XK_F6:
- if (bShifted && bCtrled) wKey = KCSF6;
- else if (bShifted) wKey = KSF6;
- else if (bCtrled) wKey = KCF6;
- else if (bAlted) wKey = KMF6;
- else wKey = KF6;
- break;
-
- case XK_F7:
- if (bShifted && bCtrled) wKey = KCSF7;
- else if (bShifted) wKey = KSF7;
- else if (bCtrled) wKey = KCF7;
- else if (bAlted) wKey = KMF7;
- else wKey = KF7;
- break;
-
- case XK_F8:
- if (bShifted && bCtrled) wKey = KCSF8;
- else if (bShifted) wKey = KSF8;
- else if (bCtrled) wKey = KCF8;
- else if (bAlted) wKey = KMF8;
- else wKey = KF8;
- break;
-
- case XK_F9:
- if (bShifted && bCtrled) wKey = KCSF9;
- else if (bShifted) wKey = KSF9;
- else if (bCtrled) wKey = KCF9;
- else if (bAlted) wKey = KMF9;
- else wKey = KF9;
- break;
-
- case XK_F10:
- if (bShifted && bCtrled) wKey = KCSF10;
- else if (bShifted) wKey = KSF10;
- else if (bCtrled) wKey = KCF10;
- else if (bAlted) wKey = KMF10;
- else wKey = KF10;
- break;
-
- case XK_F11:
- if (bShifted && bCtrled) wKey = KCSF11;
- else if (bShifted) wKey = KSF11;
- else if (bCtrled) wKey = KCF11;
- else if (bAlted) wKey = KMF11;
- else wKey = KF11;
- break;
-
- case XK_F12:
- if (bShifted && bCtrled) wKey = KCSF12;
- else if (bShifted) wKey = KSF12;
- else if (bCtrled) wKey = KCF12;
- else if (bAlted) wKey = KMF12;
- else wKey = KF12;
- break;
-
- default:
- mapped = FALSE;
- }
-
- *pwKey = wKey;
- return (mapped);
- }
- /* keypadkey mapping
- */
- static BOOL mapKPkey(pwKey)
- KeySym *pwKey;
- {
- KeySym wKey = *pwKey;
- BOOL mapped = TRUE;
-
- switch(wKey)
- {
- case XK_KP_0:
- wKey = KP0;
- break;
-
- case XK_KP_1:
- wKey = KP1;
- break;
-
- case XK_KP_2:
- wKey = KP2;
- break;
-
- case XK_KP_3:
- wKey = KP3;
- break;
-
- case XK_KP_4:
- wKey = KP4;
- break;
-
- case XK_KP_5:
- wKey = KP5;
- break;
-
- case XK_KP_6:
- wKey = KP6;
- break;
-
- case XK_KP_7:
- wKey = KP7;
- break;
-
- case XK_KP_8:
- wKey = KP8;
- break;
-
- case XK_KP_9:
- wKey = KP9;
- break;
-
- case XK_KP_Add:
- wKey = KPADD;
- break;
-
- case XK_KP_Multiply:
- wKey = KPMUL;
- break;
-
- case XK_KP_Subtract:
- wKey = KPSUB;
- break;
-
- case XK_KP_Decimal:
- wKey = KPDEL;
- break;
-
- case XK_KP_Divide:
- wKey = KPDIV;
- break;
-
- case XK_KP_Enter:
- wKey = KPENTER;
- break;
-
- default:
- mapped = FALSE;
- }
-
- *pwKey = wKey;
- s_IgnoreWM_CHAR = mapped; /* generated an event to ignore */
- return (mapped);
- }
- static BOOL mapAlphaKey(pKey, bShifted, bCtrled, bAlted, event)
- KeySym *pKey;
- BOOL bShifted;
- BOOL bCtrled;
- BOOL bAlted;
- XKeyEvent *event;
- {
- #define LEN 10
- char buffer[LEN];
- KCHAR wkey;
- int count;
-
- /* strip junk
- */
- wkey = (KCHAR)(*pKey & 0xFF);
-
- /* some modifier keys need to be uppercased (this
- * captures normal translated events which would normally be in
- * in the buffer)
- */
- if (bAlted || bCtrled || bShifted)
- {
- if ((wkey >= 'a') && (wkey <= 'z'))
- {
- wkey = TOUPPER(wkey); /* defensive.... */
-
- /* Ctrl overrides Alt, Alt overrides Shift... no multiple
- * modifiers at this level. Note that Shift already took effect
- * above anyway
- */
- wkey = (bCtrled ? CCHR(wkey) : (bAlted ? (wkey | METABIT) : wkey));
- }
- else if ((wkey >= '0') && (wkey <= '9') && !bShifted)
- {
- wkey = (bAlted ? (wkey | METABIT) : wkey);
- }
-
- /* must be special char
- */
- else if (bShifted)
- {
- KeySym newkey;
- char buffer[10];
-
- if (XLookupString(event, buffer, 10, &newkey, 0) != NoSymbol)
- {
- wkey = buffer[0];
- }
- else
- return (FALSE); /* ack! */
- }
- }
-
- *pKey = (KeySym)wkey; /* return value */
- return (TRUE); /* should be impossible to fail */
- }
- #endif
-