/* NOTE: THE MOUSE HANDLER CODE IS JUST BEFORE MOUSE_INSTALL FUNCTION AT THE BOTTOM OF THE PAGE*/


#define black 0	
#define white 255          
#define none -1    

int xpos_mouse=500,ypos_mouse=350;   
char temp1,temp2,temp3;
char tempx;

int cursor[176]={
white,white,none,none,none,none,none,none,none,none,none,
white,black,white,none,none,none,none,none,none,none,none,
white,black,black,white,none,none,none,none,none,none,none,
white,black,black,black,white,none,none,none,none,none,none,
white,black,black,black,black,white,none,none,none,none,none,
white,black,black,black,black,black,white,none,none,none,none,
white,black,black,black,black,black,black,white,none,none,none,
white,black,black,black,black,black,black,black,white,none,none,
white,black,black,black,black,black,black,black,black,white,none,
white,black,black,black,black,black,white,white,white,white,white,
white,black,black,white,black,black,white,none,none,none,none,
white,black,white,none,white,black,black,white,none,none,none,
white,white,none,none,white,black,black,white,none,none,none,
white,none,none,none,none,white,black,black,white,none,none,
none,none,none,none,none,white,black,black,white,none,none,
none,none,none,none,none,none,white,white,none,none,none};

int cursor_area[176]={
none,none,none,none,none,none,none,none,none,none,none,
none,none,none,none,none,none,none,none,none,none,none,
none,none,none,none,none,none,none,none,none,none,none,
none,none,none,none,none,none,none,none,none,none,none,
none,none,none,none,none,none,none,none,none,none,none,
none,none,none,none,none,none,none,none,none,none,none,
none,none,none,none,none,none,none,none,none,none,none,
none,none,none,none,none,none,none,none,none,none,none,
none,none,none,none,none,none,none,none,none,none,none,
none,none,none,none,none,none,none,none,none,none,none,
none,none,none,none,none,none,none,none,none,none,none,
none,none,none,none,none,none,none,none,none,none,none,
none,none,none,none,none,none,none,none,none,none,none,
none,none,none,none,none,none,none,none,none,none,none,
none,none,none,none,none,none,none,none,none,none,none,
none,none,none,none,none,none,none,none,none,none,none};


char getdata()
{
temp1=inportb(0x60);
return temp1;
}

void mouse_wait(unsigned char thetype)
{
long timeout=100000;
	if(thetype==0)
	{
		while(timeout--)
		{
		temp1=inportb(0x64);
		
			if((temp1 & 1)==1)
			{
			return;
			}
		}
		return;
	}
	if(thetype==1)
	{
		while(timeout--)
		{
		temp1=inportb(0x64);
		
			if((temp1 & 2)==0)
			{
			return;
			}
		}
		return;
	}
}


unsigned char mouse_read()
{
mouse_wait(0);
tempx=inportb(0x60);
return tempx;
}

void mouse_write(unsigned char a_write)
{
mouse_wait(1);
outportb(0x64,0xD4);

mouse_wait(1);
tempx=a_write;
outportb(0x60,tempx);
}


int putcursor_area(int xpos,int ypos)
{
int x,y,color,cursor_width,cursor_height;
int red,green,blue;
y=ypos;
x=xpos;
cursor_width=xpos+11;
cursor_height=ypos+16;
int cvalue=0;
for(y=ypos;y<cursor_height;y++)
{
	for(x=xpos;x<cursor_width;x++)
	{
	color=cursor_area[cvalue];
	red=(color>>16)&255;
	green=(color>>8)&255;
	blue=(color&255);
	putpixel(x,y,red,green,blue);
	cvalue=cvalue+1;
	}
}
}

int getcursor_area(int xpos,int ypos)
{
int x,y,color,cursor_width,cursor_height;
int value=0;
y=ypos;
x=xpos;
cursor_width=xpos+11;
cursor_height=ypos+16;
int cvalue=0;
for(y=ypos;y<cursor_height;y++)
{
for(x=xpos;x<cursor_width;x++)
{
value=getpixel(x,y);
cursor_area[cvalue]=value;
cvalue++;
}
}
}

int drawcursor(int xpos,int ypos)
{
int x,y,color,cursor_width,cursor_height;
y=ypos;
x=xpos;
cursor_width=xpos+11;
cursor_height=ypos+16;
int cvalue=0;
for(y=ypos;y<cursor_height;y++)
{
	for(x=xpos;x<cursor_width;x++)
	{
	color=cursor[cvalue];
		if((color!=none)&&(x<1024)&&(y<768))
		{	
		putpixel(x,y,color,color,color);
		}
	cvalue++;
	}
}

}



void mouse_handler(struct regs *r)
{
char x;
signed char y,z,a;	/*Never declare them as a integer*/

	
temp2=inportb(0x64);
	if((temp2&1)==1)		/* if data is available lets get them from port 0x60*/
	{
		if((temp2&32)==32)	/*check to see if this is the mouse data. If it is keyboard data, we simply read it & ignore*/
		{
		temp2=0;
		x=getdata();	/*let's get all the 3 bytes of available data*/
		y=getdata();
		z=getdata();

									
		   	putcursor_area(xpos_mouse,ypos_mouse);   /*this function restores pixels consumed by our cursor to the original pixels before the cursor was drawn*/ 			
			xpos_mouse=xpos_mouse+(y/50);	/* 1/50 is the scaling factor for Bochs.Remove this /50 if using under QEMU*/
			ypos_mouse=ypos_mouse-(z/50);

			/*let's checkout the position of the cursor. If it exceeds the screen limit, we'll simply restore it to the screen limit*/

			if(xpos_mouse>1023) xpos_mouse=1023;
			else if(xpos_mouse<1) xpos_mouse=1;
			if(ypos_mouse>767) ypos_mouse=767;
			else if(ypos_mouse<1) ypos_mouse=1;

			getcursor_area(xpos_mouse,ypos_mouse);  /*Before drawing the cursor let's save the pixels in a array so that we can restore it later when the cursor is moved*/
			drawcursor(xpos_mouse,ypos_mouse);	/*Draw the cursor at position (xpos_mouse,ypos_mouse) */
			
		}

		else	/*this statement is for keyboard data*/
		{
		x=getdata();	/*let's read the keyboard data and simply ignore it*/
		}
	
	}
			
		
 
 outportb(0xA0,0x20);	/*let's acknowledge the master and slave controller that we have
			  finished processing the interrupt*/
 outportb(0x20,0x20);

}

/* Installs the mouse handler into IRQ12 */
void mouse_install()
{

unsigned char _status;
	getcursor_area(xpos_mouse,ypos_mouse);
	drawcursor(xpos_mouse,ypos_mouse);
	
	mouse_wait(1);	/*let's now enable the mouse port to generate interrupts*/
	outportb(0x64,0xA8);
	
	mouse_wait(1);	/*modify the "compaq status byte", to enable the interrupt*/
	outportb(0x64,0x20);
	mouse_wait(0);

	tempx=inportb(0x60);
	_status=(tempx | 2);

	mouse_wait(1);
	outportb(0x64,0x60);
	mouse_wait(1);
	tempx=_status;
	outportb(0x60,tempx);
	
	mouse_write(0xF6);
	mouse_read();

mouse_write(0xF4);
mouse_read();
	
	irq_install_handler(12, mouse_handler);
}