/*
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
 */


#include <X11/Xlib.h>
#include <X11/keysym.h>

#include "mat_common.h"

#include "com_sun_glass_events_KeyEvent.h"

struct KeyMapEntry
{
    KeySym      	keysym;
    jint		jKeyCode;
};

// erk - this only works with a US 101 keyboard...
static const struct KeyMapEntry gKeyMap[] =
{
    { XK_A,	com_sun_glass_events_KeyEvent_VK_A},
    { XK_B,	com_sun_glass_events_KeyEvent_VK_B},
    { XK_C,	com_sun_glass_events_KeyEvent_VK_C},
    { XK_D,	com_sun_glass_events_KeyEvent_VK_D},
    { XK_E,	com_sun_glass_events_KeyEvent_VK_E},
    { XK_F,	com_sun_glass_events_KeyEvent_VK_F},
    { XK_G,	com_sun_glass_events_KeyEvent_VK_G},
    { XK_H,	com_sun_glass_events_KeyEvent_VK_H},
    { XK_I,	com_sun_glass_events_KeyEvent_VK_I},
    { XK_J,	com_sun_glass_events_KeyEvent_VK_J},
    { XK_K,	com_sun_glass_events_KeyEvent_VK_K},
    { XK_L,	com_sun_glass_events_KeyEvent_VK_L},
    { XK_M,	com_sun_glass_events_KeyEvent_VK_M},
    { XK_N,	com_sun_glass_events_KeyEvent_VK_N},
    { XK_O,	com_sun_glass_events_KeyEvent_VK_O},
    { XK_P,	com_sun_glass_events_KeyEvent_VK_P},
    { XK_Q,	com_sun_glass_events_KeyEvent_VK_Q},
    { XK_R,	com_sun_glass_events_KeyEvent_VK_R},
    { XK_S,	com_sun_glass_events_KeyEvent_VK_S},
    { XK_T,	com_sun_glass_events_KeyEvent_VK_T},
    { XK_U,	com_sun_glass_events_KeyEvent_VK_U},
    { XK_V,	com_sun_glass_events_KeyEvent_VK_V},
    { XK_W,	com_sun_glass_events_KeyEvent_VK_W},
    { XK_X,	com_sun_glass_events_KeyEvent_VK_X},
    { XK_Y,	com_sun_glass_events_KeyEvent_VK_Y},
    { XK_Z,	com_sun_glass_events_KeyEvent_VK_Z},

    { XK_a,	com_sun_glass_events_KeyEvent_VK_A},
    { XK_b,	com_sun_glass_events_KeyEvent_VK_B},
    { XK_c,	com_sun_glass_events_KeyEvent_VK_C},
    { XK_d,	com_sun_glass_events_KeyEvent_VK_D},
    { XK_e,	com_sun_glass_events_KeyEvent_VK_E},
    { XK_f,	com_sun_glass_events_KeyEvent_VK_F},
    { XK_g,	com_sun_glass_events_KeyEvent_VK_G},
    { XK_h,	com_sun_glass_events_KeyEvent_VK_H},
    { XK_i,	com_sun_glass_events_KeyEvent_VK_I},
    { XK_j,	com_sun_glass_events_KeyEvent_VK_J},
    { XK_k,	com_sun_glass_events_KeyEvent_VK_K},
    { XK_l,	com_sun_glass_events_KeyEvent_VK_L},
    { XK_m,	com_sun_glass_events_KeyEvent_VK_M},
    { XK_n,	com_sun_glass_events_KeyEvent_VK_N},
    { XK_o,	com_sun_glass_events_KeyEvent_VK_O},
    { XK_p,	com_sun_glass_events_KeyEvent_VK_P},
    { XK_q,	com_sun_glass_events_KeyEvent_VK_Q},
    { XK_r,	com_sun_glass_events_KeyEvent_VK_R},
    { XK_s,	com_sun_glass_events_KeyEvent_VK_S},
    { XK_t,	com_sun_glass_events_KeyEvent_VK_T},
    { XK_u,	com_sun_glass_events_KeyEvent_VK_U},
    { XK_v,	com_sun_glass_events_KeyEvent_VK_V},
    { XK_w,	com_sun_glass_events_KeyEvent_VK_W},
    { XK_x,	com_sun_glass_events_KeyEvent_VK_X},
    { XK_y,	com_sun_glass_events_KeyEvent_VK_Y},
    { XK_z,	com_sun_glass_events_KeyEvent_VK_Z},

    { XK_grave,	        com_sun_glass_events_KeyEvent_VK_BACK_QUOTE},
    { XK_1,     	com_sun_glass_events_KeyEvent_VK_1},
    { XK_2,	        com_sun_glass_events_KeyEvent_VK_2},
    { XK_3,	        com_sun_glass_events_KeyEvent_VK_3},
    { XK_4,	        com_sun_glass_events_KeyEvent_VK_4},
    { XK_5,	        com_sun_glass_events_KeyEvent_VK_5},
    { XK_6,	        com_sun_glass_events_KeyEvent_VK_6},
    { XK_7,	        com_sun_glass_events_KeyEvent_VK_7},
    { XK_8,	        com_sun_glass_events_KeyEvent_VK_8},
    { XK_9,	        com_sun_glass_events_KeyEvent_VK_9},
    { XK_0,	        com_sun_glass_events_KeyEvent_VK_0},
    { XK_minus,		com_sun_glass_events_KeyEvent_VK_MINUS},
    { XK_equal,		com_sun_glass_events_KeyEvent_VK_EQUALS},

    { XK_asciitilde,	com_sun_glass_events_KeyEvent_VK_BACK_QUOTE},
    { XK_exclam,	com_sun_glass_events_KeyEvent_VK_1},
    { XK_at,    	com_sun_glass_events_KeyEvent_VK_2},
    { XK_numbersign,	com_sun_glass_events_KeyEvent_VK_3},
    { XK_dollar,	com_sun_glass_events_KeyEvent_VK_4},
    { XK_percent,	com_sun_glass_events_KeyEvent_VK_5},
    { XK_asciicircum,	com_sun_glass_events_KeyEvent_VK_6},
    { XK_ampersand,	com_sun_glass_events_KeyEvent_VK_7},
    { XK_asterisk,	com_sun_glass_events_KeyEvent_VK_8},
    { XK_parenleft,	com_sun_glass_events_KeyEvent_VK_9},
    { XK_parenright,	com_sun_glass_events_KeyEvent_VK_0},
    { XK_underscore,	com_sun_glass_events_KeyEvent_VK_MINUS},
    { XK_plus,		com_sun_glass_events_KeyEvent_VK_EQUALS},

    { XK_braceleft,	com_sun_glass_events_KeyEvent_VK_OPEN_BRACKET},
    { XK_braceright,	com_sun_glass_events_KeyEvent_VK_CLOSE_BRACKET},
    { XK_backslash,	com_sun_glass_events_KeyEvent_VK_BACK_SLASH},

    { XK_bracketleft,	com_sun_glass_events_KeyEvent_VK_OPEN_BRACKET},
    { XK_bracketright,	com_sun_glass_events_KeyEvent_VK_CLOSE_BRACKET},
    { XK_bar,	        com_sun_glass_events_KeyEvent_VK_BACK_SLASH},

    { XK_semicolon,	com_sun_glass_events_KeyEvent_VK_SEMICOLON},
    { XK_apostrophe,	com_sun_glass_events_KeyEvent_VK_QUOTE},

    { XK_colon,	        com_sun_glass_events_KeyEvent_VK_SEMICOLON},
    { XK_quotedbl,	com_sun_glass_events_KeyEvent_VK_QUOTE},

    { XK_comma,		com_sun_glass_events_KeyEvent_VK_COMMA},
    { XK_period,	com_sun_glass_events_KeyEvent_VK_PERIOD},
    { XK_slash,		com_sun_glass_events_KeyEvent_VK_SLASH},

    { XK_less,		com_sun_glass_events_KeyEvent_VK_COMMA},
    { XK_greater,	com_sun_glass_events_KeyEvent_VK_PERIOD},
    { XK_question,	com_sun_glass_events_KeyEvent_VK_SLASH},

    { XK_Escape,	com_sun_glass_events_KeyEvent_VK_ESCAPE},
    { XK_Return,	com_sun_glass_events_KeyEvent_VK_ENTER},
    { XK_space,		com_sun_glass_events_KeyEvent_VK_SPACE},
    { XK_BackSpace,	com_sun_glass_events_KeyEvent_VK_BACKSPACE},
    { XK_Tab,		com_sun_glass_events_KeyEvent_VK_TAB},

    { XK_F1,		com_sun_glass_events_KeyEvent_VK_F1},
    { XK_F2,		com_sun_glass_events_KeyEvent_VK_F2},
    { XK_F3,		com_sun_glass_events_KeyEvent_VK_F3},
    { XK_F4,		com_sun_glass_events_KeyEvent_VK_F4},
    { XK_F5,		com_sun_glass_events_KeyEvent_VK_F5},
    { XK_F6,		com_sun_glass_events_KeyEvent_VK_F6},
    { XK_F7,		com_sun_glass_events_KeyEvent_VK_F7},
    { XK_F8,		com_sun_glass_events_KeyEvent_VK_F8},
    { XK_F9,		com_sun_glass_events_KeyEvent_VK_F9},
    { XK_F10,		com_sun_glass_events_KeyEvent_VK_F10},
    { XK_F11,		com_sun_glass_events_KeyEvent_VK_F11},
    { XK_F12,		com_sun_glass_events_KeyEvent_VK_F12},

    { XK_Page_Down,	com_sun_glass_events_KeyEvent_VK_PAGE_DOWN},
    { XK_Page_Up,	com_sun_glass_events_KeyEvent_VK_PAGE_UP},
    { XK_End,		com_sun_glass_events_KeyEvent_VK_END},
    { XK_Home,		com_sun_glass_events_KeyEvent_VK_HOME},
    { XK_Insert,	com_sun_glass_events_KeyEvent_VK_INSERT},

    { XK_Up,		com_sun_glass_events_KeyEvent_VK_UP},
    { XK_Down,		com_sun_glass_events_KeyEvent_VK_DOWN},
    { XK_Left,		com_sun_glass_events_KeyEvent_VK_LEFT},
    { XK_Right,		com_sun_glass_events_KeyEvent_VK_RIGHT},
};
static const int gKeyMapSize = sizeof(gKeyMap) / sizeof(struct KeyMapEntry);

jint mat_GetJavaKeyCode(KeySym keysym)
{
    int i;
    for (i=0; i<gKeyMapSize; i++)
	{
        if (gKeyMap[i].keysym == keysym) {
            return gKeyMap[i].jKeyCode;
        }
    }
    return com_sun_glass_events_KeyEvent_VK_UNDEFINED;
}
