home *** CD-ROM | disk | FTP | other *** search
- /*
- MoveMouse.c
-
- Dan Sears (sears@netcom.com) writes,
- "The Apple file MoveMouse.c is based on some code from Jon Wtte and it uses the
- Cursor Device Manager. You should read the tech note that describes the
- API for this manager, "HW 01 - ADB (The Untold Story: Space Aliens ate my
- mouse)".
-
- This is from Apple. It may be useful to people who need an equivalent to
- SetMouse.c that works on ALL Macs. - dgp, 12/94
-
- NOTE: The THINK C 6 compiler choked on one of the (legal) recursive structure
- definitions in the original version of this file. (The structure definitions
- are now all in Apple's CursorDevices.h file.) The THINK C 7 and CodeWarrior
- compilers don't complain.
-
- From Apple Developer Services:
-
- In the past, moving the cursor programatically on the Macintosh has
- entailed the manipulation of low memory globals. This has worked for
- all macs for quite some time. But with the advent of the power book
- series and many 3rd party track balls, it has become apparent that the
- low memory globals no longer contained adequate information for the
- system to truly support all types of positioning devices. So, with the
- new Centris CPU's, and CPU's from all lines after we are implementing a
- new tool set that provides a much more comprehensive set of utilities
- for dealing with the cursor location and mouse button state. We call
- this new manager the Cursor Device Manager, and DTS will be documenting
- it in a future tech not, but since many machines are now shipping that
- do not use the traditional low memory globals to get the mouse location
- and position the mouse, it is important to "leak" a little of the tech
- note information early.
-
- First, the CDM (Cursor device manager) has its own trap ($AADB) and you
- can determine if a machine supports the CDM by using the standard TrapAvailable
- routine. (See inside mac for the source code to Trap
- Available). Once you have determined that the CDM exists, you should
- try not to use the low memory globals RawMouse and MTemp to get the
- current cursor location, or set a new cursor location. Instead the CDM
- provides 3 calls which you can use to get this info, the first is
- CrsrDevNextDevice which will give you the next cursor device record in
- the CDM list, if you pass it a NULL, it will give you the head of the
- list. Once you have the first cursor device you can use it to get the
- Cursor data record which contains the current cursor location in its
- where field. You can set the current location with either the
- CrsrDevMove (which moves relative to the current location) or the
- CrsrDevMoveTo (which moves to an absolute location) routines.
-
- The following is some sample code which implement some generic routines
- that work on all macintoshes. These routines are written not for speed
- but for clarity, so if you are writing a device driver or time critical
- code there are some things that you can do to speed up these routines,
- like predetermine at the outset if the CDM exists, this way you
- wouldn't have to check every time you want to use it, and getting the
- cursor device only once and keeping the pointer to it around between
- calls.
-
- Anyway, here is some sample code that moves the mouse diagonally until
- the mouse button is pressed. For a further explanation of how the older
- low memory global method works, see the developer support tech info
- database on AppleLink.
-
- HISTORY:
- 8/94 dgp Downloaded from ftp://nada.kth.se/pub/hacks/mac-faq/MoveMouse.c
- 10/94 dgp made minimal changes so that it would compile in CodeWarrior C. Untested.
- 12/94 dgp updated for compatibility with Universal Header "CursorDevices.h". Untested.
- 1/5/95 dgp updated for compatiblity with Universal Headers 2, in which CursorDevices.h was
- extensively changed. Untested.
- */
-
- #include "VideoToolbox.h" // TrapAvailable
- #if !UNIVERSAL_HEADERS
- #error "Sorry, this file cannot be compiled without Apple's Universal Header files, preferably v. 2"
- #endif
- #include <CursorDevices.h>
- #define _CursorDeviceDispatch 0xAADB // new cursor device manager trap
- #if __powerc
- #define CallCursorTask() // not needed on powerpc, so give empty definition
- #else
- pascal void CallCursorTask(void) = {0x2078,0x08EE,0x4E90};
- /*
- MOVE.L jCrsrTask,A0
- JSR (A0)
- */
- #endif
- // Old style mouse moving equates for the low memory globals
- #define xRawMouse 0x082C // low memory global that has current mouse loc
- #define xMTemp 0x0828 // low memory global that has current mouse loc
- #define xCrsrNew 0x08CE // set after you change mtemp and rawmouse
- #define xCrsrCouple 0x08CF // true if the cursor is tied to the mouse
-
- void GetMouseDevicePosition(Point *currentPoint);
- void SetMouseDevicePosition(Point newPoint);
- void SetMouseDevicePositionRel(Point newPoint);
- void FudgeCursor(void);
-
- #if !defined(UNIVERSAL_HEADERS) || UNIVERSAL_HEADERS<2
- #define CursorDevice CrsrDevice
- #define CursorDeviceNextDevice CrsrDevNextDevice
- #define CursorDeviceMove CrsrDevMove
- #define CursorDeviceMoveTo CrsrDevMoveTo
- #endif
-
- void GetMouseDevicePosition(Point *currentPoint)
- {
- // This routine returns the current cursor location
- CursorDevice *firstMouse;
- if (TrapAvailable(_CursorDeviceDispatch)) {
- // If we get here we have the CDM
- firstMouse = NULL; // start at head of cursor dev list
- CursorDeviceNextDevice(&firstMouse); // get the next cursor device
- // Now get the current cursor location from the Cursor data
- *currentPoint = firstMouse->whichCursor->where;
- } else {
- // No CDM so we use the low memory global
- *currentPoint = *(Point *)xRawMouse;
- }
- }
-
- void SetMouseDevicePosition(Point newPoint)
- {
- // This routine sets the mouse position to the passed point
- CursorDevice *firstMouse;
-
- if (TrapAvailable(_CursorDeviceDispatch)) {
- // If we get here we have the CDM
- firstMouse = NULL; // start at head of cursor dev list
- CursorDeviceNextDevice(&firstMouse); // get the next cursor device
- // Call CDM to move the mouse
- CursorDeviceMoveTo(firstMouse,(long)newPoint.h,(long)newPoint.v);
- } else {
- // No CDM so we use the low memory globals
- *(Point *)xRawMouse = newPoint;
- *(Point *)xMTemp = newPoint;
- *(Ptr)xCrsrNew = *(Ptr)xCrsrCouple; // Set CrsrNew if coupled
- CallCursorTask(); // must call jCrsrTask to update system
- }
- }
-
- void SetMouseDevicePositionRel(Point newPoint)
- {
- // Adds the passed point to the current mouse location
- CursorDevice *firstMouse;
- Point tempPt;
- if (TrapAvailable(_CursorDeviceDispatch)) {
- // If we get here we have the CDM
- firstMouse = NULL; // start at head of cursor dev list
- CursorDeviceNextDevice(&firstMouse); // get the next cursor device
- // Call CDM to move the mouse relative
- CursorDeviceMove(firstMouse,(long)newPoint.h,(long)newPoint.v);
- } else {
- // No CDM so we use the low memory globals
- tempPt = *(Point *)xRawMouse;
- tempPt.h += newPoint.h;
- tempPt.v += newPoint.v;
- *(Point *)xRawMouse = tempPt;
- *(Point *)xMTemp = tempPt;
- *(Ptr)xCrsrNew = *(Ptr)xCrsrCouple;
- CallCursorTask();
- }
- }
-
- void FudgeCursor(void)
- {
- // Now here is a routine that you can drop into the traffic light sample that
- // can be called to slowly move the mouse to the lower right hand corner of
- // the screen it exits when the mouse button is held down
-
- Point RandPt;
- long fred;
-
- GetMouseDevicePosition(&RandPt);
- do {
- RandPt.h += 1; // Bump to the next pixel across
- RandPt.v += 1; // Bump to the next pixel down
-
- SetMouseDevicePosition(RandPt); // move absolute
-
- Delay(10,&fred); // wait 10 ticks for smooth drawing
- } while (!Button()); // quit when the button is down.
- }
-