Difference between revisions of "Meta:Samples/Software"

From TSG Doc
Jump to navigation Jump to search
Line 34: Line 34:
 
===Template===
 
===Template===
  
<syntaxhighlight lang="python">
+
<syntaxhighlight lang="python" line>
 
import time
 
import time
 
from ctypes import windll, byref, c_char, Structure, WinError, POINTER, WINFUNCTYPE
 
from ctypes import windll, byref, c_char, Structure, WinError, POINTER, WINFUNCTYPE

Revision as of 12:02, 28 April 2015

Introduction.


Features

This software is made to do this, it has these unique selling points:

  • Feature 1[1]
  • Feature 2
  • Feature 3

Requirements

  • Microsoft Windows XP or later.
  • Intel Core 2 Duo E8600 or higher.
  • Python


Installation

Windows 7 64-bit

  1. Step 1
  2. Step 2
  3. Step 3


Configuration

General settings.


Usage

Templates and how-to's go here.


Template

  1import time
  2from ctypes import windll, byref, c_char, Structure, WinError, POINTER, WINFUNCTYPE
  3from ctypes.wintypes import BOOL, HMONITOR, HDC, RECT, LPARAM, DWORD, BYTE, WCHAR, HANDLE
  4
  5
  6_MONITORENUMPROC = WINFUNCTYPE(BOOL, HMONITOR, HDC, POINTER(RECT), LPARAM)
  7
  8class _PHYSICAL_MONITOR(Structure):
  9	_fields_ = [('handle', HANDLE), ('description', WCHAR * 128)]
 10
 11
 12def _iter_physical_monitors(close_handles=True):
 13	"""Iterates physical monitors.
 14
 15	The handles are closed automatically whenever the iterator is advanced.
 16	This means that the iterator should always be fully exhausted!
 17
 18	If you want to keep handles e.g. because you need to store all of them and
 19	use them later, set `close_handles` to False and close them manually."""
 20
 21	def callback(hmonitor, hdc, lprect, lparam):
 22		monitors.append(HMONITOR(hmonitor))
 23		return True
 24
 25	monitors = []
 26	if not windll.user32.EnumDisplayMonitors(None, None, _MONITORENUMPROC(callback), None):
 27		raise WinError('EnumDisplayMonitors failed')
 28
 29	for monitor in monitors:
 30		# Get physical monitor count
 31		count = DWORD()
 32		if not windll.dxva2.GetNumberOfPhysicalMonitorsFromHMONITOR(monitor, byref(count)):
 33			raise WinError()
 34		# Get physical monitor handles
 35		physical_array = (_PHYSICAL_MONITOR * count.value)()
 36		if not windll.dxva2.GetPhysicalMonitorsFromHMONITOR(monitor, count.value, physical_array):
 37			raise WinError()
 38		
 39		for physical in physical_array:
 40			handle = physical.handle
 41
 42			# Get physical monitor capabilities. This may take a while...
 43			length = DWORD()
 44			if not windll.dxva2.GetCapabilitiesStringLength(HANDLE(handle), byref(length)):
 45				raise WinError()
 46			capabilities_string = (c_char * length.value)()
 47			if not windll.dxva2.CapabilitiesRequestAndCapabilitiesReply(HANDLE(handle), capabilities_string, length):
 48				raise WinError()
 49			raw_capabilities = capabilities_string.value.decode('ascii')				
 50			capabilities = _parse_capabilities_string(raw_capabilities)			
 51			if capabilities:
 52				# We only care about the model info for now.
 53				yield [capabilities['model'], handle]
 54			if close_handles:
 55				if not windll.dxva2.DestroyPhysicalMonitor(handle):
 56					raise WinError()
 57		
 58
 59def _parse_capabilities_string(capabilities_string):
 60		level = 0
 61		capabilities = {}
 62		open_p = {}
 63		close_p = {0: 0}
 64		id = {}
 65		for i, chr in enumerate(capabilities_string):
 66			if chr == '(':
 67				if i == 0:
 68					close_p[0] = 1
 69					continue
 70				open_p[level] = i
 71				if level == 0:
 72					id[0] = capabilities_string[close_p[0] + 1:i]
 73				level += 1
 74			elif chr == ')':
 75				level -= 1
 76				close_p[level] = i
 77				if level == 0:
 78					values = capabilities_string[open_p[0] + 1:i]
 79					# We only care about the model info for now.
 80					if id[0] == 'model':
 81						capabilities[id[0]] = values
 82		return capabilities
 83
 84
 85def set_vcp_feature(monitor, code, value):
 86	"""Sends a DDC command to the specified monitor.
 87
 88	See this link for a list of commands:
 89	ftp://ftp.cis.nctu.edu.tw/pub/csie/Software/X11/private/VeSaSpEcS/VESA_Document_Center_Monitor_Interface/mccsV3.pdf
 90	"""
 91	if not windll.dxva2.SetVCPFeature(HANDLE(monitor), BYTE(code), DWORD(value)):
 92		raise WinError()
 93
 94
 95for model, handle in _iter_physical_monitors():
 96	if model == "XL2420Z":
 97		set_vcp_feature(handle, 0xDC, 12) # picture mode
 98		
 99		time.sleep(2) # wait for picture mode to load
100		
101		set_vcp_feature(handle, 0x10, 31) # brightness
102		set_vcp_feature(handle, 0x12, 50) # contrast
103		set_vcp_feature(handle, 0xF0, 0) # AMA (overdrive)

See Also


References

  1. Some reference