/*
    SDL - Simple DirectMedia Layer
    Copyright (C) 1997-2012 Sam Lantinga

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    Sam Lantinga
    slouken@libsdl.org
*/
#include "SDL_config.h"

#include "SDL.h"
#include "../../events/SDL_sysevents.h"
#include "../../events/SDL_events_c.h"

#include "SDL_quinnvideo.h"
#include "SDL_quinnevents_c.h"

#include "quinn.h"


static const int sdl_keyset[] = {
	0, 		SDLK_ESCAPE, 	SDLK_1, 	SDLK_2,
	SDLK_3, 	SDLK_4, 	SDLK_5, 	SDLK_6,
	SDLK_7, 	SDLK_8, 	SDLK_9, 	SDLK_0,
	SDLK_MINUS, 	SDLK_EQUALS, 	SDLK_BACKSPACE, 	SDLK_TAB,
	SDLK_q, 	SDLK_w, 	SDLK_e, 	SDLK_r,
	SDLK_t, 	SDLK_y, SDLK_u, 	SDLK_i,
	SDLK_o, SDLK_p, 	SDLK_LEFTBRACKET, 	SDLK_RIGHTBRACKET,
	SDLK_RETURN, SDLK_LCTRL, 		SDLK_a, 	SDLK_s,
	SDLK_d, SDLK_f,	SDLK_g,	SDLK_h,
	SDLK_j, SDLK_k, SDLK_l, SDLK_SEMICOLON,
	SDLK_QUOTE, SDLK_BACKQUOTE, 	SDLK_LSHIFT, 	SDLK_BACKSLASH,
	SDLK_z, SDLK_x, SDLK_c, SDLK_v,
	SDLK_b, SDLK_n,	SDLK_m,	SDLK_COMMA,
	SDLK_PERIOD, SDLK_SLASH,	SDLK_RSHIFT,		0,
	SDLK_LALT, SDLK_SPACE, SDLK_CAPSLOCK, SDLK_F1,
	SDLK_F2, SDLK_F3, SDLK_F4, SDLK_F5,
	SDLK_F6, SDLK_F7, SDLK_F8, SDLK_F9,
	SDLK_F10, 0, 0, SDLK_HOME,
	SDLK_UP, SDLK_PAGEUP, 0, SDLK_LEFT,
	0, SDLK_RIGHT, 0, SDLK_END,
	SDLK_DOWN, SDLK_PAGEDOWN, SDLK_INSERT, SDLK_DELETE,
	0, 0, 0, SDLK_F11, SDLK_F12
};

static SDLKey keymap[256];
static SDLMod modstate = KMOD_NONE;

int mouse_state = 0;

static SDLMod make_mod(char *keycodes) {
	SDLMod ret = KMOD_NONE;

	if (keycodes[29]) ret |= KMOD_LCTRL;
	if (keycodes[42]) ret |= KMOD_LSHIFT;
	if (keycodes[56]) ret |= KMOD_LALT;
	if (keycodes[54]) ret |= KMOD_RSHIFT;

	return ret;
}

static struct window_update_req current_req;
static int current_req_current = 0;

static int _lmstate = 0;
static int _rmstate = 0;
static int _mmstate = 0;

static char _k_last_state[256];



void QUINN_recv_event(struct window_update_req *req) {
	memcpy(&current_req, req, sizeof(struct window_update_req));
	current_req_current = 1;
}


void QUINN_PumpEvents(_THIS) {
	int i;

	if (current_req_current) {
		if (current_req.lclick != _lmstate) {
			if (current_req.lclick) {
				SDL_PrivateMouseButton(SDL_PRESSED, 1, current_req.mousex, current_req.mousey);
			} else {
				SDL_PrivateMouseButton(SDL_RELEASED, 1, current_req.mousex, current_req.mousey);
			}
		}

		if (current_req.rclick != _rmstate) {
			if (current_req.rclick) {
				SDL_PrivateMouseButton(SDL_PRESSED, 3, current_req.mousex, current_req.mousey);
			} else {
				SDL_PrivateMouseButton(SDL_RELEASED, 3, current_req.mousex, current_req.mousey);
			}
		}

		if (current_req.mclick != _mmstate) {
			if (current_req.mclick) {
				SDL_PrivateMouseButton(SDL_PRESSED, 2, current_req.mousex, current_req.mousey);
			} else {
				SDL_PrivateMouseButton(SDL_RELEASED, 2, current_req.mousex, current_req.mousey);
			}
		}

		_lmstate = current_req.lclick;
		_rmstate = current_req.rclick;
		_mmstate = current_req.mclick;

		SDL_PrivateMouseMotion(0, 0, current_req.mousex, current_req.mousey);

		SDL_keysym keysym;

		for (i=0;i<88;i++) {
			if (_k_last_state[i] != current_req.key_codes[i]) {
				keysym.scancode = i;
				keysym.unicode = 0;
				keysym.mod = make_mod(current_req.key_codes);
				if (current_req.key_codes[i] == 1) {
					// key down
					keysym.sym = sdl_keyset[i];
					SDL_PrivateKeyboard(SDL_PRESSED, &keysym);
				} else {
					// key up
					keysym.sym = sdl_keyset[i];
					SDL_PrivateKeyboard(SDL_RELEASED, &keysym);
				}
			}
			_k_last_state[i] = current_req.key_codes[i];
		}

		current_req_current = 0;
	}
}

void QUINN_InitOSKeymap(_THIS)
{
	/* do nothing. */
}

/* end of SDL_nullevents.c ... */
