Small improvements in build process - dont compile graphical code when not used
[ZeXOS.git] / kernel / drivers / char / mouse / com.c
blob8d4788bc49599ea2a4ee5e013d3ff9ce2d17b3b8
1 /*
2 * ZeX/OS
3 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2010 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <build.h>
22 #include <config.h>
24 #ifdef CONFIG_DRV_COMMOUSE
25 #include <system.h>
26 #include <arch/io.h>
27 #include <rs232.h>
28 #include <mouse.h>
29 #include <dev.h>
31 dev_mouse_t commouse;
33 unsigned commouse_handler ()
35 char b1 = rs232_read_nonblock ();
37 if ((b1 & (1 << 6)) == 0) {
38 commouse.pos_x = 0;
39 commouse.pos_y = 0;
40 return 0;
43 // beginning of packet
44 char b2 = rs232_read ();
45 char b3 = rs232_read ();
47 commouse.pos_x = (int) (char) ((b1 << 6) | (b2 & 0x3f));
48 commouse.pos_y = (int) (char) (((b1 << 4) & 0xc0) | (b3 & 0x3f));
50 unsigned short flags = (b1 >> 4) & 3;
52 commouse.flags = (flags == 0x2) ? MOUSE_FLAG_BUTTON1 : 0;
53 commouse.flags |= (flags == 0x1) ? MOUSE_FLAG_BUTTON2 : 0;
54 commouse.flags |= (flags == 0x4) ? MOUSE_FLAG_BUTTON3 : 0;
56 return 1;
59 unsigned commouse_init ()
61 unsigned timeout;
63 for (timeout = 3; timeout; timeout --) {
64 char protocol = rs232_read_nonblock ();
66 if (protocol == 'M')
67 return 1;
70 return 0;
73 bool commouse_acthandler (unsigned act, char *block, unsigned block_len)
75 switch (act) {
76 case DEV_ACT_INIT:
78 dev_flags_t *flags = (dev_flags_t *) block;
80 if (!flags)
81 return 0;
83 if (block_len != sizeof (dev_flags_t))
84 return 0;
86 unsigned r = commouse_init ();
88 if (!r)
89 return 0;
91 flags->iomem = (void *) &commouse;
92 flags->iolen = sizeof (dev_mouse_t);
94 memset (flags->iomem, 0, sizeof (dev_mouse_t));
96 return 1;
98 break;
99 case DEV_ACT_READ:
101 memcpy (block, &commouse, sizeof (commouse));
103 return 1;
105 break;
106 case DEV_ACT_WRITE:
108 memcpy (&commouse, block, sizeof (commouse));
110 return 1;
112 break;
113 case DEV_ACT_UPDATE:
115 commouse_handler ();
117 return 1;
119 break;
122 return 0;
124 #endif