Subversion Repositories shark

Rev

Rev 569 | 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__
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
 
166
        mouse_x = x;
167
        if (x < (mouse_xmin_tick / mouse_threshold))
168
                mouse_x = mouse_xmin_tick / mouse_threshold;
169
        if (x > (mouse_xmax_tick / mouse_threshold))
170
                mouse_x = mouse_xmax_tick / mouse_threshold;
171
 
172
        mouse_y = y;
173
        if (y < (mouse_ymin_tick / mouse_threshold))
174
                mouse_y = mouse_ymin_tick / mouse_threshold;
175
        if (y > (mouse_ymax_tick / mouse_threshold))
176
                mouse_y = mouse_ymax_tick / mouse_threshold;
177
 
178
        mouse_z = z;
179
 
180
        mouse_enabled = TRUE;
181
}
182
 
549 mauro 183
void mouse_getlimits(int *xmin, int *ymin, int *xmax, int *ymax)
523 mauro 184
{
548 mauro 185
        if (xmin)
186
                *xmin = mouse_xmin_tick / mouse_threshold;
187
        if (ymin)
188
                *ymin = mouse_ymin_tick / mouse_threshold;
189
        if (xmax)
190
                *xmax = mouse_xmax_tick / mouse_threshold;
191
        if (ymax)
192
                *ymax = mouse_ymax_tick / mouse_threshold;
523 mauro 193
}
194
 
549 mauro 195
int mouse_setlimits(int xmin, int ymin, int xmax, int ymax)
523 mauro 196
{
197
        if ((xmin < 0) && (ymin < 0) && (xmax < xmin) && (ymax < ymin))
198
                return -1;
199
 
200
        mouse_xmin_tick = xmin * mouse_threshold;
201
        mouse_ymin_tick = ymin * mouse_threshold;
202
        mouse_xmax_tick = xmax * mouse_threshold;
203
        mouse_ymax_tick = ymax * mouse_threshold;
204
 
205
        return 0;
206
}
207
 
208
void mouse_hook(MOUSE_HANDLER h)
209
{
538 mauro 210
        user_mouse_handler = h;
523 mauro 211
}
212
 
213
int mouse_getthreshold(void)
214
{
215
        return mouse_threshold;
216
}
217
 
218
int mouse_setthreshold(int t)
219
{
220
        mouse_enabled = FALSE;
221
 
222
        if ((t>0) && (t<=200)) {
223
                mouse_xmin_tick = (mouse_xmin_tick * t) / mouse_threshold;
224
                mouse_ymin_tick = (mouse_ymin_tick * t) / mouse_threshold;
225
                mouse_xmax_tick = (mouse_xmax_tick * t) / mouse_threshold;
226
                mouse_ymax_tick = (mouse_ymax_tick * t) / mouse_threshold;
227
                mouse_threshold = t;
228
                return 0;
229
        } else {
230
                return -1;
231
        }
232
 
233
        mouse_enabled = TRUE;
234
}
235
 
236
/**** End User Functions ****/
237
 
519 mauro 238
/* Init the Linux Speaker Driver */
549 mauro 239
int MOUSE26_installed(void)
240
{
241
        return mouse_installed;
242
}
243
 
