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


/**
 * BuildEvent.cc
 *
 * Description: build 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 "x.h"
#include "xvoice.h"

/**
 * structure for mapping 7bit ascii chars to X Keysyms
 */
typedef struct
{
    KeySym sym;
    unsigned int state;
} SymEntry;

/**
 * array, keyed by 8-bit char code, mapping into X KeySyms and ShiftStates
 */
SymEntry asciiSymMap[256] =
{
    { 0, 0 },              //0
    { 0, 0 },
    { 0, 0 },
    { 0, 0 },
    { 0, 0 },
    { 0, 0 },
    { 0, 0 },
    { 0, 0 },
    { XK_BackSpace, 0 },
    { XK_Tab, 0 },          //9
    { XK_Linefeed, 0 },
    { 0, 0 },
    { 0, 0 },
    { XK_Return, 0 },
    { 0, 0 },
    { 0, 0 },
    { 0, 0 },
    { 0, 0 },
    { 0, 0 },
    { 0, 0 },               
    { 0, 0 },              //20
    { 0, 0 },
    { 0, 0 },
    { 0, 0 },
    { 0, 0 },
    { 0, 0 },
    { 0, 0 },
    { 0, 0 },
    { 0, 0 },
    { 0, 0 },                
    { 0, 0 },              //30
    { 0, 0 },
    { XK_space, 0 },
    { XK_exclam, ShiftMask },
    { XK_quotedbl, ShiftMask },
    { XK_numbersign, ShiftMask },             //35
    { XK_dollar, ShiftMask },
    { XK_percent, ShiftMask },
    { XK_ampersand, ShiftMask },
    { XK_apostrophe, 0 },         
    { XK_parenleft, ShiftMask }, //40
    { XK_parenright, ShiftMask },
    { XK_asterisk, ShiftMask },
    { XK_plus, ShiftMask },
    { XK_comma, 0 },
    { XK_minus, 0 },
    { XK_period, 0 },
    { XK_slash, 0 },
    { XK_0, 0 },
    { XK_1, 0 },                  
    { XK_2, 0 },                 //50
    { XK_3, 0 },
    { XK_4, 0 },
    { XK_5, 0 },
    { XK_6, 0 },
    { XK_7, 0 },
    { XK_8, 0 },
    { XK_9, 0 },
    { XK_colon, ShiftMask },
    { XK_semicolon, 0 },
    { XK_less, ShiftMask },
    { XK_equal, 0 },
    { XK_greater, ShiftMask },
    { XK_question, ShiftMask },
    { XK_at, ShiftMask },
    { XK_A, ShiftMask },
    { XK_B, ShiftMask },
    { XK_C, ShiftMask },
    { XK_D, ShiftMask },
    { XK_E, ShiftMask },
    { XK_F, ShiftMask },
    { XK_G, ShiftMask },
    { XK_H, ShiftMask },
    { XK_I, ShiftMask },
    { XK_J, ShiftMask },
    { XK_K, ShiftMask },
    { XK_L, ShiftMask },
    { XK_M, ShiftMask },
    { XK_N, ShiftMask },
    { XK_O, ShiftMask },
    { XK_P, ShiftMask },
    { XK_Q, ShiftMask },
    { XK_R, ShiftMask },
    { XK_S, ShiftMask },
    { XK_T, ShiftMask },
    { XK_U, ShiftMask },
    { XK_V, ShiftMask },
    { XK_W, ShiftMask },
    { XK_X, ShiftMask },
    { XK_Y, ShiftMask },
    { XK_Z, ShiftMask },
    { XK_bracketleft, 0 },
    { XK_backslash, 0 },
    { XK_bracketright, 0 },
    { XK_asciicircum, ShiftMask},
    { XK_underscore, ShiftMask },
    { XK_grave, 0 },
    { XK_a, 0 },
    { XK_b, 0 },
    { XK_c, 0 },
    { XK_d, 0 },
    { XK_e, 0 },
    { XK_f, 0 },
    { XK_g, 0 },
    { XK_h, 0 },
    { XK_i, 0 },
    { XK_j, 0 },
    { XK_k, 0 },
    { XK_l, 0 },
    { XK_m, 0 },
    { XK_n, 0 },
    { XK_o, 0 },
    { XK_p, 0 },
    { XK_q, 0 },
    { XK_r, 0 },
    { XK_s, 0 },
    { XK_t, 0 },
    { XK_u, 0 },
    { XK_v, 0 },
    { XK_w, 0 },
    { XK_x, 0 },
    { XK_y, 0 },
    { XK_z, 0 },  //122
    { XK_braceleft, ShiftMask },
    { XK_bar, ShiftMask },
    { XK_braceright, ShiftMask },
    { XK_asciitilde, ShiftMask },
    { XK_Page_Up, 0},
    { XK_Page_Down, 0 },
    { XK_Right, 0 },   
    { XK_Left, 0 },
    { XK_Up, 0 },
    { XK_Down, 0 },
    { XK_Escape, 0 }
};

void buildKeyEvent( XEvent* xev, int c, bool alt, bool ctrl, bool shift )
{


    // lookup the appropriate keysym, and then translate to an X KeyCode
    KeySym ks = asciiSymMap[(unsigned char)c].sym;
    KeyCode kc = XKeysymToKeycode( gDisplay, ks );

    // build the event
    xev->xkey.type = KeyPress;
    xev->xkey.window = 0;
    xev->xkey.subwindow = None;
    xev->xkey.display = gDisplay;
    xev->xkey.serial = 0L;
    xev->xkey.send_event = False;
    xev->xkey.time = CurrentTime;
    xev->xkey.same_screen = True;
    xev->xkey.x = 0;
    xev->xkey.y = 0;
    xev->xkey.x_root = 0;
    xev->xkey.y_root = 0;
    xev->xkey.keycode = kc;
    xev->xkey.state = asciiSymMap[(unsigned char)c].state;
    if ( alt )
        xev->xkey.state |= Mod1Mask;
    if ( ctrl )
        xev->xkey.state |= ControlMask;
    if ( shift )
        xev->xkey.state |= ShiftMask;
}

void buildButtonEvent( XEvent* xev,
                       int button, bool down,
                       int x, int y,
                       bool alt, bool ctrl, bool shift )
{
    // build the event
    xev->xbutton.type = down? ButtonPress : ButtonRelease;
    xev->xbutton.window = 0;
    xev->xbutton.subwindow = None;
    xev->xbutton.display = gDisplay;
    xev->xbutton.serial = 0L;
    xev->xbutton.send_event = False;
    xev->xbutton.time = CurrentTime;
    xev->xbutton.same_screen = True;
    xev->xbutton.x = 0;
    xev->xbutton.y = 0;
    xev->xbutton.x_root = 0;
    xev->xbutton.y_root = 0;
    xev->xbutton.button = button;
    xev->xbutton.state = 0;
    if ( alt )
        xev->xbutton.state |= Mod1Mask;
    if ( ctrl )
        xev->xbutton.state |= ControlMask;
    if ( shift )
        xev->xbutton.state |= ShiftMask;
}