From dd378601608849679ead6e2cccb74f7f29c131dc Mon Sep 17 00:00:00 2001
From: Joe Wasson <jwasson+github@gmail.com>
Date: Wed, 27 Jul 2016 08:43:02 -0700
Subject: [PATCH] Add one-hand support.

This adds an action, `ACTION_SWAP_HANDS`, that swaps the the keys on the keyboard across a keymap-defined hemisphere in order to support one-hand typing without requiring a separate one-handed layer. See updated `doc/keymap.md` for more information.
---
 doc/keymap.md                 | 18 ++++++++++++++++++
 tmk_core/common.mk            |  4 ++++
 tmk_core/common/action.c      | 33 +++++++++++++++++++++++++++++++++
 tmk_core/common/action.h      | 18 ++++++++++++++++++
 tmk_core/common/action_code.h |  8 +++++++-
 5 files changed, 80 insertions(+), 1 deletion(-)

diff --git a/doc/keymap.md b/doc/keymap.md
index d1985e567c..1285ad6cd1 100644
--- a/doc/keymap.md
+++ b/doc/keymap.md
@@ -455,6 +455,24 @@ Turn the backlight on and off without changing level.
 
 
 
+### 2.6 Swap-Hands Action
+The swap-hands action allows support for one-handed keyboards without requiring a separate layer. Set `ONEHAND_ENABLE` in the Makefile and define a `hand_swap_config` entry in your keymap. Now whenever the `ACTION_SWAP_HANDS` command is executed the keyboard is mirrored. For instance, to type "Hello, World" on QWERTY you would type `^Ge^s^s^w^c W^wr^sd`
+
+The configuration table is a simple 2-dimensional array to map from column/row to new column/row. Example `hand_swap_config` for Planck:
+
+```
+const keypos_t hand_swap_config[MATRIX_ROWS][MATRIX_COLS] = {
+  {{11, 0}, {10, 0}, {9, 0}, {8, 0}, {7, 0}, {6, 0}, {5, 0}, {4, 0}, {3, 0}, {2, 0}, {1, 0}, {0, 0}},
+  {{11, 1}, {10, 1}, {9, 1}, {8, 1}, {7, 1}, {6, 1}, {5, 1}, {4, 1}, {3, 1}, {2, 1}, {1, 1}, {0, 1}},
+  {{11, 2}, {10, 2}, {9, 2}, {8, 2}, {7, 2}, {6, 2}, {5, 2}, {4, 2}, {3, 2}, {2, 2}, {1, 2}, {0, 2}},
+  {{11, 3}, {10, 3}, {9, 3}, {8, 3}, {7, 3}, {6, 3}, {5, 3}, {4, 3}, {3, 3}, {2, 3}, {1, 3}, {0, 3}},
+};
+```
+
+Note that the array indices are reversed same as the matrix and the values are of type `keypos_t` which is `{col, row}` and all values are zero-based. In the example above, `hand_swap_config[2][4]` (third row, fifth column) would return {7, 2} (third row, eighth column).
+
+
+
 ## 3. Layer switching Example
 There are some ways to switch layer with 'Layer' actions.
 
diff --git a/tmk_core/common.mk b/tmk_core/common.mk
index aa05b9491d..429c571435 100644
--- a/tmk_core/common.mk
+++ b/tmk_core/common.mk
@@ -85,6 +85,10 @@ ifeq ($(strip $(BLUETOOTH_ENABLE)), yes)
     OPT_DEFS += -DBLUETOOTH_ENABLE
 endif
 
+ifeq ($(strip $(ONEHAND_ENABLE)), yes)
+    OPT_DEFS += -DONEHAND_ENABLE
+endif
+
 ifeq ($(strip $(KEYMAP_SECTION_ENABLE)), yes)
     OPT_DEFS += -DKEYMAP_SECTION_ENABLE
 
diff --git a/tmk_core/common/action.c b/tmk_core/common/action.c
index be6dea2b79..0413b1a997 100644
--- a/tmk_core/common/action.c
+++ b/tmk_core/common/action.c
@@ -41,6 +41,12 @@ void action_exec(keyevent_t event)
         dprint("EVENT: "); debug_event(event); dprintln();
     }
 
+#ifdef ONEHAND_ENABLE
+    if (!IS_NOEVENT(event)) {
+        process_hand_swap(&event);
+    }
+#endif
+
     keyrecord_t record = { .event = event };
 
 #ifndef NO_ACTION_TAPPING
@@ -53,6 +59,26 @@ void action_exec(keyevent_t event)
 #endif
 }
 
