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/*
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MiNT /dev/mouse driver
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Patrice Mandin
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <fcntl.h>
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <unistd.h>
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../../events/SDL_events_c.h"
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_ataridevmouse_c.h"
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Defines */
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define DEVICE_NAME	"/dev/mouse"
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Local variables */
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int handle = -1;
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int mouseb, prev_mouseb;
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Functions */
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_AtariDevMouse_Open(void)
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int r;
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const char *mousedev;
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/*
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		TODO: Fix the MiNT device driver, that locks mouse for other
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		applications, so this is disabled till fixed
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 */
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0;
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* First, try SDL_MOUSEDEV device */
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mousedev = SDL_getenv("SDL_MOUSEDEV");
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (!mousedev) {
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		handle = open(mousedev, 0);
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Failed, try default device */
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (handle<0) {
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		handle = open(DEVICE_NAME, 0);
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (handle<0) {
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		handle = -1;
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 0;
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set non blocking mode */
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	r = fcntl(handle, F_GETFL, 0);
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (r<0) {
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		close(handle);
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		handle = -1;
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 0;
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	r |= O_NDELAY;
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	r = fcntl(handle, F_SETFL, r);
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (r<0) {
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		close(handle);
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		handle = -1;
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 0;
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	prev_mouseb = 7;
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 1;
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_AtariDevMouse_Close(void)
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (handle>0) {
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		close(handle);
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		handle = -1;
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int atari_GetButton(int button)
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch(button)
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 0:
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return SDL_BUTTON_RIGHT;
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 1:
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return SDL_BUTTON_MIDDLE;
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		default:
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return SDL_BUTTON_LEFT;
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_AtariDevMouse_PostMouseEvents(_THIS, SDL_bool buttonEvents)
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	unsigned char buffer[3];
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int mousex, mousey;
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (handle<0) {
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return;
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mousex = mousey = 0;
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while (read(handle, buffer, sizeof(buffer))==sizeof(buffer)) {
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mouseb = buffer[0] & 7;
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mousex += (char) buffer[1];
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mousey += (char) buffer[2];
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Mouse button events */
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (buttonEvents && (mouseb != prev_mouseb)) {
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			int i;
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for (i=0;i<3;i++) {
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				int curbutton, prevbutton;
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				curbutton = mouseb & (1<<i);
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				prevbutton = prev_mouseb & (1<<i);
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if (curbutton && !prevbutton) {
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					SDL_PrivateMouseButton(SDL_RELEASED, atari_GetButton(i), 0, 0);
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if (!curbutton && prevbutton) {
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					SDL_PrivateMouseButton(SDL_PRESSED, atari_GetButton(i), 0, 0);
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			prev_mouseb = mouseb;
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Mouse motion event */
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (mousex || mousey) {
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_PrivateMouseMotion(0, 1, mousex, -mousey);
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
160