// SPDX-License-Identifier: GPL-2.0-or-later
/* SPDX-FileCopyrightText: 2004-2015  Simon Wunderlich <sw@simonwunderlich.de>
 */

#include "global.h"
#include <SDL3/SDL.h>
/*  this file reads user input */
/*  this is done right now by SDL-polling */

int user_init_sdl(void)
{
	return 0;
}

int user_main_sdl(void)
{
	SDL_Event event;
	uint32_t unicode = 0;

	while (SDL_PollEvent(&event)) {
		switch (event.type) {
		case SDL_EVENT_MOUSE_MOTION :
			/*    s3dprintf(VLOW,"Current mouse position is: (%d, %d),button %d", event.motion.x, event.motion.y,event.button.button); */
			switch (event.button.button) {
			case SDL_BUTTON_LEFT:
				user_mouse(0, 2, event.motion.x, event.motion.y);
				break;
			case SDL_BUTTON_MIDDLE:
				user_mouse(1, 2, event.motion.x, event.motion.y);
				break;
			case SDL_BUTTON_RIGHT:
			case SDL_BUTTON_RMASK:
				user_mouse(2, 2, event.motion.x, event.motion.y);
				break;
			case 0:
				user_mouse(-1, -1, event.motion.x, event.motion.y);
				break;
				/*  no button ... */
			default:
				s3dprintf(LOW, "don't know button %d", event.button.button);

			}
			break;
		case SDL_EVENT_MOUSE_BUTTON_DOWN :
			switch (event.button.button) {
			case SDL_BUTTON_LEFT:
				user_mouse(0, 0, event.motion.x, event.motion.y);
				break;
			case SDL_BUTTON_MIDDLE:
				user_mouse(1, 0, event.motion.x, event.motion.y);
				break;
			case SDL_BUTTON_RIGHT:
				user_mouse(2, 0, event.motion.x, event.motion.y);
				break;
			default:
				s3dprintf(LOW, "don't know button %d", event.button.button);
			}
			break;
		case SDL_EVENT_MOUSE_BUTTON_UP :
			switch (event.button.button) {
			case SDL_BUTTON_LEFT:
				user_mouse(0, 1, event.motion.x, event.motion.y);
				break;
			case SDL_BUTTON_MIDDLE:
				user_mouse(1, 1, event.motion.x, event.motion.y);
				break;
			case SDL_BUTTON_RIGHT:
				user_mouse(2, 1, event.motion.x, event.motion.y);
				break;
			default:
				s3dprintf(LOW, "don't know button %d", event.button.button);
			}
			break;
		case SDL_EVENT_MOUSE_WHEEL :
			if (event.wheel.y > 0) {
				user_mouse(3, 0, event.motion.x, event.motion.y);
				user_mouse(3, 1, event.motion.x, event.motion.y);
			}
			if (event.wheel.y < 0) {
				user_mouse(4, 0, event.motion.x, event.motion.y);
				user_mouse(4, 1, event.motion.x, event.motion.y);
			}
			break;
		case SDL_EVENT_KEY_DOWN :
		case SDL_EVENT_KEY_UP :
			unicode = 0;
			if (event.key.key < 256) {
				unicode = event.key.key;
				if (unicode >= 'a' && unicode <= 'z') {
					int shifted = !!(event.key.mod & SDL_KMOD_SHIFT);
					int capslock = !!(event.key.mod & SDL_KMOD_CAPS);
					if ((shifted ^ capslock) != 0) {
						unicode = SDL_toupper(unicode);
					}
				}
			}
			user_key(event.key.key, unicode, event.key.mod,
				 (event.type == SDL_EVENT_KEY_UP) ? 1 : 0);
			break;
		case SDL_EVENT_QUIT :
			s3dprintf(HIGH, "SDL_QUIT");
			quit();
			break;
		case SDL_EVENT_WINDOW_RESIZED :
			graphics_reshape(event.window.data1, event.window.data2);
			break;
		case SDL_EVENT_USER :
			s3dprintf(VLOW, "SDL_USEREVENT");
			break;
		case SDL_EVENT_JOYSTICK_AXIS_MOTION :
			s3dprintf(VLOW, "SDL_JOYAXISMOTION");
			break;
		case SDL_EVENT_JOYSTICK_BALL_MOTION :
			s3dprintf(VLOW, "SDL_JOYBALLMOTION");
			break;
		case SDL_EVENT_JOYSTICK_HAT_MOTION :
			s3dprintf(VLOW, "SDL_JOYHATMOTION");
			break;
		case SDL_EVENT_JOYSTICK_BUTTON_DOWN :
			s3dprintf(VLOW, "SDL_JOYBUTTONDOWN");
			break;
		case SDL_EVENT_JOYSTICK_BUTTON_UP :
			s3dprintf(VLOW, "SDL_JOYBUTTONUP");
			break;
		default:
			s3dprintf(MED, "SDL_PollEvent(): unhandled event");
			break;
		}
	}
	return 0;

}

int user_quit_sdl(void)
{
	return 0;
}
