#include <dos.h>
#include<conio.h>
#include <graphics.h>
#include <stdio.h>
#include <stdlib.h>
union REGS i, o;

class mouse
{
    public:
    /*initialize mouse*/
    initmouse()
    {
        i.x.ax = 0;
        int86(0x33, &i, &o);
        return (o.x.ax);
    }

    //display mouse pointer
    void showmouseptr()
    {
        i.x.ax = 1;
        int86(0x33, &i, &o);
    }

    //resttricts mous emovement
    void restrictmouseptr(int x1, int y1, int x2, int y2)
    {
        i.x.ax = 7;
        i.x.cx = x1;
        i.x.dx = x2;
        int86 (0x33, &i, &o);
        i.x.ax = 8;
        i.x.cx = y1;
        i.x.dx = y2;
        int86(0x33, &i, &o);
    }
    //gets mouse coordinates and button status
    void getmousepos(int *button, int *x, int *y)
    {
        i.x.ax = 3;
        int86(0x33, &i, &o);
        *button = o.x.bx;
        *x = o.x.cx;
        *y = o.x.dx;
    }
}
main()
{
    int gd=DETECT, gm, maxx, maxy, x, y, button;
    initgraph(&gd, &gm, "");
    maxx = getmaxx();
    maxy = getmaxy();
    mouse m;
    rectangle(0, 56, maxx, maxy);
    setviewport(1, 57, maxx-1, maxy-1,1);

    gotoxy(26, 1);
    printf ("Mouse demo");

    if (m.initmouse() == 0)
    {
        closegraph();
        restorecrtmode();
        printf("md not installed");
        exit(1);
    }

    m.restrictmouseptr(1, 57, maxx - 1, maxy - 1);
    m.showmouseptr();
    gotoxy(1,2);
    printf("left button");
    gotoxy(15, 2);
    printf("right button");
    gotoxy(55, 3);
    printf("Press any key to exit");

    while(!kbhit())
    {
        m.getmousepos(&button, &x, &y);

        gotoxy(5,3);
        (button & 1) == 1 ? printf("down"): printf("up");

        gotoxy(20,3);
        (button & 2) == 2 ? printf("down"): printf("up");

        gotoxy(65,2);
        printf("X = %03d y = %03d", x, y);
    }

}