#include "WimpLib:Display.h"
#include "WimpLib:Task.h"

#include "swis.h"

void Display_SetDashPattern(int len, int p1, int p2, int p3, int p4, int p5, int p6, int p7, int p8)
{
	char p[10];

	_swi(OS_Byte, _INR(0,2), 163, 242, len);

	p[0] = 23;
	p[1] = 6;
	p[2] = p1;
	p[3] = p2;
	p[4] = p3;
	p[5] = p4;
	p[6] = p5;
	p[7] = p6;
	p[8] = p7;
	p[9] = p8;

	// OS_WriteN
	_swi(OS_WriteN, _INR(0,1), &p[0], 10);
}

void Display_Plot(int code, int x, int y)
{
	_swi(OS_Plot, _INR(0,2), code, x, y);
}

void Display_RectangleFill(const CRect* prect)
{
	_swi(OS_Plot, _INR(0,2), 4, prect->x0, prect->y0);
	_swi(OS_Plot, _INR(0,2), 101, prect->x1, prect->y1);
}

void Display_GetGraphicWindow(CRect* prect)
{
	int b[5];

	b[0] = 128;
	b[1] = 129;
	b[2] = 130;
	b[3] = 131;
	b[4] = -1;

	_swi(OS_ReadVduVariables, _INR(0,1), b, prect);
}

void Display_SetGraphicWindow(const CRect* prect)
{
	char b[9];

	b[0] = 0;
	b[1] = prect->x0;
	b[2] = prect->x0 >> 8;
	b[3] = prect->y0;
	b[4] = prect->y0 >> 8;
	b[5] = prect->x1;
	b[6] = prect->x1 >> 8;
	b[7] = prect->y1;
	b[8] = prect->y1 >> 8;

	_swi(OS_WriteN, _INR(0,1), b, 9);
}

void Display_SetGraphicWindow_OSInclusive(const CRect* prct)
{
	const Mode_Info* Mode = Task_GetModeInfo();
	CRect rct;

	rct.x0 = prct->x0 >> Mode->shiftx;
	rct.y0 = prct->y0 >> Mode->shifty;
	rct.x1 = prct->x1 >> Mode->shiftx;
	rct.y1 = prct->y1 >> Mode->shifty;

	Display_SetGraphicWindow(&rct);
}

void Display_SetGraphicWindow_OSExclusive(const CRect* prct)
{
	const Mode_Info* Mode = Task_GetModeInfo();
	CRect rct;

	rct.x0 = prct->x0 >> Mode->shiftx;
	rct.y0 = prct->y0 >> Mode->shifty;
	rct.x1 = (prct->x1 - Mode->dx) >> Mode->shiftx;
	rct.y1 = (prct->y1 - Mode->dy) >> Mode->shifty;

	Display_SetGraphicWindow(&rct);
}

void Display_SetMouseBox(const CRect* prect)
{
	char b[9];

	b[0] = 1;
	b[1] = prect->x0;
	b[2] = prect->x0 >> 8;
	b[3] = prect->y0;
	b[4] = prect->y0 >> 8;
	b[5] = prect->x1;
	b[6] = prect->x1 >> 8;
	b[7] = prect->y1;
	b[8] = prect->y1 >> 8;

	_swi(OS_Word, _INR(0,1), 21, b);
}

void Display_SetMouseBox_OSInclusive(const CRect* prct)
{
	const Mode_Info* Mode = Task_GetModeInfo();
	CRect rct;

	rct.x0 = prct->x0 >> Mode->shiftx;
	rct.y0 = prct->y0 >> Mode->shifty;
	rct.x1 = prct->x1 >> Mode->shiftx;
	rct.y1 = prct->y1 >> Mode->shifty;

	Display_SetMouseBox(&rct);
}

void Display_SetMouseBox_OSExclusive(const CRect* prct)
{
	const Mode_Info* Mode = Task_GetModeInfo();
	CRect rct;

	rct.x0 = prct->x0 >> Mode->shiftx;
	rct.y0 = prct->y0 >> Mode->shifty;
	rct.x1 = (prct->x1 - Mode->dx) >> Mode->shiftx;
	rct.y1 = (prct->y1 - Mode->dy) >> Mode->shifty;

	Display_SetMouseBox(&rct);
}
