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/>.
25 #include <libx/base.h>
26 #include <libx/cursor.h>
36 static unsigned mouse_accel
= 0;
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 */
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
);
67 static unsigned xcursor_ps2mouse_init ()
69 ps2_fd
= open ("/dev/mouseps2", O_RDONLY
);
77 unsigned xcursor_init ()
79 /* initial cursor position */
83 /* automatic mouse type selection */
87 if (xcursor_ps2mouse_init ())
89 else if (xcursor_commouse_init ())
101 /* serial mouse over rs232 */
102 return xcursor_commouse_init ();
105 return xcursor_ps2mouse_init ();
111 static void xcursor_ps2mouse_handler ()
113 /* low-level mouse structure */
115 unsigned short flags
;
116 short pos_x
; /* Difference - horizontal position */
117 short pos_y
; /* Difference - vertical position */
122 int r
= read (ps2_fd
, &mouse
, sizeof (dev_mouse_t
));
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
;
140 static void xcursor_commouse_handler ()
142 /* low-level mouse structure */
144 unsigned short flags
;
145 short pos_x
; /* Difference - horizontal position */
146 short pos_y
; /* Difference - vertical position */
151 int r
= read (com_fd
, &mouse
, sizeof (dev_mouse_t
));
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
;
169 static void xcursor_kbmouse_handler ()
171 int scancode
= getkey ();
176 cursor_x
-= mouse_accel
;
180 cursor_x
+= mouse_accel
;
184 cursor_y
-= mouse_accel
;
188 cursor_y
+= mouse_accel
;
191 cursor_state
= XCURSOR_STATE_RBUTTON
;
194 cursor_state
= XCURSOR_STATE_LBUTTON
;
203 int xcursor_update ()
205 switch (mouse_type
) {
207 /* TODO: autodetect */
208 xcursor_kbmouse_handler ();
211 /* serial mouse over rs232 */
212 xcursor_commouse_handler ();
216 xcursor_ps2mouse_handler ();
223 if (cursor_y
> (signed) (vgafb_res_y
-1))
224 cursor_y
= (vgafb_res_y
-1);
229 if (cursor_x
> (signed) (vgafb_res_x
-1))
230 cursor_x
= (vgafb_res_x
-1);
235 void xcursor_getpos (int *x
, int *y
)
241 unsigned xcursor_settype (unsigned char type
)
243 if (mouse_type
== type
)