/*
Copyright (C) 1997-2001 Id Software, Inc.

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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
// keyin.c

#ifdef GCCBROKENRELATIVEPATHS
#include "../../client/client.h"
#else
#include "../client/client.h"
#endif

#include "kernel.h"
#include "swis.h"

#include "q2swis.h"

extern unsigned sys_frame_time;
extern int gutils_handle;

#define K_KP_0 K_KP_INS
#define K_KP_1 K_KP_END
#define K_KP_2 K_KP_DOWNARROW
#define K_KP_3 K_KP_PGDN
#define K_KP_4 K_KP_LEFTARROW
//#define K_KP_5
#define K_KP_6 K_KP_RIGHTARROW
#define K_KP_7 K_KP_HOME
#define K_KP_8 K_KP_UPARROW
#define K_KP_9 K_KP_PGUP

#define K_SCROLLLOCK 0
#define K_NUMLOCK    0
#define K_CAPSLOCK   0
#define K_PRINT      0

#define K_MINUS  '-'
#define K_EQUALS '='

#define MAXKEYS 128
byte keytable[MAXKEYS] =
{
  K_ESCAPE,     K_F1,         K_F2,          K_F3,          K_F4,           K_F5,           K_F6,           K_F7,
  K_F8,         K_F9,         K_F10,         K_F11,         K_F12,          K_PRINT,        K_SCROLLLOCK,   K_PAUSE,
  '`',          '1',          '2',           '3',           '4',            '5',            '6',            '7',
  '8',          '9',          '0',           K_MINUS,       K_EQUALS,       '',            K_BACKSPACE,    K_INS,
  K_HOME,       K_PGUP,       K_NUMLOCK,     K_KP_SLASH,    '*',            '#',            K_TAB,          'q',
  'w',          'e',          'r',           't',           'y',            'u',            'i',            'o',
  'p',          '[',          ']',           '\\',          K_DEL,          K_END,          K_PGDN,         K_KP_7,
  K_KP_8,       K_KP_9,       K_KP_MINUS,    K_CTRL,        'a',            's',            'd',            'f',
  'g',          'h',          'j',           'k',           'l',            ';',            '\'',           K_ENTER,
  K_KP_4,       K_KP_5,       K_KP_6,        K_KP_PLUS,     K_SHIFT,        0,/*xtra*/      'z',            'x',
  'c',          'v',          'b',           'n',           'm',            ',',            '.',            '/',
  K_SHIFT,      K_UPARROW,    K_KP_1,        K_KP_2,        K_KP_3,         K_CAPSLOCK,     K_ALT,          K_SPACE,
  K_ALT,        K_CTRL,       K_LEFTARROW,   K_DOWNARROW,   K_RIGHTARROW,   K_KP_0,         K_KP_DEL,       K_ENTER,
  K_MOUSE1,     K_MOUSE2,     K_MOUSE3,      0,             0,              0,              0,              0,
  0,            0,            0,             0,             0,              0,              0,              0,
  0,            0,            0,             0,             0,              0,              0,              0
};

#define MOUSE_SELECT 112
#define MOUSE_MENU   113
#define MOUSE_ADJUST 114

void Sys_SendKeyEvents (void)
{
#ifdef USE_GAMEUTILS
    _kernel_oserror * e;
    int keyno, trans;
    int count = 10;

    sys_frame_time = Sys_Milliseconds();

    while(count--)
    {
        e = _swix(GameUtils_Keypressed, _IN(0) | _OUTR(0, 1), gutils_handle, &keyno, &trans);

        if (e || keyno == -1)
        {
            break;
        }

        if (keyno < MAXKEYS)
        {
            int transkey = keytable[keyno];

            if (transkey)
            {
                Key_Event(transkey, trans == 1 ? true : false, Sys_Milliseconds());
            }
        }
    }
#endif
}
