Subversion Repositories shark

Rev

Rev 544 | Rev 549 | 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
{
548 mauro 154
        if (x)
155
                *x = mouse_x;
156
        if (y)
157
                *y = mouse_y;
158
        if (z)
159
                *z = mouse_z;
160
        if (buttons)
161
                *buttons = mouse_buttons;
523 mauro 162
}
163
 
164
void mouse_setpos(int x,int y, int z)
165
{
166
        mouse_enabled = FALSE;
167
 
168
        mouse_x = x;
169
        if (x < (mouse_xmin_tick / mouse_threshold))
170
                mouse_x = mouse_xmin_tick / mouse_threshold;
171
        if (x > (mouse_xmax_tick / mouse_threshold))
172
                mouse_x = mouse_xmax_tick / mouse_threshold;
173
 
174
        mouse_y = y;
175
        if (y < (mouse_ymin_tick / mouse_threshold))
176
                mouse_y = mouse_ymin_tick / mouse_threshold;
177
        if (y > (mouse_ymax_tick / mouse_threshold))
178
                mouse_y = mouse_ymax_tick / mouse_threshold;
179
 
180
        mouse_z = z;
181
 
182
        mouse_enabled = TRUE;
183
}
184
 
185
void mouse_getlimit(int *xmin, int *ymin, int *xmax, int *ymax)
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
 
197
int mouse_setlimit(int xmin, int ymin, int xmax, int ymax)
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 */
523 mauro 241
int MOUSE26_init(MOUSE_PARMS *s)
519 mauro 242
{
523 mauro 243
        MOUSE_PARMS mparms = BASE_MOUSE;
519 mauro 244
        int status = 0;
245
 
523 mauro 246
#ifdef MOUSE_TASK
247
        TASK_MODEL *m;
248
        SOFT_TASK_MODEL base_m;
249
#endif
250
 
519 mauro 251
        if (mouse_installed == TRUE) return 0;
252
 
522 mauro 253
        /* if a NULL is passed */
523 mauro 254
        if (s == NULL)
522 mauro 255
                s = &mparms;
256
 
523 mauro 257
        /* set mouse threshold */
258
        if (s->threshold == (int) MOUSE_DEFAULT)
259
                mouse_threshold = MOUSE_DEF_THRESHOLD;
260
        else
261
                mouse_threshold = s->threshold;
262
 
263
        /* set mouse limits */
264
        if ((s->xmin == (int) MOUSE_DEFAULT) || (s->xmin < 0))
265
                mouse_xmin_tick = 0;
266
        else
267
                mouse_xmin_tick = s->xmin * mouse_threshold;
268
 
269
        if ((s->ymin == (int) MOUSE_DEFAULT) || (s->ymin < 0))
270
                mouse_ymin_tick = 0;
271
        else
272
                mouse_ymin_tick = s->ymin * mouse_threshold;
273
 
274
        if ((s->xmax == (int) MOUSE_DEFAULT) || ((s->xmax * mouse_threshold) < mouse_xmin_tick))
275
                mouse_xmax_tick = 79 * mouse_threshold;
276
        else
277
                mouse_xmax_tick = s->xmax * mouse_threshold;
278
 
279
        if ((s->ymax == (int) MOUSE_DEFAULT) || ((s->ymax * mouse_threshold) < mouse_ymin_tick))
280
                mouse_ymax_tick = 24 * mouse_threshold;
281
        else
282
                mouse_ymax_tick = s->ymax * mouse_threshold;
283
 
284
#ifdef MOUSE_TASK
522 mauro 285
        if (s->tm == (TASK_MODEL *)MOUSE_DEFAULT) {
286
                soft_task_default_model(base_m);
287
                soft_task_def_wcet(base_m,2000);
288
                soft_task_def_met(base_m,500);
289
                soft_task_def_period(base_m,8000);
290
                soft_task_def_system(base_m);
291
                soft_task_def_nokill(base_m);
292
                soft_task_def_aperiodic(base_m);
293
                m = (TASK_MODEL *)&base_m;
294
        } else
523 mauro 295
                m = s->tm;
522 mauro 296
 
523 mauro 297
        mousepid = task_create ("MouseTask", mouseProc, m, NULL);
298
        if (mousepid == -1) {
299
                return -1;
300
        }
301
#endif
302
 
519 mauro 303
        if (input_installed == FALSE) {
304
                status = INPUT26_init();
305
                if (status) {
523 mauro 306
 
519 mauro 307
                        printk(KERN_ERR "shark_mouse.c: Unable to open Input SubSystem.\n");
308
                        return -1;
309
                }
310
        }
311
 
312
        status = psmouse_init();
313
        if (status) {
314
                printk(KERN_ERR "shark_mouse.c: PsMouse_Init return: %d\n", status);
315
                return -1;
316
        }
317
 
318
        status = mouse_init();
319
        if (status) {
320
                printk(KERN_ERR "shark_mouse.c: Mouse_Init return: %d\n", status);
321
                return -1;
322
        }
323
 
324
        mouse_installed = TRUE;
523 mauro 325
        mouse_enabled = TRUE;
519 mauro 326
 
327
        return status;
328
}
329
 
330
int MOUSE26_close()
331
{
523 mauro 332
#ifdef MOUSE_TASK
333
        int free;
334
        SYS_FLAGS f;
335
#endif
336
 
519 mauro 337
        if (!mouse_installed)
338
                return -1;
339
 
523 mauro 340
        mouse_enabled = FALSE;
519 mauro 341
        mouse_exit();
342
        psmouse_exit();
523 mauro 343
 
344
#ifdef MOUSE_TASK
345
        f = kern_fsave();
346
        free = (proc_table[mousepid].status == FREE);
347
        kern_frestore(f);
348
#ifdef __MOUSE_DEBUG__
349
        printk(KERN_DEBUG "shark_mouse.c: MouseTask is %s.\n", free ? "killed" : "alive");
350
#endif
351
        if (free)
352
                task_kill (mousepid);
353
#endif
354
 
519 mauro 355
        mouse_installed = FALSE;
356
 
357
        return 0;
358
}
359