2014-04-06 04:48:44 +08:00
using NTwain.Data ;
2014-04-21 04:57:38 +08:00
using NTwain.Internals ;
2014-04-17 19:11:39 +08:00
using NTwain.Properties ;
2014-04-06 04:48:44 +08:00
using NTwain.Triplets ;
using System ;
using System.Collections.Generic ;
using System.ComponentModel ;
2014-04-06 06:33:21 +08:00
using System.Diagnostics ;
2014-04-21 06:42:51 +08:00
using System.Globalization ;
2014-04-06 09:54:08 +08:00
using System.IO ;
2014-04-06 06:33:21 +08:00
using System.Runtime.InteropServices ;
using System.Threading ;
2014-04-06 04:48:44 +08:00
namespace NTwain
{
2014-04-06 06:33:21 +08:00
/// <summary>
2014-04-20 20:10:20 +08:00
/// Basic class for interfacing with TWAIN. You should only have one of this per application process.
2014-04-06 06:33:21 +08:00
/// </summary>
2014-04-21 06:42:51 +08:00
public class TwainSession : ITwainSessionInternal
2014-04-06 04:48:44 +08:00
{
/// <summary>
2014-04-17 19:11:39 +08:00
/// Initializes a new instance of the <see cref="TwainSession" /> class.
2014-04-06 04:48:44 +08:00
/// </summary>
/// <param name="appId">The app id.</param>
/// <exception cref="System.ArgumentNullException"></exception>
2014-04-06 09:54:08 +08:00
public TwainSession ( TWIdentity appId )
2014-04-06 04:48:44 +08:00
{
if ( appId = = null ) { throw new ArgumentNullException ( "appId" ) ; }
2014-04-21 04:57:38 +08:00
2014-04-06 06:33:21 +08:00
_appId = appId ;
2014-04-21 06:42:51 +08:00
( ( ITwainSessionInternal ) this ) . ChangeState ( 1 , false ) ;
2014-04-06 04:48:44 +08:00
EnforceState = true ;
2014-04-15 07:04:48 +08:00
2014-04-19 21:10:15 +08:00
MessageLoop . Instance . EnsureStarted ( HandleWndProcMessage ) ;
2014-04-06 04:48:44 +08:00
}
2014-04-21 06:42:51 +08:00
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
2014-04-06 06:33:21 +08:00
object _callbackObj ; // kept around so it doesn't get gc'ed
2014-04-21 06:42:51 +08:00
TWIdentity _appId ;
2014-04-06 06:33:21 +08:00
TWUserInterface _twui ;
2014-04-06 04:48:44 +08:00
2014-04-17 08:39:30 +08:00
/// <summary>
2014-04-21 06:42:51 +08:00
/// Gets or sets the optional synchronization context.
2014-04-17 08:39:30 +08:00
/// This allows events to be raised on the thread
/// associated with the context.
/// </summary>
/// <value>
/// The synchronization context.
/// </value>
public SynchronizationContext SynchronizationContext { get ; set ; }
2014-04-06 08:14:19 +08:00
2014-04-21 06:42:51 +08:00
#region ITwainSessionInternal Members
2014-04-06 04:48:44 +08:00
2014-04-06 06:33:21 +08:00
/// <summary>
/// Gets the app id used for the session.
/// </summary>
/// <value>The app id.</value>
2014-04-21 06:42:51 +08:00
TWIdentity ITwainSessionInternal . AppId { get { return _appId ; } }
2014-04-06 06:33:21 +08:00
2014-04-06 04:48:44 +08:00
/// <summary>
/// Gets or sets a value indicating whether calls to triplets will verify the current twain session state.
/// </summary>
/// <value>
/// <c>true</c> if state value is enforced; otherwise, <c>false</c>.
/// </value>
public bool EnforceState { get ; set ; }
2014-04-21 06:42:51 +08:00
void ITwainSessionInternal . ChangeState ( int newState , bool notifyChange )
2014-04-06 04:48:44 +08:00
{
2014-04-06 08:14:19 +08:00
_state = newState ;
2014-04-06 04:48:44 +08:00
if ( notifyChange )
{
2014-04-21 08:45:08 +08:00
OnPropertyChanged ( "State" ) ;
2014-04-17 08:39:30 +08:00
SafeAsyncSyncableRaiseOnEvent ( OnStateChanged , StateChanged ) ;
2014-04-06 04:48:44 +08:00
}
}
2014-04-21 06:42:51 +08:00
ICommittable ITwainSessionInternal . GetPendingStateChanger ( int newState )
2014-04-06 04:48:44 +08:00
{
return new TentativeStateCommitable ( this , newState ) ;
}
2014-04-21 06:42:51 +08:00
void ITwainSessionInternal . ChangeSourceId ( TWIdentity sourceId )
2014-04-06 04:48:44 +08:00
{
SourceId = sourceId ;
2014-04-21 08:45:08 +08:00
OnPropertyChanged ( "SourceId" ) ;
2014-04-17 08:39:30 +08:00
SafeAsyncSyncableRaiseOnEvent ( OnSourceChanged , SourceChanged ) ;
2014-04-06 04:48:44 +08:00
}
2014-04-21 06:42:51 +08:00
void ITwainSessionInternal . SafeSyncableRaiseEvent ( DataTransferredEventArgs e )
{
SafeSyncableRaiseOnEvent ( OnDataTransferred , DataTransferred , e ) ;
}
void ITwainSessionInternal . SafeSyncableRaiseEvent ( TransferErrorEventArgs e )
{
SafeSyncableRaiseOnEvent ( OnTransferError , TransferError , e ) ;
}
void ITwainSessionInternal . SafeSyncableRaiseEvent ( TransferReadyEventArgs e )
{
SafeSyncableRaiseOnEvent ( OnTransferReady , TransferReady , e ) ;
}
2014-04-06 04:48:44 +08:00
#endregion
2014-04-22 18:50:58 +08:00
#region ITwainSession Members
2014-04-06 04:48:44 +08:00
/// <summary>
/// Gets the source id used for the session.
/// </summary>
/// <value>
/// The source id.
/// </value>
public TWIdentity SourceId { get ; private set ; }
2014-04-06 08:14:19 +08:00
int _state ;
2014-04-06 04:48:44 +08:00
/// <summary>
/// Gets the current state number as defined by the TWAIN spec.
/// </summary>
/// <value>
/// The state.
/// </value>
2014-04-06 08:14:19 +08:00
public int State
{
get { return _state ; }
2014-04-22 18:50:58 +08:00
private set
2014-04-06 08:14:19 +08:00
{
if ( value > 0 & & value < 8 )
{
_state = value ;
2014-04-21 08:45:08 +08:00
OnPropertyChanged ( "State" ) ;
2014-04-17 08:39:30 +08:00
SafeAsyncSyncableRaiseOnEvent ( OnStateChanged , StateChanged ) ;
2014-04-06 08:14:19 +08:00
}
}
}
2014-04-06 04:48:44 +08:00
2014-04-22 18:50:58 +08:00
static readonly CapabilityId [ ] _emptyCapList = new CapabilityId [ 0 ] ;
private IList < CapabilityId > _supportedCaps ;
/// <summary>
/// Gets the supported caps for the currently open source.
/// </summary>
/// <value>
/// The supported caps.
/// </value>
public IList < CapabilityId > SupportedCaps
{
get
{
if ( _supportedCaps = = null & & State > 3 )
{
_supportedCaps = this . GetCapabilities ( ) ;
}
return _supportedCaps ? ? _emptyCapList ;
}
private set
{
_supportedCaps = value ;
OnPropertyChanged ( "SupportedCaps" ) ;
}
}
2014-04-06 04:48:44 +08:00
#endregion
#region ITwainOperation Members
DGAudio _dgAudio ;
/// <summary>
/// Gets the triplet operations defined for audio data group.
/// </summary>
public DGAudio DGAudio
{
get
{
if ( _dgAudio = = null ) { _dgAudio = new DGAudio ( this ) ; }
return _dgAudio ;
}
}
DGControl _dgControl ;
/// <summary>
/// Gets the triplet operations defined for control data group.
/// </summary>
public DGControl DGControl
{
get
{
if ( _dgControl = = null ) { _dgControl = new DGControl ( this ) ; }
return _dgControl ;
}
}
DGImage _dgImage ;
/// <summary>
/// Gets the triplet operations defined for image data group.
/// </summary>
public DGImage DGImage
{
get
{
if ( _dgImage = = null ) { _dgImage = new DGImage ( this ) ; }
return _dgImage ;
}
}
#endregion
#region INotifyPropertyChanged Members
/// <summary>
/// Occurs when a property value changes.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged ;
/// <summary>
/// Raises the <see cref="PropertyChanged"/> event.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
2014-04-21 08:45:08 +08:00
protected void OnPropertyChanged ( string propertyName )
2014-04-06 04:48:44 +08:00
{
2014-04-22 18:50:58 +08:00
var syncer = SynchronizationContext ;
if ( syncer = = null )
2014-04-17 08:39:30 +08:00
{
try
{
var hand = PropertyChanged ;
if ( hand ! = null ) { hand ( this , new PropertyChangedEventArgs ( propertyName ) ) ; }
}
catch { }
}
else
{
2014-04-22 18:50:58 +08:00
syncer . Post ( o = >
2014-04-17 08:39:30 +08:00
{
try
{
var hand = PropertyChanged ;
if ( hand ! = null ) { hand ( this , new PropertyChangedEventArgs ( propertyName ) ) ; }
}
catch { }
} , null ) ;
}
2014-04-06 04:48:44 +08:00
}
#endregion
2014-04-06 06:33:21 +08:00
#region privileged calls that causes state change in TWAIN
/// <summary>
/// Opens the data source manager. This must be the first method used
2014-04-13 21:30:39 +08:00
/// before using other TWAIN functions. Calls to this must be followed by <see cref="CloseManager"/> when done with a TWAIN session.
2014-04-06 06:33:21 +08:00
/// </summary>
/// <returns></returns>
2014-04-15 07:04:48 +08:00
public ReturnCode OpenManager ( )
2014-04-06 06:33:21 +08:00
{
2014-04-20 09:02:44 +08:00
var rc = ReturnCode . Failure ;
2014-04-15 07:30:25 +08:00
MessageLoop . Instance . Invoke ( ( ) = >
2014-04-06 06:33:21 +08:00
{
2014-04-21 06:42:51 +08:00
Debug . WriteLine ( string . Format ( CultureInfo . InvariantCulture , "Thread {0}: OpenManager." , Thread . CurrentThread . ManagedThreadId ) ) ;
2014-04-15 07:30:25 +08:00
rc = DGControl . Parent . OpenDsm ( MessageLoop . Instance . LoopHandle ) ;
if ( rc = = ReturnCode . Success )
2014-04-06 06:33:21 +08:00
{
2014-04-15 07:30:25 +08:00
// if twain2 then get memory management functions
if ( ( _appId . DataFunctionalities & DataFunctionalities . Dsm2 ) = = DataFunctionalities . Dsm2 )
2014-04-06 06:33:21 +08:00
{
2014-04-15 07:30:25 +08:00
TWEntryPoint entry ;
rc = DGControl . EntryPoint . Get ( out entry ) ;
if ( rc = = ReturnCode . Success )
{
2014-04-21 04:57:38 +08:00
Platform . MemoryManager = entry ;
2014-04-15 07:30:25 +08:00
Debug . WriteLine ( "Using TWAIN2 memory functions." ) ;
}
else
{
CloseManager ( ) ;
}
2014-04-06 06:33:21 +08:00
}
}
2014-04-15 07:30:25 +08:00
} ) ;
2014-04-06 06:33:21 +08:00
return rc ;
}
/// <summary>
/// Closes the data source manager.
/// </summary>
/// <returns></returns>
public ReturnCode CloseManager ( )
{
2014-04-20 20:10:20 +08:00
var rc = ReturnCode . Failure ;
2014-04-15 07:30:25 +08:00
MessageLoop . Instance . Invoke ( ( ) = >
{
2014-04-21 06:42:51 +08:00
Debug . WriteLine ( string . Format ( CultureInfo . InvariantCulture , "Thread {0}: CloseManager." , Thread . CurrentThread . ManagedThreadId ) ) ;
2014-04-06 06:33:21 +08:00
2014-04-15 07:30:25 +08:00
rc = DGControl . Parent . CloseDsm ( MessageLoop . Instance . LoopHandle ) ;
2014-04-22 18:50:58 +08:00
if ( rc = = ReturnCode . Success )
{
Platform . MemoryManager = null ;
}
2014-04-15 07:30:25 +08:00
} ) ;
2014-04-06 06:33:21 +08:00
return rc ;
}
/// <summary>
/// Loads the specified source into main memory and causes its initialization.
2014-04-20 20:10:20 +08:00
/// Calls to this must be followed by
2014-04-13 21:30:39 +08:00
/// <see cref="CloseSource" /> when not using it anymore.
2014-04-06 06:33:21 +08:00
/// </summary>
/// <param name="sourceProductName">Name of the source.</param>
/// <returns></returns>
2014-04-20 20:10:20 +08:00
/// <exception cref="System.ArgumentException">sourceProductName</exception>
2014-04-06 06:33:21 +08:00
public ReturnCode OpenSource ( string sourceProductName )
{
2014-04-17 19:11:39 +08:00
if ( string . IsNullOrEmpty ( sourceProductName ) ) { throw new ArgumentException ( Resources . SourceRequired , "sourceProductName" ) ; }
2014-04-06 06:33:21 +08:00
2014-04-20 20:10:20 +08:00
var rc = ReturnCode . Failure ;
2014-04-15 07:30:25 +08:00
MessageLoop . Instance . Invoke ( ( ) = >
{
2014-04-21 06:42:51 +08:00
Debug . WriteLine ( string . Format ( CultureInfo . InvariantCulture , "Thread {0}: OpenSource." , Thread . CurrentThread . ManagedThreadId ) ) ;
2014-04-06 06:33:21 +08:00
2014-04-22 18:50:58 +08:00
var source = new TWIdentity
{
ProductName = sourceProductName
} ;
2014-04-06 06:33:21 +08:00
2014-04-15 07:30:25 +08:00
rc = DGControl . Identity . OpenDS ( source ) ;
} ) ;
2014-04-06 06:33:21 +08:00
return rc ;
}
/// <summary>
/// When an application is finished with a Source, it must formally close the session between them
/// using this operation. This is necessary in case the Source only supports connection with a single
/// application (many desktop scanners will behave this way). A Source such as this cannot be
/// accessed by other applications until its current session is terminated
/// </summary>
/// <returns></returns>
public ReturnCode CloseSource ( )
{
2014-04-20 09:02:44 +08:00
var rc = ReturnCode . Failure ;
2014-04-15 07:30:25 +08:00
MessageLoop . Instance . Invoke ( ( ) = >
2014-04-06 06:33:21 +08:00
{
2014-04-21 06:42:51 +08:00
Debug . WriteLine ( string . Format ( CultureInfo . InvariantCulture , "Thread {0}: CloseSource." , Thread . CurrentThread . ManagedThreadId ) ) ;
2014-04-15 07:30:25 +08:00
rc = DGControl . Identity . CloseDS ( ) ;
if ( rc = = ReturnCode . Success )
{
SupportedCaps = null ;
}
} ) ;
2014-04-06 06:33:21 +08:00
return rc ;
}
/// <summary>
2014-04-13 21:30:39 +08:00
/// Enables the source to start transferring.
2014-04-06 06:33:21 +08:00
/// </summary>
/// <param name="mode">The mode.</param>
/// <param name="modal">if set to <c>true</c> any driver UI will display as modal.</param>
/// <param name="windowHandle">The window handle if modal.</param>
/// <returns></returns>
2014-04-15 07:04:48 +08:00
public ReturnCode EnableSource ( SourceEnableMode mode , bool modal , IntPtr windowHandle )
2014-04-06 06:33:21 +08:00
{
2014-04-20 09:02:44 +08:00
var rc = ReturnCode . Failure ;
2014-04-06 06:33:21 +08:00
2014-04-15 07:04:48 +08:00
MessageLoop . Instance . Invoke ( ( ) = >
2014-04-06 06:33:21 +08:00
{
2014-04-21 06:42:51 +08:00
Debug . WriteLine ( string . Format ( CultureInfo . InvariantCulture , "Thread {0}: EnableSource." , Thread . CurrentThread . ManagedThreadId ) ) ;
2014-04-15 07:30:25 +08:00
2014-04-15 07:04:48 +08:00
// app v2.2 or higher uses callback2
if ( _appId . ProtocolMajor > = 2 & & _appId . ProtocolMinor > = 2 )
2014-04-06 06:33:21 +08:00
{
2014-04-15 07:04:48 +08:00
var cb = new TWCallback2 ( HandleCallback ) ;
var rc2 = DGControl . Callback2 . RegisterCallback ( cb ) ;
if ( rc2 = = ReturnCode . Success )
{
Debug . WriteLine ( "Registered callback2 OK." ) ;
_callbackObj = cb ;
}
2014-04-06 06:33:21 +08:00
}
2014-04-15 07:04:48 +08:00
else
{
var cb = new TWCallback ( HandleCallback ) ;
2014-04-06 06:33:21 +08:00
2014-04-15 07:04:48 +08:00
var rc2 = DGControl . Callback . RegisterCallback ( cb ) ;
2014-04-06 06:33:21 +08:00
2014-04-15 07:04:48 +08:00
if ( rc2 = = ReturnCode . Success )
{
Debug . WriteLine ( "Registered callback OK." ) ;
_callbackObj = cb ;
}
2014-04-06 06:33:21 +08:00
}
2014-04-15 07:04:48 +08:00
_twui = new TWUserInterface ( ) ;
_twui . ShowUI = mode = = SourceEnableMode . ShowUI ;
_twui . ModalUI = modal ;
_twui . hParent = windowHandle ;
2014-04-06 06:33:21 +08:00
2014-04-15 07:04:48 +08:00
if ( mode = = SourceEnableMode . ShowUIOnly )
{
rc = DGControl . UserInterface . EnableDSUIOnly ( _twui ) ;
}
else
{
rc = DGControl . UserInterface . EnableDS ( _twui ) ;
}
2014-04-22 18:50:58 +08:00
if ( rc ! = ReturnCode . Success )
{
_callbackObj = null ;
}
2014-04-15 07:04:48 +08:00
} ) ;
return rc ;
2014-04-06 06:33:21 +08:00
}
2014-04-21 06:42:51 +08:00
ReturnCode ITwainSessionInternal . DisableSource ( )
2014-04-06 06:33:21 +08:00
{
2014-04-20 09:02:44 +08:00
var rc = ReturnCode . Failure ;
2014-04-15 07:04:48 +08:00
MessageLoop . Instance . Invoke ( ( ) = >
2014-04-06 06:33:21 +08:00
{
2014-04-21 06:42:51 +08:00
Debug . WriteLine ( string . Format ( CultureInfo . InvariantCulture , "Thread {0}: DisableSource." , Thread . CurrentThread . ManagedThreadId ) ) ;
2014-04-15 07:30:25 +08:00
2014-04-15 07:04:48 +08:00
rc = DGControl . UserInterface . DisableDS ( _twui ) ;
if ( rc = = ReturnCode . Success )
{
2014-04-19 21:10:15 +08:00
_callbackObj = null ;
2014-04-17 08:39:30 +08:00
SafeAsyncSyncableRaiseOnEvent ( OnSourceDisabled , SourceDisabled ) ;
2014-04-15 07:04:48 +08:00
}
} ) ;
2014-04-06 06:33:21 +08:00
return rc ;
}
/// <summary>
/// Forces the stepping down of an opened source when things gets out of control.
/// Used when session state and source state become out of sync.
/// </summary>
/// <param name="targetState">State of the target.</param>
public void ForceStepDown ( int targetState )
{
2014-04-21 06:42:51 +08:00
Debug . WriteLine ( string . Format ( CultureInfo . InvariantCulture , "Thread {0}: ForceStepDown." , Thread . CurrentThread . ManagedThreadId ) ) ;
2014-04-15 07:30:25 +08:00
bool origFlag = EnforceState ;
EnforceState = false ;
// From the twain spec
// Stepping Back Down the States
// DG_CONTROL / DAT_PENDINGXFERS / MSG_ENDXFER → state 7 to 6
// DG_CONTROL / DAT_PENDINGXFERS / MSG_RESET → state 6 to 5
// DG_CONTROL / DAT_USERINTERFACE / MSG_DISABLEDS → state 5 to 4
// DG_CONTROL / DAT_IDENTITY / MSG_CLOSEDS → state 4 to 3
// Ignore the status returns from the calls prior to the one yielding the desired state. For instance, if a
// call during scanning returns TWCC_SEQERROR and the desire is to return to state 5, then use the
// following commands.
// DG_CONTROL / DAT_PENDINGXFERS / MSG_ENDXFER → state 7 to 6
// DG_CONTROL / DAT_PENDINGXFERS / MSG_RESET → state 6 to 5
// Being sure to confirm that DG_CONTROL / DAT_PENDINGXFERS / MSG_RESET returned
// success, the return status from DG_CONTROL / DAT_PENDINGXFERS / MSG_ENDXFER may
// be ignored.
2014-04-15 07:51:36 +08:00
MessageLoop . Instance . Invoke ( ( ) = >
2014-04-15 07:30:25 +08:00
{
2014-04-15 07:51:36 +08:00
if ( targetState < 7 )
{
DGControl . PendingXfers . EndXfer ( new TWPendingXfers ( ) ) ;
}
if ( targetState < 6 )
{
DGControl . PendingXfers . Reset ( new TWPendingXfers ( ) ) ;
}
if ( targetState < 5 )
{
2014-04-21 06:42:51 +08:00
( ( ITwainSessionInternal ) this ) . DisableSource ( ) ;
2014-04-15 07:51:36 +08:00
}
if ( targetState < 4 )
{
CloseSource ( ) ;
}
if ( targetState < 3 )
{
CloseManager ( ) ;
}
} ) ;
2014-04-15 07:30:25 +08:00
EnforceState = origFlag ;
2014-04-06 06:33:21 +08:00
}
#endregion
2014-04-06 04:48:44 +08:00
#region custom events and overridables
2014-04-06 07:39:41 +08:00
/// <summary>
/// Occurs when <see cref="State"/> has changed.
/// </summary>
public event EventHandler StateChanged ;
/// <summary>
/// Occurs when <see cref="SourceId"/> has changed.
/// </summary>
public event EventHandler SourceChanged ;
/// <summary>
/// Occurs when source has been disabled (back to state 4).
/// </summary>
public event EventHandler SourceDisabled ;
/// <summary>
/// Occurs when the source has generated an event.
/// </summary>
public event EventHandler < DeviceEventArgs > DeviceEvent ;
/// <summary>
/// Occurs when a data transfer is ready.
/// </summary>
public event EventHandler < TransferReadyEventArgs > TransferReady ;
/// <summary>
/// Occurs when data has been transferred.
/// </summary>
public event EventHandler < DataTransferredEventArgs > DataTransferred ;
2014-04-12 23:07:13 +08:00
/// <summary>
/// Occurs when an error has been encountered during transfer.
/// </summary>
public event EventHandler < TransferErrorEventArgs > TransferError ;
2014-04-21 06:42:51 +08:00
2014-04-06 04:48:44 +08:00
/// <summary>
2014-04-22 18:50:58 +08:00
/// Raises event and if applicable marshal it asynchronously to the <see cref="SynchronizationContext"/> thread
/// without exceptions.
2014-04-06 04:48:44 +08:00
/// </summary>
2014-04-17 08:39:30 +08:00
/// <param name="onEventFunc">The on event function.</param>
/// <param name="handler">The handler.</param>
2014-04-21 06:42:51 +08:00
void SafeAsyncSyncableRaiseOnEvent ( Action onEventFunc , EventHandler handler )
2014-04-06 07:39:41 +08:00
{
2014-04-17 08:39:30 +08:00
var syncer = SynchronizationContext ;
if ( syncer = = null )
2014-04-06 07:39:41 +08:00
{
2014-04-06 08:50:13 +08:00
try
{
2014-04-21 06:42:51 +08:00
onEventFunc ( ) ;
if ( handler ! = null ) { handler ( this , EventArgs . Empty ) ; }
2014-04-06 08:50:13 +08:00
}
catch { }
2014-04-06 07:39:41 +08:00
}
2014-04-17 08:39:30 +08:00
else
2014-04-06 07:39:41 +08:00
{
2014-04-21 06:42:51 +08:00
syncer . Post ( o = >
2014-04-06 08:50:13 +08:00
{
2014-04-17 08:39:30 +08:00
try
{
2014-04-21 06:42:51 +08:00
onEventFunc ( ) ;
if ( handler ! = null ) { handler ( this , EventArgs . Empty ) ; }
2014-04-17 08:39:30 +08:00
}
catch { }
} , null ) ;
2014-04-06 07:39:41 +08:00
}
}
2014-04-06 04:48:44 +08:00
2014-04-06 06:33:21 +08:00
/// <summary>
2014-04-22 18:50:58 +08:00
/// Raises event and if applicable marshal it synchronously to the <see cref="SynchronizationContext" /> thread
/// without exceptions.
2014-04-06 06:33:21 +08:00
/// </summary>
2014-04-21 06:42:51 +08:00
/// <typeparam name="TEventArgs">The type of the event arguments.</typeparam>
2014-04-17 08:39:30 +08:00
/// <param name="onEventFunc">The on event function.</param>
/// <param name="handler">The handler.</param>
2014-04-21 06:42:51 +08:00
/// <param name="e">The TEventArgs instance containing the event data.</param>
void SafeSyncableRaiseOnEvent < TEventArgs > ( Action < TEventArgs > onEventFunc , EventHandler < TEventArgs > handler , TEventArgs e ) where TEventArgs : EventArgs
2014-04-06 07:39:41 +08:00
{
2014-04-17 08:39:30 +08:00
var syncer = SynchronizationContext ;
if ( syncer = = null )
2014-04-06 07:39:41 +08:00
{
2014-04-06 08:50:13 +08:00
try
{
2014-04-21 06:42:51 +08:00
onEventFunc ( e ) ;
if ( handler ! = null ) { handler ( this , e ) ; }
2014-04-06 08:50:13 +08:00
}
catch { }
2014-04-06 07:39:41 +08:00
}
2014-04-17 08:39:30 +08:00
else
{
2014-04-21 06:42:51 +08:00
syncer . Send ( o = >
2014-04-17 08:39:30 +08:00
{
try
{
2014-04-21 06:42:51 +08:00
onEventFunc ( e ) ;
if ( handler ! = null ) { handler ( this , e ) ; }
2014-04-17 08:39:30 +08:00
}
catch { }
} , null ) ;
}
2014-04-06 07:39:41 +08:00
}
2014-04-06 06:33:21 +08:00
2014-04-06 07:39:41 +08:00
/// <summary>
2014-04-17 08:39:30 +08:00
/// Called when <see cref="State"/> changed.
/// </summary>
protected virtual void OnStateChanged ( ) { }
/// <summary>
/// Called when <see cref="SourceId"/> changed.
/// </summary>
protected virtual void OnSourceChanged ( ) { }
/// <summary>
/// Called when source has been disabled (back to state 4).
/// </summary>
protected virtual void OnSourceDisabled ( ) { }
/// <summary>
/// Called when the source has generated an event.
2014-04-06 07:39:41 +08:00
/// </summary>
/// <param name="e">The <see cref="DeviceEventArgs"/> instance containing the event data.</param>
2014-04-17 08:39:30 +08:00
protected virtual void OnDeviceEvent ( DeviceEventArgs e ) { }
2014-04-06 06:33:21 +08:00
2014-04-06 07:39:41 +08:00
/// <summary>
2014-04-17 08:39:30 +08:00
/// Called when a data transfer is ready.
2014-04-06 07:39:41 +08:00
/// </summary>
/// <param name="e">The <see cref="TransferReadyEventArgs"/> instance containing the event data.</param>
2014-04-22 18:50:58 +08:00
protected virtual void OnTransferReady ( TransferReadyEventArgs e ) { }
2014-04-06 06:33:21 +08:00
2014-04-06 07:39:41 +08:00
/// <summary>
2014-04-17 08:39:30 +08:00
/// Called when data has been transferred.
2014-04-06 07:39:41 +08:00
/// </summary>
/// <param name="e">The <see cref="DataTransferredEventArgs"/> instance containing the event data.</param>
2014-04-22 18:50:58 +08:00
protected virtual void OnDataTransferred ( DataTransferredEventArgs e ) { }
2014-04-06 07:39:41 +08:00
2014-04-12 23:07:13 +08:00
/// <summary>
2014-04-17 08:39:30 +08:00
/// Called when an error has been encountered during transfer.
2014-04-12 23:07:13 +08:00
/// </summary>
/// <param name="e">The <see cref="TransferErrorEventArgs"/> instance containing the event data.</param>
2014-04-22 18:50:58 +08:00
protected virtual void OnTransferError ( TransferErrorEventArgs e ) { }
2014-04-17 08:39:30 +08:00
2014-04-06 07:39:41 +08:00
#endregion
2014-04-06 06:33:21 +08:00
2014-04-21 06:42:51 +08:00
#region handle twain ds message
2014-04-06 06:33:21 +08:00
2014-04-21 04:57:38 +08:00
void HandleWndProcMessage ( ref WindowsHook . MESSAGE winMsg , ref bool handled )
2014-04-06 07:39:41 +08:00
{
2014-04-15 07:04:48 +08:00
// this handles the message from a typical WndProc message loop and check if it's from the TWAIN source.
2014-04-15 07:51:36 +08:00
if ( State > = 5 )
2014-04-06 07:39:41 +08:00
{
// transform it into a pointer for twain
IntPtr msgPtr = IntPtr . Zero ;
try
{
// no need to do another lock call when using marshal alloc
2014-04-19 21:10:15 +08:00
msgPtr = Marshal . AllocHGlobal ( Marshal . SizeOf ( winMsg ) ) ;
Marshal . StructureToPtr ( winMsg , msgPtr , false ) ;
2014-04-06 07:39:41 +08:00
2014-04-15 07:51:36 +08:00
var evt = new TWEvent ( ) ;
2014-04-06 07:39:41 +08:00
evt . pEvent = msgPtr ;
2014-04-15 07:04:48 +08:00
if ( handled = ( DGControl . Event . ProcessEvent ( evt ) = = ReturnCode . DSEvent ) )
2014-04-06 09:14:46 +08:00
{
2014-04-21 06:42:51 +08:00
Debug . WriteLine ( string . Format ( CultureInfo . InvariantCulture , "Thread {0}: HandleWndProcMessage at state {1} with MSG={2}." , Thread . CurrentThread . ManagedThreadId , State , evt . TWMessage ) ) ;
2014-04-15 07:04:48 +08:00
2014-04-15 07:30:25 +08:00
HandleSourceMsg ( evt . TWMessage ) ;
2014-04-06 09:14:46 +08:00
}
2014-04-06 07:39:41 +08:00
}
finally
{
if ( msgPtr ! = IntPtr . Zero ) { Marshal . FreeHGlobal ( msgPtr ) ; }
}
}
2014-04-06 06:33:21 +08:00
}
2014-04-06 07:39:41 +08:00
ReturnCode HandleCallback ( TWIdentity origin , TWIdentity destination , DataGroups dg , DataArgumentType dat , Message msg , IntPtr data )
2014-04-06 06:33:21 +08:00
{
if ( origin ! = null & & SourceId ! = null & & origin . Id = = SourceId . Id )
{
2014-04-21 06:42:51 +08:00
Debug . WriteLine ( string . Format ( CultureInfo . InvariantCulture , "Thread {0}: CallbackHandler at state {1} with MSG={2}." , Thread . CurrentThread . ManagedThreadId , State , msg ) ) ;
2014-04-15 07:04:48 +08:00
// spec says we must handle this on the thread that enabled the DS.
// by using the internal dispatcher this will be the case.
2014-04-06 08:50:13 +08:00
2014-04-15 07:04:48 +08:00
MessageLoop . Instance . BeginInvoke ( ( ) = >
2014-04-06 06:33:21 +08:00
{
2014-04-15 07:04:48 +08:00
HandleSourceMsg ( msg ) ;
} ) ;
2014-04-06 06:33:21 +08:00
return ReturnCode . Success ;
}
return ReturnCode . Failure ;
}
2014-04-22 18:50:58 +08:00
// final method that handles msg from the source, whether it's from wndproc or callbacks
2014-04-06 08:50:13 +08:00
void HandleSourceMsg ( Message msg )
2014-04-06 07:39:41 +08:00
{
2014-04-06 08:50:13 +08:00
switch ( msg )
{
case Message . XferReady :
if ( State < 6 )
{
State = 6 ;
}
2014-04-21 06:42:51 +08:00
TransferLogic . DoTransferRoutine ( this ) ;
2014-04-06 08:50:13 +08:00
break ;
case Message . DeviceEvent :
TWDeviceEvent de ;
var rc = DGControl . DeviceEvent . Get ( out de ) ;
if ( rc = = ReturnCode . Success )
{
2014-04-17 08:39:30 +08:00
SafeSyncableRaiseOnEvent ( OnDeviceEvent , DeviceEvent , new DeviceEventArgs ( de ) ) ;
2014-04-06 08:50:13 +08:00
}
break ;
case Message . CloseDSReq :
case Message . CloseDSOK :
// even though it says closeDS it's really disable.
// dsok is sent if source is enabled with uionly
// some sources send this at other states so do a step down
if ( State > 5 )
{
ForceStepDown ( 4 ) ;
}
else if ( State = = 5 )
{
// needs this state check since some source sends this more than once
2014-04-21 06:42:51 +08:00
( ( ITwainSessionInternal ) this ) . DisableSource ( ) ;
2014-04-06 08:50:13 +08:00
}
break ;
}
}
2014-04-06 07:39:41 +08:00
2014-04-06 19:08:52 +08:00
#endregion
2014-04-06 04:48:44 +08:00
}
}