Class: Fusuma::Plugin::Remap::KeyboardRemapper

Inherits:
Object
  • Object
show all
Includes:
Revdev
Defined in:
lib/fusuma/plugin/remap/keyboard_remapper.rb

Defined Under Namespace

Classes: KeyboardSelector

Constant Summary collapse

VIRTUAL_KEYBOARD_NAME =
"fusuma_virtual_keyboard"
DEFAULT_EMERGENCY_KEYBIND =
"RIGHTCTRL+LEFTCTRL".freeze
DEVICE_CHECK_INTERVAL =

seconds - interval for checking new devices

3
KEYMAP =

Key conversion tables for better performance and readability

Revdev.constants.select { |c| c.start_with?("KEY_", "BTN_") }
.map { |c| [Revdev.const_get(c), c.to_s.delete_prefix("KEY_")] }
.to_h.freeze
CODEMAP =
Revdev.constants.select { |c| c.start_with?("KEY_", "BTN_") }
.map { |c| [c, Revdev.const_get(c)] }
.to_h.freeze

Instance Method Summary collapse

Constructor Details

#initialize(layer_manager:, fusuma_writer:, config: {}) ⇒ KeyboardRemapper

Returns a new instance of KeyboardRemapper.

Parameters:



32
33
34
35
36
37
38
# File 'lib/fusuma/plugin/remap/keyboard_remapper.rb', line 32

def initialize(layer_manager:, fusuma_writer:, config: {})
  @layer_manager = layer_manager # request to change layer
  @fusuma_writer = fusuma_writer # write event to original keyboard
  @config = config
  @device_matcher = DeviceMatcher.new
  @device_mappings = {}
end

Instance Method Details

#runObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/fusuma/plugin/remap/keyboard_remapper.rb', line 40

def run
  create_virtual_keyboard
  @source_keyboards = reload_keyboards

  # Manage modifier key states
  @modifier_state = ModifierState.new

  old_ie = nil
  layer = nil
  current_mapping = {}

  loop do
    ios = IO.select(
      [*@source_keyboards.map(&:file), @layer_manager.reader],
      nil,
      nil,
      DEVICE_CHECK_INTERVAL
    )

    # Timeout - check for new devices
    if ios.nil?
      check_and_add_new_devices
      next
    end

    readable_ios = ios.first

    # Prioritize layer changes over keyboard events to ensure
    # layer state is updated before processing key inputs
    io = if readable_ios.include?(@layer_manager.reader)
      @layer_manager.reader
    else
      readable_ios.first
    end

    if io == @layer_manager.reader
      layer = @layer_manager.receive_layer # update @current_layer
      if layer.nil?
        next
      end

      # Clear device mapping cache when layer changes
      @device_mappings = {}
      @layer_changed = true
      next
    end

    source_keyboard = @source_keyboards.find { |kbd| kbd.file == io }
    input_event = source_keyboard.read_input_event
    current_device_name = source_keyboard.device_name

    # Get device-specific mapping
    device_mapping = get_mapping_for_device(current_device_name, layer || {})

    # Wait until all virtual keys are released before applying new mapping
    if @layer_changed && virtual_keyboard_all_key_released?
      @layer_changed = false
    end

    # Use device-specific mapping (wait during layer change to prevent key stuck)
    current_mapping = @layer_changed ? current_mapping : device_mapping

    input_key = code_to_key(input_event.code)

    # Apply simple key-to-key remapping first (modmap-style)
    # e.g., CAPSLOCK -> LEFTCTRL, so modifier state tracks the remapped key
    effective_key = apply_simple_remap(current_mapping, input_key)

    if input_event.type == EV_KEY
      @emergency_stop.call(old_ie, input_event)

      old_ie = input_event

      @modifier_state.update(effective_key, input_event.value)

      if input_event.value != 2 # repeat
        data = {key: input_key, status: input_event.value, layer: layer}
        begin
          @fusuma_writer.write(data.to_msgpack)
        rescue IOError => e
          MultiLogger.error("Failed to write to fusuma_writer: #{e.message}")
          @destroy&.call(1)
          return
        end
      end
    end

    remapped, is_modifier_remap = find_remapping(current_mapping, effective_key)
    case remapped
    when String, Symbol
      # Continue to key output processing below
    when Array
      # Output sequence: e.g., [LEFTSHIFT+HOME, DELETE]
      if input_event.value == 1
        execute_modifier_remap(remapped, input_event)
      end
      next
    when Hash
      # Command execution (e.g., {:SENDKEY=>"LEFTCTRL+BTN_LEFT", :CLEARMODIFIERS=>true})
      # Skip input event processing and let Fusuma's Executor handle this
      next
    when nil
      if effective_key != input_key
        # Output simple-remapped key (e.g., CAPSLOCK -> LEFTCTRL)
        remapped_code = key_to_code(effective_key)
        if remapped_code
          remapped_event = InputEvent.new(nil, input_event.type, remapped_code, input_event.value)
          update_virtual_key_state(effective_key, remapped_event.value)
          write_event_with_log(remapped_event, context: "simple remap from #{input_key}")
        else
          write_event_with_log(input_event, context: "simple remap failed")
        end
      else
        write_event_with_log(input_event, context: "passthrough")
      end
      next
    else
      MultiLogger.warn("Invalid remapped value - type: #{remapped.class}, key: #{input_key}")
      next
    end

    # For modifier remaps, handle specially:
    # Release currently pressed modifiers → Send remapped key → Re-press modifiers
    if is_modifier_remap && input_event.value == 1
      execute_modifier_remap(remapped, input_event)
      next
    end

    # Handle key combination output (e.g., "LEFTALT+LEFT")
    # If remapped value contains "+", it's a key combination that needs special handling
    if remapped.to_s.include?("+")
      if input_event.value == 1 # only on key press
        send_key_combination(remapped, input_event.type)
      end
      next
    end

    remapped_code = key_to_code(remapped)
    if remapped_code.nil?
      MultiLogger.warn("Invalid remapped value - unknown key: #{remapped}, input: #{input_key}")
      write_event_with_log(input_event, context: "remap failed")
      next
    end

    remapped_event = InputEvent.new(nil, input_event.type, remapped_code, input_event.value)

    # Workaround: If a key was pressed before remapping started and is being released,
    # use the original key code to ensure proper key release
    if should_use_original_key?(remapped, remapped_event.value)
      remapped_event.code = input_event.code
    else
      # Only update virtual key state if we're using the remapped key
      update_virtual_key_state(remapped, remapped_event.value)
    end

    # remap to command will be nil
    # e.g) remap: { X: { command: echo 'foo' } }
    # this is because the command will be executed by fusuma process
    next if remapped_event.code.nil?

    write_event_with_log(remapped_event, context: "remapped from #{input_key}")
  rescue Errno::ENODEV => e # device is removed
    MultiLogger.error "Device is removed: #{e.message}"
    @device_mappings = {} # Clear cache for new device configuration
    @source_keyboards = reload_keyboards
  end
rescue EOFError => e # device is closed
  MultiLogger.error "Device is closed: #{e.message}"
ensure
  @destroy&.call
end