public static extern ReturnCode DsmWin32(
     [In, Out] TW_IDENTITY origin,
     [In, Out] TW_IDENTITY destination,
     DataGroups dg,
     DataArgumentType dat,
     Message msg,
     ref TW_USERINTERFACE data);
Example #2
0
        public ReturnCode DisableDS(ref TW_USERINTERFACE ui, bool wasUIonly)
        {
            var rc = DoIt(Message.DisableDS, ref ui);

            if (rc == ReturnCode.Success)
            {
                Session.State = TwainState.S4;
                Session.OnSourceDisabled(new SourceDisabledEventArgs
                {
                    Source = Session.CurrentSource,
                    UIOnly = wasUIonly
                });
            }
            return(rc);
        }
Example #3
0
        public ReturnCode EnableDSUIOnly(ref TW_USERINTERFACE ui)
        {
            var rc = ReturnCode.Failure;

            if (Session.State == TwainState.S4)
            {
                Session.State = TwainState.S5; //tentative

                rc = DoIt(Message.EnableDSUIOnly, ref ui);

                if (rc != ReturnCode.Success)
                {
                    Session.State = TwainState.S4;
                }
            }
            return(rc);
        }
Example #4
0
        public ReturnCode EnableDS(ref TW_USERINTERFACE ui)
        {
            var rc = ReturnCode.Failure;

            if (Session.State == TwainState.S4)
            {
                Session.State = TwainState.S5; //tentative

                rc = DoIt(Message.EnableDS, ref ui);

                if (!(rc == ReturnCode.Success &&
                      (ui.ShowUI == 0 && rc == ReturnCode.CheckStatus)))
                {
                    Session.State = TwainState.S4;
                }
            }
            return(rc);
        }
Example #5
0
        ReturnCode DoIt(Message msg, ref TW_USERINTERFACE ui)
        {
            if (Is32Bit)
            {
                if (IsWin)
                {
                    return(NativeMethods.DsmWin32(Session.Config.App32, Session.CurrentSource.Identity32,
                                                  DataGroups.Control, DataArgumentType.UserInterface, msg, ref ui));
                }
                else if (IsLinux)
                {
                    return(NativeMethods.DsmLinux32(Session.Config.App32, Session.CurrentSource.Identity32,
                                                    DataGroups.Control, DataArgumentType.UserInterface, msg, ref ui));
                }
                else if (IsMac)
                {
                    return(NativeMethods.DsmMac32(Session.Config.App32, Session.CurrentSource.Identity32,
                                                  DataGroups.Control, DataArgumentType.UserInterface, msg, ref ui));
                }
            }
            else
            {
                if (IsWin)
                {
                    return(NativeMethods.DsmWin64(Session.Config.App32, Session.CurrentSource.Identity32,
                                                  DataGroups.Control, DataArgumentType.UserInterface, msg, ref ui));
                }
                else if (IsLinux)
                {
                    return(NativeMethods.DsmLinux64(Session.Config.App32, Session.CurrentSource.Identity32,
                                                    DataGroups.Control, DataArgumentType.UserInterface, msg, ref ui));
                }
                else if (IsMac)
                {
                    return(NativeMethods.DsmMac64(Session.Config.App32, Session.CurrentSource.Identity32,
                                                  DataGroups.Control, DataArgumentType.UserInterface, msg, ref ui));
                }
            }

            return(ReturnCode.Failure);
        }
Example #6
0
        /// <summary>
        /// Enables the source for transferring data.
        /// </summary>
        /// <param name="showUI">if set to <c>true</c> then show driver UI. Not all sources support turning UI off.</param>
        /// <param name="windowHandle">The parent window handle if on Windows. Use <see cref="IntPtr.Zero"/> if not applicable.</param>
        /// <param name="modal">if set to <c>true</c> any driver UI may be displayed as modal.</param>
        /// <returns></returns>
        public ReturnCode Enable(bool showUI, IntPtr windowHandle, bool modal = false)
        {
            var rc = ReturnCode.Failure;

            Session.InternalInvoke(() =>
            {
                var ui = new TW_USERINTERFACE
                {
                    ShowUI  = (ushort)(showUI ? 1 : 0),
                    ModalUI = (ushort)(modal ? 1 : 0),
                    hParent = windowHandle
                };
                Session._disableDSNow = false;
                rc = Session.DGControl.UserInterface.EnableDS(ref ui);
                if (rc == ReturnCode.Success)
                {
                    Session._lastEnableUI = ui;
                }
            });
            return(rc);
        }
