Jump to content
Sign in to follow this  
jasc2v8

Input Director AutoDim

Recommended Posts

jasc2v8

InputDirector.com is a free/donation-ware Windows application that lets you control multiple Windows systems using the keyboard/mouse attached to one computer. I have no relation to the author, but I do use this daily.

Input Director AutoDimRuns at Startup and dims the Master monitor when switching to a Slave and vice versa.

AutoIT Functions Author: Ascend4nt

Func _GetDeviceGammaRampBaseRGB()

Func _GetDeviceGammaRampBaseRGB()

Tested on Windows 7 Home Premium 64-bit

Tested on Windows 7 Enterprise 64-bit

Freeware 64-bit Installer download :

https://docs.google.com/file/d/0B1fM42un8XH9MmNXX09SMVA5VlE/edit

#include 
#include 
#include 
#include 

; Install a catch-all error handler
$oMyError = ObjEvent("AutoIt.Error","Terminate")

; Set escape to debug, comment out for final executable
;HotKeySet("{ESC}", "Terminate") ; a way out of this

; Exit if InputDirector is not running
If Not ProcessExists("InputDirector.exe") Then Terminate()

; Save the rgb setting on entry so we can reset on exit
$aSavedRGB = _GetDeviceGammaRampBaseRGB()

While True

; If InputDirector is running then autodim else do nothing
If ProcessExists("InputDirector.exe") Then
_AutoDim()
EndIf

WEnd

Func _AutoDim()

; Set polling rate
Sleep(100)

; Get mouse position, return if error
$pos = MouseGetPos()
If not IsArray($pos) then return

; If the cursor is centered then
; the focus is most likely on another monitor so dim this one
; else undim
If $pos[0] = @DesktopWidth/2 And $pos[1] = @DesktopHeight/2 Then
_SetDeviceGammaRamp ($aSavedRGB[0]/2, $aSavedRGB[1]/2, $aSavedRGB[2]/2)
Else
_SetDeviceGammaRamp ($aSavedRGB[0], $aSavedRGB[1], $aSavedRGB[2])
EndIf
EndFunc

; -----------------------------------------------------------------------------------------------------
; Func _GetDeviceGammaRampBaseRGB()
;
; Function to get the 1st RGB values of the Gamma Ramp. If no other processes have modified the ramp,
; these 3 should be equal, and the rest of the ramp hopefully corresponds to what _SetDeviceGammaRamp()
; sets the values to.
;
; Return: a 3-element array representing the base RGB values of the Gamma Ramp
; $aArray[0] = Red
; $aArray[1] = Green
; $aArray[2] = Blue
;
; Author: Ascend4nt
; -----------------------------------------------------------------------------------------------------
Func _GetDeviceGammaRampBaseRGB()
Local $n_ramp, $i, $hDC, $aRet, $iErr
Local $aBaseRGB[3] = [128, 128, 128]
$hDC = _WinAPI_GetDC(0)
If ($hDC = 0) Then Return SetError(2,@error,$aBaseRGB)

$n_ramp = DllStructCreate("short[" & (256 * 3) & "]")
$aRet = DllCall("gdi32.dll", "bool", "GetDeviceGammaRamp", "handle", $hDC, "ptr", DllStructGetPtr($n_ramp))
$iErr = @error
_WinAPI_ReleaseDC(0, $hDC)
If $iErr Or Not $aRet[0] Then Return SetError(2,$iErr,$aBaseRGB)
; 1st element should be 0, so second element of each ramp should have the value we need for the base
$aBaseRGB[0] = DllStructGetData($n_ramp, 1, 1+1) - 128 ; Red
$aBaseRGB[1] = DllStructGetData($n_ramp, 1, 1+1+256) - 128 ; Green
$aBaseRGB[2] = DllStructGetData($n_ramp, 1, 1+1+512) - 128 ; Blue

If $aBaseRGB[0] > 255 Then $aBaseRGB[0] = 255
If $aBaseRGB[1] > 255 Then $aBaseRGB[1] = 255
If $aBaseRGB[2] > 255 Then $aBaseRGB[2] = 255

Return $aBaseRGB
EndFunc ;==>_GetDeviceGammaRampBaseRGB

