Rev 538 | Rev 548 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
519 | mauro | 1 | /* |
2 | * Project: S.Ha.R.K. |
||
3 | * |
||
4 | * Coordinators: |
||
5 | * Giorgio Buttazzo <giorgio@sssup.it> |
||
6 | * Paolo Gai <pj@gandalf.sssup.it> |
||
7 | * |
||
8 | * Authors : |
||
9 | * Mauro Marinoni <mauro.marinoni@unipv.it> |
||
10 | * (see the web pages for full authors list) |
||
11 | * |
||
12 | * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy) |
||
13 | * |
||
14 | * http://www.sssup.it |
||
15 | * http://retis.sssup.it |
||
16 | * http://shark.sssup.it |
||
17 | */ |
||
18 | |||
523 | mauro | 19 | //#define __MOUSE_DEBUG__ |
20 | //#define MOUSE_TASK |
||
519 | mauro | 21 | |
22 | #include <kernel/kern.h> |
||
23 | |||
24 | #include "../include/drivers/shark_input26.h" |
||
25 | #include "../include/drivers/shark_mouse26.h" |
||
26 | |||
27 | /* Devices */ |
||
28 | extern int psmouse_init(void); |
||
29 | extern int psmouse_exit(void); |
||
30 | |||
31 | /* Handlers */ |
||
32 | extern int mouse_init(void); |
||
33 | extern void mouse_exit(void); |
||
34 | |||
523 | mauro | 35 | /* Functions */ |
36 | extern int mouse_get(int *dx, int *dy, int *dz, unsigned long *buttons); |
||
37 | |||
519 | mauro | 38 | extern int input_installed; |
39 | |||
523 | mauro | 40 | /* Mouse driver currently installed/actived */ |
519 | mauro | 41 | static int mouse_installed = FALSE; |
523 | mauro | 42 | static int mouse_enabled = FALSE; |
519 | mauro | 43 | |
523 | mauro | 44 | static int mouse_xmin_tick; |
45 | static int mouse_ymin_tick; |
||
46 | static int mouse_xmax_tick; |
||
47 | static int mouse_ymax_tick; |
||
519 | mauro | 48 | |
523 | mauro | 49 | #define MOUSE_DEF_THRESHOLD 10 |
50 | static int mouse_threshold; |
||
51 | |||
52 | static int mouse_x = 0; |
||
53 | static int mouse_y = 0; |
||
54 | static int mouse_x_tick = 0; |
||
55 | static int mouse_y_tick = 0; |
||
56 | static int mouse_z = 0; |
||
57 | static unsigned long mouse_buttons = 0; |
||
58 | |||
538 | mauro | 59 | MOUSE_HANDLER user_mouse_handler = NULL; |
60 | MOUSE_HANDLER show_mouse_handler = NULL; |
||
519 | mauro | 61 | |
523 | mauro | 62 | #ifdef MOUSE_TASK |
63 | /* mouse task PID */ |
||
64 | static PID mousepid = NIL; |
||
65 | #else |
||
66 | void mouseProc(void); |
||
67 | #endif |
||
522 | mauro | 68 | |
69 | |||
519 | mauro | 70 | /* |
71 | * Start mouseProc Task |
||
72 | */ |
||
523 | mauro | 73 | void shark_mouse_exec(void) |
519 | mauro | 74 | { |
523 | mauro | 75 | #ifdef MOUSE_TASK |
76 | task_activate(mousepid); |
||
77 | #else |
||
78 | mouseProc(); |
||
79 | #endif |
||
519 | mauro | 80 | } |
81 | |||
523 | mauro | 82 | #ifdef MOUSE_TASK |
83 | TASK mouseProc(void) |
||
84 | #else |
||
85 | void mouseProc(void) |
||
86 | #endif |
||
87 | { |
||
88 | static MOUSE_EVT ev; |
||
89 | unsigned long dbuttons; |
||
90 | int dx, dy, dz; |
||
91 | int res; |
||
92 | |||
93 | #ifdef MOUSE_TASK |
||
94 | while (1) { |
||
95 | #endif |
||
96 | if (mouse_enabled) { |
||
97 | res = mouse_get(&dx, &dy, &dz, &dbuttons); |
||
98 | if (res >= 0) { |
||
99 | mouse_x_tick += dx; |
||
100 | if (mouse_x_tick < mouse_xmin_tick) |
||
101 | mouse_x_tick = mouse_xmin_tick; |
||
102 | if (mouse_x_tick > mouse_xmax_tick) |
||
103 | mouse_x_tick = mouse_xmax_tick; |
||
104 | mouse_x = (mouse_x_tick + (mouse_threshold/2)) / mouse_threshold; |
||
105 | ev.x = mouse_x; |
||
106 | ev.dx = dx; |
||
519 | mauro | 107 | |
523 | mauro | 108 | mouse_y_tick -= dy; |
109 | if (mouse_y_tick < mouse_ymin_tick) |
||
110 | mouse_y_tick = mouse_ymin_tick; |
||
111 | if (mouse_y_tick > mouse_ymax_tick) |
||
112 | mouse_y_tick = mouse_ymax_tick; |
||
113 | mouse_y = (mouse_y_tick + (mouse_threshold/2)) / mouse_threshold; |
||
114 | ev.y = mouse_y; |
||
115 | ev.dy = dy; |
||
116 | |||
117 | mouse_z += dz; |
||
118 | ev.z = mouse_z; |
||
119 | ev.dz = dz; |
||
120 | |||
121 | mouse_buttons = dbuttons; |
||
122 | ev.buttons = mouse_buttons; |
||
123 | #ifdef __MOUSE_DEBUG__ |
||
124 | printk(KERN_DEBUG "shark_mouse.c: delta ( %3d %3d %3d - %6x) -> ( %3d %3d %3d - %6x)\n", |
||
125 | dx, dy, dz, (int)dbuttons, mouse_x, mouse_y, mouse_z, (int)mouse_buttons); |
||
126 | #endif |
||
127 | /* mouse handler */ |
||
538 | mauro | 128 | if (show_mouse_handler != NULL) |
129 | show_mouse_handler(&ev); |
||
130 | else if (user_mouse_handler != NULL) |
||
131 | user_mouse_handler(&ev); |
||
523 | mauro | 132 | } |
133 | } |
||
134 | #ifdef MOUSE_TASK |
||
135 | task_endcycle(); |
||
136 | } |
||
137 | #endif |
||
138 | } |
||
139 | |||
140 | /**** Start User Functions ****/ |
||
141 | |||
142 | void mouse_enable(void) |
||
143 | { |
||
144 | mouse_enabled = TRUE; /* TODO */ |
||
145 | } |
||
146 | |||
147 | void mouse_disable(void) |
||
148 | { |
||
149 | mouse_enabled = FALSE; /* TODO */ |
||
150 | } |
||
151 | |||
152 | void mouse_getpos(int *x,int *y,int *z, unsigned long *buttons) |
||
153 | { |
||
154 | *x = mouse_x; |
||
155 | *y = mouse_y; |
||
156 | *z = mouse_z; |
||
157 | *buttons = mouse_buttons; |
||
158 | } |
||
159 | |||
160 | void mouse_setpos(int x,int y, int z) |
||
161 | { |
||
162 | mouse_enabled = FALSE; |
||
163 | |||
164 | mouse_x = x; |
||
165 | if (x < (mouse_xmin_tick / mouse_threshold)) |
||
166 | mouse_x = mouse_xmin_tick / mouse_threshold; |
||
167 | if (x > (mouse_xmax_tick / mouse_threshold)) |
||
168 | mouse_x = mouse_xmax_tick / mouse_threshold; |
||
169 | |||
170 | mouse_y = y; |
||
171 | if (y < (mouse_ymin_tick / mouse_threshold)) |
||
172 | mouse_y = mouse_ymin_tick / mouse_threshold; |
||
173 | if (y > (mouse_ymax_tick / mouse_threshold)) |
||
174 | mouse_y = mouse_ymax_tick / mouse_threshold; |
||
175 | |||
176 | mouse_z = z; |
||
177 | |||
178 | mouse_enabled = TRUE; |
||
179 | } |
||
180 | |||
181 | void mouse_getlimit(int *xmin, int *ymin, int *xmax, int *ymax) |
||
182 | { |
||
183 | *xmin = mouse_xmin_tick / mouse_threshold; |
||
184 | *ymin = mouse_ymin_tick / mouse_threshold; |
||
185 | *xmax = mouse_xmax_tick / mouse_threshold; |
||
186 | *ymax = mouse_ymax_tick / mouse_threshold; |
||
187 | } |
||
188 | |||
189 | int mouse_setlimit(int xmin, int ymin, int xmax, int ymax) |
||
190 | { |
||
191 | if ((xmin < 0) && (ymin < 0) && (xmax < xmin) && (ymax < ymin)) |
||
192 | return -1; |
||
193 | |||
194 | mouse_xmin_tick = xmin * mouse_threshold; |
||
195 | mouse_ymin_tick = ymin * mouse_threshold; |
||
196 | mouse_xmax_tick = xmax * mouse_threshold; |
||
197 | mouse_ymax_tick = ymax * mouse_threshold; |
||
198 | |||
199 | return 0; |
||
200 | } |
||
201 | |||
202 | void mouse_hook(MOUSE_HANDLER h) |
||
203 | { |
||
538 | mauro | 204 | user_mouse_handler = h; |
523 | mauro | 205 | } |
206 | |||
207 | int mouse_getthreshold(void) |
||
208 | { |
||
209 | return mouse_threshold; |
||
210 | } |
||
211 | |||
212 | int mouse_setthreshold(int t) |
||
213 | { |
||
214 | mouse_enabled = FALSE; |
||
215 | |||
216 | if ((t>0) && (t<=200)) { |
||
217 | mouse_xmin_tick = (mouse_xmin_tick * t) / mouse_threshold; |
||
218 | mouse_ymin_tick = (mouse_ymin_tick * t) / mouse_threshold; |
||
219 | mouse_xmax_tick = (mouse_xmax_tick * t) / mouse_threshold; |
||
220 | mouse_ymax_tick = (mouse_ymax_tick * t) / mouse_threshold; |
||
221 | mouse_threshold = t; |
||
222 | return 0; |
||
223 | } else { |
||
224 | return -1; |
||
225 | } |
||
226 | |||
227 | mouse_enabled = TRUE; |
||
228 | } |
||
229 | |||
230 | /**** End User Functions ****/ |
||
231 | |||
519 | mauro | 232 | /* Init the Linux Speaker Driver */ |
523 | mauro | 233 | int MOUSE26_init(MOUSE_PARMS *s) |
519 | mauro | 234 | { |
523 | mauro | 235 | MOUSE_PARMS mparms = BASE_MOUSE; |
519 | mauro | 236 | int status = 0; |
237 | |||
523 | mauro | 238 | #ifdef MOUSE_TASK |
239 | TASK_MODEL *m; |
||
240 | SOFT_TASK_MODEL base_m; |
||
241 | #endif |
||
242 | |||
519 | mauro | 243 | if (mouse_installed == TRUE) return 0; |
244 | |||
522 | mauro | 245 | /* if a NULL is passed */ |
523 | mauro | 246 | if (s == NULL) |
522 | mauro | 247 | s = &mparms; |
248 | |||
523 | mauro | 249 | /* set mouse threshold */ |
250 | if (s->threshold == (int) MOUSE_DEFAULT) |
||
251 | mouse_threshold = MOUSE_DEF_THRESHOLD; |
||
252 | else |
||
253 | mouse_threshold = s->threshold; |
||
254 | |||
255 | /* set mouse limits */ |
||
256 | if ((s->xmin == (int) MOUSE_DEFAULT) || (s->xmin < 0)) |
||
257 | mouse_xmin_tick = 0; |
||
258 | else |
||
259 | mouse_xmin_tick = s->xmin * mouse_threshold; |
||
260 | |||
261 | if ((s->ymin == (int) MOUSE_DEFAULT) || (s->ymin < 0)) |
||
262 | mouse_ymin_tick = 0; |
||
263 | else |
||
264 | mouse_ymin_tick = s->ymin * mouse_threshold; |
||
265 | |||
266 | if ((s->xmax == (int) MOUSE_DEFAULT) || ((s->xmax * mouse_threshold) < mouse_xmin_tick)) |
||
267 | mouse_xmax_tick = 79 * mouse_threshold; |
||
268 | else |
||
269 | mouse_xmax_tick = s->xmax * mouse_threshold; |
||
270 | |||
271 | if ((s->ymax == (int) MOUSE_DEFAULT) || ((s->ymax * mouse_threshold) < mouse_ymin_tick)) |
||
272 | mouse_ymax_tick = 24 * mouse_threshold; |
||
273 | else |
||
274 | mouse_ymax_tick = s->ymax * mouse_threshold; |
||
275 | |||
276 | #ifdef MOUSE_TASK |
||
522 | mauro | 277 | if (s->tm == (TASK_MODEL *)MOUSE_DEFAULT) { |
278 | soft_task_default_model(base_m); |
||
279 | soft_task_def_wcet(base_m,2000); |
||
280 | soft_task_def_met(base_m,500); |
||
281 | soft_task_def_period(base_m,8000); |
||
282 | soft_task_def_system(base_m); |
||
283 | soft_task_def_nokill(base_m); |
||
284 | soft_task_def_aperiodic(base_m); |
||
285 | m = (TASK_MODEL *)&base_m; |
||
286 | } else |
||
523 | mauro | 287 | m = s->tm; |
522 | mauro | 288 | |
523 | mauro | 289 | mousepid = task_create ("MouseTask", mouseProc, m, NULL); |
290 | if (mousepid == -1) { |
||
291 | return -1; |
||
292 | } |
||
293 | #endif |
||
294 | |||
519 | mauro | 295 | if (input_installed == FALSE) { |
296 | status = INPUT26_init(); |
||
297 | if (status) { |
||
523 | mauro | 298 | |
519 | mauro | 299 | printk(KERN_ERR "shark_mouse.c: Unable to open Input SubSystem.\n"); |
300 | return -1; |
||
301 | } |
||
302 | } |
||
303 | |||
304 | status = psmouse_init(); |
||
305 | if (status) { |
||
306 | printk(KERN_ERR "shark_mouse.c: PsMouse_Init return: %d\n", status); |
||
307 | return -1; |
||
308 | } |
||
309 | |||
310 | status = mouse_init(); |
||
311 | if (status) { |
||
312 | printk(KERN_ERR "shark_mouse.c: Mouse_Init return: %d\n", status); |
||
313 | return -1; |
||
314 | } |
||
315 | |||
316 | mouse_installed = TRUE; |
||
523 | mauro | 317 | mouse_enabled = TRUE; |
519 | mauro | 318 | |
319 | return status; |
||
320 | } |
||
321 | |||
322 | int MOUSE26_close() |
||
323 | { |
||
523 | mauro | 324 | #ifdef MOUSE_TASK |
325 | int free; |
||
326 | SYS_FLAGS f; |
||
327 | #endif |
||
328 | |||
519 | mauro | 329 | if (!mouse_installed) |
330 | return -1; |
||
331 | |||
523 | mauro | 332 | mouse_enabled = FALSE; |
519 | mauro | 333 | mouse_exit(); |
334 | psmouse_exit(); |
||
523 | mauro | 335 | |
336 | #ifdef MOUSE_TASK |
||
337 | f = kern_fsave(); |
||
338 | free = (proc_table[mousepid].status == FREE); |
||
339 | kern_frestore(f); |
||
340 | #ifdef __MOUSE_DEBUG__ |
||
341 | printk(KERN_DEBUG "shark_mouse.c: MouseTask is %s.\n", free ? "killed" : "alive"); |
||
342 | #endif |
||
343 | if (free) |
||
344 | task_kill (mousepid); |
||
345 | #endif |
||
346 | |||
519 | mauro | 347 | mouse_installed = FALSE; |
348 | |||
349 | return 0; |
||
350 | } |
||
351 |