Subversion Repositories shark

Rev

Rev 547 | Rev 1034 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
538 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
 *   Paolo Gai           <pj@gandalf.sssup.it>
10
 *   Massimiliano Giorgi <massy@gandalf.sssup.it>
11
 *   Luca Abeni          <luca@gandalf.sssup.it>
12
 *   Mauro Marinoni      <mauro.marinoni@unipv.it>
13
 *   (see the web pages for full authors list)
14
 *
15
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
16
 *
17
 * http://www.sssup.it
18
 * http://retis.sssup.it
19
 * http://shark.sssup.it
20
 */
21
 
22
/*
23
 *
24
 * Auto Pointer management
25
 *
26
 */
27
 
28
#include <kernel/kern.h>
29
#include <modules/sem.h>
30
 
31
#include "../include/drivers/shark_input26.h"
32
#include "../include/drivers/shark_mouse26.h"
33
 
34
//#define CURTXT_DEBUG
35
 
36
/* these variables are managed by this module but MUST be declared
37
 * into shark_mouse.c to prevent implicit inclusion of this module
38
 * when a user link the mouse library
39
 */
40
extern MOUSE_HANDLER show_mouse_handler;
41
extern MOUSE_HANDLER user_mouse_handler;
42
 
43
/* a mutex semaphore */
44
static sem_t mutex=-1;
45
 
46
/* Setup function */
47
int _mouse_cursor_init(int cmd, void(*my_show_cursor)(int, int), void(*my_restore_cursor)(int, int));
48
 
49
/*
50
 *
51
 * autocursor mouse handler
52
 *
53
 */
54
 
55
/* >=0 hide cursor; <0 show cursor */
56
static int mouse_cursor_state = 0;
57
 
58
/* mouse status */
549 mauro 59
int autocursormode = 0;
538 mauro 60
 
61
/* saved mouse_position */
62
static int  saved_x, saved_y;
63
 
64
static void dummy(int x,int y){}
65
 
66
static void (*show_cursor)(int x, int y) = dummy;
67
static void (*restore_cursor)(int x, int y) = dummy;
68
 
69
/* those are the 4 mouse handlers */
70
/* AUTOOFF    -> call the user handler with no mouse displayed */
71
/* WITHOUTSEM -> does not use a mutex semaphore */
72
 
73
/* with no flags */
74
static void autocursor_mouse_handler_1(MOUSE_EVT *event)
75
{
76
#ifdef CURTXT_DEBUG
77
        printk(KERN_DEBUG "mcurtxt.c: autocursor_mouse_handler_1\n");
78
#endif
79
 
80
        if (user_mouse_handler != NULL)
81
                user_mouse_handler(event);
82
 
83
        sem_wait(&mutex);
84
        if ( (mouse_cursor_state < 0) && (event->x != saved_x || event->y != saved_y)) {
85
                restore_cursor(saved_x, saved_y);
86
                saved_x = event->x;
87
                saved_y = event->y;
88
                show_cursor(saved_x, saved_y);
89
        }
90
        sem_post(&mutex);
91
}
92
 
93
/* with WITHOUTSEM flag*/
94
static void autocursor_mouse_handler_2(MOUSE_EVT *event)
95
{
96
#ifdef CURTXT_DEBUG
97
        printk(KERN_DEBUG "mcurtxt.c: autocursor_mouse_handler_2\n");
98
#endif
99
        if (user_mouse_handler != NULL)
100
                user_mouse_handler(event);
101
 
102
        if ( (mouse_cursor_state < 0) && (event->x != saved_x || event->y != saved_y)) {
103
                restore_cursor(saved_x, saved_y);
104
                saved_x = event->x;
105
                saved_y = event->y;
106
                show_cursor(saved_x, saved_y);
107
        }
108
}
109
 
