Beispiel #1
0
        public bool SendDevice(IVirtualDevice device)
        {
            if (device == null)
            {
                throw new ArgumentNullException("device");
            }
            if (!Created)
            {
                return(false);
            }
            IntPtr handle;

            if (!deviceHandles.TryGetValue(device, out handle))
            {
                return(false);
            }
            switch (device.DeviceType)
            {
            case VirtualDeviceType.Keyboard:
                return(SendKeyboard(device, handle));

            case VirtualDeviceType.TrackingInterface:
                return(SendFreetrackInterface(device, handle));

            default:
                throw new NotImplementedException();
            }
        }
Beispiel #2
0
        protected virtual void Dispose(bool disposing)
        {
            if (!mDisposed)
            {
                if (disposing)
                {
                    // dispose of managed resources
                    if (mDevice != null)
                    {
                        mDevice = null;
                    }

                    mDeviceSet = null;

                    // the Program class will dispose of the streams

                    //if (mTopOfPipeline != null)
                    //{
                    //    mTopOfPipeline.Dispose();
                    //    mTopOfPipeline = null;
                    //}
                }

                // There are no unmanaged resources to release, but
                // if we add them, they need to be released here.
            }
            mDisposed = true;

            // If it is available, make the call to the
            // base class's Dispose(Boolean) method
            //base.Dispose(disposing);
        }
Beispiel #3
0
 public void Initialize(bool isBackup, Stream topOfPipeline, IVirtualDevice device, IVirtualDeviceSet deviceSet)
 {
     mIsBackup      = isBackup;
     mTopOfPipeline = topOfPipeline;
     mDevice        = device;
     mDeviceSet     = deviceSet;
 }
Beispiel #4
0
        public bool DisconnectDevice(IVirtualDevice device)
        {
            if (device == null)
            {
                throw new ArgumentNullException("device");
            }
            if (!Created)
            {
                return(false);
            }
            IntPtr handle;

            if (!deviceHandles.TryGetValue(device, out handle))
            {
                return(true);
            }
            switch (device.DeviceType)
            {
            case VirtualDeviceType.Keyboard:
                Ivj.DisconnectKeyboard(contextHandle, handle);
                break;

            case VirtualDeviceType.TrackingInterface:
                Ivj.DisconnectFreetrackInterface(contextHandle, handle);
                break;

            default:
                throw new NotImplementedException();
            }
            deviceHandles.Remove(device);
            return(true);
        }
Beispiel #5
0
        /// <summary>
        /// Disconnects the device with the given ID from this instance.
        /// </summary>
        /// <returns><c>true</c>, if the operation wass successful, <c>false</c> otherwise.</returns>
        /// <param name="deviceId">Device identifier.</param>
        public bool DisconnectDevice(int deviceId)
        {
            IVirtualDevice device = null;

            lock (deviceSync) {
                if (deviceId < 0 || deviceId >= devices.Count)
                {
                    return(false);
                }
                device = devices[deviceId];
            }
            lock (contextSync) {
                if (!context.DisconnectDevice(device))
                {
                    return(false);
                }
            }
            lock (deviceSync) {
                if (deviceId == devices.Count - 1)
                {
                    devices.RemoveAt(deviceId);
                }
                else
                {
                    devices[deviceId] = null;
                }
            }
            if (device.DeviceType == VirtualDeviceType.TrackingInterface)
            {
                Interlocked.Decrement(ref ftRequestNo);
            }
            return(true);
        }
Beispiel #6
0
        /// <summary>
        /// Reconnects the device with the given ID.
        /// This method can be used to disconnect and connect a device without changing its ID.
        /// </summary>
        /// <returns><c>true</c>, if the operation was successful, <c>false</c> otherwise.</returns>
        /// <param name="deviceId">Device identifier.</param>
        /// <param name="device">New device specification.</param>
        public bool ReconnectDevice(int deviceId, IVirtualDevice device)
        {
            Process process = new Process();

            if (deviceId < 0)
            {
                throw new ArgumentOutOfRangeException("deviceId");
            }
            if (device == null)
            {
                throw new ArgumentOutOfRangeException("device");
            }
            // find the old device
            IVirtualDevice oldDevice;

            lock (deviceSync) {
                if (deviceId < 0 || deviceId >= devices.Count)
                {
                    return(false);
                }
                if (devices[deviceId] == null)
                {
                    return(false);
                }
                oldDevice = devices[deviceId];
            }
            // internal reconnect
            lock (contextSync) {
                if (!context.DisconnectDevice(oldDevice))
                {
                    return(false);
                }
                if (oldDevice.DeviceType == VirtualDeviceType.TrackingInterface)
                {
                    Interlocked.Decrement(ref ftRequestNo);
                }
                if (!context.ConnectDevice(device))
                {
                    return(false);
                }
                if (device.DeviceType == VirtualDeviceType.TrackingInterface)
                {
                    Interlocked.Increment(ref ftRequestNo);
                }
            }
            // exchange the devices
            // since the old device is still officially connected the device id has not been reassigned
            lock (deviceSync) {
                devices[deviceId] = device;
            }
            return(true);
        }
        private void Port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            while (port.BytesToRead >= 4 && port.BytesToRead % 4 == 0)
            {
                byte[] packet = new byte[4];
                int    read   = port.Read(packet, 0, 4);
                InterfaceDebugger.DataReceived(packet);
                switch (packet[0])
                {
                case 0x00:
                {
                    break;
                }

                case 0x01:
                {
                    IVirtualDevice device = GetDevice(packet);
                    bool           found  = false;
                    for (int i = 0; i < Subdevices.Count; i++)
                    {
                        if (Subdevices[i].Id == packet[2])         //if the device already exists, update it
                        {
                            Subdevices[i] = device;
                            found         = true;
                        }
                    }
                    //if the device doesn't already exist, add it
                    if (!found)
                    {
                        devices.Add(device);
                    }

                    //sort the list on the device ID
                    devices = devices.OrderBy(o => o.Id).ToList();
                    DeviceAttached?.Invoke(this, new VirtualDeviceEventArgs(device));
                    break;
                }

                case 0x02:
                {
                    //device sends a report
                    if (packet[1] > devices.Count - 1)
                    {
                        break;
                    }
                    devices[packet[1]].ReceivePacket(packet);
                    break;
                }
                }
            }
        }
