Subversion Repositories shark

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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