OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [services/] [gfx/] [mw/] [v2_0/] [src/] [engine/] [devmouse.c] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
/*
2
 * Copyright (c) 1999, 2000 Greg Haerr <greg@censoft.com>
3
 * Copyright (C) 1999 Bradley D. LaRonde (brad@ltc.com)
4
 * Copyright (c) 1991 David I. Bell
5
 * Permission is granted to use, distribute, or modify this source,
6
 * provided that this copyright notice remains intact.
7
 *
8
 * Device-independent top level mouse and cursor routines
9
 *
10
 * Reads data from mouse driver and tracks real position on the screen.
11
 * Intersection detection for cursor with auto removal
12
 *
13
 * Bradley D. LaRonde added absolute coordinates and z (pen pressure) Oct-1999
14
 */
15
#include <string.h>
16
#include "device.h"
17
 
18
/*
19
 * The following define specifies whether returned mouse
20
 * driver coordinates are adjusted when running in portrait
21
 * mode.  If the mouse driver doesn't adjust returned values
22
 * when in portrait mode (as is the case for the iPAQ), then
23
 * this define should be set on.
24
 */
25
#define FLIP_MOUSE_IN_PORTRAIT_MODE     1
26
 
27
static MWCOORD  xpos;           /* current x position of mouse */
28
static MWCOORD  ypos;           /* current y position of mouse */
29
static MWCOORD  minx;           /* minimum allowed x position */
30
static MWCOORD  maxx;           /* maximum allowed x position */
31
static MWCOORD  miny;           /* minimum allowed y position */
32
static MWCOORD  maxy;           /* maximum allowed y position */
33
static int      scale;          /* acceleration scale factor */
34
static int      thresh;         /* acceleration threshhold */
35
static int      buttons;        /* current state of buttons */
36
static MWBOOL   changed;        /* mouse state has changed */
37
 
38
static MWCOORD  curminx;        /* minimum x value of cursor */
39
static MWCOORD  curminy;        /* minimum y value of cursor */
40
static MWCOORD  curmaxx;        /* maximum x value of cursor */
41
static MWCOORD  curmaxy;        /* maximum y value of cursor */
42
static int      curvisible;     /* >0 if cursor is visible*/
43
static MWBOOL   curneedsrestore;/* cursor needs restoration after drawing*/
44
static MWCOORD  cursavx;        /* saved cursor location*/
45
static MWCOORD  cursavy;
46
static MWCOORD  cursavx2;
47
static MWCOORD  cursavy2;
48
static MWPIXELVAL curfg;                /* foreground color of cursor */
49
static MWPIXELVAL curbg;                /* background color of cursor */
50
static MWPIXELVAL cursavbits[MWMAX_CURSOR_SIZE * MWMAX_CURSOR_SIZE];
51
static MWIMAGEBITS cursormask[MWMAX_CURSOR_SIZE];
52
static MWIMAGEBITS cursorcolor[MWMAX_CURSOR_SIZE];
53
 
54
extern int gr_mode;
55
 
56
/*
57
 * Initialize the mouse.
58
 * This sets its position to (0, 0) with no boundaries and no buttons pressed.
59
 * Returns < 0 on error, or mouse fd on success
60
 */
61
int
62
GdOpenMouse(void)
63
{
64
        int fd;
65
 
66
        /* init mouse position info*/
67
        buttons = 0;
68
        xpos = 0;
69
        ypos = 0;
70
        minx = MIN_MWCOORD;
71
        miny = MIN_MWCOORD;
72
        maxx = MAX_MWCOORD;
73
        maxy = MAX_MWCOORD;
74
        changed = TRUE;
75
 
76
        /* init cursor position and size info*/
77
        curvisible = 0;
78
        curneedsrestore = FALSE;
79
        curminx = minx;
80
        curminy = miny;
81
        curmaxx = curminx + MWMAX_CURSOR_SIZE - 1;
82
        curmaxy = curminy + MWMAX_CURSOR_SIZE - 1;
83
 
84
        if ((fd = mousedev.Open(&mousedev)) == -1)
85
                return -1;
86
 
87
        /* get default acceleration settings*/
88
        mousedev.GetDefaultAccel(&scale, &thresh);
89
 
90
        /* handle null mouse driver by hiding cursor*/
91
        if(fd == -2)
92
                GdHideCursor(&scrdev);
93
        return fd;
94
}
95
 
