Subversion Repositories shark

Rev

Rev 522 | Rev 538 | 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
 
19
/*
20
 * Copyright (C) 2000 Paolo Gai
21
 *
22
 * This program is free software; you can redistribute it and/or modify
23
 * it under the terms of the GNU General Public License as published by
24
 * the Free Software Foundation; either version 2 of the License, or
25
 * (at your option) any later version.
26
 *
27
 * This program is distributed in the hope that it will be useful,
28
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30
 * GNU General Public License for more details.
31
 *
32
 * You should have received a copy of the GNU General Public License
33
 * along with this program; if not, write to the Free Software
34
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
35
 *
36
 */
37
 
523 mauro 38
//#define __MOUSE_DEBUG__
39
//#define MOUSE_TASK
519 mauro 40
 
41
#include <kernel/kern.h>
42
 
43
#include "../include/drivers/shark_input26.h"
44
#include "../include/drivers/shark_mouse26.h"
45
 
46
/* Devices */
47
extern int psmouse_init(void);
48
extern int psmouse_exit(void);
49
 
50
/* Handlers */
51
extern int  mouse_init(void);
52
extern void mouse_exit(void);
53
 
523 mauro 54
/* Functions */
55
extern int mouse_get(int *dx, int *dy, int *dz, unsigned long *buttons);
56
 
519 mauro 57
extern int input_installed;
58
 
523 mauro 59
/* Mouse driver currently installed/actived */
519 mauro 60
static int mouse_installed = FALSE;
523 mauro 61
static int mouse_enabled = FALSE;
519 mauro 62
 
523 mauro 63
static int mouse_xmin_tick;
64
static int mouse_ymin_tick;
65
static int mouse_xmax_tick;
66
static int mouse_ymax_tick;
519 mauro 67
 
523 mauro 68
#define MOUSE_DEF_THRESHOLD 10
69
static int mouse_threshold;
70
 
71
static int           mouse_x = 0;
72
static int           mouse_y = 0;
73
static int           mouse_x_tick = 0;
74
static int           mouse_y_tick = 0;
75
static int           mouse_z =  0;
76
static unsigned long mouse_buttons = 0;
77
 
522 mauro 78
static MOUSE_HANDLER mouse_handler = NULL;
519 mauro 79
 
523 mauro 80
#ifdef MOUSE_TASK
81
/* mouse task PID */
82
static PID mousepid = NIL;
83
#else
84
void mouseProc(void);
85
#endif
522 mauro 86
 
87
 
519 mauro 88
/*
89
 * Start mouseProc Task
90
 */
523 mauro 91
void shark_mouse_exec(void)
519 mauro 92
{
523 mauro 93
#ifdef MOUSE_TASK
94
        task_activate(mousepid);
95
#else
96
        mouseProc();
97
#endif
519 mauro 98
}
99
 
523 mauro 100
#ifdef MOUSE_TASK
101
TASK mouseProc(void)
102
#else
103
void mouseProc(void)
104
#endif
105
{
106
        static MOUSE_EVT ev;
107
        unsigned long dbuttons;
108
        int dx, dy, dz;
109
        int res;
110
 
111
#ifdef MOUSE_TASK
112
        while (1) {
113
#endif
114
                if (mouse_enabled) {
115
                        res = mouse_get(&dx, &dy, &dz, &dbuttons);
116
                        if (res >= 0) {
117
                                mouse_x_tick += dx;
118
                                if (mouse_x_tick < mouse_xmin_tick)
119
                                        mouse_x_tick = mouse_xmin_tick;
120
                                if (mouse_x_tick > mouse_xmax_tick)
121
                                        mouse_x_tick = mouse_xmax_tick;
122
                                mouse_x = (mouse_x_tick + (mouse_threshold/2)) / mouse_threshold;
123
                                ev.x  = mouse_x;
124
                                ev.dx = dx;
519 mauro 125
 
523 mauro 126
                                mouse_y_tick -= dy;
127
                                if (mouse_y_tick < mouse_ymin_tick)
128
                                        mouse_y_tick = mouse_ymin_tick;
129
                                if (mouse_y_tick > mouse_ymax_tick)
130
                                        mouse_y_tick = mouse_ymax_tick;
131
                                mouse_y = (mouse_y_tick + (mouse_threshold/2)) / mouse_threshold;
132
                                ev.y  = mouse_y;
133
                                ev.dy = dy;
134
 
135
                                mouse_z += dz;
136
                                ev.z  = mouse_z;
137
                                ev.dz = dz;
138
 
139
                                mouse_buttons = dbuttons;
140
                                ev.buttons  = mouse_buttons;
141
#ifdef __MOUSE_DEBUG__
142
                                printk(KERN_DEBUG "shark_mouse.c: delta ( %3d %3d %3d - %6x) -> ( %3d %3d %3d - %6x)\n",
143
                                       dx, dy, dz, (int)dbuttons, mouse_x, mouse_y, mouse_z, (int)mouse_buttons);
144
#endif
145
                                /* mouse handler */
146
                                if (mouse_handler!=NULL) mouse_handler(&ev);
147
                        }
148
                }
149
#ifdef MOUSE_TASK
150
                task_endcycle();
151
        }
152
#endif
153
}
154
 
155
/**** Start User Functions ****/
156
 
157
void mouse_enable(void)
158
{
159
        mouse_enabled = TRUE;   /* TODO */
160
}
161
 
162
void mouse_disable(void)
163
{
164
        mouse_enabled = FALSE;  /* TODO */
165
}
166
 
167
void mouse_getpos(int *x,int *y,int *z, unsigned long *buttons)
168
{
169
        *x = mouse_x;
170
        *y = mouse_y;
171
        *z = mouse_z;
172
        *buttons = mouse_buttons;
173
}
174
 
175
void mouse_setpos(int x,int y, int z)
176
{
177
        mouse_enabled = FALSE;
178
 
179
        mouse_x = x;
180
        if (x < (mouse_xmin_tick / mouse_threshold))
181
                mouse_x = mouse_xmin_tick / mouse_threshold;
182
        if (x > (mouse_xmax_tick / mouse_threshold))
183
                mouse_x = mouse_xmax_tick / mouse_threshold;
184
 
185
        mouse_y = y;
186
        if (y < (mouse_ymin_tick / mouse_threshold))
187
                mouse_y = mouse_ymin_tick / mouse_threshold;
188
        if (y > (mouse_ymax_tick / mouse_threshold))
189
                mouse_y = mouse_ymax_tick / mouse_threshold;
190
 
191
        mouse_z = z;
192
 
193
        mouse_enabled = TRUE;
194
}
195
 
196
void mouse_getlimit(int *xmin, int *ymin, int *xmax, int *ymax)
197
{
198
        *xmin = mouse_xmin_tick / mouse_threshold;
199
        *ymin = mouse_ymin_tick / mouse_threshold;
200
        *xmax = mouse_xmax_tick / mouse_threshold;
201
        *ymax = mouse_ymax_tick / mouse_threshold;
202
}
203
 
204
int mouse_setlimit(int xmin, int ymin, int xmax, int ymax)
205
{
206
        if ((xmin < 0) && (ymin < 0) && (xmax < xmin) && (ymax < ymin))
207
                return -1;
208
 
209
        mouse_xmin_tick = xmin * mouse_threshold;
210
        mouse_ymin_tick = ymin * mouse_threshold;
211
        mouse_xmax_tick = xmax * mouse_threshold;
212
        mouse_ymax_tick = ymax * mouse_threshold;
213
 
214
        return 0;
215
}
216
 
217
void mouse_hook(MOUSE_HANDLER h)
218
{
219
        mouse_handler = h;
220
}
221
 
222
int mouse_getthreshold(void)
223
{
224
        return mouse_threshold;
225
}
226
 
227
int mouse_setthreshold(int t)
228
{
229
        mouse_enabled = FALSE;
230
 
231
        if ((t>0) && (t<=200)) {
232
                mouse_xmin_tick = (mouse_xmin_tick * t) / mouse_threshold;
233
                mouse_ymin_tick = (mouse_ymin_tick * t) / mouse_threshold;
234
                mouse_xmax_tick = (mouse_xmax_tick * t) / mouse_threshold;
235
                mouse_ymax_tick = (mouse_ymax_tick * t) / mouse_threshold;
236
                mouse_threshold = t;
237
                return 0;
238
        } else {
239
                return -1;
240
        }
241
 
242
        mouse_enabled = TRUE;
243
}
244
 
245
/**** End User Functions ****/
246
 