Beispiel #8
0
        private bool FindDevice(IVirtualDevice device, out TreeIter iter)
        {
            for (int i = 0; i < IterNChildren (); i++) {
                IterNthChild (out iter, i);

                IVirtualDevice device2 = (IVirtualDevice) GetValue (iter, 0);
                if (device == device2) {
                    return true;
                }
            }

            iter = TreeIter.Zero;
            return false;
        }
Beispiel #9
0
        private bool SendKeyboard(IVirtualDevice device, IntPtr handle)
        {
            object[] modifiedKeys = device.GetModifiedKeys(VirtualDeviceCapability.Key);
            foreach (object key in modifiedKeys)
            {
                KeyState state    = device.GetKeyState(VirtualDeviceCapability.Key, key);
                int      keyIndex = Ivj.GetKeyboardKeyIndex(key);
                if (keyIndex > -1)
                {
                    Ivj.SetKeyboardKey(handle, keyIndex, state.SmoothPressed);
                }
            }
            bool result = Ivj.SendKeyboard(handle);

            device.Send();
            return(result);
        }
Beispiel #10
0
        private bool SendFreetrackInterface(IVirtualDevice device, IntPtr handle)
        {
            object[] modifiedKeys = device.GetModifiedKeys(VirtualDeviceCapability.Axis);
            foreach (object key in modifiedKeys)
            {
                KeyState state     = device.GetKeyState(VirtualDeviceCapability.Axis, key);
                int      axisIndex = Ivj.GetFreetrackAxisIndex(key);
                if (axisIndex > -1)
                {
                    Ivj.SetFreetrackAxis(handle, axisIndex, state.SmoothAxis, state.RawAxis);
                }
            }
            bool result = Ivj.SendFreetrackInterface(handle);

            device.Send();
            return(result);
        }
Beispiel #11
0
        public bool ConnectDevice(IVirtualDevice device)
        {
            if (device == null)
            {
                throw new ArgumentNullException("device");
            }
            if (!Created)
            {
                return(false);
            }
            if (deviceHandles.ContainsKey(device))
            {
                return(true);
            }
            IntPtr handle = IntPtr.Zero;
            IntPtr constructionInfo;

            switch (device.DeviceType)
            {
            case VirtualDeviceType.Keyboard:
                constructionInfo = Ivj.AllocKeyboardConstructionInfo();
                handle           = Ivj.ConnectKeyboard(contextHandle, constructionInfo);
                Ivj.FreeKeyboardConstructionInfo(constructionInfo);
                break;

            case VirtualDeviceType.TrackingInterface:
                constructionInfo = Ivj.AllocFreetrackConstructionInfo();
                handle           = Ivj.ConnectFreetrackInterface(contextHandle, constructionInfo);
                Ivj.FreeFreetrackConstructionInfo(constructionInfo);
                break;

            default:
                throw new NotImplementedException();
            }
            if (handle == IntPtr.Zero)
            {
                return(false);
            }
            deviceHandles.Add(device, handle);
            return(true);
        }
