src/video/ataricommon/SDL_ataridevmouse.c
author Patrice Mandin
Sun, 30 Apr 2006 21:01:25 +0000
changeset 1751 a11c13151840
parent 1420 2405517b5eab
child 1662 782fd950bd46
child 1895 c121d94672cb
child 4159 a1b03ba2fcd0
permissions -rw-r--r--
Declare volatile variables modified by interrupt routines
patmandin@1420
     1
/*
patmandin@1420
     2
    SDL - Simple DirectMedia Layer
patmandin@1420
     3
    Copyright (C) 1997-2006 Sam Lantinga
patmandin@1420
     4
patmandin@1420
     5
    This library is free software; you can redistribute it and/or
patmandin@1420
     6
    modify it under the terms of the GNU Lesser General Public
patmandin@1420
     7
    License as published by the Free Software Foundation; either
patmandin@1420
     8
    version 2.1 of the License, or (at your option) any later version.
patmandin@1420
     9
patmandin@1420
    10
    This library is distributed in the hope that it will be useful,
patmandin@1420
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
patmandin@1420
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
patmandin@1420
    13
    Lesser General Public License for more details.
patmandin@1420
    14
patmandin@1420
    15
    You should have received a copy of the GNU Lesser General Public
patmandin@1420
    16
    License along with this library; if not, write to the Free Software
patmandin@1420
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
patmandin@1420
    18
patmandin@1420
    19
    Sam Lantinga
patmandin@1420
    20
    slouken@libsdl.org
patmandin@1420
    21
*/
patmandin@1420
    22
#include "SDL_config.h"
patmandin@1420
    23
patmandin@1420
    24
/*
patmandin@1420
    25
	MiNT /dev/mouse driver
patmandin@1420
    26
patmandin@1420
    27
	Patrice Mandin
patmandin@1420
    28
*/
patmandin@1420
    29
patmandin@1420
    30
#include <fcntl.h>
patmandin@1420
    31
#include <unistd.h>
patmandin@1420
    32
patmandin@1420
    33
#include "../../events/SDL_events_c.h"
patmandin@1420
    34
#include "SDL_ataridevmouse_c.h"
patmandin@1420
    35
patmandin@1420
    36
/* Defines */
patmandin@1420
    37
patmandin@1420
    38
#define DEVICE_NAME	"/dev/mouse"
patmandin@1420
    39
patmandin@1420
    40
/* Local variables */
patmandin@1420
    41
patmandin@1420
    42
static int handle = -1;
patmandin@1420
    43
static int mouseb, prev_mouseb;
patmandin@1420
    44
patmandin@1420
    45
/* Functions */
patmandin@1420
    46
patmandin@1420
    47
int SDL_AtariDevMouse_Open(void)
patmandin@1420
    48
{
patmandin@1420
    49
	int r;
patmandin@1420
    50
	const char *mousedev;
patmandin@1420
    51
patmandin@1420
    52
	/*
patmandin@1420
    53
		TODO: Fix the MiNT device driver, that locks mouse for other
patmandin@1420
    54
		applications, so this is disabled till fixed
patmandin@1420
    55
	 */
patmandin@1420
    56
	return 0;
patmandin@1420
    57
patmandin@1420
    58
	/* First, try SDL_MOUSEDEV device */
patmandin@1420
    59
	mousedev = SDL_getenv("SDL_MOUSEDEV");
patmandin@1420
    60
	if (!mousedev) {
patmandin@1420
    61
		handle = open(mousedev, 0);
patmandin@1420
    62
	}
patmandin@1420
    63
patmandin@1420
    64
	/* Failed, try default device */
patmandin@1420
    65
	if (handle<0) {
patmandin@1420
    66
		handle = open(DEVICE_NAME, 0);
patmandin@1420
    67
	}
patmandin@1420
    68
patmandin@1420
    69
	if (handle<0) {
patmandin@1420
    70
		handle = -1;
patmandin@1420
    71
		return 0;
patmandin@1420
    72
	}
patmandin@1420
    73
patmandin@1420
    74
	/* Set non blocking mode */
patmandin@1420
    75
	r = fcntl(handle, F_GETFL, 0);
patmandin@1420
    76
	if (r<0) {
patmandin@1420
    77
		close(handle);
patmandin@1420
    78
		handle = -1;
patmandin@1420
    79
		return 0;
patmandin@1420
    80
	}
patmandin@1420
    81
patmandin@1420
    82
	r |= O_NDELAY;
patmandin@1420
    83
patmandin@1420
    84
	r = fcntl(handle, F_SETFL, r);
patmandin@1420
    85
	if (r<0) {
patmandin@1420
    86
		close(handle);
patmandin@1420
    87
		handle = -1;
patmandin@1420
    88
		return 0;
patmandin@1420
    89
	}
patmandin@1420
    90
patmandin@1420
    91
	prev_mouseb = 7;
patmandin@1420
    92
	return 1;
patmandin@1420
    93
}
patmandin@1420
    94
