Beispiel #1
0
        /// <summary>
        /// Load mapping from XML file
        /// </summary>
        /// <param name="aXmlNode"></param>
        /// <param name="aTryParseUsage"></param>
        public void Load(XmlNode aXmlNode, TryParseUsage aTryParseUsage)
        {
            _buttons = new List <Button>();
            var listButtons = aXmlNode.ChildNodes;

            foreach (XmlNode nodeButton in listButtons)
            {
                //Only process button elements
                if (!(nodeButton.NodeType == XmlNodeType.Element && nodeButton.Name == "button"))
                {
                    continue;
                }

                //Get element name and code
                var code = nodeButton.Attributes["code"].Value;

                //We do not require a name attribute anymore as the code itself can in most cases be used as a name too
                var name          = "";
                var nameAttribute = nodeButton.Attributes["name"];
                if (nameAttribute != null)
                {
                    //If we have a name attribute do use it.
                    name = nameAttribute.Value;
                }
                else
                {
                    //Otherwise use the code itself as a name
                    name = code;
                }

                //Check if this command is supported while MP is in background
                bool background = AttributeValueToBoolean(nodeButton.Attributes["background"]);

                //Check if this command supports repeats
                bool repeat = AttributeValueToBoolean(nodeButton.Attributes["repeat"]);

                // Get the required keyboard modifiers
                bool shift = AttributeValueToBoolean(nodeButton.Attributes["shift"]);
                bool ctrl  = AttributeValueToBoolean(nodeButton.Attributes["ctrl"]);
                bool alt   = AttributeValueToBoolean(nodeButton.Attributes["alt"]);
                bool win   = AttributeValueToBoolean(nodeButton.Attributes["win"]);

                //Now try and parse our usage code using the provided method
                ushort usage = 0;
                if (!aTryParseUsage(code, out usage))
                {
                    Log.Warn("HID: XML loader can't parse usage {0} for button {1}", code, name);
                    continue;
                }

                //Feed back our usage integer as a string into our legacy action parser
                code = usage.ToString();

                //Perform legacy parsing of our actions
                var actions     = new List <ConditionalAction>();
                var listActions = nodeButton.SelectNodes("action");
                foreach (XmlNode nodeAction in listActions)
                {
                    var cmdKeyChar  = 0;
                    var cmdKeyCode  = 0;
                    var condition   = nodeAction.Attributes["condition"].Value.ToUpperInvariant();
                    var conProperty = nodeAction.Attributes["conproperty"].Value.ToUpperInvariant();
                    var command     = nodeAction.Attributes["command"].Value.ToUpperInvariant();
                    var cmdProperty = nodeAction.Attributes["cmdproperty"].Value.ToUpperInvariant();
                    if ((command == "ACTION") && (cmdProperty == "93"))
                    {
                        cmdKeyChar = Convert.ToInt32(nodeAction.Attributes["cmdkeychar"].Value);
                        cmdKeyCode = Convert.ToInt32(nodeAction.Attributes["cmdkeycode"].Value);
                    }
                    var sound          = string.Empty;
                    var soundAttribute = nodeAction.Attributes["sound"];
                    if (soundAttribute != null)
                    {
                        sound = soundAttribute.Value;
                    }
                    var focus          = false;
                    var focusAttribute = nodeAction.Attributes["focus"];
                    if (focusAttribute != null)
                    {
                        focus = Convert.ToBoolean(focusAttribute.Value);
                    }
                    var layer        = Convert.ToInt32(nodeAction.Attributes["layer"].Value);
                    var conditionMap = new ConditionalAction(layer, condition, conProperty, command,
                                                             cmdProperty, cmdKeyChar, cmdKeyCode, sound, focus);
                    actions.Add(conditionMap);
                }
                var button = new Button(code, name, background, repeat, shift, ctrl, alt, win, actions);
                _buttons.Add(button);
            }
            IsLoaded = true;
        }
    /// <summary>
    /// Load mapping from XML file
    /// </summary>
    /// <param name="aXmlNode"></param>
    /// <param name="aTryParseUsage"></param>
    public void Load(XmlNode aXmlNode, TryParseUsage aTryParseUsage)
    {
      _buttons = new List<Button>();
      var listButtons = aXmlNode.ChildNodes;
      foreach (XmlNode nodeButton in listButtons)
      {
        //Only process button elements
        if (!(nodeButton.NodeType == XmlNodeType.Element && nodeButton.Name == "button"))
        {
          continue;
        }

        //Get element name and code
        var code = nodeButton.Attributes["code"].Value;

        //We do not require a name attribute anymore as the code itself can in most cases used as a name too
        var name = "";
        var nameAttribute = nodeButton.Attributes["name"];
        if (nameAttribute != null)
        {
          //If we have a name attribute do use it.
          name = nameAttribute.Value;
        }
        else
        {
          //Otherwise use the code itself as a name
          name = code;
        }

        //Check if this command is supported while MP is in background
        var background = false;
        if (nodeButton.Attributes["background"] != null &&
            (nodeButton.Attributes["background"].Value == "true" ||
             nodeButton.Attributes["background"].Value == "1"))
        {
          background = true;
        }

        //Check if this command supports repeats
        bool repeat = false;
        if (nodeButton.Attributes["repeat"] != null &&
            (nodeButton.Attributes["repeat"].Value == "true" ||
             nodeButton.Attributes["repeat"].Value == "1"))
        {
            repeat = true;
        }

        //Now try and parse our usage code using the provided method
        ushort usage = 0;
        if (!aTryParseUsage(code, out usage))
        {
          Log.Warn("HID XML configuration parser: can't parse usage {0} for button {1}", code, name);
          continue;
        }

        //Feed back our usage integer as a string into our legacy action parser
        code = usage.ToString();

        //Perform legacy parsing of our actions
        var actions = new List<ConditionalAction>();
        var listActions = nodeButton.SelectNodes("action");
        foreach (XmlNode nodeAction in listActions)
        {
          var cmdKeyChar = 0;
          var cmdKeyCode = 0;
          var condition = nodeAction.Attributes["condition"].Value.ToUpperInvariant();
          var conProperty = nodeAction.Attributes["conproperty"].Value.ToUpperInvariant();
          var command = nodeAction.Attributes["command"].Value.ToUpperInvariant();
          var cmdProperty = nodeAction.Attributes["cmdproperty"].Value.ToUpperInvariant();
          if ((command == "ACTION") && (cmdProperty == "93"))
          {
            cmdKeyChar = Convert.ToInt32(nodeAction.Attributes["cmdkeychar"].Value);
            cmdKeyCode = Convert.ToInt32(nodeAction.Attributes["cmdkeycode"].Value);
          }
          var sound = string.Empty;
          var soundAttribute = nodeAction.Attributes["sound"];
          if (soundAttribute != null)
          {
            sound = soundAttribute.Value;
          }
          var focus = false;
          var focusAttribute = nodeAction.Attributes["focus"];
          if (focusAttribute != null)
          {
            focus = Convert.ToBoolean(focusAttribute.Value);
          }
          var layer = Convert.ToInt32(nodeAction.Attributes["layer"].Value);
          var conditionMap = new ConditionalAction(layer, condition, conProperty, command,
            cmdProperty, cmdKeyChar, cmdKeyCode, sound, focus);
          actions.Add(conditionMap);
        }
        var button = new Button(code, name, background, repeat, actions);
        _buttons.Add(button);
      }
      IsLoaded = true;
    }