aboutsummaryrefslogtreecommitdiffstats
path: root/tmk_core/common/mousekey.c
blob: 23469476e264fd8bacdc56682dcf2e23c5a84c2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
Copyright 2011 Jun Wako <wakojun@gmail.com>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdint.h>
#include "keycode.h"
#include "host.h"
#include "timer.h"
#include "print.h"
#include "debug.h"
#include "mousekey.h"



static report_mouse_t mouse_report = {};
static uint8_t mousekey_repeat =  0;
static uint8_t mousekey_accel = 0;

static void mousekey_debug(void);


/*
 * Mouse keys  acceleration algorithm
 *  http://en.wikipedia.org/wiki/Mouse_keys
 *
 *  speed = delta * max_speed * (repeat / time_to_max)**((1000+curve)/1000)
 */
/* milliseconds between the initial key press and first repeated motion event (0-2550) */
uint8_t mk_delay = MOUSEKEY_DELAY/10;
/* milliseconds between repeated motion events (0-255) */
uint8_t mk_interval = MOUSEKEY_INTERVAL;
/* steady speed (in action_delta units) applied each event (0-255) */
uint8_t mk_max_speed = MOUSEKEY_MAX_SPEED;
/* number of events (count) accelerating to steady speed (0-255) */
uint8_t mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
/* ramp used to reach maximum pointer speed (NOT SUPPORTED) */
//int8_t mk_curve = 0;
/* wheel params */
uint8_t mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED;
uint8_t mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;


static uint16_t last_timer = 0;


static uint8_t move_unit(void)
{
    uint16_t unit;
    if (mousekey_accel & (1<<0)) {
        unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed)/4;
    } else if (mousekey_accel & (1<<1)) {
        unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed)/2;
    } else if (mousekey_accel & (1<<2)) {
        unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed);
    } else if (mousekey_repeat == 0) {
        unit = MOUSEKEY_MOVE_DELTA;
    } else if (mousekey_repeat >= mk_time_to_max) {
        unit = MOUSEKEY_MOVE_DELTA * mk_max_speed;
    } else {
        unit = (MOUSEKEY_MOVE_DELTA * mk_max_speed * mousekey_repeat) / mk_time_to_max;
    }
    return (unit > MOUSEKEY_MOVE_MAX ? MOUSEKEY_MOVE_MAX : (unit == 0 ? 1 : unit));
}

static uint8_t wheel_unit(void)
{
    uint16_t unit;
    if (mousekey_accel & (1<<0)) {
        unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed)/4;
    } else if (mousekey_accel & (1<<1)) {
        unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed)/2;
    } else if (mousekey_accel & (1<<2)) {
        unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed);
    } else if (mousekey_repeat == 0) {
        unit = MOUSEKEY_WHEEL_DELTA;
    } else if (mousekey_repeat >= mk_wheel_time_to_max) {
        unit = MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed;
    } else {
        unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed * mousekey_repeat) / mk_wheel_time_to_max;
    }
    return (unit > MOUSEKEY_WHEEL_MAX ? MOUSEKEY_WHEEL_MAX : (unit == 0 ? 1 : unit));
}

void mousekey_task(void)
{
    if (timer_elapsed(last_timer) < (mousekey_repeat ? mk_interval : mk_delay*10))
        return;

    if (mouse_report.x == 0 && mouse_report.y == 0 && mouse_report.v == 0 && mouse_report.h == 0)
        return;

    if (mousekey_repeat != UINT8_MAX)
        mousekey_repeat++;


    if (mouse_report.x > 0) mouse_report.x = move_unit();
    if (mouse_report.x < 0) mouse_report.x = move_unit() * -1;
    if (mouse_report.y > 0) mouse_report.y = move_unit();
    if (mouse_report.y < 0) mouse_report.y = move_unit() * -1;

    /* diagonal move [1/sqrt(2) = 0.7] */
    if (mouse_report.x && mouse_report.y) {
        mouse_report.x *= 0.7;
        mouse_report.y *= 0.7;
    }

    if (mouse_report.v > 0) mouse_report.v = wheel_unit();
    if (mouse_report.v < 0) mouse_report.v = wheel_unit() * -1;
    if (mouse_report.h > 0) mouse_report.h = wheel_unit();
    if (mouse_report.h < 0) mouse_report.h = wheel_unit() * -1;

    mousekey_send();
}

