/*
 *  myparseix.c - frontend to ParseIX() to allow trapping of mousebutton codes
 *
 *  Author: Stefan Sticht
 *
 *  Version history:
 *
 *  V1.00   initial hack
 */

/********************************************************************
 *                             interfacing                          *
 ********************************************************************/

/*
 *  include files
 */
#include <string.h>
#include <devices/inputevent.h>
#include <libraries/commodities.h>
#include <clib/commodities_protos.h>
#include <pragmas/commodities_pragmas.h>

#ifdef DEBUG
#define printf KPrintF
#include <clib/dlib_protos.h>
#endif

extern struct Library *CxBase;

/********************************************************************
 *                             functions                            *
 ********************************************************************/

long myparseix(char *description, IX *ix)
{
    char blanks[] = "           ";
    char *pos;
    long failurecode = 0l;
    UWORD code = 0;

    /*
     *  parse and remove our special keywords
     *
     */
    if (pos = strstr(description, "lbuttoncode")) {
        code |= IECODE_LBUTTON;
        strncpy(pos, blanks, 11l);
        }
    else if (pos = strstr(description, "rbuttoncode")) {
        code |= IECODE_RBUTTON;
        strncpy(pos, blanks, 11l);
        }
    else if (pos = strstr(description, "mbuttoncode")) {
        code |= IECODE_MBUTTON;
        strncpy(pos, blanks, 11l);
        }

    while (*description && (*description == ' ')) description++;

    #ifdef DEBUG
    printf("myparseix(): description is %s\n", description);
    #endif

    #ifdef DEBUG
    printf("\nmyparseix(): calling ParseIX()!!!\n");
    #endif
    if (*description) failurecode = ParseIX((UBYTE *)description, ix);
    #ifdef DEBUG
    printf("\nmyparseix(): after ParseIX()!!!\n");
    #endif

    /*
     *  now change ix for our new keywords
     *
     */
    if (code) {
        ix->ix_Code |= code;
        /*
         *  change also QualMask for mouse buttons
         *
         */
        ix->ix_QualMask |= IEQUALIFIER_MIDBUTTON | IEQUALIFIER_RBUTTON | IEQUALIFIER_LEFTBUTTON;

        /*
         *  parse for qualifiers the use wants to be considered irrelevant
         *
         */
        if (strstr(description, "-leftbutton")) ix->ix_QualMask &= ~IEQUALIFIER_LEFTBUTTON;
        if (strstr(description, "-midbutton")) ix->ix_QualMask &= ~IEQUALIFIER_MIDBUTTON;
        if (strstr(description, "-rbutton")) ix->ix_QualMask &= ~IEQUALIFIER_RBUTTON;
        }

    #ifdef DEBUG
    printf("ix_Version      = %ld\n", ix->ix_Version);
    printf("ix_Class        = 0x%lx\n", ix->ix_Class);
    printf("ix_Code         = 0x%lx\n", ix->ix_Code);
    printf("ix_CodeMask     = 0x%lx\n", ix->ix_CodeMask);
    printf("ix_Qualifier    = 0x%lx\n", ix->ix_Qualifier);
    printf("ix_QualMask     = 0x%lx\n", ix->ix_QualMask);
    printf("ix_QualSame     = 0x%lx\n", ix->ix_QualSame);
    if (failurecode) printf("myparseix(): failed!\n");
    #endif

    return(failurecode);
}
