Subversion Repositories shark

Rev

Rev 898 | 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__
1006 mauro 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
 
38
/* Mouse driver currently installed/actived */
519 mauro 39
static int mouse_installed = FALSE;
523 mauro 40
static int mouse_enabled = FALSE;
519 mauro 41
 
523 mauro 42
static int mouse_xmin_tick;
43
static int mouse_ymin_tick;
44
static int mouse_xmax_tick;
45
static int mouse_ymax_tick;
519 mauro 46
 
523 mauro 47
#define MOUSE_DEF_THRESHOLD 10
48
static int mouse_threshold;
49
 
50
static int           mouse_x = 0;
51
static int           mouse_y = 0;
52
static int           mouse_x_tick = 0;
53
static int           mouse_y_tick = 0;
54
static int           mouse_z =  0;
55
static unsigned long mouse_buttons = 0;
56
 
538 mauro 57
MOUSE_HANDLER user_mouse_handler = NULL;
58
MOUSE_HANDLER show_mouse_handler = NULL;
519 mauro 59
 
523 mauro 60
#ifdef MOUSE_TASK
61
/* mouse task PID */
62
static PID mousepid = NIL;
63
#else
64
void mouseProc(void);
65
#endif
522 mauro 66
 
67
 
519 mauro 68
/*
69
 * Start mouseProc Task
70
 */
523 mauro 71
void shark_mouse_exec(void)
519 mauro 72
{
523 mauro 73
#ifdef MOUSE_TASK
74
        task_activate(mousepid);
75
#else
76
        mouseProc();
77
#endif
519 mauro 78
}
79
 
523 mauro 80
#ifdef MOUSE_TASK
81
TASK mouseProc(void)
82
#else
83
void mouseProc(void)
84
#endif
85
{
86
        static MOUSE_EVT ev;
87
        unsigned long dbuttons;
88
        int dx, dy, dz;
89
        int res;
90
 
91
#ifdef MOUSE_TASK
92
        while (1) {
93
#endif
94
                if (mouse_enabled) {
95
                        res = mouse_get(&dx, &dy, &dz, &dbuttons);
96
                        if (res >= 0) {
97
                                mouse_x_tick += dx;
98
                                if (mouse_x_tick < mouse_xmin_tick)
99
                                        mouse_x_tick = mouse_xmin_tick;
100
                                if (mouse_x_tick > mouse_xmax_tick)
101
                                        mouse_x_tick = mouse_xmax_tick;
102
                                mouse_x = (mouse_x_tick + (mouse_threshold/2)) / mouse_threshold;
103
                                ev.x  = mouse_x;
104
                                ev.dx = dx;
519 mauro 105
 
523 mauro 106
                                mouse_y_tick -= dy;
107
                                if (mouse_y_tick < mouse_ymin_tick)
108
                                        mouse_y_tick = mouse_ymin_tick;
109
                                if (mouse_y_tick > mouse_ymax_tick)
110
                                        mouse_y_tick = mouse_ymax_tick;
111
                                mouse_y = (mouse_y_tick + (mouse_threshold/2)) / mouse_threshold;
112
                                ev.y  = mouse_y;
113
                                ev.dy = dy;
114
 
115
                                mouse_z += dz;
116
                                ev.z  = mouse_z;
117
                                ev.dz = dz;
118
 
119
                                mouse_buttons = dbuttons;
120
                                ev.buttons  = mouse_buttons;
121
#ifdef __MOUSE_DEBUG__
122
                                printk(KERN_DEBUG "shark_mouse.c: delta ( %3d %3d %3d - %6x) -> ( %3d %3d %3d - %6x)\n",
123
                                       dx, dy, dz, (int)dbuttons, mouse_x, mouse_y, mouse_z, (int)mouse_buttons);
124
#endif
125
                                /* mouse handler */
538 mauro 126
                                if (show_mouse_handler != NULL)
127
                                        show_mouse_handler(&ev);
128
                                else if (user_mouse_handler != NULL)
129
                                        user_mouse_handler(&ev);
523 mauro 130
                        }
131
                }
132
#ifdef MOUSE_TASK
133
                task_endcycle();
134
        }
135
#endif
136
}
137
 
138
/**** Start User Functions ****/
139
 
140
void mouse_enable(void)
141
{
142
        mouse_enabled = TRUE;   /* TODO */
143
}
144
 
145
void mouse_disable(void)
146
{
147
        mouse_enabled = FALSE;  /* TODO */
148
}
149
 
549 mauro 150
void mouse_getposition(int *x,int *y,int *z, unsigned long *buttons)
523 mauro 151
{
548 mauro 152
        if (x)
153
                *x = mouse_x;
154
        if (y)
155
                *y = mouse_y;
156
        if (z)
157
                *z = mouse_z;
158
        if (buttons)
159
                *buttons = mouse_buttons;
523 mauro 160
}
161
 