Beispiel #12
0
        /// <summary>
        /// Connects the given device.
        /// </summary>
        /// <returns>Id to reference the device.</returns>
        /// <param name="device">Device specification.</param>
        public int ConnectDevice(IVirtualDevice device)
        {
            if (device == null)
            {
                throw new ArgumentNullException("device");
            }
            lock (contextSync) {
                if (!context.ConnectDevice(device))
                {
                    return(-1);
                }
            }
            int result = -1;

            lock (deviceSync) {
                // find empty device index
                for (int i = 0; i < devices.Count; i++)
                {
                    if (devices[i] == null)
                    {
                        result = i;
                        break;
                    }
                }
                // add at empty index or append to end
                if (result < 0)
                {
                    result = devices.Count;
                    devices.Add(device);
                }
                else
                {
                    devices[result] = device;
                }
            }
            if (device.DeviceType == VirtualDeviceType.TrackingInterface)
            {
                Interlocked.Increment(ref ftRequestNo);
            }
            return(result);
        }
Beispiel #13
0
        /// <summary>
        /// Sends device state updates to the OS or HID backend.
        /// </summary>
        private void SendState()
        {
            var toSend = new Queue <IVirtualDevice>();

            lock (deviceSync) {
                for (int i = 0; i < devices.Count; i++)
                {
                    if (devices[i] != null && devices[i].SendRequired)
                    {
                        toSend.Enqueue(devices[i]);
                    }
                }
            }
            lock (contextSync) {
                while (toSend.Count > 0)
                {
                    IVirtualDevice device = toSend.Dequeue();
                    context.SendDevice(device);
                }
            }
        }
Beispiel #14
0
        public void PositionChanged(object sender, PositionDetectedEventArgs args)
        {
            // TODO: protect with locks
            var configCopy = config;

            VirtualDeviceManager.Instance.Invoke(delegate {
                VirtualDeviceManager.Instance.BeginUpdate();
                var toRemove = new Queue <int>();
                foreach (var pair in configCopy)
                {
                    List <Binding> bindings = pair.Value;
                    int deviceIndex         = pair.Key;
                    int deviceId            = EquipmentMaster.Instance.VirtualDevice.GetEquipment(deviceIndex, -1);
                    if (deviceId < 0)
                    {
                        LogManager.Instance.Log(LogEntry.CreateWarning(this, "Missing device " + deviceIndex));
                        continue;
                    }
                    IVirtualDevice device = VirtualDeviceManager.Instance.GetDevice(deviceId);
                    if (device == null)
                    {
                        LogManager.Instance.Log(LogEntry.CreateWarning(this, "Missing device with id " + deviceId));
                        continue;
                    }
                    device.BeginUpdate();
                    foreach (Binding bind in pair.Value)
                    {
                        KeyState state = args.Position.GetAxis(bind.Axis);
                        // TODO: apply mapping
                        if (!device.SetKeyState(bind.Capability, bind.PosKey, state))
                        {
                            string message = string.Format("Cannot set device {0} key state {1}|{2}", deviceIndex, bind.PosKey, bind.NegKey);
                            LogManager.Instance.Log(LogEntry.CreateWarning(this, message));
                        }
                    }
                    device.EndUpdate();
                }
                VirtualDeviceManager.Instance.EndUpdate();
            });
        }
Beispiel #15
0
 public VirtualDeviceEventArgs(IVirtualDevice device)
 {
     Device = device;
 }
Beispiel #16
0
        private static void ReadWriteData(IVirtualDevice device, ICommandBuffer buff, Stream stream, bool isBackup)
        {
            while (device.GetCommand(null, buff))
            {
                if (!buff.TimedOut)
                {
                    CompletionCode completionCode   = CompletionCode.DiskFull;
                    uint           bytesTransferred = 0;

                    try
                    {
                        switch (buff.CommandType)
                        {
                        case DeviceCommandType.Write:

                            if (!isBackup)
                            {
                                throw new InvalidOperationException("Cannot write in 'restore' mode");
                            }

                            bytesTransferred = (uint)buff.WriteToStream(stream);

                            completionCode = CompletionCode.Success;

                            break;

                        case DeviceCommandType.Read:

                            if (isBackup)
                            {
                                throw new InvalidOperationException("Cannot read in 'backup' mode");
                            }

                            bytesTransferred = (uint)buff.ReadFromStream(stream);

                            if (bytesTransferred > 0)
                            {
                                completionCode = CompletionCode.Success;
                            }
                            else
                            {
                                completionCode = CompletionCode.HandleEof;
                            }

                            break;

                        case DeviceCommandType.ClearError:
                            completionCode = CompletionCode.Success;
                            break;

                        case DeviceCommandType.Flush:
                            stream.Flush();
                            completionCode = CompletionCode.Success;
                            break;

                        default:
                            throw new ArgumentException(string.Format("Unknown command: {0}", buff.CommandType));
                        }
                    }
                    finally
                    {
                        device.CompleteCommand(buff, completionCode, bytesTransferred, (ulong)0);
                    }
                }
            }
        }
Beispiel #17
0
 private void AddDevice(IVirtualDevice device)
 {
     AppendValues (device);
 }
Beispiel #18
0
 private void RemoveDevice(IVirtualDevice device)
 {
     TreeIter iter;
     if (FindDevice (device, out iter)) {
         Remove (ref iter);
     }
 }