Subversion Repositories shark

Rev

Rev 547 | Go to most recent revision | Details | 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
 * Copyright (C) 2000 Paolo Gai
24
 *
25
 * This program is free software; you can redistribute it and/or modify
26
 * it under the terms of the GNU General Public License as published by
27
 * the Free Software Foundation; either version 2 of the License, or
28
 * (at your option) any later version.
29
 *
30
 * This program is distributed in the hope that it will be useful,
31
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33
 * GNU General Public License for more details.
34
 *
35
 * You should have received a copy of the GNU General Public License
36
 * along with this program; if not, write to the Free Software
37
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
38
 *
39
 */
40
 
41
/*
42
 *
43
 * Auto Pointer management
44
 *
45
 */
46
 
47
#include <kernel/kern.h>
48
#include <modules/sem.h>
49
 
50
#include "../include/drivers/shark_input26.h"
51
#include "../include/drivers/shark_mouse26.h"
52
 
53
//#define CURTXT_DEBUG
54
 
55
/* these variables are managed by this module but MUST be declared
56
 * into shark_mouse.c to prevent implicit inclusion of this module
57
 * when a user link the mouse library
58
 */
59
extern MOUSE_HANDLER show_mouse_handler;
60
extern MOUSE_HANDLER user_mouse_handler;
61
 
62
/* a mutex semaphore */
63
static sem_t mutex=-1;
64
 
65
/* Setup function */
66
int _mouse_cursor_init(int cmd, void(*my_show_cursor)(int, int), void(*my_restore_cursor)(int, int));
67
 
68
/*
69
 *
70
 * autocursor mouse handler
71
 *
72
 */
73
 
74
/* >=0 hide cursor; <0 show cursor */
75
static int mouse_cursor_state = 0;
76
 
77
/* mouse status */
78
static int autocursormode = 0;
79
 
80
/* saved mouse_position */
81
static int  saved_x, saved_y;
82
 
83
static void dummy(int x,int y){}
84
 
85
static void (*show_cursor)(int x, int y) = dummy;
86
static void (*restore_cursor)(int x, int y) = dummy;
87
 
88
/* those are the 4 mouse handlers */
89
/* AUTOOFF    -> call the user handler with no mouse displayed */
90
/* WITHOUTSEM -> does not use a mutex semaphore */
91
 
92
/* with no flags */
93
static void autocursor_mouse_handler_1(MOUSE_EVT *event)
94
{
95
#ifdef CURTXT_DEBUG
96
        printk(KERN_DEBUG "mcurtxt.c: autocursor_mouse_handler_1\n");
97
#endif
98
 
99
        if (user_mouse_handler != NULL)
100
                user_mouse_handler(event);
101
 
102
        sem_wait(&mutex);
103
        if ( (mouse_cursor_state < 0) && (event->x != saved_x || event->y != saved_y)) {
104
                restore_cursor(saved_x, saved_y);
105
                saved_x = event->x;
106
                saved_y = event->y;
107
                show_cursor(saved_x, saved_y);
108
        }
109
        sem_post(&mutex);
110
}
111
 
112
/* with WITHOUTSEM flag*/
113
static void autocursor_mouse_handler_2(MOUSE_EVT *event)
114
{
115
#ifdef CURTXT_DEBUG
116
        printk(KERN_DEBUG "mcurtxt.c: autocursor_mouse_handler_2\n");
117
#endif
118
        if (user_mouse_handler != NULL)
119
                user_mouse_handler(event);
120
 
121
        if ( (mouse_cursor_state < 0) && (event->x != saved_x || event->y != saved_y)) {
122
                restore_cursor(saved_x, saved_y);
123
                saved_x = event->x;
124
                saved_y = event->y;
125
                show_cursor(saved_x, saved_y);
126
        }
127
}
128
 
129
/* with AUTOOFF flag*/
130
static void autocursor_mouse_handler_3(MOUSE_EVT *event)
131
{
132
#ifdef CURTXT_DEBUG
133
        printk(KERN_DEBUG "mcurtxt.c: autocursor_mouse_handler_3\n");
134
#endif
135
        sem_wait(&mutex);
136
 
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
        sem_post(&mutex);
148
}
149
 
150
/* with WITHOUTSEM & AUTOOFF flags */
151
static void autocursor_mouse_handler_4(MOUSE_EVT *event)
152
{
153
#ifdef CURTXT_DEBUG
154
        printk(KERN_DEBUG "mcurtxt.c: autocursor_mouse_handler_4\n");
155
#endif
156
        if (mouse_cursor_state < 0) {
157
                restore_cursor(saved_x, saved_y);
158
                saved_x = event->x;
159
                saved_y = event->y;
160
                if (user_mouse_handler != NULL)
161
                        user_mouse_handler(event);      
162
                show_cursor(saved_x, saved_y);
163
        } else if (user_mouse_handler != NULL)
164
                user_mouse_handler(event);
165
}
166
 
167
/* --------------
168
 * TXT management
169
 * --------------
170
 */
171
 
172
/* text cursor shape */
173
#define DEFAULTTXTSHAPE 0x7700ffff
174
static DWORD saved_txtshape = DEFAULTTXTSHAPE;
175
static BYTE attr_andmask, attr_xormask;
176
static BYTE c_andmask, c_xormask;
177
 
178
/* saved values */
179
static BYTE saved_attr, saved_c;
180
 
181
/* show txt cursor */
182
void show_txt_cursor(int x, int y)
183
{
184
        int attr,c;
185
 
186
        getc_xy(x, y, &saved_attr, &saved_c);
187
        attr = (saved_attr & attr_andmask) ^ attr_xormask;
188
        c = (saved_c & c_andmask) ^ c_xormask;
189
        putc_xy(x, y, attr, c);
190
}
191
 
