Skip to content

Commit d43d88b

Browse files
authored
tmk_core/common/action.c: refactor for code size; merge multiple cases into one (qmk#11943)
* tmk_core/common/report.h: define `enum mouse_buttons` in terms of `#define MOUSE_BTN_MASK()` * tmk_core/common/action.c: collapse multiple `case KC_MS_BTN[1-8]:` into single `MOUSE_BTN_MASK(action.key.code - KC_MS_BTN1)` We all love tapping on our keyboards but this is taking the piss. This saves ~134 bytes on my ATmega32.
1 parent 90d4c68 commit d43d88b

File tree

2 files changed

+21
-72
lines changed

2 files changed

+21
-72
lines changed

tmk_core/common/action.c

Lines changed: 12 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -410,74 +410,22 @@ void process_action(keyrecord_t *record, action_t action) {
410410
case ACT_MOUSEKEY:
411411
if (event.pressed) {
412412
mousekey_on(action.key.code);
413-
switch (action.key.code) {
414-
# if defined(PS2_MOUSE_ENABLE) || defined(POINTING_DEVICE_ENABLE)
415-
case KC_MS_BTN1:
416-
register_button(true, MOUSE_BTN1);
417-
break;
418-
case KC_MS_BTN2:
419-
register_button(true, MOUSE_BTN2);
420-
break;
421-
case KC_MS_BTN3:
422-
register_button(true, MOUSE_BTN3);
423-
break;
424-
# endif
425-
# ifdef POINTING_DEVICE_ENABLE
426-
case KC_MS_BTN4:
427-
register_button(true, MOUSE_BTN4);
428-
break;
429-
case KC_MS_BTN5:
430-
register_button(true, MOUSE_BTN5);
431-
break;
432-
case KC_MS_BTN6:
433-
register_button(true, MOUSE_BTN6);
434-
break;
435-
case KC_MS_BTN7:
436-
register_button(true, MOUSE_BTN7);
437-
break;
438-
case KC_MS_BTN8:
439-
register_button(true, MOUSE_BTN8);
440-
break;
441-
# endif
442-
default:
443-
mousekey_send();
444-
break;
445-
}
446413
} else {
447414
mousekey_off(action.key.code);
448-
switch (action.key.code) {
415+
}
416+
switch (action.key.code) {
449417
# if defined(PS2_MOUSE_ENABLE) || defined(POINTING_DEVICE_ENABLE)
450-
case KC_MS_BTN1:
451-
register_button(false, MOUSE_BTN1);
452-
break;
453-
case KC_MS_BTN2:
454-
register_button(false, MOUSE_BTN2);
455-
break;
456-
case KC_MS_BTN3:
457-
register_button(false, MOUSE_BTN3);
458-
break;
459-
# endif
460-
# ifdef POINTING_DEVICE_ENABLE
461-
case KC_MS_BTN4:
462-
register_button(false, MOUSE_BTN4);
463-
break;
464-
case KC_MS_BTN5:
465-
register_button(false, MOUSE_BTN5);
466-
break;
467-
case KC_MS_BTN6:
468-
register_button(false, MOUSE_BTN6);
469-
break;
470-
case KC_MS_BTN7:
471-
register_button(false, MOUSE_BTN7);
472-
break;
473-
case KC_MS_BTN8:
474-
register_button(false, MOUSE_BTN8);
475-
break;
418+
# ifdef POINTING_DEVICE_ENABLE
419+
case KC_MS_BTN1 ... KC_MS_BTN8:
420+
# else
421+
case KC_MS_BTN1 ... KC_MS_BTN3:
422+
# endif
423+
register_button(event.pressed, MOUSE_BTN_MASK(action.key.code - KC_MS_BTN1));
424+
break;
476425
# endif
477-
default:
478-
mousekey_send();
479-
break;
480-
}
426+
default:
427+
mousekey_send();
428+
break;
481429
}
482430
break;
483431
#endif

tmk_core/common/report.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,16 @@ enum hid_report_ids {
3434
};
3535

3636
/* Mouse buttons */
37+
#define MOUSE_BTN_MASK(n) (1 << (n))
3738
enum mouse_buttons {
38-
MOUSE_BTN1 = (1 << 0),
39-
MOUSE_BTN2 = (1 << 1),
40-
MOUSE_BTN3 = (1 << 2),
41-
MOUSE_BTN4 = (1 << 3),
42-
MOUSE_BTN5 = (1 << 4),
43-
MOUSE_BTN6 = (1 << 5),
44-
MOUSE_BTN7 = (1 << 6),
45-
MOUSE_BTN8 = (1 << 7)
39+
MOUSE_BTN1 = MOUSE_BTN_MASK(0),
40+
MOUSE_BTN2 = MOUSE_BTN_MASK(1),
41+
MOUSE_BTN3 = MOUSE_BTN_MASK(2),
42+
MOUSE_BTN4 = MOUSE_BTN_MASK(3),
43+
MOUSE_BTN5 = MOUSE_BTN_MASK(4),
44+
MOUSE_BTN6 = MOUSE_BTN_MASK(5),
45+
MOUSE_BTN7 = MOUSE_BTN_MASK(6),
46+
MOUSE_BTN8 = MOUSE_BTN_MASK(7)
4647
};
4748

4849
/* Consumer Page (0x0C)

0 commit comments

Comments
 (0)