Example #7
0
        public STS DatUserinterface(DG a_dg, MSG a_msg, ref TW_USERINTERFACE a_twuserinterface)
        {
            STS sts;

            // Submit the work to the TWAIN thread...
            if (this.m_runinuithreaddelegate == null)
            {
                if ((m_threadTwain != null) && (m_threadTwain.ManagedThreadId != Thread.CurrentThread.ManagedThreadId))
                {
                    lock (m_lockTwain)
                    {
                        // Set our command variables...
                        ThreadData threaddata = default(ThreadData);
                        threaddata.twuserinterface = a_twuserinterface;
                        threaddata.twuserinterface.hParent = m_intptrHwnd;
                        threaddata.dg = a_dg;
                        threaddata.msg = a_msg;
                        threaddata.dat = DAT.USERINTERFACE;
                        m_lIndexDatUserinterface = m_twaincommand.Submit(threaddata);

                        // Submit the command and wait for the reply...
                        CallerToThreadSet();
                        ThreadToCallerWaitOne();

                        // Return the result...
                        a_twuserinterface = m_twaincommand.Get(m_lIndexDatUserinterface).twuserinterface;
                        sts = m_twaincommand.Get(m_lIndexDatUserinterface).sts;

                        // Clear the command variables...
                        m_twaincommand.Delete(m_lIndexDatUserinterface);
                    }
                    return (sts);
                }
            }

            // Well this is weird.  I'm not sure how this design snuck past,
            // I assume it's because of the async nature of the button presses,
            // so it's easier to monitor a boolean.  Regardless, we need to
            // use this data to do the right thing...
            if (m_blIsMsgclosedsok || m_blIsMsgclosedsreq)
            {
                a_msg = MSG.DISABLEDS;
            }

            // If we're doing a DISABLEDS, use the values we remembered from
            // the last ENABLEDS...
            TW_USERINTERFACE twuserinterface = a_twuserinterface;
            if (a_msg == MSG.DISABLEDS)
            {
                twuserinterface = m_twuserinterface;
            }

            // Log it...
            if (Log.GetLevel() > 0)
            {
                Log.LogSendBefore(a_dg.ToString(), DAT.USERINTERFACE.ToString(), a_msg.ToString(), UserinterfaceToCsv(twuserinterface));
            }

            // We need this to handle data sources that return MSG_XFERREADY in
            // the midst of processing MSG_ENABLEDS...
            if (a_msg == MSG.ENABLEDS)
            {
                m_blAcceptXferReady = true;
            }

            // Windows...
            if (ms_platform == Platform.WINDOWS)
            {
                // Issue the command...
                try
                {
                    if (this.m_runinuithreaddelegate == null)
                    {
                        if (m_blUseLegacyDSM)
                        {
                            sts = (STS)WindowsTwain32DsmEntryUserinterface(ref m_twidentitylegacyApp, ref m_twidentitylegacyDs, a_dg, DAT.USERINTERFACE, a_msg, ref twuserinterface);
                        }
                        else
                        {
                            sts = (STS)WindowsTwaindsmDsmEntryUserinterface(ref m_twidentitylegacyApp, ref m_twidentitylegacyDs, a_dg, DAT.USERINTERFACE, a_msg, ref twuserinterface);
                        }
                    }
                    else
                    {
                        if (m_blUseLegacyDSM)
                        {
                            lock (m_lockTwain)
                            {
                                ThreadData threaddata = default(ThreadData);
                                threaddata.twuserinterface = a_twuserinterface;
                                threaddata.twuserinterface.hParent = m_intptrHwnd;
                                threaddata.dg = a_dg;
                                threaddata.msg = a_msg;
                                threaddata.dat = DAT.USERINTERFACE;
                                m_lIndexDatUserinterface = m_twaincommand.Submit(threaddata);
                                RunInUiThread(DatUserinterfaceWindowsTwain32);
                                a_twuserinterface = m_twaincommand.Get(m_lIndexDatUserinterface).twuserinterface;
                                sts = m_twaincommand.Get(m_lIndexDatUserinterface).sts;
                                m_twaincommand.Delete(m_lIndexDatUserinterface);
                            }
                        }
                        else
                        {
                            lock (m_lockTwain)
                            {
                                ThreadData threaddata = default(ThreadData);
                                threaddata.twuserinterface = a_twuserinterface;
                                threaddata.twuserinterface.hParent = m_intptrHwnd;
                                threaddata.dg = a_dg;
                                threaddata.msg = a_msg;
                                threaddata.dat = DAT.USERINTERFACE;
                                m_lIndexDatUserinterface = m_twaincommand.Submit(threaddata);
                                RunInUiThread(DatUserinterfaceWindowsTwainDsm);
                                a_twuserinterface = m_twaincommand.Get(m_lIndexDatUserinterface).twuserinterface;
                                sts = m_twaincommand.Get(m_lIndexDatUserinterface).sts;
                                m_twaincommand.Delete(m_lIndexDatUserinterface);
                            }
                        }
                    }
                }
                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)LinuxDsmEntryUserinterface(ref m_twidentitylegacyApp, ref m_twidentitylegacyDs, a_dg, DAT.USERINTERFACE, a_msg, ref twuserinterface);
                    }
                    else
                    {
                        sts = (STS)Linux64DsmEntryUserinterface(ref m_twidentityApp, ref m_twidentityDs, a_dg, DAT.USERINTERFACE, a_msg, ref twuserinterface);
                    }
                }
                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)MacosxDsmEntryUserinterface(ref m_twidentitymacosxApp, ref m_twidentitymacosxDs, a_dg, DAT.USERINTERFACE, a_msg, ref twuserinterface);
                }
                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(), "");
            }

            // If we opened, go to state 5...
            if ((a_msg == MSG.ENABLEDS) || (a_msg == MSG.ENABLEDSUIONLY))
            {
                if (sts == STS.SUCCESS)
                {
                    m_state = STATE.S5;

                    // Remember the setting...
                    m_twuserinterface = a_twuserinterface;

                    // MSG_XFERREADY showed up while we were still processing MSG_ENABLEDS
                    if ((sts == STS.SUCCESS) && m_blAcceptXferReady && m_blIsMsgxferready)
                    {
                        m_blAcceptXferReady = false;
                        m_state = STATE.S6;
                        // TBD
                        //lock (m_lockTwain)
                        //{
                        //    m_threaddata = default(ThreadData);
                        //}
                        CallerToThreadSet();
                    }
                }
            }

            // If we disabled, go to state 4...
            else if (a_msg == MSG.DISABLEDS)
            {
                if (sts == STS.SUCCESS)
                {
                    m_blIsMsgclosedsreq = false;
                    m_blIsMsgclosedsok = false;
                    m_state = STATE.S4;
                }
            }

            // All done...
            return (AutoDatStatus(sts));
        }
