Tricks of the Windows Game Programming Gurus:Uplinking with DirectInput and Force Feedback

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

Enumerating for Joysticks

I always hate explaining callback functions and enumeration functions because they seem so complex. However, by the time you get your hands on this book, you will probably be familiar with these types of functions because DOS programming has been falling by the wayside for quite some time. If you are just learning Windows programming, this will seem like overkill, but once you get over it, you won’t have to worry about it anymore.

Basically, a callback function is something similar to the `WinProc()` in your Windows programs. It’s a function Windows calls that you supply to do something. This is fairly straightforward and understandable. Figure 9.10 shows a standard callback like that of the Windows `WinProc()`.

Figure 9.10 A callback function.

However, Win32/DirectX also uses callback functions for enumeration. Enumeration means that Windows (or DirectInput in this case) needs to have the capability to scan the system registry, or whichever database, for something that you’re looking for, such as what kind of joysticks are plugged in and available.

There are two ways to do this:

  • You could call a DirectInput function that builds a list for you and stores it in a data structure, and you later parse and extrapolate out the important information.
  • You could supply DirectInput with a callback/enumeration function that it will call for each new device that it finds. You can be the one who builds up the device list by adding each new entry every time your callback function is called.

The second method is how DirecInput works, so you just have to deal with it. Now, you might wonder why you need to do an enumeration at all. Well, you have no idea what types of joystick devices are plugged in, and even if you did, you need the exact `GUID` of one or more of them. So you need to scan for them no matter what because you need that `GUID` for the call to `CreateDevice()`.

The function that does the enumeration is called `IDIRECTINPUT::EnumDevices()` and is called directly from the main DirectInput COM object. Here’s its prototype:

    HRESULT EnumDevices(

     DWORD dwDevType, // type of device to scan for

     LPDIENUMCALLBACK lpCallback, // ptr to callback func

     LPVOID pvRef,    // 32 bit value passed back to you

     DWORD dwFlags);  // type of search to do

Let’s take a look at the parameters. First, `dwDevType` indicates what kind of devices you want to scan for; the possibilities are shown in Table 9.6.

Table 9.6 The Basic Device Types for DirectInput

Value | Description
--- | ---

`DIDEVTYPE_MOUSE` | A mouse or mouse-like device (such as a trackball).
`DIDEVTYPE_KEYBOARD` | A keyboard or keyboard-like device.
`DIDEVTYPE_JOYSTICK` | A joystick or similar device, such as a steering wheel.
`DIDEVTYPE_DEVICE` | A device that doesn’t fall into one of the previous categories.

If you want `EnumDevices()` to be more specific, you can also give it a subtype that you logically `OR` with the main type. Table 9.7 contains a partial list of subtypes for mouse and joystick device enumeration.

Table 9.7 DirectInput Subtypes (partial)

Value | Description
--- | ---

`DIDEVTYPEMOUSE_TOUCHPAD` | Standard touchpad.
`DIDEVTYPEMOUSE_TRACKBALL` | Standard trackball.
`DIDEVTYPEJOYSTICK_FLIGHTSTICK` | General flightstick.
`DIDEVTYPEJOYSTICK_GAMEPAD` | Nintendo-like gamepad.
`DIDEVTYPEJOYSTICK_RUDDER` | Simple rudder control.
`DIDEVTYPEJOYSTICK_WHEEL` | Steering wheel.
`DIDEVTYPEJOYSTICK_HEADTRACKER` | VR head tracker.

> Note: There are few dozen other subtypes that I haven’t listed. The point is, DirectInput can be as general or as specific in the search as you want it to be. However, you’re just going to use `DIDEVTYPE_JOYSTICK` as the value for `dwDevType` because you just want to find the basic, run-of-the-mill joystick(s).

The next parameter in `EnumDevices()` is a pointer to the callback function that DirectInput is going to call for each device it finds. I will show you the form of this function in a moment. The next parameter, `pvRef`, is a 32-bit pointer that points to a value that will be passed to the callback. Thus, you can modify the value in the callback if you want, or use it to pass data back instead of globally.

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.