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