-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
35 changed files
with
293 additions
and
288 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
defmodule AFK.Keycode do | ||
@moduledoc """ | ||
A keycode represents a key that when pressed affects the keyboard state in | ||
some way. | ||
The currently supported keycode types are: | ||
* `AFK.Keycode.Key` - A basic keyboard key | ||
* `AFK.Keycode.Layer` - A key that can activate other layers | ||
* `AFK.Keycode.Modifier` - A basic keyboard modifier | ||
* `AFK.Keycode.None` - A keycode that does nothing | ||
* `AFK.Keycode.Transparent` - A key that is transparent to its layer | ||
""" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
defmodule AFK.Keycode.Layer do | ||
@moduledoc """ | ||
Represents a key that can activate other layers on and off in various ways. | ||
Layers can be activated in 3 ways: | ||
* `:hold` - Temporarily activates a layer while being held | ||
* `:toggle` - Toggles a layer on or off when pressed | ||
* `:default` - Sets a layer as the default layer | ||
""" | ||
|
||
@enforce_keys [:type, :layer] | ||
defstruct [:type, :layer] | ||
|
||
@doc """ | ||
Creates a layer activation keycode. | ||
The valid types are: | ||
* `:hold` - Activates a layer while being held | ||
* `:toggle` - Toggles a layer on or off when pressed | ||
* `:default` - Sets a layer as the default layer | ||
## Examples | ||
iex> new(:hold, 1) | ||
%AFK.Keycode.Layer{layer: 1, type: :hold} | ||
iex> new(:hold, 2) | ||
%AFK.Keycode.Layer{layer: 2, type: :hold} | ||
iex> new(:toggle, 1) | ||
%AFK.Keycode.Layer{layer: 1, type: :toggle} | ||
iex> new(:default, 2) | ||
%AFK.Keycode.Layer{layer: 2, type: :default} | ||
""" | ||
def new(type, layer) when type in ~w(hold toggle default)a and is_integer(layer) and layer >= 0 do | ||
struct!(__MODULE__, | ||
type: type, | ||
layer: layer | ||
) | ||
end | ||
|
||
defimpl AFK.ApplyKeycode do | ||
alias AFK.Keycode.Layer | ||
alias AFK.State.Keymap | ||
|
||
def apply_keycode(%Layer{type: :hold} = keycode, state, key) do | ||
keymap = Keymap.add_activation(state.keymap, keycode, key) | ||
|
||
%{state | keymap: keymap} | ||
end | ||
|
||
def apply_keycode(%Layer{type: :toggle} = keycode, state, key) do | ||
keymap = Keymap.toggle_activation(state.keymap, keycode, key) | ||
|
||
%{state | keymap: keymap} | ||
end | ||
|
||
def apply_keycode(%Layer{type: :default} = keycode, state, key) do | ||
keymap = Keymap.set_default(state.keymap, keycode, key) | ||
|
||
%{state | keymap: keymap} | ||
end | ||
|
||
def unapply_keycode(%Layer{type: :hold} = keycode, state, key) do | ||
keymap = Keymap.remove_activation(state.keymap, keycode, key) | ||
|
||
%{state | keymap: keymap} | ||
end | ||
|
||
def unapply_keycode(%Layer{type: :toggle}, state, _key) do | ||
state | ||
end | ||
|
||
def unapply_keycode(%Layer{type: :default}, state, _key) do | ||
state | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
defmodule AFK.Keycode.Modifier do | ||
@moduledoc """ | ||
Represents a basic modifier keycode, like control, shift, etc. | ||
All standard modifiers on a keyboard can be represented by `Modifier` | ||
keycodes. The following is a list of all supported modifiers: | ||
* `:left_control` | ||
* `:left_shift` | ||
* `:left_alt` | ||
* `:left_super` | ||
* `:right_control` | ||
* `:right_shift` | ||
* `:right_alt` | ||
* `:right_super` | ||
""" | ||
|
||
@enforce_keys [:modifier] | ||
defstruct [:modifier] | ||
|
||
@doc """ | ||
Creates a basic modifier keycode. | ||
## Examples | ||
iex> new(:left_control) | ||
%AFK.Keycode.Modifier{modifier: :left_control} | ||
iex> new(:right_super) | ||
%AFK.Keycode.Modifier{modifier: :right_super} | ||
""" | ||
for {_value, modifier} <- AFK.Scancode.modifiers() do | ||
def new(unquote(modifier)), do: struct!(__MODULE__, modifier: unquote(modifier)) | ||
end | ||
|
||
defimpl AFK.Scancode.Protocol do | ||
for {value, modifier} <- AFK.Scancode.modifiers() do | ||
def scancode(%AFK.Keycode.Modifier{modifier: unquote(modifier)}), do: unquote(value) | ||
end | ||
end | ||
|
||
defimpl AFK.ApplyKeycode, for: AFK.Keycode.Modifier do | ||
def apply_keycode(keycode, state, key) do | ||
modifier_used? = | ||
Enum.any?(state.modifiers, fn | ||
{_key, ^keycode} -> true | ||
_ -> false | ||
end) | ||
|
||
if modifier_used? do | ||
state | ||
else | ||
modifiers = Map.put(state.modifiers, key, keycode) | ||
|
||
%{state | modifiers: modifiers} | ||
end | ||
end | ||
|
||
def unapply_keycode(keycode, state, key) do | ||
modifiers = | ||
state.modifiers | ||
|> Enum.filter(fn | ||
{^key, ^keycode} -> false | ||
_ -> true | ||
end) | ||
|> Map.new() | ||
|
||
%{state | modifiers: modifiers} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
defmodule AFK.Keycode.None do | ||
@moduledoc """ | ||
Represents a key that does nothing when pressed. | ||
""" | ||
|
||
defstruct [] | ||
|
||
@doc """ | ||
Creates a `None` keycode. | ||
""" | ||
def new, do: %__MODULE__{} | ||
|
||
defimpl AFK.ApplyKeycode do | ||
def apply_keycode(_keycode, state, _key) do | ||
state | ||
end | ||
|
||
def unapply_keycode(_keycode, state, _key) do | ||
state | ||
end | ||
end | ||
end |
2 changes: 1 addition & 1 deletion
2
lib/afk/keycodes/transparent.ex → lib/afk/keycode/transparent.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.