Example #8
0
        /// <summary>
        /// Convert a string to a userinterface...
        /// </summary>
        /// <param name="a_twuserinterface">A TWAIN structure</param>
        /// <param name="a_szUserinterface">A CSV string of the TWAIN structure</param>
        /// <returns>True if the conversion is successful</returns>
        public bool CsvToUserinterface(ref TW_USERINTERFACE a_twuserinterface, string a_szUserinterface)
        {
            // Init stuff...
            a_twuserinterface = default(TW_USERINTERFACE);

            // Build the string...
            try
            {
                string[] asz = CSV.Parse(a_szUserinterface);

                // Init stuff...
                a_twuserinterface.ShowUI = 0;
                a_twuserinterface.ModalUI = 0;
                a_twuserinterface.hParent = IntPtr.Zero;

                // Sort out the values...
                if (asz.Length >= 1)
                {
                     ushort.TryParse(asz[0], out a_twuserinterface.ShowUI);
                }
                if (asz.Length >= 2)
                {
                     ushort.TryParse(asz[1], out a_twuserinterface.ModalUI);
                }
                if (asz.Length >= 3)
                {
                    Int64 i64;
                    if (Int64.TryParse(asz[2], out i64))
                    {
                        a_twuserinterface.hParent = (IntPtr)i64;
                    }
                }
            }
            catch
            {
                return (false);
            }

            // All done...
            return (true);
        }
