Skip to content

Commit

Permalink
quantum/command.c: optimise mousekey_console() for size & legibility
Browse files Browse the repository at this point in the history
Made various tweaks to the interface, but still functionally identical.

Assume `KC_1`…`KC_0` to be contiguous, and removed `numkey2num(…)` entirely.
It was exported in `command.h` by 1a0bac8 for no obvious reason, before
which it was `static`. I doubt anyone uses it.

`mousekey_console()` is now enabled regardless of `MK_3_SPEED`.
Needs fleshing out for things other than the X11 variant though.

This commit saves 638 bytes on my ATmega32.
  • Loading branch information
liyang committed Feb 17, 2021
1 parent 0038352 commit 3ec12a5
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 190 deletions.
307 changes: 119 additions & 188 deletions quantum/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ static bool command_console(uint8_t code);
static void command_console_help(void);
#if defined(MOUSEKEY_ENABLE) && !defined(MK_3_SPEED)
static bool mousekey_console(uint8_t code);
static void mousekey_console_help(void);
#endif

static void switch_default_layer(uint8_t layer);
Expand Down Expand Up @@ -556,11 +555,10 @@ static bool command_console(uint8_t code) {
case KC_ESC:
command_state = ONESHOT;
return false;
#if defined(MOUSEKEY_ENABLE) && !defined(MK_3_SPEED)
#if defined(MOUSEKEY_ENABLE)
case KC_M:
mousekey_console_help();
print("M> ");
command_state = MOUSEKEY;
mousekey_console(KC_SLASH /* ? */);
return true;
#endif
default:
Expand All @@ -569,245 +567,178 @@ static bool command_console(uint8_t code) {
}
}

#if defined(MOUSEKEY_ENABLE) && !defined(MK_3_SPEED)
/***********************************************************
* Mousekey console
***********************************************************/
static uint8_t mousekey_param = 0;

#if defined(MOUSEKEY_ENABLE)

# if !defined(NO_PRINT) && !defined(USER_PRINT)
static void mousekey_param_print(void) {
xprintf(/* clang-format off */
"\n\t- Values -\n"

"1: delay(*10ms): %u\n"
"2: interval(ms): %u\n"
"3: max_speed: %u\n"
"4: time_to_max: %u\n"
"5: wheel_max_speed: %u\n"
"6: wheel_time_to_max: %u\n"
#ifndef MK_3_SPEED
"1: delay(*10ms): %u\n"
"2: interval(ms): %u\n"
"3: max_speed: %u\n"
"4: time_to_max: %u\n"
"5: wheel_max_speed: %u\n"
"6: wheel_time_to_max: %u\n"

, mk_delay
, mk_interval
, mk_max_speed
, mk_time_to_max
, mk_wheel_max_speed
, mk_wheel_time_to_max
#else
"no knobs sorry\n"
#endif

); /* clang-format on */
}
# endif /* !NO_PRINT && !USER_PRINT */

