1 | /*␊ |
2 | * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.␊ |
3 | *␊ |
4 | * @APPLE_LICENSE_HEADER_START@␊ |
5 | * ␊ |
6 | * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights␊ |
7 | * Reserved. This file contains Original Code and/or Modifications of␊ |
8 | * Original Code as defined in and that are subject to the Apple Public␊ |
9 | * Source License Version 2.0 (the "License"). You may not use this file␊ |
10 | * except in compliance with the License. Please obtain a copy of the␊ |
11 | * License at http://www.apple.com/publicsource and read it before using␊ |
12 | * this file.␊ |
13 | * ␊ |
14 | * The Original Code and all software distributed under the License are␊ |
15 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER␊ |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,␊ |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,␊ |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the␊ |
19 | * License for the specific language governing rights and limitations␊ |
20 | * under the License.␊ |
21 | * ␊ |
22 | * @APPLE_LICENSE_HEADER_END@␊ |
23 | *␊ |
24 | * Copyright 1993 NeXT, Inc. All rights reserved.␊ |
25 | *␊ |
26 | */␊ |
27 | ␊ |
28 | #include "boot.h"␊ |
29 | #include "vbe.h"␊ |
30 | #include "appleClut8.h"␊ |
31 | #include "gui.h"␊ |
32 | #include "IOHibernatePrivate.h"␊ |
33 | ␊ |
34 | /*␊ |
35 | * for spinning disk␊ |
36 | */␊ |
37 | static int currentIndicator = 0;␊ |
38 | ␊ |
39 | int previewTotalSectors = 0;␊ |
40 | int previewLoadedSectors = 0;␊ |
41 | uint8_t *previewSaveunder = 0;␊ |
42 | ␊ |
43 | #define VIDEO(x) (bootArgs->Video.v_ ## x)␊ |
44 | ␊ |
45 | //==========================================================================␊ |
46 | // getVBEVideoRam␊ |
47 | ␊ |
48 | uint32_t getVBEVideoRam()␊ |
49 | {␊ |
50 | ␉VBEInfoBlock vbeInfo;␊ |
51 | ␉int err, small;␊ |
52 | ␊ |
53 | ␉bzero( &vbeInfo, sizeof(vbeInfo) );␊ |
54 | ␉strcpy( (char*)&vbeInfo, "VBE2" );␊ |
55 | ␉err = getVBEInfo( &vbeInfo );␊ |
56 | ␉if (err != errSuccess)␊ |
57 | ␉␉return 0;␊ |
58 | ␊ |
59 | ␉if ( strncmp( (char *)vbeInfo.VESASignature, "VESA", 4 ) )␊ |
60 | ␉␉return 0;␊ |
61 | ␊ |
62 | ␉small = (vbeInfo.TotalMemory < 16);␊ |
63 | ␊ |
64 | ␉return vbeInfo.TotalMemory * 64 * 1024;␊ |
65 | }␊ |
66 | ␊ |
67 | //==========================================================================␊ |
68 | // getVBEInfoString␊ |
69 | ␊ |
70 | char *getVBEInfoString()␊ |
71 | {␊ |
72 | ␉VBEInfoBlock vbeInfo;␊ |
73 | ␉int err, small;␊ |
74 | ␉char* buff = NULL;␊ |
75 | ␊ |
76 | ␉bzero( &vbeInfo, sizeof(vbeInfo) );␊ |
77 | ␉strcpy( (char*)&vbeInfo, "VBE2" );␊ |
78 | ␉err = getVBEInfo( &vbeInfo );␊ |
79 | ␉if (err != errSuccess)␊ |
80 | ␉{␊ |
81 | ␉␉return 0;␊ |
82 | ␉}␊ |
83 | ␊ |
84 | ␉if ( strncmp( (char *)vbeInfo.VESASignature, "VESA", 4 ) )␊ |
85 | ␉{␊ |
86 | ␉␉return 0;␊ |
87 | ␉}␊ |
88 | ␊ |
89 | ␉buff = malloc(sizeof(char) * 256);␊ |
90 | ␉if (!buff)␊ |
91 | ␉{␊ |
92 | ␉␉return 0;␊ |
93 | ␉}␊ |
94 | ␊ |
95 | ␉small = (vbeInfo.TotalMemory < 16);␊ |
96 | ␊ |
97 | ␉snprintf(buff, 256,␊ |
98 | ␉␉"VESA v%d.%d %d%s (%s)\n",␊ |
99 | ␉␉vbeInfo.VESAVersion >> 8,␊ |
100 | ␉␉vbeInfo.VESAVersion & 0xf,␊ |
101 | ␉␉small ? (vbeInfo.TotalMemory * 64) : (vbeInfo.TotalMemory / 16),␊ |
102 | ␉␉small ? "KB" : "MB",␊ |
103 | ␉␉VBEDecodeFP(const char *, vbeInfo.OEMStringPtr) );␊ |
104 | ␊ |
105 | ␉return buff;␊ |
106 | }␊ |
107 | ␊ |
108 | //==========================================================================␊ |
109 | //␊ |
110 | ␊ |
111 | void printVBEModeInfo()␊ |
112 | {␊ |
113 | ␉VBEInfoBlock vbeInfo;␊ |
114 | ␉unsigned short * modePtr;␊ |
115 | ␉VBEModeInfoBlock modeInfo;␊ |
116 | ␉int err;␊ |
117 | ␉int line;␊ |
118 | ␉char* vbeInfoString = NULL;␊ |
119 | ␊ |
120 | ␉bzero( &vbeInfo, sizeof(vbeInfo) );␊ |
121 | ␉strcpy( (char*)&vbeInfo, "VBE2" );␊ |
122 | ␉err = getVBEInfo( &vbeInfo );␊ |
123 | ␉if ( err != errSuccess ) {␊ |
124 | ␉␉return;␊ |
125 | ␉}␊ |
126 | ␊ |
127 | ␉line = 0;␊ |
128 | ␊ |
129 | ␉// Activate and clear page 1␊ |
130 | ␉setActiveDisplayPage(1);␊ |
131 | ␉clearScreenRows(0, 24);␊ |
132 | ␉setCursorPosition( 0, 0, 1 );␊ |
133 | ␊ |
134 | ␉vbeInfoString = getVBEInfoString();␊ |
135 | ␉if (!vbeInfoString) {␊ |
136 | ␉␉printf("Error: getVBEInfoString failed\n");␊ |
137 | ␉␉return;␊ |
138 | ␉}␊ |
139 | ␉printf("%s", vbeInfoString);␊ |
140 | ␉free(vbeInfoString);␊ |
141 | ␉vbeInfoString = NULL;␊ |
142 | ␊ |
143 | ␉printf("Video modes supported:\n", VBEDecodeFP(const char *, vbeInfo.OEMStringPtr));␊ |
144 | ␊ |
145 | ␉// Loop through the mode list, and find the matching mode.␊ |
146 | ␊ |
147 | ␉for ( modePtr = VBEDecodeFP( unsigned short *, vbeInfo.VideoModePtr );␊ |
148 | *modePtr != modeEndOfList; modePtr++ )␊ |
149 | ␉{␊ |
150 | ␉␉// Get mode information.␊ |
151 | ␊ |
152 | ␉␉bzero( &modeInfo, sizeof(modeInfo) );␊ |
153 | ␉␉err = getVBEModeInfo( *modePtr, &modeInfo );␊ |
154 | ␉␉if ( err != errSuccess )␊ |
155 | ␉␉{␊ |
156 | ␉␉␉continue;␊ |
157 | ␉␉}␊ |
158 | ␊ |
159 | ␉␉printf("Mode %x: %dx%dx%d mm:%d attr:%x\n",␊ |
160 | ␉␉␉*modePtr, modeInfo.XResolution, modeInfo.YResolution,␊ |
161 | ␉␉␉modeInfo.BitsPerPixel, modeInfo.MemoryModel,␊ |
162 | ␉␉␉modeInfo.ModeAttributes);␊ |
163 | ␊ |
164 | ␉␉if (line++ >= 20)␊ |
165 | ␉␉{␊ |
166 | ␉␉␉pause();␊ |
167 | ␉␉␉line = 0;␊ |
168 | ␉␉␉clearScreenRows(0, 24);␊ |
169 | ␉␉␉setCursorPosition( 0, 0, 1 );␊ |
170 | ␉␉}␊ |
171 | ␉}␊ |
172 | ␊ |
173 | ␉if (line != 0)␊ |
174 | ␉{␊ |
175 | ␉␉pause();␊ |
176 | ␉}␊ |
177 | ␊ |
178 | ␉setActiveDisplayPage(0);␊ |
179 | }␊ |
180 | ␊ |
181 | //==============================================================================␊ |
182 | ␊ |
183 | char *getVBEModeInfoString()␊ |
184 | {␊ |
185 | ␉VBEInfoBlock vbeInfo;␊ |
186 | ␉unsigned short * modePtr;␊ |
187 | ␉VBEModeInfoBlock modeInfo;␊ |
188 | ␉int err;␊ |
189 | ␊ |
190 | ␉bzero( &vbeInfo, sizeof(vbeInfo) );␊ |
191 | ␉strcpy( (char*)&vbeInfo, "VBE2" );␊ |
192 | ␉err = getVBEInfo( &vbeInfo );␊ |
193 | ␉if ( err != errSuccess )␊ |
194 | ␉{␊ |
195 | ␉␉return 0;␊ |
196 | ␉}␊ |
197 | ␊ |
198 | ␉char *buff=malloc(sizeof(char)*3072);␊ |
199 | ␉if(!buff)␊ |
200 | ␉{␊ |
201 | ␉␉return 0;␊ |
202 | ␉}␊ |
203 | ␊ |
204 | ␉int bufflen = 0;␊ |
205 | ␊ |
206 | ␉// Loop through the mode list, and find the matching mode.␊ |
207 | ␉for ( modePtr = VBEDecodeFP( unsigned short *, vbeInfo.VideoModePtr );␊ |
208 | ␉␉(*modePtr != modeEndOfList) && (bufflen < 3072); /* prevent buffer overrun */␊ |
209 | ␉␉modePtr++ )␊ |
210 | ␉{␊ |
211 | ␉␉// Get mode information.␊ |
212 | ␊ |
213 | ␉␉bzero( &modeInfo, sizeof(modeInfo) );␊ |
214 | ␉␉err = getVBEModeInfo( *modePtr, &modeInfo );␊ |
215 | ␉␉if ( err != errSuccess )␊ |
216 | ␉␉{␊ |
217 | ␉␉␉continue;␊ |
218 | ␉␉}␊ |
219 | ␊ |
220 | ␉␉bufflen += ␊ |
221 | ␉␉␉snprintf(buff+bufflen, 3072-bufflen, "Mode %x: %dx%dx%d mm:%d attr:%x\n",␊ |
222 | ␉␉␉*modePtr, modeInfo.XResolution, modeInfo.YResolution,␊ |
223 | ␉␉␉modeInfo.BitsPerPixel, modeInfo.MemoryModel,␊ |
224 | ␉␉␉modeInfo.ModeAttributes);␊ |
225 | ␉}␊ |
226 | ␉return buff;␊ |
227 | }␊ |
228 | ␊ |
229 | //==========================================================================␊ |
230 | // getVESAModeWithProperties␊ |
231 | //␊ |
232 | // Return the VESA mode that matches the properties specified.␊ |
233 | // If a mode is not found, then return the "best" available mode.␊ |
234 | ␊ |
235 | static unsigned short getVESAModeWithProperties( unsigned short␉width,␊ |
236 | unsigned short␉height,␊ |
237 | unsigned char␉bitsPerPixel,␊ |
238 | unsigned short␉attributesSet,␊ |
239 | unsigned short␉attributesClear,␊ |
240 | VBEModeInfoBlock␉*outModeInfo,␊ |
241 | unsigned short␉*vesaVersion␊ |
242 | ␉␉␉)␊ |
243 | {␊ |
244 | ␉VBEInfoBlock vbeInfo;␊ |
245 | ␉unsigned short * modePtr;␊ |
246 | ␉VBEModeInfoBlock modeInfo;␊ |
247 | ␉unsigned char modeBitsPerPixel;␊ |
248 | ␉unsigned short matchedMode = modeEndOfList;␊ |
249 | ␉int err;␊ |
250 | ␊ |
251 | ␉// Clear output mode info.␊ |
252 | ␊ |
253 | ␉bzero( outModeInfo, sizeof(*outModeInfo) );␊ |
254 | ␊ |
255 | ␉// Get VBE controller info containing the list of supported modes.␊ |
256 | ␊ |
257 | ␉bzero( &vbeInfo, sizeof(vbeInfo) );␊ |
258 | ␉strcpy( (char*)&vbeInfo, "VBE2" );␊ |
259 | ␉err = getVBEInfo( &vbeInfo );␊ |
260 | ␉if ( err != errSuccess )␊ |
261 | ␉{␊ |
262 | ␉␉return modeEndOfList;␊ |
263 | ␉}␊ |
264 | ␊ |
265 | ␉// Report the VESA major/minor version number.␊ |
266 | ␊ |
267 | ␉if (vesaVersion)␊ |
268 | ␉{␊ |
269 | ␉␉*vesaVersion = vbeInfo.VESAVersion;␊ |
270 | ␉}␊ |
271 | ␊ |
272 | ␉// Loop through the mode list, and find the matching mode.␊ |
273 | ␊ |
274 | ␉for ( modePtr = VBEDecodeFP( unsigned short *, vbeInfo.VideoModePtr ); *modePtr != modeEndOfList; modePtr++ )␊ |
275 | ␉{␊ |
276 | ␉␉// Get mode information.␊ |
277 | ␊ |
278 | ␉␉bzero( &modeInfo, sizeof(modeInfo) );␊ |
279 | ␊ |
280 | ␉␉err = getVBEModeInfo( *modePtr, &modeInfo );␊ |
281 | ␉␉if ( err != errSuccess )␊ |
282 | ␉␉{␊ |
283 | ␉␉␉continue;␊ |
284 | ␉␉}␊ |
285 | ␊ |
286 | #if DEBUG␊ |
287 | ␉␉printf("Mode %x: %dx%dx%d mm:%d attr:%x\n",␊ |
288 | ␉␉␉*modePtr, modeInfo.XResolution, modeInfo.YResolution,␊ |
289 | ␉␉␉modeInfo.BitsPerPixel, modeInfo.MemoryModel,␊ |
290 | ␉␉␉modeInfo.ModeAttributes);␊ |
291 | #endif␊ |
292 | ␊ |
293 | ␉␉// Filter out unwanted modes based on mode attributes.␊ |
294 | ␊ |
295 | ␉␉if (((modeInfo.ModeAttributes & attributesSet) != attributesSet) || ((modeInfo.ModeAttributes & attributesClear) != 0))␊ |
296 | ␉␉{␊ |
297 | ␉␉␉continue;␊ |
298 | ␉␉}␊ |
299 | ␊ |
300 | ␉␉// Pixel depth in bits.␊ |
301 | ␊ |
302 | ␉␉modeBitsPerPixel = modeInfo.BitsPerPixel;␊ |
303 | ␊ |
304 | ␉␉if ((modeBitsPerPixel == 4) && (modeInfo.MemoryModel == 0))␊ |
305 | ␉␉{␊ |
306 | ␉␉␉// Text mode, 16 colors.␊ |
307 | ␉␉}␊ |
308 | ␉␉␉else if ((modeBitsPerPixel == 8) && (modeInfo.MemoryModel == 4))␊ |
309 | ␉␉{␊ |
310 | ␉␉␉// Packed pixel, 256 colors.␊ |
311 | ␉␉}␊ |
312 | ␉␉else if (((modeBitsPerPixel == 16) || (modeBitsPerPixel == 15)) && (modeInfo.MemoryModel == 6) && (modeInfo.RedMaskSize == 5) &&␊ |
313 | ␉␉␉(modeInfo.GreenMaskSize == 5) && (modeInfo.BlueMaskSize == 5))␊ |
314 | ␉␉{␊ |
315 | ␉␉␉// Direct color, 16 bpp (1:5:5:5).␊ |
316 | ␉␉␉modeInfo.BitsPerPixel = modeBitsPerPixel = 16;␊ |
317 | ␉␉}␊ |
318 | ␉␉else if ((modeBitsPerPixel == 32) && (modeInfo.MemoryModel == 6) && (modeInfo.RedMaskSize == 8) &&␊ |
319 | ␉␉␉(modeInfo.GreenMaskSize == 8) && (modeInfo.BlueMaskSize == 8))␊ |
320 | ␉␉{␊ |
321 | ␉␉␉// Direct color, 32 bpp (8:8:8:8).␊ |
322 | ␉␉}␊ |
323 | ␉␉else␊ |
324 | ␉␉{␊ |
325 | ␉␉␉continue;␉// Not a supported mode.␊ |
326 | ␉␉}␊ |
327 | ␊ |
328 | ␉␉// Modes larger than the specified dimensions are skipped.␊ |
329 | ␊ |
330 | ␉␉if ((modeInfo.XResolution > width) || (modeInfo.YResolution > height))␊ |
331 | ␉␉{␊ |
332 | ␉␉␉continue;␊ |
333 | ␉␉}␊ |
334 | ␊ |
335 | ␉␉// Perfect match, we're done looking.␊ |
336 | ␊ |
337 | ␉␉if (modeInfo.XResolution == width && modeInfo.YResolution == height && modeBitsPerPixel == bitsPerPixel)␊ |
338 | ␉␉{␊ |
339 | ␉␉␉matchedMode = *modePtr;␊ |
340 | ␉␉␉bcopy(&modeInfo, outModeInfo, sizeof(modeInfo));␊ |
341 | ␉␉␉break;␊ |
342 | ␉␉}␊ |
343 | ␊ |
344 | ␉␉// Save the next "best" mode in case a perfect match is not found.␊ |
345 | ␊ |
346 | ␉␉if (modeInfo.XResolution == outModeInfo->XResolution && modeInfo.YResolution == outModeInfo->YResolution &&␊ |
347 | ␉␉␉modeBitsPerPixel <= outModeInfo->BitsPerPixel)␊ |
348 | ␉␉{␊ |
349 | ␉␉␉continue;␉// Saved mode has more depth.␊ |
350 | ␉␉}␊ |
351 | ␊ |
352 | ␉␉if (modeInfo.XResolution < outModeInfo->XResolution || modeInfo.YResolution < outModeInfo->YResolution ||␊ |
353 | ␉␉␉modeBitsPerPixel < outModeInfo->BitsPerPixel)␊ |
354 | ␉␉{␊ |
355 | ␉␉␉continue;␉// Saved mode has more resolution.␊ |
356 | ␉␉}␊ |
357 | ␊ |
358 | ␉␉matchedMode = *modePtr;␊ |
359 | ␉␉bcopy(&modeInfo, outModeInfo, sizeof(modeInfo));␊ |
360 | ␉}␊ |
361 | ␊ |
362 | ␉return matchedMode;␊ |
363 | }␊ |
364 | ␊ |
365 | //==========================================================================␊ |
366 | // setupPalette␊ |
367 | /*␊ |
368 | static void setupPalette( VBEPalette *p, const unsigned char *g )␊ |
369 | {␊ |
370 | int i;␊ |
371 | unsigned char * source = (unsigned char *) g;␊ |
372 | ␊ |
373 | for (i = 0; i < 256; i++)␊ |
374 | {␊ |
375 | (*p)[i] = 0;␊ |
376 | (*p)[i] |= ((unsigned long)((*source++) >> 2)) << 16; // Red␊ |
377 | (*p)[i] |= ((unsigned long)((*source++) >> 2)) << 8; // Green␊ |
378 | (*p)[i] |= ((unsigned long)((*source++) >> 2)); // Blue␊ |
379 | }␊ |
380 | }␊ |
381 | */␊ |
382 | //==========================================================================␊ |
383 | // Simple decompressor for boot images encoded in RLE format.␊ |
384 | ␊ |
385 | char *decodeRLE( const void *rleData, int rleBlocks, int outBytes )␊ |
386 | {␊ |
387 | char *out, *cp;␊ |
388 | ␊ |
389 | struct RLEBlock {␊ |
390 | unsigned char count;␊ |
391 | unsigned char value;␊ |
392 | } * bp = (struct RLEBlock *) rleData;␊ |
393 | ␊ |
394 | out = cp = malloc( outBytes );␊ |
395 | if ( out == NULL ) return NULL;␊ |
396 | ␊ |
397 | while ( rleBlocks-- )␊ |
398 | {␊ |
399 | memset( cp, bp->value, bp->count );␊ |
400 | cp += bp->count;␊ |
401 | bp++;␊ |
402 | }␊ |
403 | ␊ |
404 | return out;␊ |
405 | }␊ |
406 | ␊ |
407 | //==========================================================================␊ |
408 | // setVESAGraphicsMode␊ |
409 | ␊ |
410 | static int setVESAGraphicsMode( unsigned short width, unsigned short height, unsigned char bitsPerPixel, unsigned short refreshRate )␊ |
411 | {␊ |
412 | ␉VBEModeInfoBlock minfo;␊ |
413 | ␉unsigned short mode = 280; // Default to 1024 * 768 * 32 (1920 * 1200 * 32 would be 330)␊ |
414 | ␉unsigned short vesaVersion;␊ |
415 | ␉int err = errFuncNotSupported;␊ |
416 | ␊ |
417 | ␉do {␊ |
418 | ␉␉mode = getVESAModeWithProperties( width, height, bitsPerPixel,␊ |
419 | maColorModeBit | maModeIsSupportedBit | maGraphicsModeBit | maLinearFrameBufferAvailBit,␊ |
420 | 0, &minfo, &vesaVersion );␊ |
421 | ␉␉if ( mode == modeEndOfList )␊ |
422 | ␉␉{␊ |
423 | ␉␉␉break;␊ |
424 | ␉␉}␊ |
425 | ␊ |
426 | ␉␉// Set the mode with default refresh rate.␊ |
427 | ␊ |
428 | ␉␉err = setVBEMode(mode | kLinearFrameBufferBit, NULL);␊ |
429 | ␊ |
430 | ␉␉if (err != errSuccess)␊ |
431 | ␉␉{␊ |
432 | ␉␉␉break;␊ |
433 | ␉␉}␊ |
434 | ␊ |
435 | ␉␉// Is this required for buggy Video BIOS implementations? If so for which adapter?␊ |
436 | ␊ |
437 | ␉␉if (minfo.BytesPerScanline == 0)␊ |
438 | ␉␉{␊ |
439 | ␉␉␉minfo.BytesPerScanline = (minfo.XResolution * minfo.BitsPerPixel) >> 3; // ((1920 * 32 = 61440) >> 3) = 7680␊ |
440 | ␉␉}␊ |
441 | ␊ |
442 | ␉␉// Update bootArgs with the data provided by the selected VESA mode.␊ |
443 | ␉␉bootArgs->Video.v_display␉= GRAPHICS_MODE;␊ |
444 | ␉␉bootArgs->Video.v_width␉␉= minfo.XResolution;␉␉/* 1920 or 1600 */␊ |
445 | ␉␉bootArgs->Video.v_height␉= minfo.YResolution;␉␉/* 1200 or 900 */␊ |
446 | ␉␉bootArgs->Video.v_depth␉␉= minfo.BitsPerPixel;␉␉/* 32 */␊ |
447 | ␉␉bootArgs->Video.v_rowBytes␉= minfo.BytesPerScanline;␉/* 7680 or 6400 */␊ |
448 | ␉␉bootArgs->Video.v_baseAddr␉= VBEMakeUInt32(minfo.PhysBasePtr);␊ |
449 | ␊ |
450 | ␉} while ( 0 );␊ |
451 | ␊ |
452 | ␉return err;␊ |
453 | }␊ |
454 | ␊ |
455 | //==============================================================================␊ |
456 | ␊ |
457 | int convertImage( unsigned short width, unsigned short height, const unsigned char *imageData, unsigned char **newImageData )␊ |
458 | {␊ |
459 | ␉int index = 0;␊ |
460 | ␉int size = (width * height); // 16384␊ |
461 | ␉int depth = VIDEO(depth);␊ |
462 | ␊ |
463 | ␉unsigned char *img = 0;␊ |
464 | ␉unsigned long *img32;␊ |
465 | ␊ |
466 | ␉switch (depth)␊ |
467 | ␉{␊ |
468 | ␉␉case 32:␊ |
469 | ␉␉␉img32 = malloc(size * 4);␊ |
470 | ␊ |
471 | ␉␉␉if (!img32)␊ |
472 | ␉␉␉{␊ |
473 | ␉␉␉␉break;␊ |
474 | ␉␉␉}␊ |
475 | ␊ |
476 | ␉␉␉for (; index < size; index++)␊ |
477 | ␉␉␉{␊ |
478 | ␉␉␉␉img32[index] = lookUpCLUTIndex(imageData[index]);␊ |
479 | ␉␉␉}␊ |
480 | ␊ |
481 | ␉␉␉img = (unsigned char *)img32;␊ |
482 | ␉␉␉break;␊ |
483 | ␉}␊ |
484 | ␊ |
485 | ␉*newImageData = img;␊ |
486 | ␊ |
487 | ␉return 0;␊ |
488 | }␊ |
489 | ␊ |
490 | //==============================================================================␊ |
491 | ␊ |
492 | int loadPngImage(const char *filename, uint16_t *width, uint16_t *height, uint8_t **imageData)␊ |
493 | {␊ |
494 | uint8_t *pngData = NULL;␊ |
495 | int pngFile = 0, pngSize;␊ |
496 | PNG_info_t *info;␊ |
497 | int error = 0;␊ |
498 | ␊ |
499 | pngFile = open_bvdev("bt(0,0)", filename, 0);␊ |
500 | if (pngFile == -1) {␊ |
501 | error = -1;␊ |
502 | goto failed;␊ |
503 | }␊ |
504 | pngSize = file_size(pngFile);␊ |
505 | if (!pngSize) {␊ |
506 | error = -1;␊ |
507 | goto failed;␊ |
508 | }␊ |
509 | pngData = malloc(pngSize);␊ |
510 | if (read(pngFile, (char *) pngData, pngSize) != pngSize) {␊ |
511 | error = -1;␊ |
512 | goto failed;␊ |
513 | }␊ |
514 | ␊ |
515 | PNG_error = -1;␊ |
516 | info = PNG_decode(pngData, pngSize);␊ |
517 | if (PNG_error != 0) {␊ |
518 | error = PNG_error;␊ |
519 | goto failed;␊ |
520 | } else if ((info->width > 0xffff) || (info->height > 0xffff)) {␊ |
521 | error = -1;␊ |
522 | goto failed;␊ |
523 | } else if ((info->width * info->height * 4) != info->image->size) {␊ |
524 | error = -1;␊ |
525 | goto failed;␊ |
526 | }␊ |
527 | ␉uint8_t *result = malloc(info->width*4*info->height);␊ |
528 | *width = info->width;␊ |
529 | *height = info->height;␊ |
530 | ␉memcpy(result, info->image->data, info->width*4*info->height);␊ |
531 | ␉*imageData = result;␊ |
532 | ␊ |
533 | failed:␊ |
534 | ␉png_alloc_free_all();␊ |
535 | if (pngData)␊ |
536 | free(pngData);␊ |
537 | if (pngFile != -1)␊ |
538 | close(pngFile);␊ |
539 | ␊ |
540 | return error;␊ |
541 | }␊ |
542 | ␊ |
543 | //==============================================================================␊ |
544 | ␊ |
545 | int loadEmbeddedPngImage(uint8_t *pngData, int pngSize, uint16_t *width, uint16_t *height, uint8_t **imageData)␊ |
546 | {␊ |
547 | PNG_info_t *info;␊ |
548 | int error = 0;␊ |
549 | ␉␊ |
550 | PNG_error = -1;␊ |
551 | info = PNG_decode(pngData, pngSize);␊ |
552 | ␉if (PNG_error != 0) {␊ |
553 | error = PNG_error;␊ |
554 | goto failed;␊ |
555 | } else if ((info->width > 0xffff) || (info->height > 0xffff)) {␊ |
556 | error = -1;␊ |
557 | goto failed;␊ |
558 | } else if ((info->width * info->height * 4) != info->image->size) {␊ |
559 | error = -1;␊ |
560 | goto failed;␊ |
561 | }␊ |
562 | ␉uint8_t *result = malloc(info->width*4*info->height);␊ |
563 | ␉*width = info->width;␊ |
564 | *height = info->height;␊ |
565 | ␉memcpy(result, info->image->data, info->width*4*info->height);␊ |
566 | ␉*imageData = result;␊ |
567 | ␊ |
568 | failed:␊ |
569 | ␉png_alloc_free_all();␊ |
570 | ␊ |
571 | return error;␊ |
572 | }␊ |
573 | ␊ |
574 | //==============================================================================␊ |
575 | ␊ |
576 | void blendImage(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t *data)␊ |
577 | {␊ |
578 | uint16_t drawWidth;␊ |
579 | uint8_t *vram = (uint8_t *) VIDEO(baseAddr) + VIDEO(rowBytes) * y + 4 * x;␊ |
580 | ␊ |
581 | drawWidth = MIN(width, VIDEO(width) - x);␊ |
582 | height = MIN(height, VIDEO(height) - y);␊ |
583 | while (height--) {␊ |
584 | ␉␉switch (VIDEO (depth))␊ |
585 | ␉␉{␊ |
586 | ␉␉␉case 32: /* Optimized version*/␊ |
587 | ␉␉␉{␊ |
588 | ␉␉␉␉uint32_t s; uint32_t* d; // Source (img) and destination (bkgd) pixels␊ |
589 | ␉␉␉␉uint32_t a; // Alpha␊ |
590 | ␉␉␉␉uint32_t dstrb, dstg, srcrb, srcg, drb, dg, rb, g, tempB; // Intermediate variables␊ |
591 | ␉␉␉␉uint16_t pos;␊ |
592 | ␊ |
593 | ␉␉␉␉for (pos = 0; pos < drawWidth * 4; pos += 4) {␊ |
594 | ␉␉␉␉␉// Fast pseudo-vector alpha blending, adapted from: http://www.stereopsis.com/doubleblend.html␊ |
595 | ␉␉␉␉␉s = *((uint32_t*) (data + pos));␊ |
596 | ␉␉␉␉␉d = (uint32_t*) (vram + pos);␊ |
597 | ␊ |
598 | ␉␉␉␉␉// Flip B and R in source␊ |
599 | ␉␉␉␉␉// TODO: use XCHG and inline assembly to do this in a faster, saner way␊ |
600 | ␉␉␉␉␉tempB = (s & 0xFF0000); // save B␊ |
601 | ␉␉␉␉␉s = (s & 0xFF00FFFF) | ((s & 0xFF) << 16); // put R in B␊ |
602 | ␉␉␉␉␉s = (s & 0xFFFFFF00) | (tempB >> 16); // put B in R␊ |
603 | ␊ |
604 | ␉␉␉␉␉a = (s >> 24) + 1;␊ |
605 | ␊ |
606 | ␉␉␉␉␉dstrb = *d & 0xFF00FF; dstg = *d & 0xFF00;␊ |
607 | ␉␉␉␉␉srcrb = s & 0xFF00FF; srcg = s & 0xFF00;␊ |
608 | ␊ |
609 | ␉␉␉␉␉drb = srcrb - dstrb;␊ |
610 | ␉␉␉␉␉dg = srcg - dstg;␊ |
611 | ␉␉␉␉␉drb *= a; dg *= a;␊ |
612 | ␉␉␉␉␉drb >>= 8; dg >>= 8;␊ |
613 | ␊ |
614 | ␉␉␉␉␉rb = (drb + dstrb) & 0xFF00FF;␊ |
615 | ␉␉␉␉␉g = (dg + dstg) & 0xFF00;␊ |
616 | ␊ |
617 | ␉␉␉␉␉*d = rb | g;␊ |
618 | ␉␉␉␉}␊ |
619 | ␉␉␉}␊ |
620 | ␉␉␉break;␊ |
621 | ␊ |
622 | ␉␉␉default: /*Universal version*/␊ |
623 | ␉␉␉{␊ |
624 | ␉␉␉␉uint32_t s; ␊ |
625 | ␉␉␉␉uint32_t a; // Alpha␊ |
626 | ␉␉␉␉uint32_t dr, dg, db, sr, sg, sb; // Intermediate variables␊ |
627 | ␉␉␉␉uint16_t pos;␊ |
628 | ␉␉␉␉int bpp = (VIDEO (depth) + 7)/8;␊ |
629 | ␊ |
630 | ␉␉␉␉for (pos = 0; pos < drawWidth; pos ++) {␊ |
631 | ␉␉␉␉␉// Fast pseudo-vector alpha blending, adapted from: http://www.stereopsis.com/doubleblend.html␊ |
632 | ␉␉␉␉␉s = *((uint32_t*) (data + 4*pos));␊ |
633 | ␉␉␉␉␉␊ |
634 | ␉␉␉␉␉sb = (s & 0xFF0000) >> 16;␊ |
635 | ␉␉␉␉␉sg = (s & 0xFF00) >> 8;␊ |
636 | ␉␉␉␉␉sr = (s & 0xFF);␊ |
637 | ␉␉␉␉␉␊ |
638 | ␉␉␉␉␉a = (s >> 24) + 1;␊ |
639 | ␊ |
640 | ␉␉␉␉␉switch (VIDEO (depth))␊ |
641 | ␉␉␉␉␉{␊ |
642 | ␉␉␉␉␉␉case 24:␊ |
643 | ␉␉␉␉␉␉␉db = ((*(uint32_t *)(vram + bpp*pos))&0xff);␊ |
644 | ␉␉␉␉␉␉␉dg = ((*(uint32_t *)(vram + bpp*pos))&0xff00)>>8;␊ |
645 | ␉␉␉␉␉␉␉dr = ((*(uint32_t *)(vram + bpp*pos))&0xff0000)>>16;␊ |
646 | ␉␉␉␉␉␉␉break;␊ |
647 | ␉␉␉␉␉␉case 16://16-bit seems to be 15-bit␊ |
648 | /*␉␉␉␉␉␉␉db = ((*(uint16_t *)(vram + bpp*pos))&0x1f)<<3;␊ |
649 | ␉␉␉␉␉␉␉dg = ((*(uint16_t *)(vram + bpp*pos))&0x07e0)>>3;␊ |
650 | ␉␉␉␉␉␉␉dr = ((*(uint16_t *)(vram + bpp*pos))&0xf800)>>8;␊ |
651 | ␉␉␉␉␉␉␉break;␉␉␉␉␉␉␉*/␊ |
652 | ␉␉␉␉␉␉case 15:␊ |
653 | ␉␉␉␉␉␉␉db = ((*(uint16_t *)(vram + bpp*pos))&0x1f)<<3;␊ |
654 | ␉␉␉␉␉␉␉dg = ((*(uint16_t *)(vram + bpp*pos))&0x03e0)>>2;␊ |
655 | ␉␉␉␉␉␉␉dr = ((*(uint16_t *)(vram + bpp*pos))&0x7c00)>>7;␊ |
656 | ␉␉␉␉␉␉␉break;␊ |
657 | ␉␉␉␉␉␉default:␊ |
658 | ␉␉␉␉␉␉␉return;␊ |
659 | ␉␉␉␉␉}␊ |
660 | ␊ |
661 | ␉␉␉␉␉dr = (((sr - dr) * a) >> 8) + dr;␊ |
662 | ␉␉␉␉␉dg = (((sg - dg) * a) >> 8) + dg;␊ |
663 | ␉␉␉␉␉db = (((sb - db) * a) >> 8) + db;␊ |
664 | ␉␉␉␉␉switch (VIDEO (depth))␊ |
665 | ␉␉␉␉␉{␊ |
666 | ␉␉␉␉␉␉case 24:␊ |
667 | ␉␉␉␉␉␉␉*(uint32_t *)(vram + bpp*pos) = (*(uint32_t *)(vram + bpp*pos) &0xff000000)␊ |
668 | ␉␉␉␉␉␉␉| (db&0xff) | ((dg&0xff)<<8) | ((dr&0xff)<<16);␊ |
669 | ␉␉␉␉␉␉␉break;␊ |
670 | ␉␉␉␉␉␉case 16:␊ |
671 | //␉␉␉␉␉␉␉*(uint16_t *)(vram + bpp*pos) = ((db&0xf8)>>3) | ((dg&0xfc)<<3) | ((dr&0xf8)<<8);␊ |
672 | //␉␉␉␉␉␉␉break;␉␉␉␉␉␉␉␊ |
673 | ␉␉␉␉␉␉case 15:␊ |
674 | ␉␉␉␉␉␉␉*(uint16_t *)(vram + bpp*pos) = ((db&0xf8)>>3) | ((dg&0xf8)<<2) | ((dr&0xf8)<<7);␊ |
675 | ␉␉␉␉␉␉␉break;␉␉␉␉␉␉␉␉␉␉␉␉␉␉␊ |
676 | ␉␉␉␉␉}␊ |
677 | ␊ |
678 | ␉␉␉␉}␊ |
679 | ␉␉␉}␊ |
680 | ␉␉␉break;␊ |
681 | }␊ |
682 | vram += VIDEO(rowBytes);␊ |
683 | data += width * 4;␊ |
684 | }␊ |
685 | }␊ |
686 | ␊ |
687 | //==============================================================================␊ |
688 | // ProgressBar␊ |
689 | void drawCheckerBoard()␊ |
690 | {␊ |
691 | uint32_t *vram = (uint32_t *) VIDEO(baseAddr);␊ |
692 | uint16_t x, y;␊ |
693 | uint8_t color;␊ |
694 | ␊ |
695 | for (y = 0; y < VIDEO(height); y++, vram += VIDEO(width)) {␊ |
696 | for (x = 0; x < VIDEO(width); x++) {␊ |
697 | color = 204 + 51 * (((x / 8) % 2) == ((y / 8) % 2));␊ |
698 | vram[x] = (color << 16) | (color << 8) | color;␊ |
699 | }␊ |
700 | }␊ |
701 | }␊ |
702 | ␊ |
703 | //==========================================================================␊ |
704 | // LookUpCLUTIndex␊ |
705 | ␊ |
706 | unsigned long lookUpCLUTIndex( unsigned char index )␊ |
707 | {␊ |
708 | ␉long colorIndex = (index * 3);␊ |
709 | ␉long red;␊ |
710 | ␉long green;␊ |
711 | ␉long blue;␊ |
712 | ␊ |
713 | ␉red = AppleLogoClut[ colorIndex ];␊ |
714 | ␉green = AppleLogoClut[ colorIndex++ ];␊ |
715 | ␉blue = AppleLogoClut[ colorIndex++ ];␊ |
716 | ␊ |
717 | ␉return (red << 16) | (green << 8) | blue;␊ |
718 | }␊ |
719 | ␊ |
720 | //==========================================================================␊ |
721 | ␊ |
722 | void *stosl(void *dst, long val, long len)␊ |
723 | {␊ |
724 | ␉asm volatile ( "rep; stosl"␊ |
725 | ␉␉␉␉ : "=c" (len), "=D" (dst)␊ |
726 | ␉␉␉␉ : "0" (len), "1" (dst), "a" (val)␊ |
727 | ␉␉␉␉ : "memory" );␊ |
728 | ␊ |
729 | ␉return dst;␊ |
730 | }␊ |
731 | ␊ |
732 | //==============================================================================␊ |
733 | ␊ |
734 | void setBackgroundColor( uint32_t color )␊ |
735 | {␊ |
736 | ␉long␉pixelBytes = VIDEO(depth) / 8;␊ |
737 | ␉char␉*vram = (char *) VIDEO(baseAddr) + VIDEO(rowBytes) + pixelBytes;␊ |
738 | ␊ |
739 | ␉int width = VIDEO(width);␊ |
740 | ␉int height = VIDEO(height);␊ |
741 | ␊ |
742 | ␉int rem = ( pixelBytes * width ) % 4;␊ |
743 | ␉int length = pixelBytes * width / 4;␊ |
744 | ␊ |
745 | ␉bcopy( &color, vram, rem );␊ |
746 | ␊ |
747 | ␉while ( height-- )␊ |
748 | ␉{␊ |
749 | ␉␉stosl( vram + rem, color, length );␊ |
750 | ␉␉vram += VIDEO(rowBytes);␊ |
751 | ␉}␊ |
752 | }␊ |
753 | ␊ |
754 | //==========================================================================␊ |
755 | // drawDataRectangle␊ |
756 | ␊ |
757 | void drawDataRectangle( unsigned short x, unsigned short y, unsigned short width, unsigned short height, unsigned char *data )␊ |
758 | {␊ |
759 | ␉unsigned short drawWidth;␊ |
760 | ␊ |
761 | ␉long pixelBytes = VIDEO(depth) / 8;␊ |
762 | ␊ |
763 | ␉unsigned char * vram = (unsigned char *) VIDEO(baseAddr) + VIDEO(rowBytes) * y + pixelBytes * x;␊ |
764 | ␊ |
765 | ␉drawWidth = MIN(width, VIDEO(width) - x);␊ |
766 | ␉height = MIN(height, VIDEO(height) - y);␊ |
767 | ␊ |
768 | ␉while ( height-- )␊ |
769 | ␉{␊ |
770 | ␉␉bcopy( data, vram, width * pixelBytes );␊ |
771 | ␉␉vram += VIDEO(rowBytes);␊ |
772 | ␉␉data += width * pixelBytes;␊ |
773 | ␉}␊ |
774 | }␊ |
775 | ␊ |
776 | //==============================================================================␊ |
777 | ␊ |
778 | void loadImageScale (void *input, int iw, int ih, int ip, void *output, int ow, int oh, int op, int or)␊ |
779 | {␊ |
780 | ␉int x, y, off;␊ |
781 | ␉int red = 0x7f, green = 0x7f, blue = 0x7f;␊ |
782 | ␉for ( x = 0; x < ow; x++)␊ |
783 | ␉␉for ( y = 0; y < oh; y++ )␊ |
784 | ␉␉{␊ |
785 | ␉␉␉off = ( x * iw ) / ow +( ( y * ih ) / oh ) * iw;␊ |
786 | ␉␉␉switch (ip)␊ |
787 | ␉␉␉{␊ |
788 | ␉␉␉␉case 16:␊ |
789 | ␉␉␉␉{␊ |
790 | ␉␉␉␉␉uint16_t val;␊ |
791 | ␉␉␉␉␉val=((uint16_t *)input)[off];␊ |
792 | ␉␉␉␉␉red=(val>>7)&0xf8;␊ |
793 | ␉␉␉␉␉green=(val>>2)&0xf8;␊ |
794 | ␉␉␉␉␉blue=(val<<3)&0xf8;␊ |
795 | ␉␉␉␉␉break;␊ |
796 | ␉␉␉␉}␊ |
797 | ␉␉␉␉case 32:␊ |
798 | ␉␉␉␉{␊ |
799 | ␉␉␉␉␉uint32_t val;␊ |
800 | ␉␉␉␉␉val=((uint32_t *)input)[off];␊ |
801 | ␉␉␉␉␉red=(val>>16)&0xff;␊ |
802 | ␉␉␉␉␉green=(val>>8)&0xff;␊ |
803 | ␉␉␉␉␉blue=(val)&0xff;␊ |
804 | ␉␉␉␉␉break;␊ |
805 | ␉␉␉␉}␊ |
806 | ␉␉␉}␊ |
807 | ␉␉␉char *ptr=(char *)output+x*(op/8)+y*or;␊ |
808 | ␉␉␉switch (op)␊ |
809 | ␉␉␉{␊ |
810 | ␉␉␉␉case 16:␊ |
811 | ␉␉␉␉␉*((uint16_t *)ptr) = ((red & 0xF8) << 7) | ␊ |
812 | ␉␉␉␉␉((green & 0xF8) << 2) |␊ |
813 | ␉␉␉␉␉((blue & 0xF8) >> 3);␊ |
814 | ␉␉␉␉␉break;␊ |
815 | ␉␉␉␉case 32 :␊ |
816 | ␉␉␉␉␉*((uint32_t *)ptr) = (red << 16) | (green << 8) | blue;␊ |
817 | ␉␉␉␉␉break;␊ |
818 | ␉␉␉}␊ |
819 | ␉␉}␊ |
820 | }␊ |
821 | ␊ |
822 | //==============================================================================␊ |
823 | ␊ |
824 | DECLARE_IOHIBERNATEPROGRESSALPHA␊ |
825 | ␊ |
826 | void drawPreview(void *src, uint8_t *saveunder)␊ |
827 | {␊ |
828 | ␉uint8_t *screen;␊ |
829 | ␉uint32_t rowBytes, pixelShift;␊ |
830 | ␉uint32_t x, y;␊ |
831 | ␉int32_t blob;␊ |
832 | ␉uint32_t alpha, in, color, result;␊ |
833 | ␉uint8_t *out;␊ |
834 | ␉void *uncomp;␊ |
835 | ␉int origwidth, origheight, origbpx;␊ |
836 | ␉uint32_t saveindex[kIOHibernateProgressCount] = { 0 };␊ |
837 | ␊ |
838 | ␉if (src && (uncomp=DecompressData(src, &origwidth, &origheight, &origbpx)))␊ |
839 | ␉{␊ |
840 | ␉␉if (!setVESAGraphicsMode(origwidth, origheight, origbpx, 0))␊ |
841 | ␉␉␉if (initGraphicsMode () != errSuccess)␊ |
842 | ␉␉␉{␊ |
843 | ␉␉␉␉return;␊ |
844 | ␉␉␉}␊ |
845 | ␊ |
846 | ␉␉screen = (uint8_t *) VIDEO (baseAddr);␊ |
847 | ␉␉rowBytes = VIDEO (rowBytes);␊ |
848 | ␉␉loadImageScale (uncomp, origwidth, origheight, origbpx, screen, VIDEO(width), VIDEO(height), VIDEO(depth), VIDEO (rowBytes));␊ |
849 | ␉}␊ |
850 | ␉else␊ |
851 | ␉{␊ |
852 | ␉␉if (initGraphicsMode () != errSuccess)␊ |
853 | ␉␉{␊ |
854 | ␉␉␉return;␊ |
855 | ␉␉}␊ |
856 | ␊ |
857 | ␉␉screen = (uint8_t *) VIDEO (baseAddr);␊ |
858 | ␉␉rowBytes = VIDEO (rowBytes);␊ |
859 | ␊ |
860 | ␉␉// Set the screen to 75% grey.␊ |
861 | ␉␉setBackgroundColor(0xffbfbfbf);␊ |
862 | ␉}␊ |
863 | ␊ |
864 | ␉pixelShift = VIDEO (depth) >> 4;␊ |
865 | ␉if (pixelShift < 1)␊ |
866 | ␉{␊ |
867 | ␉␉return;␊ |
868 | ␉}␊ |
869 | ␊ |
870 | ␉screen += ((VIDEO (width) ␊ |
871 | ␉␉␉␉- kIOHibernateProgressCount * (kIOHibernateProgressWidth + kIOHibernateProgressSpacing)) << (pixelShift - 1))␊ |
872 | ␉+ (VIDEO (height) - kIOHibernateProgressOriginY - kIOHibernateProgressHeight) * rowBytes;␊ |
873 | ␊ |
874 | ␉for (y = 0; y < kIOHibernateProgressHeight; y++)␊ |
875 | ␉{␊ |
876 | ␉␉out = screen + y * rowBytes;␊ |
877 | ␉␉for (blob = 0; blob < kIOHibernateProgressCount; blob++)␊ |
878 | ␉␉{␊ |
879 | ␉␉␉color = blob ? kIOHibernateProgressDarkGray : kIOHibernateProgressMidGray;␊ |
880 | ␉␉␉for (x = 0; x < kIOHibernateProgressWidth; x++)␊ |
881 | ␉␉␉{␊ |
882 | ␉␉␉␉alpha = gIOHibernateProgressAlpha[y][x];␊ |
883 | ␉␉␉␉result = color;␊ |
884 | ␉␉␉␉if (alpha)␊ |
885 | ␉␉␉␉{␊ |
886 | ␉␉␉␉␉if (0xff != alpha)␊ |
887 | ␉␉␉␉␉{␊ |
888 | ␉␉␉␉␉␉if (1 == pixelShift)␊ |
889 | ␉␉␉␉␉␉{␊ |
890 | ␉␉␉␉␉␉␉in = *((uint16_t *)out) & 0x1f;␉// 16␊ |
891 | ␉␉␉␉␉␉␉in = (in << 3) | (in >> 2);␊ |
892 | ␉␉␉␉␉␉}␊ |
893 | ␉␉␉␉␉␉else␊ |
894 | ␉␉␉␉␉␉{␊ |
895 | ␉␉␉␉␉␉␉in = *((uint32_t *)out) & 0xff;␉// 32␊ |
896 | ␉␉␉␉␉␉}␊ |
897 | ␊ |
898 | ␉␉␉␉␉␉saveunder[blob * kIOHibernateProgressSaveUnderSize + saveindex[blob]++] = in;␊ |
899 | ␉␉␉␉␉␉result = ((255 - alpha) * in + alpha * result + 0xff) >> 8;␊ |
900 | ␉␉␉␉␉}␊ |
901 | ␉␉␉␉␉if (1 == pixelShift)␊ |
902 | ␉␉␉␉␉{␊ |
903 | ␉␉␉␉␉␉result >>= 3;␊ |
904 | ␉␉␉␉␉␉*((uint16_t *)out) = (result << 10) | (result << 5) | result;␉// 16␊ |
905 | ␉␉␉␉␉}␊ |
906 | ␉␉␉␉␉else␊ |
907 | ␉␉␉␉␉{␊ |
908 | ␉␉␉␉␉␉*((uint32_t *)out) = (result << 16) | (result << 8) | result;␉// 32␊ |
909 | ␉␉␉␉␉}␊ |
910 | ␊ |
911 | ␉␉␉␉}␊ |
912 | ␉␉␉␉out += (1 << pixelShift);␊ |
913 | ␉␉␉}␊ |
914 | ␉␉␉out += (kIOHibernateProgressSpacing << pixelShift);␊ |
915 | ␉␉}␊ |
916 | ␉}␊ |
917 | }␊ |
918 | ␊ |
919 | //==============================================================================␊ |
920 | ␊ |
921 | void updateProgressBar(uint8_t *saveunder, int32_t firstBlob, int32_t select)␊ |
922 | {␊ |
923 | ␉uint8_t␉␉*screen;␊ |
924 | ␉uint32_t␉rowBytes, pixelShift;␊ |
925 | ␉uint32_t␉x, y;␊ |
926 | ␉int32_t␉␉blob, lastBlob;␊ |
927 | ␉uint32_t␉alpha, in, color, result;␊ |
928 | ␉uint8_t␉␉*out;␊ |
929 | ␉uint32_t saveindex[kIOHibernateProgressCount] = { 0 };␊ |
930 | ␊ |
931 | ␉pixelShift = VIDEO(depth) >> 4;␊ |
932 | ␉if (pixelShift < 1) return;␊ |
933 | ␉screen = (uint8_t *) VIDEO (baseAddr);␊ |
934 | ␉rowBytes = VIDEO (rowBytes);␊ |
935 | ␊ |
936 | ␉screen += ((VIDEO (width) ␊ |
937 | ␉␉␉␉- kIOHibernateProgressCount * (kIOHibernateProgressWidth + kIOHibernateProgressSpacing)) << (pixelShift - 1))␊ |
938 | ␉+ (VIDEO (height) - kIOHibernateProgressOriginY - kIOHibernateProgressHeight) * rowBytes;␊ |
939 | ␊ |
940 | ␉lastBlob = (select < kIOHibernateProgressCount) ? select : (kIOHibernateProgressCount - 1);␊ |
941 | ␊ |
942 | ␉screen += (firstBlob * (kIOHibernateProgressWidth + kIOHibernateProgressSpacing)) << pixelShift;␊ |
943 | ␊ |
944 | ␉for (y = 0; y < kIOHibernateProgressHeight; y++)␊ |
945 | ␉{␊ |
946 | ␉␉out = screen + y * rowBytes;␊ |
947 | ␉␉for (blob = firstBlob; blob <= lastBlob; blob++)␊ |
948 | ␉␉{␊ |
949 | ␉␉␉color = (blob < select) ? kIOHibernateProgressLightGray : kIOHibernateProgressMidGray;␊ |
950 | ␉␉␉for (x = 0; x < kIOHibernateProgressWidth; x++)␊ |
951 | ␉␉␉{␊ |
952 | ␉␉␉␉alpha = gIOHibernateProgressAlpha[y][x];␊ |
953 | ␉␉␉␉result = color;␊ |
954 | ␉␉␉␉if (alpha)␊ |
955 | ␉␉␉␉{␊ |
956 | ␉␉␉␉␉if (0xff != alpha)␊ |
957 | ␉␉␉␉␉{␊ |
958 | ␉␉␉␉␉␉in = saveunder[blob * kIOHibernateProgressSaveUnderSize + saveindex[blob]++];␊ |
959 | ␉␉␉␉␉␉result = ((255 - alpha) * in + alpha * result + 0xff) / 255;␊ |
960 | ␉␉␉␉␉}␊ |
961 | ␊ |
962 | ␉␉␉␉␉if (1 == pixelShift)␊ |
963 | ␉␉␉␉␉{␊ |
964 | ␉␉␉␉␉␉result >>= 3;␊ |
965 | ␉␉␉␉␉␉*((uint16_t *)out) = (result << 10) | (result << 5) | result;␉// 16␊ |
966 | ␉␉␉␉␉}␊ |
967 | ␉␉␉␉␉else␊ |
968 | ␉␉␉␉␉{␊ |
969 | ␉␉␉␉␉␉*((uint32_t *)out) = (result << 16) | (result << 8) | result;␉// 32␊ |
970 | ␉␉␉␉␉}␊ |
971 | ␉␉␉␉}␊ |
972 | ␉␉␉␉out += (1 << pixelShift);␊ |
973 | ␉␉␉}␊ |
974 | ␉␉␉out += (kIOHibernateProgressSpacing << pixelShift);␊ |
975 | ␉␉}␊ |
976 | ␉}␊ |
977 | }␊ |
978 | ␊ |
979 | //==========================================================================␊ |
980 | // setVESATextMode␊ |
981 | ␊ |
982 | static int setVESATextMode( unsigned short cols, unsigned short rows, unsigned char bitsPerPixel )␊ |
983 | {␊ |
984 | ␉VBEModeInfoBlock minfo;␊ |
985 | ␉unsigned short mode = modeEndOfList;␊ |
986 | ␊ |
987 | ␉if ( (cols != 80) || (rows != 25) ) // not 80x25 mode␊ |
988 | ␉{␊ |
989 | ␉␉mode = getVESAModeWithProperties( cols, rows, bitsPerPixel,␊ |
990 | maColorModeBit |␊ |
991 | maModeIsSupportedBit,␊ |
992 | maGraphicsModeBit,␊ |
993 | &minfo, NULL );␊ |
994 | ␉}␊ |
995 | ␊ |
996 | ␉if ( ( mode == modeEndOfList ) || ( setVBEMode(mode, NULL) != errSuccess ) )␊ |
997 | ␉{␊ |
998 | ␉␉video_mode( 2 ); // VGA BIOS, 80x25 text mode.␊ |
999 | ␉␉minfo.XResolution = 80;␊ |
1000 | ␉␉minfo.YResolution = 25;␊ |
1001 | ␉}␊ |
1002 | ␊ |
1003 | ␉// Update KernBootStruct using info provided by the selected␊ |
1004 | ␉// VESA mode.␊ |
1005 | ␊ |
1006 | ␉bootArgs->Video.v_display = VGA_TEXT_MODE;␊ |
1007 | ␉bootArgs->Video.v_baseAddr = 0xb8000;␊ |
1008 | ␉bootArgs->Video.v_width = minfo.XResolution;␊ |
1009 | ␉bootArgs->Video.v_height = minfo.YResolution;␊ |
1010 | ␉bootArgs->Video.v_depth = 8;␊ |
1011 | ␉bootArgs->Video.v_rowBytes = 0x8000;␊ |
1012 | ␊ |
1013 | ␉return errSuccess; // always return success␊ |
1014 | }␊ |
1015 | ␊ |
1016 | //==========================================================================␊ |
1017 | // getNumberArrayFromProperty␊ |
1018 | ␊ |
1019 | static int getNumberArrayFromProperty( const char *propKey,␊ |
1020 | unsigned long numbers[],␊ |
1021 | unsigned long maxArrayCount )␊ |
1022 | {␊ |
1023 | ␉char␉␉*propStr;␊ |
1024 | ␉unsigned long␉count = 0;␊ |
1025 | ␊ |
1026 | ␉propStr = newStringForKey((char *)propKey , &bootInfo->chameleonConfig);␊ |
1027 | ␊ |
1028 | ␉if (propStr)␊ |
1029 | ␉{␊ |
1030 | ␉␉char *delimiter = propStr;␊ |
1031 | ␉␉char *p = propStr;␊ |
1032 | ␊ |
1033 | ␉␉while ((count < maxArrayCount) && (*p != '\0'))␊ |
1034 | ␉␉{␊ |
1035 | ␉␉␉unsigned long val = strtoul(p, &delimiter, 10);␊ |
1036 | ␉␉␉if (p != delimiter)␊ |
1037 | ␉␉␉{␊ |
1038 | ␉␉␉␉numbers[count++] = val;␊ |
1039 | ␉␉␉␉p = delimiter;␊ |
1040 | ␉␉␉}␊ |
1041 | ␉␉␉while ((*p != '\0') && !isdigit(*p))␊ |
1042 | ␉␉␉␉p++;␊ |
1043 | ␉␉}␊ |
1044 | ␊ |
1045 | ␉␉free(propStr);␊ |
1046 | ␉}␊ |
1047 | ␊ |
1048 | ␉return count;␊ |
1049 | }␊ |
1050 | ␊ |
1051 | //==============================================================================␊ |
1052 | ␊ |
1053 | int initGraphicsMode ()␊ |
1054 | {␊ |
1055 | ␉unsigned long params[4];␊ |
1056 | ␉int count;␊ |
1057 | ␊ |
1058 | ␉params[3] = 0;␊ |
1059 | ␉count = getNumberArrayFromProperty( kGraphicsModeKey, params, 4 );␊ |
1060 | ␊ |
1061 | ␉// Try to find a resolution if "Graphics Mode" setting is not available.␊ |
1062 | ␉if ( count < 3 )␊ |
1063 | ␉{␊ |
1064 | ␉␉// Use the default resolution if we don't have an initialized GUI.␊ |
1065 | ␉␉if (gui.screen.width == 0 || gui.screen.height == 0)␊ |
1066 | ␉␉{␊ |
1067 | ␉␉␉gui.screen.width = DEFAULT_SCREEN_WIDTH;␊ |
1068 | ␉␉␉gui.screen.height = DEFAULT_SCREEN_HEIGHT;␊ |
1069 | ␉␉}␊ |
1070 | ␊ |
1071 | ␉␉params[0] = gui.screen.width;␊ |
1072 | ␉␉params[1] = gui.screen.height;␊ |
1073 | ␉␉params[2] = 32;␊ |
1074 | ␉}␊ |
1075 | ␊ |
1076 | ␉// Map from pixel format to bits per pixel.␊ |
1077 | ␊ |
1078 | ␉if ( params[2] == 256 )␊ |
1079 | ␉␉params[2] = 8;␊ |
1080 | ␊ |
1081 | ␉if ( params[2] == 555 )␊ |
1082 | ␉␉params[2] = 16;␊ |
1083 | ␊ |
1084 | ␉if ( params[2] == 888 )␊ |
1085 | ␉␉params[2] = 32;␊ |
1086 | ␊ |
1087 | ␉return setVESAGraphicsMode( params[0], params[1], params[2], params[3] );␊ |
1088 | }␊ |
1089 | ␊ |
1090 | //==========================================================================␊ |
1091 | // setVideoMode␊ |
1092 | //␊ |
1093 | // Set the video mode to VGA_TEXT_MODE or GRAPHICS_MODE.␊ |
1094 | ␊ |
1095 | void setVideoMode( int mode )␊ |
1096 | {␊ |
1097 | ␉unsigned long params[4];␊ |
1098 | ␉int err = errSuccess;␊ |
1099 | ␊ |
1100 | ␉if ( mode == GRAPHICS_MODE )␊ |
1101 | ␉{␊ |
1102 | ␉␉if ( (err = initGraphicsMode()) == errSuccess )␊ |
1103 | ␉␉{␊ |
1104 | ␉␉␉// Tell the kernel to use text mode on a linear frame buffer display␊ |
1105 | ␉␉␉bootArgs->Video.v_display = (gVerboseMode) ? /* 2 */ FB_TEXT_MODE : /* 1 */ GRAPHICS_MODE;␊ |
1106 | ␉␉}␊ |
1107 | ␉}␊ |
1108 | ␊ |
1109 | ␉if ( (mode == VGA_TEXT_MODE) || (err != errSuccess) )␊ |
1110 | ␉{␊ |
1111 | ␉␉params[0] = 80; // Default text mode is 80x25.␊ |
1112 | ␉␉params[1] = 25;␊ |
1113 | ␊ |
1114 | ␉␉setVESATextMode(params[0], params[1], 4);␊ |
1115 | ␉␉bootArgs->Video.v_display = VGA_TEXT_MODE;␊ |
1116 | ␉}␊ |
1117 | }␊ |
1118 | ␊ |
1119 | //==============================================================================␊ |
1120 | void getGraphicModeParams(unsigned long params[])␊ |
1121 | {␊ |
1122 | ␉params[3] = 0;␊ |
1123 | ␊ |
1124 | ␉VBEModeInfoBlock minfo;␊ |
1125 | ␊ |
1126 | ␉unsigned short vesaVersion;␊ |
1127 | ␉unsigned short mode = modeEndOfList;␊ |
1128 | ␊ |
1129 | ␉getNumberArrayFromProperty(kGraphicsModeKey, params, 4);␊ |
1130 | ␊ |
1131 | ␉mode = getVESAModeWithProperties(params[0], params[1], params[2],␊ |
1132 | ␉␉␉␉␉␉␉␉␉ maColorModeBit |␊ |
1133 | ␉␉␉␉␉␉␉␉␉ maModeIsSupportedBit |␊ |
1134 | ␉␉␉␉␉␉␉␉␉ maGraphicsModeBit |␊ |
1135 | ␉␉␉␉␉␉␉␉␉ maLinearFrameBufferAvailBit,␊ |
1136 | ␉␉␉␉␉␉␉␉␉ 0,␊ |
1137 | ␉␉␉␉␉␉␉␉␉ &minfo, &vesaVersion);␊ |
1138 | ␊ |
1139 | ␉params[0] = minfo.XResolution;␊ |
1140 | ␉params[1] = minfo.YResolution;␊ |
1141 | ␉params[2] = 32;␊ |
1142 | }␊ |
1143 | ␉␊ |
1144 | //==========================================================================␊ |
1145 | // Return the current video mode, VGA_TEXT_MODE or GRAPHICS_MODE.␊ |
1146 | ␊ |
1147 | int getVideoMode(void)␊ |
1148 | {␊ |
1149 | return bootArgs->Video.v_display;␊ |
1150 | }␊ |
1151 | ␊ |
1152 | //==========================================================================␊ |
1153 | // Display and clear the activity indicator.␊ |
1154 | ␊ |
1155 | // BASIC Indicator␊ |
1156 | //static char indicator[] = {'-', '\\', '|', '/', '\0'};␊ |
1157 | ␊ |
1158 | // Bouncing ball .oOo.␊ |
1159 | static char indicator[] = {46, 111, 79, 248, 79, 111, '\0'};␊ |
1160 | ␊ |
1161 | // ┤┘┴└├┌┬┐␊ |
1162 | //static char indicator[] = {180, 217, 193, 192, 195, 218, 194, 191, '\0'};␊ |
1163 | ␊ |
1164 | // To prevent a ridiculously fast-spinning indicator,␊ |
1165 | // ensure a minimum of 1/9 sec between animation frames.␊ |
1166 | #define MIN_TICKS 2␊ |
1167 | ␊ |
1168 | //==============================================================================␊ |
1169 | ␊ |
1170 | void spinActivityIndicator(int sectors)␊ |
1171 | {␊ |
1172 | static unsigned long lastTickTime = 0, currentTickTime;␊ |
1173 | ␊ |
1174 | ␉if (previewTotalSectors && previewSaveunder)␊ |
1175 | ␉{␊ |
1176 | ␉␉int blob, lastBlob;␊ |
1177 | ␊ |
1178 | ␉␉lastBlob = (previewLoadedSectors * kIOHibernateProgressCount) / previewTotalSectors;␊ |
1179 | ␉␉previewLoadedSectors+=sectors;␊ |
1180 | ␉␉blob = (previewLoadedSectors * kIOHibernateProgressCount) / previewTotalSectors;␊ |
1181 | ␉␉␊ |
1182 | ␉␉if (blob!=lastBlob)␊ |
1183 | ␉␉{␊ |
1184 | ␉␉␉updateProgressBar (previewSaveunder, lastBlob, blob);␊ |
1185 | ␉␉}␊ |
1186 | ␉␉return;␊ |
1187 | ␉}␊ |
1188 | ␊ |
1189 | ␉currentTickTime = time18(); // late binding␊ |
1190 | ␉if (currentTickTime < lastTickTime + MIN_TICKS)␊ |
1191 | ␉{␊ |
1192 | ␉␉return;␊ |
1193 | ␉}␊ |
1194 | ␉else␊ |
1195 | ␉{␊ |
1196 | ␉␉lastTickTime = currentTickTime;␊ |
1197 | ␉}␊ |
1198 | ␊ |
1199 | ␉if (getVideoMode() == VGA_TEXT_MODE)␊ |
1200 | ␉{␊ |
1201 | ␉␉if (currentIndicator >= sizeof(indicator))␊ |
1202 | ␉␉{␊ |
1203 | ␉␉␉currentIndicator = 0;␊ |
1204 | ␉␉}␊ |
1205 | ␉␉putchar(indicator[currentIndicator++]);␊ |
1206 | ␉␉putchar('\b');␊ |
1207 | ␉}␊ |
1208 | }␊ |
1209 | ␊ |
1210 | //==============================================================================␊ |
1211 | ␊ |
1212 | void clearActivityIndicator( void )␊ |
1213 | {␊ |
1214 | ␉if ( getVideoMode() == VGA_TEXT_MODE )␊ |
1215 | ␉{␊ |
1216 | ␉␉putchar(' ');␊ |
1217 | ␉␉putchar('\b');␊ |
1218 | ␉}␊ |
1219 | }␊ |
1220 | ␊ |
1221 | //==============================================================================␊ |
1222 | |