110
/* with AUTOOFF flag*/
111
static void autocursor_mouse_handler_3(MOUSE_EVT *event)
112
{
113
#ifdef CURTXT_DEBUG
114
        printk(KERN_DEBUG "mcurtxt.c: autocursor_mouse_handler_3\n");
115
#endif
116
        sem_wait(&mutex);
117
 
118
        if (mouse_cursor_state < 0) {
119
                restore_cursor(saved_x, saved_y);
120
                saved_x = event->x;
121
                saved_y = event->y;
122
                if (user_mouse_handler != NULL)
123
                        user_mouse_handler(event);      
124
                show_cursor(saved_x, saved_y);
125
        } else if (user_mouse_handler != NULL)
126
                user_mouse_handler(event);
127
 
128
        sem_post(&mutex);
129
}
130
 
131
/* with WITHOUTSEM & AUTOOFF flags */
132
static void autocursor_mouse_handler_4(MOUSE_EVT *event)
133
{
134
#ifdef CURTXT_DEBUG
135
        printk(KERN_DEBUG "mcurtxt.c: autocursor_mouse_handler_4\n");
136
#endif
137
        if (mouse_cursor_state < 0) {
138
                restore_cursor(saved_x, saved_y);
139
                saved_x = event->x;
140
                saved_y = event->y;
141
                if (user_mouse_handler != NULL)
142
                        user_mouse_handler(event);      
143
                show_cursor(saved_x, saved_y);
144
        } else if (user_mouse_handler != NULL)
145
                user_mouse_handler(event);
146
}
147
 
148
/* --------------
149
 * TXT management
150
 * --------------
151
 */
152
 
153
/* text cursor shape */
154
#define DEFAULTTXTSHAPE 0x7700ffff
155
static DWORD saved_txtshape = DEFAULTTXTSHAPE;
156
static BYTE attr_andmask, attr_xormask;
157
static BYTE c_andmask, c_xormask;
158
 
159
/* saved values */
160
static BYTE saved_attr, saved_c;
161
 
162
/* show txt cursor */
163
void show_txt_cursor(int x, int y)
164
{
165
        int attr,c;
166
 
167
        getc_xy(x, y, &saved_attr, &saved_c);
168
        attr = (saved_attr & attr_andmask) ^ attr_xormask;
169
        c = (saved_c & c_andmask) ^ c_xormask;
170
        putc_xy(x, y, attr, c);
171
}
172
 
173
/* restore txt cursor */
174
static void restore_txt_cursor(int x, int y)
175
{
176
        putc_xy(x, y, saved_attr, saved_c);
177
}
178
 
179
/* define text shape */
180
int mouse_txtshape(DWORD img)
181
{
182
        int cond;
183
 
184
        if (img == DEFAULT)
185
                img = DEFAULTTXTSHAPE;
186
 
187
        cond = ( (autocursormode & STATUSMASK) == ENABLE && (autocursormode & TXTCURSOR) == TXTCURSOR );
188
 
189
        if (cond)
190
                restore_txt_cursor(saved_x, saved_y);
191
 
192
        saved_txtshape = img;
193
        c_andmask = img & 0xff;
194
        attr_andmask = (img & 0xff00) >> 8;
195
        c_xormask = (img & 0xff0000) >> 16;
196
        attr_xormask = (img & 0xff000000) >> 24;
197
 
198
#ifdef CURTXT_DEBUG
199
        printk(KERN_DEBUG "MouseTxt_Shape: %x %x %x %x\n", c_andmask, attr_andmask, c_xormask, attr_xormask);
200
#endif
201
        if (cond)
202
                show_txt_cursor(saved_x, saved_y);
203
 
204
        return 0;
205
}
206
 
207
/*
208
 * User interface to autocursor functions
209
 */
210
 
211
/* display the cursor */
212
#define MOUSE_ON() { \
213
        int unused; \
214
        unsigned long lunused; \
215
        mouse_cursor_state--; \
216
        if (mouse_cursor_state == -1) { \
549 mauro 217
                mouse_getposition(&saved_x, &saved_y, &unused, &lunused); \
538 mauro 218
                show_cursor(saved_x, saved_y); \
219
        } \
