All Downloads are FREE. Search and download functionalities are using the official Maven repository.

oshi.hardware.platform.mac.MacDisplay Maven / Gradle / Ivy

package oshi.hardware.platform.mac;

import oshi.hardware.Display;
import oshi.hardware.common.AbstractDisplay;
import oshi.jna.platform.mac.CoreFoundation;
import oshi.jna.platform.mac.IOKit;
import oshi.util.platform.mac.CfUtil;
import oshi.util.platform.mac.IOKitUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.PointerByReference;

import java.util.ArrayList;
import java.util.List;

/**
 * @author angju
 * 
 */
public class MacDisplay extends AbstractDisplay {

    private final static Logger LOG = LoggerFactory.getLogger(MacDisplay.class.getName());
    private static final long serialVersionUID = 1L;


    private static final CoreFoundation.CFStringRef cfEdid = CoreFoundation.CFStringRef.toCFString("IODisplayEDID");

    public MacDisplay(byte[] edid) {
        super(edid);
        LOG.debug("Initialized MacDisplay");
    }

    /**
     * Gets Display Information
     *
     * @return An array of Display objects representing monitors, etc.
     */
    public static Display[] getDisplays() {
        List displays = new ArrayList();
        // Iterate IO Registry IODisplayConnect
        IntByReference serviceIterator = new IntByReference();
        IOKitUtil.getMatchingServices("IODisplayConnect", serviceIterator);
        int sdService = IOKit.INSTANCE.IOIteratorNext(serviceIterator.getValue());
        while (sdService != 0) {
            // Display properties are in a child entry
            IntByReference properties = new IntByReference();
            int ret = IOKit.INSTANCE.IORegistryEntryGetChildEntry(sdService, "IOService", properties);
            if (ret == 0) {
                // look up the edid by key
                CoreFoundation.CFTypeRef edid = IOKit.INSTANCE.IORegistryEntryCreateCFProperty(properties.getValue(), cfEdid,
                        CfUtil.ALLOCATOR, 0);
                if (edid != null) {
                    // Edid is a byte array of 128 bytes
                    int length = CoreFoundation.INSTANCE.CFDataGetLength(edid);
                    PointerByReference p = CoreFoundation.INSTANCE.CFDataGetBytePtr(edid);
                    displays.add(new MacDisplay(p.getPointer().getByteArray(0, length)));
                    CfUtil.release(edid);
                }
            }
            // iterate
            IOKit.INSTANCE.IOObjectRelease(sdService);
            sdService = IOKit.INSTANCE.IOIteratorNext(serviceIterator.getValue());
        }
        IOKit.INSTANCE.IOObjectRelease(serviceIterator.getValue());
        return displays.toArray(new Display[displays.size()]);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy