aboutsummaryrefslogtreecommitdiff
path: root/src/kernel/input/ps2/mouse.c
blob: 97ae25600a367ab2a3f8971ecfb3db9b7a939b57 (plain) (blame)
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
#include <kernel/interrupts/interrupts.h>
#include <kernel/io/io.h>
#include <kernel/graphics/vesa.h>

char mouse_cycle = 0;
char mouse_byte[3];
int mouse_x = 0;
int mouse_y = 0;
int mouse_but_1 = 0;
int mouse_but_2 = 0;
int mouse_but_3 = 0;

void mouse_handler(struct regs *a_r)
{
    switch (mouse_cycle) {
        case 0:
            mouse_byte[0] = inb(0x60);
            if (((mouse_byte[0] >> 3) & 1) == 1) mouse_cycle++;
            else mouse_cycle = 0;
            break;
        case 1:
            mouse_byte[1] = inb(0x60);
            mouse_cycle++;
            break;
        case 2:
            mouse_byte[2] = inb(0x60);
            mouse_x += mouse_byte[1];
            mouse_y -= mouse_byte[2];
            mouse_but_1 = mouse_byte[0] & 1;
            mouse_but_2 = (mouse_byte[0] >> 1) & 1;
            mouse_but_3 = (mouse_byte[0] >> 2) & 1;
            mouse_cycle = 0;

            if (mouse_x < 0) mouse_x = 0;
            if (mouse_y < 0) mouse_y = 0;
            if (mouse_x > vbe_width - 1) mouse_x = vbe_width - 1;
            if (mouse_y > vbe_height - 1) mouse_y = vbe_height - 1;
            vesa_draw_cursor(mouse_x, mouse_y);
            break;
        default:
            break;
    }
}

void mouse_wait(unsigned char a_type)
{
    unsigned int time_out = 100000;
    if (a_type == 0) {
        while (time_out--)
            if ((inb(0x64) & 1) == 1)
                return;
        return;
    } else {
        while (time_out--)
            if ((inb(0x64) & 2) == 0)
                return;
        return;
    }
}

void mouse_write(unsigned char a_write)
{
    mouse_wait(1);
    outb(0x64, 0xD4);
    mouse_wait(1);
    outb(0x60, a_write);
}

char mouse_read()
{
    mouse_wait(0);
    return inb(0x60);
}

void mouse_install()
{
    unsigned char status;

    // Enable auxiliary mouse device
    mouse_wait(1);
    outb(0x64, 0xA8);

    // Enable interrupts
    mouse_wait(1);
    outb(0x64, 0x20);
    mouse_wait(0);
    status = (inb(0x60) | 2);
    mouse_wait(1);
    outb(0x64, 0x60);
    mouse_wait(1);
    outb(0x60, status);

    // Use default settings
    mouse_write(0xF6);
    mouse_read();

    // Enable mouse
    mouse_write(0xF4);
    mouse_read();

    // Setup the mouse handler
    irq_install_handler(12, mouse_handler);
    info("Installed mouse handler");
}