Example #9
0
 /// <summary>
 /// Convert the contents of a userinterface to a string that we can show in
 /// our simple GUI...
 /// </summary>
 /// <param name="a_twuserinterface">A TWAIN structure</param>
 /// <returns>A CSV string of the TWAIN structure</returns>
 public string UserinterfaceToCsv(TW_USERINTERFACE a_twuserinterface)
 {
     try
     {
         CSV csv = new CSV();
         csv.Add(a_twuserinterface.ShowUI.ToString());
         csv.Add(a_twuserinterface.ModalUI.ToString());
         return (csv.Get());
     }
     catch
     {
         return ("***error***");
     }
 }
Example #10
0
 private static extern UInt16 MacosxDsmEntryUserinterface
 (
     ref TW_IDENTITY_MACOSX origin,
     ref TW_IDENTITY_MACOSX dest,
     DG dg,
     DAT dat,
     MSG msg,
     ref TW_USERINTERFACE twuserinterface
 );
Example #11
0
 private static extern UInt16 Linux64DsmEntryUserinterface
 (
     ref TW_IDENTITY origin,
     ref TW_IDENTITY dest,
     DG dg,
     DAT dat,
     MSG msg,
     ref TW_USERINTERFACE twuserinterface
 );
Example #12
0
 private static extern UInt16 WindowsTwaindsmDsmEntryUserinterface
 (
     ref TW_IDENTITY_LEGACY origin,
     ref TW_IDENTITY_LEGACY dest,
     DG dg,
     DAT dat,
     MSG msg,
     ref TW_USERINTERFACE twuserinterface
 );