patmandin@1420
    95
void SDL_AtariDevMouse_Close(void)
patmandin@1420
    96
{
patmandin@1420
    97
	if (handle>0) {
patmandin@1420
    98
		close(handle);
patmandin@1420
    99
		handle = -1;
patmandin@1420
   100
	}
patmandin@1420
   101
}
patmandin@1420
   102
patmandin@1420
   103
static int atari_GetButton(int button)
patmandin@1420
   104
{
patmandin@1420
   105
	switch(button)
patmandin@1420
   106
	{
patmandin@1420
   107
		case 0:
patmandin@1420
   108
			return SDL_BUTTON_RIGHT;
patmandin@1420
   109
		case 1:
patmandin@1420
   110
			return SDL_BUTTON_MIDDLE;
patmandin@1420
   111
		default:
patmandin@1420
   112
			break;
patmandin@1420
   113
	}
patmandin@1420
   114
patmandin@1420
   115
	return SDL_BUTTON_LEFT;
patmandin@1420
   116
}
patmandin@1420
   117
patmandin@1420
   118
void SDL_AtariDevMouse_PostMouseEvents(_THIS, SDL_bool buttonEvents)
patmandin@1420
   119
{
patmandin@1420
   120
	unsigned char buffer[3];
patmandin@1420
   121
	int mousex, mousey;
patmandin@1420
   122
patmandin@1420
   123
	if (handle<0) {
patmandin@1420
   124
		return;
patmandin@1420
   125
	}
patmandin@1420
   126
patmandin@1420
   127
	mousex = mousey = 0;
patmandin@1420
   128
	while (read(handle, buffer, sizeof(buffer))==sizeof(buffer)) {
patmandin@1420
   129
		mouseb = buffer[0] & 7;
patmandin@1420
   130
		mousex += (char) buffer[1];
patmandin@1420
   131
		mousey += (char) buffer[2];
patmandin@1420
   132
patmandin@1420
   133
		/* Mouse button events */
patmandin@1420
   134
		if (buttonEvents && (mouseb != prev_mouseb)) {
patmandin@1420
   135
			int i;
patmandin@1420
   136
patmandin@1420
   137
			for (i=0;i<3;i++) {
patmandin@1420
   138
				int curbutton, prevbutton;
patmandin@1420
   139
patmandin@1420
   140
				curbutton = mouseb & (1<<i);
patmandin@1420
   141
				prevbutton = prev_mouseb & (1<<i);
patmandin@1420
   142
			
patmandin@1420
   143
				if (curbutton && !prevbutton) {
patmandin@1420
   144
					SDL_PrivateMouseButton(SDL_RELEASED, atari_GetButton(i), 0, 0);
patmandin@1420
   145
				}
patmandin@1420
   146
				if (!curbutton && prevbutton) {
patmandin@1420
   147
					SDL_PrivateMouseButton(SDL_PRESSED, atari_GetButton(i), 0, 0);
patmandin@1420
   148
				}
patmandin@1420
   149
			}
patmandin@1420
   150
patmandin@1420
   151
			prev_mouseb = mouseb;
patmandin@1420
   152
		}
patmandin@1420
   153
	}
patmandin@1420
   154
patmandin@1420
   155
	/* Mouse motion event */
patmandin@1420
   156
	if (mousex || mousey) {
patmandin@1420
   157
		SDL_PrivateMouseMotion(0, 1, mousex, -mousey);
patmandin@1420
   158
	}
patmandin@1420
   159
}