home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Fred Fish Collection 1.5
/
ffcollection-1-5-1992-11.iso
/
ff_disks
/
001-099
/
ff005.lzh
/
mouse
/
mouse.c
< prev
Wrap
C/C++ Source or Header
|
1986-01-11
|
8KB
|
282 lines
/*
*
* DISCLAIMER:
*
* This program is provided as a service to the programmer
* community to demonstrate one or more features of the Amiga
* personal computer. These code samples may be freely used
* for commercial or noncommercial purposes.
*
* Commodore Electronics, Ltd ("Commodore") makes no
* warranties, either expressed or implied, with respect
* to the program described herein, its quality, performance,
* merchantability, or fitness for any particular purpose.
* This program is provided "as is" and the entire risk
* as to its quality and performance is with the user.
* Should the program prove defective following its
* purchase, the user (and not the creator of the program,
* Commodore, their distributors or their retailers)
* assumes the entire cost of all necessary damages. In
* no event will Commodore be liable for direct, indirect,
* incidental or consequential damages resulting from any
* defect in the program even if it has been advised of the
* possibility of such damages. Some laws do not allow
* the exclusion or limitation of implied warranties or
* liabilities for incidental or consequential damages,
* so the above limitation or exclusion may not apply.
*
*/
/* mouse.c */
/* *********************************************************************** */
/* mouse test, for right game port on the Amiga.
Notes: The right port is used for this test because the input.device
task is busy continuously with the lefthand port, feeding input events
to intuition or console devices. If Intuition is not activated
(applications which take over the whole machine may decide not to
activate Intuition), and if no console.device is activated either,
the input.device will never activate... allowing the application
free reign to use either the left OR the right hand joystick/mouse
port. If either intuition or the console device are activated,
the lefthand port will yield, at best, every alternate input
event to an external application such as this test program.
This will undoubtedly mess up either of the two applications
and should therefore be avoided. It was ok to use the right
port in this case, since the system has no particular interest
in monitoring it.
Using a function called SetMPort, you can reconfigure so that the
mouse is expected in the other port, but that isnt demonstrated here.
Author: Rob Peck, 12/1/85
This code may be freely utilized to develop programs for the Amiga.
*********************************************************************** */
#include <exec/types.h>
#include <exec/devices.h>
#include <graphics/gfx.h>
#include <devices/gameport.h>
#include <devices/inputevent.h>
LONG GfxBase=0;
#define XMOVE 10
#define YMOVE 10
#define MAX(m,n) (m > n ? m : n)
struct GamePortTrigger mousetrigger = {
GPTF_UPKEYS + GPTF_DOWNKEYS,
1800,
XMOVE,
YMOVE };
/* trigger on all mouse key transitions, and every 30 seconds, and
for any 10 in an x or y direction */
struct InputEvent *game_data; /* pointer into the returned data area
* where input event has been sent */
SHORT error;
struct IOStdReq *game_io_msg;
BYTE gamebuffer[sizeof( struct InputEvent )];
BYTE *gamedata;
SHORT testval;
struct MsgPort *game_msg_port;
SHORT movesize;
extern struct MsgPort *CreatePort();
extern struct IOStdReq *CreateStdIO();
SHORT codeval, timeouts;
#define IF_NOT_IDLE_TWO_MINUTES while(timeouts < 4)
main()
{
GfxBase = OpenLibrary("graphics.library", 0);
if (GfxBase == NULL)
{
printf("Unable to open graphics library\n");
exit(1000);
}
printf("Mouseport Demo\n");
printf("\nMove Mouse from Left Port to Right Port\n");
printf("\nThen move the mouse and click its buttons");
timeouts = 0;
gamedata = &gamebuffer[0];
/* point to first location in game buffer */
game_msg_port = CreatePort(0,0);
/* provide a port for the IO response */
if(game_msg_port == 0)
{
printf("\nError While Performing CreatePort");
exit(-1);
}
game_io_msg = CreateStdIO(game_msg_port);
/* make an io request block for communicating with
the keyboard */
if(game_io_msg == 0)
{
printf("\nError While Performing CreateStdIO");
DeletePort(game_msg_port);
exit(-2);
}
error = OpenDevice("gameport.device",1,game_io_msg,0);
/* open the device for access, unit 1 is right port */
if(error != 0)
{
printf("\nError while opening the device, exiting");
DeleteStdIO(game_io_msg);
DeletePort(game_msg_port);
exit(-3);
}
game_io_msg->io_Length = sizeof(struct InputEvent);
/* read one event each time we go back to the gameport */
game_io_msg->io_Data = (APTR)gamebuffer;
/* show where to put the data when read */
game_data = (struct InputEvent *)gamebuffer;
/* test the mouse in this loop */
set_controller_type(GPCT_MOUSE);
game_io_msg->io_Command = GPD_SETTRIGGER;
/* specify the trigger conditions */
game_io_msg->io_Data = (APTR)&mousetrigger;
/* show where to find the trigger condition info */
/* this command doesn't wait... returns immediately */
SendIO(game_io_msg);
WaitPort(game_msg_port);
GetMsg(game_msg_port);
printf("\nI will report:");
printf("\n Mouse X or Y moves if either is over 10 counts");
printf("\n Button presses (along with mouse moves if any)");
printf("\n Or every 30 seconds (along with mouse moves if any)");
printf("\n if neither move or click happens\n");
printf("\nIf no activity for 2 minutes, the program exits\n");
game_io_msg->io_Command = GPD_READEVENT;
/* from now on, just read input events */
game_io_msg->io_Data = (APTR)gamebuffer;
/* into the input buffer, one at a time. */
/* read-event waits for the preset conditions */
IF_NOT_IDLE_TWO_MINUTES
{
game_io_msg->io_Length = sizeof(struct InputEvent);
/* read one event each time we go back to the gameport */
printf("\n Waiting For Mouse Report\n");
SendIO(game_io_msg);
WaitPort(game_msg_port);
/* this is NOT a busy wait... it is a task-sleep */
GetMsg(game_msg_port);
codeval = game_data->ie_Code;
switch(codeval)
{
case IECODE_LBUTTON:
printf("\nMouse Left Button Pressed");
maybe_mouse_moved();
break;
case IECODE_RBUTTON:
printf("\nMouse Right Button Pressed");
maybe_mouse_moved();
break;
case (IECODE_LBUTTON + IECODE_UP_PREFIX):
printf("\nMouse Left Button Released");
maybe_mouse_moved();
break;
case (IECODE_RBUTTON + IECODE_UP_PREFIX):
printf("\nMouse Right Button Released");
maybe_mouse_moved();
break;
case IECODE_NOBUTTON:
timeouts++; /* after 2 minutes, dump program
* if user loses interest
*/
movesize = maybe_mouse_moved();
if(movesize == 0)
printf("\n30 seconds passed, no trigger events");
else if(movesize < XMOVE && movesize < YMOVE )
{
printf("\n(Even though less than trigger count,");
printf("\n reporting mouse move at the selected");
printf("\n timing interval for user info)");
}
break;
default:
break;
}
}
set_controller_type(GPCT_NOCONTROLLER);
CloseDevice(game_io_msg);
DeleteStdIO(game_io_msg);
DeletePort(game_msg_port);
printf("\nExiting program... 2 minutes with no activity sensed\n1> ");
return(0);
}
/* if mouse didnt move far enough to trigger a report, then caller
* will also report that 30 seconds (1800 vblanks) has elapsed
*/
int maybe_mouse_moved()
{
int xmove, ymove;
xmove = game_data->ie_X;
ymove = game_data->ie_Y;
if(xmove != 0 || ymove != 0)
{
printf("\nMouse Moved by X-value %ld, Y-value %ld",
xmove, ymove);
timeouts = 0;
}
if(xmove < 0) xmove = -xmove;
if(ymove < 0) ymove = -ymove;
return(MAX(xmove,ymove));
}
int set_controller_type(type)
SHORT type;
{
game_io_msg->io_Command = GPD_SETCTYPE;
/* set type of controller to mouse */
*gamedata = type;
/* this means dont generate any more reports */
SendIO(game_io_msg);
/* set it up */
/* this command doesn't wait... returns immediately */
WaitPort(game_msg_port);
GetMsg(game_msg_port);
return(0);
}