📄 devmouse.c
字号:
/* * Copyright (c) 1999, 2000, 2002 Greg Haerr <greg@censoft.com> * Portions Copyright (c) 2002 by Koninklijke Philips Electronics N.V. * Copyright (C) 1999 Bradley D. LaRonde (brad@ltc.com) * Copyright (c) 1991 David I. Bell * Permission is granted to use, distribute, or modify this source, * provided that this copyright notice remains intact. * * Device-independent top level mouse and cursor routines * * Reads data from mouse driver and tracks real position on the screen. * Intersection detection for cursor with auto removal * * Bradley D. LaRonde added absolute coordinates and z (pen pressure) Oct-1999 */#include <string.h>#include "device.h"/* * The following define specifies whether returned mouse * driver coordinates are adjusted when running in portrait * mode. If the mouse driver doesn't adjust returned values * when in portrait mode (as is the case for the iPAQ), then * this define should be set on. */#ifndef FLIP_MOUSE_IN_PORTRAIT_MODE#define FLIP_MOUSE_IN_PORTRAIT_MODE 1#endifstatic MWCOORD xpos; /* current x position of mouse */static MWCOORD ypos; /* current y position of mouse */static MWCOORD minx; /* minimum allowed x position */static MWCOORD maxx; /* maximum allowed x position */static MWCOORD miny; /* minimum allowed y position */static MWCOORD maxy; /* maximum allowed y position */static int scale; /* acceleration scale factor */static int thresh; /* acceleration threshhold */static int buttons; /* current state of buttons */static MWBOOL changed; /* mouse state has changed */static MWCOORD curminx; /* minimum x value of cursor */static MWCOORD curminy; /* minimum y value of cursor */static MWCOORD curmaxx; /* maximum x value of cursor */static MWCOORD curmaxy; /* maximum y value of cursor */static int curvisible; /* >0 if cursor is visible*/static MWBOOL curneedsrestore;/* cursor needs restoration after drawing*/static MWCOORD cursavx; /* saved cursor location*/static MWCOORD cursavy;static MWCOORD cursavx2;static MWCOORD cursavy2;static MWPIXELVAL curfg; /* foreground color of cursor */static MWPIXELVAL curbg; /* background color of cursor */static MWPIXELVAL cursavbits[MWMAX_CURSOR_SIZE * MWMAX_CURSOR_SIZE];static MWIMAGEBITS cursormask[MWMAX_CURSOR_BUFLEN];static MWIMAGEBITS cursorcolor[MWMAX_CURSOR_BUFLEN];extern int gr_mode;/* Advance declarations */static int filter_relative(int, int, int, int *, int *, int, int);static int filter_transform(int, int *, int *);#if FLIP_MOUSE_IN_PORTRAIT_MODEstatic int filter_relrotate(int, int *xpos, int *ypos, int x, int y);static int filter_absrotate(int, int *xpos, int *ypos);#endif/** * Initialize the mouse. * This sets its position to (0, 0) with no boundaries and no buttons pressed. * * @return < 0 on error, or mouse fd on success */intGdOpenMouse(void){ int fd; /* init mouse position info*/ buttons = 0; xpos = 0; ypos = 0; minx = MIN_MWCOORD; miny = MIN_MWCOORD; maxx = MAX_MWCOORD; maxy = MAX_MWCOORD; changed = TRUE; /* init cursor position and size info*/ curvisible = 0; curneedsrestore = FALSE; curminx = minx; curminy = miny; curmaxx = curminx + MWMAX_CURSOR_SIZE - 1; curmaxy = curminy + MWMAX_CURSOR_SIZE - 1; if ((fd = mousedev.Open(&mousedev)) == -1) return -1; /* get default acceleration settings*/ mousedev.GetDefaultAccel(&scale, &thresh); /* Force all mice to !RAW mode until the values are set */ mousedev.flags &= ~MOUSE_RAW; /* handle null mouse driver by hiding cursor*/ if(fd == -2) GdHideCursor(&scrdev); return fd;}/** * * Terminate the use of the mouse. */voidGdCloseMouse(void){ mousedev.Close();}/** * Gets the buttons which are supported by the mouse. * * @param buttons On return, a bitmask indicating the buttons which are * supported by the mouse. */voidGdGetButtonInfo(int *buttons){ *buttons = mousedev.GetButtonInfo();}/** * Restrict the coordinates of the mouse to the specified coordinates. * * @param newminx Minimum X co-ordinate. * @param newminy Minimum Y co-ordinate. * @param newmaxx Maximum X co-ordinate. * @param newmaxy Maximum Y co-ordinate. */voidGdRestrictMouse(MWCOORD newminx, MWCOORD newminy, MWCOORD newmaxx, MWCOORD newmaxy){ minx = newminx; miny = newminy; maxx = newmaxx; maxy = newmaxy; GdMoveMouse(xpos, ypos);}/** * Set the acceleration threshhold and scale factors. * Acceleration makes the cursor move further for faster movements. * Basically, at mouse speeds above the threshold, the excess distance * moved is multiplied by the scale factor. For example, with a threshhold * of 5 and a scale of 3, the following gives examples of the original and * modified mouse movements: * input: 0 4 5 6 9 20 * output: 0 4 5 8 17 50 * * @param newthresh Threshold where acceleration starts * @param newscale Acceleration factor. */voidGdSetAccelMouse(int newthresh, int newscale){ if (newthresh < 0) newthresh = 0; if (newscale < 0) newscale = 0; thresh = newthresh; scale = newscale;}/** * Move the mouse to the specified coordinates. * The location is limited by the current mouse coordinate restrictions. * * @param newx New X co-ordinate. * @param newy New Y co-ordinate. */voidGdMoveMouse(MWCOORD newx, MWCOORD newy){ if (!(mousedev.flags & MOUSE_RAW)) { if (newx < minx) newx = minx; if (newx > maxx) newx = maxx; if (newy < miny) newy = miny; if (newy > maxy) newy = maxy; } if (newx == xpos && newy == ypos) return; changed = TRUE; xpos = newx; ypos = newy;}/** * Read the current location and button states of the mouse. * Returns -1 on read error. * Returns 0 if no new data is available from the mouse driver, * or if the new data shows no change in button state or position. * Returns 1 if the mouse driver tells us a changed button state * or position. Button state and position are always both returned, * even if only one or the other changes. * Do not block. * * @param px On return, holds the mouse X co-ordinate. * @param py On return, holds the mouse Y co-ordinate. * @param pb On return, holds the buttons status. * @return -1 on error, 0 if mouse has not moved, 1 if mouse has moved. */intGdReadMouse(MWCOORD *px, MWCOORD *py, int *pb){ MWCOORD x, y, z; int newbuttons; /* new button state */ int status; /* status of reading mouse */ MWCOORD dx, dy; *px = xpos; *py = ypos; *pb = buttons; if (changed) { changed = FALSE; return 1; } /* read the mouse position */ status = mousedev.Read(&x, &y, &z, &newbuttons); if (status < 0) return -1; /* no new info from the mouse driver? */ if (status == 0) return 0; /* Relative mice have their own process */ if (status == 1) { int rx, ry; filter_relative(status, thresh, scale, &rx, &ry, x, y);#if FLIP_MOUSE_IN_PORTRAIT_MODE dx = xpos; dy = ypos; filter_relrotate(status, &dx, &dy, x, y);#else dx = xpos + rx; dy = ypos + ry;#endif } else { dx = x; dy = y; /* Transformed coords should already have their rotation handled */ if (mousedev.flags & MOUSE_TRANSFORM) { if (!filter_transform(status, &dx, &dy)) return 0; }#if FLIP_MOUSE_IN_PORTRAIT_MODE if (!(mousedev.flags & MOUSE_RAW)) filter_absrotate(status, &dx, &dy);#endif } /* * At this point, we should have a valid mouse point. Check the button * state and set the flags accordingly. We do this *after* the filters, * because some of the filters (like the transform) need to be called * several times before we get valid data. */ if (buttons != newbuttons) { changed = TRUE; buttons = newbuttons; } /* Finally, move the mouse */ if (status != 3) GdMoveMouse(dx, dy); /* anything change? */ if (!changed) return 0; /* report new mouse data */ changed = FALSE; *px = xpos; *py = ypos; *pb = buttons; return 1;}/** * Set the cursor position. * * @param newx New X co-ordinate. * @param newy New Y co-ordinate. */voidGdMoveCursor(MWCOORD newx, MWCOORD newy){ MWCOORD shiftx; MWCOORD shifty; shiftx = newx - curminx; shifty = newy - curminy; if(shiftx == 0 && shifty == 0) return; curminx += shiftx; curmaxx += shiftx; curminy += shifty; curmaxy += shifty; /* Restore the screen under the mouse pointer*/ GdHideCursor(&scrdev); /* Draw the new pointer*/ GdShowCursor(&scrdev);}/** * return current mouse position in x, y * * @param px On return, holds the cursor X co-ordinate. * @param py On return, holds the cursor Y co-ordinate. * @return TRUE iff cursor is visible. */MWBOOLGdGetCursorPos(MWCOORD *px, MWCOORD *py){ *px = xpos; *py = ypos; return curvisible > 0; /* return TRUE if visible*/}/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -