#include "CursorManager.h"
#include "ServerCursor.h"
#include <ByteOrder.h>
#include <new>
#include <stdio.h>
using std::nothrow;
ServerCursor::ServerCursor(BRect r, color_space format, int32 flags,
BPoint hotspot, int32 bytesPerRow, screen_id screen)
:
ServerBitmap(r, format, flags, bytesPerRow, screen),
fHotSpot(hotspot),
fOwningTeam(-1),
fCursorData(NULL),
fManager(NULL)
{
fHotSpot.ConstrainTo(Bounds());
AllocateBuffer();
}
ServerCursor::ServerCursor(const uint8* data)
:
ServerBitmap(BRect(0, 0, 15, 15), B_RGBA32, 0),
fHotSpot(0, 0),
fOwningTeam(-1),
fCursorData(NULL),
fManager(NULL)
{
if (data) {
AllocateBuffer();
uint8* buffer = Bits();
if (!buffer)
return;
uint16* cursorBits = (uint16*)(data + 4);
uint16* transparencyBits = (uint16*)(data + 36);
fHotSpot.Set(data[3], data[2]);
for (int32 j = 0; j < 16; j++) {
uint32* bits = (uint32*)(buffer + (j * BytesPerRow()));
uint16 cursorLine = __swap_int16(cursorBits[j]);
uint16 transparencyLine = __swap_int16(transparencyBits[j]);
uint16 mask = 1 << 15;
for (int32 i = 0; i < 16; i++, mask >>= 1) {
if (cursorLine & mask)
bits[i] = 0xff000000;
else if (transparencyLine & mask)
bits[i] = 0xffffffff;
else
bits[i] = 0x00000000;
}
}
fCursorData = new (nothrow) uint8[68];
if (fCursorData)
memcpy(fCursorData, data, 68);
} else {
fWidth = 0;
fHeight = 0;
fBytesPerRow = 0;
fSpace = B_NO_COLOR_SPACE;
}
}
ServerCursor::ServerCursor(const uint8* alreadyPaddedData, uint32 width,
uint32 height, color_space format)
:
ServerBitmap(BRect(0, 0, width - 1, height - 1), format, 0),
fHotSpot(0, 0),
fOwningTeam(-1),
fCursorData(NULL),
fManager(NULL)
{
AllocateBuffer();
if (Bits())
memcpy(Bits(), alreadyPaddedData, BitsLength());
}
ServerCursor::ServerCursor(const ServerCursor* cursor)
:
ServerBitmap(cursor),
fHotSpot(0, 0),
fOwningTeam(-1),
fCursorData(NULL),
fManager(NULL)
{
AllocateBuffer();
if (cursor) {
if (Bits() && cursor->Bits())
memcpy(Bits(), cursor->Bits(), BitsLength());
fHotSpot = cursor->fHotSpot;
if (cursor->fCursorData) {
fCursorData = new (nothrow) uint8[68];
if (fCursorData)
memcpy(fCursorData, cursor->fCursorData, 68);
}
}
}
ServerCursor::~ServerCursor()
{
delete[] fCursorData;
}
void
ServerCursor::SetHotSpot(BPoint hotSpot)
{
fHotSpot = hotSpot;
fHotSpot.ConstrainTo(Bounds());
}
void
ServerCursor::AttachedToManager(CursorManager* manager)
{
fManager = manager;
}