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