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