Skip to content

Commit edd2e46

Browse files
committed
Perform movement calculations using floats
Using floats made dividing the speed by a factor much easier to calculate. Also, as I've used ptrkeys as my primary pointing mechanism the importance of being able to use it single-handedly has been made obvious to me, and the changes to the bindings reflect this.
1 parent 7e85aab commit edd2e46

File tree

5 files changed

+53
-50
lines changed

5 files changed

+53
-50
lines changed

command.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ void move2scroll(const Arg *enable); // enable is treated as a boolean.
4141
void togglem2s(const Arg *ignored);
4242
void scrollstart(const Arg *dir);
4343
void scrollstop(const Arg *dir);
44-
// n is an unsigned int.
45-
void multiplyspeed(const Arg *uint);
46-
void dividespeed(const Arg *uint);
44+
// factor is a float.
45+
void multiplyspeed(const Arg *factor);
46+
void dividespeed(const Arg *factor);
4747

4848
// Clicking:
4949
// btn can be any value from enum Mouse.

config.def.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// "Frames" or updates per second.
22
#define FPS 60
33
// Pixels per second.
4-
#define BASE_SPEED 150
4+
#define BASE_SPEED 1000.0
55
// Events per second.
66
#define BASE_SCROLL 10
77

@@ -29,12 +29,12 @@ static Key keys[] = {
2929
// Scrolling
3030
{0, XK_Shift_L, 0, move2scroll, {.i=1}, move2scroll, {.i=0}},
3131
{0, XK_f, 0, togglem2s, {0}, NULL, {0}},
32-
// Accelerate using the right hand.
33-
{0, XK_j, 0, multiplyspeed, {.i=2}, dividespeed, {.i=2}},
34-
{0, XK_k, 0, multiplyspeed, {.i=4}, dividespeed, {.i=4}},
35-
{0, XK_Alt_L, 0, multiplyspeed, {.i=6}, dividespeed, {.i=6}},
36-
{0, XK_l, 0, multiplyspeed, {.i=8}, dividespeed, {.i=8}},
37-
{0, XK_semicolon, 0, multiplyspeed, {.i=64}, dividespeed, {.i=64}},
32+
// Speed multiply/divide.
33+
{0, XK_Alt_L, 0, dividespeed, {.f=8}, multiplyspeed, {.f=8}},
34+
{0, XK_j, 0, dividespeed, {.f=8}, multiplyspeed, {.f=8}},
35+
{0, XK_k, 0, dividespeed, {.f=2}, multiplyspeed, {.f=2}},
36+
{0, XK_l, 0, multiplyspeed, {.f=4}, dividespeed, {.f=4}},
37+
{0, XK_semicolon, 0, multiplyspeed, {.f=8}, dividespeed, {.f=8}},
3838
// Left-handed clicking.
3939
{0, XK_e, 0, clickpress, {.ui=BTNRIGHT}, clickrelease, {.ui=BTNRIGHT}},
4040
{0, XK_r, 0, clickpress, {.ui=BTNMIDDLE}, clickrelease, {.ui=BTNMIDDLE}},

debug.h

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
void
2+
printmovement()
3+
{
4+
Movement *p = &mvptr;
5+
tracef("ptr: base=%.3g dir=%u mul=%.3g xc=%d yc=%d",
6+
p->basespeed, p->dir, p->mul, p->xcont, p->ycont);
7+
Movement *s = &mvscroll;
8+
tracef("scroll: base=%.3g dir=%u mul=%.3g xc=%d yc=%d",
9+
s->basespeed, s->dir, s->mul, s->xcont, s->ycont);
10+
}

pk.c

+29-36
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <assert.h>
22
#include <errno.h>
3+
#include <math.h>
34
#include <stdlib.h>
45
#include <string.h>
56
#include <time.h>
@@ -198,42 +199,36 @@ static void
198199
requestpointermovement(Movement *m, int usec)
199200
{
200201
if (!m->dir) return;
201-
// -1, 0, 1
202-
int xsign = ((m->dir & RIGHT) ? 1 : 0) - ((m->dir & LEFT) ? 1 : 0);
203-
int ysign = ((m->dir & UP) ? 1 : 0) - ((m->dir & DOWN) ? 1 : 0);
204-
// x, y units per second
205-
int xps = m->basespeed * xsign * m->mul * usec + m->xrem;
206-
int yps = m->basespeed * ysign * m->mul * usec + m->yrem;
207-
int dx = xps / 1000000;
208-
int dy = - yps / 1000000;
209-
// Subunit remainders.
210-
m->xrem = xps % 1000000;
211-
m->yrem = yps % 1000000;
202+
// xsign and ysign can be one of -1, 0, 1.
203+
double xsign = ((m->dir & RIGHT) ? 1 : 0) - ((m->dir & LEFT) ? 1 : 0);
204+
double ysign = ((m->dir & UP) ? 1 : 0) - ((m->dir & DOWN) ? 1 : 0);
205+
double dx = m->basespeed * xsign * m->mul * usec / 1e6 + m->xrem;
206+
double dy = - m->basespeed * ysign * m->mul * usec / 1e6 + m->yrem;
207+
double dummy;
208+
m->xrem = modf(dx, &dummy);
209+
m->yrem = modf(dy, &dummy);
212210

213-
XWarpPointer(dpy, None, None, 0, 0, 0, 0, dx, dy);
211+
XWarpPointer(dpy, None, None, 0, 0, 0, 0, (int) dx, (int) dy);
214212
}
215213

216214
static void
217215
requestscrolling(Movement *m, int usec)
218216
{
219217
if (!m->dir) return;
220-
// -1, 0, 1
221-
int xsign = ((m->dir & RIGHT) ? 1 : 0) - ((m->dir & LEFT) ? 1 : 0);
222-
int ysign = ((m->dir & UP) ? 1 : 0) - ((m->dir & DOWN) ? 1 : 0);
223-
// x, y units per second
224-
int xps = m->basespeed * xsign * m->mul * usec + m->xrem;
225-
int yps = m->basespeed * ysign * m->mul * usec + m->yrem;
226-
int dx = xps / 1000000;
227-
int dy = - yps / 1000000;
228-
// Subunit remainders.
229-
m->xrem = xps % 1000000;
230-
m->yrem = yps % 1000000;
218+
// xsign and ysign can be one of -1, 0, 1.
219+
double xsign = ((m->dir & RIGHT) ? 1 : 0) - ((m->dir & LEFT) ? 1 : 0);
220+
double ysign = ((m->dir & UP) ? 1 : 0) - ((m->dir & DOWN) ? 1 : 0);
221+
double dx = m->basespeed * xsign * m->mul * usec / 1e6 + m->xrem;
222+
double dy = m->basespeed * ysign * m->mul * usec / 1e6 + m->yrem;
223+
double dummy;
224+
m->xrem = modf(dx, &dummy);
225+
m->yrem = modf(dy, &dummy);
231226

232227
unsigned int xbutton = (m->dir & LEFT) ? SCROLLLEFT : SCROLLRIGHT;
233-
unsigned int ybutton = (m->dir & UP) ? 4 : 5;
228+
unsigned int ybutton = (m->dir & UP) ? SCROLLUP : SCROLLDOWN;
234229

235-
int xevents = abs(dx);
236-
int yevents = abs(dy);
230+
int xevents = abs((int) dx);
231+
int yevents = abs((int) dy);
237232
// Scroll immediately after a scroll key is pressed, but adjust the
238233
// remainder so the configured number of scroll events occur in the first
239234
// second.
@@ -576,10 +571,8 @@ move2scroll(const Arg *enable)
576571
void
577572
togglem2s(const Arg *ignored)
578573
{
579-
printmovement();
580574
Arg arg = {.i=!ismove2scroll};
581575
move2scroll(&arg);
582-
printmovement();
583576
}
584577

585578
void
@@ -597,19 +590,19 @@ scrollstop(const Arg *dir)
597590
}
598591

599592
void
600-
multiplyspeed(const Arg *n)
593+
multiplyspeed(const Arg *factor)
601594
{
602-
if (!n) die("multiplyspeed: NULL arg");
603-
mvptr.mul += n->ui;
604-
mvscroll.mul += n->ui;
595+
if (!factor) die("multiplyspeed: NULL arg");
596+
mvptr.mul *= factor->f;
597+
mvscroll.mul *= factor->f;
605598
}
606599

607600
void
608-
dividespeed(const Arg *n)
601+
dividespeed(const Arg *factor)
609602
{
610-
if (!n) die("dividespeed: NULL arg");
611-
mvptr.mul -= n->ui;
612-
mvscroll.mul -= n->ui;
603+
if (!factor) die("dividespeed: NULL arg");
604+
mvptr.mul /= factor->f;
605+
mvscroll.mul /= factor->f;
613606
}
614607

615608
void

pk.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
#include <X11/Xlib.h>
55

66
typedef struct {
7-
unsigned int basespeed;
7+
double basespeed;
88
unsigned int dir; // Bits from enum Direction.
9-
unsigned int mul;
10-
int xrem, yrem; // Subunit remainders.
9+
double mul;
10+
double xrem, yrem; // Subunit remainders.
1111
int xcont, ycont; // Continuing a movement?
1212
} Movement;
1313

1414
typedef union {
1515
int i;
1616
unsigned int ui;
1717
unsigned long ul;
18-
float f;
18+
double f;
1919
const void *v;
2020
} Arg;
2121

0 commit comments

Comments
 (0)