19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/ioctl.h>
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_mouse.h"
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../../events/SDL_events_c.h"
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_cursor_c.h"
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_gsvideo.h"
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_gsmouse_c.h"
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The implementation dependent data for the window manager cursor */
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct WMcursor {
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int unused;
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* There isn't any implementation dependent data */
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid GS_FreeWMCursor(_THIS, WMcursor *cursor)
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return;
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* There isn't any implementation dependent data */
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallWMcursor *GS_CreateWMCursor(_THIS,
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Uint8 *data, Uint8 *mask, int w, int h, int hot_x, int hot_y)
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return((WMcursor *)0x01);
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void GS_MoveCursor(_THIS, SDL_Cursor *cursor, int x, int y)
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Surface *screen;
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct ps2_image image;
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Rect area;
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int mouse_y1, mouse_y2;
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	void *saved_pixels;
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int screen_updated;
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Lock so we don't interrupt an update with mouse motion */
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_LockCursor();
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Make sure any pending DMA has completed */
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( dma_pending ) {
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		ioctl(console_fd, PS2IOC_SENDQCT, 1);
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		dma_pending = 0;
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Remove the cursor image from the DMA area */
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	screen = this->screen;
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	saved_pixels = screen->pixels;
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	screen->pixels = mapped_mem + screen->offset;
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	screen_updated = 0;
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( cursor_drawn ) {
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_EraseCursorNoLock(screen);
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		cursor_drawn = 0;
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		screen_updated = 1;
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Save the current mouse area */
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_MouseRect(&area);
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mouse_y1 = area.y;
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mouse_y2 = area.y+area.h;
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Only draw the new cursor if there was one passed in */
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( cursor ) {
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Set the new location */
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		cursor->area.x = (x - cursor->hot_x);
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		cursor->area.y = (y - cursor->hot_y);
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Draw the cursor at the new location */
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( (SDL_cursorstate & CURSOR_VISIBLE) && screen->pixels ) {
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_DrawCursorNoLock(screen);
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			cursor_drawn = 1;
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			screen_updated = 1;
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	screen->pixels = saved_pixels;
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Update the affected area of the screen */
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( screen_updated ) {
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_MouseRect(&area);
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( area.y < mouse_y1 ) {
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			mouse_y1 = area.y;
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( (area.y+area.h) > mouse_y2 ) {
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			mouse_y2 = area.y+area.h;
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		image = screen_image;
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		image.y += screen->offset / screen->pitch + mouse_y1;
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		image.h = mouse_y2 - mouse_y1;
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		image.ptr = mapped_mem +
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		            (image.y - screen_image.y) * screen->pitch;
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		ioctl(console_fd, PS2IOC_LOADIMAGE, &image);
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Need to scale offscreen image to TV output */
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( image.y > 0 ) {
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			scaleimage_nonblock(console_fd,
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			                    tex_tags_mem, scale_tags_mem);
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We're finished */
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_UnlockCursor();
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid GS_MoveWMCursor(_THIS, int x, int y)
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GS_MoveCursor(this, SDL_cursor, x, y);
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint GS_ShowWMCursor(_THIS, WMcursor *wmcursor)
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Cursor *cursor;
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int x, y;
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Draw the cursor at the appropriate location */
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_GetMouseState(&x, &y);
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( wmcursor ) {
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		cursor = SDL_cursor;
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		cursor = NULL;
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GS_MoveCursor(this, cursor, x, y);
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(1);
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
147