Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Macro: fix and redesign #116

Open
tmk opened this issue Apr 23, 2014 · 10 comments
Open

Macro: fix and redesign #116

tmk opened this issue Apr 23, 2014 · 10 comments

Comments

@tmk
Copy link
Owner

tmk commented Apr 23, 2014

At this moment macro uses add_weak_mods for all mdofiers. This is intented to prevent macro from breaking real/physical key state when the modifier in macro is released. But add_mods is needed for some macro usage, for example, when you want a macro which behaves as real modifier key.

Later I'll change macro action behaviour:
KEY_DOWN: uses register_code even for modifier
KEY_UP: uses register_code even for modifier

And add macro actions:
ADD_WEAK_MOD: uses weak_add_mods
DEL_WEAK_MOD: uses weak_del_mods
ADD_MOD:
DEL_MOD:
ADD_KEY:
DEL_KEY:
SEND: uses send_keyboard_report

add_weak_mods(MOD_BIT(macro));

@tmk tmk changed the title Macro Macro: fix and add new action Apr 23, 2014
@tmk tmk added the TODO label Jan 19, 2015
@tmk
Copy link
Owner Author

tmk commented Jan 19, 2015

use case #136

@tmk
Copy link
Owner Author

tmk commented Oct 22, 2015

And this use case.
e852582#commitcomment-13926250

Users may want to change modifier state in their macro definition.

  • store modifier state
  • restore modifier state
  • clear modifier state

@tmk tmk changed the title Macro: fix and add new action Macro: fix and redesign Oct 22, 2015
@tmk
Copy link
Owner Author

tmk commented Oct 22, 2015

This commit e852582 probably fixed #136.

@p3lim
Copy link
Contributor

p3lim commented Oct 23, 2015

Implemented storing, restoring and clearing modifiers.
This does not take arguments though, which is probably wanted/needed.

From a1260b5fbc1813229738d81109af2c8dd68101c1 Mon Sep 17 00:00:00 2001
From: Adrian L Lange <mail@p3lim.net>
Date: Fri, 23 Oct 2015 20:38:50 +0200
Subject: [PATCH] Add support for storing, restoring and clearing modifiers in
 macros

---
 common/action_macro.c | 13 +++++++++++++
 common/action_macro.h | 12 ++++++++++++
 2 files changed, 25 insertions(+)

diff --git a/common/action_macro.c b/common/action_macro.c
index ffaf125..34e22e5 100644
--- a/common/action_macro.c
+++ b/common/action_macro.c
@@ -34,6 +34,8 @@ void action_macro_play(const macro_t *macro_p)
     macro_t macro = END;
     uint8_t interval = 0;

+    uint8_t mod_storage;
+
     if (!macro_p) return;
     while (true) {
         switch (MACRO_READ()) {
@@ -66,6 +68,17 @@ void action_macro_play(const macro_t *macro_p)
                 interval = MACRO_READ();
                 dprintf("INTERVAL(%u)\n", interval);
                 break;
+            case MOD_STORE:
+                mod_storage = get_mods();
+                break;
+            case MOD_RESTORE:
+                set_mods(mod_storage);
+                send_keyboard_report();
+                break;
+            case MOD_CLEAR:
+                clear_mods();
+                send_keyboard_report();
+                break;
             case 0x04 ... 0x73:
                 dprintf("DOWN(%02X)\n", macro);
                 register_code(macro);
diff --git a/common/action_macro.h b/common/action_macro.h
index aedc32e..4cf2216 100644
--- a/common/action_macro.h
+++ b/common/action_macro.h
@@ -64,6 +64,9 @@ enum macro_command_id{
     /* 0x74 - 0x83 */
     WAIT                = 0x74,
     INTERVAL,
+    MOD_STORE,
+    MOD_RESTORE,
+    MOD_CLEAR,

     /* 0x84 - 0xf3 (reserved for keycode up) */

@@ -82,6 +85,9 @@ enum macro_command_id{
 #define TYPE(key)       DOWN(key), UP(key)
 #define WAIT(ms)        WAIT, (ms)
 #define INTERVAL(ms)    INTERVAL, (ms)
+#define STORE()         MOD_STORE
+#define RESTORE()       MOD_RESTORE
+#define CLEAR()         MOD_CLEAR

 /* key down */
 #define D(key)          DOWN(KC_##key)
@@ -93,6 +99,12 @@ enum macro_command_id{
 #define W(ms)           WAIT(ms)
 /* interval */
 #define I(ms)           INTERVAL(ms)
+/* store modifier(s) */
+#define SM()            STORE()
+/* restore modifier(s) */
+#define RM()            RESTORE()
+/* clear modifier(s) */
+#define CM()            CLEAR()

 /* for backward comaptibility */
 #define MD(key)         DOWN(KC_##key)
-- 
2.6.2.windows.1

@p3lim
Copy link
Contributor

p3lim commented Oct 23, 2015

With that patch, I'm able to write my macro like this:

return (record->event.pressed ? MACRO(T(F20), CM(), SM(), T(SLSH), RM(), T(O), END) : MACRO_NONE);

Instead of how you suggested in e852582#commitcomment-13938301

@tmk
Copy link
Owner Author

tmk commented Oct 23, 2015

Ah, nice. It is almost exactly what I had in mind.
I like to make STORE() atomic, which only stores modifier state.

diff --git a/tmk_core/common/action_macro.c b/tmk_core/common/action_macro.c
index ffaf125..34e22e5 100644
--- a/tmk_core/common/action_macro.c
+++ b/tmk_core/common/action_macro.c
@@ -34,6 +34,8 @@ void action_macro_play(const macro_t *macro_p)
     macro_t macro = END;
     uint8_t interval = 0;

+    uint8_t mod_storage;
+
     if (!macro_p) return;
     while (true) {
         switch (MACRO_READ()) {
@@ -66,6 +68,17 @@ void action_macro_play(const macro_t *macro_p)
                 interval = MACRO_READ();
                 dprintf("INTERVAL(%u)\n", interval);
                 break;
+            case MOD_STORE:
+                mod_storage = get_mods();
+                break;
+            case MOD_RESTORE:
+                set_mods(mod_storage);
+                send_keyboard_report();
+                break;
+            case MOD_CLEAR:
+                clear_mods();
+                send_keyboard_report();
+                break;
             case 0x04 ... 0x73:
                 dprintf("DOWN(%02X)\n", macro);
                 register_code(macro);

@p3lim
Copy link
Contributor

p3lim commented Oct 23, 2015

So you'd have to store > clear > rest of macro > restore > end?

@tmk
Copy link
Owner Author

tmk commented Oct 23, 2015

yep.

@p3lim
Copy link
Contributor

p3lim commented Oct 23, 2015

I'd be fine with that.

Updated my patch in case you want to use it.

p3lim added a commit to p3lim/keyboard_firmware that referenced this issue Dec 14, 2015
- This required a change to the core, see tmk/tmk_keyboard#116.
@tmk
Copy link
Owner Author

tmk commented Jan 15, 2016

Applied patch, thank you. 5a196b6

Three commands were added:

SM(): store modifier state
RM(): restore modifier state
CM(): clear modifier state

seancaffery pushed a commit to seancaffery/tmk_keyboard that referenced this issue Feb 3, 2016
Fix for broken link in readme.
@tmk tmk added the KEYMAP label Nov 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants