Fixed possible memory corruption in commands exec and netexec; fixed command netcp...
[ZeXOS.git] / libx / cursor / xcursor.c
blob44ee60138edc142c8c85324cb3f7aaddc7b7ec34
1 /*
2 * ZeX/OS
3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <stdio.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include <libx/base.h>
26 #include <libx/cursor.h>
28 /* kb mouse */
29 #define ARROWLEFT 75
30 #define ARROWRIGHT 77
31 #define ARROWUP 72
32 #define ARROWDOWN 80
33 #define ENTER 28
34 #define DEL 14
36 static unsigned mouse_accel = 0;
38 /* ps2 mouse */
39 #define MOUSE_FLAG_BUTTON1 0x1 /* Left mouse button */
40 #define MOUSE_FLAG_BUTTON2 0x2 /* Right mouse button */
41 #define MOUSE_FLAG_BUTTON3 0x4 /* Middle mouse button */
42 #define MOUSE_FLAG_BUTTON4 0x8 /* Extra mouse button */
43 #define MOUSE_FLAG_BUTTON5 0x10 /* Extra mouse button */
44 #define MOUSE_FLAG_SCROLLUP 0x20 /* Scroll button/wheel - step up */
45 #define MOUSE_FLAG_SCROLLDOWN 0x40 /* Scroll button/wheel - step down */
47 static int ps2_fd;
48 static int com_fd;
50 static int cursor_x;
51 static int cursor_y;
52 static int cursor_state;
54 static char mouse_type = -1;
57 static unsigned xcursor_commouse_init ()
59 com_fd = open ("/dev/mousecom", O_RDONLY);
61 if (com_fd < 1)
62 return 0;
64 return 1;
67 static unsigned xcursor_ps2mouse_init ()
69 ps2_fd = open ("/dev/mouseps2", O_RDONLY);
71 if (ps2_fd < 1)
72 return 0;
74 return 1;
77 unsigned xcursor_init ()
79 /* initial cursor position */
80 cursor_x = 0;
81 cursor_y = 0;
83 /* automatic mouse type selection */
84 if (mouse_type != -1)
85 goto manual;
87 if (xcursor_ps2mouse_init ())
88 mouse_type = 0x2;
89 else if (xcursor_commouse_init ())
90 mouse_type = 0x1;
91 else
92 mouse_type = 0x0;
94 return 1;
95 manual:
96 switch (mouse_type) {
97 case 0x0:
98 mouse_accel = 1;
99 return 1;
100 case 0x1:
101 /* serial mouse over rs232 */
102 return xcursor_commouse_init ();
103 case 0x2:
104 /* ps/2 mouse */
105 return xcursor_ps2mouse_init ();
108 return 0;
111 static void xcursor_ps2mouse_handler ()
113 /* low-level mouse structure */
114 typedef struct {
115 unsigned short flags;
116 short pos_x; /* Difference - horizontal position */
117 short pos_y; /* Difference - vertical position */
118 } dev_mouse_t;
120 dev_mouse_t mouse;
122 int r = read (ps2_fd, &mouse, sizeof (dev_mouse_t));
124 if (!r)
125 return;
127 lseek (ps2_fd, 0, SEEK_SET);
129 cursor_x += mouse.pos_x;
130 cursor_y += mouse.pos_y;
132 if (mouse.flags & MOUSE_FLAG_BUTTON2)
133 cursor_state = XCURSOR_STATE_RBUTTON;
134 else if (mouse.flags & MOUSE_FLAG_BUTTON1)
135 cursor_state = XCURSOR_STATE_LBUTTON;
136 else
137 cursor_state = 0x0;
140 static void xcursor_commouse_handler ()
142 /* low-level mouse structure */
143 typedef struct {
144 unsigned short flags;
145 short pos_x; /* Difference - horizontal position */
146 short pos_y; /* Difference - vertical position */
147 } dev_mouse_t;
149 dev_mouse_t mouse;
151 int r = read (com_fd, &mouse, sizeof (dev_mouse_t));
153 if (!r)
154 return;
156 lseek (com_fd, 0, SEEK_SET);
158 cursor_x += (int) mouse.pos_x;
159 cursor_y += (int) mouse.pos_y;
161 if (mouse.flags & MOUSE_FLAG_BUTTON2)
162 cursor_state = XCURSOR_STATE_RBUTTON;
163 else if (mouse.flags & MOUSE_FLAG_BUTTON1)
164 cursor_state = XCURSOR_STATE_LBUTTON;
165 else
166 cursor_state = 0x0;
169 static void xcursor_kbmouse_handler ()
171 int scancode = getkey ();
173 switch (scancode) {
174 case ARROWLEFT:
175 mouse_accel ++;
176 cursor_x -= mouse_accel;
177 return;
178 case ARROWRIGHT:
179 mouse_accel ++;
180 cursor_x += mouse_accel;
181 return;
182 case ARROWUP:
183 mouse_accel ++;
184 cursor_y -= mouse_accel;
185 return;
186 case ARROWDOWN:
187 mouse_accel ++;
188 cursor_y += mouse_accel;
189 return;
190 case DEL:
191 cursor_state = XCURSOR_STATE_RBUTTON;
192 return;
193 case ENTER:
194 cursor_state = XCURSOR_STATE_LBUTTON;
195 return;
198 mouse_accel = 4;
200 cursor_state = 0x0;
203 int xcursor_update ()
205 switch (mouse_type) {
206 case 0x0:
207 /* TODO: autodetect */
208 xcursor_kbmouse_handler ();
209 break;
210 case 0x1:
211 /* serial mouse over rs232 */
212 xcursor_commouse_handler ();
213 break;
214 case 0x2:
215 /* ps/2 mouse */
216 xcursor_ps2mouse_handler ();
217 break;
220 if (cursor_y < 0)
221 cursor_y = 0;
223 if (cursor_y > (signed) (vgafb_res_y-1))
224 cursor_y = (vgafb_res_y-1);
226 if (cursor_x < 0)
227 cursor_x = 0;
229 if (cursor_x > (signed) (vgafb_res_x-1))
230 cursor_x = (vgafb_res_x-1);
232 return cursor_state;
235 void xcursor_getpos (int *x, int *y)
237 *x = cursor_x;
238 *y = cursor_y;
241 unsigned xcursor_settype (unsigned char type)
243 if (mouse_type == type)
244 return 0;
246 mouse_type = type;
248 return 1;