+#ifdef ONEHAND_ENABLE
+bool swap_hands = false;
+
+void process_hand_swap(keyevent_t *event) {
+    static swap_state_row_t swap_state[MATRIX_ROWS];
+
+    keypos_t pos = event->key;
+    swap_state_row_t col_bit = (swap_state_row_t)1<<pos.col;
+    bool do_swap = event->pressed ? swap_hands :
+                                    swap_state[pos.row] & (col_bit);
+
+    if (do_swap) {
+        event->key = hand_swap_config[pos.row][pos.col];
+        swap_state[pos.row] |= col_bit;
+    } else {
+        swap_state[pos.row] &= ~(col_bit);
+    }
+}
+#endif
+
 #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
 bool disable_action_cache = false;
 
@@ -439,6 +465,13 @@ void process_action(keyrecord_t *record, action_t action)
             break;
 #endif
         case ACT_COMMAND:
+            switch (action.command.id) {
+#ifdef ONEHAND_ENABLE
+                case CMD_SWAP_HANDS:
+                    swap_hands = event.pressed;
+                    break;
+#endif
+            }
             break;
 #ifndef NO_ACTION_FUNCTION
         case ACT_FUNCTION:
diff --git a/tmk_core/common/action.h b/tmk_core/common/action.h
index e8aa12a7cb..b9bdfe642c 100644
--- a/tmk_core/common/action.h
+++ b/tmk_core/common/action.h
@@ -65,6 +65,24 @@ bool process_record_quantum(keyrecord_t *record);
 #if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
 extern bool disable_action_cache;
 #endif
+
+/* Code for handling one-handed key modifiers. */
+#ifdef ONEHAND_ENABLE
+extern bool swap_hands;
+extern const keypos_t hand_swap_config[MATRIX_ROWS][MATRIX_COLS];
+#if (MATRIX_COLS <= 8)
+typedef  uint8_t    swap_state_row_t;
+#elif (MATRIX_COLS <= 16)
+typedef  uint16_t   swap_state_row_t;
+#elif (MATRIX_COLS <= 32)
+typedef  uint32_t   swap_state_row_t;
+#else
+#error "MATRIX_COLS: invalid value"
+#endif
+
+void process_hand_swap(keyevent_t *record);
+#endif
+
 void process_record_nocache(keyrecord_t *record);
 void process_record(keyrecord_t *record);
 void process_action(keyrecord_t *record, action_t action);
diff --git a/tmk_core/common/action_code.h b/tmk_core/common/action_code.h
index ca729aaece..95d2cbf3e4 100644
--- a/tmk_core/common/action_code.h
+++ b/tmk_core/common/action_code.h
@@ -295,6 +295,10 @@ enum backlight_opt {
     BACKLIGHT_STEP     = 3,
     BACKLIGHT_LEVEL    = 4,
 };
+
+enum command_id {
+    CMD_SWAP_HANDS = 0x14,
+};
 /* Macro */
 #define ACTION_MACRO(id)                ACTION(ACT_MACRO, (id))
 #define ACTION_MACRO_TAP(id)            ACTION(ACT_MACRO, FUNC_TAP<<8 | (id))
@@ -306,7 +310,7 @@ enum backlight_opt {
 #define ACTION_BACKLIGHT_STEP()         ACTION(ACT_BACKLIGHT, BACKLIGHT_STEP << 8)
 #define ACTION_BACKLIGHT_LEVEL(level)   ACTION(ACT_BACKLIGHT, BACKLIGHT_LEVEL << 8 | (level))
 /* Command */
-#define ACTION_COMMAND(id, opt)         ACTION(ACT_COMMAND,  (opt)<<8 | (addr))
+#define ACTION_COMMAND(id, opt)         ACTION(ACT_COMMAND,  (opt)<<8 | (id))
 /* Function */
 enum function_opts {
     FUNC_TAP = 0x8,     /* indciates function is tappable */
@@ -314,5 +318,7 @@ enum function_opts {
 #define ACTION_FUNCTION(id)             ACTION(ACT_FUNCTION, (id))
 #define ACTION_FUNCTION_TAP(id)         ACTION(ACT_FUNCTION, FUNC_TAP<<8 | (id))
 #define ACTION_FUNCTION_OPT(id, opt)    ACTION(ACT_FUNCTION, (opt)<<8 | (id))
+/* OneHand Support */
+#define ACTION_SWAP_HANDS()             ACTION_COMMAND(CMD_SWAP_HANDS, 0)
 
 #endif /* ACTION_CODE_H */