/// <summary> /// Retrieves the latest device information from connected touch devices. /// </summary> internal void Refresh() { try { // Keep track of old tablets so that we can properly log connects/disconnects Dictionary <IntPtr, PointerTabletDevice> oldTablets = _tabletDeviceMap; _tabletDeviceMap = new Dictionary <IntPtr, PointerTabletDevice>(); TabletDevices.Clear(); uint deviceCount = 0; // Pattern is to first get the count, then declare an array of that size // which is then marshaled via the second call with the proper data. IsValid = UnsafeNativeMethods.GetPointerDevices(ref deviceCount, null); if (IsValid) { UnsafeNativeMethods.POINTER_DEVICE_INFO[] deviceInfos = new UnsafeNativeMethods.POINTER_DEVICE_INFO[deviceCount]; IsValid = UnsafeNativeMethods.GetPointerDevices(ref deviceCount, deviceInfos); if (IsValid) { foreach (var deviceInfo in deviceInfos) { // Old PenIMC code gets this id via a straight cast from COM pointer address // into an int32. This does a very similar thing semantically using the pointer // to the tablet from the WM_POINTER stack. While it may have similar issues // (chopping the upper bits, duplicate ids) we don't use this id internally // and have never received complaints about this in the WISP stack. int id = MS.Win32.NativeMethods.IntPtrToInt32(deviceInfo.device); PointerTabletDeviceInfo ptdi = new PointerTabletDeviceInfo(id, deviceInfo); // Don't add a device that fails initialization. This means we will try a refresh // next time around if we receive stylus input and the device is not available. // <see cref="HwndPointerInputProvider.UpdateCurrentTabletAndStylus"> if (ptdi.TryInitialize()) { PointerTabletDevice tablet = new PointerTabletDevice(ptdi); if (!oldTablets.Remove(tablet.Device)) { // We only create a TabletDevice when one is connected (physically or virtually). // As such we have to log when there is no corresponding old tablet being refreshed. StylusTraceLogger.LogDeviceConnect( new StylusTraceLogger.StylusDeviceInfo( tablet.Id, tablet.Name, tablet.ProductId, tablet.TabletHardwareCapabilities, tablet.TabletSize, tablet.ScreenSize, tablet.Type, tablet.StylusDevices.Count)); } _tabletDeviceMap[tablet.Device] = tablet; TabletDevices.Add(tablet.TabletDevice); } } } // Any tablet leftover here was not refreshed from the previous set of tablets // and should be logged as disconnected. foreach (var oldTablet in oldTablets.Values) { StylusTraceLogger.LogDeviceDisconnect(oldTablet.Id); } } } catch (Win32Exception) { IsValid = false; } }
/// <summary> /// DDVSO:197685 /// This is a vestigial function that isn't used anymore but needs to remain in /// order to implement ICollection. Before this class implemented the CopyTo /// specific to an array of TabletDevice and used this solely, but the switch to /// List<TabletDevice> for the underlying collection no longer needs this. The /// public interface must remain unchanged so we keep this anyway. /// </summary> /// <param name="array"></param> /// <param name="index"></param> void ICollection.CopyTo(Array array, int index) { Array.Copy(TabletDevices.ToArray(), 0, array, index, Count); }
/// <summary> /// Copy the TabletDevice objects in the collection to another array /// of TabletDevices. /// </summary> /// <param name="array">destination array</param> /// <param name="index">position in destination array to begin copying</param> public void CopyTo(TabletDevice[] array, int index) { TabletDevices.CopyTo(array, index); }
/// <summary> /// Standard implementation of IEnumerable which enables callers to use the /// foreach construct to enumerate through each TabletDevice in the collection. /// </summary> IEnumerator IEnumerable.GetEnumerator() { return(TabletDevices.GetEnumerator()); }