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

Add last activity functions for pointing device #20079

Merged
merged 11 commits into from
Mar 31, 2023
52 changes: 30 additions & 22 deletions quantum/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,22 @@ void last_encoder_activity_trigger(void) {
last_encoder_modification_time = last_input_modification_time = sync_timer_read32();
}

void set_activity_timestamps(uint32_t matrix_timestamp, uint32_t encoder_timestamp) {
last_matrix_modification_time = matrix_timestamp;
last_encoder_modification_time = encoder_timestamp;
last_input_modification_time = MAX(matrix_timestamp, encoder_timestamp);
static uint32_t last_pointing_device_modification_time = 0;
uint32_t last_pointing_device_activity_time(void) {
return last_pointing_device_modification_time;
}
uint32_t last_pointing_device_activity_elapsed(void) {
return sync_timer_elapsed32(last_pointing_device_modification_time);
}
void last_pointing_device_activity_trigger(void) {
last_pointing_device_modification_time = last_input_modification_time = sync_timer_read32();
}

void set_activity_timestamps(uint32_t matrix_timestamp, uint32_t encoder_timestamp, uint32_t pointing_device_timestamp) {
last_matrix_modification_time = matrix_timestamp;
last_encoder_modification_time = encoder_timestamp;
last_pointing_device_modification_time = pointing_device_timestamp;
last_input_modification_time = MAX(matrix_timestamp, MAX(encoder_timestamp, pointing_device_timestamp));
}

// Only enable this if console is enabled to print to
Expand Down Expand Up @@ -598,9 +610,10 @@ void quantum_task(void) {

/** \brief Main task that is repeatedly called as fast as possible. */
void keyboard_task(void) {
const bool matrix_changed = matrix_task();
if (matrix_changed) {
__attribute__((unused)) bool activity_has_occurred = false;
if (matrix_task()) {
last_matrix_activity_trigger();
activity_has_occurred = true;
}

quantum_task();
Expand All @@ -627,33 +640,32 @@ void keyboard_task(void) {
#endif

#ifdef ENCODER_ENABLE
const bool encoders_changed = encoder_read();
if (encoders_changed) {
if (encoder_read()) {
last_encoder_activity_trigger();
activity_has_occurred = true;
}
#endif

#ifdef POINTING_DEVICE_ENABLE
if (pointing_device_task()) {
last_pointing_device_activity_trigger();
activity_has_occurred = true;
}
#endif

#ifdef OLED_ENABLE
oled_task();
# if OLED_TIMEOUT > 0
// Wake up oled if user is using those fabulous keys or spinning those encoders!
# ifdef ENCODER_ENABLE
if (matrix_changed || encoders_changed) oled_on();
# else
if (matrix_changed) oled_on();
# endif
if (activity_has_occurred) oled_on();
# endif
#endif

#ifdef ST7565_ENABLE
st7565_task();
# if ST7565_TIMEOUT > 0
// Wake up display if user is using those fabulous keys or spinning those encoders!
# ifdef ENCODER_ENABLE
if (matrix_changed || encoders_changed) st7565_on();
# else
if (matrix_changed) st7565_on();
# endif
if (activity_has_occurred) st7565_on();
# endif
#endif

Expand All @@ -666,10 +678,6 @@ void keyboard_task(void) {
ps2_mouse_task();
#endif

#ifdef POINTING_DEVICE_ENABLE
pointing_device_task();
#endif

#ifdef MIDI_ENABLE
midi_task();
#endif
Expand Down
9 changes: 6 additions & 3 deletions quantum/keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,19 @@ void housekeeping_task(void); // To be executed by the main loop in each ba
void housekeeping_task_kb(void); // To be overridden by keyboard-level code
void housekeeping_task_user(void); // To be overridden by user/keymap-level code

uint32_t last_input_activity_time(void); // Timestamp of the last matrix or encoder activity
uint32_t last_input_activity_elapsed(void); // Number of milliseconds since the last matrix or encoder activity
uint32_t last_input_activity_time(void); // Timestamp of the last matrix or encoder or pointing device activity
uint32_t last_input_activity_elapsed(void); // Number of milliseconds since the last matrix or encoder or pointing device activity

uint32_t last_matrix_activity_time(void); // Timestamp of the last matrix activity
uint32_t last_matrix_activity_elapsed(void); // Number of milliseconds since the last matrix activity

uint32_t last_encoder_activity_time(void); // Timestamp of the last encoder activity
uint32_t last_encoder_activity_elapsed(void); // Number of milliseconds since the last encoder activity

void set_activity_timestamps(uint32_t matrix_timestamp, uint32_t encoder_timestamp); // Set the timestamps of the last matrix and encoder activity
uint32_t last_pointing_device_activity_time(void); // Timestamp of the last pointing device activity
uint32_t last_pointing_device_activity_elapsed(void); // Number of milliseconds since the last pointing device activity

void set_activity_timestamps(uint32_t matrix_timestamp, uint32_t encoder_timestamp, uint32_t pointing_device_timestamp); // Set the timestamps of the last matrix and encoder activity

uint32_t get_matrix_scan_rate(void);

Expand Down
28 changes: 18 additions & 10 deletions quantum/pointing_device/pointing_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ uint16_t pointing_device_get_shared_cpi(void) {

#endif // defined(SPLIT_POINTING_ENABLE)

static report_mouse_t local_mouse_report = {};
static report_mouse_t local_mouse_report = {};
static bool pointing_device_force_send = false;

extern const pointing_device_driver_t pointing_device_driver;

Expand Down Expand Up @@ -163,18 +164,20 @@ __attribute__((weak)) void pointing_device_init(void) {
* This sends the mouse report generated by pointing_device_task if changed since the last report. Once send zeros mouse report except buttons.
*
*/
__attribute__((weak)) void pointing_device_send(void) {
static report_mouse_t old_report = {};
__attribute__((weak)) bool pointing_device_send(void) {
static report_mouse_t old_report = {};
bool should_send_report = has_mouse_report_changed(&local_mouse_report, &old_report);

// If you need to do other things, like debugging, this is the place to do it.
if (has_mouse_report_changed(&local_mouse_report, &old_report)) {
if (should_send_report) {
host_mouse_send(&local_mouse_report);
}
// send it and 0 it out except for buttons, so those stay until they are explicity over-ridden using update_pointing_device
uint8_t buttons = local_mouse_report.buttons;
memset(&local_mouse_report, 0, sizeof(local_mouse_report));
local_mouse_report.buttons = buttons;
memcpy(&old_report, &local_mouse_report, sizeof(local_mouse_report));

return should_send_report || buttons;
}

/**
Expand Down Expand Up @@ -220,18 +223,18 @@ report_mouse_t pointing_device_adjust_by_defines(report_mouse_t mouse_report) {
* It applies any optional configuration e.g. rotation or axis inversion and then initiates a send.
*
*/
__attribute__((weak)) void pointing_device_task(void) {
__attribute__((weak)) bool pointing_device_task(void) {
#if defined(SPLIT_POINTING_ENABLE)
// Don't poll the target side pointing device.
if (!is_keyboard_master()) {
return;
return false;
};
#endif

#if (POINTING_DEVICE_TASK_THROTTLE_MS > 0)
static uint32_t last_exec = 0;
if (timer_elapsed32(last_exec) < POINTING_DEVICE_TASK_THROTTLE_MS) {
return;
return false;
}
last_exec = timer_read32();
#endif
Expand Down Expand Up @@ -286,7 +289,11 @@ __attribute__((weak)) void pointing_device_task(void) {
report_mouse_t mousekey_report = mousekey_get_report();
local_mouse_report.buttons = local_mouse_report.buttons | mousekey_report.buttons;
#endif
pointing_device_send();

const bool send_report = pointing_device_send() || pointing_device_force_send;
pointing_device_force_send = false;

return send_report;
}

/**
Expand All @@ -304,7 +311,8 @@ report_mouse_t pointing_device_get_report(void) {
* @param[in] mouse_report
*/
void pointing_device_set_report(report_mouse_t mouse_report) {
local_mouse_report = mouse_report;
pointing_device_force_send = has_mouse_report_changed(&local_mouse_report, &mouse_report);
memcpy(&local_mouse_report, &mouse_report, sizeof(local_mouse_report));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions quantum/pointing_device/pointing_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ typedef int16_t clamp_range_t;
#endif

void pointing_device_init(void);
void pointing_device_task(void);
void pointing_device_send(void);
bool pointing_device_task(void);
bool pointing_device_send(void);
report_mouse_t pointing_device_get_report(void);
void pointing_device_set_report(report_mouse_t mouse_report);
uint16_t pointing_device_get_cpi(void);
Expand Down
7 changes: 4 additions & 3 deletions quantum/split_common/transactions.c
Original file line number Diff line number Diff line change
Expand Up @@ -795,13 +795,14 @@ static void haptic_handlers_slave(matrix_row_t master_matrix[], matrix_row_t sla
static bool activity_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
static uint32_t last_update = 0;
split_slave_activity_sync_t activity_sync;
activity_sync.matrix_timestamp = last_matrix_activity_time();
activity_sync.encoder_timestamp = last_encoder_activity_time();
activity_sync.matrix_timestamp = last_matrix_activity_time();
activity_sync.encoder_timestamp = last_encoder_activity_time();
activity_sync.pointing_device_timestamp = last_pointing_device_activity_time();
return send_if_data_mismatch(PUT_ACTIVITY, &last_update, &activity_sync, &split_shmem->activity_sync, sizeof(activity_sync));
}

static void activity_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) {
set_activity_timestamps(split_shmem->activity_sync.matrix_timestamp, split_shmem->activity_sync.encoder_timestamp);
set_activity_timestamps(split_shmem->activity_sync.matrix_timestamp, split_shmem->activity_sync.encoder_timestamp, split_shmem->activity_sync.pointing_device_timestamp);
}

// clang-format off
Expand Down
1 change: 1 addition & 0 deletions quantum/split_common/transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ typedef struct _split_slave_haptic_sync_t {
typedef struct _split_slave_activity_sync_t {
uint32_t matrix_timestamp;
uint32_t encoder_timestamp;
uint32_t pointing_device_timestamp;
} split_slave_activity_sync_t;
#endif // defined(SPLIT_ACTIVITY_ENABLE)

Expand Down