; -----------------------------------------------------------------------------------------------------
; Func _SetDeviceGammaRamp($vRed = 128, $vGreen = 128, $vBlue = 128)
;
; Sets GammaRamps for Red, Green, and Blue. If all 3 inputs are equal, the net effect
; is that the brightness is adjusted.
;
; $vRed = value from 0 - 255
; $vGreen = value from 0 - 255
; $vBlue = value from 0 - 255
;
; Original AutoIt version (before fixes) appears to be at:
; http://autoit.de./index.php?page=Thread&postID=156967
;
; -----------------------------------------------------------------------------------------------------
Func _SetDeviceGammaRamp($vRed = 128, $vGreen = 128, $vBlue = 128)
Local $n_ramp, $rVar, $gVar, $bVar, $aRet, $i, $hDC, $iErr

If ($vRed < 0 Or $vRed > 255) Or _
($vGreen < 0 Or $vGreen > 255) Or _
($vBlue < 0 Or $vBlue > 255) Then
Return SetError(1,0,-1) ; Invalid value for one of the colors
EndIf

$n_ramp = DllStructCreate("short[" & (256 * 3) & "]")

For $i = 0 To 255
$rVar = $i * ($vRed + 128)
If $rVar > 65535 Then $rVar = 65535
$gVar = $i * ($vGreen + 128)
If $gVar > 65535 Then $gVar = 65535
$bVar = $i * ($vBlue + 128)
If $bVar > 65535 Then $bVar = 65535
; +1 to account for 1-based index in a 0-255 based loop
DllStructSetData($n_ramp, 1, Int($rVar), $i+1) ;red
DllStructSetData($n_ramp, 1, Int($gVar), $i+1 + 256) ;green
DllStructSetData($n_ramp, 1, Int($bVar), $i+1 + 512) ;blue
Next

$hDC = _WinAPI_GetDC(0)
If ($hDC = 0) Then Return SetError(-1,@error,-1)

$aRet = DllCall("gdi32.dll", "bool", "SetDeviceGammaRamp", "handle", $hDC, "ptr", DllStructGetPtr($n_ramp))
$iErr = @error
_WinAPI_ReleaseDC(0, $hDC)

If $iErr Or Not $aRet[0] Then Return SetError(-1,$iErr,-1)

Return 0
EndFunc ;==>_SetDeviceGammaRamp

Func Terminate()
_SetDeviceGammaRamp($aSavedRGB[0], $aSavedRGB[1], $aSavedRGB[2])
Exit 0
EndFunc ;==>Terminate

Share this post


Link to post
Share on other sites
Noddle

Hi,

looks like you missed a #include files,

and for me, $aSavedRGB needed to be defined,

and you did not save the RGB values higher up in your code.

I ran this on y laptop, without input-director, and it made my screen dark

so i movd "$aSavedRGB = _GetDeviceGammaRampBaseRGB()" higer up in the code

NIgel

#include <WinAPI.au3>
dim $aSavedRGB[3]

; Install a catch-all error handler
$oMyError = ObjEvent("AutoIt.Error", "Terminate")

; Set escape to debug, comment out for final executable
;HotKeySet("{ESC}", "Terminate") ; a way out of this

; Save the rgb setting on entry so we can reset on exit
$aSavedRGB = _GetDeviceGammaRampBaseRGB()

; Exit if InputDirector is not running
If Not ProcessExists("InputDirector.exe") Then Terminate()

While True

; If InputDirector is running then autodim else do nothing
If ProcessExists("InputDirector.exe") Then
_AutoDim()
EndIf

WEnd

Exit

Func _AutoDim()

; Set polling rate
Sleep(100)

; Get mouse position, return if error
$pos = MouseGetPos()
If Not IsArray($pos) Then Return

; If the cursor is centered then
; the focus is most likely on another monitor so dim this one
; else undim
If $pos[0] = @DesktopWidth / 2 And $pos[1] = @DesktopHeight / 2 Then
_SetDeviceGammaRamp($aSavedRGB[0] / 2, $aSavedRGB[1] / 2, $aSavedRGB[2] / 2)
Else
_SetDeviceGammaRamp($aSavedRGB[0], $aSavedRGB[1], $aSavedRGB[2])
EndIf
EndFunc ;==>_AutoDim

