Home | History | Annotate | Line # | Download | only in pbsdboot
      1  1.3  takemura /*	$NetBSD: palette.c,v 1.3 2001/02/25 12:58:38 takemura Exp $	*/
      2  1.1  takemura 
      3  1.1  takemura /*-
      4  1.1  takemura  * Copyright (c) 1999 Shin Takemura.
      5  1.1  takemura  * All rights reserved.
      6  1.1  takemura  *
      7  1.1  takemura  * This software is part of the PocketBSD.
      8  1.1  takemura  *
      9  1.1  takemura  * Redistribution and use in source and binary forms, with or without
     10  1.1  takemura  * modification, are permitted provided that the following conditions
     11  1.1  takemura  * are met:
     12  1.1  takemura  * 1. Redistributions of source code must retain the above copyright
     13  1.1  takemura  *    notice, this list of conditions and the following disclaimer.
     14  1.1  takemura  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  takemura  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  takemura  *    documentation and/or other materials provided with the distribution.
     17  1.1  takemura  * 3. All advertising materials mentioning features or use of this software
     18  1.1  takemura  *    must display the following acknowledgement:
     19  1.1  takemura  *	This product includes software developed by the PocketBSD project
     20  1.1  takemura  *	and its contributors.
     21  1.1  takemura  * 4. Neither the name of the project nor the names of its contributors
     22  1.1  takemura  *    may be used to endorse or promote products derived from this software
     23  1.1  takemura  *    without specific prior written permission.
     24  1.1  takemura  *
     25  1.1  takemura  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  1.1  takemura  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  1.1  takemura  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  1.1  takemura  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  1.1  takemura  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  1.1  takemura  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  1.1  takemura  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  1.1  takemura  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  1.1  takemura  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  1.1  takemura  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  1.1  takemura  * SUCH DAMAGE.
     36  1.1  takemura  *
     37  1.1  takemura  */
     38  1.1  takemura #include <pbsdboot.h>
     39  1.1  takemura 
     40  1.3  takemura #include <dev/hpc/hpccmapvar.h>
     41  1.2  takemura 
     42  1.1  takemura /*
     43  1.1  takemura  * If Windows have no color palette, we have nothing to do here.
     44  1.1  takemura  */
     45  1.1  takemura #if ( _WIN32_WCE >= 200 ) // WIN CE Ver 2.00 or greater
     46  1.1  takemura #define HAVE_COLOR_PALETTE
     47  1.1  takemura #endif
     48  1.1  takemura 
     49  1.3  takemura #ifndef ASSERT
     50  1.1  takemura #define ASSERT(a) \
     51  1.1  takemura 	if (!(a)) { \
     52  1.1  takemura 		msg_printf(MSG_ERROR, TEXT("assertion failure"), \
     53  1.1  takemura 			TEXT(#a)); \
     54  1.1  takemura 	}
     55  1.3  takemura #endif /* !ASSERT */
     56  1.1  takemura 
     57  1.1  takemura #ifdef HAVE_COLOR_PALETTE
     58  1.1  takemura enum palette_status palette_succeeded = PAL_ERROR;
     59  1.1  takemura static void palette_setup(PALETTEENTRY *pal);
     60  1.1  takemura #else
     61  1.1  takemura enum palette_status palette_succeeded = PAL_NOERROR;
     62  1.1  takemura #endif
     63  1.1  takemura 
     64  1.1  takemura static HPALETTE pal = NULL;
     65  1.1  takemura 
     66  1.1  takemura void
     67  1.1  takemura palette_init(HWND hWnd)
     68  1.1  takemura {
     69  1.1  takemura #ifdef HAVE_COLOR_PALETTE
     70  1.1  takemura 	HDC hdc;
     71  1.1  takemura 	PAINTSTRUCT ps;
     72  1.1  takemura 	LOGPALETTE* logpal;
     73  1.1  takemura 
     74  1.1  takemura 	debug_printf(TEXT("*** palette init ***\n"));
     75  1.1  takemura 
     76  1.1  takemura 	hdc = BeginPaint(hWnd, &ps);
     77  1.1  takemura 	if (GetDeviceCaps(hdc, TECHNOLOGY) == DT_RASDISPLAY &&
     78  1.1  takemura 	    (GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE) &&
     79  1.1  takemura 	    GetDeviceCaps(hdc, COLORRES) == 8) {
     80  1.1  takemura 		ASSERT(GetDeviceCaps(hdc, BITSPIXEL) == 8);
     81  1.1  takemura 		ASSERT(GetDeviceCaps(hdc, PLANES) == 1);
     82  1.1  takemura 		/*
     83  1.1  takemura 		 * create logical palette
     84  1.1  takemura 		 */
     85  1.1  takemura 		logpal = LocalAlloc (LPTR,
     86  1.1  takemura 				     (sizeof(LOGPALETTE) +
     87  1.1  takemura 				      sizeof(PALETTEENTRY) * 256));
     88  1.1  takemura 		logpal->palVersion = 0x300;
     89  1.1  takemura 		logpal->palNumEntries = 256;
     90  1.1  takemura 		palette_setup(logpal->palPalEntry);
     91  1.1  takemura 
     92  1.1  takemura 		pal = CreatePalette(logpal);
     93  1.1  takemura 		if (pal == NULL) {
     94  1.1  takemura 			debug_printf(TEXT("CreatePalette() failed"));
     95  1.1  takemura 			msg_printf(MSG_ERROR, TEXT("Error"),
     96  1.1  takemura 				   TEXT("CreatePalette() failed: %d"),
     97  1.1  takemura 				   GetLastError());
     98  1.1  takemura 		}
     99  1.1  takemura 		LocalFree((HLOCAL)logpal);
    100  1.1  takemura 	} else {
    101  1.1  takemura 		/*
    102  1.1  takemura 		 * The screen is not 8 bit depth nor indexed color.
    103  1.1  takemura 		 */
    104  1.1  takemura 		palette_succeeded = PAL_NOERROR;
    105  1.1  takemura 	}
    106  1.1  takemura 	EndPaint(hWnd, &ps);
    107  1.1  takemura #endif /* HAVE_COLOR_PALETTE */
    108  1.1  takemura }
    109  1.1  takemura 
    110  1.1  takemura #ifdef HAVE_COLOR_PALETTE
    111  1.1  takemura static void
    112  1.1  takemura palette_setup(PALETTEENTRY *pal)
    113  1.1  takemura {
    114  1.2  takemura 	int i;
    115  1.1  takemura 
    116  1.2  takemura 	for (i = 0; i < 256; i++) {
    117  1.1  takemura 		pal[i].peFlags = PC_EXPLICIT;
    118  1.2  takemura 		pal[i].peRed = bivideo_cmap_r[i];
    119  1.2  takemura 		pal[i].peGreen = bivideo_cmap_g[i];
    120  1.2  takemura 		pal[i].peBlue = bivideo_cmap_b[i];
    121  1.1  takemura 	}
    122  1.1  takemura }
    123  1.1  takemura #endif /* HAVE_COLOR_PALETTE */
    124  1.1  takemura 
    125  1.1  takemura void
    126  1.1  takemura palette_set(HWND hWnd)
    127  1.1  takemura {
    128  1.1  takemura #ifdef HAVE_COLOR_PALETTE
    129  1.1  takemura 	int n;
    130  1.1  takemura 	HDC hdc;
    131  1.1  takemura 	PAINTSTRUCT ps;
    132  1.1  takemura 	HPALETTE prev_pal;
    133  1.1  takemura 
    134  1.1  takemura 	debug_printf(TEXT("*** palette set ***\n"));
    135  1.1  takemura 
    136  1.1  takemura 	if (pal != NULL) {
    137  1.1  takemura 		hdc = BeginPaint(hWnd, &ps);
    138  1.1  takemura 		prev_pal = SelectPalette(hdc, pal, FALSE);
    139  1.1  takemura 		if (prev_pal == NULL) {
    140  1.1  takemura 			debug_printf(TEXT("SelectPalette() failed"));
    141  1.1  takemura 			msg_printf(MSG_ERROR, TEXT("Error"),
    142  1.1  takemura 			    TEXT("SelectPalette() failed: %d"),
    143  1.1  takemura 			    GetLastError());
    144  1.1  takemura 		} else {
    145  1.1  takemura 			n = RealizePalette(hdc);
    146  1.1  takemura 			debug_printf(TEXT("RealizePalette() = %d\n"), n);
    147  1.1  takemura 			if (n == GDI_ERROR) {
    148  1.1  takemura 				debug_printf(TEXT("RealizePalette() failed"));
    149  1.1  takemura 				msg_printf(MSG_ERROR, TEXT("Error"),
    150  1.1  takemura 				    TEXT("RealizePalette() failed: %d"),
    151  1.1  takemura 				    GetLastError());
    152  1.1  takemura 			}
    153  1.1  takemura 		}
    154  1.1  takemura 		EndPaint(hWnd, &ps);
    155  1.1  takemura 	}
    156  1.1  takemura #endif /* HAVE_COLOR_PALETTE */
    157  1.1  takemura }
    158  1.1  takemura 
    159  1.1  takemura void
    160  1.1  takemura palette_check(HWND hWnd)
    161  1.1  takemura {
    162  1.1  takemura #ifdef HAVE_COLOR_PALETTE
    163  1.1  takemura 	HDC hdc;
    164  1.1  takemura 	PAINTSTRUCT ps;
    165  1.1  takemura 	PALETTEENTRY syspal[256];
    166  1.1  takemura 	PALETTEENTRY canonpal[256];
    167  1.1  takemura 	int i, n, error;
    168  1.1  takemura 
    169  1.1  takemura 	debug_printf(TEXT("*** palette check ***\n"));
    170  1.1  takemura 
    171  1.1  takemura 	error = 0;
    172  1.1  takemura 	if (pal != NULL) {
    173  1.1  takemura 		hdc = BeginPaint(hWnd, &ps);
    174  1.1  takemura 
    175  1.1  takemura 		palette_setup(canonpal);
    176  1.1  takemura 
    177  1.1  takemura 		n = GetSystemPaletteEntries(hdc, 0, 256, syspal);
    178  1.1  takemura 		if (n == 0) {
    179  1.1  takemura 			msg_printf(MSG_ERROR, TEXT("Error"),
    180  1.1  takemura 			    TEXT("GetSystemPaletteEntries() failed: %d"),
    181  1.1  takemura 			    GetLastError());
    182  1.1  takemura 			error++;
    183  1.1  takemura 		} else {
    184  1.1  takemura 			for (i = 0; i < n; i++) {
    185  1.1  takemura 				debug_printf(TEXT("%3d: %02x %02x %02x"),
    186  1.1  takemura 				    i,
    187  1.1  takemura 				    syspal[i].peRed,
    188  1.1  takemura 				    syspal[i].peGreen,
    189  1.1  takemura 				    syspal[i].peBlue);
    190  1.1  takemura 				if (syspal[i].peRed != canonpal[i].peRed ||
    191  1.1  takemura 				    syspal[i].peGreen != canonpal[i].peGreen ||
    192  1.1  takemura 				    syspal[i].peBlue != canonpal[i].peBlue ) {
    193  1.1  takemura 					debug_printf(TEXT(" *"));
    194  1.1  takemura 					error++;
    195  1.1  takemura 				}
    196  1.1  takemura 				debug_printf(TEXT("\n"));
    197  1.1  takemura 			}
    198  1.1  takemura 		}
    199  1.1  takemura 		EndPaint(hWnd, &ps);
    200  1.1  takemura 		if (error) {
    201  1.1  takemura 			palette_succeeded = PAL_ERROR;
    202  1.1  takemura 		} else {
    203  1.1  takemura 			palette_succeeded = PAL_SUCCEEDED;
    204  1.1  takemura 		}
    205  1.1  takemura 	}
    206  1.1  takemura #endif /* HAVE_COLOR_PALETTE */
    207  1.1  takemura }
    208