523 mauro 244
int MOUSE26_init(MOUSE_PARMS *s)
519 mauro 245
{
523 mauro 246
        MOUSE_PARMS mparms = BASE_MOUSE;
519 mauro 247
        int status = 0;
248
 
523 mauro 249
#ifdef MOUSE_TASK
250
        TASK_MODEL *m;
251
        SOFT_TASK_MODEL base_m;
252
#endif
253
 
519 mauro 254
        if (mouse_installed == TRUE) return 0;
255
 
522 mauro 256
        /* if a NULL is passed */
523 mauro 257
        if (s == NULL)
522 mauro 258
                s = &mparms;
259
 
523 mauro 260
        /* set mouse threshold */
549 mauro 261
        if ((s->threshold == (int) MOUSE_DEFAULT) || (s->threshold == 0))
523 mauro 262
                mouse_threshold = MOUSE_DEF_THRESHOLD;
263
        else
264
                mouse_threshold = s->threshold;
265
 
266
        /* set mouse limits */
267
        if ((s->xmin == (int) MOUSE_DEFAULT) || (s->xmin < 0))
268
                mouse_xmin_tick = 0;
269
        else
270
                mouse_xmin_tick = s->xmin * mouse_threshold;
271
 
272
        if ((s->ymin == (int) MOUSE_DEFAULT) || (s->ymin < 0))
273
                mouse_ymin_tick = 0;
274
        else
275
                mouse_ymin_tick = s->ymin * mouse_threshold;
276
 
277
        if ((s->xmax == (int) MOUSE_DEFAULT) || ((s->xmax * mouse_threshold) < mouse_xmin_tick))
278
                mouse_xmax_tick = 79 * mouse_threshold;
279
        else
280
                mouse_xmax_tick = s->xmax * mouse_threshold;
281
 
282
        if ((s->ymax == (int) MOUSE_DEFAULT) || ((s->ymax * mouse_threshold) < mouse_ymin_tick))
283
                mouse_ymax_tick = 24 * mouse_threshold;
284
        else
285
                mouse_ymax_tick = s->ymax * mouse_threshold;
286
 
549 mauro 287
        /* set initial mouse position */
288
        if (s->x0 == (int) MOUSE_DEFAULT)
289
                mouse_x_tick = mouse_x = 0;
290
        else {
291
                mouse_x_tick = s->x0 * mouse_threshold;
292
                mouse_x = mouse_x_tick / mouse_threshold;
293
        }
294
 
295
        if (s->y0 == (int) MOUSE_DEFAULT)
296
                mouse_y_tick = mouse_y = 0;
297
        else {
298
                mouse_y_tick = s->y0 * mouse_threshold;
299
                mouse_y = mouse_y_tick / mouse_threshold;
300
        }
301
 
302
        if (s->z0 == (int) MOUSE_DEFAULT)
303
                mouse_z = 0;
304
        else
305
                mouse_z = s->z0;
306
 
523 mauro 307
#ifdef MOUSE_TASK
522 mauro 308
        if (s->tm == (TASK_MODEL *)MOUSE_DEFAULT) {
309
                soft_task_default_model(base_m);
310
                soft_task_def_wcet(base_m,2000);
311
                soft_task_def_met(base_m,500);
312
                soft_task_def_period(base_m,8000);
313
                soft_task_def_system(base_m);
826 mauro 314
                soft_task_def_nokill (base_m);
522 mauro 315
                soft_task_def_aperiodic(base_m);
316
                m = (TASK_MODEL *)&base_m;
317
        } else
523 mauro 318
                m = s->tm;
522 mauro 319
 
523 mauro 320
        mousepid = task_create ("MouseTask", mouseProc, m, NULL);
321
        if (mousepid == -1) {
322
                return -1;
323
        }
324
#endif
325
 
549 mauro 326
        if (INPUT26_installed() == FALSE) {
519 mauro 327
                status = INPUT26_init();
328
                if (status) {
523 mauro 329
 
519 mauro 330
                        printk(KERN_ERR "shark_mouse.c: Unable to open Input SubSystem.\n");
331
                        return -1;
332
                }
333
        }
334
 
335
        status = psmouse_init();
336
        if (status) {
337
                printk(KERN_ERR "shark_mouse.c: PsMouse_Init return: %d\n", status);
338
                return -1;
339
        }
340
 
341
        status = mouse_init();
342
        if (status) {
343
                printk(KERN_ERR "shark_mouse.c: Mouse_Init return: %d\n", status);
344
                return -1;
345
        }
346
 
347
        mouse_installed = TRUE;
523 mauro 348
        mouse_enabled = TRUE;
519 mauro 349
 
350
        return status;
351
}
352
 
353
int MOUSE26_close()
354
{
523 mauro 355
#ifdef MOUSE_TASK
356
        int free;
357
        SYS_FLAGS f;
358
#endif
359
 
519 mauro 360
        if (!mouse_installed)
361
                return -1;
362
 
523 mauro 363
        mouse_enabled = FALSE;
519 mauro 364
        mouse_exit();
365
        psmouse_exit();
523 mauro 366
 
367
#ifdef MOUSE_TASK
368
        f = kern_fsave();
369
        free = (proc_table[mousepid].status == FREE);
370
        kern_frestore(f);
371
#ifdef __MOUSE_DEBUG__
372
        printk(KERN_DEBUG "shark_mouse.c: MouseTask is %s.\n", free ? "killed" : "alive");
373
#endif
374
        if (free)
375
                task_kill (mousepid);
376
#endif
377
 
519 mauro 378
        mouse_installed = FALSE;
379
 
380
        return 0;
381
}
382