def decode_animation(self, file, filename): # If file is not an animated GIF, it will be loaded as a single-frame animation. file_bytes = file.read() data = c_void_p(cf.CFDataCreate(None, file_bytes, len(file_bytes))) sourceRef = c_void_p(quartz.CGImageSourceCreateWithData(data, None)) # Get number of frames in the animation. count = quartz.CGImageSourceGetCount(sourceRef) frames = [] for index in range(count): # Try to determine frame duration from GIF properties dictionary. duration = 0.1 # default duration if none found props = c_void_p(quartz.CGImageSourceCopyPropertiesAtIndex(sourceRef, index, None)) if cf.CFDictionaryContainsKey(props, kCGImagePropertyGIFDictionary): gif_props = c_void_p(cf.CFDictionaryGetValue(props, kCGImagePropertyGIFDictionary)) if cf.CFDictionaryContainsKey(gif_props, kCGImagePropertyGIFDelayTime): duration = cfnumber_to_number(c_void_p(cf.CFDictionaryGetValue(gif_props, kCGImagePropertyGIFDelayTime))) cf.CFRelease(props) image = self._get_pyglet_ImageData_from_source_at_index(sourceRef, index) frames.append( AnimationFrame(image, duration) ) cf.CFRelease(data) cf.CFRelease(sourceRef) return Animation(frames)
def decode(self, file, filename): file_bytes = file.read() data = c_void_p(cf.CFDataCreate(None, file_bytes, len(file_bytes))) # Second argument is an options dictionary. It might be a good idea to provide # a value for kCGImageSourceTypeIdentifierHint here using filename extension. sourceRef = c_void_p(quartz.CGImageSourceCreateWithData(data, None)) image = self._get_pyglet_ImageData_from_source_at_index(sourceRef, 0) cf.CFRelease(data) cf.CFRelease(sourceRef) return image
def _get_elements(self): cfarray = c_void_p(iokit.IOHIDDeviceCopyMatchingElements(self.deviceRef, None, 0)) if not cfarray: # requires "Security & Privacy / Input Monitoring", see #95 return [] elements = cfarray_to_list(cfarray) cf.CFRelease(cfarray) return elements
def get_devices(self): # Tell manager that we are willing to match *any* device. # (Alternatively, we could restrict by device usage, or usage page.) iokit.IOHIDManagerSetDeviceMatching(self.managerRef, None) # Copy the device set and convert it to python. cfset = c_void_p(iokit.IOHIDManagerCopyDevices(self.managerRef)) self.devices = cfset_to_set(cfset) cf.CFRelease(cfset)
def get_modes(self): cgmodes = c_void_p( quartz.CGDisplayCopyAllDisplayModes(self._cg_display_id, None)) modes = [ CocoaScreenMode(self, cgmode) for cgmode in cfarray_to_list(cgmodes) ] cf.CFRelease(cgmodes) return modes
def _get_elements(self): try: cfarray = c_void_p( iokit.IOHIDDeviceCopyMatchingElements(self.deviceRef, None, 0)) elements = cfarray_to_list(cfarray) cf.CFRelease(cfarray) except: return [] return elements
def getBitsPerPixel(self, cgmode): # from /System/Library/Frameworks/IOKit.framework/Headers/graphics/IOGraphicsTypes.h IO8BitIndexedPixels = "PPPPPPPP" IO16BitDirectPixels = "-RRRRRGGGGGBBBBB" IO32BitDirectPixels = "--------RRRRRRRRGGGGGGGGBBBBBBBB" cfstring = c_void_p(quartz.CGDisplayModeCopyPixelEncoding(cgmode)) pixelEncoding = cfstring_to_string(cfstring) cf.CFRelease(cfstring) if pixelEncoding == IO8BitIndexedPixels: return 8 if pixelEncoding == IO16BitDirectPixels: return 16 if pixelEncoding == IO32BitDirectPixels: return 32 return 0
def get_property(self, name): cfname = CFSTR(name) cfvalue = c_void_p(iokit.IOHIDDeviceGetProperty( self.deviceRef, cfname)) cf.CFRelease(cfname) return cftype_to_value(cfvalue)
def get_elements(self): cfarray = c_void_p( iokit.IOHIDDeviceCopyMatchingElements(self.deviceRef, None, 0)) self.elements = cfarray_to_list(cfarray) cf.CFRelease(cfarray)