palette.c revision 1.2 1 /* $NetBSD: palette.c,v 1.2 2000/03/20 10:47:35 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 <arch/hpcmips/dev/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 #define ASSERT(a) \
50 if (!(a)) { \
51 msg_printf(MSG_ERROR, TEXT("assertion failure"), \
52 TEXT(#a)); \
53 }
54
55 #ifdef HAVE_COLOR_PALETTE
56 enum palette_status palette_succeeded = PAL_ERROR;
57 static void palette_setup(PALETTEENTRY *pal);
58 #else
59 enum palette_status palette_succeeded = PAL_NOERROR;
60 #endif
61
62 static HPALETTE pal = NULL;
63
64 void
65 palette_init(HWND hWnd)
66 {
67 #ifdef HAVE_COLOR_PALETTE
68 HDC hdc;
69 PAINTSTRUCT ps;
70 LOGPALETTE* logpal;
71
72 debug_printf(TEXT("*** palette init ***\n"));
73
74 hdc = BeginPaint(hWnd, &ps);
75 if (GetDeviceCaps(hdc, TECHNOLOGY) == DT_RASDISPLAY &&
76 (GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE) &&
77 GetDeviceCaps(hdc, COLORRES) == 8) {
78 ASSERT(GetDeviceCaps(hdc, BITSPIXEL) == 8);
79 ASSERT(GetDeviceCaps(hdc, PLANES) == 1);
80 /*
81 * create logical palette
82 */
83 logpal = LocalAlloc (LPTR,
84 (sizeof(LOGPALETTE) +
85 sizeof(PALETTEENTRY) * 256));
86 logpal->palVersion = 0x300;
87 logpal->palNumEntries = 256;
88 palette_setup(logpal->palPalEntry);
89
90 pal = CreatePalette(logpal);
91 if (pal == NULL) {
92 debug_printf(TEXT("CreatePalette() failed"));
93 msg_printf(MSG_ERROR, TEXT("Error"),
94 TEXT("CreatePalette() failed: %d"),
95 GetLastError());
96 }
97 LocalFree((HLOCAL)logpal);
98 } else {
99 /*
100 * The screen is not 8 bit depth nor indexed color.
101 */
102 palette_succeeded = PAL_NOERROR;
103 }
104 EndPaint(hWnd, &ps);
105 #endif /* HAVE_COLOR_PALETTE */
106 }
107
108 #ifdef HAVE_COLOR_PALETTE
109 static void
110 palette_setup(PALETTEENTRY *pal)
111 {
112 int i;
113
114 for (i = 0; i < 256; i++) {
115 pal[i].peFlags = PC_EXPLICIT;
116 pal[i].peRed = bivideo_cmap_r[i];
117 pal[i].peGreen = bivideo_cmap_g[i];
118 pal[i].peBlue = bivideo_cmap_b[i];
119 }
120 }
121 #endif /* HAVE_COLOR_PALETTE */
122
123 void
124 palette_set(HWND hWnd)
125 {
126 #ifdef HAVE_COLOR_PALETTE
127 int n;
128 HDC hdc;
129 PAINTSTRUCT ps;
130 HPALETTE prev_pal;
131
132 debug_printf(TEXT("*** palette set ***\n"));
133
134 if (pal != NULL) {
135 hdc = BeginPaint(hWnd, &ps);
136 prev_pal = SelectPalette(hdc, pal, FALSE);
137 if (prev_pal == NULL) {
138 debug_printf(TEXT("SelectPalette() failed"));
139 msg_printf(MSG_ERROR, TEXT("Error"),
140 TEXT("SelectPalette() failed: %d"),
141 GetLastError());
142 } else {
143 n = RealizePalette(hdc);
144 debug_printf(TEXT("RealizePalette() = %d\n"), n);
145 if (n == GDI_ERROR) {
146 debug_printf(TEXT("RealizePalette() failed"));
147 msg_printf(MSG_ERROR, TEXT("Error"),
148 TEXT("RealizePalette() failed: %d"),
149 GetLastError());
150 }
151 }
152 EndPaint(hWnd, &ps);
153 }
154 #endif /* HAVE_COLOR_PALETTE */
155 }
156
157 void
158 palette_check(HWND hWnd)
159 {
160 #ifdef HAVE_COLOR_PALETTE
161 HDC hdc;
162 PAINTSTRUCT ps;
163 PALETTEENTRY syspal[256];
164 PALETTEENTRY canonpal[256];
165 int i, n, error;
166
167 debug_printf(TEXT("*** palette check ***\n"));
168
169 error = 0;
170 if (pal != NULL) {
171 hdc = BeginPaint(hWnd, &ps);
172
173 palette_setup(canonpal);
174
175 n = GetSystemPaletteEntries(hdc, 0, 256, syspal);
176 if (n == 0) {
177 msg_printf(MSG_ERROR, TEXT("Error"),
178 TEXT("GetSystemPaletteEntries() failed: %d"),
179 GetLastError());
180 error++;
181 } else {
182 for (i = 0; i < n; i++) {
183 debug_printf(TEXT("%3d: %02x %02x %02x"),
184 i,
185 syspal[i].peRed,
186 syspal[i].peGreen,
187 syspal[i].peBlue);
188 if (syspal[i].peRed != canonpal[i].peRed ||
189 syspal[i].peGreen != canonpal[i].peGreen ||
190 syspal[i].peBlue != canonpal[i].peBlue ) {
191 debug_printf(TEXT(" *"));
192 error++;
193 }
194 debug_printf(TEXT("\n"));
195 }
196 }
197 EndPaint(hWnd, &ps);
198 if (error) {
199 palette_succeeded = PAL_ERROR;
200 } else {
201 palette_succeeded = PAL_SUCCEEDED;
202 }
203 }
204 #endif /* HAVE_COLOR_PALETTE */
205 }
206