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