mirror of
https://github.com/soukoku/ntwain.git
synced 2025-12-20 03:59:56 +08:00
More cleanup.
This commit is contained in:
@@ -299,6 +299,7 @@ namespace NTwain
|
|||||||
/// <param name="modal">if set to <c>true</c> any driver UI will display as modal.</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>
|
/// <param name="windowHandle">The window handle if modal.</param>
|
||||||
/// <param name="context">The
|
/// <param name="context">The
|
||||||
|
/// Windows only.
|
||||||
/// <see cref="SynchronizationContext" /> that is required for certain operations.
|
/// <see cref="SynchronizationContext" /> that is required for certain operations.
|
||||||
/// It is recommended you call this method in an UI thread and pass in
|
/// It is recommended you call this method in an UI thread and pass in
|
||||||
/// <see cref="SynchronizationContext.Current" />
|
/// <see cref="SynchronizationContext.Current" />
|
||||||
@@ -307,7 +308,11 @@ namespace NTwain
|
|||||||
/// <exception cref="ArgumentNullException">context</exception>
|
/// <exception cref="ArgumentNullException">context</exception>
|
||||||
public ReturnCode EnableSource(SourceEnableMode mode, bool modal, HandleRef windowHandle, SynchronizationContext context)
|
public ReturnCode EnableSource(SourceEnableMode mode, bool modal, HandleRef windowHandle, SynchronizationContext context)
|
||||||
{
|
{
|
||||||
if (context == null) { throw new ArgumentNullException("context"); }
|
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
|
||||||
|
{
|
||||||
|
if (context == null) { throw new ArgumentNullException("context"); }
|
||||||
|
}
|
||||||
|
|
||||||
Debug.WriteLine(string.Format("Thread {0}: EnableSource.", Thread.CurrentThread.ManagedThreadId));
|
Debug.WriteLine(string.Format("Thread {0}: EnableSource.", Thread.CurrentThread.ManagedThreadId));
|
||||||
|
|
||||||
_syncer = context;
|
_syncer = context;
|
||||||
@@ -456,7 +461,11 @@ namespace NTwain
|
|||||||
var hand = StateChanged;
|
var hand = StateChanged;
|
||||||
if (hand != null)
|
if (hand != null)
|
||||||
{
|
{
|
||||||
hand(this, EventArgs.Empty);
|
try
|
||||||
|
{
|
||||||
|
hand(this, EventArgs.Empty);
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -468,7 +477,11 @@ namespace NTwain
|
|||||||
var hand = SourceChanged;
|
var hand = SourceChanged;
|
||||||
if (hand != null)
|
if (hand != null)
|
||||||
{
|
{
|
||||||
hand(this, EventArgs.Empty);
|
try
|
||||||
|
{
|
||||||
|
hand(this, EventArgs.Empty);
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -480,7 +493,11 @@ namespace NTwain
|
|||||||
var hand = SourceDisabled;
|
var hand = SourceDisabled;
|
||||||
if (hand != null)
|
if (hand != null)
|
||||||
{
|
{
|
||||||
hand(this, EventArgs.Empty);
|
try
|
||||||
|
{
|
||||||
|
hand(this, EventArgs.Empty);
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -493,7 +510,11 @@ namespace NTwain
|
|||||||
var hand = DeviceEvent;
|
var hand = DeviceEvent;
|
||||||
if (hand != null)
|
if (hand != null)
|
||||||
{
|
{
|
||||||
hand(this, e);
|
try
|
||||||
|
{
|
||||||
|
hand(this, e);
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -506,7 +527,11 @@ namespace NTwain
|
|||||||
var hand = TransferReady;
|
var hand = TransferReady;
|
||||||
if (hand != null)
|
if (hand != null)
|
||||||
{
|
{
|
||||||
hand(this, e);
|
try
|
||||||
|
{
|
||||||
|
hand(this, e);
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -519,7 +544,11 @@ namespace NTwain
|
|||||||
var hand = DataTransferred;
|
var hand = DataTransferred;
|
||||||
if (hand != null)
|
if (hand != null)
|
||||||
{
|
{
|
||||||
hand(this, e);
|
try
|
||||||
|
{
|
||||||
|
hand(this, e);
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -534,6 +563,7 @@ namespace NTwain
|
|||||||
/// <returns>True if handled by TWAIN.</returns>
|
/// <returns>True if handled by TWAIN.</returns>
|
||||||
protected bool HandleWndProcMessage(ref MESSAGE message)
|
protected bool HandleWndProcMessage(ref MESSAGE message)
|
||||||
{
|
{
|
||||||
|
var handled = false;
|
||||||
if (State >= 4) // technically we should only handle on state >= 5 but there might be missed msgs if we wait until state changes after enabling ds
|
if (State >= 4) // technically we should only handle on state >= 5 but there might be missed msgs if we wait until state changes after enabling ds
|
||||||
{
|
{
|
||||||
// transform it into a pointer for twain
|
// transform it into a pointer for twain
|
||||||
@@ -546,17 +576,16 @@ namespace NTwain
|
|||||||
|
|
||||||
TWEvent evt = new TWEvent();
|
TWEvent evt = new TWEvent();
|
||||||
evt.pEvent = msgPtr;
|
evt.pEvent = msgPtr;
|
||||||
var rc = DGControl.Event.ProcessEvent(evt);
|
handled = DGControl.Event.ProcessEvent(evt) == ReturnCode.DSEvent;
|
||||||
// TODO: not sure what to DAT to pass
|
|
||||||
HandleSourceMsg(null, null, DataGroups.Control, DataArgumentType.Null, evt.TWMessage, IntPtr.Zero);
|
HandleSourceMsg(evt.TWMessage);
|
||||||
return rc == ReturnCode.DSEvent;
|
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
if (msgPtr != IntPtr.Zero) { Marshal.FreeHGlobal(msgPtr); }
|
if (msgPtr != IntPtr.Zero) { Marshal.FreeHGlobal(msgPtr); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return handled;
|
||||||
}
|
}
|
||||||
|
|
||||||
ReturnCode HandleCallback(TWIdentity origin, TWIdentity destination, DataGroups dg, DataArgumentType dat, Message msg, IntPtr data)
|
ReturnCode HandleCallback(TWIdentity origin, TWIdentity destination, DataGroups dg, DataArgumentType dat, Message msg, IntPtr data)
|
||||||
@@ -568,6 +597,7 @@ namespace NTwain
|
|||||||
// but it's usually already the same thread and doesn't work (failure + seqError) w/o jumping to another thread and back.
|
// but it's usually already the same thread and doesn't work (failure + seqError) w/o jumping to another thread and back.
|
||||||
// My guess is the DS needs to see the Success return code first before letting transfer happen
|
// My guess is the DS needs to see the Success return code first before letting transfer happen
|
||||||
// so this is an hack to make it happen.
|
// so this is an hack to make it happen.
|
||||||
|
|
||||||
// TODO: find a better method.
|
// TODO: find a better method.
|
||||||
ThreadPool.QueueUserWorkItem(o =>
|
ThreadPool.QueueUserWorkItem(o =>
|
||||||
{
|
{
|
||||||
@@ -576,13 +606,13 @@ namespace NTwain
|
|||||||
{
|
{
|
||||||
_syncer.Send(blah =>
|
_syncer.Send(blah =>
|
||||||
{
|
{
|
||||||
HandleSourceMsg(origin, destination, dg, dat, msg, data);
|
HandleSourceMsg(msg);
|
||||||
}, null);
|
}, null);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// no context? better hope for the best!
|
// no context? better hope for the best!
|
||||||
HandleSourceMsg(origin, destination, dg, dat, msg, data);
|
HandleSourceMsg(msg);
|
||||||
}
|
}
|
||||||
}, _syncer);
|
}, _syncer);
|
||||||
return ReturnCode.Success;
|
return ReturnCode.Success;
|
||||||
@@ -590,16 +620,57 @@ namespace NTwain
|
|||||||
return ReturnCode.Failure;
|
return ReturnCode.Failure;
|
||||||
}
|
}
|
||||||
|
|
||||||
// final method that handles stuff from the source, whether it's from wndproc or callbacks
|
// method that handles msg from the source, whether it's from wndproc or callbacks
|
||||||
protected virtual void HandleSourceMsg(TWIdentity origin, TWIdentity destination, DataGroups dg, DataArgumentType dat, Message msg, IntPtr data)
|
void HandleSourceMsg(Message msg)
|
||||||
{
|
{
|
||||||
if (msg != Message.Null)
|
if (msg != Message.Null)
|
||||||
{
|
{
|
||||||
Debug.WriteLine(string.Format("Thread {0}: HandleSourceMsg at state {1} with DG={2} DAT={3} MSG={4}.", Thread.CurrentThread.ManagedThreadId, State, dg, dat, msg));
|
Debug.WriteLine(string.Format("Thread {0}: HandleSourceMsg at state {1} with MSG={2}.", Thread.CurrentThread.ManagedThreadId, State, msg));
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new NotImplementedException();
|
switch (msg)
|
||||||
|
{
|
||||||
|
case Message.XferReady:
|
||||||
|
if (State < 6)
|
||||||
|
{
|
||||||
|
State = 6;
|
||||||
|
}
|
||||||
|
DoTransferRoutine();
|
||||||
|
break;
|
||||||
|
case Message.DeviceEvent:
|
||||||
|
TWDeviceEvent de;
|
||||||
|
var rc = DGControl.DeviceEvent.Get(out de);
|
||||||
|
if (rc == ReturnCode.Success)
|
||||||
|
{
|
||||||
|
OnDeviceEvent(new DeviceEventArgs(de));
|
||||||
|
}
|
||||||
|
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
|
||||||
|
DisableSource();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Does the TWAIN transfer routine at state 6.
|
||||||
|
/// </summary>
|
||||||
|
/// <exception cref="NotImplementedException"></exception>
|
||||||
|
protected virtual void DoTransferRoutine()
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ namespace NTwain
|
|||||||
/// <exception cref="System.ArgumentNullException"></exception>
|
/// <exception cref="System.ArgumentNullException"></exception>
|
||||||
public TwainSessionOld(TWIdentity appId) : base(appId) { }
|
public TwainSessionOld(TWIdentity appId) : base(appId) { }
|
||||||
|
|
||||||
private void DoTransferRoutine()
|
protected override void DoTransferRoutine()
|
||||||
{
|
{
|
||||||
TWPendingXfers pending = new TWPendingXfers();
|
TWPendingXfers pending = new TWPendingXfers();
|
||||||
var rc = ReturnCode.Success;
|
var rc = ReturnCode.Success;
|
||||||
@@ -191,48 +191,6 @@ namespace NTwain
|
|||||||
|
|
||||||
#region messaging use
|
#region messaging use
|
||||||
|
|
||||||
protected override void HandleSourceMsg(TWIdentity origin, TWIdentity destination, DataGroups dg, DataArgumentType dat, Values.Message msg, IntPtr data)
|
|
||||||
{
|
|
||||||
if (msg != Values.Message.Null)
|
|
||||||
{
|
|
||||||
Debug.WriteLine(string.Format("Thread {0}: HandleSourceMsg at state {1} with DG={2} DAT={3} MSG={4}.", Thread.CurrentThread.ManagedThreadId, State, dg, dat, msg));
|
|
||||||
}
|
|
||||||
switch (msg)
|
|
||||||
{
|
|
||||||
case Values.Message.XferReady:
|
|
||||||
if (State < 6)
|
|
||||||
State = 6;
|
|
||||||
// this is the meat of all twain stuff
|
|
||||||
DoTransferRoutine();
|
|
||||||
break;
|
|
||||||
case Values.Message.DeviceEvent:
|
|
||||||
TWDeviceEvent de;
|
|
||||||
var rc = DGControl.DeviceEvent.Get(out de);
|
|
||||||
if (rc == ReturnCode.Success)
|
|
||||||
{
|
|
||||||
OnDeviceEvent(new DeviceEventArgs(de));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case Values.Message.CloseDSReq:
|
|
||||||
case Values.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
|
|
||||||
DisableSource();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Message loop processor for winform.
|
/// Message loop processor for winform.
|
||||||
/// Use this by adding the <see cref="TwainSessionOld"/> as an <see cref="IMessageFilter "/>.
|
/// Use this by adding the <see cref="TwainSessionOld"/> as an <see cref="IMessageFilter "/>.
|
||||||
|
|||||||
Reference in New Issue
Block a user