<commit_before>#!/usr/bin/ruby
# -*- coding: utf-8 -*-

require 'inc.filter.rb'

class RemapClass
  @@index = 0
  @@entries = {
    :initialize                   => [],
    :handlevirtualkey             => [],
    :remap_setkeyboardtype        => [],
    :remap_key                    => [],
    :remap_consumer               => [],
    :remap_pointing               => [],
    :remap_simultaneouskeypresses => [],
    :get_statusmessage            => [],
    :enabled                      => [],
  }

  def RemapClass.get_entries
    return @@entries
  end

  def initialize(name)
    @name = name
    @filter = Filter.new()

    @code = {
      # functions
      :initialize                   => '',
      :handlevirtualkey             => '',
      :remap_setkeyboardtype        => '',
      :remap_key                    => '',
      :remap_consumer               => '',
      :remap_pointing               => '',
      :remap_simultaneouskeypresses => '',
      :get_statusmessage            => '',
      # values
      :keycode                      => '',
      :variable                     => [],
    }
  end
  attr_accessor :name, :filter, :code

  def +(other)
    other.code.each do |k,v|
      @code[k] += v
    end
    self
  end

  def append_to_code_initialize(params)
    params.split(/,/).each do |p|
      @code[:initialize] += "value#{@@index}_.add(#{p.strip});\n"
    end
  end
  protected :append_to_code_initialize

  # return true if 'line' contains autogen/filter definition.
  def parse(line)
    return true if @filter.parse(line)

    if /<autogen>--(.+?)-- (.+)<\/autogen>/ =~ line then
      operation = $1
      params = $2
      @@index += 1

      case operation
      when 'SetKeyboardType'
        @code[:remap_setkeyboardtype] += "keyboardType = #{params}.get();\n";

      when 'ShowStatusMessage'
        @code[:get_statusmessage] += "return #{params};\n"

      when 'SimultaneousKeyPresses'
        @code[:variable] << { :index => @@index, :class => "EventInputQueue::Remap" }
        @code[:keycode] += "VK_SIMULTANEOUSKEYPRESSES_#{name}_#{@@index} --AUTO--\n"
        @code[:initialize] += "value#{@@index}_.initialize(KeyCode::VK_SIMULTANEOUSKEYPRESSES_#{name}_#{@@index}, #{params});\n"
        @code[:remap_simultaneouskeypresses] += "value#{@@index}_.remap();\n"
        @code[:handlevirtualkey] += "if (value#{@@index}_.handleVirtualKey(params)) return true;\n"

      when 'KeyToKey'
        append_to_code_initialize(params)
        @code[:variable] << { :index => @@index, :class => "RemapUtil::KeyToKey" }
        @code[:remap_key] += "if (value#{@@index}_.remap(remapParams)) break;\n"

      when 'DoublePressModifier'
        @code[:variable] << { :index => @@index, :class => "DoublePressModifier" }
        @code[:remap_key] += "if (value#{@@index}_.remap(remapParams, #{params})) break;\n"

      when 'IgnoreMultipleSameKeyPress'
        append_to_code_initialize(params)
        @code[:variable] << { :index => @@index, :class => "IgnoreMultipleSameKeyPress" }
        @code[:remap_key] += "if (value#{@@index}_.remap(remapParams)) break;\n"

      when 'KeyToConsumer'
        append_to_code_initialize(params)
        @code[:variable] << { :index => @@index, :class => "RemapUtil::KeyToConsumer" }
        @code[:remap_key] += "if (value#{@@index}_.remap(remapParams)) break;\n"

      when 'KeyToPointingButton'
        @code[:variable] << { :index => @@index, :class => "RemapUtil::KeyToPointingButton" }
        @code[:remap_key] += "if (value#{@@index}_.remap(remapParams, #{params})) break;\n"

      when 'KeyOverlaidModifier'
        @code[:variable] << { :index => @@index, :class => "KeyOverlaidModifier" }
        @code[:remap_key] += "if (value#{@@index}_.remap(remapParams, #{params})) break;\n"

      when 'KeyOverlaidModifierWithRepeat'
        @code[:variable] << { :index => @@index, :class => "KeyOverlaidModifier" }
        @code[:remap_key] += "if (value#{@@index}_.remapWithRepeat(remapParams, #{params})) break;\n"

      when 'ModifierHoldingKeyToKey'
        @code[:variable] << { :index => @@index, :class => "ModifierHoldingKeyToKey" }
        @code[:remap_key] += "if (value#{@@index}_.remap(remapParams, #{params})) break;\n"

      when 'HoldingKeyToKey'
        @code[:variable] << { :index => @@index, :class => "HoldingKeyToKey" }
        @code[:remap_key] += "if (value#{@@index}_.remap(remapParams, #{params})) break;\n"

      when 'ConsumerToKey'
        append_to_code_initialize(params)
        @code[:variable] << { :index => @@index, :class => "RemapUtil::ConsumerToKey" }
        @code[:remap_consumer] += "if (value#{@@index}_.remap(remapParams)) break;\n"

      when 'ConsumerToConsumer'
        append_to_code_initialize(params)
        @code[:variable] << { :index => @@index, :class => "RemapUtil::ConsumerToConsumer" }
        @code[:remap_consumer] += "if (value#{@@index}_.remap(remapParams)) break;\n"

      when 'PointingRelativeToScroll'
        @code[:variable] << { :index => @@index, :class => "RemapUtil::PointingRelativeToScroll" }
        @code[:remap_pointing] += "if (value#{@@index}_.remap(remapParams, #{params})) break;\n"

      when 'PointingButtonToPointingButton'
        @code[:variable] << { :index => @@index, :class => "RemapUtil::PointingButtonToPointingButton" }
        @code[:remap_pointing] += "if (value#{@@index}_.remap(remapParams, #{params})) break;\n"

      when 'PointingButtonToKey'
        @code[:variable] << { :index => @@index, :class => "RemapUtil::PointingButtonToKey" }
        @code[:remap_pointing] += "if (value#{@@index}_.remap(remapParams, #{params})) break;\n"

      else
        print "%%% ERROR #{type} %%%\n#{l}\n"
        exit 1
      end

      return true
    end

    return false
  end

  def fixup
    [:remap_key, :remap_consumer,:remap_pointing, :remap_simultaneouskeypresses].each do |k|
      unless @code[k].empty? then
        c  = "do {\n"
        c += @filter.to_code
        c += @code[k]
        c += "} while (false);\n"
        @code[k] = c
      end
    end
  end

  def empty?
    @code.each do |k, v|
      return false unless v.empty?
    end
    return true
  end

  def to_code
    return '' if empty?

    classname = "RemapClass_#{@name}"

    code  = "class #{classname} {\n"
    code += "public:\n"

    # ----------------------------------------------------------------------
    unless @code[:initialize].empty? then
      code += "static void initialize(void) {\n"
      code += @code[:initialize]
      code += "}\n"

      @@entries[:initialize] << "#{classname}::initialize"
    end

    # ----------------------------------------------------------------------
    unless @code[:remap_setkeyboardtype].empty? then
      code += "static void remap_setkeyboardtype(KeyboardType& keyboardType) {\n"
      code += @code[:remap_setkeyboardtype]
      code += "}\n"

      @@entries[:remap_setkeyboardtype] << "#{classname}::remap_setkeyboardtype"
    else
      @@entries[:remap_setkeyboardtype] << "NULL"
    end

    # ----------------------------------------------------------------------
    unless @code[:remap_key].empty? then
      code += "static void remap_key(RemapParams& remapParams) {\n"
      code += @code[:remap_key]<commit_msg>update inc.remapclass.rb around initialize generator<commit_after>#!/usr/bin/ruby
