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

Update harpoond.c #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 60 additions & 33 deletions harpoond.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,39 @@
#define WIRED_COMMAND_PREFIX 0x08
#define DONGLE_COMMAND_PREFIX 0x09

/* Set custom configuration */
/* Main LED */
#define M_RED 0x1e
#define M_GREEN 0x00
#define M_BLUE 0x4c

/* Indicator LED */
#define I_RED 0x1e
#define I_GREEN 0x00
#define I_BLUE 0x4c

/* DPI, (708 hex = 1800) */
#define DPI 0x08, 0x70

/*
* Note that values are encoded in little-endian system, meaning the least
* significant portion (bytes) of a number comes first. For example, if you
* want to set 3200 DPI, first convert it to hexadecimal, which is 0xC80,
* then split it in half and reverse the order: 0x00, 0xC8, finally pass it
* to the function call above.
* More examples:
* 3000 DPI -> 0xBB8 -> 0xBB, 0x08 -> 0x08, 0xBB
* 10000 DPI -> 0x2710 -> 0x27, 0x10 -> 0x10, 0x27
*
* Once you set a new DPI value and recompile harpoond, please stop it, turn
* the mouse off and on then start harpoond again.
*
* Also, note that the mouse is kind of picky with these values, some values
* are completely ignored. I couldn't figure out any pattern here, so you
* need to spend some time on trial & error until you get a value that works
* and is close to what you want.
*/

static volatile short RUNNING = 1;
static struct timeval zero_tv = {0};

Expand Down Expand Up @@ -112,38 +145,13 @@ static void init_device(Device *device)
transfer(device, 5, 0x09, 0x01, 0x03, 0x00, 0x02);
transfer(device, 4, device->command_prefix, 0x0d, 0x00, 0x01);

/* Set custom configuration */
transfer(device, 13, device->command_prefix,
0x06, 0x00, 0x06, 0x00, 0x00, 0x00, /* Do not change */
0x00, /* Indicator LED's red */
0x00, /* Main LED's red */
0xff, /* Indicator LED's green */
0x00, /* Main LED's green */
0x00, /* Indicator LED's blue */
0x00); /* Main LED's blue */
I_RED, M_RED, I_GREEN, M_GREEN, I_BLUE, M_BLUE);

transfer(device, 6, device->command_prefix,
0x01, 0x20, 0x00, /* Do not change */
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "Do not change" comment can be removed now, also DPI can be moved to this line.

0x08, 0x70); /* DPI, (708 hex = 1800) */

/*
* Note that values are encoded in little-endian system, meaning the least
* significant portion (bytes) of a number comes first. For example, if you
* want to set 3200 DPI, first convert it to hexadecimal, which is 0xC80,
* then split it in half and reverse the order: 0x00, 0xC8, finally pass it
* to the function call above.
* More examples:
* 3000 DPI -> 0xBB8 -> 0xBB, 0x08 -> 0x08, 0xBB
* 10000 DPI -> 0x2710 -> 0x27, 0x10 -> 0x10, 0x27
*
* Once you set a new DPI value and recompile harpoond, please stop it, turn
* the mouse off and on then start harpoond again.
*
* Also, note that the mouse is kind of picky with these values, some values
* are completely ignored. I couldn't figure out any pattern here, so you
* need to spend some time on trial & error until you get a value that works
* and is close to what you want.
*/
DPI);

ungrab_device(device);

Expand Down Expand Up @@ -211,6 +219,16 @@ static void keep_alive(Device *device)
ungrab_device(device);
}

int handle_events(int r)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This r parameter seems unnecessary, its value is never read.

{
r = libusb_handle_events_timeout_completed(NULL, &zero_tv, NULL);
if (r < 0)
fprintf(stderr, "libusb failed to handle events: %s\n", libusb_error_name(r));
sleep(2);

return r;
}

int main()
{
int r;
Expand Down Expand Up @@ -275,12 +293,21 @@ int main()
/*
* KEEP ALIVE AND EVENT LOOP
*/
while (RUNNING) {
keep_alive(&device);
r = libusb_handle_events_timeout_completed(NULL, &zero_tv, NULL);
if (r < 0)
fprintf(stderr, "libusb failed to handle events: %s\n", libusb_error_name(r));
sleep(2);
if(RUNNING) {
// Force applying new config
while (!device.initialized) {
keep_alive(&device);
r = handle_events(r);
}

for (int i = 0; RUNNING; ++i) {
/* Send keep_alive signal every WAIT_TIME s, without blocking whole function. */
if(i >= WAIT_TIME / 2) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just set WAIT_TIME as 25 or 30 seconds?

Copy link
Author

@ChaosInfinited ChaosInfinited May 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that due to losing packets or some race condition, I tried using 30 but it just would reset after a minute, instead of persisting current settings
25 was the longest, stable interval I found

keep_alive(&device);
i = 0;
}
r = handle_events(r);
}
}

/*
Expand Down