Skip to content

Commit

Permalink
finally fix hscroll fully and add maybe ability to change multiplier …
Browse files Browse the repository at this point in the history
…at runtime
  • Loading branch information
Alabastard-64 committed Jan 21, 2023
1 parent ec686da commit c14127c
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 166 deletions.
17 changes: 10 additions & 7 deletions quantum/pointing_device/pointing_device_modes.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void set_pointing_mode(pointing_mode_t pointing_mode) {
// skip if same
if (!memcmp(&pointing_mode_context.mode, &pointing_mode, sizeof(pointing_mode_t))) return;
memcpy(&pointing_mode_context.mode, &pointing_mode, sizeof(pointing_mode_t));
dprintf("PM status saved!\n");
//dprintf("PM status saved!\n");
// Prevent zero divisor
if (!pointing_mode_context.mode.divisor) {
pointing_mode_context.mode.divisor = POINTING_DEFAULT_DIVISOR;
Expand All @@ -116,7 +116,7 @@ void set_pointing_mode_id(uint8_t mode_id) {
if (pointing_mode_context.mode.id != mode_id) {
pointing_mode_reset();
pointing_mode_context.mode.id = mode_id;
dprintf("PMID Set: %d\n", mode_id);
//dprintf("PMID Set: %d\n", mode_id);
}
}

Expand All @@ -130,10 +130,10 @@ void set_pointing_mode_id(uint8_t mode_id) {
void toggle_pointing_mode_id(uint8_t mode_id) {
if (pointing_mode_context.config.tg_mode_id == mode_id) {
pointing_mode_context.config.tg_mode_id = POINTING_MODE_DEFAULT;
dprintf("Toggled PMID: %d\n Off", mode_id);
//dprintf("Toggled PMID: %d\n Off", mode_id);
} else {
pointing_mode_context.config.tg_mode_id = mode_id;
dprintf("Toggled PMID: %d\n On", mode_id);
//dprintf("Toggled PMID: %d\n On", mode_id);
}
if (pointing_mode_context.mode.id != pointing_mode_context.config.tg_mode_id) pointing_mode_reset();
}
Expand Down Expand Up @@ -365,16 +365,19 @@ static report_mouse_t process_pointing_mode(pointing_mode_t pointing_mode, repor
# ifdef MOUSE_SCROLL_HIRES_ENABLE
{
uint8_t cur_divisor = pointing_mode.divisor;
uint8_t drag_multiplier = MAX(MOUSE_SCROLL_MULTIPLIER / cur_divisor, 1);
if (RESOLUTION_MULTIPLIER_H) {
if (IS_HIRES_H_ACTIVE) {
int16_t drag_multiplier = MOUSE_SCROLL_MULTIPLIER_H / cur_divisor;
if(!(drag_multiplier >> 1)) drag_multiplier |= 0x0001;
pointing_mode.x *= drag_multiplier;
pointing_mode.divisor = 1;
}
# endif
mouse_report.h = pointing_device_hv_clamp(pointing_mode.x / (int16_t)pointing_mode.divisor);
pointing_mode.x -= (int16_t)mouse_report.h * (int16_t)pointing_mode.divisor;
# ifdef MOUSE_SCROLL_HIRES_ENABLE
if (RESOLUTION_MULTIPLIER_V) {
if (IS_HIRES_V_ACTIVE) {
int16_t drag_multiplier = MOUSE_SCROLL_MULTIPLIER_V / cur_divisor;
if(!(drag_multiplier >> 1)) drag_multiplier |= 0x0001;
pointing_mode.y *= drag_multiplier;
pointing_mode.divisor = 1;
} else {
Expand Down
11 changes: 5 additions & 6 deletions tmk_core/protocol/chibios/usb_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ union {
#define HID_SET_REPORT 0x09
#define HID_SET_IDLE 0x0A
#define HID_SET_PROTOCOL 0x0B
#define HID_FEATURE_REPORT 0x03

/*
* Handles the GET_DESCRIPTOR callback
Expand Down Expand Up @@ -556,7 +555,7 @@ static void usb_event_cb(USBDriver *usbp, usbevent_t event) {
}
#ifdef MOUSE_SCROLL_HIRES_ENABLE
/* Reset multiplier on reset */
resolution_multiplier = 0;
resolution_multiplier_reset();
#endif
return;

Expand Down Expand Up @@ -615,7 +614,7 @@ static void set_led_transfer_cb(USBDriver *usbp) {
#ifdef MOUSE_SCROLL_HIRES_ENABLE
static void set_multiplier_cb(USBDriver *usbp) {
if (usbp->setup[6] == 2 && set_report_buf[0] == REPORT_ID_MULTIPLIER) {
resolution_multiplier = set_report_buf[1];
mouse_scroll_res_report.multiplier = set_report_buf[1];
}
}
#endif
Expand Down Expand Up @@ -657,7 +656,7 @@ static bool usb_request_hook_cb(USBDriver *usbp) {
return TRUE;
break;
case REPORT_ID_MULTIPLIER:
usbSetupTransfer(usbp, (uint8_t *)&resolution_multiplier, sizeof(resolution_multiplier), NULL);
usbSetupTransfer(usbp, (uint8_t *)&mouse_scroll_res_report, sizeof(report_mouse_scroll_res_t), NULL);
return TRUE;
break;
}
Expand All @@ -679,7 +678,7 @@ static bool usb_request_hook_cb(USBDriver *usbp) {
break;
# ifdef MOUSE_SCROLL_HIRES_ENABLE
case REPORT_ID_MULTIPLIER:
usbSetupTransfer(usbp, (uint8_t *)&resolution_multiplier, sizeof(resolution_multiplier), NULL);
usbSetupTransfer(usbp, (uint8_t *)&mouse_scroll_res_report, sizeof(report_mouse_scroll_res_t), NULL);
return TRUE;
break;
# endif
Expand Down Expand Up @@ -724,7 +723,7 @@ static bool usb_request_hook_cb(USBDriver *usbp) {
break;
#else
switch(usbp->setup[2]) {
case REPORT_ID_MOUSE:
case REPORT_ID_KEYBOARD:
usbSetupTransfer(usbp, set_report_buf, sizeof(set_report_buf), set_led_transfer_cb);
return TRUE;
break;
Expand Down
26 changes: 11 additions & 15 deletions tmk_core/protocol/lufa/lufa.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ static void Console_Task(void) {
*/
void EVENT_USB_Device_Connect(void) {
print("[C]");
#ifdef MOUSE_SCROLL_HIRES_ENABLE
/* Reset multiplier on reset */
resolution_multiplier_reset();
#endif
/* For battery powered device */
if (!USB_IsInitialized) {
USB_Disable();
Expand Down Expand Up @@ -292,10 +296,6 @@ void EVENT_USB_Device_Disconnect(void) {
void EVENT_USB_Device_Reset(void) {
print("[R]");
usb_device_state_set_reset();
#ifdef MOUSE_SCROLL_HIRES_ENABLE
/* Reset multiplier on reset */
resolution_multiplier = 0;
#endif
}

/** \brief Event USB Device Connect
Expand Down Expand Up @@ -460,8 +460,8 @@ void EVENT_USB_Device_ControlRequest(void) {
case MOUSE_INTERFACE:
# ifdef MOUSE_SCROLL_HIRES_ENABLE
if ((USB_ControlRequest.wValue & 0xff) == REPORT_ID_MULTIPLIER) {
ReportData = &resolution_multiplier;
ReportSize = sizeof(resolution_multiplier);
ReportData = (uint8_t *)&mouse_scroll_res_report;
ReportSize = sizeof(report_mouse_scroll_res_t);
break;
}
# endif
Expand All @@ -480,8 +480,8 @@ void EVENT_USB_Device_ControlRequest(void) {
# endif
# if defined(MOUSE_ENABLE) && defined(MOUSE_SCROLL_HIRES_ENABLE) && defined(MOUSE_SHARED_EP)
case REPORT_ID_MULTIPLIER:
ReportData = &resolution_multiplier;
ReportSize = sizeof(resolution_multiplier);
ReportData = (uint8_t *)&mouse_scroll_res_report;
ReportSize = sizeof(report_mouse_scroll_res_t);
break;
# endif
}
Expand Down Expand Up @@ -513,17 +513,16 @@ void EVENT_USB_Device_ControlRequest(void) {

uint8_t report_id = USB_ControlRequest.wValue & 0xFF;
// Discard report ID byte as we already have it from wValue
if (USB_ControlRequest.wLength == 2) {
Endpoint_Discard_8();
}
if(USB_ControlRequest.wLength > 1) Endpoint_Discard_8();
switch(report_id) {

case REPORT_ID_KEYBOARD:
case REPORT_ID_NKRO:
keyboard_led_state = Endpoint_Read_8();
break;
#ifdef MOUSE_SCROLL_HIRES_ENABLE
case REPORT_ID_MULTIPLIER:
resolution_multiplier = Endpoint_Read_8();
mouse_scroll_res_report.multiplier = Endpoint_Read_8();
break;
#endif
}
Expand Down Expand Up @@ -630,9 +629,6 @@ static void send_keyboard(report_keyboard_t *report) {
static void send_mouse(report_mouse_t *report) {
#ifdef MOUSE_ENABLE
send_report(MOUSE_IN_EPNUM, report, sizeof(report_mouse_t));
# ifdef MOUSE_HIRES_SCROLL_ENABLE
hires_scroll_reset();
# endif
#endif
}

Expand Down
36 changes: 29 additions & 7 deletions tmk_core/protocol/report.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ static int8_t cb_count = 0;
#endif

#ifdef MOUSE_SCROLL_HIRES_ENABLE
static uint8_t resolution_multiplier_stored = 0;
uint8_t resolution_multiplier = 0;
report_mouse_scroll_res_t mouse_scroll_res_report = {
.report_id = REPORT_ID_MULTIPLIER,
.multiplier = 0
};
#endif

/** \brief has_anykey
Expand Down Expand Up @@ -297,12 +299,32 @@ __attribute__((weak)) bool has_mouse_report_changed(report_mouse_t* new_report,
}

# ifdef MOUSE_SCROLL_HIRES_ENABLE
void hires_scroll_disable_next(uint8_t axis) {
resolution_multiplier_stored |= resolution_multiplier;
resolution_multiplier &= ~(axis);
bool set_hires_scroll_multiplier(uint8_t axis, uint8_t value) {
static uint8_t max_multiplier;
uint8_t multiplier_temp = mouse_scroll_res_report.multiplier;
max_multiplier |= multiplier_temp;

uint8_t set_value = (value / MOUSE_SCROLL_MULTIPLIER_RESOLUTION) & 0x0F;

switch(axis) {
case HIRES_V:
mouse_scroll_res_report.axis.v = set_value;
break;

case HIRES_H:
mouse_scroll_res_report.axis.h = set_value;
break;

case HIRES_BOTH:
mouse_scroll_res_report.axis.v = set_value;
mouse_scroll_res_report.axis.h = set_value;
}
mouse_scroll_res_report.multiplier &= max_multiplier;
return mouse_scroll_res_report.multiplier != multiplier_temp;
}
void hires_scroll_reset(void) {
resolution_multiplier |= resolution_multiplier_stored;

void resolution_multiplier_reset(void) {
mouse_scroll_res_report.multiplier = 0;
}
# endif
#endif // MOUSE_ENABLE
56 changes: 36 additions & 20 deletions tmk_core/protocol/report.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

// clang-format off

#ifdef MOUSE_SCROLL_HIRES_ENABLE
# ifndef MOUSE_SCROLL_MULTIPLIER
# define MOUSE_SCROLL_MULTIPLIER 120
# elif (MOUSE_SCROLL_MULTIPLIER > 120 || MOUSE_SCROLL_MULTIPLIER < 1)
# error "MOUSE_SCROLL_MULTIPLIER out of bounds must be in range of 1-120"
# endif
enum {
HIRES_V = 0x0F,
HIRES_H = 0xF0,
HIRES_BOTH = HIRES_V | HIRES_H,
};
#endif

/* HID report IDs */
enum hid_report_ids {
REPORT_ID_KEYBOARD = 1,
Expand Down Expand Up @@ -134,6 +121,12 @@ enum desktop_usages {
SYSTEM_DISPLAY_TOGGLE_INT_EXT = 0xB5
};

enum mouse_scroll_hires_axes {
HIRES_V,
HIRES_H,
HIRES_BOTH
};

// clang-format on

#define NKRO_SHARED_EP
Expand Down Expand Up @@ -227,11 +220,19 @@ typedef int16_t mouse_hv_report_t;
typedef int8_t mouse_hv_report_t;
#endif

#ifdef MOUSE_SCROLL_HIRES_ENABLE
extern uint8_t resolution_multiplier;
# define RESOLUTION_MULTIPLIER_V (resolution_multiplier & HIRES_V)
# define RESOLUTION_MULTIPLIER_H (resolution_multiplier & HIRES_H)
#endif
typedef union {
uint8_t raw[2];
struct {
uint8_t report_id;
union {
uint8_t multiplier;
struct {
uint8_t v : 4;
uint8_t h : 4;
} axis;
};
};
} __attribute__((packed)) report_mouse_scroll_res_t;

typedef struct {
#if defined(MOUSE_SHARED_EP) || defined(MOUSE_SCROLL_HIRES_ENABLE)
Expand Down Expand Up @@ -369,8 +370,23 @@ bool has_mouse_report_changed(report_mouse_t* new_report, report_mouse_t* old_re
#endif

#ifdef MOUSE_SCROLL_HIRES_ENABLE
void disable_hires_scroll_on_next(uint8_t axis);
void hires_scroll_reset(void);
extern report_mouse_scroll_res_t mouse_scroll_res_report;

bool set_hires_scroll_multiplier(uint8_t axis, uint8_t value);
void resolution_multiplier_reset(void);

# define MOUSE_SCROLL_MULTIPLIER_RESOLUTION (MOUSE_SCROLL_MULTIPLIER_MAX / 15)
# define IS_HIRES_V_ACTIVE (bool)(mouse_scroll_res_report.axis.v)
# define IS_HIRES_H_ACTIVE (bool)(mouse_scroll_res_report.axis.h)
# define MOUSE_SCROLL_MULTIPLIER_V (uint8_t)(mouse_scroll_res_report.axis.v * MOUSE_SCROLL_MULTIPLIER_RESOLUTION)
# define MOUSE_SCROLL_MULTIPLIER_H (uint8_t)(mouse_scroll_res_report.axis.h * MOUSE_SCROLL_MULTIPLIER_RESOLUTION)
# define MOUSE_SCROLL_MULTIPLIER_FULL (mouse_scroll_res_report.multiplier)

# ifndef MOUSE_SCROLL_MULTIPLIER_MAX
# define MOUSE_SCROLL_MULTIPLIER_MAX 120
# elif (MOUSE_SCROLL_MULTIPLIER_MAX > 120 || MOUSE_SCROLL_MULTIPLIER_MAX < 1)
# error "MOUSE_SCROLL_MULTIPLIER_MAX out of bounds must be in range of 1-120"
# endif
#endif

#ifdef __cplusplus
Expand Down
33 changes: 16 additions & 17 deletions tmk_core/protocol/usb_descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,28 +153,27 @@ const USB_Descriptor_HIDReport_Datatype_t PROGMEM SharedReport[] = {
# endif
HID_RI_INPUT(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_RELATIVE),

// Vertical/Horizontal Wheel (2 or 4 bytes)
// Vertical/Horizontal Wheel (2 - 5 bytes)
// Vertical wheel (1 - 2.5 bytes)
# ifdef MOUSE_SCROLL_HIRES_ENABLE
HID_RI_COLLECTION(8, 0x02), // Logical collection
// Resolution Multiplier (2 bits)
// Resolution Multiplier (4 bits)
HID_RI_REPORT_ID(8, REPORT_ID_MULTIPLIER),
HID_RI_USAGE(8, 0x48), // Resolution Multiplier
HID_RI_REPORT_COUNT(8, 0x01),
HID_RI_REPORT_SIZE(8, 0x04),
HID_RI_LOGICAL_MINIMUM(8, 0x00),
HID_RI_LOGICAL_MAXIMUM(8, 0x0F),
HID_RI_PHYSICAL_MINIMUM(8, 0x01), // Min 1
HID_RI_PHYSICAL_MAXIMUM(8, MOUSE_SCROLL_MULTIPLIER), // Max 120
HID_RI_PHYSICAL_MAXIMUM(8, MOUSE_SCROLL_MULTIPLIER_MAX), // Max 120
HID_RI_FEATURE(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
HID_RI_PHYSICAL_MINIMUM(8, 0x00), // Reset Global Value
HID_RI_PHYSICAL_MAXIMUM(8, 0x00),

HID_RI_REPORT_ID(8, REPORT_ID_MOUSE),
# endif // MOUSE_SCROLL_HIRES_ENABLE
// Vertical wheel (1-2 bytes)

HID_RI_USAGE(8, 0x38), // Wheel (V)
# ifdef MOUSE_SCROLL_HIRES_ENABLE
HID_RI_PHYSICAL_MINIMUM(8, 0x00), // Reset Global Value
HID_RI_PHYSICAL_MAXIMUM(8, 0x00),
# endif
# ifdef MOUSE_SCROLL_EXTENDED_REPORT
HID_RI_LOGICAL_MINIMUM(16, -32767),
HID_RI_LOGICAL_MAXIMUM(16, 32767),
Expand All @@ -190,28 +189,28 @@ const USB_Descriptor_HIDReport_Datatype_t PROGMEM SharedReport[] = {
# ifdef MOUSE_SCROLL_HIRES_ENABLE
HID_RI_END_COLLECTION(0), // Logical
# endif
// Horizontal Wheel (1 or 2 bytes)
// Horizontal Wheel (1 - 2.5 bytes)
# ifdef MOUSE_SCROLL_HIRES_ENABLE
HID_RI_COLLECTION(8, 0x02), // Logical collection
// Resolution Multiplier (2 bits)
// Resolution Multiplier (4 bits)
HID_RI_REPORT_ID(8, REPORT_ID_MULTIPLIER),
//HID_RI_USAGE_PAGE(8, 0x01), // Generic Desktop
HID_RI_USAGE(8, 0x48), // Resolution Multiplier
HID_RI_REPORT_COUNT(8, 0x01),
HID_RI_REPORT_SIZE(8, 0x04),
HID_RI_LOGICAL_MINIMUM(8, 0x00),
HID_RI_LOGICAL_MAXIMUM(8, 0x0F),
HID_RI_LOGICAL_MINIMUM(8, 0x00), // Min 0
HID_RI_LOGICAL_MAXIMUM(8, 0x0F), // Max 15
HID_RI_PHYSICAL_MINIMUM(8, 0x01), // Min 1
HID_RI_PHYSICAL_MAXIMUM(8, MOUSE_SCROLL_MULTIPLIER), // Max 120
HID_RI_PHYSICAL_MAXIMUM(8, MOUSE_SCROLL_MULTIPLIER_MAX), // Max 120
HID_RI_FEATURE(8, HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
HID_RI_PHYSICAL_MINIMUM(8, 0x00), // Reset Global Value
HID_RI_PHYSICAL_MAXIMUM(8, 0x00),

HID_RI_REPORT_ID(8, REPORT_ID_MOUSE),
# endif // MOUSE_SCROLL_HIRES_ENABLE
HID_RI_USAGE_PAGE(8, 0x0C), // Consumer
HID_RI_USAGE(16, 0x0238), // AC Pan (Horizontal Wheel)
# ifdef MOUSE_SCROLL_HIRES_ENABLE
HID_RI_PHYSICAL_MINIMUM(8, 0x00), // Reset Global Value
HID_RI_PHYSICAL_MAXIMUM(8, 0x00),
# endif

# ifdef MOUSE_SCROLL_EXTENDED_REPORT
HID_RI_LOGICAL_MINIMUM(16, -32767),
HID_RI_LOGICAL_MAXIMUM(16, 32767),
Expand Down
Loading

0 comments on commit c14127c

Please sign in to comment.