Determining Joystick Capabilities

The various joysticks in use today can support two or three axes and a variety of button configurations. Also, joysticks support different ranges of motion and polling frequencies. Joystick drivers can support either one or two joysticks. Two functions allow you to determine the capabilities of the joystick services and joystick devices installed on a system.

Note:

The IBMJOY.DRV joystick driver supports either one 3-axis joystick or two 2-axis joysticks. The driver includes a configuration routine (accessed through the Control Panel Drivers applet) that allows a user to specify which type of device is connected.

Getting the Driver Capabilities

You can use the joyGetNumDevs function to determine the number of joystick devices supported by the joystick services. This function has the following syntax:

WORD joyGetNumDevs()

This function returns the number of supported joysticks, or zero if there is no joystick support. The value returned is not necessarily the number of joysticks attached to the system. To determine whether a joystick is attached, call the joyGetPos function for the device. The joyGetPos function, discussed in “Polling the Joystick,” later in this chapter, returns JOYERR_UNPLUGGED if the specified device is disconnected.

The following code fragment determines whether the joystick services are available and then determines if a joystick is attached to one of the ports:

JOYINFO joyinfo;
WORD wNumDevs, wDeviceID;
BOOL bDev1Attached, bDev2Attached;

    if((wNumDevs = joyGetNumDevs()) == 0)
        return ERR_NODRIVER;

    bDev1Attached = joyGetPos(JOYSTICKID1,&joyinfo) != JOYERR_UNPLUGGED;
    bDev2Attached = wNumDevs == 2 &&
                     joyGetPos(JOYSTICKID2,&joyinfo) != JOYERR_UNPLUGGED;

    if(bDev1Attached || bDev2Attached)      // Decide which joystick to use
        wDeviceID = bDev1Attached ? JOYSTICKID1 : JOYSTICKID2;
    else
        return ERR_NODEVICE;

Getting the Joystick Capabilities

You can use the joyGetDevCaps function to obtain the specific capabilities of each joystick attached to a given system. The joyGetDevCaps function has the following syntax:

WORD joyGetDevCaps(wID, lpJoyCaps, wSize)

The wID parameter identifies the joystick as either JOYSTICKID1 or JOYSTICKID2. The lpJoyCaps parameter points to a JOYCAPS structure to be filled by the function. The wSize parameter specifies the size of the JOYCAPS structure.

Summary: JOYCAPS Structure

The JOYCAPS structure specifies the range of each axis on the joystick, the number of buttons, and the maximum and minimum polling frequency. This structure has the following fields:

Field Description

wMid Manufacturer identification
wPid Product identification
szPname[MAXPNAMELEN] Product name in a null-terminated string
wXmin, wXmax Minimum and maximum x-position values
wYmin, wYmax Minimum and maximum y-position values
wZmin, wZmax Minimum and maximum z-position values
wNumButtons Number of buttons
wPeriodMin Minimum period between messages
wPeriodMax Maximum period between messages