220
}
221
 
222
static void mouse_on_sem(void)
223
{
224
        sem_wait(&mutex);
225
        MOUSE_ON();
226
        sem_post(&mutex);
227
}
228
 
229
static void mouse_on_nosem(void)
230
{
231
        MOUSE_ON();
232
}
233
 
234
void (*mouse_on)(void)=mouse_on_nosem;
235
 
236
/* hide the cursor */
237
#define MOUSE_OFF() { \
238
        mouse_cursor_state++; \
239
        if (mouse_cursor_state == 0) \
240
                restore_cursor(saved_x, saved_y); \
241
}
242
 
243
void mouse_off_sem(void)
244
{
245
        sem_wait(&mutex);
246
        MOUSE_OFF();
247
        sem_post(&mutex);
248
}
249
 
250
void mouse_off_nosem(void)
251
{
252
        MOUSE_OFF();
253
}
254
 
255
void (*mouse_off)(void) = mouse_off_nosem;
256
 
257
static MOUSE_HANDLER wich_handler(int cmd)
258
{
259
        if ( ((cmd & WITHOUTSEM) == WITHOUTSEM) && ((cmd & AUTOOFF) == AUTOOFF) )
260
                return autocursor_mouse_handler_4;
261
        if ((cmd & WITHOUTSEM) == WITHOUTSEM)
262
                return autocursor_mouse_handler_2;
263
        if ((cmd & AUTOOFF) == AUTOOFF)
264
                return autocursor_mouse_handler_3;
265
        return autocursor_mouse_handler_1;
266
}
267
 
268
int mouse_txtcursor(int cmd)
269
{
270
        mouse_txtshape(saved_txtshape);  
271
        return _mouse_cursor_init(cmd|TXTCURSOR, show_txt_cursor, restore_txt_cursor);
272
}
273
 
274
/* Generic functions, both for text & graphics mode */
275
 
276
int _mouse_cursor_init(int cmd, void(*my_show_cursor)(int, int), void(*my_restore_cursor)(int, int))
277
{
278
        if (mutex == -1) {
279
                if (sem_init(&mutex, 0, 1) == -1)
280
                        return -1;
281
        }
282
 
283
#ifdef CURTXT_DEBUG
284
        printk(KERN_DEBUG "mouse_cursor_init: command %x\n", cmd);
285
#endif
286
 
287
        switch (cmd & STATUSMASK) {
288
                case DISABLE:
289
                        //show_mouse_handler = user_mouse_handler;
290
                        show_mouse_handler = NULL;
291
                        restore_cursor(saved_x, saved_y);
292
                        show_cursor = dummy;
293
                        restore_cursor = dummy;
294
                        mouse_cursor_state = 0;
295
                        break;
296
                case ENABLE:
297
                        restore_cursor(saved_x, saved_y);
298
                        restore_cursor = my_restore_cursor;
299
                        show_cursor = my_show_cursor;      
300
                        if ((autocursormode & STATUSMASK) == DISABLE) {
301
                                //user_mouse_handler = show_mouse_handler;
302
                                show_mouse_handler = wich_handler(cmd);
303
                        }
304
                        mouse_cursor_state = -1;
305
                        break;
306
                default:
307
                        return -1;
308
        }  
309
 
310
        autocursormode = cmd;
311
 
312
        if ((autocursormode & STATUSMASK) == ENABLE) {
313
                if ((cmd & WITHOUTSEM) == WITHOUTSEM) {
314
                        mouse_on = mouse_on_nosem;
315
                        mouse_off = mouse_off_nosem;
316
                } else {
317
                        mouse_on = mouse_on_sem;
318
                        mouse_off = mouse_off_sem;
319
                }
320
        }
321
 
322
        return 0;
323
}
324
 
325
void _mouse_cursor_getposition(int *xptr, int *yptr)
326
{
327
        *xptr = saved_x;
328
        *yptr = saved_y;
329
}