Microsoft DirectX 8.1 (C++) |
Polling and Event Notification
There are two ways to find out whether input data is available: polling and event notification.
Polling a device means regularly getting the current state of the device objects or checking the contents of the event buffer. Polling is typically used by real-time games that are never idle, but instead are constantly updating and rendering the game world.
Polling
In a C++ application, polling would typically be done within the message loop.
Some joysticks and other game devices, or particular objects on them, do not generate hardware interrupts and do not return any data or signal any events until you call the IDirectInputDevice8::Poll method. (This behind-the-scenes polling is not to be confused with the kind of application polling just discussed. Poll does not retrieve any data, but merely makes data available.)
To find out whether it is necessary to call Poll each time you want to retrieve data, first set the data format for the device, then call the IDirectInputDevice8::GetCapabilities method and check for the DIDC_POLLEDDATAFORMAT flag in the DIDEVCAPS structure.
Do not confuse the DIDC_POLLEDDATAFORMAT flag with the DIDC_POLLEDDEVICE flag. The latter is set if any object on the device requires polling, regardless of data format. You can then find out whether this is the case for a particular object by calling the IDirectInputDevice8::GetObjectInfo method and checking for the DIDOI_POLLED flag in the DIDEVICEOBJECTINSTANCE structure.
The DIDC_POLLEDDEVICE flag describes the worst case for the device, not the actual situation. For example, a Human Interface Device (HID) mouse with software-controllable resolution might be marked as DIDC_POLLEDDEVICE because reading the resolution information requires polling. If you want to retrieve only the standard button and axis data, polling the device under these conditions is not necessary. However, there is no harm in calling the IDirectInputDevice8::Poll method for any input device. If the call is unnecessary, it has no effect and is very fast.
Event Notification
Event notification is suitable for applications such as the Scrawl Sample that wait for input before doing anything.
To use event notification, set up a thread-synchronization object with the Microsoft® Win32® CreateEvent function, and then associate this event with the device by passing its handle to the IDirectInputDevice8::SetEventNotification method. The event is then signaled by Microsoft DirectInput® whenever the state of the device changes. Your application can receive notification of the event with a Win32 function such as WaitForSingleObject, and then respond by checking the input buffer to find out what the event was. For code examples, see the Scrawl sample and IDirectInputDevice8::SetEventNotification.