Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Win32 Monitor Configuration API and demo #332

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Features
--------
* Updated AIX natives and build - [@twall](https://github.com/twall).
* [#290](https://github.com/twall/jna/pull/290): Improved the stacktrace for the exceptions thrown by `com.sun.jna.Structure` - [@ebourg](https://github.com/ebourg).
* Added Win32 Monitor Configuration API
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please follow the syntax of other CHANGELOG entries, copy from above. Thx.


Bug Fixes
---------
Expand Down
204 changes: 204 additions & 0 deletions contrib/monitordemo/src/com/sun/jna/contrib/demo/MonitorInfoDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
/*
* Copyright 2014 Martin Steiger
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.sun.jna.contrib.demo;

import com.sun.jna.Memory;
import com.sun.jna.platform.EnumUtils;
import com.sun.jna.platform.win32.Dxva2;
import com.sun.jna.platform.win32.HighLevelMonitorConfigurationAPI;
import com.sun.jna.platform.win32.HighLevelMonitorConfigurationAPI.MC_COLOR_TEMPERATURE;
import com.sun.jna.platform.win32.HighLevelMonitorConfigurationAPI.MC_DISPLAY_TECHNOLOGY_TYPE;
import com.sun.jna.platform.win32.HighLevelMonitorConfigurationAPI.MC_DRIVE_TYPE;
import com.sun.jna.platform.win32.HighLevelMonitorConfigurationAPI.MC_GAIN_TYPE;
import com.sun.jna.platform.win32.HighLevelMonitorConfigurationAPI.MC_POSITION_TYPE;
import com.sun.jna.platform.win32.HighLevelMonitorConfigurationAPI.MC_SIZE_TYPE;
import com.sun.jna.platform.win32.LowLevelMonitorConfigurationAPI.MC_TIMING_REPORT;
import com.sun.jna.platform.win32.PhysicalMonitorEnumerationAPI.PHYSICAL_MONITOR;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WTypes.LPSTR;
import com.sun.jna.platform.win32.WinDef.DWORD;
import com.sun.jna.platform.win32.WinDef.DWORDByReference;
import com.sun.jna.platform.win32.WinDef.HDC;
import com.sun.jna.platform.win32.WinDef.LPARAM;
import com.sun.jna.platform.win32.WinDef.RECT;
import com.sun.jna.platform.win32.WinNT.HANDLE;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.platform.win32.WinUser.HMONITOR;
import com.sun.jna.platform.win32.WinUser.MONITORENUMPROC;
import com.sun.jna.platform.win32.WinUser.MONITORINFOEX;

/**
* A small demo that tests the Win32 monitor API.
* All available physical and virtual monitors are enumerated and
* their capabilities printed to stdout
* @author Martin Steiger
*/
public class MonitorInfoDemo
{
/**
* @param args (ignored)
*/
public static void main(String[] args)
{
System.out.println("Installed Physical Monitors: " + User32.INSTANCE.GetSystemMetrics(WinUser.SM_CMONITORS));

User32.INSTANCE.EnumDisplayMonitors(null, null, new MONITORENUMPROC() {

@Override
public int apply(HMONITOR hMonitor, HDC hdc, RECT rect, LPARAM lparam)
{
enumerate(hMonitor);

return 1;
}

}, new LPARAM(0));
}

static void enumerate(HMONITOR hMonitor)
{
System.out.println("Found HMONITOR: " + hMonitor.getPointer().toString());

MONITORINFOEX info = new MONITORINFOEX();
User32.INSTANCE.GetMonitorInfo(hMonitor, info);
System.out.println("Screen " + info.rcMonitor);
System.out.println("Work area " + info.rcWork);
boolean isPrimary = (info.dwFlags & WinUser.MONITORINFOF_PRIMARY) != 0;
System.out.println("Primary? " + (isPrimary ? "yes" : "no"));
System.out.println("Device " + new String(info.szDevice));

DWORDByReference pdwNumberOfPhysicalMonitors = new DWORDByReference();
Dxva2.INSTANCE.GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, pdwNumberOfPhysicalMonitors);
int monitorCount = pdwNumberOfPhysicalMonitors.getValue().intValue();

System.out.println("HMONITOR is linked to " + monitorCount + " physical monitors");

PHYSICAL_MONITOR[] physMons = new PHYSICAL_MONITOR[monitorCount];
Dxva2.INSTANCE.GetPhysicalMonitorsFromHMONITOR(hMonitor, monitorCount, physMons);

for (int i = 0; i < monitorCount; i++)
{
HANDLE hPhysicalMonitor = physMons[0].hPhysicalMonitor;
System.out.println("Monitor " + i + " - " + new String(physMons[i].szPhysicalMonitorDescription));

enumeratePhysicalMonitor(hPhysicalMonitor);
}
}

/**
* @param hPhysicalMonitor
*/
private static void enumeratePhysicalMonitor(HANDLE hPhysicalMonitor)
{
MC_DISPLAY_TECHNOLOGY_TYPE.ByReference techType = new MC_DISPLAY_TECHNOLOGY_TYPE.ByReference();
Dxva2.INSTANCE.GetMonitorTechnologyType(hPhysicalMonitor, techType);
System.out.println("TECHTYPE: " + techType.getValue());

DWORDByReference temps = new DWORDByReference();
DWORDByReference caps = new DWORDByReference();
Dxva2.INSTANCE.GetMonitorCapabilities(hPhysicalMonitor, caps, temps);
System.out.println("CAPS " + EnumUtils.setFromInteger(caps.getValue().intValue(), HighLevelMonitorConfigurationAPI.MC_CAPS.class));
System.out.println("Temps " + temps.getValue());

// Brightness
DWORDByReference pdwMinimumBrightness = new DWORDByReference();
DWORDByReference pdwCurrentBrightness = new DWORDByReference();
DWORDByReference pdwMaximumBrightness = new DWORDByReference();
Dxva2.INSTANCE.GetMonitorBrightness(hPhysicalMonitor, pdwMinimumBrightness, pdwCurrentBrightness, pdwMaximumBrightness);

System.out.println("Brightness Min: " + pdwMinimumBrightness.getValue());
System.out.println("Brightness Current: " + pdwCurrentBrightness.getValue());
System.out.println("Brightness Max: " + pdwMaximumBrightness.getValue());

// Contrast
DWORDByReference pdwMinimumContrast = new DWORDByReference();
DWORDByReference pdwCurrentContrast = new DWORDByReference();
DWORDByReference pdwMaximumContrast = new DWORDByReference();
Dxva2.INSTANCE.GetMonitorContrast(hPhysicalMonitor, pdwMinimumContrast, pdwCurrentContrast, pdwMaximumContrast);

System.out.println("Contrast Min: " + pdwMinimumContrast.getValue());
System.out.println("Contrast Current: " + pdwCurrentContrast.getValue());
System.out.println("Contrast Max: " + pdwMaximumContrast.getValue());

// Temperature
MC_COLOR_TEMPERATURE.ByReference pctCurrentColorTemperature = new MC_COLOR_TEMPERATURE.ByReference();
Dxva2.INSTANCE.GetMonitorColorTemperature(hPhysicalMonitor, pctCurrentColorTemperature);
System.out.println("Current Temp: " + pctCurrentColorTemperature.getValue());

// Capabilities string
DWORDByReference pdwCapabilitiesStringLengthInCharacters = new DWORDByReference();
Dxva2.INSTANCE.GetCapabilitiesStringLength(hPhysicalMonitor, pdwCapabilitiesStringLengthInCharacters);
DWORD capStrLen = pdwCapabilitiesStringLengthInCharacters.getValue();

LPSTR pszASCIICapabilitiesString = new LPSTR(new Memory(capStrLen.intValue()));
Dxva2.INSTANCE.CapabilitiesRequestAndCapabilitiesReply(hPhysicalMonitor, pszASCIICapabilitiesString, capStrLen);
System.out.println("Cap-String:" + new String(pszASCIICapabilitiesString.getPointer().getString(0)));

// Position
MC_POSITION_TYPE ptPositionType = MC_POSITION_TYPE.MC_HORIZONTAL_POSITION;
DWORDByReference pdwMinimumPosition = new DWORDByReference();
DWORDByReference pdwCurrentPosition = new DWORDByReference();
DWORDByReference pdwMaximumPosition = new DWORDByReference();
Dxva2.INSTANCE.GetMonitorDisplayAreaPosition(hPhysicalMonitor, ptPositionType, pdwMinimumPosition, pdwCurrentPosition, pdwMaximumPosition);

System.out.println("Position (horz) Min: " + pdwMinimumPosition.getValue());
System.out.println("Position (horz) Current: " + pdwCurrentPosition.getValue());
System.out.println("Position (horz) Max: " + pdwMaximumPosition.getValue());

// Size
MC_SIZE_TYPE ptSizeType = MC_SIZE_TYPE.MC_WIDTH;
DWORDByReference pdwMinimumSize = new DWORDByReference();
DWORDByReference pdwCurrentSize = new DWORDByReference();
DWORDByReference pdwMaximumSize = new DWORDByReference();
Dxva2.INSTANCE.GetMonitorDisplayAreaSize(hPhysicalMonitor, ptSizeType, pdwMinimumSize, pdwCurrentSize, pdwMaximumSize);

System.out.println("Width Min: " + pdwMinimumSize.getValue());
System.out.println("Width Current: " + pdwCurrentSize.getValue());
System.out.println("Width Max: " + pdwMaximumSize.getValue());

// Gain
MC_GAIN_TYPE ptGainType = MC_GAIN_TYPE.MC_RED_GAIN;
DWORDByReference pdwMinimumGain = new DWORDByReference();
DWORDByReference pdwCurrentGain = new DWORDByReference();
DWORDByReference pdwMaximumGain = new DWORDByReference();
Dxva2.INSTANCE.GetMonitorRedGreenOrBlueGain(hPhysicalMonitor, ptGainType, pdwMinimumGain, pdwCurrentGain, pdwMaximumGain);

System.out.println("Red Gain Min: " + pdwMinimumSize.getValue());
System.out.println("Red Gain Current: " + pdwCurrentSize.getValue());
System.out.println("Red Gain Max: " + pdwMaximumSize.getValue());

// Drive
MC_DRIVE_TYPE ptDriveType = MC_DRIVE_TYPE.MC_RED_DRIVE;
DWORDByReference pdwMinimumDrive = new DWORDByReference();
DWORDByReference pdwCurrentDrive = new DWORDByReference();
DWORDByReference pdwMaximumDrive = new DWORDByReference();
Dxva2.INSTANCE.GetMonitorRedGreenOrBlueDrive(hPhysicalMonitor, ptDriveType, pdwMinimumDrive, pdwCurrentDrive, pdwMaximumDrive);

System.out.println("Red Drive Min: " + pdwMinimumSize.getValue());
System.out.println("Red Drive Current: " + pdwCurrentSize.getValue());
System.out.println("Red Drive Max: " + pdwMaximumSize.getValue());

// Timing Report
MC_TIMING_REPORT pmtrMonitorTimingReport = new MC_TIMING_REPORT();
Dxva2.INSTANCE.GetTimingReport(hPhysicalMonitor, pmtrMonitorTimingReport);
System.out.println("HorizontalFrequencyInHZ " + pmtrMonitorTimingReport.dwHorizontalFrequencyInHZ);
System.out.println("VerticalFrequencyInHZ " + pmtrMonitorTimingReport.dwVerticalFrequencyInHZ);

System.out.println("--------------------------------------");
}

}
62 changes: 62 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/EnumConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2014 Martin Steiger
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.sun.jna.platform;

import com.sun.jna.FromNativeContext;
import com.sun.jna.ToNativeContext;
import com.sun.jna.TypeConverter;

/**
* A {@link TypeConverter} that maps an integer enum value to
* an actual Java enum.
* @param <T> the enum type
* @author Martin Steiger
*/
public class EnumConverter<T extends Enum<T>> implements TypeConverter {

private final Class<T> clazz;

/**
* @param clazz the enum class
*/
public EnumConverter(Class<T> clazz)
{
this.clazz = clazz;
}

@Override
public T fromNative(Object input, FromNativeContext context) {
Integer i = (Integer) input;

T[] vals = clazz.getEnumConstants();
return vals[i];
}

@Override
public Integer toNative(Object input, ToNativeContext context) {
T t = clazz.cast(input);

return Integer.valueOf(t.ordinal());
}

@Override
public Class<Integer> nativeType() {
return Integer.class;
}
}


105 changes: 105 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/EnumUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright 2014 Martin Steiger
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.sun.jna.platform;

import java.util.HashSet;
import java.util.Set;

import com.sun.jna.platform.win32.FlagEnum;

/**
* Several helper methods to convert integer flag (sets)
* into enum (sets)
* @author Martin Steiger
*/
public class EnumUtils
{
/**
* Uninitialized integer flag
*/
public static final int UNINITIALIZED = -1;

/**
* @param val the enum
* @return the index of the enum in the enum list
*/
public static <E extends Enum<E>> int toInteger(E val)
{
@SuppressWarnings("unchecked")
E[] vals = (E[]) val.getClass().getEnumConstants();

for (int idx = 0; idx < vals.length; idx++)
{
if (vals[idx] == val)
return idx;
}

throw new IllegalArgumentException();
}

/**
* @param idx the enum index
* @param clazz the enum class
* @return the enum at position idx
*/
public static <E extends Enum<E>> E fromInteger(int idx, Class<E> clazz)
{
if (idx == UNINITIALIZED)
return null;

E[] vals = clazz.getEnumConstants();
return vals[idx];
}

/**
* @param flags the ORed flags
* @param clazz the enum class
* @return the representing set
*/
public static <T extends FlagEnum> Set<T> setFromInteger(int flags, Class<T> clazz)
{
T[] vals = clazz.getEnumConstants();
Set<T> result = new HashSet<T>();

for (T val : vals)
{
if ((flags & val.getFlag()) != 0)
{
result.add(val);
}
}

return result;
}

/**
* @param set the set to convert
* @return the flags combined into an integer
*/
public static <T extends FlagEnum> int setToInteger(Set<T> set) {
int sum = 0;

for (T t : set)
{
sum |= t.getFlag();
}

return sum;
}
}


Loading