protected bool Write(OutputReport oOutRep)
        {
            bool success = false;

            try
            {
                // ����� �� ������������ ���... HidD_SetOutputReport �������� � ���������� ������ ��� Feature !
                //success = HidD_SetOutputReport(m_hHandle, oOutRep.Buffer, oOutRep.BufferLength);
                if (m_oFile != null)
                {
                    if (m_oFile.CanWrite)
                    {
                        m_oFile.Write(oOutRep.Buffer, 0, oOutRep.BufferLength);
                        success = true;
                    }
                }
            }
            catch (Exception ex)
            {
                // ���� ��������� � ������������ Report ID - �� �� ���������
                if (ex.HResult != -2147024809)
                {
                    HandleDeviceRemoved();
                    OnDeviceRemoved?.Invoke(this, new EventArgs());
                    Dispose();
                }
            }
            return(success);
        }
        /// <summary>
        /// Callback for above. Care with this as it will be called on the background thread from the async read
        /// </summary>
        /// <param name="iResult">Async result parameter</param>
        protected void ReadCompleted(IAsyncResult iResult)
        {
            if (m_iFile == null)
            {
                return;
            }

            byte[] arrBuff = (byte[])iResult.AsyncState;
            try
            {
                m_iFile.EndRead(iResult);
                try
                {
                    InputReport oInRep = CreateInputReport();
                    oInRep.SetData(arrBuff);
                    HandleDataReceived(oInRep);
                }
                finally
                {
                    BeginAsyncRead();
                }
            }
            catch (IOException ex)
            {
                HandleDeviceRemoved();
                OnDeviceRemoved?.Invoke(this, new EventArgs());
                Dispose();
            }
        }
Exemple #3
0
        /// <summary>
        /// This method will filter the messages that are passed for usb device change messages only.
        /// And parse them and take the appropriate action
        /// </summary>
        /// <param name="m">a ref to Messages, The messages that are thrown by windows to the application.</param>
        /// <example> This sample shows how to implement this method in your form.
        /// <code>
        ///protected override void WndProc(ref Message m)
        ///{
        ///    usb.ParseMessages(ref m);
        ///    base.WndProc(ref m);	    // pass message on to base form
        ///}
        ///</code>
        ///</example>
        public void ParseMessages(ref Message m)
        {
            if (m.Msg == WM_DEVICECHANGE)               // we got a device change message! A USB device was inserted or removed
            {
                switch (m.WParam.ToInt32())             // Check the W parameter to see if a device was inserted or removed
                {
                case DEVICE_ARRIVAL:                    // inserted
                    OnDeviceArrived?.Invoke(this, new EventArgs());
                    break;

                case DEVICE_REMOVECOMPLETE:                         // removed
                    OnDeviceRemoved?.Invoke(this, new EventArgs());
                    break;
                }
            }
        }
        protected bool WriteFeature(FeatureReport oFeaRep)
        {
            bool success = false;

            try
            {
                success = HidD_SetFeature(m_hHandle, oFeaRep.Buffer, oFeaRep.BufferLength);
            }
            catch (IOException)
            {
                HandleDeviceRemoved();
                OnDeviceRemoved?.Invoke(this, new EventArgs());
                Dispose();
            }
            return(success);
        }
        protected bool ReadFeature(FeatureReport oFeaRep)
        {
            byte[] arrBuff = new byte[FeatureReportLength];
            bool   success = false;

            try
            {
                success = HidD_GetFeature(m_hHandle, arrBuff, arrBuff.Length);
                oFeaRep.SetData(arrBuff);
            }
            catch (IOException)
            {
                HandleDeviceRemoved();
                OnDeviceRemoved?.Invoke(this, new EventArgs());
                Dispose();
            }

            return(success);
        }