549 mauro 162
void mouse_setposition(int x,int y, int z)
523 mauro 163
{
164
        mouse_enabled = FALSE;
165
 
898 mauro 166
        mouse_x_tick = x * mouse_threshold;
167
        if (mouse_x_tick < mouse_xmin_tick)
168
                mouse_x_tick = mouse_xmin_tick;
169
        if (mouse_x_tick > mouse_xmax_tick)
170
                mouse_x_tick = mouse_xmax_tick;
171
        mouse_x = (mouse_x_tick + (mouse_threshold/2)) / mouse_threshold;
523 mauro 172
 
898 mauro 173
        mouse_y_tick = y * mouse_threshold;
174
        if (mouse_y_tick < mouse_ymin_tick)
175
                mouse_y_tick = mouse_ymin_tick;
176
        if (mouse_y_tick > mouse_ymax_tick)
177
                mouse_y_tick = mouse_ymax_tick;
178
        mouse_y = (mouse_y_tick + (mouse_threshold/2)) / mouse_threshold;
179
 
523 mauro 180
        mouse_z = z;
181
 
182
        mouse_enabled = TRUE;
183
}
184
 
549 mauro 185
void mouse_getlimits(int *xmin, int *ymin, int *xmax, int *ymax)
523 mauro 186
{
548 mauro 187
        if (xmin)
188
                *xmin = mouse_xmin_tick / mouse_threshold;
189
        if (ymin)
190
                *ymin = mouse_ymin_tick / mouse_threshold;
191
        if (xmax)
192
                *xmax = mouse_xmax_tick / mouse_threshold;
193
        if (ymax)
194
                *ymax = mouse_ymax_tick / mouse_threshold;
523 mauro 195
}
196
 
549 mauro 197
int mouse_setlimits(int xmin, int ymin, int xmax, int ymax)
523 mauro 198
{
199
        if ((xmin < 0) && (ymin < 0) && (xmax < xmin) && (ymax < ymin))
200
                return -1;
201
 
202
        mouse_xmin_tick = xmin * mouse_threshold;
203
        mouse_ymin_tick = ymin * mouse_threshold;
204
        mouse_xmax_tick = xmax * mouse_threshold;
205
        mouse_ymax_tick = ymax * mouse_threshold;
206
 
207
        return 0;
208
}
209
 
210
void mouse_hook(MOUSE_HANDLER h)
211
{
538 mauro 212
        user_mouse_handler = h;
523 mauro 213
}
214
 
215
int mouse_getthreshold(void)
216
{
217
        return mouse_threshold;
218
}
219
 
220
int mouse_setthreshold(int t)
221
{
222
        mouse_enabled = FALSE;
223
 
224
        if ((t>0) && (t<=200)) {
225
                mouse_xmin_tick = (mouse_xmin_tick * t) / mouse_threshold;
226
                mouse_ymin_tick = (mouse_ymin_tick * t) / mouse_threshold;
227
                mouse_xmax_tick = (mouse_xmax_tick * t) / mouse_threshold;
228
                mouse_ymax_tick = (mouse_ymax_tick * t) / mouse_threshold;
229
                mouse_threshold = t;
230
                return 0;
231
        } else {
232
                return -1;
233
        }
234
 
235
        mouse_enabled = TRUE;
236
}
237
 
238
/**** End User Functions ****/
239
 
519 mauro 240
/* Init the Linux Speaker Driver */
549 mauro 241
int MOUSE26_installed(void)
242
{
243
        return mouse_installed;
244
}
245
 
