home *** CD-ROM | disk | FTP | other *** search
- /*
- kbmacro.c
-
- % kb_Macro keystroke macro recorder.
-
- OWL 1.2
- Copyright (c) 1990 by Oakland Group, Inc.
- ALL RIGHTS RESERVED.
-
- Revision History:
- -----------------
- 8/23/90 jdc fixed *indexp++ and warnings
- 8/29/90 jdc ifdef'ed out mouse support, added msg_fptr
- 9/07/90 jmd changed index to indeks to avoid conflicts
- 9/09/90 jmd made do_macmsg macro
- 10/28/90 jdc fixed boolean/int ret conflict
- 10/30/90 jdc allowed original value 'bleed through' for looped macros
- 12/07/90 jmd changed local variable 'keyindex' to keyindex to avoid
- conflict with array of the same name
- */
-
- #include "oakhead.h"
- #include "disppriv.h"
-
- #include "scancode.h"
- #include "kbrecord.h"
-
- #include <ctype.h>
-
- OSTATIC int krec_Check(unsigned wait);
- OSTATIC int krec_Read(moupos_struct *mposp);
- OSTATIC boolean key_mousecheck(int keyind, int *indexp, moupos_struct *mposp);
- OSTATIC int key_FindKeyS(int key);
- OSTATIC int key_FindNameS(char *name);
-
- #define KBMACRO_MAXNEST 22
-
- /** static data **/
-
- static boolean macron = FALSE; /* macros on or off */
- static int macmode = KB_MACRO; /* mode value */
- static int learnkey = FN5; /* key to begin/end learning */
- static int learnindex = 0; /* index of current key learning */
-
- static int nest = -1; /* macro nest level */
- static int keyindex[KBMACRO_MAXNEST+2]; /* index into kb_keylist */
- static int indeks[KBMACRO_MAXNEST+2]; /* index for into macro iarray */
-
- static dig_hReadEvent_func ((*hReadEventFptr)) = FNULL;
- static dig_hCheckEvent_func ((*hCheckEventFptr)) = FNULL;
- static macromsg_fptr msgfptr = FNULL;
-
- #define do_macmsg(m, k) ((msgfptr == FNULL) ? TRUE : msgfptr(m, k))
- /* -------------------------------------------------------------------------- */
-
- boolean kb_Macro(int mode, int key, moupos_struct *mposp, macromsg_fptr macromsg)
- /*
- Operates a key mapped macro handler.
-
- 'mode' determines the operating mode, 'key' is a scancode.
- it is also passed as a message to the msgfptr.
-
- KB_MACRO initialize for macro use. learnkey = key;
-
- KB_MACKEY set for learning, set currkey. currkey = key;
- KB_MACLEARN add a keystroke to the currkey macro. mackey = key;
- KB_MACSTOP finish learning keystrokes.
-
- KB_MACOFF restore previous keystroke processing.
- KB_MACCLEAR clear a macro. currkey = key;
- KB_MACRESET clear all macros.
-
- KB_MACSTART msg for macro message function from krec_Read.
-
- if 0 is passed as the learnkey then it remains unchanged.
-
- if size == -1 (see kb_keylist in kblist.c) then no macro is allowed
- for that key.
- */
- {
- int i;
- boolean ret = TRUE;
-
- owl_Assert(disp_Ok(), OE_KR_DISP);
-
- if (mode == KB_MACPLAY || mode == KB_MACSTOP) {
- ret = do_macmsg(mode, key);
- }
- else if (mode == KB_MACOFF) {
- if (macron == TRUE && (ret = do_macmsg(mode, key)) == TRUE) {
-
- macron = FALSE;
- macmode = KB_MACOFF;
-
- /* restore keybord handler */
- curr_dmgr->disp.dig.hReadEvent = hReadEventFptr;
- curr_dmgr->disp.dig.hCheckEvent = hCheckEventFptr;
- }
- return(ret);
- }
- else if (mode == KB_MACRESET) {
-
- if (!do_macmsg(mode, key)) {
- return(FALSE);
- }
- for (i = 0; kb_keylist[i].scan != 0; i++) {
-
- if (kb_keylist[i].macro != NULL) {
- ia_Close(kb_keylist[i].macro);
- kb_keylist[i].macro = NULL;
- kb_keylist[i].size = 0;
- }
- }
- macmode = (macron) ? KB_MACRO:KB_MACOFF;
- return(TRUE);
- }
- else if (mode == KB_MACCLEAR) {
-
- if ((i = key_FindKeyS(key)) != 0 && kb_keylist[i].macro != NULL
- && (ret = do_macmsg(mode, key)) == TRUE) {
- ia_Close(kb_keylist[i].macro);
- kb_keylist[i].macro = NULL;
- kb_keylist[i].size = 0;
- }
- return(ret);
- }
- else if (macron == FALSE) {
-
- if (!do_macmsg(KB_MACRO, key)) {
- return(FALSE);
- }
- macron = TRUE;
-
- /* save old keyboard handler */
- hReadEventFptr = curr_dmgr->disp.dig.hReadEvent;
- hCheckEventFptr = curr_dmgr->disp.dig.hCheckEvent;
-
- /* set new keyboard handler */
- curr_dmgr->disp.dig.hReadEvent = krec_Read;
- curr_dmgr->disp.dig.hCheckEvent = krec_Check;
- }
- macmode = KB_MACRO;
-
- if (mode == KB_MACRO) {
-
- if (key != 0) {
- learnkey = key;
- }
- msgfptr = macromsg;
- }
- else if (mode == KB_MACSTART) {
- if (do_macmsg(KB_MACSTART, key)) {
- macmode = KB_MACKEY;
- }
- }
- else if (mode == KB_MACLEARN) {
-
- if (kb_keylist[learnindex].scan == 0 /* not in keylist */
- || (kb_keylist[learnindex]).size == -1 /* no macro allowed */
- || !do_macmsg(KB_MACLEARN, key)) {
- return(FALSE);
- }
-
- ia_Put((kb_keylist[learnindex]).macro, (kb_keylist[learnindex]).size, key);
- (kb_keylist[learnindex]).size++;
-
- #ifdef MOUSE_MACROS
- if (key == HARD_MEV) {
- ia_Put((kb_keylist[learnindex]).macro, (kb_keylist[learnindex]).size, mposp->x);
- (kb_keylist[learnindex]).size++;
- ia_Put((kb_keylist[learnindex]).macro, (kb_keylist[learnindex]).size, mposp->y);
- (kb_keylist[learnindex]).size++;
- ia_Put((kb_keylist[learnindex]).macro, (kb_keylist[learnindex]).size, mposp->event);
- (kb_keylist[learnindex]).size++;
- }
- #else
- oak_notused(mposp);
- #endif
- macmode = KB_MACLEARN;
- }
- else if (mode == KB_MACKEY) {
-
- learnindex = key_FindKeyS(key);
-
- if (kb_keylist[learnindex].scan == 0 /* not in keylist */
- || (kb_keylist[learnindex]).size == -1 /* no macro allowed */
- || !do_macmsg(KB_MACKEY, key)) {
- return(FALSE);
- }
-
- (kb_keylist[learnindex]).size = 0;
-
- if ((kb_keylist[learnindex]).macro == NULL
- && ((kb_keylist[learnindex]).macro = ia_Open(1)) == NULL) {
- return(FALSE);
- }
- macmode = KB_MACLEARN;
- }
-
- return(ret);
- }
-
- int key_GetCurrKey(void)
- /*
- return the currkey (current learning key).
-
- returns KEY_NONE if no learning key.
- */
- {
- if (macmode == KB_MACOFF || kb_keylist[learnindex].scan == 0) {
- return(KEY_NONE);
- }
-
- return(kb_keylist[learnindex].scan);
- }
-
- int key_GetLearnKey(void)
- /*
- return the key that toggles macro learning.
- */
- {
- return(learnkey);
- }
-
- int key_GetMode(void)
- /*
- return the current mode value.
- */
- {
- return(macmode);
- }
-
- char *key_GetName(int key)
- /*
- return name of 'key'
- */
- {
- char *name;
-
- name = kb_keylist[key_FindKeyS(key)].name;
-
- return((*name == '\0') ? NULL : name);
- }
-
- boolean key_IsMacro(int key)
- /*
- returns: TRUE if key has a macro and mode != KB_MACOFF
- FALSE otherwise
- */
- {
- return((macmode != KB_MACOFF && kb_keylist[key_FindKeyS(key)].size > 0)
- ? TRUE : FALSE);
- }
-
- boolean key_SetMacroStr(char *str)
- /*
- set a macro from the macro string 'str'.
-
- format: "{currkey}:{mackey1}abc def{mackey2}{mackey3}..."
-
- returns: FALSE if 'str' is not valid, TRUE if sucessful.
- */
- {
- int key, i, j, k, numcount, commacount;
- char buff[KB_NAME_MAXLEN + 2];
- moupos_struct mpos;
-
- /* find the currkey name; {name}: */
-
- if (*str != '{') {
- return(FALSE);
- }
- str++; /* allow for opening brace */
-
- /* copy the currkey into buff, find the closing brace */
- for (i = 0; str[i] != '}'; i++) {
- if (i > KB_NAME_MAXLEN || str[i] == '\0') return(FALSE);
- buff[i] = str[i];
- }
- buff[i] = '\0';
-
- /* set the currkey */
- if ((key = kb_keylist[key_FindNameS(buff)].scan) == 0
- || !kb_Macro(KB_MACKEY, key, &mpos, FNULL)) {
-
- return(FALSE);
- }
-
- i += 2; /* move past the colon */
- do {
- /* skip newlines, find the opening brace or printable character */
- for ( ; str[i] == '\n'; i++) ;
-
- if (str[i] == '\0') break; /* done */
-
- if (str[i] == '{') {
-
- /* move past opening brace */
- i++;
-
- if (str[i] == '{') {
-
- /* opening braces are in braces */
- for ( ; str[i] == '{'; i++) {
- key = (int)str[i];
- }
- }
- else {
- /* copy the key name into buff, find the closing brace */
- /* deal with mouse case */
- for (j = 0, numcount = 0, commacount = 0, k = -1;
- str[i] != '}'; j++, i++) {
-
- if (j > KB_NAME_MAXLEN || str[i] == '\0') return(FALSE);
- buff[j] = str[i];
-
- if (isdigit(str[i]) && numcount == commacount) {
- numcount++;
- if (k == -1) k = j;
- }
- else if (str[i] == ',' && commacount == numcount - 1) {
- commacount++;
- }
- }
- buff[j] = '\0';
- if (numcount == 3 && commacount == 2) {
- sscanf(buff + k, "%d,%d,%u", &(mpos.x), &(mpos.y), &(mpos.event));
- buff[k] = '\0';
- }
-
- if ((key = kb_keylist[key_FindNameS(buff)].scan) == 0) {
- return(FALSE);
- }
- }
- i++; /* move past the closing brace */
- }
- else {
- key = (int)str[i];
- i++;
- }
- kb_Macro(KB_MACLEARN, key, &mpos, FNULL);
-
- } while (str[i] != '\0');
-
- kb_Macro(KB_MACSTOP, 0, &mpos, FNULL);
- return(TRUE);
- }
-
- boolean key_GetMacroStr(int key, char *str)
- /*
- fill 'str' with the macro string for 'key'.
-
- format: "{currkey}:{mackey1}abc def{mackey2}{mackey3}..."
-
- returns: FALSE if 'key' has no macro, TRUE otherwise.
- */
- {
- int i, indeks;
- char *s, *keyname;
-
- *str = '\0';
-
- if (kb_keylist[(indeks = key_FindKeyS(key))].scan == 0
- || kb_keylist[indeks].size <= 0) {
- return(FALSE); /* no macro */
- }
- *str++ = '{';
- for (s = kb_keylist[indeks].name; *s != '\0'; s++, str++) {
- *str = *s;
- }
- *str++ = '}';
- *str++ = ':';
-
- for (i = 0; i < kb_keylist[indeks].size; ) {
-
- key = ia_Get(kb_keylist[indeks].macro, i);
-
- if (key == '{') { /* opening braces are in braces */
- *str++ = '{';
-
- do {
- *str++ = '{';
- i++;
- } while (ia_Get(kb_keylist[indeks].macro, i) == '{');
-
- *str++ = '}';
- }
- else if (*(keyname = kb_keylist[key_FindKeyS(key)].name) == '\0') {
- *str++ = (char)key;
- i++;
- }
- else {
- *str++ = '{';
- for (s = keyname; *s != '\0'; s++) {
- *str++ = *s;
- }
-
- #ifdef MOUSE_MACROS
- if (key == HARD_MEV) {
- sprintf(str, "%d,%d,%u",
- ia_Get(kb_keylist[indeks].macro, i + 1),
- ia_Get(kb_keylist[indeks].macro, i + 2),
- ia_Get(kb_keylist[indeks].macro, i + 3));
- i += 3;
- str += strlen(str);
- }
- #endif
- *str++ = '}';
- i++;
- }
- }
- *str = '\0';
-
- return(TRUE);
- }
-
- /* -------------------------------------------------------------------------- */
-
- static int krec_Check(unsigned wait)
- /*
- Substitute kb_Check handler
-
- returns TRUE if in a multi keystroke macro
- or the original check function return.
- */
- {
- return(nest >= 0 || (*hCheckEventFptr)(wait));
- }
-
- static int krec_Read(moupos_struct *mposp)
- /*
- effects: reads next key from keyboard buffer.
- returns: ASCII value of character in bits 0-7,
- and the scan code of the character in
- bits 8-15.
-
- see kb_Macro().
- */
- {
- int scancode, scan1;
-
- if (nest >= 0) { /* in a multi-keystroke macro */
-
- if (indeks[nest] >= kb_keylist[keyindex[nest]].size) {
-
- if (--nest >= 0) {
- indeks[nest]++;
- }
- }
-
- if (nest >= 0) {
-
- scancode = ia_Get((kb_keylist[keyindex[nest]]).macro, indeks[nest]);
- if (++nest > KBMACRO_MAXNEST) {
- do_macmsg(KB_MAXNEST, scancode);
- nest = -1;
- return(KEY_NONE);
- }
- keyindex[nest] = key_FindKeyS(scancode);
-
- while (keyindex[nest] != keyindex[nest -1] && (kb_keylist[keyindex[nest]]).size > 0) {
-
- indeks[nest] = 0;
- scancode = ia_Get((kb_keylist[keyindex[nest]]).macro, indeks[nest]);
- if (++nest > KBMACRO_MAXNEST) {
- do_macmsg(KB_MAXNEST, scancode);
- nest = -1;
- return(KEY_NONE);
- }
- if ((keyindex[nest] = key_FindKeyS(scancode)) == keyindex[nest -1]) {
- break;
- }
- }
- nest--;
-
- key_mousecheck(keyindex[nest], &(indeks[nest]), mposp);
- indeks[nest]++;
-
- return(scancode);
- }
- }
-
- scan1 = scancode = (*hReadEventFptr)(mposp);/* read a raw keystroke */
-
- if (scancode == learnkey) { /* toggle learn mode */
- if (macmode == KB_MACRO) {
- kb_Macro(KB_MACSTART, scancode, mposp, FNULL);
- }
- else {
- kb_Macro(KB_MACSTOP, scancode, mposp, FNULL);
- }
- }
- else if (macmode == KB_MACKEY) { /* key to learn for */
- kb_Macro(KB_MACKEY, scancode, mposp, FNULL);
- }
- else if (macron) { /* begin macro playback */
-
- if (macmode == KB_MACLEARN) {
- kb_Macro(KB_MACLEARN, scancode, mposp, FNULL);
- }
- else if (!kb_Macro(KB_MACPLAY, scancode, mposp, FNULL)) {
- return(scancode);
- }
-
- nest = 0;
- keyindex[nest] = key_FindKeyS(scancode);
-
- if ((kb_keylist[keyindex[nest]]).size <= 0) {
- nest = -1;
- }
- else {
- while ((kb_keylist[keyindex[nest]]).size > 0) {
-
- indeks[nest] = 0;
- scancode = ia_Get((kb_keylist[keyindex[nest]]).macro, indeks[nest]);
- if (++nest > KBMACRO_MAXNEST) {
- do_macmsg(KB_MAXNEST, scancode);
- nest = -1;
- return(scan1);
- }
- if ((keyindex[nest] = key_FindKeyS(scancode)) == keyindex[nest -1]) {
- break;
- }
- }
- nest--;
-
- key_mousecheck(keyindex[nest], &(indeks[nest]), mposp);
- indeks[nest]++;
- }
- }
-
- return(scancode);
- }
-
- static boolean key_mousecheck(int keyind, int *indexp, moupos_struct *mposp)
- {
- boolean ret = FALSE;
-
- #ifdef MOUSE_MACROS
- if (kb_keylist[keyind].scan == HARD_MEV) {
- (*indexp)++;
- mposp->x = ia_Get((kb_keylist[keyind]).macro, *indexp);
- (*indexp)++;
- mposp->y = ia_Get((kb_keylist[keyind]).macro, *indexp);
- (*indexp)++;
- mposp->event = ia_Get((kb_keylist[keyind]).macro, *indexp);
-
- ret = TRUE;
- }
- #else
- oak_notused(mposp);
- oak_notused(keyind);
- oak_notused(indexp);
- #endif
-
- return(ret);
- }
-
- static int key_FindKeyS(int key)
- /*
- Given a key value find its key_struct index.
- */
- {
- int i;
-
- for (i = 0; kb_keylist[i].scan != 0; i++) {
- if (kb_keylist[i].scan == key) {
- break;
- }
- }
-
- return(i);
- }
-
- static int key_FindNameS(char *name)
- /*
- Given a key name find the key_struct index.
- */
- {
- int i;
-
- for (i = 0; kb_keylist[i].scan != 0; i++) {
- if (strcmp(kb_keylist[i].name, name) == 0) {
- break;
- }
- }
-
- return(i);
- }
-