src/video/ataricommon/SDL_ataridevmouse.c
changeset 1420 2405517b5eab
child 1662 782fd950bd46
child 1895 c121d94672cb
child 4159 a1b03ba2fcd0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/ataricommon/SDL_ataridevmouse.c	Thu Feb 23 21:51:10 2006 +0000
     1.3 @@ -0,0 +1,159 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2006 Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Lesser General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2.1 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Lesser General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Lesser General Public
    1.19 +    License along with this library; if not, write to the Free Software
    1.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +#include "SDL_config.h"
    1.26 +
    1.27 +/*
    1.28 +	MiNT /dev/mouse driver
    1.29 +
    1.30 +	Patrice Mandin
    1.31 +*/
    1.32 +
    1.33 +#include <fcntl.h>
    1.34 +#include <unistd.h>
    1.35 +
    1.36 +#include "../../events/SDL_events_c.h"
    1.37 +#include "SDL_ataridevmouse_c.h"
    1.38 +
    1.39 +/* Defines */
    1.40 +
    1.41 +#define DEVICE_NAME	"/dev/mouse"
    1.42 +
    1.43 +/* Local variables */
    1.44 +
    1.45 +static int handle = -1;
    1.46 +static int mouseb, prev_mouseb;
    1.47 +
    1.48 +/* Functions */
    1.49 +
    1.50 +int SDL_AtariDevMouse_Open(void)
    1.51 +{
    1.52 +	int r;
    1.53 +	const char *mousedev;
    1.54 +
    1.55 +	/*
    1.56 +		TODO: Fix the MiNT device driver, that locks mouse for other
    1.57 +		applications, so this is disabled till fixed
    1.58 +	 */
    1.59 +	return 0;
    1.60 +
    1.61 +	/* First, try SDL_MOUSEDEV device */
    1.62 +	mousedev = SDL_getenv("SDL_MOUSEDEV");
    1.63 +	if (!mousedev) {
    1.64 +		handle = open(mousedev, 0);
    1.65 +	}
    1.66 +
    1.67 +	/* Failed, try default device */
    1.68 +	if (handle<0) {
    1.69 +		handle = open(DEVICE_NAME, 0);
    1.70 +	}
    1.71 +
    1.72 +	if (handle<0) {
    1.73 +		handle = -1;
    1.74 +		return 0;
    1.75 +	}
    1.76 +
    1.77 +	/* Set non blocking mode */
    1.78 +	r = fcntl(handle, F_GETFL, 0);
    1.79 +	if (r<0) {
    1.80 +		close(handle);
    1.81 +		handle = -1;
    1.82 +		return 0;
    1.83 +	}
    1.84 +
    1.85 +	r |= O_NDELAY;
    1.86 +
    1.87 +	r = fcntl(handle, F_SETFL, r);
    1.88 +	if (r<0) {
    1.89 +		close(handle);
    1.90 +		handle = -1;
    1.91 +		return 0;
    1.92 +	}
    1.93 +
    1.94 +	prev_mouseb = 7;
    1.95 +	return 1;
    1.96 +}
    1.97 +
    1.98 +void SDL_AtariDevMouse_Close(void)
    1.99 +{
   1.100 +	if (handle>0) {
   1.101 +		close(handle);
   1.102 +		handle = -1;
   1.103 +	}
   1.104 +}
   1.105 +
   1.106 +static int atari_GetButton(int button)
   1.107 +{
   1.108 +	switch(button)
   1.109 +	{
   1.110 +		case 0:
   1.111 +			return SDL_BUTTON_RIGHT;
   1.112 +		case 1:
   1.113 +			return SDL_BUTTON_MIDDLE;
   1.114 +		default:
   1.115 +			break;
   1.116 +	}
   1.117 +
   1.118 +	return SDL_BUTTON_LEFT;
   1.119 +}
   1.120 +
   1.121 +void SDL_AtariDevMouse_PostMouseEvents(_THIS, SDL_bool buttonEvents)
   1.122 +{
   1.123 +	unsigned char buffer[3];
   1.124 +	int mousex, mousey;
   1.125 +
   1.126 +	if (handle<0) {
   1.127 +		return;
   1.128 +	}
   1.129 +
   1.130 +	mousex = mousey = 0;
   1.131 +	while (read(handle, buffer, sizeof(buffer))==sizeof(buffer)) {
   1.132 +		mouseb = buffer[0] & 7;
   1.133 +		mousex += (char) buffer[1];
   1.134 +		mousey += (char) buffer[2];
   1.135 +
   1.136 +		/* Mouse button events */
   1.137 +		if (buttonEvents && (mouseb != prev_mouseb)) {
   1.138 +			int i;
   1.139 +
   1.140 +			for (i=0;i<3;i++) {
   1.141 +				int curbutton, prevbutton;
   1.142 +
   1.143 +				curbutton = mouseb & (1<<i);
   1.144 +				prevbutton = prev_mouseb & (1<<i);
   1.145 +			
   1.146 +				if (curbutton && !prevbutton) {
   1.147 +					SDL_PrivateMouseButton(SDL_RELEASED, atari_GetButton(i), 0, 0);
   1.148 +				}
   1.149 +				if (!curbutton && prevbutton) {
   1.150 +					SDL_PrivateMouseButton(SDL_PRESSED, atari_GetButton(i), 0, 0);
   1.151 +				}
   1.152 +			}
   1.153 +
   1.154 +			prev_mouseb = mouseb;
   1.155 +		}
   1.156 +	}
   1.157 +
   1.158 +	/* Mouse motion event */
   1.159 +	if (mousex || mousey) {
   1.160 +		SDL_PrivateMouseMotion(0, 1, mousex, -mousey);
   1.161 +	}
   1.162 +}