523 mauro 246
int MOUSE26_init(MOUSE_PARMS *s)
519 mauro 247
{
523 mauro 248
        MOUSE_PARMS mparms = BASE_MOUSE;
519 mauro 249
        int status = 0;
250
 
523 mauro 251
#ifdef MOUSE_TASK
252
        TASK_MODEL *m;
253
        SOFT_TASK_MODEL base_m;
254
#endif
255
 
519 mauro 256
        if (mouse_installed == TRUE) return 0;
257
 
522 mauro 258
        /* if a NULL is passed */
523 mauro 259
        if (s == NULL)
522 mauro 260
                s = &mparms;
261
 
523 mauro 262
        /* set mouse threshold */
549 mauro 263
        if ((s->threshold == (int) MOUSE_DEFAULT) || (s->threshold == 0))
523 mauro 264
                mouse_threshold = MOUSE_DEF_THRESHOLD;
265
        else
266
                mouse_threshold = s->threshold;
267
 
268
        /* set mouse limits */
269
        if ((s->xmin == (int) MOUSE_DEFAULT) || (s->xmin < 0))
270
                mouse_xmin_tick = 0;
271
        else
272
                mouse_xmin_tick = s->xmin * mouse_threshold;
273
 
274
        if ((s->ymin == (int) MOUSE_DEFAULT) || (s->ymin < 0))
275
                mouse_ymin_tick = 0;
276
        else
277
                mouse_ymin_tick = s->ymin * mouse_threshold;
278
 
279
        if ((s->xmax == (int) MOUSE_DEFAULT) || ((s->xmax * mouse_threshold) < mouse_xmin_tick))
280
                mouse_xmax_tick = 79 * mouse_threshold;
281
        else
282
                mouse_xmax_tick = s->xmax * mouse_threshold;
283
 
284
        if ((s->ymax == (int) MOUSE_DEFAULT) || ((s->ymax * mouse_threshold) < mouse_ymin_tick))
285
                mouse_ymax_tick = 24 * mouse_threshold;
286
        else
287
                mouse_ymax_tick = s->ymax * mouse_threshold;
288
 
549 mauro 289
        /* set initial mouse position */
290
        if (s->x0 == (int) MOUSE_DEFAULT)
291
                mouse_x_tick = mouse_x = 0;
292
        else {
293
                mouse_x_tick = s->x0 * mouse_threshold;
294
                mouse_x = mouse_x_tick / mouse_threshold;
295
        }
296
 
297
        if (s->y0 == (int) MOUSE_DEFAULT)
298
                mouse_y_tick = mouse_y = 0;
299
        else {
300
                mouse_y_tick = s->y0 * mouse_threshold;
301
                mouse_y = mouse_y_tick / mouse_threshold;
302
        }
303
 
304
        if (s->z0 == (int) MOUSE_DEFAULT)
305
                mouse_z = 0;
306
        else
307
                mouse_z = s->z0;
308
 
523 mauro 309
#ifdef MOUSE_TASK
522 mauro 310
        if (s->tm == (TASK_MODEL *)MOUSE_DEFAULT) {
311
                soft_task_default_model(base_m);
312
                soft_task_def_wcet(base_m,2000);
313
                soft_task_def_met(base_m,500);
314
                soft_task_def_period(base_m,8000);
315
                soft_task_def_system(base_m);
826 mauro 316
                soft_task_def_nokill (base_m);
522 mauro 317
                soft_task_def_aperiodic(base_m);
318
                m = (TASK_MODEL *)&base_m;
319
        } else
523 mauro 320
                m = s->tm;
522 mauro 321
 
523 mauro 322
        mousepid = task_create ("MouseTask", mouseProc, m, NULL);
323
        if (mousepid == -1) {
324
                return -1;
325
        }
326
#endif
327
 
549 mauro 328
        if (INPUT26_installed() == FALSE) {
519 mauro 329
                status = INPUT26_init();
330
                if (status) {
523 mauro 331
 
519 mauro 332
                        printk(KERN_ERR "shark_mouse.c: Unable to open Input SubSystem.\n");
333
                        return -1;
334
                }
335
        }
336
 
337
        status = psmouse_init();
338
        if (status) {
339
                printk(KERN_ERR "shark_mouse.c: PsMouse_Init return: %d\n", status);
340
                return -1;
341
        }
342
 
343
        status = mouse_init();
344
        if (status) {
345
                printk(KERN_ERR "shark_mouse.c: Mouse_Init return: %d\n", status);
346
                return -1;
347
        }
348
 
349
        mouse_installed = TRUE;
523 mauro 350
        mouse_enabled = TRUE;
519 mauro 351
 
352
        return status;
353
}
354
 
355
int MOUSE26_close()
356
{
523 mauro 357
#ifdef MOUSE_TASK
358
        int free;
359
        SYS_FLAGS f;
360
#endif
361
 
519 mauro 362
        if (!mouse_installed)
363
                return -1;
364
 
523 mauro 365
        mouse_enabled = FALSE;
519 mauro 366
        mouse_exit();
367
        psmouse_exit();
523 mauro 368
 
369
#ifdef MOUSE_TASK
370
        f = kern_fsave();
371
        free = (proc_table[mousepid].status == FREE);
372
        kern_frestore(f);
373
#ifdef __MOUSE_DEBUG__
374
        printk(KERN_DEBUG "shark_mouse.c: MouseTask is %s.\n", free ? "killed" : "alive");
375
#endif
376
        if (free)
377
                task_kill (mousepid);
378
#endif
379
 
519 mauro 380
        mouse_installed = FALSE;
381
 
382
        return 0;
383
}
384