//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- // UpdateDeviceStates() //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void PlugIn::UpdateDeviceStates() { // Get the current state of all the devices plugged in from the SampleAssistant DPA::Sample::AutoFreeUnboundedArray<DPA::Sample::DeviceState> deviceStates; DPA::Sample::GetDeviceStates(mAssistantPort, mDeviceEventPort->GetMachPort(), deviceStates); // Determine how many devices have been removed (if any). // This will be done by iterating over all the devices the PlugIn knows about and making sure they are present in the deviceStates array { std::vector<Device*> removedDevices; for (UInt32 i = 0 ; i < GetNumberDevices() ; ++i) { Device* device = static_cast<Device*>(GetDeviceByIndex(i)); bool found = false; // See if it can be located in the deviceStates array for (UInt32 ii = 0; ii < deviceStates.GetLength() ; ++ii) { if (deviceStates[ii].mGUID == device->GetDeviceGUID()) { found = true; break; } } // If the device was not found, stick into the vector of unplugged devices if (not found) removedDevices.push_back(device); } // Remove all the unplugged devices for (std::vector<Device*>::iterator i = removedDevices.begin() ; i != removedDevices.end() ; ++i) DeviceRemoved(**i); } // Determine how many new devices are present { for (UInt32 i = 0; i < deviceStates.GetLength() ; ++i) { try { // This will throw an exception if the device is not found (void) GetDeviceByGUID(deviceStates[i].mGUID); } catch (...) { // No device was found with the indicated GUID, so it is a new device DeviceArrived(deviceStates[i].mGUID, deviceStates[i].mRegistryPath); } } } }
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- // GetDeviceByGUID() //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Device& PlugIn::GetDeviceByGUID(UInt64 guid) const { Device* device = 0; bool found = false; // Iterate over the plugIn's devices and find the one whose guid matches for (UInt32 i = 0 ; i < GetNumberDevices() ; ++i) { device = static_cast<Device*>(GetDeviceByIndex(i)); ThrowIfNULL(device, CAException(kCMIOHardwareBadDeviceError), "CMIO::DP::Sample::PlugIn::GetDeviceByGUID: no device for index"); if (guid == device->GetDeviceGUID()) { found = true; break; } } // Make sure that a device was actually found if (not found) throw CAException(kCMIOHardwareBadDeviceError); return *device; }