519 mauro 247
/* Init the Linux Speaker Driver */
523 mauro 248
int MOUSE26_init(MOUSE_PARMS *s)
519 mauro 249
{
523 mauro 250
        MOUSE_PARMS mparms = BASE_MOUSE;
519 mauro 251
        int status = 0;
252
 
523 mauro 253
#ifdef MOUSE_TASK
254
        TASK_MODEL *m;
255
        SOFT_TASK_MODEL base_m;
256
#endif
257
 
519 mauro 258
        if (mouse_installed == TRUE) return 0;
259
 
522 mauro 260
        /* if a NULL is passed */
523 mauro 261
        if (s == NULL)
522 mauro 262
                s = &mparms;
263
 
523 mauro 264
        /* set mouse threshold */
265
        if (s->threshold == (int) MOUSE_DEFAULT)
266
                mouse_threshold = MOUSE_DEF_THRESHOLD;
267
        else
268
                mouse_threshold = s->threshold;
269
 
270
        /* set mouse limits */
271
        if ((s->xmin == (int) MOUSE_DEFAULT) || (s->xmin < 0))
272
                mouse_xmin_tick = 0;
273
        else
274
                mouse_xmin_tick = s->xmin * mouse_threshold;
275
 
276
        if ((s->ymin == (int) MOUSE_DEFAULT) || (s->ymin < 0))
277
                mouse_ymin_tick = 0;
278
        else
279
                mouse_ymin_tick = s->ymin * mouse_threshold;
280
 
281
        if ((s->xmax == (int) MOUSE_DEFAULT) || ((s->xmax * mouse_threshold) < mouse_xmin_tick))
282
                mouse_xmax_tick = 79 * mouse_threshold;
283
        else
284
                mouse_xmax_tick = s->xmax * mouse_threshold;
285
 
286
        if ((s->ymax == (int) MOUSE_DEFAULT) || ((s->ymax * mouse_threshold) < mouse_ymin_tick))
287
                mouse_ymax_tick = 24 * mouse_threshold;
288
        else
289
                mouse_ymax_tick = s->ymax * mouse_threshold;
290
 
291
#ifdef MOUSE_TASK
522 mauro 292
        if (s->tm == (TASK_MODEL *)MOUSE_DEFAULT) {
293
                soft_task_default_model(base_m);
294
                soft_task_def_wcet(base_m,2000);
295
                soft_task_def_met(base_m,500);
296
                soft_task_def_period(base_m,8000);
297
                soft_task_def_system(base_m);
298
                soft_task_def_nokill(base_m);
299
                soft_task_def_aperiodic(base_m);
300
                m = (TASK_MODEL *)&base_m;
301
        } else
523 mauro 302
                m = s->tm;
522 mauro 303
 
523 mauro 304
        mousepid = task_create ("MouseTask", mouseProc, m, NULL);
305
        if (mousepid == -1) {
306
                return -1;
307
        }
308
#endif
309
 
519 mauro 310
        if (input_installed == FALSE) {
311
                status = INPUT26_init();
312
                if (status) {
523 mauro 313
 
519 mauro 314
                        printk(KERN_ERR "shark_mouse.c: Unable to open Input SubSystem.\n");
315
                        return -1;
316
                }
317
        }
318
 
319
        status = psmouse_init();
320
        if (status) {
321
                printk(KERN_ERR "shark_mouse.c: PsMouse_Init return: %d\n", status);
322
                return -1;
323
        }
324
 
325
        status = mouse_init();
326
        if (status) {
327
                printk(KERN_ERR "shark_mouse.c: Mouse_Init return: %d\n", status);
328
                return -1;
329
        }
330
 
331
        mouse_installed = TRUE;
523 mauro 332
        mouse_enabled = TRUE;
519 mauro 333
 
334
        return status;
335
}
336
 
337
int MOUSE26_close()
338
{
523 mauro 339
#ifdef MOUSE_TASK
340
        int free;
341
        SYS_FLAGS f;
342
#endif
343
 
519 mauro 344
        if (!mouse_installed)
345
                return -1;
346
 
523 mauro 347
        mouse_enabled = FALSE;
519 mauro 348
        mouse_exit();
349
        psmouse_exit();
523 mauro 350
 
351
#ifdef MOUSE_TASK
352
        f = kern_fsave();
353
        free = (proc_table[mousepid].status == FREE);
354
        kern_frestore(f);
355
#ifdef __MOUSE_DEBUG__
356
        printk(KERN_DEBUG "shark_mouse.c: MouseTask is %s.\n", free ? "killed" : "alive");
357
#endif
358
        if (free)
359
                task_kill (mousepid);
360
#endif
361
 
519 mauro 362
        mouse_installed = FALSE;
363
 
364
        return 0;
365
}
366