Example #13
0
        /// <summary>
        /// Issue capabilities commands...
        /// </summary>
        /// <param name="a_dg">Data group</param>
        /// <param name="a_msg">Operation</param>
        /// <param name="a_twuserinterface">USERINTERFACE structure</param>
        /// <returns>TWAIN status</returns>
        public virtual STS DatUserinterface(DG a_dg, MSG a_msg, ref TW_USERINTERFACE a_twuserinterface)
        {
            STS sts;

            // Submit the work to the TWAIN thread...
            //
            // Normally this would be a good thing, however we have a problem on Windows,
            // in that any window created by this call has to appear in the same thread as
            // the HINSTANCE that was passed to the TWAIN driver's DllMain.  Otherwise the
            // dispatcher won't be able to make the connection to forward events to the
            // driver's window.
            //
            // The same problem could occur on destroy, especially when trying to free
            // resources.  So we're not forwarding this message to our thread.  It'll run
            // in the context of the caller's thread.
            /*
            if ((m_threadTwain != null) && (m_threadTwain.ManagedThreadId != Thread.CurrentThread.ManagedThreadId))
            {
                lock (m_lockTwain)
                {
                    // Set our command variables...
                    m_threaddata = default(ThreadData);
                    m_threaddata.twuserinterface = a_twuserinterface;
                    m_threaddata.dg = a_dg;
                    m_threaddata.msg = a_msg;
                    m_threaddata.dat = DAT.USERINTERFACE;

                    // Submit the command and wait for the reply...
                    CallerToThreadSet();
                    ThreadToCallerWaitOne();

                    // Return the result...
                    a_twuserinterface = m_threaddata.twuserinterface;
                    sts = m_threaddata.sts;

                    // Clear the command variables...
                    m_threaddata = default(ThreadData);
                }
                return (sts);
            }
            */

            // Log it...
            if (Log.GetLevel() > 0)
            {
                Log.LogSendBefore(a_dg, DAT.USERINTERFACE, a_msg, UserinterfaceToCsv(a_twuserinterface));
            }

            // We need this to handle data sources that return MSG_XFERREADY in
            // the midst of processing MSG_ENABLEDS...
            if (a_msg == MSG.ENABLEDS)
            {
                m_blAcceptXferReady = true;
            }

            // Windows...
            if (ms_platform == Platform.WINDOWS)
            {
                // Issue the command...
                try
                {
                    if (m_blUseLegacyDSM)
                    {
                        sts = (STS)WindowsTwain32DsmEntryUserinterface(ref m_twidentitylegacyApp, ref m_twidentitylegacyDs, a_dg, DAT.USERINTERFACE, a_msg, ref a_twuserinterface);
                    }
                    else
                    {
                        sts = (STS)WindowsTwaindsmDsmEntryUserinterface(ref m_twidentitylegacyApp, ref m_twidentitylegacyDs, a_dg, DAT.USERINTERFACE, a_msg, ref a_twuserinterface);
                    }
                }
                catch
                {
                    // The driver crashed...
                    Log.LogSendAfter(STS.BUMMER, "");
                    return (STS.BUMMER);
                }
            }

            // Linux...
            else if (ms_platform == Platform.LINUX)
            {
                // Issue the command...
                try
                {
                    if (GetMachineWordBitSize() == 32)
                    {
                        sts = (STS)LinuxDsmEntryUserinterface(ref m_twidentitylegacyApp, ref m_twidentitylegacyDs, a_dg, DAT.USERINTERFACE, a_msg, ref a_twuserinterface);
                    }
                    else
                    {
                        sts = (STS)Linux64DsmEntryUserinterface(ref m_twidentityApp, ref m_twidentityDs, a_dg, DAT.USERINTERFACE, a_msg, ref a_twuserinterface);
                    }
                }
                catch
                {
                    // The driver crashed...
                    Log.LogSendAfter(STS.BUMMER, "");
                    return (STS.BUMMER);
                }
            }

            // Mac OS X, which has to be different...
            else if (ms_platform == Platform.MACOSX)
            {
                // Issue the command...
                try
                {
                    sts = (STS)MacosxDsmEntryUserinterface(ref m_twidentitymacosxApp, ref m_twidentitymacosxDs, a_dg, DAT.USERINTERFACE, a_msg, ref a_twuserinterface);
                }
                catch
                {
                    // The driver crashed...
                    Log.LogSendAfter(STS.BUMMER, "");
                    return (STS.BUMMER);
                }
            }

            // Uh-oh...
            else
            {
                Log.LogSendAfter(STS.BUMMER, "");
                return (STS.BUMMER);
            }

            // Log it...
            if (Log.GetLevel() > 0)
            {
                Log.LogSendAfter(sts, "");
            }

            // If we opened, go to state 5...
            if ((a_msg == MSG.ENABLEDS) || (a_msg == MSG.ENABLEDSUIONLY))
            {
                if (sts == STS.SUCCESS)
                {
                    m_state = STATE.S5;

                    // MSG_XFERREADY showed up while we were still processing MSG_ENABLEDS
                    if ((sts == STS.SUCCESS) && m_blAcceptXferReady && m_blIsMsgxferready)
                    {
                        m_blAcceptXferReady = false;
                        m_state = STATE.S6;
                        CallerToThreadSet();
                    }
                }
            }

            // If we disabled, go to state 4...
            else if (a_msg == MSG.DISABLEDS)
            {
                if (sts == STS.SUCCESS)
                {
                    m_blIsMsgclosedsreq = false;
                    m_blIsMsgclosedsok = false;
                    m_state = STATE.S4;
                }
            }

            // All done...
            return (AutoDatStatus(sts));
        }
Example #14
0
        /// <summary>
        /// Convert a string to a userinterface...
        /// </summary>
        /// <param name="a_twuserinterface">A TWAIN structure</param>
        /// <param name="a_szUserinterface">A CSV string of the TWAIN structure</param>
        /// <returns>True if the conversion is successful</returns>
        public virtual bool CsvToUserinterface(ref TW_USERINTERFACE a_twuserinterface, string a_szUserinterface)
        {
            // Init stuff...
            a_twuserinterface = default(TW_USERINTERFACE);

            // Build the string...
            try
            {
                string[] asz = CSV.Parse(a_szUserinterface);

                // Sort out the values...
                a_twuserinterface.ShowUI = ushort.Parse(asz[0]);
                a_twuserinterface.ModalUI = ushort.Parse(asz[1]);
            }
            catch
            {
                return (false);
            }

            // All done...
            return (true);
        }