Fixed possible memory corruption in commands exec and netexec; fixed command netcp...
[ZeXOS.git] / kernel / drivers / char / mouse / com.c
blobe9c48af9538e0a27d47c26288272b49914a1e1ec
1 /*
2 * ZeX/OS
3 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program 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
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <build.h>
21 #include <system.h>
22 #include <arch/io.h>
23 #include <rs232.h>
24 #include <mouse.h>
25 #include <dev.h>
27 dev_mouse_t commouse;
29 unsigned commouse_handler ()
31 char b1 = rs232_read_nonblock ();
33 if ((b1 & (1 << 6)) == 0) {
34 commouse.pos_x = 0;
35 commouse.pos_y = 0;
36 return 0;
39 // beginning of packet
40 char b2 = rs232_read ();
41 char b3 = rs232_read ();
43 commouse.pos_x = (int) (char) ((b1 << 6) | (b2 & 0x3f));
44 commouse.pos_y = (int) (char) (((b1 << 4) & 0xc0) | (b3 & 0x3f));
46 unsigned short flags = (b1 >> 4) & 3;
48 commouse.flags = (flags == 0x2) ? MOUSE_FLAG_BUTTON1 : 0;
49 commouse.flags |= (flags == 0x1) ? MOUSE_FLAG_BUTTON2 : 0;
50 commouse.flags |= (flags == 0x4) ? MOUSE_FLAG_BUTTON3 : 0;
52 return 1;
55 unsigned commouse_init ()
57 unsigned timeout;
59 for (timeout = 3; timeout; timeout --) {
60 char protocol = rs232_read_nonblock ();
62 if (protocol == 'M')
63 return 1;
66 return 0;
69 bool commouse_acthandler (unsigned act, char *block, unsigned block_len)
71 switch (act) {
72 case DEV_ACT_INIT:
74 dev_flags_t *flags = (dev_flags_t *) block;
76 if (!flags)
77 return 0;
79 if (block_len != sizeof (dev_flags_t))
80 return 0;
82 unsigned r = commouse_init ();
84 if (!r)
85 return 0;
87 flags->iomem = (void *) &commouse;
88 flags->iolen = sizeof (dev_mouse_t);
90 memset (flags->iomem, 0, sizeof (dev_mouse_t));
92 return 1;
94 break;
95 case DEV_ACT_READ:
97 memcpy (block, &commouse, sizeof (commouse));
99 return 1;
101 break;
102 case DEV_ACT_WRITE:
104 memcpy (&commouse, block, sizeof (commouse));
106 return 1;
108 break;
109 case DEV_ACT_UPDATE:
111 commouse_handler ();
113 return 1;
115 break;
118 return 0;