/* Translation of Windows virtual keys to X KeySyms

Copyright (C) 2015 Ken D. Olum

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 3 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, see <http://www.gnu.org/licenses/>.
*/


#include "xdragon.h"
#include <windows.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>


KeySym translation[256];     /* Map Windows virtual keys to KeySyms */

/* Return KeySym (or NoSymbol) corresponding to Windows virtual key */
KeySym translate_key(unsigned char windows_key) {
  return translation[windows_key];}

#define translate(windows, x) translation[VK_##windows] = XK_##x;

/* Set up translation table */
setup_translation() {
  unsigned char c;

  memset(translation, NoSymbol, sizeof(translation)); /* Clear array */
  for (c=0x41;c<=0x7a;c++)
    translation[c] = c;		/* ASCII letters same */
  for (c=0x30;c<=0x39;c++)
    translation[c] = c;		/* ASCII numbers same */
  for (c=VK_NUMPAD0;c<=VK_NUMPAD9;c++)
    translation[c] = c-VK_NUMPAD0+XK_KP_0; /* Keypad numbers */
  for (c=VK_F1;c<=VK_F24;c++)
    translation[c] = c-VK_F1+XK_F1; /* Function keys */

  translate(BACK, BackSpace);
  translate(TAB, Tab);
  translate(CLEAR, Clear);
  translate(RETURN, Return);
  translate(SHIFT, Shift_L);
  translate(CONTROL, Control_L);
  translate(MENU, Alt_L);
  translate(PAUSE, Pause);
  translate(CAPITAL, Caps_Lock);
  translate(ESCAPE, Escape);
  translate(SPACE, space);
  translate(PRIOR, Page_Up);
  translate(NEXT, Page_Down);
  translate(END, End);
  translate(HOME, Home);
  translate(LEFT, Left);
  translate(UP, Up);
  translate(RIGHT, Right);
  translate(DOWN, Down);
  translate(SELECT, Select);
  translate(PRINT, Print);
  translate(EXECUTE, Execute);
  translate(SNAPSHOT, Print);
  translate(INSERT, Insert);
  translate(DELETE, Delete);
  translate(HELP, Help);
  translate(MULTIPLY, KP_Multiply);
  translate(ADD, KP_Add);
  translate(SEPARATOR, KP_Separator);
  translate(SUBTRACT, KP_Subtract);
  translate(DECIMAL, KP_Decimal);
  translate(DIVIDE, KP_Divide);
  translate(NUMLOCK, Num_Lock);
  translate(SCROLL, Scroll_Lock);
  translate(LSHIFT, Shift_L);
  translate(RSHIFT, Shift_R);
  translate(LCONTROL, Control_L);
  translate(RCONTROL, Control_R);
  translate(LMENU, Alt_L);
  translate(RMENU, Alt_R);
  translate(OEM_1, semicolon);
  translate(OEM_PLUS, equal);
  translate(OEM_COMMA, comma);
  translate(OEM_MINUS, minus);
  translate(OEM_PERIOD, period);
  translate(OEM_2, slash);
  translate(OEM_3, quoteleft);
  translate(OEM_4, bracketleft);
  translate(OEM_5, backslash);
  translate(OEM_6, bracketright);
  translate(OEM_7, apostrophe);
}