void mousekey_on(uint8_t code)
{
    if      (code == KC_MS_UP)       mouse_report.y = move_unit() * -1;
    else if (code == KC_MS_DOWN)     mouse_report.y = move_unit();
    else if (code == KC_MS_LEFT)     mouse_report.x = move_unit() * -1;
    else if (code == KC_MS_RIGHT)    mouse_report.x = move_unit();
    else if (code == KC_MS_WH_UP)    mouse_report.v = wheel_unit();
    else if (code == KC_MS_WH_DOWN)  mouse_report.v = wheel_unit() * -1;
    else if (code == KC_MS_WH_LEFT)  mouse_report.h = wheel_unit() * -1;
    else if (code == KC_MS_WH_RIGHT) mouse_report.h = wheel_unit();
    else if (code == KC_MS_BTN1)     mouse_report.buttons |= MOUSE_BTN1;
    else if (code == KC_MS_BTN2)     mouse_report.buttons |= MOUSE_BTN2;
    else if (code == KC_MS_BTN3)     mouse_report.buttons |= MOUSE_BTN3;
    else if (code == KC_MS_BTN4)     mouse_report.buttons |= MOUSE_BTN4;
    else if (code == KC_MS_BTN5)     mouse_report.buttons |= MOUSE_BTN5;
    else if (code == KC_MS_ACCEL0)   mousekey_accel |= (1<<0);
    else if (code == KC_MS_ACCEL1)   mousekey_accel |= (1<<1);
    else if (code == KC_MS_ACCEL2)   mousekey_accel |= (1<<2);
}

void mousekey_off(uint8_t code)
{
    if      (code == KC_MS_UP       && mouse_report.y < 0) mouse_report.y = 0;
    else if (code == KC_MS_DOWN     && mouse_report.y > 0) mouse_report.y = 0;
    else if (code == KC_MS_LEFT     && mouse_report.x < 0) mouse_report.x = 0;
    else if (code == KC_MS_RIGHT    && mouse_report.x > 0) mouse_report.x = 0;
    else if (code == KC_MS_WH_UP    && mouse_report.v > 0) mouse_report.v = 0;
    else if (code == KC_MS_WH_DOWN  && mouse_report.v < 0) mouse_report.v = 0;
    else if (code == KC_MS_WH_LEFT  && mouse_report.h < 0) mouse_report.h = 0;
    else if (code == KC_MS_WH_RIGHT && mouse_report.h > 0) mouse_report.h = 0;
    else if (code == KC_MS_BTN1) mouse_report.buttons &= ~MOUSE_BTN1;
    else if (code == KC_MS_BTN2) mouse_report.buttons &= ~MOUSE_BTN2;
    else if (code == KC_MS_BTN3) mouse_report.buttons &= ~MOUSE_BTN3;
    else if (code == KC_MS_BTN4) mouse_report.buttons &= ~MOUSE_BTN4;
    else if (code == KC_MS_BTN5) mouse_report.buttons &= ~MOUSE_BTN5;
    else if (code == KC_MS_ACCEL0) mousekey_accel &= ~(1<<0);
    else if (code == KC_MS_ACCEL1) mousekey_accel &= ~(1<<1);
    else if (code == KC_MS_ACCEL2) mousekey_accel &= ~(1<<2);

    if (mouse_report.x == 0 && mouse_report.y == 0 && mouse_report.v == 0 && mouse_report.h == 0)
        mousekey_repeat = 0;
}

void mousekey_send(void)
{
    mousekey_debug();
    host_mouse_send(&mouse_report);
    last_timer = timer_read();
}

void mousekey_clear(void)
{
    mouse_report = (report_mouse_t){};
    mousekey_repeat = 0;
    mousekey_accel = 0;
}

