Page 1 of 1

Accessing Mouse

Posted: Wed Feb 22, 2006 11:12 am
by Tolga
Hi. I saw some documents about mouse. But i don't understand. How can access to ps/2 mouse? Which port? or Which IRQ? (Protected Mode ;D )

Re:Accessing Mouse

Posted: Wed Feb 22, 2006 12:29 pm
by Kemp
Have you looked in the Wiki?

Though on a side-note, that page isn't linked from the front.

Re:Accessing Mouse

Posted: Wed Feb 22, 2006 7:11 pm
by GLneo
first get the mouse set up:

Code: Select all

void init_ps2_mouse()
{
    int x;
    unsigned char data_read;
    for(x = 0; x < 5; x++)
    {
        outport64(0xA7);                    //reset every thing
        outport64(0xA8);
        outport64(0xD4);
        outport60(0xF5);
        data_read = inport60();             //    did it 
        if (data_read != 0xFA) continue;    //    work ?
        outport64(0xD4);
        outport60(0xFF);
        data_read = inport60();             //    did it
        if (data_read != 0xFA) continue;    //    work ?
        data_read = inport60();             //    did the self-
        if (data_read != 0xAA) continue;    //    test work ?
        mouse_type = inport60();
        outport64(0xD4);
        outport60(0xE6);
        data_read = inport60();             //    did it
        if (data_read != 0xFA) continue;    //    work ?
/*
        outport64(0xD4);
        outport60(0xF3);
        data_read = inport60();             //    did it
        if (data_read != 0xFA) continue;    //    work ?
        outport60(210);
        data_read = inport60();             //    did it
        if (data_read != 0xFA) continue;    //    work ?
*/
        outport64(0x20);                    
        data_read = inport60();             //
        data_read |= 0x02;                  // 
        outport64(0x60);                    //    get it to report data
        outport60(data_read);               //
        outport64(0xD4);                    //
        outport60(0xF4);
        data_read = inport60();             //    did it
        if (data_read != 0xFA) continue;    //    work ?
        break;
    }
}
if you need out and in ports for 0x60 0x64 the are just functions to wait for the ports to clear, they should look like this:

Code: Select all

inline void outport60(unsigned char data)
{
    volatile unsigned char good = 0x02;
    while ((good & 0x02) != 0)
        good = inport(0x64);
    outport(0x60, data);
}

inline unsigned inport60()
{
    volatile unsigned char good = 0x00;
    while ((good & 0x01) == 0)
        good = inport(0x64);
    return inport(0x60);
}

inline void outport64(unsigned char data)
{
    volatile unsigned char good = 0x02;
    while ((good & 0x02) != 0)
        good = inport(0x64);
    outport(0x64, data);
}

inline unsigned inport64()
{
    return inport(0x64);
}
you may want to know what my init function does stuff(a lot of useful things you can change : mouse speed, resalution, ect...), then to read data set up your int handler:

Code: Select all

void set_mouse()
{
    init_ps2_mouse(); // code you saw erlyer
    irq_setter(12, ps2_mouse); set mouse code to IRQ 12
}
now every time you get an irq 12 three bytes are sent, the first is buttons and position, and the next two are all position, you can put it togeter like this:

Code: Select all

int x = 80, y = 80;
unsigned char data[3];
static volatile int button[3] = {0, 0, 0};

void ps2_mouse()
{
    data[0] = inport60();
    data[1] = inport60();
    data[2] = inport60();
    if ((data[0] & 0x01) != button[0])
    {
        button[0] ^= 1;
        if (button[0]) left_button_down(x, y);
        else left_button_up(x, y);
    }
    if ((data[0] & 0x04) != button[1])
    {
        button[1] ^= 1;
        if (button[1]) middle_button_down(x, y);
        else middle_button_up(x, y);
    }
    if ((data[0] & 0x02) != button[2])
    {
        button[2] ^= 1;
        if (button[2]) right_button_down(x, y);
        else right_button_up(x, y);
    }
    if (data[0] & 0x10)
        x += (int)((256 - data[1]) * -1);
    else
        x += (int)data[1];
    if (data[0] & 0x20)
        y += (int) (256 - data[2]);
    else
        y += (int)(data[2] * -1);
    if (y > 184) y = 184;
    else if (y < 0) y = 0;
    if (x > 311) x = 311;
    else if (x < 0) x = 0;
}
this code sets x and y to mouse positions(they start out at 80 , but you can change this, becouse motion in ps/2 mice is relitive, also this funtion keeps the mouse within 0<x<311 and 0<y<184, but you can change that) and fuctions are called if buttons are pushed:

Code: Select all

oid left_button_down(int x, int y)
{
}

void left_button_up(int x, int y)
{
}

void right_button_down(int x, int y)
{
}

void right_button_up(int x, int y)
{
}

void middle_button_down(int x, int y)
{
}

void middle_button_up(int x, int y)
{
}
thats all!

Re:Accessing Mouse

Posted: Thu Feb 23, 2006 2:19 am
by Rob
Just a side note, your mouse button handlers get called with the old X & Y coordinates instead of the new!

Re:Accessing Mouse

Posted: Thu Feb 23, 2006 3:07 pm
by astrocrep
Wow thats really good!

Thanks for your contrib.

-Rich