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