CFComparisonResult _compareCFDisplayModes (CGDisplayModeRef *mode1Ptr, CGDisplayModeRef *mode2Ptr, void *context) { CGDisplayModeRef mode1 = (CGDisplayModeRef)mode1Ptr; CGDisplayModeRef mode2 = (CGDisplayModeRef)mode2Ptr; size_t width1 = CGDisplayModeGetWidth(mode1); size_t width2 = CGDisplayModeGetWidth(mode2); if(width1 == width2) { size_t height1 = CGDisplayModeGetWidth(mode1); size_t height2 = CGDisplayModeGetWidth(mode2); if(height1 == height2) { size_t refreshRate1 = CGDisplayModeGetRefreshRate(mode1); size_t refreshRate2 = CGDisplayModeGetRefreshRate(mode2); if(refreshRate1 == refreshRate2) { long bitDepth1 = bitDepth(mode1); long bitDepth2 = bitDepth(mode2); if(bitDepth1 == bitDepth2) return kCFCompareEqualTo; else return (bitDepth1 < bitDepth2) ? kCFCompareLessThan : kCFCompareGreaterThan; } else return (refreshRate1 < refreshRate2) ? kCFCompareLessThan : kCFCompareGreaterThan; } else return (height1 < height2) ? kCFCompareLessThan : kCFCompareGreaterThan; } else return (width1 < width2) ? kCFCompareLessThan : kCFCompareGreaterThan; }
image::image(bool endian, unsigned depth, uint64_t alphaMask, uint64_t redMask, uint64_t greenMask, uint64_t blueMask) { _endian = endian; _depth = depth; _alpha = {alphaMask, bitDepth(alphaMask), bitShift(alphaMask)}; _red = {redMask, bitDepth(redMask), bitShift(redMask )}; _green = {greenMask, bitDepth(greenMask), bitShift(greenMask)}; _blue = {blueMask, bitDepth(blueMask), bitShift(blueMask )}; }
unsigned int listCurrentMode(CGDirectDisplayID display, int displayNum) { unsigned int returncode = 1; CGDisplayModeRef currentMode = CGDisplayCopyDisplayMode(display); if (currentMode == NULL) { NSLog(CFSTR("%s"), "Error: unable to copy current display mode"); returncode = 0; } NSLog(CFSTR("Display %d: %ux%ux%u@%.0f"), displayNum, CGDisplayModeGetWidth(currentMode), CGDisplayModeGetHeight(currentMode), bitDepth(currentMode), CGDisplayModeGetRefreshRate(currentMode)); CGDisplayModeRelease(currentMode); return returncode; }
unsigned int configureDisplay(CGDirectDisplayID display, struct config *config, int displayNum) { unsigned int returncode = 1; CFArrayRef allModes = CGDisplayCopyAllDisplayModes(display, NULL); if (allModes == NULL) { NSLog(CFSTR("Error: failed trying to look up modes for display %u"), displayNum); } CGDisplayModeRef newMode = NULL; CGDisplayModeRef possibleMode; size_t pw; // possible width. size_t ph; // possible height. size_t pd; // possible depth. double pr; // possible refresh rate int looking = 1; // used to decide whether to continue looking for modes. int i; for (i = 0 ; i < CFArrayGetCount(allModes) && looking; i++) { possibleMode = (CGDisplayModeRef)CFArrayGetValueAtIndex(allModes, i); pw = CGDisplayModeGetWidth(possibleMode); ph = CGDisplayModeGetHeight(possibleMode); pd = bitDepth(possibleMode); pr = CGDisplayModeGetRefreshRate(possibleMode); if (pw == config->w && ph == config->h && pd == config->d && pr == config->r) { looking = 0; // Stop looking for more modes! newMode = possibleMode; } } CFRelease(allModes); if (newMode != NULL) { NSLog(CFSTR("set mode on display %u to %ux%ux%u@%.0f"), displayNum, pw, ph, pd, pr); setDisplayToMode(display,newMode); } else { NSLog(CFSTR("Error: mode %ux%ux%u@%f not available on display %u"), config->w, config->h, config->d, config->r, displayNum); returncode = 0; } return returncode; }
unsigned int listAvailableModes(CGDirectDisplayID display, int displayNum) { unsigned int returncode = 1; int i; CFArrayRef allModes = CGDisplayCopyAllDisplayModes(display, NULL); if (allModes == NULL) { returncode = 0; } #ifndef LIST_DEBUG printf("Available Modes on Display %d\n", displayNum); #endif CGDisplayModeRef mode; for (i = 0; i < CFArrayGetCount(allModes) && returncode; i++) { mode = (CGDisplayModeRef) CFArrayGetValueAtIndex(allModes, i); // This formatting is functional but it ought to be done less poorly. #ifndef LIST_DEBUG if (i % MODES_PER_LINE == 0) { printf(" "); } else { printf("\t"); } char modestr [50]; sprintf(modestr, "%lux%lux%lu@%.0f", CGDisplayModeGetWidth(mode), CGDisplayModeGetHeight(mode), bitDepth(mode), CGDisplayModeGetRefreshRate(mode)); printf("%-20s ", modestr); if (i % MODES_PER_LINE == MODES_PER_LINE - 1) { printf("\n"); } #else uint32_t ioflags = CGDisplayModeGetIOFlags(mode); printf("display: %d %4lux%4lux%2lu@%.0f usable:%u ioflags:%4x valid:%u safe:%u default:%u", displayNum, CGDisplayModeGetWidth(mode), CGDisplayModeGetHeight(mode), bitDepth(mode), CGDisplayModeGetRefreshRate(mode), CGDisplayModeIsUsableForDesktopGUI(mode), ioflags, ioflags & kDisplayModeValidFlag ?1:0, ioflags & kDisplayModeSafeFlag ?1:0, ioflags & kDisplayModeDefaultFlag ?1:0 ); printf(" safety:%u alwaysshow:%u nevershow:%u notresize:%u requirepan:%u int:%u simul:%u", ioflags & kDisplayModeSafetyFlags ?1:0, ioflags & kDisplayModeAlwaysShowFlag ?1:0, ioflags & kDisplayModeNeverShowFlag ?1:0, ioflags & kDisplayModeNotResizeFlag ?1:0, ioflags & kDisplayModeRequiresPanFlag ?1:0, ioflags & kDisplayModeInterlacedFlag ?1:0, ioflags & kDisplayModeSimulscanFlag ?1:0 ); printf(" builtin:%u notpreset:%u stretched:%u notgfxqual:%u valagnstdisp:%u tv:%u vldmirror:%u\n", ioflags & kDisplayModeBuiltInFlag ?1:0, ioflags & kDisplayModeNotPresetFlag ?1:0, ioflags & kDisplayModeStretchedFlag ?1:0, ioflags & kDisplayModeNotGraphicsQualityFlag ?1:0, ioflags & kDisplayModeValidateAgainstDisplay ?1:0, ioflags & kDisplayModeTelevisionFlag ?1:0, ioflags & kDisplayModeValidForMirroringFlag ?1:0 ); #endif } CFRelease(allModes); return returncode; }
void force8bit(){ if( bitDepth() < 8 ) png_set_packing( png ); else png_set_strip_16( png ); }
unsigned int listAvailableModes(CGDirectDisplayID display, int displayNum) { unsigned int returncode = 1; int numModes = 0; int i; CFArrayRef allModes = CGDisplayCopyAllDisplayModes(display, NULL); if (allModes == NULL) { returncode = 0; } numModes = CFArrayGetCount(allModes); // sort the array of display modes CFMutableArrayRef allModesSorted = CFArrayCreateMutableCopy( kCFAllocatorDefault, numModes, allModes ); CFArraySortValues( allModesSorted, CFRangeMake(0, CFArrayGetCount(allModesSorted)), (CFComparatorFunction) _compareCFDisplayModes, NULL ); #ifndef LIST_DEBUG if(displayNum != 0) printf("\n\n"); printf("Available Modes on Display %d\n", displayNum); #endif CGDisplayModeRef mode; int modesPerColumn = numModes / MODES_PER_LINE; for (i = 0; (i < numModes) && returncode; i++) { int rowNumber = (i / MODES_PER_LINE); int idxDisplayMode = (i % MODES_PER_LINE) * modesPerColumn + rowNumber; // if there are an even number of display modes to display, // the last mode must have it's index decremented by 1 idxDisplayMode = MIN(idxDisplayMode, numModes - 1); mode = (CGDisplayModeRef) CFArrayGetValueAtIndex(allModesSorted, idxDisplayMode); // This formatting is functional but it ought to be done less poorly. #ifndef LIST_DEBUG if (i % MODES_PER_LINE == 0) { printf(" "); } else { printf("\t"); } char modestr [50]; sprintf(modestr, "%4lux%4lux%lu@%.0f", CGDisplayModeGetWidth(mode), CGDisplayModeGetHeight(mode), bitDepth(mode), CGDisplayModeGetRefreshRate(mode)); printf("%-20s ", modestr); if(i % MODES_PER_LINE == MODES_PER_LINE - 1) printf("\n"); #else uint32_t ioflags = CGDisplayModeGetIOFlags(mode); printf("display: %d %4lux%4lux%2lu@%.0f usable:%u ioflags:%4x valid:%u safe:%u default:%u", displayNum, CGDisplayModeGetWidth(mode), CGDisplayModeGetHeight(mode), bitDepth(mode), CGDisplayModeGetRefreshRate(mode), CGDisplayModeIsUsableForDesktopGUI(mode), ioflags, ioflags & kDisplayModeValidFlag ?1:0, ioflags & kDisplayModeSafeFlag ?1:0, ioflags & kDisplayModeDefaultFlag ?1:0 ); printf(" safety:%u alwaysshow:%u nevershow:%u notresize:%u requirepan:%u int:%u simul:%u", ioflags & kDisplayModeSafetyFlags ?1:0, ioflags & kDisplayModeAlwaysShowFlag ?1:0, ioflags & kDisplayModeNeverShowFlag ?1:0, ioflags & kDisplayModeNotResizeFlag ?1:0, ioflags & kDisplayModeRequiresPanFlag ?1:0, ioflags & kDisplayModeInterlacedFlag ?1:0, ioflags & kDisplayModeSimulscanFlag ?1:0 ); printf(" builtin:%u notpreset:%u stretched:%u notgfxqual:%u valagnstdisp:%u tv:%u vldmirror:%u\n", ioflags & kDisplayModeBuiltInFlag ?1:0, ioflags & kDisplayModeNotPresetFlag ?1:0, ioflags & kDisplayModeStretchedFlag ?1:0, ioflags & kDisplayModeNotGraphicsQualityFlag ?1:0, ioflags & kDisplayModeValidateAgainstDisplay ?1:0, ioflags & kDisplayModeTelevisionFlag ?1:0, ioflags & kDisplayModeValidForMirroringFlag ?1:0 ); #endif } CFRelease(allModes); CFRelease(allModesSorted); return returncode; }