//#define PRINT_SET_VAL(v) print(#v " = "); print_dec(v); print("\n");
# define PRINT_SET_VAL(v) xprintf(# v " = %d\n", (v))
static void mousekey_param_inc(uint8_t param, uint8_t inc) {
switch (param) {
case 1:
if (mk_delay + inc < UINT8_MAX)
mk_delay += inc;
else
mk_delay = UINT8_MAX;
PRINT_SET_VAL(mk_delay);
break;
case 2:
if (mk_interval + inc < UINT8_MAX)
mk_interval += inc;
else
mk_interval = UINT8_MAX;
PRINT_SET_VAL(mk_interval);
break;
case 3:
if (mk_max_speed + inc < UINT8_MAX)
mk_max_speed += inc;
else
mk_max_speed = UINT8_MAX;
PRINT_SET_VAL(mk_max_speed);
break;
case 4:
if (mk_time_to_max + inc < UINT8_MAX)
mk_time_to_max += inc;
else
mk_time_to_max = UINT8_MAX;
PRINT_SET_VAL(mk_time_to_max);
break;
case 5:
if (mk_wheel_max_speed + inc < UINT8_MAX)
mk_wheel_max_speed += inc;
else
mk_wheel_max_speed = UINT8_MAX;
PRINT_SET_VAL(mk_wheel_max_speed);
break;
case 6:
if (mk_wheel_time_to_max + inc < UINT8_MAX)
mk_wheel_time_to_max += inc;
else
mk_wheel_time_to_max = UINT8_MAX;
PRINT_SET_VAL(mk_wheel_time_to_max);
break;
}
}
# if !defined(NO_PRINT) && !defined(USER_PRINT)
static void mousekey_console_help(void) {
mousekey_param_print();
xprintf(/* clang-format off */
"p: print values\n"
"d: set defaults\n"
"up: +1\n"
"dn: -1\n"
"lt: +10\n"
"rt: -10\n"
"ESC/q: quit\n"

#ifndef MK_3_SPEED
"\n"
"speed = delta * max_speed * (repeat / time_to_max)\n"
"where delta: cursor=%d, wheel=%d\n"
"See http://en.wikipedia.org/wiki/Mouse_keys\n"
, MOUSEKEY_MOVE_DELTA, MOUSEKEY_WHEEL_DELTA
#endif

static void mousekey_param_dec(uint8_t param, uint8_t dec) {
switch (param) {
case 1:
if (mk_delay > dec)
mk_delay -= dec;
else
mk_delay = 0;
PRINT_SET_VAL(mk_delay);
break;
case 2:
if (mk_interval > dec)
mk_interval -= dec;
else
mk_interval = 0;
PRINT_SET_VAL(mk_interval);
break;
case 3:
if (mk_max_speed > dec)
mk_max_speed -= dec;
else
mk_max_speed = 0;
PRINT_SET_VAL(mk_max_speed);
break;
case 4:
if (mk_time_to_max > dec)
mk_time_to_max -= dec;
else
mk_time_to_max = 0;
PRINT_SET_VAL(mk_time_to_max);
break;
case 5:
if (mk_wheel_max_speed > dec)
mk_wheel_max_speed -= dec;
else
mk_wheel_max_speed = 0;
PRINT_SET_VAL(mk_wheel_max_speed);
break;
case 6:
if (mk_wheel_time_to_max > dec)
mk_wheel_time_to_max -= dec;
else
mk_wheel_time_to_max = 0;
PRINT_SET_VAL(mk_wheel_time_to_max);
break;
}
); /* clang-format on */
}
# endif /* !NO_PRINT && !USER_PRINT */

static void mousekey_console_help(void) {
print("\n\t- Mousekey -\n"
"ESC/q: quit\n"
"1: delay(*10ms)\n"
"2: interval(ms)\n"
"3: max_speed\n"
"4: time_to_max\n"
"5: wheel_max_speed\n"
"6: wheel_time_to_max\n"
"\n"
"p: print values\n"
"d: set defaults\n"
"up: +1\n"
"down: -1\n"
"pgup: +10\n"
"pgdown: -10\n"
"\n"
"speed = delta * max_speed * (repeat / time_to_max)\n");
xprintf("where delta: cursor=%d, wheel=%d\n"
"See http://en.wikipedia.org/wiki/Mouse_keys\n",
MOUSEKEY_MOVE_DELTA, MOUSEKEY_WHEEL_DELTA);
}
/* Only used by `quantum/command.c` / `command_proc()`. To avoid
* any doubt: we return `false` to return to the main console,
* which differs from the `bool` that `command_proc()` returns. */
bool mousekey_console(uint8_t code) {
static uint8_t param = 0;
static uint8_t *pp = NULL;
static char * desc = NULL;

# if defined(NO_PRINT) || defined(USER_PRINT) /* -Wunused-parameter */
(void)desc;
# endif

int8_t change = 0;

static bool mousekey_console(uint8_t code) {
switch (code) {
case KC_H:
case KC_SLASH: /* ? */
# if !defined(NO_PRINT) && !defined(USER_PRINT)
print("\n\t- Mousekey -\n");
mousekey_console_help();
# endif
break;

case KC_Q:
case KC_ESC:
if (mousekey_param) {
mousekey_param = 0;
} else {
print("C> ");
command_state = CONSOLE;
return false;
}
print("q\n");
if (!param) return false;
param = 0;
pp = NULL;
desc = NULL;
break;

case KC_P:
# if !defined(NO_PRINT) && !defined(USER_PRINT)
print("\n\t- Values -\n");
mousekey_param_print();
# endif
break;
case KC_1:
case KC_2:
case KC_3:
case KC_4:
case KC_5:
case KC_6:
mousekey_param = numkey2num(code);
break;
case KC_UP:
mousekey_param_inc(mousekey_param, 1);
break;
case KC_DOWN:
mousekey_param_dec(mousekey_param, 1);
break;
case KC_PGUP:
mousekey_param_inc(mousekey_param, 10);
break;
case KC_PGDN:
mousekey_param_dec(mousekey_param, 10);
break;

case KC_1 ... KC_0: /* KC_0 gives param = 10 */
param = 1 + code - KC_1;
switch (param) { /* clang-format off */
# define PARAM(n, v) case n: pp = &(v); desc = #v; break

#ifndef MK_3_SPEED
PARAM(1, mk_delay);
PARAM(2, mk_interval);
PARAM(3, mk_max_speed);
PARAM(4, mk_time_to_max);
PARAM(5, mk_wheel_max_speed);
PARAM(6, mk_wheel_time_to_max);
#endif /* MK_3_SPEED */

# undef PARAM
default:
param = 0;
print("?\n");
break;
} /* clang-format on */
if (param) xprintf("%u\n", param);
break;

/* clang-format off */
case KC_UP: change = +1; break;
case KC_DOWN: change = -1; break;
case KC_LEFT: change = -10; break;
case KC_RIGHT: change = +10; break;
/* clang-format on */

case KC_D:

# ifndef MK_3_SPEED
mk_delay = MOUSEKEY_DELAY / 10;
mk_interval = MOUSEKEY_INTERVAL;
mk_max_speed = MOUSEKEY_MAX_SPEED;
mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED;
mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
print("set default\n");
# endif /* MK_3_SPEED */

print("defaults\n");
break;

default:
print("?");
return false;
print("?\n");
break;
}

if (change) {
if (pp) {
int16_t val = *pp + change;
if (val > (int16_t)UINT8_MAX)
*pp = UINT8_MAX;
else if (val < 0)
*pp = 0;
else
*pp = (uint8_t)val;
xprintf("= %u\n", *pp);
} else {
print("?\n");
}
}
if (mousekey_param) {
xprintf("M%d> ", mousekey_param);

if (param) {
xprintf("M%u:%s> ", param, desc ? desc : "???");
} else {
print("M>");
print("M> ");
}
return true;
}
#endif

#endif /* MOUSEKEY_ENABLE */

/***********************************************************
* Utilities
***********************************************************/
uint8_t numkey2num(uint8_t code) {
switch (code) {
case KC_1:
return 1;
case KC_2:
return 2;
case KC_3:
return 3;
case KC_4:
return 4;
case KC_5:
return 5;
case KC_6:
return 6;
case KC_7:
return 7;
case KC_8:
return 8;
case KC_9:
return 9;
case KC_0:
return 0;
}
return 0;
}

static void switch_default_layer(uint8_t layer) {
xprintf("L%d\n", layer);
Expand Down
3 changes: 1 addition & 2 deletions quantum/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ bool command_extra(uint8_t code);
bool command_console_extra(uint8_t code);

#ifdef COMMAND_ENABLE
uint8_t numkey2num(uint8_t code);
bool command_proc(uint8_t code);
bool command_proc(uint8_t code);
#else
# define command_proc(code) false
#endif
Expand Down

0 comments on commit 3ec12a5

Please sign in to comment.