public static extern ReturnCode DsmWin32( [In, Out] TW_IDENTITY origin, [In, Out] TW_IDENTITY destination, DataGroups dg, DataArgumentType dat, Message msg, ref TW_DEVICEEVENT data);
private void HandleSourceMsg(Message msg) { Debug.WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId}: {nameof(HandleSourceMsg)}({msg})"); switch (msg) { case Message.DeviceEvent: TW_DEVICEEVENT de = default; var rc = DGControl.DeviceEvent.Get(ref de); if (rc == ReturnCode.Success) { OnDeviceEventReceived(new DeviceEventArgs { Data = de }); } break; case Message.XferReady: if (State < TwainState.S6) { State = TwainState.S6; } DoTransferRoutine(); break; case Message.CloseDSReq: if (_state > TwainState.S5) { // do it after end of current xfer routine. _disableDSNow = true; } else { DGControl.UserInterface.DisableDS(ref _lastEnableUI, false); } break; case Message.CloseDSOK: DGControl.UserInterface.DisableDS(ref _lastEnableUI, true); break; } }
public ReturnCode Get(ref TW_DEVICEEVENT evt) { if (Is32Bit) { if (IsWin) { return(NativeMethods.DsmWin32(Session.Config.App32, Session.CurrentSource.Identity32, DataGroups.Control, DataArgumentType.DeviceEvent, Message.Get, ref evt)); } if (IsLinux) { return(NativeMethods.DsmLinux32(Session.Config.App32, Session.CurrentSource.Identity32, DataGroups.Control, DataArgumentType.DeviceEvent, Message.Get, ref evt)); } if (IsMac) { return(NativeMethods.DsmMac32(Session.Config.App32, Session.CurrentSource.Identity32, DataGroups.Control, DataArgumentType.DeviceEvent, Message.Get, ref evt)); } } if (IsWin) { return(NativeMethods.DsmWin64(Session.Config.App32, Session.CurrentSource.Identity32, DataGroups.Control, DataArgumentType.DeviceEvent, Message.Get, ref evt)); } if (IsLinux) { return(NativeMethods.DsmLinux64(Session.Config.App32, Session.CurrentSource.Identity32, DataGroups.Control, DataArgumentType.DeviceEvent, Message.Get, ref evt)); } if (IsMac) { return(NativeMethods.DsmMac64(Session.Config.App32, Session.CurrentSource.Identity32, DataGroups.Control, DataArgumentType.DeviceEvent, Message.Get, ref evt)); } return(ReturnCode.Failure); }
/// <summary> /// Get device events... /// </summary> /// <param name="a_dg">Data group</param> /// <param name="a_msg">Operation</param> /// <param name="a_twdeviceevent">DEVICEEVENT structure</param> /// <returns>TWAIN status</returns> public STS DatDeviceevent(DG a_dg, MSG a_msg, ref TW_DEVICEEVENT a_twdeviceevent) { STS sts; // Submit the work to the TWAIN thread... if ((m_threadTwain != null) && (m_threadTwain.ManagedThreadId != Thread.CurrentThread.ManagedThreadId)) { lock (m_lockTwain) { // Set our command variables... ThreadData threaddata = default(ThreadData); threaddata.twdeviceevent = a_twdeviceevent; threaddata.dg = a_dg; threaddata.msg = a_msg; threaddata.dat = DAT.DEVICEEVENT; long lIndex = m_twaincommand.Submit(threaddata); // Submit the command and wait for the reply... CallerToThreadSet(); ThreadToCallerWaitOne(); // Return the result... a_twdeviceevent = m_twaincommand.Get(lIndex).twdeviceevent; sts = m_twaincommand.Get(lIndex).sts; // Clear the command variables... m_twaincommand.Delete(lIndex); } return (sts); } // Log it... if (Log.GetLevel() > 0) { Log.LogSendBefore(a_dg.ToString(), DAT.DEVICEEVENT.ToString(), a_msg.ToString(), ""); } // Windows... if (ms_platform == Platform.WINDOWS) { // Issue the command... try { if (m_blUseLegacyDSM) { sts = (STS)WindowsTwain32DsmEntryDeviceevent(ref m_twidentitylegacyApp, ref m_twidentitylegacyDs, a_dg, DAT.DEVICEEVENT, a_msg, ref a_twdeviceevent); } else { sts = (STS)WindowsTwaindsmDsmEntryDeviceevent(ref m_twidentitylegacyApp, ref m_twidentitylegacyDs, a_dg, DAT.DEVICEEVENT, a_msg, ref a_twdeviceevent); } } catch { // The driver crashed... Log.LogSendAfter(STS.BUMMER.ToString(), ""); return (STS.BUMMER); } } // Linux... else if (ms_platform == Platform.LINUX) { // Issue the command... try { if (GetMachineWordBitSize() == 32) { sts = (STS)LinuxDsmEntryDeviceevent(ref m_twidentitylegacyApp, ref m_twidentitylegacyDs, a_dg, DAT.DEVICEEVENT, a_msg, ref a_twdeviceevent); } else { sts = (STS)Linux64DsmEntryDeviceevent(ref m_twidentityApp, ref m_twidentityDs, a_dg, DAT.DEVICEEVENT, a_msg, ref a_twdeviceevent); } } catch { // The driver crashed... Log.LogSendAfter(STS.BUMMER.ToString(), ""); return (STS.BUMMER); } } // Mac OS X, which has to be different... else if (ms_platform == Platform.MACOSX) { // Issue the command... try { sts = (STS)MacosxDsmEntryDeviceevent(ref m_twidentitymacosxApp, ref m_twidentitymacosxDs, a_dg, DAT.DEVICEEVENT, a_msg, ref a_twdeviceevent); } catch { // The driver crashed... Log.LogSendAfter(STS.BUMMER.ToString(), ""); return (STS.BUMMER); } } // Uh-oh... else { Log.LogSendAfter(STS.BUMMER.ToString(), ""); return (STS.BUMMER); } // Log it... if (Log.GetLevel() > 0) { Log.LogSendAfter(sts.ToString(), DeviceeventToCsv(a_twdeviceevent)); } // All done... return (AutoDatStatus(sts)); }
/// <summary> /// Convert the contents of a device event to a string that we can show in /// our simple GUI... /// </summary> /// <param name="a_twdeviceevent">A TWAIN structure</param> /// <returns>A CSV string of the TWAIN structure</returns> public string DeviceeventToCsv(TW_DEVICEEVENT a_twdeviceevent) { try { CSV csv = new CSV(); csv.Add(((TWDE)a_twdeviceevent.Event).ToString()); csv.Add(a_twdeviceevent.DeviceName.Get()); csv.Add(a_twdeviceevent.BatteryMinutes.ToString()); csv.Add(a_twdeviceevent.BatteryPercentage.ToString()); csv.Add(a_twdeviceevent.PowerSupply.ToString()); csv.Add(((double)a_twdeviceevent.XResolution.Whole + ((double)a_twdeviceevent.XResolution.Frac / 65536.0)).ToString()); csv.Add(((double)a_twdeviceevent.YResolution.Whole + ((double)a_twdeviceevent.YResolution.Frac / 65536.0)).ToString()); csv.Add(a_twdeviceevent.FlashUsed2.ToString()); csv.Add(a_twdeviceevent.AutomaticCapture.ToString()); csv.Add(a_twdeviceevent.TimeBeforeFirstCapture.ToString()); csv.Add(a_twdeviceevent.TimeBetweenCaptures.ToString()); return (csv.Get()); } catch { return ("***error***"); } }
private static extern UInt16 MacosxDsmEntryDeviceevent ( ref TW_IDENTITY_MACOSX origin, ref TW_IDENTITY_MACOSX dest, DG dg, DAT dat, MSG msg, ref TW_DEVICEEVENT twdeviceevent );
private static extern UInt16 Linux64DsmEntryDeviceevent ( ref TW_IDENTITY origin, ref TW_IDENTITY dest, DG dg, DAT dat, MSG msg, ref TW_DEVICEEVENT twdeviceevent );
private static extern UInt16 WindowsTwaindsmDsmEntryDeviceevent ( ref TW_IDENTITY_LEGACY origin, ref TW_IDENTITY_LEGACY dest, DG dg, DAT dat, MSG msg, ref TW_DEVICEEVENT twdeviceevent );