#include <pbsdboot.h>
#include <dev/hpc/hpccmapvar.h>
#if ( _WIN32_WCE >= 200 )
#define HAVE_COLOR_PALETTE
#endif
#ifndef ASSERT
#define ASSERT(a) \
if (!(a)) { \
msg_printf(MSG_ERROR, TEXT("assertion failure"), \
TEXT(#a)); \
}
#endif
#ifdef HAVE_COLOR_PALETTE
enum palette_status palette_succeeded = PAL_ERROR;
static void palette_setup(PALETTEENTRY *pal);
#else
enum palette_status palette_succeeded = PAL_NOERROR;
#endif
static HPALETTE pal = NULL;
void
palette_init(HWND hWnd)
{
#ifdef HAVE_COLOR_PALETTE
HDC hdc;
PAINTSTRUCT ps;
LOGPALETTE* logpal;
debug_printf(TEXT("*** palette init ***\n"));
hdc = BeginPaint(hWnd, &ps);
if (GetDeviceCaps(hdc, TECHNOLOGY) == DT_RASDISPLAY &&
(GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE) &&
GetDeviceCaps(hdc, COLORRES) == 8) {
ASSERT(GetDeviceCaps(hdc, BITSPIXEL) == 8);
ASSERT(GetDeviceCaps(hdc, PLANES) == 1);
logpal = LocalAlloc (LPTR,
(sizeof(LOGPALETTE) +
sizeof(PALETTEENTRY) * 256));
logpal->palVersion = 0x300;
logpal->palNumEntries = 256;
palette_setup(logpal->palPalEntry);
pal = CreatePalette(logpal);
if (pal == NULL) {
debug_printf(TEXT("CreatePalette() failed"));
msg_printf(MSG_ERROR, TEXT("Error"),
TEXT("CreatePalette() failed: %d"),
GetLastError());
}
LocalFree((HLOCAL)logpal);
} else {
palette_succeeded = PAL_NOERROR;
}
EndPaint(hWnd, &ps);
#endif
}
#ifdef HAVE_COLOR_PALETTE
static void
palette_setup(PALETTEENTRY *pal)
{
int i;
for (i = 0; i < 256; i++) {
pal[i].peFlags = PC_EXPLICIT;
pal[i].peRed = bivideo_cmap_r[i];
pal[i].peGreen = bivideo_cmap_g[i];
pal[i].peBlue = bivideo_cmap_b[i];
}
}
#endif
void
palette_set(HWND hWnd)
{
#ifdef HAVE_COLOR_PALETTE
int n;
HDC hdc;
PAINTSTRUCT ps;
HPALETTE prev_pal;
debug_printf(TEXT("*** palette set ***\n"));
if (pal != NULL) {
hdc = BeginPaint(hWnd, &ps);
prev_pal = SelectPalette(hdc, pal, FALSE);
if (prev_pal == NULL) {
debug_printf(TEXT("SelectPalette() failed"));
msg_printf(MSG_ERROR, TEXT("Error"),
TEXT("SelectPalette() failed: %d"),
GetLastError());
} else {
n = RealizePalette(hdc);
debug_printf(TEXT("RealizePalette() = %d\n"), n);
if (n == GDI_ERROR) {
debug_printf(TEXT("RealizePalette() failed"));
msg_printf(MSG_ERROR, TEXT("Error"),
TEXT("RealizePalette() failed: %d"),
GetLastError());
}
}
EndPaint(hWnd, &ps);
}
#endif
}
void
palette_check(HWND hWnd)
{
#ifdef HAVE_COLOR_PALETTE
HDC hdc;
PAINTSTRUCT ps;
PALETTEENTRY syspal[256];
PALETTEENTRY canonpal[256];
int i, n, error;
debug_printf(TEXT("*** palette check ***\n"));
error = 0;
if (pal != NULL) {
hdc = BeginPaint(hWnd, &ps);
palette_setup(canonpal);
n = GetSystemPaletteEntries(hdc, 0, 256, syspal);
if (n == 0) {
msg_printf(MSG_ERROR, TEXT("Error"),
TEXT("GetSystemPaletteEntries() failed: %d"),
GetLastError());
error++;
} else {
for (i = 0; i < n; i++) {
debug_printf(TEXT("%3d: %02x %02x %02x"),
i,
syspal[i].peRed,
syspal[i].peGreen,
syspal[i].peBlue);
if (syspal[i].peRed != canonpal[i].peRed ||
syspal[i].peGreen != canonpal[i].peGreen ||
syspal[i].peBlue != canonpal[i].peBlue ) {
debug_printf(TEXT(" *"));
error++;
}
debug_printf(TEXT("\n"));
}
}
EndPaint(hWnd, &ps);
if (error) {
palette_succeeded = PAL_ERROR;
} else {
palette_succeeded = PAL_SUCCEEDED;
}
}
#endif
}