/*
  Copyright (C) 2010-2011  Charles Pence <charles@charlespence.net>
  Copyright (C) 1996-1997  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 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 "pch.h"
#include "quakedef.h"

Uint8 old_buttons;

/*
===========
Force_CenterView_f
===========
*/
void Force_CenterView_f (void)
{
    cl.viewangles[PITCH] = 0;
}


/*
===========
IN_ShowMouse
===========
*/
void IN_ShowMouse (void)
{
    SDL_WM_GrabInput(SDL_GRAB_OFF);
    SDL_ShowCursor(SDL_ENABLE);
}


/*
===========
IN_HideMouse
===========
*/
void IN_HideMouse (void)
{
    SDL_ShowCursor(SDL_DISABLE);
    SDL_WM_GrabInput(SDL_GRAB_ON);
}

/*
===========
IN_Init
===========
*/
void IN_Init (void)
{
    Cmd_AddCommand ("force_centerview", Force_CenterView_f);
}

/*
===========
IN_Shutdown
===========
*/
void IN_Shutdown (void)
{
    IN_ShowMouse ();
}

int MapButtons(int sdl_button)
{
    switch (sdl_button)
    {
    case SDL_BUTTON_LEFT:
        return K_MOUSE1;
    case SDL_BUTTON_RIGHT:
        return K_MOUSE2;
    case SDL_BUTTON_MIDDLE:
        return K_MOUSE3;
    case SDL_BUTTON_X1:
        return K_MOUSE4;
    case SDL_BUTTON_X2:
        return K_MOUSE5;
    case SDL_BUTTON_WHEELUP:
        return K_MWHEELUP;
    case SDL_BUTTON_WHEELDOWN:
        return K_MWHEELDOWN;
    default:
        return -1;
    }
}


/*
===========
IN_MouseMove
===========
*/
void IN_MouseMove (usercmd_t *cmd)
{
    int i, x, y;
    Uint8 buttons;

    buttons = SDL_GetRelativeMouseState(&x, &y);

    for (i = SDL_BUTTON_LEFT ; i <= SDL_BUTTON_X2 ; i++)
    {
        int key = MapButtons(i);
        if (key == -1)
            continue;

        if ((buttons & SDL_BUTTON(i)) && !(old_buttons & SDL_BUTTON(i)))
            Key_Event(key, true);
        if (!(buttons & SDL_BUTTON(i)) && (old_buttons & SDL_BUTTON(i)))
            Key_Event(key, false);
    }

    old_buttons = buttons;

    x *= Cvar_VariableValue("sensitivity");
    y *= Cvar_VariableValue("sensitivity");

// add mouse X/Y movement to cmd
    if ((in_strafe.state & 1) || (Cvar_VariableValue("lookstrafe") && (in_mlook.state & 1)))
        cmd->sidemove += Cvar_VariableValue("m_side") * x;
    else
        cl.viewangles[YAW] -= Cvar_VariableValue("m_yaw") * x;

    if (in_mlook.state & 1)
        V_StopPitchDrift ();

    if ((in_mlook.state & 1) && !(in_strafe.state & 1))
    {
        cl.viewangles[PITCH] += Cvar_VariableValue("m_pitch") * y;
        if (cl.viewangles[PITCH] > 80)
            cl.viewangles[PITCH] = 80;
        if (cl.viewangles[PITCH] < -70)
            cl.viewangles[PITCH] = -70;
    }
    else
    {
        if ((in_strafe.state & 1) && noclip_anglehack)
            cmd->upmove -= Cvar_VariableValue("m_forward") * y;
        else
            cmd->forwardmove -= Cvar_VariableValue("m_forward") * y;
    }
}


/*
===========
IN_Move
===========
*/
void IN_Move (usercmd_t *cmd)
{
    IN_MouseMove (cmd);
}


/*
===================
IN_ClearStates
===================
*/
void IN_ClearStates (void)
{
    old_buttons = 0;
}