# -*- coding: utf-8 -*-

require 'inc.filter.rb'

class RemapClass
  @@index = 0
  @@entries = {
    :initialize                   => [],
    :handlevirtualkey             => [],
    :remap_setkeyboardtype        => [],
    :remap_key                    => [],
    :remap_consumer               => [],
    :remap_pointing               => [],
    :remap_simultaneouskeypresses => [],
    :get_statusmessage            => [],
    :enabled                      => [],
  }

  def RemapClass.get_entries
    return @@entries
  end

  def initialize(name)
    @name = name
    @filter = Filter.new()

    @code = {
      # functions
      :initialize                   => '',
      :handlevirtualkey             => '',
      :remap_setkeyboardtype        => '',
      :remap_key                    => '',
      :remap_consumer               => '',
      :remap_pointing               => '',
      :remap_simultaneouskeypresses => '',
      :get_statusmessage            => '',
      # values
      :keycode                      => '',
      :variable                     => [],
    }
  end
  attr_accessor :name, :filter, :code

  def +(other)
    other.code.each do |k,v|
      @code[k] += v
    end
    self
  end

  def append_to_code_initialize(params)
    # We split the initialize function per value.
    # If a large number of values exist, the kernel stack is wasted when the monolithic initialize function is called.
    # So, we split it.
    @code[:initialize] += "static void initialize_value#{@@index}(void) {\n"
    params.split(/,/).each do |p|
      @code[:initialize] += "value#{@@index}_.add(#{p.strip});\n"
    end
    @code[:initialize] += "}\n"

    @@entries[:initialize] << "RemapClass_#{@name}::initialize_value#{@@index}"
  end
  protected :append_to_code_initialize

  # return true if 'line' contains autogen/filter definition.
  def parse(line)
    return true if @filter.parse(line)

    if /<autogen>--(.+?)-- (.+)<\/autogen>/ =~ line then
      operation = $1
      params = $2
      @@index += 1

      case operation
      when 'SetKeyboardType'
        @code[:remap_setkeyboardtype] += "keyboardType = #{params}.get();\n";

      when 'ShowStatusMessage'
        @code[:get_statusmessage] += "return #{params};\n"

      when 'SimultaneousKeyPresses'
        @code[:variable] << { :index => @@index, :class => "EventInputQueue::Remap" }
        @code[:keycode] += "VK_SIMULTANEOUSKEYPRESSES_#{name}_#{@@index} --AUTO--\n"
        @code[:remap_simultaneouskeypresses] += "value#{@@index}_.remap();\n"
        @code[:handlevirtualkey] += "if (value#{@@index}_.handleVirtualKey(params)) return true;\n"

        @code[:initialize] += "static void initialize_value#{@@index}(void) {\n"
        @code[:initialize] += "value#{@@index}_.initialize(KeyCode::VK_SIMULTANEOUSKEYPRESSES_#{name}_#{@@index}, #{params});\n"
        @code[:initialize] += "}\n"
        @@entries[:initialize] << "RemapClass_#{@name}::initialize_value#{@@index}"

      when 'KeyToKey'
        append_to_code_initialize(params)
        @code[:variable] << { :index => @@index, :class => "RemapUtil::KeyToKey" }
        @code[:remap_key] += "if (value#{@@index}_.remap(remapParams)) break;\n"

      when 'DoublePressModifier'
        @code[:variable] << { :index => @@index, :class => "DoublePressModifier" }
        @code[:remap_key] += "if (value#{@@index}_.remap(remapParams, #{params})) break;\n"

      when 'IgnoreMultipleSameKeyPress'
        append_to_code_initialize(params)
        @code[:variable] << { :index => @@index, :class => "IgnoreMultipleSameKeyPress" }
        @code[:remap_key] += "if (value#{@@index}_.remap(remapParams)) break;\n"

      when 'KeyToConsumer'
        append_to_code_initialize(params)
        @code[:variable] << { :index => @@index, :class => "RemapUtil::KeyToConsumer" }
        @code[:remap_key] += "if (value#{@@index}_.remap(remapParams)) break;\n"

      when 'KeyToPointingButton'
        @code[:variable] << { :index => @@index, :class => "RemapUtil::KeyToPointingButton" }
        @code[:remap_key] += "if (value#{@@index}_.remap(remapParams, #{params})) break;\n"

      when 'KeyOverlaidModifier'
        @code[:variable] << { :index => @@index, :class => "KeyOverlaidModifier" }
        @code[:remap_key] += "if (value#{@@index}_.remap(remapParams, #{params})) break;\n"

      when 'KeyOverlaidModifierWithRepeat'
        @code[:variable] << { :index => @@index, :class => "KeyOverlaidModifier" }
        @code[:remap_key] += "if (value#{@@index}_.remapWithRepeat(remapParams, #{params})) break;\n"

      when 'ModifierHoldingKeyToKey'
        @code[:variable] << { :index => @@index, :class => "ModifierHoldingKeyToKey" }
        @code[:remap_key] += "if (value#{@@index}_.remap(remapParams, #{params})) break;\n"

      when 'HoldingKeyToKey'
        @code[:variable] << { :index => @@index, :class => "HoldingKeyToKey" }
        @code[:remap_key] += "if (value#{@@index}_.remap(remapParams, #{params})) break;\n"

      when 'ConsumerToKey'
        append_to_code_initialize(params)
        @code[:variable] << { :index => @@index, :class => "RemapUtil::ConsumerToKey" }
        @code[:remap_consumer] += "if (value#{@@index}_.remap(remapParams)) break;\n"

      when 'ConsumerToConsumer'
        append_to_code_initialize(params)
        @code[:variable] << { :index => @@index, :class => "RemapUtil::ConsumerToConsumer" }
        @code[:remap_consumer] += "if (value#{@@index}_.remap(remapParams)) break;\n"

      when 'PointingRelativeToScroll'
        @code[:variable] << { :index => @@index, :class => "RemapUtil::PointingRelativeToScroll" }
        @code[:remap_pointing] += "if (value#{@@index}_.remap(remapParams, #{params})) break;\n"

      when 'PointingButtonToPointingButton'
        @code[:variable] << { :index => @@index, :class => "RemapUtil::PointingButtonToPointingButton" }
        @code[:remap_pointing] += "if (value#{@@index}_.remap(remapParams, #{params})) break;\n"

      when 'PointingButtonToKey'
        @code[:variable] << { :index => @@index, :class => "RemapUtil::PointingButtonToKey" }
        @code[:remap_pointing] += "if (value#{@@index}_.remap(remapParams, #{params})) break;\n"

      else
        print "%%% ERROR #{type} %%%\n#{l}\n"
        exit 1
      end

      return true
    end

    return false
  end

  def fixup
    [:remap_key, :remap_consumer,:remap_pointing, :remap_simultaneouskeypresses].each do |k|
      unless @code[k].empty? then
        c  = "do {\n"
        c += @filter.to_code
        c += @code[k]
        c += "} while (false);\n"
        @code[k] = c
      end
    end
  end

  def empty?
    @code.each do |k, v|
      return false unless v.empty?
    end
    return true
  end

  def to_code
    return '' if empty?

    classname = "RemapClass_#{@name}"

    code  = "class #{classname} {\n"
    code += "public:\n"

    # ----------------------------------------------------------------------
    code += @code[:initialize]

    # ----------------------------------------------------------------------
    unless @code[:remap_setkeyboardtype].empty? then
      code += "static void remap_setkeyboardtype(KeyboardType& keyboardType) {\n"
      code += @code[:remap_setkeyboardtype]
      code += "}\n"

      @@entries[:remap_setkeyboardtype] << "#{classname}::remap_setkeyboardtype"
    else
      @@entries[:remap_setkeyboardtype] << "NULL"
    end

    # ----------------------------------------------------------------------
    unless @code[:remap_key].empty? then
      code += "static void remap_key(RemapParams& remapParams) {\n"
      code += @code[:remap_key]
{'fraction_non_alphanumeric': 0.19997317596566525, 'fraction_numerical': 0.0008047210300429185, 'mean_word_length': 2.5263655710569877, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 105, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4317804', 'n_tokens_mistral': 4859, 'n_tokens_neox': 4736, 'n_words': 846}