192
/* restore txt cursor */
193
static void restore_txt_cursor(int x, int y)
194
{
195
        putc_xy(x, y, saved_attr, saved_c);
196
}
197
 
198
/* define text shape */
199
int mouse_txtshape(DWORD img)
200
{
201
        int cond;
202
 
203
        if (img == DEFAULT)
204
                img = DEFAULTTXTSHAPE;
205
 
206
        cond = ( (autocursormode & STATUSMASK) == ENABLE && (autocursormode & TXTCURSOR) == TXTCURSOR );
207
 
208
        if (cond)
209
                restore_txt_cursor(saved_x, saved_y);
210
 
211
        saved_txtshape = img;
212
        c_andmask = img & 0xff;
213
        attr_andmask = (img & 0xff00) >> 8;
214
        c_xormask = (img & 0xff0000) >> 16;
215
        attr_xormask = (img & 0xff000000) >> 24;
216
 
217
#ifdef CURTXT_DEBUG
218
        printk(KERN_DEBUG "MouseTxt_Shape: %x %x %x %x\n", c_andmask, attr_andmask, c_xormask, attr_xormask);
219
#endif
220
        if (cond)
221
                show_txt_cursor(saved_x, saved_y);
222
 
223
        return 0;
224
}
225
 
226
/*
227
 * User interface to autocursor functions
228
 */
229
 
230
/* display the cursor */
231
#define MOUSE_ON() { \
232
        int unused; \
233
        unsigned long lunused; \
234
        mouse_cursor_state--; \
235
        if (mouse_cursor_state == -1) { \
236
                mouse_getpos(&saved_x, &saved_y, &unused, &lunused); \
237
                show_cursor(saved_x, saved_y); \
238
        } \
239
}
240
 
241
static void mouse_on_sem(void)
242
{
243
        sem_wait(&mutex);
244
        MOUSE_ON();
245
        sem_post(&mutex);
246
}
247
 
248
static void mouse_on_nosem(void)
249
{
250
        MOUSE_ON();
251
}
252
 
253
void (*mouse_on)(void)=mouse_on_nosem;
254
 
255
/* hide the cursor */
256
#define MOUSE_OFF() { \
257
        mouse_cursor_state++; \
258
        if (mouse_cursor_state == 0) \
259
                restore_cursor(saved_x, saved_y); \
260
}
261
 
262
void mouse_off_sem(void)
263
{
264
        sem_wait(&mutex);
265
        MOUSE_OFF();
266
        sem_post(&mutex);
267
}
268
 
269
void mouse_off_nosem(void)
270
{
271
        MOUSE_OFF();
272
}
273
 
274
void (*mouse_off)(void) = mouse_off_nosem;
275
 
276
static MOUSE_HANDLER wich_handler(int cmd)
277
{
278
        if ( ((cmd & WITHOUTSEM) == WITHOUTSEM) && ((cmd & AUTOOFF) == AUTOOFF) )
279
                return autocursor_mouse_handler_4;
280
        if ((cmd & WITHOUTSEM) == WITHOUTSEM)
281
                return autocursor_mouse_handler_2;
282
        if ((cmd & AUTOOFF) == AUTOOFF)
283
                return autocursor_mouse_handler_3;
284
        return autocursor_mouse_handler_1;
285
}
286
 
287
int mouse_txtcursor(int cmd)
288
{
289
        mouse_txtshape(saved_txtshape);  
290
        return _mouse_cursor_init(cmd|TXTCURSOR, show_txt_cursor, restore_txt_cursor);
291
}
292
 
293
/* Generic functions, both for text & graphics mode */
294
 
295
int _mouse_cursor_init(int cmd, void(*my_show_cursor)(int, int), void(*my_restore_cursor)(int, int))
296
{
297
        if (mutex == -1) {
298
                if (sem_init(&mutex, 0, 1) == -1)
299
                        return -1;
300
        }
301
 
302
#ifdef CURTXT_DEBUG
303
        printk(KERN_DEBUG "mouse_cursor_init: command %x\n", cmd);
304
#endif
305
 
306
        switch (cmd & STATUSMASK) {
307
                case DISABLE:
308
                        //show_mouse_handler = user_mouse_handler;
309
                        show_mouse_handler = NULL;
310
                        restore_cursor(saved_x, saved_y);
311
                        show_cursor = dummy;
312
                        restore_cursor = dummy;
313
                        mouse_cursor_state = 0;
314
                        break;
315
                case ENABLE:
316
                        restore_cursor(saved_x, saved_y);
317
                        restore_cursor = my_restore_cursor;
318
                        show_cursor = my_show_cursor;      
319
                        if ((autocursormode & STATUSMASK) == DISABLE) {
320
                                //user_mouse_handler = show_mouse_handler;
321
                                show_mouse_handler = wich_handler(cmd);
322
                        }
323
                        mouse_cursor_state = -1;
324
                        break;
325
                default:
326
                        return -1;
327
        }  
328
 
329
        autocursormode = cmd;
330
 
331
        if ((autocursormode & STATUSMASK) == ENABLE) {
332
                if ((cmd & WITHOUTSEM) == WITHOUTSEM) {
333
                        mouse_on = mouse_on_nosem;
334
                        mouse_off = mouse_off_nosem;
335
                } else {
336
                        mouse_on = mouse_on_sem;
337
                        mouse_off = mouse_off_sem;
338
                }
339
        }
340
 
341
        return 0;
342
}
343
 
344
void _mouse_cursor_getposition(int *xptr, int *yptr)
345
{
346
        *xptr = saved_x;
347
        *yptr = saved_y;
348
}