www.pudn.com > xvoice-0.8.1.rar > EventStream.cc


/**
 * EventStream.cc
 *
 * Description: implementation of an ordered list of X events
 *
 * Copyright (c) 1999, David Z. Creemer.
 * See the LICENSE file. All rights not granted therein are reserved.
 *
 * @author David Z. Creemer
 * @version 1.0
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include 
#include 
#include 
#include 

#include "x.h"

#include "MainWin.h"
#include "Error.h"
#include "BuildEvent.h"
#include "EventStream.h"

struct sKeyValue {
    char *name;
    int value;
}; 

static struct sKeyValue skeys[] = { 
    "page up",          127,
    "page down",        128,
    "right arrow",      129,
    "left arrow",       130,
    "up arrow",         131,
    "down arrow",       132,
    "escape",           133,
        NULL,           -1
};

static sKeyValue* specialKey(const char *string)
{
    int i = 0;
    while (skeys[i].name != NULL) 
    {
        if (!strcasecmp(string, skeys[i].name)) return &skeys[i];
        ++i;
    }
    return NULL;
}

//Parse a single key in an event stream
bool EventStream::parseKeyEvent( const char** a )
{
    const char** attrs;
    int c = '\0';
    bool alt = false, ctrl = false, shift = false;

    attrs = a;
    while ( *attrs )
    {
        const char* key = *attrs++;
        const char* value = *attrs++;
        
        if ( strcasecmp( key, "alt" ) == 0 )
        {
            alt = ( strcasecmp( value, "true" ) == 0 );
        }
        else if ( strcasecmp( key, "control" ) == 0 )
        {
            ctrl = ( strcasecmp( value, "true" ) == 0 );
        }
        else if ( strcasecmp( key, "shift" ) == 0 )
        {
            shift = ( strcasecmp( value, "true" ) == 0 );
        }
    }

    attrs = a;
    while ( *attrs )
    {
        const char* key = *attrs++;
        const char* value = *attrs++;
        sKeyValue *sk;
        if ( strcasecmp( key, "char" ) == 0 )
        {
            while (*value) {
                c = *value;	    	  
                //need to handle \t, \n \r explicitly:
                if(c=='\\'){
                    if(value[1] == 't'){
                        c=9;
                    } else if(value[1]=='r'){
                        c=13;
                    } else if(value[1]=='b'){
                        c=8;
                    } else {
                        c=value[1];
                    }
                    value+=2;
                } else if ((sk=specialKey(value))!=NULL) {
                    dbgprintf(("name %s, value %d\n",sk->name,sk->value));
                    c = sk->value;
                    value+=strlen(sk->name);
                } else {
                    ++value;
                }
                Event ev(alt, ctrl, shift, c);
                push_back(ev);
            }
        }
    }

    return true;
}

bool EventStream::parseMouseEvent( const char** attrs )
{
    char down = true;
    int button =1 ;
    int x = 0, y = 0;
    bool alt = false, ctrl = false, shift = false;

    while ( *attrs )
    {
        const char* key = *attrs++;
        const char* value = *attrs++;

        if ( strcasecmp( key, "button" ) == 0 )
        {
            button = atoi( value );
        }
        else if ( strcasecmp( key, "x" ) == 0 )
        {
            x = atoi( value );
        }
        else if ( strcasecmp( key, "y" ) == 0 )
        {
            y = atoi( value );
        }
        else if ( strcasecmp( key, "down" ) == 0 )
        {
            down = ( strcasecmp( value, "true" ) == 0 );
        }
        else if ( strcasecmp( key, "alt" ) == 0 )
        {
            alt = ( strcasecmp( value, "true" ) == 0 );
        }
        else if ( strcasecmp( key, "control" ) == 0 )
        {
            ctrl = ( strcasecmp( value, "true" ) == 0 );
        }
        else if ( strcasecmp( key, "shift" ) == 0 )
        {
            shift = ( strcasecmp( value, "true" ) == 0 );
        }
        else
        {
            fprintf( stderr, "Unknown attribute: %s\n", key );
            return false;
        }
    }

    Event ev( button, down, x, y, alt, ctrl, shift );
    push_back(ev);
    return true;
}
 

bool EventStream::parseGrammarEvent( const char** attrs )
{
    const char *name = NULL;
    bool enable = true;

    while ( *attrs )
    {
        const char* key = *attrs++;
        const char* value = *attrs++;

        if ( strcasecmp( key, "name" ) == 0 )
        {
            name = value;
        }
        else if ( strcasecmp( key, "action" ) == 0 )
        {
            enable = ( strcasecmp( value, "on" ) == 0 );
            dbgprintf(("action %d\n", enable));
        }
    }
    if (name == NULL) return false;
    Event ev( enable, name );
    push_back(ev);
    return true;
}