static void mousekey_debug(void)
{
    if (!debug_mouse) return;
    print("mousekey [btn|x y v h](rep/acl): [");
    phex(mouse_report.buttons); print("|");
    print_decs(mouse_report.x); print(" ");
    print_decs(mouse_report.y); print(" ");
    print_decs(mouse_report.v); print(" ");
    print_decs(mouse_report.h); print("](");
    print_dec(mousekey_repeat); print("/");
    print_dec(mousekey_accel); print(")\n");
}
="p">].next, struct pfn_info, list); list_del(&pg->list); /* We may have to halve the chunk a number of times. */ while ( i != order ) { PFN_ORDER(pg) = --i; list_add_tail(&pg->list, &heap[zone][i]); pg += 1 << i; } map_alloc(page_to_pfn(pg), 1 << order); avail[zone] -= 1 << order; spin_unlock(&heap_lock); return pg; no_memory: spin_unlock(&heap_lock); return NULL; } /* Free 2^@order set of pages. */ void free_heap_pages(int zone, struct pfn_info *pg, int order) { unsigned long mask; spin_lock(&heap_lock); map_free(page_to_pfn(pg), 1 << order); avail[zone] += 1 << order; /* Merge chunks as far as possible. */ while ( order < MAX_ORDER ) { mask = 1 << order; if ( (page_to_pfn(pg) & mask) ) { /* Merge with predecessor block? */ if ( allocated_in_map(page_to_pfn(pg)-mask) || (PFN_ORDER(pg-mask) != order) ) break; list_del(&(pg-mask)->list); pg -= mask; } else { /* Merge with successor block? */ if ( allocated_in_map(page_to_pfn(pg)+mask) || (PFN_ORDER(pg+mask) != order) ) break; list_del(&(pg+mask)->list); } order++; } PFN_ORDER(pg) = order; list_add_tail(&pg->list, &heap[zone][order]); spin_unlock(&heap_lock); } /* * Scrub all unallocated pages in all heap zones. This function is more * convoluted than appears necessary because we do not want to continuously * hold the lock or disable interrupts while scrubbing very large memory areas. */ void scrub_heap_pages(void) { void *p; unsigned long pfn, flags; for ( pfn = 0; pfn < (bitmap_size * 8); pfn++ ) { /* Quick lock-free check. */ if ( allocated_in_map(pfn) ) continue; spin_lock_irqsave(&heap_lock, flags); /* Re-check page status with lock held. */ if ( !allocated_in_map(pfn) ) { p = map_domain_mem(pfn << PAGE_SHIFT); clear_page(p); unmap_domain_mem(p); } spin_unlock_irqrestore(&heap_lock, flags); } } /************************* * XEN-HEAP SUB-ALLOCATOR */ void init_xenheap_pages(unsigned long ps, unsigned long pe) { unsigned long flags; ps = round_pgup(ps); pe = round_pgdown(pe); memguard_guard_range(__va(ps), pe - ps); local_irq_save(flags); init_heap_pages(MEMZONE_XEN, phys_to_page(ps), (pe - ps) >> PAGE_SHIFT); local_irq_restore(flags); } unsigned long alloc_xenheap_pages(int order) { unsigned long flags; struct pfn_info *pg; int i, attempts = 0; retry: local_irq_save(flags); pg = alloc_heap_pages(MEMZONE_XEN, order); local_irq_restore(flags); if ( unlikely(pg == NULL) ) goto no_memory; memguard_unguard_range(page_to_virt(pg), 1 << (order + PAGE_SHIFT)); for ( i = 0; i < (1 << order); i++ ) { pg[i].count_info = 0; pg[i].u.inuse.domain = NULL; pg[i].u.inuse.type_info = 0; } return (unsigned long)page_to_virt(pg); no_memory: if ( attempts++ < 8 ) { xmem_cache_reap(); goto retry; } printk("Cannot handle page request order %d!\n", order); dump_slabinfo(); return 0; } void free_xenheap_pages(unsigned long p, int order) { unsigned long flags; memguard_guard_range((void *)p, 1 << (order + PAGE_SHIFT)); local_irq_save(flags); free_heap_pages(MEMZONE_XEN, virt_to_page(p), order); local_irq_restore(flags); } /************************* * DOMAIN-HEAP SUB-ALLOCATOR */ void init_domheap_pages(unsigned long ps, unsigned long pe) { ASSERT(!in_irq()); ps = round_pgup(ps); pe = round_pgdown(pe); init_heap_pages(MEMZONE_DOM, phys_to_page(ps), (pe - ps) >> PAGE_SHIFT); } struct pfn_info *alloc_domheap_pages(struct domain *d, int order) { struct pfn_info *pg; unsigned long mask, flushed_mask, pfn_stamp, cpu_stamp; int i, j; ASSERT(!in_irq()); if ( unlikely((pg = alloc_heap_pages(MEMZONE_DOM, order)) == NULL) ) return NULL; flushed_mask = 0; for ( i = 0; i < (1 << order); i++ ) { if ( (mask = (pg[i].u.free.cpu_mask & ~flushed_mask)) != 0 ) { pfn_stamp = pg[i].tlbflush_timestamp; for ( j = 0; (mask != 0) && (j < smp_num_cpus); j++ ) { if ( mask & (1<<j) ) { cpu_stamp = tlbflush_time[j]; if ( !NEED_FLUSH(cpu_stamp, pfn_stamp) ) mask &= ~(1<<j); } } if ( unlikely(mask != 0) ) { flush_tlb_mask(mask); perfc_incrc(need_flush_tlb_flush); flushed_mask |= mask; } } pg[i].count_info = 0; pg[i].u.inuse.domain = NULL; pg[i].u.inuse.type_info = 0; } if ( d == NULL ) return pg; spin_lock(&d->page_alloc_lock); if ( unlikely(test_bit(DF_DYING, &d->flags)) || unlikely((d->tot_pages + (1 << order)) > d->max_pages) ) { DPRINTK("Over-allocation for domain %u: %u > %u\n", d->id, d->tot_pages + (1 << order), d->max_pages); DPRINTK("...or the domain is dying (%d)\n", !!test_bit(DF_DYING, &d->flags)); spin_unlock(&d->page_alloc_lock); free_heap_pages(MEMZONE_DOM, pg, order); return NULL; } if ( unlikely(d->tot_pages == 0) ) get_knownalive_domain(d); d->tot_pages += 1 << order; for ( i = 0; i < (1 << order); i++ ) { pg[i].u.inuse.domain = d; wmb(); /* Domain pointer must be visible before updating refcnt. */ pg[i].count_info |= PGC_allocated | 1; list_add_tail(&pg[i].list, &d->page_list); } spin_unlock(&d->page_alloc_lock); return pg; } void free_domheap_pages(struct pfn_info *pg, int order) { int i, drop_dom_ref; struct domain *d = pg->u.inuse.domain; void *p; ASSERT(!in_irq()); if ( unlikely(IS_XEN_HEAP_FRAME(pg)) ) { /* NB. May recursively lock from domain_relinquish_memory(). */ spin_lock_recursive(&d->page_alloc_lock); for ( i = 0; i < (1 << order); i++ ) list_del(&pg[i].list); d->xenheap_pages -= 1 << order; drop_dom_ref = (d->xenheap_pages == 0); spin_unlock_recursive(&d->page_alloc_lock); } else if ( likely(d != NULL) ) { /* NB. May recursively lock from domain_relinquish_memory(). */ spin_lock_recursive(&d->page_alloc_lock); for ( i = 0; i < (1 << order); i++ ) { ASSERT((pg[i].u.inuse.type_info & PGT_count_mask) == 0); pg[i].tlbflush_timestamp = tlbflush_current_time(); pg[i].u.free.cpu_mask = 1 << d->processor; list_del(&pg[i].list); /* * Normally we expect a domain to clear pages before freeing them, * if it cares about the secrecy of their contents. However, after * a domain has died we assume responsibility for erasure. */ if ( unlikely(test_bit(DF_DYING, &d->flags)) ) { p = map_domain_mem(page_to_phys(&pg[i])); clear_page(p); unmap_domain_mem(p); } } d->tot_pages -= 1 << order; drop_dom_ref = (d->tot_pages == 0); spin_unlock_recursive(&d->page_alloc_lock); free_heap_pages(MEMZONE_DOM, pg, order); } else { /* Freeing an anonymous domain-heap page. */ free_heap_pages(MEMZONE_DOM, pg, order); drop_dom_ref = 0; } if ( drop_dom_ref ) put_domain(d); } unsigned long avail_domheap_pages(void) { return avail[MEMZONE_DOM]; }