Exemple #6
0
        /// <summary>
        /// Callback for above. Care with this as it will be called on the background thread from the async read
        /// </summary>
        /// <param name="iResult">Async result parameter</param>
        protected void ReadCompleted(IAsyncResult iResult)
        {
            if (!m_isConnected || (m_oFile == null))
            {
                return;
            }

            // retrieve the read buffer
            var arrBuff = (byte[])iResult.AsyncState;

            try
            {
                m_oFile.EndRead(iResult);                       // call end read : this throws any exceptions that happened during the read

                try
                {
                    HandleDataReceived(new HIDReport(arrBuff));                         // pass the new input report on to the higher level handler
                }
                finally
                {
                    BeginAsyncRead();                           // when all that is done, kick off another read for the next report
                }
            }
            catch (IOException)                 // if we got an IO exception, the device was removed
            {
                //HandleDeviceRemoved();
                OnDeviceRemoved?.Invoke(this, new EventArgs());

                Dispose();

                // The device was removed!
                //MessageBox.Show(ex.Message, "Warning !", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            catch (Exception ex)
            {
                // The device was removed!
                MessageBox.Show(ex.Message, "ERROR !", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Exemple #7
0
        public bool RemoveDevice(Device device)
        {
            if (deviceDictionary.ContainsKey(device))
            {
                DeviceCode deviceCode = deviceDictionary[device];
                deviceDictionary.Remove(device);
                switch (deviceCode)
                {
                case DeviceCode.Head:
                    HeadDeviceConnected = deviceDictionary.Values.Any(x => x == DeviceCode.Head);
                    break;

                case DeviceCode.HandTake:
                    HandTakeDeviceConnected = deviceDictionary.Values.Any(x => x == DeviceCode.HandTake);
                    break;
                }
                OnDeviceRemoved?.Invoke(this, device);
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemple #8
0
        /* Handles device removing errors. */
        private void OnDeviceRemovedHandler()
        {
            log.Fatal("Device(" + Index + ") unexpectedly removed!");

            OnDeviceRemoved?.Invoke(this);
        }
        /// <summary>
        /// Prevents a default instance of the <see cref="AndroidController"/> class from being created.
        /// </summary>
        private AndroidController()
        {
            this.connectedDevices = new List <string>();
            ResourceFolderManager.Register(ANDROID_CONTROLLER_TMP_FOLDER);
            this.resourceDirectory = ResourceFolderManager.GetRegisteredFolderPath(ANDROID_CONTROLLER_TMP_FOLDER);
            //CreateResourceDirectories();
            //ExtractResources();
            this.m_eventWatcher = new ManagementEventWatcher(
                new WqlEventQuery("SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2")
                );
            this.m_eventWatcher.EventArrived += (s, evt) => {
                var m_oldConnectedDevices = connectedDevices;
                var m_newConnectedDevices = ConnectedDevices;

                if (m_oldConnectedDevices.Count != m_newConnectedDevices.Count)
                {
                    // List sizes do not match.
                    // Depending on whether the new list is longer or short, fire the corresponding event.
                    if (m_newConnectedDevices.Count > m_oldConnectedDevices.Count)
                    {
                        // A new device was added.
                        // Find out which device it was.
                        foreach (string m_device in m_newConnectedDevices)
                        {
                            if (!m_oldConnectedDevices.Contains(m_device))
                            {
                                // We've found the device that was added.
                                // We know this because the old list doesn't contain this device's serial.
                                // Now fire OnDeviceAdded event(s).
                                foreach (DeviceAddedEventHandler m_handler in OnDeviceAdded.GetInvocationList())
                                {
                                    m_handler(
                                        this,
                                        new OnDeviceAddedEventArgs(
                                            "A new device was added to the local machine.",
                                            GetConnectedDevice(m_device)
                                            )
                                        );
                                }
                            }
                        }
                    }
                    else
                    {
                        // A device was removed.
                        // Find out which one it was.
                        foreach (string m_device in m_oldConnectedDevices)
                        {
                            if (!m_newConnectedDevices.Contains(m_device))
                            {
                                // We've found the device that was removed.
                                // We know this because the new list doesn't contain the device's serial.
                                // Now fire OnDeviceRemoved event(s).
                                foreach (DeviceRemovedEventHandler m_handler in OnDeviceRemoved.GetInvocationList())
                                {
                                    m_handler(
                                        this,
                                        new OnDeviceRemovedEventArgs(
                                            "A device was removed from the local machine.",
                                            m_device
                                            )
                                        );
                                }
                            }
                        }
                    }
                }
                else
                {
                    return;    // Nothing to do here, so don't bother.
                }
            };
            if (m_monitorUSB)
            {
                m_eventWatcher.Start();
            }
        }
Exemple #10
0
        private void Update()
        {
            if (SDL_Installed && !SDL_Initialized)
            {
                InitSDL();
            }
            if (!SDL_Initialized)
            {
                return;
            }

            SDL.SDL_Event e;

            while (SDL.SDL_PollEvent(out e) > 0)
            {
                switch (e.type)
                {
                case SDL.SDL_EventType.SDL_KEYDOWN:
                    OnKey?.Invoke(e, true);
                    break;

                case SDL.SDL_EventType.SDL_KEYUP:
                    OnKey?.Invoke(e, false);
                    break;

                case SDL.SDL_EventType.SDL_CONTROLLERAXISMOTION:
                    OnAxisMotion?.Invoke(e);
                    break;

                case SDL.SDL_EventType.SDL_CONTROLLERBUTTONDOWN:
                    OnButton?.Invoke(e, true);
                    break;

                case SDL.SDL_EventType.SDL_CONTROLLERBUTTONUP:
                    OnButton?.Invoke(e, false);
                    break;

                case SDL.SDL_EventType.SDL_CONTROLLERDEVICEADDED:
                    Controller.AddDevice(e.cdevice.which);
                    OnDeviceAdded?.Invoke(e);
                    break;

                case SDL.SDL_EventType.SDL_CONTROLLERDEVICEREMOVED:
                    Controller.RemoveDisconnected();
                    OnDeviceRemoved?.Invoke(e);
                    break;

                case SDL.SDL_EventType.SDL_CONTROLLERDEVICEREMAPPED:
                    OnDeviceRemapped?.Invoke(e);
                    break;

                case SDL.SDL_EventType.SDL_JOYAXISMOTION:
                    OnAxisMotion?.Invoke(e);
                    break;

                case SDL.SDL_EventType.SDL_JOYBALLMOTION:
                    OnBallMotion?.Invoke(e);
                    break;

                case SDL.SDL_EventType.SDL_JOYHATMOTION:
                    OnHatMotion?.Invoke(e);
                    break;

                case SDL.SDL_EventType.SDL_JOYBUTTONDOWN:
                    OnButton?.Invoke(e, true);
                    break;

                case SDL.SDL_EventType.SDL_JOYBUTTONUP:
                    OnButton?.Invoke(e, false);
                    break;

                case SDL.SDL_EventType.SDL_JOYDEVICEADDED:
                    Controller.AddDevice(e.jdevice.which);
                    OnDeviceAdded?.Invoke(e);
                    break;

                case SDL.SDL_EventType.SDL_JOYDEVICEREMOVED:
                    Controller.RemoveDisconnected();
                    OnDeviceRemoved?.Invoke(e);
                    break;

                default:
                    break;
                }
            }
        }