; -----------------------------------------------------------------------------------------------------
; Func _GetDeviceGammaRampBaseRGB()
;
; Function to get the 1st RGB values of the Gamma Ramp. If no other processes have modified the ramp,
; these 3 should be equal, and the rest of the ramp hopefully corresponds to what _SetDeviceGammaRamp()
; sets the values to.
;
; Return: a 3-element array representing the base RGB values of the Gamma Ramp
; $aArray[0] = Red
; $aArray[1] = Green
; $aArray[2] = Blue
;
; Author: Ascend4nt
; -----------------------------------------------------------------------------------------------------
Func _GetDeviceGammaRampBaseRGB()
Local $n_ramp, $i, $hDC, $aRet, $iErr
Local $aBaseRGB[3] = [128, 128, 128]
$hDC = _WinAPI_GetDC(0)
If ($hDC = 0) Then Return SetError(2, @error, $aBaseRGB)

$n_ramp = DllStructCreate("short[" & (256 * 3) & "]")
$aRet = DllCall("gdi32.dll", "bool", "GetDeviceGammaRamp", "handle", $hDC, "ptr", DllStructGetPtr($n_ramp))
$iErr = @error
_WinAPI_ReleaseDC(0, $hDC)
If $iErr Or Not $aRet[0] Then Return SetError(2, $iErr, $aBaseRGB)
; 1st element should be 0, so second element of each ramp should have the value we need for the base
$aBaseRGB[0] = DllStructGetData($n_ramp, 1, 1 + 1) - 128 ; Red
$aBaseRGB[1] = DllStructGetData($n_ramp, 1, 1 + 1 + 256) - 128 ; Green
$aBaseRGB[2] = DllStructGetData($n_ramp, 1, 1 + 1 + 512) - 128 ; Blue

If $aBaseRGB[0] > 255 Then $aBaseRGB[0] = 255
If $aBaseRGB[1] > 255 Then $aBaseRGB[1] = 255
If $aBaseRGB[2] > 255 Then $aBaseRGB[2] = 255

Return $aBaseRGB
EndFunc ;==>_GetDeviceGammaRampBaseRGB

; -----------------------------------------------------------------------------------------------------
; Func _SetDeviceGammaRamp($vRed = 128, $vGreen = 128, $vBlue = 128)
;
; Sets GammaRamps for Red, Green, and Blue. If all 3 inputs are equal, the net effect
; is that the brightness is adjusted.
;
; $vRed = value from 0 - 255
; $vGreen = value from 0 - 255
; $vBlue = value from 0 - 255
;
; Original AutoIt version (before fixes) appears to be at:
; http://autoit.de./index.php?page=Thread&postID=156967
;
; -----------------------------------------------------------------------------------------------------
Func _SetDeviceGammaRamp($vRed = 128, $vGreen = 128, $vBlue = 128)
Local $n_ramp, $rVar, $gVar, $bVar, $aRet, $i, $hDC, $iErr

If ($vRed < 0 Or $vRed > 255) Or _
($vGreen < 0 Or $vGreen > 255) Or _
($vBlue < 0 Or $vBlue > 255) Then
Return SetError(1, 0, -1) ; Invalid value for one of the colors
EndIf

$n_ramp = DllStructCreate("short[" & (256 * 3) & "]")

For $i = 0 To 255
$rVar = $i * ($vRed + 128)
If $rVar > 65535 Then $rVar = 65535
$gVar = $i * ($vGreen + 128)
If $gVar > 65535 Then $gVar = 65535
$bVar = $i * ($vBlue + 128)
If $bVar > 65535 Then $bVar = 65535
; +1 to account for 1-based index in a 0-255 based loop
DllStructSetData($n_ramp, 1, Int($rVar), $i + 1) ;red
DllStructSetData($n_ramp, 1, Int($gVar), $i + 1 + 256) ;green
DllStructSetData($n_ramp, 1, Int($bVar), $i + 1 + 512) ;blue
Next

$hDC = _WinAPI_GetDC(0)
If ($hDC = 0) Then Return SetError(-1, @error, -1)

$aRet = DllCall("gdi32.dll", "bool", "SetDeviceGammaRamp", "handle", $hDC, "ptr", DllStructGetPtr($n_ramp))
$iErr = @error
_WinAPI_ReleaseDC(0, $hDC)

If $iErr Or Not $aRet[0] Then Return SetError(-1, $iErr, -1)

Return 0
EndFunc ;==>_SetDeviceGammaRamp

Func Terminate()
_SetDeviceGammaRamp($aSavedRGB[0], $aSavedRGB[1], $aSavedRGB[2])
Exit 0
EndFunc ;==>Terminate

Nigel

Edited by Noddle

Share this post


Link to post
Share on other sites
jasc2v8

Thank you, Nigel!

I very much appreciate your constructive feedback, and corrections!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×