Skip to content

Commit bf870bb

Browse files
msteigerdblock
authored andcommitted
Added Win32 Monitor Configuration API in com.sun.jna.platform.win32.Dxva2.
1 parent 21d459b commit bf870bb

13 files changed

Lines changed: 2063 additions & 3 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Features
99
--------
1010
* Updated AIX natives and build - [@twall](https://github.com/twall).
1111
* [#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).
12+
* [#332](https://github.com/twall/jna/pull/332): Added Win32 Monitor Configuration API in `com.sun.jna.platform.win32.Dxva2` - [@msteiger](https://github.com/msteiger).
1213
* Added Winspool monitor sample and updated Kernel32, WinBase, Winspool - [@wolftobias](https://github.com/wolftobias).
1314
* Added Some minor changes to MS Office samples Test and small changes to the MS Office samples Bug Fixes - [@wolftobias](https://github.com/wolftobias).
1415
* [#333](https://github.com/twall/jna/pull/333): Added `CoTaskMemAlloc`, `CoTaskMemRealloc` and `CoTaskMemFree` to `com.sun.jna.platform.win32.Ole32` - [@msteiger](https://github.com/msteiger).
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
/*
2+
* Copyright 2014 Martin Steiger
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.sun.jna.contrib.demo;
18+
19+
import com.sun.jna.Memory;
20+
import com.sun.jna.platform.EnumUtils;
21+
import com.sun.jna.platform.win32.Dxva2;
22+
import com.sun.jna.platform.win32.HighLevelMonitorConfigurationAPI;
23+
import com.sun.jna.platform.win32.HighLevelMonitorConfigurationAPI.MC_COLOR_TEMPERATURE;
24+
import com.sun.jna.platform.win32.HighLevelMonitorConfigurationAPI.MC_DISPLAY_TECHNOLOGY_TYPE;
25+
import com.sun.jna.platform.win32.HighLevelMonitorConfigurationAPI.MC_DRIVE_TYPE;
26+
import com.sun.jna.platform.win32.HighLevelMonitorConfigurationAPI.MC_GAIN_TYPE;
27+
import com.sun.jna.platform.win32.HighLevelMonitorConfigurationAPI.MC_POSITION_TYPE;
28+
import com.sun.jna.platform.win32.HighLevelMonitorConfigurationAPI.MC_SIZE_TYPE;
29+
import com.sun.jna.platform.win32.LowLevelMonitorConfigurationAPI.MC_TIMING_REPORT;
30+
import com.sun.jna.platform.win32.PhysicalMonitorEnumerationAPI.PHYSICAL_MONITOR;
31+
import com.sun.jna.platform.win32.User32;
32+
import com.sun.jna.platform.win32.WTypes.LPSTR;
33+
import com.sun.jna.platform.win32.WinDef.DWORD;
34+
import com.sun.jna.platform.win32.WinDef.DWORDByReference;
35+
import com.sun.jna.platform.win32.WinDef.HDC;
36+
import com.sun.jna.platform.win32.WinDef.LPARAM;
37+
import com.sun.jna.platform.win32.WinDef.RECT;
38+
import com.sun.jna.platform.win32.WinNT.HANDLE;
39+
import com.sun.jna.platform.win32.WinUser;
40+
import com.sun.jna.platform.win32.WinUser.HMONITOR;
41+
import com.sun.jna.platform.win32.WinUser.MONITORENUMPROC;
42+
import com.sun.jna.platform.win32.WinUser.MONITORINFOEX;
43+
44+
/**
45+
* A small demo that tests the Win32 monitor API.
46+
* All available physical and virtual monitors are enumerated and
47+
* their capabilities printed to stdout
48+
* @author Martin Steiger
49+
*/
50+
public class MonitorInfoDemo
51+
{
52+
/**
53+
* @param args (ignored)
54+
*/
55+
public static void main(String[] args)
56+
{
57+
System.out.println("Installed Physical Monitors: " + User32.INSTANCE.GetSystemMetrics(WinUser.SM_CMONITORS));
58+
59+
User32.INSTANCE.EnumDisplayMonitors(null, null, new MONITORENUMPROC() {
60+
61+
@Override
62+
public int apply(HMONITOR hMonitor, HDC hdc, RECT rect, LPARAM lparam)
63+
{
64+
enumerate(hMonitor);
65+
66+
return 1;
67+
}
68+
69+
}, new LPARAM(0));
70+
}
71+
72+
static void enumerate(HMONITOR hMonitor)
73+
{
74+
System.out.println("Found HMONITOR: " + hMonitor.getPointer().toString());
75+
76+
MONITORINFOEX info = new MONITORINFOEX();
77+
User32.INSTANCE.GetMonitorInfo(hMonitor, info);
78+
System.out.println("Screen " + info.rcMonitor);
79+
System.out.println("Work area " + info.rcWork);
80+
boolean isPrimary = (info.dwFlags & WinUser.MONITORINFOF_PRIMARY) != 0;
81+
System.out.println("Primary? " + (isPrimary ? "yes" : "no"));
82+
System.out.println("Device " + new String(info.szDevice));
83+
84+
DWORDByReference pdwNumberOfPhysicalMonitors = new DWORDByReference();
85+
Dxva2.INSTANCE.GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, pdwNumberOfPhysicalMonitors);
86+
int monitorCount = pdwNumberOfPhysicalMonitors.getValue().intValue();
87+
88+
System.out.println("HMONITOR is linked to " + monitorCount + " physical monitors");
89+
90+
PHYSICAL_MONITOR[] physMons = new PHYSICAL_MONITOR[monitorCount];
91+
Dxva2.INSTANCE.GetPhysicalMonitorsFromHMONITOR(hMonitor, monitorCount, physMons);
92+
93+
for (int i = 0; i < monitorCount; i++)
94+
{
95+
HANDLE hPhysicalMonitor = physMons[0].hPhysicalMonitor;
96+
System.out.println("Monitor " + i + " - " + new String(physMons[i].szPhysicalMonitorDescription));
97+
98+
enumeratePhysicalMonitor(hPhysicalMonitor);
99+
}
100+
101+
Dxva2.INSTANCE.DestroyPhysicalMonitors(monitorCount, physMons);
102+
}
103+
104+
/**
105+
* @param hPhysicalMonitor
106+
*/
107+
private static void enumeratePhysicalMonitor(HANDLE hPhysicalMonitor)
108+
{
109+
MC_DISPLAY_TECHNOLOGY_TYPE.ByReference techType = new MC_DISPLAY_TECHNOLOGY_TYPE.ByReference();
110+
Dxva2.INSTANCE.GetMonitorTechnologyType(hPhysicalMonitor, techType);
111+
System.out.println("TECHTYPE: " + techType.getValue());
112+
113+
DWORDByReference temps = new DWORDByReference();
114+
DWORDByReference caps = new DWORDByReference();
115+
Dxva2.INSTANCE.GetMonitorCapabilities(hPhysicalMonitor, caps, temps);
116+
System.out.println("CAPS " + EnumUtils.setFromInteger(caps.getValue().intValue(), HighLevelMonitorConfigurationAPI.MC_CAPS.class));
117+
System.out.println("Temps " + temps.getValue());
118+
119+
// Brightness
120+
DWORDByReference pdwMinimumBrightness = new DWORDByReference();
121+
DWORDByReference pdwCurrentBrightness = new DWORDByReference();
122+
DWORDByReference pdwMaximumBrightness = new DWORDByReference();
123+
Dxva2.INSTANCE.GetMonitorBrightness(hPhysicalMonitor, pdwMinimumBrightness, pdwCurrentBrightness, pdwMaximumBrightness);
124+
125+
System.out.println("Brightness Min: " + pdwMinimumBrightness.getValue());
126+
System.out.println("Brightness Current: " + pdwCurrentBrightness.getValue());
127+
System.out.println("Brightness Max: " + pdwMaximumBrightness.getValue());
128+
129+
// Contrast
130+
DWORDByReference pdwMinimumContrast = new DWORDByReference();
131+
DWORDByReference pdwCurrentContrast = new DWORDByReference();
132+
DWORDByReference pdwMaximumContrast = new DWORDByReference();
133+
Dxva2.INSTANCE.GetMonitorContrast(hPhysicalMonitor, pdwMinimumContrast, pdwCurrentContrast, pdwMaximumContrast);
134+
135+
System.out.println("Contrast Min: " + pdwMinimumContrast.getValue());
136+
System.out.println("Contrast Current: " + pdwCurrentContrast.getValue());
137+
System.out.println("Contrast Max: " + pdwMaximumContrast.getValue());
138+
139+
// Temperature
140+
MC_COLOR_TEMPERATURE.ByReference pctCurrentColorTemperature = new MC_COLOR_TEMPERATURE.ByReference();
141+
Dxva2.INSTANCE.GetMonitorColorTemperature(hPhysicalMonitor, pctCurrentColorTemperature);
142+
System.out.println("Current Temp: " + pctCurrentColorTemperature.getValue());
143+
144+
// Capabilities string
145+
DWORDByReference pdwCapabilitiesStringLengthInCharacters = new DWORDByReference();
146+
Dxva2.INSTANCE.GetCapabilitiesStringLength(hPhysicalMonitor, pdwCapabilitiesStringLengthInCharacters);
147+
DWORD capStrLen = pdwCapabilitiesStringLengthInCharacters.getValue();
148+
149+
LPSTR pszASCIICapabilitiesString = new LPSTR(new Memory(capStrLen.intValue()));
150+
Dxva2.INSTANCE.CapabilitiesRequestAndCapabilitiesReply(hPhysicalMonitor, pszASCIICapabilitiesString, capStrLen);
151+
System.out.println("Cap-String:" + new String(pszASCIICapabilitiesString.getPointer().getString(0)));
152+
153+
// Position
154+
MC_POSITION_TYPE ptPositionType = MC_POSITION_TYPE.MC_HORIZONTAL_POSITION;
155+
DWORDByReference pdwMinimumPosition = new DWORDByReference();
156+
DWORDByReference pdwCurrentPosition = new DWORDByReference();
157+
DWORDByReference pdwMaximumPosition = new DWORDByReference();
158+
Dxva2.INSTANCE.GetMonitorDisplayAreaPosition(hPhysicalMonitor, ptPositionType, pdwMinimumPosition, pdwCurrentPosition, pdwMaximumPosition);
159+
160+
System.out.println("Position (horz) Min: " + pdwMinimumPosition.getValue());
161+
System.out.println("Position (horz) Current: " + pdwCurrentPosition.getValue());
162+
System.out.println("Position (horz) Max: " + pdwMaximumPosition.getValue());
163+
164+
// Size
165+
MC_SIZE_TYPE ptSizeType = MC_SIZE_TYPE.MC_WIDTH;
166+
DWORDByReference pdwMinimumSize = new DWORDByReference();
167+
DWORDByReference pdwCurrentSize = new DWORDByReference();
168+
DWORDByReference pdwMaximumSize = new DWORDByReference();
169+
Dxva2.INSTANCE.GetMonitorDisplayAreaSize(hPhysicalMonitor, ptSizeType, pdwMinimumSize, pdwCurrentSize, pdwMaximumSize);
170+
171+
System.out.println("Width Min: " + pdwMinimumSize.getValue());
172+
System.out.println("Width Current: " + pdwCurrentSize.getValue());
173+
System.out.println("Width Max: " + pdwMaximumSize.getValue());
174+
175+
// Gain
176+
MC_GAIN_TYPE ptGainType = MC_GAIN_TYPE.MC_RED_GAIN;
177+
DWORDByReference pdwMinimumGain = new DWORDByReference();
178+
DWORDByReference pdwCurrentGain = new DWORDByReference();
179+
DWORDByReference pdwMaximumGain = new DWORDByReference();
180+
Dxva2.INSTANCE.GetMonitorRedGreenOrBlueGain(hPhysicalMonitor, ptGainType, pdwMinimumGain, pdwCurrentGain, pdwMaximumGain);
181+
182+
System.out.println("Red Gain Min: " + pdwMinimumSize.getValue());
183+
System.out.println("Red Gain Current: " + pdwCurrentSize.getValue());
184+
System.out.println("Red Gain Max: " + pdwMaximumSize.getValue());
185+
186+
// Drive
187+
MC_DRIVE_TYPE ptDriveType = MC_DRIVE_TYPE.MC_RED_DRIVE;
188+
DWORDByReference pdwMinimumDrive = new DWORDByReference();
189+
DWORDByReference pdwCurrentDrive = new DWORDByReference();
190+
DWORDByReference pdwMaximumDrive = new DWORDByReference();
191+
Dxva2.INSTANCE.GetMonitorRedGreenOrBlueDrive(hPhysicalMonitor, ptDriveType, pdwMinimumDrive, pdwCurrentDrive, pdwMaximumDrive);
192+
193+
System.out.println("Red Drive Min: " + pdwMinimumSize.getValue());
194+
System.out.println("Red Drive Current: " + pdwCurrentSize.getValue());
195+
System.out.println("Red Drive Max: " + pdwMaximumSize.getValue());
196+
197+
// Timing Report
198+
MC_TIMING_REPORT pmtrMonitorTimingReport = new MC_TIMING_REPORT();
199+
Dxva2.INSTANCE.GetTimingReport(hPhysicalMonitor, pmtrMonitorTimingReport);
200+
System.out.println("HorizontalFrequencyInHZ " + pmtrMonitorTimingReport.dwHorizontalFrequencyInHZ);
201+
System.out.println("VerticalFrequencyInHZ " + pmtrMonitorTimingReport.dwVerticalFrequencyInHZ);
202+
203+
System.out.println("--------------------------------------");
204+
}
205+
206+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2014 Martin Steiger
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.sun.jna.platform;
18+
19+
import com.sun.jna.FromNativeContext;
20+
import com.sun.jna.ToNativeContext;
21+
import com.sun.jna.TypeConverter;
22+
23+
/**
24+
* A {@link TypeConverter} that maps an integer enum value to
25+
* an actual Java enum.
26+
* @param <T> the enum type
27+
* @author Martin Steiger
28+
*/
29+
public class EnumConverter<T extends Enum<T>> implements TypeConverter {
30+
31+
private final Class<T> clazz;
32+
33+
/**
34+
* @param clazz the enum class
35+
*/
36+
public EnumConverter(Class<T> clazz)
37+
{
38+
this.clazz = clazz;
39+
}
40+
41+
@Override
42+
public T fromNative(Object input, FromNativeContext context) {
43+
Integer i = (Integer) input;
44+
45+
T[] vals = clazz.getEnumConstants();
46+
return vals[i];
47+
}
48+
49+
@Override
50+
public Integer toNative(Object input, ToNativeContext context) {
51+
T t = clazz.cast(input);
52+
53+
return Integer.valueOf(t.ordinal());
54+
}
55+
56+
@Override
57+
public Class<Integer> nativeType() {
58+
return Integer.class;
59+
}
60+
}
61+
62+
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2014 Martin Steiger
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.sun.jna.platform;
18+
19+
import java.util.HashSet;
20+
import java.util.Set;
21+
22+
import com.sun.jna.platform.win32.FlagEnum;
23+
24+
/**
25+
* Several helper methods to convert integer flag (sets)
26+
* into enum (sets)
27+
* @author Martin Steiger
28+
*/
29+
public class EnumUtils
30+
{
31+
/**
32+
* Uninitialized integer flag
33+
*/
34+
public static final int UNINITIALIZED = -1;
35+
36+
/**
37+
* @param val the enum
38+
* @return the index of the enum in the enum list
39+
*/
40+
public static <E extends Enum<E>> int toInteger(E val)
41+
{
42+
@SuppressWarnings("unchecked")
43+
E[] vals = (E[]) val.getClass().getEnumConstants();
44+
45+
for (int idx = 0; idx < vals.length; idx++)
46+
{
47+
if (vals[idx] == val)
48+
return idx;
49+
}
50+
51+
throw new IllegalArgumentException();
52+
}
53+
54+
/**
55+
* @param idx the enum index
56+
* @param clazz the enum class
57+
* @return the enum at position idx
58+
*/
59+
public static <E extends Enum<E>> E fromInteger(int idx, Class<E> clazz)
60+
{
61+
if (idx == UNINITIALIZED)
62+
return null;
63+
64+
E[] vals = clazz.getEnumConstants();
65+
return vals[idx];
66+
}
67+
68+
/**
69+
* @param flags the ORed flags
70+
* @param clazz the enum class
71+
* @return the representing set
72+
*/
73+
public static <T extends FlagEnum> Set<T> setFromInteger(int flags, Class<T> clazz)
74+
{
75+
T[] vals = clazz.getEnumConstants();
76+
Set<T> result = new HashSet<T>();
77+
78+
for (T val : vals)
79+
{
80+
if ((flags & val.getFlag()) != 0)
81+
{
82+
result.add(val);
83+
}
84+
}
85+
86+
return result;
87+
}
88+
89+
/**
90+
* @param set the set to convert
91+
* @return the flags combined into an integer
92+
*/
93+
public static <T extends FlagEnum> int setToInteger(Set<T> set) {
94+
int sum = 0;
95+
96+
for (T t : set)
97+
{
98+
sum |= t.getFlag();
99+
}
100+
101+
return sum;
102+
}
103+
}
104+
105+

0 commit comments

Comments
 (0)