96
/*
97
 * Terminate the use of the mouse.
98
 */
99
void
100
GdCloseMouse(void)
101
{
102
        mousedev.Close();
103
}
104
 
105
void
106
GdGetButtonInfo(int *buttons)
107
{
108
        *buttons = mousedev.GetButtonInfo();
109
}
110
 
111
/*
112
 * Restrict the coordinates of the mouse to the specified coordinates.
113
 */
114
void
115
GdRestrictMouse(MWCOORD newminx, MWCOORD newminy, MWCOORD newmaxx,
116
        MWCOORD newmaxy)
117
{
118
        minx = newminx;
119
        miny = newminy;
120
        maxx = newmaxx;
121
        maxy = newmaxy;
122
        GdMoveMouse(xpos, ypos);
123
}
124
 
125
/*
126
 * Set the acceleration threshhold and scale factors.
127
 * Acceleration makes the cursor move further for faster movements.
128
 * Basically, at mouse speeds above the threshold, the excess distance
129
 * moved is multiplied by the scale factor.  For example, with a threshhold
130
 * of 5 and a scale of 3, the following gives examples of the original and
131
 * modified mouse movements:
132
 *      input:          0        4       5       6       9       20
133
 *      output:         0        4       5       8       17      50
134
 */
135
void
136
GdSetAccelMouse(int newthresh, int newscale)
137
{
138
        if (newthresh < 0)
139
                newthresh = 0;
140
        if (newscale < 0)
141
                newscale = 0;
142
        thresh = newthresh;
143
        scale = newscale;
144
}
145
 
146
/*
147
 * Move the mouse to the specified coordinates.
148
 * The location is limited by the current mouse coordinate restrictions.
149
 */
150
void
151
GdMoveMouse(MWCOORD newx, MWCOORD newy)
152
{
153
        if (newx < minx)
154
                newx = minx;
155
        if (newx > maxx)
156
                newx = maxx;
157
        if (newy < miny)
158
                newy = miny;
159
        if (newy > maxy)
160
                newy = maxy;
161
        if (newx == xpos && newy == ypos)
162
                return;
163
 
164
        changed = TRUE;
165
        xpos = newx;
166
        ypos = newy;
167
}
168
 
169
/*
170
 * Read the current location and button states of the mouse.
171
 * Returns -1 on read error.
172
 * Returns 0 if no new data is available from the mouse driver,
173
 * or if the new data shows no change in button state or position.
174
 * Returns 1 if the mouse driver tells us a changed button state
175
 * or position. Button state and position are always both returned,
176
 * even if only one or the other changes.
177
 * Do not block.
178
 */
179
int
180
GdReadMouse(MWCOORD *px, MWCOORD *py, int *pb)
181
{
182
        MWCOORD x, y, z;
183
        int     newbuttons;     /* new button state */
184
        int     sign;           /* sign of change */
185
        int     status;         /* status of reading mouse */
186
 
187
        *px = xpos;
188
        *py = ypos;
189
        *pb = buttons;
190
 
191
        if (changed) {
192
                changed = FALSE;
193
                return 1;
194
        }
195
 
196
        /* read the mouse position */
197
        status = mousedev.Read(&x, &y, &z, &newbuttons);
198
        if (status < 0)
199
                return -1;
200
 
201
        /* no new info from the mouse driver? */
202
        if (status == 0)
203
                return 0;
204
 
205
        /* has the button state changed? */
206
        if (buttons != newbuttons) {
207
                changed = TRUE;
208
                buttons = newbuttons;
209
        }
210
 
211
        /* depending on the kind of data that we have */
212
        switch(status) {
213
        case 1: /* relative position change reported, figure new position */
214
                sign = 1;
215
                if (x < 0) {
216
                        sign = -1;
217
                        x = -x;
218
                }
219
                if (x > thresh)
220
                        x = thresh + (x - thresh) * scale;
221
                x *= sign;
222
 
223
                sign = 1;
224
                if (y < 0) {
225
                        sign = -1;
226
                        y = -y;
227
                }
228
                if (y > thresh)
229
                        y = thresh + (y - thresh) * scale;
230
                y *= sign;
231
 
232
#if FLIP_MOUSE_IN_PORTRAIT_MODE
233
                if (scrdev.portrait == MWPORTRAIT_RIGHT)
234
                        GdMoveMouse(xpos + y, ypos - x);        /* right*/
235
                else if (scrdev.portrait == MWPORTRAIT_LEFT)
236
                        GdMoveMouse(xpos - y, ypos + x);        /* left*/
237
                else if (scrdev.portrait == MWPORTRAIT_DOWN)
238
                        GdMoveMouse(xpos + x, ypos - y);        /* down*/
239
                else
240
#endif
241
                        GdMoveMouse(xpos + x, ypos + y);
242
                break;
243
 
244
        case 2: /* absolute position reported */
245
#if FLIP_MOUSE_IN_PORTRAIT_MODE
246
                if (scrdev.portrait == MWPORTRAIT_RIGHT)
247
                        GdMoveMouse(y, scrdev.xres - x - 1);    /* right*/
248
                else if (scrdev.portrait == MWPORTRAIT_LEFT)
249
                        GdMoveMouse(scrdev.yres - y - 1, x);    /* left*/
250
                else if (scrdev.portrait == MWPORTRAIT_DOWN)
251
                        GdMoveMouse(x, scrdev.yres - y - 1);    /* down?*/
252
                else
253
#endif
254
                        GdMoveMouse(x, y);
255
                break;
256
 
257
        case 3: /* only button data is available */
258
                break;
259
        }
260
 
261
        /* didn't anything change? */
262
        if (!changed)
263
                return 0;
264
 
265
        /* report new mouse data */
266
        changed = FALSE;
267
        *px = xpos;
268
        *py = ypos;
269
        *pb = buttons;
270
        return 1;
271
}
272
 
273
/*
274
 * Set the cursor position.
275
 */
276
void
277
GdMoveCursor(MWCOORD newx, MWCOORD newy)
278
{
279
        MWCOORD shiftx;
280
        MWCOORD shifty;
281
 
282
        shiftx = newx - curminx;
283
        shifty = newy - curminy;
284
        if(shiftx == 0 && shifty == 0)
285
                return;
286
        curminx += shiftx;
287
        curmaxx += shiftx;
288
        curminy += shifty;
289
        curmaxy += shifty;
290
 
291
        /* Restore the screen under the mouse pointer*/
292
        GdHideCursor(&scrdev);
293
 
294
        /* Draw the new pointer*/
295
        GdShowCursor(&scrdev);
296
}
297
 
298
/* return current mouse position in x, y*/
299
MWBOOL
300
GdGetCursorPos(MWCOORD *px, MWCOORD *py)
301
{
302
        *px = xpos;
303
        *py = ypos;
304
        return curvisible > 0;   /* return TRUE if visible*/
305
}
306
 
307
/*
308
 * Set the cursor size and bitmaps.
309
 */
310
void
311
GdSetCursor(PMWCURSOR pcursor)
312
{
313
        int     bytes;
314
 
315
        GdHideCursor(&scrdev);
316
        curmaxx = curminx + pcursor->width - 1;
317
        curmaxy = curminy + pcursor->height - 1;
318
 
319
        curfg = GdFindColor(pcursor->fgcolor);
320
        curbg = GdFindColor(pcursor->bgcolor);
321
        bytes = MWIMAGE_WORDS(pcursor->width) * pcursor->height
322
                        * sizeof(MWIMAGEBITS);
323
        memcpy(cursorcolor, pcursor->image, bytes);
324
        memcpy(cursormask, pcursor->mask, bytes);
325
 
326
        GdShowCursor(&scrdev);
327
}
328
 
329
 
330
/*
331
 * Draw the mouse pointer.  Save the screen contents underneath
332
 * before drawing. Returns previous cursor state.
333
 */
334
int
335
GdShowCursor(PSD psd)
336
{
337
        MWCOORD                 x;
338
        MWCOORD                 y;
339
        MWPIXELVAL *    saveptr;
340
        MWIMAGEBITS *   cursorptr;
341
        MWIMAGEBITS *   maskptr;
342
        MWIMAGEBITS     curbit, cbits, mbits;
343
        MWPIXELVAL      oldcolor;
344
        MWPIXELVAL      newcolor;
345
        int             oldmode;
346
        int             prevcursor = curvisible;
347
 
348
        if(++curvisible != 1)
349
                return prevcursor;
350
        oldmode = gr_mode;
351
        gr_mode = MWMODE_COPY;
352
 
353
        saveptr = cursavbits;
354
        cursavx = curminx;
355
        cursavy = curminy;
356
        cursavx2 = curmaxx;
357
        cursavy2 = curmaxy;
358
        cursorptr = cursorcolor;
359
        maskptr = cursormask;
360
 
361
        for (y = curminy; y <= curmaxy; y++) {
362
                cbits = *cursorptr++;
363
                mbits = *maskptr++;
364
                curbit = MWIMAGE_FIRSTBIT;
365
                for (x = curminx; x <= curmaxx; x++) {
366
                        if(x >= 0 && x < psd->xvirtres &&
367
                           y >= 0 && y < psd->yvirtres) {
368
                                oldcolor = psd->ReadPixel(psd, x, y);
369
                                if (curbit & mbits) {
370
                                        newcolor = (curbit&cbits)? curbg: curfg;
371
                                        if (oldcolor != newcolor)
372
                                               psd->DrawPixel(psd, x, y, newcolor);
373
                                }
374
                                *saveptr++ = oldcolor;
375
                        }
376
                        curbit = MWIMAGE_NEXTBIT(curbit);
377
                }
378
        }
379
 
380
        gr_mode = oldmode;
381
        return prevcursor;
382
}
383
 
384
/*
385
 * Restore the screen overwritten by the cursor.
386
 */
387
int
388
GdHideCursor(PSD psd)
389
{
390
        MWPIXELVAL *    saveptr;
391
        MWCOORD                 x, y;
392
        int             oldmode;
393
        int             prevcursor = curvisible;
394
 
395
        if(curvisible-- <= 0)
396
                return prevcursor;
397
        oldmode = gr_mode;
398
        gr_mode = MWMODE_COPY;
399
 
400
        saveptr = cursavbits;
401
        for (y = cursavy; y <= cursavy2; y++) {
402
                for (x = cursavx; x <= cursavx2; x++) {
403
                        if(x >= 0 && x < psd->xvirtres &&
404
                           y >= 0 && y < psd->yvirtres) {
405
                                psd->DrawPixel(psd, x, y, *saveptr++);
406
                        }
407
                }
408
        }
409
        gr_mode = oldmode;
410
        return prevcursor;
411
}
412
 
413
/* Check to see if the mouse pointer is about to be overwritten.
414
 * If so, then remove the cursor so that the graphics operation
415
 * works correctly.  If the cursor is removed, then this fact will
416
 * be remembered and a later call to GdFixCursor will restore it.
417
 */
418
void
419
GdCheckCursor(PSD psd,MWCOORD x1,MWCOORD y1,MWCOORD x2,MWCOORD y2)
420
{
421
        MWCOORD temp;
422
 
423
        if (curvisible <= 0 || (psd->flags & PSF_SCREEN) == 0)
424
                return;
425
 
426
        if (x1 > x2) {
427
                temp = x1;
428
                x1 = x2;
429
                x2 = temp;
430
        }
431
        if (y1 > y2) {
432
                temp = y1;
433
                y1 = y2;
434
                y2 = temp;
435
        }
436
        if (x1 > curmaxx || x2 < curminx || y1 > curmaxy || y2 < curminy)
437
                return;
438
 
439
        GdHideCursor(psd);
440
        curneedsrestore = TRUE;
441
}
442
 
443
 
444
/* Redisplay the cursor if it was removed because of a graphics operation. */
445
void
446
GdFixCursor(PSD psd)
447
{
448
        if (curneedsrestore && (psd->flags & PSF_SCREEN)) {
449
                GdShowCursor(psd);
450
                curneedsrestore = FALSE;
451
        }
452
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.